functions.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. -- Localize for performance.
  2. local math_floor = math.floor
  3. -- Typedata is used when traversing the network, without touching the node.
  4. -- It must contain as much data as needed to get the node even if unloaded.
  5. -- This must be done after node construction.
  6. -- This should also be done when punched, to allow old nodes to be upgraded.
  7. generator.initialize_typedata =
  8. function(pos)
  9. local meta = minetest.get_meta(pos)
  10. meta:set_string("technic_machine", "yes")
  11. meta:set_string("technic_type", "generator")
  12. meta:set_string("technic_tier", "lv|mv")
  13. -- The active nodetype should have all same properties and functions.
  14. meta:set_string("technic_name", "generator:inactive")
  15. end
  16. generator.on_punch =
  17. function(pos, node, puncher, pointed_thing)
  18. generator.initialize_typedata(pos)
  19. generator.trigger_update(pos)
  20. end
  21. generator.compose_formspec =
  22. function(fuel_percent)
  23. local formspec =
  24. "size[8,8.5]" ..
  25. default.formspec.get_form_colors() ..
  26. default.formspec.get_form_image() ..
  27. default.formspec.get_slot_colors() ..
  28. "label[3.5,0;Fuel Supply]" ..
  29. "list[context;fuel;3.5,2;1,1;]" ..
  30. utility.progress_image(3.5, 1, "default_furnace_fire_bg.png", "default_furnace_fire_fg.png", fuel_percent) ..
  31. "label[1.5,0;Upgrades]" ..
  32. "list[context;upgrades;1.5,1;1,2]" ..
  33. "label[5.5,0;Configuration]" ..
  34. "list[context;config;5.5,1;1,2]" ..
  35. "list[current_player;main;0,4.25;8,1;]" ..
  36. "list[current_player;main;0,5.5;8,3;8]" ..
  37. "listring[context;fuel]" ..
  38. "listring[current_player;main]" ..
  39. default.get_hotbar_bg(0, 4.25)
  40. return formspec
  41. end
  42. generator.can_dig =
  43. function(pos, player)
  44. local meta = minetest.get_meta(pos)
  45. local inv = meta:get_inventory()
  46. return inv:is_empty("fuel") and
  47. inv:is_empty("upgrades") and
  48. inv:is_empty("config")
  49. end
  50. generator.allow_metadata_inventory_put =
  51. function(pos, listname, index, stack, player)
  52. -- Listname `dst` is unused.
  53. return machines.allow_metadata_inventory_put(
  54. pos, listname, index, stack, player,
  55. {"fuel", "mesefuel"}, "fuel", "src", nil, "config", "upgrades")
  56. end
  57. generator.allow_metadata_inventory_move =
  58. function(pos, from_list, from_index, to_list, to_index, count, player)
  59. local pname = player:get_player_name()
  60. if minetest.test_protection(pos, pname) then
  61. return 0
  62. end
  63. if from_list == to_list then
  64. return count
  65. end
  66. return 0
  67. end
  68. generator.allow_metadata_inventory_take =
  69. function(pos, listname, index, stack, player)
  70. local pname = player:get_player_name()
  71. if minetest.test_protection(pos, pname) then
  72. return 0
  73. end
  74. return stack:get_count()
  75. end
  76. generator.on_timer =
  77. function(pos, elapsed)
  78. machines.log_update(pos, "Generator")
  79. local result = false
  80. local meta = minetest.get_meta(pos)
  81. local inv = meta:get_inventory()
  82. local fuellist = inv:get_list("fuel")
  83. local time = meta:get_float("time")
  84. local maxtime = meta:get_float("maxtime")
  85. local fuel_state = ""
  86. local percent = 0
  87. local eups = meta:get_int("eups") -- EUs per/sec.
  88. local eu_buffered = meta:get_int("eu_buffered")
  89. local buffer_max = 10000
  90. -- Manage fuel and EU-producing stuff.
  91. if time > 0 then
  92. -- Keep burning current fuel item.
  93. time = time - 1
  94. meta:set_float("time", time)
  95. percent = math_floor(time / maxtime * 100)
  96. fuel_state = percent .. "%"
  97. eu_buffered = eu_buffered + eups
  98. -- Restart timer.
  99. result = true
  100. else
  101. if eu_buffered >= buffer_max then
  102. -- Batteries full, shutdown generator.
  103. -- We can only get here if the buffer wasn't purged during the last
  104. -- iteration. That means the network can't absorb any more power.
  105. fuel_state = "Standby-Mode"
  106. eups = 0
  107. machines.swap_node(pos, "generator:inactive")
  108. minetest.get_node_timer(pos):stop()
  109. else
  110. -- Burntime has run out (and batteries not full), get new fuel item.
  111. if fuellist[1]:get_count() > 0 then
  112. local fuel, afterfuel
  113. -- Try to get fuel.
  114. fuel, afterfuel = minetest.get_craft_result({
  115. method="fuel", width=1, items=fuellist,
  116. })
  117. eups = 200
  118. -- If not regular fuel, try to get mesefuel.
  119. if fuel.time == 0 then
  120. fuel, afterfuel = minetest.get_craft_result({
  121. method="mesefuel", width=1, items=fuellist,
  122. })
  123. eups = 600
  124. end
  125. if fuel.time > 0 then
  126. -- We got a valid fuel item, consume it.
  127. inv:set_stack("fuel", 1, afterfuel.items[1])
  128. meta:set_float("time", fuel.time)
  129. meta:set_float("maxtime", fuel.time)
  130. machines.swap_node(pos, "generator:active")
  131. percent = 100
  132. fuel_state = "100%"
  133. result = true -- Restart timer.
  134. else
  135. fuel_state = "Not Fuel"
  136. eups = 0
  137. machines.swap_node(pos, "generator:inactive")
  138. minetest.get_node_timer(pos):stop()
  139. end
  140. else
  141. -- No more fuel, shutdown generator.
  142. fuel_state = "Empty"
  143. eups = 0
  144. machines.swap_node(pos, "generator:inactive")
  145. minetest.get_node_timer(pos):stop()
  146. end
  147. end
  148. end
  149. -- Deliver charge onto the network.
  150. -- We allow the buffer to be temporarily greater than its max for this.
  151. if eu_buffered >= buffer_max then
  152. eu_buffered = generator.deliver_charge(pos, eu_buffered)
  153. end
  154. -- Clamp buffer level to capacity.
  155. if eu_buffered > buffer_max then
  156. eu_buffered = buffer_max
  157. end
  158. local machine_state = "Standby"
  159. if result then machine_state = "Active" end
  160. local infotext = "Power Generator (" .. machine_state .. ")\n" ..
  161. "EUs per/sec: " .. eups .. "\nFuel Burn: " .. fuel_state .. "\n" ..
  162. "Buffered: " .. eu_buffered .. " EUs"
  163. meta:set_int("eups", eups)
  164. meta:set_int("eu_buffered", eu_buffered)
  165. meta:set_string("infotext", infotext)
  166. meta:set_string("formspec", generator.compose_formspec(percent))
  167. return result
  168. end
  169. -- Deliver EUs to batteries on the network. Return EUs left over.
  170. generator.deliver_charge =
  171. function(pos, charge)
  172. -- The generator can charge MV batteries as well as LV batteries.
  173. local hubs = machines.get_adjacent_network_hubs(pos, {"lv", "mv"})
  174. if hubs then
  175. for k, v in ipairs(hubs) do
  176. if charge > 0 then
  177. -- Gets back the energy left over.
  178. charge = machines.deliver_charge_to_network(pos, v, charge)
  179. end
  180. end
  181. end
  182. return charge
  183. end
  184. generator.on_blast =
  185. function(pos)
  186. local drops = {}
  187. default.get_inventory_drops(pos, "fuel", drops)
  188. default.get_inventory_drops(pos, "upgrades", drops)
  189. default.get_inventory_drops(pos, "config", drops)
  190. drops[#drops+1] = "generator:inactive"
  191. minetest.remove_node(pos)
  192. return drops
  193. end
  194. generator.on_construct =
  195. function(pos)
  196. generator.initialize_typedata(pos)
  197. local meta = minetest.get_meta(pos)
  198. local inv = meta:get_inventory()
  199. meta:set_string("infotext", "Power Generator (Standby)")
  200. meta:set_string("formspec", generator.compose_formspec(0))
  201. inv:set_size("fuel", 1)
  202. inv:set_size("upgrades", 2)
  203. inv:set_size("config", 2)
  204. end
  205. generator.after_place_node =
  206. function(pos, placer, itemstack, pointed_thing)
  207. end
  208. generator.trigger_update =
  209. function(pos)
  210. local timer = minetest.get_node_timer(pos)
  211. if not timer:is_started() then
  212. timer:start(1.0)
  213. end
  214. end
  215. generator.on_metadata_inventory_move =
  216. function(pos)
  217. generator.trigger_update(pos)
  218. end
  219. generator.on_metadata_inventory_put =
  220. function(pos)
  221. generator.trigger_update(pos)
  222. end
  223. generator.on_metadata_inventory_take =
  224. function(pos, listname, index, stack, player)
  225. generator.trigger_update(pos)
  226. end
  227. if not generator.functions_loaded then
  228. local c = "generator:core"
  229. local f = generator.modpath .. "/functions.lua"
  230. reload.register_file(c, f, false)
  231. generator.functions_loaded = true
  232. end