copying save functionality from mapgen.js