Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
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")]
5
6class 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 static const 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
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))
96 else
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 = 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 if (currentBuildValue > m_iToBuildValue * 0.5)
139
140 if (currentBuildValue < m_iToBuildValue)
141 return;
142
145 }
146
147 //------------------------------------------------------------------------------------------------
151 int GetBuildingValue(int prefabID)
152 {
153 ResourceName resName = GetCompositionResourceName(prefabID);
154 if (resName.IsEmpty())
155 return EMPTY_BUILDING_VALUE;
156
157 SCR_CampaignBuildingManagerComponent buildingManagerComponent = GetBuildingManagerComponent();
158 if (!buildingManagerComponent)
159 return EMPTY_BUILDING_VALUE;
160
161 SCR_CampaignBuildingCompositionOutlineManager outlineManager = buildingManagerComponent.GetOutlineManager();
162 if (!outlineManager)
163 return EMPTY_BUILDING_VALUE;
164
165 return outlineManager.GetCompositionBuildingValue(resName);
166 }
167
168 //------------------------------------------------------------------------------------------------
173 {
174 SCR_CampaignBuildingManagerComponent buildingManagerComponent = GetBuildingManagerComponent();
175 if (!buildingManagerComponent)
176 return string.Empty;
177
178 return buildingManagerComponent.GetCompositionResourceName(prefabID);
179 }
180
181 //------------------------------------------------------------------------------------------------
182 protected SCR_CampaignBuildingManagerComponent GetBuildingManagerComponent()
183 {
184 BaseGameMode gameMode = GetGame().GetGameMode();
185 if (!gameMode)
186 return null;
187
188 return SCR_CampaignBuildingManagerComponent.Cast(gameMode.FindComponent(SCR_CampaignBuildingManagerComponent));
189 }
190
191 //------------------------------------------------------------------------------------------------
194 void SetPrefabId(int prefabId)
195 {
196 m_iPrefabId = prefabId;
197 m_iToBuildValue = GetBuildingValue(prefabId);
198
199 if (m_OnCompositionIdSet)
200 m_OnCompositionIdSet.Invoke(prefabId);
201
202 EvaluateBuildingStatus(m_fCurrentBuildValue);
203
204 Replication.BumpMe();
205 }
206
207 //------------------------------------------------------------------------------------------------
210 {
211 return m_iPrefabId;
212 }
213
214 //------------------------------------------------------------------------------------------------
217 {
219 if (!ent)
220 return;
221
222 SCR_EditorLinkComponent linkComponent = SCR_EditorLinkComponent.Cast(ent.FindComponent(SCR_EditorLinkComponent));
223 if (!linkComponent)
224 return;
225
226 linkComponent.SpawnComposition();
227
228 // 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.
230
231 EntitySpawnParams spawnParams = new EntitySpawnParams;
232 ent.GetWorldTransform(spawnParams.Transform);
233
235 if (entity)
236 {
238 SCR_RefPreviewEntity.SpawnAndApplyReference(entity, params);
239 }
240
241 SCR_EntityHelper.DeleteEntityAndChildren(GetOwner());
242 }
243
244 //------------------------------------------------------------------------------------------------
247 {
248 if (m_PreviewEntity)
249 return;
250
252 if (!ent)
253 return;
254
256 if (resName.IsEmpty())
257 return;
258
259 Resource res = Resource.Load(resName);
260 if (!res.IsValid())
261 return;
262
263 BaseGameMode gameMode = GetGame().GetGameMode();
264 if (!gameMode)
265 return;
266
267 SCR_CampaignBuildingManagerComponent buildingManagerComponent = SCR_CampaignBuildingManagerComponent.Cast(gameMode.FindComponent(SCR_CampaignBuildingManagerComponent));
268 if (!buildingManagerComponent)
269 return;
270
271 EntitySpawnParams spawnParams = new EntitySpawnParams();
272 spawnParams.TransformMode = ETransformMode.WORLD;
273 ent.GetWorldTransform(spawnParams.Transform);
274
275 m_PreviewEntity = SCR_PrefabPreviewEntity.SpawnPreviewFromPrefab(res, "SCR_PrefabPreviewEntity", ent.GetWorld(), spawnParams, "{58F07022C12D0CF5}Assets/Editor/PlacingPreview/Preview.emat");
276 m_PreviewEntity.SetPreviewTransform(spawnParams.Transform, EEditorTransformVertical.TERRAIN);
277 if (!m_PreviewEntity)
278 return;
279
280 ent.AddChild(m_PreviewEntity, -1, EAddChildFlags.RECALC_LOCAL_TRANSFORM);
281
282 m_PreviewEntity.Update();
283
285 if (!playerEntity)
286 return;
287
289 if (!characterController)
290 return;
291
292 characterController.GetOnPlayerDeath().Insert(DeletePreview);
293 }
294
295 //------------------------------------------------------------------------------------------------
298 {
300 if (!modeEntity)
301 return;
302
303 if (modeEntity.IsOpened())
304 SpawnPreview();
305 }
306
307 //------------------------------------------------------------------------------------------------
310 {
311 if (!m_PreviewEntity)
312 return;
313
315 if (!playerEntity)
316 return;
317
319 if (!characterController)
320 return;
321
322 characterController.GetOnPlayerDeath().Remove(DeletePreview);
323
324 // if there was open an editor, remove the invoker
326 if (modeEntity)
327 modeEntity.GetOnClosed().Remove(DeletePreview);
328
329 IEntity ent = m_PreviewEntity.GetRootParent();
330 if (!ent)
331 ent = GetOwner().GetRootParent();
332
333 if (ent)
335
336 m_PreviewEntity.Update();
337 SCR_EntityHelper.DeleteEntityAndChildren(m_PreviewEntity);
338 }
339
340 //------------------------------------------------------------------------------------------------
343 void AddBuildingValue(int value)
344 {
345 m_fCurrentBuildValue += value;
346 m_fCurrentBuildValue = Math.Clamp(m_fCurrentBuildValue, 0, m_iToBuildValue);
347
348 if (m_OnAddBuildingValueInt)
349 m_OnAddBuildingValueInt.Invoke(m_fCurrentBuildValue);
350
351 if (m_OnAddBuildingValueVoid)
352 m_OnAddBuildingValueVoid.Invoke();
353
354 Replication.BumpMe();
355 }
356
357 //------------------------------------------------------------------------------------------------
360 void SetBuildingValue(float newValue)
361 {
362 newValue = Math.Clamp(newValue, 0, m_iToBuildValue);
363 m_fCurrentBuildValue = newValue;
364
365 if (m_OnAddBuildingValueInt)
366 m_OnAddBuildingValueInt.Invoke(m_fCurrentBuildValue);
367
368 if (m_OnAddBuildingValueVoid)
369 m_OnAddBuildingValueVoid.Invoke();
370
371 Replication.BumpMe();
372 }
373
374 //------------------------------------------------------------------------------------------------
377 {
378 return m_iToBuildValue;
379 }
380
381 //------------------------------------------------------------------------------------------------
384 {
385 return m_fCurrentBuildValue;
386 }
387
388 //------------------------------------------------------------------------------------------------
392 {
394 if (!core)
395 return null;
396
397 SCR_EditorManagerEntity editorManager = core.GetEditorManager();
398 if (!editorManager)
399 return null;
400
401 return editorManager.FindModeEntity(EEditorMode.BUILDING);
402 }
403
404 //------------------------------------------------------------------------------------------------
408 {
409 IEntity rootEnt = GetOwner().GetRootParent();
410 if (!rootEnt)
411 return;
412
413 SCR_CampaignBuildingCompositionComponent compositionComponent = SCR_CampaignBuildingCompositionComponent.Cast(rootEnt.FindComponent(SCR_CampaignBuildingCompositionComponent));
414 if (!compositionComponent)
415 return;
416
417 compositionComponent.SetInteractionLockServer(true);
419 }
420
421 //------------------------------------------------------------------------------------------------
422 protected void SnapEntityToTerrain(IEntity entity)
423 {
424 if (!entity)
425 return;
426
427 vector transform[4];
428 Math3D.MatrixIdentity4(transform);
429
430 transform[3] = entity.GetOrigin();
431
432 BaseWorld world = GetGame().GetWorld();
433 if (!world)
434 return;
435
436 transform[3][1] = world.GetSurfaceY(transform[3][0], transform[3][2]);
437 entity.SetTransform(transform);
438 }
439
440 //------------------------------------------------------------------------------------------------
441 override void OnChildAdded(IEntity parent, IEntity child)
442 {
443 super.OnChildAdded(parent, child);
444
445 // child local transform is applied one frame later on server so snapping to terrain needs to be delayed by one frame
446 RplComponent rplComp = RplComponent.Cast(GetOwner().FindComponent(RplComponent));
447 if (!rplComp || rplComp.IsProxy())
448 SnapEntityToTerrain(child);
449 else
450 GetGame().GetCallqueue().CallLater(SnapEntityToTerrain, 0, false, child);
451 }
452
453 //------------------------------------------------------------------------------------------------
455 protected void CreateUnsafeAreaEvent()
456 {
457 vector boundMin, boundMax;
458 GetOwner().GetWorldBounds(boundMin, boundMax);
459 float previewSize = vector.DistanceXZ(boundMin, boundMax) * 1.25;
460
462 unsafeAreaEvent.SetDangerType(EAIDangerEventType.Danger_UnsafeArea);
463 unsafeAreaEvent.SetPosition(GetOwner().GetOrigin());
464 unsafeAreaEvent.SetRadius(previewSize);
465 GetGame().GetAIWorld().RequestBroadcastDangerEvent(unsafeAreaEvent);
466 }
467
468 //------------------------------------------------------------------------------------------------
469 // destructor
471 {
472 // Delete preview when the layout is removed (composition build, layout deleted from editor mode, etc.);
474 }
475}
EAIDangerEventType
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition DebugMenuID.c:4
ArmaReforgerScripted GetGame()
Definition game.c:1398
vector GetOrigin()
SCR_CacheNoteComponentClass ScriptComponentClass RplProp()] protected ref array< string > m_aLines
void SetPrefabId(int prefabId)
SCR_EditorModeEntity GetBuildingModeEntity()
void SpawnComposition()
Spawn the composition belonging to this layout.
void ~SCR_CampaignBuildingLayoutComponent()
void AddBuildingValue(int value)
void SpawnPreviewIfBuildingModeOpened()
Spawn the composition preview if the building mode is active (open).
ResourceName GetCompositionResourceName(int prefabID)
SCR_CampaignBuildingManagerComponent GetBuildingManagerComponent()
int GetBuildingValue(int prefabID)
ScriptInvokerVoid GetOnCompositionIdSet()
bool IsLayoutInBuildingRange(notnull SCR_EditorModeEntity editorModeEntity)
void SnapEntityToTerrain(IEntity entity)
void SpawnPreview()
Create a preview if it does not exist.
void OnEditorModeChanged(SCR_EditorModeEntity currentModeEntity, SCR_EditorModeEntity prevModeEntity)
void SetBuildingValue(float newValue)
void EvaluateBuildingStatus(int currentBuildValue)
void CreateUnsafeAreaEvent()
Create a danger event for AI to clear the area where the composition is about to spawn.
ScriptInvokerVoid GetOnAddBuildingValueVoid()
ScriptInvokerInt GetOnAddBuildingValueInt()
enum SCR_ECompassType EntityEditorProps(category:"GameScripted/Gadgets", description:"Compass", color:"0 0 255 255")
Prefab data class for compass component.
void SCR_EditorManagerEntity(IEntitySource src, IEntity parent)
IEntity m_PreviewEntity
ScriptInvokerBase< ScriptInvokerVoidMethod > ScriptInvokerVoid
ScriptInvokerBase< ScriptInvokerIntMethod > ScriptInvokerInt
Diagnostic and developer menu system.
Definition DiagMenu.c:18
proto external GenericComponent FindComponent(typename typeName)
proto external Managed FindComponent(typename typeName)
proto external vector GetOrigin()
proto external int AddChild(notnull IEntity child, TNodeId pivot, EAddChildFlags flags=EAddChildFlags.AUTO_TRANSFORM)
Add Entity to hierarchy. Pivot is pivot index, or -1 for center of parent.
proto external void GetWorldTransform(out vector mat[])
See IEntity::GetTransform.
proto external BaseWorld GetWorld()
proto external bool SetTransform(vector mat[4])
proto external void GetWorldBounds(out vector mins, out vector maxs)
proto external void RemoveChild(notnull IEntity child, bool keepTransform=false)
Remove Entity from hierarchy.
proto external IEntity GetRootParent()
Definition Math.c:13
Main replication API.
Definition Replication.c:14
Object holding reference to resource. In destructor release the resource.
Definition Resource.c:25
int GetCompositionBuildingValue(ResourceName originalComposition)
Search all SCR_CampaignBuildingCompositionLayoutManager entry and try to find a building value for gi...
static SCR_EditableEntityComponent GetEditableEntity(IEntity owner)
Core component to manage SCR_EditorManagerEntity.
SCR_EditorManagerEntity GetEditorManager()
Network packet of variables for entity placing and transformation.
static SCR_EditorPreviewParams CreateParams(vector transform[4], RplId parentID=Replication.INVALID_ID, EEditorTransformVertical verticalMode=EEditorTransformVertical.SEA, bool isUnderwater=false, SCR_EditableEntityComponent target=null, EEditableEntityInteraction targetInteraction=EEditableEntityInteraction.NONE)
static IEntity GetLocalMainEntity()
void OnChildAdded(IEntity parent, IEntity child)
Called on parent entity when child entity is added into hierarchy.
proto external GenericEntity GetOwner()
Get owner entity.
void EntitySpawnParams()
Definition gameLib.c:130
EEditorMode
Editor mode that defines overall functionality.
Definition EEditorMode.c:6
EEditorTransformVertical
Vertical transformation mode.
EAddChildFlags