From 177e8f4b9ddf2f2be44221a0fb975a596682b5a7 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Sun, 28 Apr 2024 08:22:27 +0000 Subject: [PATCH 1/4] Code cleanup --- mods/HUD/mcl_experience/orb.lua | 66 +++++++++++++++------------------ 1 file changed, 29 insertions(+), 37 deletions(-) diff --git a/mods/HUD/mcl_experience/orb.lua b/mods/HUD/mcl_experience/orb.lua index 9d426a1d7..6e9b703b9 100644 --- a/mods/HUD/mcl_experience/orb.lua +++ b/mods/HUD/mcl_experience/orb.lua @@ -1,7 +1,11 @@ +-- Constants local size_min, size_max = 20, 59 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 = { + -- min and max XP amount for a given size {-32768, 2}, -- 1 { 3, 6}, -- 2 { 7, 16}, -- 3 @@ -25,15 +29,6 @@ local function xp_to_size(xp) return ((i - 1) / (l - 1) * delta_size + size_min) / 100 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) --if item set to be collected then only execute go to player if self.collected == true then @@ -41,32 +36,32 @@ local function xp_step(self, dtime) self.collected = false return 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 self.object:set_acceleration(vector.new(0,0,0)) self.disable_physics(self) --get the variables - pos = self.object:get_pos() - pos2 = collector:get_pos() + local pos = self.object: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 - direction = vector.direction(pos,pos2) - distance = vector.distance(pos2,pos) - multiplier = distance + local direction = vector.direction(pos,pos2) + local distance = vector.distance(pos2,pos) + local multiplier = distance if multiplier < 1 then multiplier = 1 end - goal = vector.multiply(direction,multiplier) - currentvel = self.object:get_velocity() + local goal = vector.multiply(direction,multiplier) + local currentvel = self.object:get_velocity() if distance > 1 then multiplier = 20 - distance - velocity = vector.multiply(direction,multiplier) + local velocity = vector.multiply(direction,multiplier) 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)) elseif distance < 0.8 then mcl_experience.add_xp(collector, self._xp) @@ -86,17 +81,14 @@ local function xp_step(self, dtime) return end - pos = self.object:get_pos() + local pos = self.object:get_pos() + if not pos then return end - if pos then - node = minetest.get_node_or_nil({ - x = pos.x, - y = pos.y -0.25, - z = pos.z - }) - else - return - end + local node = minetest.get_node_or_nil({ + x = pos.x, + y = pos.y -0.25, + z = pos.z + }) -- Remove nodes in 'ignore' if node and node.name == "ignore" then @@ -109,18 +101,18 @@ local function xp_step(self, dtime) end -- Slide on slippery nodes - vel = self.object:get_velocity() - def = node and minetest.registered_nodes[node.name] - is_moving = (def and not def.walkable) or + local vel = self.object:get_velocity() + local def = node and minetest.registered_nodes[node.name] + local is_moving = (def and not def.walkable) or vel.x ~= 0 or vel.y ~= 0 or vel.z ~= 0 - is_slippery = false + local is_slippery = false 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 if is_slippery and (math.abs(vel.x) > 0.2 or math.abs(vel.z) > 0.2) then -- Horizontal deceleration - slip_factor = 4.0 / (slippery + 4) + local slip_factor = 4.0 / (slippery + 4) self.object:set_acceleration({ x = -vel.x * slip_factor, 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 local xp = tonumber(staticdata) or 0 self._xp = xp - size = xp_to_size(xp) + local size = xp_to_size(xp) self.object:set_properties({ visual_size = {x = size, y = size}, From 7ae05d9c069abe45266de6ea4e9b24549117c0e1 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Sun, 28 Apr 2024 08:38:52 +0000 Subject: [PATCH 2/4] Add persistence to XP orbs, cleanup xp_to_size --- mods/HUD/mcl_experience/orb.lua | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/mods/HUD/mcl_experience/orb.lua b/mods/HUD/mcl_experience/orb.lua index 6e9b703b9..65ea226c4 100644 --- a/mods/HUD/mcl_experience/orb.lua +++ b/mods/HUD/mcl_experience/orb.lua @@ -1,6 +1,7 @@ -- Constants -local size_min, size_max = 20, 59 -local delta_size = size_max - size_min +local size_min = 20 / 100 -- minimum size, prescaled +local size_max = 59 / 100 -- maximum size, prescaled +local delta_size = (size_max - size_min) / 10 -- Size change for each XP size level local max_orb_age = 300 -- seconds local gravity = vector.new(0, -((tonumber(minetest.settings:get("movement_gravity"))) or 9.81), 0) @@ -20,13 +21,18 @@ local size_to_xp = { } local function xp_to_size(xp) - local i, l = 1, #size_to_xp + xp = xp or 0 - while xp > size_to_xp[i][1] and i < l do - i = i + 1 + -- Find the size for the xp amount + for i=1,11 do + local bucket = size_to_xp[i] + if xp >= bucket[1] and xp <= bucket[2] then + return (i - 1) * delta_size + size_min + end end - return ((i - 1) / (l - 1) * delta_size + size_min) / 100 + -- Fallback is the minimum size + return size_min end local function xp_step(self, dtime) @@ -36,6 +42,7 @@ local function xp_step(self, dtime) self.collected = false return end + 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 self.object:set_acceleration(vector.new(0,0,0)) @@ -70,11 +77,11 @@ local function xp_step(self, dtime) return else self.collector = nil - self.enable_physics(self) + self:enable_physics() end end - + -- Age orbs self.age = self.age + dtime if self.age > max_orb_age then self.object:remove() @@ -152,7 +159,6 @@ minetest.register_entity("mcl_experience:orb", { initial_sprite_basepos = {x = 0, y = 0}, is_visible = true, pointable = false, - static_save = false, }, moving_state = true, slippery_state = false, @@ -191,6 +197,9 @@ minetest.register_entity("mcl_experience:orb", { }) self.object:set_sprite({x=1,y=math.random(1,14)}, 14, 0.05, false) end, + get_staticdata = function(self) + tostring(self._xp or 0) + end, enable_physics = function(self) if not self.physical_state then From 94981d9c093b5684cb6c463bf3ebaa00a714efca Mon Sep 17 00:00:00 2001 From: teknomunk Date: Sun, 28 Apr 2024 08:46:13 +0000 Subject: [PATCH 3/4] Add return for get_staticdata --- mods/HUD/mcl_experience/orb.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mods/HUD/mcl_experience/orb.lua b/mods/HUD/mcl_experience/orb.lua index 65ea226c4..3b4c10b42 100644 --- a/mods/HUD/mcl_experience/orb.lua +++ b/mods/HUD/mcl_experience/orb.lua @@ -198,7 +198,7 @@ minetest.register_entity("mcl_experience:orb", { self.object:set_sprite({x=1,y=math.random(1,14)}, 14, 0.05, false) end, get_staticdata = function(self) - tostring(self._xp or 0) + return tostring(self._xp or 0) end, enable_physics = function(self) From bb9ed4498b87d4656b04bf5f36505604bbbd86ed Mon Sep 17 00:00:00 2001 From: teknomunk Date: Wed, 26 Jun 2024 06:52:10 -0500 Subject: [PATCH 4/4] Address review comments --- mods/HUD/mcl_experience/orb.lua | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/mods/HUD/mcl_experience/orb.lua b/mods/HUD/mcl_experience/orb.lua index 3b4c10b42..aa77f00a2 100644 --- a/mods/HUD/mcl_experience/orb.lua +++ b/mods/HUD/mcl_experience/orb.lua @@ -61,15 +61,13 @@ local function xp_step(self, dtime) if multiplier < 1 then multiplier = 1 end - local goal = vector.multiply(direction,multiplier) local currentvel = self.object:get_velocity() if distance > 1 then multiplier = 20 - distance - local velocity = vector.multiply(direction,multiplier) - goal = velocity - 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)) + local velocity = vector.multiply(direction, multiplier) + local acceleration = vector.new(velocity.x - currentvel.x, velocity.y - currentvel.y, velocity.z - currentvel.z) + self.object:add_velocity(vector.add(acceleration, player_velocity)) elseif distance < 0.8 then mcl_experience.add_xp(collector, self._xp) self.object:remove() @@ -91,9 +89,10 @@ local function xp_step(self, dtime) local pos = self.object:get_pos() if not pos then return end + -- Get the node directly below the XP orb local node = minetest.get_node_or_nil({ x = pos.x, - y = pos.y -0.25, + y = pos.y - 0.25, -- Orb collision box is +/-0.2, so go a bit below that z = pos.z })