Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_AmbientVehicleSystem.c
Go to the documentation of this file.
1void OnAmbientVehicleSpawnedDelegate(SCR_AmbientVehicleSpawnPointComponent spawnpoint, Vehicle vehicle);
2
4
5typedef ScriptInvokerBase<OnAmbientVehicleSpawnedDelegate> OnAmbientVehicleSpawnedInvoker;
6
8{
9 override static void InitInfo(WorldSystemInfo outInfo)
10 {
11 outInfo
12 .SetAbstract(false)
13 .SetLocation(ESystemLocation.Server)
14 .AddPoint(ESystemPoint.FixedFrame);
15 }
16
17 protected static const int CHECK_INTERVAL = 3; //s, how often should an individual spawnpoint be checked
18 protected static const int DESPAWN_TIMEOUT = 10000; //ms
19 protected static const int PARKED_THRESHOLD = 5; //m, how far away can the spawned vehicle moved before being considered used
20
21 protected static const int SPAWN_RADIUS_MIN_SQ = 500 * 500; // Square value for distance checks
22 protected static const int SPAWN_RADIUS_MAX_SQ = 1000 * 1000; // Square value for distance checks
23 protected static const int DESPAWN_RADIUS_DIFF_SQ = 200 * 200; // Square value for distance checks
24
25 protected ref array<SCR_AmbientVehicleSpawnPointComponent> m_aSpawnpoints = {};
26 protected ref array<IEntity> m_aPlayers = {};
27
29
30 protected int m_iIndexToCheck;
31 protected int m_iSpawnDistanceSq;
32 protected int m_iDespawnDistanceSq;
33
34 protected float m_fTimer;
35 protected float m_fCheckInterval;
36
37 //------------------------------------------------------------------------------------------------
38 override event protected void OnInit()
39 {
40 // No need to run updates unless some spawnpoints are actually registered
41 if (m_aSpawnpoints.IsEmpty())
42 Enable(false);
43
45
46 // Calculate (de)spawn distance based on view distance, have it squared for faster distance calculation
47 int fractionVD = GetGame().GetViewDistance() * 0.3;
48 m_iSpawnDistanceSq = fractionVD * fractionVD;
52
54 if (!gameMode)
55 return;
56
58 gameMode.GetOnPlayerKilled().Insert(OnPlayerKilled);
61 }
62
63 //------------------------------------------------------------------------------------------------
64 override event protected void OnCleanup()
65 {
67 if (!gameMode)
68 return;
69
71 gameMode.GetOnPlayerKilled().Remove(OnPlayerKilled);
74 }
75
76 //------------------------------------------------------------------------------------------------
77 override event protected void OnUpdatePoint(WorldUpdatePointArgs args)
78 {
79 if (!GetGame().AreGameFlagsSet(EGameFlags.SpawnVehicles))
80 {
81 Enable(false);
82 return;
83 }
84
85 m_fTimer += args.GetTimeSliceSeconds();
87 return;
88
89 m_fTimer = 0;
90
92 if (!m_aSpawnpoints.IsIndexValid(m_iIndexToCheck))
94 }
95
96 //------------------------------------------------------------------------------------------------
97 override event bool ShouldBePaused()
98 {
99 return true;
100 }
101
102 //------------------------------------------------------------------------------------------------
104 {
105 World world = GetGame().GetWorld();
106 if (!world)
107 return null;
108
109 return SCR_AmbientVehicleSystem.Cast(world.FindSystem(SCR_AmbientVehicleSystem));
110 }
111
112 //------------------------------------------------------------------------------------------------
120
121 //------------------------------------------------------------------------------------------------
122 protected void UpdateCheckInterval()
123 {
125 }
126
127 //------------------------------------------------------------------------------------------------
128 protected void RefreshPlayerList()
129 {
130 m_aPlayers.Clear();
131 array<int> playerIds = {};
132 PlayerManager pc = GetGame().GetPlayerManager();
133 int playersCount = pc.GetPlayers(playerIds);
134
135 foreach (int playerId : playerIds)
136 {
137 IEntity player = pc.GetPlayerControlledEntity(playerId);
138
139 if (!player)
140 continue;
141
142 CharacterControllerComponent comp = CharacterControllerComponent.Cast(player.FindComponent(CharacterControllerComponent));
143
144 if (!comp || comp.IsDead())
145 continue;
146
147 m_aPlayers.Insert(player);
148 }
149 }
150
151 //------------------------------------------------------------------------------------------------
152 protected void OnPlayerSpawnedOrDeleted(int playerId, IEntity player)
153 {
155 }
156
157 //------------------------------------------------------------------------------------------------
158 protected void ProcessSpawnpoint(int spawnpointIndex)
159 {
160 if (!m_aSpawnpoints || !m_aSpawnpoints.IsIndexValid(spawnpointIndex))
161 return;
162
163 SCR_AmbientVehicleSpawnPointComponent spawnpoint = m_aSpawnpoints[spawnpointIndex];
164 if (!spawnpoint || spawnpoint.GetIsDepleted())
165 return;
166
167 Vehicle spawnedVeh = spawnpoint.GetSpawnedVehicle();
168 ChimeraWorld world = GetWorld();
169 WorldTimestamp currentTime = world.GetServerTimestamp();
170 int respawnPeriod = spawnpoint.GetRespawnPeriod();
171
172 // Non-respawning vehicle has beed deleted by other means
173 // Ignore this spawnpoint from now on
174 if (!spawnedVeh && spawnpoint.GetIsSpawnProcessed() && respawnPeriod <= 0)
175 {
176 spawnpoint.SetIsDepleted(true);
177 return;
178 }
179
180 if (!spawnedVeh)
181 {
182 WorldTimestamp respawnTimestamp = spawnpoint.GetRespawnTimestamp();
183
184 // Respawn timer is ticking
185 if (respawnTimestamp.Greater(currentTime))
186 return;
187
188 if (spawnpoint.GetIsFirstSpawnDone())
189 {
190 // Vehicle has been deleted, setup respawn timer if enabled
191 if (respawnTimestamp == 0 && respawnPeriod > 0)
192 {
193 spawnpoint.SetRespawnTimestamp(currentTime.PlusSeconds(respawnPeriod));
194 return;
195 }
196 }
197 }
198
199 vector location = spawnpoint.GetOwner().GetOrigin();
200
201 // Only handle vehicles which are still on their spawnpoints
202 if (spawnedVeh && vector.DistanceXZ(spawnedVeh.GetOrigin(), location) > PARKED_THRESHOLD)
203 {
204 spawnpoint.RemoveInteractionHandlers(spawnedVeh);
205
206 // Non-respawning spawnpoints get depleted once the vehicle leaves the spawnpoint
207 if (spawnpoint.GetRespawnPeriod() <= 0)
208 spawnpoint.SetIsDepleted(true);
209
210 return;
211 }
212
213 bool playersNear = false;
214 bool playersFar = true;
215 int distance;
216
217 // Define if any player is close enough to spawn or if all players are far enough to despawn
218 foreach (IEntity player : m_aPlayers)
219 {
220 if (!player)
221 continue;
222
223 distance = vector.DistanceSq(player.GetOrigin(), location);
224
226 {
227 playersFar = false;
228
230 {
231 playersNear = true;
232 break;
233 }
234 }
235 }
236
237 if (!spawnedVeh && playersNear)
238 {
239 Vehicle vehicle = spawnpoint.SpawnVehicle();
240
241 if (vehicle && m_OnVehicleSpawned)
242 m_OnVehicleSpawned.Invoke(spawnpoint, vehicle);
243
244 return;
245 }
246
247 // Delay is used so dying players don't see the despawn happen
248 if (spawnedVeh && playersFar && spawnpoint.IsDespawnAllowed())
249 {
250 const WorldTimestamp despawnT = spawnpoint.GetDespawnTimestamp();
251 if (despawnT == 0)
252 {
253 spawnpoint.SetDespawnTimestamp(currentTime.PlusMilliseconds(DESPAWN_TIMEOUT));
254 }
255 else if (currentTime.Greater(despawnT))
256 {
257 spawnpoint.DespawnVehicle();
258 }
259 }
260 else
261 {
262 spawnpoint.SetDespawnTimestamp(null);
263 }
264 }
265
266 //------------------------------------------------------------------------------------------------
267 void RegisterSpawnpoint(notnull SCR_AmbientVehicleSpawnPointComponent spawnpoint)
268 {
269 if (!IsEnabled())
270 Enable(true);
271
272 m_aSpawnpoints.Insert(spawnpoint);
274 }
275
276 //------------------------------------------------------------------------------------------------
277 void UnregisterSpawnpoint(notnull SCR_AmbientVehicleSpawnPointComponent spawnpoint)
278 {
279 m_aSpawnpoints.RemoveItem(spawnpoint);
280 m_iIndexToCheck = 0;
281
282 if (!m_aSpawnpoints.IsEmpty())
283 {
285 return;
286 }
287
288 Enable(false);
289 }
290
291 //------------------------------------------------------------------------------------------------
292 int GetSpawnpoints(out notnull array<SCR_AmbientVehicleSpawnPointComponent> spawnpoints)
293 {
294 return spawnpoints.Copy(m_aSpawnpoints);
295 }
296
297 //------------------------------------------------------------------------------------------------
298 void OnPlayerDisconnected(int playerId, KickCauseCode cause = KickCauseCode.NONE, int timeout = -1)
299 {
301 }
302
303 //------------------------------------------------------------------------------------------------
304 void OnPlayerKilled(notnull SCR_InstigatorContextData instigatorContextData)
305 {
307 }
308}
vector location
ArmaReforgerScripted GetGame()
Definition game.c:1398
bool AreGameFlagsSet(EGameFlags checkGameFlags)
Definition game.c:517
EGameFlags
GameMode Game Flags represented by bit mask.
Definition game.c:17
ScriptInvokerBase< OnAmbientVehicleSpawnedDelegate > OnAmbientVehicleSpawnedInvoker
func OnAmbientVehicleSpawnedDelegate
SCR_BaseGameMode GetGameMode()
float distance
proto external Managed FindComponent(typename typeName)
Definition Math.c:13
void OnUpdatePoint(WorldUpdatePointArgs args)
int GetSpawnpoints(out notnull array< SCR_AmbientVehicleSpawnPointComponent > spawnpoints)
void OnPlayerDisconnected(int playerId, KickCauseCode cause=KickCauseCode.NONE, int timeout=-1)
void RegisterSpawnpoint(notnull SCR_AmbientVehicleSpawnPointComponent spawnpoint)
void ProcessSpawnpoint(int spawnpointIndex)
void UnregisterSpawnpoint(notnull SCR_AmbientVehicleSpawnPointComponent spawnpoint)
OnAmbientVehicleSpawnedInvoker GetOnVehicleSpawned()
ref OnAmbientVehicleSpawnedInvoker m_OnVehicleSpawned
static SCR_AmbientVehicleSystem GetInstance()
override event bool ShouldBePaused()
ref array< SCR_AmbientVehicleSpawnPointComponent > m_aSpawnpoints
void OnPlayerKilled(notnull SCR_InstigatorContextData instigatorContextData)
void OnPlayerSpawnedOrDeleted(int playerId, IEntity player)
ScriptInvokerBase< SCR_BaseGameMode_OnPlayerDisconnected > GetOnPlayerDisconnected()
ScriptInvokerBase< SCR_BaseGameMode_PlayerIdAndEntity > GetOnPlayerSpawned()
ScriptInvokerBase< SCR_BaseGameMode_PlayerIdAndEntity > GetOnPlayerDeleted()
ScriptInvokerBase< SCR_BaseGameMode_OnControllableDestroyed > GetOnPlayerKilled()
Definition World.c:16
Structure holding world system meta-information required by the engine.
Structure holding extra data of WorldSystem update point.
enum EPhysicsLayerPresets Vehicle
Definition gameLib.c:24
WorldSystemPoint ESystemPoint
Definition gameLib.c:7
WorldSystemLocation ESystemLocation
Definition gameLib.c:10
int IsEnabled()
Returns true if the light is enabled.