Module:BATTD costs: Difference between revisions
mNo edit summary |
mNo edit summary |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 3: | Line 3: | ||
-- helper functions | -- helper functions | ||
local function GetTotalCosts(baseCost, initialPurchasedUpgrades, accessibleUpgrades, | local function GetTotalCosts(baseCost, initialPurchasedUpgrades, accessibleUpgrades, startingUpgrade, | ||
upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, Y, N) | upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, Y, N) | ||
local sFormat = string.format | local sFormat = string.format | ||
| Line 31: | Line 31: | ||
end | end | ||
local function | local function RecursiveGetTotalCosts(currentCost, purchasedUpgrades, myUpgradeId) | ||
-- skip this upgrade if we don't have all the prereqs | |||
for _, prereqId in ipairs(upgradePrereqsById[myUpgradeId]) do | for _, prereqId in ipairs(upgradePrereqsById[myUpgradeId]) do | ||
if not | if not purchasedUpgrades[prereqId] then return false end | ||
end | end | ||
-- skip this upgrade if it's locked by an already purchased upgrade | -- skip this upgrade if it's locked by an already purchased upgrade | ||
for purchasedUpgradeId, _ in pairs(purchasedUpgrades) do | for purchasedUpgradeId, _ in pairs(purchasedUpgrades) do | ||
if upgradeLocksById[purchasedUpgradeId][ | if upgradeLocksById[purchasedUpgradeId][myUpgradeId] then return false end | ||
end | end | ||
--upgradeLocksById[startingUpgrade][myUpgradeId] = true | |||
-- shallow copy purchasedUpgrades but with this upgrade included in it now | -- shallow copy purchasedUpgrades but with this upgrade included in it now | ||
local purchasedUpgradesNext = { [ | local purchasedUpgradesNext = { [myUpgradeId] = true } | ||
for purchasedUpgradeId, _ in pairs(purchasedUpgrades) do | for purchasedUpgradeId, _ in pairs(purchasedUpgrades) do | ||
purchasedUpgradesNext[purchasedUpgradeId] = true | purchasedUpgradesNext[purchasedUpgradeId] = true | ||
end | end | ||
-- | -- add row to output | ||
row = {sFormat("|-\n|$%s||$%s", FormatNum(currentCost), FormatNum(currentCost*0.7))} | |||
for accessibleUpgradeId, _ in pairs(accessibleUpgrades) do | for accessibleUpgradeId, _ in pairs(accessibleUpgrades) do | ||
row[#row+1] = sFormat("||%s", purchasedUpgradesNext[accessibleUpgradeId] and Y or N) | |||
end | end | ||
outputTable[#outputTable+1] = tConcat(row, "") | |||
for | -- depth first | ||
for _, prereqOfId in ipairs(upgradePrereqsOfById[myUpgradeId]) do | |||
RecursiveGetTotalCosts(currentCost + upgradesById[prereqOfId].cost, purchasedUpgradesNext, prereqOfId) | |||
end | end | ||
-- breadth next | |||
for purchasedUpgradeId, _ in pairs(purchasedUpgrades) do | |||
for | for _, prereqOfId in ipairs(upgradePrereqsOfById[purchasedUpgradeId]) do | ||
--RecursiveGetTotalCosts(currentCost + upgradesById[prereqOfId].cost, purchasedUpgradesNext, prereqOfId) | |||
-- | |||
end | end | ||
end | end | ||
return true | |||
end | end | ||
RecursiveGetTotalCosts(baseCost, initialPurchasedUpgrades, | RecursiveGetTotalCosts(baseCost, initialPurchasedUpgrades, startingUpgrade) | ||
outputTable[#outputTable+1] = "|}" | outputTable[#outputTable+1] = "|}" | ||
| Line 111: | Line 86: | ||
local query = mw.ext.cargo.query | local query = mw.ext.cargo.query | ||
local upgradesById = {} | local upgradesById = { root = {} } -- key:value table - upgrade:upgrade data | ||
local upgradeLocksById = {} | local upgradeLocksById = { root = {} } -- key:values table - upgrades:upgrades that this upgrade locks | ||
local upgradePrereqsById = {} -- key:values table - upgrade:prerequisites to purchase this upgrade | local upgradePrereqsById = { root = {} } -- key:values table - upgrade:prerequisites to purchase this upgrade | ||
local upgradePrereqsOfById = { -- key:values table - upgrades:upgrades this is a prerequisite of | local upgradePrereqsOfById = { root = {} } -- key:values table - upgrades:upgrades this is a prerequisite of | ||
} | -- tower cost + total cost of this upgrade and all its prereqs | ||
local baseCost = tonumber(query("battd_characters", "cost", { | |||
where=sFormat("name='%s'", towerName) | |||
})[1].cost) | |||
-- cargo queries for data | -- cargo queries for data | ||
| Line 168: | Line 146: | ||
end | end | ||
end | end | ||
return upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, baseCost | return upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, baseCost | ||
| Line 181: | Line 154: | ||
local purchasedUpgrades = {} -- this upgrade and any other upgrades that had to already be purchased to get it | local purchasedUpgrades = {} -- this upgrade and any other upgrades that had to already be purchased to get it | ||
local accessibleUpgrades = {} -- list of upgrades that are possible to reach from this upgrade | local accessibleUpgrades = {} -- list of upgrades that are possible to reach from this upgrade by some route | ||
-- build purchasedUpgrades recursively | -- build purchasedUpgrades recursively | ||
local function GetPurchasedUpgrades(myUpgradeId) | local function GetPurchasedUpgrades(myUpgradeId) | ||
baseCost = baseCost + upgradesById[myUpgradeId].cost | if not purchasedUpgrades[myUpgradeId] then baseCost = baseCost + upgradesById[myUpgradeId].cost end | ||
for _, prereqId in ipairs(upgradePrereqsById[myUpgradeId]) do | for _, prereqId in ipairs(upgradePrereqsById[myUpgradeId]) do | ||
if prereqId ~= "root" then GetPurchasedUpgrades( | if prereqId ~= "root" then GetPurchasedUpgrades(prereqId) end | ||
purchasedUpgrades[prereqId] = true | |||
end | end | ||
end | end | ||
| Line 207: | Line 179: | ||
-- find upgrades that are reachable from the current upgrades owned | -- find upgrades that are reachable from the current upgrades owned | ||
for _, prereqId in ipairs(upgradePrereqsById[myUpgradeId]) do | for _, prereqId in ipairs(upgradePrereqsById[myUpgradeId]) do | ||
-- upgrades with multiple prereqs are only accessible if we can reach all its prereqs | -- upgrades with multiple prereqs are only accessible if we can reach all its prereqs | ||
-- if it is accessible, it'll be reached again on another recursion | -- if it is accessible, it'll be reached again on another recursion | ||
if not (accessibleUpgrades[prereqId] or purchasedUpgrades[prereqId] or prereqId == "root") or prereqId == upgradeId then return end | if not (accessibleUpgrades[prereqId] or purchasedUpgrades[prereqId] or prereqId == "root") or prereqId == upgradeId then return end | ||
end | end | ||
if not purchasedUpgrades[myUpgradeId] then accessibleUpgrades[myUpgradeId] = true end | if not purchasedUpgrades[myUpgradeId] then accessibleUpgrades[myUpgradeId] = true end | ||
| Line 232: | Line 200: | ||
end | end | ||
return GetTotalCosts(baseCost, purchasedUpgrades, accessibleUpgrades, | return GetTotalCosts(baseCost, purchasedUpgrades, accessibleUpgrades, upgradeId, | ||
upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, Y, N) | upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, Y, N) | ||
end | end | ||