Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_ResourceConsumer.c
Go to the documentation of this file.
2 class SCR_ResourceActor : ScriptAndConfig
3 {
4  static const float RESOURCES_LOWER_LIMIT = 0.0;
5  protected static const float UPDATE_PERIOD = 10.0 / 60.0;
6  protected static const int CODEC_GENERATOR_PACKET_BYTESIZE = 28;
7 
8  [Attribute(defvalue: string.Empty, uiwidget: UIWidgets.EditBox, desc: "Identifier for debug prints", category: "Debugging")]
9  string m_sDebugName;
10 
11  [Attribute(defvalue: EResourceRights.NONE.ToString(), uiwidget: UIWidgets.ComboBox, desc: "Limits the taking of resources to a specific group", enums: ParamEnumArray.FromEnum(EResourceRights))]
12  protected EResourceRights m_eResourceRights;
13 
14  [Attribute(defvalue: EResourceType.SUPPLIES.ToString(), uiwidget: UIWidgets.ComboBox, desc: "Sets the type of Resource to be used.\nOnly a transaction matching Resource types can be successfully concluded.", enums: ParamEnumArray.FromEnum(EResourceType))]
15  protected EResourceType m_eResourceType;
16 
17  protected SCR_ResourceComponent m_ResourceComponent;
18  protected IEntity m_Owner;
19 
20  //------------------------------------------------------------------------------------------------
22  Color GetDebugColor()
23  {
24  return Color.FromInt(m_ResourceComponent.GetDebugColor().PackToInt());
25  }
26 
27  //------------------------------------------------------------------------------------------------
29  int GetGridUpdateId()
30  {
31  return m_ResourceComponent.GetGridUpdateId();
32  }
33 
34  //------------------------------------------------------------------------------------------------
36  vector GetOwnerOrigin()
37  {
38  // TODO: Make it so that the nullity of this never happen in the first place.
39  if (m_Owner)
40  return m_Owner.GetOrigin();
41 
42  return vector.Zero;
43  }
44 
45  //------------------------------------------------------------------------------------------------
47  string GetDebugName()
48  {
49  return m_sDebugName;
50  }
51 
52  //------------------------------------------------------------------------------------------------
54  int GetDebugNameHash()
55  {
56  return m_sDebugName.Hash();
57  }
58 
59  //------------------------------------------------------------------------------------------------
61  SCR_ResourceComponent GetComponent()
62  {
63  return m_ResourceComponent;
64  }
65 
66  //------------------------------------------------------------------------------------------------
68  IEntity GetOwner()
69  {
70  return m_Owner;
71  }
72 
73  //------------------------------------------------------------------------------------------------
75  EResourceRights GetResourceRight()
76  {
77  return m_eResourceRights;
78  }
79 
80  //------------------------------------------------------------------------------------------------
83  bool ShouldUpdate()
84  {
85  return false;
86  }
87 
88  //------------------------------------------------------------------------------------------------
90  bool IsIsolated()
91  {
92  return m_eResourceRights == EResourceRights.SELF || m_eResourceRights == EResourceRights.NONE;
93  }
94 
95  //------------------------------------------------------------------------------------------------
98  bool IsGridUpdateIdGreaterThan(int gridUpdateId)
99  {
100  return m_ResourceComponent.IsGridUpdateIdGreaterThan(gridUpdateId);
101  }
102 
103  //------------------------------------------------------------------------------------------------
105  void SetResourceRights(EResourceRights rights)
106  {
107  m_eResourceRights = rights;
108  }
109 
110  //------------------------------------------------------------------------------------------------
112  void SetGridUpdateId(int gridUpdateId)
113  {
114  m_ResourceComponent.SetGridUpdateId(gridUpdateId);
115  }
116 
117  //------------------------------------------------------------------------------------------------
120  void Update(float timeslice)
121  {
122 
123  }
124 
125  //------------------------------------------------------------------------------------------------
128  void UpdateInner(float timeslice)
129  {
130 
131  }
132 
133  //------------------------------------------------------------------------------------------------
135  void Clear()
136  {
137 
138  }
139 
140  //------------------------------------------------------------------------------------------------
141  // destructor
142  void ~SCR_ResourceActor()
143  {
144  Clear();
145  }
146 }
147 
149 class SCR_ResourceInteractor : SCR_ResourceActor
150 {
151  protected ref ScriptInvoker m_OnResourcesChangedInvoker;
152  protected ref ScriptInvoker m_OnMaxResourcesChangedInvoker;
153  protected int m_iGridUpdateId = int.MIN;
155  protected float m_fAggregatedResourceValue = -1.0;
156  protected float m_fAggregatedMaxResourceValue = -1.0;
157  protected vector m_LastPosition = vector.Zero;
158 
159  //------------------------------------------------------------------------------------------------
160  override int GetGridUpdateId()
161  {
162  return m_iGridUpdateId;
163  }
164 
165  //------------------------------------------------------------------------------------------------
167  float GetResourceGridRange()
168  {
169  return 0.0;
170  }
171 
172  //------------------------------------------------------------------------------------------------
174  float GetAggregatedResourceValue()
175  {
176  return 0.0;
177  }
178 
179  //------------------------------------------------------------------------------------------------
181  float GetAggregatedMaxResourceValue()
182  {
183  return 0.0;
184  }
185 
186  //------------------------------------------------------------------------------------------------
188  int GetContainerCount()
189  {
190  return 0;
191  }
192 
193  //------------------------------------------------------------------------------------------------
195  vector GetLastPosition()
196  {
197  return m_LastPosition;
198  }
199 
200  //------------------------------------------------------------------------------------------------
202  EResourceType GetResourceType()
203  {
204  return m_eResourceType;
205  }
206 
207  //------------------------------------------------------------------------------------------------
209  SCR_ResourceContainerQueueBase GetContainerQueue()
210  {
211  return null;
212  }
213 
214  //------------------------------------------------------------------------------------------------
216  ScriptInvoker GetOnResourcesChanged()
217  {
218  if (!m_OnResourcesChangedInvoker)
219  m_OnResourcesChangedInvoker = new ScriptInvoker();
220 
221  return m_OnResourcesChangedInvoker;
222 
223  }
224 
225  //------------------------------------------------------------------------------------------------
227  ScriptInvoker GetOnMaxResourcesChanged()
228  {
229  if (!m_OnMaxResourcesChangedInvoker)
230  m_OnMaxResourcesChangedInvoker = new ScriptInvoker();
231 
232  return m_OnMaxResourcesChangedInvoker;
233 
234  }
235 
236  //------------------------------------------------------------------------------------------------
238  EResourceGeneratorID GetIdentifier()
239  {
240  return EResourceGeneratorID.INVALID;
241  }
242 
243  //------------------------------------------------------------------------------------------------
244  override bool IsGridUpdateIdGreaterThan(int gridUpdateId)
245  {
246  return m_iGridUpdateId > gridUpdateId;
247  }
248 
249  //------------------------------------------------------------------------------------------------
252  bool IsAllowed(notnull SCR_ResourceContainer container)
253  {
254  if (container.GetResourceType() != m_eResourceType)
255  return false;
256 
257  switch (m_eResourceRights)
258  {
259  case EResourceRights.NONE:
260  return false;
261 
262  case EResourceRights.SELF:
263  return m_Owner == container.GetOwner();
264 
265  case EResourceRights.SQUAD:
266  if (container.GetResourceRight() == EResourceRights.ALL)
267  return true;
268  // TODO: Logic for detecting the squad.
269  return false;
270 
271  case EResourceRights.FACTION:
272  if (container.GetResourceRight() == EResourceRights.ALL)
273  return true;
274 
275  FactionAffiliationComponent interactorFactionComponent = m_ResourceComponent.GetFactionAffiliationComponent();
276 
277  if (!interactorFactionComponent)
278  return false;
279 
280  FactionAffiliationComponent containerrFactionComponent = container.GetComponent().GetFactionAffiliationComponent();
281 
282  if (!containerrFactionComponent)
283  return false;
284 
285  return interactorFactionComponent.GetAffiliatedFaction() == containerrFactionComponent.GetAffiliatedFaction();
286 
287  case EResourceRights.ALL:
288  return true;
289  }
290 
291  return true;
292  }
293 
294  //------------------------------------------------------------------------------------------------
298  bool IsAllowed(notnull IEntity entity, EResourceType resourceType)
299  {
300  if (resourceType != m_eResourceType)
301  return false;
302 
303  switch (m_eResourceRights)
304  {
305  case EResourceRights.NONE:
306  return false;
307 
308  case EResourceRights.SELF:
309  return m_Owner == entity;
310 
311  case EResourceRights.SQUAD:
312  // TODO: Logic for detecting the squad.
313  case EResourceRights.FACTION:
314  FactionAffiliationComponent interactorFactionComponent = m_ResourceComponent.GetFactionAffiliationComponent();
315 
316  if (!interactorFactionComponent)
317  return false;
318 
319  SCR_ResourceComponent containerResourceComponent = SCR_ResourceComponent.Cast(entity.FindComponent(SCR_ResourceComponent));
320 
321  if (!containerResourceComponent)
322  return false;
323 
324  SCR_ResourceContainer container;
325 
326  if (!containerResourceComponent.GetContainer(resourceType, container))
327  return false;
328 
329  if (container.GetResourceRight() == EResourceRights.ALL)
330  return true;
331 
332  FactionAffiliationComponent containerrFactionComponent = container.GetComponent().GetFactionAffiliationComponent();
333 
334  if (!containerrFactionComponent)
335  return false;
336 
337  return interactorFactionComponent.GetAffiliatedFaction() == containerrFactionComponent.GetAffiliatedFaction();
338 
339  case EResourceRights.ALL:
340  return true;
341  }
342 
343  return true;
344  }
345 
346  //------------------------------------------------------------------------------------------------
350  bool CanInteractWith(notnull SCR_ResourceContainer container)
351  {
352  return IsAllowed(container) && container.IsAllowed(this);
353  }
354 
355  //------------------------------------------------------------------------------------------------
359  int FindContainer(notnull SCR_ResourceContainer container)
360  {
361  return SCR_ResourceContainerQueueBase.INVALID_CONTAINER_INDEX;
362  }
363 
364  //------------------------------------------------------------------------------------------------
365  override void SetGridUpdateId(int gridUpdateId)
366  {
367  if (m_iGridUpdateId > gridUpdateId)
368  return;
369 
370  m_iGridUpdateId = gridUpdateId;
371  }
372 
373  //------------------------------------------------------------------------------------------------
375  void UpdateLastPosition()
376  {
377  m_LastPosition = GetOwnerOrigin();
378  }
379 
380  //------------------------------------------------------------------------------------------------
384  bool RegisterContainer(notnull SCR_ResourceContainer container)
385  {
386  return false;
387  }
388 
389  //------------------------------------------------------------------------------------------------
393  bool RegisterContainerForced(notnull SCR_ResourceContainer container)
394  {
395  return false;
396  }
397 
398  //------------------------------------------------------------------------------------------------
402  bool UnregisterContainer(int containerIndex)
403  {
404  return false;
405  }
406 
407  //------------------------------------------------------------------------------------------------
411  bool UnregisterContainer(notnull SCR_ResourceContainer container)
412  {
413  return false;
414  }
415 
416  //------------------------------------------------------------------------------------------------
418  void Replicate()
419  {
420  m_ResourceComponent.Replicate();
421  }
422 
423  //------------------------------------------------------------------------------------------------
425  void ReplicateEx()
426  {
427  m_ResourceComponent.ReplicateEx();
428  }
429 
430  //------------------------------------------------------------------------------------------------
434  void UpdateContainerResourceValue(SCR_ResourceContainer container, float previousValue);
435 
436  //------------------------------------------------------------------------------------------------
440  void UpdateContainerMaxResourceValue(SCR_ResourceContainer container, float previousValue);
441 
442  //------------------------------------------------------------------------------------------------
444  void OnResourcesChanged(float previousValue)
445  {
446  m_fAggregatedResourceValue = GetAggregatedResourceValue();
447 
448  if (m_OnResourcesChangedInvoker)
449  m_OnResourcesChangedInvoker.Invoke(this, previousValue);
450 
451  if (!m_ResourceComponent)
452  return;
453 
454  m_ResourceComponent.Replicate();
455  }
456 
457  //------------------------------------------------------------------------------------------------
459  void OnMaxResourcesChanged(float previousValue)
460  {
461  m_fAggregatedMaxResourceValue = GetAggregatedMaxResourceValue();
462 
463  if (m_OnMaxResourcesChangedInvoker)
464  m_OnMaxResourcesChangedInvoker.Invoke(this, previousValue);
465 
466  if (!m_ResourceComponent)
467  return;
468 
469  m_ResourceComponent.Replicate();
470  }
471 
472  //------------------------------------------------------------------------------------------------
474  void OnContainerRegistered(notnull SCR_ResourceContainer container)
475  {
476  container.LinkInteractor(this);
477  }
478 
479  //------------------------------------------------------------------------------------------------
481  void OnContainerUnregistered(notnull SCR_ResourceContainer container)
482  {
483  container.UnlinkInteractor(this);
484  }
485 
486  //------------------------------------------------------------------------------------------------
489  void Initialize(notnull IEntity owner)
490  {
491  m_Owner = owner;
492  m_ResourceComponent = SCR_ResourceComponent.Cast(owner.FindComponent(SCR_ResourceComponent));
493  }
494 
495  //------------------------------------------------------------------------------------------------
496  override void Clear()
497  {
498  super.Clear();
499 
500  m_fAggregatedResourceValue = -1.0;
501  m_fAggregatedMaxResourceValue = -1.0;
502  }
503 }
504 
505 [BaseContainerProps(configRoot: true)]
506 class SCR_ResourceConsumer : SCR_ResourceInteractor
507 {
508  protected static const int CODEC_CONSUMER_PACKET_BYTESIZE = 38;
509 
510  [Attribute(defvalue: EResourceGeneratorID.DEFAULT_STORAGE.ToString(), uiwidget: UIWidgets.ComboBox, desc: "Identifier for the generator used for storage", enums: ParamEnumArray.FromEnum(EResourceGeneratorID))]
511  protected EResourceGeneratorID m_eGeneratorIdentifier;
512 
513  [Attribute(defvalue: "0.0", uiwidget: UIWidgets.SpinBox, desc: "Sets the range in which Resource is sought.",params: "0.0 10000.0 1.0")]
514  protected float m_fResourceRange;
515 
516  [Attribute("1", uiwidget: UIWidgets.SpinBox, params: string.Format("0.0 %1 1.0", float.MAX))]
517  protected float m_fBuyMultiplier;
518 
519  [Attribute(uiwidget: UIWidgets.SpinBox, params: string.Format("0.0 %1 1.0", float.MAX))]
520  protected float m_fSellMultiplier;
521 
522  [Attribute(uiwidget: UIWidgets.CheckBox)]
523  protected bool m_bIsIgnoringItself;
524 
525  [Attribute(uiwidget: UIWidgets.Object)]
526  protected ref SCR_ResourceConsumerContainerQueue m_ContainerQueue;
527 
528  protected bool m_bIsConsuming;
529  protected bool m_bIsExchanging;
530  protected ref ScriptInvoker m_OnResourceRangeChangedInvoker;
531  protected ref ScriptInvoker m_OnBuyMultiplierChangedInvoker;
532  protected ref ScriptInvoker m_OnSellMultiplierChangedInvoker;
533  protected ref ScriptInvoker m_OnConsumtionStateChangedInvoker; // TODO: Consumtion -> Consumption
534  protected ref ScriptInvoker m_OnExchangeStateChangedInvoker;
535 
536  //------------------------------------------------------------------------------------------------
537  override float GetResourceGridRange()
538  {
539  return m_fResourceRange;
540  }
541 
542  //------------------------------------------------------------------------------------------------
544  float GetResourceRange()
545  {
546  return m_fResourceRange;
547  }
548 
549  //------------------------------------------------------------------------------------------------
551  float GetBuyMultiplier()
552  {
553  return m_fBuyMultiplier;
554  }
555 
556  //------------------------------------------------------------------------------------------------
558  float GetSellMultiplier()
559  {
560  return m_fSellMultiplier;
561  }
562 
563  //------------------------------------------------------------------------------------------------
564  override float GetAggregatedResourceValue()
565  {
566  if (!m_ContainerQueue)
567  return m_fAggregatedResourceValue;
568 
569  return m_ContainerQueue.GetAggregatedResourceValue();
570  }
571 
572  //------------------------------------------------------------------------------------------------
573  override float GetAggregatedMaxResourceValue()
574  {
575  if (!m_ContainerQueue)
576  return m_fAggregatedMaxResourceValue;
577 
578  return m_ContainerQueue.GetAggregatedMaxResourceValue();
579  }
580 
581  //------------------------------------------------------------------------------------------------
582  override int GetContainerCount()
583  {
584  if (!m_ContainerQueue)
585  return 0.0;
586 
587  return m_ContainerQueue.GetContainerCount();
588  }
589 
590  //------------------------------------------------------------------------------------------------
591  override SCR_ResourceContainerQueueBase GetContainerQueue()
592  {
593  return m_ContainerQueue;
594  }
595 
596  //------------------------------------------------------------------------------------------------
598  ScriptInvoker GetOnResourceRangeChanged()
599  {
600  if (!m_OnResourceRangeChangedInvoker)
601  m_OnResourceRangeChangedInvoker = new ScriptInvoker();
602 
603  return m_OnResourceRangeChangedInvoker;
604  }
605 
606  //------------------------------------------------------------------------------------------------
608  ScriptInvoker GetOnBuyMultiplierChanged()
609  {
610  if (!m_OnBuyMultiplierChangedInvoker)
611  m_OnBuyMultiplierChangedInvoker = new ScriptInvoker();
612 
613  return m_OnBuyMultiplierChangedInvoker;
614  }
615 
616  //------------------------------------------------------------------------------------------------
618  ScriptInvoker GetOnSellMultiplierChanged()
619  {
620  if (!m_OnSellMultiplierChangedInvoker)
621  m_OnSellMultiplierChangedInvoker = new ScriptInvoker();
622 
623  return m_OnSellMultiplierChangedInvoker;
624  }
625 
626  //------------------------------------------------------------------------------------------------
628  // TODO: Consumtion -> Consumption
629  ScriptInvoker GetOnConsumtionStateChanged()
630  {
631  if (!m_OnConsumtionStateChangedInvoker)
632  m_OnConsumtionStateChangedInvoker = new ScriptInvoker();
633 
634  return m_OnConsumtionStateChangedInvoker;
635  }
636 
637  //------------------------------------------------------------------------------------------------
639  ScriptInvoker GetOnExchangeStateChanged()
640  {
641  if (!m_OnExchangeStateChangedInvoker)
642  m_OnExchangeStateChangedInvoker = new ScriptInvoker();
643 
644  return m_OnExchangeStateChangedInvoker;
645  }
646 
647  //------------------------------------------------------------------------------------------------
649  EResourceGeneratorID GetGeneratorIdentifier()
650  {
651  return m_eGeneratorIdentifier;
652  }
653 
654  //------------------------------------------------------------------------------------------------
655  override EResourceGeneratorID GetIdentifier()
656  {
657  return m_eGeneratorIdentifier;
658  }
659 
660  //------------------------------------------------------------------------------------------------
662  bool IsIgnoringItself()
663  {
664  return m_bIsIgnoringItself;
665  }
666 
667  //------------------------------------------------------------------------------------------------
669  bool IsConsuming()
670  {
671  return m_bIsConsuming;
672  }
673 
674  //------------------------------------------------------------------------------------------------
676  bool IsExchanging()
677  {
678  return m_bIsExchanging;
679  }
680 
681  //------------------------------------------------------------------------------------------------
682  override bool ShouldUpdate()
683  {
684  return false;
685  }
686 
687  //------------------------------------------------------------------------------------------------
688  override bool CanInteractWith(notnull SCR_ResourceContainer container)
689  {
690  return (!m_bIsIgnoringItself || container.GetOwner() != m_Owner) && super.CanInteractWith(container);
691  }
692 
693  //------------------------------------------------------------------------------------------------
694  override int FindContainer(notnull SCR_ResourceContainer container)
695  {
696  if (!m_ContainerQueue)
697  return super.FindContainer(container);
698 
699  return m_ContainerQueue.FindContainer(container);
700  }
701 
702  //------------------------------------------------------------------------------------------------
706  bool SetResourceRange(float value, bool notifyChange = true)
707  {
708  float previousValue = m_fResourceRange;
709  m_fResourceRange = Math.Max(value, 0.0);
710 
711  if (previousValue == m_fResourceRange)
712  return false;
713 
714  if (notifyChange)
715  OnResourceRangeChanged(previousValue);
716 
717  return true;
718  }
719 
720  //------------------------------------------------------------------------------------------------
724  bool SetBuyMultiplier(float value, bool notifyChange = true)
725  {
726  float previousValue = m_fBuyMultiplier;
727  m_fBuyMultiplier = Math.Max(value, 0.0);
728 
729  if (previousValue == m_fBuyMultiplier)
730  return false;
731 
732  if (notifyChange)
733  OnBuyMultiplierChanged(previousValue);
734 
735  return true;
736  }
737 
738  //------------------------------------------------------------------------------------------------
742  bool SetSellMultiplier(float value, bool notifyChange = true)
743  {
744  float previousValue = m_fSellMultiplier;
745  m_fSellMultiplier = Math.Max(value, 0.0);
746 
747  if (previousValue == m_fSellMultiplier)
748  return false;
749 
750  if (notifyChange)
751  OnSellMultiplierChanged(previousValue);
752 
753  return true;
754  }
755 
756  //------------------------------------------------------------------------------------------------
761  // TODO: Consumtion -> Consumption
762  bool EnableConsumtion(bool shouldEnable, bool notifyChange = true)
763  {
764  bool previousValue = m_bIsConsuming;
765  m_bIsConsuming = shouldEnable;
766 
767  if (shouldEnable == m_bIsConsuming)
768  return false;
769 
770  if (notifyChange)
771  OnConsumtionStateChanged(previousValue);
772 
773  return true;
774  }
775 
776  //------------------------------------------------------------------------------------------------
781  bool EnableExchange(bool shouldEnable, bool notifyChange = true)
782  {
783  bool previousValue = m_bIsExchanging;
784  m_bIsExchanging = shouldEnable;
785 
786  if (previousValue == m_bIsExchanging)
787  return false;
788 
789  if (notifyChange)
790  OnExchangeStateChanged(previousValue);
791 
792  return true;
793  }
794 
795  //------------------------------------------------------------------------------------------------
796  override bool RegisterContainer(notnull SCR_ResourceContainer container)
797  {
798  if (CanInteractWith(container)
799  && m_ContainerQueue
800  && m_ContainerQueue.RegisterContainer(container) != SCR_ResourceContainerQueueBase.INVALID_CONTAINER_INDEX)
801  {
802  OnContainerRegistered(container);
803 
804  return true;
805  }
806 
807  return false;
808  }
809 
810  //------------------------------------------------------------------------------------------------
811  override bool RegisterContainerForced(notnull SCR_ResourceContainer container)
812  {
813  if (m_ContainerQueue && m_ContainerQueue.RegisterContainer(container) != SCR_ResourceContainerQueueBase.INVALID_CONTAINER_INDEX)
814  {
815  OnContainerRegistered(container);
816 
817  return true;
818  }
819 
820  return false;
821  }
822 
823  //------------------------------------------------------------------------------------------------
824  override bool UnregisterContainer(int containerIndex)
825  {
826  return m_ContainerQueue && m_ContainerQueue.PopContainerAt(containerIndex);
827  }
828 
829  //------------------------------------------------------------------------------------------------
830  override bool UnregisterContainer(notnull SCR_ResourceContainer container)
831  {
832  return m_ContainerQueue && m_ContainerQueue.PopContainerAt(m_ContainerQueue.FindContainer(container));
833  }
834 
835  //------------------------------------------------------------------------------------------------
840  SCR_ResourceConsumtionResponse RequestAvailability(float resourceCost, bool performFullQuery = false)
841  {
842  GetGame().GetResourceGrid().UpdateInteractor(this);
843 
844  SCR_ResourceConsumtionResponse response = new SCR_ResourceConsumtionResponse(GetAggregatedResourceValue(), m_fBuyMultiplier, m_fResourceRange, EResourceReason.SUFFICIENT);
845 
846  if (!m_bIsConsuming)
847  return response;
848 
849  if (resourceCost > response.GetAvailableSupply())
850  {
851  response.SetReason(EResourceReason.INSUFICIENT);
852 
853  return response;
854  }
855 
856  if (resourceCost != 0.0 && response.GetAvailableSupply() == 0)
857  {
858  response.SetReason(EResourceReason.UNAVAILABLE);
859 
860  return response;
861  }
862 
863  return response;
864  }
865 
866  //------------------------------------------------------------------------------------------------
870  // TODO: Consumtion -> Consumption
871  SCR_ResourceConsumtionResponse RequestConsumtion(float resourceCost)
872  {
873  SCR_ResourceConsumtionResponse response = RequestAvailability(resourceCost, true);
874 
875  if (!response || response.GetReason() != EResourceReason.SUFFICIENT || !m_bIsConsuming)
876  return response;
877 
878  float resourceUsed;
879  SCR_ResourceContainer container;
880  SCR_ResourceEncapsulator encapsulator;
881 
882  m_ContainerQueue.PerformSorting();
883 
884  int containerCount = m_ContainerQueue.GetContainerCount();
885 
886  for (int i = 0; i < containerCount && resourceCost > 0.0; i++)
887  {
888  container = m_ContainerQueue.GetContainerAt(i);
889  resourceUsed = Math.Min(resourceCost, container.GetResourceValue());
890  resourceCost -= resourceUsed;
891  encapsulator = container.GetResourceEncapsulator();
892 
893  if (encapsulator)
894  encapsulator.RequestConsumtion(resourceUsed);
895  else
896  container.DecreaseResourceValue(resourceUsed);
897  }
898 
899  m_ResourceComponent.Replicate();
900 
901  return response;
902  }
903 
904  //------------------------------------------------------------------------------------------------
906  void DebugDraw()
907  {
908  // TODO: Make it so that the nullity of these never happen in the first place.
909  if (!m_Owner || !m_ResourceComponent)
910  return;
911 
912  vector origin = GetOwnerOrigin();
913  Color color = Color.FromInt(m_ResourceComponent.GetDebugColor().PackToInt());
914 
915  color.Scale(0.2);
916  color.SetA(1.0);
917  Shape.CreateSphere(m_ResourceComponent.GetDebugColor().PackToInt(), ShapeFlags.TRANSP | ShapeFlags.ONCE | ShapeFlags.DOUBLESIDE | ShapeFlags.NOZWRITE, origin, m_fResourceRange);
918  DebugTextWorldSpace.Create(GetGame().GetWorld(), string.Format(" %1 \n %2 containers \n %3 / %4 resources \n %5 m ", m_sDebugName, GetContainerCount(), GetAggregatedResourceValue(), GetAggregatedMaxResourceValue(), m_fResourceRange), DebugTextFlags.ONCE | DebugTextFlags.CENTER | DebugTextFlags.FACE_CAMERA, origin[0], origin[1] + m_fResourceRange, origin[2], 10.0, 0xFFFFFFFF, color.PackToInt());
919 
920  if (m_ContainerQueue)
921  m_ContainerQueue.DebugDraw();
922  }
923 
924  //------------------------------------------------------------------------------------------------
925  override void UpdateContainerResourceValue(SCR_ResourceContainer container, float previousValue)
926  {
927  OnResourcesChanged(
928  m_ContainerQueue.UpdateContainerResourceValue(container.GetResourceValue(), previousValue)
929  );
930  }
931 
932  //------------------------------------------------------------------------------------------------
933  override void UpdateContainerMaxResourceValue(SCR_ResourceContainer container, float previousValue)
934  {
935  OnMaxResourcesChanged(
936  m_ContainerQueue.UpdateContainerMaxResourceValue(container.GetMaxResourceValue(), previousValue)
937  );
938  }
939 
940  //------------------------------------------------------------------------------------------------
941  protected void OnResourceRangeChanged(float previousValue)
942  {
943  if (m_OnResourceRangeChangedInvoker)
944  m_OnResourceRangeChangedInvoker.Invoke(this, previousValue);
945  }
946 
947  //------------------------------------------------------------------------------------------------
948  protected void OnBuyMultiplierChanged(float previousValue)
949  {
950  if (m_OnBuyMultiplierChangedInvoker)
951  m_OnBuyMultiplierChangedInvoker.Invoke(this, previousValue);
952  }
953 
954  //------------------------------------------------------------------------------------------------
955  protected void OnSellMultiplierChanged(float previousValue)
956  {
957  if (m_OnSellMultiplierChangedInvoker)
958  m_OnSellMultiplierChangedInvoker.Invoke(this, previousValue);
959  }
960 
961  //------------------------------------------------------------------------------------------------
962  // TODO: Consumtion -> Consumption
963  protected void OnConsumtionStateChanged(float previousValue)
964  {
965  if (m_OnConsumtionStateChangedInvoker)
966  m_OnConsumtionStateChangedInvoker.Invoke(this, previousValue);
967  }
968 
969  //------------------------------------------------------------------------------------------------
970  protected void OnExchangeStateChanged(float previousValue)
971  {
972  if (m_OnExchangeStateChangedInvoker)
973  m_OnExchangeStateChangedInvoker.Invoke(this, previousValue);
974  }
975 
976  //------------------------------------------------------------------------------------------------
977  override void Initialize(notnull IEntity owner)
978  {
979  super.Initialize(owner);
980 
981  SCR_ResourceContainer container = m_ResourceComponent.GetContainer(m_eResourceType);
982 
983  if (container && container.IsEncapsulated())
984  return;
985 
986  m_bIsConsuming = true;
987 
988  if (m_ContainerQueue)
989  m_ContainerQueue.Initialize(this);
990 
991  if (container)
992  RegisterContainer(container);
993  }
994 
995  //------------------------------------------------------------------------------------------------
996  override void Clear()
997  {
998  super.Clear();
999 
1000  if (m_ContainerQueue)
1001  m_ContainerQueue.Clear();
1002 
1003  GetGame().GetResourceSystemSubscriptionManager().OnResourceInteractorDeleted(this);
1004  }
1005 
1006  //------------------------------------------------------------------------------------------------
1007  protected bool PropCompareNetworkedVariables(SSnapSerializerBase snapshot, ScriptCtx hint)
1008  {
1009  RplId componentRplId = Replication.FindId(m_ResourceComponent);
1010 
1011  return snapshot.Compare(componentRplId, 4)
1012  && snapshot.Compare(m_fResourceRange, 4)
1013  && snapshot.Compare(m_fAggregatedResourceValue, 4)
1014  && snapshot.Compare(m_fAggregatedMaxResourceValue, 4)
1015  && snapshot.Compare(m_fBuyMultiplier, 4)
1016  && snapshot.Compare(m_fSellMultiplier, 4)
1017  && snapshot.Compare(m_bIsConsuming, 1)
1018  && snapshot.Compare(m_bIsExchanging, 1)
1019  && snapshot.Compare(m_eResourceRights, 4)
1020  && snapshot.Compare(m_eResourceType, 4)
1021  && snapshot.Compare(m_eGeneratorIdentifier, 4);
1022  }
1023 
1024  //------------------------------------------------------------------------------------------------
1025  protected bool ExtractNetworkedVariables(SSnapSerializerBase snapshot, ScriptCtx hint)
1026  {
1027  float aggregatedResourceValue = GetAggregatedResourceValue();
1028  float aggregatedMaxResourceValue = GetAggregatedMaxResourceValue();
1029  RplId componentRplId = Replication.FindId(m_ResourceComponent);
1030 
1031  snapshot.SerializeBytes(componentRplId, 4);
1032  snapshot.SerializeBytes(m_fResourceRange, 4);
1033  snapshot.SerializeBytes(aggregatedResourceValue, 4);
1034  snapshot.SerializeBytes(aggregatedMaxResourceValue, 4);
1035  snapshot.SerializeBytes(m_fBuyMultiplier, 4);
1036  snapshot.SerializeBytes(m_fSellMultiplier, 4);
1037  snapshot.SerializeBytes(m_bIsConsuming, 1);
1038  snapshot.SerializeBytes(m_bIsExchanging, 1);
1039  snapshot.SerializeBytes(m_eResourceRights, 4);
1040  snapshot.SerializeBytes(m_eResourceType, 4);
1041  snapshot.SerializeBytes(m_eGeneratorIdentifier, 4);
1042 
1043  return true;
1044  }
1045 
1046  //------------------------------------------------------------------------------------------------
1047  protected bool InjectNetworkedVariables(SSnapSerializerBase snapshot, ScriptCtx hint)
1048  {
1049  RplId componentRplId;
1050 
1051  snapshot.SerializeBytes(componentRplId, 4);
1052  snapshot.SerializeBytes(m_fResourceRange, 4);
1053  snapshot.SerializeBytes(m_fAggregatedResourceValue, 4);
1054  snapshot.SerializeBytes(m_fAggregatedMaxResourceValue, 4);
1055  snapshot.SerializeBytes(m_fBuyMultiplier, 4);
1056  snapshot.SerializeBytes(m_fSellMultiplier, 4);
1057  snapshot.SerializeBytes(m_bIsConsuming, 1);
1058  snapshot.SerializeBytes(m_bIsExchanging, 1);
1059  snapshot.SerializeBytes(m_eResourceRights, 4);
1060  snapshot.SerializeBytes(m_eResourceType, 4);
1061  snapshot.SerializeBytes(m_eGeneratorIdentifier, 4);
1062 
1063  m_ResourceComponent = SCR_ResourceComponent.Cast(Replication.FindItem(componentRplId));
1064 
1065  if (!m_ResourceComponent)
1066  return false;
1067 
1068  m_Owner = m_ResourceComponent.GetOwner();
1069 
1070  return true;
1071  }
1072 
1073  //------------------------------------------------------------------------------------------------
1078  static void Encode(SSnapSerializerBase snapshot, ScriptCtx ctx, ScriptBitSerializer packet)
1079  {
1080  snapshot.Serialize(packet, SCR_ResourceConsumer.CODEC_CONSUMER_PACKET_BYTESIZE);
1081  }
1082 
1083  //------------------------------------------------------------------------------------------------
1089  static bool Decode(ScriptBitSerializer packet, ScriptCtx ctx, SSnapSerializerBase snapshot)
1090  {
1091  return snapshot.Serialize(packet, SCR_ResourceConsumer.CODEC_CONSUMER_PACKET_BYTESIZE);
1092  }
1093 
1094  //------------------------------------------------------------------------------------------------
1100  static bool SnapCompare(SSnapSerializerBase lhs, SSnapSerializerBase rhs , ScriptCtx ctx)
1101  {
1102  return lhs.CompareSnapshots(rhs, SCR_ResourceConsumer.CODEC_CONSUMER_PACKET_BYTESIZE);
1103  }
1104 
1105  //------------------------------------------------------------------------------------------------
1111  static bool PropCompare(SCR_ResourceConsumer instance, SSnapSerializerBase snapshot, ScriptCtx ctx)
1112  {
1113  return instance.PropCompareNetworkedVariables(snapshot, ctx);
1114  }
1115 
1116  //------------------------------------------------------------------------------------------------
1122  static bool Extract(SCR_ResourceConsumer instance, ScriptCtx ctx, SSnapSerializerBase snapshot)
1123  {
1124  return instance.ExtractNetworkedVariables(snapshot, ctx);
1125  }
1126 
1127  //------------------------------------------------------------------------------------------------
1133  static bool Inject(SSnapSerializerBase snapshot, ScriptCtx ctx, SCR_ResourceConsumer instance)
1134  {
1135  return instance.InjectNetworkedVariables(snapshot, ctx);
1136  }
1137 }
Replicate
void Replicate()
Definition: SCR_ResourceComponent.c:1400
Clear
void Clear(GenericEntity entity)
Definition: SCR_GadgetManagerComponent.c:105
GetGridUpdateId
int GetGridUpdateId()
Definition: SCR_ResourceComponent.c:827
DebugDraw
protected void DebugDraw()
Definition: SCR_ResourceComponent.c:1335
SCR_ResourceConsumerContainerQueue
Definition: SCR_ResourceConsumerContainerQueue.c:2
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
EResourceReason
EResourceReason
Definition: SCR_ResourceConsumtionResponse.c:1
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
SetGridUpdateId
void SetGridUpdateId(int gridUpdateId)
Definition: SCR_ResourceComponent.c:896
SCR_ResourceContainerQueueBase
Definition: SCR_ResourceContainerQueue.c:2
IsGridUpdateIdGreaterThan
bool IsGridUpdateIdGreaterThan(int gridUpdateId)
Definition: SCR_ResourceComponent.c:856
SCR_ResourceActor
Definition: SCR_ResourceConsumer.c:2
Attribute
typedef Attribute
Post-process effect of scripted camera.
ReplicateEx
void ReplicateEx()
Definition: SCR_ResourceComponent.c:1413
Initialize
void Initialize()
Definition: SCR_CampaignMilitaryBaseComponent.c:181
EResourceType
EResourceType
Definition: SCR_ResourceContainer.c:1
SCR_ResourceConsumtionResponse
void SCR_ResourceConsumtionResponse(float availableResource=0, float resourceMultiplier=0, float range=0, EResourceReason reasonCode=EResourceReason.UNAVAILABLE)
Definition: SCR_ResourceConsumtionResponse.c:17
GetLastPosition
vector GetLastPosition()
Definition: SCR_ResourceComponent.c:834
m_ResourceComponent
protected SCR_ResourceComponent m_ResourceComponent
Definition: SCR_CampaignBuildingProviderComponent.c:51
EResourceRights
EResourceRights
Definition: SCR_ResourceContainer.c:8
m_LastPosition
protected vector m_LastPosition
Definition: SCR_ResourceComponent.c:125
UpdateLastPosition
void UpdateLastPosition()
Updates the serial number for the current processing call of the resource grid onto this component.
Definition: SCR_ResourceComponent.c:940
SCR_ResourceContainer
Definition: SCR_ResourceContainer.c:34
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
m_iGridUpdateId
protected int m_iGridUpdateId
Definition: SCR_ResourceComponent.c:50
SCR_ResourceEncapsulator
Definition: SCR_ResourceEncapsulator.c:1
m_Owner
SCR_AIGroupUtilityComponentClass m_Owner
BaseContainerProps
SCR_AIGoalReaction_Follow BaseContainerProps
Handles insects that are supposed to be spawned around selected prefabs defined in prefab names array...
Definition: SCR_AIGoalReaction.c:468
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180
EResourceGeneratorID
EResourceGeneratorID
Definition: SCR_ResourceGenerator.c:1