Exemple #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);
        }
Exemple #2
0
        public void UnpackTest()
        {
            CommunicationStream stream = EventTestingUtilities.BuildStream(
                1, 15, 15
                );

            goalComs.SetStream(stream);
            goToEvent = new GoToEvent();
            List <Point> nodes = (List <Point>)EventTestingUtilities.GetInstanceField(typeof(GoToEvent), goToEvent, "locations");

            Assert.AreEqual(new Point(1, 1), nodes[0]);
        }
        public void GoToListener(GoToEvent goToEvent)
        {
            Entity entity = goToEvent.Entity;

            if (!entity.HasComponent <GeoEntity>())
            {
                Error("GoTo ERROR: Missing GeoEntity");
                return;
            }

            GeoEntity geoEntity = entity.GetComponent <GeoEntity>();
            Army      army      = entity.GetComponent <Army>();

            FindPathEvent findPathEvent = new FindPathEvent(geoEntity.Position, goToEvent.Goal);

            JEventBus.GetDefault().Post(findPathEvent);
            if (findPathEvent.CalculatedPath == null || findPathEvent.CalculatedPath.Count == 0)
            {
                Warning("GoTo ERROR: Path not found");
                return;
            }

            //Go to the same place (SPACE)
            if (findPathEvent.CalculatedPath.Count == 1)
            {
                findPathEvent.CalculatedPath.Add(findPathEvent.CalculatedPath[0]);
            }

            int i = 0;

            while (army.MovementPoints > 0)
            {
                if (i >= findPathEvent.CalculatedPath.Count - 1)
                {
                    break;
                }
                MoveToNextEvent moveToNextEvent = new MoveToNextEvent(findPathEvent.CalculatedPath, entity, i);
                JEventBus.GetDefault().Post(moveToNextEvent);
                i++;
            }

            Debug("GoTo OK: " + geoEntity.Position);
            if (geoEntity.Position.Equals(goToEvent.Goal))
            {
                goToEvent.Complete = true;
            }
        }
Exemple #4
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);
        }
Exemple #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);
        }
Exemple #6
0
 public void Init()
 {
     goToEvent = new GoToEvent(new Point(1, 1));
 }