Advertisement
Guest User

BaseBuilder.cs

a guest
Aug 28th, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. #region Copyright & License Information
  2. /*
  3.  * Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
  4.  * This file is part of OpenRA, which is free software. It is made
  5.  * available to you under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation. For more information,
  7.  * see COPYING.
  8.  */
  9. #endregion
  10.  
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using OpenRA.FileFormats;
  15. using OpenRA.Mods.RA.Buildings;
  16. using OpenRA.Traits;
  17. using OpenRA.Mods.RA.Activities;
  18. using XRandom = OpenRA.Thirdparty.Random;
  19.  
  20. namespace OpenRA.Mods.RA.AI
  21. {
  22.     class BaseBuilder
  23.     {
  24.         enum BuildState { ChooseItem, WaitForProduction, WaitForFeedback }
  25.  
  26.         BuildState state = BuildState.WaitForFeedback;
  27.         string category;
  28.         HackyAI ai;
  29.         int lastThinkTick;
  30.         Func<ProductionQueue, ActorInfo> chooseItem;
  31.  
  32.         public BaseBuilder(HackyAI ai, string category, Func<ProductionQueue, ActorInfo> chooseItem)
  33.         {
  34.             this.ai = ai;
  35.             this.category = category;
  36.             this.chooseItem = chooseItem;
  37.         }
  38.  
  39.         public void Tick()
  40.         {
  41.             // Pick a free queue
  42.             var queue = ai.FindQueues( category ).FirstOrDefault();
  43.             if (queue == null)
  44.                 return;
  45.  
  46.             var currentBuilding = queue.CurrentItem();
  47.             switch (state)
  48.             {
  49.                 case BuildState.ChooseItem:
  50.                     {
  51.                         var item = chooseItem(queue);
  52.                         if (item == null)
  53.                         {
  54.                             state = BuildState.WaitForFeedback;
  55.                             lastThinkTick = ai.ticks;
  56.                         }
  57.                         else
  58.                         {
  59.                             HackyAI.BotDebug("AI: Starting production of {0}".F(item.Name));
  60.                             state = BuildState.WaitForProduction;
  61.                             ai.world.IssueOrder(Order.StartProduction(queue.self, item.Name, 1));
  62.                         }
  63.                     }
  64.                     break;
  65.  
  66.                 case BuildState.WaitForProduction:
  67.                     if (currentBuilding == null) return;    /* let it happen.. */
  68.  
  69.                     else if (currentBuilding.Paused)
  70.                         ai.world.IssueOrder(Order.PauseProduction(queue.self, currentBuilding.Item, false));
  71.                     else if (currentBuilding.Done)
  72.                     {
  73.                         state = BuildState.WaitForFeedback;
  74.                         lastThinkTick = ai.ticks;
  75.  
  76.                         /* place the building */
  77.                         CPos? location = null;
  78.  
  79.                         // AI improvement
  80.                         if (ai.pending.Contains(currentBuilding.Item))
  81.                             location = ai.pendingCPos.ElementAt(ai.pending.IndexOf(currentBuilding.Item));
  82.  
  83.                         if (location == new CPos(0,0))
  84.                             location = ai.ChooseBuildLocation(currentBuilding.Item);
  85.  
  86.                         if (location == null)
  87.                         {
  88.                             HackyAI.BotDebug("AI: Nowhere to place {0}".F(currentBuilding.Item));
  89.                             ai.world.IssueOrder(Order.CancelProduction(queue.self, currentBuilding.Item, 1));
  90.                         }
  91.                         else
  92.                         {
  93.                             ai.world.IssueOrder(new Order("PlaceBuilding", ai.p.PlayerActor, false)
  94.                                 {
  95.                                     TargetLocation = location.Value,
  96.                                     TargetString = currentBuilding.Item
  97.                                 });
  98.                         }
  99.                     }
  100.                     break;
  101.  
  102.                 case BuildState.WaitForFeedback:
  103.                     if (ai.ticks - lastThinkTick > HackyAI.feedbackTime)
  104.                         state = BuildState.ChooseItem;
  105.                     break;
  106.             }
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement