1 --if you attempt to read this, god bless you
4 minetest,vector,math,table,pairs,next
6 minetest,vector,math,table,pairs,next
9 local get_node = minetest.get_node
10 local get_item_group = minetest.get_item_group
11 local get_meta = minetest.get_meta
12 local facedir_to_dir = minetest.facedir_to_dir
13 local content_id = minetest.get_name_from_content_id
14 local get_content_id = minetest.get_content_id
15 local get_voxel_manip = minetest.get_voxel_manip
16 local after = minetest.after
18 local swap_node = minetest.swap_node
19 local registered_nodes
20 minetest.register_on_mods_loaded(function()
21 registered_nodes = minetest.registered_nodes
26 local floor = math.floor
27 local ceil = math.ceil
30 local new_vec = vector.new
31 local add_vec = vector.add
32 local sub_vec = vector.subtract
33 local vector_distance = vector.distance
34 local vec_equals = vector.equals
36 local activator_table = {} -- this holds the translation data of activator tables (activator functions)
37 local capacitor_table = {}
38 local player_detection_table = {}
39 --local instructions = 0
44 redstone.max_state = 9 -- the limit to power transmission
46 redstone.player_detector_add = function(pos)
47 player_detection_table[minetest.serialize(pos)] = pos
50 redstone.player_detector_remove = function(pos)
51 player_detection_table[minetest.serialize(pos)] = nil
55 -- enables mods to create data functions
56 function redstone.register_activator(data)
57 activator_table[data.name] = {
58 activate = data.activate,
59 deactivate = data.deactivate
63 -- enables mods to create data functions
64 function redstone.register_capacitor(data)
65 capacitor_table[data.name] = {
71 local path = minetest.get_modpath("redstone")
72 dofile(path.."/functions.lua")
73 dofile(path.."/torch.lua")
74 dofile(path.."/lever.lua")
75 dofile(path.."/button.lua")
76 dofile(path.."/repeater.lua")
77 dofile(path.."/light.lua")
78 dofile(path.."/piston.lua")
79 --dofile(path.."/comparator.lua")
80 dofile(path.."/craft.lua")
81 dofile(path.."/ore.lua")
82 dofile(path.."/inverter.lua")
83 dofile(path.."/player_detector.lua")
84 dofile(path.."/space_maker.lua")
85 --dofile(path.."/pressure_plate.lua")
86 dofile(path.."/capacitors.lua")
87 dofile(path.."/breaker.lua")
88 dofile(path.."/detector.lua")
91 --this is written out manually so that
92 --math.abs is not needed
111 --thanks to RhodiumToad for helping me figure out a good method to do this
113 local pool = {} -- this holds all redstone data (literal 3d virtual memory map)
116 local function data_injection(pos,data)
117 --instructions = instructions + 1
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 --instructions = instructions + 1
124 --print("building 3d memory")
125 --delete data from 3d memory
127 if pool and pool[pos.x] and pool[pos.x][pos.y] then
128 pool[pos.x][pos.y][pos.z] = data
129 --instructions = instructions + 1
130 --print("deleting 3d memory")
131 if pool[pos.x][pos.y] and not next(pool[pos.x][pos.y]) then
132 pool[pos.x][pos.y] = nil
133 --instructions = instructions + 1
134 -- only run this if y axis is empty
135 if pool[pos.x] and not next(pool[pos.x]) then
137 --instructions = instructions + 1
147 ___ \ / ___ ,'\_ | .-. \ /|
148 \ / | |,'__ \ ,'\_ | \ | | | | ,' |_ /|
149 _ | | | |\/ \ \ | \ | |\_| _ | |_| | _ '-. .-',' |_ _
150 // | | | |____| | | |\_|| |__ // | | ,'_`. | | '-. .-',' `. ,'\_
151 \\_| |_,' .-, _ | | | | |\ \ // .| |\_/ | / \ || | | | / |\ \| \
152 `-. .-'| |/ / | | | | | | \ \// | | | | | || | | | | |_\ || |\_|
153 | | | || \_| | | | /_\ \ / | |` | | | || | | | | .---'| |
154 | | | |\___,_\ /_\ _ // | | | \_/ || | | | | | /\| |
155 /_\ | | //_____// .||` _ `._,' | | | | \ `-' /| |
156 /_\ `------' \ | /-\ND _ `.\ | | `._,' /_\
159 / |__| /_\ |\ /| |_) |_ |_)
160 \__ | | / \ | \/ | |_) |__ | \
170 local r_max = redstone.max_state
171 local function create_boundary_box(pos)
172 --instructions = instructions + 1
174 for x = pos.x-r_max,pos.x+r_max do
175 --instructions = instructions + 1
177 for y = pos.y-r_max,pos.y+r_max do
178 --instructions = instructions + 1
180 for z = pos.z-r_max,pos.z+r_max do
181 --instructions = instructions + 1
182 temp_pool = pool[x][y][z]
183 --instructions = instructions + 1
185 if not table_3d[x] then table_3d[x] = {} end
186 if not table_3d[x][y] then table_3d[x][y] = {} end
188 if (x == pos.x-r_max or x == pos.x+r_max or
189 y == pos.y-r_max or y == pos.y+r_max or
190 z == pos.z-r_max or z == pos.z+r_max) and
191 temp_pool.dust and temp_pool.dust > 1 then
192 table_3d[x][y][z] = {torch=temp_pool.dust}
194 if temp_pool.dust then
195 table_3d[x][y][z] = {dust=0,origin=temp_pool.dust}
197 table_3d[x][y][z] = temp_pool
211 local function capacitor_pathfind(source,mem_map)
212 for _,order in pairs(order) do
214 i = add_vec(source,order)
215 if not mem_map[i.x] then mem_map[i.x] = {} end
216 if not mem_map[i.x][i.y] then mem_map[i.x][i.y] = {} end
218 if not mem_map[i.x][i.y][i.z] then
220 if i and pool and pool[i.x] and pool[i.x][i.y] and pool[i.x][i.y][i.z] then
221 index = pool[i.x][i.y][i.z]
222 if index.capacitor then
223 mem_map[i.x][i.y][i.z] = {name = index.name, capacitor = 0, source = index.source}
227 capacitor_pathfind(i,mem_map)
238 local function capacitor_sniff(pos)
240 table_3d = capacitor_pathfind(pos,table_3d)
241 found = table_3d.found
244 for x,datax in pairs(table_3d) do
245 for y,datay in pairs(datax) do
246 for z,data in pairs(datay) do
247 temp_pool = pool[x][y][z]
249 temp_pool.capacitor = 1
250 if capacitor_table[temp_pool.name] then
251 swap_node(new_vec(x,y,z),{name=capacitor_table[temp_pool.name].on})
252 redstone.update(new_vec(x,y,z))
259 for x,datax in pairs(table_3d) do
260 for y,datay in pairs(datax) do
261 for z,data in pairs(datay) do
262 temp_pool = pool[x][y][z]
264 temp_pool.capacitor = 0
265 if capacitor_table[temp_pool.name] then
266 swap_node(new_vec(x,y,z),{name=capacitor_table[temp_pool.name].off})
267 redstone.update(new_vec(x,y,z))
282 local non_directional_activator = function(pos)
283 if pool[pos.x] and pool[pos.x][pos.y] and pool[pos.x][pos.y][pos.z] then
284 temp_pool = pool[pos.x][pos.y][pos.z]
286 for _,order in pairs(order) do
287 n_pos = add_vec(pos,order)
288 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
289 temp_pool2 = pool[n_pos.x][n_pos.y][n_pos.z]
291 if (not temp_pool2.directional_activator and temp_pool2.torch) or
292 (temp_pool2.dust and temp_pool2.dust > 0) or
293 (temp_pool2.torch_directional and vector.equals(temp_pool2.output, pos)) then
294 if activator_table[temp_pool.name] and activator_table[temp_pool.name].activate then
295 activator_table[temp_pool.name].activate(pos)
302 if activator_table[temp_pool.name] and activator_table[temp_pool.name].deactivate then
303 activator_table[temp_pool.name].deactivate(pos)
309 -- directional activators
315 local directional_activator = function(pos)
322 if not (pool[pos.x] and pool[pos.x][pos.y] and pool[pos.x][pos.y][pos.z]) then return end
324 temp_pool = pool[pos.x][pos.y][pos.z]
326 if not temp_pool then ignore = true end
329 input = temp_pool.input
332 if not input then ignore = true end
335 input = temp_pool.input
338 if not ignore and pool and pool[input.x] and pool[input.x][input.y] and pool[input.x][input.y][input.z] then
339 temp_pool2 = pool[input.x][input.y][input.z]
344 if not temp_pool2 then ignore = true end
346 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
347 (not temp_pool2.directional_activator and temp_pool2.torch) or (temp_pool2.capacitor and temp_pool2.capacitor > 0)) then
348 if activator_table[temp_pool.name] and activator_table[temp_pool.name].activate then
349 activator_table[temp_pool.name].activate(pos)
355 if activator_table[temp_pool.name] and activator_table[temp_pool.name].deactivate then
356 activator_table[temp_pool.name].deactivate(pos)
360 --make redstone wire pass on current one level lower than it is
363 local passed_on_level
365 local function redstone_distribute(pos,power,mem_map,output)
369 --directional torches
374 if mem_map.dust[x] and mem_map.dust[x][y] and mem_map.dust[x][y][z] then
375 if mem_map.dust[x][y][z].dust < power then
376 mem_map.dust[x][y][z].dust = power
377 redstone_distribute(new_vec(x,y,z),power,mem_map,nil)
382 for _,order in pairs(order) do
383 --instructions = instructions + 1
384 i = add_vec(pos,order)
388 if mem_map.dust[x] and mem_map.dust[x][y] and mem_map.dust[x][y][z] then
389 if mem_map.dust[x][y][z].dust < power then
390 mem_map.dust[x][y][z].dust = power
391 redstone_distribute(new_vec(x,y,z),power,mem_map,nil)
410 _,' _o\ ,::.`:' ; ; . '
414 `.______,-,----._ ,' ;: ,/ , ,`
415 / /,-';' \ ; `: ,'/,::.:::
416 ,',;-'-'_,--; ; :. ,',',;::::::
417 ( /___,-' `. ;::,,'o/ ,:::::::
426 local function dust_sniff(pos,mem_map,boundary,single,origin,ignore)
428 --print("all position index--")
429 for _,order in pairs(order) do
430 --instructions = instructions + 1
431 i = add_vec(pos,order)
433 if not mem_map[i.x] then mem_map[i.x] = {} end
434 if not mem_map[i.x][i.y] then mem_map[i.x][i.y] = {} end
436 if not mem_map[i.x][i.y][i.z] then
437 if i and boundary and boundary[i.x] and boundary[i.x][i.y] and boundary[i.x][i.y][i.z] then
438 index = boundary[i.x][i.y][i.z]
442 mem_map[i.x][i.y][i.z] = true
444 if not mem_map.dust[i.x] then mem_map.dust[i.x] = {} end
445 if not mem_map.dust[i.x][i.y] then mem_map.dust[i.x][i.y] = {} end
447 mem_map.dust[i.x][i.y][i.z] = index
449 dust_sniff(i,mem_map,boundary)
451 elseif index.torch and index.torch > 1 then
452 if index.torch_directional and vec_equals(pos,index.output) then
454 mem_map[i.x][i.y][i.z] = true
456 if not mem_map.torch[i.x] then mem_map.torch[i.x] = {} end
457 if not mem_map.torch[i.x][i.y] then mem_map.torch[i.x][i.y] = {} end
459 mem_map.torch[i.x][i.y][i.z] = index
462 elseif not index.torch_directional then
464 mem_map[i.x][i.y][i.z] = true
466 if not mem_map.torch[i.x] then mem_map.torch[i.x] = {} end
467 if not mem_map.torch[i.x][i.y] then mem_map.torch[i.x][i.y] = {} end
469 mem_map.torch[i.x][i.y][i.z] = index
473 if index.activator then
474 mem_map[i.x][i.y][i.z] = true
476 if not mem_map.activator[i.x] then mem_map.activator[i.x] = {} end
477 if not mem_map.activator[i.x][i.y] then mem_map.activator[i.x][i.y] = {} end
479 mem_map.activator[i.x][i.y][i.z] = index
480 elseif index.directional_activator and vec_equals(pos,index.input) then
482 mem_map[i.x][i.y][i.z] = true
484 if not mem_map.activator[i.x] then mem_map.activator[i.x] = {} end
485 if not mem_map.activator[i.x][i.y] then mem_map.activator[i.x][i.y] = {} end
487 mem_map.activator[i.x][i.y][i.z] = index
493 --print("single position index")
498 if not mem_map[i.x] then mem_map[i.x] = {} end
499 if not mem_map[i.x][i.y] then mem_map[i.x][i.y] = {} end
501 if not mem_map[i.x][i.y][i.z] then
502 if i and boundary and boundary[i.x] and boundary[i.x][i.y] and boundary[i.x][i.y][i.z] then
503 index = boundary[i.x][i.y][i.z]
506 mem_map[i.x][i.y][i.z] = true
508 if not mem_map.dust[i.x] then mem_map.dust[i.x] = {} end
509 if not mem_map.dust[i.x][i.y] then mem_map.dust[i.x][i.y] = {} end
511 mem_map.dust[i.x][i.y][i.z] = index
513 dust_sniff(i,mem_map,boundary)
515 elseif index.torch and index.torch > 1 then
516 if index.torch_directional and (vec_equals(origin,index.output) or ignore) then
518 mem_map[i.x][i.y][i.z] = true
520 if not mem_map.torch[i.x] then mem_map.torch[i.x] = {} end
521 if not mem_map.torch[i.x][i.y] then mem_map.torch[i.x][i.y] = {} end
523 mem_map.torch[i.x][i.y][i.z] = index
526 elseif not index.torch_directional then
528 mem_map[i.x][i.y][i.z] = true
530 if not mem_map.torch[i.x] then mem_map.torch[i.x] = {} end
531 if not mem_map.torch[i.x][i.y] then mem_map.torch[i.x][i.y] = {} end
533 mem_map.torch[i.x][i.y][i.z] = index
537 if index.activator then
538 mem_map[i.x][i.y][i.z] = true
540 if not mem_map.activator[i.x] then mem_map.activator[i.x] = {} end
541 if not mem_map.activator[i.x][i.y] then mem_map.activator[i.x][i.y] = {} end
543 mem_map.activator[i.x][i.y][i.z] = index
544 elseif index.directional_activator and (vec_equals(origin,index.input) or ignore) then
546 mem_map[i.x][i.y][i.z] = true
548 if not mem_map.activator[i.x] then mem_map.activator[i.x] = {} end
549 if not mem_map.activator[i.x][i.y] then mem_map.activator[i.x][i.y] = {} end
551 mem_map.activator[i.x][i.y][i.z] = index
559 --make all power sources push power out
569 local function calculate(pos,is_capacitor)
570 if not is_capacitor then
571 boundary = create_boundary_box(pos)
576 dust_map.activator = {}
578 dust_detected = false
582 if boundary[pos.x] and boundary[pos.x][pos.y] and boundary[pos.x][pos.y][pos.z] then
583 if boundary[pos.x][pos.y][pos.z].torch_directional or boundary[pos.x][pos.y][pos.z].directional_activator then
588 -- sniff all possible dust within boundaries
589 if not directional then
590 dust_sniff(pos,dust_map,boundary)
591 for _,pos2 in pairs(order) do
592 pos3 = add_vec(pos,pos2)
593 if boundary[pos3.x] and boundary[pos3.x][pos3.y] and boundary[pos3.x][pos3.y][pos3.z] and
594 not (dust_map[pos3.x] and dust_map[pos3.x][pos3.y] and dust_map[pos3.x][pos3.y][pos3.z]) then
595 temp_pool3 = boundary[pos3.x][pos3.y][pos3.z]
596 if temp_pool3.dust then
597 dust_sniff(pos3,dust_map,boundary)
602 dust_sniff(pos,dust_map,boundary,true,pos,true)
604 local input = boundary[pos.x][pos.y][pos.z].input
605 local output = boundary[pos.x][pos.y][pos.z].output
607 if input and boundary[input.x] and boundary[input.x][input.y] and boundary[input.x][input.y][input.z] then
608 dust_sniff(input,dust_map,boundary,true,pos)
610 if output and boundary[output.x] and boundary[output.x][output.y] and boundary[output.x][output.y][output.z] then
611 dust_sniff(output,dust_map,boundary,true,pos)
615 for x,datax in pairs(dust_map.torch) do
616 for y,datay in pairs(datax) do
617 for z,data in pairs(datay) do
619 if data.torch_directional then
620 redstone_distribute(new_vec(x,y,z),data.torch,dust_map,data.output)
622 redstone_distribute(new_vec(x,y,z),data.torch,dust_map)
629 --set dust, set pool memory
630 for x,datax in pairs(dust_map.dust) do
631 for y,datay in pairs(datax) do
632 for z,data in pairs(datay) do
633 if data.dust and data.dust ~= data.origin then
634 swap_node(new_vec(x,y,z),{name="redstone:dust_"..data.dust})
635 data_injection(new_vec(x,y,z),data)
642 --this must be run at the end
643 for x,datax in pairs(dust_map.activator) do
644 for y,datay in pairs(datax) do
645 for z,data in pairs(datay) do
646 if data.directional_activator then
647 directional_activator(new_vec(x,y,z))
648 elseif data.activator then
649 non_directional_activator(new_vec(x,y,z))
660 function redstone.inject(pos,data)
661 data_injection(pos,data)
670 local function player_detector_calculation()
671 for _,pos in pairs(player_detection_table) do
672 level = pool[pos.x][pos.y][pos.z].torch
674 for _,player in ipairs(minetest.get_connected_players()) do
675 pos2 = player:get_pos()
676 power = floor(11-vector_distance(pos2,pos))
677 if power > r_max then
679 elseif power < 0 then
689 swap_node(pos,{name="redstone:player_detector_"..max})
690 redstone.inject(pos,{
691 name = "redstone:player_detector_"..max,
700 local recursion_check = {}
703 function redstone.update(pos,is_capacitor)
704 local count = table.getn(queue)
705 local s_pos = minetest.serialize(pos)
706 if not recursion_check[s_pos] then
707 recursion_check[s_pos] = 0
709 recursion_check[s_pos] = recursion_check[s_pos] + 1
710 if recursion_check[s_pos] > 25 then
711 --print(recursion_check[s_pos])
712 minetest.after(0,function()
713 bad_node = minetest.get_node(pos).name
714 bad_node = minetest.get_node_drops(bad_node, "main:rubypick")
715 for _,nodey in pairs(bad_node) do
716 minetest.throw_item(pos,nodey)
718 minetest.remove_node(pos)
719 data_injection(pos,nil)
724 calculate(pos,is_capacitor)
726 local dtime_goal = 0.02
728 minetest.register_globalstep(function(dtime)
729 player_detector_calculation()
732 if dtime > dtime_goal then
733 sleep = dtime - dtime_goal
738 for index,element in pairs(queue) do
739 calculate(element.pos,element.is_capacitor)
745 sleep = sleep - dtime
751 --if instructions and instructions > 0 then
752 -- print(instructions)
758 ----------------------------------------------------------------------------
762 local instruction_order = {
780 -- this is used for dynamic instruction set rebuilds
781 local function instruction_rebuild(pos,delete)
783 local instruction_set = {}
784 for _,pos2 in pairs(instruction_order) do
785 local pos3 = vector.add(pos,pos2)
786 if pool[pos3.x] and pool[pos3.x][pos3.y] and pool[pos3.x][pos3.y][pos3.z] then
787 table.insert(instruction_set,pos2)
791 for _,pos2 in pairs(instruction_order) do
796 print(dump(instruction_set))
801 -- this is used for creating fast data for the game to utilize
802 local function initial_instruction_build(pos)
803 local instruction_set = {}
804 for _,pos2 in pairs(instruction_order) do
805 local pos3 = vector.add(pos,pos2)
806 if pool[pos3.x] and pool[pos3.x][pos3.y] and pool[pos3.x][pos3.y][pos3.z] then
807 table.insert(instruction_set,pos2)
810 pool[pos.x][pos.y][pos.z].instruction_set = instruction_set
833 minetest.register_craftitem("redstone:dust", {
834 description = "Redstone Dust",
835 inventory_image = "redstone_dust_item.png",
836 wield_image = "redstone_dust_item.png",
837 wield_scale = {x = 1, y = 1, z = 1 + 1/16},
838 liquids_pointable = false,
839 on_place = function(itemstack, placer, pointed_thing)
840 if not pointed_thing.type == "node" then
843 local sneak = placer:get_player_control().sneak
844 local noddef = registered_nodes[get_node(pointed_thing.under).name]
845 if not sneak and noddef.on_rightclick then
846 minetest.item_place(itemstack, placer, pointed_thing)
850 local _,worked = minetest.item_place(ItemStack("redstone:dust_0"), placer, pointed_thing)
852 itemstack:take_item()
858 --power levels r_max-1 being the highest
859 local d_max = r_max-1
862 local color = floor(255 * (i/d_max))
864 minetest.register_node("redstone:dust_"..i,{
865 description = "Redstone Dust",
866 wield_image = "redstone_dust_item.png",
868 "redstone_dust_main.png^[colorize:red:"..color, "redstone_turn.png^[colorize:red:"..color,
869 "redstone_t.png^[colorize:red:"..color, "redstone_cross.png^[colorize:red:"..color
872 drawtype = "raillike",
874 sunlight_propagates = true,
875 is_ground_content = false,
877 node_placement_prediction = "",
880 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
882 sounds = main.stoneSound(),
883 groups={dig_immediate=1,attached_node=1,redstone_dust=1,redstone=1,redstone_power=i},
884 drop="redstone:dust",
885 on_construct = function(pos)
886 data_injection(pos,{dust=i})
887 --instruction_rebuild(pos)
890 after_destruct = function(pos)
891 data_injection(pos,nil)
892 --instruction_rebuild(pos,true)
895 connects_to = {"group:redstone"},
898 minetest.register_lbm({
899 name = "redstone:"..i,
900 nodenames = {"redstone:dust_"..i},
901 run_at_every_load = true,
902 action = function(pos)
903 data_injection(pos,{dust=i})
904 --minetest.after(0,function()
905 --initial_instruction_build(pos)