charger.lua 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. charger = charger or {}
  2. charger.modpath = minetest.get_modpath("machines")
  3. -- Localize for performance.
  4. local math_random = math.random
  5. local BUFFER_SIZE = tech.charger.buffer
  6. local ENERGY_AMOUNT = tech.charger.power
  7. charger.compose_formspec =
  8. function(pos)
  9. local formspec =
  10. "size[8,4.5]" ..
  11. default.gui_bg ..
  12. default.gui_bg_img ..
  13. default.gui_slots ..
  14. "label[1.0,0.5;Upgrade]" ..
  15. "list[context;upg;1.0,1;1,1]" ..
  16. "label[3.5,0.5;Energy Buffer]" ..
  17. "list[context;buffer;3.5,1;1,1]" ..
  18. "label[6.0,0.5;Tool Recharge]" ..
  19. "list[context;main;6.0,1;1,1]" ..
  20. "list[current_player;main;0,3.5;8,1;]" ..
  21. "listring[context;main]" ..
  22. "listring[current_player;main]" ..
  23. default.get_hotbar_bg(0, 3.5)
  24. return formspec
  25. end
  26. charger.on_receive_fields =
  27. function(pos, formname, fields, sender)
  28. end
  29. charger.compose_infotext =
  30. function(pos)
  31. local meta = minetest.get_meta(pos)
  32. local state = "Standby"
  33. local eups = 0
  34. if meta:get_int("active") == 1 then
  35. state = "Active"
  36. eups = ENERGY_AMOUNT
  37. end
  38. local infotext = "LV Charging Device (" .. state .. ")\n" ..
  39. "Demand: " .. eups .. " EU Per/Sec"
  40. return infotext
  41. end
  42. charger.trigger_update =
  43. function(pos)
  44. local timer = minetest.get_node_timer(pos)
  45. -- Restart timer even if already running.
  46. timer:start(1.0)
  47. end
  48. charger.on_punch =
  49. function(pos, node, puncher, pointed_thing)
  50. charger.trigger_update(pos)
  51. local meta = minetest.get_meta(pos)
  52. local inv = meta:get_inventory()
  53. inv:set_size("upg", 1)
  54. end
  55. charger.can_dig =
  56. function(pos, player)
  57. local meta = minetest.get_meta(pos)
  58. local inv = meta:get_inventory()
  59. return inv:is_empty("main") and inv:is_empty("upg")
  60. end
  61. charger.on_timer =
  62. function(pos, elapsed)
  63. --minetest.chat_send_all("# Server: Elapsed time is " .. elapsed .. "!")
  64. local keeprunning = false
  65. local meta = minetest.get_meta(pos)
  66. local inv = meta:get_inventory()
  67. local the_tool -- Set to tool's itemstack if we have one.
  68. local got_energy = false
  69. local energy_gotten = 0
  70. -- Assuming we can keep running unless someone says otherwise.
  71. keeprunning = true
  72. do
  73. local tool = inv:get_stack("main", 1)
  74. if tool:get_count() == 1 then
  75. local name = tool:get_name()
  76. local def = minetest.registered_items[name]
  77. if def and def.wear_represents and def.wear_represents == "eu_charge" then
  78. local wear = tool:get_wear()
  79. if wear == 1 then
  80. -- Tool fully charged.
  81. keeprunning = false
  82. goto cancel
  83. elseif wear > 1 then
  84. -- We got a tool and it needs recharging.
  85. the_tool = tool
  86. else
  87. -- Tool has never been charged before (wear is 0).
  88. assert(wear == 0)
  89. wear = 65534
  90. tool:set_wear(wear)
  91. inv:set_stack("main", 1, tool)
  92. -- Wait for next iteration before beginning charge.
  93. goto cancel
  94. end
  95. else
  96. -- Unknown item.
  97. keeprunning = false
  98. goto cancel
  99. end
  100. else
  101. -- No tool.
  102. keeprunning = false
  103. goto cancel
  104. end
  105. end
  106. -- Consume energy (but only if there is a tool to charge).
  107. if the_tool then
  108. local energy = inv:get_stack("buffer", 1)
  109. if energy:get_count() >= ENERGY_AMOUNT then
  110. energy:set_count(energy:get_count() - ENERGY_AMOUNT)
  111. inv:set_stack("buffer", 1, energy)
  112. -- We have enough energy.
  113. energy_gotten = ENERGY_AMOUNT
  114. got_energy = true
  115. else
  116. -- Try to get energy from network.
  117. local owner = meta:get_string("owner")
  118. local gotten = net2.get_energy(pos, owner, BUFFER_SIZE, "lv")
  119. if gotten >= ENERGY_AMOUNT then
  120. energy = ItemStack("atomic:energy " .. (energy:get_count() + gotten))
  121. inv:set_stack("buffer", 1, energy)
  122. -- Wait for next iteration before producing again.
  123. goto cancel
  124. end
  125. -- Not enough energy!
  126. keeprunning = false
  127. goto cancel
  128. end
  129. end
  130. -- Charge the tool.
  131. if the_tool and got_energy then
  132. local wear = the_tool:get_wear()
  133. wear = wear - energy_gotten
  134. if wear < 1 then
  135. wear = 1
  136. end
  137. the_tool:set_wear(wear)
  138. inv:set_stack("main", 1, the_tool)
  139. end
  140. -- Jump here if something prevents machine from working.
  141. ::cancel::
  142. -- Determine mode (active or sleep) and set timer accordingly.
  143. if keeprunning then
  144. minetest.get_node_timer(pos):start(1.0)
  145. meta:set_int("active", 1)
  146. else
  147. -- Slow down timer during sleep periods to reduce load.
  148. minetest.get_node_timer(pos):start(math_random(1, 3*60))
  149. meta:set_int("active", 0)
  150. end
  151. -- Update infotext.
  152. meta:set_string("formspec", charger.compose_formspec(pos))
  153. meta:set_string("infotext", charger.compose_infotext(pos))
  154. end
  155. charger.on_construct =
  156. function(pos)
  157. end
  158. charger.after_place_node =
  159. function(pos, placer, itemstack, pointed_thing)
  160. local meta = minetest.get_meta(pos)
  161. local node = minetest.get_node(pos)
  162. local owner = placer:get_player_name()
  163. local inv = meta:get_inventory()
  164. meta:set_string("owner", owner)
  165. meta:set_string("nodename", node.name)
  166. inv:set_size("buffer", 1)
  167. inv:set_size("main", 1)
  168. inv:set_size("upg", 1)
  169. net2.clear_caches(pos, owner, "lv")
  170. meta:set_string("formspec", charger.compose_formspec(pos))
  171. meta:set_string("infotext", charger.compose_infotext(pos))
  172. nodestore.add_node(pos)
  173. local timer = minetest.get_node_timer(pos)
  174. timer:start(1.0)
  175. end
  176. charger.on_blast =
  177. function(pos)
  178. local drops = {}
  179. drops[#drops+1] = "charger:charger"
  180. default.get_inventory_drops(pos, "main", drops)
  181. minetest.remove_node(pos)
  182. return drops
  183. end
  184. charger.has_public_access =
  185. function(pos)
  186. local meta = minetest.get_meta(pos)
  187. local inv = meta:get_inventory()
  188. -- There is only 1 upgrade slot.
  189. local s1 = inv:get_stack("upg", 1)
  190. if s1 and s1:get_count() > 0 then
  191. if minetest.get_item_group(s1:get_name(), "chest") > 0 then
  192. return true
  193. end
  194. end
  195. end
  196. charger.allow_metadata_inventory_put =
  197. function(pos, listname, index, stack, player)
  198. local pname = player:get_player_name()
  199. local public = charger.has_public_access(pos)
  200. local protected = false
  201. if minetest.test_protection(pos, pname) then
  202. protected = true
  203. end
  204. if listname == "main" and (not protected or public) then
  205. local def = minetest.registered_items[stack:get_name()]
  206. if def and def.wear_represents and def.wear_represents == "eu_charge" then
  207. return 1
  208. end
  209. elseif listname == "upg" and not protected then
  210. if minetest.get_item_group(stack:get_name(), "chest") > 0 then
  211. return stack:get_count()
  212. elseif stack:get_name() == "battery:battery" then
  213. return stack:get_count()
  214. elseif stack:get_name() == "techcrafts:control_logic_unit" then
  215. return stack:get_count()
  216. end
  217. end
  218. return 0
  219. end
  220. charger.allow_metadata_inventory_move =
  221. function(pos, from_list, from_index, to_list, to_index, count, player)
  222. local pname = player:get_player_name()
  223. local public = charger.has_public_access(pos)
  224. local protected = false
  225. if minetest.test_protection(pos, pname) then
  226. protected = true
  227. end
  228. if (from_list == "upg" or to_list == "upg") and protected then
  229. -- Don't permit public users to mess with the upgrades.
  230. return 0
  231. end
  232. if not protected or public then
  233. -- Can't touch the output energy buffer.
  234. if from_list == "buffer" or to_list == "buffer" then
  235. return 0
  236. end
  237. if from_list == to_list then
  238. return count
  239. end
  240. end
  241. return 0
  242. end
  243. charger.allow_metadata_inventory_take =
  244. function(pos, listname, index, stack, player)
  245. local pname = player:get_player_name()
  246. local public = charger.has_public_access(pos)
  247. local protected = false
  248. if minetest.test_protection(pos, pname) then
  249. protected = true
  250. end
  251. if listname == "main" and (not protected or public) then
  252. return stack:get_count()
  253. elseif listname == "upg" and not protected then
  254. return stack:get_count()
  255. end
  256. return 0
  257. end
  258. charger.on_metadata_inventory_move =
  259. function(pos)
  260. charger.trigger_update(pos)
  261. end
  262. charger.on_metadata_inventory_put =
  263. function(pos)
  264. charger.trigger_update(pos)
  265. end
  266. charger.on_metadata_inventory_take =
  267. function(pos, listname, index, stack, player)
  268. charger.trigger_update(pos)
  269. end
  270. charger.on_destruct =
  271. function(pos)
  272. local meta = minetest.get_meta(pos)
  273. net2.clear_caches(pos, meta:get_string("owner"), "lv")
  274. nodestore.del_node(pos)
  275. end
  276. if not charger.run_once then
  277. minetest.register_node(":charger:charger", {
  278. description = "LV Charging Device\n\nA machine for recharging energy tools.\nConnects to a power-network.",
  279. tiles = {
  280. "charger_top.png",
  281. "charger_top.png",
  282. "charger_side.png",
  283. "charger_side.png",
  284. "charger_side.png",
  285. "charger_side.png",
  286. },
  287. groups = utility.dig_groups("machine"),
  288. paramtype2 = "facedir",
  289. is_ground_content = false,
  290. sounds = default.node_sound_metal_defaults(),
  291. drop = "charger:charger",
  292. on_rotate = function(...)
  293. return screwdriver.rotate_simple(...) end,
  294. allow_metadata_inventory_put = function(...)
  295. return charger.allow_metadata_inventory_put(...) end,
  296. allow_metadata_inventory_move = function(...)
  297. return charger.allow_metadata_inventory_move(...) end,
  298. allow_metadata_inventory_take = function(...)
  299. return charger.allow_metadata_inventory_take(...) end,
  300. on_metadata_inventory_move = function(...)
  301. return charger.on_metadata_inventory_move(...) end,
  302. on_metadata_inventory_put = function(...)
  303. return charger.on_metadata_inventory_put(...) end,
  304. on_metadata_inventory_take = function(...)
  305. return charger.on_metadata_inventory_take(...) end,
  306. on_punch = function(...)
  307. return charger.on_punch(...) end,
  308. can_dig = function(...)
  309. return charger.can_dig(...) end,
  310. on_timer = function(...)
  311. return charger.on_timer(...) end,
  312. on_construct = function(...)
  313. return charger.on_construct(...) end,
  314. on_destruct = function(...)
  315. return charger.on_destruct(...) end,
  316. on_blast = function(...)
  317. return charger.on_blast(...) end,
  318. after_place_node = function(...)
  319. return charger.after_place_node(...) end,
  320. on_receive_fields = function(...)
  321. return charger.on_receive_fields(...) end,
  322. })
  323. ---[[
  324. minetest.register_craft({
  325. output = "charger:charger",
  326. recipe = {
  327. {"techcrafts:control_logic_unit"},
  328. {"fine_wire:silver"},
  329. {"bat2:bt0_lv"},
  330. },
  331. })
  332. --]]
  333. local c = "charger:core"
  334. local f = charger.modpath .. "/charger.lua"
  335. reload.register_file(c, f, false)
  336. charger.run_once = true
  337. end