adaptive-ai/moondrum.lua
2018-06-26 19:20:49 +02:00

189 lines
6 KiB
Lua

-- Vegetable food for adaptive AI
-- Mushroom
local helper = adaptive_ai.helper
local moondrum = {}
-- local round = adaptive_ai.calc.round
local ceil = math.ceil
local floor = math.floor
local range = adaptive_ai.population.default_range
local chat_log = adaptive_ai.chat_log
local spread = {-floor(range/2), floor(range/2)}
minetest.register_node("adaptive_ai:moondrum_mushroom", {
drawtype = "plantlike",
description = "Moondrum Mushroom",
tiles = {"aa_moondrum_mushroom.png"},
inventory_image = "aa_moondrum_mushroom.png",
wield_image = "aa_moondrum_mushroom.png",
paramtype = "light",
sunlight_propagates = true,
walkable = false,
buildable_to = true,
groups = {snappy = 3, attached_node = 1, flammable = 1},
sounds = default.node_sound_leaves_defaults(),
on_use = minetest.item_eat(2),
selection_box = {
type = "fixed",
fixed = {-4 / 16, -0.5, -4 / 16, 4 / 16, -1 / 16, 4 / 16},
},
})
minetest.register_node("adaptive_ai:moondrum_roots", {
description = "Moondrum Mycelium",
tiles = {"aa_moondrum_roots.png"},
groups = {cracky = 2},
drop = "adaptive_ai:moondrum_roots 1"
})
local airlike_groups = {"flora"}
local soil_groups = {"soil", "sand", "wood"}
local soil_nodes = {"group:soil", "group:sand", "group:wood"}
local search_surface = function(pos)
return helper.search_surface(pos, airlike_groups)
end
local is_soil = function(name)
return helper.get_node_group(name, soil_groups)
end
local is_airlike = function(name)
return name == "air" or helper.get_node_group(name, airlike_groups)
end
local plant_moondrum = function(pos)
if not pos then return end -- skip if nil
local soil = minetest.get_node(pos)
--chat_log("Planting moondrum on "..soil.name.."...")
if is_soil(soil.name) then
pos.y = pos.y + 1
minetest.swap_node(pos, {name = "adaptive_ai:moondrum_mushroom"})
--chat_log("Planting succesful!")
return true
end
--chat_log("Planting failed!")
return false
end
local plant_random_moondrum = function(pos)
-- local new_pos = {x = pos.x + math.random(-10, 10), y = pos.y, z = pos.z + math.random(-10, 10)}
local minp = {x = pos.x + spread[1], y = pos.y + spread[1]*2, z = pos.z + spread[1]}
local maxp = {x = pos.x + spread[2], y = pos.y + spread[2]*2, z = pos.z + spread[2]}
local nodelist = minetest.find_nodes_in_area_under_air(minp, maxp, soil_nodes)
local new_pos = nodelist and nodelist[math.random(#nodelist)] or nil
return plant_moondrum(new_pos)
end
local spawn_moondrum_mushroom = function(pos)
--chat_log("Spawning mushroom from roots...")
local p = {x = pos.x, y = pos.y, z = pos.z}
p = search_surface(p, airlike_groups)
p.y = p.y-1
local top = minetest.get_node(p) --{x=p.x, y=p.y-1, z=p.z}
if not top then
return
elseif top.name == "adaptive_ai:moondrum_mushroom" then
plant_random_moondrum(pos)
return
else -- if is_airlike
if plant_moondrum({x=p.x, y=p.y-1, z=p.z}) then
return
else
plant_random_moondrum(pos)
return
end
end
end
minetest.register_abm({
label = "Moondrum sprouting",
nodenames = {"adaptive_ai:moondrum_roots"},
interval = 5.0,
chance = 10,
action = function(pos, node, active_object_count, active_object_count_wider)
local minp = {x = pos.x + spread[1], y = pos.y + spread[1]*2, z = pos.z + spread[1]}
local maxp = {x = pos.x + spread[2], y = pos.y + spread[2]*2, z = pos.z + spread[2]}
local nearby_mushrooms = minetest.find_nodes_in_area_under_air(minp, maxp, "adaptive_ai:moondrum_mushroom")
if #nearby_mushrooms < 20 then
for i = 1,20-#nearby_mushrooms do
spawn_moondrum_mushroom(pos)
end
else
spawn_moondrum_mushroom(pos)
end
end
})
minetest.register_abm({
label = "Moondrum spreading",
nodenames = {"adaptive_ai:moondrum_mushroom"},
interval = 30.0,
chance = 20,
action = function(pos, node, active_object_count, active_object_count_wider)
local minp = {x = pos.x + spread[1], y = pos.y + spread[1]*2, z = pos.z + spread[1]}
local maxp = {x = pos.x + spread[2], y = pos.y + spread[2]*2, z = pos.z + spread[2]}
local nodelist = minetest.find_nodes_in_area_under_air(minp, maxp, soil_groups)
local new_pos = nodelist and nodelist[math.random(#nodelist)] or nil
plant_moondrum(new_pos)
--chat_log("Spreading moondrum from spores!")
end
})
--[[
minetest.register_abm({
label = "Moondrum sprouting near water",
nodenames = {"adaptive_ai:moondrum_roots"},
neighbors = {"default:water_source", "default:water_flowing"},
interval = 30.0,
chance = 1,
action = spawn_moondrum_mushroom
})
minetest.register_abm({
label = "Moondrum spreading near water"
nodenames = {"adaptive_ai:moondrum_mushroom"},
neighbors = {"default:water_source", "default:water_flowing"},
interval = 30.0,
chance = 2,
action = function(pos, node, active_object_count, active_object_count_wider)
new_pos = {x = pos.x + math.random(-2, 2), y = pos.y, z = pos.z + math.random(-2, 2)}
new_pos = search_surface(new_pos, valid_air)
plant_moondrum(new_pos)
--chat_log("Spreading moondrum from fertile mushroom spores!")
end
})
minetest.register_abm({
nodenames = {"adaptive_ai:moondrum_mushroom"},
interval = 10.0,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
local objs = minetest.get_objects_inside_radius(pos, imps.vision)
local imp = {}
for _,obj in ipairs(objs) do
imp = obj:get_luaimp()
if imp and imp.name == "adaptive_ai:imp" then
--chat_log("Imp found")
if not imp.stats.closest_food_pos or distance(obj:getpos(), imp.stats.closest_food_pos) > distance(obj:getpos(), pos) then
imp.stats.closest_food_pos = pos
end
end
end
end
})
--]]
adaptive_ai.moondrum = moondrum