diff --git a/data-canary/scripts/actions/other/spellbook.lua b/data-canary/scripts/actions/other/spellbook.lua deleted file mode 100644 index 600f60a02e9..00000000000 --- a/data-canary/scripts/actions/other/spellbook.lua +++ /dev/null @@ -1,38 +0,0 @@ -local spellbook = Action() - -function spellbook.onUse(player, item, fromPosition, target, toPosition, isHotkey) - local text = {} - local spells = {} - for _, spell in ipairs(player:getInstantSpells()) do - if spell.level ~= 0 then - if spell.manapercent > 0 then - spell.mana = spell.manapercent .. "%" - end - spells[#spells + 1] = spell - end - end - - table.sort(spells, function(a, b) - return a.level < b.level - end) - - local prevLevel = -1 - for i, spell in ipairs(spells) do - if prevLevel ~= spell.level then - if i == 1 then - text[#text == nil and 1 or #text + 1] = "Spells for Level " - else - text[#text + 1] = "\nSpells for Level " - end - text[#text + 1] = spell.level .. "\n" - prevLevel = spell.level - end - text[#text + 1] = spell.words .. " - " .. spell.name .. " : " .. spell.mana .. "\n" - end - - player:showTextDialog(item:getId(), table.concat(text)) - return true -end - -spellbook:id(3059, 6120, 8900, 8901, 8902, 8903, 8904, 8918, 14769, 16107, 20088, 20089, 20090, 21400) -spellbook:register() diff --git a/data-otservbr-global/scripts/actions/spellbook.lua b/data-otservbr-global/scripts/actions/spellbook.lua deleted file mode 100644 index 2e1328bcac4..00000000000 --- a/data-otservbr-global/scripts/actions/spellbook.lua +++ /dev/null @@ -1,58 +0,0 @@ -local spellbook = Action() - -function spellbook.onUse(player, item, fromPosition, target, toPosition, isHotkey) - local text = "" - local tlvl = {} - local tml = {} - - for _, spell in ipairs(player:getInstantSpells()) do - if spell.level ~= 0 or spell.mlevel ~= 0 then - if spell.manapercent > 0 then - spell.mana = spell.manapercent .. "%" - end - if spell.level > 0 then - tlvl[#tlvl + 1] = spell - elseif spell.mlevel > 0 then - tml[#tml + 1] = spell - end - end - end - - table.sort(tlvl, function(a, b) - return a.level < b.level - end) - local prevLevel = -1 - for i, spell in ipairs(tlvl) do - local line = "" - if prevLevel ~= spell.level then - if i ~= 1 then - line = "\n" - end - line = line .. "Spells for Level " .. spell.level .. "\n" - prevLevel = spell.level - end - text = text .. line .. " " .. spell.words .. " - " .. spell.name .. " : " .. spell.mana .. "\n" - end - text = text .. "\n" - table.sort(tml, function(a, b) - return a.mlevel < b.mlevel - end) - local prevmLevel = -1 - for i, spell in ipairs(tml) do - local line = "" - if prevLevel ~= spell.mlevel then - if i ~= 1 then - line = "\n" - end - line = line .. "Spells for Magic Level " .. spell.mlevel .. "\n" - prevmLevel = spell.mlevel - end - text = text .. line .. " " .. spell.words .. " - " .. spell.name .. " : " .. spell.mana .. "\n" - end - - player:showTextDialog(item:getId(), text) - 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() diff --git a/data/scripts/actions/items/spellbook.lua b/data/scripts/actions/items/spellbook.lua new file mode 100644 index 00000000000..ef777b68269 --- /dev/null +++ b/data/scripts/actions/items/spellbook.lua @@ -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()