Skip to content

Commit

Permalink
Merge branch 'feat/soulpit' of https://github.com/opentibiabr/canary
Browse files Browse the repository at this point in the history
…into feat/soulpit
  • Loading branch information
phacUFPE committed Dec 6, 2024
2 parents b585691 + b1aa5b3 commit b284851
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions data/scripts/actions/items/exalted_core.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
local exaltedCore = Action()

local function getPreviousDifficultyLevel(currentLevel)
for level, value in pairs(SoulPit.SoulCoresConfiguration.monstersDifficulties) do
if value == currentLevel - 1 then
return level
end
end
return nil
end

local function getSoulCoreItemForMonster(monsterName)
local lowerMonsterName = monsterName:lower()
local soulCoreName = SoulPit.SoulCoresConfiguration.monsterVariationsSoulCore[monsterName]

if soulCoreName then
local newSoulCoreId = getItemIdByName(soulCoreName)
if newSoulCoreId then
return newSoulCoreId
end
else
local newMonsterSoulCore = string.format("%s soul core", monsterName)
local newSoulCoreId = getItemIdByName(newMonsterSoulCore)
if newSoulCoreId then
return newSoulCoreId
end
end

return false
end

function exaltedCore.onUse(player, item, fromPosition, target, toPosition, isHotkey)
local itemName = target:getName()
local monsterName = itemName:match("^(.-) soul core")

if not monsterName then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You can only use Exalted Core with a Soul Core.")
player:getPosition():sendMagicEffect(CONST_ME_POFF)
return false
end

local monsterType = MonsterType(monsterName)
if not monsterType then
player:sendTextMessage(MESSAGE_GAME_HIGHLIGHT, "Invalid monster type. Please contact an administrator.")
player:getPosition():sendMagicEffect(CONST_ME_POFF)
return false
end

local currentDifficulty = monsterType:getBestiaryStars()
local previousDifficultyLevel = getPreviousDifficultyLevel(currentDifficulty)
local previousDifficultyMonsters = nil

if previousDifficultyLevel then
previousDifficultyMonsters = monsterType:getMonstersByBestiaryStars(SoulPit.SoulCoresConfiguration.monstersDifficulties[previousDifficultyLevel])
else
previousDifficultyLevel = currentDifficulty
previousDifficultyMonsters = monsterType:getMonstersByBestiaryStars(SoulPit.SoulCoresConfiguration.monstersDifficulties[currentDifficulty])
end

if #previousDifficultyMonsters == 0 then
player:sendTextMessage(MESSAGE_GAME_HIGHLIGHT, "No monsters available for the previous difficulty level.")
player:getPosition():sendMagicEffect(CONST_ME_POFF)
return false
end

local newMonsterType = previousDifficultyMonsters[math.random(#previousDifficultyMonsters)]
local newSoulCoreItem = getSoulCoreItemForMonster(newMonsterType:getName())
if not newSoulCoreItem then -- Retry a second time.
newSoulCoreItem = getSoulCoreItemForMonster(newMonsterType:getName())
if not newSoulCoreItem then
player:sendTextMessage(MESSAGE_GAME_HIGHLIGHT, "Failed to generate a Soul Core.")
player:getPosition():sendMagicEffect(CONST_ME_POFF)
return false
end
end

if player:getFreeCapacity() < ItemType(newSoulCoreItem):getWeight() then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You do not have enough capacity.")
player:getPosition():sendMagicEffect(CONST_ME_POFF)
return false
end

player:addItem(newSoulCoreItem, 1)
player:removeItem(target:getId(), 1)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("You have received a %s soul core.", newMonsterType:getName()))
player:removeItem(item:getId(), 1)
player:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE)
return true
end

exaltedCore:id(37110)
exaltedCore:register()

0 comments on commit b284851

Please sign in to comment.