abm.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. minetest.register_abm({
  2. label = "Farming soil",
  3. nodenames = {"group:field"},
  4. interval = 15,
  5. chance = 4,
  6. action = function(pos, node)
  7. local starttime=os.clock()
  8. local n_def = minetest.registered_nodes[node.name] or nil
  9. local wet = n_def.soil.wet or nil
  10. local base = n_def.soil.base or nil
  11. local dry = n_def.soil.dry or nil
  12. if not n_def or not n_def.soil or not wet or not base or not dry then
  13. return
  14. end
  15. pos.y = pos.y + 1
  16. local nn = minetest.get_node_or_nil(pos)
  17. if not nn or not nn.name then
  18. return
  19. end
  20. local nn_def = minetest.registered_nodes[nn.name] or nil
  21. pos.y = pos.y - 1
  22. if nn_def and nn_def.walkable and minetest.get_item_group(nn.name, "plant") == 0 then
  23. minetest.set_node(pos, {name = base})
  24. return
  25. end
  26. -- check if there is water nearby
  27. local wet_lvl = minetest.get_item_group(node.name, "wet")
  28. if minetest.find_node_near(pos, 3, {"group:water"}) then
  29. -- if it is dry soil and not base node, turn it into wet soil
  30. if wet_lvl == 0 then
  31. minetest.set_node(pos, {name = wet})
  32. end
  33. else
  34. -- only turn back if there are no unloaded blocks (and therefore
  35. -- possible water sources) nearby
  36. if not minetest.find_node_near(pos, 3, {"ignore"}) then
  37. -- turn it back into base if it is already dry
  38. if wet_lvl == 0 then
  39. -- only turn it back if there is no plant/seed on top of it
  40. if minetest.get_item_group(nn.name, "plant") == 0 and minetest.get_item_group(nn.name, "seed") == 0 then
  41. minetest.set_node(pos, {name = base})
  42. end
  43. -- if its wet turn it back into dry soil
  44. elseif wet_lvl == 1 then
  45. minetest.set_node(pos, {name = dry})
  46. end
  47. end
  48. end
  49. -- table.insert(farming.time_farming,1000*(os.clock()-starttime))
  50. end,
  51. })
  52. minetest.register_abm({
  53. label="crops getting ill",
  54. nodenames="group:infectable",
  55. intervall = 120,
  56. change=50,
  57. action = function(pos)
  58. local starttime=os.clock()
  59. local node=minetest.get_node(pos)
  60. if node.name == "air" or node.name == "ignore" then
  61. return
  62. end
  63. local ndef = minetest.registered_nodes[node.name]
  64. if ndef.groups.infectable == nil then
  65. return
  66. end
  67. local meta = minetest.get_meta(pos)
  68. local ill_rate=meta:get_int("farming:weakness")
  69. if ill_rate == nil then
  70. return
  71. else
  72. if ill_rate >0 then
  73. else
  74. ill_rate = 5
  75. end
  76. end
  77. if math.random(1,ill_rate)==1 then
  78. farming.plant_infect(pos)
  79. end
  80. -- table.insert(farming.time_ill,1000*(os.clock()-starttime))
  81. end
  82. })
  83. minetest.register_abm({
  84. label="Planting crops",
  85. nodenames = farming.change_soil,
  86. neighbors = {"air","group:grass","group:dry_grass"},
  87. interval = farming.abm_planting+math.random(-1,1), -- little noise
  88. chance = farming.abm_planting_chance,
  89. action = function(pos)
  90. local starttime=os.clock()
  91. local ptabove={x=pos.x,y=pos.y+1,z=pos.z}
  92. local above = minetest.get_node(ptabove)
  93. if above.name ~= "air" then
  94. if (minetest.get_item_group(above.name, "grass")==0) or (minetest.get_item_group(above.name, "dry_grass")==0) then
  95. return
  96. end
  97. end
  98. local ptlight=minetest.get_node_light(ptabove)
  99. if ptlight < farming.min_light then
  100. return
  101. end
  102. local ptlight=minetest.get_node_light(ptabove,.5)
  103. if ptlight < farming.min_light then
  104. return
  105. end
  106. -- only for positions, where not too many plants are nearby
  107. -- first check if any crops are nearby, because the counting
  108. -- of nearby crops is time consuming
  109. if minetest.find_node_near(pos,4,"group:farming") ~= nil then
  110. if #minetest.find_nodes_in_area(vector.subtract(pos,4),vector.add(pos,4),"group:farming") > 2 then
  111. return
  112. end
  113. end
  114. local node_y=pos.y
  115. local sc={}
  116. for _,line in ipairs(farming.spreading_crops) do
  117. if line.y_min<=node_y and line.y_max>=node_y then
  118. local node_temp=minetest.get_heat(pos)
  119. if line.temp_min<=node_temp and line.temp_max>=node_temp then
  120. local node_hum=minetest.get_humidity(pos)
  121. if line.hum_min<=node_hum and line.hum_max>=node_hum then
  122. if line.light_min<ptlight and line.light_max >= ptlight then
  123. for k=1,line.base_rate do
  124. table.insert(sc,line.name)
  125. end
  126. end
  127. end
  128. end
  129. end
  130. end
  131. if #sc > 0 then
  132. minetest.add_node(ptabove, {name=sc[math.random(1,#sc)],param2=1})
  133. minetest.get_node_timer(ptabove):start(math.random(10, 15))
  134. farming.set_node_metadata(ptabove)
  135. end
  136. -- table.insert(farming.time_planting,1000*(os.clock()-starttime))
  137. end,
  138. })
  139. -- for optimisation only
  140. --minetest.register_on_shutdown(function()
  141. --[[
  142. for _,colu in ipairs({"time_plantinfect","time_plantcured","time_plantpunch","time_planting","time_ill","time_farming",
  143. "time_digharvest","time_steptimer","time_infect","time_seedtimer","time_wilttimer",
  144. "time_tooldig","time_usehook","time_calclight","time_placeseed","time_setmeta"}) do
  145. if (#farming[colu] > 0 ) then
  146. local tv=farming[colu]
  147. table.sort(tv)
  148. print(colu.." "..tv[math.ceil(#tv*0.25)].." - "..tv[math.ceil(#tv*0.5)].." - "..tv[math.ceil(#tv*0.75)])
  149. end
  150. end
  151. ]]
  152. --end)
  153. --[[
  154. for _,colu in ipairs({"time_plantinfect","time_plantcured","time_plantpunch","time_planting","time_ill","time_farming",
  155. "time_digharvest","time_steptimer","time_infect","time_seedtimer","time_wilttimer",
  156. "time_tooldig","time_usehook","time_placeseed","time_calclight","time_setmeta"}) do
  157. farming[colu]={}
  158. end
  159. ]]