Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
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")]
4class 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
21
26class SCR_SiteSlotEntity : GenericEntity
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 {
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
181 IEntitySource src = api.EntityToSource(this);
182 api.SetVariableValue(src, null, "coords", pos.ToString(false));
183 }
184 void _WB_OrientToTerrain(IEntitySource entitySource)
185 {
187 IEntitySource src = api.EntityToSource(this);
188
190 src.Get("angles", angles);
191 api.SetVariableValue(src, null, "angles", "0 " + angles[1] + " 0");
192
193 vector transform[4];
194 GetWorldTransform(transform);
195 vector pos = transform[3];
196
197 IEntity child;
198 vector min, max, posChild;
199
200#ifdef SLOT_ENTITY_DEBUG
201 m_DebugShapes.Clear();
202#endif
203
204 for (int i, count = entitySource.GetNumChildren(); i < count; i++)
205 {
206 IEntitySource childSrc = entitySource.GetChild(i);
207 child = api.SourceToEntity(childSrc);
208 child.GetBounds(min, max);
209
210 float posY = -float.MAX;
211 childSrc.Get("angles", angles);
212 angles[2] = _WB_GetAngle(api, pos + transform[0] * min[0], pos + transform[0] * max[0], posY);
213 angles[0] = -_WB_GetAngle(api, pos + transform[2] * min[2], pos + transform[2] * max[2], posY);
214
215 posChild = pos;
216 posChild[1] = posY;
217 posChild = CoordToLocal(posChild);
218
219 api.SetVariableValue(childSrc, null, "coords", posChild.ToString(false));
220 api.SetVariableValue(childSrc, null, "angles", string.Format("%1 %2 %3", angles[0], angles[1], angles[2]));
221 }
222 }
223 float _WB_GetAngle(WorldEditorAPI api, vector posA, vector posB, out float posY)
224 {
225 float dis = vector.Distance(posA, posB);
226 if (dis == 0)
227 {
228 Print(string.Format("Slot entity %1 at position %2 has zero size!", this, GetWorldTransformAxis(3)), LogLevel.WARNING);
229 return 0;
230 }
231
232 posA[1] = api.GetTerrainSurfaceY(posA[0], posA[2]);
233 posB[1] = api.GetTerrainSurfaceY(posB[0], posB[2]);
234
235 vector posCenter = vector.Lerp(posA, posB, 0.5);
236 posY = Math.Max(posY, posCenter[1]);
237
238#ifdef SLOT_ENTITY_DEBUG
239 m_DebugShapes.Insert(Shape.CreateSphere(Color.RED, ShapeFlags.VISIBLE, posA, 0.5));
240 m_DebugShapes.Insert(Shape.CreateSphere(Color.GREEN, ShapeFlags.VISIBLE, posB, 0.5));
241 m_DebugShapes.Insert(Shape.CreateSphere(Color.BLUE, ShapeFlags.VISIBLE, posCenter, 0.5));
242#endif
243
244 return Math.Tan((posB[1] - posA[1]) / dis) * Math.RAD2DEG;
245 }
246
247 override array<ref WB_UIMenuItem> _WB_GetContextMenuItems()
248 {
249 array<ref WB_UIMenuItem> items = { new WB_UIMenuItem("Snap and orient to terrain", 0) };
250
251 if (GetName().IsEmpty())
252 items.Insert(new WB_UIMenuItem("Auto-assign slot name", 1));
253
254 return items;
255 }
256 override void _WB_OnContextMenu(int id)
257 {
258 switch (id)
259 {
260 case 0:
261 {
263 vector mat[4];
264 api.BeginEntityAction();
265 IEntitySource entitySource = _WB_GetEditorAPI().EntityToSource(this);
266 _WB_SnapToTerrain(entitySource);
267 _WB_OrientToTerrain(entitySource);
268 api.EndEntityAction();
269 break;
270 }
271 case 1:
272 {
274 api.BeginEntityAction();
275 api.EndEntityAction();
276 break;
277 }
278 }
279 }
280#ifdef SLOT_ENTITY_DEBUG
281 protected ref array<ref Shape> m_DebugShapes = {};
282#endif
283 override bool _WB_OnKeyChanged(BaseContainer src, string key, BaseContainerList ownerContainers, IEntity parent)
284 {
285 if (key == "coords")
286 {
288 if (!api.UndoOrRedoIsRestoring())
289 {
290 IEntitySource entitySource = src.ToEntitySource();
291 _WB_OrientToTerrain(entitySource);
292 }
293 }
294 return false;
295 }
296#endif
297};
ref DSGameConfig game
Definition DSConfig.c:81
ArmaReforgerScripted GetGame()
Definition game.c:1398
ref array< string > angles
ResourceName resourceName
Definition SCR_AIGroup.c:66
enum SCR_ECompassType EntityEditorProps(category:"GameScripted/Gadgets", description:"Compass", color:"0 0 255 255")
Prefab data class for compass component.
bool IsOccupied()
Returns true, if slot is occupied.
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
int Type
Definition Color.c:13
Definition Debug.c:13
event array< ref WB_UIMenuItem > _WB_GetContextMenuItems()
An opportunity to append items into editor's "Entity" context menu. Do not call editor API here!
proto external WorldEditorAPI _WB_GetEditorAPI()
This returns world editor API, which is safe to use from editor events bellow.
event bool _WB_OnKeyChanged(BaseContainer src, string key, BaseContainerList ownerContainers, IEntity parent)
Any property value has been changed. You can use editor API here and do some additional edit actions ...
event void _WB_OnContextMenu(int id)
User has chosen any of your menu item from editor's "Entity" menu which you have recently provided in...
void IEntity(IEntitySource src, IEntity parent)
protected script Constructor
proto external vector GetOrigin()
proto external vector GetWorldTransformAxis(int axis)
See IEntity::GetTransformAxis.
proto external void GetBounds(out vector mins, out vector maxs)
proto external void GetWorldTransform(out vector mat[])
See IEntity::GetTransform.
proto external BaseWorld GetWorld()
proto external EntityFlags SetFlags(EntityFlags flags, bool recursively=false)
proto external EntityPrefabData GetPrefabData()
proto external string GetName()
proto external EntityFlags ClearFlags(EntityFlags flags, bool recursively=false)
proto external vector CoordToLocal(vector coord)
Definition Math.c:13
Object holding reference to resource. In destructor release the resource.
Definition Resource.c:25
void RequestNavmeshRebuildEntity(IEntity entity)
static SCR_EditableEntityComponent GetEditableEntity(IEntity owner)
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 bool IsEditMode()
Definition Functions.c:1566
Instance of created debug visualizer.
Definition Shape.c:14
void EntitySpawnParams()
Definition gameLib.c:130
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
ShapeFlags
Definition ShapeFlags.c:13
SCR_FieldOfViewSettings Attribute
EEditorTransformVertical
Vertical transformation mode.
EntityFlags
Various entity flags.
Definition EntityFlags.c:14
RespawnSystemComponentClass GameComponentClass vector vector rotation
proto external string ToString()
Plain C++ pointer, no weak pointers, no memory management.
proto native bool IsEmpty()