Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_AmbientVehicleSpawnPointComponent.c
Go to the documentation of this file.
4
5class SCR_AmbientVehicleSpawnPointComponent : ScriptComponent
6{
7 [Attribute("0", UIWidgets.EditBox, "How often will the vehicle respawn when destroyed. (seconds, 0 = no respawn)", "0 inf 1")]
8 protected int m_iRespawnPeriod;
9
10 [Attribute("0", desc: "If true, the vehicle will not despawn on spawnpoint if its damage or fuel changed.")]
12
13 [Attribute("0", UIWidgets.ComboBox, "Select Entity Labels which you want to optionally include to random spawn. If you want to spawn everything, you can leave it out empty.", "", ParamEnumArray.FromEnum(EEditableEntityLabel))]
14 protected ref array<EEditableEntityLabel> m_aIncludedEditableEntityLabels;
15
16 [Attribute("0", UIWidgets.ComboBox, "Select Entity Labels which you want to exclude from random spawn.", "", ParamEnumArray.FromEnum(EEditableEntityLabel))]
17 protected ref array<EEditableEntityLabel> m_aExcludedEditableEntityLabels;
18
19 [Attribute("0", desc: "If true, only assets with ALL of provided included labels will be used.")]
21
22 protected static const int SPAWNING_RADIUS = 5; //m, check empty space on a spawnpoint with this radius
23 protected static const int IGNORE_CHANGES_DELAY_MS = 1000; //ms, how long after vehicle spawn should changes to fuel or damage be ignored before despawn is disabled
24
25 protected bool m_bDepleted;
26 protected bool m_bFirstSpawnDone;
27 protected bool m_bSpawnProcessed;
28 protected bool m_bAllowDespawn = true;
29
30 protected float m_fSpawnTimestamp;
31
34
36
37 protected Vehicle m_Vehicle;
38
40
41 //------------------------------------------------------------------------------------------------
44 {
45 return m_iRespawnPeriod;
46 }
47
48 //------------------------------------------------------------------------------------------------
50 void SetIsDepleted(bool depleted)
51 {
52 m_bDepleted = depleted;
53 }
54
55 //------------------------------------------------------------------------------------------------
58 {
59 return m_bDepleted;
60 }
61
62 //------------------------------------------------------------------------------------------------
65 {
66 return m_bFirstSpawnDone;
67 }
68
69 //------------------------------------------------------------------------------------------------
72 {
73 return m_bSpawnProcessed;
74 }
75
76 //------------------------------------------------------------------------------------------------
78 {
79 return m_bAllowDespawn;
80 }
81
82 //------------------------------------------------------------------------------------------------
83 void SetAllowDespawn(bool allow)
84 {
85 m_bAllowDespawn = allow;
86 }
87
88 //------------------------------------------------------------------------------------------------
94
95 //------------------------------------------------------------------------------------------------
101
102 //------------------------------------------------------------------------------------------------
105 {
106 m_fRespawnTimestamp = timestamp;
107 }
108
109 //------------------------------------------------------------------------------------------------
115
116 //------------------------------------------------------------------------------------------------
119 {
120 return m_Vehicle;
121 }
122
123 //------------------------------------------------------------------------------------------------
125 {
126 m_Vehicle = vehicle;
127 m_bSpawnProcessed = true;
128 m_bFirstSpawnDone = true;
129 }
130
131 //------------------------------------------------------------------------------------------------
135 {
137 if (!comp)
138 return null;
139
140 SCR_Faction faction = SCR_Faction.Cast(comp.GetAffiliatedFaction());
141 if (!faction)
142 faction = SCR_Faction.Cast(comp.GetDefaultAffiliatedFaction());
143
144 if (faction != m_SavedFaction || (!faction && m_sPrefab.IsEmpty()))
145 Update(faction);
146
147 if (m_sPrefab.IsEmpty())
148 return null;
149
150 vector pos;
151 const bool spawnEmpty = SCR_WorldTools.FindEmptyTerrainPosition(pos, GetOwner().GetOrigin(), SPAWNING_RADIUS, SPAWNING_RADIUS);
152 if (!spawnEmpty)
153 {
154#ifdef WORKBENCH
155 Print("SCR_AmbientVehicleSpawnPointComponent: FindEmptyTerrainPosition failed at " + GetOwner().GetOrigin().ToString(), LogLevel.WARNING);
156#endif
157
158 // In case this spawnpoint is blocked from the start, don't process it anymore
159 // Prevents unexpected behavior such as vehicles spawning on a spot where a service composition has been built and after a session load dismantled
161 m_bDepleted = true;
162
163 return null;
164 }
165
167 params.TransformMode = ETransformMode.WORLD;
168 GetOwner().GetTransform(params.Transform);
169
170 if (m_Vehicle)
172
174 m_fRespawnTimestamp = null;
175 m_bFirstSpawnDone = true;
176 m_bSpawnProcessed = true;
177
178 if (!m_Vehicle)
179 return null;
180
181 m_fSpawnTimestamp = GetGame().GetWorld().GetWorldTime();
182
183 // Activate handbrake so the vehicles don't go downhill on their own when spawned
185 if (carController)
186 carController.SetPersistentHandBrake(true);
187
188 // Snap to terrain
189 Physics physicsComponent = m_Vehicle.GetPhysics();
190 if (physicsComponent)
191 physicsComponent.SetVelocity("0 -1 0");
192
193 EventHandlerManagerComponent handler = EventHandlerManagerComponent.Cast(m_Vehicle.FindComponent(EventHandlerManagerComponent));
194 if (handler)
195 handler.RegisterScriptHandler("OnDestroyed", this, OnVehicleDestroyed);
196
199
200 return m_Vehicle;
201 }
202
203 //------------------------------------------------------------------------------------------------
205 protected void OnVehicleDestroyed(IEntity vehicle)
206 {
207 m_Vehicle = null;
208 m_bAllowDespawn = true;
209
210 if (m_iRespawnPeriod > 0)
211 {
212 ChimeraWorld world = GetOwner().GetWorld();
213 m_fRespawnTimestamp = world.GetServerTimestamp().PlusSeconds(m_iRespawnPeriod);
214 }
215 else
216 m_bDepleted = true;
217 }
218
219 //------------------------------------------------------------------------------------------------
220 protected void AddInteractionHandlers(notnull IEntity vehicle)
221 {
222 SCR_DamageManagerComponent damageManager = SCR_DamageManagerComponent.GetDamageManager(vehicle);
223
224 if (damageManager)
225 {
226 array<HitZone> hitZones = {};
227 damageManager.GetAllHitZonesInHierarchy(hitZones);
228 SCR_HitZone hitZoneCast;
229
230 foreach (HitZone hitZone : hitZones)
231 {
232 hitZoneCast = SCR_HitZone.Cast(hitZone);
233
234 if (!hitZoneCast)
235 continue;
236
237 hitZoneCast.GetOnDamageStateChanged().Insert(OnDamageChanged);
238 }
239 }
240
241 SCR_FuelManagerComponent fuelManager = SCR_FuelManagerComponent.Cast(vehicle.FindComponent(SCR_FuelManagerComponent));
242
243 if (fuelManager)
244 fuelManager.GetOnFuelChanged().Insert(OnFuelChanged);
245 }
246
247 //------------------------------------------------------------------------------------------------
248 void RemoveInteractionHandlers(notnull IEntity vehicle)
249 {
250 SCR_DamageManagerComponent damageManager = SCR_DamageManagerComponent.GetDamageManager(vehicle);
251 if (damageManager)
252 {
253 array<HitZone> hitZones = {};
254 damageManager.GetAllHitZonesInHierarchy(hitZones);
255 SCR_HitZone hitZoneCast;
256
257 foreach (HitZone hitZone : hitZones)
258 {
259 hitZoneCast = SCR_HitZone.Cast(hitZone);
260
261 if (!hitZoneCast)
262 continue;
263
264 hitZoneCast.GetOnDamageStateChanged().Remove(OnDamageChanged);
265 }
266 }
267
268 SCR_FuelManagerComponent fuelManager = SCR_FuelManagerComponent.Cast(vehicle.FindComponent(SCR_FuelManagerComponent));
269 if (fuelManager)
270 fuelManager.GetOnFuelChanged().Remove(OnFuelChanged);
271 }
272
273 //------------------------------------------------------------------------------------------------
276 {
277 m_fDespawnTimestamp = null;
278 m_bSpawnProcessed = false;
279 RplComponent.DeleteRplEntity(m_Vehicle, false);
280 }
281
282 //------------------------------------------------------------------------------------------------
283 void OnFuelChanged(float newFuel)
284 {
285 // Give it some time in case there are some operations right after spawn
286 if (m_bAllowDespawn && GetGame().GetWorld().GetWorldTime() > m_fSpawnTimestamp + IGNORE_CHANGES_DELAY_MS)
287 {
288 m_bAllowDespawn = false;
289
290 if (m_Vehicle)
292 }
293 }
294
295 //------------------------------------------------------------------------------------------------
297 {
298 // Give it some time in case there are some operations right after spawn
299 if (m_bAllowDespawn && GetGame().GetWorld().GetWorldTime() > m_fSpawnTimestamp + IGNORE_CHANGES_DELAY_MS)
300 {
301 m_bAllowDespawn = false;
302
303 if (m_Vehicle)
305 }
306 }
307
308 //------------------------------------------------------------------------------------------------
309 protected void Update(SCR_Faction faction)
310 {
311 m_SavedFaction = faction;
312 SCR_EntityCatalog entityCatalog;
313
314 if (faction)
315 {
316 entityCatalog = faction.GetFactionEntityCatalogOfType(EEntityCatalogType.VEHICLE);
317 }
318 else
319 {
320 SCR_EntityCatalogManagerComponent comp = SCR_EntityCatalogManagerComponent.GetInstance();
321
322 if (!comp)
323 return;
324
325 entityCatalog = comp.GetEntityCatalogOfType(EEntityCatalogType.VEHICLE);
326 }
327
328 if (!entityCatalog)
329 return;
330
331 array<SCR_EntityCatalogEntry> data = {};
333
334 if (data.IsEmpty())
335 return;
336
337 m_sPrefab = (data.GetRandomElement().GetPrefab());
338 }
339
340 //------------------------------------------------------------------------------------------------
341 override void OnPostInit(IEntity owner)
342 {
344
345 if (!factionComponent)
346 {
347 Print("SCR_AmbientVehicleSpawnPointComponent: SCR_FactionAffiliationComponent not found on owner entity. Vehicle spawning will not be available.", LogLevel.WARNING);
348 return;
349 }
350
352 if (manager)
353 manager.RegisterSpawnpoint(this);
354 }
355
356 //------------------------------------------------------------------------------------------------
357 override void OnDelete(IEntity owner)
358 {
359 if (m_Vehicle)
360 {
361 EventHandlerManagerComponent handler = EventHandlerManagerComponent.Cast(m_Vehicle.FindComponent(EventHandlerManagerComponent));
362 if (handler)
363 handler.RemoveScriptHandler("OnDestroyed", this, OnVehicleDestroyed);
364
367 }
368
370 if (manager)
371 manager.UnregisterSpawnpoint(this);
372 }
373}
EEditableEntityLabel
EEntityCatalogType
IEntity SpawnEntityPrefabEx(ResourceName prefab, bool randomizeEditableVariant, BaseWorld world=null, EntitySpawnParams params=null)
Definition game.c:90
ArmaReforgerScripted GetGame()
Definition game.c:1398
vector GetOrigin()
enum EAICompartmentType m_Vehicle
WorldTimestamp GetDespawnTimestamp()
void SetDespawnTimestamp(WorldTimestamp time)
void SetRespawnTimestamp(WorldTimestamp timestamp)
WorldTimestamp GetRespawnTimestamp()
ref array< EEditableEntityLabel > m_aExcludedEditableEntityLabels
void SetIsDepleted(bool depleted)
void OnDamageChanged(SCR_HitZone hitZone)
void RemoveInteractionHandlers(notnull IEntity vehicle)
WorldTimestamp m_fDespawnTimestamp
void SetAllowDespawn(bool allow)
void AddInteractionHandlers(notnull IEntity vehicle)
void SetSpawnedVehicle(Vehicle vehicle)
ref array< EEditableEntityLabel > m_aIncludedEditableEntityLabels
WorldTimestamp m_fRespawnTimestamp
void OnVehicleDestroyed(IEntity vehicle)
Get all prefabs that have the spawner data
void SCR_FuelManagerComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
void OnFuelChanged(float newFuel)
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
proto external GenericComponent FindComponent(typename typeName)
proto external Managed FindComponent(typename typeName)
proto external BaseWorld GetWorld()
proto external void GetTransform(out vector mat[])
void RegisterSpawnpoint(notnull SCR_AmbientVehicleSpawnPointComponent spawnpoint)
void UnregisterSpawnpoint(notnull SCR_AmbientVehicleSpawnPointComponent spawnpoint)
static SCR_AmbientVehicleSystem GetInstance()
Get list of entities that all contain all any the included labels and NONE exclude labels Ignores Disabled Entries as well Sometimes it is quicker to get the if false any needs to be true return List size *int GetFullFilteredEntityListWithLabels(notnull out array< SCR_EntityCatalogEntry > filteredEntityList, array< EEditableEntityLabel > includedLabels=null, array< EEditableEntityLabel > excludedLabels=null, bool needsAllIncluded=true)
SCR_EntityCatalog GetFactionEntityCatalogOfType(EEntityCatalogType catalogType, bool printNotFound=true)
ScriptInvoker GetOnDamageStateChanged(bool createNew=true)
Definition SCR_HitZone.c:32
proto external GenericEntity GetOwner()
Get owner entity.
enum EPhysicsLayerPresets Vehicle
Definition gameLib.c:24
void EntitySpawnParams()
Definition gameLib.c:130
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
SCR_FieldOfViewSettings Attribute
proto external string ToString()
Plain C++ pointer, no weak pointers, no memory management.