1
0
mirror of https://codeberg.org/tenplus1/mobs_redo.git synced 2025-01-08 08:50:20 +01:00

code tidy

This commit is contained in:
tenplus1 2024-12-26 08:48:35 +00:00
parent 219d477c2a
commit dca4159fc4
3 changed files with 36 additions and 94 deletions

124
api.lua
View File

@ -19,7 +19,7 @@ end
mobs = { mobs = {
mod = "redo", mod = "redo",
version = "20241220", version = "20241226",
spawning_mobs = {}, spawning_mobs = {},
translate = S, translate = S,
node_snow = has(minetest.registered_aliases["mapgen_snow"]) node_snow = has(minetest.registered_aliases["mapgen_snow"])
@ -31,24 +31,17 @@ mobs.fallback_node = mobs.node_dirt
-- localize common functions -- localize common functions
local pi = math.pi local pi, abs = math.pi, math.abs
local square = math.sqrt local square, random = math.sqrt, math.random
local sin = math.sin local sin, cos = math.sin, math.cos
local cos = math.cos local min, max = math.min, math.max
local abs = math.abs local floor, ceil = math.floor, math.ceil
local min = math.min local rad, deg = math.rad, math.deg
local max = math.max
local random = math.random
local floor = math.floor
local ceil = math.ceil
local rad = math.rad
local deg = math.deg
local atann = math.atan local atann = math.atan
local atan = function(x) local function atan(x)
if not x or x ~= x then return 0 else return atann(x) end if not x or x ~= x then return 0 else return atann(x) end
end end
local table_copy = table.copy local table_copy, table_remove = table.copy, table.remove
local table_remove = table.remove
local vdirection = vector.direction local vdirection = vector.direction
local vmultiply = vector.multiply local vmultiply = vector.multiply
local vsubtract = vector.subtract local vsubtract = vector.subtract
@ -81,7 +74,7 @@ local active_limit = tonumber(settings:get("mob_active_limit")) or 0
local mob_chance_multiplier = tonumber(settings:get("mob_chance_multiplier") or 1) local mob_chance_multiplier = tonumber(settings:get("mob_chance_multiplier") or 1)
local peaceful_player_enabled = settings:get_bool("enable_peaceful_player") local peaceful_player_enabled = settings:get_bool("enable_peaceful_player")
local mob_smooth_rotate = settings:get_bool("mob_smooth_rotate") ~= false local mob_smooth_rotate = settings:get_bool("mob_smooth_rotate") ~= false
local mob_height_fix = settings:get_bool("mob_height_fix") ~= false local mob_height_fix = settings:get_bool("mob_height_fix")
local mob_log_spawn = settings:get_bool("mob_log_spawn") == true local mob_log_spawn = settings:get_bool("mob_log_spawn") == true
local active_mobs = 0 local active_mobs = 0
@ -364,10 +357,8 @@ function mob_class:set_yaw(yaw, delay)
delay = mob_smooth_rotate and delay or 0 delay = mob_smooth_rotate and delay or 0
-- simplified yaw clamp -- simplified yaw clamp
if yaw > 6.283185 then if yaw > 6.283185 then yaw = yaw - 6.283185
yaw = yaw - 6.283185 elseif yaw < 0 then yaw = 6.283185 + yaw
elseif yaw < 0 then
yaw = 6.283185 + yaw
end end
if delay == 0 then self.object:set_yaw(yaw) ; return yaw ; end if delay == 0 then self.object:set_yaw(yaw) ; return yaw ; end
@ -543,7 +534,7 @@ function mob_class:do_stay_near()
{x = pos.x - r, y = pos.y - 1, z = pos.z - r}, {x = pos.x - r, y = pos.y - 1, z = pos.z - r},
{x = pos.x + r, y = pos.y + 1, z = pos.z + r}, self.stay_near[1]) {x = pos.x + r, y = pos.y + 1, z = pos.z + r}, self.stay_near[1])
if #nearby_nodes < 1 then return false end if #nearby_nodes == 0 then return false end
self:yaw_to_pos(nearby_nodes[random(#nearby_nodes)]) self:yaw_to_pos(nearby_nodes[random(#nearby_nodes)])
self:set_animation("walk") self:set_animation("walk")
@ -713,10 +704,7 @@ function mob_class:item_drop()
end end
if obj and obj:get_luaentity() then if obj and obj:get_luaentity() then
obj:set_velocity({x = random() - 0.5, y = 4, z = random() - 0.5})
obj:set_velocity({
x = random(-10, 10) / 9, y = 6, z = random(-10, 10) / 9})
elseif obj then elseif obj then
obj:remove() -- item does not exist obj:remove() -- item does not exist
end end
@ -812,9 +800,7 @@ function mob_class:check_for_death(cmi_cause)
-- reset vars and set state -- reset vars and set state
self.attack = nil self.attack = nil
self.following = nil self.following = nil
self.v_start = false self.v_start = false ; self.timer = 0 ; self.blinktimer = 0
self.timer = 0
self.blinktimer = 0
self.passive = true self.passive = true
self.state = "die" self.state = "die"
self.fly = false self.fly = false
@ -1495,14 +1481,12 @@ function mob_class:smart_mobs(s, p, dist, dtime)
if not has_lineofsight then if not has_lineofsight then
if los_switcher then if los_switcher then
use_pathfind = true use_pathfind = true ; los_switcher = false
los_switcher = false
end -- cannot see target! end -- cannot see target!
else else
if not los_switcher then if not los_switcher then
los_switcher = true los_switcher = true ; use_pathfind = false
use_pathfind = false
minetest.after(1, function(self) minetest.after(1, function(self)
@ -1830,8 +1814,7 @@ function mob_class:do_runaway_from()
if not self.runaway_from then return end if not self.runaway_from then return end
local s = self.object:get_pos() ; if not s then return end local s = self.object:get_pos() ; if not s then return end
local p, sp, dist, pname local p, sp, dist, pname, player, obj, min_player, name
local player, obj, min_player, name
local min_dist = self.view_range + 1 local min_dist = self.view_range + 1
local objs = minetest.get_objects_inside_radius(s, self.view_range) local objs = minetest.get_objects_inside_radius(s, self.view_range)
@ -1845,15 +1828,13 @@ function mob_class:do_runaway_from()
if is_invisible(self, pname) or self.owner == pname then if is_invisible(self, pname) or self.owner == pname then
name = "" name = ""
else else
player = objs[n] player = objs[n] ; name = "player"
name = "player"
end end
else else
obj = objs[n]:get_luaentity() obj = objs[n]:get_luaentity()
if obj then if obj then
player = obj.object player = obj.object ; name = obj.name or ""
name = obj.name or ""
end end
end end
@ -2022,9 +2003,7 @@ function mob_class:stop_attack()
self.attack = nil self.attack = nil
self.following = nil self.following = nil
self.v_start = false self.v_start = false ; self.timer = 0 ; self.blinktimer = 0
self.timer = 0
self.blinktimer = 0
self.path.way = nil self.path.way = nil
self:set_velocity(0) self:set_velocity(0)
self.state = "stand" self.state = "stand"
@ -2408,8 +2387,7 @@ function mob_class:do_states(dtime)
local p2, s2 = p, s local p2, s2 = p, s
p2.y = p2.y + .5 p2.y = p2.y + .5 ; s2.y = s2.y + .5
s2.y = s2.y + .5
if self:line_of_sight(p2, s2) then if self:line_of_sight(p2, s2) then
@ -2439,8 +2417,7 @@ function mob_class:do_states(dtime)
or (self.attack_type == "dogshoot" and dist > self.reach and or (self.attack_type == "dogshoot" and dist > self.reach and
self:dogswitch() == 0) then self:dogswitch() == 0) then
p.y = p.y - .5 p.y = p.y - .5 ; s.y = s.y + .5
s.y = s.y + .5
local vec = {x = p.x - s.x, y = p.y - s.y, z = p.z - s.z} local vec = {x = p.x - s.x, y = p.y - s.y, z = p.z - s.z}
@ -2867,9 +2844,7 @@ local function clean_staticdata(self)
t = type(stat) t = type(stat)
if t ~= "function" and t ~= "nil" and t ~= "userdata" if t ~= "function" and t ~= "nil" and t ~= "userdata"
and _ ~= "object" and _ ~= "_cmi_components" then and _ ~= "object" and _ ~= "_cmi_components" then tmp[_] = self[_] end
tmp[_] = self[_]
end
end end
return tmp return tmp
@ -2909,29 +2884,7 @@ function mob_class:mob_staticdata()
if use_cmi then if use_cmi then
self.serialized_cmi_components = cmi.serialize_components(self._cmi_components) self.serialized_cmi_components = cmi.serialize_components(self._cmi_components)
end end
--[[
-- move existing variables to new table for future compatibility
-- using self.initial_properties lost some variables when backing up?!?
if not self.backup_properties then
self.backup_properties = {
hp_max = self.hp_max,
physical = self.physical,
collisionbox = self.collisionbox,
selectionbox = self.selectionbox,
visual = self.visual,
visual_size = self.visual_size,
mesh = self.mesh,
textures = self.textures,
make_footstep_sound = self.make_footstep_sound,
stepheight = self.stepheight,
glow = self.glow,
-- nametag = self.nametag,
damage_texture_modifier = self.damage_texture_modifier,
-- infotext = self.infotext
}
end
]]
return minetest.serialize(clean_staticdata(self)) return minetest.serialize(clean_staticdata(self))
end end
@ -3003,10 +2956,8 @@ function mob_class:mob_activate(staticdata, def, dtime)
-- get texture, model and size -- get texture, model and size
local textures = self.base_texture local textures = self.base_texture
local mesh = self.base_mesh local mesh, vis_size = self.base_mesh, self.base_size
local vis_size = self.base_size local colbox, selbox = self.base_colbox, self.base_selbox
local colbox = self.base_colbox
local selbox = self.base_selbox
-- is there a specific texture if gotten -- is there a specific texture if gotten
if self.gotten and def.gotten_texture then textures = def.gotten_texture end if self.gotten and def.gotten_texture then textures = def.gotten_texture end
@ -3177,10 +3128,7 @@ function mob_class:on_step(dtime, moveresult)
if use_cmi then cmi.notify_step(self.object, dtime) end if use_cmi then cmi.notify_step(self.object, dtime) end
local pos = self.object:get_pos() local pos = self.object:get_pos()
local yaw = self.object:get_yaw() local yaw = self.object:get_yaw() ; if not yaw then return end
-- early warning check, if no yaw then no entity, skip rest of function
if not yaw then return end
self.node_timer = (self.node_timer or 0) + dtime self.node_timer = (self.node_timer or 0) + dtime
@ -3642,8 +3590,7 @@ function mobs:add_mob(pos, def)
end end
if def.owner then if def.owner then
ent.tamed = true ent.tamed = true ; ent.owner = def.owner
ent.owner = def.owner
end end
if def.nametag then if def.nametag then
@ -3890,13 +3837,10 @@ function mobs:spawn(def)
def.name, def.name,
def.nodes or {"group:soil", "group:stone"}, def.nodes or {"group:soil", "group:stone"},
def.neighbors or {"air"}, def.neighbors or {"air"},
def.min_light or 0, def.min_light or 0, def.max_light or 15,
def.max_light or 15, def.interval or 30, def.chance or 5000,
def.interval or 30,
def.chance or 5000,
def.active_object_count or 1, def.active_object_count or 1,
def.min_height or -31000, def.min_height or -31000, def.max_height or 31000,
def.max_height or 31000,
def.day_toggle, def.day_toggle,
def.on_spawn, def.on_spawn,
def.on_map_load) def.on_map_load)
@ -4520,8 +4464,7 @@ function mobs:feed_tame(self, clicker, feed_count, breed, tame)
and (name == self.owner or minetest.check_player_privs(name, "protection_bypass")) then and (name == self.owner or minetest.check_player_privs(name, "protection_bypass")) then
-- store mob and nametag stack in external variables -- store mob and nametag stack in external variables
mob_obj[name] = self mob_obj[name] = self ; mob_sta[name] = item
mob_sta[name] = item
local prop = self.object:get_properties() local prop = self.object:get_properties()
local tag = self._nametag or "" local tag = self._nametag or ""
@ -4584,8 +4527,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end end
-- reset external variables -- reset external variables
mob_obj[name] = nil mob_obj[name] = nil ; mob_sta[name] = nil
mob_sta[name] = nil
end end
end) end)

View File

@ -849,8 +849,8 @@ External Settings for "minetest.conf"
mob for obstructions before spawning, otherwise it mob for obstructions before spawning, otherwise it
defaults to checking the height of the mob only. defaults to checking the height of the mob only.
'mob_smooth_rotate' Enables smooth rotation when mobs turn by default. 'mob_smooth_rotate' Enables smooth rotation when mobs turn by default.
'mob_height_fix' Enabled by default, increases smaller mob heights so they wont 'mob_height_fix' Disabled by default, increases smaller mob heights so they
glitch through certain nodes. cannot glitch through certain nodes.
'mob_pathfinding_enable' Enable pathfinding. 'mob_pathfinding_enable' Enable pathfinding.
'mob_pathfinding_stuck_timeout' How long before stuck mobs start searching. (default 3.0) 'mob_pathfinding_stuck_timeout' How long before stuck mobs start searching. (default 3.0)
'mob_pathfinding_stuck_path_timeout' How long will mob follow path before giving up. (default 5.0) 'mob_pathfinding_stuck_path_timeout' How long will mob follow path before giving up. (default 5.0)

View File

@ -56,7 +56,7 @@ enable_peaceful_player (Mobs do not attack peaceful player without reason) bool
mob_smooth_rotate (Smooth rotation for mobs) bool true mob_smooth_rotate (Smooth rotation for mobs) bool true
# Fix Mob Height if too low so they cannot escape through specific nodes # Fix Mob Height if too low so they cannot escape through specific nodes
mob_height_fix (Fix Mob Height) bool true mob_height_fix (Fix Mob Height) bool false
mob_log_spawn (Log Mob Spawning) bool false mob_log_spawn (Log Mob Spawning) bool false