]> git.somenet.org - factorio/some-zoom.git/blob - utils.lua
RELEASE 1.1.2 - Version-Bump. No functional changes. (I am still alive :)
[factorio/some-zoom.git] / utils.lua
1 -- utils.lua by binbinhfr, v1.0.16
2
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
5
6 colors = {
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},
12
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},
22
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},
28 }
29
30 anticolors = {
31     white = colors.black,
32     black = colors.white,
33     darkgrey = colors.white,
34     grey = colors.black,
35     lightgrey = colors.black,
36
37     red = colors.white,
38     darkred = colors.white,
39     lightred = colors.black,
40     green = colors.black,
41     darkgreen = colors.white,
42     lightgreen = colors.black,
43     blue = colors.white,
44     darkblue = colors.white,
45     lightblue = colors.black,
46
47     orange = colors.black,
48     yellow = colors.black,
49     pink = colors.white,
50     purple = colors.white,
51     brown = colors.white,
52 }
53
54 lightcolors = {
55     white = colors.lightgrey,
56     grey = colors.darkgrey,
57     lightgrey = colors.grey,
58
59     red = colors.lightred,
60     green = colors.lightgreen,
61     blue = colors.lightblue,
62     yellow = colors.orange,
63     pink = colors.purple,
64 }
65
66 local author_name1 = "BinbinHfr"
67 local author_name2 = "binbin"
68
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)
73 end
74
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+)")
79
80     v1a = tonumber(v1a)
81     v1b = tonumber(v1b)
82     v1c = tonumber(v1c)
83     v2a = tonumber(v2a)
84     v2b = tonumber(v2b)
85     v2c = tonumber(v2c)
86
87     if v1a > v2a then
88         return 1
89     elseif v1a < v2a then
90         return -1
91     elseif v1b > v2b then
92         return 1
93     elseif v1b < v2b then
94         return -1
95     elseif v1c > v2c then
96         return 1
97     elseif v1c < v2c then
98         return -1
99     else
100         return 0
101     end
102 end
103
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+)")
108     local ret
109
110     v1a = tonumber(v1a)
111     v1b = tonumber(v1b)
112     v1c = tonumber(v1c)
113     v2a = tonumber(v2a)
114     v2b = tonumber(v2b)
115     v2c = tonumber(v2c)
116
117     if v1a > v2a then
118         ret = false
119     elseif v1a < v2a then
120         ret = true
121     elseif v1b > v2b then
122         ret = false
123     elseif v1b < v2b then
124         ret = true
125     elseif v1c < v2c then
126         ret = true
127     else
128         ret = false
129     end
130
131     debug_print( "older_version ", v1, "<", v2, "=", ret )
132
133     return(ret)
134 end
135
136 --------------------------------------------------------------------------------------
137 function debug_active(...)
138     -- can be called everywhere, except in on_load where game is not existing
139     local s = ""
140
141     for i, v in ipairs({...}) do
142         s = s .. tostring(v)
143     end
144
145     if s == "RAZ" or debug_do_raz == true then
146         game.remove_path(debug_file)
147         debug_do_raz = false
148     elseif s == "CLEAR" then
149         for _, player in pairs(game.players) do
150             if player.connected then player.clear_console() end
151         end
152     end
153
154     s = debug_mod_name .. "(" .. game.tick .. "): " .. s
155     game.write_file( debug_file, s .. "\n", true )
156
157     for _, player in pairs(game.players) do
158         if player.connected then player.print(s) end
159     end
160 end
161
162 if debug_status == 1 then debug_print = debug_active else debug_print = function() end end
163
164 --------------------------------------------------------------------------------------
165 function message_all(s)
166     for _, player in pairs(game.players) do
167         if player.connected then
168             player.print(s)
169         end
170     end
171 end
172
173 --------------------------------------------------------------------------------------
174 function message_force(force, s)
175     for _, player in pairs(force.players) do
176         if player.connected then
177             player.print(s)
178         end
179     end
180 end
181
182 --------------------------------------------------------------------------------------
183 function square_area( origin, radius )
184     return {
185         {x=origin.x - radius, y=origin.y - radius},
186         {x=origin.x + radius, y=origin.y + radius}
187     }
188 end
189
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) )
195 end
196
197 --------------------------------------------------------------------------------------
198 function distance_square( pos1, pos2 )
199     return( math.max(math.abs(pos2.x - pos1.x),math.abs(pos2.y - pos1.y)) )
200 end
201
202 --------------------------------------------------------------------------------------
203 function pos_offset( pos, offset )
204     return { x=pos.x + offset.x, y=pos.y + offset.y }
205 end
206
207 --------------------------------------------------------------------------------------
208 function surface_area(surf)
209     local x1, y1, x2, y2 = 0,0,0,0
210
211     for chunk in surf.get_chunks() do
212         if chunk.x < x1 then
213             x1 = chunk.x
214         elseif chunk.x > x2 then
215             x2 = chunk.x
216         end
217         if chunk.y < y1 then
218             y1 = chunk.y
219         elseif chunk.y > y2 then
220             y2 = chunk.y
221         end
222     end
223
224     return( {{x1*32-8,y1*32-8},{x2*32+40,y2*32+40}} )
225 end
226
227 --------------------------------------------------------------------------------------
228 function iif( cond, val1, val2 )
229     if cond then
230         return val1
231     else
232         return val2
233     end
234 end
235
236 --------------------------------------------------------------------------------------
237 function add_list(list, obj)
238     -- to avoid duplicates...
239     for i, obj2 in pairs(list) do
240         if obj2 == obj then
241             return(false)
242         end
243     end
244     table.insert(list,obj)
245     return(true)
246 end
247
248 --------------------------------------------------------------------------------------
249 function del_list(list, obj)
250     for i, obj2 in pairs(list) do
251         if obj2 == obj then
252             table.remove( list, i )
253             return(true)
254         end
255     end
256     return(false)
257 end
258
259 --------------------------------------------------------------------------------------
260 function in_list(list, obj)
261     for k, obj2 in pairs(list) do
262         if obj2 == obj then
263             return(k)
264         end
265     end
266     return(nil)
267 end
268
269 --------------------------------------------------------------------------------------
270 function size_list(list)
271     local n = 0
272     for i in pairs(list) do
273         n = n + 1
274     end
275     return(n)
276 end
277
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)
283     end
284 end
285
286 ------------------------------------------------------------------------------------
287 function is_dev(player)
288     return( player.name == author_name1 or player.name == author_name2 )
289 end
290
291 --------------------------------------------------------------------------------------
292 function dupli_proto( type, name1, name2 )
293     if data.raw[type][name1] then
294         local proto = table.deepcopy(data.raw[type][name1])
295         proto.name = name2
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
300         return(proto)
301     else
302         error("prototype unknown " .. name1 )
303         return(nil)
304     end
305 end
306
307 --------------------------------------------------------------------------------------
308 function debug_guis( guip, indent )
309     if guip == nil then return end
310     debug_print( indent .. string.rep("....",indent) .. " " .. guip.name )
311     indent = indent+1
312     for k, gui in pairs(guip.children_names) do
313         debug_guis( guip[gui], indent )
314     end
315 end
316
317 --------------------------------------------------------------------------------------
318 function extract_monolith(filename, x, y, w, h)
319     return {
320         type = "monolith",
321
322         top_monolith_border = 0,
323         right_monolith_border = 0,
324         bottom_monolith_border = 0,
325         left_monolith_border = 0,
326
327         monolith_image = {
328             filename = filename,
329             priority = "extra-high-no-scale",
330             width = w,
331             height = h,
332             x = x,
333             y = y,
334         },
335     }
336 end
337