Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_ScenarioFrameworkTaskDeliver.c
Go to the documentation of this file.
1 //------------------------------------------------------------------------------------------------
3 {
4 };
5 
6 /*
7  TASK DELIVERY STATES
8  -----------------------------------------
9  | Delivery | Item | Item |
10  | Point exists | possesed | Dropped |
11  -----------------------------------------
12  | 0 | 0 | 0 | 0
13  -----------------------------------------
14  | 0 | 0 | 1 | 1
15  -----------------------------------------
16  | 0 | 1 | 0 | 2
17  -----------------------------------------
18  | 1 | 0 | 0 | 4
19  -----------------------------------------
20  | 1 | 0 | 1 | 5
21  -----------------------------------------
22  | 1 | 1 | 0 | 6
23  -----------------------------------------
24 */
25 
26 //------------------------------------------------------------------------------------------------
28 {
29  protected string m_sDeliveryTriggerName;
30 
31  protected int m_iObjectState = 0; //see the table above
32  protected SCR_BaseTriggerEntity m_TriggerDeliver;
33  protected bool m_bDeliveryItemFound;
34  protected bool m_bTaskPositionUpdated;
35 
36  //------------------------------------------------------------------------------------------------
37  // return 0 if item is not possesed and 1 if possesed (someone has it in his possesion)
38  int GetTaskDeliverState()
39  {
40  return m_iObjectState;
41  }
42 
43  //------------------------------------------------------------------------------------------------
44  void SetDeliveryTrigger()
45  {
46  if (!GetGame().GetWorld())
47  return;
48 
49  if (m_sDeliveryTriggerName.IsEmpty())
50  {
51  Print("ScenarioFramework: Task Deliver trigger is set with empty attribute SetDeliveryTrigger", LogLevel.ERROR);
52  return;
53  }
54 
55  IEntity entity = GetGame().GetWorld().FindEntityByName(m_sDeliveryTriggerName);
56  if (!entity)
57  return;
58 
59  SetDeliveryTrigger(SCR_BaseTriggerEntity.Cast(entity));
60  }
61 
62  //------------------------------------------------------------------------------------------------
63  void SetDeliveryTrigger(SCR_BaseTriggerEntity trigger)
64  {
65  if (!trigger)
66  return;
67 
68  m_TriggerDeliver = trigger;
69  trigger.GetOnActivate().Insert(OnDeliveryTriggerActivated);
70  }
71 
72  //------------------------------------------------------------------------------------------------
73  void SetTriggerNameToDeliver(string triggerName)
74  {
75  if (triggerName.IsEmpty())
76  {
77  Print("ScenarioFramework: Task Deliver trigger is set with empty attribute SetTriggerNameToDeliver", LogLevel.ERROR);
78  return;
79  }
80 
81  m_sDeliveryTriggerName = triggerName;
82  }
83 
84  //------------------------------------------------------------------------------------------------
85  string GetTriggerNameToDeliver()
86  {
87  return m_sDeliveryTriggerName;
88  }
89 
90  //------------------------------------------------------------------------------------------------
91  void OnDeliveryTriggerActivated(notnull SCR_CharacterTriggerEntity trigger)
92  {
93  if (!m_SupportEntity || !m_Asset)
94  return;
95 
96  array<IEntity> entitiesInside = {};
97  array<SCR_ChimeraCharacter> chimeraCharacters = {};
98  trigger.GetEntitiesInside(entitiesInside);
99  foreach (IEntity entity : entitiesInside)
100  {
101  if (entity == m_Asset)
102  {
103  m_SupportEntity.FinishTask(this);
104  return;
105  }
106 
107  if (SCR_ChimeraCharacter.Cast(entity))
108  chimeraCharacters.Insert(SCR_ChimeraCharacter.Cast(entity))
109  }
110 
111  foreach (SCR_ChimeraCharacter character : chimeraCharacters)
112  {
114  if (!inventoryComponent)
115  continue;
116 
117  if (inventoryComponent.Contains(m_Asset))
118  {
119  m_SupportEntity.FinishTask(this);
120  break;
121  }
122  }
123  }
124 
125  //------------------------------------------------------------------------------------------------
126  void OnObjectPossessed(IEntity item, BaseInventoryStorageComponent pStorageOwner)
127  {
128  if(!item || item != m_Asset)
129  return;
130 
131  UpdateTaskTitleAndDescription(1);
132  if (!m_bDeliveryItemFound)
133  {
134  m_bDeliveryItemFound = true;
135  SetState(SCR_TaskState.PROGRESSED);
136  }
137  }
138 
139  //------------------------------------------------------------------------------------------------
140  void OnObjectDropped(IEntity item, BaseInventoryStorageComponent pStorageOwner)
141  {
142  if(!item || item != m_Asset)
143  return;
144 
145  SetState(SCR_TaskState.UPDATED);
146  UpdateTaskTitleAndDescription(0);
147  }
148 
149  //------------------------------------------------------------------------------------------------
150  void UpdateTaskTitleAndDescription(int iPossessed = -1)
151  {
152  if (iPossessed == -1)
153  {
154  //the request comes from outside (most likely the Delivery point has been created)
155  //check if the item has been already possessed or not
156  if (m_iObjectState & 0x01)
157  iPossessed = 1;
158  else
159  iPossessed = 0;
160  }
161 
162  //Decide based on the task state - see the table on top of the class
163  if (!m_TriggerDeliver)
164  {
165  //delivery point still doesn't exist
166  if (iPossessed == 1)
167  {
168  m_iObjectState = 1;
169  }
170  else
171  {
172  //dropped
173  m_iObjectState = 2;
174  if (!m_bTaskPositionUpdated)
175  UpdateDroppedTaskMarker();
176  }
177  }
178  else
179  {
180  if (iPossessed == 1)
181  {
182  m_iObjectState = 5;
183  m_SupportEntity.MoveTask(m_TriggerDeliver.GetOrigin(), this.GetTaskID());
184  }
185  else
186  {
187  //dropped
188  m_iObjectState = 6;
189  if (!m_bTaskPositionUpdated)
190  UpdateDroppedTaskMarker();
191  }
192  }
193 
194  if (!m_LayerTask)
195  return;
196 
197  SCR_ScenarioFrameworkSlotTask subject = m_LayerTask.GetSlotTask();
198  if (!subject)
199  return;
200 
201  if (GetTitle() != subject.GetTaskTitle(m_iObjectState))
202  m_SupportEntity.SetTaskTitle(this, subject.GetTaskTitle(m_iObjectState));
203 
204  if (GetDescription() != subject.GetTaskDescription(m_iObjectState))
205  m_SupportEntity.SetTaskDescription(this, subject.GetTaskDescription(m_iObjectState));
206  }
207 
208  //------------------------------------------------------------------------------------------------
209  protected void UpdateDroppedTaskMarker()
210  {
211  m_bTaskPositionUpdated = true;
213  if (!layerTaskDeliver)
214  {
215  Print("ScenarioFramework: Task Deliver failed to access LayerTask", LogLevel.ERROR);
216  return;
217  }
218 
219  //We want to delay position movement of the Task marker on the map by given time
220  GetGame().GetCallqueue().CallLater(MoveTaskMarkerPosition, 1000 * layerTaskDeliver.GetIntelMapMarkerUpdateDelay(), false);
221  }
222 
223  //------------------------------------------------------------------------------------------------
224  protected void MoveTaskMarkerPosition()
225  {
226  m_bTaskPositionUpdated = false;
227  if (m_Asset)
228  m_SupportEntity.MoveTask(m_Asset.GetOrigin(), this.GetTaskID());
229  else
230  Print("ScenarioFramework: Task Deliver does not have m_Asset properly assigned for MoveTaskMarkerPosition", LogLevel.ERROR);
231  }
232 
233  //------------------------------------------------------------------------------------------------
234  override void UpdateMapTaskIcon()
235  {
236  super.UpdateMapTaskIcon();
237  if (!GetTaskIconkWidget())
238  return;
239 
240  if (m_iObjectState == 1)
241  {
242  GetTaskIconkWidget().SetOpacity(0); //hide the icon on map until the delivery point isn't created
243  GetTaskIconkWidget().SetVisible(false);
244  }
245  else
246  {
247  GetTaskIconkWidget().SetOpacity(1);
248  GetTaskIconkWidget().SetVisible(true);
249  }
250  }
251 
252  //------------------------------------------------------------------------------------------------
253  protected void OnItemCarrierChanged(InventoryStorageSlot oldSlot, InventoryStorageSlot newSlot)
254  {
255  if (oldSlot)
256  {
257  EventHandlerManagerComponent EventHandlerMgr = EventHandlerManagerComponent.Cast(oldSlot.GetOwner().FindComponent(EventHandlerManagerComponent));
258  if (EventHandlerMgr)
259  EventHandlerMgr.RemoveScriptHandler("OnDestroyed", this, OnDestroyed);
260  }
261 
262  if (newSlot)
263  {
264  EventHandlerManagerComponent EventHandlerMgr = EventHandlerManagerComponent.Cast(newSlot.GetOwner().FindComponent(EventHandlerManagerComponent));
265  if (EventHandlerMgr)
266  EventHandlerMgr.RegisterScriptHandler("OnDestroyed", this, OnDestroyed);
267  }
268  }
269 
270  //------------------------------------------------------------------------------------------------
272  protected void OnDestroyed(IEntity destroyedEntity)
273  {
274  if (!destroyedEntity)
275  return;
276 
277  if (!m_Asset)
278  return;
279 
280  InventoryItemComponent invComp = InventoryItemComponent.Cast(m_Asset.FindComponent(InventoryItemComponent));
281  if (!invComp)
282  return;
283 
284  InventoryStorageSlot parentSlot = invComp.GetParentSlot();
285  if (!parentSlot)
286  return;
287 
288  InventoryStorageManagerComponent inventoryComponent = InventoryStorageManagerComponent.Cast(destroyedEntity.FindComponent(InventoryStorageManagerComponent));
289  if (!inventoryComponent)
290  return;
291 
292  if (inventoryComponent.Contains(m_Asset))
293  {
294  inventoryComponent.TryRemoveItemFromStorage(m_Asset, parentSlot.GetStorage());
295  m_Asset.SetOrigin(destroyedEntity.GetOrigin());
296  return;
297  }
298  }
299 
300  //------------------------------------------------------------------------------------------------
301  protected void OnDisconnected(int iPlayerID)
302  {
303  IEntity player = GetGame().GetPlayerManager().GetPlayerControlledEntity(iPlayerID);
304  if (!player)
305  return;
306 
307  OnDestroyed(player);
308  }
309 
310  //------------------------------------------------------------------------------------------------
311  protected void RegisterPlayer(int iPlayerID, IEntity playerEntity)
312  {
313  IEntity player = GetGame().GetPlayerManager().GetPlayerControlledEntity(iPlayerID);
314  if (!player)
315  return;
316 
317  SCR_InventoryStorageManagerComponent inventoryComponent = SCR_InventoryStorageManagerComponent.Cast(player.FindComponent(SCR_InventoryStorageManagerComponent));
318  if (!inventoryComponent)
319  return;
320 
321  inventoryComponent.m_OnItemAddedInvoker.Insert(OnObjectPossessed);
322  inventoryComponent.m_OnItemRemovedInvoker.Insert(OnObjectDropped);
323 
324  EventHandlerManagerComponent EventHandlerMgr = EventHandlerManagerComponent.Cast(player.FindComponent(EventHandlerManagerComponent));
325  if (EventHandlerMgr)
326  EventHandlerMgr.RegisterScriptHandler("OnDestroyed", this, OnDestroyed);
327  }
328 
329  //------------------------------------------------------------------------------------------------
330  override bool SetSupportEntity()
331  {
333 
334  if (!m_SupportEntity)
335  {
336  Print("ScenarioFramework: Task Deliver support entity not found in the world, task won't be created!", LogLevel.ERROR);
337  return false;
338  }
339 
340  return true;
341  }
342 
343  //------------------------------------------------------------------------------------------------
344  override void Init()
345  {
346  super.Init();
347 
348  if (!m_Asset)
349  return;
350 
351  InventoryItemComponent invComp = InventoryItemComponent.Cast(m_Asset.FindComponent(InventoryItemComponent));
352  if (!invComp)
353  return;
354 
355  invComp.m_OnParentSlotChangedInvoker.Insert(OnItemCarrierChanged);
356 
357  array<int> aPlayerIDs = {};
358  int iNrOfPlayersConnected = GetGame().GetPlayerManager().GetPlayers(aPlayerIDs);
359 
360  foreach (int i : aPlayerIDs)
361  {
362  RegisterPlayer(i, null);
363  }
364 
366  if (!gameMode)
367  return;
368 
369  gameMode.GetOnPlayerSpawned().Insert(RegisterPlayer);
370  gameMode.GetOnPlayerDisconnected().Insert(OnDisconnected);
371  }
372 
373  //------------------------------------------------------------------------------------------------
374  protected void InvokedSetDeliveryTrigger()
375  {
376  SetDeliveryTrigger();
377  }
378 }
SCR_BaseGameMode
Definition: SCR_BaseGameMode.c:137
InventoryStorageSlot
Definition: InventoryStorageSlot.c:12
SCR_ScenarioFrameworkLayerTaskDeliver
void SCR_ScenarioFrameworkLayerTaskDeliver(IEntityComponentSource src, IEntity ent, IEntity parent)
Definition: SCR_ScenarioFrameworkLayerTaskDeliver.c:93
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
GetGameMode
SCR_BaseGameMode GetGameMode()
Definition: SCR_BaseGameModeComponent.c:15
InventoryStorageManagerComponent
Definition: InventoryStorageManagerComponent.c:12
GetTaskManager
SCR_BaseTaskManager GetTaskManager()
Definition: SCR_BaseTaskManager.c:7
SCR_TaskDeliver
Definition: SCR_ScenarioFrameworkTaskDeliver.c:27
SCR_TaskDeliverClass
Definition: SCR_ScenarioFrameworkTaskDeliver.c:2
InventoryItemComponent
Definition: InventoryItemComponent.c:12
SCR_CharacterTriggerEntity
Definition: SCR_CharacterTriggerEntity.c:17
SCR_TaskState
SCR_TaskState
Definition: SCR_TaskState.c:2
SCR_ScenarioFrameworkTaskDeliverSupportEntity
Definition: SCR_ScenarioFrameworkTaskDeliverSupportEntity.c:8
SCR_ScenarioFrameworkTask
Definition: SCR_ScenarioFrameworkTask.c:7
SCR_ScenarioFrameworkTaskClass
Definition: SCR_ScenarioFrameworkTask.c:2