From 3dcaa839626d284ad17e51489b449d8f8a1ce739 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Mon, 28 Aug 2017 01:51:21 +0200 Subject: [PATCH] Add redstone comparators by ex-bart --- .../ITEMS/REDSTONE/mcl_comparator/depends.txt | 2 + mods/ITEMS/REDSTONE/mcl_comparator/init.lua | 318 ++++++++++++++++++ .../textures/mcl_comparator_comp.png | Bin 0 -> 102 bytes .../textures/mcl_comparator_ends_comp.png | Bin 0 -> 145 bytes .../textures/mcl_comparator_ends_off.png | Bin 0 -> 167 bytes .../textures/mcl_comparator_ends_on.png | Bin 0 -> 167 bytes .../textures/mcl_comparator_ends_sub.png | Bin 0 -> 155 bytes .../textures/mcl_comparator_item.png | Bin 0 -> 278 bytes .../textures/mcl_comparator_off.png | Bin 0 -> 233 bytes .../textures/mcl_comparator_on.png | Bin 0 -> 562 bytes .../textures/mcl_comparator_sides_comp.png | Bin 0 -> 140 bytes .../textures/mcl_comparator_sides_off.png | Bin 0 -> 167 bytes .../textures/mcl_comparator_sides_on.png | Bin 0 -> 167 bytes .../textures/mcl_comparator_sides_sub.png | Bin 0 -> 154 bytes .../textures/mcl_comparator_sub.png | Bin 0 -> 102 bytes 15 files changed, 320 insertions(+) create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/depends.txt create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/init.lua create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_comp.png create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_ends_comp.png create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_ends_off.png create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_ends_on.png create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_ends_sub.png create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_item.png create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_off.png create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_on.png create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_sides_comp.png create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_sides_off.png create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_sides_on.png create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_sides_sub.png create mode 100644 mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_sub.png diff --git a/mods/ITEMS/REDSTONE/mcl_comparator/depends.txt b/mods/ITEMS/REDSTONE/mcl_comparator/depends.txt new file mode 100644 index 000000000..4fdbda79f --- /dev/null +++ b/mods/ITEMS/REDSTONE/mcl_comparator/depends.txt @@ -0,0 +1,2 @@ +mesecons +doc? diff --git a/mods/ITEMS/REDSTONE/mcl_comparator/init.lua b/mods/ITEMS/REDSTONE/mcl_comparator/init.lua new file mode 100644 index 000000000..b0f6fed33 --- /dev/null +++ b/mods/ITEMS/REDSTONE/mcl_comparator/init.lua @@ -0,0 +1,318 @@ +-- Functions that get the input/output rules of the comparator + +local comparator_get_output_rules = function(node) + local rules = {{x = -1, y = 0, z = 0}} + for i = 0, node.param2 do + rules = mesecon:rotate_rules_left(rules) + end + return rules +end + + +local comparator_get_input_rules = function(node) + local rules = { + -- we rely on this order in update_self below + {x = 1, y = 0, z = 0}, -- back + {x = 0, y = 0, z = -1}, -- side + {x = 0, y = 0, z = 1}, -- side + } + for i = 0, node.param2 do + rules = mesecon:rotate_rules_left(rules) + end + return rules +end + + +-- Functions that are called after the delay time + +local comparator_turnon = function(params) + local rules = comparator_get_output_rules(params.node) + mesecon:receptor_on(params.pos, rules) +end + + +local comparator_turnoff = function(params) + local rules = comparator_get_output_rules(params.node) + mesecon:receptor_off(params.pos, rules) +end + + +-- Functions that set the correct node type an schedule a turnon/off + +local comparator_activate = function(pos, node) + local def = minetest.registered_nodes[node.name] + mesecon:swap_node(pos, def.comparator_onstate) + minetest.after(0.1, comparator_turnon , {pos = pos, node = node}) +end + + +local comparator_deactivate = function(pos, node) + local def = minetest.registered_nodes[node.name] + mesecon:swap_node(pos, def.comparator_offstate) + minetest.after(0.1, comparator_turnoff, {pos = pos, node = node}) +end + + +-- wether pos has an inventory that contains at least one item +local container_inventory_nonempty = function(pos) + local invnode = minetest.get_node(pos) + local invnodedef = minetest.registered_nodes[invnode.name] + -- Ignore stale nodes + if not invnodedef then return false end + + -- Only accept containers. When a container is dug, it's inventory + -- seems to stay. and we don't want to accept the inventory of an air + -- block + if not invnodedef.groups.container then return false end + + local inv = minetest.get_inventory({type="node", pos=pos}) + if not inv then return false end + + for listname, _ in pairs(inv:get_lists()) do + if not inv:is_empty(listname) then return true end + end + + return false +end + +-- whether the comparator should be on according to its inputs +local comparator_desired_on = function(pos, node) + local my_input_rules = comparator_get_input_rules(node); + local back_rule = my_input_rules[1] + local state = mesecon:is_powered_from(pos, back_rule) + or container_inventory_nonempty(vector.add(pos, back_rule)) + + -- if back input if off, we don't need to check side inputs + if not state then return false end + + -- without power levels, side inputs have no influence on output in compare + -- mode + local mode = minetest.registered_nodes[node.name].comparator_mode + if mode == "comp" then return state end + + -- subtract mode, subtract max(side_inputs) from back input + local side_state = false + for ri = 2,3 do + side_state = side_state or mesecon:is_powered_from(pos, my_input_rules[ri]) + if side_state then break end + end + -- state is known to be true + return not side_state +end + + +-- update comparator state, if needed +local update_self = function(pos, node) + node = node or minetest.get_node(pos) + local old_state = mesecon:is_receptor_on(node.name) + local new_state = comparator_desired_on(pos, node) + if new_state ~= old_state then + if new_state then + comparator_activate(pos, node) + else + comparator_deactivate(pos, node) + end + end +end + + +-- compute tile depending on state and mode +local get_tiles = function(state, mode) + local top = "mcl_comparator_"..state..".png^".. + "mcl_comparator_"..mode..".png" + local sides = "mcl_comparator_sides_"..state..".png^".. + "mcl_comparator_sides_"..mode..".png" + local ends = "mcl_comparator_ends_"..state..".png^".. + "mcl_comparator_ends_"..mode..".png" + return { + top, "mcl_stairs_stone_slab_top.png", + sides, sides.."^[transformFX", + ends, ends, + } +end + +-- Given one mode, get the other mode +local flipmode = function(mode) + if mode == "comp" then return "sub" + elseif mode == "sub" then return "comp" + end +end + +local make_rightclick_handler = function(state, mode) + local newnodename = + "mcl_comparator:comparator_"..state.."_"..flipmode(mode) + return function (pos, node) + mesecon:swap_node(pos,newnodename) + end +end + + +-- Register the 2 (states) x 2 (modes) comparators + +local longdesc = "Redstone comparators are redstone components which ".. + "compare redstone signals and measure various node states, such as ".. + "how full inventories are." +local usagehelp = "To power a redstone comparater, send a signal in “arrow” ".. + "direction, or place the block to measure there. Send the signal ".. + "to compare with in from the side." +local icon = "mcl_comparator_item.png" + +local node_boxes = { + comp = { + { -8/16, -8/16, -8/16, + 8/16, -6/16, 8/16 }, -- the main slab + { -1/16, -6/16, 6/16, + 1/16, -4/16, 4/16 }, -- front torch + { -4/16, -6/16, -5/16, + -2/16, -1/16, -3/16 }, -- left back torch + { 2/16, -6/16, -5/16, + 4/16, -1/16, -3/16 }, -- right back torch + }, + sub = { + { -8/16, -8/16, -8/16, + 8/16, -6/16, 8/16 }, -- the main slab + { -1/16, -6/16, 6/16, + 1/16, -3/16, 4/16 }, -- front torch (active) + { -4/16, -6/16, -5/16, + -2/16, -1/16, -3/16 }, -- left back torch + { 2/16, -6/16, -5/16, + 4/16, -1/16, -3/16 }, -- right back torch + }, +} + +local collision_box = { + type = "fixed", + fixed = { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 }, +} + +local state_strs = { + [ mesecon.state.on ] = "on", + [ mesecon.state.off ] = "off", +} + +local groups = { + dig_immediate = 3, + dig_by_water = 1, + destroy_by_lava_flow = 1, + dig_by_piston = 1, + attached_node = 1, +} + +for _, mode in pairs{"comp", "sub"} do +for _, state in pairs{mesecon.state.on, mesecon.state.off} do + local state_str = state_strs[state] + local nodename = + "mcl_comparator:comparator_"..state_strs[state].."_"..mode + + local nodedef = { + description = "Redstone Comparator", + inventory_image = icon, + wield_image = icon, + _doc_items_longdesc = longdesc, + _doc_items_usagehelp = usagehelp, + drawtype = "nodebox", + tiles = get_tiles(state_strs[state], mode), + wield_image = "mcl_comparator_off.png", + walkable = true, + selection_box = collision_box, + collision_box = collision_box, + node_box = { + type = "fixed", + fixed = node_boxes[mode], + }, + groups = groups, + paramtype = "light", + paramtype2 = "facedir", + sunlight_propagates = false, + is_ground_content = false, + drop = 'mcl_comparator:comparator_off_comp', + on_construct = update_self, + on_rightclick = + make_rightclick_handler(state_strs[state], mode), + comparator_mode = mode, + comparator_onstate = "mcl_comparator:comparator_on_"..mode, + comparator_offstate = "mcl_comparator:comparator_off_"..mode, + sounds = mcl_sounds.node_sound_stone_defaults(), + mesecons = { + receptor = { + state = state, + rules = comparator_get_output_rules, + }, + effector = { + rules = comparator_get_input_rules, + action_change = update_self, + } + } + } + + if mode == "comp" and state == mesecon.state.off then + -- This is the prototype + nodedef._doc_items_create_entry = true + else + nodedef.groups = table.copy(nodedef.groups) + nodedef.groups.not_in_creative_inventory = 1 + local extra_desc = {} + if mode == "sub" then + table.insert(extra_desc, "Subtract") + end + if state == mesecon.state.on then + table.insert(extra_desc, "Powered") + end + nodedef.description = nodedef.description.. + " ("..table.concat(extra_desc, ", ")..")" + end + + minetest.register_node(nodename, nodedef) +end +end + +-- Register recipies +local rstorch = "mesecons_torch:mesecon_torch_on" +local quartz = "mcl_nether:quartz" +local stone = "mcl_core:stone" + +minetest.register_craft({ + output = "mcl_comparator:comparator_off_comp", + recipe = { + { "", rstorch, "" }, + { rstorch, quartz, rstorch }, + { stone, stone, stone }, + } +}) + +-- Register active block handlers +minetest.register_abm({ + label = "Comparator check for containers", + nodenames = { + "mcl_comparator:comparator_off_comp", + "mcl_comparator:comparator_off_sub", + }, + neighbors = {"group:container"}, + interval = 1, + chance = 1, + action = update_self, +}) + +minetest.register_abm({ + label = "Comparator check for no containers", + nodenames = { + "mcl_comparator:comparator_on_comp", + "mcl_comparator:comparator_on_sub", + }, + -- needs to run regardless of neighbors to make sure we detect when a + -- container is dug + interval = 1, + chance = 1, + action = update_self, +}) + + +-- Add entry aliases for the Help +if minetest.get_modpath("doc") then + doc.add_entry_alias("nodes", "mcl_comparator:comparator_off_comp", + "nodes", "mcl_comparator:comparator_off_sub") + doc.add_entry_alias("nodes", "mcl_comparator:comparator_off_comp", + "nodes", "mcl_comparator:comparator_on_comp") + doc.add_entry_alias("nodes", "mcl_comparator:comparator_off_comp", + "nodes", "mcl_comparator:comparator_on_sub") +end diff --git a/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_comp.png b/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_comp.png new file mode 100644 index 0000000000000000000000000000000000000000..9967cf5c15b877cd99bf08fda4b69b7e221707eb GIT binary patch literal 102 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4kiW$h6xih%orFL*h+%@f*HUdZyEbU1_lOc zPZ!6K3dUp!g#<+v7MCTNA0|#*xN^4JK@A3m@9c~-A1&Hw2~zLr>gTe~DWM4f)+!iD literal 0 HcmV?d00001 diff --git a/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_ends_comp.png b/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_ends_comp.png new file mode 100644 index 0000000000000000000000000000000000000000..3b8e7477bd6d8b117b9d0962e1eb9fe647a0ca11 GIT binary patch literal 145 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd7G?$phPQVgfdr%jd_r6q7#MtHWGsY))OdIz z{oLas!|O^43v$vXcQ)VQds@T5z`$4%Y{ thK>Tpp!bqL?Kc>lZlM^`tGyIVn_iJpgZtf!&?qbqmOrG|{6bYmIDohO1EjxuaMW%TRPs}*h_ O3q4)^T-G@yGywqelrMh( literal 0 HcmV?d00001 diff --git a/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_ends_on.png b/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_ends_on.png new file mode 100644 index 0000000000000000000000000000000000000000..33505b3830a784eba1c9ca6ca33de0cd1812b4c7 GIT binary patch literal 167 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4kiW$h6xih%orFL*h+%@f*HUdZyEbU1_p*0 zPZ!6K3dUp!wgizcD_4nmdOZ?3$l|cm#L#e#(u@U42emI86bWXT-xlF7Jx6JVg6#>` z47obNHV^x9j;?MVcejE<6Fm>-SWiU(Mpy2lOAQ%C>BcgQJ5K~T9A#*7G|fsAwCe&{ N=;`X`vd$@?2>|`#Eph+= literal 0 HcmV?d00001 diff --git a/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_ends_sub.png b/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_ends_sub.png new file mode 100644 index 0000000000000000000000000000000000000000..f0697465dfaaef26925c8a96a34f6dd0d2304700 GIT binary patch literal 155 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd7G?$phPQVgfdmu-d_r6q7#R9gRf{Ag)5OFo z61)N(Oe6i=<08ZBN(u{d(k6E{AD7N8XJBApED7=pW^j0RBaMN9LBrF#Yy z2!oK1LL0-BgG(45EoJ=qUh=2?27{B_oJM=9GZ+~3PqVV~&OVvQz`(%Z>FVdQ&MBb@ E0IX3fzyJUM literal 0 HcmV?d00001 diff --git a/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_item.png b/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_item.png new file mode 100644 index 0000000000000000000000000000000000000000..7febe579c1cc5ea4f34d434e6c89a9e3de6d2a5e GIT binary patch literal 278 zcmV+x0qOpUP)e73=COLM{8hNgn@#NjEGD(B`qNrU_~!DDIK1i zmv?k?O+h*`vJCwI000PdQchC<|Nj60|Nj8&sd~Zy004eTL_t(|0d!1PdcYtUa%J(m z_5Noq*uIuT9B7Ha3B+Dx46Qp9TtRPbzUl)IY5bLd8mI9-4-Lp9oG~-VzoU5V1SUVo zHIX3R_|_%ug3O=%A=be>+_oFVFl4*c_6}5WyQUmc@6$Z+6{`LO%M)q!4MK9dOs`(f j$d!eK2sFy#ztH*zIBgrCw|=kT00000NkvXXu0mjfV!>P# literal 0 HcmV?d00001 diff --git a/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_on.png b/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_on.png new file mode 100644 index 0000000000000000000000000000000000000000..c2e250d50c4bdc6f43235626f3e9883e4614ae88 GIT binary patch literal 562 zcmV-20?qx2P)Px$>`6pHR4C7FlEG^lK@`TnY}{mZb)&mZ7KADlB8$)j1M#5VOQ7^#3WZ#HX#caG zdMM-{NOB1YNe+@~2q;>z8E3n3cU`S%?BU7KlzxZd8+g3=z3_gG?0DkipCl-2+f{6+olea-8zj`Dq+RLij@XZofx_ifI+Xc(%Q} z2f)yDx8G~jcR2Sr_v~V++iBB~+q-*qeiMKqW3ls_-A?<>{(j#bCDZzN8Ue72B`S!+ zD4kpfpa38@=nWgq*0;k$0ED-Eu-OvAmq97ie$X2_PMLFWt5S`_h#my6RjG3BIZjy! zpQ2Z2LBVuF4G5a5<&|?!k|*Sj33{tiWz3|ldC7IgTGWW7>e+;Z12FmWbpqVsU%mYF z5kMHvo~2}HIsj*5sxhnPsu);fxtNT zC`8N3eh(w^a>#k)ue^KvT7G8vljo67BT6sz2k6oBU=LhM8~^|S07*qoM6N<$f;0#P AQ2+n{ literal 0 HcmV?d00001 diff --git a/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_sides_comp.png b/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_sides_comp.png new file mode 100644 index 0000000000000000000000000000000000000000..536d61bf937ebe0ee8296470734a78f5caad8ef1 GIT binary patch literal 140 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4kiW$h6xih%orFL*h+%@f*HUdZyEbU1_lO4 zPZ!6K3dZCG>JqGr54gJ>bMok5;dQv0Wn&ZIw`9IG%Td=hW#t_fQsPS%u3I9|(Xfcs mGSk6i=Vr;ihL{x11xXB1X=+N_zZ{eSnd<54=d#Wzp$Pz`g(g-2 literal 0 HcmV?d00001 diff --git a/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_sides_off.png b/mods/ITEMS/REDSTONE/mcl_comparator/textures/mcl_comparator_sides_off.png new file mode 100644 index 0000000000000000000000000000000000000000..ba70c3efe99f1358e26b47d3468271d1f7148305 GIT binary patch literal 167 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd7G?$phPQVgfdte8d_r6q7#MtHWGsY))OdI* z61)N(Ok+K5(t}(g{oLas!|O^43v$vXcQ$LANh>liFff(``2{mLJiC#`z`&sA>Eakt z!I-?jE+K>=*oUWwDS%Dbr-f1N&>zM{TbNfovsM1jE~Zwg=-5`y&&v>G$7bLg*we+p Pz`)??>gTe~DWM4fm=Hs4f_->;m;%^@eOegR4*g+Vw1s)aGh5~V>|$z_ijHmN{Jac7c5DW|fjwOe P3=9mOu6{1-oD!M-aR(DcMW