init.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. function register_gate(name, inputnumber, assess, recipe)
  48. local get_inputrules = inputnumber == 2 and gate_get_input_rules_twoinputs or
  49. gate_get_input_rules_oneinput
  50. local description = "Mesecons 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. drawtype = "nodebox",
  58. drop = basename.."_off",
  59. selection_box = nodebox,
  60. node_box = nodebox,
  61. walkable = true,
  62. sounds = default.node_sound_stone_defaults(),
  63. assess = assess,
  64. onstate = basename.."_on",
  65. offstate = basename.."_off",
  66. inputnumber = inputnumber
  67. },{
  68. tiles = {"jeija_microcontroller_bottom.png^".."jeija_gate_off.png^"..
  69. "jeija_gate_"..name..".png"},
  70. groups = {dig_immediate = 2, overheat = 1},
  71. mesecons = { receptor = {
  72. state = "off",
  73. rules = gate_get_output_rules
  74. }, effector = {
  75. rules = get_inputrules,
  76. action_change = update_gate
  77. }}
  78. },{
  79. tiles = {"jeija_microcontroller_bottom.png^".."jeija_gate_on.png^"..
  80. "jeija_gate_"..name..".png"},
  81. groups = {dig_immediate = 2, not_in_creative_inventory = 1, overheat = 1},
  82. mesecons = { receptor = {
  83. state = "on",
  84. rules = gate_get_output_rules
  85. }, effector = {
  86. rules = get_inputrules,
  87. action_change = update_gate
  88. }}
  89. })
  90. minetest.register_craft({output = basename.."_off", recipe = recipe})
  91. end
  92. register_gate("diode", 1, function (input) return input end,
  93. {{"mesecons:mesecon", "mesecons_torch:mesecon_torch_on", "mesecons_torch:mesecon_torch_on"}})
  94. register_gate("not", 1, function (input) return not input end,
  95. {{"mesecons:mesecon", "mesecons_torch:mesecon_torch_on", "mesecons:mesecon"}})
  96. register_gate("and", 2, function (val1, val2) return val1 and val2 end,
  97. {{"mesecons:mesecon", "", ""},
  98. {"", "mesecons_materials:silicon", "mesecons:mesecon"},
  99. {"mesecons:mesecon", "", ""}})
  100. register_gate("nand", 2, function (val1, val2) return not (val1 and val2) end,
  101. {{"mesecons:mesecon", "", ""},
  102. {"", "mesecons_materials:silicon", "mesecons_torch:mesecon_torch_on"},
  103. {"mesecons:mesecon", "", ""}})
  104. register_gate("xor", 2, function (val1, val2) return (val1 or val2) and not (val1 and val2) end,
  105. {{"mesecons:mesecon", "", ""},
  106. {"", "mesecons_materials:silicon", "mesecons_materials:silicon"},
  107. {"mesecons:mesecon", "", ""}})
  108. register_gate("nor", 2, function (val1, val2) return not (val1 or val2) end,
  109. {{"mesecons:mesecon", "", ""},
  110. {"", "mesecons:mesecon", "mesecons_torch:mesecon_torch_on"},
  111. {"mesecons:mesecon", "", ""}})
  112. register_gate("or", 2, function (val1, val2) return (val1 or val2) end,
  113. {{"mesecons:mesecon", "", ""},
  114. {"", "mesecons:mesecon", "mesecons:mesecon"},
  115. {"mesecons:mesecon", "", ""}})