Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
MapBounds.cs
Go to the documentation of this file.
1using System;
2using Newtonsoft.Json;
3using UnityEngine;
4
5public class MapBounds : EClass
6{
7 [JsonProperty]
8 public int x;
9
10 [JsonProperty]
11 public int z;
12
13 [JsonProperty]
14 public int maxX;
15
16 [JsonProperty]
17 public int maxZ;
18
19 [JsonProperty]
20 public int Size;
21
22 public int CenterX => (x + maxX) / 2;
23
24 public int CenterZ => (z + maxZ) / 2;
25
26 public int Width => maxX - x + 1;
27
28 public int Height => maxZ - z + 1;
29
30 public void SetBounds(int _x, int _z, int _maxX, int _maxZ)
31 {
32 x = _x;
33 z = _z;
34 maxX = _maxX;
35 maxZ = _maxZ;
36 }
37
38 public bool Contains(int dx, int dz)
39 {
40 if (dx >= x && dz >= z && dx <= maxX)
41 {
42 return dz <= maxZ;
43 }
44 return false;
45 }
46
47 public bool Contains(Point p)
48 {
49 return Contains(p.x, p.z);
50 }
51
53 {
54 return new Point(CenterX, CenterZ);
55 }
56
58 {
59 return new Point(x + EClass.rnd(maxX - x), maxZ);
60 }
61
63 {
64 return new Point(maxX, z + EClass.rnd(maxZ - z));
65 }
66
68 {
69 return new Point(x + EClass.rnd(maxX - x), z);
70 }
71
73 {
74 return new Point(x, z + EClass.rnd(maxZ - z));
75 }
76
77 public Point GetTopPos(float rate = -1f)
78 {
79 return GetSpawnPos(x, maxZ, maxX, maxZ);
80 }
81
82 public Point GetRightPos(float rate = -1f)
83 {
84 return GetSpawnPos(maxX, z, maxX, maxZ);
85 }
86
87 public Point GetBottomPos(float rate = -1f)
88 {
89 return GetSpawnPos(x, z, maxX, z);
90 }
91
92 public Point GetLeftPos(float rate = -1f)
93 {
94 return GetSpawnPos(x, z, x, maxZ);
95 }
96
98 {
99 return new Point(x + EClass.rnd(Width), z + EClass.rnd(Height));
100 }
101
102 public Point GetSpawnPos(int x, int z, int maxX, int maxZ)
103 {
104 Point point = new Point();
105 for (int i = z; i < maxZ + 1; i++)
106 {
107 for (int j = x; j < maxX + 1; j++)
108 {
109 point.Set(j, i);
110 foreach (Thing thing in point.Things)
111 {
112 if (thing.trait is TraitRoadSign)
113 {
114 if (thing.dir == 0)
115 {
116 point.z--;
117 }
118 else if (thing.dir == 1)
119 {
120 point.x++;
121 }
122 else if (thing.dir == 2)
123 {
124 point.z++;
125 }
126 else
127 {
128 point.x--;
129 }
130 return point;
131 }
132 }
133 }
134 }
135 return new Point(x + (maxX - x) / 2, z + (maxZ - z) / 2);
136 }
137
138 public bool CanExpand(int a)
139 {
140 if (x - a < 1 && z - a < 1 && maxX + a >= EClass._map.Size - 1)
141 {
142 return maxZ + a < EClass._map.Size - 1;
143 }
144 return true;
145 }
146
147 public void Expand(int a)
148 {
149 x -= a;
150 z -= a;
151 maxX += a;
152 maxZ += a;
153 if (x < 1)
154 {
155 x = 1;
156 }
157 if (z < 1)
158 {
159 z = 1;
160 }
161 if (maxX >= EClass._map.Size - 1)
162 {
163 maxX = EClass._map.Size - 2;
164 }
165 if (maxZ >= EClass._map.Size - 1)
166 {
167 maxZ = EClass._map.Size - 2;
168 }
169 }
170
171 public Point GetSurface(int x, int z, bool walkable = true)
172 {
173 Point point = new Point(x, z);
174 point.Clamp();
175 if (!walkable || !point.cell.blocked)
176 {
177 return point;
178 }
179 return Point.Invalid;
180 }
181
182 public Point GetRandomSurface(int x, int z, int radius, bool walkable = true, bool allowWater = false)
183 {
184 for (int i = 0; i < radius * radius * 2; i++)
185 {
186 Point surface = GetSurface(x + EClass.rnd(radius) - EClass.rnd(radius), z + EClass.rnd(radius) - EClass.rnd(radius), walkable);
187 if (surface.IsValid && (allowWater || !surface.IsWater))
188 {
189 return surface;
190 }
191 }
192 return EClass._map.GetCenterPos();
193 }
194
195 public Point GetRandomSurface(bool centered = false, bool walkable = true, bool allowWater = false)
196 {
197 for (int i = 0; i < 10000; i++)
198 {
199 Point surface = GetSurface(centered ? (CenterX + EClass.rnd(Width / 4) - EClass.rnd(Width / 4)) : (x + EClass.rnd(Width)), centered ? (CenterZ + EClass.rnd(Height / 4) - EClass.rnd(Height / 4)) : (z + EClass.rnd(Height)), walkable);
200 if (surface.IsValid && (allowWater || !surface.IsWater))
201 {
202 return surface;
203 }
204 }
205 return GetSurface(CenterX, CenterZ, walkable: false);
206 }
207
209 {
210 for (int i = 0; i < 10000; i++)
211 {
212 Point randomPoint = EClass._map.GetRandomPoint();
213 if (!randomPoint.cell.blocked && randomPoint.cell.room == null && randomPoint.cell.light <= 0 && randomPoint.IsValid)
214 {
215 return randomPoint;
216 }
217 }
218 return GetCenterPos().GetNearestPoint();
219 }
220
221 public Point GetRandomEdge(int r = 3)
222 {
223 int num = 0;
224 int num2 = 0;
225 for (int i = 0; i < 10000; i++)
226 {
227 if (EClass.rnd(2) == 0)
228 {
229 num = ((EClass.rnd(2) == 0) ? (x + EClass.rnd(r)) : (maxX - EClass.rnd(r)));
230 num2 = z + EClass.rnd(Height);
231 }
232 else
233 {
234 num2 = ((EClass.rnd(2) == 0) ? (z + EClass.rnd(r)) : (maxZ - EClass.rnd(r)));
235 num = x + EClass.rnd(Width);
236 }
237 Point surface = GetSurface(num, num2, walkable: false);
238 if (surface.IsValid)
239 {
240 return surface;
241 }
242 }
243 return GetSurface(Size / 2, Size / 2, walkable: false);
244 }
245
246 public Point GetRandomSpace(int width, int height, int tries = 100)
247 {
248 Point point = new Point();
249 Point point2 = new Point();
250 for (int i = 0; i < tries; i++)
251 {
252 bool flag = true;
253 point2.Set(x + EClass.rnd(maxX - x), z + EClass.rnd(maxZ - z));
254 for (int j = 0; j < height; j++)
255 {
256 for (int k = 0; k < width; k++)
257 {
258 point.Set(point2.x + k, point2.z + j);
259 if (point.x > maxX || point.z > maxZ || point.IsBlocked || point.cell.HasZoneStairs())
260 {
261 flag = false;
262 break;
263 }
264 }
265 if (!flag)
266 {
267 break;
268 }
269 }
270 if (flag)
271 {
272 return point2;
273 }
274 }
275 Debug.Log("valid space not found:" + width + "/" + height);
276 return null;
277 }
278
279 public void ForeachCell(Action<Cell> action)
280 {
281 Cell[,] cells = EClass._map.cells;
282 for (int i = x; i <= maxX; i++)
283 {
284 for (int j = z; j <= maxZ; j++)
285 {
286 action(cells[i, j]);
287 }
288 }
289 }
290
291 public void ForeachPoint(Action<Point> action)
292 {
293 Point point = new Point();
294 for (int i = x; i <= maxX; i++)
295 {
296 for (int j = z; j <= maxZ; j++)
297 {
298 action(point.Set(i, j));
299 }
300 }
301 }
302
303 public void ForeachXYZ(Action<int, int> action)
304 {
305 for (int i = x; i <= maxX; i++)
306 {
307 for (int j = z; j <= maxZ; j++)
308 {
309 action(i, j);
310 }
311 }
312 }
313}
Trait trait
Definition: Card.cs:49
int dir
Definition: Card.cs:142
Definition: Cell.cs:7
Room room
Definition: Cell.cs:102
byte light
Definition: Cell.cs:60
bool HasZoneStairs(bool includeLocked=true)
Definition: Cell.cs:1713
Definition: EClass.cs:5
static int rnd(int a)
Definition: EClass.cs:50
static Map _map
Definition: EClass.cs:18
Point GetRandomEdge(int r=3)
Definition: MapBounds.cs:221
Point GetLeftPos(float rate=-1f)
Definition: MapBounds.cs:92
int CenterZ
Definition: MapBounds.cs:24
int Size
Definition: MapBounds.cs:20
int maxZ
Definition: MapBounds.cs:17
void SetBounds(int _x, int _z, int _maxX, int _maxZ)
Definition: MapBounds.cs:30
Point GetSurface(int x, int z, bool walkable=true)
Definition: MapBounds.cs:171
int maxX
Definition: MapBounds.cs:14
Point GetRandomSpawnPos()
Definition: MapBounds.cs:208
void ForeachCell(Action< Cell > action)
Definition: MapBounds.cs:279
bool CanExpand(int a)
Definition: MapBounds.cs:138
void ForeachPoint(Action< Point > action)
Definition: MapBounds.cs:291
Point GetBottomPos(float rate=-1f)
Definition: MapBounds.cs:87
Point GetRandomLeftPos()
Definition: MapBounds.cs:72
void Expand(int a)
Definition: MapBounds.cs:147
Point GetRandomSurface(int x, int z, int radius, bool walkable=true, bool allowWater=false)
Definition: MapBounds.cs:182
Point GetRandomBottomPos()
Definition: MapBounds.cs:67
bool Contains(Point p)
Definition: MapBounds.cs:47
int Width
Definition: MapBounds.cs:26
int x
Definition: MapBounds.cs:8
Point GetRandomSurface(bool centered=false, bool walkable=true, bool allowWater=false)
Definition: MapBounds.cs:195
Point GetTopPos(float rate=-1f)
Definition: MapBounds.cs:77
Point GetRandomSpace(int width, int height, int tries=100)
Definition: MapBounds.cs:246
void ForeachXYZ(Action< int, int > action)
Definition: MapBounds.cs:303
Point GetCenterPos()
Definition: MapBounds.cs:52
Point GetRandomTopPos()
Definition: MapBounds.cs:57
int Height
Definition: MapBounds.cs:28
int CenterX
Definition: MapBounds.cs:22
Point GetRandomRightPos()
Definition: MapBounds.cs:62
bool Contains(int dx, int dz)
Definition: MapBounds.cs:38
Point GetRightPos(float rate=-1f)
Definition: MapBounds.cs:82
Point GetRandomPoint()
Definition: MapBounds.cs:97
Point GetSpawnPos(int x, int z, int maxX, int maxZ)
Definition: MapBounds.cs:102
Point GetRandomPoint(Point center, int radius, int tries=100, bool mustBeWalkable=true, bool requireLos=true)
Definition: Map.cs:2193
Cell[,] cells
Definition: Map.cs:85
Definition: Point.cs:9
static Point Invalid
Definition: Point.cs:28
Point Set(int _x, int _z)
Definition: Point.cs:479
bool IsBlocked
Definition: Point.cs:339
bool IsWater
Definition: Point.cs:129
List< Thing > Things
Definition: Point.cs:314
int x
Definition: Point.cs:36
int z
Definition: Point.cs:39
bool IsValid
Definition: Point.cs:88
Point Clamp(bool useBounds=false)
Definition: Point.cs:972
Cell cell
Definition: Point.cs:51
Point GetNearestPoint(bool allowBlock=false, bool allowChara=true, bool allowInstalled=true, bool ignoreCenter=false)
Definition: Point.cs:595
Definition: Thing.cs:8