Skip to content

Commit

Permalink
chore: moving spellbook into place (#2214)
Browse files Browse the repository at this point in the history
Added two new local functions (sortSpellsByLevel and appendSpellsInfo).
The main logic of the onUse function has been retained, but now uses
these auxiliary functions to order and generate the text of spells.w
  • Loading branch information
omarcopires authored Feb 15, 2024
1 parent e1ac3a3 commit 3b07860
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 96 deletions.
38 changes: 0 additions & 38 deletions data-canary/scripts/actions/other/spellbook.lua

This file was deleted.

58 changes: 0 additions & 58 deletions data-otservbr-global/scripts/actions/spellbook.lua

This file was deleted.

55 changes: 55 additions & 0 deletions data/scripts/actions/items/spellbook.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
local function sortSpellsByLevel(spellList, levelKey)
table.sort(spellList, function(a, b)
return a[levelKey] < b[levelKey]
end)
end

local function appendSpellsInfo(spellList, header, levelKey, manaKey)
local text = ""
local prevLevel = -1

for i, spell in ipairs(spellList) do
local line = ""
if prevLevel ~= spell[levelKey] then
line = (i == 1 and "" or "\n") .. header .. spell[levelKey] .. "\n"
prevLevel = spell[levelKey]
end

text = text .. line .. " " .. spell.words .. " - " .. spell.name .. " : " .. spell[manaKey] .. "\n"
end
return text
end

local spellbook = Action()

function spellbook.onUse(player, item, fromPosition, target, toPosition, isHotkey)
local spellsForLevel = {}
local spellsForMagicLevel = {}

for _, spell in ipairs(player:getInstantSpells()) do
if (spell.level > 0 or spell.mlevel > 0) and spell.level + spell.mlevel > 0 then
if spell.manapercent > 0 then
spell.mana = spell.manapercent .. "%"
end

if spell.level > 0 then
spellsForLevel[#spellsForLevel + 1] = spell
else
spellsForMagicLevel[#spellsForMagicLevel + 1] = spell
end
end
end

sortSpellsByLevel(spellsForLevel, "level")
sortSpellsByLevel(spellsForMagicLevel, "mlevel")

local spellsText = appendSpellsInfo(spellsForLevel, "Spells for Level ", "level", "mana")
spellsText = spellsText .. "\n"
spellsText = spellsText .. appendSpellsInfo(spellsForMagicLevel, "Spells for Magic Level ", "mlevel", "mana")

player:showTextDialog(item:getId(), spellsText)
return true
end

spellbook:id(3059, 6120, 8072, 8073, 8074, 8075, 8076, 8090, 11691, 14769, 16107, 20088, 21400, 22755, 25699, 29431, 20089, 20090, 34153)
spellbook:register()

0 comments on commit 3b07860

Please sign in to comment.