Elin Decompiled Documentation EA 23.351 Nightly
Loading...
Searching...
No Matches
PartialMap.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using System.IO;
3using Ionic.Zip;
4using Newtonsoft.Json;
5using SFB;
6using UnityEngine;
7
8public class PartialMap : EClass
9{
10 public enum ApplyMode
11 {
12 Render,
13 HitTest,
14 Apply,
16 }
17
18 public class Result
19 {
20 public bool hasLight;
21
22 public List<int> ruined = new List<int>();
23 }
24
25 public static bool relative;
26
27 [JsonProperty]
28 public string name;
29
30 [JsonProperty]
31 public int offsetX;
32
33 [JsonProperty]
34 public int offsetZ;
35
36 [JsonProperty]
37 public int w;
38
39 [JsonProperty]
40 public int h;
41
42 [JsonProperty]
43 public int dir;
44
45 [JsonProperty]
46 public int baseHeight;
47
48 [JsonProperty]
49 public int baseHeightWater;
50
51 [JsonProperty]
52 public int value;
53
54 [JsonProperty]
55 public bool allowRotate;
56
57 [JsonProperty]
58 public bool ignoreBlock;
59
60 public int localOffsetX;
61
62 public int localOffsetZ;
63
64 public int destX;
65
66 public int destZ;
67
68 public string path;
69
70 public bool editMode;
71
72 public bool procedural;
73
74 public float ruinChance;
75
76 public Map map;
77
79
80 public List<Cell> cellsMarked = new List<Cell>();
81
82 public HashSet<int> validPoints = new HashSet<int>();
83
84 public Result result = new Result();
85
86 public static string PathTemp => CorePath.MapPieceSave + "_temp.mp";
87
88 public bool altDirPosition => true;
89
90 public string ID => new FileInfo(path).Name.Replace(".mp", "");
91
92 public void Rotate()
93 {
94 dir++;
95 if (dir > 3)
96 {
97 dir = 0;
98 }
99 SE.Rotate();
101 }
102
103 public void ClearMarkedCells()
104 {
105 foreach (Cell item in cellsMarked)
106 {
107 item.skipRender = false;
108 }
109 cellsMarked.Clear();
110 }
111
112 public void Apply(Point _point, ApplyMode mode)
113 {
114 byte height = _point.cell.height;
115 int num = (_point.cell.IsFloorWater ? baseHeightWater : baseHeight);
116 Point point = new Point(_point);
117 point.x += localOffsetX;
118 point.z += localOffsetZ;
119 if (altDirPosition)
120 {
121 switch (dir)
122 {
123 case 1:
124 point.z += w - 1;
125 break;
126 case 2:
127 point.x += w - 1;
128 point.z += h - 1;
129 break;
130 case 3:
131 point.x += h - 1;
132 break;
133 }
134 }
135 destX = point.x;
136 destZ = point.z;
137 Point p = new Point();
138 if (mode == ApplyMode.Render)
139 {
141 }
142 if (mode == ApplyMode.Evaluate)
143 {
144 value = 0;
145 }
146 int num2 = 0;
147 int num3 = 0;
148 int minX = point.x;
149 int minZ = point.z;
150 int maxX = point.x + w;
151 int maxZ = point.z + h;
152 switch (dir)
153 {
154 case 1:
155 minX = point.x;
156 maxX = point.x + h;
157 minZ = point.z - w + 1;
158 maxZ = point.z + 1;
159 break;
160 case 2:
161 minX = point.x - w + 1;
162 maxX = point.x + 1;
163 minZ = point.z - h + 1;
164 maxZ = point.z + 1;
165 break;
166 case 3:
167 minX = point.x - h + 1;
168 maxX = point.x + 1;
169 minZ = point.z;
170 maxZ = point.z + w;
171 break;
172 }
176 this.result.ruined.Clear();
177 Cell c1;
179 for (int i = 0; i < w; i++)
180 {
181 for (int j = 0; j < h; j++)
182 {
183 Cell cell = map.cells[i, j];
184 p.Set(point.x + num2, point.z + num3);
185 if (EClass._map.Contains(p))
186 {
187 c1 = EClass._map.cells[p.x, p.z];
188 result = HitResult.Valid;
189 if (c1.isFloating)
190 {
191 result = HitResult.Invalid;
192 }
193 if (result == HitResult.Valid)
194 {
195 validPoints.Add(p.index);
196 }
197 if (mode == ApplyMode.Render)
198 {
199 c1.skipRender = true;
200 cellsMarked.Add(c1);
201 if (result != HitResult.Valid)
202 {
203 MeshPass guidePass = ActionMode.Copy.GetGuidePass(point);
204 int num4 = (int)result;
205 Vector3 vector = p.Position();
206 guidePass.Add(vector.x, vector.y, vector.z - 0.01f, num4, 0.3f);
207 }
208 else
209 {
210 Render(cell.sourceFloor, cell.floorDir, 0);
211 Render(cell.sourceBlock, cell.HasWall ? FixWall(cell) : cell.blockDir, 0);
212 Render(cell.sourceObj, cell.objDir, 0);
213 Render(cell.sourceBridge, cell.floorDir, (cell.bridgeHeight != 0) ? (cell.bridgeHeight - cell.height) : 0);
215 {
216 Recipe orCreate = Recipe.GetOrCreate(EClass.sources.cards.map[card.id].RecipeID);
217 if (card.x - offsetX == i && card.z - offsetZ == j)
218 {
219 if (relative)
220 {
221 orCreate?.OnRenderMarker(p, active: true, result, main: true, card.dir, height + cell.height - num);
222 }
223 else
224 {
225 orCreate?.OnRenderMarker(p, active: true, result, main: true, card.dir, cell.height);
226 }
227 }
228 }
230 }
231 }
232 if (mode == ApplyMode.Apply && result == HitResult.Valid)
233 {
234 if (Rand.Range(0f, 1f) < ruinChance)
235 {
236 this.result.ruined.Add(p.index);
237 }
238 else if (cell._floor != 77 || editMode)
239 {
240 if (cell._block == 149 && !editMode)
241 {
242 c1._block = interior.block.id;
243 c1._blockMat = interior.block.mat;
244 }
245 else if (cell._block == 150 && !editMode)
246 {
247 c1._block = exterior.block.id;
248 c1._blockMat = exterior.block.mat;
249 }
250 else
251 {
252 c1._block = cell._block;
253 c1._blockMat = cell._blockMat;
254 }
255 if (cell._floor == 2 && !editMode)
256 {
257 c1._floor = interior.floor.id;
258 c1._floorMat = interior.floor.mat;
259 }
260 else if (cell._floor == 3 && !editMode)
261 {
262 c1._floor = exterior.floor.id;
263 c1._floorMat = exterior.floor.mat;
264 }
265 else
266 {
267 c1._floor = cell._floor;
268 c1._floorMat = cell._floorMat;
269 }
270 c1._roofBlock = cell._roofBlock;
271 c1._roofBlockMat = cell._roofBlockMat;
272 c1.obj = cell.obj;
273 c1.objMat = cell.objMat;
274 c1.objVal = cell.objVal;
275 c1.decal = cell.decal;
276 c1._dirs = cell._dirs;
277 c1._bridge = cell._bridge;
278 c1._bridgeMat = cell._bridgeMat;
279 c1.bridgePillar = cell.bridgePillar;
280 c1.hidePillar = cell.hidePillar;
281 c1.toggleOcclusion = cell.toggleOcclusion;
282 c1.isModified = true;
283 if (c1.HasWall)
284 {
285 c1.blockDir = FixWall(c1);
286 }
287 if (relative || procedural)
288 {
289 c1.height = (byte)Mathf.Clamp(height + cell.height - num, 0, 255);
290 c1.bridgeHeight = (byte)Mathf.Clamp((cell.bridgeHeight != 0) ? (c1.height + (cell.bridgeHeight - cell.height)) : 0, 0, 255);
291 }
292 else
293 {
294 c1.height = cell.height;
295 c1.bridgeHeight = cell.bridgeHeight;
296 }
297 }
298 }
299 }
300 switch (dir)
301 {
302 case 0:
303 num3++;
304 if (num3 >= h)
305 {
306 num3 = 0;
307 num2++;
308 }
309 break;
310 case 1:
311 num2++;
312 if (num2 >= h)
313 {
314 num2 = 0;
315 num3--;
316 }
317 break;
318 case 2:
319 num3--;
320 if (num3 <= -h)
321 {
322 num3 = 0;
323 num2--;
324 }
325 break;
326 case 3:
327 num2--;
328 if (num2 <= -h)
329 {
330 num2 = 0;
331 num3++;
332 }
333 break;
334 }
335 }
336 }
337 if (mode != ApplyMode.Apply)
338 {
339 return;
340 }
341 p.Set(point.x, point.z);
343 EClass._map.things.ForeachReverse(delegate(Thing t)
344 {
345 if (validPoints.Contains(t.pos.index) && t.pos.x >= minX && t.pos.z >= minZ && t.pos.x < maxX && t.pos.z < maxZ && t.trait.CanBeDestroyed)
346 {
347 t.Destroy();
348 }
349 });
350 if (exportData != null)
351 {
352 exportData.serializedCards.Restore(EClass._map, null, addToZone: true, this);
353 }
356 void Render(TileRow row, int dir, int bridgeHeight)
357 {
358 if (row.id != 0)
359 {
360 Recipe orCreate2 = Recipe.GetOrCreate(row.RecipeID);
361 if (relative)
362 {
363 orCreate2?.OnRenderMarker(p, active: true, result, main: true, dir, c1.height + bridgeHeight);
364 }
365 else
366 {
367 orCreate2?.OnRenderMarker(p, active: true, result, main: true, dir, c1.height + bridgeHeight);
368 }
369 }
370 }
371 }
372
373 public int FixWall(Cell c)
374 {
375 switch (dir)
376 {
377 case 1:
378 if (c.blockDir == 0)
379 {
380 return 1;
381 }
382 if (c.blockDir == 1)
383 {
384 return 0;
385 }
386 break;
387 case 3:
388 if (c.blockDir == 0)
389 {
390 return 1;
391 }
392 if (c.blockDir == 1)
393 {
394 return 0;
395 }
396 if (c.blockDir == 2)
397 {
398 return 0;
399 }
400 break;
401 }
402 return c.blockDir;
403 }
404
405 public void Update()
406 {
407 if (!path.IsEmpty())
408 {
409 Map.UpdateMetaData(path, this);
410 }
411 }
412
413 public void Save(int _x, int _z, int _w, int _h)
414 {
415 offsetX = _x;
416 offsetZ = _z;
417 w = _w;
418 h = _h;
419 value = 0;
420 for (int i = _x; i < _x + w; i++)
421 {
422 for (int j = _z; j < _z + h; j++)
423 {
424 Cell cell = EClass._map.cells[i, j];
427 value += Card.GetTilePrice(cell.sourceObj, cell.matObj);
430 }
431 }
432 foreach (Thing thing in EClass._map.things)
433 {
434 if (thing.trait.CanCopyInBlueprint && thing.pos.Within(_x, _z, _w, _h) && thing.IsInstalled)
435 {
436 value += thing.GetPrice() * thing.Num;
437 }
438 }
440 }
441
442 public static PartialMap Apply(string path, Point pos)
443 {
444 PartialMap partialMap = Load(CorePath.MapPieceSave + path);
445 partialMap.ruinChance = 0f;
446 partialMap.Apply(pos, ApplyMode.Apply);
447 return partialMap;
448 }
449
450 public static PartialMap Load(string path = null)
451 {
452 bool flag = path.IsEmpty();
453 if (flag)
454 {
455 path = PathTemp;
456 }
457 if (!File.Exists(path))
458 {
459 return null;
460 }
461 MapMetaData metaData = Map.GetMetaData(path);
462 if (metaData == null || metaData.partial == null)
463 {
464 return null;
465 }
466 PartialMap partial = metaData.partial;
467 if (!flag)
468 {
469 partial.path = path;
470 }
471 partial.exportData = EClass._zone.Import(path);
472 partial._Load();
473 return partial;
474 }
475
476 public void _Load()
477 {
478 map = GameIO.LoadFile<Map>(EClass._zone.pathTemp + "map");
479 map.Load(EClass._zone.pathTemp, import: false, this);
480 if (map == null)
481 {
482 Debug.Log("Map is null:" + name + "/" + PathTemp);
483 return;
484 }
486 baseHeight = 255;
487 baseHeightWater = 255;
488 for (int i = 0; i < w; i++)
489 {
490 for (int j = 0; j < h; j++)
491 {
492 Cell cell = map.cells[i, j];
493 if (cell.height < baseHeight)
494 {
495 baseHeightWater = cell.height;
496 }
497 if (cell.height < baseHeight && !cell.IsFloorWater)
498 {
499 baseHeight = cell.height;
500 }
501 }
502 }
503 if (baseHeight == 255)
504 {
505 baseHeight = 0;
506 }
507 }
508
509 public static void Delete(string path)
510 {
511 string fullFileNameWithoutExtension = path.GetFullFileNameWithoutExtension();
512 IO.DeleteFile(path);
513 IO.DeleteFile(fullFileNameWithoutExtension + ".jpg");
514 IO.DeleteFile(fullFileNameWithoutExtension + ".txt");
515 }
516
517 public static void ExportDialog(string dir = null)
518 {
520 {
521 string pathDest = StandaloneFileBrowser.SaveFilePanel("Export Map Piece", dir ?? CorePath.MapPieceSaveUser, "new map piece", "mp");
522 if (!string.IsNullOrEmpty(pathDest))
523 {
524 SavePreview(PathTemp, pathDest);
525 Msg.SayRaw("Exported Zone");
526 }
527 });
528 }
529
530 public static void SavePreview(string path, string pathDest)
531 {
532 EClass.core.actionsNextFrame.Add(delegate
533 {
534 string fileName = CorePath.Temp + "preview.jpg";
535 Texture2D texture2D = ScreenCapture.CaptureScreenshotAsTexture();
536 int num = 200;
537 int num2 = 100;
538 RenderTexture renderTexture = new RenderTexture(num, num2, 0);
539 renderTexture.Create();
540 RenderTexture active = RenderTexture.active;
541 RenderTexture.active = renderTexture;
542 Graphics.Blit(texture2D, renderTexture);
543 Texture2D texture2D2 = new Texture2D(num, num2, texture2D.format, mipChain: false);
544 texture2D2.ReadPixels(new Rect(0f, 0f, num, num2), 0, 0);
545 texture2D2.Apply();
546 RenderTexture.active = active;
547 renderTexture.Release();
548 File.WriteAllBytes(fileName, texture2D2.EncodeToJPG());
549 ZipFile zipFile = ZipFile.Read(path);
550 zipFile.ExtractExistingFile = ExtractExistingFileAction.OverwriteSilently;
551 zipFile.AddFile(fileName, "");
552 zipFile.Save(pathDest);
553 zipFile.Dispose();
554 IO.DeleteFile(fileName);
555 Object.Destroy(texture2D2);
556 Object.Destroy(texture2D);
557 Object.Destroy(renderTexture);
558 });
559 }
560}
if(!match.Success)
HitResult
Definition: HitResult.cs:2
virtual MeshPass GetGuidePass(Point point)
Definition: ActionMode.cs:1296
static AM_Copy Copy
Definition: ActionMode.cs:45
void WaitForEndOfFrame(Action action)
Definition: BaseCore.cs:61
List< Action > actionsNextFrame
Definition: BaseCore.cs:31
TileGroup exterior
TileGroup interior
Definition: Card.cs:11
static int GetTilePrice(TileRow row, SourceMaterial.Row mat)
Definition: Card.cs:7657
int GetCurrency(string id="money")
Definition: Card.cs:4156
Point pos
Definition: Card.cs:60
Trait trait
Definition: Card.cs:54
void Destroy()
Definition: Card.cs:5314
bool IsInstalled
Definition: Card.cs:2451
int Num
Definition: Card.cs:161
Definition: Cell.cs:7
int _bridgeMat
Definition: Cell.cs:56
SourceBlock.Row sourceBlock
Definition: Cell.cs:1090
int _floor
Definition: Cell.cs:46
SourceMaterial.Row matBlock
Definition: Cell.cs:1066
SourceFloor.Row sourceFloor
Definition: Cell.cs:1092
SourceMaterial.Row matRoofBlock
Definition: Cell.cs:1064
bool toggleOcclusion
Definition: Cell.cs:666
byte decal
Definition: Cell.cs:70
int obj
Definition: Cell.cs:50
int objMat
Definition: Cell.cs:52
bool isFloating
Definition: Cell.cs:486
SourceMaterial.Row matObj
Definition: Cell.cs:1074
SourceMaterial.Row matBridge
Definition: Cell.cs:1072
bool IsFloorWater
Definition: Cell.cs:763
int _roofBlock
Definition: Cell.cs:58
byte objVal
Definition: Cell.cs:68
bool HasWall
Definition: Cell.cs:869
int bridgePillar
Definition: Cell.cs:74
int _floorMat
Definition: Cell.cs:48
byte height
Definition: Cell.cs:94
SourceFloor.Row sourceBridge
Definition: Cell.cs:1094
int _roofBlockMat
Definition: Cell.cs:60
SourceMaterial.Row matFloor
Definition: Cell.cs:1068
int floorDir
Definition: Cell.cs:958
bool hidePillar
Definition: Cell.cs:654
SourceObj.Row sourceObj
Definition: Cell.cs:1112
int objDir
Definition: Cell.cs:946
SourceBlock.Row sourceRoofBlock
Definition: Cell.cs:1088
int _blockMat
Definition: Cell.cs:44
byte bridgeHeight
Definition: Cell.cs:96
int blockDir
Definition: Cell.cs:934
byte _dirs
Definition: Cell.cs:66
int _bridge
Definition: Cell.cs:54
int _block
Definition: Cell.cs:42
static string MapPieceSaveUser
Definition: CorePath.cs:198
static string MapPieceSave
Definition: CorePath.cs:196
static void RebuildCritter(Cell cell)
Definition: Critter.cs:93
Definition: EClass.cs:6
static Core core
Definition: EClass.cs:7
static Zone _zone
Definition: EClass.cs:21
static Map _map
Definition: EClass.cs:19
static SourceManager sources
Definition: EClass.cs:43
static Chara pc
Definition: EClass.cs:15
Definition: GameIO.cs:11
bool Contains(int dx, int dz)
Definition: MapBounds.cs:38
PartialMap partial
Definition: MapMetaData.cs:22
Definition: Map.cs:13
static void UpdateMetaData(string pathZip, PartialMap partial=null)
Definition: Map.cs:870
List< Thing > things
Definition: Map.cs:52
void SetReference()
Definition: Map.cs:200
Cell[,] cells
Definition: Map.cs:88
static MapMetaData GetMetaData(string pathZip)
Definition: Map.cs:843
void Load(string path, bool import=false, PartialMap partial=null)
Definition: Map.cs:634
void RefreshAllTiles()
Definition: Map.cs:2248
void Add(Point point, float tile=0f, float color=0f)
Definition: MeshPass.cs:122
Definition: Msg.cs:5
static string SayRaw(string text)
Definition: Msg.cs:118
List< int > ruined
Definition: PartialMap.cs:22
bool allowRotate
Definition: PartialMap.cs:55
static void Delete(string path)
Definition: PartialMap.cs:509
int localOffsetZ
Definition: PartialMap.cs:62
string path
Definition: PartialMap.cs:68
int offsetX
Definition: PartialMap.cs:31
int baseHeight
Definition: PartialMap.cs:46
ZoneExportData exportData
Definition: PartialMap.cs:78
int offsetZ
Definition: PartialMap.cs:34
int baseHeightWater
Definition: PartialMap.cs:49
void Apply(Point _point, ApplyMode mode)
Definition: PartialMap.cs:112
int FixWall(Cell c)
Definition: PartialMap.cs:373
void _Load()
Definition: PartialMap.cs:476
static void SavePreview(string path, string pathDest)
Definition: PartialMap.cs:530
void ClearMarkedCells()
Definition: PartialMap.cs:103
static PartialMap Load(string path=null)
Definition: PartialMap.cs:450
int localOffsetX
Definition: PartialMap.cs:60
static string PathTemp
Definition: PartialMap.cs:86
bool ignoreBlock
Definition: PartialMap.cs:58
void Update()
Definition: PartialMap.cs:405
float ruinChance
Definition: PartialMap.cs:74
string ID
Definition: PartialMap.cs:90
static PartialMap Apply(string path, Point pos)
Definition: PartialMap.cs:442
HashSet< int > validPoints
Definition: PartialMap.cs:82
Result result
Definition: PartialMap.cs:84
List< Cell > cellsMarked
Definition: PartialMap.cs:80
static void ExportDialog(string dir=null)
Definition: PartialMap.cs:517
string name
Definition: PartialMap.cs:28
bool altDirPosition
Definition: PartialMap.cs:88
static bool relative
Definition: PartialMap.cs:25
bool editMode
Definition: PartialMap.cs:70
void Save(int _x, int _z, int _w, int _h)
Definition: PartialMap.cs:413
void Rotate()
Definition: PartialMap.cs:92
bool procedural
Definition: PartialMap.cs:72
Definition: Point.cs:9
int index
Definition: Point.cs:49
ref Vector3 Position(int height)
Definition: Point.cs:553
Point Set(int _x, int _z)
Definition: Point.cs:503
int x
Definition: Point.cs:36
int z
Definition: Point.cs:39
bool Within(int _x, int _z, int _w, int _h)
Definition: Point.cs:397
Cell cell
Definition: Point.cs:51
Definition: Rand.cs:4
static int Range(int min, int max)
Definition: Rand.cs:49
Definition: Recipe.cs:7
virtual void OnRenderMarker(Point point, bool active, HitResult result, bool main, int dir, int bridgeHeight)
Definition: Recipe.cs:735
static Recipe GetOrCreate(string id)
Definition: Recipe.cs:310
virtual string RecipeID
Definition: RenderRow.cs:108
static string SaveFilePanel(string title, string directory, string defaultName, string extension)
List< Data > cards
void Restore(Map map, Map orgMap, bool addToZone, PartialMap partial=null)
Dictionary< string, CardRow > map
Definition: SourceCard.cs:9
SourceCard cards
Definition: Thing.cs:8
override int GetPrice(CurrencyType currency=CurrencyType.Money, bool sell=false, PriceType priceType=PriceType.Default, Chara c=null)
Definition: Thing.cs:833
int id
Definition: TileRow.cs:9
virtual bool CanBeDestroyed
Definition: Trait.cs:307
virtual bool CanCopyInBlueprint
Definition: Trait.cs:166
SerializedCards serializedCards
ZoneExportData Import(string path)
Definition: Zone.cs:3466
BiomeProfile biome
Definition: Zone.cs:134
void Export(string path, PartialMap partial=null, bool usermap=false)
Definition: Zone.cs:3475
string pathTemp
Definition: Zone.cs:168