Skip to content

Commit

Permalink
Merge pull request #6858 from sruon/shami_multitrade
Browse files Browse the repository at this point in the history
[lua] Shami/Shemo accept different seals type in single trade
  • Loading branch information
zach2good authored Feb 6, 2025
2 parents 8ca645e + bef84e5 commit 4c5b8f8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 40 deletions.
59 changes: 59 additions & 0 deletions scripts/globals/seals.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
-----------------------------------
-- Seals and Crests trade (Shami/Shemo)
-----------------------------------
xi = xi or {}
xi.seals = xi.seals or {}

---@class sealItems
---@field [xi.item] { [integer]: integer, [integer]: integer }
xi.seals.sealItems =
{
-- Trade Item ID Seal ID, Retrieve Option
[xi.item.BEASTMENS_SEAL ] = { 0, 2 },
[xi.item.KINDREDS_SEAL ] = { 1, 1 },
[xi.item.KINDREDS_CREST ] = { 2, 3 },
[xi.item.HIGH_KINDREDS_CREST ] = { 3, 4 },
[xi.item.SACRED_KINDREDS_CREST] = { 4, 5 },
}

---@nodiscard
---@param trade CTradeContainer
---@return table
local function getSealTradeOption(trade)
local sealsInTrade = {}
for itemID, sealData in pairs(xi.seals.sealItems) do
if npcUtil.tradeHas(trade, itemID) then
table.insert(sealsInTrade, { sealData[1], trade:getItemQty(itemID) })
end
end

return sealsInTrade
end

-- Trading Seals/Crests
function xi.seals.onTrade(player, npc, trade, eventParams)
local sealOptions = getSealTradeOption(trade)

if next(sealOptions) then
local confirmedSeals = {}
for _, sealOption in ipairs(sealOptions) do
local storedSeals = player:getSeals(sealOption[1])
local itemCount = sealOption[2]
eventParams[sealOption[1] + 2] = bit.lshift(storedSeals + itemCount, 16)
table.insert(confirmedSeals, { sealOption[1], itemCount })
end

player:startEvent(unpack(eventParams))
for _, sealData in ipairs(confirmedSeals) do
local sealId = sealData[1]
local sealCount = sealData[2]
player:addSeals(sealCount, sealId)
end

player:confirmTrade()

return true
end

return false
end
45 changes: 6 additions & 39 deletions scripts/zones/Port_Jeuno/npcs/Shami.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@
---@type TNpcEntity
local entity = {}

---@class shamiSealItems
---@field [xi.item] { [integer]: integer, [integer]: integer }
local shamiSealItems =
{
-- Trade Item ID Seal ID, Retrieve Option
[xi.item.BEASTMENS_SEAL ] = { 0, 2 },
[xi.item.KINDREDS_SEAL ] = { 1, 1 },
[xi.item.KINDREDS_CREST ] = { 2, 3 },
[xi.item.HIGH_KINDREDS_CREST ] = { 3, 4 },
[xi.item.SACRED_KINDREDS_CREST] = { 4, 5 },
}

---@class shamiOrbItems
---@field [xi.item] { [integer]: integer, [integer]: integer, [integer]: integer, [integer]: integer } }
local shamiOrbItems =
Expand All @@ -41,24 +29,11 @@ local shamiOrbItems =
[xi.item.MACROCOSMIC_ORB] = { 11, 15, 4, 20 },
}

---@nodiscard
---@param trade CTradeContainer
---@return integer?
local function getSealTradeOption(trade)
for itemID, sealData in pairs(shamiSealItems) do
if npcUtil.tradeHasOnly(trade, itemID) then
return sealData[1]
end
end

return nil
end

---@nodiscard
---@param option integer
---@return xi.item?, integer?, integer?
local function convertSealRetrieveOption(option)
for itemID, sealData in pairs(shamiSealItems) do
for itemID, sealData in pairs(xi.seals.sealItems) do
if (option + sealData[2]) % 256 == 0 then
local sealCount = (option + sealData[2]) / 256 - 1

Expand Down Expand Up @@ -103,19 +78,9 @@ local function getOrbDataFromOption(option)
end

entity.onTrade = function(player, npc, trade)
-- Trading Seals/Crests
local sealOption = getSealTradeOption(trade)

if sealOption ~= nil then
local eventParams = { 321, 0, 0, 0, 0, 0 }
local storedSeals = player:getSeals(sealOption)
local itemCount = trade:getItemCount()

eventParams[sealOption + 2] = bit.lshift(storedSeals + itemCount, 16)
player:startEvent(unpack(eventParams))
player:addSeals(itemCount, sealOption)
player:confirmTrade()
local eventParams = { 321, 0, 0, 0, 0, 0 }

if xi.seals.onTrade(player, npc, trade, eventParams) then
return
end

Expand All @@ -137,7 +102,8 @@ entity.onTrigger = function(player, npc)
if beastmensSeal + kindredsSeal + kindredsCrest + highKindredsCrest + sacredKindredsCrest == 0 then
player:startEvent(23) -- Standard dialog ?
else
player:startEvent(322, (kindredsSeal * 65536) + beastmensSeal, (highKindredsCrest * 65536) + kindredsCrest, sacredKindredsCrest, 0, 1, 0, 0) -- Standard dialog with menu
local purchasedOrbs = player:getCharVar('ShamiPurchasedOrbs') -- Skips the first longer dialog if the player has already purchased orbs
player:startEvent(322, (kindredsSeal * 65536) + beastmensSeal, (highKindredsCrest * 65536) + kindredsCrest, sacredKindredsCrest, 0, 1, 0, 0, math.min(purchasedOrbs, 1)) -- Standard dialog with menu
end
end

Expand Down Expand Up @@ -184,6 +150,7 @@ entity.onEventFinish = function(player, csid, option, npc)
npcUtil.giveItem(player, itemID)
then
player:delSeals(sealCost, sealType)
player:incrementCharVar('ShamiPurchasedOrbs', 1)
end
end
end
Expand Down
10 changes: 9 additions & 1 deletion scripts/zones/Port_Jeuno/npcs/Shemo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ local sacredKindredCrest = 4
-- 1:Convert at 2: 1 (campaignAdditional Seal Battlefield Spoils Campaign)
local isTradeNum2 = 0

entity.onTrade = function(player, npc, trade)
local eventParams = { 353, 0, 0, 0, 0, 0 }

xi.seals.onTrade(player, npc, trade, eventParams)
end

entity.onTrigger = function(player, npc)
local csid = 352
local sealShift = {}
Expand All @@ -36,7 +42,8 @@ entity.onTrigger = function(player, npc)
end
end

player:startEvent(csid, sealBit1, sealBit2, sealBit3, isTradeNum2)
local shemoTradesCount = player:getCharVar('ShemoTrades') -- Skips first menu if player has traded before
player:startEvent(csid, sealBit1, sealBit2, sealBit3, isTradeNum2, 0, 0, 0, math.min(shemoTradesCount, 1))
end

entity.onEventFinish = function(player, csid, option, npc)
Expand All @@ -53,6 +60,7 @@ entity.onEventFinish = function(player, csid, option, npc)
if inSeal >= 0 and outSeal >= 0 then
player:delSeals(dellseals, outSeal)
player:addSeals(addnum, inSeal)
player:incrementCharVar('ShemoTrades', 1)
end
end
end
Expand Down

0 comments on commit 4c5b8f8

Please sign in to comment.