water_life/spawn.lua

121 lines
5.4 KiB
Lua
Raw Normal View History

2019-11-30 13:01:47 +01:00
local timer = 0
local pi = math.pi
local function spawnstep(dtime)
timer = timer + dtime
if timer > 5 then
for _,plyr in ipairs(minetest.get_connected_players()) do
local coin = math.random(1000)
--minetest.chat_send_all(dump(coin))
if plyr then -- each player gets a spawn chance every 5s on average
local pos = plyr:get_pos()
local yaw = plyr:get_look_horizontal()
local animal = water_life.count_objects(pos)
2020-01-09 17:21:22 +07:00
--minetest.chat_send_all("yaw = "..dump(yaw).." mobs: "..dump(animal.all).." sharks: "..dump(animal.sharks).." whales: "..dump(animal.whales))
2019-11-30 13:01:47 +01:00
if animal.all > water_life.maxmobs then break end
-- find a pos randomly in look direction of player
2020-01-09 17:21:22 +07:00
local radius = (water_life.abr * 12) - 1 -- 75% from 16 = 12 nodes
2019-11-30 13:01:47 +01:00
radius = math.random(7,radius)
2020-01-09 17:21:22 +07:00
local angel = math.random() * 1.1781 -- look for random angel 0 - 75 degrees
2019-11-30 13:01:47 +01:00
if water_life.leftorright() then yaw = yaw + angel else yaw = yaw - angel end -- add or substract to/from yaw
local pos2 = mobkit.pos_translate2d(pos,yaw,radius)
2020-01-09 17:21:22 +07:00
pos2.y = pos2.y - math.random(water_life.abr * 5)
local node = minetest.get_node(pos2)
--minetest.chat_send_all(dump(node.name))
local liquidflag = nil
2019-11-30 13:01:47 +01:00
2020-01-09 17:21:22 +07:00
if node.name == "default:water_source" then
2019-11-30 13:01:47 +01:00
2020-01-09 17:21:22 +07:00
liquidflag = "sea"
elseif node.name == "default:river_water_source" then
2019-11-30 13:01:47 +01:00
2020-01-09 17:21:22 +07:00
liquidflag = "river"
end
if liquidflag then
2019-11-30 13:01:47 +01:00
local mobname = 'water_life:shark'
if water_life.shark_spawn_rate >= coin then
2020-01-09 17:21:22 +07:00
if animal.sharks < (water_life.maxsharks) and liquidflag == "sea" then
local a=pos2.x
local b=pos2.y
local c=pos2.z
local water = minetest.find_nodes_in_area({x=a-3, y=b-3, z=c-3}, {x=a+4, y=b+4, z=c+4}, {"default:water_source"})
if #water > 128 then -- sharks need water, much water
2019-11-30 13:01:47 +01:00
2020-01-09 17:21:22 +07:00
local obj=minetest.add_entity(pos2,mobname) -- ok spawn it already damnit
end
end
2019-11-30 13:01:47 +01:00
end
local mobname = 'water_life:fish'
2020-01-09 17:21:22 +07:00
local nearlife = water_life.count_objects(pos2,16)
if water_life.fish_spawn_rate >= coin and ((animal.all < (water_life.maxmobs-5)) or nearlife.fish > 5) and liquidflag == "river" then
--pos2.y = height+1.01
2019-11-30 13:01:47 +01:00
local a=pos2.x
local b=pos2.y
local c=pos2.z
2020-02-01 15:33:08 +07:00
local table = minetest.get_biome_data(pos)
if table and minetest.get_biome_name(table.biome) == "rainforest" then mobname = "water_life:piranha" end
2020-01-09 17:21:22 +07:00
2019-11-30 13:01:47 +01:00
local water = minetest.find_nodes_in_area({x=a-2, y=b-2, z=c-2}, {x=a+2, y=b+2, z=c+2}, {"default:river_water_source"})
2020-02-01 15:33:08 +07:00
if water and #water > 10 then -- little fish need little water
if mobname == "water_life:fish" then
local obj=minetest.add_entity(pos2,mobname) -- ok spawn it already damnit
else
for i = 1,3,1 do
local obj=minetest.add_entity(pos2,mobname) -- ok spawn it already damnit
end
end
2020-01-09 17:21:22 +07:00
end
2019-11-30 13:01:47 +01:00
end
2020-01-09 17:21:22 +07:00
if water_life.whale_spawn_rate >= coin and animal.whales < (water_life.maxwhales) and liquidflag == "sea" then
pos2.y = pos2.y -4
2019-11-30 13:01:47 +01:00
mobname = 'water_life:whale'
local a=pos2.x
local b=pos2.y
local c=pos2.z
local water = minetest.find_nodes_in_area({x=a-5, y=b-5, z=c-5}, {x=a+5, y=b+5, z=c+5}, {"default:water_source"})
2020-01-10 17:12:25 +07:00
--minetest.chat_send_all(dump(#water))
2020-01-09 17:21:22 +07:00
if #water > 900 then -- whales need water, much water
local obj=minetest.add_entity(pos2,mobname) -- ok spawn it already damnit
end
2019-11-30 13:01:47 +01:00
end
end
end
end
timer = 0
end
end
minetest.register_globalstep(spawnstep)