mod-sneeker/init.lua

433 lines
10 KiB
Lua
Raw Normal View History

2021-05-07 02:32:24 +02:00
2021-05-08 20:42:35 +02:00
sneeker = {}
sneeker.modname = core.get_current_modname()
sneeker.modpath = core.get_modpath(sneeker.modname)
2021-05-07 02:32:24 +02:00
2021-05-08 23:46:27 +02:00
local debugging = core.settings:get_bool("enable_debug_mods", false)
sneeker.log = function(lvl, msg)
if lvl == "debug" and not debugging then return end
if msg == nil then
msg = lvl
lvl = nil
end
msg = "[" .. sneeker.modname .. "] " .. msg
if lvl == "debug" then
msg = "[DEBUG]" .. msg
lvl = nil;
end
if not lvl then
core.log(msg)
else
core.log(lvl, msg)
end
end
local scripts = {
"settings",
"tnt_function",
"spawn",
}
for _, script in ipairs(scripts) do
dofile(sneeker.modpath .. "/" .. script .. ".lua")
end
2021-05-07 02:32:24 +02:00
2021-05-08 20:42:35 +02:00
local function jump(self, pos, direction)
2021-05-07 02:58:47 +02:00
local velocity = self.object:get_velocity()
2021-05-08 20:42:35 +02:00
if core.registered_nodes[core.get_node(pos).name].climbable then
self.object:set_velocity({x=velocity.x, y=4, z=velocity.z})
2021-05-07 02:32:24 +02:00
return
end
2021-05-08 20:42:35 +02:00
local spos = {x=pos.x+direction.x, y=pos.y, z=pos.z+direction.z}
local node = core.get_node_or_nil(spos)
2021-05-07 02:32:24 +02:00
spos.y = spos.y+1
2021-05-08 20:42:35 +02:00
local node2 = core.get_node_or_nil(spos)
local def, def2 = {}
2021-05-07 02:32:24 +02:00
if node and node.name then
2021-05-08 20:42:35 +02:00
def = core.registered_items[node.name]
2021-05-07 02:32:24 +02:00
end
if node2 and node2.name then
2021-05-08 20:42:35 +02:00
def2 = core.registered_items[node2.name]
2021-05-07 02:32:24 +02:00
end
if def and def.walkable
and def2 and not def2.walkable
and def.drawtype ~= "fencelike" then
2021-05-07 02:58:47 +02:00
self.object:set_velocity({
2021-05-08 20:42:35 +02:00
x = velocity.x*2.2,
y = self.jump_height,
z = velocity.z*2.2
2021-05-07 02:32:24 +02:00
})
end
end
local function random_turn(self)
2021-05-08 20:42:35 +02:00
if self.turn_timer > math.random(2, 5) then
local select_turn = math.random(1, 3)
2021-05-07 02:32:24 +02:00
if select_turn == 1 then
self.turn = "left"
elseif select_turn == 2 then
self.turn = "right"
elseif select_turn == 3 then
self.turn = "straight"
end
self.turn_timer = 0
self.turn_speed = 0.05*math.random()
end
end
local def = {
hp_max = 20,
physical = true,
2021-05-08 20:42:35 +02:00
collisionbox = {-0.25, -0.7, -0.25, 0.25, 0.8, 0.25},
2021-05-07 02:32:24 +02:00
visual = "mesh",
mesh = "character.b3d",
2021-05-08 20:18:50 +02:00
textures = {"sneeker.png"},
2021-05-07 02:32:24 +02:00
makes_footstep_sound = false,
-- Original
animation = {
stand_START = 0,
stand_END = 79,
walk_START = 168,
walk_END = 187
},
walk_speed = 1.5,
jump_height = 5,
animation_speed = 30,
knockback_level = 2
}
2021-05-08 20:42:35 +02:00
def.on_activate = function(self, staticdata)
2021-05-07 02:32:24 +02:00
self.yaw = 0
self.anim = 1
self.timer = 0
self.visualx = 1
self.jump_timer = 0
self.turn_timer = 0
self.turn_speed = 0
self.powered = false
self.knockback = false
2021-05-08 20:42:35 +02:00
self.state = math.random(1, 2)
2021-05-07 02:58:47 +02:00
self.old_y = self.object:get_pos().y
-- despawning
2021-05-09 01:59:34 +02:00
self.lifetime = sneeker.lifetime
self.lifetimer = 0
self.check_despawn_player_distance = true
2021-05-07 02:32:24 +02:00
2021-05-08 20:42:35 +02:00
local data = core.deserialize(staticdata)
2021-05-07 02:32:24 +02:00
if data and type(data) == "table" then
if data.powered == true then
self.powered = true
2021-05-08 20:18:50 +02:00
self.object:set_properties({textures = {"sneeker_powered.png"}})
2021-05-07 02:32:24 +02:00
end
else
2021-05-08 20:42:35 +02:00
if math.random(0, 20) == 20 then
2021-05-07 02:32:24 +02:00
self.powered = true
2021-05-08 20:18:50 +02:00
self.object:set_properties({textures = {"sneeker_powered.png"}})
2021-05-07 02:32:24 +02:00
end
end
end
local function isnan(n)
return tostring(n) == tostring((-1)^.5)
end
2021-05-07 02:32:24 +02:00
def.on_step = function(self, dtime)
2021-05-09 01:59:34 +02:00
-- update lifetime timer
-- FIXME: this is longer than realtime
self.lifetimer = self.lifetimer + dtime
if self.lifetimer >= self.lifetime then
-- TODO: should have a death animation
self.object:remove()
return true
end
2021-05-07 02:32:24 +02:00
if self.knockback then
2021-05-09 01:59:34 +02:00
return false
2021-05-07 02:32:24 +02:00
end
local ANIM_STAND = 1
local ANIM_WALK = 2
2021-05-07 02:58:47 +02:00
local pos = self.object:get_pos()
if sneeker.despawn_player_far then
-- run check about once per 60 seconds
local interval_reached = math.floor(self.lifetimer % 60) == 0
if not interval_reached and not self.check_despawn_player_distance then
self.check_despawn_player_distance = true
end
if interval_reached and self.check_despawn_player_distance then
local player_nearby = false
for _, entity in ipairs(core.get_objects_inside_radius(pos, sneeker.despawn_player_radius)) do
if entity:is_player() then
player_nearby = true
break
end
end
if not player_nearby then
self.object:remove()
return true
end
-- set flag to not check again until next interval
self.check_despawn_player_distance = false
end
end
2021-05-07 02:58:47 +02:00
local yaw = self.object:get_yaw()
2021-05-08 20:42:35 +02:00
local inside = core.get_objects_inside_radius(pos, 10)
2021-05-07 02:32:24 +02:00
local walk_speed = self.walk_speed
local animation = self.animation
local anim_speed = self.animation_speed
2021-05-07 02:58:47 +02:00
local velocity = self.object:get_velocity()
2021-05-07 02:32:24 +02:00
self.timer = self.timer+0.01
self.turn_timer = self.turn_timer+0.01
self.jump_timer = self.jump_timer+0.01
if not self.chase
2021-05-08 20:42:35 +02:00
and self.timer > math.random(2, 5) then
2021-05-07 02:32:24 +02:00
if math.random() > 0.8 then
self.state = "stand"
else
self.state = "walk"
end
self.timer = 0
end
if self.turn == "right" then
self.yaw = self.yaw+self.turn_speed
2021-05-07 02:58:47 +02:00
self.object:set_yaw(self.yaw)
2021-05-07 02:32:24 +02:00
elseif self.turn == "left" then
self.yaw = self.yaw-self.turn_speed
2021-05-07 02:58:47 +02:00
self.object:set_yaw(self.yaw)
2021-05-07 02:32:24 +02:00
end
if self.chase and self.visualx < 2 then
if self.hiss == false then
2021-05-08 20:42:35 +02:00
core.sound_play("sneeker_hiss", {pos=pos, gain=1.5, max_hear_distance=2*64})
2021-05-07 02:32:24 +02:00
end
self.visualx = self.visualx+0.05
self.object:set_properties({
2021-05-08 20:42:35 +02:00
visual_size = {x=self.visualx, y=1}
2021-05-07 02:32:24 +02:00
})
self.hiss = true
elseif self.visualx > 1 then
self.visualx = self.visualx-0.05
self.object:set_properties({
2021-05-08 20:42:35 +02:00
visual_size = {x=self.visualx, y=1}
2021-05-07 02:32:24 +02:00
})
self.hiss = false
end
self.chase = false
2021-05-08 20:42:35 +02:00
for _, object in ipairs(inside) do
2021-05-07 02:32:24 +02:00
if object:is_player() then
self.state = "chase"
end
end
if self.state == "stand" then
if self.anim ~= ANIM_STAND then
2021-05-08 20:42:35 +02:00
self.object:set_animation({x=animation.stand_START, y=animation.stand_END}, anim_speed, 0)
2021-05-07 02:32:24 +02:00
self.anim = ANIM_STAND
end
random_turn(self)
if velocity.x ~= 0
or velocity.z ~= 0 then
2021-05-08 20:42:35 +02:00
self.object:set_velocity({x=0, y=velocity.y, z=0})
2021-05-07 02:32:24 +02:00
end
end
if self.state == "walk" then
if self.anim ~= ANIM_WALK then
2021-05-08 20:42:35 +02:00
self.object:set_animation({x=animation.walk_START, y=animation.walk_END}, anim_speed, 0)
2021-05-07 02:32:24 +02:00
self.anim = ANIM_WALK
end
2021-05-08 20:42:35 +02:00
self.direction = {x=math.sin(yaw)*-1, y=-10, z=math.cos(yaw)}
2021-05-07 02:32:24 +02:00
if self.direction then
2021-05-08 20:42:35 +02:00
self.object:set_velocity({x=self.direction.x*walk_speed, y=velocity.y, z=self.direction.z*walk_speed})
2021-05-07 02:32:24 +02:00
end
random_turn(self)
2021-05-07 02:58:47 +02:00
local velocity = self.object:get_velocity()
2021-05-07 02:32:24 +02:00
if self.turn_timer > 1 then
local direction = self.direction
2021-05-08 20:42:35 +02:00
local npos = {x=pos.x+direction.x, y=pos.y+0.2, z=pos.z+direction.z}
2021-05-07 02:32:24 +02:00
if velocity.x == 0 or velocity.z == 0
2021-05-08 20:42:35 +02:00
or core.registered_nodes[core.get_node(npos).name].walkable then
local select_turn = math.random(1, 2)
2021-05-07 02:32:24 +02:00
if select_turn == 1 then
self.turn = "left"
elseif select_turn == 2 then
self.turn = "right"
end
self.turn_timer = 0
self.turn_speed = 0.05*math.random()
end
end
-- Jump
if self.jump_timer > 0.2 then
2021-05-08 20:42:35 +02:00
jump(self, pos, self.direction)
2021-05-07 02:32:24 +02:00
end
end
if self.state == "chase" then
if self.anim ~= ANIM_WALK then
2021-05-08 20:42:35 +02:00
self.object:set_animation({x=animation.walk_START, y=animation.walk_END}, anim_speed, 0)
2021-05-07 02:32:24 +02:00
self.anim = ANIM_WALK
end
self.turn = "straight"
2021-05-08 20:42:35 +02:00
local inside_2 = core.get_objects_inside_radius(pos, 2)
2021-05-07 02:32:24 +02:00
-- Boom
if #inside_2 ~= 0 then
2021-05-08 20:42:35 +02:00
for _, object in ipairs(inside_2) do
2021-05-07 02:32:24 +02:00
if object:is_player() and object:get_hp() ~= 0 then
self.chase = true
if self.visualx >= 2 then
self.object:remove()
2021-05-08 20:42:35 +02:00
sneeker.boom(pos, self.powered)
core.sound_play("sneeker_explode", {pos=pos, gain=sneeker.boom_gain, max_hear_distance=2*64})
return true
2021-05-07 02:32:24 +02:00
end
end
end
end
if #inside ~= 0 then
2021-05-08 20:42:35 +02:00
for _, object in ipairs(inside) do
2021-05-07 02:32:24 +02:00
if object:is_player() and object:get_hp() ~= 0 then
if #inside_2 ~= 0 then
2021-05-08 20:42:35 +02:00
for _, object in ipairs(inside_2) do
2021-05-07 02:32:24 +02:00
-- Stop move
if object:is_player() then
if self.anim ~= ANIM_STAND then
2021-05-08 20:42:35 +02:00
self.object:set_animation({x=animation.stand_START, y=animation.stand_END}, anim_speed, 0)
2021-05-07 02:32:24 +02:00
self.anim = ANIM_STAND
end
2021-05-08 20:42:35 +02:00
self.object:set_velocity({x=0, y=velocity.y, z=0})
2021-05-07 02:32:24 +02:00
return
end
end
end
2021-05-07 02:58:47 +02:00
local ppos = object:get_pos()
2021-05-08 20:42:35 +02:00
self.vec = {x=ppos.x-pos.x, y=ppos.y-pos.y, z=ppos.z-pos.z}
2021-05-07 02:32:24 +02:00
self.yaw = math.atan(self.vec.z/self.vec.x)+math.pi^2
if ppos.x > pos.x then
self.yaw = self.yaw+math.pi
end
self.yaw = self.yaw-2
2021-05-07 02:58:47 +02:00
self.object:set_yaw(self.yaw)
2021-05-08 20:42:35 +02:00
self.direction = {x=math.sin(self.yaw)*-1, y=0, z=math.cos(self.yaw)}
2021-05-07 02:32:24 +02:00
local direction = self.direction
-- FIXME: hack
local can_set = true
for _, c in ipairs({direction.x*2.5, direction.z*2.5}) do
if isnan(c) then
can_set = false
break
end
end
if can_set then
2021-05-08 20:42:35 +02:00
self.object:set_velocity({x=direction.x*2.5, y=velocity.y, z=direction.z*2.5})
end
2021-05-07 02:32:24 +02:00
-- Jump
if self.jump_timer > 0.2 then
2021-05-08 20:42:35 +02:00
jump(self, pos, direction)
2021-05-07 02:32:24 +02:00
end
end
end
else
self.state = "stand"
end
end
-- Swim
2021-05-08 20:42:35 +02:00
local node = core.get_node(pos)
if core.get_item_group(node.name, "water") ~= 0 then
self.object:set_acceleration({x=0, y=1, z=0})
2021-05-07 02:58:47 +02:00
local velocity = self.object:get_velocity()
if self.object:get_velocity().y > 5 then
2021-05-08 20:42:35 +02:00
self.object:set_velocity({x=0, y=velocity.y-velocity.y/2, z=0})
2021-05-07 02:32:24 +02:00
else
2021-05-08 20:42:35 +02:00
self.object:set_velocity({x=0, y=velocity.y+1, z=0})
2021-05-07 02:32:24 +02:00
end
else
2021-05-08 20:42:35 +02:00
self.object:set_acceleration({x=0, y=-10, z=0})
2021-05-07 02:32:24 +02:00
end
2021-05-09 01:59:34 +02:00
return true
2021-05-07 02:32:24 +02:00
end
2021-05-08 20:42:35 +02:00
def.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
2021-05-07 02:32:24 +02:00
if self.knockback == false then
local knockback_level = self.knockback_level
2021-05-08 20:42:35 +02:00
self.object:set_velocity({x=dir.x*knockback_level, y=3, z=dir.z*knockback_level})
2021-05-07 02:32:24 +02:00
self.knockback = true
2021-05-08 20:42:35 +02:00
core.after(0.6, function()
2021-05-07 02:32:24 +02:00
self.knockback = false
end)
end
if self.object:get_hp() < 1 then
2021-05-07 02:58:47 +02:00
local pos = self.object:get_pos()
2021-05-08 20:42:35 +02:00
local x = 1/math.random(1, 5)*dir.x
local z = 1/math.random(1, 5)*dir.z
local p = {x=pos.x+x, y=pos.y, z=pos.z+z}
local node = core.get_node_or_nil(p)
2021-05-07 02:32:24 +02:00
if node == nil or not node.name or node.name ~= "air" then
p = pos
end
2021-05-08 20:42:35 +02:00
local obj = core.add_item(p, {name="tnt:gunpowder", count=math.random(0, 2)})
2021-05-07 02:32:24 +02:00
end
end
def.get_staticdata = function(self)
2021-05-08 20:42:35 +02:00
return core.serialize({
2021-05-07 02:32:24 +02:00
powered = self.powered
})
end
2021-05-08 20:42:35 +02:00
core.register_entity("sneeker:sneeker", def)
2021-05-07 02:32:24 +02:00
2021-05-08 20:42:35 +02:00
core.register_craftitem("sneeker:spawnegg", {
2021-05-08 20:18:50 +02:00
description = "Sneeker Spawn Egg",
inventory_image = "sneeker_spawnegg.png",
2021-05-07 02:32:24 +02:00
stack_max = 64,
2021-05-08 20:42:35 +02:00
on_place = function(itemstack, placer, pointed_thing)
2021-05-07 02:32:24 +02:00
if pointed_thing.type == "node" then
local pos = pointed_thing.above
pos.y = pos.y+1
2021-05-08 20:42:35 +02:00
core.add_entity(pos, "sneeker:sneeker")
2021-05-08 20:44:42 +02:00
if not core.settings:get_bool("creative_mode", false) then
2021-05-07 02:32:24 +02:00
itemstack:take_item()
end
return itemstack
end
end
})