public BadFactionPlayer() { // Initialise the bad faction's characters foragerAI = null; forager = null; builderAI = null; builder = null; hiveAI = null; hive = null; barrackAI = null; barrack = null; depotAI = null; depot = null; // Set the computer's faction if (RTSFactionManager.Instance != null && RTSFactionManager.Instance.Factions.Count != 1) badFaction = RTSFactionManager.Instance.Factions[1].FactionType; // Initialise the opening strategy openingStrategy = OpeningStrategy().GetEnumerator(); strategyIterator = 0; foragerStrategy = ForagerStrategy().GetEnumerator(); foragerIterator = 0; // Initalise the first action currentAction = Action.Null; }
protected override void TickTasks() { base.TickTasks(); RTSBuilding controlledObj = ControlledObject; if (controlledObj == null) { return; } switch (CurrentTask.Type) { case Task.Types.ProductUnit: //!!!!!!temp if (ControlledObject.BuildUnitType == null) { DoTask(new Task(Task.Types.Stop), false); } break; } }
// Identify the bad faction's entities private void IdentifyCharacters(LinkedList<Entity> mapChildren) { builderAI = null; builder = null; hiveAI = null; hive = null; barrackAI = null; barrack = null; depotAI = null; depot = null; // For each map entity foreach (Entity entity in mapChildren) { // Is this a GenericAntCharacter entity //GenericAntCharacter unit = entity as GenericAntCharacter; //RTSUnit rtsunit = entity as RTSUnit; if (entity.Type.Name == "ForagerAnt") { ForagerAnt unit = entity as ForagerAnt; if (unit != null) { if (unit.Intellect != null) { if (unit.Intellect.Faction == badFaction) { if (foragerAI == null) { AntUnitAI intellect = unit.Intellect as AntUnitAI; if (intellect != null) { foragerAI = intellect; forager = unit; } } } } } } else if (entity.Type.Name == "BuilderAnt") { GenericAntCharacter unit = entity as GenericAntCharacter; if (unit != null) { if (unit.Intellect != null) { if (unit.Intellect.Faction == badFaction) { // Identify the builder ant if it has not already been found if (builderAI == null) { AntUnitAI intellect = unit.Intellect as AntUnitAI; if (intellect != null) { // Builder ant found builderAI = intellect; builder = unit; } } } } } } /*else if (entity.Type.Name == "RTSDepot") { RTSMine mine = entity as RTSMine; if (mine != null) { if (mine.Intellect != null) { if (mine.Intellect.Faction == badFaction) { if (depotAI == null) { RTSBuildingAI intellect = mine.Intellect as RTSBuildingAI; if (intellect != null) { // barrack found depotAI = intellect; depot = mine; Log.Warning("depot = (RTSMine)building"); } } } } } }*/ else { // Is this a RTSBuilding entity RTSBuilding building = entity as RTSBuilding; if (building != null) { if (building.Intellect != null) { if (building.Intellect.Faction == badFaction) { // Identify the hive if it has not already been found if (hiveAI == null && building.Type.Name == "AntColmena") { RTSBuildingAI intellect = building.Intellect as RTSBuildingAI; if (intellect != null) { // Hive found hiveAI = intellect; hive = building; } } // Identify the barracks if it has not already been found else if (barrackAI == null && building.Type.Name == "AntBarrack") { RTSBuildingAI intellect = building.Intellect as RTSBuildingAI; if (intellect != null) { // barrack found barrackAI = intellect; barrack = building; } } } } } } } }
protected virtual void TickTasks() { RTSUnit controlledObj = ControlledObject; if (controlledObj == null) { return; } switch (currentTask.Type) { //Stop case Task.Types.Stop: controlledObj.Stop(); break; //Move case Task.Types.Move: case Task.Types.BreakableMove: if (currentTask.Entity != null) { controlledObj.Move(currentTask.Entity.Position); } else { Vec3 pos = currentTask.Position; if ((controlledObj.Position.ToVec2() - pos.ToVec2()).LengthFast() < 1.5f && Math.Abs(controlledObj.Position.Z - pos.Z) < 3.0f) { //get to DoNextTask(); } else { controlledObj.Move(pos); } } break; //Attack, Repair case Task.Types.Attack: case Task.Types.BreakableAttack: case Task.Types.Repair: case Task.Types.BreakableRepair: { //healed if ((currentTask.Type == Task.Types.Repair || currentTask.Type == Task.Types.BreakableRepair) && currentTask.Entity != null) { if (currentTask.Entity.Life == currentTask.Entity.Type.LifeMax) { DoNextTask(); break; } } float needDistance = controlledObj.Type.OptimalAttackDistanceRange.Maximum; Vec3 targetPos; if (currentTask.Entity != null) { targetPos = currentTask.Entity.Position; } else { targetPos = currentTask.Position; } float distance = (controlledObj.Position - targetPos).LengthFast(); if (distance != 0) { bool lineVisibility = false; { if (distance < needDistance) { lineVisibility = true; //direct line visibility check Vec3 start = initialWeapons[0].Position; Ray ray = new Ray(start, targetPos - start); RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing( ray, (int)ContactGroup.CastOnlyContact); foreach (RayCastResult result in piercingResult) { MapObject obj = MapSystemWorld.GetMapObjectByBody(result.Shape.Body); if (obj != null && obj == currentTask.Entity) { break; } if (obj != controlledObj) { lineVisibility = false; break; } } } } //movement control if (lineVisibility) { //stop controlledObj.Stop(); RTSCharacter character = controlledObj as RTSCharacter; if (character != null) { character.SetLookDirection(targetPos); } } else { //move to target controlledObj.Move(targetPos); } //weapons control if (lineVisibility) { foreach (Weapon weapon in initialWeapons) { Vec3 pos = targetPos; Gun gun = weapon as Gun; if (gun != null && currentTask.Entity != null) { gun.GetAdvanceAttackTargetPosition(false, currentTask.Entity, false, out pos); } weapon.SetForceFireRotationLookTo(pos); if (weapon.Ready) { Range range; range = weapon.Type.WeaponNormalMode.UseDistanceRange; if (distance >= range.Minimum && distance <= range.Maximum) { weapon.TryFire(false); } range = weapon.Type.WeaponAlternativeMode.UseDistanceRange; if (distance >= range.Minimum && distance <= range.Maximum) { weapon.TryFire(true); } } } } } } break; //BuildBuilding case Task.Types.BuildBuilding: { float needDistance = controlledObj.Type.OptimalAttackDistanceRange.Maximum; Vec3 targetPos = currentTask.Position; float distance = (controlledObj.Position - targetPos).LengthFast(); if (distance < needDistance) { controlledObj.Stop(); //get to //check free area for build bool free; { Bounds bounds; { PhysicsModel physicsModel = PhysicsWorld.Instance.LoadPhysicsModel( currentTask.EntityType.PhysicsModel); if (physicsModel == null) { Log.Fatal(string.Format("No physics model for \"{0}\"", currentTask.EntityType.ToString())); } bounds = physicsModel.GetGlobalBounds(); bounds += targetPos; } Rect rect = new Rect(bounds.Minimum.ToVec2(), bounds.Maximum.ToVec2()); free = GridPathFindSystem.Instance.IsFreeInMapMotion(rect); } if (!free) { //not free DoNextTask(); break; } //check cost RTSFactionManager.FactionItem factionItem = RTSFactionManager.Instance.GetFactionItemByType(Faction); if (factionItem != null) { float cost = ((RTSBuildingType)currentTask.EntityType).BuildCost; if (factionItem.Money - cost < 0) { //No money DoNextTask(); break; } factionItem.Money -= cost; } RTSBuilding building = (RTSBuilding)Entities.Instance.Create(currentTask.EntityType, Map.Instance); building.Position = currentTask.Position; building.InitialFaction = Faction; building.PostCreate(); building.BuildedProgress = 0; building.Life = 1; //Repair DoTaskInternal(new Task(Task.Types.Repair, building)); } else { controlledObj.Move(targetPos); } } break; } }