Module:BATTD costs: Difference between revisions
mNo edit summary |
attempting a rewrite |
||
| Line 2: | Line 2: | ||
-- LOCAL FUNCTIONS | -- LOCAL FUNCTIONS | ||
local function GetTotalCosts(baseCost, upgradesById, upgradeLocksById, upgradePrereqsOfById, Y, N) | |||
local sFormat = string.format | |||
local sGsub = string.gsub | |||
local headerRow = {"{|class=\"wikitable sortable\" style=\"text-align:center\"\n!Total cost!!Sell value"} | |||
for id, upgrade in ipairs(upgradesById) do | |||
headerRow[#headerRow+1] = sFormat("!![[%s|%s]]", upgrade._pageName, upgrade.name) | |||
end | |||
local outputTable = {tConcat(headerRow, "")} | |||
-- english-only alternative to FormatNum that takes up less time | |||
local function FormatNum(num) | |||
local formatted = tostring(num) | |||
-- skip if less than 1000 | |||
local k = (num > 999 and 1 or 0) | |||
while k ~= 0 do | |||
formatted, k = sGsub(formatted, "^(-?%d+)(%d%d%d)", "%1,%2") | |||
end | |||
return formatted | |||
end | |||
local function RecursiveGetTotalCosts(currentCost, purchasedUpgrades, availableUpgrades) | |||
row = {sFormat("\n|-\n|$%s||$%s", FormatNum(currentCost), FormatNum(currentCost*0.7))} | |||
for id, upgrade in pairs(upgradesById) do | |||
row[#row+1] = sFormat("||%s", purchasedUpgrades[id] and Y or N) | |||
end | |||
outputTable[#outputTable+1] = tConcat(row, "") | |||
for availableUpgradeId, availableUpgrade in pairs(availableUpgrades) do | |||
-- shallow copy purchasedUpgrades | |||
local purchasedUpgradesNext = { availableUpgrade } | |||
local availableUpgradesNext = {} | |||
local doNext = true | |||
for purchasedUpgradeId, purchasedUpgrade in pairs(purchasedUpgrades) do | |||
if upgradeLocksById[purchasedUpgradeId] and upgradeLocksById[purchasedUpgradeId][availableUpgradeId] then | |||
doNext = false | |||
break | |||
end | |||
purchasedUpgradesNext[i+2] = purchasedUpgrade | |||
for _, nextUpgrade in pairs(upgradePrereqsOfById[purchasedUpgrade.id]) do | |||
if not upgradeLocksById[purchasedUpgradeId] and upgradeLocksById[purchasedUpgradeId][availableUpgradeId] then | |||
availableUpgradesNext[#availableUpgradesNext+1] = nextUpgrade | |||
end | |||
end | |||
end | |||
if doNext then | |||
RecursiveGetTotalCosts(currentCost + availableUpgrade.cost, purchasedUpgradesNext, availableUpgradesNext) | |||
end | |||
end | |||
end | |||
outputTable[#outputTable+1] = "\n|}" | |||
return tConcat(outputTable, "") | |||
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 | ||
local lang = mw.language.new("en") | local lang = mw.language.new("en") | ||
| Line 15: | Line 122: | ||
local upgradeLocksById = {} -- key:values table - upgrades:upgrades that this upgrade locks | local upgradeLocksById = {} -- key:values table - upgrades:upgrades that this upgrade locks | ||
local upgradePrereqsById = {} -- key:values table - upgrade:prerequisites to purchase this upgrade | local upgradePrereqsById = {} -- key:values table - upgrade:prerequisites to purchase this upgrade | ||
local upgradePrereqsOfById = { -- key:values table - upgrades:upgrades this is a prerequisite of | local upgradePrereqsOfById = {} -- key:values table - upgrades:upgrades this is a prerequisite of | ||
local notPurchasedUpgrades = {} -- this upgrade and all upgrades the player has to have already purchased to get it | |||
-- cargo queries for data | -- cargo queries for data | ||
| Line 40: | Line 143: | ||
for i, upgrade in ipairs(upgrades) do | for i, upgrade in ipairs(upgrades) do | ||
upgradesById[upgrade.id] = upgrade | upgradesById[upgrade.id] = upgrade | ||
notPurchasedUpgrades[upgrade.id] = upgrade | |||
if upgrade.name == upgradeName then upgradeId = upgrade.id end | if upgrade.name == upgradeName then upgradeId = upgrade.id end | ||
end | end | ||
| Line 54: | Line 158: | ||
else | else | ||
-- if this upgrade is a root upgrade and is not already purchased | -- if this upgrade is a root upgrade and is not already purchased | ||
upgradePrereqsOfById["root"][#upgradePrereqsOfById["root"]+1] = upgrade.thisId | --upgradePrereqsOfById["root"][#upgradePrereqsOfById["root"]+1] = upgrade.thisId | ||
end | end | ||
| Line 68: | Line 172: | ||
local function getAlreadyPurchasedUpgrades(myUpgradeId) | local function getAlreadyPurchasedUpgrades(myUpgradeId) | ||
local cost = upgradesById[myUpgradeId].cost | local cost = upgradesById[myUpgradeId].cost | ||
notPurchasedUpgrades[myUpgradeId] = nil | |||
for i, id in ipairs(upgradePrereqsById[myUpgradeId]) do | for i, id in ipairs(upgradePrereqsById[myUpgradeId]) do | ||
| Line 75: | Line 179: | ||
return cost | return cost | ||
end | end | ||
-- build upgradeLocksById | -- build upgradeLocksById | ||
| Line 92: | Line 191: | ||
end | end | ||
-- tower cost + total cost of this upgrade and all its prereqs | |||
local | local baseCost = getAlreadyPurchasedUpgrades(upgradeId) + query("battd_characters", "cost", { | ||
where=sFormat("name='%s'", towerName) | |||
})[1].cost | |||
return GetTotalCosts(baseCost, notPurchasedUpgrades, upgradeLocksById, upgradePrereqsOfById, Y, N) | |||
end | end | ||