Example #1
0
            public AiWorker(GameMap gameMap, string myPlayerGuid, List<Company> companies, PlayerAIBase.PlayerOrdersEvent sendOrders)
            {
                this.gameMap = gameMap;
                MyPlayerGuid = myPlayerGuid;
                Companies = companies;
                this.sendOrders = sendOrders;
                messages = new Queue<StatusMessage>();

                // get it ready to handle events.
                EventThread = new AutoResetEvent(false);
                ExitThread = false;
            }
Example #2
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckValidMap())
                return;

            if (CheckSaveDirty())
                return;

            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.CheckFileExists = true;
                dlg.DefaultExt = "xml";
                if (! string.IsNullOrEmpty(initialDir))
                    dlg.InitialDirectory = initialDir;
                dlg.Filter = "Map file (*.xml)|*.xml";
                if (dlg.ShowDialog(this) != DialogResult.OK)
                    return;
                filename = dlg.FileName;
                initialDir = Path.GetDirectoryName(filename);
            }
            XDocument xml = XDocument.Load(filename);
            Text = string.Format(caption, Path.GetFileName(filename));
            Map = new GameMap(xml, companies);
            mapDisplay.NewMap();

            InitModes();
        }
Example #3
0
        /// <summary>
        /// Called when the game starts, providing all info.
        /// </summary>
        /// <param name="map">The game map.</param>
        /// <param name="me">The player being setup..</param>
        /// <param name="players">All the players.</param>
        /// <param name="companies">All companies on the board.</param>
        /// <param name="passengers">All the passengers.</param>
        /// <param name="ordersEvent">Callback to pass orders to the engine.</param>
        public void Setup(GameMap map, Player me, List<Player> players, List<Company> companies, List<Passenger> passengers, PlayerAIBase.PlayerOrdersEvent ordersEvent)
        {
            // start the thread
            aiWorker = new AiWorker(map, me.Guid, companies, ordersEvent);
            aiThread = new Thread(aiWorker.MainLoop) {Name = me.Name.Replace(' ', '_'), IsBackground = true};
            aiThread.Start();

            // cause it to pick a passenger to pick up and calculate the path to that company.
            aiWorker.AddMessage(PlayerAIBase.STATUS.NO_PATH, me, AllPickups(me, passengers));
        }
Example #4
0
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckValidMap())
                return;

            if (CheckSaveDirty())
                return;

            using (NewMap dlg = new NewMap())
            {
                if (dlg.ShowDialog(this) != DialogResult.OK)
                    return;
                Map = null; // empty prev memory
                Map = new GameMap(dlg.MapWidth, dlg.MapHeight);
                mapDisplay.NewMap();
            }
            filename = null;

            InitModes();
        }
Example #5
0
        public static List<Point> CalculatePath(GameMap map, Point start, Point end)
        {
            // should never happen but just to be sure
            if (start == end)
                return new List<Point> {start};

            // nodes are points we have walked to
            Dictionary<Point, TrailPoint> nodes = new Dictionary<Point, TrailPoint>();
            // points we have in a TrailPoint, but not yet evaluated.
            List<TrailPoint> notEvaluated = new List<TrailPoint>();

            TrailPoint tpOn = new TrailPoint(start, end, 0);
            while (true)
            {
                nodes.Add(tpOn.MapTile, tpOn);

                // get the neighbors
                TrailPoint tpClosest = null;
                foreach (Point ptOffset in offsets)
                {
                    Point pt = new Point(tpOn.MapTile.X + ptOffset.X, tpOn.MapTile.Y + ptOffset.Y);
                    MapSquare square = map.SquareOrDefault(pt);
                    // off the map or not a road/bus stop
                    if ((square == null) || (!square.Tile.IsDriveable))
                        continue;

                    // already evaluated - add it in
                    if (nodes.ContainsKey(pt))
                    {
                        TrailPoint tpAlreadyEvaluated = nodes[pt];
                        tpAlreadyEvaluated.Cost = Math.Min(tpAlreadyEvaluated.Cost, tpOn.Cost + 1);
                        tpOn.Neighbors.Add(tpAlreadyEvaluated);
                        continue;
                    }

                    // add this one in
                    TrailPoint tpNeighbor = new TrailPoint(pt, end, tpOn.Cost + 1);
                    tpOn.Neighbors.Add(tpNeighbor);
                    // may already be in notEvaluated. If so remove it as this is a more recent cost estimate
                    int indTp = notEvaluated.FindIndex(tp => tp.MapTile == tpNeighbor.MapTile);
                    if (indTp != -1)
                        notEvaluated.RemoveAt(indTp);

                    // we only assign to tpClosest if it is closer to the destination. If it's further away, then we
                    // use notEvaluated below to find the one closest to the dest that we have not walked yet.
                    if (tpClosest == null)
                    {
                        if (tpNeighbor.Distance < tpOn.Distance)
                            // new neighbor is closer - work from this next.
                            tpClosest = tpNeighbor;
                        else
                            // this is further away - put in the list to try if a better route is not found
                            notEvaluated.Add(tpNeighbor);
                    }
                    else
                        if (tpClosest.Distance <= tpNeighbor.Distance)
                            // this is further away - put in the list to try if a better route is not found
                            notEvaluated.Add(tpNeighbor);
                        else
                        {
                            // this is closer than tpOn and another neighbor - use it next.
                            notEvaluated.Add(tpClosest);
                            tpClosest = tpNeighbor;
                        }
                }

                // re-calc based on neighbors
                tpOn.RecalculateDistance(ptOffMap, map.Width);

                // if no closest, then get from notEvaluated. This is where it guarantees that we are getting the shortest
                // route - we go in here if the above did not move a step closer. This may not either as the best choice
                // may be the neighbor we didn't go with above - but we drop into this to find the closest based on what we know.
                if (tpClosest == null)
                {
                    if (notEvaluated.Count == 0)
                    {
                        Trap.trap();
                        break;
                    }
                    // We need the closest one as that's how we find the shortest path.
                    tpClosest = notEvaluated[0];
                    int index = 0;
                    for (int ind = 1; ind < notEvaluated.Count; ind++ )
                    {
                        TrailPoint tpNotEval = notEvaluated[ind];
                        if (tpNotEval.Distance >= tpClosest.Distance)
                            continue;
                        tpClosest = tpNotEval;
                        index = ind;
                    }
                    notEvaluated.RemoveAt(index);
                }

                // if we're at end - we're done!
                if (tpClosest.MapTile == end)
                {
                    tpClosest.Neighbors.Add(tpOn);
                    nodes.Add(tpClosest.MapTile, tpClosest);
                    break;
                }

                // try this one
                tpOn = tpClosest;
            }

            List<Point> path = new List<Point>();
            if (! nodes.ContainsKey(end))
            {
                Trap.trap();
                return path;
            }

            // Create the return path - from end back to beginning.
            tpOn = nodes[end];
            path.Add(tpOn.MapTile);
            while (tpOn.MapTile != start)
            {
                List<TrailPoint> neighbors = tpOn.Neighbors;
                int cost = tpOn.Cost;

                tpOn = tpOn.Neighbors[0];
                for (int ind = 1; ind < neighbors.Count; ind++)
                    if (neighbors[ind].Cost < tpOn.Cost)
                        tpOn = neighbors[ind];

                // we didn't get to the start.
                if (tpOn.Cost >= cost)
                {
                    Trap.trap();
                    return path;
                }
                path.Insert(0, tpOn.MapTile);
            }

            return path;
        }
Example #6
0
        public void Setup(GameMap map, Player me, List<Player> players, List<Company> companies, List<Passenger> passengers,
				   PlayerAIBase.PlayerOrdersEvent ordersEvent)
        {
            ai.Setup(map, me, players, companies, passengers, ordersEvent);
        }
Example #7
0
        /// <summary>
        /// Called when the game starts, providing all info.
        /// </summary>
        public void Setup(GameMap map, Player me, List<Player> players, List<Company> companies, List<Passenger> passengers, PlayerAIBase.PlayerOrdersEvent ordersEvent)
        {
            sendOrders = ordersEvent;

            XDocument doc = new XDocument();
            XElement elemRoot = new XElement("setup", new XAttribute("game-start", true), new XAttribute("my-guid", me.Guid));
            doc.Add(elemRoot);

            // the map
            XElement elemMap = new XElement("map", new XAttribute("width", map.Width), new XAttribute("height", map.Height), new XAttribute("units-tile", TileMovement.UNITS_PER_TILE));
            elemRoot.Add(elemMap);
            for (int x = 0; x < map.Width; x++ )
                for (int y = 0; y < map.Height; y++)
                {
                    MapSquare square = map.Squares[x][y];
                    XElement elemRow = new XElement("tile", new XAttribute("x", x), new XAttribute("y", y),
                                                    new XAttribute("type", square.Tile.Type));
                    if (square.Tile.IsDriveable)
                    {
                        elemRow.Add(new XAttribute("direction", square.Tile.Direction));
                        if (square.StopSigns != MapSquare.STOP_SIGNS.NONE)
                            elemRow.Add(new XAttribute("stop-sign", square.StopSigns));
                        if (square.SignalDirection != MapSquare.SIGNAL_DIRECTION.NONE)
                            elemRow.Add(new XAttribute("signal", true));
                    }
                    elemMap.Add(elemRow);
                }

            // all players (including me)
            XElement elemPlayers = new XElement("players");
            elemRoot.Add(elemPlayers);
            foreach (Player plyrOn in players)
                elemPlayers.Add(new XElement("player", new XAttribute("guid", plyrOn.Guid),
                                                   new XAttribute("name", plyrOn.Name),
                                                   new XAttribute("limo-x", plyrOn.Limo.Location.TilePosition.X),
                                                   new XAttribute("limo-y", plyrOn.Limo.Location.TilePosition.Y),
                                                   new XAttribute("limo-angle", plyrOn.Limo.Location.Angle)));

            // all companies
            XElement elemCompanies = new XElement("companies");
            elemRoot.Add(elemCompanies);
            foreach (Company cmpnyOn in companies)
                elemCompanies.Add(new XElement("company", new XAttribute("name", cmpnyOn.Name),
                            new XAttribute("bus-stop-x", cmpnyOn.BusStop.X), new XAttribute("bus-stop-y", cmpnyOn.BusStop.Y)));

            // all passengers
            XElement elemPassengers = new XElement("passengers");
            elemRoot.Add(elemPassengers);
            foreach (Passenger psngrOn in passengers)
            {
                XElement elemPassenger = new XElement("passenger", new XAttribute("name", psngrOn.Name), new XAttribute("points-delivered", psngrOn.PointsDelivered));
                // if due to a re-start, these can be null
                if (psngrOn.Lobby != null)
                    elemPassenger.Add(new XAttribute("lobby", psngrOn.Lobby.Name));
                if (psngrOn.Destination != null)
                    elemPassenger.Add(new XAttribute("destination", psngrOn.Destination.Name));
                foreach (Company route in psngrOn.Companies)
                    elemPassenger.Add(new XElement("route", route.Name));
                foreach (Passenger enemy in psngrOn.Enemies)
                    elemPassenger.Add(new XElement("enemy", enemy.Name));
                elemPassengers.Add(elemPassenger);
            }

            framework.tcpServer.SendMessage(TcpGuid, doc.ToString());
        }
Example #8
0
 public MapInfo(GameMap map)
 {
     Trap.trap();
     this.map = map;
 }