Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentPathfinding.cs
浏览该文件的文档.
1using Engine;
4
5namespace Game {
8
10
12
14
16
17 public Random m_random = new();
18
20
21 public double? m_lastPathfindingTime;
22
24
26
27 public double m_nextUpdateTime;
28
30
32
33 public const float m_minPathfindingPeriod = 6f;
34
35 public const float m_pathfindingCongestionCapacity = 500f;
36
37 public const float m_pathfindingCongestionCapacityLimit = 1000f;
38
39 public const float m_pathfindingCongestionDecayRate = 20f;
40
41 public static bool DrawPathfinding;
42
43 public Vector3? Destination { get; set; }
44
45 public float Range { get; set; }
46
47 public float Speed { get; set; }
48
49 public int MaxPathfindingPositions { get; set; }
50
51 public bool UseRandomMovements { get; set; }
52
53 public bool IgnoreHeightDifference { get; set; }
54
55 public bool RaycastDestination { get; set; }
56
57 public ComponentBody DoNotAvoidBody { get; set; }
58
59 public bool IsStuck { get; set; }
60
62
63 public virtual void SetDestination(Vector3? destination,
64 float speed,
65 float range,
66 int maxPathfindingPositions,
67 bool useRandomMovements,
68 bool ignoreHeightDifference,
69 bool raycastDestination,
70 ComponentBody doNotAvoidBody) {
71 Destination = destination;
72 Speed = speed;
73 Range = range;
74 MaxPathfindingPositions = maxPathfindingPositions;
75 UseRandomMovements = useRandomMovements;
76 IgnoreHeightDifference = ignoreHeightDifference;
77 RaycastDestination = raycastDestination;
78 DoNotAvoidBody = doNotAvoidBody;
80 m_nextUpdateTime = 0.0;
81 }
82
83 public virtual void Stop() {
85 null,
86 0f,
87 0f,
88 0,
89 false,
90 false,
91 false,
92 null
93 );
94 m_componentPilot.Stop();
95 IsStuck = false;
96 }
97
98 public virtual void Update(float dt) {
99 if (m_subsystemTime.GameTime >= m_nextUpdateTime) {
100 float num = m_random.Float(0.08f, 0.12f);
101 m_nextUpdateTime = m_subsystemTime.GameTime + num;
103 m_stateMachine.Update();
104 }
105 }
106
107 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
108 m_subsystemTime = Project.FindSubsystem<SubsystemTime>(true);
110 m_componentCreature = Entity.FindComponent<ComponentCreature>(true);
111 m_componentPilot = Entity.FindComponent<ComponentPilot>(true);
112 m_stateMachine.AddState(
113 "Stopped",
114 delegate {
115 Stop();
117 },
118 delegate {
119 if (Destination.HasValue) {
120 m_stateMachine.TransitionTo("MovingDirect");
121 }
122 },
123 null
124 );
125 m_stateMachine.AddState(
126 "MovingDirect",
127 delegate {
128 IsStuck = false;
130 },
131 delegate {
132 if (!Destination.HasValue) {
133 m_stateMachine.TransitionTo("Stopped");
134 }
135 else if (m_destinationChanged) {
136 m_componentPilot.SetDestination(
138 Speed,
139 Range,
142 Speed >= 1f,
144 );
145 m_destinationChanged = false;
146 }
147 else if (!m_componentPilot.Destination.HasValue) {
148 m_stateMachine.TransitionTo("Stopped");
149 }
150 else if (m_componentPilot.IsStuck) {
152 && m_componentCreature.ComponentLocomotion.WalkSpeed > 0f) {
153 m_stateMachine.TransitionTo("SearchingForPath");
154 }
155 else if (UseRandomMovements) {
156 m_stateMachine.TransitionTo("MovingRandomly");
157 }
158 else {
159 m_stateMachine.TransitionTo("Stuck");
160 }
161 }
162 },
163 null
164 );
165 m_stateMachine.AddState(
166 "SearchingForPath",
167 delegate {
168 m_pathfindingResult.IsCompleted = false;
169 m_pathfindingResult.IsInProgress = false;
170 },
171 delegate {
172 if (!Destination.HasValue) {
173 m_stateMachine.TransitionTo("Stopped");
174 }
175 else if (!m_pathfindingResult.IsInProgress
176 && (!m_lastPathfindingTime.HasValue || m_subsystemTime.GameTime - m_lastPathfindingTime > 8.0)
177 && m_pathfindingCongestion < 500f) {
180 Vector3 start = m_componentCreature.ComponentBody.Position + new Vector3(0f, 0.01f, 0f);
181 Vector3 end = Destination.Value + new Vector3(0f, 0.01f, 0f);
182 ComponentMiner componentMiner = Entity.FindComponent<ComponentMiner>();
183 bool ignoreDoors = componentMiner != null && componentMiner.AutoInteractRate > 0f && m_random.Bool(0.5f);
184 m_subsystemPathfinding.QueuePathSearch(
185 start,
186 end,
187 1f,
188 m_componentCreature.ComponentBody.BoxSize,
189 ignoreDoors,
192 );
193 }
194 else if (UseRandomMovements) {
195 m_stateMachine.TransitionTo("MovingRandomly");
196 }
197 if (m_pathfindingResult.IsCompleted) {
199 if (m_pathfindingResult.Path.Count > 0) {
200 m_stateMachine.TransitionTo("MovingWithPath");
201 }
202 else if (UseRandomMovements) {
203 m_stateMachine.TransitionTo("MovingRandomly");
204 }
205 else {
206 m_stateMachine.TransitionTo("Stuck");
207 }
208 }
209 },
210 null
211 );
212 m_stateMachine.AddState(
213 "MovingWithPath",
214 delegate {
215 m_componentPilot.Stop();
217 },
218 delegate {
219 if (!Destination.HasValue) {
220 m_stateMachine.TransitionTo("Stopped");
221 }
222 else if (!m_componentPilot.Destination.HasValue) {
223 if (m_pathfindingResult.Path.Count > 0) {
224 Vector3 value = m_pathfindingResult.Path.Array[m_pathfindingResult.Path.Count - 1];
225 m_componentPilot.SetDestination(
226 value,
227 MathUtils.Min(Speed, 0.75f),
228 0.75f,
229 false,
230 false,
231 Speed >= 1f,
233 );
234 m_pathfindingResult.Path.RemoveAt(m_pathfindingResult.Path.Count - 1);
235 }
236 else {
237 m_stateMachine.TransitionTo("MovingDirect");
238 }
239 }
240 else if (m_componentPilot.IsStuck) {
241 if (UseRandomMovements) {
242 m_stateMachine.TransitionTo("MovingRandomly");
243 }
244 else {
245 m_stateMachine.TransitionTo("Stuck");
246 }
247 }
248 else {
249 float num = Vector3.DistanceSquared(m_componentCreature.ComponentBody.Position, Destination.Value);
251 m_stateMachine.TransitionTo("MovingDirect");
252 }
253 }
254 },
255 null
256 );
257 m_stateMachine.AddState(
258 "MovingRandomly",
259 delegate {
260 m_componentPilot.SetDestination(
261 m_componentCreature.ComponentBody.Position + new Vector3(5f * m_random.Float(-1f, 1f), 0f, 5f * m_random.Float(-1f, 1f)),
262 1f,
263 1f,
264 true,
265 false,
266 false,
268 );
270 },
271 delegate {
272 if (!Destination.HasValue) {
273 m_stateMachine.TransitionTo("Stopped");
274 }
275 else if (m_randomMoveCount > 3) {
276 m_stateMachine.TransitionTo("Stuck");
277 }
278 else if (m_componentPilot.IsStuck
279 || !m_componentPilot.Destination.HasValue) {
280 m_stateMachine.TransitionTo("MovingDirect");
281 }
282 },
283 null
284 );
285 m_stateMachine.AddState(
286 "Stuck",
287 delegate { IsStuck = true; },
288 delegate {
289 if (!Destination.HasValue) {
290 m_stateMachine.TransitionTo("Stopped");
291 }
292 else if (m_destinationChanged) {
293 m_destinationChanged = false;
294 m_stateMachine.TransitionTo("MovingDirect");
295 }
296 },
297 null
298 );
299 m_stateMachine.TransitionTo("Stopped");
300 }
301 }
302}
Engine.Vector3 Vector3
static int Min(int x1, int x2)
static int Max(int x1, int x2)
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
SubsystemPathfinding m_subsystemPathfinding
virtual void SetDestination(Vector3? destination, float speed, float range, int maxPathfindingPositions, bool useRandomMovements, bool ignoreHeightDifference, bool raycastDestination, ComponentBody doNotAvoidBody)
ValuesDictionary ValuesDictionary
static float DistanceSquared(Vector3 v1, Vector3 v2)