1 -- utils.lua by binbinhfr, v1.0.16
3 -- define debug_status to 1 or nil in the control.lua, before statement require("utils")
4 -- define also debug_file and debug_mod_name
7 white = {r = 1, g = 1, b = 1},
8 black = {r = 0, g = 0, b = 0},
9 darkgrey = {r = 0.25, g = 0.25, b = 0.25},
10 grey = {r = 0.5, g = 0.5, b = 0.5},
11 lightgrey = {r = 0.75, g = 0.75, b = 0.75},
13 red = {r = 1, g = 0, b = 0},
14 darkred = {r = 0.5, g = 0, b = 0},
15 lightred = {r = 1, g = 0.5, b = 0.5},
16 green = {r = 0, g = 1, b = 0},
17 darkgreen = {r = 0, g = 0.5, b = 0},
18 lightgreen = {r = 0.5, g = 1, b = 0.5},
19 blue = {r = 0, g = 0, b = 1},
20 darkblue = {r = 0, g = 0, b = 0.5},
21 lightblue = {r = 0.5, g = 0.5, b = 1},
23 orange = {r = 1, g = 0.55, b = 0.1},
24 yellow = {r = 1, g = 1, b = 0},
25 pink = {r = 1, g = 0, b = 1},
26 purple = {r = 0.6, g = 0.1, b = 0.6},
27 brown = {r = 0.6, g = 0.4, b = 0.1},
33 darkgrey = colors.white,
35 lightgrey = colors.black,
38 darkred = colors.white,
39 lightred = colors.black,
41 darkgreen = colors.white,
42 lightgreen = colors.black,
44 darkblue = colors.white,
45 lightblue = colors.black,
47 orange = colors.black,
48 yellow = colors.black,
50 purple = colors.white,
55 white = colors.lightgrey,
56 grey = colors.darkgrey,
57 lightgrey = colors.grey,
59 red = colors.lightred,
60 green = colors.lightgreen,
61 blue = colors.lightblue,
62 yellow = colors.orange,
66 local author_name1 = "BinbinHfr"
67 local author_name2 = "binbin"
69 --------------------------------------------------------------------------------------
70 function read_version(v)
71 local v1, v2, v3 = string.match(v, "(%d+).(%d+).(%d+)")
72 debug_print( "version cut = ", v1,v2,v3)
75 --------------------------------------------------------------------------------------
76 function compare_versions(v1,v2)
77 local v1a, v1b, v1c = string.match(v1, "(%d+).(%d+).(%d+)")
78 local v2a, v2b, v2c = string.match(v2, "(%d+).(%d+).(%d+)")
104 --------------------------------------------------------------------------------------
105 function older_version(v1,v2)
106 local v1a, v1b, v1c = string.match(v1, "(%d+).(%d+).(%d+)")
107 local v2a, v2b, v2c = string.match(v2, "(%d+).(%d+).(%d+)")
119 elseif v1a < v2a then
121 elseif v1b > v2b then
123 elseif v1b < v2b then
125 elseif v1c < v2c then
131 debug_print( "older_version ", v1, "<", v2, "=", ret )
136 --------------------------------------------------------------------------------------
137 function debug_active(...)
138 -- can be called everywhere, except in on_load where game is not existing
141 for i, v in ipairs({...}) do
145 if s == "RAZ" or debug_do_raz == true then
146 game.remove_path(debug_file)
148 elseif s == "CLEAR" then
149 for _, player in pairs(game.players) do
150 if player.connected then player.clear_console() end
154 s = debug_mod_name .. "(" .. game.tick .. "): " .. s
155 game.write_file( debug_file, s .. "\n", true )
157 for _, player in pairs(game.players) do
158 if player.connected then player.print(s) end
162 if debug_status == 1 then debug_print = debug_active else debug_print = function() end end
164 --------------------------------------------------------------------------------------
165 function message_all(s)
166 for _, player in pairs(game.players) do
167 if player.connected then
173 --------------------------------------------------------------------------------------
174 function message_force(force, s)
175 for _, player in pairs(force.players) do
176 if player.connected then
182 --------------------------------------------------------------------------------------
183 function square_area( origin, radius )
185 {x=origin.x - radius, y=origin.y - radius},
186 {x=origin.x + radius, y=origin.y + radius}
190 --------------------------------------------------------------------------------------
191 function distance( pos1, pos2 )
192 local dx = pos2.x - pos1.x
193 local dy = pos2.y - pos1.y
194 return( math.sqrt(dx*dx+dy*dy) )
197 --------------------------------------------------------------------------------------
198 function distance_square( pos1, pos2 )
199 return( math.max(math.abs(pos2.x - pos1.x),math.abs(pos2.y - pos1.y)) )
202 --------------------------------------------------------------------------------------
203 function pos_offset( pos, offset )
204 return { x=pos.x + offset.x, y=pos.y + offset.y }
207 --------------------------------------------------------------------------------------
208 function surface_area(surf)
209 local x1, y1, x2, y2 = 0,0,0,0
211 for chunk in surf.get_chunks() do
214 elseif chunk.x > x2 then
219 elseif chunk.y > y2 then
224 return( {{x1*32-8,y1*32-8},{x2*32+40,y2*32+40}} )
227 --------------------------------------------------------------------------------------
228 function iif( cond, val1, val2 )
236 --------------------------------------------------------------------------------------
237 function add_list(list, obj)
238 -- to avoid duplicates...
239 for i, obj2 in pairs(list) do
244 table.insert(list,obj)
248 --------------------------------------------------------------------------------------
249 function del_list(list, obj)
250 for i, obj2 in pairs(list) do
252 table.remove( list, i )
259 --------------------------------------------------------------------------------------
260 function in_list(list, obj)
261 for k, obj2 in pairs(list) do
269 --------------------------------------------------------------------------------------
270 function size_list(list)
272 for i in pairs(list) do
278 --------------------------------------------------------------------------------------
279 function concat_lists(list1, list2)
280 -- add list2 into list1 , do not avoid duplicates...
281 for i, obj in pairs(list2) do
282 table.insert(list1,obj)
286 ------------------------------------------------------------------------------------
287 function is_dev(player)
288 return( player.name == author_name1 or player.name == author_name2 )
291 --------------------------------------------------------------------------------------
292 function dupli_proto( type, name1, name2 )
293 if data.raw[type][name1] then
294 local proto = table.deepcopy(data.raw[type][name1])
296 if proto.minable and proto.minable.result then proto.minable.result = name2 end
297 if proto.place_result then proto.place_result = name2 end
298 if proto.take_result then proto.take_result = name2 end
299 if proto.result then proto.result = name2 end
302 error("prototype unknown " .. name1 )
307 --------------------------------------------------------------------------------------
308 function debug_guis( guip, indent )
309 if guip == nil then return end
310 debug_print( indent .. string.rep("....",indent) .. " " .. guip.name )
312 for k, gui in pairs(guip.children_names) do
313 debug_guis( guip[gui], indent )
317 --------------------------------------------------------------------------------------
318 function extract_monolith(filename, x, y, w, h)
322 top_monolith_border = 0,
323 right_monolith_border = 0,
324 bottom_monolith_border = 0,
325 left_monolith_border = 0,
329 priority = "extra-high-no-scale",