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