-
Notifications
You must be signed in to change notification settings - Fork 0
/
SupercowLevel.cs
360 lines (327 loc) · 13.8 KB
/
SupercowLevel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using System.Text;
using System.Drawing;
using System.Globalization;
using System.Collections.Generic;
using System.IO;
using System;
namespace Nevosoft.Supercow
{
/// <summary>
/// Сlass of a typical object in <see cref="Level"/>
/// </summary>
public struct LevelObject
{
/// <summary>
/// Name of the gameobject whose type this object is
/// </summary>
public string Name;
/// <summary>
/// Position and size of the object
/// </summary>
public RectangleF Rectangle;
/// <summary>
/// Layer on which the object is to be displayed
/// </summary>
public int DrawLayer;
/// <summary>
/// Angle of rotation of the object
/// </summary>
public float Rotation;
/// <summary>
/// The object's endpoint, in case the object can use it (platforms, enemies, triangles)
/// </summary>
public PointF EndPosition;
/// <summary>
/// Whether the view of the object is inverted or not
/// </summary>
public bool Inverted;
/// <summary>
/// Extraparameter of the object
/// <list type="bullet">
/// <item>
/// <term><see cref="string"/></term>
/// <description>If the object is tablet, it looks for a line with that name in the tips.txt file of the game
/// <br/>Example:<code>tip_1_1_1</code></description>
/// </item>
/// <item>
/// <term><see cref="int"/></term>
/// <description>If the object is an enemy, sets the size (maximum 3)<br/>Example:<code>2</code></description>
/// </item>
/// <item>
/// <term><see cref="decimal"/></term>
/// <description>If the object is a jewel or power up or even guillotine, sets the respawn speed in seconds<br/>Example:<code>0.5</code></description>
/// </item>
/// <item>
/// <term><see cref="decimal"/></term>
/// <description>If the object is a moving platform, sets the speed
/// <br/>(not in seconds, in who the fuck knows what)
/// <br/>It can also have a <see cref="string"/> meaning sound playback:
/// <list type="bullet">
/// <item>
/// <term>s</term>
/// <description>Platform floats and plays a corresponding sound</description>
/// </item>
/// <item>
/// <term>m</term>
/// <description>Platform floats silently</description>
/// </item>
/// </list><br/>Example:<code>69.420s</code></description>
/// </item>
/// </list>
/// </summary>
public string ExtraParameter;
}
/// <summary>
/// Level class from the Supercow game. Has the ".lev" extension
/// </summary>
public class Level
{
/// <summary>
/// Level name. Not used in the game, but exists in file, so why not
/// </summary>
public string Name { get; set; } = "";
/// <summary>
/// Number of the background to be used in the level. Depends on the set backgrounds in the game
/// </summary>
public int Background { get; set; } = 0;
/// <summary>
/// Number of the task to be used in the level
/// <list type="bullet">
/// <item>
/// <term>0</term>
/// <description>Pass to exit</description>
/// </item>
/// <item>
/// <term>1</term>
/// <description>Find keys</description>
/// </item>
/// <item>
/// <term>2</term>
/// <description>Eliminate enemies</description>
/// </item>
/// <item>
/// <term>3</term>
/// <description>Find gems</description>
/// </item>
/// <item>
/// <term>4</term>
/// <description>Clear garbage</description>
/// </item>
/// <item>
/// <term>5</term>
/// <description>Eliminate boss</description>
/// </item>
/// </list>
/// </summary>
public int Task { get; set; } = 0;
/// <summary>
/// Number of the music to be used in the level. Depends on the set music in the game
/// </summary>
public int Music { get; set; } = 0;
/// <summary>
/// List of <see cref="LevelObject"/>s on the level
/// </summary>
public List<LevelObject> Objects { get; set; } = new List<LevelObject>();
/// <summary>
/// Array of grounds located on the level<br/>First number is the array layer (maximum 5)
/// <br/>The second and third numbers are the X and Y positions on the array
/// </summary>
/// <returns>
/// Ground number at the given coordinates
/// </returns>
public int[][,] Grounds { get; set; } = new int[6][,]
{ new int[256,64], new int[256,64], new int[256,64],
new int[256,64], new int[256,64], new int[256,64] };
/// <summary>
/// Create object of class
/// </summary>
public Level() { }
/// <summary>
/// Open level from file
/// </summary>
public Level(string filename) => Load(filename);
/// <summary>
/// Open level from <see cref="Stream"/>
/// </summary>
public Level(Stream stream) => Load(stream);
/// <summary>
/// Create object of class from file
/// </summary>
public static Level FromFile(string filename) => new Level(filename);
/// <summary>
/// Create object of class from <see cref="Stream"/>
/// </summary>
public static Level FromStream(Stream stream) => new Level(stream);
/// <summary>
/// Save level to file
/// </summary>
public void Save(string filename)
{
using (FileStream fs = new FileStream(filename,
FileMode.Create, FileAccess.Write, FileShare.None))
Save(fs);
}
private void Load(string filename)
{
if (!File.Exists(filename))
throw new FileNotFoundException("File \"" + filename + "\" not found!");
using (FileStream FS = new FileStream(filename, FileMode.Open))
Load(FS);
}
private void Load(Stream stream)
{
if (stream == null)
throw new ArgumentNullException(null);
if (!(stream.CanRead && stream.CanSeek))
throw new FileLoadException("Stream reading or seeking is not avaiable!");
stream.Seek(0, SeekOrigin.Begin);
using (StreamReader sr = new StreamReader(stream))
{
string content = sr.ReadToEnd();
Name = ParseString(content, "levelname=");
Objects.Capacity = ParseInt(content, "numlevelobjs=");
Background = ParseInt(content, "curlevelback=");
Task = ParseInt(content, "curleveltask=");
Music = ParseInt(content, "curlevelmusic=");
var index = 0;
for (var i = 0; i < Objects.Capacity; i++)
{
index = content.IndexOf("<obj", index);
if (index == -1) break;
var obj = new LevelObject();
var endIndex = content.IndexOf("</obj", index);
obj.Name = ParseString(content, "name=", index);
var rect = new float[4];
ParseFloatArray(content, "rect=", rect, index);
obj.Rectangle = RectangleF.FromLTRB(rect[0], rect[1], rect[2], rect[3]);
obj.DrawLayer = ParseInt(content, "drawlayer=", index);
obj.Rotation = ParseFloat(content, "rotation=", index);
var endPos = new float[2];
ParseFloatArray(content, "endpos=", endPos, index);
obj.EndPosition = new PointF(endPos[0], endPos[1]);
obj.Inverted = ParseInt(content, "invertobj=", index) != 0;
var extraparamIndex = content.IndexOf("extraparam=", index);
if (extraparamIndex != -1 && extraparamIndex < endIndex)
obj.ExtraParameter = ParseString(content, "extraparam=", index);
Objects.Add(obj);
index = endIndex;
}
for (var j = 0; j < 6; j++)
{
var prefix = $"groundlayer{j}=";
var layerIndex = content.IndexOf(prefix);
if (layerIndex == -1) break;
layerIndex += prefix.Length;
layerIndex += 2;
for (var k = 0; k < 64; k++)
{
for (var m = 0; m < 256; m++)
{
Grounds[j][m, k] = content[layerIndex] - '0';
layerIndex++;
}
layerIndex += 2;
}
}
}
}
static string ParseString(string content, string prefix, int startIndex = 0)
{
var res = content.IndexOf(prefix, startIndex);
if (res == -1) return "";
var src = res + prefix.Length;
var end = content.IndexOf("\r", src);
return content.Substring(src, end - src);
}
static int ParseInt(string content, string prefix, int startIndex = 0)
{
var value = ParseString(content, prefix, startIndex);
if (value != "") return (int)double.Parse(value);
return 0;
}
static float ParseFloat(string content, string prefix, int startIndex = 0)
{
var value = ParseString(content, prefix, startIndex);
if (value != "") return (float)double.Parse(value, CultureInfo.InvariantCulture);
return 0;
}
static void ParseFloatArray(string content, string prefix, float[] arr, int startIndex = 0)
{
var value = ParseString(content, prefix, startIndex);
if (value == "") return;
var endIndex = value.IndexOf(")");
if (endIndex == -1) endIndex = value.IndexOf("}");
if (endIndex == -1) return;
var index = 1;
var i = 0;
while (true)
{
var currEndIndex = value.IndexOf(",", index);
if (currEndIndex == -1) currEndIndex = value.IndexOf("}", index);
if (currEndIndex == -1) currEndIndex = value.IndexOf(")", index);
if (currEndIndex == -1)
break;
arr[i] = (float)double.Parse(value.Substring(index, currEndIndex - index), CultureInfo.InvariantCulture);
index = currEndIndex + 1;
if (index >= value.Length) break;
i++;
}
}
/// <summary>
/// Save level to <see cref="Stream"/>
/// </summary>
public void Save(Stream stream)
{
if (stream == null)
throw new ArgumentNullException(null);
if (!(stream.CanWrite && stream.CanSeek))
throw new FileLoadException("Stream writing or seeking is not avaiable!");
stream.Seek(0, SeekOrigin.Begin);
using (StreamWriter sw = new StreamWriter(stream, Encoding.UTF8))
{
sw.Write($"levelname={Name}\r\n");
sw.Write($"numlevelobjs={Objects.Count}\r\n\r\n");
sw.Write($"curlevelback={Background}\r\n\r\n");
sw.Write($"curleveltask={Task}\r\n\r\n");
sw.Write($"curlevelmusic={Music}\r\n\r\n");
for (int i = 0; i < Objects.Count; i++)
{
var obj = Objects[i];
sw.Write($"<obj{i}/>\r\n");
sw.Write($" name={obj.Name}\r\n");
var r = obj.Rectangle;
sw.Write($" rect={{{DecToStr(r.Left, "0.00")},{DecToStr(r.Top, "0.00")}," +
$"{DecToStr(r.Right, "0.00")},{DecToStr(r.Bottom, "0.00")}}}\r\n");
sw.Write($" drawlayer={obj.DrawLayer}\r\n");
sw.Write($" rotation={DecToStr(obj.Rotation, "0.000")}\r\n");
var ep = obj.EndPosition;
sw.Write($" endpos={{{DecToStr(ep.X, "0.00")},{DecToStr(ep.Y, "0.00")}}}\r\n");
sw.Write($" invertobj={(obj.Inverted ? 1 : 0)}\r\n");
if (obj.ExtraParameter != "" && obj.ExtraParameter != null)
sw.Write($" extraparam={obj.ExtraParameter}\r\n\r\n");
sw.Write($"</obj{i}>\r\n\r\n\r\n");
}
sw.Write($"\r\n\r\n");
for (var j = 0; j < 6; j++)
{
sw.Write($"groundlayer{j}=\r\n");
for (var k = 0; k < 64; k++)
{
for (var m = 0; m < 256; m++)
{
sw.Write(Grounds[j][m, k]);
}
sw.Write($"\r\n");
}
sw.Write($"\r\n");
}
sw.Write($"\r\n\r\n");
}
}
private static string DecToStr(object obj, string format)
{
if (obj is float dec) return dec.ToString(format, CultureInfo.InvariantCulture);
return "";
}
}
}