Module:BATTD costs: Difference between revisions

mNo edit summary
No edit summary
Line 62: Line 62:
-- lock this upgrade combination so we never retread this ground again
-- lock this upgrade combination so we never retread this ground again
if not initialPurchasedUpgrades[purchasedUpgradeId] then
if not initialPurchasedUpgrades[purchasedUpgradeId] then
upgradeLocksById[purchasedUpgradeId][availableUpgradeId] = true
upgradeLocksById[purchasedUpgradeId][availableUpgradeId] = true
upgradeLocksById[availableUpgradeId][purchasedUpgradeId] = true
upgradeLocksById[availableUpgradeId][purchasedUpgradeId] = true
end
end
end
end
Line 107: Line 107:
end
end


local function Upgrade(towerName, upgradeName, Y, N)
local function SetUpTables(towerName)
local sFormat = string.format
local sFormat = string.format
local query = mw.ext.cargo.query
local query = mw.ext.cargo.query
local outputTable = {}
local upgradeId = ""
local upgradesById = {} -- key:value table  - upgrade:upgrade data
local upgradesById = {} -- key:value table  - upgrade:upgrade data
Line 120: Line 117:
root = {}
root = {}
}
}
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 nextUpgrades = {} -- list of upgrades that are currently purchasable from this upgrade
-- cargo queries for data
-- cargo queries for data
Line 157: Line 151:
end
end
else
else
-- if this upgrade is a root upgrade
-- if this upgrade has no prereqs, give it a fake upgrade called "root" as a prereq
upgradePrereqsOfById["root"][#upgradePrereqsOfById["root"]+1] = upgrade.thisId
upgradePrereqsOfById["root"][#upgradePrereqsOfById["root"]+1] = upgrade.thisId
upgradePrereqsById[upgrade.thisId] = {"root"}
upgradePrereqsById[upgrade.thisId] = {"root"}
Line 176: Line 170:
end
end
end
end
-- 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)
return upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, baseCost
end
local function Upgrade(towerName, upgradeName, Y, N)
local upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, baseCost = SetUpTables(towerName)
local upgradeId = ""
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 nextUpgrades = {} -- list of upgrades that are currently purchasable from this upgrade
-- build purchasedUpgrades recursively
-- build purchasedUpgrades recursively
local function GetPurchasedUpgrades(myUpgradeId)
local function GetPurchasedUpgrades(myUpgradeId)
local cost = upgradesById[myUpgradeId].cost
baseCost = baseCost + upgradesById[myUpgradeId].cost
purchasedUpgrades[myUpgradeId] = true
purchasedUpgrades[myUpgradeId] = true
for _, prereqId in ipairs(upgradePrereqsById[myUpgradeId]) do
for _, prereqId in ipairs(upgradePrereqsById[myUpgradeId]) do
if prereqId ~= "root" then cost = cost + GetPurchasedUpgrades(prereqId) end
if prereqId ~= "root" then GetPurchasedUpgrades(myUpgradeId) end
end
end
return cost
end
end
-- tower cost + total cost of this upgrade and all its prereqs
local baseCost = GetPurchasedUpgrades(upgradeId) + query("battd_characters", "cost", {
where=sFormat("name='%s'", towerName)
})[1].cost
-- 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
Line 236: Line 241:


p["tower"] = function(frame)
p["tower"] = function(frame)
return ""
local upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, baseCost = SetUpTables(frame.args[1])
end
end