workshop.lua 9.9 KB

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