addsharks

This commit is contained in:
berengma 2019-10-09 17:50:59 +02:00
parent 252266f5d0
commit 7f17e87272
6 changed files with 211 additions and 11 deletions

222
init.lua
View File

@ -1,10 +1,20 @@
math.randomseed(os.time()) --init random seed
minetest.register_entity(":sharks:shark", {
on_activate = function(self, staticdata)
self.object:remove()
end,
})
local abr = minetest.get_mapgen_setting('active_block_range') or 1
local abo = minetest.get_mapgen_setting('active_object_send_range_blocks') or 2
local nodename_water = minetest.registered_aliases.mapgen_water_source
local maxwhales = 1 -- (2 ^ (abo -1)) + 2
local maxwhales = 1
local maxsharks = (2 ^ (abr -1)) + 1
local abs = math.abs
local pi = math.pi
@ -23,6 +33,29 @@ local spawn_reduction = minetest.settings:get('whale_spawn_reduction') or 0.4
local function leftorright()
local rnd = math.random()
if rnd > 0.5 then return true else return false end
end
-- count sharks at position
local function count_sharks(pos)
local all_objects = minetest.get_objects_inside_radius(pos, abo * 16)
local sharks = 0
local _,obj
for _,obj in ipairs(all_objects) do
local entity = obj:get_luaentity()
if entity and entity.name == "water_life:shark" then
sharks = sharks +1
end
end
return sharks
end
-- count whales at position
local function count_whales(pos)
@ -152,17 +185,17 @@ local function chose_turn(self,pos,yaw)
local revpos1 = mobkit.pos_shift(revpos,{x=-3,y=-2,z=-3})
revpos = mobkit.pos_shift(revpos,{x=3,y=3,z=3})
local ccheck= minetest.find_nodes_in_area(clockpos,clockpos1, {"group:water"})
local rcheck = minetest.find_nodes_in_area(revpos,revpos1, {"group:water"})
local ccheck= minetest.find_nodes_in_area(clockpos,clockpos1, {"group:water","default:sand_with_kelp"})
local rcheck = minetest.find_nodes_in_area(revpos,revpos1, {"group:water","default:sand_with_kelp"})
--minetest.chat_send_all(dump(#rcheck).." : "..dump(#ccheck).." "..dump(remember).." --> "..dump(self.isonground))
local a = #ccheck
local b = #rcheck
if a > b+25 then
if a > b+15 then
mobkit.remember(self,"turn", "1")
return false
elseif a < b-25 then
elseif a < b-15 then
mobkit.remember(self,"turn","0")
return true
@ -205,8 +238,8 @@ local function whale_brain(self)
yaw = yaw - pi
local vcheck= minetest.find_nodes_in_area(up,down, {"group:water"})
local hcheck = minetest.find_nodes_in_area(left,right, {"group:water"})
local vcheck= minetest.find_nodes_in_area(up,down, {"group:water","default:sand_with_kelp"})
local hcheck = minetest.find_nodes_in_area(left,right, {"group:water","default:sand_with_kelp"})
--minetest.chat_send_all(dump(#vcheck).." - "..dump(#hcheck))
if #vcheck < 54 or #hcheck < 49 then
mobkit.clear_queue_high(self)
@ -308,7 +341,7 @@ minetest.register_entity("water_life:whale",{
collide_with_objects = true,
collisionbox = {-3, -2, -3, 3, 2, 3},
visual = "mesh",
mesh = "whale.b3d",
mesh = "water_life_whale.b3d",
textures = {"water_life_whale.png"},
visual_size = {x = 3.5, y = 3.5},
static_save = true,
@ -327,9 +360,9 @@ minetest.register_entity("water_life:whale",{
timeout=300,
attack={range=1.5,damage_groups={fleshy=15}},
sounds = {
random = "whale_1",
death = "whale_1",
distance = 128,
random = "water_life_whale",
death = "water_life_whale",
distance = 50,
},
animation = {
@ -355,3 +388,170 @@ minetest.register_entity("water_life:whale",{
})
minetest.register_globalstep(spawnstep)
--sharks
local function shark_brain(self)
if self.hp <= 0 then
mobkit.clear_queue_high(self)
mobkit.hq_die(self)
return
end
if mobkit.timer(self,1) then
local whale = mobkit.get_closest_entity(self,"water_life:whale")
if whale then
local spos = self.object:get_pos()
local wpos = whale:get_pos()
local distance = math.floor(vector.distance(spos,wpos))
if distance < 15 then
local yaw = self.object:get_yaw()
mobkit.clear_queue_high(self)
mobkit.hq_aqua_turn(self,40,yaw+(pi/2),5)
end
end
local prty = mobkit.get_queue_priority(self)
if prty < 20 then
local target = mobkit.get_nearby_player(self)
local food = mobkit.get_nearby_entity(self,"wildlife:deer")
if target and mobkit.is_alive(target) and mobkit.is_in_deep(target) then
mobkit.hq_aqua_attack(self,20,target,7)
end
if food and mobkit.is_in_deep(food) then
mobkit.clear_queue_high(self)
mobkit.hq_aqua_attack(self,30,food,7)
end
end
end
if mobkit.is_queue_empty_high(self) then mobkit.hq_aqua_roam(self,10,5) end
end
-- spawning is too specific to be included in the api, this is an example.
-- a modder will want to refer to specific names according to games/mods they're using
-- in order for mobs not to spawn on treetops, certain biomes etc.
local function shark_spawnstep(dtime)
for _,plyr in ipairs(minetest.get_connected_players()) do
if random()<dtime*0.2 then -- each player gets a spawn chance every 5s on average
local vel = plyr:get_player_velocity()
local spd = vector.length(vel)
local chance = spawn_rate * 1/(spd*0.75+1) -- chance is quadrupled for speed=4
local yaw
if spd > 1 then
-- spawn in the front arc
yaw = minetest.dir_to_yaw(vel) + random()*0.35 - 0.75
else
-- random yaw
yaw = random()*pi*2 - pi
end
local pos = plyr:get_pos()
local dir = vector.multiply(minetest.yaw_to_dir(yaw),abr*16)
local pos2 = vector.add(pos,dir)
pos2.y=pos2.y-5
local height, liquidflag = mobkit.get_terrain_height(pos2,32)
if not liquidflag then return end
if height and mobkit.nodeatpos({x=pos2.x,y=height-0.01,z=pos2.z}).is_ground_content then
local objs = minetest.get_objects_inside_radius(pos,abr*16+5)
local wcnt=0
local dcnt=0
local mobname = 'water_life:shark'
if liquidflag then -- sharks
local spnode = mobkit.nodeatpos({x=pos2.x,y=height+0.01,z=pos2.z})
local spnode2 = mobkit.nodeatpos({x=pos2.x,y=height+1.01,z=pos2.z}) -- node above to make sure won't spawn in shallows
nodename_water = nodename_water or minetest.registered_aliases.mapgen_water_source
if spnode and spnode2 and spnode.name == nodename_water and spnode2.name == nodename_water then
mobname = 'water_life:shark'
else
return
end
end
if chance < random() then
pos2.y = height+1.01
objs = minetest.get_objects_inside_radius(pos2,abr*16-2)
for _,obj in ipairs(objs) do -- do not spawn if another player around
if obj:is_player() then return end
end
local a=pos2.x
local b=pos2.y
local c=pos2.z
local water = minetest.find_nodes_in_area({x=a-4, y=b-4, z=c-4}, {x=a+4, y=b+4, z=c+4}, {"default:water_source"})
if #water < 128 then return end -- sharks need water, much water
local ms = count_sharks(pos)
--minetest.chat_send_all("Maxsharks = "..maxsharks.." counted: "..ms.." abo="..abo.." abr="..abr)
if ms > (maxsharks-1) then return end -- sharks are no sardines
local obj=minetest.add_entity(pos2,mobname) -- ok spawn it already damnit
end
end
end
end
end
minetest.register_entity("water_life:shark",{
-- common props
physical = true,
stepheight = 0.1, --EVIL!
collide_with_objects = true,
collisionbox = {-0.5, -0.3, -0.5, 0.5, 0.3, 0.5},
visual = "mesh",
mesh = "water_life_shark.b3d",
textures = {"water_life_shark3tex.png"},
visual_size = {x = 1.5, y = 1.5},
static_save = true,
makes_footstep_sound = true,
on_step = mobkit.stepfunc, -- required
on_activate = mobkit.actfunc, -- required
get_staticdata = mobkit.statfunc,
-- api props
springiness=0,
buoyancy = 0.98, -- portion of hitbox submerged
max_speed = 7, -- no matter which number is here, sharks always at same speed
jump_height = 1.26,
view_range = 32,
-- lung_capacity = 0, -- seconds
max_hp = 50,
timeout=60,
attack={range=0.8,damage_groups={fleshy=7}},
sounds = {
attack='water_life_sharkattack',
},
animation = {
def={range={x=1,y=59},speed=40,loop=true},
fast={range={x=1,y=59},speed=80,loop=true},
back={range={x=15,y=1},speed=-15,loop=false},
},
brainfunc = shark_brain,
on_punch=function(self, puncher, time_from_last_punch, tool_capabilities, dir)
if mobkit.is_alive(self) then
local hvel = vector.multiply(vector.normalize({x=dir.x,y=0,z=dir.z}),4)
self.object:set_velocity({x=hvel.x,y=2,z=hvel.z})
mobkit.hurt(self,tool_capabilities.damage_groups.fleshy or 1)
if type(puncher)=='userdata' and puncher:is_player() then -- if hit by a player
mobkit.clear_queue_high(self) -- abandon whatever they've been doing
mobkit.hq_aqua_attack(self,20,puncher,6) -- get revenge
end
end
end,
})
minetest.register_globalstep(shark_spawnstep)

BIN
models/water_life_shark.b3d Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 788 B