Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_CampaignBuildingDisassemblyUserAction.c
Go to the documentation of this file.
2 {
3  protected SCR_CampaignBuildingCompositionComponent m_CompositionComponent;
4  protected SCR_EditableEntityComponent m_EditableEntity;
5  protected SCR_EditorManagerEntity m_EditorManager;
6  protected FactionAffiliationComponent m_FactionComponent;
7  protected ref array<SCR_EditableVehicleComponent> m_EditableVehicle = {};
8  protected bool m_bCompositionSpawned;
9  protected bool m_bTurretCollected;
10  // Temporary solution how to prevent disassembly of HQ in Conflict.
11  protected bool m_DoNotDisassemble;
12  protected IEntity m_RootEntity;
13  protected IEntity m_User;
14 
15  protected SCR_GadgetManagerComponent m_GadgetManager;
16 
17  //------------------------------------------------------------------------------------------------
18  protected override void Init(IEntity pOwnerEntity, GenericComponent pManagerComponent)
19  {
20  m_RootEntity = pOwnerEntity.GetRootParent();
21 
22  m_CompositionComponent = SCR_CampaignBuildingCompositionComponent.Cast(m_RootEntity.FindComponent(SCR_CampaignBuildingCompositionComponent));
23  m_EditableEntity = SCR_EditableEntityComponent.Cast(m_RootEntity.FindComponent(SCR_EditableEntityComponent));
24 
25  SetEditorManager();
26 
27  if (m_CompositionComponent && GetOwner() == GetOwner().GetRootParent())
28  {
29  m_CompositionComponent.GetOnCompositionSpawned().Insert(OnCompositionSpawned);
30 
31  BaseGameMode gameMode = GetGame().GetGameMode();
32  if (!gameMode)
33  return;
34 
35  SCR_CampaignBuildingManagerComponent buildingManagerComponent = SCR_CampaignBuildingManagerComponent.Cast(gameMode.FindComponent(SCR_CampaignBuildingManagerComponent));
36  if (!buildingManagerComponent)
37  return;
38 
39  if (buildingManagerComponent.CanDisassembleSameFactionOnly())
40  m_CompositionComponent.GetOnBuilderSet().Insert(CacheFactionAffiliationComponent);
41  }
42 
43  // Temporary solution how to prevent disassembly of HQ in Conflict.
44  SCR_GameModeCampaign campaignGameMode = SCR_GameModeCampaign.Cast(GetGame().GetGameMode());
45  if (!campaignGameMode || !m_EditableEntity)
46  return;
47 
48  SCR_EditableEntityUIInfo editableEntityUIInfo = SCR_EditableEntityUIInfo.Cast(m_EditableEntity.GetInfo(GetOwner()));
49  if (!editableEntityUIInfo)
50  return;
51 
52  array<EEditableEntityLabel> entityLabels = {};
53  editableEntityUIInfo.GetEntityLabels(entityLabels);
54  if (entityLabels.Contains(EEditableEntityLabel.SERVICE_HQ))
55  m_DoNotDisassemble = true;
56  }
57 
58  //------------------------------------------------------------------------------------------------
59  override void OnActionStart(IEntity pUserEntity)
60  {
61  ChimeraCharacter character = ChimeraCharacter.Cast(pUserEntity);
62  if (!character)
63  return;
64 
65  CharacterControllerComponent charController = character.GetCharacterController();
66  if (charController)
67  {
68 
69  CharacterAnimationComponent pAnimationComponent = charController.GetAnimationComponent();
70  int itemActionId = pAnimationComponent.BindCommand("CMD_Item_Action");
71 
73  params.SetEntity(GetBuildingTool(pUserEntity));
74  params.SetAllowMovementDuringAction(false);
75  params.SetKeepInHandAfterSuccess(true);
76  params.SetCommandID(itemActionId);
77  params.SetCommandIntArg(2);
78 
79  charController.TryUseItemOverrideParams(params);
80  }
81 
82  m_User = pUserEntity;
83 
84  super.OnActionStart(pUserEntity);
85  }
86 
87  //------------------------------------------------------------------------------------------------
88  override void OnActionCanceled(IEntity pOwnerEntity, IEntity pUserEntity)
89  {
90  m_User = null;
91 
92  ChimeraCharacter character = ChimeraCharacter.Cast(pUserEntity);
93  if (!character)
94  return;
95 
96  CharacterControllerComponent charController = character.GetCharacterController();
97  if (charController)
98  {
99  CharacterAnimationComponent pAnimationComponent = charController.GetAnimationComponent();
100  int itemActionId = pAnimationComponent.BindCommand("CMD_Item_Action");
101  CharacterCommandHandlerComponent cmdHandler = CharacterCommandHandlerComponent.Cast(pAnimationComponent.GetCommandHandler());
102  if (cmdHandler)
103  cmdHandler.FinishItemUse();
104  }
105  }
106 
107  //------------------------------------------------------------------------------------------------
108  void CancelPlayerAnimation(notnull IEntity entity)
109  {
110  ChimeraCharacter character = ChimeraCharacter.Cast(entity);
111  if (!character)
112  return;
113 
114  CharacterControllerComponent charController = character.GetCharacterController();
115  if (charController)
116  {
117  CharacterAnimationComponent pAnimationComponent = charController.GetAnimationComponent();
118  CharacterCommandHandlerComponent cmdHandler = CharacterCommandHandlerComponent.Cast(pAnimationComponent.GetCommandHandler());
119  cmdHandler.FinishItemUse();
120  }
121  }
122 
123  //------------------------------------------------------------------------------------------------
124  override void PerformAction(IEntity pOwnerEntity, IEntity pUserEntity)
125  {
126  SCR_CampaignBuildingNetworkComponent networkComponent = GetNetworkManager();
127  if (!networkComponent || !m_RootEntity)
128  return;
129 
130  if (HasCompositionService())
131  TryToSendNotification(pOwnerEntity, pUserEntity, networkComponent);
132 
133  networkComponent.DeleteCompositionByUserAction(m_RootEntity);
134  }
135 
136  //------------------------------------------------------------------------------------------------
137  void TryToSendNotification(IEntity pOwnerEntity, IEntity pUserEntity, notnull SCR_CampaignBuildingNetworkComponent networkComponent)
138  {
139  if (!m_CompositionComponent)
140  return;
141 
142  int playerId = GetGame().GetPlayerManager().GetPlayerIdFromControlledEntity(pUserEntity);
143 
144  IEntity provider = m_CompositionComponent.GetProviderEntity();
145  if (!provider)
146  return;
147 
148  SCR_CampaignBuildingProviderComponent providerComponent = SCR_CampaignBuildingProviderComponent.Cast(provider.FindComponent(SCR_CampaignBuildingProviderComponent));
149  if (!providerComponent)
150  return;
151 
152  array<SCR_MilitaryBaseComponent> bases = {};
153  providerComponent.GetBases(bases);
154  if (bases.IsEmpty())
155  return;
156 
157  int callsign = bases[0].GetCallsign();
158  if (callsign == SCR_MilitaryBaseComponent.INVALID_BASE_CALLSIGN)
159  return;
160 
161  Faction baseFaction = bases[0].GetFaction();
162  Faction playerFaction = SCR_FactionManager.SGetPlayerFaction(playerId);
163  if (playerFaction != baseFaction)
164  return;
165 
166  networkComponent.SendDeleteNotification(m_EditableEntity.GetOwner(), playerId, callsign);
167  }
168 
169  //------------------------------------------------------------------------------------------------
170  // Check if the editable entity component has a service trait set.
171  bool HasCompositionService()
172  {
173  if (!m_EditableEntity)
174  return false;
175 
176  SCR_EditableEntityUIInfo editableEntityUIInfo = SCR_EditableEntityUIInfo.Cast(m_EditableEntity.GetInfo());
177  return editableEntityUIInfo && editableEntityUIInfo.HasEntityLabel(EEditableEntityLabel.TRAIT_SERVICE);
178  }
179 
180  //------------------------------------------------------------------------------------------------
181  // The user action is shown when the preview is visible - means player has a building tool.
182  override bool CanBeShownScript(IEntity user)
183  {
184  if (m_DoNotDisassemble)
185  return false;
186 
187  if (!IsPlayerFactionSame(user))
188  return false;
189 
190  if (!m_GadgetManager)
191  {
192  m_GadgetManager = SCR_GadgetManagerComponent.GetGadgetManager(user);
193 
195  if (playerController)
196  playerController.m_OnControlledEntityChanged.Insert(SetNewGadgetManager);
197 
198  return false;
199  }
200 
201  if (!SCR_CampaignBuildingGadgetToolComponent.Cast(m_GadgetManager.GetHeldGadgetComponent()))
202  return false;
203 
204  // The user action is on entity with composition component, show it if the composition is spawned.
205  if (GetOwner() == GetOwner().GetRootParent())
206  return m_bCompositionSpawned;
207 
208  return GetOwner().FindComponent(SCR_CampaignBuildingLayoutComponent);
209  }
210 
211  //------------------------------------------------------------------------------------------------
213  void SetNewGadgetManager(IEntity from, IEntity to)
214  {
215  m_GadgetManager = SCR_GadgetManagerComponent.GetGadgetManager(to);
216  }
217 
218  //------------------------------------------------------------------------------------------------
219  override bool CanBePerformedScript(IEntity user)
220  {
221  if (m_bCompositionSpawned && !m_bTurretCollected)
222  GetAllTurretsInComposition();
223 
224  // If the editor manager doesn't exists, try to get one and set as for an example when connecting to a server with build compositions, the editor manager doesn't exist when the user action inicialized.
225  if (!m_EditorManager)
226  SetEditorManager();
227 
228  if (!m_EditorManager || m_EditorManager.IsOpened())
229  return false;
230 
231  array<CompartmentAccessComponent> crewCompartmentAccess = {};
232 
233  foreach (SCR_EditableVehicleComponent editableVehicle : m_EditableVehicle)
234  {
235  if (editableVehicle.GetCrew(crewCompartmentAccess, false) != 0)
236  return false;
237  }
238 
239  SCR_GadgetManagerComponent gadgetManager = SCR_GadgetManagerComponent.GetGadgetManager(user);
240  if (!gadgetManager)
241  return false;
242 
243  SCR_GadgetComponent gadgetComponent = gadgetManager.GetHeldGadgetComponent();
244  if (!gadgetComponent)
245  return false;
246 
247  return (gadgetComponent.GetMode() == EGadgetMode.IN_HAND);
248  }
249 
250  //------------------------------------------------------------------------------------------------
251  override bool HasLocalEffectOnlyScript()
252  {
253  return true;
254  }
255 
256  //------------------------------------------------------------------------------------------------
257  void SetEditorManager()
258  {
260  if (!core)
261  return;
262 
263  m_EditorManager = core.GetEditorManager();
264  }
265 
266  //------------------------------------------------------------------------------------------------
268  void GetAllTurretsInComposition()
269  {
270  m_bTurretCollected = true;
271  set<SCR_EditableEntityComponent> editableEntities = new set<SCR_EditableEntityComponent>();
272  m_EditableEntity.GetChildren(editableEntities);
273 
274  foreach (SCR_EditableEntityComponent ent : editableEntities)
275  {
276  SCR_EditableVehicleComponent editableVehicle = SCR_EditableVehicleComponent.Cast(ent);
277  if (editableVehicle)
278  m_EditableVehicle.Insert(editableVehicle);
279  }
280  }
281 
282  //------------------------------------------------------------------------------------------------
284  void OnCompositionSpawned(bool compositionSpawned)
285  {
286  m_bCompositionSpawned = compositionSpawned;
287  if (m_CompositionComponent)
288  m_CompositionComponent.GetOnCompositionSpawned().Remove(OnCompositionSpawned);
289  }
290 
291  //------------------------------------------------------------------------------------------------
293  void CacheFactionAffiliationComponent()
294  {
295  IEntity provider = m_CompositionComponent.GetProviderEntity();
296  if (!provider)
297  return;
298 
299  m_FactionComponent = FactionAffiliationComponent.Cast(provider.FindComponent(FactionAffiliationComponent));
300  }
301 
302  //------------------------------------------------------------------------------------------------
304  IEntity GetBuildingTool(notnull IEntity ent)
305  {
306  SCR_GadgetManagerComponent gadgetManager = SCR_GadgetManagerComponent.GetGadgetManager(ent);
307  if (!gadgetManager)
308  return null;
309 
310  return gadgetManager.GetHeldGadget();
311  }
312 
313  //------------------------------------------------------------------------------------------------
315  bool IsPlayerFactionSame(notnull IEntity user)
316  {
317  if (!m_FactionComponent)
318  return true;
319 
320  int playerId = GetGame().GetPlayerManager().GetPlayerIdFromControlledEntity(user);
321  Faction playerFaction = SCR_FactionManager.SGetPlayerFaction(playerId);
322 
323  return m_FactionComponent.GetAffiliatedFaction() == playerFaction;
324  }
325 
326  //------------------------------------------------------------------------------------------------
328  protected SCR_CampaignBuildingNetworkComponent GetNetworkManager()
329  {
330  PlayerController playerController = GetGame().GetPlayerController();
331  if (!playerController)
332  return null;
333 
334  return SCR_CampaignBuildingNetworkComponent.Cast(playerController.FindComponent(SCR_CampaignBuildingNetworkComponent));
335  }
336 
337  //------------------------------------------------------------------------------------------------
338  // Destructor
340  {
341  if (m_User)
342  CancelPlayerAnimation(m_User);
343  }
344 }
SCR_CampaignBuildingDisassemblyUserAction
Definition: SCR_CampaignBuildingDisassemblyUserAction.c:1
EEditableEntityLabel
EEditableEntityLabel
Definition: EEditableEntityLabel.c:1
SCR_PlayerController
Definition: SCR_PlayerController.c:31
m_FactionComponent
protected SCR_FactionAffiliationComponent m_FactionComponent
Definition: SCR_MilitaryBaseComponent.c:46
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_GadgetManagerComponent
Definition: SCR_GadgetManagerComponent.c:138
ItemUseParameters
Definition: ItemUseParameters.c:15
ScriptedUserAction
Definition: ScriptedUserAction.c:12
SCR_EditableEntityUIInfo
Definition: SCR_EditableEntityUIInfo.c:2
GetPlayerController
proto external PlayerController GetPlayerController()
Definition: SCR_PlayerDeployMenuHandlerComponent.c:307
GetGameMode
SCR_BaseGameMode GetGameMode()
Definition: SCR_BaseGameModeComponent.c:15
SCR_GameModeCampaign
void SCR_GameModeCampaign(IEntitySource src, IEntity parent)
Definition: SCR_GameModeCampaign.c:1927
SCR_EditorManagerCore
Core component to manage SCR_EditorManagerEntity.
Definition: SCR_EditorManagerCore.c:5
m_GadgetManager
SCR_GadgetManagerComponent m_GadgetManager
Definition: SCR_GadgetManagerComponent.c:2
GetOwner
IEntity GetOwner()
Owner entity of the fuel tank.
Definition: SCR_FuelNode.c:128
m_EditorManager
protected SCR_EditorManagerEntity m_EditorManager
Definition: SCR_VotingEditor.c:5
m_User
private IEntity m_User
Definition: InteractableBoxComponent.c:11
SCR_EditableEntityComponent
Definition: SCR_EditableEntityComponent.c:13
Faction
Definition: Faction.c:12
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
SCR_FactionManager
void SCR_FactionManager(IEntitySource src, IEntity parent)
Definition: SCR_FactionManager.c:461
SCR_EditorManagerEntity
Definition: SCR_EditorManagerEntity.c:26