Exemplo n.º 1
0
        public void Think(Entity thinker, JEventBus eventBus)
        {
            _eventBus = eventBus;
            GeoEntity geoEntity = thinker.GetComponent <GeoEntity>();
            ArmyAi    armyAi    = thinker.GetComponent <ArmyAi>();
            Army      army      = thinker.GetComponent <Army>();

            FindStructureInArea findStructureInArea = new FindStructureInArea(geoEntity.Position, armyAi.SearchRadius);

            JEventBus.GetDefault().Post(findStructureInArea);

            Entity nearestStructure = null;

            foreach (var structureEntity in findStructureInArea.Results)
            {
                Structure structure    = structureEntity.GetComponent <Structure>();
                GeoEntity structureGeo = structureEntity.GetComponent <GeoEntity>();
                long      geoIndex     = GetGeoIndex(structureGeo.Position);
                if (StructureIsVisited(geoIndex, armyAi) || structure.Fraction == army.Fraction)
                {
                    continue;
                }
                //Check accessibility
                FindPathEvent findPathEvent = new FindPathEvent(geoEntity.Position, structureGeo.Position);
                _eventBus.Post(findPathEvent);
                if (findPathEvent.CalculatedPath == null)
                {
                    continue;
                }

                nearestStructure = structureEntity;
                break;
            }

            if (nearestStructure == null)
            {
                Debug.WriteLine(army + " Skip to FindResources");
                armyAi.ArmyStateMachine.Fire(ArmyTrigger.FindResources);
                return;
            }

            armyAi.SearchRadius = 10;

            GeoEntity resourcePosition = nearestStructure.GetComponent <GeoEntity>();

            GoToEvent goToEvent = new GoToEvent(thinker, resourcePosition.Position);

            Debug.WriteLine(army + " Go For Structure: " + goToEvent.Goal);
            JEventBus.GetDefault().Post(goToEvent);

            if (goToEvent.Complete)
            {
                armyAi.VisitedStructures.Add(GetGeoIndex(resourcePosition.Position));
            }

            armyAi.ArmyStateMachine.Fire(ArmyTrigger.FinishAction);
        }
Exemplo n.º 2
0
        private ArmyAi PrepareArmyAi(EntityWorld entityWorld)
        {
            ArmyAi armyAi = entityWorld.GetComponentFromPool <ArmyAi>();

            armyAi.DefaultDecisionThinker = new DefaultDecisionThinker();
            armyAi.DecisionThinkers.Add(ArmyState.Idle, new IdleDecisionThinker());
            armyAi.DecisionThinkers.Add(ArmyState.SearchForResource, new SearchForResourceDecisionThinker());
            armyAi.DecisionThinkers.Add(ArmyState.SearchForStructure, new SearchForStructureDecisionThinker());
            armyAi.DecisionThinkers.Add(ArmyState.SearchForEnemy, new ArmyEncounterDecisionThinker());

            return(armyAi);
        }
Exemplo n.º 3
0
        public void Think(Entity thinker, JEventBus eventBus)
        {
            _eventBus = eventBus;
            GeoEntity geoEntity = thinker.GetComponent <GeoEntity>();
            ArmyAi    armyAi    = thinker.GetComponent <ArmyAi>();
            Army      army      = thinker.GetComponent <Army>();

            FindResourceInArea findResourceInArea = new FindResourceInArea(geoEntity.Position, armyAi.SearchRadius);

            JEventBus.GetDefault().Post(findResourceInArea);

            Entity nearestResource = null;

            foreach (var resourceEntity in findResourceInArea.Results)
            {
                Resource  resource    = resourceEntity.GetComponent <Resource>();
                GeoEntity resourceGeo = resourceEntity.GetComponent <GeoEntity>();

                FindPathEvent findPathEvent = new FindPathEvent(geoEntity.Position, resourceGeo.Position);
                _eventBus.Post(findPathEvent);
                if (findPathEvent.CalculatedPath == null)
                {
                    continue;
                }

                nearestResource = resourceEntity;
                break;
            }
            if (nearestResource == null)
            {
                armyAi.SearchRadius += 5;
                Debug.WriteLine(army + " Skip to IDLE");
                armyAi.ArmyStateMachine.Fire(ArmyTrigger.FinishAction);
                return;
            }

            armyAi.SearchRadius = 10;

            GeoEntity resourcePosition = nearestResource.GetComponent <GeoEntity>();

            GoToEvent goToEvent = new GoToEvent(thinker, resourcePosition.Position);

            Debug.WriteLine(army + " Go For Resource: " + goToEvent.Goal);
            JEventBus.GetDefault().Post(goToEvent);

            armyAi.ArmyStateMachine.Fire(ArmyTrigger.FinishAction);
        }
Exemplo n.º 4
0
        public override void Process(Entity entity)
        {
            ArmyAi armyAi = entity.GetComponent <ArmyAi>();
            Army   army   = entity.GetComponent <Army>();

            _eventBus.Register(armyAi.DefaultDecisionThinker);
            armyAi.DefaultDecisionThinker.Think(entity, _eventBus);

            float lastMovementPoints = 0; //protection against standing in place

            while (army.MovementPoints > 0 && army.MovementPoints != lastMovementPoints)
            {
                lastMovementPoints = army.MovementPoints;
                IDecisionThinker decisionThinker = armyAi.DecisionThinkers[armyAi.ArmyStateMachine.State];
                decisionThinker.Think(entity, _eventBus);
            }


            _eventBus.Unregister(armyAi.DefaultDecisionThinker);
            // Debug.WriteLine("Update AI");
        }
Exemplo n.º 5
0
        public void Think(Entity thinker, JEventBus eventBus)
        {
            _eventBus = eventBus;
            GeoEntity geoEntity = thinker.GetComponent <GeoEntity>();
            ArmyAi    armyAi    = thinker.GetComponent <ArmyAi>();
            Army      army      = thinker.GetComponent <Army>();

            FindArmyInArea findStructureInArea = new FindArmyInArea(geoEntity.Position, armyAi.SearchRadius * 3);

            JEventBus.GetDefault().Post(findStructureInArea);

            Entity nearestEnemyArmy = null;

            foreach (var structureEntity in findStructureInArea.Results)
            {
                Army encounteredArmy = structureEntity.GetComponent <Army>();
                if (encounteredArmy.Fraction == army.Fraction)
                {
                    continue;
                }
                nearestEnemyArmy = structureEntity;
                break;
            }

            if (nearestEnemyArmy == null)
            {
                Debug.WriteLine(army + " Skip to FindStructure");
                armyAi.ArmyStateMachine.Fire(ArmyTrigger.FindStructure);
                return;
            }

            GeoEntity armyPosition = nearestEnemyArmy.GetComponent <GeoEntity>();

            GoToEvent goToEvent = new GoToEvent(thinker, armyPosition.Position);

            Debug.WriteLine(army + " Go For Battle!: " + goToEvent.Goal);
            JEventBus.GetDefault().Post(goToEvent);

            armyAi.ArmyStateMachine.Fire(ArmyTrigger.FinishAction);
        }
Exemplo n.º 6
0
 private bool StructureIsVisited(long geoIndex, ArmyAi armyAi)
 {
     return(armyAi.VisitedStructures.Contains(geoIndex));
 }