From d125d0a5dbad76881de869fac621ea1d0c28561a Mon Sep 17 00:00:00 2001 From: TenPlus1 Date: Thu, 21 May 2020 20:38:08 +0100 Subject: [PATCH] tweak and tidy code (thanks MoNTE48) --- api.lua | 119 ++++++++++++++++++++++++++-------------------------- crafts.lua | 6 +-- spawner.lua | 14 +++---- 3 files changed, 69 insertions(+), 70 deletions(-) diff --git a/api.lua b/api.lua index 87858a7..8fe9c87 100644 --- a/api.lua +++ b/api.lua @@ -6,7 +6,7 @@ local use_cmi = minetest.global_exists("cmi") mobs = { mod = "redo", - version = "20200519", + version = "20200521", intllib = S, invis = minetest.global_exists("invisibility") and invisibility or {} } @@ -27,9 +27,11 @@ local cos = math.cos local abs = math.abs local min = math.min local max = math.max -local atann = math.atan local random = math.random local floor = math.floor +local ceil = math.ceil +local rad = math.rad +local atann = math.atan local atan = function(x) if not x or x ~= x then --error("atan bassed NaN") @@ -47,7 +49,6 @@ local peaceful_only = minetest.settings:get_bool("only_peaceful_mobs") local disable_blood = minetest.settings:get_bool("mobs_disable_blood") local mobs_drop_items = minetest.settings:get_bool("mobs_drop_items") ~= false local mobs_griefing = minetest.settings:get_bool("mobs_griefing") ~= false -local creative = minetest.settings:get_bool("creative_mode") local spawn_protected = minetest.settings:get_bool("mobs_spawn_protected") ~= false local remove_far = minetest.settings:get_bool("remove_far_mobs") ~= false local difficulty = tonumber(minetest.settings:get("mob_difficulty")) or 1.0 @@ -163,7 +164,7 @@ function mob_class:mob_sound(sound) if self.child then pitch = pitch * 1.5 end -- a little random pitch to be different - pitch = pitch + math.random(-10, 10) * 0.005 + pitch = pitch + random(-10, 10) * 0.005 if sound then minetest.sound_play(sound, { @@ -532,7 +533,7 @@ function mob_class:attempt_flight_correction(override) return false end - local escape_target = flyable_nodes[math.random(1, #flyable_nodes)] + local escape_target = flyable_nodes[random(#flyable_nodes)] local escape_direction = vector.direction(pos, escape_target) self.object:set_velocity( @@ -608,7 +609,7 @@ function mob_class:do_stay_near() local searchnodes = self.stay_near[1] local chance = self.stay_near[2] or 10 - if random(1, chance) > 1 then + if random(chance) > 1 then return false end @@ -716,7 +717,7 @@ function mob_class:item_drop() for n = 1, #self.drops do - if random(1, self.drops[n].chance) == 1 then + if random(self.drops[n].chance) == 1 then num = random(self.drops[n].min or 0, self.drops[n].max or 1) item = self.drops[n].name @@ -1315,11 +1316,12 @@ function mob_class:breed() local pos = self.object:get_pos() - effect({x = pos.x, y = pos.y + 1, z = pos.z}, 8, "heart.png", 3, 4, 1, 0.1) + effect({x = pos.x, y = pos.y + 1, z = pos.z}, 8, + "heart.png", 3, 4, 1, 0.1) local objs = minetest.get_objects_inside_radius(pos, 3) local num = 0 - local ent = nil + local ent for n = 1, #objs do @@ -1333,12 +1335,12 @@ function mob_class:breed() if ent.name == self.name then canmate = true else - local entname = string.split(ent.name,":") - local selfname = string.split(self.name,":") + local entname = ent.name:split(":") + local selfname = self.name:split(":") if entname[1] == selfname[1] then - entname = string.split(entname[2],"_") - selfname = string.split(selfname[2],"_") + entname = entname[2]:split("_") + selfname = selfname[2]:split("_") if entname[1] == selfname[1] then canmate = true @@ -1446,7 +1448,7 @@ function mob_class:replace(pos) or not self.replace_what or self.child == true or vel.y ~= 0 - or random(1, self.replace_rate) > 1 then + or random(self.replace_rate) > 1 then return end @@ -1606,10 +1608,9 @@ function mob_class:smart_mobs(s, p, dist, dtime) end end + -- lets try find a path, first take care of positions + -- since pathfinder is very sensitive if use_pathfind then - -- lets try find a path, first take care of positions - -- since pathfinder is very sensitive - local sheight = self.collisionbox[5] - self.collisionbox[2] -- round position to center of node to avoid stuck in walls -- also adjust height for player models! @@ -1637,7 +1638,8 @@ function mob_class:smart_mobs(s, p, dist, dtime) local jumpheight = 0 if self.jump and self.jump_height >= 4 then - jumpheight = min(math.ceil(self.jump_height / 4), 4) + jumpheight = min(ceil(self.jump_height / 4), 4) + elseif self.stepheight > 0.5 then jumpheight = 1 end @@ -1689,7 +1691,7 @@ function mob_class:smart_mobs(s, p, dist, dtime) end end - local sheight = math.ceil(self.collisionbox[5]) + 1 + local sheight = ceil(self.collisionbox[5]) + 1 -- assume mob is 2 blocks high so it digs above its head s.y = s.y + sheight @@ -1821,7 +1823,8 @@ function mob_class:general_attack() if objs[n]:is_player() then -- if player invisible or mob cannot attack then remove from list - if self.attack_players == false + if not damage_enabled + or self.attack_players == false or (self.owner and self.type ~= "monster") or mobs.invis[objs[n]:get_player_name()] or not specific_attack(self.specific_attack, "player") then @@ -1874,7 +1877,7 @@ function mob_class:general_attack() end -- attack closest player or mob - if min_player and random(1, 100) > self.attack_chance then + if min_player and random(100) > self.attack_chance then self:do_attack(min_player) end end @@ -2061,8 +2064,6 @@ function mob_class:follow_flop() -- swimmers flop when out of their element, and swim again when back in if self.fly then - local s = self.object:get_pos() - if not self:attempt_flight_correction() then self.state = "flop" @@ -2115,7 +2116,7 @@ function mob_class:do_states(dtime) if self.state == "stand" then - if random(1, 4) == 1 then + if random(4) == 1 then local lp local s = self.object:get_pos() @@ -2146,7 +2147,7 @@ function mob_class:do_states(dtime) if self.order ~= "stand" and self.walk_chance ~= 0 and self.facing_fence ~= true - and random(1, 100) <= self.walk_chance + and random(100) <= self.walk_chance and self.at_cliff == false then self:set_velocity(self.walk_velocity) @@ -2157,7 +2158,7 @@ function mob_class:do_states(dtime) elseif self.state == "walk" then local s = self.object:get_pos() - local lp = nil + local lp -- is there something I need to avoid? if self.water_damage > 0 @@ -2203,7 +2204,7 @@ function mob_class:do_states(dtime) yaw = self:set_yaw(yaw, 8) -- otherwise randomly turn - elseif random(1, 100) <= 30 then + elseif random(100) <= 30 then yaw = yaw + random(-0.5, 0.5) @@ -2219,7 +2220,7 @@ function mob_class:do_states(dtime) -- stand for great fall in front if self.facing_fence == true or self.at_cliff - or random(1, 100) <= self.stand_chance then + or random(100) <= self.stand_chance then -- don't stand if mob flies and keep_flying set if (self.fly and not self.keep_flying) @@ -2273,7 +2274,8 @@ function mob_class:do_states(dtime) or not self.attack or not self.attack:get_pos() or self.attack:get_hp() <= 0 - or (self.attack:is_player() and mobs.invis[ self.attack:get_player_name() ]) then + or (self.attack:is_player() + and mobs.invis[ self.attack:get_player_name() ]) then --print(" ** stop attacking **", dist, self.view_range) @@ -2323,7 +2325,7 @@ function mob_class:do_states(dtime) self.timer = 0 self.blinktimer = 0 self.blinkstatus = false - self.object:settexturemod("") + self.object:set_texture_mod("") end -- walk right up to player unless the timer is active @@ -2540,9 +2542,11 @@ function mob_class:do_states(dtime) -- punch player (or what player is attached to) local attached = self.attack:get_attach() + if attached then self.attack = attached end + self.attack:punch(self.object, 1.0, { full_punch_interval = 1.0, damage_groups = {fleshy = self.damage} @@ -2568,8 +2572,6 @@ function mob_class:do_states(dtime) p.y = p.y - .5 s.y = s.y + .5 - local dist = get_distance(p, s) - local vec = { x = p.x - s.x, y = p.y - s.y, @@ -2582,7 +2584,7 @@ function mob_class:do_states(dtime) if self.shoot_interval and self.timer > self.shoot_interval - and random(1, 100) <= 60 then + and random(100) <= 60 then self.timer = 0 self:set_animation("shoot") @@ -2847,7 +2849,7 @@ function mob_class:on_punch(hitter, tflp, tool_capabilities, dir, damage) -- do we have a single blood texture or multiple? if type(self.blood_texture) == "table" then - blood = self.blood_texture[random(1, #self.blood_texture)] + blood = self.blood_texture[random(#self.blood_texture)] end effect(pos, amount, blood, 1, 2, 1.75, nil, nil, true) @@ -2871,10 +2873,11 @@ function mob_class:on_punch(hitter, tflp, tool_capabilities, dir, damage) if not self.object:get_luaentity() then return end - self.object:settexturemod("^[colorize:#c9900070") + self.object:set_texture_mod("^[colorize:#c9900070") - core.after(0.3, function() - self.object:settexturemod("") + minetest.after(0.3, function() + if not self.object:get_luaentity() then return end + self.object:set_texture_mod(self.texture_mods) end) end) ]] @@ -2942,7 +2945,7 @@ function mob_class:on_punch(hitter, tflp, tool_capabilities, dir, damage) -- alert others to the attack local objs = minetest.get_objects_inside_radius( hitter:get_pos(), self.view_range) - local obj = nil + local obj for n = 1, #objs do @@ -3002,7 +3005,7 @@ function mob_class:mob_staticdata() -- used to rotate older mobs if self.drawtype and self.drawtype == "side" then - self.rotate = math.rad(90) + self.rotate = rad(90) end if use_cmi then @@ -3077,7 +3080,7 @@ function mob_class:mob_activate(staticdata, def, dtime) end self.base_texture = def.textures and - def.textures[random(1, #def.textures)] + def.textures[random(#def.textures)] self.base_mesh = def.mesh self.base_size = self.visual_size self.base_colbox = self.collisionbox @@ -3385,7 +3388,7 @@ function mob_class:on_step(dtime, moveresult) end -- mob plays random sound at times - if random(1, 100) == 1 then + if random(100) == 1 then self:mob_sound(self.sounds.random) end @@ -3457,7 +3460,7 @@ minetest.register_entity(name, setmetatable({ do_custom = def.do_custom, jump_height = def.jump_height, drawtype = def.drawtype, -- DEPRECATED, use rotate instead - rotate = math.rad(def.rotate or 0), -- 0=front 90=side 180=back 270=side2 + rotate = rad(def.rotate or 0), -- 0=front 90=side 180=back 270=side2 glow = def.glow, lifetimer = def.lifetimer, hp_min = max(1, (def.hp_min or 5) * difficulty), @@ -3731,31 +3734,31 @@ function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light, -- do we have enough space to spawn mob? (thanks wuzzy) local ent = minetest.registered_entities[name] local width_x = max(1, - math.ceil(ent.collisionbox[4] - ent.collisionbox[1])) + ceil(ent.collisionbox[4] - ent.collisionbox[1])) local min_x, max_x if width_x % 2 == 0 then - max_x = math.floor(width_x / 2) + max_x = floor(width_x / 2) min_x = -(max_x - 1) else - max_x = math.floor(width_x / 2) + max_x = floor(width_x / 2) min_x = -max_x end local width_z = max(1, - math.ceil(ent.collisionbox[6] - ent.collisionbox[3])) + ceil(ent.collisionbox[6] - ent.collisionbox[3])) local min_z, max_z if width_z % 2 == 0 then - max_z = math.floor(width_z / 2) + max_z = floor(width_z / 2) min_z = -(max_z - 1) else - max_z = math.floor(width_z / 2) + max_z = floor(width_z / 2) min_z = -max_z end local max_y = max(0, - math.ceil(ent.collisionbox[5] - ent.collisionbox[2]) - 1) + ceil(ent.collisionbox[5] - ent.collisionbox[2]) - 1) for y = 0, max_y do for x = min_x, max_x do @@ -4015,7 +4018,7 @@ function mobs:register_egg(mob, desc, background, addegg, no_creative) local grp = {spawn_egg = 1} -- do NOT add this egg to creative inventory (e.g. dungeon master) - if creative and no_creative == true then + if no_creative == true then grp.not_in_creative_inventory = 1 end @@ -4058,8 +4061,8 @@ function mobs:register_egg(mob, desc, background, addegg, no_creative) pos.y = pos.y + 1 local data = itemstack:get_metadata() - local mob = minetest.add_entity(pos, mob, data) - local ent = mob and mob:get_luaentity() + local smob = minetest.add_entity(pos, mob, data) + local ent = smob and smob:get_luaentity() if not ent then return end -- sanity check @@ -4117,8 +4120,8 @@ function mobs:register_egg(mob, desc, background, addegg, no_creative) pos.y = pos.y + 1 - local mob = minetest.add_entity(pos, mob) - local ent = mob and mob:get_luaentity() + local smob = minetest.add_entity(pos, mob) + local ent = smob and smob:get_luaentity() if not ent then return end -- sanity check @@ -4248,7 +4251,7 @@ function mobs:capture_mob(self, clicker, chance_hand, chance_net, end -- calculate chance.. add to inventory if successful? - if chance and chance > 0 and random(1, 100) <= chance then + if chance and chance > 0 and random(100) <= chance then -- default mob egg local new_stack = ItemStack(mobname) @@ -4481,8 +4484,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end -- limit name entered to 64 characters long - if string.len(fields.name) > 64 then - fields.name = string.sub(fields.name, 1, 64) + if fields.name:len() > 64 then + fields.name = fields.name:sub(1, 64) end -- update nametag @@ -4527,8 +4530,6 @@ function mobs:alias_mob(old_name, new_name) minetest.add_entity(self.object:get_pos(), new_name, staticdata) - else --- active_mobs = active_mobs - 1 end remove_mob(self) diff --git a/crafts.lua b/crafts.lua index 890fe21..73d14da 100644 --- a/crafts.lua +++ b/crafts.lua @@ -302,8 +302,6 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) and fields.name and fields.name ~= "" then - local name = player:get_player_name() - -- does mob still exist? if not tex_obj or not tex_obj:get_luaentity() then @@ -318,8 +316,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end -- limit name entered to 64 characters long - if string.len(fields.name) > 64 then - fields.name = string.sub(fields.name, 1, 64) + if fields.name:len() > 64 then + fields.name = fields.name:sub(1, 64) end -- update texture diff --git a/spawner.lua b/spawner.lua index dfe20f2..8940850 100644 --- a/spawner.lua +++ b/spawner.lua @@ -19,7 +19,8 @@ minetest.register_node("mobs:spawner", { -- text entry formspec meta:set_string("formspec", - "field[text;" .. S("Mob MinLight MaxLight Amount PlayerDist") .. ";${command}]") + "field[text;" .. S("Mob MinLight MaxLight Amount PlayerDist") + .. ";${command}]") meta:set_string("infotext", S("Spawner Not Active (enter settings)")) meta:set_string("command", spawner_default) end, @@ -115,10 +116,10 @@ minetest.register_abm({ -- check objects inside 9x9 area around spawner local objs = minetest.get_objects_inside_radius(pos, 9) local count = 0 - local ent = nil + local ent -- count mob objects of same type in area - for k, obj in ipairs(objs) do + for _, obj in ipairs(objs) do ent = obj:get_luaentity() @@ -136,9 +137,9 @@ minetest.register_abm({ if pla > 0 then local in_range = 0 - local objs = minetest.get_objects_inside_radius(pos, pla) + local objsp = minetest.get_objects_inside_radius(pos, pla) - for _,oir in pairs(objs) do + for _, oir in pairs(objsp) do if oir:is_player() then @@ -157,8 +158,7 @@ minetest.register_abm({ -- find air blocks within 5 nodes of spawner local air = minetest.find_nodes_in_area( {x = pos.x - 5, y = pos.y + yof, z = pos.z - 5}, - {x = pos.x + 5, y = pos.y + yof, z = pos.z + 5}, - {"air"}) + {x = pos.x + 5, y = pos.y + yof, z = pos.z + 5}, {"air"}) -- spawn in random air block if air and #air > 0 then