init.lua 5.1 KB

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