public void GetTileDataTest() { string mapFileName = "testMap"; string mapTitle = "Test"; uint mapWidth = 1; uint mapHeight = 1; Map target = new Map(mapTitle, mapFileName, mapWidth, mapHeight); Tile testTile = new Tile(); testTile.IsPassable = true; Tile[] expected = { testTile }; Tile[] actual = target.GetTileData(); for (int i = 0; i < actual.Length; i++) { if (expected.Length != actual.Length) Assert.Fail("Expected size does not equal actual size"); Assert.AreEqual(expected[i].Effect, actual[i].Effect); Assert.AreEqual(expected[i].EventID, actual[i].EventID); Assert.AreEqual(expected[i].Height, actual[i].Height); Assert.AreEqual(expected[i].ID, actual[i].ID); Assert.AreEqual(expected[i].IsPassable, actual[i].IsPassable); Assert.AreEqual(expected[i].ObjectID, actual[i].ObjectID); Assert.AreEqual(expected[i].Type, actual[i].Type); } }
/// <summary> /// Download the map from Echelon. /// </summary> /// <param name="mapName">Filename of the map to be downloaded.</param> private void DownloadMap(string mapName) { Options options = ServiceManager.Game.Options; Message = String.Format("Downloading map {0}...", mapName); Value = 25; VTankObject.Map map = ServiceManager.Echelon.GetProxy().DownloadMap(mapName); Message = "Reading map..."; Console.WriteLine("Finished downloading map {0}.", map.filename); try { DirectoryInfo directory = new DirectoryInfo(options.MapsFolder); if (!directory.Exists) { directory.Create(); } } catch (Exception) { options.MapsFolder = String.Format(@"{0}\{1}\maps\", Environment.GetEnvironmentVariable("APPDATA"), "VTank"); try { Directory.CreateDirectory(options.MapsFolder); } catch (Exception) { } } Map realMap = new Map( map.title, map.filename, (uint)map.width, (uint)map.height); List<int> buf = new List<int>(); for (int i = 0; i < map.supportedGameModes.Length; i++) { buf.Add(map.supportedGameModes[i]); } realMap.SetGameModes(buf); VTankObject.Tile[] tiles = map.tileData; // Set the tile data. for (uint y = 0; y < (uint)map.height; y++) { for (uint x = 0; x < (uint)map.width; x++) { int position = (int)(y) * map.width + (int)(x); VTankObject.Tile tempTile = tiles[position]; Tile tile = new Tile( (uint)tempTile.id, (ushort)tempTile.objectId, (ushort)tempTile.eventId, tempTile.passable, (ushort)tempTile.height, (ushort)tempTile.type, (ushort)tempTile.effect); realMap.SetTile(x, y, tile); } } try { realMap.SaveMap(options.MapsFolder); } catch (Exception) { options.MapsFolder = String.Format(@"{0}\{1}\maps\", Environment.GetEnvironmentVariable("APPDATA"), "VTank"); try { Directory.CreateDirectory(options.MapsFolder); realMap.SaveMap(options.MapsFolder); } catch (Exception ex) { ServiceManager.Game.Console.DebugPrint("[WARNING] Cannot save map: {0}", ex.Message); } } currentMapInstance = realMap; }
/// <summary> /// Set new data for a tile at a specific location on a 2D map plane. /// </summary> /// <param name="x">X-coordinate (in tiles) of the tile.</param> /// <param name="y">Y-coordinate (in tiles) of the tile.</param> /// <param name="tile">Tile to set to the given (x, y) location.</param> public void SetTile(uint x, uint y, Tile tile) { tile_data[y * Width + x] = tile; }
/// <summary> /// Load a map that exists from disk using the stored filename. /// If the map can't be found, this method throws a FileNotFoundException. If otherwise /// an IO error occurs, a more general-purpose IOException is thrown. /// </summary> /// <see>System.IO.IOException</see> public void LoadMap() { using (BinaryReader reader = new BinaryReader(File.Open(FileName, FileMode.Open, FileAccess.Read))) { byte version_byte = (byte)reader.Read(); if (version_byte != FORMAT_VERSION) { throw new IOException("Version mismatch: The map " + FileName + " uses an invalid format version."); } char c; StringBuilder stringBuffer = new StringBuilder(); while ((c = reader.ReadChar()) != '\n') { stringBuffer.Append(c); } title = stringBuffer.ToString(); byte[] buf = reader.ReadBytes(4); int width = ToInt32(buf[0], buf[1], buf[2], buf[3]); buf = reader.ReadBytes(4); int height = ToInt32(buf[0], buf[1], buf[2], buf[3]); string gameModes = ""; byte b; while ((b = reader.ReadByte()) != '\n') { gameModes += b; } if (width <= 0 || height <= 0) { throw new IOException("Map size cannot have a width or height less than or equal to 0"); } tile_data = new Tile[width * height]; version = version_byte; Title = title; Width = (uint)width; Height = (uint)height; for (int i = 0; i < gameModes.Length; i++) { supportedGameModes.Add(gameModes[i]); } // Now read file contents into the tile data. // The cast is justified if the map is smaller than 2 GiB (on 32 bit systems) int byte_count = Tile.BYTES_PER_TILE * width * height; byte[] buffer = new byte[byte_count]; reader.Read(buffer, 0, byte_count); for (int i = 0, j = 0; i < byte_count; i += Tile.BYTES_PER_TILE, j++) { int tileId = (buffer[i]) | ((buffer[i + 1] << 8)) | ((buffer[i + 2] << 16)) | ((buffer[i + 3] << 24)); int objectId = (buffer[i + 4]) + ((buffer[i + 5] << 8)); int eventId = (buffer[i + 6]) + ((buffer[i + 7] << 8)); bool passable = (buffer[i + 8] == 0) ? false : true; int tile_height = (buffer[i + 9]); int type = (buffer[i + 10]); int effect = (buffer[i + 11]); tile_data[j] = new Tile( (uint)tileId, (ushort)objectId, (ushort)eventId, passable, (ushort)tile_height, (ushort)type, (ushort)effect); } } sha1sum = Hash.CalculateSHA1OfFile(FileName); }
/// <summary> /// Create a new map from scratch. Initializes all tiles to ID '0'. /// It will attempt to write the map to disk with the given filename. If an error occurs, /// a System.IO.IOException will be thrown. /// </summary> /// <param name="mapTitle">Title of the map.</param> /// <param name="mapFileName">The filename of the map.</param> /// <param name="mapWidth">Width (in tiles) of the map.</param> /// <param name="mapHeight">Height (in tiles) of the map.</param> /// <see>System.IO.IOException</see> public void CreateMap(string mapTitle, string mapFileName, uint mapWidth, uint mapHeight) { Title = mapTitle; FileName = mapFileName; Width = mapWidth; Height = mapHeight; tile_data = new Tile[Width * Height]; version = FORMAT_VERSION; uint mapSize = Width * Height; for (uint i = 0; i < mapSize; i++) { tile_data[i] = new Tile(0, 0, 0, true, 0, 0, 0); } SaveMap(); }
public void GetTileTest() { string mapFileName = "testMap"; string mapTitle = "Test"; uint mapWidth = 1; uint mapHeight = 1; Map target = new Map(mapTitle, mapFileName, mapWidth, mapHeight); uint x = 0; uint y = 0; Tile expected = new Tile(); expected.IsPassable = true; Tile actual; actual = target.GetTile(x, y); Assert.AreEqual(expected, actual); }
public void SetTileTest() { string mapFileName = "testMap"; string mapTitle = "Test"; uint mapWidth = 2; uint mapHeight = 2; Map target = new Map(mapTitle, mapFileName, mapWidth, mapHeight); uint x = 1; uint y = 1; Tile tile = new Tile(); target.SetTile(x, y, tile); Assert.IsFalse(target.GetTile(x, y).IsPassable); }