Skip to content

Commit

Permalink
improve: set exercise function and table to local (opentibiabr#2318)
Browse files Browse the repository at this point in the history
  • Loading branch information
luanluciano93 authored Feb 26, 2024
1 parent ed2421b commit ffd1ea0
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 130 deletions.
125 changes: 0 additions & 125 deletions data/libs/systems/exercise_training.lua

This file was deleted.

1 change: 0 additions & 1 deletion data/libs/systems/load.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ dofile(CORE_DIRECTORY .. "/libs/systems/concoctions.lua")
dofile(CORE_DIRECTORY .. "/libs/systems/daily_reward.lua")
dofile(CORE_DIRECTORY .. "/libs/systems/encounters.lua")
dofile(CORE_DIRECTORY .. "/libs/systems/exaltation_forge.lua")
dofile(CORE_DIRECTORY .. "/libs/systems/exercise_training.lua")
dofile(CORE_DIRECTORY .. "/libs/systems/familiar.lua")
dofile(CORE_DIRECTORY .. "/libs/systems/features.lua")
dofile(CORE_DIRECTORY .. "/libs/systems/hazard.lua")
Expand Down
133 changes: 129 additions & 4 deletions data/scripts/actions/items/exercise_training_weapons.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,132 @@
local exhaustionTime = 10

local exerciseWeaponsTable = {
-- MELE
[28540] = { skill = SKILL_SWORD },
[28552] = { skill = SKILL_SWORD },
[35279] = { skill = SKILL_SWORD },
[35285] = { skill = SKILL_SWORD },
[28553] = { skill = SKILL_AXE },
[28541] = { skill = SKILL_AXE },
[35280] = { skill = SKILL_AXE },
[35286] = { skill = SKILL_AXE },
[28554] = { skill = SKILL_CLUB },
[28542] = { skill = SKILL_CLUB },
[35281] = { skill = SKILL_CLUB },
[35287] = { skill = SKILL_CLUB },
[44064] = { skill = SKILL_SHIELD },
[44065] = { skill = SKILL_SHIELD },
[44066] = { skill = SKILL_SHIELD },
[44067] = { skill = SKILL_SHIELD },
-- ROD
[28544] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_SMALLICE, allowFarUse = true },
[28556] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_SMALLICE, allowFarUse = true },
[35283] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_SMALLICE, allowFarUse = true },
[35289] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_SMALLICE, allowFarUse = true },
-- RANGE
[28543] = { skill = SKILL_DISTANCE, effect = CONST_ANI_SIMPLEARROW, allowFarUse = true },
[28555] = { skill = SKILL_DISTANCE, effect = CONST_ANI_SIMPLEARROW, allowFarUse = true },
[35282] = { skill = SKILL_DISTANCE, effect = CONST_ANI_SIMPLEARROW, allowFarUse = true },
[35288] = { skill = SKILL_DISTANCE, effect = CONST_ANI_SIMPLEARROW, allowFarUse = true },
-- WAND
[28545] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_FIRE, allowFarUse = true },
[28557] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_FIRE, allowFarUse = true },
[35284] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_FIRE, allowFarUse = true },
[35290] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_FIRE, allowFarUse = true },
}

local dummies = Game.getDummies()

local function leaveExerciseTraining(playerId)
if _G.OnExerciseTraining[playerId] then
stopEvent(_G.OnExerciseTraining[playerId].event)
_G.OnExerciseTraining[playerId] = nil
end

local player = Player(playerId)
if player then
player:setTraining(false)
end
return
end

local function exerciseTrainingEvent(playerId, tilePosition, weaponId, dummyId)
local player = Player(playerId)
if not player then
return leaveExerciseTraining(playerId)
end

if player:isTraining() == 0 then
player:sendTextMessage(MESSAGE_FAILURE, "You have stopped training.")
return leaveExerciseTraining(playerId)
end

if not Tile(tilePosition):getItemById(dummyId) then
player:sendTextMessage(MESSAGE_FAILURE, "Someone has moved the dummy, the training has stopped.")
leaveExerciseTraining(playerId)
return false
end

local playerPosition = player:getPosition()
if not playerPosition:isProtectionZoneTile() then
player:sendTextMessage(MESSAGE_FAILURE, "You are no longer in a protection zone, the training has stopped.")
leaveExerciseTraining(playerId)
return false
end

if player:getItemCount(weaponId) <= 0 then
player:sendTextMessage(MESSAGE_FAILURE, "You need the training weapon in the backpack, the training has stopped.")
leaveExerciseTraining(playerId)
return false
end

local weapon = player:getItemById(weaponId, true)
if not weapon:isItem() or not weapon:hasAttribute(ITEM_ATTRIBUTE_CHARGES) then
player:sendTextMessage(MESSAGE_FAILURE, "The selected item is not a training weapon, the training has stopped.")
leaveExerciseTraining(playerId)
return false
end

local weaponCharges = weapon:getAttribute(ITEM_ATTRIBUTE_CHARGES)
if not weaponCharges or weaponCharges <= 0 then
weapon:remove(1) -- ??
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your training weapon has disappeared.")
leaveExerciseTraining(playerId)
return false
end

if not dummies[dummyId] then
return false
end

local rate = dummies[dummyId] / 100
local isMagic = exerciseWeaponsTable[weaponId].skill == SKILL_MAGLEVEL
if isMagic then
player:addManaSpent(500 * rate)
else
player:addSkillTries(exerciseWeaponsTable[weaponId].skill, 7 * rate)
end

weapon:setAttribute(ITEM_ATTRIBUTE_CHARGES, (weaponCharges - 1))
tilePosition:sendMagicEffect(CONST_ME_HITAREA)

if exerciseWeaponsTable[weaponId].effect then
playerPosition:sendDistanceEffect(tilePosition, exerciseWeaponsTable[weaponId].effect)
end

if weapon:getAttribute(ITEM_ATTRIBUTE_CHARGES) <= 0 then
weapon:remove(1)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your training weapon has disappeared.")
leaveExerciseTraining(playerId)
return false
end

local vocation = player:getVocation()
_G.OnExerciseTraining[playerId].event = addEvent(exerciseTrainingEvent, vocation:getBaseAttackSpeed() / configManager.getFloat(configKeys.RATE_EXERCISE_TRAINING_SPEED), playerId, tilePosition, weaponId, dummyId)
return true
end

local function isDummy(id)
local dummies = Game.getDummies()
return dummies[id] and dummies[id] > 0
end

Expand All @@ -22,7 +147,7 @@ function exerciseTraining.onUse(player, item, fromPosition, target, toPosition,
end

local playerPos = player:getPosition()
if not ExerciseWeaponsTable[item.itemid].allowFarUse and (playerPos:getDistance(target:getPosition()) > 1) then
if not exerciseWeaponsTable[item.itemid].allowFarUse and (playerPos:getDistance(target:getPosition()) > 1) then
player:sendTextMessage(MESSAGE_FAILURE, "Get closer to the dummy.")
return true
end
Expand Down Expand Up @@ -62,7 +187,7 @@ function exerciseTraining.onUse(player, item, fromPosition, target, toPosition,

_G.OnExerciseTraining[playerId] = {}
if not _G.OnExerciseTraining[playerId].event then
_G.OnExerciseTraining[playerId].event = addEvent(ExerciseEvent, 0, playerId, targetPos, item.itemid, targetId)
_G.OnExerciseTraining[playerId].event = addEvent(exerciseTrainingEvent, 0, playerId, targetPos, item.itemid, targetId)
_G.OnExerciseTraining[playerId].dummyPos = targetPos
player:setTraining(true)
player:setExhaustion("training-exhaustion", exhaustionTime)
Expand All @@ -73,7 +198,7 @@ function exerciseTraining.onUse(player, item, fromPosition, target, toPosition,
return false
end

for weaponId, weapon in pairs(ExerciseWeaponsTable) do
for weaponId, weapon in pairs(exerciseWeaponsTable) do
exerciseTraining:id(weaponId)
if weapon.allowFarUse then
exerciseTraining:allowFarUse(true)
Expand Down

0 comments on commit ffd1ea0

Please sign in to comment.