Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_EditableVehicleComponent.c
Go to the documentation of this file.
1[ComponentEditorProps(category: "GameScripted/Editor (Editables)", description: "", icon: "WBData/ComponentEditorProps/componentEditor.png")]
5
7
9class SCR_EditableVehicleComponent : SCR_EditableEntityComponent
10{
11 protected SCR_BaseCompartmentManagerComponent m_CompartmentManager;
14
15 protected int m_iPlayersInVehicle;
16
17 //------------------------------------------------------------------------------------------------
20 {
22 }
23
24 //------------------------------------------------------------------------------------------------
25 protected void OnFactionUpdate(FactionAffiliationComponent owner, Faction previousFaction, Faction newFaction)
26 {
27 if (m_OnUIRefresh)
28 m_OnUIRefresh.Invoke();
29 }
30
31 //------------------------------------------------------------------------------------------------
32 protected void OnDestroyed(IEntity owner)
33 {
34 if (m_OnUIRefresh)
35 m_OnUIRefresh.Invoke();
36 }
37
38 //------------------------------------------------------------------------------------------------
40 {
41 //--- Destroyed entities have no faction
42 if (IsDestroyed())
43 return null;
44
46 return m_VehicleFactionAffiliation.GetAffiliatedFaction();
47
48 return null;
49 }
50
51 //------------------------------------------------------------------------------------------------
53 {
54 if (!m_OnUIRefresh)
56
57 return m_OnUIRefresh;
58 }
59
60 //------------------------------------------------------------------------------------------------
62 {
64 return null;
65
67 array<BaseCompartmentSlot> compartments = {};
68 for (int i = 0, count = m_CompartmentManager.GetCompartments(compartments); i < count; i++)
69 {
70 occupant = SCR_EditableEntityComponent.GetEditableEntity(compartments[i].GetOccupant());
71 if (occupant)
72 return occupant.GetAIGroup();
73 }
74 return null;
75 }
76
77 //------------------------------------------------------------------------------------------------
79 {
81 return null;
82
84 array<BaseCompartmentSlot> compartments = {};
85 for (int i = 0, count = m_CompartmentManager.GetCompartments(compartments); i < count; i++)
86 {
87 occupant = SCR_EditableEntityComponent.GetEditableEntity(compartments[i].GetOccupant());
88 if (occupant)
89 return occupant;
90 }
91 return null;
92 }
93
94 //------------------------------------------------------------------------------------------------
95 override int GetPlayerID()
96 {
98 return 0;
99
100 PlayerManager playerManager = GetGame().GetPlayerManager();
101
103 array<BaseCompartmentSlot> compartments = {};
104 int playerIdToReturn = 0;
105 for (int i = 0, count = m_CompartmentManager.GetCompartments(compartments); i < count; i++)
106 {
107 BaseCompartmentSlot compartment = compartments[i];
108 occupant = SCR_EditableEntityComponent.GetEditableEntity(compartment.GetOccupant());
109 if (occupant)
110 {
111 int playerId = occupant.GetPlayerID();
112 if (playerId <= 0)
113 continue;
114
115 if (playerIdToReturn == 0)
116 playerIdToReturn = playerId;
117 else if (compartment.GetType() == ECompartmentType.PILOT)
118 return playerId;
119 }
120 }
121
122 return playerIdToReturn;
123 }
124
125 //------------------------------------------------------------------------------------------------
128 {
130 return;
131
133 array<BaseCompartmentSlot> compartments = {};
134 PlayerManager playerManager = GetGame().GetPlayerManager();
135
136 int playerCount = 0;
137 for (int i = 0, count = m_CompartmentManager.GetCompartments(compartments); i < count; i++)
138 {
139 occupant = SCR_EditableEntityComponent.GetEditableEntity(compartments[i].GetOccupant());
140 if (occupant)
141 {
142 int playerId = playerManager.GetPlayerIdFromControlledEntity(occupant.GetOwner());
143 if (playerId != 0)
144 playerCount++;
145 }
146 }
147
148 m_iPlayersInVehicle = playerCount;
149 if (m_OnUIRefresh)
150 m_OnUIRefresh.Invoke();
151 }
152
153 //------------------------------------------------------------------------------------------------
154 override int GetCrew(out notnull array<CompartmentAccessComponent> crewCompartmentAccess, bool ignorePlayers = true)
155 {
156 SCR_BaseCompartmentManagerComponent compartmentManager = SCR_BaseCompartmentManagerComponent.Cast(GetOwner().FindComponent(SCR_BaseCompartmentManagerComponent));
157 if (!compartmentManager)
158 return 0;
159
160 PlayerManager playerManager;
161
162 //If Ignore players
163 if (ignorePlayers)
164 playerManager = GetGame().GetPlayerManager();
165
166 array<IEntity> occupants = {};
167 compartmentManager.GetOccupants(occupants);
168 CompartmentAccessComponent compartmentAccess;
169
170 ChimeraCharacter character;
171 foreach (IEntity occupant : occupants)
172 {
173 character = ChimeraCharacter.Cast(occupant);
174 if (!character)
175 continue;
176
177 if (ignorePlayers && playerManager.GetPlayerIdFromControlledEntity(character) > 0)
178 continue;
179
180 compartmentAccess = character.GetCompartmentAccessComponent();
181 if (compartmentAccess && compartmentAccess.IsInCompartment())
182 crewCompartmentAccess.Insert(compartmentAccess);
183 }
184
185 return crewCompartmentAccess.Count();
186 }
187
188 //------------------------------------------------------------------------------------------------
191 {
192 SCR_BaseCompartmentManagerComponent compartmentManager = SCR_BaseCompartmentManagerComponent.Cast(GetOwner().FindComponent(SCR_BaseCompartmentManagerComponent));
193 if (!compartmentManager)
194 return;
195
196 array<IEntity> occupants = {};
197 SCR_EditableCharacterComponent editableCharacter;
198 compartmentManager.GetOccupants(occupants);
199
200 foreach (IEntity occupant : occupants)
201 {
202 editableCharacter = SCR_EditableCharacterComponent.Cast(occupant.FindComponent(SCR_EditableCharacterComponent));
203 if (editableCharacter)
204 editableCharacter.PlayerTeleportedByParentFeedback(true);
205 }
206 }
207
208 //------------------------------------------------------------------------------------------------
209 override bool SetTransform(vector transform[4], bool changedByUser = false)
210 {
211 if (!super.SetTransform(transform, changedByUser))
212 return false;
213
214 //~ Add feedback to players that they are teleported when inside of the vehicle
216 return true;
217 }
218
219 //--------------------------------------------------- Spawn occupants ---------------------------------------------------\\
220
221 //------------------------------------------------------------------------------------------------
231 bool CanOccupyVehicleWithCharacters(array<ECompartmentType> compartmentTypes, bool checkHasDefaultOccupantsData, int playerID, bool checkEditorBudget = true, bool checkOccupyingFaction = true, bool checkForFreeCompartments = true, bool checkForFreeDefaultCompartments = false)
232 {
233 FactionKey factionKey = string.Empty;
234
236 return false;
237
238 if (checkOccupyingFaction)
239 {
241 if (uiInfo)
242 factionKey = uiInfo.GetFactionKey();
243 }
244
245 bool hasEnoughBudgetForDefaultCompartments;
246 bool hasFreeDefaultCompartments;
247
248 if (checkEditorBudget || checkForFreeDefaultCompartments)
249 hasEnoughBudgetForDefaultCompartments = HasEnoughBudgetForDefaultOccupants(compartmentTypes, playerID, EEditorMode.EDIT, hasFreeDefaultCompartments);
250
251 if (checkEditorBudget && !hasEnoughBudgetForDefaultCompartments)
252 return false;
253
254 if (checkForFreeDefaultCompartments && !hasFreeDefaultCompartments)
255 return false;
256
257 return m_CompartmentManager.CanOccupy(compartmentTypes, checkHasDefaultOccupantsData, factionKey, checkOccupyingFaction, checkForFreeCompartments);
258 }
259
260 //------------------------------------------------------------------------------------------------
261 //~ Check if there is enough budget to spawn default occupants
262 protected bool HasEnoughBudgetForDefaultOccupants(array<ECompartmentType> compartmentTypes, int playerID, EEditorMode editorMode, out bool noFreeDefaultCompartments)
263 {
264 noFreeDefaultCompartments = true;
265
266 array<BaseCompartmentSlot> compartments = {};
267
268 //~ Get all free compartments of given types
269 foreach (ECompartmentType compartmentType : compartmentTypes)
270 {
271 m_CompartmentManager.GetFreeCompartmentsOfType(compartments, compartmentType);
272 }
273
274 array<ResourceName> occupantsToSpawn = {};
275 ResourceName occupant;
276
277 foreach (BaseCompartmentSlot compartment : compartments)
278 {
279 occupant = compartment.GetDefaultOccupantPrefab();
280 if (!occupant.IsEmpty())
281 occupantsToSpawn.Insert(occupant);
282 }
283
284 //~ Is empty so it has enough budget but will also return that there are no free seats
285 if (occupantsToSpawn.IsEmpty())
286 {
287 noFreeDefaultCompartments = false;
288 return true;
289 }
290
291 SCR_EditorManagerEntity editorManager;
292
293 //~ Get EditorManager from given player
294 if (playerID > 0)
295 {
297 if (!editorCore)
298 return false;
299
300 editorManager = editorCore.GetEditorManager(playerID);
301 }
302 //~ Get Editor manager from local player
303 else
304 {
305 editorManager = SCR_EditorManagerEntity.GetInstance();
306 }
307
308 if (!editorManager)
309 return false;
310
311 SCR_EditorModeEntity modeEntity = editorManager.FindModeEntity(editorMode);
312 if (!modeEntity)
313 return false;
314
315 SCR_PlacingEditorComponent placingComponent;
316
317 placingComponent = SCR_PlacingEditorComponent.Cast(modeEntity.FindComponent(SCR_PlacingEditorComponent));
318 return placingComponent.IsThereEnoughBudgetToSpawnVehicleOccupants(occupantsToSpawn);
319 }
320
321 //------------------------------------------------------------------------------------------------
324 void OccupyVehicleWithDefaultCharacters(notnull array<ECompartmentType> compartmentTypes)
325 {
326 m_CompartmentManager.SpawnDefaultOccupants(compartmentTypes);
327 }
328
329 //------------------------------------------------------------------------------------------------
332 SCR_BaseCompartmentManagerComponent GetCompartmentManager()
333 {
335 }
336
337 //------------------------------------------------------------------------------------------------
339 {
340 //~ No vehicle placing flag
341 if (!SCR_Enum.HasPartialFlag(flags, EEditorPlacingFlags.VEHICLE_CREWED | EEditorPlacingFlags.VEHICLE_PASSENGER))
342 return this;
343
344 //~ Todo: Check if EEditorPlacingFlags.CHARACTER_PLAYER and spawn the first character as player. If Only EEditorPlacingFlags.CHARACTER_PLAYER add itself as pilot (or any other availible slots)
345
347 array<ECompartmentType> compartmentsToFill = {};
348
349 bool hasFreeSeats;
350
351 //~ Check crew budget
352 if (SCR_Enum.HasFlag(flags, EEditorPlacingFlags.VEHICLE_CREWED) && HasEnoughBudgetForDefaultOccupants(SCR_BaseCompartmentManagerComponent.CREW_COMPARTMENT_TYPES, playerID, EEditorMode.EDIT, hasFreeSeats))
353 {
354 //~ Occupy with Crew
355 if (!uiInfo || !uiInfo.GetEditorPlaceAsOneGroup())
356 OccupyVehicleWithDefaultCharacters(SCR_BaseCompartmentManagerComponent.CREW_COMPARTMENT_TYPES);
357 else
358 compartmentsToFill.InsertAll(SCR_BaseCompartmentManagerComponent.CREW_COMPARTMENT_TYPES);
359 }
360
361 //~ Check passenger budget
362 if (SCR_Enum.HasFlag(flags, EEditorPlacingFlags.VEHICLE_PASSENGER) && HasEnoughBudgetForDefaultOccupants(SCR_BaseCompartmentManagerComponent.PASSENGER_COMPARTMENT_TYPES, playerID, EEditorMode.EDIT, hasFreeSeats))
363 {
364 //~ Occupy with Passengers
365 if (!uiInfo || !uiInfo.GetEditorPlaceAsOneGroup())
366 OccupyVehicleWithDefaultCharacters(SCR_BaseCompartmentManagerComponent.PASSENGER_COMPARTMENT_TYPES);
367 else
368 compartmentsToFill.InsertAll(SCR_BaseCompartmentManagerComponent.PASSENGER_COMPARTMENT_TYPES);
369 }
370
371 //~ Occupy vehicle with both Crew and Passengers in one group (If both placing flags where selected)
372 if (!compartmentsToFill.IsEmpty())
373 OccupyVehicleWithDefaultCharacters(compartmentsToFill);
374
375 return this;
376 }
377
378 //------------------------------------------------------------------------------------------------
379 override void EOnPhysicsActive(IEntity owner, bool activeState)
380 {
381 //--- Move to root when the vehicle is activated
382 if (activeState)
383 SetParentEntity(null);
384 //ClearEventMask(m_Owner, EntityEvent.PHYSICSACTIVE);
385 }
386
387 //------------------------------------------------------------------------------------------------
388 override void OnPostInit(IEntity owner)
389 {
390 if (DiagMenu.GetValue(SCR_DebugMenuID.DEBUGUI_EDITOR_ENTITIES_DISABLE))
391 return;
392
393 super.OnPostInit(owner);
394
395 //SetEventMask(owner, EntityEvent.PHYSICSACTIVE);
396
397 m_CompartmentManager = SCR_BaseCompartmentManagerComponent.Cast(owner.FindComponent(SCR_BaseCompartmentManagerComponent));
400 m_VehicleFactionAffiliation.GetOnFactionChanged().Insert(OnFactionUpdate);
401
402 EventHandlerManagerComponent eventHandlerManager = EventHandlerManagerComponent.Cast(owner.FindComponent(EventHandlerManagerComponent));
403 if (eventHandlerManager)
404 eventHandlerManager.RegisterScriptHandler("OnDestroyed", owner, OnDestroyed);
405 }
406
407 //------------------------------------------------------------------------------------------------
408 // destructor
410 {
412 m_VehicleFactionAffiliation.GetOnFactionChanged().Remove(OnFactionUpdate);
413 }
414}
SCR_EAIThreatSectorFlags flags
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition DebugMenuID.c:4
ECompartmentType
EEditorPlacingFlags
ArmaReforgerScripted GetGame()
Definition game.c:1398
enum EAIGroupCombatMode ComponentEditorProps(category:"GameScripted/AI", description:"Component for utility AI system for groups")
IEntity GetOccupant(IEntity slot)
ref ScriptInvoker m_OnUIRefresh
bool HasEnoughBudgetForDefaultOccupants(array< ECompartmentType > compartmentTypes, int playerID, EEditorMode editorMode, out bool noFreeDefaultCompartments)
void OccupyVehicleWithDefaultCharacters(notnull array< ECompartmentType > compartmentTypes)
SCR_BaseCompartmentManagerComponent GetCompartmentManager()
void OnDestroyed(IEntity owner)
bool CanOccupyVehicleWithCharacters(array< ECompartmentType > compartmentTypes, bool checkHasDefaultOccupantsData, int playerID, bool checkEditorBudget=true, bool checkOccupyingFaction=true, bool checkForFreeCompartments=true, bool checkForFreeDefaultCompartments=false)
void ~SCR_EditableVehicleComponent()
SCR_VehicleFactionAffiliationComponent m_VehicleFactionAffiliation
void OnFactionUpdate(FactionAffiliationComponent owner, Faction previousFaction, Faction newFaction)
void UpdatePlayerCountInVehicle()
void PlayerTeleportedFeedback()
Add feedback to players that they are teleported when inside of the vehicle.
int GetPlayerCountInVehicle()
void SCR_EditorManagerEntity(IEntitySource src, IEntity parent)
override SCR_UIInfo GetInfo()
SCR_BaseCompartmentManagerComponent m_CompartmentManager
Diagnostic and developer menu system.
Definition DiagMenu.c:18
proto external GenericComponent FindComponent(typename typeName)
proto external Managed FindComponent(typename typeName)
bool SetTransform(vector transform[4], bool changedByUser=false)
SCR_EditableEntityComponent EOnEditorPlace(out SCR_EditableEntityComponent parent, SCR_EditableEntityComponent recipient, EEditorPlacingFlags flags, bool isQueue, int playerID=0)
SCR_EditableEntityComponent GetAIEntity()
static SCR_EditableEntityComponent GetEditableEntity(IEntity owner)
SCR_EditableEntityComponent SetParentEntity(SCR_EditableEntityComponent parentEntity, bool changedByUser=false)
int GetCrew(out notnull array< CompartmentAccessComponent > crewCompartmentAccess, bool ignorePlayers=true)
SCR_EditableEntityComponent GetAIGroup()
Core component to manage SCR_EditorManagerEntity.
SCR_EditorManagerEntity GetEditorManager()
bool IsThereEnoughBudgetToSpawnVehicleOccupants(array< ResourceName > resources)
void EOnPhysicsActive(IEntity owner, bool activeState)
proto external GenericEntity GetOwner()
Get owner entity.
EEditorMode
Editor mode that defines overall functionality.
Definition EEditorMode.c:6
ScriptInvokerBase< func > ScriptInvoker
Definition tools.c:134