// return new or current assignment, or null to cancel current assignment, or do nothing is no current assignment protected override IAssignment GetNewOrCurrentAssignment(ActionPriority priority) { if (priority == ActionPriority.Idle) { return(this.CurrentAssignment); } if (m_target == null) { m_target = AIHelpers.FindNearestEnemy(this.Worker, LivingCategory.Civilized); if (m_target == null) { // continue patrolling if (this.CurrentAssignment == null || (this.CurrentAssignment is RandomMoveAssignment) == false) { trace.TraceInformation("Start random move"); return(new RandomMoveAssignment(null)); } else { trace.TraceVerbose("Continue patrolling"); return(this.CurrentAssignment); } } trace.TraceInformation("Found target: {0}", m_target); } Debug.Assert(m_target != null); if (this.CurrentAssignment == null || (this.CurrentAssignment is AttackAssignment) == false) { trace.TraceInformation("Start attacking: {0}", m_target); var assignment = new AttackAssignment(this, m_target); return(assignment); } else { trace.TraceInformation("Continue attacking: {0}", m_target); return(this.CurrentAssignment); } }
// return new or current assignment, or null to cancel current assignment, or do nothing is no current assignment protected override IAssignment GetNewOrCurrentAssignment(ActionPriority priority) { if (priority == ActionPriority.High) { return(this.CurrentAssignment); } var worker = this.Worker; bool hasAssignment = this.CurrentAssignment != null; bool hasOtherAssignment = this.CurrentAssignment == null && this.Worker.HasAction; if (hasOtherAssignment) { return(null); } switch (this.State) { case HervivoreAIState.Grazing: { var enemies = AIHelpers.FindEnemies(worker, LivingCategory.Carnivore | LivingCategory.Civilized | LivingCategory.Monster); if (enemies.Any()) { var fleeVector = GetFleeVector(enemies); trace.TraceInformation("Changing to Flee state, v = {0}", fleeVector); this.State = HervivoreAIState.Fleeing; m_fleeFinishedTick = worker.World.TickNumber + FLEE_TIME; m_fleeAssignment = new FleeMoveAssignment(this); m_fleeAssignment.SetFleeVector(fleeVector); return(m_fleeAssignment); } if (hasAssignment) { return(this.CurrentAssignment); } return(new Dwarrowdelf.Jobs.Assignments.GrazeMoveAssignment(this, this.Group)); } case HervivoreAIState.Fleeing: { var enemies = AIHelpers.FindEnemies(worker, LivingCategory.Carnivore | LivingCategory.Civilized | LivingCategory.Monster); if (enemies.Any()) { var fleeVector = GetFleeVector(enemies); trace.TraceInformation("Updating fleevector: {0}", fleeVector); m_fleeFinishedTick = worker.World.TickNumber + FLEE_TIME; m_fleeAssignment.SetFleeVector(fleeVector); return(m_fleeAssignment); } else if (worker.World.TickNumber < m_fleeFinishedTick) { trace.TraceInformation("Continue fleeing"); return(m_fleeAssignment); } else { m_fleeAssignment.Abort(); trace.TraceInformation("Changing to Graze state"); this.State = HervivoreAIState.Grazing; return(new Dwarrowdelf.Jobs.Assignments.GrazeMoveAssignment(this, this.Group)); } } default: throw new Exception(); } }