Page 1 of 1

[Solved]Create a Fixed Height Map from a Simple Image

PostPosted: Wed Nov 06, 2019 00:21
by sirrobzeroone
I'm working on a simple and small game were I don't want the map to be dynamically generated from random perlin noise. In fact I already have the map mapped out in 2800x2800 greyscale image. I'm going to use a scale of 1 pixel to 1 voxel/meter.

Essentially I just wish to use this as a fixed height map data for the game so overriding all the generic biome etc generation.

I've had a look at but this seems to go a step to far for my needs and is limited to gisTIFF, plus Im having a shocker getting python to run with the required libraries on win10. im way out of my depth and I cant seem to find a simple tool that will read the brightness value of each pixel and spit out the result into some form of "xy:brightness" value - understand thats 7.8 million odd values but I only need to do this once.

For example I found this on stackoverflow using lua and its very close to what I need to do, although expect it'll take me a few weeks to make it work -

If I can get that data in some sort of format, I was then going to:
~ Delete any xy coord were brightness is == 1 height - this removes about half my data points, I'll just have a match check and if its nil assume height == 1, I may even be able to set this to alpha on initial run in and save me a step.

~ Feed the left over points into a single large table in lua or if I need split it into a few tables if performance is to degraded.

~ use that as the basic height map, and build the basic map gen elements on from there (I have a very small 20x20 data pt version running via map gen already)

~ generate the whole map once using the minetest engine

~ save all the sqlite, map meta et al data inside my game directory somewhere.

~ When someone first loads the game have lua check if the above files exist in the world folder if they don't copy them over

~ For every new "world" created for the game the player will effectively start on the exact same world map and a fixed pt.

If those last two points cant be done I'll just have to run the map gen each game creation but I figured copying files around is way quicker than running the mapgen code each time unessecarly.

I did play around with worldedit/mapedit, but Im not keen to manually set 7.6 million data pts by hand when I'm pretty sure the computer can do this for me off a nice image.

Anyways before I go through the process of the above I just wanted to check theirs:

~ Not a simpler/easier/faster way to have a pre-designed game map run into a game so its always fixed?
~ The approach above is not way off the wall or theres not a better/faster/cleaner approach?
~ Can Lua copy world map data around on load?
~ Any other suggestions :)

Thanks again for any help

Re: Create a Fixed Height Map from a Simple Image

PostPosted: Wed Nov 06, 2019 04:50
by Extex
I also would like to know.

Re: Create a Fixed Height Map from a Simple Image

PostPosted: Wed Nov 06, 2019 07:49
by sirrobzeroone
I've managed to use "Love" to write a little lua script that can output the coordinates and a value for each pixel in an image. I haven't tested to see how it copes with a 7.6 million odd values but I'll check that out next, I expect I may need to segment the image up a little.

Slightly amused the open source lua 2d engine was a snap to use to create a height map for opensource 3d voxel engine :).

edit: took about 15 mins but it processed my image which is about 3041x3041 = 9247681 data points....I removed all my "0" which is ocean floor and lost about 2.5million data points. Text file is still big but I'm hopeful I only need to do this once and can then have the map setup permanently and can just copy it over in map db format for each new game. I'll keep plugging away. Theoretically it should be possible to have minetest do the image manipulation however "Love" has a getpixel function out of the box, I think I can make it a stand alone Lua app using Love. If my proposed process above works I'll github the result and do a small UI. I am focused on trying to use the minetest mapgen as much as possible still as it has some cool features I'd like to make use of so this is very much just replacing the perlin noise step of map generation.

Re: Create a Fixed Height Map from a Simple Image

PostPosted: Thu Nov 07, 2019 21:51
by sirrobzeroone
I made some tweaks to the "Love" script and my approximate 3000x3000 image now runs in about 2mins. At the moment it can recognise 255 different height points, but i could extend that to 65025 pretty easily...but man i'd hate to craft something like that by hand even on an image. i have heights ranging from 5 to 160 at the moment 0 game height equates to 127. it uses the red channel value only to get 255, hence toss blue in and you get 65k.

Importing the data points to Minetest works okay slight delay on initial load as the 9.6million data points are loaded into a lua table/array/object. chunk generation via lua with a dirt map runs bettween 300-1000ms so a bit slow for a empty lua map, but works.

next to see if i can copy via lua script the map file to a new world on load. initail reading seems to indicate lua maybe able to do this but im a bit worried about the .sqlite files, plain txt files look easy enough....Im hoping to have to avoid using the OS to do this otherwise Ill be trying to detect operating system, although that may turn out to be to try

Re: Create a Fixed Height Map from a Simple Image

PostPosted: Fri Nov 08, 2019 02:55
by sirrobzeroone
The below code seems to work, I don't think it's os specific but might be as I've manually appended to the file path. I realized after I wrote and checked the below i could have used minetest.get_dir_list to check if map.sqlite exists. If it does exist nothing gets copied if it dosent exists all the predefined map/world files for your game get copied over.

Place the code into the top of the 1st mod you have that loads, for me thats my game specific mod inside init.lua. under that mod have a folder called "world" and place all your fully generated world files, dont forget underground etc so best to use least I think that would work, or just walk, float everywhere....if your maps small enough.

Code: Select all
--                                Copy files                              --
local worldpath = minetest.get_worldpath()

local check_exist ="/map.sqlite","rb")

   if check_exist == nil then
      -- Copy .mt files
         local infile ="/world/","r")
         local instr = infile:read("*all")
      -- Copy .sqlite files
         local infile2 ="/world/auth.sqlite","rb")
         local instr2 = infile2:read("*all")
         local infile2 ="/world/map.sqlite","rb")
         local instr2 = infile2:read("*all")
         local infile2 ="/world/players.sqlite","rb")
         local instr2 = infile2:read("*all")
      -- Copy .txt files
         local infile ="/world/env_meta.txt","r")
         local instr3 = infile:read("*all")

         local infile ="/world/force_loaded.txt","r")
         local instr3 = infile:read("*all")
         local infile ="/world/ipban.txt","r")
         local instr3 = infile:read("*all")

         local infile ="/world/map_meta.txt","r")
         local instr3 = infile:read("*all")

Edit: if theres any interest I could make this copying bit a standalone mod and include a small demo world/files.

Edit2: If you left the map generation stuff turned on you can do a partial map gen so for example use mapgen v7 make a big starting city and save that as the player start point. Then when the player loads for first time they start in the awesome pre-gened city but as they wander outwards stuff is created on the fly, although given its the same seed everytime they play it would be very very close to the same. thought it was worth mentioning as I tested that scenario as well.

Re: [Solved]Create a Fixed Height Map from a Simple Image

PostPosted: Sat Dec 07, 2019 21:55
by Bastrabun
I'm interested :)

Can you maybe make a short list of instructions to document the whole process, from image to worldfile?

Also, examples would be appreciated.

Asking, because I wanted to do something similar, from greyscale image to ingame heightmap.

Re: [Solved]Create a Fixed Height Map from a Simple Image

PostPosted: Sun Dec 08, 2019 18:43
by ShadMOrdre
I've recently began working with Voronoi diagrams, and this sounds like it would be a good tool to have in my toolchain. It's very time consuming to fly around a world, just to see how the Voronoi diagram is affecting the terrain.