-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQrtSpawnService.cs
More file actions
295 lines (251 loc) · 10.5 KB
/
Copy pathQrtSpawnService.cs
File metadata and controls
295 lines (251 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using System;
using System.Collections.Generic;
using PlayerRoles;
using PlayerRoles.FirstPersonControl.Spawnpoints;
using UnityEngine;
using Logger = LabApi.Features.Console.Logger;
namespace QRTForces
{
public sealed class QrtSpawnService
{
private const string LogPrefix = "[QRTForces]";
private const float DefaultRotation = 0f;
private const float MaximumCoordinateAbs = 10000f;
private const float ZeroPointToleranceSqr = 0.01f;
private const float GroundProbeUpDistance = 4f;
private const float GroundProbeDownDistance = 24f;
private const float GroundNormalMinY = 0.65f;
private const float GroundOffset = 0.08f;
private const float CapsuleRadius = 0.35f;
private const float CapsuleHeight = 1.8f;
private const float MinimumPlayerSpacing = 1.1f;
private const float GoldenAngleRadians = 2.39996323f;
private readonly QrtConfig config;
public QrtSpawnService(QrtConfig config)
{
this.config = config ?? QrtConfig.CreateDefault();
}
internal List<QrtSpawnPoint> CreateSpawnPlans(int requestedCount)
{
List<QrtSpawnPoint> plans = new List<QrtSpawnPoint>();
if (requestedCount <= 0)
{
return plans;
}
if (config.Spawn != null && config.Spawn.UseConfiguredPosition)
{
Vector3 configuredPosition = ToVector3(config.Spawn.Position);
TryAppendSafePlans(configuredPosition, DefaultRotation, "配置坐标", requestedCount, plans);
if (plans.Count >= requestedCount)
{
return plans;
}
Logger.Warn(
$"{LogPrefix} 配置生成点只解析出 {plans.Count}/{requestedCount} 个安全落点,将继续尝试默认地表 MTF 生成点。");
}
if (TryGetDefaultMtfSpawnpoint(out Vector3 defaultPosition, out float defaultRotation))
{
TryAppendSafePlans(defaultPosition, defaultRotation, "默认地表 MTF 生成点", requestedCount, plans);
}
else
{
Logger.Warn($"{LogPrefix} 无法从 RoleSpawnpointManager 获取默认地表 MTF 生成点。");
}
if (plans.Count <= 0)
{
Logger.Error($"{LogPrefix} 未解析到任何安全快速反应部队生成点,本次 QRT 成员将保留原生承载角色出生位置。");
}
else if (plans.Count < requestedCount)
{
Logger.Warn(
$"{LogPrefix} 安全快速反应部队生成点不足:安全点={plans.Count},需要={requestedCount}。剩余成员将保留原生承载角色出生位置。");
}
return plans;
}
private void TryAppendSafePlans(
Vector3 basePosition,
float horizontalRotation,
string source,
int requestedCount,
List<QrtSpawnPoint> plans)
{
int beforeCount = plans.Count;
if (!IsReasonableWorldPosition(basePosition, out string invalidReason))
{
Logger.Warn($"{LogPrefix} {source} 不可用:{invalidReason}。坐标={FormatVector(basePosition)}");
return;
}
try
{
foreach (Vector3 offset in BuildOffsetCandidates(requestedCount))
{
if (plans.Count >= requestedCount)
{
break;
}
Vector3 candidate = basePosition + offset;
if (!TryResolveSafeLanding(candidate, out Vector3 landing, out _))
{
continue;
}
if (!IsFarEnoughFromExistingPlans(landing, plans))
{
continue;
}
plans.Add(new QrtSpawnPoint(landing, horizontalRotation, source));
}
}
catch (Exception exception)
{
Logger.Warn($"{LogPrefix} {source} 安全落点解析发生异常,将跳过该来源。错误:{exception.Message}");
}
int addedCount = plans.Count - beforeCount;
if (addedCount > 0)
{
Logger.Info(
$"{LogPrefix} {source} 已解析出 {addedCount} 个安全快速反应部队生成点,累计={plans.Count}/{requestedCount}。");
}
else
{
Logger.Warn($"{LogPrefix} {source} 未能解析出安全快速反应部队生成点。基础坐标={FormatVector(basePosition)}");
}
}
private bool TryResolveSafeLanding(Vector3 candidate, out Vector3 landing, out string reason)
{
landing = Vector3.zero;
if (!IsReasonableWorldPosition(candidate, out reason))
{
return false;
}
Vector3 rayStart = candidate + Vector3.up * GroundProbeUpDistance;
float rayDistance = GroundProbeUpDistance + GroundProbeDownDistance;
if (!Physics.Raycast(rayStart, Vector3.down, out RaycastHit hit, rayDistance, Physics.DefaultRaycastLayers, QueryTriggerInteraction.Ignore))
{
reason = "向下射线未找到地面";
return false;
}
if (hit.normal.y < GroundNormalMinY)
{
reason = $"地面坡度过大,法线Y={hit.normal.y:0.00}";
return false;
}
landing = hit.point + Vector3.up * GroundOffset;
if (!IsReasonableWorldPosition(landing, out reason))
{
return false;
}
if (!HasBodyClearance(landing, out reason))
{
return false;
}
return true;
}
private static bool HasBodyClearance(Vector3 landing, out string reason)
{
Vector3 capsuleBottom = landing + Vector3.up * (CapsuleRadius + GroundOffset);
Vector3 capsuleTop = landing + Vector3.up * (CapsuleHeight - CapsuleRadius);
if (Physics.CheckCapsule(capsuleBottom, capsuleTop, CapsuleRadius, Physics.DefaultRaycastLayers, QueryTriggerInteraction.Ignore))
{
reason = "玩家身体胶囊空间被碰撞体占用";
return false;
}
reason = string.Empty;
return true;
}
private IEnumerable<Vector3> BuildOffsetCandidates(int requestedCount)
{
yield return Vector3.zero;
float spreadRadius = config.Spawn == null ? new QrtSpawnConfig().SpreadRadius : config.Spawn.SpreadRadius;
spreadRadius = Math.Max(MinimumPlayerSpacing, spreadRadius);
int sampleCount = Math.Max(24, requestedCount * 12);
for (int ring = 1; ring <= 3; ring++)
{
float radius = Math.Max(MinimumPlayerSpacing, spreadRadius * ring / 3f);
for (int i = 0; i < sampleCount; i++)
{
float angle = (i * GoldenAngleRadians) + (ring * 0.41f);
yield return new Vector3((float)Math.Cos(angle) * radius, 0f, (float)Math.Sin(angle) * radius);
}
}
}
private static bool TryGetDefaultMtfSpawnpoint(out Vector3 position, out float horizontalRotation)
{
position = Vector3.zero;
horizontalRotation = DefaultRotation;
try
{
if (!RoleSpawnpointManager.TryGetSpawnpointForRole(RoleTypeId.NtfPrivate, out ISpawnpointHandler spawnpointHandler) ||
spawnpointHandler == null)
{
return false;
}
return spawnpointHandler.TryGetSpawnpoint(out position, out horizontalRotation);
}
catch (Exception exception)
{
Logger.Warn($"{LogPrefix} 获取默认地表 MTF 生成点时发生异常:{exception.Message}");
return false;
}
}
private static bool IsFarEnoughFromExistingPlans(Vector3 position, IEnumerable<QrtSpawnPoint> plans)
{
float minimumDistanceSqr = MinimumPlayerSpacing * MinimumPlayerSpacing;
foreach (QrtSpawnPoint plan in plans)
{
Vector3 delta = position - plan.Position;
delta.y = 0f;
if (delta.sqrMagnitude < minimumDistanceSqr)
{
return false;
}
}
return true;
}
private static bool IsReasonableWorldPosition(Vector3 position, out string reason)
{
if (!IsFinite(position.x) || !IsFinite(position.y) || !IsFinite(position.z))
{
reason = "坐标包含 NaN 或 Infinity";
return false;
}
if (Math.Abs(position.x) > MaximumCoordinateAbs ||
Math.Abs(position.y) > MaximumCoordinateAbs ||
Math.Abs(position.z) > MaximumCoordinateAbs)
{
reason = $"坐标绝对值超过 {MaximumCoordinateAbs}";
return false;
}
if (position.sqrMagnitude <= ZeroPointToleranceSqr)
{
reason = "坐标接近 Vector3.zero,疑似非法回退点";
return false;
}
reason = string.Empty;
return true;
}
private static Vector3 ToVector3(QrtVector3Config position)
{
return position == null ? Vector3.zero : new Vector3(position.X, position.Y, position.Z);
}
private static bool IsFinite(float value)
{
return !float.IsNaN(value) && !float.IsInfinity(value);
}
private static string FormatVector(Vector3 position)
{
return $"({position.x:0.00}, {position.y:0.00}, {position.z:0.00})";
}
}
internal sealed class QrtSpawnPoint
{
public QrtSpawnPoint(Vector3 position, float horizontalRotation, string source)
{
Position = position;
HorizontalRotation = horizontalRotation;
Source = source ?? string.Empty;
}
public Vector3 Position { get; }
public float HorizontalRotation { get; }
public string Source { get; }
}
}