workshop.lua 10.0 KB

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