Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Main function for handling save logic; returns false, or upgrade name and upgrade cost
- function SaveForUpgrade(unit, brain)
- LOG("SaveForUpgrade: "..repr(brain.Nickname).." / "..repr(unit:GetUnitId()).." @ "..repr(GetMinute()))
- local goldRank = GetGoldRank(unit, brain)
- local warRank = brain.Score.WarRank
- local warScore = brain.Score.WarScore
- local enemyRank = GetEnemyTeamBrain(unit).Score.WarRank
- LOG("\tGold Rank: "..repr(goldRank)..", War Rank: "..repr(warRank)..", Enemy War Rank: "..repr(enemyRank))
- --Deferred init for the upgrade queue, to allow other mods to hook SaveForUpgrades
- if upgradeQueue == false then
- LOG("\tInitializing upgrade queue..")
- --Assign costs on startup
- for num, upgrade in SaveForUpgrades do
- upgrade.Cost = UpgradeCost(upgrade.Name)
- end
- --Then copy the map table to the queue
- upgradeQueue = table.deepcopy(SaveForUpgrades)
- end
- --Prune purchased upgrades
- while TeamHasUpgrade(unit, upgradeQueue[1].Name) do
- LOG("\tTeam has upgrade "..repr(upgradeQueue[1].Name)..", removing from queue..")
- table.remove(upgradeQueue, 1)
- end
- --Get a list of upgrades that we should be saving for now
- local currentUpgrades = {}
- LOG("\tUpgrades to save for:")
- for num, data in upgradeQueue do
- if (data.Rank and warRank >= data.Rank) or (data.Score and warScore >= data.Score)
- or (data.ERank and enemyRank >= data.ERank) or (data.EHas and EnemyHasUpgrade(unit, data.Name)) then
- LOG("\t\t"..repr(data.Name).." - "..repr(data.Cost))
- table.insert(currentUpgrades, table.copy(data))
- end
- end
- if table.getn(currentUpgrades) < 1 then
- LOG("\t\t(none)")
- end
- --Return which one this AI should save for, if any
- local saveFor = currentUpgrades[goldRank]
- if saveFor then
- LOG("\tSaving for upgrade "..repr(saveFor.Name)..", cost "..repr(saveFor.Cost))
- return saveFor.Name, saveFor.Cost
- else
- LOG("\tNot saving.")
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement