well.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. local function formspec_shovel(pos)
  2. local spos = pos.x .. "," .. pos.y .. "," .. pos.z
  3. local formspec =
  4. 'formspec_version[3]'..
  5. 'size[10.25,9]'..
  6. 'textarea[1.5,.25;8.5,3.5;;;You need to dig for water. '..
  7. 'Depending on where you are you may need to dig for a while to find water. '..
  8. 'Put a shovel in the slot to start digging. '..
  9. '(The shovel will dig through all nodes until water is found.)]'..
  10. 'list[nodemeta:'..spos..';bucket;.25,.25;1,1;]'..
  11. 'list[current_player;main;.25,4;8,4;]'..
  12. 'listring[]'
  13. return formspec
  14. end
  15. local function formspec_water(pos, depth)
  16. local spos = pos.x .. "," .. pos.y .. "," .. pos.z
  17. local formspec =
  18. 'formspec_version[3]'..
  19. 'size[10.25,9]'..
  20. 'textarea[1.5,.25;8.5,3.5;;;Fantastic! water has been discovered, only '..depth..
  21. ' nodes below the well. This means it will take about '..(depth/2)..
  22. ' seconds to get a bucket of water! (The bucket will be filled automatically)]'..
  23. 'list[nodemeta:'..spos..';bucket;.25,.25;1,1;]'..
  24. 'list[current_player;main;.25,4;8,4;]'..
  25. 'listring[]'
  26. return formspec
  27. end
  28. local biome_table = {
  29. ['rainforest'] = {min=5,max=15},
  30. ['grassland'] = {min=7,max=20},
  31. ['savanna'] = {min=15,max=45},
  32. ['deciduous_forest'] = {min=10,max=30},
  33. ['coniferous_forest'] = {min=12,max=25},
  34. ['coniferous_forest_dunes'] = {min=12,max=25},
  35. ['tundra'] = {min=15,max=20},
  36. ['icesheet'] = {min=21,max=25},
  37. ['taiga'] = {min=8,max=19},
  38. ['snowy_grassland'] = {min=7,max=20},
  39. ['grassland_dunes'] = {min=7,max=20},
  40. }
  41. minetest.register_craftitem('furniture:well_top', {
  42. description = 'Top of well',
  43. inventory_image = 'furniture_well_top.png'
  44. })
  45. minetest.register_craft({
  46. output = 'furniture:well',
  47. recipe = {
  48. {'default:stone', 'default:stone', 'default:stone'},
  49. {'default:stone', 'furniture:well_top', 'default:stone'},
  50. {'default:stone', 'default:stone', 'default:stone'}
  51. }
  52. })
  53. minetest.register_node('furniture:well', {
  54. description = 'Well',
  55. drawtype = 'mesh',
  56. mesh = 'furniture_well.obj',
  57. tiles = {'furniture_well.png'},
  58. paramtype = 'light',
  59. paramtype2 = 'facedir',
  60. selection_box = {
  61. type = 'fixed',
  62. fixed = {{-.6875, -.5, .5, .6875, .125, .6875},
  63. {.5, -.5, -.6875, .6875, .125, .6875},
  64. {-.6875, -.5, -.6875, -.5, .125, .6875},
  65. {-.6875, -.5, -.6875, .6875, .125, -.5},
  66. {-.6875, .75, -.9375, .6875, 1.375, .9375}}
  67. },
  68. collision_box = {
  69. type = 'fixed',
  70. fixed = {{-.6875, -.5, .5, .6875, .125, .6875},
  71. {.5, -.5, -.6875, .6875, .125, .6875},
  72. {-.6875, -.5, -.6875, -.5, .125, .6875},
  73. {-.6875, -.5, -.6875, .6875, .125, -.5},
  74. {-.6875, .75, -.9375, .6875, 1.375, .9375}}
  75. },
  76. groups = {cracky=2},
  77. on_construct = function(pos)
  78. local meta = minetest.get_meta(pos)
  79. local inv = meta:get_inventory()
  80. inv:set_size('bucket', 1)
  81. meta:set_int('water', 0)
  82. meta:set_string('working', 'false')
  83. meta:set_string('infotext', 'Dig for water')
  84. local biome_data = minetest.get_biome_data(pos)
  85. local biome_id = biome_data.biome
  86. local biome_name = minetest.get_biome_name(biome_id)
  87. meta:set_string('biome', biome_name)
  88. end,
  89. can_dig = function(pos,player)
  90. local inv = minetest.get_meta(pos):get_inventory()
  91. return inv:is_empty('bucket')
  92. end,
  93. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  94. local input = stack:get_name()
  95. local meta = minetest.get_meta(pos)
  96. local working = meta:get_string('working')
  97. if listname == 'bucket' then
  98. if input == 'bucket:bucket_empty' then
  99. if working == 'false' then
  100. return 0
  101. else
  102. return 1
  103. end
  104. elseif minetest.get_item_group(input, 'shovel') > 0 then
  105. if working == 'true' then
  106. return 0
  107. else
  108. return 1
  109. end
  110. else
  111. return 0
  112. end
  113. end
  114. end,
  115. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  116. local meta = minetest.get_meta(pos)
  117. local item = stack:get_name()
  118. local timer = minetest.get_node_timer(pos)
  119. if item == 'bucket:bucket_empty' then
  120. meta:set_string('infotext', 'Getting water...')
  121. local inv = meta:get_inventory()
  122. local bucket = inv:get_stack('bucket', 1)
  123. local water_depth = meta:get_int('water')
  124. timer:start(water_depth/2)
  125. else
  126. meta:set_string('infotext', 'Digging well...')
  127. timer:start(1)
  128. end
  129. end,
  130. on_rightclick = function(pos, node, clicker)
  131. local name = clicker:get_player_name()
  132. local meta = minetest.get_meta(pos)
  133. local working = meta:get_string('working')
  134. if working == 'false' then
  135. minetest.show_formspec(name, 'furniture:well', formspec_shovel(pos))
  136. else
  137. local depth = meta:get_int('water')
  138. minetest.show_formspec(name, 'furniture:well', formspec_water(pos, depth))
  139. end
  140. end,
  141. on_metadata_inventory_take = function(pos)
  142. local timer = minetest.get_node_timer(pos)
  143. local meta = minetest.get_meta(pos)
  144. timer:stop()
  145. meta:set_string('infotext', 'Ready to use.')
  146. end,
  147. on_timer = function(pos)
  148. local timer = minetest.get_node_timer(pos)
  149. local meta = minetest.get_meta(pos)
  150. local working = meta:get_string('working')
  151. local biome = meta:get_string('biome')
  152. if working == 'true' then --get water
  153. local inv = meta:get_inventory()
  154. inv:set_stack('bucket', 1, 'bucket:bucket_river_water')
  155. meta:set_string('infotext', 'Ready to use.')
  156. else
  157. local inv = meta:get_inventory()
  158. local shovel = inv:get_stack('bucket', 1):get_name()
  159. if minetest.get_item_group(shovel, 'shovel') > 0 then
  160. if biome_table[biome] then
  161. local min = biome_table[biome].min or 50
  162. local max = biome_table[biome].max or 100
  163. local chance = math.random(min, max)
  164. local depth = meta:get_int('water')
  165. local below = depth + 1
  166. if below < chance then
  167. local pos2 = {x=pos.x, y=pos.y-below, z=pos.z}
  168. --pos:offset(0, -below, 0)
  169. minetest.remove_node(pos2)
  170. meta:set_int('water', below)
  171. timer:start(1)
  172. else
  173. meta:set_string('working', 'true')
  174. meta:set_string('infotext', 'Ready to use.')
  175. end
  176. end
  177. end
  178. end
  179. end,
  180. })