water_life/spawn.lua

238 lines
9.2 KiB
Lua
Raw Normal View History

2019-11-30 13:01:47 +01:00
local timer = 0
local pi = math.pi
2020-02-05 21:33:11 +07:00
local random = water_life.random
2019-11-30 13:01:47 +01:00
2020-06-06 09:17:40 +02:00
local function getcount(name)
if not name then
return 0
else
return name
end
end
2019-11-30 13:01:47 +01:00
local function spawnstep(dtime)
timer = timer + dtime
2020-04-04 13:38:39 +02:00
if timer > 10 then
2019-11-30 13:01:47 +01:00
for _,plyr in ipairs(minetest.get_connected_players()) do
2019-11-30 13:01:47 +01:00
local toomuch = false
2020-05-31 18:51:00 +02:00
if plyr and plyr:is_player() and plyr:get_pos().y > -50 and plyr:get_pos().y < 150 then -- each player gets a spawn chance every 10s on average
2019-11-30 13:01:47 +01:00
local pos = plyr:get_pos()
local yaw = plyr:get_look_horizontal()
local animal = water_life.count_objects(pos)
2019-11-30 13:01:47 +01:00
if animal.all > water_life.maxmobs then toomuch = true end
2019-11-30 13:01:47 +01:00
-- find a pos randomly in look direction of player
local radius = (water_life.abr * 12) -- 75% from 16 = 12 nodes
radius = random(7,radius) -- not nearer than 7 nodes in front of player
local angel = math.rad(random(75)) -- look for random angel 0 - 75 degrees
if water_life.leftorright() then yaw = yaw + angel else yaw = yaw - angel end -- add or substract to/from yaw
2019-11-30 13:01:47 +01:00
local pos2 = mobkit.pos_translate2d(pos,yaw,radius) -- calculate position
local depth = water_life.water_depth(pos2,25) -- get surface pos and water depth
2020-06-14 16:54:26 +02:00
local bdata = water_life_get_biome_data(pos2) -- get biome data at spaen position
2020-06-21 18:44:54 +02:00
local savepos = pos2
local dalam = depth.depth
local ground = {}
ground = depth.surface
if depth.depth > 0 then
if water_life.radar_debug then
2020-06-28 15:52:40 +02:00
water_life.temp_show(depth.surface,9,5)
minetest.chat_send_all(">>> Depth ="..dump(depth.depth).." <<< "..dump(depth.type))
2020-06-14 16:54:26 +02:00
minetest.chat_send_all(dump(bdata.name))
end
2020-06-21 18:44:54 +02:00
pos2 = depth.surface
2020-06-28 15:52:40 +02:00
ground = mobkit.pos_shift(ground,{y=(dalam*-1)})
end
local liquidflag = nil
2019-11-30 13:01:47 +01:00
if depth.type == "default:water_source" then
liquidflag = "sea"
2020-01-09 17:21:22 +07:00
elseif depth.type == "default:river_water_source" then
liquidflag = "river"
2020-02-05 21:33:11 +07:00
elseif depth.type == "water_life:muddy_river_water_source" then
liquidflag = "muddy"
2020-01-09 17:21:22 +07:00
end
2020-01-09 17:21:22 +07:00
if liquidflag and not toomuch then
2020-06-21 18:44:54 +02:00
--minetest.chat_send_all(dump(depth.surface))
2020-06-14 16:54:26 +02:00
local mobname = 'water_life:croc'
local faktor = 100 - getcount(animal[mobname]) * 33
if random(100) < faktor then
local fits = false
if string.match(bdata.name,"rainforest") or string.match(bdata.name,"savanna") then fits = true end
if depth.depth < 4 and fits then --shark min water depth
local obj=minetest.add_entity(depth.surface,mobname) -- ok spawn it already damnit
end
end
2020-06-28 15:52:40 +02:00
--minetest.chat_send_all(dump(minetest.pos_to_string(depth.surface)).." "..dump(minetest.pos_to_string(ground)))
local mobname = 'water_life:shark'
if water_life.shark_spawn_rate >= random(1000) then
2020-06-06 09:17:40 +02:00
local bcheck = water_life.count_objects(pos2,12)
2020-06-21 13:34:06 +02:00
if getcount(animal[mobname]) < water_life.maxsharks and liquidflag == "sea" and not bcheck["water_life:shark_buoy"]
and not animal["water_life:croc"] then
2020-01-09 17:21:22 +07:00
if depth.depth > 4 then --shark min water depth
2020-06-28 15:52:40 +02:00
local obj=minetest.add_entity(mobkit.pos_shift(ground,{y=2}),mobname) -- spawn it 2 nodes above sea ground
end
end
2019-11-30 13:01:47 +01:00
end
2020-06-28 15:52:40 +02:00
--minetest.chat_send_all(dump(minetest.pos_to_string(depth.surface)).." "..dump(minetest.pos_to_string(ground)))
mobname = "water_life:urchin"
if water_life.urchin_spawn_rate >= random(1000) then
2020-06-21 18:44:54 +02:00
local coraltable = minetest.find_nodes_in_area({x=ground.x-5, y=ground.y-2, z=ground.z-5}, {x=ground.x+5, y=ground.y+2, z=ground.z+5}, water_life.urchinspawn)
2020-06-06 09:17:40 +02:00
--local nearlife = water_life.count_objects(ground,5,"water_life:urchin")
if coraltable and #coraltable > 0 and getcount(animal[mobname]) < 15 and liquidflag == "sea" then
local coralpos = coraltable[random(#coraltable)]
coralpos.y = coralpos.y +1
local node = minetest.get_node(coralpos)
2020-02-11 18:29:16 +07:00
if node.name == "default:water_source" then
local obj= water_life.set_urchin(coralpos) --minetest.add_entity(coralpos,mobname)
end
end
end
2020-06-28 15:52:40 +02:00
--minetest.chat_send_all(dump(minetest.pos_to_string(depth.surface)).." "..dump(minetest.pos_to_string(ground)))
mobname = "water_life:clams"
if water_life.clams_spawn_rate >= random(1000) then
2020-06-28 15:52:40 +02:00
local clpos1 = mobkit.pos_shift(ground,{x=-8, y=-2, z=8})
local clpos2 = mobkit.pos_shift(ground,{x=8, y=2, z=8})
local coraltable = minetest.find_nodes_in_area(clpos1, clpos2, water_life.clams_spawn)
--minetest.chat_send_all("seagrass ="..dump(#coraltable))
local nearlife = water_life.count_objects(ground,8,"water_life:clams")
if coraltable and #coraltable > 0 and getcount(animal[mobname]) < 10 and liquidflag == "sea" then
local coralpos = mobkit.pos_shift(coraltable[random(#coraltable)],{y=1})
local node = minetest.get_node(coralpos)
if node.name == "default:water_source" then
2020-06-28 15:52:40 +02:00
local obj= water_life.set_urchin(coralpos,"water_life:clams")
end
end
end
2020-06-28 15:52:40 +02:00
--minetest.chat_send_all(dump(minetest.pos_to_string(depth.surface)).." "..dump(minetest.pos_to_string(ground)))
mobname = "water_life:jellyfish"
2020-06-21 18:44:54 +02:00
2020-06-06 09:17:40 +02:00
local faktor = 100 - getcount(animal[mobname]) * 20
if random(100) < faktor and liquidflag == "sea" then
2020-06-28 15:52:40 +02:00
local obj=minetest.add_entity(mobkit.pos_shift(ground,{y=2}),mobname)
end
2020-05-31 18:51:00 +02:00
mobname = "water_life:coralfish"
2020-06-28 15:52:40 +02:00
--minetest.chat_send_all(dump(minetest.pos_to_string(depth.surface)).." "..dump(minetest.pos_to_string(ground)))
2020-06-06 18:21:35 +02:00
2020-06-17 06:42:07 +02:00
local coraltable = minetest.find_nodes_in_area({x=ground.x-5, y=ground.y-2, z=ground.z-5}, {x=ground.x+5, y=ground.y+2, z=ground.z+5}, water_life.urchinspawn)
2020-06-06 09:17:40 +02:00
--local nearlife = water_life.count_objects(ground,nil,mobname)
2020-06-06 18:21:35 +02:00
faktor = 100 - getcount(animal[mobname]) * 6.66
2020-05-31 18:51:00 +02:00
if random(100) < faktor and liquidflag == "sea" and #coraltable > 1 then
local cfish = coraltable[random(#coraltable)]
cfish.y = cfish.y +1
2020-06-06 18:21:35 +02:00
local maxfish = random(3,7)
for i = 1,maxfish,1 do
local obj=minetest.add_entity(cfish,mobname)
if obj then
local entity = obj:get_luaentity()
entity.base = cfish
entity.head = random(65535)
end
end
2020-05-31 18:51:00 +02:00
end
2020-06-28 15:52:40 +02:00
--minetest.chat_send_all(dump(minetest.pos_to_string(depth.surface)).." "..dump(minetest.pos_to_string(ground)))
2020-05-31 18:51:00 +02:00
mobname = "water_life:clownfish"
2020-06-06 18:21:35 +02:00
faktor = 100 - getcount(animal[mobname]) * 50
2020-05-31 18:51:00 +02:00
if random(100) < faktor and liquidflag == "sea" and #coraltable > 1 then
local cfish = coraltable[random(#coraltable)]
cfish.y = cfish.y +1
local obj=minetest.add_entity(cfish,mobname)
2020-06-06 18:21:35 +02:00
if obj then
local entity = obj:get_luaentity()
entity.base = cfish
end
2020-05-31 18:51:00 +02:00
end
2020-06-28 15:52:40 +02:00
--minetest.chat_send_all(dump(minetest.pos_to_string(depth.surface)).." "..dump(minetest.pos_to_string(ground)))
2020-05-31 18:51:00 +02:00
mobname = 'water_life:fish'
2020-06-06 09:17:40 +02:00
--local nearlife = water_life.count_objects(pos2,24,"water_life:piranha")
if water_life.fish_spawn_rate >= random(1000) and ((animal.all < (water_life.maxmobs-5)) or getcount(animal[mobname]) < 5) and (liquidflag == "river" or liquidflag == "muddy") then
2019-11-30 13:01:47 +01:00
local table = minetest.get_biome_data(pos)
if table and water_life.piranha_biomes[minetest.get_biome_name(table.biome)] then mobname = "water_life:piranha" end
if depth.depth > 2 then -- min water depth for piranha and riverfish
2020-02-01 15:33:08 +07:00
if mobname == "water_life:fish" then
local obj=minetest.add_entity(pos2,mobname) -- ok spawn it already damnit
else
2020-06-06 09:17:40 +02:00
if getcount(animal[mobname]) < 10 then
2020-02-01 16:11:34 +07:00
for i = 1,3,1 do
local obj=minetest.add_entity(pos2,mobname) -- ok spawn it already damnit
2020-02-01 16:11:34 +07:00
end
2020-02-01 15:33:08 +07:00
end
end
end
2019-11-30 13:01:47 +01:00
end
2020-06-21 18:44:54 +02:00
2020-06-28 15:52:40 +02:00
--minetest.chat_send_all(dump(minetest.pos_to_string(depth.surface)).." "..dump(minetest.pos_to_string(ground)))
mobname = 'water_life:whale'
2020-06-06 09:17:40 +02:00
if water_life.whale_spawn_rate >= random(1000) and getcount(animal[mobname]) < (water_life.maxwhales) and liquidflag == "sea" then
2020-06-28 15:52:40 +02:00
if depth.depth > 8 then -- min water depth for whales
local gotwhale = true
local whpos = mobkit.pos_shift(depth.surface,{y=-3})
for i = 0,3,1 do
local whpos2 = mobkit.pos_translate2d(whpos,math.rad(i*90),30)
local under = water_life.find_collision(whpos,whpos2, false)
2020-06-28 15:52:40 +02:00
minetest.chat_send_all(dump(under))
if under and under < 25 then
gotwhale = false
break
end
end
if gotwhale then local obj=minetest.add_entity(depth.surface,mobname) end -- ok spawn it already damnit
end
end
2019-11-30 13:01:47 +01:00
end
end
end
timer = 0
end
2019-11-30 13:01:47 +01:00
end
minetest.register_globalstep(spawnstep)