Advertisement
mikeyy

AIGlobals.SaveForUpgrade v3

Jun 23rd, 2011
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.09 KB | None | 0 0
  1. --Main function for handling save logic; returns false, or upgrade name and upgrade cost
  2. function SaveForUpgrade(unit, brain)
  3.     LOG("SaveForUpgrade: "..repr(brain.Nickname).." / "..repr(unit:GetUnitId()).." @ "..repr(GetMinute()))
  4.     local goldRank = GetGoldRank(unit, brain)
  5.     local warRank = brain.Score.WarRank
  6.     local warScore = brain.Score.WarScore
  7.     local enemyRank = GetEnemyTeamBrain(unit).Score.WarRank
  8.     LOG("\tGold Rank: "..repr(goldRank)..", War Rank: "..repr(warRank)..", Enemy War Rank: "..repr(enemyRank))
  9.  
  10.     --Deferred init for the upgrade queue, to allow other mods to hook SaveForUpgrades
  11.     if upgradeQueue == false then
  12.         LOG("\tInitializing upgrade queue..")
  13.         --Assign costs on startup
  14.         for num, upgrade in SaveForUpgrades do
  15.             upgrade.Cost = UpgradeCost(upgrade.Name)
  16.         end
  17.         --Then copy the map table to the queue
  18.         upgradeQueue = table.deepcopy(SaveForUpgrades)
  19.     end
  20.  
  21.     --Prune purchased upgrades
  22.     while TeamHasUpgrade(unit, upgradeQueue[1].Name) do
  23.         LOG("\tTeam has upgrade "..repr(upgradeQueue[1].Name)..", removing from queue..")
  24.         table.remove(upgradeQueue, 1)
  25.     end
  26.  
  27.     --Get a list of upgrades that we should be saving for now
  28.     local currentUpgrades = {}
  29.     LOG("\tUpgrades to save for:")
  30.     for num, data in upgradeQueue do
  31.         if (data.Rank and warRank >= data.Rank) or (data.Score and warScore >= data.Score)
  32.         or (data.ERank and enemyRank >= data.ERank) or (data.EHas and EnemyHasUpgrade(unit, data.Name)) then
  33.             LOG("\t\t"..repr(data.Name).." - "..repr(data.Cost))
  34.             table.insert(currentUpgrades, table.copy(data))
  35.         end
  36.     end
  37.     if table.getn(currentUpgrades) < 1 then
  38.         LOG("\t\t(none)")
  39.     end
  40.  
  41.     --Return which one this AI should save for, if any
  42.     local saveFor = currentUpgrades[goldRank]
  43.     if saveFor then
  44.         LOG("\tSaving for upgrade "..repr(saveFor.Name)..", cost "..repr(saveFor.Cost))
  45.         return saveFor.Name, saveFor.Cost
  46.     else
  47.         LOG("\tNot saving.")
  48.         return false
  49.     end
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement