3 using System.Collections;
4 using System.Collections.Generic;
7 public class TileGroup {
9 public ArrayList Tiles = new ArrayList();
11 public void Write(LispWriter writer) {
12 writer.StartList("tilegroup");
14 writer.Write("name", Name);
15 writer.Write("tiles", Tiles);
17 writer.EndList("tilegroup");
20 public void Parse(Lisp.Parser parser) {
22 while(parser.Parse() && parser.Depth >= d) {
23 if(parser.Depth == d+1) {
24 if(parser.Type != Parser.LispType.SYMBOL)
25 throw new Exception("expected SYMBOL at supertux-tiles level, but got \"" + parser.StringValue + "\"");
26 string symbol = parser.SymbolValue;
30 Name = parser.StringValue;
34 Tiles.Add(parser.IntegerValue);
35 } while(parser.Parse()
36 && parser.Type == Parser.LispType.INTEGER);
39 Console.WriteLine("Unknown section " + symbol);
47 public class TileSet {
48 private bool tooNew = false;
50 /// <summary>Whether version of tileset file is too new</summary>
57 public ArrayList Tiles = new ArrayList();
58 public ArrayList TileGroups = new ArrayList();
60 public void Write(string filename) {
61 FileStream fs = new FileStream(filename, FileMode.Create);
63 TextWriter tw = new StreamWriter(fs);
64 LispWriter writer = new LispWriter(tw);
66 writer.WriteComment("Generated by tiler");
67 writer.StartList("supertux-tiles");
68 foreach(TileGroup tilegroup in TileGroups) {
69 tilegroup.Write(writer);
71 foreach(Tile tile in Tiles) {
77 writer.EndList("supertux-tiles");
82 public void Parse(string filename) {
83 FileStream fs = new FileStream(filename, FileMode.Open);
84 StreamReader stream = new StreamReader(fs);
86 Lisp.Parser parser = new Lisp.Parser(stream);
88 if(parser.Type != Parser.LispType.START_LIST)
89 throw new Exception("Expected START_LIST");
91 if(parser.Type != Parser.LispType.SYMBOL)
92 throw new Exception("Expected symbol");
93 if(parser.SymbolValue != "supertux-tiles")
94 throw new Exception("not a supertux tile files but " +
102 public void ParseTiles(Lisp.Parser parser) {
104 int d = parser.Depth;
105 while(parser.Parse() && parser.Depth >= d) {
106 if(parser.Depth == d && parser.Type != Parser.LispType.START_LIST) {
107 Console.WriteLine("non-cons type in list...");
111 if(parser.Depth == d+1) {
112 if(parser.Type != Parser.LispType.SYMBOL) {
113 throw new Exception("Expected symbol in list element");
115 switch(parser.SymbolValue) {
120 TileGroup tilegroup = new TileGroup();
121 tilegroup.Parse(parser);
122 TileGroups.Add(tilegroup);
125 Tile tile = new Tile();
128 while(tile.ID >= Tiles.Count)
130 Tiles[tile.ID] = tile;
133 ParseMoreTiles(parser);
136 "Warning: new syntax of \"More tiles in one image\" file isn't currently supported");
138 "And this means: YOU WON'T BE ABLE TO SAVE YOUR CHANGES !!!");
141 throw new Exception("Unexpected listentry: " +
148 public void ParseMoreTiles(Lisp.Parser parser)
152 List<int> ids = new List<int>();
153 List<int> attributes = new List<int>();
154 List<int> datas = new List<int>();
155 List<string> imageNames = new List<string>();
156 ArrayList images = new ArrayList();
159 int d = parser.Depth;
160 while(parser.Parse() && parser.Depth >= d) {
161 if(parser.Depth == d+1) {
162 if(parser.Type != Parser.LispType.SYMBOL)
163 throw new Exception("expected SYMBOL at supertux-tiles---tiles level, but got \"" + parser.StringValue + "\"");
164 string symbol = parser.SymbolValue;
168 blockWidth = parser.IntegerValue;
171 blockHeight = parser.IntegerValue;
174 Parser.ParseIntList(parser, ids);
177 Parser.ParseIntList(parser, attributes);
180 Parser.ParseIntList(parser, datas);
183 animFps = parser.IntegerValue;
186 int subDepth = parser.Depth;
187 while(parser.Depth >= subDepth) {
188 imageNames.Add(parser.StringValue);
189 ImageRegion region = new ImageRegion();
190 region.ImageFile = parser.StringValue;
196 Console.WriteLine("Unknown tiles element " + symbol);
201 if(ids.Count != blockWidth * blockHeight)
202 throw new ApplicationException("Must have width*height ids in tiles block, but found " + ids.Count.ToString());
203 if((attributes.Count != blockWidth * blockHeight) && attributes.Count > 0) //missing atributes == all-are-0-attributes
204 throw new ApplicationException("Must have width*height attributes in tiles block");
205 if((datas.Count != blockWidth * blockHeight) && datas.Count > 0) //missing DATAs == all-are-0-DATAs
206 throw new ApplicationException("Must have width*height DATAs in tiles block");
209 for(int y = 0; y < blockHeight; ++y) {
210 for(int x = 0; x < blockWidth; ++x) {
212 Tile tile = new Tile();
214 tile.Images = new ArrayList(images);
216 tile.Attributes = (attributes.Count > 0)?attributes[id]:0; //missing atributes == all-are-0-attributes
217 tile.Data = (datas.Count > 0)?datas[id]:0; //missing DATAs == all-are-0-DATAs
219 while(Tiles.Count <= tile.ID)
222 Tiles[tile.ID] = tile;
230 private void SkipList(Lisp.Parser parser)
232 int d = parser.Depth;
233 while(parser.Parse() && parser.Depth >= d)