NoxiaZ

Untitled

Oct 26th, 2018
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.28 KB | None | 0 0
  1. using Google.Protobuf.Collections;
  2. using SC2API_CSharp;
  3. using SC2APIProtocol;
  4. using SC2Bot.Controllers;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Numerics;
  8. using Action = SC2APIProtocol.Action;
  9.  
  10. namespace SC2Bot
  11. {
  12.     public class Unit
  13.     {
  14.         private SC2APIProtocol.Unit original;
  15.         public UnitTypeData UnitTypeData => BaseController.GameData.Units[(int)UnitTypeID];
  16.         public string Name => UnitTypeData.Name;
  17.         public uint UnitTypeID => original.UnitType;
  18.         public float Integrity => (original.Health + original.Shield) / (original.HealthMax + original.ShieldMax);
  19.         public Vector3 Position => original.Pos.ToVector3();
  20.         public Point WorldPosition => original.Pos;
  21.         public ulong Tag => original.Tag;
  22.         public float BuildProgress => original.BuildProgress;
  23.         public UnitOrder Order => original.Orders.FirstOrDefault() ?? new UnitOrder();
  24.         public RepeatedField<UnitOrder> Orders => original.Orders;
  25.         public int Supply => (int)UnitTypeData.FoodRequired;
  26.         public bool IsVisible => original.DisplayType == DisplayType.Visible;
  27.         public int IdealWorkers => original.IdealHarvesters;
  28.         public int AssignedWorkers => original.AssignedHarvesters;
  29.         public bool MissingWorkers { get; set; } = true;
  30.         public Alliance Alliance => original.Alliance;
  31.         public RepeatedField<uint> Buffs => original.BuffIds;
  32.         public float Energy => original.Energy;
  33.         public RepeatedField<Attribute> Attributes => UnitTypeData.Attributes;
  34.         public Unit(SC2APIProtocol.Unit unit)
  35.         {
  36.             Update(unit);
  37.         }
  38.  
  39.         public void Update(SC2APIProtocol.Unit unit)
  40.         {
  41.             original = unit;
  42.         }
  43.  
  44.  
  45.         public double GetDistance(Unit otherUnit)
  46.         {
  47.             return Vector3.Distance(Position, otherUnit.Position);
  48.         }
  49.  
  50.         public double GetDistanceSquared(Unit otherUnit)
  51.         {
  52.             return Vector3.DistanceSquared(Position, otherUnit.Position);
  53.         }
  54.  
  55.         public double GetDistanceSquared(Vector3 location)
  56.         {
  57.             return Vector3.DistanceSquared(Position, location);
  58.         }
  59.  
  60.         public double GetDistance(Vector3 location)
  61.         {
  62.             return Vector3.Distance(Position, location);
  63.         }
  64.  
  65.         public void Train(uint unitType, bool queue = false)
  66.         {
  67.             if (!queue && Orders.Count > 0)
  68.                 return;
  69.  
  70.             var abilityID = Abilities.GetID(unitType);
  71.             var action = UnitController.CreateRawUnitCommand(abilityID);
  72.             action.ActionRaw.UnitCommand.UnitTags.Add(Tag);
  73.             BaseController.Actions.Add(action);
  74.         }
  75.  
  76.         public void UseAbility(uint abilityID, Unit targetUnit)
  77.         {
  78.             var constructAction = UnitController.CreateRawUnitCommand(abilityID);
  79.             constructAction.ActionRaw.UnitCommand.UnitTags.Add(Tag);
  80.             constructAction.ActionRaw.UnitCommand.TargetUnitTag = targetUnit.Tag;
  81.             BaseController.Actions.Add(constructAction);
  82.         }
  83.  
  84.         private void FocusCamera()
  85.         {
  86.             var action = new Action();
  87.             action.ActionRaw = new ActionRaw();
  88.             action.ActionRaw.CameraMove = new ActionRawCameraMove();
  89.             action.ActionRaw.CameraMove.CenterWorldSpace = new Point();
  90.             action.ActionRaw.CameraMove.CenterWorldSpace.X = Position.X;
  91.             action.ActionRaw.CameraMove.CenterWorldSpace.Y = Position.Y;
  92.             action.ActionRaw.CameraMove.CenterWorldSpace.Z = Position.Z;
  93.             BaseController.Actions.Add(action);
  94.         }
  95.  
  96.         public void Move(Vector3 target)
  97.         {
  98.             var action = UnitController.CreateRawUnitCommand(Abilities.MOVE);
  99.             action.ActionRaw.UnitCommand.TargetWorldSpacePos = new Point2D();
  100.             action.ActionRaw.UnitCommand.TargetWorldSpacePos.X = target.X;
  101.             action.ActionRaw.UnitCommand.TargetWorldSpacePos.Y = target.Y;
  102.             action.ActionRaw.UnitCommand.UnitTags.Add(Tag);
  103.             BaseController.Actions.Add(action);
  104.         }
  105.  
  106.         public void Attack(Vector3 target)
  107.         {
  108.             var action = UnitController.CreateRawUnitCommand(Abilities.ATTACK);
  109.             action.ActionRaw.UnitCommand.TargetWorldSpacePos = new Point2D();
  110.             action.ActionRaw.UnitCommand.TargetWorldSpacePos.X = target.X;
  111.             action.ActionRaw.UnitCommand.TargetWorldSpacePos.Y = target.Y;
  112.             action.ActionRaw.UnitCommand.UnitTags.Add(Tag);
  113.             BaseController.Actions.Add(action);
  114.         }
  115.  
  116.         public void Research(int researchType)
  117.         {
  118.             var research = UnitController.GameData.Upgrades[researchType];
  119.             var abilityID = research.AbilityId;
  120.             var action = UnitController.CreateRawUnitCommand(abilityID);
  121.             action.ActionRaw.UnitCommand.UnitTags.Add(Tag);
  122.             BaseController.AddAction(action);
  123.         }
  124.  
  125.         public void Smart(Unit unit)
  126.         {
  127.             var action = UnitController.CreateRawUnitCommand(Abilities.SMART);
  128.             action.ActionRaw.UnitCommand.TargetUnitTag = unit.Tag;
  129.             action.ActionRaw.UnitCommand.UnitTags.Add(Tag);
  130.             BaseController.Actions.Add(action);
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment