Elin Decompiled Documentation EA 23.102 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 = (byte)interior.block.id;
243 c1._blockMat = (byte)interior.block.mat;
244 }
245 else if (cell._block == 150 && !editMode)
246 {
247 c1._block = (byte)exterior.block.id;
248 c1._blockMat = (byte)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 = (byte)interior.floor.id;
258 c1._floorMat = (byte)interior.floor.mat;
259 }
260 else if (cell._floor == 3 && !editMode)
261 {
262 c1._floor = (byte)exterior.floor.id;
263 c1._floorMat = (byte)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.isModified = true;
281 if (c1.HasWall)
282 {
283 c1.blockDir = FixWall(c1);
284 }
285 if (relative || procedural)
286 {
287 c1.height = (byte)Mathf.Clamp(height + cell.height - num, 0, 255);
288 c1.bridgeHeight = (byte)Mathf.Clamp((cell.bridgeHeight != 0) ? (c1.height + (cell.bridgeHeight - cell.height)) : 0, 0, 255);
289 }
290 else
291 {
292 c1.height = cell.height;
293 c1.bridgeHeight = cell.bridgeHeight;
294 }
295 }
296 }
297 }
298 switch (dir)
299 {
300 case 0:
301 num3++;
302 if (num3 >= h)
303 {
304 num3 = 0;
305 num2++;
306 }
307 break;
308 case 1:
309 num2++;
310 if (num2 >= h)
311 {
312 num2 = 0;
313 num3--;
314 }
315 break;
316 case 2:
317 num3--;
318 if (num3 <= -h)
319 {
320 num3 = 0;
321 num2--;
322 }
323 break;
324 case 3:
325 num2--;
326 if (num2 <= -h)
327 {
328 num2 = 0;
329 num3++;
330 }
331 break;
332 }
333 }
334 }
335 if (mode != ApplyMode.Apply)
336 {
337 return;
338 }
339 p.Set(point.x, point.z);
341 EClass._map.things.ForeachReverse(delegate(Thing t)
342 {
343 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)
344 {
345 t.Destroy();
346 }
347 });
348 if (exportData != null)
349 {
350 exportData.serializedCards.Restore(EClass._map, null, addToZone: true, this);
351 }
354 void Render(TileRow row, int dir, int bridgeHeight)
355 {
356 if (row.id != 0)
357 {
358 Recipe orCreate2 = Recipe.GetOrCreate(row.RecipeID);
359 if (relative)
360 {
361 orCreate2?.OnRenderMarker(p, active: true, result, main: true, dir, c1.height + bridgeHeight);
362 }
363 else
364 {
365 orCreate2?.OnRenderMarker(p, active: true, result, main: true, dir, c1.height + bridgeHeight);
366 }
367 }
368 }
369 }
370
371 public int FixWall(Cell c)
372 {
373 switch (dir)
374 {
375 case 1:
376 if (c.blockDir == 0)
377 {
378 return 1;
379 }
380 if (c.blockDir == 1)
381 {
382 return 0;
383 }
384 break;
385 case 3:
386 if (c.blockDir == 0)
387 {
388 return 1;
389 }
390 if (c.blockDir == 1)
391 {
392 return 0;
393 }
394 if (c.blockDir == 2)
395 {
396 return 0;
397 }
398 break;
399 }
400 return c.blockDir;
401 }
402
403 public void Update()
404 {
405 if (!path.IsEmpty())
406 {
407 Map.UpdateMetaData(path, this);
408 }
409 }
410
411 public void Save(int _x, int _z, int _w, int _h)
412 {
413 offsetX = _x;
414 offsetZ = _z;
415 w = _w;
416 h = _h;
417 value = 0;
418 for (int i = _x; i < _x + w; i++)
419 {
420 for (int j = _z; j < _z + h; j++)
421 {
422 Cell cell = EClass._map.cells[i, j];
425 value += Card.GetTilePrice(cell.sourceObj, cell.matObj);
428 }
429 }
430 foreach (Thing thing in EClass._map.things)
431 {
432 if (thing.trait.CanCopyInBlueprint && thing.pos.Within(_x, _z, _w, _h) && thing.IsInstalled)
433 {
434 value += thing.GetPrice() * thing.Num;
435 }
436 }
438 }
439
440 public static PartialMap Apply(string path, Point pos)
441 {
442 PartialMap partialMap = Load(CorePath.MapPieceSave + path);
443 partialMap.ruinChance = 0f;
444 partialMap.Apply(pos, ApplyMode.Apply);
445 return partialMap;
446 }
447
448 public static PartialMap Load(string path = null)
449 {
450 bool flag = path.IsEmpty();
451 if (flag)
452 {
453 path = PathTemp;
454 }
455 if (!File.Exists(path))
456 {
457 return null;
458 }
459 MapMetaData metaData = Map.GetMetaData(path);
460 if (metaData == null || metaData.partial == null)
461 {
462 return null;
463 }
464 PartialMap partial = metaData.partial;
465 if (!flag)
466 {
467 partial.path = path;
468 }
469 partial.exportData = EClass._zone.Import(path);
470 partial._Load();
471 return partial;
472 }
473
474 public void _Load()
475 {
476 map = GameIO.LoadFile<Map>(EClass._zone.pathTemp + "map");
477 map.Load(EClass._zone.pathTemp, import: false, this);
478 if (map == null)
479 {
480 Debug.Log("Map is null:" + name + "/" + PathTemp);
481 return;
482 }
484 baseHeight = 255;
485 baseHeightWater = 255;
486 for (int i = 0; i < w; i++)
487 {
488 for (int j = 0; j < h; j++)
489 {
490 Cell cell = map.cells[i, j];
491 if (cell.height < baseHeight)
492 {
493 baseHeightWater = cell.height;
494 }
495 if (cell.height < baseHeight && !cell.IsFloorWater)
496 {
497 baseHeight = cell.height;
498 }
499 }
500 }
501 if (baseHeight == 255)
502 {
503 baseHeight = 0;
504 }
505 }
506
507 public static void Delete(string path)
508 {
509 string fullFileNameWithoutExtension = path.GetFullFileNameWithoutExtension();
510 IO.DeleteFile(path);
511 IO.DeleteFile(fullFileNameWithoutExtension + ".jpg");
512 IO.DeleteFile(fullFileNameWithoutExtension + ".txt");
513 }
514
515 public static void ExportDialog(string dir = null)
516 {
518 {
519 string pathDest = StandaloneFileBrowser.SaveFilePanel("Export Map Piece", dir ?? CorePath.MapPieceSaveUser, "new map piece", "mp");
520 if (!string.IsNullOrEmpty(pathDest))
521 {
522 SavePreview(PathTemp, pathDest);
523 Msg.SayRaw("Exported Zone");
524 }
525 });
526 }
527
528 public static void SavePreview(string path, string pathDest)
529 {
530 EClass.core.actionsNextFrame.Add(delegate
531 {
532 string fileName = CorePath.Temp + "preview.jpg";
533 Texture2D texture2D = ScreenCapture.CaptureScreenshotAsTexture();
534 int num = 200;
535 int num2 = 100;
536 RenderTexture renderTexture = new RenderTexture(num, num2, 0);
537 renderTexture.Create();
538 RenderTexture active = RenderTexture.active;
539 RenderTexture.active = renderTexture;
540 Graphics.Blit(texture2D, renderTexture);
541 Texture2D texture2D2 = new Texture2D(num, num2, texture2D.format, mipChain: false);
542 texture2D2.ReadPixels(new Rect(0f, 0f, num, num2), 0, 0);
543 texture2D2.Apply();
544 RenderTexture.active = active;
545 renderTexture.Release();
546 File.WriteAllBytes(fileName, texture2D2.EncodeToJPG());
547 ZipFile zipFile = ZipFile.Read(path);
548 zipFile.ExtractExistingFile = ExtractExistingFileAction.OverwriteSilently;
549 zipFile.AddFile(fileName, "");
550 zipFile.Save(pathDest);
551 zipFile.Dispose();
552 IO.DeleteFile(fileName);
553 Object.Destroy(texture2D2);
554 Object.Destroy(texture2D);
555 Object.Destroy(renderTexture);
556 });
557 }
558}
HitResult
Definition: HitResult.cs:2
if(item3.idFile==idFirstFile &&item3.id==idFirstTopic)
Definition: UIBook.cs:627
virtual MeshPass GetGuidePass(Point point)
Definition: ActionMode.cs:1263
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
virtual int GetPrice(CurrencyType currency=CurrencyType.Money, bool sell=false, PriceType priceType=PriceType.Default, Chara c=null)
Definition: Card.cs:6449
static int GetTilePrice(TileRow row, SourceMaterial.Row mat)
Definition: Card.cs:6345
int GetCurrency(string id="money")
Definition: Card.cs:3652
Point pos
Definition: Card.cs:55
Trait trait
Definition: Card.cs:49
void Destroy()
Definition: Card.cs:4538
bool IsInstalled
Definition: Card.cs:2241
int Num
Definition: Card.cs:154
Definition: Cell.cs:7
byte _block
Definition: Cell.cs:30
SourceBlock.Row sourceBlock
Definition: Cell.cs:1052
SourceMaterial.Row matBlock
Definition: Cell.cs:1030
byte objMat
Definition: Cell.cs:42
SourceFloor.Row sourceFloor
Definition: Cell.cs:1054
byte _bridge
Definition: Cell.cs:46
SourceMaterial.Row matRoofBlock
Definition: Cell.cs:1028
byte _floor
Definition: Cell.cs:34
byte decal
Definition: Cell.cs:44
bool isFloating
Definition: Cell.cs:474
SourceMaterial.Row matObj
Definition: Cell.cs:1036
SourceMaterial.Row matBridge
Definition: Cell.cs:1034
bool IsFloorWater
Definition: Cell.cs:727
byte objVal
Definition: Cell.cs:40
byte _floorMat
Definition: Cell.cs:36
bool HasWall
Definition: Cell.cs:833
byte _blockMat
Definition: Cell.cs:32
byte _roofBlock
Definition: Cell.cs:50
byte _bridgeMat
Definition: Cell.cs:48
byte height
Definition: Cell.cs:72
SourceFloor.Row sourceBridge
Definition: Cell.cs:1056
SourceMaterial.Row matFloor
Definition: Cell.cs:1032
int floorDir
Definition: Cell.cs:922
byte _roofBlockMat
Definition: Cell.cs:52
byte obj
Definition: Cell.cs:38
SourceObj.Row sourceObj
Definition: Cell.cs:1072
int objDir
Definition: Cell.cs:910
SourceBlock.Row sourceRoofBlock
Definition: Cell.cs:1050
byte bridgeHeight
Definition: Cell.cs:74
int blockDir
Definition: Cell.cs:898
byte _dirs
Definition: Cell.cs:28
byte bridgePillar
Definition: Cell.cs:82
static string MapPieceSaveUser
Definition: CorePath.cs:202
static string MapPieceSave
Definition: CorePath.cs:200
static void RebuildCritter(Cell cell)
Definition: Critter.cs:93
Definition: EClass.cs:5
static Core core
Definition: EClass.cs:6
static Zone _zone
Definition: EClass.cs:20
static Map _map
Definition: EClass.cs:18
static SourceManager sources
Definition: EClass.cs:42
static Chara pc
Definition: EClass.cs:14
Definition: GameIO.cs:10
bool Contains(int dx, int dz)
Definition: MapBounds.cs:38
PartialMap partial
Definition: MapMetaData.cs:19
Definition: Map.cs:13
static void UpdateMetaData(string pathZip, PartialMap partial=null)
Definition: Map.cs:759
List< Thing > things
Definition: Map.cs:49
void SetReference()
Definition: Map.cs:195
Cell[,] cells
Definition: Map.cs:85
static MapMetaData GetMetaData(string pathZip)
Definition: Map.cs:732
void Load(string path, bool import=false, PartialMap partial=null)
Definition: Map.cs:542
void RefreshAllTiles()
Definition: Map.cs:2086
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:113
List< int > ruined
Definition: PartialMap.cs:22
bool allowRotate
Definition: PartialMap.cs:55
static void Delete(string path)
Definition: PartialMap.cs:507
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:371
void _Load()
Definition: PartialMap.cs:474
static void SavePreview(string path, string pathDest)
Definition: PartialMap.cs:528
void ClearMarkedCells()
Definition: PartialMap.cs:103
static PartialMap Load(string path=null)
Definition: PartialMap.cs:448
int localOffsetX
Definition: PartialMap.cs:60
static string PathTemp
Definition: PartialMap.cs:86
bool ignoreBlock
Definition: PartialMap.cs:58
void Update()
Definition: PartialMap.cs:403
float ruinChance
Definition: PartialMap.cs:74
string ID
Definition: PartialMap.cs:90
static PartialMap Apply(string path, Point pos)
Definition: PartialMap.cs:440
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:515
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:411
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:524
Point Set(int _x, int _z)
Definition: Point.cs:479
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:373
Cell cell
Definition: Point.cs:51
Definition: Rand.cs:4
static int Range(int min, int max)
Definition: Rand.cs:42
Definition: Recipe.cs:7
virtual void OnRenderMarker(Point point, bool active, HitResult result, bool main, int dir, int bridgeHeight)
Definition: Recipe.cs:688
static Recipe GetOrCreate(string id)
Definition: Recipe.cs:293
virtual string RecipeID
Definition: RenderRow.cs:105
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:8
SourceCard cards
Definition: Thing.cs:8
int id
Definition: TileRow.cs:8
virtual bool CanBeDestroyed
Definition: Trait.cs:285
virtual bool CanCopyInBlueprint
Definition: Trait.cs:148
SerializedCards serializedCards
ZoneExportData Import(string path)
Definition: Zone.cs:2889
BiomeProfile biome
Definition: Zone.cs:126
void Export(string path, PartialMap partial=null, bool usermap=false)
Definition: Zone.cs:2898
string pathTemp
Definition: Zone.cs:158