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

Post Reply
User avatar
sirrobzeroone
Member
Posts: 593
Joined: Mon Jul 16, 2018 07:56
GitHub: sirrobzeroone
Contact:

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

by sirrobzeroone » Post

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 viewtopic.php?f=9&t=19387 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 - https://stackoverflow.com/questions/882 ... o-csv?rq=1

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
Last edited by sirrobzeroone on Fri Nov 08, 2019 02:59, edited 1 time in total.

User avatar
Extex
Member
Posts: 245
Joined: Wed Mar 14, 2018 23:14
GitHub: Extex101
In-game: Extex

Re: Create a Fixed Height Map from a Simple Image

by Extex » Post

I also would like to know.
Creator of jelys_pizzaria and motorbike, and player of persistent kingdoms. RIP

User avatar
sirrobzeroone
Member
Posts: 593
Joined: Mon Jul 16, 2018 07:56
GitHub: sirrobzeroone
Contact:

Re: Create a Fixed Height Map from a Simple Image

by sirrobzeroone » Post

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.

User avatar
sirrobzeroone
Member
Posts: 593
Joined: Mon Jul 16, 2018 07:56
GitHub: sirrobzeroone
Contact:

Re: Create a Fixed Height Map from a Simple Image

by sirrobzeroone » Post

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 easy...off to try

User avatar
sirrobzeroone
Member
Posts: 593
Joined: Mon Jul 16, 2018 07:56
GitHub: sirrobzeroone
Contact:

Re: Create a Fixed Height Map from a Simple Image

by sirrobzeroone » Post

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 minetest.emerge_area...at 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 = io.open(worldpath.."/map.sqlite","rb")
io.close()

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

			local infile = io.open(path.."/world/force_loaded.txt","r")
			local instr3 = infile:read("*all")
			infile:close()
			minetest.safe_file_write(worldpath.."/force_loaded.txt",instr3)
			
			local infile = io.open(path.."/world/ipban.txt","r")
			local instr3 = infile:read("*all")
			infile:close()
			minetest.safe_file_write(worldpath.."/ipban.txt",instr3)

			local infile = io.open(path.."/world/map_meta.txt","r")
			local instr3 = infile:read("*all")
			infile:close()
			minetest.safe_file_write(worldpath.."/map_meta.txt",instr3)
	end

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.

Bastrabun
Member
Posts: 212
Joined: Mon Nov 04, 2019 19:48

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

by Bastrabun » Post

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.
Whatever I say is CC0

ShadMOrdre
Member
Posts: 1118
Joined: Mon Dec 29, 2014 08:07
Location: USA

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

by ShadMOrdre » Post

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.

Shad

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests