Module:BATTD costs: Difference between revisions

mNo edit summary
saving progress
Line 3: Line 3:
-- LOCAL FUNCTIONS
-- LOCAL FUNCTIONS


local function GetTotalCosts(baseCost, accessibleUpgrades, upgradesById, upgradeLocksById, upgradePrereqsOfById, Y, N)
local function GetTotalCosts(baseCost, initialPurchasedUpgrades, accessibleUpgrades, upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, Y, N)
local sFormat = string.format
local sFormat = string.format
local tConcat = table.concat
local tConcat = table.concat
Line 10: Line 10:
local headerRow = {"{|class=\"wikitable sortable\" style=\"text-align:center\"\n!Total cost!!Sell value"}
local headerRow = {"{|class=\"wikitable sortable\" style=\"text-align:center\"\n!Total cost!!Sell value"}
for i, upgrade in ipairs(accessibleUpgrades) do
mw.logObject(accessibleUpgrades)
headerRow[i+1] = sFormat("!![[%s|%s]]", upgradesById[upgrade]._pageName, upgradesById[upgrade].name)
for upgradeId, _ in ipairs(accessibleUpgrades) do
headerRow[#headerRow+1] = sFormat("!![[%s|%s]]", upgradesById[upgradeId]._pageName, upgradesById[upgradeId].name)
end
end
Line 27: Line 29:
     
     
    return formatted
    return formatted
end
local function GetPurchasedAndAvailableUpgrades(purchasedUpgrades, availableUpgradeId)
-- skip this upgrade if it is a leaf
if not upgradePrereqsOfById[availableUpgradeId] then return nil, nil end
-- skip this upgrade if we don't have all the prereqs
if upgradePrereqsById[availableUpgradeId] then
for _, prereqId in ipairs(upgradePrereqsById[availableUpgradeId]) do
if not purchasedUpgrades[prereqId] then
return nil, nil
end
end
end
-- shallow copy purchasedUpgrades and availableUpgrades
local purchasedUpgradesNext = {}
local availableUpgradesNext = {}
for purchasedUpgradeId, _ in pairs(purchasedUpgrades) do
if upgradeLocksById[purchasedUpgradeId] and upgradeLocksById[purchasedUpgradeId][availableUpgradeId] then
return nil, nil
end
purchasedUpgradesNext[purchasedUpgradeId] = true
for nextUpgradeId, _ in pairs(upgradePrereqsOfById[purchasedUpgradeId]) do
if not upgradeLocksById[purchasedUpgradeId] or not upgradeLocksById[purchasedUpgradeId][nextUpgradeId] then
availableUpgradesNext[nextUpgradeId] = true
end
end
end
purchasedUpgradesNext[availableUpgradeId] = true
return purchasedUpgradesNext, availableUpgradesNext
end
end
Line 32: Line 70:
row = {sFormat("\n|-\n|$%s||$%s", FormatNum(currentCost), FormatNum(currentCost*0.7))}
row = {sFormat("\n|-\n|$%s||$%s", FormatNum(currentCost), FormatNum(currentCost*0.7))}
mw.logObject(purchasedUpgrades)
--mw.logObject(purchasedUpgrades)
mw.logObject(availableUpgrades)
--mw.logObject(availableUpgrades)
for i, upgrade in ipairs(accessibleUpgrades) do
for upgradeId, _ in pairs(accessibleUpgrades) do
row[i+1] = sFormat("||%s", purchasedUpgrades[upgrade.id] and Y or N)
row[#row+1] = sFormat("||%s", purchasedUpgrades[upgradeId] and Y or N)
end
end
outputTable[#outputTable+1] = tConcat(row, "")
outputTable[#outputTable+1] = tConcat(row, "")
for _, availableUpgradeId in ipairs(availableUpgrades) do
for availableUpgradeId, _ in pairs(availableUpgrades) do
-- shallow copy purchasedUpgrades and availableUpgrades
local purchasedUpgradesNext, availableUpgradesNext = GetPurchasedAndAvailableUpgrades(purchasedUpgrades, availableUpgradeId)
local purchasedUpgradesNext = {}
local availableUpgradesNext = {}
local doNext = true
for _, purchasedUpgradeId in ipairs(purchasedUpgrades) do
if purchasedUpgradesNext and availableUpgradesNext then
if upgradeLocksById[purchasedUpgradeId] and upgradeLocksById[purchasedUpgradeId][availableUpgradeId] then
purchasedUpgradesNext[availableUpgradeId] = true
doNext = false
--RecursiveGetTotalCosts(currentCost + upgradesById[availableUpgradeId].cost, purchasedUpgradesNext, availableUpgradesNext)
break
end
purchasedUpgradesNext[#purchasedUpgradesNext+1] = upgradesById[purchasedUpgradeId]
for _, nextUpgradeId in ipairs(upgradePrereqsOfById[purchasedUpgradeId]) do
if not upgradeLocksById[purchasedUpgradeId] and not upgradeLocksById[purchasedUpgradeId][nextUpgradeId] then
availableUpgradesNext[#availableUpgradesNext+1] = nextUpgradeId
end
end
end
if doNext then
purchasedUpgradesNext[#purchasedUpgradesNext+1] = availableUpgradeId
RecursiveGetTotalCosts(currentCost + upgradesById[availableUpgradeId].cost, purchasedUpgradesNext, availableUpgradesNext)
RecursiveGetTotalCosts(currentCost + upgradesById[availableUpgradeId].cost, purchasedUpgradesNext, availableUpgradesNext)
end
end
end
end
end
end
RecursiveGetTotalCosts(baseCost, {}, upgradePrereqsOfById.root)
local rootUpgrades = {}
for _, upgradeId in ipairs(upgradePrereqsOfById.root) do
rootUpgrades[upgradeId] = true
end
RecursiveGetTotalCosts(baseCost, initialPurchasedUpgrades, rootUpgrades)
outputTable[#outputTable+1] = "\n|}"
outputTable[#outputTable+1] = "\n|}"
Line 74: Line 101:
return tConcat(outputTable, "")
return tConcat(outputTable, "")
end
end
local a = [[-- add this combination's row to outputTable
row = {sFormat("\n|-\n|$%s||$%s", lang:formatNum(currentCost), lang:formatNum(currentCost*0.7))}
for i, upgrade in ipairs(upgrades) do
if not alreadyPurchasedUpgrades[upgrade.id] and not upgradeLocksById[upgradeId][upgrade.id] then
row[#row+1] = sFormat("||%s", purchasedUpgrades[upgrade.id] and Y or N)
end
end
outputTable[#outputTable+1] = tConcat(row, "\n")
for thisUpgradeId, _ in pairs(purchasedUpgrades) do
-- skip if this upgrade is a leaf
if upgradePrereqsOfById[thisUpgradeId] then
for _, nextUpgradeId in pairs(upgradePrereqsOfById[thisUpgradeId]) do
if not purchasedUpgrades[nextUpgradeId] then
-- shallow copy purchasedUpgrades and check if nextUpgradeId is locked by another upgrade
local purchasedUpgradesNext = {}
local doNext = true
for purchasedUpgrade, _ in pairs(purchasedUpgrades) do
-- if nextUpgradeId is locked by a purchasedUpgrade then don't proceed
if upgradeLocksById[purchasedUpgrade] and upgradeLocksById[purchasedUpgrade][nextUpgradeId] then
doNext = false
break
end
purchasedUpgradesNext[purchasedUpgrade] = true
end
if doNext then
purchasedUpgradesNext[nextUpgradeId] = true
recursiveGetTotalCosts(currentCost + upgradesById[nextUpgradeId].cost, purchasedUpgradesNext)
-- lock this combination so it isn't repeated on the graph again
if not upgradeLocksById[thisUpgradeId] then upgradeLocksById[thisUpgradeId] = {} end
upgradeLocksById[thisUpgradeId][nextUpgradeId] = true
if not upgradeLocksById[nextUpgradeId] then upgradeLocksById[nextUpgradeId] = {} end
upgradeLocksById[nextUpgradeId][thisUpgradeId] = true
end
end
end
end
end]]


local function upgrade(towerName, upgradeName, Y, N)
local function Upgrade(towerName, upgradeName, Y, N)
local sFormat = string.format
local sFormat = string.format
local query = mw.ext.cargo.query
local query = mw.ext.cargo.query
Line 164: Line 149:
end
end
else
else
-- if this upgrade is a root upgrade and is not already purchased
-- if this upgrade is a root upgrade
upgradePrereqsOfById["root"][#upgradePrereqsOfById["root"]+1] = upgrade.thisId
upgradePrereqsOfById["root"][#upgradePrereqsOfById["root"]+1] = upgrade.thisId
upgradePrereqsById[upgrade.thisId] = {"root"}
end
end
Line 188: Line 174:
-- build alreadyPurchasedUpgrades
-- build alreadyPurchasedUpgrades
local function getAlreadyPurchasedUpgrades(myUpgradeId)
local function GetPurchasedUpgrades(myUpgradeId)
local cost = upgradesById[myUpgradeId].cost
local cost = upgradesById[myUpgradeId].cost
purchasedUpgrades[myUpgradeId] = true
purchasedUpgrades[myUpgradeId] = true
for i, id in ipairs(upgradePrereqsById[myUpgradeId]) do
for _, id in ipairs(upgradePrereqsById[myUpgradeId]) do
cost = cost + getAlreadyPurchasedUpgrades(id)
if not id == "root" then cost = cost + GetPurchasedUpgrades(id) end
end
end
return cost
return cost
Line 199: Line 185:
-- tower cost + total cost of this upgrade and all its prereqs
-- tower cost + total cost of this upgrade and all its prereqs
local baseCost = getAlreadyPurchasedUpgrades(upgradeId) + query("battd_characters", "cost", {
local baseCost = GetPurchasedUpgrades(upgradeId) + query("battd_characters", "cost", {
where=sFormat("name='%s'", towerName)
where=sFormat("name='%s'", towerName)
})[1].cost
})[1].cost
local function getAccessibleUpgrades(myUpgradeId)
local function GetAccessibleUpgrades(myUpgradeId)
for _, upgrade in ipairs(upgradePrereqsOfById[myUpgradeId]) do
for _, upgrade in ipairs(upgradePrereqsOfById[myUpgradeId]) do
Line 213: Line 199:
end
end
accessibleUpgrades[#accessibleUpgrades+1] = upgrade
-- upgrades with multiple prereqs are only accessible if we can reach all prereqs
for _, prereqId in ipairs(upgradePrereqsById[upgrade]) do
if not accessibleUpgrades[prereqId] then return end
end
accessibleUpgrades[myUpgradeId] = true
if upgradePrereqsOfById[upgrade] then
if upgradePrereqsOfById[upgrade] then
getAccessibleUpgrades(upgrade)
GetAccessibleUpgrades(upgrade)
end
end
end
end
end
end
end
end
getAccessibleUpgrades("root")
GetAccessibleUpgrades("root")
--return ""
--return ""
return GetTotalCosts(baseCost, accessibleUpgrades, upgradesById, upgradeLocksById, upgradePrereqsOfById, Y, N)
return GetTotalCosts(baseCost, purchasedUpgrades, accessibleUpgrades, upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, Y, N)
end
end


-- GLOBAL FUNCTIONS
-- GLOBAL FUNCTIONS


function p.tower(frame)
p["tower"] = function(frame)
return ""
return ""
end
end


function p.upgrade(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")
return Upgrade(frame.args[1], frame.args[2], "Y", "N")
end
end


return p
return p