Module:BATTD costs: Difference between revisions

mNo edit summary
mNo edit summary
Line 1: Line 1:
local p = {}
local p = {}


-- LOCAL FUNCTIONS
-- helper functions
 
 


local function GetTotalCosts(baseCost, initialPurchasedUpgrades, accessibleUpgrades, nextUpgrades,
local function GetTotalCosts(baseCost, initialPurchasedUpgrades, accessibleUpgrades, nextUpgrades,
Line 29: Line 31:
     
     
    return formatted
    return formatted
end
local function IsUpgradeAvailable(myUpgradeId, purchasedUpgrades)
for _, prereqId in ipairs(upgradePrereqsById[myUpgradeId]) do
if not (prereqId == "root" or purchasedUpgrades[prereqId]) then return false end
end
for _, upgradeLock in ipairs(upgradeLocksById[myUpgradeId]) do
if purchasedUpgrades[upgradeLock] then return false end
end
return not purchasedUpgrades[myUpgradeId]
end
end
local function GetPurchasedAndAvailableUpgrades(purchasedUpgrades, availableUpgradeId)
local function GetPurchasedAndAvailableUpgrades(purchasedUpgrades, availableUpgradeId)
-- skip this upgrade if we don't have all the prereqs
-- skip this upgrade if we don't have all the prereqs
if prereqId ~= "root" then
for _, prereqId in ipairs(upgradePrereqsById[availableUpgradeId]) do
for _, prereqId in ipairs(upgradePrereqsById[availableUpgradeId]) do
if not (prereqId == "root" or purchasedUpgrades[prereqId]) then return nil, nil end
if not purchasedUpgrades[prereqId] then return nil, nil end
end
end
end
-- skip this upgrade if it is 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] and upgradeLocksById[purchasedUpgradeId][availableUpgradeId] then
if upgradeLocksById[purchasedUpgradeId][availableUpgradeId] then return nil, nil end
return nil, nil
end
end
end
-- shallow copy purchasedUpgrades and availableUpgrades
-- shallow copy purchasedUpgrades but with this upgrade included in it now
local purchasedUpgradesNext = {}
local purchasedUpgradesNext = { [availableUpgradeId] = true }
local availableUpgradesNext = {}
for purchasedUpgradeId, _ in pairs(purchasedUpgrades) do
for purchasedUpgradeId, _ in pairs(purchasedUpgrades) do
if upgradeLocksById[purchasedUpgradeId] then
upgradeLocksById[purchasedUpgradeId][availableUpgradeId] = true
else
upgradeLocksById[purchasedUpgradeId] = {[availableUpgradeId] = true}
end
-- add this upgrade to the list of purchased upgrades
purchasedUpgradesNext[purchasedUpgradeId] = true
purchasedUpgradesNext[purchasedUpgradeId] = true
if upgradePrereqsOfById[purchasedUpgradeId] then
-- lock this upgrade combination so we never retread this ground again
for _, nextUpgradeId in ipairs(upgradePrereqsOfById[purchasedUpgradeId]) do
--upgradeLocksById[purchasedUpgradeId][availableUpgradeId] = true
if accessibleUpgrades[nextUpgradeId] and not purchasedUpgrades[nextUpgradeId] then availableUpgradesNext[nextUpgradeId] = true end
--upgradeLocksById[availableUpgradeId][purchasedUpgradeId] = true
end
end
end
end
purchasedUpgradesNext[availableUpgradeId] = true
-- determine new availableUpgrades
local availableUpgradesNext = {}
for accessibleUpgradeId, _ in pairs(accessibleUpgrades) do
if IsUpgradeAvailable(accessibleUpgradeId, purchasedUpgradesNext) then availableUpgradesNext[accessibleUpgradeId] = true end
end
return purchasedUpgradesNext, availableUpgradesNext
return purchasedUpgradesNext, availableUpgradesNext
Line 84: Line 89:
outputTable[#outputTable+1] = tConcat(row, "")
outputTable[#outputTable+1] = tConcat(row, "")
if purchasedUpgrades and availableUpgrades then
for availableUpgradeId, _ in pairs(availableUpgrades) do
for availableUpgradeId, _ in pairs(availableUpgrades) do
local purchasedUpgradesNext, availableUpgradesNext = GetPurchasedAndAvailableUpgrades(purchasedUpgrades, availableUpgradeId)
local purchasedUpgradesNext, availableUpgradesNext = GetPurchasedAndAvailableUpgrades(purchasedUpgrades, availableUpgradeId)
if purchasedUpgradesNext and availableUpgradesNext then
if purchasedUpgradesNext and availableUpgradesNext then
purchasedUpgradesNext[availableUpgradeId] = true
--if #outputTable == 20 then return end
RecursiveGetTotalCosts(currentCost + upgradesById[availableUpgradeId].cost, purchasedUpgradesNext, availableUpgradesNext)
--if #outputTable == 20 then return end
RecursiveGetTotalCosts(currentCost + upgradesById[availableUpgradeId].cost, purchasedUpgradesNext, availableUpgradesNext)
end
end
end
end
end
Line 141: Line 143:
upgradesById[upgrade.id] = upgrade
upgradesById[upgrade.id] = upgrade
if upgrade.name == upgradeName then upgradeId = upgrade.id end
if upgrade.name == upgradeName then upgradeId = upgrade.id end
upgradeLocksById[upgrade.id] = {}
end
end


Line 169: Line 173:
for _, upgrade in ipairs(upgradeLocks) do
for _, upgrade in ipairs(upgradeLocks) do
if upgrade.lockId then
if upgrade.lockId then
if upgradeLocksById[upgrade.thisId] then
upgradeLocksById[upgrade.thisId][upgrade.lockId] = true
upgradeLocksById[upgrade.thisId][upgrade.lockId] = true
else
upgradeLocksById[upgrade.thisId] = {[upgrade.lockId] = true}
end
end
end
end
end
Line 199: Line 199:
-- if this upgrade is locked by an already purchased upgrade, then it is inaccessible
-- 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
for _, upgradeLock in ipairs(upgradeLocksById[myUpgradeId]) do
if purchasedUpgrades[upgradeLock] then return end
if purchasedUpgrades[upgradeLock] then return end
end
end
end