This repository was archived by the owner on Feb 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMap.cs
More file actions
392 lines (358 loc) · 10.4 KB
/
Map.cs
File metadata and controls
392 lines (358 loc) · 10.4 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* This file is part of Project Hybrasyl.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* without ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the Affero General Public License
* for more details.
*
* You should have received a copy of the Affero General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* (C) 2015 Kyle Speck (kojasou@hybrasyl.com)
*
* Authors: Kyle Speck <kojasou@hybrasyl.com>
*
*/
using Capricorn.IO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
namespace HybrasylEditor
{
public class Map
{
private static Dictionary<int, Bitmap> cachedFloor;
private static Dictionary<int, Bitmap> cachedWalls;
private int id;
private string name;
private int width;
private int height;
private int music;
private MapFlags flags;
private MapTile[] tiles;
private Dictionary<Point, Warp> warps;
private Dictionary<Point, Npc> npcs;
private Dictionary<Point, string> signs;
private List<FixedSpawn> spawnsFixed;
private List<ZoneSpawn> spawnsZoned;
public int Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Width
{
get { return width; }
set { width = value; }
}
public int Height
{
get { return height; }
set { height = value; }
}
public int Music
{
get { return music; }
set { music = value; }
}
public MapFlags Flags
{
get { return flags; }
set { flags = value; }
}
[Browsable(false)]
public MapTile[] Tiles
{
get { return tiles; }
}
[Browsable(true)]
public List<FixedSpawn> SpawnsFixed
{
get { return spawnsFixed; }
}
[Browsable(true)]
public List<ZoneSpawn> SpawnsZone
{
get { return spawnsZoned; }
}
[Browsable(false)]
public Dictionary<Point, Warp> Warps
{
get { return warps; }
}
[Browsable(false)]
public Dictionary<Point, Npc> Npcs
{
get { return npcs; }
}
[Browsable(false)]
public Dictionary<Point, string> Signs
{
get { return signs; }
}
public MapTile this[int x, int y]
{
get { return tiles[y * width + x]; }
set { tiles[y * width + x] = value; }
}
public MapTile this[Point point]
{
get { return tiles[point.Y * width + point.X]; }
set { tiles[point.Y * width + point.X] = value; }
}
static Map()
{
cachedFloor = new Dictionary<int, Bitmap>();
cachedWalls = new Dictionary<int, Bitmap>();
}
public Map()
{
var random = new Random();
this.tiles = new MapTile[0];
this.warps = new Dictionary<Point, Warp>();
this.npcs = new Dictionary<Point, Npc>();
this.signs = new Dictionary<Point, string>();
this.spawnsFixed = new List<FixedSpawn>();
this.spawnsZoned = new List<ZoneSpawn>();
}
public Map(List<FixedSpawn> fixedSpawns, List<ZoneSpawn> zoneSpawns)
: this()
{
if (fixedSpawns != null)
this.spawnsFixed = fixedSpawns;
if (zoneSpawns != null)
this.spawnsZoned = zoneSpawns;
}
public void Add(Npc npc)
{
this.npcs.Add(npc.Point, npc);
}
public void Add(Warp warp)
{
this.warps.Add(warp.SourcePoint, warp);
}
public void SetData(byte[] buffer)
{
if (buffer.Length % 6 != 0) return;
tiles = new MapTile[buffer.Length / 6];
using (var stream = new MemoryStream(buffer))
{
using (var reader = new BinaryReader(stream))
{
for (int i = 0; i < tiles.Length; ++i)
{
int floor = reader.ReadUInt16();
int leftWall = reader.ReadUInt16();
int rightWall = reader.ReadUInt16();
tiles[i] = new MapTile(floor, leftWall, rightWall);
}
}
}
}
}
public struct MapTile
{
private int floor;
private int leftWall;
private int rightWall;
private static byte[] sotp;
public int Floor
{
get { return floor; }
}
public int LeftWall
{
get { return leftWall; }
}
public int RightWall
{
get { return rightWall; }
}
static MapTile()
{
sotp = DATArchive.Ia.ExtractFile("sotp.dat");
}
public MapTile(int floor, int leftWall, int rightWall)
{
this.floor = floor;
this.leftWall = leftWall;
this.rightWall = rightWall;
}
public bool IsObstacle
{
get
{
return (leftWall > 0 && (sotp[leftWall - 1] & 0x0F) == 0x0F) || (rightWall > 0 && (sotp[rightWall - 1] & 0x0F) == 0x0F);
}
}
public bool LeftBlends
{
get
{
return leftWall > 0 && (sotp[leftWall - 1] & 0x80) == 0x80;
}
}
public bool RightBlends
{
get
{
return rightWall > 0 && (sotp[rightWall - 1] & 0x80) == 0x80;
}
}
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public class FixedSpawn
{
public const int DEFAULT_SPEED = 1;
public const bool DEFAULT_HOSTILE = true;
public const string DEFAULT_STRATEGY = "random";
public string name { get; set; }
public int checkrate { get; set; }
public int min { get; set; }
public int max { get; set; }
public int every { get; set; }
public float percentage { get; set; }
// properties with defaults
public int _speed = DEFAULT_SPEED;
public int speed
{
get { return _speed; }
set { _speed = value; }
}
public bool _hostile = DEFAULT_HOSTILE;
public bool hostile
{
get { return _hostile; }
set { _hostile = value; }
}
public string _strategy = DEFAULT_STRATEGY;
public string strategy
{
get { return _strategy; }
set { _strategy = value; }
}
public FixedSpawn() { }
public override string ToString()
{
return string.Format("{{name={0}, checkRate={1}}}", name, checkrate);
}
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public class ZoneSpawn
{
public const int DEFAULT_SPEED = 1;
public const bool DEFAULT_HOSTILE = true;
public const string DEFAULT_STRATEGY = "random";
public string name { get; set; }
public int checkrate { get; set; }
public Point start_point { get; set; }
public Point end_point { get; set; }
public int min { get; set; }
public int max { get; set; }
public int every { get; set; }
public float percentage { get; set; }
// properties with defaults
public int _speed = DEFAULT_SPEED;
public int speed
{
get { return _speed; }
set { _speed = value; }
}
public bool _hostile = DEFAULT_HOSTILE;
public bool hostile
{
get { return _hostile; }
set { _hostile = value; }
}
public string _strategy = DEFAULT_STRATEGY;
public string strategy
{
get { return _strategy; }
set { _strategy = value; }
}
public ZoneSpawn() { }
public override string ToString()
{
return string.Format("{{name={0}, start={{{1},{2}}}, end={{{3},{4}}}, checkRate={5}}}", name, start_point.X, start_point.Y, end_point.X, end_point.Y, checkrate);
}
}
public struct Warp
{
private Point sourcePoint;
private Location target;
private int minimumLevel;
private int maximumLevel;
private int minimumAbility;
private bool monsterCanUse;
public Point SourcePoint
{
get { return sourcePoint; }
set { sourcePoint = value; }
}
public Location Target
{
get { return target; }
set { target = value; }
}
public int MinimumLevel
{
get { return minimumLevel; }
set { minimumLevel = value; }
}
public int MaximumLevel
{
get { return maximumLevel; }
set { maximumLevel = value; }
}
public int MinimumAbility
{
get { return minimumAbility; }
set { minimumAbility = value; }
}
public bool MonsterCanUse
{
get { return monsterCanUse; }
set { monsterCanUse = value; }
}
}
public struct Location
{
private int mapId;
private Point point;
public int MapId
{
get { return mapId; }
set { mapId = value; }
}
public Point Point
{
get { return point; }
set { point = value; }
}
public override string ToString()
{
return string.Format("{{MapId={0}, X={1}, Y={2}}}", mapId, point.X, point.Y);
}
}
[Flags]
public enum MapFlags
{
Snow = 1,
Rain = 2,
Darkness = Snow | Rain,
NoMap = 64,
SnowTileset = 128
}
}