forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockMultilayer.cs
More file actions
292 lines (219 loc) · 8.25 KB
/
Copy pathBlockMultilayer.cs
File metadata and controls
292 lines (219 loc) · 8.25 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
using System;
using System.IO;
namespace L2dotNET.GeoEngine.Geodata
{
public class BlockMultilayer : ABlock
{
private const int MaxLayers = byte.MaxValue;
private static MemoryStream _temp;
protected byte[] Buffer;
public static void Initialize()
{
_temp = new MemoryStream(GeoStructure.BlockCells * MaxLayers * 3);
if (BitConverter.IsLittleEndian)
return;
byte[] superTemp = _temp.ToArray();
Array.Reverse(superTemp);
_temp.Write(superTemp, 0, _temp.ToArray().Length);
}
public static void Release()
{
_temp = null;
}
protected BlockMultilayer()
{
Buffer = null;
}
public BlockMultilayer(MemoryStream ms)
{
for (int cell = 0; cell < GeoStructure.BlockCells; cell++)
{
byte layers = (byte)ms.ReadByte();
_temp.Write(ms.ToArray(), 1, layers);
// loop over layers
for (byte layer = 0; layer < layers; layer++)
{
// add nswe
_temp.WriteByte(ms.ToArray()[layer]);
// add height
_temp.WriteByte(ms.ToArray()[layer]);
}
}
Array.Copy(_temp.ToArray(), Buffer, _temp.Position);
_temp.SetLength(0);
}
public override short GetHeight(int index)
{
return (short)((Buffer[index + 1] & 0x00FF) | (Buffer[index + 2] << 8));
}
public override short GetHeightAbove(int geoX, int geoY, int worldZ)
{
int index = 0;
for (int i = 0; i < (((geoX % GeoStructure.BlockCellsX) * GeoStructure.BlockCellsY) + (geoY % GeoStructure.BlockCellsY)); i++)
index += (Buffer[index] * 3) + 1;
byte layers = Buffer[index++];
index += (layers - 1) * 3;
while (layers-- > 0)
{
int height = (Buffer[index + 1] & 0x00FF) | (Buffer[index + 2] << 8);
if (height > worldZ)
return (short)height;
index -= 3;
}
return short.MinValue;
}
public override short GetHeightBelow(int geoX, int geoY, int worldZ)
{
int index = 0;
for (int i = 0; i < (((geoX % GeoStructure.BlockCellsX) * GeoStructure.BlockCellsY) + (geoY % GeoStructure.BlockCellsY)); i++)
index += (Buffer[index] * 3) + 1;
byte layers = Buffer[index++];
while (layers-- > 0)
{
int height = (Buffer[index + 1] & 0x00FF) | (Buffer[index + 2] << 8);
if (height < worldZ)
return (short)height;
index += 3;
}
return short.MaxValue;
}
public override short GetHeightNearest(int geoX, int geoY, int worldZ)
{
int index = GetIndexNearest(geoX, geoY, worldZ);
return (short)((Buffer[index + 1] & 0x00FF) | (Buffer[index + 2] << 8));
}
public override short GetHeightNearestOriginal(int geoX, int geoY, int worldZ)
{
return GetHeightNearest(geoX, geoY, worldZ);
}
public override short GetHeightOriginal(int index)
{
return (short)((Buffer[index + 1] & 0x00FF) | (Buffer[index + 2] << 8));
}
public override int GetIndexAbove(int geoX, int geoY, int worldZ)
{
int index = 0;
for (int i = 0; i < (((geoX % GeoStructure.BlockCellsX) * GeoStructure.BlockCellsY) + (geoY % GeoStructure.BlockCellsY)); i++)
index += (Buffer[index] * 3) + 1;
byte layers = Buffer[index++];
index += (layers - 1) * 3;
while (layers-- > 0)
{
int height = (Buffer[index + 1] & 0x00FF) | (Buffer[index + 2] << 8);
if (height > worldZ)
return index;
index -= 3;
}
return -1;
}
public override int GetIndexAboveOriginal(int geoX, int geoY, int worldZ)
{
return GetIndexAbove(geoX, geoY, worldZ);
}
public override int GetIndexBelow(int geoX, int geoY, int worldZ)
{
int index = 0;
for (int i = 0; i < (((geoX % GeoStructure.BlockCellsX) * GeoStructure.BlockCellsY) + (geoY % GeoStructure.BlockCellsY)); i++)
index += (Buffer[index] * 3) + 1;
byte layers = Buffer[index++];
while (layers-- > 0)
{
int height = (Buffer[index + 1] & 0x00FF) | (Buffer[index + 2] << 8);
if (height < worldZ)
return index;
index += 3;
}
return -1;
}
public override int GetIndexBelowOriginal(int geoX, int geoY, int worldZ)
{
return GetIndexBelow(geoX, geoY, worldZ);
}
public override int GetIndexNearest(int geoX, int geoY, int worldZ)
{
int index = 0;
for (int i = 0; i < (((geoX % GeoStructure.BlockCellsX) * GeoStructure.BlockCellsY) + (geoY % GeoStructure.BlockCellsY)); i++)
index += (Buffer[index] * 3) + 1;
byte layers = Buffer[index++];
int limit = int.MaxValue;
while (layers-- > 0)
{
int height = (Buffer[index + 1] & 0x00FF) | (Buffer[index + 2] << 8);
int distance = Math.Abs(height - worldZ);
if (distance > limit)
break;
limit = distance;
index += 3;
}
return index - 3;
}
public override byte GetNswe(int index)
{
return Buffer[index];
}
public override byte GetNsweAbove(int geoX, int geoY, int worldZ)
{
int index = 0;
for (int i = 0; i < (((geoX % GeoStructure.BlockCellsX) * GeoStructure.BlockCellsY) + (geoY % GeoStructure.BlockCellsY)); i++)
index += (Buffer[index] * 3) + 1;
byte layers = Buffer[index++];
index += (layers - 1) * 3;
while (layers-- > 0)
{
int height = (Buffer[index + 1] & 0x00FF) | (Buffer[index + 2] << 8);
if (height > worldZ)
return Buffer[index];
index -= 3;
}
return 0;
}
public override byte GetNsweBelow(int geoX, int geoY, int worldZ)
{
int index = 0;
for (int i = 0; i < (((geoX % GeoStructure.BlockCellsX) * GeoStructure.BlockCellsY) + (geoY % GeoStructure.BlockCellsY)); i++)
index += (Buffer[index] * 3) + 1;
byte layers = Buffer[index++];
while (layers-- > 0)
{
int height = (Buffer[index + 1] & 0x00FF) | (Buffer[index + 2] << 8);
if (height < worldZ)
return Buffer[index];
index += 3;
}
return 0;
}
public override byte GetNsweNearest(int geoX, int geoY, int worldZ)
{
int index = GetIndexNearest(geoX, geoY, worldZ);
return Buffer[index];
}
public override byte GetNsweNearestOriginal(int geoX, int geoY, int worldZ)
{
return GetNsweNearest(geoX, geoY, worldZ);
}
public override byte GetNsweOriginal(int index)
{
return Buffer[index];
}
public override bool HasGeoPos()
{
return true;
}
public override void SaveBlock(BufferedStream stream)
{
stream.WriteByte(GeoStructure.TypeMultilayerL2D);
int index = 0;
for (int i = 0; i < GeoStructure.BlockCells; i++)
{
byte layers = Buffer[index++];
stream.WriteByte(layers);
stream.Write(Buffer, index, layers * 3);
index += layers * 3;
}
}
public override void SetNswe(int index, byte nswe)
{
Buffer[index] = nswe;
}
}
}