scanner.lua: Parameterize coordinate conversion methods
This commit is contained in:
		
							
								
								
									
										7
									
								
								init.lua
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								init.lua
									
									
									
									
									
								
							| @@ -49,10 +49,15 @@ function fromchunk(coord) | |||||||
|     return math.floor(coord * _cartographer.CHUNK_SIZE); |     return math.floor(coord * _cartographer.CHUNK_SIZE); | ||||||
| end | end | ||||||
|  |  | ||||||
|  | local chunk = { | ||||||
|  |     to = tochunk, | ||||||
|  |     from = fromchunk, | ||||||
|  | }; | ||||||
|  |  | ||||||
| -- Includes | -- Includes | ||||||
| cartographer.skin = loadfile(modpath .. "/skin_api.lua") (); | cartographer.skin = loadfile(modpath .. "/skin_api.lua") (); | ||||||
| cartographer.gui = loadfile(modpath .. "/formspec.lua") (); | cartographer.gui = loadfile(modpath .. "/formspec.lua") (); | ||||||
| loadfile(modpath .. "/scanner.lua") (map_data); | loadfile(modpath .. "/scanner.lua") (map_data, chunk); | ||||||
| loadfile(modpath .. "/map_api.lua") (); | loadfile(modpath .. "/map_api.lua") (); | ||||||
| loadfile(modpath .. "/items.lua") (); | loadfile(modpath .. "/items.lua") (); | ||||||
| _cartographer.generate_marker_formspec = loadfile(modpath .. "/marker_formspec.lua") (_cartographer.marker_lookup, cartographer.gui); | _cartographer.generate_marker_formspec = loadfile(modpath .. "/marker_formspec.lua") (_cartographer.marker_lookup, cartographer.gui); | ||||||
|   | |||||||
							
								
								
									
										39
									
								
								scanner.lua
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								scanner.lua
									
									
									
									
									
								
							| @@ -1,9 +1,6 @@ | |||||||
| -- Arguments | -- Arguments | ||||||
| -- map_data: The cartographer map data table | -- map_data: The cartographer map data table | ||||||
| local map_data = ...; | local map_data, chunk = ...; | ||||||
|  |  | ||||||
| -- Constants |  | ||||||
| local CHUNK_SIZE = _cartographer.CHUNK_SIZE; |  | ||||||
|  |  | ||||||
| -- Register a new tile in map data | -- Register a new tile in map data | ||||||
| -- x: The x position in map coordinates | -- x: The x position in map coordinates | ||||||
| @@ -124,18 +121,18 @@ local function get_biome(min, max) | |||||||
| end | end | ||||||
|  |  | ||||||
| local function on_generated(min, max, _) | local function on_generated(min, max, _) | ||||||
|     for i = tochunk(min.x),tochunk(max.x),1 do |     for i = chunk.to(min.x),chunk.to(max.x),1 do | ||||||
|         for j = tochunk(min.z),tochunk(max.z),1 do |         for j = chunk.to(min.z),chunk.to(max.z),1 do | ||||||
|  |  | ||||||
|             local sub_min = { |             local sub_min = { | ||||||
|                 x = i * CHUNK_SIZE, |                 x = chunk.from(i), | ||||||
|                 y = min.y, |                 y = min.y, | ||||||
|                 z = j * CHUNK_SIZE, |                 z = chunk.from(j), | ||||||
|             }; |             }; | ||||||
|             local sub_max = { |             local sub_max = { | ||||||
|                 x = i * CHUNK_SIZE + CHUNK_SIZE, |                 x = chunk.from(i + 1), | ||||||
|                 y = max.y, |                 y = max.y, | ||||||
|                 z = j * CHUNK_SIZE + CHUNK_SIZE, |                 z = chunk.from(j + 1), | ||||||
|             }; |             }; | ||||||
|             local biome, height = get_mapgen_biome(sub_min, sub_max, min, max); |             local biome, height = get_mapgen_biome(sub_min, sub_max, min, max); | ||||||
|             if  biome ~= nil then |             if  biome ~= nil then | ||||||
| @@ -166,12 +163,12 @@ end | |||||||
| -- pos: The position as a table, in world coordinates | -- pos: The position as a table, in world coordinates | ||||||
| function cartographer.queue_region(pos) | function cartographer.queue_region(pos) | ||||||
|     local converted = { |     local converted = { | ||||||
|         x = fromchunk(tochunk(pos.x)), |         x = chunk.from(chunk.to(pos.x)), | ||||||
|         y = fromchunk(tochunk(pos.y)), |         y = chunk.from(chunk.to(pos.y)), | ||||||
|         z = fromchunk(tochunk(pos.z)), |         z = chunk.from(chunk.to(pos.z)), | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     if is_scan_handled(tochunk(pos.x), pos.y, tochunk(pos.z)) then |     if is_scan_handled(chunk.to(pos.x), pos.y, chunk.to(pos.z)) then | ||||||
|         return; |         return; | ||||||
|     end |     end | ||||||
|  |  | ||||||
| @@ -193,20 +190,24 @@ function cartographer.scan_regions() | |||||||
|     end |     end | ||||||
|  |  | ||||||
|     local startpos = cartographer.scan_queue[1]; |     local startpos = cartographer.scan_queue[1]; | ||||||
|  |     local chunk_x = chunk.to(startpos.x); | ||||||
|  |     local chunk_y = chunk.to(startpos.y); | ||||||
|  |     local chunk_z = chunk.to(startpos.z); | ||||||
|  |  | ||||||
|     local endpos = { |     local endpos = { | ||||||
|         x = startpos.x + CHUNK_SIZE, |         x = chunk.from(chunk_x + 1), | ||||||
|         y = startpos.y + CHUNK_SIZE, |         y = chunk.from(chunk_y + 1), | ||||||
|         z = startpos.z + CHUNK_SIZE, |         z = chunk.from(chunk_z + 1), | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     if is_scan_handled(tochunk(startpos.x), startpos.y, tochunk(startpos.z)) then |     if is_scan_handled(chunk_x, startpos.y, chunk_z) then | ||||||
|         table.remove(cartographer.scan_queue, 1); |         table.remove(cartographer.scan_queue, 1); | ||||||
|         return; |         return; | ||||||
|     end |     end | ||||||
|  |  | ||||||
|     local biome,height = get_biome(startpos, endpos); |     local biome,height = get_biome(startpos, endpos); | ||||||
|     if  biome ~= nil then |     if  biome ~= nil then | ||||||
|         register_tile(tochunk(startpos.x), tochunk(startpos.z), biome, height, true) |         register_tile(chunk_x, chunk_z, biome, height, true) | ||||||
|     end |     end | ||||||
|  |  | ||||||
|     table.remove(cartographer.scan_queue, 1); |     table.remove(cartographer.scan_queue, 1); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user