Refactor marker api calls into marker_api.lua
This commit is contained in:
		
							
								
								
									
										6
									
								
								init.lua
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								init.lua
									
									
									
									
									
								
							| @@ -53,12 +53,14 @@ local audio = loadfile(modpath .. "/audio.lua") (); | ||||
| local biomes = loadfile(modpath .. "/biome_api.lua") (); | ||||
|  | ||||
| local scanner = loadfile(modpath .. "/scanner.lua") (map_data, chunk); | ||||
| local maps = loadfile(modpath .. "/map_api.lua") (map_data, chunk, scanner, marker_lookup); | ||||
| local map_formspec = loadfile(modpath .. "/map_formspec.lua") (map_data, gui, skin, biomes); | ||||
| local maps = loadfile(modpath .. "/map_api.lua") (map_data, chunk, scanner); | ||||
| local markers = loadfile(modpath .. "/marker_api.lua") (marker_lookup); | ||||
| local map_formspec = loadfile(modpath .. "/map_formspec.lua") (map_data, gui, skin, biomes, markers); | ||||
| loadfile(modpath .. "/items.lua") (chunk, marker_lookup, gui, skin, audio, maps, map_formspec); | ||||
| loadfile(modpath .. "/commands.lua") (chunk, audio, map_formspec); | ||||
| loadfile(modpath .. "/table.lua") (gui, skin, audio, maps); | ||||
|  | ||||
| cartographer.skin = skin; | ||||
| cartographer.biomes = biomes; | ||||
| cartographer.markers = markers; | ||||
| cartographer.maps = maps; | ||||
|   | ||||
							
								
								
									
										75
									
								
								map_api.lua
									
									
									
									
									
								
							
							
						
						
									
										75
									
								
								map_api.lua
									
									
									
									
									
								
							| @@ -1,4 +1,4 @@ | ||||
| local map_data, chunk, scanner, marker_lookup = ...; | ||||
| local map_data, chunk, scanner = ...; | ||||
|  | ||||
| local Map = {}; | ||||
| Map.__index = Map; | ||||
| @@ -144,60 +144,6 @@ local function fill_loop() | ||||
| end | ||||
| minetest.after(5, fill_loop); | ||||
|  | ||||
| -- Format marker ids to allow their use as formspec element ids. | ||||
| -- We're mostly concerned with guarding against the : character because it is | ||||
| -- common for ids and has an alternate meaning in formspecs. | ||||
| -- id: The id to format | ||||
| -- | ||||
| -- Returns the formatted id | ||||
| local function format_marker_id(id) | ||||
|     return id:gsub(":", "_"); | ||||
| end | ||||
|  | ||||
| -- Find the marker data for a given id | ||||
| -- id: The id to search for | ||||
| -- | ||||
| -- Returns the marker data, or nil if not found | ||||
| function cartographer.get_marker_data(id) | ||||
|     if not id then | ||||
|         return nil; | ||||
|     end | ||||
|  | ||||
|     id = format_marker_id(id); | ||||
|     for _,marker in pairs(marker_lookup) do | ||||
|         if marker.id == id then | ||||
|             return marker; | ||||
|         end | ||||
|     end | ||||
|  | ||||
|     return nil; | ||||
| end | ||||
|  | ||||
| -- Register a marker with textures to display | ||||
| -- id: A string containing the id of the marker | ||||
| -- name: A string containing the displayedname of the marker | ||||
| -- textures: A table of texture names. | ||||
| --           These should correspond with detail levels, | ||||
| --           any detail level past the length of the table will return the last texture | ||||
| function cartographer.register_marker(id, name, textures) | ||||
|     if not id then | ||||
|         return nil; | ||||
|     end | ||||
|  | ||||
|     id = format_marker_id(id); | ||||
|     local existing_marker = cartographer.get_marker_data(id); | ||||
|     if existing_marker then | ||||
|         existing_marker.name = name; | ||||
|         existing_marker.textures = textures; | ||||
|     else | ||||
|         marker_lookup[#marker_lookup+1] = { | ||||
|             id = id, | ||||
|             name = name, | ||||
|             textures = textures, | ||||
|         }; | ||||
|     end | ||||
| end | ||||
|  | ||||
| -- Get an entry from a list for a given detail level | ||||
| -- textures: An array of textures | ||||
| -- detail: The detail level | ||||
| @@ -207,23 +153,4 @@ function cartographer.detail_texture(textures, detail) | ||||
|     return textures[math.min(detail, #textures)]; | ||||
| end | ||||
|  | ||||
| -- Get the texture name (minus extension) for the given marker and detail level. | ||||
| -- id: A string containing the marker id | ||||
| -- detail: The detail level | ||||
| -- Returns a string with a texture name, or nil if no matching marker was found. | ||||
| function cartographer.get_marker_texture(id, detail) | ||||
|     if not id then | ||||
|         return nil; | ||||
|     end | ||||
|  | ||||
|     id = format_marker_id(id); | ||||
|     local marker = cartographer.get_marker_data(id); | ||||
|  | ||||
|     if marker then | ||||
|         return marker.textures[math.min(detail, #marker.textures)]; | ||||
|     end | ||||
|  | ||||
|     return nil; | ||||
| end | ||||
|  | ||||
| return maps; | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| -- Arguments | ||||
| -- map_data: The cartographer map data table | ||||
| local map_data, gui, skin, biomes = ...; | ||||
| local map_data, gui, skin, biomes, markers = ...; | ||||
|  | ||||
| -- Constants | ||||
| local TILE_SIZE = 0.25; | ||||
| @@ -112,7 +112,7 @@ local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, h | ||||
|                     }; | ||||
|  | ||||
|                     if get_marker then | ||||
|                         local marker = cartographer.get_marker_texture(get_marker(..., i, j), detail); | ||||
|                         local marker = markers.get_texture(get_marker(..., i, j), detail); | ||||
|                         if marker then | ||||
|                             str = str .. gui.image { | ||||
|                                 x = fx, | ||||
|   | ||||
							
								
								
									
										80
									
								
								marker_api.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								marker_api.lua
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,80 @@ | ||||
| local marker_lookup = ...; | ||||
|  | ||||
| -- Format marker ids to allow their use as formspec element ids. | ||||
| -- We're mostly concerned with guarding against the : character because it is | ||||
| -- common for ids and has an alternate meaning in formspecs. | ||||
| -- id: The id to format | ||||
| -- | ||||
| -- Returns the formatted id | ||||
| local function format_marker_id(id) | ||||
|     return id:gsub(":", "_"); | ||||
| end | ||||
|  | ||||
| -- Find the marker data for a given id | ||||
| -- id: The id to search for | ||||
| -- | ||||
| -- Returns the marker data, or nil if not found | ||||
| local function get_marker(id) | ||||
|     if not id then | ||||
|         return nil; | ||||
|     end | ||||
|  | ||||
|     id = format_marker_id(id); | ||||
|     for _,marker in pairs(marker_lookup) do | ||||
|         if marker.id == id then | ||||
|             return marker; | ||||
|         end | ||||
|     end | ||||
|  | ||||
|     return nil; | ||||
| end | ||||
|  | ||||
| -- Register a marker with textures to display | ||||
| -- id: A string containing the id of the marker | ||||
| -- name: A string containing the displayedname of the marker | ||||
| -- textures: A table of texture names. | ||||
| --           These should correspond with detail levels, | ||||
| --           any detail level past the length of the table will return the last texture | ||||
| local function add_marker(id, name, textures) | ||||
|     if not id then | ||||
|         return nil; | ||||
|     end | ||||
|  | ||||
|     id = format_marker_id(id); | ||||
|     local existing_marker = get_marker(id); | ||||
|     if existing_marker then | ||||
|         existing_marker.name = name; | ||||
|         existing_marker.textures = textures; | ||||
|     else | ||||
|         marker_lookup[#marker_lookup+1] = { | ||||
|             id = id, | ||||
|             name = name, | ||||
|             textures = textures, | ||||
|         }; | ||||
|     end | ||||
| end | ||||
|  | ||||
| -- Get the texture name (minus extension) for the given marker and detail level. | ||||
| -- id: A string containing the marker id | ||||
| -- detail: The detail level | ||||
| -- Returns a string with a texture name, or nil if no matching marker was found. | ||||
| local function get_marker_texture(id, detail) | ||||
|     if not id then | ||||
|         return nil; | ||||
|     end | ||||
|  | ||||
|     id = format_marker_id(id); | ||||
|     local marker = get_marker(id); | ||||
|  | ||||
|     if marker then | ||||
|         return marker.textures[math.min(detail, #marker.textures)]; | ||||
|     end | ||||
|  | ||||
|     return nil; | ||||
| end | ||||
|  | ||||
| return { | ||||
|     add = add_marker, | ||||
|     get = get_marker, | ||||
|     get_texture = get_marker_texture, | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user