示例#1
0
        public void createRoadQueue(string planetName)
        {
            if (roadQueues.Where(x => x.planetName == planetName).Count() > 0)
            {
                Log.Message("Empire - Road queue for " + Find.World.info.name + " already exists.");
                return;
            }
            FCRoadBuilderQueue queue = new FCRoadBuilderQueue(planetName, roadDef);

            roadQueues.Add(queue);
        }
示例#2
0
        public void calculateRoadPath(FCRoadBuilderQueue queue)
        {
            if (roadDef == null)
            {
                //Log.Message("===== Road def Null =====");
                return;
            }
            FactionFC        faction    = Find.World.GetComponent <FactionFC>();
            List <WorldPath> buildQueue = new List <WorldPath>();


            List <int> settlementLocations = new List <int>();

            //get list of settlements
            foreach (SettlementFC settlement in faction.settlements)
            {
                if (settlement.planetName == queue.planetName)
                {
                    settlementLocations.Add(settlement.mapLocation);
                }
            }

            //TO DO -- add player settlement locations here
            foreach (Settlement settlement in Find.World.worldObjects.Settlements)
            {
                if (settlement.Faction.IsPlayer)
                {
                    settlementLocations.Add(settlement.Tile);
                }
            }
            //from each settlement location, pair up with every other settlement location
            for (int i = settlementLocations.Count() - 1; i > 0; i--)
            {
                for (int k = 0; k < (settlementLocations.Count() - 1); k++)
                {
                    //Log.Message("3 - " + i + "i = k" + k);
                    WorldPath path = Find.WorldPathFinder.FindPath(settlementLocations[i], settlementLocations[k], null, null);
                    if (path != null && path != WorldPath.NotFound)
                    {
                        if (!testPath(path))
                        {
                            buildQueue.Add(path);
                        }
                    }
                }
                settlementLocations.RemoveAt(i);
            }
            queue.ToBuild = buildQueue;
            //Log.Message(queue.ToBuild.Count().ToString() + " number of road paths calculated");
        }
示例#3
0
        public void createNextSegment(FCRoadBuilderQueue queue)
        {
            WorldGrid grid = Find.WorldGrid;
            int       num  = 1;
            //Log.Message("Loop initiated =====");
            List <int>       tilesConstructed = new List <int>();
            List <WorldPath> removeQueue      = new List <WorldPath>();

            foreach (WorldPath path in queue.ToBuild)
            {
                //Log.Message(num + " - start");
                num += 1;
                List <int> nodeList = (List <int>)(typeof(WorldPath).GetField("nodes", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(path));
                typeof(WorldPath).GetField("curNodeIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(path, nodeList.Count - 1, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, null, null);
                //Log.Message("New Path - " + nodeList.Count());
                WorldPath newPath = new WorldPath();
                newPath = path;
                int nodes    = newPath.NodesLeftCount;
                int prevtile = newPath.FirstNode;
                int tile;

                //create segments
                //check if segment already has road
                //if not, create segment and return

                //do same but backwards
                for (int i = 0; i < nodes - 1; i++)
                {
                    if (newPath.NodesLeftCount == 1)
                    {
                        tile = newPath.LastNode;
                    }
                    else
                    {
                        tile = newPath.ConsumeNextNode();
                    }
                    if (tilesConstructed.Contains(prevtile))
                    {
                        //Log.Message(num + " tried to make a road from an already constructed tile");
                        goto Next;
                    }

                    //Log.Message(tile + "n - o" + prevtile);
                    RoadDef def = grid.GetRoadDef(prevtile, tile);
                    if (def == roadDef)
                    {
                        prevtile = tile;
                        continue;
                    }
                    if (def != null && isNewRoadBetter(def, roadDef))
                    {
                        grid.tiles[prevtile].potentialRoads.RemoveAll((Tile.RoadLink rl) => rl.neighbor == tile);
                        grid.tiles[tile].potentialRoads.RemoveAll((Tile.RoadLink rl) => rl.neighbor == prevtile);

                        grid.tiles[prevtile].potentialRoads.Add(new Tile.RoadLink {
                            neighbor = tile, road = roadDef
                        });
                        grid.tiles[tile].potentialRoads.Add(new Tile.RoadLink {
                            neighbor = prevtile, road = roadDef
                        });
                        tilesConstructed.Add(tile);
                    }
                    else if (def == null)  //if null
                    {
                        if (grid.tiles[prevtile].potentialRoads == null)
                        {
                            grid.tiles[prevtile].potentialRoads = new List <Tile.RoadLink>();
                        }
                        if (grid.tiles[tile].potentialRoads == null)
                        {
                            grid.tiles[tile].potentialRoads = new List <Tile.RoadLink>();
                        }
                        grid.tiles[prevtile].potentialRoads.Add(new Tile.RoadLink {
                            neighbor = tile, road = roadDef
                        });
                        grid.tiles[tile].potentialRoads.Add(new Tile.RoadLink {
                            neighbor = prevtile, road = roadDef
                        });
                        tilesConstructed.Add(tile);
                        if (tile == newPath.LastNode)
                        {
                            //Log.Message("Removed " + num + " from queue");
                            removeQueue.Add(path);
                        }
                    }
                    else
                    {
                        prevtile = tile;
                        continue;
                    }

                    //Regen worldmap
                    try
                    {
                        Find.World.renderer.SetDirty <WorldLayer_Roads>();
                        Find.World.renderer.SetDirty <WorldLayer_Paths>();
                        Find.WorldPathGrid.RecalculatePerceivedMovementDifficultyAt(prevtile);
                        Find.WorldPathGrid.RecalculatePerceivedMovementDifficultyAt(tile);
                    }
                    catch (Exception e)
                    {
                    }
                    //Log.Message("Created road step");
                    goto Next;
                }
Next:
                continue;
            }

            foreach (WorldPath queueRemove in removeQueue)
            {
                //Log.Message("Removing queue");
                queue.ToBuild.Remove(queueRemove);
            }
        }