Skip to content

Commit

Permalink
Separate onGainExperience logic into its own file (#4505)
Browse files Browse the repository at this point in the history
* Separate onGainExperience logic into its own file

* applying the bonus correctly before the stamina changes

* check exp
  • Loading branch information
MillhioreBT committed Jul 19, 2023
1 parent 93d9c05 commit f288cf3
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 79 deletions.
79 changes: 0 additions & 79 deletions data/events/scripts/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -218,86 +218,7 @@ function Player:onPodiumEdit(item, outfit, direction, isVisible)
item:setOutfit(podiumOutfit)
end

local soulCondition = Condition(CONDITION_SOUL, CONDITIONID_DEFAULT)
soulCondition:setTicks(4 * 60 * 1000)
soulCondition:setParameter(CONDITION_PARAM_SOULGAIN, 1)

local function useStamina(player)
local staminaMinutes = player:getStamina()
if staminaMinutes == 0 then
return
end

local playerId = player:getId()
if not nextUseStaminaTime[playerId] then
nextUseStaminaTime[playerId] = 0
end

local currentTime = os.time()
local timePassed = currentTime - nextUseStaminaTime[playerId]
if timePassed <= 0 then
return
end

if timePassed > 60 then
if staminaMinutes > 2 then
staminaMinutes = staminaMinutes - 2
else
staminaMinutes = 0
end
nextUseStaminaTime[playerId] = currentTime + 120
else
staminaMinutes = staminaMinutes - 1
nextUseStaminaTime[playerId] = currentTime + 60
end
player:setStamina(staminaMinutes)
end

function Player:onGainExperience(source, exp, rawExp, sendText)
if not source or source:isPlayer() then
return exp
end

local level = self:getLevel()

-- Soul regeneration
local vocation = self:getVocation()
if self:getSoul() < vocation:getMaxSoul() and exp >= level then
soulCondition:setParameter(CONDITION_PARAM_SOULTICKS, vocation:getSoulGainTicks() * 1000)
self:addCondition(soulCondition)
end

-- Apply experience stage multiplier
exp = exp * Game.getExperienceStage(level)

-- Stamina modifier
if configManager.getBoolean(configKeys.STAMINA_SYSTEM) then
useStamina(self)

local staminaMinutes = self:getStamina()
if staminaMinutes > 2340 and self:isPremium() then
exp = exp * 1.5
elseif staminaMinutes <= 840 then
exp = exp * 0.5
end
end

-- Apply low level bonus
exp = exp * (1 + self:calculateLowLevelBonus(level) / 100)

if sendText then
local pos = self:getPosition()
local expString = exp .. (exp ~= 1 and " experience points." or " experience point.")
self:sendTextMessage(MESSAGE_EXPERIENCE, "You gained " .. expString, pos, exp, TEXTCOLOR_WHITE_EXP)

local spectators = Game.getSpectators(pos, false, true)
for _, spectator in ipairs(spectators) do
if spectator ~= self then
spectator:sendTextMessage(MESSAGE_EXPERIENCE_OTHERS, self:getName() .. " gained " .. expString)
end
end
end

local onGainExperience = EventCallback.onGainExperience
return onGainExperience and onGainExperience(self, source, exp, rawExp, sendText) or exp
end
Expand Down
95 changes: 95 additions & 0 deletions data/scripts/eventcallbacks/player/default_onGainExperience.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
local soulCondition = Condition(CONDITION_SOUL, CONDITIONID_DEFAULT)
soulCondition:setTicks(4 * 60 * 1000)
soulCondition:setParameter(CONDITION_PARAM_SOULGAIN, 1)

local function useStamina(player)
local staminaMinutes = player:getStamina()
if staminaMinutes == 0 then
return
end

local playerId = player:getId()
if not nextUseStaminaTime[playerId] then
nextUseStaminaTime[playerId] = 0
end

local currentTime = os.time()
local timePassed = currentTime - nextUseStaminaTime[playerId]
if timePassed <= 0 then
return
end

if timePassed > 60 then
if staminaMinutes > 2 then
staminaMinutes = staminaMinutes - 2
else
staminaMinutes = 0
end
nextUseStaminaTime[playerId] = currentTime + 120
else
staminaMinutes = staminaMinutes - 1
nextUseStaminaTime[playerId] = currentTime + 60
end
player:setStamina(staminaMinutes)
end

local default = EventCallback

function default.onGainExperience(player, source, exp, rawExp, sendText)
if not source or source:isPlayer() then
return exp
end

local level = player:getLevel()

-- Soul regeneration
local vocation = player:getVocation()
if player:getSoul() < vocation:getMaxSoul() and exp >= level then
soulCondition:setParameter(CONDITION_PARAM_SOULTICKS, vocation:getSoulGainTicks() * 1000)
player:addCondition(soulCondition)
end

-- Apply experience stage multiplier
exp = exp * Game.getExperienceStage(level)

-- Apply low level bonus
exp = exp * (1 + player:calculateLowLevelBonus(level) / 100)

-- Stamina modifier
if configManager.getBoolean(configKeys.STAMINA_SYSTEM) then
useStamina(player)

local staminaMinutes = player:getStamina()
if staminaMinutes > 2340 and player:isPremium() then
exp = exp * 1.5
elseif staminaMinutes <= 840 then
exp = exp * 0.5
end
end
return exp
end

default:register()

-- For this event, we use the trigger index math.huge so that this event is called last, thus ensuring that the
-- experience message is sent to the client with the correct value.

local message = EventCallback

function message.onGainExperience(player, source, exp, rawExp, sendText)
if sendText and exp ~= 0 then
local pos = player:getPosition()
local expString = exp .. (exp ~= 1 and " experience points." or " experience point.")
player:sendTextMessage(MESSAGE_EXPERIENCE, "You gained " .. expString, pos, exp, TEXTCOLOR_WHITE_EXP)

local spectators = Game.getSpectators(pos, false, true)
for _, spectator in ipairs(spectators) do
if spectator ~= player then
spectator:sendTextMessage(MESSAGE_EXPERIENCE_OTHERS, player:getName() .. " gained " .. expString)
end
end
end
return exp
end

message:register(math.huge)

0 comments on commit f288cf3

Please sign in to comment.