public StarNode(Square sqr, StarNode parent, int cost, int direction) { this.mySqr = sqr; this.parentSqr = parent; this.cost = cost; this.direction = direction; }
public void updatePlayer(int x, int y, int dir, bool shot, int health, int coins, int points, Data_Items.Square newSqr) { this.position = new Vector2(x, y); this.direction = dir; this.points = points; this.coins = coins; this.health = health; this.shot = shot; if(health <= 0) { isAlive = false; currentSquare.removePlayer(); } else if(this.currentSquare != newSqr) { if (this.currentSquare.getPlayer() == this) { this.currentSquare.removePlayer(); } this.currentSquare = newSqr; this.currentSquare.addPlayer(this); } }
//get the coin pile with the least cost path public void GetBestCoin(Square[] sqrs, Coin[] coins, Square current, int direction, out List<StarNode> bestP, out Coin targetCoin) { AStar ast = new AStar(sqrs, mapSize); List<StarNode> bestPath = null; Coin target = null; int minCost = int.MaxValue; foreach(Coin coin in coins) { int index = (int)(coin.position.X+(coin.position.Y*mapSize)); if (sqrs[index].getSType() == 3) { continue; } List<StarNode> path = ast.calculatePath(current, sqrs[index], direction); int cost = path[path.Count - 1].cost; if ((cost < minCost) && ((cost * Util.Constants.Timeout) < coin.remainTime)) { bestPath = path; minCost = cost; target = coin; } } bestP = bestPath; targetCoin = target; //return this.generateCommandList(bestPath, direction); }
public Player(int n, int x, int y, int dir, Color clr, Data_Items.Square current) { this.playerNumber = n; this.position = new Vector2(x, y); this.direction = dir; this.color = clr; this.points = 0; this.coins = 0; this.isAlive = true; this.health = 100; this.shot = false; this.currentSquare = current; }
//get the lifepack with the least cost path public void GetBestHealth(Square[] sqrs, LifePack[] packs, Square current, int direction, out List<StarNode> bestP, out LifePack targetPack) { AStar ast = new AStar(sqrs, mapSize); List<StarNode> bestPath = null; LifePack target = null; int minCost = int.MaxValue; foreach (LifePack pack in packs) { int index = (int)(pack.position.X + (pack.position.Y * mapSize)); List<StarNode> path = ast.calculatePath(current, sqrs[index], direction); int cost = path[path.Count - 1].cost; if ((cost < minCost) && ((cost * Util.Constants.Timeout) < pack.remainTime)) { bestPath = path; minCost = cost; target = pack; } } bestP = bestPath; targetPack = target; }
//calculate the least cost path between two given squares public List<StarNode> calculatePath(Square source, Square destination, int direction) { List<StarNode> openList = new List<StarNode>(); List<StarNode> closedList = new List<StarNode>(); List<Square> closedSqrs = new List<Square>(); openList.Add(new StarNode(source, null, 0, direction)); bool finished = false; int k = 0; while(!finished) { k++; int fullCost = int.MaxValue; //get the node with the least cost foreach(StarNode node in openList) { if (current.mySqr == null) { current = node; fullCost = current.cost + getHeuristic(current.mySqr, destination); } else if ((getHeuristic(node.mySqr, destination) + node.cost) < fullCost) { current = node; fullCost = current.cost + getHeuristic(current.mySqr, destination); } } int currentDirection = current.direction; int xCord = (int)current.mySqr.getCoord().X; int yCord = (int)current.mySqr.getCoord().Y; int cost = 0; int iMin = (xCord > 0) ? -1 : 0; int iMax = (xCord < (mapSize - 1)) ? 1 : 0; int jMin = (yCord > 0) ? -1 : 0; int jMax = (yCord < (mapSize - 1)) ? 1 : 0; for (int i = iMin; i <= iMax; i++) { for (int j = jMin; j <= jMax; j++) { if ((i != 0) && (j != 0)) //if a diagonal square { continue; } if ((i == 0) && (j == 0)) //if the center square { continue; } bool isDir = false; if ((j == -1) && (currentDirection == 0)) { isDir = true; } else if ((j == 1) && (currentDirection == 2)) { isDir = true; } else if ((i == -1) && (currentDirection == 3)) { isDir = true; } else if ((i == 1) && (currentDirection == 1)) { isDir = true; } int index = (xCord+i) + ((yCord+j) * mapSize); Square currentSqr = squares[index]; cost = this.getCost(xCord + i, yCord + j, isDir); int tempDirection = currentDirection; if (j == -1) { tempDirection = 0; } else if (j == 1) { tempDirection = 2; } else if (i == -1) { tempDirection = 4; } else if (i == 1) { tempDirection = 1; } if ((cost >= 0) && (!closedSqrs.Contains(currentSqr))) { openList.Add(new StarNode(currentSqr, current, (cost + current.cost), tempDirection)); } } } openList.Remove(current); closedList.Add(current); closedSqrs.Add(current.mySqr); if((current.mySqr == destination)||(openList.Count == 0)) { finished = true; } current = new StarNode(null, null, 0, 0); } return this.filterClosedL(closedList); //return closedList; }
public AStar(Square[] sqrs, int mapSize) { this.squares = sqrs; this.mapSize = mapSize; }
//calculate a heuristic value for a given source and destination public int getHeuristic(Square source, Square target) { int hDistance = Math.Abs((int)source.getCoord().X - (int)target.getCoord().X); int vDistance = Math.Abs((int)source.getCoord().Y - (int)target.getCoord().Y); int heurCost = hDistance + vDistance; return heurCost; }
//execute a list of commands private void ExecuteCommands() { if ((players[myNumber].currentSquare.getCoord().X != sqrPath[0].getCoord().X) || (players[myNumber].currentSquare.getCoord().Y != sqrPath[0].getCoord().Y)) { ClearPath(); return; } int command = commands[0]; if (command < 4) { if (this.VacantSquare(command)) { if (players[myNumber].direction == command) { lastSquare = sqrPath[0]; sqrPath.RemoveAt(0); } cmn.move(command); lastCommand = commands[0]; commands.RemoveAt(0); } else { cmn.shoot(); } } else if (command == 5) { cmn.shoot(); lastCommand = commands[0]; commands.RemoveAt(0); } sendOK = false; }
protected override void Initialize() { graphics.PreferredBackBufferWidth = Util.Constants.ScreenWidth; graphics.PreferredBackBufferHeight = Util.Constants.ScreenHeight; graphics.IsFullScreen = false; graphics.ApplyChanges(); Window.Title = "Battle Tanks"; //mapSize = Util.Constants.MapSize; scale = Util.Constants.Scale; offset = Util.Constants.Offset; imageSize = Util.Constants.ImageSize; //Console.WriteLine(imageSize); cmn = new Communicator(serverIP,clientIP, serverPortNumber, clientPortNumber); playersSet = false; commands = new List<int>(); sqrPath = new List<Square>(); targetCoin = new Data_Items.Coin(0,0,0,0,0); targetPack = new Data_Items.LifePack(0, 0, 0, 0); lastTime = 0; shootcount = (float)0.1; sendOK = false; opMode = ""; lastCommand = -1; lastSquare = null; isInRange = false; rangeDir = 0; skip = false; playerDead = false; gameInitializing = false; gameFinished = false; rect = new Texture2D(graphics.GraphicsDevice, 90, 30); Color[] data = new Color[90 * 30]; for (int i = 0; i < data.Length; ++i) data[i] = Color.White; rect.SetData(data); tableBack = new Texture2D(graphics.GraphicsDevice, 385, 215); data = new Color[385 * 215]; for (int i = 0; i < data.Length; ++i) data[i] = Color.White; tableBack.SetData(data); base.Initialize(); }
//update the squares array private void UpdateArena(String str) { //< x>,<y>,<damage-level>;< x>,<y>,<damage-level>;< x>,<y>,<damage-level>;< x>,<y>,<damage-level>…..< x>,<y>,<damage-level> String[] mainParts = str.Split(';'); foreach (String st in mainParts) { String[] subParts = st.Split(','); int x = int.Parse(subParts[0]); int y = int.Parse(subParts[1]); int damage = int.Parse(subParts[2]); int index = x + (y * mapSize); if (damage == 4) { squares[index] = new Data_Items.Square(x, y, 0); } else { squares[index] = new Data_Items.Brick(x, y, ((4 - damage) * 25)); } } }
//Fill the square array according to map size private void SetUpArena() { for (int i = 0; i < mapSize; i++) { for (int j = 0; j < mapSize; j++) { int index = i + (j * mapSize); squares[index] = new Data_Items.Square(i, j, 0); } } }