コード例 #1
0
        public TownNetwork()
        {
            List <Graph <TownNode> > graphs = new List <Graph <TownNode> >();
            var towns = new AITownList();

            foreach (var(town, _) in towns)
            {
                graphs.Add(new Graph <TownNode>(new TownNode(town, new Point(AITown.GetLocation(town)), AITown.GetName(town))));
            }

            while (graphs.Count > 1)
            {
                //if (graphs.Count % 10 == 0)
                //{
                //    AILog.Info($"Count: {graphs.Count}");
                //}

                int graphMin               = 100000;
                Graph <TownNode> graph1    = null;
                Graph <TownNode> graph2    = null;
                TownNode         bestNode1 = null;
                TownNode         bestNode2 = null;
                int graph2Index            = -1;
                for (int a = 0; a < graphs.Count; a++)
                {
                    for (int b = 0; b < graphs.Count; b++)
                    {
                        if (a != b)
                        {
                            TownNode node1 = null;
                            TownNode node2 = null;
                            int      dist  = graphMin;
                            foreach (var n1 in graphs[a].Nodes)
                            {
                                foreach (var n2 in graphs[b].Nodes)
                                {
                                    var nodeDist = n1.GetDistance(n2);
                                    if (nodeDist < dist)
                                    {
                                        dist  = nodeDist;
                                        node1 = n1;
                                        node2 = n2;
                                    }
                                }
                            }

                            if (dist < graphMin)
                            {
                                bestNode1   = node1;
                                bestNode2   = node2;
                                graph1      = graphs[a];
                                graph2      = graphs[b];
                                graph2Index = b;
                                graphMin    = dist;
                            }
                        }
                    }
                }

                graph1.Nodes.AddRange(graph2.Nodes);
                graph1.Edges.AddRange(graph2.Edges);
                graph1.Edges.Add(new Edge <TownNode>()
                {
                    Node1 = bestNode1, Node2 = bestNode2
                });
                graphs.RemoveAt(graph2Index);
                AILog.Info($"Combine {bestNode1.Name} and {bestNode2.Name} ({graphMin}).");
            }

            this.Graph = graphs[0];
            foreach (var edge in this.Graph.Edges)
            {
                AILog.Info($"- {edge.Node1.Name} <-> {edge.Node2.Name}");
            }
        }
コード例 #2
0
        protected /*override*/ void Start2()
        {
            SignManager signManager = new SignManager();

            this.signs = new AISignList();
            var list = new AIRailTypeList();

            AIRail.SetCurrentRailType(list.Begin());

            AICompany.SetLoanAmount(AICompany.GetMaxLoanAmount());

            ////*
            //RailBuilder.BuildRail(
            //    AIMap.GetTileIndex(42, 121),
            //    AIMap.GetTileIndex(41, 121),
            //    AIMap.GetTileIndex(31, 121),
            //    AIMap.GetTileIndex(30, 121),
            //    new HashSet<TileIndex>(),
            //    1,
            //    this.BuildSign);
            ///*/

            //var i = 39;
            //var ns = RailBuilder.GetNeighbors(
            //    AIMap.GetTileIndex(i, 121),
            //    new RailBuilder.PathInfo(
            //        AIMap.GetTileIndex(i + 1, 121),
            //        1,
            //        1,
            //        RailBuilder.BuildType.Rail,
            //        new RailBuilder.PathInfo(
            //            AIMap.GetTileIndex(i + 2, 121),
            //            1,
            //            1,
            //            RailBuilder.BuildType.Rail,
            //            null)),
            //    this.BuildSign);

            //foreach (var n in ns)
            //{
            //    this.BuildSign(n.Tile, n.Cost.ToString());
            //}
            ///* */

            ///*
            try
            {
                if (this.workStatus == 0)
                {
                    var towns = new AITownList();
                    towns.Valuate(AITown.GetPopulation);
                    RailStationBuildResult s1 = null;
                    foreach (var(town, _) in towns)
                    {
                        if (s1 == null)
                        {
                            s1 = RailStationBuilder.BuildStationNear(AITown.GetLocation(town), this.platformSize, 2);
                        }
                        else
                        {
                            RailStationBuildResult s2        = RailStationBuilder.BuildStationNear(AITown.GetLocation(town), this.platformSize, 2);
                            HashSet <TileIndex>    forbidden = new HashSet <TileIndex>();
                            forbidden.Add(s1.ExitFarther);
                            forbidden.Add(s2.EntryFarther);
                            var good = RailBuilder.BuildRail(s1.EntryCloser, s1.EntryFarther, s2.ExitFarther, s2.ExitCloser, forbidden);
                            signManager.ClearSigns();
                            if (good)
                            {
                                forbidden = new HashSet <TileIndex>();
                                good      = RailBuilder.BuildRail(s1.ExitCloser, s1.ExitFarther, s2.EntryFarther, s2.EntryCloser, forbidden, 1);
                            }

                            if (good)
                            {
                                if (s1.DepotTile != AIMap.TILE_INVALID)
                                {
                                    TrainManager.BuildTrain(s1.DepotTile, new StationID[] { s1.StationID, s2.StationID }, this.BuildSign);
                                }
                                else
                                {
                                    TrainManager.BuildTrain(s2.DepotTile, new StationID[] { s2.StationID, s1.StationID }, this.BuildSign);
                                }
                            }
                            else
                            {
                                AILog.Info("No route found.");
                            }

                            s1 = null;

                            //if (AICompany.GetBankBalance(AICompany.COMPANY_SELF) < 50000)
                            {
                                // Better to stop before bankrupcy.
                                AILog.Info("Better to stop before bankrupcy.");
                                break;
                            }
                        }
                    }
                }

                this.workStatus = 1;
            }
            catch
            {
                AILog.Error("An error happened");
            }
            /* */

            signManager.ClearSigns();
            AILog.Info("Sign count: " + this.signs.Count());
            AIController.Sleep(150);

            AILog.Info("Remove all signs");
            foreach (var(signId, _) in this.signs)
            {
                AISign.RemoveSign(signId);
            }

            while (true)
            {
                // Main loop
                //this.setMinLoan();
                CsTestAi.SetMinimumLoanAmount();
                Sleep(100);
            }
        }