public static TileSet loadTileSet(RabbitPlatform game, string setFileName) { TileSetInfo info; TileSet ts; TileSetFile file = new TileSetFile(setFileName); TileMask[] masks; if (file.ReadMagic() != "TS") { throw new FileLoadException("Wrong file format!", file.path); } info = new TileSetInfo(file.ReadUInt32(), file.ReadUInt32(), file.ReadByte()); masks = new TileMask[info.Width * info.Height]; Texture2D texture = TileGraphicsFilePart.fromStream(game, file.BaseStream); for (int y = 0; y < info.Height; y++) { for (int x = 0; x < info.Width; x++) { masks[x + y * info.Width] = TileMaskFilePart.fromStream(file.BaseStream); } } ts = TileSet.loadFromTexture(texture, info, masks); return(ts); }
public void MakeNew() { IsLoaded = false; MyTileSetInfo = new TileSetInfo(); CustomStartEnd = false; Pillars = new BlockGroup(); Platforms = new BlockGroup(); Ceilings = new BlockGroup(); StartBlock = new BlockGroup(); EndBlock = new BlockGroup(); FixedWidths = false; ProvidesTemplates = false; StandInType = TileSets.None; ObstacleUpgrades = new List <Upgrade>(); FlexibleHeight = false; HasCeiling = false; Name = ""; MyPath = ""; Guid = 0; Tint = new Vector4(1); ScreenshotString = ""; MyBackgroundType = BackgroundType.Random; CoinScoreColor = new Color(220, 255, 255); }
public void UpdateQuad(int flattenedTileIndex, ref CCTileGidAndFlags tileGID, bool updateBuffer = false) { int adjustedTileIndex = flattenedTileIndex - TileStartIndex; int adjustedStartVertexIndex = adjustedTileIndex * NumOfCornersPerQuad; if (tileGID.Gid == 0) { for (int i = 0; i < NumOfCornersPerQuad; i++) { QuadsVertexBuffer.Data[adjustedStartVertexIndex + i] = new CCV3F_C4B_T2F(); } if (updateBuffer) { QuadsVertexBuffer.UpdateBuffer(adjustedStartVertexIndex, NumOfCornersPerQuad); } return; } else if (tileGID.Gid < TileSetInfo.FirstGid || tileGID.Gid > TileSetInfo.LastGid) { return; } float left, right, top, bottom, vertexZ; vertexZ = TileMapLayer.TileVertexZ(flattenedTileIndex); CCSize tileSize = TileSetInfo.TileTexelSize * CCTileMapLayer.DefaultTexelToContentSizeRatios; CCSize texSize = TileSetInfo.TilesheetSize; CCPoint tilePos = TileMapLayer.TilePosition(flattenedTileIndex); var quadVertexData = QuadsVertexBuffer.Data; var quad = new CCV3F_C4B_T2F_Quad(); // vertices if ((tileGID.Flags & CCTileFlags.TileDiagonal) != 0) { left = tilePos.X; right = tilePos.X + tileSize.Height; bottom = tilePos.Y + tileSize.Width; top = tilePos.Y; } else { left = tilePos.X; right = tilePos.X + tileSize.Width; bottom = tilePos.Y + tileSize.Height; top = tilePos.Y; } float temp; if ((tileGID.Flags & CCTileFlags.Vertical) != 0) { temp = top; top = bottom; bottom = temp; } if ((tileGID.Flags & CCTileFlags.Horizontal) != 0) { temp = left; left = right; right = temp; } if ((tileGID.Flags & CCTileFlags.TileDiagonal) != 0) { // FIXME: not working correcly quad.BottomLeft.Vertices = new CCVertex3F(left, bottom, vertexZ); quad.BottomRight.Vertices = new CCVertex3F(left, top, vertexZ); quad.TopLeft.Vertices = new CCVertex3F(right, bottom, vertexZ); quad.TopRight.Vertices = new CCVertex3F(right, top, vertexZ); } else { quad.BottomLeft.Vertices = new CCVertex3F(left, bottom, vertexZ); quad.BottomRight.Vertices = new CCVertex3F(right, bottom, vertexZ); quad.TopLeft.Vertices = new CCVertex3F(left, top, vertexZ); quad.TopRight.Vertices = new CCVertex3F(right, top, vertexZ); } // texcoords CCRect tileTexture = TileSetInfo.TextureRectForGID(tileGID.Gid); left = ((tileTexture.Origin.X) / texSize.Width) + 0.5f / texSize.Width; right = left + ((tileTexture.Size.Width) / texSize.Width) - 1.0f / texSize.Width; bottom = ((tileTexture.Origin.Y) / texSize.Height) + 0.5f / texSize.Height; top = bottom + ((tileTexture.Size.Height) / texSize.Height) - 1.0f / texSize.Height; quad.BottomLeft.TexCoords = new CCTex2F(left, bottom); quad.BottomRight.TexCoords = new CCTex2F(right, bottom); quad.TopLeft.TexCoords = new CCTex2F(left, top); quad.TopRight.TexCoords = new CCTex2F(right, top); quad.BottomLeft.Colors = CCColor4B.White; quad.BottomRight.Colors = CCColor4B.White; quad.TopLeft.Colors = CCColor4B.White; quad.TopRight.Colors = CCColor4B.White; QuadsVertexBuffer.Data.Elements[adjustedStartVertexIndex] = quad.TopLeft; QuadsVertexBuffer.Data.Elements[adjustedStartVertexIndex + 1] = quad.BottomLeft; QuadsVertexBuffer.Data.Elements[adjustedStartVertexIndex + 2] = quad.TopRight; QuadsVertexBuffer.Data.Elements[adjustedStartVertexIndex + 3] = quad.BottomRight; if (updateBuffer) { QuadsVertexBuffer.UpdateBuffer(adjustedStartVertexIndex, NumOfCornersPerQuad); } }
/// <summary> /// Constructor that attempts to load all data during initialization. This will discard any /// ppreviously loaded data and replace it with this new information. /// </summary> /// <param name="ContentMgr">The content manager to use when loading image data</param> /// <param name="XMLFile">The path to the XML file containing the tileset information</param> /// <param name="RootPath">The root path in the XML file to find the tile information tags</param> public void LoadTileSetXML(ContentManager ContentMgr, string XMLFile, string RootPath) { XmlDocument TilesXML; XmlNodeList TilesList, NamedTilesList; TileSetInfo NewSet; string Name; UInt32 MarchID; Point Coords; cTileSetList.Clear(); try { TilesXML = new XmlDocument(); TilesXML.Load(XMLFile); } catch (Exception ExErr) { throw new Exception(String.Format("Failed to load XML File {1}{0}Exception {2}{0}Message {3}", Environment.NewLine, XMLFile, ExErr.GetType().ToString(), ExErr.Message)); } TilesList = TilesXML.DocumentElement.SelectNodes(RootPath + "/tiles"); foreach (XmlNode TileNode in TilesList) { NewSet = new TileSetInfo(); NewSet.MarchingSquaresList = new Dictionary <MarchingSquaresTiles, Point>(); NewSet.NamedTileList = new Dictionary <string, Point>(); //Load all tile set details from the XML file if (TileNode.Attributes["name"] != null) { NewSet.Name = TileNode.Attributes["name"].InnerText; } else { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a tiles tag with no name attribute.", Environment.NewLine, XMLFile)); } if (TileNode.Attributes["file"] != null) { NewSet.ImageFile = TileNode.Attributes["file"].InnerText; } else { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a tiles tag named {2} with no file attribute.", Environment.NewLine, XMLFile, NewSet.Name)); } if (TileNode.Attributes["columns"] != null) { if (Int32.TryParse(TileNode.Attributes["columns"].InnerText, out NewSet.ColumnCnt) == false) { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a tiles tag named {2} with an invalid columns attribute.", Environment.NewLine, XMLFile, NewSet.Name)); } } else { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a tiles tag named {2} with no columns attribute.", Environment.NewLine, XMLFile, NewSet.Name)); } if (TileNode.Attributes["rows"] != null) { if (Int32.TryParse(TileNode.Attributes["columns"].InnerText, out NewSet.RowCnt) == false) { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a tiles tag named {2} with an invalid rows attribute.", Environment.NewLine, XMLFile, NewSet.Name)); } } else { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a tiles tag named {2} with no rows attribute.", Environment.NewLine, XMLFile, NewSet.Name)); } if (TileNode.Attributes["columnwidth"] != null) { if (Int32.TryParse(TileNode.Attributes["columnwidth"].InnerText, out NewSet.TileWidth) == false) { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a tiles tag named {2} with an invalid columnwidth attribute.", Environment.NewLine, XMLFile, NewSet.Name)); } } else { NewSet.TileWidth = -1; } if (TileNode.Attributes["rowheight"] != null) { if (Int32.TryParse(TileNode.Attributes["rowheight"].InnerText, out NewSet.TileHeight) == false) { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a tiles tag named {2} with an invalid rowheight attribute.", Environment.NewLine, XMLFile, NewSet.Name)); } } else { NewSet.TileHeight = -1; } NamedTilesList = TileNode.SelectNodes("namedtile"); foreach (XmlNode NamedTileNode in NamedTilesList) { if (NamedTileNode.Attributes["column"] != null) { if (Int32.TryParse(NamedTileNode.Attributes["column"].InnerText, out Coords.X) == false) { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a tiles tag named {2} with a namedtile tag with an invalid column attribute.", Environment.NewLine, XMLFile, NewSet.Name)); } } else { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a tiles tag named {2} with a namedtile tag with an missing column attribute.", Environment.NewLine, XMLFile, NewSet.Name)); } if (NamedTileNode.Attributes["row"] != null) { if (Int32.TryParse(NamedTileNode.Attributes["row"].InnerText, out Coords.Y) == false) { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a tiles tag named {2} with a namedtile tag with an invalid row attribute.", Environment.NewLine, XMLFile, NewSet.Name)); } } else { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a tiles tag named {2} with a namedtile tag with an missing row attribute.", Environment.NewLine, XMLFile, NewSet.Name)); } if (NamedTileNode.Attributes["name"] != null) { Name = NamedTileNode.Attributes["name"].InnerText; NewSet.NamedTileList.Add(Name, Coords); } if (NamedTileNode.Attributes["marchid"] != null) { Name = NamedTileNode.Attributes["marchid"].InnerText; MarchID = MDLN.Tools.TypeTools.BinaryStringToUInt32(Name); NewSet.MarchingSquaresList.Add((MarchingSquaresTiles)MarchID, Coords); } } //Load the image data for use NewSet.TileTexture = ContentMgr.Load <Texture2D>(NewSet.ImageFile); //Add this tile set to the list cTileSetList.Add(NewSet.Name, NewSet); } }
void UpdateQuadAt(int tileCoordX, int tileCoordY, bool updateBuffer = true) { int flattenedTileIndex = FlattenedTileIndex(tileCoordX, tileCoordY); CCTileGidAndFlags tileGID = tileGIDAndFlagsArray[flattenedTileIndex]; CCTileMapVertAndIndexBuffer drawBuffer = drawBufferManager.GetDrawBufferAtIndex(flattenedTileIndex); int adjustedTileIndex = flattenedTileIndex - drawBuffer.TileStartIndex; if (tileGID.Gid == 0) { drawBuffer.QuadsVertexBuffer.Data[adjustedTileIndex] = new CCV3F_C4B_T2F_Quad(); if (updateBuffer) { drawBuffer.QuadsVertexBuffer.UpdateBuffer(adjustedTileIndex, 1); } return; } float left, right, top, bottom, vertexZ; vertexZ = TileVertexZ(tileCoordX, tileCoordY); CCSize tileSize = TileSetInfo.TileTexelSize * CCTileMapLayer.DefaultTexelToContentSizeRatios; CCSize texSize = TileSetInfo.TilesheetSize; CCPoint tilePos = TilePosition(tileCoordX, tileCoordY); var quad = drawBuffer.QuadsVertexBuffer.Data[adjustedTileIndex]; // vertices if ((tileGID.Flags & CCTileFlags.TileDiagonal) != 0) { left = tilePos.X; right = tilePos.X + tileSize.Height; bottom = tilePos.Y + tileSize.Width; top = tilePos.Y; } else { left = tilePos.X; right = tilePos.X + tileSize.Width; bottom = tilePos.Y + tileSize.Height; top = tilePos.Y; } float temp; if ((tileGID.Flags & CCTileFlags.Vertical) != 0) { temp = top; top = bottom; bottom = temp; } if ((tileGID.Flags & CCTileFlags.Horizontal) != 0) { temp = left; left = right; right = temp; } if ((tileGID.Flags & CCTileFlags.TileDiagonal) != 0) { // FIXME: not working correcly quad.BottomLeft.Vertices = new CCVertex3F(left, bottom, vertexZ); quad.BottomRight.Vertices = new CCVertex3F(left, top, vertexZ); quad.TopLeft.Vertices = new CCVertex3F(right, bottom, vertexZ); quad.TopRight.Vertices = new CCVertex3F(right, top, vertexZ); } else { quad.BottomLeft.Vertices = new CCVertex3F(left, bottom, vertexZ); quad.BottomRight.Vertices = new CCVertex3F(right, bottom, vertexZ); quad.TopLeft.Vertices = new CCVertex3F(left, top, vertexZ); quad.TopRight.Vertices = new CCVertex3F(right, top, vertexZ); } // texcoords CCRect tileTexture = TileSetInfo.TextureRectForGID(tileGID.Gid); left = ((tileTexture.Origin.X) / texSize.Width) + 0.5f / texSize.Width; right = left + ((tileTexture.Size.Width) / texSize.Width) - 1.0f / texSize.Width; bottom = ((tileTexture.Origin.Y) / texSize.Height) + 0.5f / texSize.Height; top = bottom + ((tileTexture.Size.Height) / texSize.Height) - 1.0f / texSize.Height; quad.BottomLeft.TexCoords = new CCTex2F(left, bottom); quad.BottomRight.TexCoords = new CCTex2F(right, bottom); quad.TopLeft.TexCoords = new CCTex2F(left, top); quad.TopRight.TexCoords = new CCTex2F(right, top); quad.BottomLeft.Colors = CCColor4B.White; quad.BottomRight.Colors = CCColor4B.White; quad.TopLeft.Colors = CCColor4B.White; quad.TopRight.Colors = CCColor4B.White; drawBuffer.QuadsVertexBuffer.Data[adjustedTileIndex] = quad; if (updateBuffer) { drawBuffer.QuadsVertexBuffer.UpdateBuffer(adjustedTileIndex, 1); } }
public CCSprite ExtractTile(int column, int row, bool addToTileMapLayer = true) { if (!AreValidTileCoordinates(column, row)) { return(null); } CCTileGidAndFlags gidAndFlags = TileGIDAndFlags(column, row); int flattendedIndex = FlattenedTileIndex(column, row); CCRect texRect = TileSetInfo.TextureRectForGID(gidAndFlags.Gid); CCSprite tileSprite = new CCSprite(tileSetTexture, texRect); tileSprite.ContentSize = texRect.Size * CCTileMapLayer.DefaultTexelToContentSizeRatios; tileSprite.Position = TilePosition(column, row); tileSprite.VertexZ = TileVertexZ(column, row); tileSprite.AnchorPoint = CCPoint.Zero; tileSprite.Opacity = Opacity; tileSprite.FlipX = false; tileSprite.FlipY = false; tileSprite.Rotation = 0.0f; if ((gidAndFlags.Flags & CCTileFlags.TileDiagonal) != 0) { CCSize halfContentSize = tileSprite.ContentSize * 0.5f; tileSprite.AnchorPoint = CCPoint.AnchorMiddle; tileSprite.Position += new CCPoint(halfContentSize.Width, halfContentSize.Height); CCTileFlags horAndVertFlag = gidAndFlags.Flags & (CCTileFlags.Horizontal | CCTileFlags.Vertical); // Handle the 4 diagonally flipped states. if (horAndVertFlag == CCTileFlags.Horizontal) { tileSprite.Rotation = 90.0f; } else if (horAndVertFlag == CCTileFlags.Vertical) { tileSprite.Rotation = 270.0f; } else if (horAndVertFlag == (CCTileFlags.Vertical | CCTileFlags.Horizontal)) { tileSprite.Rotation = 90.0f; tileSprite.FlipX = true; } else { tileSprite.Rotation = 270.0f; tileSprite.FlipX = true; } } else { if ((gidAndFlags.Flags & CCTileFlags.Horizontal) != 0) { tileSprite.FlipX = true; } if ((gidAndFlags.Flags & CCTileFlags.Vertical) != 0) { tileSprite.FlipY = true; } } if (addToTileMapLayer) { AddChild(tileSprite, flattendedIndex, flattendedIndex); } RemoveTile(column, row); return(tileSprite); }
private void Run(Utility utility, string[] args) { // HACK: The engine code assumes that Game.modData is set. Game.ModData = modData = utility.ModData; var filename = args[1]; if (!Game.ModData.DefaultFileSystem.Exists(filename)) { throw new IOException($"Couldn't find map {filename} in maps.vol or filesystem."); } using (var stream = Game.ModData.DefaultFileSystem.Open(filename)) { var tag = stream.ReadUInt32(); // always 4113 var unknown = stream.ReadUInt32(); // always 0 var lgTileWidth = stream.ReadInt32(); var tileHeight = stream.ReadInt32(); // always 64 var numTileSets = stream.ReadInt32(); // always 512 // Update map header fields // Round height up to nearest power of 2 var newHeight = tileHeight; newHeight -= 1; newHeight |= (newHeight >> 1); newHeight |= (newHeight >> 2); newHeight |= (newHeight >> 4); newHeight |= (newHeight >> 8); newHeight |= (newHeight >> 16); newHeight++; var width = 1 << lgTileWidth; // Calculate map width var height = newHeight; map = new Map(modData, modData.DefaultTileSets["default"], width + 2, (int)height + 2) { Title = Path.GetFileNameWithoutExtension(filename), Author = "OpenOP2", RequiresMod = modData.Manifest.Id, }; var tiles = new List <int>(); for (var i = 0; i < width * height; i++) { var tile = stream.ReadInt32(); tiles.Add(tile); } var clipRect = new ClipRect() { X1 = stream.ReadInt32(), Y1 = stream.ReadInt32(), X2 = stream.ReadInt32(), Y2 = stream.ReadInt32(), }; SetBounds(map, width, height); // map.SetBounds(new PPos(clipRect.X1, clipRect.Y1), new PPos(width - clipRect.X2, (int)height - clipRect.Y2)); // map.SetBounds(new PPos(clipRect.X1, clipRect.Y1), new PPos(clipRect.X1 + clipRect.X2, clipRect.Y1 - clipRect.Y2)); // Read in tileset mappings var tileIds = new List <TileSetInfo>(); var tilesetStartIndices = new List <uint>(); var tilesetTileIndex = 0; for (int i = 0; i < numTileSets; i++) { var stringLength = stream.ReadInt32(); if (stringLength <= 0) { continue; } var tilesetMapping = new TileSetInfo() { TileSetName = stream.ReadASCII(stringLength), NumTiles = stream.ReadInt32(), }; tilesetStartIndices.Add((uint)tilesetTileIndex); tilesetTileIndex += tilesetMapping.NumTiles; tileIds.Add(tilesetMapping); } var testString = stream.ReadASCII(10); if (!testString.StartsWith("TILE SET")) { throw new IOException("Couldn't find TILE SET tag."); } var numMappings = stream.ReadInt32(); var mappings = new TileSetMapping[numMappings]; for (var i = 0; i < numMappings; i++) { mappings[i] = new TileSetMapping { TileSetIndex = stream.ReadInt16(), TileIndex = stream.ReadInt16(), NumTileReplacements = stream.ReadInt16(), CycleDelay = stream.ReadInt16(), }; } var numTerrainTypes = stream.ReadInt32(); var terrains = new TerrainType[numTerrainTypes]; stream.Seek(numTerrainTypes * 264, SeekOrigin.Current); var checkTag = stream.ReadUInt32(); if (checkTag != tag) { throw new IOException("Format error: Tag did not match header tag."); } var checkTag2 = stream.ReadInt32(); // the same all the time? var numActors = stream.ReadInt32(); // I think? // TODO: The rest of the tiles // Actually place the tiles for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { var tileXUpper = x >> 5; var tileXLower = x & 0x1F; var tileOffset = (((tileXUpper * height) + y) << 5) + tileXLower; var tile = tiles[tileOffset]; var tile2XUpper = tile >> 5; var tile2XLower = tile & 0x1F; // Get the tile mapping index var cellType = (tile & 0x1F); var tileMappingIndex = (tile & 0xFFE0) >> 5; var actorMappingIndex = (tile & 0x7FF0000) >> 11; var lava = (tile & 0x00000001) >> 27; var lavaPossible = (tile & 0x00000001) >> 28; var expand = (tile & 0x00000001) >> 29; var microbe = (tile & 0x00000001) >> 30; var wallOrBuilding = (tile & 0x00000001) >> 31; if (actorMappingIndex != 0 || lavaPossible != 0 || wallOrBuilding != 0) { throw new Exception("Actor mapping was " + actorMappingIndex); } var thisMapping = mappings[tileMappingIndex]; var startIndex = tilesetStartIndices[thisMapping.TileSetIndex]; map.Tiles[new CPos(x + 1, y + 1)] = new TerrainTile((ushort)(startIndex + thisMapping.TileIndex), 0); } } } mapPlayers = new MapPlayers(map.Rules, 0); map.PlayerDefinitions = mapPlayers.ToMiniYaml(); map.FixOpenAreas(); var dest = Path.GetFileNameWithoutExtension(args[1]) + ".oramap"; var mapLocations = Game.ModData.Manifest.MapFolders; var userMapPath = mapLocations.First(mapLocation => mapLocation.Value == "User"); var targetPath = Path.Combine(Platform.ResolvePath(userMapPath.Key.Substring(1)), dest); map.Save(ZipFileLoader.Create(targetPath)); Console.WriteLine(targetPath + " saved."); }