Module:BATTD costs: Difference between revisions
mNo edit summary |
mNo edit summary |
||
| Line 114: | Line 114: | ||
root = {} | root = {} | ||
} | } | ||
local purchasedUpgrades = {} | local purchasedUpgrades = {} -- this upgrade and any other upgrades that had to already be purchased to get it | ||
local accessibleUpgrades = {} -- list of upgrades that | local accessibleUpgrades = {} -- list of upgrades that are possible to reach from this upgrade | ||
local nextUpgrades = {} | local nextUpgrades = {} -- list of upgrades that are currently purchasable from this upgrade | ||
-- cargo queries for data | -- cargo queries for data | ||
| Line 163: | Line 163: | ||
-- build upgradeLocksById | -- build upgradeLocksById | ||
for | for _, upgrade in ipairs(upgradeLocks) do | ||
if upgrade.lockId then | if upgrade.lockId then | ||
if upgradeLocksById[upgrade.thisId] then | if upgradeLocksById[upgrade.thisId] then | ||
| Line 173: | Line 173: | ||
end | end | ||
-- build purchasedUpgrades | -- build purchasedUpgrades recursively | ||
local function GetPurchasedUpgrades(myUpgradeId) | local function GetPurchasedUpgrades(myUpgradeId) | ||
local cost = upgradesById[myUpgradeId].cost | local cost = upgradesById[myUpgradeId].cost | ||
| Line 191: | Line 191: | ||
-- generate a list of upgrades that are reachable from this upgrade, but do not have this upgrade as a prerequisite | -- generate a list of upgrades that are reachable from this upgrade, but do not have this upgrade as a prerequisite | ||
-- also generate a list of upgrades that are immediately reachable from this upgrade | -- also generate a list of upgrades that are immediately reachable from this upgrade | ||
local function | local function DetermineAccessibility(myUpgradeId) | ||
if myUpgradeId == upgradeId then return end | |||
-- if this upgrade is locked by an already purchased upgrade, then it is inaccessible | |||
if upgradeLocksById[myUpgradeId] then | |||
for _, upgradeLock in ipairs(upgradeLocksById[myUpgradeId]) do | |||
if purchasedUpgrades[upgradeLock] then return end | |||
end | |||
end | |||
-- find upgrades that are reachable from the current upgrades owned | |||
local isNextUpgrade = true | |||
for _, prereqId in ipairs(upgradePrereqsById[myUpgradeId]) do | |||
-- 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 not (accessibleUpgrades[prereqId] or purchasedUpgrades[prereqId] or prereqId == "root") or prereqId == upgradeId then return end | |||
if not (purchasedUpgrades[prereqId] or prereqId == "root") or purchasedUpgrades[myUpgradeId] then isNextUpgrade = nil end | |||
end | |||
nextUpgrades[myUpgradeId] = isNextUpgrade | |||
if not purchasedUpgrades[myUpgradeId] then accessibleUpgrades[myUpgradeId] = true end | |||
-- determine accessibility of next upgrades | |||
if upgradePrereqsOfById[myUpgradeId] then | |||
for _, prereqOfId in ipairs(upgradePrereqsOfById[myUpgradeId]) do | |||
DetermineAccessibility(prereqOfId) | |||
end | end | ||
end | end | ||
end | |||
-- start with the root | |||
for _, prereqOfId in ipairs(upgradePrereqsOfById.root) do | |||
DetermineAccessibility(prereqOfId) | |||
end | end | ||
return GetTotalCosts(baseCost, purchasedUpgrades, accessibleUpgrades, nextUpgrades, | return GetTotalCosts(baseCost, purchasedUpgrades, accessibleUpgrades, nextUpgrades, | ||
upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, Y, N) | upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, Y, N) | ||
| Line 238: | Line 238: | ||
p["upgrade"] = function(frame) | p["upgrade"] = function(frame) | ||
return Upgrade(frame.args[1], frame.args[2], frame:expandTemplate{title = "Y", args = {}}, frame:expandTemplate{title = "N", args = {}}) | --return Upgrade(frame.args[1], frame.args[2], frame:expandTemplate{title = "Y", args = {}}, frame:expandTemplate{title = "N", args = {}}) | ||
return Upgrade(frame.args[1], frame.args[2], "Y", "N") | |||
end | end | ||
return p | return p | ||