private void FindPath()
        {
            this.squadPawns
                = map.mapPawns
                  .AllPawnsSpawned.FindAll((obj) => obj.Faction == Faction.OfPlayer);

            Log.Message("Starting PathFinding");

            SquadPather pather =
                new SquadPather(map);


            Log.Message("Starting PathFinding" + squadPawns[0]
                        .Position.GetRegion(map)
                        .Equals(this.target)
                        );

            this.path = pather.GetSquadPathFromTo(
                squadPawns[0].Position.GetRegion(map),
                this.target,
                this.fac,
                100
                );

            Log.Message("nodes Counts " + this.path.nodes.Count);
        }
Exemplo n.º 2
0
        private Job Search(Pawn pawn)
        {
            //float verbRange = pawn.equipment?.PrimaryEq?.PrimaryVerb.verbProps.range ?? 0;

            var AntiScore = 0;

            var Score = FindSightAndRange(pawn, out AntiScore);


            if (AntiScore > squadPawns.Count())
            {
                return(new Job(JobDefOf.FleeAndCower));
            }

            if (AntiScore == 0 && Score == 0)
            {
                SquadPather pather = new SquadPather(map);

                Pawn enemy = null;

                var Min = 10000;

                var temp_x = 0;

                foreach (Pawn p in map.mapPawns.AllPawnsSpawned.FindAll(t => faction.HostileTo(t.Faction)))
                {
                    var temp = FindSightAndRangeAll(p, out temp_x);

                    if (temp < Min)
                    {
                        enemy = p;
                        Min   = temp;
                    }
                }

                if (enemy == null)
                {
                    return(null);
                }

                if (Math.Sqrt(
                        Math.Pow(pawn.Position.x - enemy.Position.x, 2)
                        +
                        Math.Pow(pawn.Position.y - enemy.Position.y, 2)
                        ) <= getWeaponRange(pawn))
                {
                    foreach (Pawn p in squadPawns)
                    {
                        var jobs = squadOrdersForPawns[pawn].jobs;

                        Job combat = new Job(JobDefOf.WaitCombat);

                        combat.expireRequiresEnemiesNearby = false;

                        jobs.Add(combat);
                    }
                }

                SquadPath path = pather.GetSquadPathFromTo(pawn.GetRegion(), enemy.GetRegion(), faction, 0f);

                if (path.nodes.Count > 0)
                {
                    path.nodes.RemoveAt(0);
                }
                else
                {
                    return(null);
                }


                foreach (Pawn p in squadPawns)
                {
                    var range = getWeaponRange(p);

                    int region_num = (int)(range / 10f) + 1;

                    squadOrdersForPawns[pawn].jobs.Clear();

                    var jobs = squadOrdersForPawns[pawn].jobs;

                    for (int i = 0; i < path.nodes.Count - region_num; i++)
                    {
                        Job job = new Job(JobDefOf.Goto, path.nodes[i].RandomCell);

                        job.expireRequiresEnemiesNearby = false;

                        jobs.Add(job);

                        job = new Job(JobDefOf.WaitCombat);

                        jobs.Add(job);
                    }

                    jobs.Add(new Job(CE_JobDefOf.RunForCover));

                    Job combat = new Job(JobDefOf.WaitCombat);

                    combat.expireRequiresEnemiesNearby = false;

                    jobs.Add(combat);
                }
            }

            if (squadOrdersForPawns[pawn].jobs.Count == 0)
            {
                this.Search(pawn);

                Job jb = new Job(JobDefOf.WaitCombat);

                jb.expiryInterval = 60 * 4;

                return(jb);
            }

            Job temp_job = squadOrdersForPawns[pawn].jobs[0];

            squadOrdersForPawns[pawn].jobs.RemoveAt(0);

            return(temp_job);
        }
        private SquadPath GetPathFromTo(Region startRegion, Region targetRegion, Faction fac, float fortLimit)
        {
            //TODO get the list of all pawn in the map
            //TODO (Filter Pawns that are Hostiles to the faction TODO Optimize)

            this.fac = fac;

            this.pawns =
                map.mapPawns.
                AllPawnsSpawned
                .FindAll((obj) => obj.Faction.HostileTo(fac))
                .ToList();

            List <Pawn> RemoverList =
                new List <Pawn>();

            foreach (Pawn pawn in pawns)
            {
                try
                {
                    if (pawn.Faction == null)
                    {
                        RemoverList.Add(pawn);

                        continue;
                    }

                    if (!pawn.Faction.HostileTo(fac))
                    {
                        RemoverList.Add(pawn);

                        continue;
                    }

                    if (pawn.equipment == null)
                    {
                        RemoverList.Add(pawn);

                        continue;
                    }

                    if (!pawn.equipment.HasAnything())
                    {
                        RemoverList.Add(pawn);

                        continue;
                    }

                    if (pawn.equipment.PrimaryEq == null)
                    {
                        RemoverList.Add(pawn);

                        continue;
                    }

                    if (pawn.equipment.PrimaryEq.PrimaryVerb == null)
                    {
                        RemoverList.Add(pawn);

                        continue;
                    }

                    if (pawn.equipment.PrimaryEq.PrimaryVerb.verbProps == null)
                    {
                        RemoverList.Add(pawn);

                        continue;
                    }
                }
                catch (Exception)
                {
                    RemoverList.Add(pawn);

                    continue;
                }
            }

            foreach (Pawn pawn in RemoverList)
            {
                pawns.Remove(pawn);
            }

            RemoverList.Clear();

            List <Region> Path =
                GetPathAStar(startRegion
                             , targetRegion
                             , fac
                             , fortLimit).ToList();

            Path.Reverse();

            //Smooth Down the Path TODO Need Rework
            if (Path.Count != 0)
            {
                List <int> temp =
                    new List <int>();

                for (int i = 0; i < Path.Count - 2; i += 2)
                {
                    temp.Add(i);
                }

                temp.Reverse();

                foreach (int i in temp)
                {
                    Path.RemoveAt(i);
                }

                temp.Clear();
            }

            var squadPath =
                new SquadPath();


            Log.Message("nodes num " + Path.Count);

            squadPath.nodes = Path;

            return(squadPath);
        }