init.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. local nodebox = {
  2. type = "fixed",
  3. fixed = {{-8/16, -8/16, -8/16, 8/16, -7/16, 8/16 }},
  4. }
  5. local function gate_rotate_rules(node, rules)
  6. for rotations = 0, node.param2 - 1 do
  7. rules = mesecon.rotate_rules_left(rules)
  8. end
  9. return rules
  10. end
  11. local function gate_get_output_rules(node)
  12. return gate_rotate_rules(node, {{x=1, y=0, z=0}})
  13. end
  14. local function gate_get_input_rules_oneinput(node)
  15. return gate_rotate_rules(node, {{x=-1, y=0, z=0}})
  16. end
  17. local function gate_get_input_rules_twoinputs(node)
  18. return gate_rotate_rules(node, {{x=0, y=0, z=1, name="input1"},
  19. {x=0, y=0, z=-1, name="input2"}})
  20. end
  21. local function set_gate(pos, node, state)
  22. local gate = minetest.registered_nodes[node.name]
  23. if mesecon.do_overheat(pos) then
  24. minetest.remove_node(pos)
  25. mesecon.receptor_off(pos, gate_get_output_rules(node))
  26. minetest.add_item(pos, gate.drop)
  27. elseif state then
  28. minetest.swap_node(pos, {name = gate.onstate, param2=node.param2})
  29. mesecon.receptor_on(pos, gate_get_output_rules(node))
  30. else
  31. minetest.swap_node(pos, {name = gate.offstate, param2=node.param2})
  32. mesecon.receptor_off(pos, gate_get_output_rules(node))
  33. end
  34. end
  35. local function update_gate(pos, node, link, newstate)
  36. local gate = minetest.registered_nodes[node.name]
  37. if gate.inputnumber == 1 then
  38. set_gate(pos, node, gate.assess(newstate == "on"))
  39. elseif gate.inputnumber == 2 then
  40. local meta = minetest.get_meta(pos)
  41. meta:set_int(link.name, newstate == "on" and 1 or 0)
  42. local val1 = meta:get_int("input1") == 1
  43. local val2 = meta:get_int("input2") == 1
  44. set_gate(pos, node, gate.assess(val1, val2))
  45. end
  46. end
  47. local function register_gate(name, inputnumber, assess, recipe, description)
  48. local get_inputrules = inputnumber == 2 and gate_get_input_rules_twoinputs or
  49. gate_get_input_rules_oneinput
  50. description = "Logic Gate: "..name
  51. local basename = "mesecons_gates:"..name
  52. mesecon.register_node(basename, {
  53. description = description,
  54. inventory_image = "jeija_gate_off.png^jeija_gate_"..name..".png",
  55. paramtype = "light",
  56. paramtype2 = "facedir",
  57. is_ground_content = false,
  58. drawtype = "nodebox",
  59. drop = basename.."_off",
  60. selection_box = nodebox,
  61. node_box = nodebox,
  62. walkable = true,
  63. sounds = default.node_sound_stone_defaults(),
  64. assess = assess,
  65. onstate = basename.."_on",
  66. offstate = basename.."_off",
  67. inputnumber = inputnumber,
  68. after_dig_node = mesecon.do_cooldown,
  69. },{
  70. tiles = {"jeija_microcontroller_bottom.png^".."jeija_gate_off.png^"..
  71. "jeija_gate_"..name..".png"},
  72. groups = {dig_immediate = 2, overheat = 1},
  73. mesecons = { receptor = {
  74. state = "off",
  75. rules = gate_get_output_rules
  76. }, effector = {
  77. rules = get_inputrules,
  78. action_change = update_gate
  79. }}
  80. },{
  81. tiles = {"jeija_microcontroller_bottom.png^".."jeija_gate_on.png^"..
  82. "jeija_gate_"..name..".png"},
  83. groups = {dig_immediate = 2, not_in_creative_inventory = 1, overheat = 1},
  84. mesecons = { receptor = {
  85. state = "on",
  86. rules = gate_get_output_rules
  87. }, effector = {
  88. rules = get_inputrules,
  89. action_change = update_gate
  90. }}
  91. })
  92. minetest.register_craft({output = basename.."_off", recipe = recipe})
  93. end
  94. register_gate("diode", 1, function (input) return input end,
  95. {{"mesecons:mesecon", "mesecons_torch:mesecon_torch_on", "mesecons_torch:mesecon_torch_on"}},
  96. "Diode")
  97. register_gate("not", 1, function (input) return not input end,
  98. {{"mesecons:mesecon", "mesecons_torch:mesecon_torch_on", "mesecons:mesecon"}},
  99. "NOT Gate")
  100. register_gate("and", 2, function (val1, val2) return val1 and val2 end,
  101. {{"mesecons:mesecon", "", ""},
  102. {"", "mesecons_materials:silicon", "mesecons:mesecon"},
  103. {"mesecons:mesecon", "", ""}},
  104. "AND Gate")
  105. register_gate("nand", 2, function (val1, val2) return not (val1 and val2) end,
  106. {{"mesecons:mesecon", "", ""},
  107. {"", "mesecons_materials:silicon", "mesecons_torch:mesecon_torch_on"},
  108. {"mesecons:mesecon", "", ""}},
  109. "NAND Gate")
  110. register_gate("xor", 2, function (val1, val2) return (val1 or val2) and not (val1 and val2) end,
  111. {{"mesecons:mesecon", "", ""},
  112. {"", "mesecons_materials:silicon", "mesecons_materials:silicon"},
  113. {"mesecons:mesecon", "", ""}},
  114. "XOR Gate")
  115. register_gate("nor", 2, function (val1, val2) return not (val1 or val2) end,
  116. {{"mesecons:mesecon", "", ""},
  117. {"", "mesecons:mesecon", "mesecons_torch:mesecon_torch_on"},
  118. {"mesecons:mesecon", "", ""}},
  119. "NOR Gate")
  120. register_gate("or", 2, function (val1, val2) return (val1 or val2) end,
  121. {{"mesecons:mesecon", "", ""},
  122. {"", "mesecons:mesecon", "mesecons:mesecon"},
  123. {"mesecons:mesecon", "", ""}},
  124. "OR Gate")