diff --git a/data/scripts/actions/items/exalted_core.lua b/data/scripts/actions/items/exalted_core.lua new file mode 100644 index 00000000000..a45633a3af7 --- /dev/null +++ b/data/scripts/actions/items/exalted_core.lua @@ -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()