food_bushes.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. local function bush_overview(pos, food)
  2. local formspec =
  3. kobo.resource_inv..
  4. 'textarea[3.25,0;7.5,1;;;Berry Bush]'..
  5. 'image[0,0;3,3;kobo_bush_snap.png]'
  6. if food then
  7. local meta = core.get_meta(pos)
  8. local remaining = meta:get_int('remaining')
  9. if remaining == 0 then remaining = 100 end
  10. local scale = (remaining/100) * 3
  11. formspec = formspec..
  12. 'textarea[3.25,.75;7.5,2.25;;;Harvest berries to collect food.\n'..
  13. 'These bushes have '..remaining..' food remaining.]'..
  14. 'image[0,4.5;'..scale..',.25;kobo_inv_fg.png]'
  15. else
  16. local timer = core.get_node_timer(pos)
  17. local remaining = math.ceil((timer:get_timeout()- timer:get_elapsed())/60)
  18. formspec = formspec..
  19. 'textarea[3.25,.75;7.5,2.25;;;Harvest berries to collect food.\n'..
  20. 'Berries grow back in '..remaining..' minute.]'
  21. end
  22. return formspec
  23. end
  24. local function bush_overview_wrapper(player, pos, food)
  25. local refresh = kobo.refresh[player]
  26. if refresh then
  27. core.show_formspec(player, 'kobo:bush', bush_overview(pos, food))
  28. core.after(.25, function()
  29. bush_overview_wrapper(player, pos, food)
  30. end)
  31. end
  32. end
  33. local bush_box = {
  34. type = 'fixed',
  35. fixed = {{-.45, -.5, -.45, .45, -.1, .45}}
  36. }
  37. core.register_node('kobo:bush_food', { --This has food
  38. description = 'Food Bush',
  39. drawtype = 'mesh',
  40. mesh = 'kobo_bush.obj',
  41. tiles = {'kobo_bush_food.png'},
  42. paramtype = 'light',
  43. paramtype2 = 'facedir',
  44. groups = {food=2},
  45. selection_box = bush_box,
  46. collision_box = bush_box,
  47. _initial_count = 100,
  48. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  49. local player_name = clicker:get_player_name()
  50. kobo.player_pos[player_name] = pos
  51. kobo.refresh[player_name] = true
  52. bush_overview_wrapper(player_name, pos, true)
  53. end,
  54. on_punch = function(pos, node, puncher, pointed_thing)
  55. local meta = core.get_meta(pos)
  56. local remaining = meta:get_int('remaining')
  57. if remaining == 0 then -- Tree hasn't been punched yet. Add to player inventory.
  58. if kobo.add_player_inventory_hand(puncher, 'kobo:food') then
  59. meta:set_int('remaining', 99)
  60. end
  61. elseif remaining == 1 then --Last bit of wood, add to player inventory, and remove the node.
  62. if kobo.add_player_inventory_hand(puncher, 'kobo:food') then
  63. core.swap_node(pos, {name = 'kobo:bush', param2 = node.param2})
  64. local timer = core.get_node_timer(pos)
  65. timer:start(600)
  66. end
  67. else --Add inventory to player, cap at some level.
  68. if kobo.add_player_inventory_hand(puncher, 'kobo:food') then
  69. meta:set_int('remaining', remaining - 1)
  70. end
  71. end
  72. end,
  73. })
  74. core.register_node('kobo:bush', { --This has NO food
  75. description = 'Bush',
  76. drawtype = 'mesh',
  77. mesh = 'kobo_bush.obj',
  78. tiles = {'kobo_bush.png'},
  79. paramtype = 'light',
  80. paramtype2 = 'facedir',
  81. selection_box = bush_box,
  82. collision_box = bush_box,
  83. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  84. local player_name = clicker:get_player_name()
  85. kobo.player_pos[player_name] = pos
  86. kobo.refresh[player_name] = true
  87. bush_overview_wrapper(player_name, pos, false)
  88. end,
  89. on_punch = function(pos, node, puncher, pointed_thing)
  90. core.chat_send_player(puncher:get_player_name(), 'These bushes currently have no food.')
  91. end,
  92. on_timer = function(pos)
  93. local node = core.get_node(pos)
  94. core.set_node(pos, {name = 'kobo:bush_food', param2 = node.param2})
  95. end,
  96. })
  97. core.register_craftitem('kobo:food', {
  98. description = 'Food',
  99. inventory_image = 'kobo_food.png',
  100. stack_max = 25,
  101. })
  102. core.register_decoration({
  103. deco_type = 'simple',
  104. place_on = {'kobo:grass'},
  105. sidelen = 16,
  106. noise_params = {
  107. offset = .01,
  108. scale = 0.05,
  109. spread = {x = 300, y = 20, z = 300},
  110. seed = 684132,
  111. octaves = 4,
  112. persist = .5,
  113. lacunarity = 2,
  114. flags = 'ease'
  115. },
  116. y_min = -1,
  117. y_max = 2,
  118. decoration = 'kobo:bush_food',
  119. param2 = 0,
  120. param2_max = 3,
  121. biomes = {'grassland'},
  122. })
  123. core.register_on_player_receive_fields(function(player, formname, fields)
  124. if formname == 'kobo:bush' then
  125. if fields.remove then
  126. local name = player:get_player_name()
  127. local pos = kobo.player_pos[name]
  128. core.remove_node(pos)
  129. kobo.refresh[name] = false
  130. core.close_formspec(name, 'kobo:bush')
  131. end
  132. end
  133. end)