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);
        }
        public void TestPathfinderSystem()
        {
            GenericOpenHeroesRunner.CreateInstance(new ByteArrayMapLoader(ByteArrayHelper.CreateBase(128)));
            FindPathEvent findPathEvent = new FindPathEvent(new Point(0, 0), new Point(100, 100));

            JEventBus.GetDefault().Post(findPathEvent);

            Assert.IsNotNull(findPathEvent.CalculatedPath);
            Assert.IsNotEmpty(findPathEvent.CalculatedPath);
            Assert.AreEqual(101, findPathEvent.CalculatedPath.Count);
        }
Exemplo n.º 3
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;
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 5
0
        public void FindPathListener(FindPathEvent findPathEvent)
        {
            byte costOfMove = _pathFinder.GetCostOfMove(findPathEvent.End.X, findPathEvent.End.Y);

            bool entranceMode = TerrainApi.IsGround(findPathEvent.End) && StructureApi.IsEntrance(findPathEvent.End);

            if (entranceMode)
            {
                _pathFinder.ChangeCostOfMove(findPathEvent.End.X, findPathEvent.End.Y, 1);
            }

            var result = _pathFinder.FindPath(findPathEvent.Start, findPathEvent.End);

            if (result != null)
            {
                findPathEvent.CalculatedPath = result.Select(step => new Point(step.X, step.Y)).ToList();
                findPathEvent.CalculatedPath.Reverse();
            }

            if (entranceMode)
            {
                _pathFinder.ChangeCostOfMove(findPathEvent.End.X, findPathEvent.End.Y, costOfMove);
            }
        }