// Receive in a seperate thread public MessageHandler(MainGrid active_grid) { c = Communicator.GetInstance(active_grid); ThreadStart ts = new ThreadStart(c.ReceiveData); Thread t = new Thread(ts,50000000); t.Start(); }
public static bool isDirectShootable(MainGrid mg) { foreach (Tank tk in mg.Tanks.Values.ToList<Tank>()) { if (tk.Player_name == mg.Playername) continue; else if (shootable(mg, tk)) { return true; } } return false; }
public static void updateStats(MainGrid mg) { BaseLogic.mg = mg; player = mg.Playername; health = mg.Tanks[mg.Playername].Health; score = mg.Tanks[mg.Playername].Points; coins = mg.Tanks[mg.Playername].Coins; if (health <= 50) { IsHealthLow = true; } isCoinsNeeded(); }
public static bool shootable(MainGrid grid, AbstractEntity ent) { Tank ourPlayer = grid.getTank(grid.Playername); if (ourPlayer.Location.X == ent.Location.X) { // Our player facing north or south if ((ourPlayer.Location.Y > ent.Location.Y & ourPlayer.Direction == 0) || (ourPlayer.Location.Y < ent.Location.Y & ourPlayer.Direction == 2)) { foreach (StoneWall stone in grid.StoneWalls.Values.ToList<StoneWall>()) { // intercepting stones if (MotionLogic.isInBetween(ent, ourPlayer, stone)) { return false; } } return true; } else { return false; } } else if (ourPlayer.Location.Y == ent.Location.Y) { // our player facing east or west if ((ourPlayer.Location.X > ent.Location.X & ourPlayer.Direction == 3) || (ourPlayer.Location.X < ent.Location.X & ourPlayer.Direction == 1)) { foreach (StoneWall stone in grid.StoneWalls.Values.ToList<StoneWall>()) { // intercepting stones if (MotionLogic.isInBetween(ent, ourPlayer, stone)) { return false; } } return true; } else { return false; } } else { return false; } }
public Game1() { graphics = new GraphicsDeviceManager(this); // The game terrain active_grid = new MainGrid(); // Receive messages from server and update the grid msghandler = new MessageHandler(active_grid); // Send messages to the server and play the game msgSender = new MessageSender(msghandler); // Initiating the Game AI gameAI = new GameAI(active_grid, msgSender); // Initiating timer timer = new TimeKeeper(active_grid); Content.RootDirectory = "Content"; }
// Get the location of the place with no vulnerability public static Vector2 getNearestSafePlace(MainGrid mg, Graph g) { List<Node> safePlaces = getSafePlaces(mg, g); Node nearestNode = null; int tempDist = 10000; foreach (Node n in safePlaces) { if (tempDist > g.getPathByNode(n).Count) { tempDist = g.getPathByNode(n).Count; nearestNode = n; } } return new Vector2(nearestNode.getX(), nearestNode.getY()); }
public static Vector2 getBestCoin(MainGrid mg, Graph g) { // Get the player location Vector2 playerLocation = mg.getTank(mg.Playername).Location; // get the nearest coin pile int dist = 1000; int tempDist; Vector2 nearestCoin = playerLocation; ; foreach (Coin item in mg.Coins.Values.ToList<Coin>()) { tempDist = g.getPathByEntity(item).Count; if (tempDist < dist) { dist = tempDist; nearestCoin = item.Location; } } return nearestCoin; }
public static Vector2 getBestBrickWall(MainGrid mg, Graph g) { // Get the player location Vector2 playerLocation = mg.getTank(mg.Playername).Location; // get the nearest Brick int dist = 1000; int tempDist; Vector2 nearestBrick = playerLocation; ; foreach (BrickWall item in mg.BrickWalls.Values.ToList<BrickWall>()) { tempDist = g.getPathByEntity(item).Count; if (tempDist < dist) { dist = tempDist; nearestBrick = item.Location; } } return nearestBrick; }
// For testing purposes only public Game1(bool test) { graphics = new GraphicsDeviceManager(this); // The game terrain active_grid = new MainGrid(); // Receive messages from server and update the grid msghandler = new MessageHandler(active_grid); // Send messages to the server and play the game msgSender = new MessageSender(msghandler); Content.RootDirectory = "Content"; MessageParser p1; MessageParser p2; MessageParser p3; MessageParser p4; MessageParser p5; MessageParser p6; MessageParser p7; p1 = new GlobalBroadCastHandler(active_grid); p2 = new AquirablesHandler(active_grid); p3 = new MovingAndShootingHandler(active_grid); p4 = new GameInidiationHandler(active_grid); p5 = new JoinMessageParser(active_grid); p6 = new JoinSuccessHandler(active_grid); p7 = new Finalizer(active_grid); p1.setNext(p2); p2.setNext(p3); p3.setNext(p4); p4.setNext(p5); p5.setNext(p6); p6.setNext(p7); timer = new TimeKeeper(active_grid); p1.handleMessage("I:P2:5,3;1,4;3,6;0,8;2,6;4,8;6,3;5,7;1,3:2,4;6,7;7,2;8,6;2,7;1,8;7,4;8,1;0,3;7,1:4,3;6,8;9,3;0,2;1,7;2,3;5,8;9,8;5,2;7,6#"); p1.handleMessage("S:P0;0,0;0:P1;0,9;0:P2;9,0;0#"); p1.handleMessage("C:1,0:5000:1747#"); //p1.handleMessage("C:3,8:10000:1747#"); p1.handleMessage("C:8,8:15000:1747#"); p1.handleMessage("C:6,2:15000:1747#"); p1.handleMessage("L:5,5:5000#"); //p1.handleMessage("L:5,6:5000#"); gameAI = new GameAI(active_grid, msgSender); }
public static Tank getNearestEnemy(MainGrid mg, Graph g) { // Get the player location Vector2 playerLocation = mg.getTank(mg.Playername).Location; // get the nearest coin pile int dist = 1000; int tempDist; Tank nearestEnemy = new Tank(); foreach (Tank tank in mg.Tanks.Values.ToList<Tank>()) { tempDist = g.getPathByEntity(tank).Count; if (tank.Player_name == mg.Playername) continue; if (tempDist < dist) { dist = tempDist; nearestEnemy = tank; } } return nearestEnemy; }
public Graph(MainGrid mg, string player) { this.mg = mg; this.player = player; nodes = new Node[10, 10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { nodes[i, j] = new Node(); nodes[i, j].Type = Components.Empty; nodes[i, j].setX(i); nodes[i, j].setY(j); } } // Setting up Component types for each node foreach (StoneWall itm in mg.StoneWalls.Values.ToList<StoneWall>()) { nodes[(int)itm.Location.X, (int)itm.Location.Y].Type = Components.Stone; } foreach (BrickWall itm in mg.BrickWalls.Values.ToList<BrickWall>()) { nodes[(int)itm.Location.X, (int)itm.Location.Y].Type = Components.Brick; } foreach (Waters itm in mg.Waters.Values.ToList<Waters>()) { nodes[(int)itm.Location.X, (int)itm.Location.Y].Type = Components.Water; } foreach (Tank itm in mg.Tanks.Values.ToList<Tank>()) { if (itm.Player_name == mg.Playername) continue; nodes[(int)itm.Location.X, (int)itm.Location.Y].Type = Components.Tank; } // Setting up neighbour nodes for each node for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { // if waters, or stone walls or brick wall it does not have neighbours but has parents if (nodes[i, j].Type == Components.Water || nodes[i, j].Type == Components.Stone || nodes[i, j].Type == Components.Brick) { continue; } else { // up if (MotionLogic.isValidCell(i - 1, j)) { nodes[i, j].addNeighbour(nodes[i - 1, j]); } // down if (MotionLogic.isValidCell(i + 1, j)) { nodes[i, j].addNeighbour(nodes[i + 1, j]); } // right if (MotionLogic.isValidCell(i, j + 1)) { nodes[i, j].addNeighbour(nodes[i, j + 1]); } // left if (MotionLogic.isValidCell(i, j - 1)) { nodes[i, j].addNeighbour(nodes[i, j - 1]); } } } } // Set distance to our player as zero which is the head foreach (Tank itm in mg.Tanks.Values.ToList<Tank>()) { if (itm.Player_name == player) { nodes[(int)itm.Location.X, (int)itm.Location.Y].setDist(0); } } head = nodes[(int)mg.Tanks[player].Location.X, (int)mg.Tanks[player].Location.Y]; }
public void TestMethod1() { Console.WriteLine("Testing"); MainGrid active_grid = new MainGrid(); MessageParser p1 = new GlobalBroadCastHandler(active_grid); MessageParser p2 = new AquirablesHandler(active_grid); MessageParser p3 = new MovingAndShootingHandler(active_grid); MessageParser p4 = new GameInidiationHandler(active_grid); MessageParser p5 = new JoinMessageParser(active_grid); MessageParser p6 = new JoinSuccessHandler(active_grid); MessageParser p7 = new Finalizer(active_grid); p1.setNext(p2); p2.setNext(p3); p3.setNext(p4); p4.setNext(p5); p5.setNext(p6); p6.setNext(p7); p1.handleMessage("I:P0:5,3;1,4;3,6;0,8;2,6;4,8;6,3;5,7;1,3:2,4;6,7;7,2;8,6;2,7;1,8;7,4;8,1;0,3;7,1:4,3;6,8;9,3;0,2;1,7;2,3;5,8;9,8;5,2;7,6#"); p1.handleMessage("S:P0;0,0;0:P1;0,9;0:P2;9,0;0#"); p1.handleMessage("C:3,8:16423:1747#"); p1.handleMessage("G:P0;0,0;0;0;100;0;0:P1;0,9;0;0;100;0;0:P2;9,0;0;0;100;0;0:5,3,0;1,4,0;3,6,0;0,8,0;2,6,0;4,8,0;6,3,0;5,7,0;1,3,0#"); p1.handleMessage("L:2,8:59130#"); Graph g = new Graph(active_grid, "P0"); Node[,] ns = g.getNodes(); Console.WriteLine("Main grid"); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Console.Write(string.Format("{0,5}", ns[i, j].getDist()) + " "); } Console.WriteLine(); } Dijkstra.run(g); Console.WriteLine("After Running Dijkstra"); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Console.Write(string.Format("{0,5}", ns[i, j].getDist()) + " " + ns[i, j].Type + " "); } Console.WriteLine(); } Console.WriteLine("Done"); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (ns[i, j].getParent() != null) Console.Write(string.Format("{0,5}", ns[i, j].getParent().getX() + "," + ns[i, j].getParent().getY()) + " "); else { Console.Write(string.Format("{0,5}", "_") + " "); } } Console.WriteLine(); } Stack<Node> path = g.getPathByNode(g.getNodes()[9, 9]); //Stack<Node> path = g.getPath(active_grid.getCoin(new Microsoft.Xna.Framework.Vector2(3,8))); while (path.Count > 0) { Node n = path.Pop(); int x = n.getX(); int y = n.getY(); Console.WriteLine(x + " , " + y); } }
// Instantiating the message parser public MessageParser(MainGrid active_grid) { this.active_grid = active_grid; this.nextHandler = null; }
public AquirablesHandler(MainGrid active_grid) : base(active_grid) { }
public JoinMessageParser(MainGrid active_grid) : base(active_grid) { }
private static List<Node> getSafePlaces(MainGrid mg, Graph g) { List<Node> safePlaces = new List<Node>(); Vector2 myLocation = mg.Tanks[mg.Playername].Location; Node[,] nodes = g.getNodes(); Tank ourPlayer = mg.Tanks[mg.Playername]; Node n; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { n = nodes[i, j]; if (n.Type == Components.Empty) { foreach (Tank tk in mg.Tanks.Values.ToList<Tank>()) { if (tk.Player_name == mg.Playername) continue; else { switch (tk.Direction) { case 0: // North // Same X cordinate and our Y is lower if (ourPlayer.Location.X == tk.Location.X && ourPlayer.Location.Y < tk.Location.Y) { // At Risk } else { // Safe safePlaces.Add(nodes[i, j]); } break; case 1: // East // Same Y cordinate and our X is greater if (ourPlayer.Location.Y == tk.Location.Y && ourPlayer.Location.X > tk.Location.X) { // At Risk } else { // Safe safePlaces.Add(nodes[i, j]); } break; case 2: // South // Same X cordinate and our Y is greater if (ourPlayer.Location.X == tk.Location.X && ourPlayer.Location.Y > tk.Location.Y) { // At Risk } else { // Safe safePlaces.Add(nodes[i, j]); } break; case 3: // West // Same Y cordinate and our X is lower if (ourPlayer.Location.Y == tk.Location.Y && ourPlayer.Location.X < tk.Location.X) { // At Risk } else { // Safe safePlaces.Add(nodes[i, j]); } break; } } } } } } return safePlaces; }
public GameAI(MainGrid mg, MessageSender ms) { this.mg = mg; this.ms = ms; }
public MovingAndShootingHandler(MainGrid active_grid) : base(active_grid) { }
public Finalizer(MainGrid active_grid) : base(active_grid) { }
public Traverser(MainGrid mg, MessageSender msg, string player) { this.main_grid = mg; this.ms = msg; this.player_name = player; }
// Check if the current position is vulnerable for an attack public static bool shouldEscape(MainGrid grid, Graph g) { Tank ourPlayer = grid.getTank(grid.Playername); foreach (Tank tank in grid.Tanks.Values.ToList<Tank>()) { if (ourPlayer.Location.X == tank.Location.X) { if ((ourPlayer.Location.Y > tank.Location.Y & tank.Direction == 2) || (ourPlayer.Location.Y < tank.Location.Y & tank.Direction == 0)) { foreach (StoneWall stone in grid.StoneWalls.Values.ToList<StoneWall>()) { if ((ourPlayer.Location.X < stone.Location.X && stone.Location.X < tank.Location.X) || (tank.Location.X < stone.Location.X && stone.Location.X < ourPlayer.Location.X)) { return false; } } return true; } else { return false; } } else if (ourPlayer.Location.Y == tank.Location.Y) { if ((ourPlayer.Location.X > tank.Location.X & tank.Direction == 1) || (ourPlayer.Location.X < tank.Location.X & tank.Direction == 3)) { foreach (StoneWall stone in grid.StoneWalls.Values.ToList<StoneWall>()) { if ((ourPlayer.Location.Y < stone.Location.Y && stone.Location.Y < tank.Location.Y) || (tank.Location.Y < stone.Location.Y && stone.Location.Y < ourPlayer.Location.Y)) { return false; } } return true; } else { return false; } } else { return false; } } return false; }