Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_CampaignBuildingLayoutComponent.c
Go to the documentation of this file.
1 [EntityEditorProps(category: "GameScripted/Building", description: "Component attached to a composition layout, holding information about the final composition to be built")]
2 class SCR_CampaignBuildingLayoutComponentClass : ScriptComponentClass
3 {
4 }
5 
6 class SCR_CampaignBuildingLayoutComponent : ScriptComponent
7 {
8  [RplProp(onRplName: "SpawnPreviewIfBuildingModeOpened")]
9  protected int m_iPrefabId = INVALID_PREFAB_ID;
10 
11  [RplProp()]
12  protected int m_iToBuildValue;
13 
14  // The value is float, because GM can set a building progress in percentage and if the value would be an int, the rounding of value is causing a confusion.
15  [RplProp()]
16  protected float m_fCurrentBuildValue;
17 
19 
20  protected const static int EMPTY_BUILDING_VALUE = 0;
21  protected static const int INVALID_PREFAB_ID = -1;
22  protected ref ScriptInvokerInt m_OnAddBuildingValueInt;
23  protected ref ScriptInvokerVoid m_OnAddBuildingValueVoid;
24  protected ref ScriptInvokerVoid m_OnCompositionIdSet;
25 
26  //------------------------------------------------------------------------------------------------
27  override void OnPostInit(IEntity owner)
28  {
31 
32  if (!SetOnEditorOpenEvent())
33  {
35  if (core)
36  core.Event_OnEditorManagerInitOwner.Insert(SetOnEditorOpenEvent);
37  }
38 
40  }
41 
42  //------------------------------------------------------------------------------------------------
46  {
47  SCR_EditorManagerEntity editorManagerEntity = SCR_EditorManagerEntity.GetInstance();
48  if (editorManagerEntity)
49  editorManagerEntity.GetOnModeChange().Insert(OnEditorModeChanged);
50 
52  if (core)
53  core.Event_OnEditorManagerInitOwner.Remove(SetOnEditorOpenEvent);
54 
55  return editorManagerEntity != null;
56  }
57 
58  //------------------------------------------------------------------------------------------------
61  {
62  if (!m_OnAddBuildingValueInt)
63  m_OnAddBuildingValueInt = new ScriptInvokerInt();
64 
65  return m_OnAddBuildingValueInt;
66  }
67 
68  //------------------------------------------------------------------------------------------------
71  {
72  if (!m_OnAddBuildingValueVoid)
73  m_OnAddBuildingValueVoid = new ScriptInvokerVoid();
74 
75  return m_OnAddBuildingValueVoid;
76  }
77 
78  //------------------------------------------------------------------------------------------------
81  {
82  if (!m_OnCompositionIdSet)
83  m_OnCompositionIdSet = new ScriptInvokerVoid();
84 
85  return m_OnCompositionIdSet;
86  }
87 
88  //------------------------------------------------------------------------------------------------
92  void OnEditorModeChanged(SCR_EditorModeEntity currentModeEntity, SCR_EditorModeEntity prevModeEntity)
93  {
94  if (currentModeEntity && currentModeEntity.GetModeType() == EEditorMode.BUILDING && IsLayoutInBuildingRange(currentModeEntity))
95  SpawnPreview();
96  else
97  DeletePreview();
98  }
99 
100  //------------------------------------------------------------------------------------------------
104  bool IsLayoutInBuildingRange(notnull SCR_EditorModeEntity editorModeEntity)
105  {
106  SCR_CampaignBuildingEditorComponent buildingEditorComponent = SCR_CampaignBuildingEditorComponent.Cast(editorModeEntity.FindComponent(SCR_CampaignBuildingEditorComponent));
107  if (!buildingEditorComponent)
108  return false;
109 
110  SCR_CampaignBuildingProviderComponent providerComponent = SCR_CampaignBuildingProviderComponent.Cast(buildingEditorComponent.GetProviderComponent());
111  if (!providerComponent)
112  return false;
113 
114  return vector.DistanceSq(GetOwner().GetOrigin(), providerComponent.GetOwner().GetOrigin()) <= providerComponent.GetBuildingRadius() * providerComponent.GetBuildingRadius();
115  }
116 
117  //------------------------------------------------------------------------------------------------
120  {
121  return m_PreviewEntity;
122  }
123 
124  //------------------------------------------------------------------------------------------------
127  void EvaluateBuildingStatus(int currentBuildValue)
128  {
129 #ifdef ENABLE_DIAG
130  if (DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_CAMPAIGN_INSTANT_BUILDING))
131  {
132  // Calling the method one frame later, otherwise it can causing spawning the same composition twice.
133  GetGame().GetCallqueue().CallLater(SpawnComposition, 0, false);
134  return;
135  }
136 #endif
137 
138  if (currentBuildValue < m_iToBuildValue)
139  return;
140 
143  }
144 
145  //------------------------------------------------------------------------------------------------
149  int GetBuildingValue(int prefabID)
150  {
151  ResourceName resName = GetCompositionResourceName(prefabID);
152  if (resName.IsEmpty())
153  return EMPTY_BUILDING_VALUE;
154 
155  SCR_CampaignBuildingManagerComponent buildingManagerComponent = GetBuildingManagerComponent();
156  if (!buildingManagerComponent)
157  return EMPTY_BUILDING_VALUE;
158 
159  SCR_CampaignBuildingCompositionOutlineManager outlineManager = buildingManagerComponent.GetOutlineManager();
160  if (!outlineManager)
161  return EMPTY_BUILDING_VALUE;
162 
163  return outlineManager.GetCompositionBuildingValue(resName);
164  }
165 
166  //------------------------------------------------------------------------------------------------
170  protected ResourceName GetCompositionResourceName(int prefabID)
171  {
172  SCR_CampaignBuildingManagerComponent buildingManagerComponent = GetBuildingManagerComponent();
173  if (!buildingManagerComponent)
174  return string.Empty;
175 
176  return buildingManagerComponent.GetCompositionResourceName(prefabID);
177  }
178 
179  //------------------------------------------------------------------------------------------------
180  protected SCR_CampaignBuildingManagerComponent GetBuildingManagerComponent()
181  {
182  BaseGameMode gameMode = GetGame().GetGameMode();
183  if (!gameMode)
184  return null;
185 
186  return SCR_CampaignBuildingManagerComponent.Cast(gameMode.FindComponent(SCR_CampaignBuildingManagerComponent));
187  }
188 
189  //------------------------------------------------------------------------------------------------
192  void SetPrefabId(int prefabId)
193  {
194  m_iPrefabId = prefabId;
195  m_iToBuildValue = GetBuildingValue(prefabId);
196 
197  if (m_OnCompositionIdSet)
198  m_OnCompositionIdSet.Invoke(prefabId);
199 
200  EvaluateBuildingStatus(m_fCurrentBuildValue);
201 
202  Replication.BumpMe();
203  }
204 
205  //------------------------------------------------------------------------------------------------
208  {
209  return m_iPrefabId;
210  }
211 
212  //------------------------------------------------------------------------------------------------
215  {
216  IEntity ent = GetOwner().GetRootParent();
217  if (!ent)
218  return;
219 
220  SCR_EditorLinkComponent linkComponent = SCR_EditorLinkComponent.Cast(ent.FindComponent(SCR_EditorLinkComponent));
221  if (!linkComponent)
222  return;
223 
224  linkComponent.SpawnComposition();
225 
226  // Call this as the composition can be spawned directly if the building value is set to zero. Yet we want to lock the composition to disable the interaction.
228 
229  EntitySpawnParams spawnParams = new EntitySpawnParams;
230  ent.GetWorldTransform(spawnParams.Transform);
231 
232  SCR_EditableEntityComponent entity = SCR_EditableEntityComponent.GetEditableEntity(ent);
233  if (entity)
234  {
235  SCR_EditorPreviewParams params = SCR_EditorPreviewParams.CreateParams(spawnParams.Transform, verticalMode: EEditorTransformVertical.TERRAIN);
236  SCR_RefPreviewEntity.SpawnAndApplyReference(entity, params);
237  }
238 
239  SCR_EntityHelper.DeleteEntityAndChildren(GetOwner());
240  }
241 
242  //------------------------------------------------------------------------------------------------
245  {
246  if (m_PreviewEntity)
247  return;
248 
249  IEntity ent = GetOwner().GetRootParent();
250  if (!ent)
251  return;
252 
253  ResourceName resName = GetCompositionResourceName(m_iPrefabId);
254  if (resName.IsEmpty())
255  return;
256 
257  Resource res = Resource.Load(resName);
258  if (!res.IsValid())
259  return;
260 
261  BaseGameMode gameMode = GetGame().GetGameMode();
262  if (!gameMode)
263  return;
264 
265  SCR_CampaignBuildingManagerComponent buildingManagerComponent = SCR_CampaignBuildingManagerComponent.Cast(gameMode.FindComponent(SCR_CampaignBuildingManagerComponent));
266  if (!buildingManagerComponent)
267  return;
268 
269  EntitySpawnParams spawnParams = new EntitySpawnParams();
270  spawnParams.TransformMode = ETransformMode.WORLD;
271  ent.GetWorldTransform(spawnParams.Transform);
272 
273  m_PreviewEntity = SCR_PrefabPreviewEntity.SpawnPreviewFromPrefab(res, "SCR_PrefabPreviewEntity", ent.GetWorld(), spawnParams, "{58F07022C12D0CF5}Assets/Editor/PlacingPreview/Preview.emat");
274  m_PreviewEntity.SetPreviewTransform(spawnParams.Transform, EEditorTransformVertical.TERRAIN);
275  if (!m_PreviewEntity)
276  return;
277 
278  ent.AddChild(m_PreviewEntity, -1, EAddChildFlags.RECALC_LOCAL_TRANSFORM);
279 
280  m_PreviewEntity.Update();
281 
282  IEntity playerEntity = SCR_PlayerController.GetLocalMainEntity();
283  if (!playerEntity)
284  return;
285 
286  SCR_CharacterControllerComponent characterController = SCR_CharacterControllerComponent.Cast(playerEntity.FindComponent(SCR_CharacterControllerComponent));
287  if (!characterController)
288  return;
289 
290  characterController.GetOnPlayerDeath().Insert(DeletePreview);
291  }
292 
293  //------------------------------------------------------------------------------------------------
296  {
298  if (!modeEntity)
299  return;
300 
301  if (modeEntity.IsOpened())
302  SpawnPreview();
303  }
304 
305  //------------------------------------------------------------------------------------------------
308  {
309  IEntity playerEntity = SCR_PlayerController.GetLocalMainEntity();
310  if (!playerEntity)
311  return;
312 
313  SCR_CharacterControllerComponent characterController = SCR_CharacterControllerComponent.Cast(playerEntity.FindComponent(SCR_CharacterControllerComponent));
314  if (!characterController)
315  return;
316 
317  characterController.GetOnPlayerDeath().Remove(DeletePreview);
318 
319  // if there was open an editor, remove the invoker
321  if (modeEntity)
322  modeEntity.GetOnClosed().Remove(DeletePreview);
323 
324  SCR_EntityHelper.DeleteEntityAndChildren(m_PreviewEntity);
325  }
326 
327  //------------------------------------------------------------------------------------------------
330  void AddBuildingValue(int value)
331  {
332  m_fCurrentBuildValue += value;
333  m_fCurrentBuildValue = Math.Clamp(m_fCurrentBuildValue, 0, m_iToBuildValue);
334 
335  if (m_OnAddBuildingValueInt)
336  m_OnAddBuildingValueInt.Invoke(m_fCurrentBuildValue);
337 
338  if (m_OnAddBuildingValueVoid)
339  m_OnAddBuildingValueVoid.Invoke();
340 
341  Replication.BumpMe();
342  }
343 
344  //------------------------------------------------------------------------------------------------
347  void SetBuildingValue(float newValue)
348  {
349  newValue = Math.Clamp(newValue, 0, m_iToBuildValue);
350  m_fCurrentBuildValue = newValue;
351 
352  if (m_OnAddBuildingValueInt)
353  m_OnAddBuildingValueInt.Invoke(m_fCurrentBuildValue);
354 
355  if (m_OnAddBuildingValueVoid)
356  m_OnAddBuildingValueVoid.Invoke();
357 
358  Replication.BumpMe();
359  }
360 
361  //------------------------------------------------------------------------------------------------
364  {
365  return m_iToBuildValue;
366  }
367 
368  //------------------------------------------------------------------------------------------------
371  {
372  return m_fCurrentBuildValue;
373  }
374 
375  //------------------------------------------------------------------------------------------------
379  {
381  if (!core)
382  return null;
383 
384  SCR_EditorManagerEntity editorManager = core.GetEditorManager();
385  if (!editorManager)
386  return null;
387 
388  return editorManager.FindModeEntity(EEditorMode.BUILDING);
389  }
390 
391  //------------------------------------------------------------------------------------------------
395  {
396  IEntity rootEnt = GetOwner().GetRootParent();
397  if (!rootEnt)
398  return;
399 
400  SCR_CampaignBuildingCompositionComponent compositionComponent = SCR_CampaignBuildingCompositionComponent.Cast(rootEnt.FindComponent(SCR_CampaignBuildingCompositionComponent));
401  if (!compositionComponent)
402  return;
403 
404  compositionComponent.SetInteractionLockServer(true);
406  }
407 
408  //------------------------------------------------------------------------------------------------
409  protected void SnapEntityToTerrain(IEntity entity)
410  {
411  if (!entity)
412  return;
413 
414  vector transform[4];
415  Math3D.MatrixIdentity4(transform);
416 
417  transform[3] = entity.GetOrigin();
418 
419  BaseWorld world = GetGame().GetWorld();
420  if (!world)
421  return;
422 
423  transform[3][1] = world.GetSurfaceY(transform[3][0], transform[3][2]);
424  entity.SetTransform(transform);
425  }
426 
427  //------------------------------------------------------------------------------------------------
428  override void OnChildAdded(IEntity parent, IEntity child)
429  {
430  super.OnChildAdded(parent, child);
431 
432  // child local transform is applied one frame later on server so snapping to terrain needs to be delayed by one frame
433  RplComponent rplComp = RplComponent.Cast(GetOwner().FindComponent(RplComponent));
434  if (!rplComp || rplComp.IsProxy())
435  SnapEntityToTerrain(child);
436  else
437  GetGame().GetCallqueue().CallLater(SnapEntityToTerrain, 0, false, child);
438  }
439 
440  //------------------------------------------------------------------------------------------------
441  // destructor
443  {
444  // Delete preview when the layout is removed (composition build, layout deleted from editor mode, etc.);
445  DeletePreview();
446  }
447 }
GetCompositionResourceName
protected ResourceName GetCompositionResourceName(int prefabID)
Definition: SCR_CampaignBuildingLayoutComponent.c:170
SCR_PlayerController
Definition: SCR_PlayerController.c:31
SCR_EntityHelper
Definition: SCR_EntityHelper.c:1
m_iPrefabId
protected int m_iPrefabId
Definition: SCR_CampaignBuildingCompositionComponent.c:20
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
DeletePreview
void DeletePreview()
Definition: SCR_CampaignBuildingLayoutComponent.c:307
ScriptComponent
SCR_SiteSlotEntityClass ScriptComponent
ScriptInvokerInt
ScriptInvokerBase< ScriptInvokerIntMethod > ScriptInvokerInt
Definition: SCR_ScriptInvokerHelper.c:24
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
GetBuildingManagerComponent
protected SCR_CampaignBuildingManagerComponent GetBuildingManagerComponent()
Definition: SCR_CampaignBuildingLayoutComponent.c:180
~SCR_CampaignBuildingLayoutComponent
void ~SCR_CampaignBuildingLayoutComponent()
Definition: SCR_CampaignBuildingLayoutComponent.c:442
IsLayoutInBuildingRange
bool IsLayoutInBuildingRange(notnull SCR_EditorModeEntity editorModeEntity)
Definition: SCR_CampaignBuildingLayoutComponent.c:104
SCR_BasePreviewEntity
Definition: SCR_BasePreviewEntity.c:9
SnapEntityToTerrain
protected void SnapEntityToTerrain(IEntity entity)
Definition: SCR_CampaignBuildingLayoutComponent.c:409
SCR_EditorModeEntity
Definition: SCR_EditorModeEntity.c:22
m_PreviewEntity
protected IEntity m_PreviewEntity
Definition: SCR_ItemPlacementComponent.c:23
LockCompositionInteraction
void LockCompositionInteraction()
Definition: SCR_CampaignBuildingLayoutComponent.c:394
GetPrefabId
int GetPrefabId()
Definition: SCR_CampaignBuildingLayoutComponent.c:207
SetOnEditorOpenEvent
bool SetOnEditorOpenEvent()
Definition: SCR_CampaignBuildingLayoutComponent.c:45
SpawnPreviewIfBuildingModeOpened
void SpawnPreviewIfBuildingModeOpened()
Spawn the composition preview if the building mode is active (open)
Definition: SCR_CampaignBuildingLayoutComponent.c:295
SCR_CharacterControllerComponent
Definition: SCR_CharacterControllerComponent.c:35
GetOnAddBuildingValueVoid
ScriptInvokerVoid GetOnAddBuildingValueVoid()
Definition: SCR_CampaignBuildingLayoutComponent.c:70
GetToBuildValue
int GetToBuildValue()
Definition: SCR_CampaignBuildingLayoutComponent.c:363
GetOnCompositionIdSet
ScriptInvokerVoid GetOnCompositionIdSet()
Definition: SCR_CampaignBuildingLayoutComponent.c:80
EEditorTransformVertical
EEditorTransformVertical
Vertical transformation mode.
Definition: EEditorTransformVertical.c:5
GetOrigin
vector GetOrigin()
Definition: SCR_AIUtilityComponent.c:279
SCR_CampaignBuildingCompositionOutlineManager
Definition: SCR_CampaignBuildingCompositionOutlineRegistry.c:6
SCR_EditorManagerCore
Core component to manage SCR_EditorManagerEntity.
Definition: SCR_EditorManagerCore.c:5
AddBuildingValue
void AddBuildingValue(int value)
Definition: SCR_CampaignBuildingLayoutComponent.c:330
SetBuildingValue
void SetBuildingValue(float newValue)
Definition: SCR_CampaignBuildingLayoutComponent.c:347
OnPostInit
override void OnPostInit(IEntity owner)
Called on PostInit when all components are added.
Definition: SCR_AIConfigComponent.c:72
SCR_CampaignBuildingLayoutComponentClass
Definition: SCR_CampaignBuildingLayoutComponent.c:2
SCR_PrefabPreviewEntity
Definition: SCR_PrefabPreviewEntity.c:8
GetOnAddBuildingValueInt
ScriptInvokerInt GetOnAddBuildingValueInt()
Definition: SCR_CampaignBuildingLayoutComponent.c:60
SpawnPreview
void SpawnPreview()
Create a preview if it does not exist.
Definition: SCR_CampaignBuildingLayoutComponent.c:244
GetCurrentBuildValue
float GetCurrentBuildValue()
Definition: SCR_CampaignBuildingLayoutComponent.c:370
GetBuildingModeEntity
SCR_EditorModeEntity GetBuildingModeEntity()
Definition: SCR_CampaignBuildingLayoutComponent.c:378
GetOwner
IEntity GetOwner()
Owner entity of the fuel tank.
Definition: SCR_FuelNode.c:128
EvaluateBuildingStatus
void EvaluateBuildingStatus(int currentBuildValue)
Definition: SCR_CampaignBuildingLayoutComponent.c:127
SCR_EditableEntityComponent
Definition: SCR_EditableEntityComponent.c:13
HasBuildingPreview
bool HasBuildingPreview()
Definition: SCR_CampaignBuildingLayoutComponent.c:119
EEditorMode
EEditorMode
Editor mode that defines overall functionality.
Definition: EEditorMode.c:5
ScriptInvokerVoid
ScriptInvokerBase< ScriptInvokerVoidMethod > ScriptInvokerVoid
Definition: SCR_ScriptInvokerHelper.c:9
OnChildAdded
override void OnChildAdded(IEntity parent, IEntity child)
Definition: SCR_CampaignBuildingLayoutComponent.c:428
SetPrefabId
void SetPrefabId(int prefabId)
Definition: SCR_CampaignBuildingLayoutComponent.c:192
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
SCR_RefPreviewEntity
Definition: SCR_RefPreviewEntity.c:10
OnEditorModeChanged
void OnEditorModeChanged(SCR_EditorModeEntity currentModeEntity, SCR_EditorModeEntity prevModeEntity)
Definition: SCR_CampaignBuildingLayoutComponent.c:92
SpawnComposition
void SpawnComposition()
Spawn the composition belonging to this layout.
Definition: SCR_CampaignBuildingLayoutComponent.c:214
SCR_DebugMenuID
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition: DebugMenuID.c:3
SCR_EditorPreviewParams
Network packet of variables for entity placing and transformation.
Definition: SCR_EditorPreviewParams.c:4
RplProp
SCR_CampaignBuildingLayoutComponentClass ScriptComponentClass RplProp(onRplName:"SpawnPreviewIfBuildingModeOpened")
Definition: SCR_CampaignBuildingLayoutComponent.c:8
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180
GetBuildingValue
int GetBuildingValue(int prefabID)
Definition: SCR_CampaignBuildingLayoutComponent.c:149
SCR_EditorManagerEntity
Definition: SCR_EditorManagerEntity.c:26