Elin Decompiled Documentation EA 23.153 Nightly
Loading...
Searching...
No Matches
Crawler.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using UnityEngine;
4
5public class Crawler : EScriptable
6{
7 public class Member
8 {
9 public Point pos = new Point();
10
11 public int life;
12 }
13
14 public class Result
15 {
16 public List<Point> points = new List<Point>();
17
19 }
20
21 public enum MoveType
22 {
23 Surface,
24 Block
25 }
26
27 public enum StartType
28 {
29 Surface,
30 Block
31 }
32
33 public static int[,] mapping = new int[0, 0];
34
35 public static int sync;
36
37 public int life;
38
39 public int member;
40
41 public int radius;
42
43 public int repeat;
44
45 public bool stickToStartBiome;
46
47 public bool skipBorder;
48
50
52
53 [NonSerialized]
54 public int Size;
55
56 [NonSerialized]
57 public List<Member> members = new List<Member>();
58
59 public bool CrawlUntil(Map map, Func<Point> onStart, int tries, Func<Result, bool> canComplete, Action onFail = null)
60 {
61 if (tries <= 0)
62 {
63 return false;
64 }
65 for (int i = 0; i < tries; i++)
66 {
67 bool flag = false;
68 for (int j = 0; j < 100; j++)
69 {
70 Point point = onStart();
71 switch (startType)
72 {
73 case StartType.Surface:
74 if (!point.HasBlock)
75 {
76 flag = true;
77 }
78 break;
79 case StartType.Block:
80 if (point.sourceBlock.tileType.IsFullBlock && point.Installed == null)
81 {
82 flag = true;
83 }
84 break;
85 }
86 }
87 if (!flag)
88 {
89 continue;
90 }
91 Result arg = Crawl(map, onStart());
92 if (canComplete(arg))
93 {
94 if (i > 0)
95 {
96 Debug.Log("CrawlUntil Complete:" + i);
97 }
98 return true;
99 }
100 }
101 if (onFail != null)
102 {
103 Debug.Log("CrawlUntil Fail" + tries);
104 onFail?.Invoke();
105 }
106 return false;
107 }
108
109 public void Crawl(Map map)
110 {
111 for (int i = 0; (float)i < map.sizeModifier * (float)repeat + 1f; i++)
112 {
113 Crawl(map, map.GetRandomSurface(), delegate(Point p)
114 {
115 p.cell.biome.Populate(p);
116 });
117 }
118 }
119
120 public Result Crawl(Map map, Point _start, Action<Point> onNewVisit = null)
121 {
122 if (members.Count == 0)
123 {
124 for (int i = 0; i < this.member; i++)
125 {
126 members.Add(new Member());
127 }
128 }
129 Point point = _start.Copy();
130 point.Clamp();
131 Result result = new Result();
132 BiomeProfile biome = point.cell.biome;
133 result.startPos = _start;
134 sync++;
135 Size = map.Size;
136 int num = (skipBorder ? 2 : 0);
137 int num2 = (skipBorder ? (Size - 2) : Size);
138 if (mapping.Length != Size)
139 {
140 mapping = new int[map.Size, map.Size];
141 }
142 foreach (Member member2 in members)
143 {
144 member2.pos.Set(point);
145 member2.life = life;
146 }
147 for (int j = 0; j < life; j++)
148 {
149 for (int k = 0; k < members.Count; k++)
150 {
151 Member member = members[k];
152 int num3 = EScriptable.rnd(3) - 1;
153 int num4 = EScriptable.rnd(3) - 1;
154 int num5 = member.pos.x + num3;
155 int num6 = member.pos.z + num4;
156 if (point.Distance(num5, num6) > radius || num5 < num || num6 < num || num5 >= num2 || num6 >= num2)
157 {
158 continue;
159 }
160 Cell cell = map.cells[num5, num6];
161 if (stickToStartBiome && cell.biome != biome)
162 {
163 continue;
164 }
165 switch (moveType)
166 {
167 case MoveType.Surface:
168 if (cell._block != 0 || cell.IsTopWater)
169 {
170 continue;
171 }
172 break;
173 case MoveType.Block:
174 if (cell._block == 0 || !cell.sourceBlock.tileType.IsFullBlock || cell.Installed != null)
175 {
176 continue;
177 }
178 break;
179 }
180 member.pos.Set(num5, num6);
181 if (mapping[num5, num6] != sync)
182 {
183 mapping[num5, num6] = sync;
184 Point point2 = member.pos.Copy();
185 result.points.Add(point2);
186 onNewVisit?.Invoke(point2);
187 }
188 }
189 }
190 return result;
191 }
192
193 public static Crawler Create(string id)
194 {
195 return ResourceCache.Load<Crawler>("World/Map/Crawler/crawler " + id);
196 }
197}
void Populate(Point point, bool interior=false)
Definition: Cell.cs:7
static Cell[,] cells
Definition: Cell.cs:24
byte _block
Definition: Cell.cs:30
SourceBlock.Row sourceBlock
Definition: Cell.cs:1052
BiomeProfile biome
Definition: Cell.cs:1075
Thing Installed
Definition: Cell.cs:975
bool IsTopWater
Definition: Cell.cs:700
Point pos
Definition: Crawler.cs:9
Point startPos
Definition: Crawler.cs:18
List< Point > points
Definition: Crawler.cs:16
static int sync
Definition: Crawler.cs:35
MoveType
Definition: Crawler.cs:22
int life
Definition: Crawler.cs:37
bool skipBorder
Definition: Crawler.cs:47
StartType startType
Definition: Crawler.cs:49
Result Crawl(Map map, Point _start, Action< Point > onNewVisit=null)
Definition: Crawler.cs:120
void Crawl(Map map)
Definition: Crawler.cs:109
int repeat
Definition: Crawler.cs:43
static Crawler Create(string id)
Definition: Crawler.cs:193
static int[,] mapping
Definition: Crawler.cs:33
MoveType moveType
Definition: Crawler.cs:51
int Size
Definition: Crawler.cs:54
int radius
Definition: Crawler.cs:41
List< Member > members
Definition: Crawler.cs:57
bool CrawlUntil(Map map, Func< Point > onStart, int tries, Func< Result, bool > canComplete, Action onFail=null)
Definition: Crawler.cs:59
bool stickToStartBiome
Definition: Crawler.cs:45
int member
Definition: Crawler.cs:39
StartType
Definition: Crawler.cs:28
static int rnd(int a)
Definition: EScriptable.cs:5
int Size
Definition: MapBounds.cs:20
Point GetRandomSurface(int x, int z, int radius, bool walkable=true, bool allowWater=false)
Definition: MapBounds.cs:182
Definition: Map.cs:13
float sizeModifier
Definition: Map.cs:127
Definition: Point.cs:9
Thing Installed
Definition: Point.cs:315
SourceBlock.Row sourceBlock
Definition: Point.cs:63
Point Copy()
Definition: Point.cs:479
Point Set(int _x, int _z)
Definition: Point.cs:491
Point Clamp(bool useBounds=false)
Definition: Point.cs:988
bool HasBlock
Definition: Point.cs:141
Cell cell
Definition: Point.cs:51