Esempio n. 1
0
        // 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();
            }
        }
Esempio n. 2
0
 public void OnObservableJobStatusChanged(IJob job, JobStatus status)
 {
     if (m_fleeAssignment != null && job == m_fleeAssignment)
         m_fleeAssignment = null;
 }