init.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. -- |\ /| ____ ____ ____ _____ ____ _____
  2. -- | \ / | | | | | | | |\ | |
  3. -- | \/ | |___ ____ |___ | | | | \ | |____
  4. -- | | | | | | | | | \ | |
  5. -- | | |___ ____| |___ |____ |____| | \| ____|
  6. -- by Jeija, Uberi (Temperest), sfan5, VanessaE, Hawk777 and contributors
  7. --
  8. --
  9. --
  10. -- This mod adds mesecons[=minecraft redstone] and different receptors/effectors to minetest.
  11. -- See the documentation on the forum for additional information, especially about crafting
  12. --
  13. --
  14. -- For basic development resources, see http://mesecons.net/developers.html
  15. --
  16. --
  17. --
  18. --Quick draft for the mesecons array in the node's definition
  19. --mesecons =
  20. --{
  21. -- receptor =
  22. -- {
  23. -- state = mesecon.state.on/off
  24. -- rules = rules/get_rules
  25. -- },
  26. -- effector =
  27. -- {
  28. -- action_on = function
  29. -- action_off = function
  30. -- action_change = function
  31. -- rules = rules/get_rules
  32. -- },
  33. -- conductor =
  34. -- {
  35. -- state = mesecon.state.on/off
  36. -- offstate = opposite state (for state = on only)
  37. -- onstate = opposite state (for state = off only)
  38. -- rules = rules/get_rules
  39. -- }
  40. --}
  41. -- PUBLIC VARIABLES
  42. mesecon={} -- contains all functions and all global variables
  43. mesecon.queue={} -- contains the ActionQueue
  44. mesecon.queue.funcs={} -- contains all ActionQueue functions
  45. -- Settings
  46. dofile(minetest.get_modpath("mesecons").."/settings.lua")
  47. -- Utilities like comparing positions,
  48. -- adding positions and rules,
  49. -- mostly things that make the source look cleaner
  50. dofile(minetest.get_modpath("mesecons").."/util.lua");
  51. -- Presets (eg default rules)
  52. dofile(minetest.get_modpath("mesecons").."/presets.lua");
  53. -- The ActionQueue
  54. -- Saves all the actions that have to be execute in the future
  55. dofile(minetest.get_modpath("mesecons").."/actionqueue.lua");
  56. -- Internal stuff
  57. -- This is the most important file
  58. -- it handles signal transmission and basically everything else
  59. -- It is also responsible for managing the nodedef things,
  60. -- like calling action_on/off/change
  61. dofile(minetest.get_modpath("mesecons").."/internal.lua");
  62. -- API
  63. -- these are the only functions you need to remember
  64. mesecon.queue:add_function("receptor_on", function (pos, rules)
  65. mesecon.vm_begin()
  66. rules = rules or mesecon.rules.default
  67. -- Call turnon on all linking positions
  68. for _, rule in ipairs(mesecon.flattenrules(rules)) do
  69. local np = vector.add(pos, rule)
  70. local rulenames = mesecon.rules_link_rule_all(pos, rule)
  71. for _, rulename in ipairs(rulenames) do
  72. mesecon.turnon(np, rulename)
  73. end
  74. end
  75. mesecon.vm_commit()
  76. end)
  77. function mesecon.receptor_on(pos, rules)
  78. mesecon.queue:add_action(pos, "receptor_on", {rules}, nil, rules)
  79. end
  80. mesecon.queue:add_function("receptor_off", function (pos, rules)
  81. rules = rules or mesecon.rules.default
  82. -- Call turnoff on all linking positions
  83. for _, rule in ipairs(mesecon.flattenrules(rules)) do
  84. local np = vector.add(pos, rule)
  85. local rulenames = mesecon.rules_link_rule_all(pos, rule)
  86. for _, rulename in ipairs(rulenames) do
  87. mesecon.vm_begin()
  88. mesecon.changesignal(np, minetest.get_node(np), rulename, mesecon.state.off, 2)
  89. -- Turnoff returns true if turnoff process was successful, no onstate receptor
  90. -- was found along the way. Commit changes that were made in voxelmanip. If turnoff
  91. -- returns true, an onstate receptor was found, abort voxelmanip transaction.
  92. if (mesecon.turnoff(np, rulename)) then
  93. mesecon.vm_commit()
  94. else
  95. mesecon.vm_abort()
  96. end
  97. end
  98. end
  99. end)
  100. function mesecon.receptor_off(pos, rules)
  101. mesecon.queue:add_action(pos, "receptor_off", {rules}, nil, rules)
  102. end
  103. print("[OK] Mesecons")
  104. -- Deprecated stuff
  105. -- To be removed in future releases
  106. dofile(minetest.get_modpath("mesecons").."/legacy.lua");
  107. --Services like turnoff receptor on dignode and so on
  108. dofile(minetest.get_modpath("mesecons").."/services.lua");