//sets next position for player to move to: called by keyboard processing functions. validates new position against level, //so can't move to blocked position, or position off grid public void SetNextLocation(Coord2 newLoc, Map level) { if (timerMs > 0) return; if (level.ValidPosition(newLoc)) { targetPosition = newLoc; timerMs = moveTime; } }
public Dijkstra(int gridSize, Map map) { Name = "Dijkstra"; profile = new Profile("Dijkstra Path Time"); GridSize = gridSize; nodes = new NodeCollection(gridSize); path = new List<Coord2>(); this.map = map; }
public ScentMap(int gridSize, Map map) { name = "Scent Map"; this.gridSize = gridSize; buffer1 = new ScentBuffer(gridSize); buffer2 = new ScentBuffer(gridSize); sourceValue = 0; this.map = map; }
/// <summary> /// Create a new Dijkstra pathfinding object. /// </summary> /// <param name="gridSize">The size of the grid being searched for paths.</param> /// <param name="map">The map being used.</param> public Dijkstra(int gridSize, Map map) { Name = "Dijkstra"; GridSize = gridSize; nodes = new NodeCollection(gridSize); path = new List<Coord2>(); pathConnectors = new List<Line>(); this.map = map; }
/// <summary> /// Sets target position: the next grid location to move to /// </summary> /// <param name="pos">Next grid position. Must be within 1 cell of current position(in x and y directions) and a valid map position.</param> /// <param name="level">The map being moved on.</param> /// <returns></returns> public bool SetNextGridPosition(Coord2 pos, Map level) { if (pos.X < (gridPosition.X - 1)) return false; if (pos.X > (gridPosition.X + 1)) return false; if (pos.Y < (gridPosition.Y - 1)) return false; if (pos.Y > (gridPosition.Y + 1)) return false; if (!level.ValidPosition(pos)) return false; targetPosition = pos; return true; }
/// <summary> /// Handles animation moving from current grid position (gridLocation) to next grid position (targetLocation). /// </summary> public virtual void Update(GameTime gameTime, Map level, Player plr) { timerMs -= gameTime.ElapsedGameTime.Milliseconds; if (timerMs <= 0) { gridPosition = targetPosition; ChooseNextGridLocation(level, plr); timerMs = moveTime; } //calculate screen position screenPosition = (gridPosition * 15) + ((((targetPosition * 15) - (gridPosition * 15)) * (moveTime - timerMs)) / moveTime); }
/// <summary> /// Creates a new scent map. /// </summary> /// <param name="gridSize">The size of the grid being used by this scent map.</param> /// <param name="map">The map object being used by this scent map.</param> public ScentMap(int gridSize, Map map) { name = "Scent Map"; this.gridSize = gridSize; buffer1 = new ScentBuffer(gridSize); buffer2 = new ScentBuffer(gridSize); sourceValue = 1; this.map = map; path = new List<Coord2>(); isLive = true; scentColor = Color.Red; }
public TestConfig(int pathDist, int nRuns, string output, PathfinderAlgorithm al, Map map, string mapName) { this.pathDist = pathDist; this.mapName = mapName; nOfTestRuns = nRuns; outputFile = output; algo = al; startTime = DateTime.Now; endTime = new DateTime(); // Calculate number of obstacles nOfObstacles = 0; nOfObstacles = CalcNumberOfObstacles(map); }
/// <summary> /// Create a new pathfinder of the given type. /// </summary> /// <param name="algorithm">The type of algorithm the pathfinder should use.</param> /// <param name="gridSize">The grid size the pathfinder is to work with.</param> /// <returns></returns> public static Pathfinder CreatePathfinder(PathfinderAlgorithm algorithm, int gridSize, Map map) { switch (algorithm) { case PathfinderAlgorithm.Dijkstra: return (Pathfinder)new Dijkstra(gridSize, map); case PathfinderAlgorithm.AStar: return (Pathfinder)new AStar(gridSize, map); case PathfinderAlgorithm.ScentMap: return (Pathfinder)new ScentMap(gridSize, map); default: Console.WriteLine("PathfinderFactory: Attempted to create unrecognized pathfinder type. Returning Dijkstra.\n"); return (Pathfinder)new Dijkstra(gridSize, map); } }
//Handles animation moving from current grid position (gridLocation) to next grid position (targetLocation) public void Update(GameTime gameTime, Map map) { if (timerMs > 0) { timerMs -= gameTime.ElapsedGameTime.Milliseconds; if (timerMs <= 0) { timerMs = 0; gridPosition = targetPosition; } } //calculate screen position screenPosition = (gridPosition * 15) + ((((targetPosition * 15) - (gridPosition * 15)) * (moveTime - timerMs)) / moveTime); }
protected override void ChooseNextGridLocation(Map level, Player plr) { try { if (level.pathfinder.GetAlgorithm() != PathfinderAlgorithm.ScentMap) throw new Exception("Wrong bot used for pathfinding algorithm."); // Get a copy of the scent map ScentMap map = (level.pathfinder as ScentMap); // Get neighbouring coordinates Coord2[] neighbours = GetNeighbours(); Coord2 highest = new Coord2(0, 0); bool validFound = false; for (int i = 0; i < neighbours.Length; i++) // Ensures a valid position is initially selected { if (level.ValidPosition(neighbours[i])) { highest = neighbours[i]; validFound = true; break; } } if (validFound) // Only update if at least one neighbouring positon is in fact valid on the map { // Find the highest neighbouring coorindate for (int i = 0; i < neighbours.Length; i++) { if (level.ValidPosition(neighbours[i])) { if (map.Scents.data[neighbours[i].X, neighbours[i].Y] > map.Scents.data[highest.X, highest.Y]) highest = neighbours[i]; } } SetNextGridPosition(highest, level); } } catch (Exception e) { Console.WriteLine("Bot Exception: " + e.Message); } }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); tile.TileSetTexture = Content.Load<Texture2D>("Textures/Tilesets/tileset"); Arial6 = Content.Load<SpriteFont>("Fonts/Arial6"); Arial12 = Content.Load<SpriteFont>("Fonts/Arial12"); map = new Map(Content.Load<Texture2D>("Textures/Tilesets/mousemap")); Camera.WorldWidth = ((map.MapWidth - 2) * Tile.StepX); Camera.WorldHeight = ((map.MapHeight - 2) * Tile.StepY); highlight = Content.Load<Texture2D>("Textures/Tilesets/highlight"); border = Content.Load<Texture2D>("Textures/Tilesets/border"); debugHUD = new Texture2D(GraphicsDevice, 1, 1); debugHUD.SetData(new Color[] { Color.White }); //Vlad to separate class or character class with common methods vlad = new Character(Content.Load<Texture2D>("Textures/Characters/T_Vlad_Sword_Walking_48x48")); vlad.AddAnimation("WalkEast", 0, 48 * 0, 48, 48, 8, 0.1f); vlad.AddAnimation("WalkNorth", 0, 48 * 1, 48, 48, 8, 0.1f); vlad.AddAnimation("WalkNorthEast", 0, 48 * 2, 48, 48, 8, 0.1f); vlad.AddAnimation("WalkNorthWest", 0, 48 * 3, 48, 48, 8, 0.1f); vlad.AddAnimation("WalkSouth", 0, 48 * 4, 48, 48, 8, 0.1f); vlad.AddAnimation("WalkSouthEast", 0, 48 * 5, 48, 48, 8, 0.1f); vlad.AddAnimation("WalkSouthWest", 0, 48 * 6, 48, 48, 8, 0.1f); vlad.AddAnimation("WalkWest", 0, 48 * 7, 48, 48, 8, 0.1f); vlad.AddAnimation("IdleEast", 0, 48 * 0, 48, 48, 1, 0.2f); vlad.AddAnimation("IdleNorth", 0, 48 * 1, 48, 48, 1, 0.2f); vlad.AddAnimation("IdleNorthEast", 0, 48 * 2, 48, 48, 1, 0.2f); vlad.AddAnimation("IdleNorthWest", 0, 48 * 3, 48, 48, 1, 0.2f); vlad.AddAnimation("IdleSouth", 0, 48 * 4, 48, 48, 1, 0.2f); vlad.AddAnimation("IdleSouthEast", 0, 48 * 5, 48, 48, 1, 0.2f); vlad.AddAnimation("IdleSouthWest", 0, 48 * 6, 48, 48, 1, 0.2f); vlad.AddAnimation("IdleWest", 0, 48 * 7, 48, 48, 1, 0.2f); vlad.Position = new Vector2(180, 160); vlad.DrawOffset = new Vector2(-24, -38); vlad.CurrentAnimation = "WalkEast"; vlad.IsAnimating = true; }
//this function is filled in by a derived class: must use SetNextGridLocation to actually move the bot protected override void ChooseNextGridLocation(Map level, Player plr) { try { if (level.pathfinder.GetAlgorithm() != PathfinderAlgorithm.AStar && level.pathfinder.GetAlgorithm() != PathfinderAlgorithm.Dijkstra) throw new Exception("Wrong bot used for pathfinding algorithm."); List<Coord2> path = level.pathfinder.GetPath(); if (path.Count > 0 && currentPathIndex != path.Count - 1) { SetNextGridPosition(path[currentPathIndex], level); currentPathIndex++; } else currentPathIndex = 0; } catch(Exception e) { Console.WriteLine("Bot Exception: " + e.Message); } }
protected override void ChooseNextGridLocation(Map level, Player plr) { //Coord2[] neighbours = GetNeighbours(); //int cVal = 0; //Coord2 cPos = new Coord2(0, 0); //for (int i = 0; i < neighbours.Length; i++) //{ // if(level.ValidPosition(neighbours[i])) // { // int lVal = level.scentMap.Buffer1.data[neighbours[i].X, neighbours[i].Y]; // if (lVal > cVal) // { // cVal = lVal; // cPos = new Coord2(neighbours[i].X, neighbours[i].Y); // } // } //} //SetNextGridPosition(cPos, level); }
public Level(Map map, Player plr, AiBotBase bot) { this.map = map; this.player = plr; this.bot = bot; }
public void Move(Map map, KeyboardState ks) { MoveDir = Vector2.Zero; if (Target.X >= 0 && Target.Y >= 0) { if ((int)Position.X < (int)Target.X) MoveDir += new Vector2(1, 0); if ((int)Position.X > (int)Target.X) MoveDir += new Vector2(-1, 0); if ((int)Position.Y < (int)Target.Y) MoveDir += new Vector2(0, 1); if ((int)Position.Y > (int)Target.Y) MoveDir += new Vector2(0, -1); } if (Position.X == Target.X && Position.Y == Target.Y) { if (path.Count > 0) moveToNextTarget(); else Target = new Point(-1, -1); } MoveBy((int)MoveDir.X, (int)MoveDir.Y); SetMoveAnimation(); float x = MathHelper.Clamp(Position.X, 0 + (-2) * DrawOffset.X, Camera.WorldWidth); float y = MathHelper.Clamp(Position.Y, 0 + (-2) * DrawOffset.Y, Camera.WorldHeight); Position = new Vector2(x, y); }
/// <summary> /// Create a new AStar pathfinder object. /// </summary> /// <param name="gridSize">The size of the grid being used.</param> /// <param name="map">The map object being used.</param> public AStar(int gridSize, Map map) : base(gridSize, map) { Name = "A Star"; }
//this function is filled in by a derived class: must use SetNextGridLocation to actually move the bot protected abstract void ChooseNextGridLocation(Map level, Player plr);
protected override void ChooseNextGridLocation(Map level, Player plr) { SetNextGridPosition(gridPosition, level); }
public void SetTarget(Map map, Point target) { path = map.getPath(new Point((int)Position.X, (int)Position.Y), target); moveToNextTarget(); }
/// <summary> /// Calculates the number of blocked spaces in the given map. /// </summary> private int CalcNumberOfObstacles(Map map) { int count = 0; for(int x = 0; x < map.GridSize; x++) { for(int y = 0; y < map.GridSize; y++) { if(!map.ValidPosition(new Coord2(x, y))) count++; } } return count; }
public AStar(int gridSize, Map map) : base(gridSize, map) { nodes = new NodeCollection(gridSize); Name = "A Star"; }