sequencer.lua 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. --[[
  2. Tubelib Addons 2
  3. ================
  4. Copyright (C) 2017-2020 Joachim Stolberg
  5. AGPL v3
  6. See LICENSE.txt for more information
  7. sequencer.lua:
  8. ]]--
  9. -- Load support for I18n
  10. local S = tubelib_addons2.S
  11. local RUNNING_STATE = 1
  12. local STOP_STATE = 0
  13. local NUM_SLOTS = 8
  14. local sAction = ",on,off"
  15. local kvAction = {[""]=1, ["on"]=2, ["off"]=3}
  16. local tAction = {nil, "on", "off"}
  17. local function formspec(state, rules, endless)
  18. endless = endless == 1 and "true" or "false"
  19. local tbl = {"size[8,9.2]"..
  20. default.gui_bg..
  21. default.gui_bg_img..
  22. default.gui_slots..
  23. "label[0,0;Number(s)]label[2.1,0;"..S("Command").."]label[6.4,0;Offset/s]"}
  24. for idx, rule in ipairs(rules or {}) do
  25. tbl[#tbl+1] = "field[0.2,"..(-0.2+idx)..";2,1;num"..idx..";;"..(rule.num or "").."]"
  26. tbl[#tbl+1] = "dropdown[2,"..(-0.4+idx)..";3.9,1;act"..idx..";"..sAction..";"..(rule.act or "").."]"
  27. tbl[#tbl+1] = "field[6.2,"..(-0.2+idx)..";2,1;offs"..idx..";;"..(rule.offs or "").."]"
  28. end
  29. tbl[#tbl+1] = "checkbox[0,8.5;endless;"..S("Run endless")..";"..endless.."]"
  30. tbl[#tbl+1] = "image_button[5,8.5;1,1;".. tubelib.state_button(state) ..";button;]"
  31. tbl[#tbl+1] = "button[6.2,8.5;1.5,1;"..S("help")..";help]"
  32. return table.concat(tbl)
  33. end
  34. local function formspec_help()
  35. return "size[8,9.2]"..
  36. default.gui_bg..
  37. default.gui_bg_img..
  38. default.gui_slots..
  39. "label[2,0;"..S("Sequencer Help").."]"..
  40. "label[0,1;"..S("Define a sequence of commands\nto control other machines.").."]"..
  41. "label[0,2.2;"..S("Numbers(s) are the node numbers,\nthe command shall sent to.").."]"..
  42. "label[0,3.4;"..S("The commands 'on'/'off' are used\n for machines and other nodes.").."]"..
  43. "label[0,4.6;"..S("Offset is the time to the\nnext line in seconds (1..999).").."]"..
  44. "label[0,5.8;"..S("If endless is set, the Sequencer\nrestarts again and again.").."]"..
  45. "label[0,7;"..S("The command ' ' does nothing,\nonly consuming the offset time.").."]"..
  46. "button[3,8;2,1;"..S("exit")..";close]"
  47. end
  48. local function stop_the_sequencer(pos)
  49. local node = minetest.get_node(pos)
  50. local meta = minetest.get_meta(pos)
  51. local number = meta:get_string("number")
  52. meta:set_int("running", STOP_STATE)
  53. meta:set_string("infotext", S("Tubelib Sequencer").." "..number..": stopped")
  54. local rules = minetest.deserialize(meta:get_string("rules"))
  55. local endless = meta:get_int("endless") or 0
  56. meta:set_string("formspec", formspec(tubelib.STOPPED, rules, endless))
  57. minetest.get_node_timer(pos):stop()
  58. return false
  59. end
  60. local function get_next_slot(idx, rules, endless)
  61. idx = idx + 1
  62. if idx <= #rules and rules[idx].offs ~= "" and rules[idx].num ~= "" then
  63. return idx
  64. elseif endless == 1 then
  65. return 1
  66. end
  67. return nil
  68. end
  69. local function restart_timer(pos, time)
  70. local timer = minetest.get_node_timer(pos)
  71. if timer:is_started() then
  72. timer:stop()
  73. end
  74. if type(time) == "number" then
  75. timer:start(time)
  76. end
  77. end
  78. local function check_rules(pos, elapsed)
  79. if tubelib.data_not_corrupted(pos) then
  80. local meta = minetest.get_meta(pos)
  81. local rules = minetest.deserialize(meta:get_string("rules"))
  82. if rules then
  83. local running = meta:get_int("running")
  84. local index = meta:get_int("index") or 1
  85. local number = meta:get_string("number")
  86. local endless = meta:get_int("endless") or 0
  87. local placer_name = meta:get_string("placer_name")
  88. while true do -- process all rules as long as offs == 0
  89. local rule = rules[index]
  90. local offs = rules[index].offs
  91. if type(offs) == "string" then
  92. offs = 0
  93. end
  94. tubelib.send_message(rule.num, placer_name, nil, tAction[rule.act], number)
  95. index = get_next_slot(index, rules, endless)
  96. if index ~= nil and offs ~= nil and running == 1 then
  97. -- after the last rule a pause with 1 or more sec is required
  98. if index == 1 and offs < 1 then
  99. offs = 1
  100. end
  101. meta:set_string("infotext", S("Tubelib Sequencer").." "..number..": running ("..index.."/"..NUM_SLOTS..")")
  102. meta:set_int("index", index)
  103. if offs > 0 then
  104. minetest.after(0, restart_timer, pos, offs)
  105. return false
  106. end
  107. else
  108. return stop_the_sequencer(pos)
  109. end
  110. end
  111. end
  112. return false
  113. end
  114. return false
  115. end
  116. local function start_the_sequencer(pos)
  117. local node = minetest.get_node(pos)
  118. local meta = minetest.get_meta(pos)
  119. local number = meta:get_string("number")
  120. meta:set_int("running", 1)
  121. meta:set_int("index", 1)
  122. meta:set_string("infotext", S("Tubelib Sequencer").." "..number..": running (1/"..NUM_SLOTS..")")
  123. local rules = minetest.deserialize(meta:get_string("rules"))
  124. local endless = meta:get_int("endless") or 0
  125. meta:set_string("formspec", formspec(tubelib.RUNNING, rules, endless))
  126. minetest.get_node_timer(pos):start(0.1)
  127. return false
  128. end
  129. local function on_receive_fields(pos, formname, fields, player)
  130. if tubelib.data_not_corrupted(pos) then
  131. local meta = minetest.get_meta(pos)
  132. local running = meta:get_int("running")
  133. if minetest.is_protected(pos, player:get_player_name()) then
  134. return
  135. end
  136. if fields.help ~= nil then
  137. meta:set_string("formspec", formspec_help())
  138. return
  139. end
  140. local endless = meta:get_int("endless") or 0
  141. if fields.endless ~= nil then
  142. endless = fields.endless == "true" and 1 or 0
  143. meta:set_int("index", 1)
  144. end
  145. meta:set_int("endless", endless)
  146. local rules = minetest.deserialize(meta:get_string("rules"))
  147. if fields.exit ~= nil then
  148. meta:set_string("formspec", formspec(tubelib.state(running), rules, endless))
  149. return
  150. end
  151. for idx = 1,NUM_SLOTS do
  152. if fields["offs"..idx] ~= nil then
  153. rules[idx].offs = tonumber(fields["offs"..idx]) or ""
  154. end
  155. if fields["num"..idx] ~= nil and tubelib.check_numbers(fields["num"..idx]) then
  156. rules[idx].num = fields["num"..idx]
  157. end
  158. if fields["act"..idx] ~= nil then
  159. rules[idx].act = kvAction[fields["act"..idx]]
  160. end
  161. end
  162. meta:set_string("rules", minetest.serialize(rules))
  163. if fields.button ~= nil then
  164. if running > STOP_STATE then
  165. stop_the_sequencer(pos)
  166. else
  167. start_the_sequencer(pos)
  168. end
  169. elseif fields.num1 ~= nil then -- any other change?
  170. stop_the_sequencer(pos)
  171. else
  172. local endless = meta:get_int("endless") or 0
  173. meta:set_string("formspec", formspec(tubelib.state(running), rules, endless))
  174. end
  175. end
  176. end
  177. minetest.register_node("tubelib_addons2:sequencer", {
  178. description = S("Tubelib Sequencer"),
  179. tiles = {
  180. -- up, down, right, left, back, front
  181. 'tubelib_front.png',
  182. 'tubelib_front.png',
  183. 'tubelib_front.png^tubelib_addons2_sequencer.png',
  184. },
  185. after_place_node = function(pos, placer)
  186. local meta = minetest.get_meta(pos)
  187. local number = tubelib.add_node(pos, "tubelib_addons2:sequencer")
  188. local rules = {}
  189. for idx = 1,NUM_SLOTS do
  190. rules[idx] = {offs = "", num = "", act = 1}
  191. end
  192. meta:set_string("placer_name", placer:get_player_name())
  193. meta:set_string("rules", minetest.serialize(rules))
  194. meta:set_string("number", number)
  195. meta:set_int("index", 1)
  196. meta:set_int("endless", 0)
  197. meta:get_int("running", STOP_STATE)
  198. meta:set_string("formspec", formspec(tubelib.STOPPED, rules, 0))
  199. meta:set_string("infotext", "Tubelib Sequencer "..number)
  200. end,
  201. on_receive_fields = on_receive_fields,
  202. on_dig = function(pos, node, puncher, pointed_thing)
  203. if minetest.is_protected(pos, puncher:get_player_name()) then
  204. return
  205. end
  206. local meta = minetest.get_meta(pos)
  207. local running = meta:get_int("running")
  208. if running ~= 1 then
  209. minetest.node_dig(pos, node, puncher, pointed_thing)
  210. tubelib.remove_node(pos)
  211. end
  212. end,
  213. on_timer = check_rules,
  214. paramtype = "light",
  215. sunlight_propagates = true,
  216. paramtype2 = "facedir",
  217. groups = {choppy=2, cracky=2, crumbly=2},
  218. is_ground_content = false,
  219. sounds = default.node_sound_stone_defaults(),
  220. })
  221. minetest.register_craft({
  222. output = "tubelib_addons2:sequencer",
  223. recipe = {
  224. {"group:wood", "group:wood", ""},
  225. {"default:mese_crystal", "tubelib:wlanchip", ""},
  226. {"group:wood", "group:wood", ""},
  227. },
  228. })
  229. tubelib.register_node("tubelib_addons2:sequencer", {}, {
  230. on_recv_message = function(pos, topic, payload)
  231. local node = minetest.get_node(pos)
  232. if topic == "on" then
  233. start_the_sequencer(pos)
  234. elseif topic == "off" then
  235. -- do not stop immediately
  236. local meta = minetest.get_meta(pos)
  237. meta:set_int("endless", 0)
  238. end
  239. end,
  240. on_node_load = function(pos)
  241. local meta = minetest.get_meta(pos)
  242. if meta:get_int("running") ~= STOP_STATE then
  243. meta:set_int("running", RUNNING_STATE)
  244. minetest.get_node_timer(pos):start(1)
  245. end
  246. end,
  247. })