logging.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. local function logging_1_overview(pos)
  2. local meta = core.get_meta(pos)
  3. local inventory = meta:get_int('inventory')
  4. local pos1 = vector.add(pos, {x=-3, y=0, z=-3})
  5. local pos2 = vector.add(pos, {x=3, y=0, z=3})
  6. local trees = core.find_nodes_in_area(pos1, pos2, 'group:lumber')
  7. local health = meta:get_int('health')
  8. local hp_scale = (health/50) * 3
  9. local inv_scale = (inventory/50) * 3
  10. local formspec =
  11. kobo.building_inv..
  12. 'textarea[3.25,0;7.5,1;;;Logging Camp (tier 1)]'..
  13. 'image[0,0;3,3;kobo_logging_1_snap.png]'..
  14. 'textarea[3.25,.75;7.5,2.25;;;There are '..#trees..' tree(s) available to chop.]'..
  15. 'image_button[3.25,2;1,1;kobo_evergreen_ico.png;add_trees;;true;false]'..
  16. 'tooltip[add_trees;Plant trees (50 lumber, 25 stone);black;white]'..
  17. 'image[0,3.75;'..hp_scale..',.25;kobo_health_fg.png]'..
  18. 'label[0,4.25;Inventory]'..
  19. 'image[0,4.5;3,.25;kobo_graph_bg.png]'..
  20. 'image[0,4.5;'..inv_scale..',.25;kobo_inv_fg.png]'
  21. return formspec
  22. end
  23. local function logging_1_overview_wrapper(player, pos)
  24. local refresh = kobo.refresh[player]
  25. if refresh then
  26. core.show_formspec(player, 'kobo:logging_1', logging_1_overview(pos))
  27. core.after(.25, function()
  28. logging_1_overview_wrapper(player, pos)
  29. end)
  30. end
  31. end
  32. local function logging_2_overview(pos)
  33. local meta = core.get_meta(pos)
  34. local inventory = meta:get_int('inventory')
  35. local pos1 = vector.add(pos, {x=-6, y=0, z=-6})
  36. local pos2 = vector.add(pos, {x=6, y=0, z=6})
  37. local trees = core.find_nodes_in_area(pos1, pos2, 'group:lumber')
  38. local health = meta:get_int('health')
  39. local hp_scale = (health/125) * 3
  40. local inv_scale = (inventory/100) * 3
  41. local formspec =
  42. kobo.building_inv..
  43. 'textarea[3.25,0;7.5,1;;;Logging Camp (tier 2)]'..
  44. 'image[0,0;3,3;kobo_logging_2_snap.png]'..
  45. 'textarea[3.25,.75;7.5,2.25;;;There are '..#trees..' tree(s) available to chop.]'..
  46. 'image_button[3.25,2;1,1;kobo_evergreen_ico.png;add_trees;;true;false]'..
  47. 'tooltip[add_trees;Plant trees (50 lumber, 25 stone);black;white]'..
  48. 'image[0,3.75;'..hp_scale..',.25;kobo_health_fg.png]'..
  49. 'label[0,4.25;Inventory]'..
  50. 'image[0,4.5;3,.25;kobo_graph_bg.png]'..
  51. 'image[0,4.5;'..inv_scale..',.25;kobo_inv_fg.png]'
  52. return formspec
  53. end
  54. local function logging_2_overview_wrapper(player, pos)
  55. local refresh = kobo.refresh[player]
  56. if refresh then
  57. core.show_formspec(player, 'kobo:logging_2', logging_2_overview(pos))
  58. core.after(.25, function()
  59. logging_2_overview_wrapper(player, pos)
  60. end)
  61. end
  62. end
  63. local logging_1_box = {
  64. type = 'fixed',
  65. fixed = {{-.55, -.5, -.55, 1.55, -.375, 1.55}}
  66. }
  67. local logging_2_box = {
  68. type = 'fixed',
  69. fixed = {{-.55, -.5, -.55, 1.55, -.375, 1.55}}
  70. }
  71. local pathways = {
  72. { 0, -1},
  73. {-1, 0},
  74. { 0, 1},
  75. { 1, 0},
  76. }
  77. core.register_node('kobo:logging_1', {
  78. description = 'Logging Camp (tier 1)',
  79. drawtype = 'mesh',
  80. mesh = 'kobo_logging_1.obj',
  81. tiles = {'kobo_logging_1.png'},
  82. paramtype = 'light',
  83. paramtype2 = 'facedir',
  84. range = 15,
  85. selection_box = logging_1_box,
  86. collision_box = logging_1_box,
  87. groups = {collector=1, player_building=1},
  88. _snapshot = 'kobo_logging_1_snap.png',
  89. _cost = {50, 100, 25, 0},
  90. _health = 50,
  91. _max_inv = 50,
  92. _material = 'lumber',
  93. _long_desc = 'An automatic lumber harvester. You will need inventory transportation units to pick up lumber and transfer it to a storage facility.',
  94. after_place_node = function(pos, placer)
  95. local player_name = placer:get_player_name()
  96. if kobo.space_for_building(pos) then
  97. local pos_check = vector.subtract(pos, {x=0, y=1, z=0})
  98. local node_check = core.get_node(pos_check)
  99. if core.get_item_group(node_check.name, 'ground') > 0 then
  100. local meta = core.get_meta(pos)
  101. local timer = core.get_node_timer(pos)
  102. local resources = kobo.resources[player_name]
  103. meta:set_string('owner', player_name)
  104. meta:set_int('inventory', 0)
  105. meta:set_int('health', 50)
  106. kobo.place_lights(pos)
  107. timer:start(5)
  108. resources.xp = resources.xp + 1
  109. local node = core.get_node(pos)
  110. local fdir = node.param2 % 32
  111. local path1 = {x=pos.x + pathways[fdir+1][1], y=pos.y, z=pos.z + pathways[fdir+1][2]}
  112. local path2 = {x=pos.x + (2*(pathways[fdir+1][1])), y=pos.y, z=pos.z + (2*(pathways[fdir+1][2]))}
  113. core.set_node(path1,{name = 'kobo:empty'})
  114. core.set_node(path2,{name = 'kobo:empty'})
  115. else
  116. core.chat_send_player(player_name, 'You can\'t place this here.')
  117. core.remove_node(pos)
  118. return true
  119. end
  120. else
  121. core.chat_send_player(player_name, 'There is something in the way.')
  122. core.remove_node(pos)
  123. return true
  124. end
  125. end,
  126. after_destruct = function(pos, oldnode)
  127. kobo.remove_lights(pos)
  128. kobo.remove_building_spaces(pos, oldnode)
  129. local fdir = oldnode.param2 % 32
  130. local path1 = {x=pos.x + pathways[fdir+1][1], y=pos.y, z=pos.z + pathways[fdir+1][2]}
  131. local path2 = {x=pos.x + (2*(pathways[fdir+1][1])), y=pos.y, z=pos.z + (2*(pathways[fdir+1][2]))}
  132. core.remove_node(path1)
  133. core.remove_node(path2)
  134. end,
  135. on_timer = function(pos)
  136. local meta = core.get_meta(pos)
  137. local inventory = meta:get_int('inventory')
  138. local timer = core.get_node_timer(pos)
  139. if inventory <= 50 then
  140. local pos1 = vector.add(pos, {x=-3, y=0, z=-3})
  141. local pos2 = vector.add(pos, {x=3, y=0, z=3})
  142. local trees = core.find_nodes_in_area(pos1, pos2, 'group:lumber')
  143. if trees then
  144. meta:set_int('inventory', inventory + #trees)
  145. for i = 1, #trees do
  146. local tree_meta = core.get_meta(trees)
  147. local remaining = tree_meta:get_int('remaining')
  148. local meta = core.get_meta(pos)
  149. local inv = meta:get_inventory()
  150. if remaining == 0 then -- Tree hasn't been punched yet.
  151. tree_meta:set_int('remaining', 250)
  152. elseif remaining <= 1 then --Last bit of wood, add to inventory, and remove the node.
  153. core.remove_node(trees)
  154. else --Add inventory
  155. tree_meta:set_int('remaining', remaining - 1)
  156. end
  157. end
  158. end
  159. end
  160. timer:start(5)
  161. end,
  162. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  163. local player_name = clicker:get_player_name()
  164. local meta = core.get_meta(pos)
  165. local owner = meta:get_string('owner')
  166. if owner == player_name or core.check_player_privs(clicker, {server = true}) then
  167. kobo.player_pos[player_name] = pos
  168. kobo.refresh[player_name] = true
  169. logging_1_overview_wrapper(player_name, pos)
  170. end
  171. end,
  172. })
  173. core.register_node('kobo:logging_2', {
  174. description = 'Logging Camp (tier 2)',
  175. drawtype = 'mesh',
  176. mesh = 'kobo_logging_2.obj',
  177. tiles = {'kobo_logging_2.png'},
  178. inventory_image = 'kobo_logging_2_ico.png',
  179. wield_image = 'kobo_logging_2_ico.png',
  180. paramtype = 'light',
  181. paramtype2 = 'facedir',
  182. range = 15,
  183. selection_box = logging_2_box,
  184. collision_box = logging_2_box,
  185. groups = {collector=1, player_building=1},
  186. _snapshot = 'kobo_logging_2_snap.png',
  187. _cost = {50, 200, 50, 0},
  188. _health = 125,
  189. _max_inv = 100,
  190. _material = 'lumber',
  191. _long_desc = 'An automatic lumber harvester. You will need inventory transportation units to pick up lumber and transfer it to a storage facility.',
  192. after_place_node = function(pos, placer)
  193. local player_name = placer:get_player_name()
  194. if kobo.space_for_building(pos) then
  195. local pos_check = vector.subtract(pos, {x=0, y=1, z=0})
  196. local node_check = core.get_node(pos_check)
  197. if core.get_item_group(node_check.name, 'ground') > 0 then
  198. local meta = core.get_meta(pos)
  199. local timer = core.get_node_timer(pos)
  200. local resources = kobo.resources[player_name]
  201. meta:set_string('owner', player_name)
  202. meta:set_string('infotext', 'Logging Camp')
  203. meta:set_int('inventory', 0)
  204. meta:set_int('health', 125)
  205. kobo.place_lights(pos)
  206. timer:start(5)
  207. resources.xp = resources.xp + 1
  208. local node = core.get_node(pos)
  209. local fdir = node.param2 % 32
  210. local path1 = {x=pos.x + pathways[fdir+1][1], y=pos.y, z=pos.z + pathways[fdir+1][2]}
  211. local path2 = {x=pos.x + (2*(pathways[fdir+1][1])), y=pos.y, z=pos.z + (2*(pathways[fdir+1][2]))}
  212. core.set_node(path1,{name = 'kobo:empty'})
  213. core.set_node(path2,{name = 'kobo:empty'})
  214. else
  215. core.chat_send_player(player_name, 'You can\'t place this here.')
  216. core.remove_node(pos)
  217. return true
  218. end
  219. else
  220. core.chat_send_player(player_name, 'There is something in the way.')
  221. core.remove_node(pos)
  222. return true
  223. end
  224. end,
  225. after_destruct = function(pos, oldnode)
  226. kobo.remove_lights(pos)
  227. kobo.remove_building_spaces(pos, oldnode)
  228. kobo.remove_building_spaces(pos, oldnode)
  229. local fdir = oldnode.param2 % 32
  230. local path1 = {x=pos.x + pathways[fdir+1][1], y=pos.y, z=pos.z + pathways[fdir+1][2]}
  231. local path2 = {x=pos.x + (2*(pathways[fdir+1][1])), y=pos.y, z=pos.z + (2*(pathways[fdir+1][2]))}
  232. core.remove_node(path1)
  233. core.remove_node(path2)
  234. end,
  235. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  236. local player_name = clicker:get_player_name()
  237. local meta = core.get_meta(pos)
  238. local owner = meta:get_string('owner')
  239. if owner == player_name or core.check_player_privs(clicker, {server = true}) then
  240. kobo.refresh[player_name] = true
  241. logging_2_overview_wrapper(player_name, pos)
  242. kobo.player_pos[player_name] = pos
  243. end
  244. end,
  245. on_timer = function(pos)
  246. local meta = core.get_meta(pos)
  247. local inventory = meta:get_int('inventory')
  248. local timer = core.get_node_timer(pos)
  249. if inventory < 100 then
  250. local pos1 = vector.add(pos, {x=-6, y=0, z=-6})
  251. local pos2 = vector.add(pos, {x=6, y=0, z=6})
  252. local trees = core.find_nodes_in_area(pos1, pos2, 'group:lumber')
  253. if trees then
  254. meta:set_int('inventory', inventory + #trees)
  255. for i = 1, #trees do
  256. local tree_meta = core.get_meta(trees)
  257. local remaining = tree_meta:get_int('remaining')
  258. local meta = core.get_meta(pos)
  259. local inv = meta:get_inventory()
  260. if remaining == 0 then -- Tree hasn't been punched yet.
  261. tree_meta:set_int('remaining', 250)
  262. elseif remaining <= 1 then --Last bit of wood, add to inventory, and remove the node.
  263. core.remove_node(trees)
  264. else --Add inventory
  265. tree_meta:set_int('remaining', remaining - 1)
  266. end
  267. end
  268. end
  269. end
  270. timer:start(5)
  271. end,
  272. })
  273. core.register_on_player_receive_fields(function(player, formname, fields)
  274. if formname == 'kobo:logging_1' then
  275. local name = player:get_player_name()
  276. local pos = kobo.player_pos[name]
  277. if fields.add_trees then
  278. local resources = kobo.resources[name] or {}
  279. if resources.lumber >= 50 and resources.stone >= 25 then
  280. local pos1 = vector.add(pos, {x=-1, y=0, z=-1})
  281. local pos2 = vector.add(pos, {x=1, y=0, z=1})
  282. local open_space = core.find_nodes_in_area(pos1, pos2, 'air')
  283. if #open_space > 1 then
  284. local spot = math.random(1, #open_space)
  285. core.set_node(open_space[spot], {name = 'kobo:evergreen_1'})
  286. local timer = core.get_node_timer(open_space[spot])
  287. timer:start(300)
  288. resources.stone = resources.stone - 25
  289. resources.lumber = resources.lumber - 100
  290. kobo.hud_refresh(player)
  291. else
  292. pos1 = vector.add(pos, {x=-2, y=0, z=-2})
  293. pos2 = vector.add(pos, {x=2, y=0, z=2})
  294. open_space = core.find_nodes_in_area(pos1, pos2, 'air')
  295. if #open_space > 1 then
  296. local spot = math.random(1, #open_space)
  297. core.set_node(open_space[spot], {name = 'kobo:evergreen_1'})
  298. local timer = core.get_node_timer(open_space[spot])
  299. timer:start(300)
  300. resources.stone = resources.stone - 25
  301. resources.lumber = resources.lumber - 100
  302. kobo.hud_refresh(player)
  303. end
  304. end
  305. else
  306. core.chat_send_player(name, 'You need more resources.')
  307. end
  308. elseif fields.remove then
  309. local resources = kobo.resources[name]
  310. core.remove_node(pos)
  311. resources.food = resources.food + 37
  312. resources.lumber = resources.lumber + 75
  313. resources.stone = resources.stone + 18
  314. kobo.hud_refresh(player)
  315. kobo.refresh[name] = false
  316. core.close_formspec(name, 'kobo:logging_1')
  317. elseif fields.repair then
  318. local pos = kobo.player_pos[name]
  319. local new_pos = vector.add(pos, {x=0, y=1, z=0})
  320. core.set_node(new_pos, {name='kobo:repair'})
  321. core.chat_send_player(name, 'Summonend a repairman.')
  322. end
  323. elseif formname == 'kobo:logging_2' then
  324. local name = player:get_player_name()
  325. local pos = kobo.player_pos[name]
  326. if fields.add_trees then
  327. local resources = kobo.resources[name] or {}
  328. if resources.lumber >= 50 and resources.stone >= 25 then
  329. local pos1 = vector.add(pos, {x=-1, y=0, z=-1})
  330. local pos2 = vector.add(pos, {x=1, y=0, z=1})
  331. local open_space = core.find_nodes_in_area(pos1, pos2, 'air')
  332. if #open_space >= 1 then
  333. local spot = math.random(1, #open_space)
  334. core.set_node(open_space[spot], {name = 'kobo:evergreen_1'})
  335. local timer = core.get_node_timer(open_space[spot])
  336. timer:start(300)
  337. resources.stone = resources.stone - 25
  338. resources.lumber = resources.lumber - 100
  339. kobo.hud_refresh(player)
  340. else
  341. pos1 = vector.add(pos, {x=-2, y=0, z=-2})
  342. pos2 = vector.add(pos, {x=2, y=0, z=2})
  343. open_space = core.find_nodes_in_area(pos1, pos2, 'air')
  344. if #open_space >= 1 then
  345. local spot = math.random(1, #open_space)
  346. core.set_node(open_space[spot], {name = 'kobo:evergreen_1'})
  347. local timer = core.get_node_timer(open_space[spot])
  348. timer:start(300)
  349. resources.stone = resources.stone - 25
  350. resources.lumber = resources.lumber - 100
  351. kobo.hud_refresh(player)
  352. end
  353. end
  354. else
  355. core.chat_send_player(name, 'You need more resources.')
  356. end
  357. elseif fields.remove then
  358. local resources = kobo.resources[name]
  359. core.remove_node(pos)
  360. resources.food = resources.food + 37
  361. resources.lumber = resources.lumber + 150
  362. resources.stone = resources.stone + 36
  363. kobo.hud_refresh(player)
  364. core.close_formspec(name, 'kobo:logging_2')
  365. kobo.refresh[name] = false
  366. elseif fields.repair then
  367. local pos = kobo.player_pos[name]
  368. local new_pos = vector.add(pos, {x=0, y=1, z=0})
  369. core.set_node(new_pos, {name='kobo:repair'})
  370. core.chat_send_player(name, 'Summonend a repairman.')
  371. end
  372. end
  373. end)