Advertisement
mikeyy

AIGlobals.SaveForUpgrade v2

Jun 18th, 2011
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 KB | None | 0 0
  1. --Mithy: New system for determining which upgrade an AI should be saving for, if any
  2. --This is only run once per item or upgrade shopping check
  3.  
  4. --Map table for upgrades to save for, and from what rank / enemy rank
  5. --Rank is the own War Rank to begin saving for this upgrade
  6. --ERank an optional enemy War Rank at which to begin saving
  7. --EHas is an optional bool that tells the AI to save if the enemy has this upgrade
  8. -- all of these conditions are OR; any of them will trigger saving
  9. -- item cost mod (e.g. coin purse) is taken into account when determining how much to save
  10. SaveForUpgrades = {
  11.     [1] = { Name = 'CGoldIncome01', Rank = 2 },
  12.     [2] = { Name = 'CTroopNumber03', Rank = 6, ERank = 7, EHas = true },
  13.     [3] = { Name = 'CTroopNumber05', Rank = 6, ERank = 7, EHas = true },
  14.     [4] = { Name = 'CTroopNumber04', Rank = 7 },
  15.     [5] = { Name = 'CTroopNumber06', Rank = 9 },
  16. }
  17. --Table for holding upcoming upgrades
  18. local upgradeQueue = false
  19.  
  20. --Returns a number corresponding to this AI's current gold ranking among team AIs
  21. function GetGoldRank(unit, brain)
  22.     local goldTable = {}
  23.     for k, abrain in ArmyBrains do
  24.         if not abrain.TeamBrain and abrain.BrainController == 'AI' and IsAlly(brain:GetArmyIndex(), abrain:GetArmyIndex()) then
  25.             table.insert(goldTable, {Army = abrain:GetArmyIndex(), Gold = abrain.mGold})
  26.         end
  27.     end
  28.     if table.getn(goldTable) < 2 then
  29.         return 1
  30.     else
  31.         table.sort(goldTable, sort_down_by 'Gold')
  32.         return table.find(goldTable, brain:GetArmyIndex(), function(a, b) return a.Army == b end)
  33.     end
  34.     return false
  35. end
  36.  
  37. --Main function for handling save logic; returns false, or upgrade name and upgrade cost
  38. function SaveForUpgrade(unit, brain)
  39.     LOG("SaveForUpgrade: "..repr(brain.Nickname).." / "..repr(unit:GetUnitId()).." @ "..repr(GetMinute()))
  40.     local goldRank = GetGoldRank(unit, brain)
  41.     local warRank = brain.Score.WarRank
  42.     local enemyRank = GetEnemyTeamBrain(unit).Score.WarRank
  43.     LOG("\tGold Rank: "..repr(goldRank)..", War Rank: "..repr(warRank)..", Enemy War Rank: "..repr(enemyRank))
  44.  
  45.     --Deferred init for the upgrade queue, to allow other mods to hook SaveForUpgrades
  46.     if upgradeQueue == false then
  47.         LOG("\tInitializing upgrade queue..")
  48.         --Assign costs on startup
  49.         for num, upgrade in SaveForUpgrades do
  50.             upgrade.Cost = UpgradeCost(upgrade.Name)
  51.         end
  52.         --Then copy the map table to the queue
  53.         upgradeQueue = table.deepcopy(SaveForUpgrades)
  54.     end
  55.  
  56.     --Prune purchased upgrades
  57.     while TeamHasUpgrade(unit, upgradeQueue[1].Name) do
  58.         LOG("\tTeam has upgrade "..repr(upgradeQueue[1].Name)..", removing from queue..")
  59.         table.remove(upgradeQueue, 1)
  60.     end
  61.  
  62.     --Get a list of upgrades that we should be saving for now
  63.     local currentUpgrades = {}
  64.     LOG("\tUpgrades to save for:")
  65.     for num, data in upgradeQueue do
  66.         if (data.Rank and warRank >= data.Rank) or (data.ERank and enemyRank >= data.ERank) or (data.EHas and EnemyHasUpgrade(unit, data.Name)) then
  67.             LOG("\t\t"..repr(data.Name).." - "..repr(data.Cost))
  68.             table.insert(currentUpgrades, table.copy(data))
  69.         end
  70.     end
  71.     if table.getn(currentUpgrades) < 1 then
  72.         LOG("\t\t(none)")
  73.     end
  74.  
  75.     --Return which one this AI should save for, if any
  76.     local saveFor = currentUpgrades[goldRank]
  77.     if saveFor then
  78.         LOG("\tSaving for upgrade "..repr(saveFor.Name)..", cost "..repr(saveFor.Cost))
  79.         return saveFor.Name, saveFor.Cost
  80.     else
  81.         LOG("\tNot saving.")
  82.         return false
  83.     end
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement