2 minetest,vector,math,table,pairs,next
4 minetest,vector,math,table,pairs,next
7 local get_node = minetest.get_node
8 local get_item_group = minetest.get_item_group
9 local get_meta = minetest.get_meta
10 local facedir_to_dir = minetest.facedir_to_dir
11 local content_id = minetest.get_name_from_content_id
12 local get_content_id = minetest.get_content_id
13 local get_voxel_manip = minetest.get_voxel_manip
14 local after = minetest.after
16 local swap_node = minetest.swap_node
17 local registered_nodes
18 minetest.register_on_mods_loaded(function()
19 registered_nodes = minetest.registered_nodes
24 local floor = math.floor
27 local new_vec = vector.new
28 local add_vec = vector.add
29 local sub_vec = vector.subtract
30 local vector_distance = vector.distance
32 local activator_table = {} -- this holds the translation data of activator tables (activator functions)
37 -- enables mods to create data functions
38 function redstone.register_activator(data)
39 activator_table[data.name] = {
40 activate = data.activate,
41 deactivate = data.deactivate
45 local path = minetest.get_modpath("redstone")
46 --dofile(path.."/functions.lua")
47 --dofile(path.."/wire.lua")
48 dofile(path.."/torch.lua")
49 dofile(path.."/lever.lua")
50 dofile(path.."/button.lua")
51 dofile(path.."/repeater.lua")
52 dofile(path.."/light.lua")
53 --dofile(path.."/piston.lua")
54 --dofile(path.."/comparator.lua")
55 --dofile(path.."/craft.lua")
56 --dofile(path.."/ore.lua")
57 dofile(path.."/inverter.lua")
58 --dofile(path.."/player_detector.lua")
59 --dofile(path.."/space_maker.lua")
60 --dofile(path.."/pressure_plate.lua")
63 --this is written out manually so that
64 --math.abs is not needed
66 {x=1, y=0, z=0}, {x=-1, y=0, z= 0},
67 {x=0, y=0, z=1}, {x= 0, y=0, z=-1},
69 {x=0, y=1, z=0}, {x= 0, y=-1, z=0},
71 {x=1, y=1, z=0}, {x=-1, y=1, z= 0},
72 {x=0, y=1, z=1}, {x= 0, y=1, z=-1},
74 {x=1, y=-1, z=0}, {x=-1, y=-1, z= 0},
75 {x=0, y=-1, z=1}, {x= 0, y=-1, z=-1},
78 --thanks to RhodiumToad for helping me figure out a good method to do this
80 local pool = {} -- this holds all redstone data (literal 3d virtual memory map)
84 local function create_boundary_box(pos)
86 for x = pos.x-9,pos.x+9 do
88 for y = pos.y-9,pos.y+9 do
90 for z = pos.z-9,pos.z+9 do
91 temp_pool = pool[x][y][z]
93 if not table_3d[x] then table_3d[x] = {} end
94 if not table_3d[x][y] then table_3d[x][y] = {} end
96 if (x == pos.x-9 or x == pos.x+9 or
97 y == pos.y-9 or y == pos.y+9 or
98 z == pos.z-9 or z == pos.z+9) and
99 temp_pool.dust and temp_pool.dust > 1 then
100 table_3d[x][y][z] = {torch=temp_pool.dust}
102 if temp_pool.dust then
103 table_3d[x][y][z] = {dust=0,origin=temp_pool.dust}
105 table_3d[x][y][z] = temp_pool
117 local function data_injection(pos,data)
118 -- add data into 3d memory
120 if not pool[pos.x] then pool[pos.x] = {} end
121 if not pool[pos.x][pos.y] then pool[pos.x][pos.y] = {} end
122 pool[pos.x][pos.y][pos.z] = data
123 --delete data from 3d memory
125 if pool and pool[pos.x] and pool[pos.x][pos.y] then
126 pool[pos.x][pos.y][pos.z] = data
127 if pool[pos.x][pos.y] and not next(pool[pos.x][pos.y]) then
128 pool[pos.x][pos.y] = nil
129 -- only run this if y axis is empty
130 if pool[pos.x] and not next(pool[pos.x]) then
143 local non_directional_activator = function(pos)
144 temp_pool = pool[pos.x][pos.y][pos.z]
145 for _,order in pairs(order) do
146 n_pos = add_vec(pos,order)
147 if pool[n_pos.x] and pool[n_pos.x][n_pos.y] and pool[n_pos.x][n_pos.y][n_pos.z] then
148 temp_pool2 = pool[n_pos.x][n_pos.y][n_pos.z]
150 if (not temp_pool2.directional_activator and temp_pool2.torch) or
151 (temp_pool2.dust and temp_pool2.dust > 0) then
152 if activator_table[temp_pool.name].activate then
153 activator_table[temp_pool.name].activate(pos)
160 if activator_table[temp_pool.name].deactivate then
161 activator_table[temp_pool.name].deactivate(pos)
165 -- directional activators
171 local directional_activator = function(pos)
177 temp_pool = pool[pos.x][pos.y][pos.z]
179 if not temp_pool then ignore = true end
182 input = temp_pool.input
185 if not input then ignore = true end
188 input = temp_pool.input
191 if not ignore and pool and pool[input.x] and pool[input.x][input.y] and pool[input.x][input.y][input.z] then
192 temp_pool2 = pool[input.x][input.y][input.z]
197 if not temp_pool2 then ignore = true end
199 if not ignore and ((temp_pool2.dust and temp_pool2.dust > 0) or (temp_pool2.torch and temp_pool2.directional_activator and temp_pool2.dir == temp_pool.dir) or
200 (not temp_pool2.directional_activator and temp_pool2.torch)) then
201 if activator_table[temp_pool.name].activate then
202 activator_table[temp_pool.name].activate(pos)
208 if activator_table[temp_pool.name].deactivate then
209 activator_table[temp_pool.name].deactivate(pos)
213 --make redstone wire pass on current one level lower than it is
216 local passed_on_level
217 local function redstone_pathfinder(source,source_level,boundary,output)
218 if not source_level then return end
219 --directional torches
222 if i and boundary and boundary[i.x] and boundary[i.x][i.y] and boundary[i.x][i.y][i.z] then
223 index = boundary[i.x][i.y][i.z]
226 passed_on_level = source_level - 1
227 if passed_on_level > 0 then
228 boundary[i.x][i.y][i.z].dust = passed_on_level
229 redstone_pathfinder(i,passed_on_level,boundary,nil)
235 for _,order in pairs(order) do
236 i = add_vec(source,new_vec(order.x,order.y,order.z))
237 if i and boundary and boundary[i.x] and boundary[i.x][i.y] and boundary[i.x][i.y][i.z] then
238 index = boundary[i.x][i.y][i.z]
240 passed_on_level = source_level - 1
241 if passed_on_level > 0 and index.dust < source_level then
242 boundary[i.x][i.y][i.z].dust = passed_on_level
243 redstone_pathfinder(i,passed_on_level,boundary,nil)
255 --make all power sources push power out
260 local function calculate(pos)
261 boundary = create_boundary_box(pos)
262 --pathfind through memory map
263 for x,index_x in pairs(boundary) do
264 for y,index_y in pairs(index_x) do
265 for z,data in pairs(index_y) do
266 --allow data values for torches
267 if data.torch and not data.torch_directional then
268 redstone_pathfinder(new_vec(x,y,z),data.torch,boundary)
269 boundary[x][y][z] = nil
270 elseif data.torch_directional then
271 redstone_pathfinder(new_vec(x,y,z),data.torch,boundary,data.output)
276 --reassemble the table into a position list minetest can understand
277 --run through and set dust
278 for x,datax in pairs(boundary) do
279 for y,datay in pairs(datax) do
280 for z,data in pairs(datay) do
281 if data.dust and data.dust ~= data.origin then
282 swap_node(new_vec(x,y,z),{name="redstone:dust_"..data.dust})
285 --write data back to memory pool
289 --delete the data to speed up next loop
290 boundary[x][y][z] = nil
297 --this must be done after the memory is written
298 for x,datax in pairs(boundary) do
299 for y,datay in pairs(datax) do
300 for z,data in pairs(datay) do
301 if data.directional_activator then
302 directional_activator(new_vec(x,y,z))
303 elseif data.activator then
304 non_directional_activator(new_vec(x,y,z))
312 function redstone.inject(pos,data)
313 data_injection(pos,data)
317 local recursion_check = {}
318 function redstone.update(pos)
319 local s_pos = minetest.serialize(pos)
320 if not recursion_check[s_pos] then
321 recursion_check[s_pos] = 0
323 recursion_check[s_pos] = recursion_check[s_pos] + 1
324 --print(recursion_check[s_pos])
325 if recursion_check[s_pos] > 6 then
326 minetest.after(0,function()
327 minetest.dig_node(pos)
328 data_injection(pos,nil)
337 minetest.register_globalstep(function()
342 ----------------------------------------------------------------------------
376 minetest.register_craftitem("redstone:dust", {
377 description = "Redstone Dust",
378 inventory_image = "redstone_dust_item.png",
379 wield_image = "redstone_dust_item.png",
380 wield_scale = {x = 1, y = 1, z = 1 + 1/16},
381 liquids_pointable = false,
382 on_place = function(itemstack, placer, pointed_thing)
383 if not pointed_thing.type == "node" then
386 local sneak = placer:get_player_control().sneak
387 local noddef = registered_nodes[get_node(pointed_thing.under).name]
388 if not sneak and noddef.on_rightclick then
389 minetest.item_place(itemstack, placer, pointed_thing)
393 local _,worked = minetest.item_place(ItemStack("redstone:dust_0"), placer, pointed_thing)
395 itemstack:take_item()
400 --minetest.add_node(pointed_thing.above, {name="redstone:dust_0"})
401 --itemstack:take_item(1)
402 --minetest.sound_play("stone", {pos=pointed_thing.above})
408 --8 power levels 8 being the highest
411 local coloring = floor(color)
412 minetest.register_node("redstone:dust_"..i,{
413 description = "Redstone Dust",
414 wield_image = "redstone_dust_item.png",
416 "redstone_dust_main.png^[colorize:red:"..coloring, "redstone_turn.png^[colorize:red:"..coloring,
417 "redstone_t.png^[colorize:red:"..coloring, "redstone_cross.png^[colorize:red:"..coloring
420 drawtype = "raillike",
422 sunlight_propagates = true,
423 is_ground_content = false,
425 node_placement_prediction = "",
428 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
430 sounds = main.stoneSound(),
431 groups={dig_immediate=1,attached_node=1,redstone_dust=1,redstone=1,redstone_power=i},
432 drop="redstone:dust",
433 on_construct = function(pos)
434 data_injection(pos,{dust=i})
437 after_destruct = function(pos)
438 data_injection(pos,nil)
441 connects_to = {"group:redstone"},
445 minetest.register_lbm({
446 name = "redstone:"..i,
447 nodenames = {"redstone:dust_"..i},
448 run_at_every_load = true,
449 action = function(pos)
450 data_injection(pos,{dust=i})