Redstone: Add basic blastnode handling
[MineClone/MineClone2.git] / mods / ITEMS / REDSTONE / mesecons / util.lua
blob91da1e951aa3015f57e2d796824af968b1d26e2e
1 function mesecon.move_node(pos, newpos)
2 local node = minetest.get_node(pos)
3 local meta = minetest.get_meta(pos):to_table()
4 minetest.remove_node(pos)
5 minetest.set_node(newpos, node)
6 minetest.get_meta(pos):from_table(meta)
7 end
9 --Rules rotation Functions:
10 function mesecon.rotate_rules_right(rules)
11 local nr = {}
12 for i, rule in ipairs(rules) do
13 table.insert(nr, {
14 x = -rule.z,
15 y = rule.y,
16 z = rule.x,
17 name = rule.name,
18 spread = rule.spread,})
19 end
20 return nr
21 end
23 function mesecon.rotate_rules_left(rules)
24 local nr = {}
25 for i, rule in ipairs(rules) do
26 table.insert(nr, {
27 x = rule.z,
28 y = rule.y,
29 z = -rule.x,
30 name = rule.name,
31 spread = rule.spread,})
32 end
33 return nr
34 end
36 function mesecon.rotate_rules_down(rules)
37 local nr = {}
38 for i, rule in ipairs(rules) do
39 table.insert(nr, {
40 x = -rule.y,
41 y = rule.x,
42 z = rule.z,
43 name = rule.name,
44 spread = rule.spread,})
45 end
46 return nr
47 end
49 function mesecon.rotate_rules_up(rules)
50 local nr = {}
51 for i, rule in ipairs(rules) do
52 table.insert(nr, {
53 x = rule.y,
54 y = -rule.x,
55 z = rule.z,
56 name = rule.name,
57 spread = rule.spread,})
58 end
59 return nr
60 end
62 function mesecon.flattenrules(allrules)
63 --[[
66 {xyz},
67 {xyz},
70 {xyz},
71 {xyz},
74 --]]
75 if allrules[1] and
76 allrules[1].x then
77 return allrules
78 end
80 local shallowrules = {}
81 for _, metarule in ipairs( allrules) do
82 for _, rule in ipairs(metarule ) do
83 table.insert(shallowrules, rule)
84 end
85 end
86 return shallowrules
87 --[[
89 {xyz},
90 {xyz},
91 {xyz},
92 {xyz},
94 --]]
95 end
97 function mesecon.rule2bit(findrule, allrules)
98 --get the bit of the metarule the rule is in, or bit 1
99 if (allrules[1] and
100 allrules[1].x) or
101 not findrule then
102 return 1
104 for m,metarule in ipairs( allrules) do
105 for _, rule in ipairs(metarule ) do
106 if vector.equals(findrule, rule) then
107 return m
113 function mesecon.rule2metaindex(findrule, allrules)
114 --get the metarule the rule is in, or allrules
115 if allrules[1].x then
116 return nil
119 if not(findrule) then
120 return mesecon.flattenrules(allrules)
123 for m, metarule in ipairs( allrules) do
124 for _, rule in ipairs(metarule ) do
125 if vector.equals(findrule, rule) then
126 return m
132 function mesecon.rule2meta(findrule, allrules)
133 if #allrules == 0 then return {} end
135 local index = mesecon.rule2metaindex(findrule, allrules)
136 if index == nil then
137 if allrules[1].x then
138 return allrules
139 else
140 return {}
143 return allrules[index]
146 -- Returns the 6 immediate neighbors of pos
147 -- (nodes which touch the sides of pos).
148 -- NOT PART OF ORIGINAL MESECONS!
149 function mesecon.mcl_get_neighbors(pos)
150 local r = mesecon.rules.alldirs
151 local e = {}
152 for i=1, #r do
153 table.insert(e, { pos = vector.add(pos, r[i]), link = r[i] })
155 return e
158 function mesecon.dec2bin(n)
159 local x, y = math.floor(n / 2), n % 2
160 if (n > 1) then
161 return mesecon.dec2bin(x)..y
162 else
163 return ""..y
167 function mesecon.getstate(nodename, states)
168 for state, name in ipairs(states) do
169 if name == nodename then
170 return state
173 error(nodename.." doesn't mention itself in "..dump(states))
176 function mesecon.getbinstate(nodename, states)
177 return mesecon.dec2bin(mesecon.getstate(nodename, states)-1)
180 function mesecon.get_bit(binary,bit)
181 bit = bit or 1
182 local c = binary:len()-(bit-1)
183 return binary:sub(c,c) == "1"
186 function mesecon.set_bit(binary,bit,value)
187 if value == "1" then
188 if not mesecon.get_bit(binary,bit) then
189 return mesecon.dec2bin(tonumber(binary,2)+math.pow(2,bit-1))
191 elseif value == "0" then
192 if mesecon.get_bit(binary,bit) then
193 return mesecon.dec2bin(tonumber(binary,2)-math.pow(2,bit-1))
196 return binary
200 function mesecon.invertRule(r)
201 local spread = r.spread
202 r = vector.multiply(r, -1)
203 if spread then
204 r.spread = true
206 return r
209 function mesecon.tablecopy(table) -- deep table copy
210 if type(table) ~= "table" then return table end -- no need to copy
211 local newtable = {}
213 for idx, item in pairs(table) do
214 if type(item) == "table" then
215 newtable[idx] = mesecon.tablecopy(item)
216 else
217 newtable[idx] = item
221 return newtable
224 function mesecon.cmpAny(t1, t2)
225 if type(t1) ~= type(t2) then return false end
226 if type(t1) ~= "table" and type(t2) ~= "table" then return t1 == t2 end
228 for i, e in pairs(t1) do
229 if not mesecon.cmpAny(e, t2[i]) then return false end
232 return true
235 -- does not overwrite values; number keys (ipairs) are appended, not overwritten
236 function mesecon.mergetable(source, dest)
237 local rval = mesecon.tablecopy(dest)
239 for k, v in pairs(source) do
240 rval[k] = dest[k] or mesecon.tablecopy(v)
242 for i, v in ipairs(source) do
243 table.insert(rval, mesecon.tablecopy(v))
246 return rval
249 function mesecon.register_node(name, spec_common, spec_off, spec_on)
250 spec_common.drop = spec_common.drop or name .. "_off"
251 spec_common.on_blast = spec_common.on_blast or mesecon.on_blastnode
252 spec_common.__mesecon_basename = name
253 spec_on.__mesecon_state = "on"
254 spec_off.__mesecon_state = "off"
256 spec_on = mesecon.mergetable(spec_common, spec_on);
257 spec_off = mesecon.mergetable(spec_common, spec_off);
259 minetest.register_node(name .. "_on", spec_on)
260 minetest.register_node(name .. "_off", spec_off)
263 -- swap onstate and offstate nodes, returns new state
264 function mesecon.flipstate(pos, node)
265 local nodedef = minetest.registered_nodes[node.name]
266 local newstate
267 if (nodedef.__mesecon_state == "on") then newstate = "off" end
268 if (nodedef.__mesecon_state == "off") then newstate = "on" end
270 minetest.swap_node(pos, {name = nodedef.__mesecon_basename .. "_" .. newstate,
271 param2 = node.param2})
273 return newstate
276 -- File writing / reading utilities
277 local wpath = minetest.get_worldpath()
278 function mesecon.file2table(filename)
279 local f = io.open(wpath..DIR_DELIM..filename, "r")
280 if f == nil then return {} end
281 local t = f:read("*all")
282 f:close()
283 if t == "" or t == nil then return {} end
284 return minetest.deserialize(t)
287 function mesecon.table2file(filename, table)
288 local f = io.open(wpath..DIR_DELIM..filename, "w")
289 f:write(minetest.serialize(table))
290 f:close()
293 -- Block position "hashing" (convert to integer) functions for voxelmanip cache
294 local BLOCKSIZE = 16
296 -- convert node position --> block hash
297 local function hash_blockpos(pos)
298 return minetest.hash_node_position({
299 x = math.floor(pos.x/BLOCKSIZE),
300 y = math.floor(pos.y/BLOCKSIZE),
301 z = math.floor(pos.z/BLOCKSIZE)
305 -- Maps from a hashed mapblock position (as returned by hash_blockpos) to a
306 -- table.
308 -- Contents of the table are:
309 -- “vm” → the VoxelManipulator
310 -- “va” → the VoxelArea
311 -- “data” → the data array
312 -- “param1” → the param1 array
313 -- “param2” → the param2 array
314 -- “dirty” → true if data has been modified
316 -- Nil if no VM-based transaction is in progress.
317 local vm_cache = nil
319 -- Starts a VoxelManipulator-based transaction.
321 -- During a VM transaction, calls to vm_get_node and vm_swap_node operate on a
322 -- cached copy of the world loaded via VoxelManipulators. That cache can later
323 -- be committed to the real map by means of vm_commit or discarded by means of
324 -- vm_abort.
325 function mesecon.vm_begin()
326 vm_cache = {}
329 -- Finishes a VoxelManipulator-based transaction, freeing the VMs and map data
330 -- and writing back any modified areas.
331 function mesecon.vm_commit()
332 for hash, tbl in pairs(vm_cache) do
333 if tbl.dirty then
334 local vm = tbl.vm
335 vm:set_data(tbl.data)
336 vm:write_to_map()
337 vm:update_map()
340 vm_cache = nil
343 -- Finishes a VoxelManipulator-based transaction, freeing the VMs and throwing
344 -- away any modified areas.
345 function mesecon.vm_abort()
346 vm_cache = nil
349 -- Gets the cache entry covering a position, populating it if necessary.
350 local function vm_get_or_create_entry(pos)
351 local hash = hash_blockpos(pos)
352 local tbl = vm_cache[hash]
353 if not tbl then
354 local vm = minetest.get_voxel_manip(pos, pos)
355 local min_pos, max_pos = vm:get_emerged_area()
356 local va = VoxelArea:new{MinEdge = min_pos, MaxEdge = max_pos}
357 tbl = {vm = vm, va = va, data = vm:get_data(), param1 = vm:get_light_data(), param2 = vm:get_param2_data(), dirty = false}
358 vm_cache[hash] = tbl
360 return tbl
363 -- Gets the node at a given position during a VoxelManipulator-based
364 -- transaction.
365 function mesecon.vm_get_node(pos)
366 local tbl = vm_get_or_create_entry(pos)
367 local index = tbl.va:indexp(pos)
368 local node_value = tbl.data[index]
369 if node_value == core.CONTENT_IGNORE then
370 return nil
371 else
372 local node_param1 = tbl.param1[index]
373 local node_param2 = tbl.param2[index]
374 return {name = minetest.get_name_from_content_id(node_value), param1 = node_param1, param2 = node_param2}
378 -- Sets a node’s name during a VoxelManipulator-based transaction.
380 -- Existing param1, param2, and metadata are left alone.
381 function mesecon.vm_swap_node(pos, name)
382 local tbl = vm_get_or_create_entry(pos)
383 local index = tbl.va:indexp(pos)
384 tbl.data[index] = minetest.get_content_id(name)
385 tbl.dirty = true
388 -- Gets the node at a given position, regardless of whether it is loaded or
389 -- not, respecting a transaction if one is in progress.
391 -- Outside a VM transaction, if the mapblock is not loaded, it is pulled into
392 -- the server’s main map data cache and then accessed from there.
394 -- Inside a VM transaction, the transaction’s VM cache is used.
395 function mesecon.get_node_force(pos)
396 if vm_cache then
397 return mesecon.vm_get_node(pos)
398 else
399 local node = minetest.get_node_or_nil(pos)
400 if node == nil then
401 -- Node is not currently loaded; use a VoxelManipulator to prime
402 -- the mapblock cache and try again.
403 minetest.get_voxel_manip(pos, pos)
404 node = minetest.get_node_or_nil(pos)
406 return node
410 -- Swaps the node at a given position, regardless of whether it is loaded or
411 -- not, respecting a transaction if one is in progress.
413 -- Outside a VM transaction, if the mapblock is not loaded, it is pulled into
414 -- the server’s main map data cache and then accessed from there.
416 -- Inside a VM transaction, the transaction’s VM cache is used.
418 -- This function can only be used to change the node’s name, not its parameters
419 -- or metadata.
420 function mesecon.swap_node_force(pos, name)
421 if vm_cache then
422 return mesecon.vm_swap_node(pos, name)
423 else
424 -- This serves to both ensure the mapblock is loaded and also hand us
425 -- the old node table so we can preserve param2.
426 local node = mesecon.get_node_force(pos)
427 node.name = name
428 minetest.swap_node(pos, node)
432 -- Autoconnect Hooks
433 -- Nodes like conductors may change their appearance and their connection rules
434 -- right after being placed or after being dug, e.g. the default wires use this
435 -- to automatically connect to linking nodes after placement.
436 -- After placement, the update function will be executed immediately so that the
437 -- possibly changed rules can be taken into account when recalculating the circuit.
438 -- After digging, the update function will be queued and executed after
439 -- recalculating the circuit. The update function must take care of updating the
440 -- node at the given position itself, but also all of the other nodes the given
441 -- position may have (had) a linking connection to.
442 mesecon.autoconnect_hooks = {}
444 -- name: A unique name for the hook, e.g. "foowire". Used to name the actionqueue function.
445 -- fct: The update function with parameters function(pos, node)
446 function mesecon.register_autoconnect_hook(name, fct)
447 mesecon.autoconnect_hooks[name] = fct
448 mesecon.queue:add_function("autoconnect_hook_"..name, fct)
451 function mesecon.execute_autoconnect_hooks_now(pos, node)
452 for _, fct in pairs(mesecon.autoconnect_hooks) do
453 fct(pos, node)
457 function mesecon.execute_autoconnect_hooks_queue(pos, node)
458 for name in pairs(mesecon.autoconnect_hooks) do
459 mesecon.queue:add_action(pos, "autoconnect_hook_"..name, {node})