functions.lua 7.7 KB

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