Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_SiteSlotEntity.c
Go to the documentation of this file.
1 //#define SLOT_ENTITY_DEBUG
2 
3 [EntityEditorProps(category: "GameScripted/Editor", description: "Slot in which entities can be placed in", color: "255 0 0 255")]
4 class SCR_SiteSlotEntityClass: GenericEntityClass
5 {
6  [Attribute(defvalue: "-1", category: "Slot", desc: "Forced rotation step. 0 means unlimited rotation, -1 disables rotation.")]
7  private float m_fRotationStep;
8 
13  float GetRotationStep()
14  {
15  return m_fRotationStep;
16  }
17 };
18 
27 {
35  IEntity SpawnEntityInSlot(Resource resource, float rotation = -1, EEditorTransformVertical verticalMode = EEditorTransformVertical.TERRAIN)
36  {
37  //--- Already occupied
38  if (GetOccupant()) return null;
39 
40  ArmaReforgerScripted game = GetGame();
41  if (!game) return null;
42 
43  //--- Get slot transformation
44  EntitySpawnParams spawnParams = new EntitySpawnParams;
45  GetWorldTransform(spawnParams.Transform);
46  spawnParams.TransformMode = ETransformMode.WORLD;
47 
48  //--- Apply rotation
49  if (rotation != -1)
50  {
51  vector angles = Math3D.MatrixToAngles(spawnParams.Transform);
52  angles[0] = rotation;
53  Math3D.AnglesToMatrix(angles, spawnParams.Transform);
54  }
55 
56  //--- Spawn the prefab
57  string resourceName = resource.GetResource().GetResourceName();
58  IEntity baseEntity = game.SpawnEntityPrefab(resource, GetWorld(), spawnParams);
59  if (!baseEntity) return null;
60 
61  //--- Mark as occupied
62  SetOccupant(baseEntity);
63 
64  //--- Orient according to vertical settings
65  bool updateNavmesh = true;
66  if (verticalMode != -1)
67  {
68  SCR_EditableEntityComponent entity = SCR_EditableEntityComponent.GetEditableEntity(baseEntity);
69  if (entity)
70  {
71  SCR_EditorPreviewParams params = SCR_EditorPreviewParams.CreateParams(spawnParams.Transform, verticalMode: verticalMode);
72  SCR_RefPreviewEntity.SpawnAndApplyReference(entity, params);
73  updateNavmesh = false;
74  }
75  else
76  {
77  //--- Handled in SCR_SlotCompositionComponent.EOnInit() now
78  //SCR_SlotCompositionComponent composition = SCR_SlotCompositionComponent.Cast(baseEntity.FindComponent(SCR_SlotCompositionComponent));
79  //if (composition) composition.OrientToTerrain();
80  }
81  }
82 
83  //--- Update navmesh (if the reference entity didn't do it already)
84  if (updateNavmesh)
85  {
86  SCR_AIWorld aiWorld = SCR_AIWorld.Cast(GetGame().GetAIWorld());
87  if (aiWorld)
88  aiWorld.RequestNavmeshRebuildEntity(baseEntity);
89  }
90 
91  Print(string.Format("Entity @\"%1\" spawned in slot '%2'.", resourceName, GetName()), LogLevel.VERBOSE);
92 
93  return baseEntity;
94  }
99  float GetRotationStep()
100  {
102  if (!prefabData) return 0;
103 
104  return prefabData.GetRotationStep();
105  }
111  IEntity GetOccupant()
112  {
113  SCR_CompositionSlotManagerComponent manager = SCR_CompositionSlotManagerComponent.GetInstance();
114  if (manager)
115  {
116  return manager.GetOccupant(this);
117  }
118  else
119  {
120  Debug.Error2(Type().ToString(), "Cannot get slot occupant, SCR_CompositionSlotManagerComponent is missing in the game mode entity!");
121  return null;
122  }
123  }
128  bool IsOccupied()
129  {
130  SCR_CompositionSlotManagerComponent manager = SCR_CompositionSlotManagerComponent.GetInstance();
131  if (manager)
132  {
133  return manager.IsOccupied(this);
134  }
135  else
136  {
137  Debug.Error2(Type().ToString(), "Cannot check if a slot is occupied, SCR_CompositionSlotManagerComponent is missing in the game mode entity!");
138  return false;
139  }
140  }
147  void SetOccupant(IEntity occupant)
148  {
149  SCR_CompositionSlotManagerComponent manager = SCR_CompositionSlotManagerComponent.GetInstance();
150  if (manager)
151  {
152  manager.SetOccupant(this, occupant);
153  }
154  else
155  {
156  Debug.Error2(Type().ToString(), "Cannot set slot occupant, SCR_CompositionSlotManagerComponent is missing in the game mode entity!");
157  }
158  }
159 
160  void SCR_SiteSlotEntity(IEntitySource src, IEntity parent)
161  {
162  if (SCR_Global.IsEditMode(this)) return;
163 
164  ClearFlags(EntityFlags.TRACEABLE, false);
165  SetFlags(EntityFlags.STATIC, false);
166  }
167 
168 #ifdef WORKBENCH
169  const string NAME_FORMAT = "%1_%2"; //--- Format of the name, where %1 is the prefab file name and %2 is an iterator
170  const int ITERATOR_DIGITS = 3; //--- Number of digits in the iterator (e.g., 3 digits results in 007)
171  const int ITERATORS_LIMIT = 1000; //--- How many iterators are changed for a free name
172 
173  void _WB_SnapToTerrain(IEntitySource entitySource)
174  {
175  vector pos = GetOrigin();
176  pos[1] = GetWorld().GetSurfaceY(pos[0], pos[2]);
177  if (entitySource.GetParent())
178  pos = SCR_BaseContainerTools.GetLocalCoords(entitySource.GetParent(), pos);
179 
180  WorldEditorAPI api = _WB_GetEditorAPI();
181  IEntitySource src = api.EntityToSource(this);
182  api.SetVariableValue(src, null, "coords", pos.ToString(false));
183  }
184  void _WB_OrientToTerrain(IEntitySource entitySource)
185  {
186  WorldEditorAPI api = _WB_GetEditorAPI();
187  IEntitySource src = api.EntityToSource(this);
188 
189  api.SetVariableValue(src, null, "angleX", "0");
190  api.SetVariableValue(src, null, "angleZ", "0");
191 
192  vector transform[4];
193  GetWorldTransform(transform);
194  vector pos = transform[3];
195 
196  IEntity child;
197  vector min, max, posChild;
198 
199 #ifdef SLOT_ENTITY_DEBUG
200  m_DebugShapes.Clear();
201 #endif
202 
203  for (int i = 0, count = entitySource.GetNumChildren(); i < count; i++)
204  {
205  IEntitySource childSrc = entitySource.GetChild(i);
206  child = api.SourceToEntity(childSrc);
207  child.GetBounds(min, max);
208 
209  float posY = -float.MAX;
210  float angleZ = _WB_GetAngle(api, pos + transform[0] * min[0], pos + transform[0] * max[0], posY);
211  float angleX = _WB_GetAngle(api, pos + transform[2] * min[2], pos + transform[2] * max[2], posY);
212 
213  posChild = pos;
214  posChild[1] = posY;
215  posChild = CoordToLocal(posChild);
216 
217  api.SetVariableValue(childSrc, null, "coords", posChild.ToString(false));
218  api.SetVariableValue(childSrc, null, "angleX", angleX.ToString());
219  api.SetVariableValue(childSrc, null, "angleZ", (-angleZ).ToString());
220  }
221  }
222  float _WB_GetAngle(WorldEditorAPI api, vector posA, vector posB, out float posY)
223  {
224  float dis = vector.Distance(posA, posB);
225  if (dis == 0)
226  {
227  Print(string.Format("Slot entity %1 at position %2 has zero size!", this, GetWorldTransformAxis(3)), LogLevel.WARNING);
228  return 0;
229  }
230 
231  posA[1] = api.GetTerrainSurfaceY(posA[0], posA[2]);
232  posB[1] = api.GetTerrainSurfaceY(posB[0], posB[2]);
233 
234  vector posCenter = vector.Lerp(posA, posB, 0.5);
235  posY = Math.Max(posY, posCenter[1]);
236 
237 #ifdef SLOT_ENTITY_DEBUG
238  m_DebugShapes.Insert(Shape.CreateSphere(Color.RED, ShapeFlags.VISIBLE, posA, 0.5));
239  m_DebugShapes.Insert(Shape.CreateSphere(Color.GREEN, ShapeFlags.VISIBLE, posB, 0.5));
240  m_DebugShapes.Insert(Shape.CreateSphere(Color.BLUE, ShapeFlags.VISIBLE, posCenter, 0.5));
241 #endif
242 
243  return Math.Tan((posB[1] - posA[1]) / dis) * Math.RAD2DEG;
244  }
245 
246  override array<ref WB_UIMenuItem> _WB_GetContextMenuItems()
247  {
248  array<ref WB_UIMenuItem> items = { new WB_UIMenuItem("Snap and orient to terrain", 0) };
249 
250  if (GetName().IsEmpty())
251  items.Insert(new WB_UIMenuItem("Auto-assign slot name", 1));
252 
253  return items;
254  }
255  override void _WB_OnContextMenu(int id)
256  {
257  switch (id)
258  {
259  case 0:
260  {
261  WorldEditorAPI api = _WB_GetEditorAPI();
262  vector mat[4];
263  api.BeginEntityAction();
264  IEntitySource entitySource = _WB_GetEditorAPI().EntityToSource(this);
265  _WB_SnapToTerrain(entitySource);
266  _WB_OrientToTerrain(entitySource);
267  api.EndEntityAction();
268  break;
269  }
270  case 1:
271  {
272  WorldEditorAPI api = _WB_GetEditorAPI();
273  api.BeginEntityAction();
274  api.EndEntityAction();
275  break;
276  }
277  }
278  }
279 #ifdef SLOT_ENTITY_DEBUG
280  protected ref array<ref Shape> m_DebugShapes = {};
281 #endif
282  override bool _WB_OnKeyChanged(BaseContainer src, string key, BaseContainerList ownerContainers, IEntity parent)
283  {
284  if (key == "coords")
285  {
286  WorldEditorAPI api = _WB_GetEditorAPI();
287  if (!api.UndoOrRedoIsRestoring())
288  {
289  IEntitySource entitySource = src.ToEntitySource();
290  _WB_OrientToTerrain(entitySource);
291  }
292  }
293  return false;
294  }
295 #endif
296 };
GetPrefabData
SCR_VehicleDamageManagerComponentClass GetPrefabData()
Definition: SCR_VehicleDamageManagerComponent.c:260
GetName
string GetName()
Definition: SCR_ScenarioFrameworkLayerBase.c:85
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
GenericEntity
SCR_GenericBoxEntityClass GenericEntity
SCR_AIWorld
Definition: SCR_AIWorld.c:23
SCR_SiteSlotEntityClass
Definition: SCR_SiteSlotEntity.c:4
EEditorTransformVertical
EEditorTransformVertical
Vertical transformation mode.
Definition: EEditorTransformVertical.c:5
GetOrigin
vector GetOrigin()
Definition: SCR_AIUtilityComponent.c:279
Attribute
typedef Attribute
Post-process effect of scripted camera.
rotation
RespawnSystemComponentClass GameComponentClass vector vector rotation
Definition: RespawnSystemComponent.c:23
SCR_EditableEntityComponent
Definition: SCR_EditableEntityComponent.c:13
SCR_BaseContainerTools
Definition: SCR_BaseContainerTools.c:3
SCR_Global
Definition: Functions.c:6
SCR_SiteSlotEntity
Definition: SCR_SiteSlotEntity.c:26
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
SCR_RefPreviewEntity
Definition: SCR_RefPreviewEntity.c:10
SCR_EditorPreviewParams
Network packet of variables for entity placing and transformation.
Definition: SCR_EditorPreviewParams.c:4
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180