farm.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. local function farmhouse_overview(pos)
  2. local meta = core.get_meta(pos)
  3. local health = meta:get_int('health')
  4. local inventory = meta:get_int('inventory')
  5. local pos1 = vector.add(pos, {x=-3, y=0, z=-3})
  6. local pos2 = vector.add(pos, {x=3, y=0, z=3})
  7. local fields = core.find_nodes_in_area(pos1, pos2, 'kobo:farm_field')
  8. local hp_scale = (health/200) * 3
  9. local inv_scale = (inventory/250) * 3
  10. local formspec =
  11. kobo.building_inv..
  12. 'textarea[3.25,0;7.5,1;;;Farmhouse]'..
  13. 'image[0,0;3,3;kobo_farmhouse_snap.png]'..
  14. 'textarea[3.25,.75;7.5,1;;;Take charge of your own destiny. Gardens are better than grass...\n'..
  15. 'There are '..#fields..' surrounding farm fields.]'..
  16. 'image_button[3.25,2;1,1;kobo_farm_field_ico.png;add_field;;true;false]'..
  17. 'tooltip[add_field;Plant a new field (50 lumber, 100 food);black;white]'..
  18. 'image[0,3.75;'..hp_scale..',.25;kobo_health_fg.png]'..
  19. 'label[0,4.25;Inventory]'..
  20. 'image[0,4.5;3,.25;kobo_graph_bg.png]'..
  21. 'image[0,4.5;'..inv_scale..',.25;kobo_inv_fg.png]'
  22. return formspec
  23. end
  24. local function farmhouse_overview_wrapper(player, pos)
  25. local refresh = kobo.refresh[player]
  26. if refresh then
  27. core.show_formspec(player, 'kobo:farmhouse', farmhouse_overview(pos))
  28. core.after(.25, function()
  29. farmhouse_overview_wrapper(player, pos)
  30. end)
  31. end
  32. end
  33. local function farm_field_overview(pos)
  34. local meta = core.get_meta(pos)
  35. local health = meta:get_int('health')
  36. local inventory = meta:get_int('remaining')
  37. local hp_scale = (health/200) * 3
  38. local inv_scale = (inventory/250) * 3
  39. local formspec = kobo.building_inv
  40. if inventory >= 2 then
  41. formspec = formspec ..
  42. 'textarea[3.25,0;7.5,1;;;Farmfield]'..
  43. 'image[0,0;3,3;kobo_farm_field_snap.png]'..
  44. 'textarea[3.25,.75;7.5,2.25;;;This certainly tastes better than grass.]'..
  45. 'image[0,3.75;'..hp_scale..',.25;kobo_health_fg.png]'..
  46. 'label[0,4.25;Inventory]'..
  47. 'image[0,4.5;3,.25;kobo_graph_bg.png]'..
  48. 'image[0,4.5;'..inv_scale..',.25;kobo_inv_fg.png]'
  49. else
  50. local timer = core.get_node_timer(pos)
  51. local remaining = math.ceil((timer:get_timeout()- timer:get_elapsed())/60)
  52. formspec = formspec ..
  53. 'textarea[3.25,0;7.5,1;;;Empty Farmfield]'..
  54. 'image[0,0;3,3;kobo_farm_field_snap.png]'..
  55. 'textarea[3.25,.75;7.5,2.25;;;Crops are growing, about '..remaining..' more minutes before they can be harvested.]'..
  56. 'image[0,3.75;'..hp_scale..',.25;kobo_health_fg.png]'..
  57. 'label[0,4.25;Inventory]'..
  58. 'image[0,4.5;3,.25;kobo_graph_bg.png]'..
  59. 'image[0,4.5;0,.25;kobo_inv_fg.png]'
  60. end
  61. return formspec
  62. end
  63. local function farm_field_overview_wrapper(player, pos)
  64. local refresh = kobo.refresh[player]
  65. if refresh then
  66. core.show_formspec(player, 'kobo:farm_field', farm_field_overview(pos))
  67. core.after(.25, function()
  68. farm_field_overview_wrapper(player, pos)
  69. end)
  70. end
  71. end
  72. local tower_col = {
  73. type = 'fixed',
  74. fixed = {{-.4375, -.5, -.4375, .4375, -.3125, .4375}}
  75. }
  76. local tower_sel = {
  77. type = 'fixed',
  78. fixed = {{-.4375, -.5, -.4375, .4375, .25, .4375}}
  79. }
  80. core.register_node('kobo:farmhouse', {
  81. description = 'Farmhouse',
  82. drawtype = 'mesh',
  83. mesh = 'kobo_farmhouse.obj',
  84. tiles = {'kobo_farmhouse.png'},
  85. paramtype = 'light',
  86. paramtype2 = 'facedir',
  87. range = 15,
  88. selection_box = tower_sel,
  89. collision_box = tower_col,
  90. groups = {collector = 1, player_building=1},
  91. _snapshot = 'kobo_farmhouse_snap.png',
  92. _cost = {150, 200, 25, 100},
  93. _health = 200,
  94. _max_inv = 250,
  95. _material = 'food',
  96. _long_desc = 'No need for berry bushes when you can have a farm.',
  97. after_place_node = function(pos, placer)
  98. local pos_check = vector.subtract(pos, {x=0, y=1, z=0})
  99. local node_check = core.get_node(pos_check)
  100. if core.get_item_group(node_check.name, 'ground') > 0 then
  101. local player_name = placer:get_player_name()
  102. local timer = core.get_node_timer(pos)
  103. local meta = core.get_meta(pos)
  104. local resources = kobo.resources[player_name]
  105. meta:set_string('owner', player_name)
  106. meta:set_int('inventory', 0)
  107. meta:set_int('health', 200)
  108. kobo.place_lights(pos)
  109. resources.xp = resources.xp + 1
  110. timer:start(5)
  111. else
  112. core.chat_send_player(player_name, 'You can\'t place this here.')
  113. core.remove_node(pos)
  114. return true
  115. end
  116. end,
  117. after_destruct = function(pos)
  118. kobo.remove_lights(pos)
  119. end,
  120. on_rightclick = function(pos, node, clicker)
  121. local player_name = clicker:get_player_name()
  122. local meta = core.get_meta(pos)
  123. local owner = meta:get_string('owner')
  124. if owner == player_name or core.check_player_privs(clicker, {server = true}) then
  125. kobo.player_pos[player_name] = pos
  126. kobo.refresh[player_name] = true
  127. farmhouse_overview_wrapper(player_name, pos)
  128. kobo.save()
  129. end
  130. end,
  131. on_timer = function(pos)
  132. local timer = core.get_node_timer(pos)
  133. local meta = core.get_meta(pos)
  134. local inventory = meta:get_int('inventory')
  135. if inventory <= 224 then
  136. local pos1 = vector.add(pos, {x=-3, y=0, z=-3})
  137. local pos2 = vector.add(pos, {x=3, y=0, z=3})
  138. local fields = core.find_nodes_in_area(pos1, pos2, 'kobo:farm_field')
  139. if fields then
  140. meta:set_int('inventory', inventory + #fields)
  141. for i = 1, #fields do
  142. local fields_meta = core.get_meta(fields[i])
  143. local remaining = fields_meta:get_int('remaining')
  144. local meta = core.get_meta(pos)
  145. local inv = meta:get_inventory()
  146. if remaining == 0 then
  147. fields_meta:set_int('remaining', 250)
  148. elseif remaining <= 1 then
  149. core.swap_node(fields[i], {name = 'kobo:farm_field_empty'})
  150. local timer = core.get_node_timer(fields[i])
  151. timer:start(300)
  152. else
  153. fields_meta:set_int('remaining', remaining - 1)
  154. end
  155. end
  156. end
  157. end
  158. timer:start(5)
  159. end,
  160. })
  161. local field_box = {
  162. type = 'fixed',
  163. fixed = {{-.5, -.5, -.5, .5, -.4375, .5}}
  164. }
  165. core.register_node('kobo:farm_field', {
  166. description = 'Farmfield',
  167. drawtype = 'mesh',
  168. mesh = 'kobo_farm_field.obj',
  169. tiles = {'kobo_farm_field.png'},
  170. use_texture_alpha = 'clip',
  171. paramtype = 'light',
  172. paramtype2 = 'facedir',
  173. groups = {food=1},
  174. walkable = false,
  175. selection_box = field_box,
  176. collision_box = field_box,
  177. _initial_count = 250,
  178. after_place_node = function(pos)
  179. local pos_check = vector.subtract(pos, {x=0, y=1, z=0})
  180. local node_check = core.get_node(pos_check)
  181. if core.get_item_group(node_check.name, 'ground') > 0 then
  182. local meta = core.get_meta(pos)
  183. meta:set_int('remaining', 250)
  184. meta:set_int('health', 200)
  185. else
  186. core.chat_send_player(name, 'You can\'t place that here.')
  187. core.remove_node(pos)
  188. return true
  189. end
  190. end,
  191. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  192. local player_name = clicker:get_player_name()
  193. kobo.player_pos[player_name] = pos
  194. kobo.refresh[player_name] = true
  195. farm_field_overview_wrapper(player_name, pos)
  196. end,
  197. on_punch = function(pos, node, puncher, pointed_thing)
  198. local meta = core.get_meta(pos)
  199. local remaining = meta:get_int('remaining')
  200. if remaining == 0 then
  201. if kobo.add_player_inventory_hand(puncher, 'kobo:food') then
  202. meta:set_int('remaining', 249)
  203. end
  204. elseif remaining <= 1 then
  205. if kobo.add_player_inventory_hand(puncher, 'kobo:food') then
  206. core.swap_node(pos, {name = 'kobo:farm_field_empty'})
  207. local timer = core.get_node_timer(pos)
  208. timer:start(300)
  209. end
  210. else
  211. if kobo.add_player_inventory_hand(puncher, 'kobo:food') then
  212. meta:set_int('remaining', remaining - 1)
  213. end
  214. end
  215. end,
  216. })
  217. core.register_node('kobo:farm_field_empty', {
  218. description = 'Empty Farmfield',
  219. drawtype = 'mesh',
  220. mesh = 'kobo_farm_field_empty.obj',
  221. tiles = {'kobo_farm_field.png'},
  222. paramtype = 'light',
  223. paramtype2 = 'facedir',
  224. walkable = false,
  225. selection_box = field_box,
  226. collision_box = field_box,
  227. _initial_count = 0,
  228. on_construct = function(pos)
  229. local meta = core.get_meta(pos)
  230. meta:set_int('remaining', 0)
  231. meta:set_int('health', 200)
  232. end,
  233. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  234. local player_name = clicker:get_player_name()
  235. kobo.player_pos[player_name] = pos
  236. kobo.refresh[player_name] = true
  237. farm_field_overview_wrapper(player_name, pos)
  238. end,
  239. on_timer = function(pos)
  240. local meta = core.get_meta(pos)
  241. core.swap_node(pos, {name = 'kobo:farm_field'})
  242. meta:set_int('remaining', 250)
  243. end,
  244. })
  245. core.register_on_player_receive_fields(function(player, formname, fields)
  246. if formname == 'kobo:farmhouse' then
  247. local name = player:get_player_name()
  248. local pos = kobo.player_pos[name]
  249. if fields.remove then
  250. local resources = kobo.resources[name] or {}
  251. resources.food = resources.food + 113
  252. resources.lumber = resources.lumber + 150
  253. resources.metal = resources.metal + 19
  254. resources.stone = resources.stone + 75
  255. kobo.refresh[name] = false
  256. core.remove_node(pos)
  257. kobo.hud_refresh(player)
  258. core.close_formspec(name, 'kobo:farmhouse')
  259. elseif fields.add_field then
  260. local resources = kobo.resources[name] or {}
  261. if resources.lumber >= 50 and resources.food >= 100 then
  262. local pos1 = vector.add(pos, {x=-1, y=0, z=-1})
  263. local pos2 = vector.add(pos, {x=1, y=0, z=1})
  264. local open_space = core.find_nodes_in_area(pos1, pos2, 'air')
  265. if #open_space >= 1 then
  266. local spot = math.random(1, #open_space)
  267. core.set_node(open_space[spot], {name = 'kobo:farm_field_empty'})
  268. local timer = core.get_node_timer(open_space[spot])
  269. timer:start(300)
  270. resources.food = resources.food - 100
  271. resources.lumber = resources.lumber - 50
  272. kobo.hud_refresh(player)
  273. else
  274. pos1 = vector.add(pos, {x=-2, y=0, z=-2})
  275. pos2 = vector.add(pos, {x=2, y=0, z=2})
  276. open_space = core.find_nodes_in_area(pos1, pos2, 'air')
  277. if #open_space >= 1 then
  278. local spot = math.random(1, #open_space)
  279. core.set_node(open_space[spot], {name = 'kobo:farm_field_empty'})
  280. local timer = core.get_node_timer(open_space[spot])
  281. timer:start(300)
  282. resources.food = resources.food - 100
  283. resources.lumber = resources.lumber - 50
  284. kobo.hud_refresh(player)
  285. end
  286. end
  287. else
  288. core.chat_send_player(name, 'You need more resources.')
  289. end
  290. elseif fields.repair then
  291. local pos = kobo.player_pos[name]
  292. local new_pos = vector.add(pos, {x=0, y=1, z=0})
  293. core.set_node(new_pos, {name='kobo:repair'})
  294. core.chat_send_player(name, 'Summonend a repairman.')
  295. end
  296. elseif formname == 'kobo:farm_field' then
  297. local name = player:get_player_name()
  298. local pos = kobo.player_pos[name]
  299. if fields.remove then
  300. core.remove_node(pos)
  301. kobo.refresh[name] = false
  302. core.close_formspec(name, 'kobo:farm_field')
  303. elseif fields.repair then
  304. core.chat_send_player(name, 'You can\'t repair this.')
  305. end
  306. end
  307. end)