Code cleanup

This commit is contained in:
teknomunk 2024-04-28 08:22:27 +00:00
parent 026ea5940c
commit 177e8f4b9d
1 changed files with 29 additions and 37 deletions

View File

@ -1,7 +1,11 @@
-- Constants
local size_min, size_max = 20, 59 local size_min, size_max = 20, 59
local delta_size = size_max - size_min local delta_size = size_max - size_min
local max_orb_age = 300 -- seconds
local gravity = vector.new(0, -((tonumber(minetest.settings:get("movement_gravity"))) or 9.81), 0)
local size_to_xp = { local size_to_xp = {
-- min and max XP amount for a given size
{-32768, 2}, -- 1 {-32768, 2}, -- 1
{ 3, 6}, -- 2 { 3, 6}, -- 2
{ 7, 16}, -- 3 { 7, 16}, -- 3
@ -25,15 +29,6 @@ local function xp_to_size(xp)
return ((i - 1) / (l - 1) * delta_size + size_min) / 100 return ((i - 1) / (l - 1) * delta_size + size_min) / 100
end end
local max_orb_age = 300 -- seconds
local gravity = vector.new(0, -((tonumber(minetest.settings:get("movement_gravity"))) or 9.81), 0)
local collector, pos, pos2
local direction, distance, player_velocity, goal
local currentvel, acceleration, multiplier, velocity
local node, vel, def
local is_moving, is_slippery, slippery, slip_factor
local size
local function xp_step(self, dtime) local function xp_step(self, dtime)
--if item set to be collected then only execute go to player --if item set to be collected then only execute go to player
if self.collected == true then if self.collected == true then
@ -41,32 +36,32 @@ local function xp_step(self, dtime)
self.collected = false self.collected = false
return return
end end
collector = minetest.get_player_by_name(self.collector) local collector = minetest.get_player_by_name(self.collector)
if collector and collector:get_hp() > 0 and vector.distance(self.object:get_pos(),collector:get_pos()) < 7.25 then if collector and collector:get_hp() > 0 and vector.distance(self.object:get_pos(),collector:get_pos()) < 7.25 then
self.object:set_acceleration(vector.new(0,0,0)) self.object:set_acceleration(vector.new(0,0,0))
self.disable_physics(self) self.disable_physics(self)
--get the variables --get the variables
pos = self.object:get_pos() local pos = self.object:get_pos()
pos2 = collector:get_pos() local pos2 = collector:get_pos()
player_velocity = collector:get_velocity() or collector:get_player_velocity() local player_velocity = collector:get_velocity() or collector:get_player_velocity()
pos2.y = pos2.y + 0.8 pos2.y = pos2.y + 0.8
direction = vector.direction(pos,pos2) local direction = vector.direction(pos,pos2)
distance = vector.distance(pos2,pos) local distance = vector.distance(pos2,pos)
multiplier = distance local multiplier = distance
if multiplier < 1 then if multiplier < 1 then
multiplier = 1 multiplier = 1
end end
goal = vector.multiply(direction,multiplier) local goal = vector.multiply(direction,multiplier)
currentvel = self.object:get_velocity() local currentvel = self.object:get_velocity()
if distance > 1 then if distance > 1 then
multiplier = 20 - distance multiplier = 20 - distance
velocity = vector.multiply(direction,multiplier) local velocity = vector.multiply(direction,multiplier)
goal = velocity goal = velocity
acceleration = vector.new(goal.x-currentvel.x,goal.y-currentvel.y,goal.z-currentvel.z) local acceleration = vector.new(goal.x-currentvel.x,goal.y-currentvel.y,goal.z-currentvel.z)
self.object:add_velocity(vector.add(acceleration,player_velocity)) self.object:add_velocity(vector.add(acceleration,player_velocity))
elseif distance < 0.8 then elseif distance < 0.8 then
mcl_experience.add_xp(collector, self._xp) mcl_experience.add_xp(collector, self._xp)
@ -86,17 +81,14 @@ local function xp_step(self, dtime)
return return
end end
pos = self.object:get_pos() local pos = self.object:get_pos()
if not pos then return end
if pos then local node = minetest.get_node_or_nil({
node = minetest.get_node_or_nil({ x = pos.x,
x = pos.x, y = pos.y -0.25,
y = pos.y -0.25, z = pos.z
z = pos.z })
})
else
return
end
-- Remove nodes in 'ignore' -- Remove nodes in 'ignore'
if node and node.name == "ignore" then if node and node.name == "ignore" then
@ -109,18 +101,18 @@ local function xp_step(self, dtime)
end end
-- Slide on slippery nodes -- Slide on slippery nodes
vel = self.object:get_velocity() local vel = self.object:get_velocity()
def = node and minetest.registered_nodes[node.name] local def = node and minetest.registered_nodes[node.name]
is_moving = (def and not def.walkable) or local is_moving = (def and not def.walkable) or
vel.x ~= 0 or vel.y ~= 0 or vel.z ~= 0 vel.x ~= 0 or vel.y ~= 0 or vel.z ~= 0
is_slippery = false local is_slippery = false
if def and def.walkable then if def and def.walkable then
slippery = minetest.get_item_group(node.name, "slippery") local slippery = minetest.get_item_group(node.name, "slippery")
is_slippery = slippery ~= 0 is_slippery = slippery ~= 0
if is_slippery and (math.abs(vel.x) > 0.2 or math.abs(vel.z) > 0.2) then if is_slippery and (math.abs(vel.x) > 0.2 or math.abs(vel.z) > 0.2) then
-- Horizontal deceleration -- Horizontal deceleration
slip_factor = 4.0 / (slippery + 4) local slip_factor = 4.0 / (slippery + 4)
self.object:set_acceleration({ self.object:set_acceleration({
x = -vel.x * slip_factor, x = -vel.x * slip_factor,
y = 0, y = 0,
@ -191,7 +183,7 @@ minetest.register_entity("mcl_experience:orb", {
-- This was a minetest bug for a while: https://github.com/minetest/minetest/issues/14420 -- This was a minetest bug for a while: https://github.com/minetest/minetest/issues/14420
local xp = tonumber(staticdata) or 0 local xp = tonumber(staticdata) or 0
self._xp = xp self._xp = xp
size = xp_to_size(xp) local size = xp_to_size(xp)
self.object:set_properties({ self.object:set_properties({
visual_size = {x = size, y = size}, visual_size = {x = size, y = size},