Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_ResourceConsumer.c
Go to the documentation of this file.
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 = 32;
7
8 [Attribute(defvalue: string.Empty, uiwidget: UIWidgets.EditBox, desc: "Identifier for debug prints", category: "Debugging")]
10
11 [Attribute(defvalue: EResourceRights.NONE.ToString(), uiwidget: UIWidgets.ComboBox, desc: "Limits the taking of resources to a specific group", enums: ParamEnumArray.FromEnum(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))]
16
17 protected SCR_ResourceComponent m_ResourceComponent;
18 protected IEntity m_Owner;
19
20 //------------------------------------------------------------------------------------------------
23 {
24 return Color.FromInt(m_ResourceComponent.GetDebugColor().PackToInt());
25 }
26
27 //------------------------------------------------------------------------------------------------
30 {
31 return m_ResourceComponent.GetGridUpdateId();
32 }
33
34 //------------------------------------------------------------------------------------------------
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 //------------------------------------------------------------------------------------------------
55 {
56 return m_sDebugName.Hash();
57 }
58
59 //------------------------------------------------------------------------------------------------
61 SCR_ResourceComponent GetComponent()
62 {
64 }
65
66 //------------------------------------------------------------------------------------------------
69 {
70 return m_Owner;
71 }
72
73 //------------------------------------------------------------------------------------------------
79
80 //------------------------------------------------------------------------------------------------
84 {
85 return false;
86 }
87
88 //------------------------------------------------------------------------------------------------
91 {
93 }
94
95 //------------------------------------------------------------------------------------------------
98 bool IsGridUpdateIdGreaterThan(int gridUpdateId)
99 {
100 return m_ResourceComponent.IsGridUpdateIdGreaterThan(gridUpdateId);
101 }
102
103 //------------------------------------------------------------------------------------------------
106 {
107 m_eResourceRights = rights;
108 }
109
110 //------------------------------------------------------------------------------------------------
112 void SetGridUpdateId(int gridUpdateId)
113 {
114 m_ResourceComponent.SetGridUpdateId(gridUpdateId);
115 }
116
117 //------------------------------------------------------------------------------------------------
120 [Obsolete("Use SCR_ResourceActor::Update(WorldTimestamp timestamp) instead.")]
121 void Update(float timeslice);
122
123 //------------------------------------------------------------------------------------------------
126 void Update(WorldTimestamp timestamp);
127
128 //------------------------------------------------------------------------------------------------
131 void UpdateInner(float timeslice)
132 {
133
134 }
135
136 //------------------------------------------------------------------------------------------------
138 void Clear()
139 {
140
141 }
142
143 //------------------------------------------------------------------------------------------------
144 // destructor
146 {
147 Clear();
148 }
149}
150
152class SCR_ResourceInteractor : SCR_ResourceActor
153{
154 protected ref ScriptInvoker<SCR_ResourceInteractor, float> m_OnResourcesChangedInvoker;
155 protected ref ScriptInvoker<SCR_ResourceInteractor, float> m_OnMaxResourcesChangedInvoker;
156 protected int m_iGridUpdateId = int.MIN;
158 protected float m_fAggregatedResourceValue = -1.0;
159 protected float m_fAggregatedMaxResourceValue = -1.0;
160 protected vector m_LastPosition = vector.Zero;
161
162 //------------------------------------------------------------------------------------------------
163 override int GetGridUpdateId()
164 {
165 return m_iGridUpdateId;
166 }
167
168 //------------------------------------------------------------------------------------------------
170 float GetResourceGridRange()
171 {
172 return 0.0;
173 }
174
175 //------------------------------------------------------------------------------------------------
177 float GetAggregatedResourceValue()
178 {
179 return 0.0;
180 }
181
182 //------------------------------------------------------------------------------------------------
184 float GetAggregatedMaxResourceValue()
185 {
186 return 0.0;
187 }
188
189 //------------------------------------------------------------------------------------------------
191 int GetContainerCount()
192 {
193 return 0;
194 }
195
196 //------------------------------------------------------------------------------------------------
198 vector GetLastPosition()
199 {
200 return m_LastPosition;
201 }
202
203 //------------------------------------------------------------------------------------------------
206 {
207 return m_eResourceType;
208 }
209
210 //------------------------------------------------------------------------------------------------
212 SCR_ResourceContainerQueueBase GetContainerQueue()
213 {
214 return null;
215 }
216
217 //------------------------------------------------------------------------------------------------
220 {
221 if (!m_OnResourcesChangedInvoker)
222 m_OnResourcesChangedInvoker = new ScriptInvoker<SCR_ResourceInteractor, float>();
223
224 return m_OnResourcesChangedInvoker;
225
226 }
227
228 //------------------------------------------------------------------------------------------------
230 ScriptInvoker<SCR_ResourceInteractor, float> GetOnMaxResourcesChanged()
231 {
232 if (!m_OnMaxResourcesChangedInvoker)
233 m_OnMaxResourcesChangedInvoker = new ScriptInvoker<SCR_ResourceInteractor, float>();
234
235 return m_OnMaxResourcesChangedInvoker;
236
237 }
238
239 //------------------------------------------------------------------------------------------------
241 EResourceGeneratorID GetIdentifier()
242 {
243 return EResourceGeneratorID.INVALID;
244 }
245
246 //------------------------------------------------------------------------------------------------
247 override bool IsGridUpdateIdGreaterThan(int gridUpdateId)
248 {
249 return m_iGridUpdateId > gridUpdateId;
250 }
251
252 //------------------------------------------------------------------------------------------------
255 bool IsAllowed(notnull SCR_ResourceContainer container)
256 {
257 if (container.GetResourceType() != m_eResourceType)
258 return false;
259
260 switch (m_eResourceRights)
261 {
262 case EResourceRights.NONE:
263 return false;
264
265 case EResourceRights.SELF:
266 return m_Owner == container.GetOwner();
267
268 case EResourceRights.SQUAD:
269 if (container.GetResourceRight() == EResourceRights.ALL)
270 return true;
271 // TODO: Logic for detecting the squad.
272 return false;
273
274 case EResourceRights.FACTION:
275 if (container.GetResourceRight() == EResourceRights.ALL)
276 return true;
277
278 FactionAffiliationComponent interactorFactionComponent = m_ResourceComponent.GetFactionAffiliationComponent();
279
280 if (!interactorFactionComponent)
281 return true;
282
283 FactionAffiliationComponent containerFactionComponent = container.GetComponent().GetFactionAffiliationComponent();
284
285 if (!containerFactionComponent)
286 return true;
287
288 Faction interactorFaction = interactorFactionComponent.GetAffiliatedFaction();
289 Faction containerFaction = containerFactionComponent.GetAffiliatedFaction();
290
291 if (!interactorFaction || !containerFaction)
292 return true;
293
294 return interactorFaction == containerFaction || interactorFaction.IsFactionFriendly(containerFaction);
295
296 case EResourceRights.ALL:
297 return true;
298 }
299
300 return true;
301 }
302
303 //------------------------------------------------------------------------------------------------
307 bool IsAllowed(notnull IEntity entity, EResourceType resourceType)
308 {
309 if (resourceType != m_eResourceType)
310 return false;
311
312 switch (m_eResourceRights)
313 {
314 case EResourceRights.NONE:
315 return false;
316
317 case EResourceRights.SELF:
318 return m_Owner == entity;
319
320 case EResourceRights.SQUAD:
321 // TODO: Logic for detecting the squad.
322 case EResourceRights.FACTION:
323 FactionAffiliationComponent interactorFactionComponent = m_ResourceComponent.GetFactionAffiliationComponent();
324
325 if (!interactorFactionComponent)
326 return false;
327
328 SCR_ResourceComponent containerResourceComponent = SCR_ResourceComponent.Cast(entity.FindComponent(SCR_ResourceComponent));
329
330 if (!containerResourceComponent)
331 return false;
332
333 SCR_ResourceContainer container;
334
335 if (!containerResourceComponent.GetContainer(resourceType, container))
336 return false;
337
338 if (container.GetResourceRight() == EResourceRights.ALL)
339 return true;
340
341 FactionAffiliationComponent containerrFactionComponent = container.GetComponent().GetFactionAffiliationComponent();
342
343 if (!containerrFactionComponent)
344 return false;
345
346 return interactorFactionComponent.GetAffiliatedFaction() == containerrFactionComponent.GetAffiliatedFaction();
347
348 case EResourceRights.ALL:
349 return true;
350 }
351
352 return true;
353 }
354
355 //------------------------------------------------------------------------------------------------
359 bool CanInteractWith(notnull SCR_ResourceContainer container)
360 {
361 return IsAllowed(container) && container.IsAllowed(this);
362 }
363
364 //------------------------------------------------------------------------------------------------
368 int FindContainer(notnull SCR_ResourceContainer container)
369 {
370 return SCR_ResourceContainerQueueBase.INVALID_CONTAINER_INDEX;
371 }
372
373 //------------------------------------------------------------------------------------------------
374 override void SetGridUpdateId(int gridUpdateId)
375 {
376 if (m_iGridUpdateId > gridUpdateId)
377 return;
378
379 m_iGridUpdateId = gridUpdateId;
380 }
381
382 //------------------------------------------------------------------------------------------------
384 void UpdateLastPosition()
385 {
386 m_LastPosition = GetOwnerOrigin();
387 }
388
389 //------------------------------------------------------------------------------------------------
393 bool RegisterContainer(notnull SCR_ResourceContainer container)
394 {
395 return false;
396 }
397
398 //------------------------------------------------------------------------------------------------
402 bool RegisterContainerForced(notnull SCR_ResourceContainer container)
403 {
404 return false;
405 }
406
407 //------------------------------------------------------------------------------------------------
411 bool UnregisterContainer(int containerIndex)
412 {
413 return false;
414 }
415
416 //------------------------------------------------------------------------------------------------
420 bool UnregisterContainer(notnull SCR_ResourceContainer container)
421 {
422 return false;
423 }
424
425 //------------------------------------------------------------------------------------------------
427 void Replicate()
428 {
429 m_ResourceComponent.Replicate();
430 }
431
432 //------------------------------------------------------------------------------------------------
434 void ReplicateEx()
435 {
436 m_ResourceComponent.ReplicateEx();
437 }
438
439 //------------------------------------------------------------------------------------------------
443 void UpdateContainerResourceValue(SCR_ResourceContainer container, float previousValue);
444
445 //------------------------------------------------------------------------------------------------
449 void UpdateContainerMaxResourceValue(SCR_ResourceContainer container, float previousValue);
450
451 //------------------------------------------------------------------------------------------------
454 void OnResourceGridUpdated(notnull SCR_ResourceGrid grid)
455 {
456 SetGridUpdateId(grid.GetGridUpdateId());
458 Replicate();
459 }
460
461 //------------------------------------------------------------------------------------------------
463 void OnResourcesChanged(float previousValue)
464 {
465 m_fAggregatedResourceValue = GetAggregatedResourceValue();
466
467 if (m_OnResourcesChangedInvoker)
468 m_OnResourcesChangedInvoker.Invoke(this, previousValue);
469
471 return;
472
473 m_ResourceComponent.Replicate();
474 }
475
476 //------------------------------------------------------------------------------------------------
478 void OnMaxResourcesChanged(float previousValue)
479 {
480 m_fAggregatedMaxResourceValue = GetAggregatedMaxResourceValue();
481
482 if (m_OnMaxResourcesChangedInvoker)
483 m_OnMaxResourcesChangedInvoker.Invoke(this, previousValue);
484
486 return;
487
488 m_ResourceComponent.Replicate();
489 }
490
491 //------------------------------------------------------------------------------------------------
493 void OnContainerRegistered(notnull SCR_ResourceContainer container)
494 {
495 container.LinkInteractor(this);
496 OnResourcesChanged(GetAggregatedResourceValue() - container.GetResourceValue());
497 OnMaxResourcesChanged(GetAggregatedMaxResourceValue() - container.GetMaxResourceValue());
498 }
499
500 //------------------------------------------------------------------------------------------------
502 void OnContainerUnregistered(notnull SCR_ResourceContainer container)
503 {
504 container.UnlinkInteractor(this);
505 OnResourcesChanged(GetAggregatedResourceValue() + container.GetResourceValue());
506 OnMaxResourcesChanged(GetAggregatedMaxResourceValue() + container.GetMaxResourceValue());
507 }
508
509 //------------------------------------------------------------------------------------------------
512 void Initialize(notnull IEntity owner)
513 {
514 m_Owner = owner;
515 m_ResourceComponent = SCR_ResourceComponent.Cast(owner.FindComponent(SCR_ResourceComponent));
516 }
517
518 //------------------------------------------------------------------------------------------------
519 override void Clear()
520 {
521 super.Clear();
522
523 m_fAggregatedResourceValue = -1.0;
524 m_fAggregatedMaxResourceValue = -1.0;
525 }
526}
527
528[BaseContainerProps(configRoot: true)]
529class SCR_ResourceConsumer : SCR_ResourceInteractor
530{
531 protected static const int CODEC_CONSUMER_PACKET_BYTESIZE = 38;
532
533 [Attribute(defvalue: EResourceGeneratorID.DEFAULT_STORAGE.ToString(), uiwidget: UIWidgets.ComboBox, desc: "Identifier for the generator used for storage", enums: ParamEnumArray.FromEnum(EResourceGeneratorID))]
534 protected EResourceGeneratorID m_eGeneratorIdentifier;
535
536 [Attribute(defvalue: "0.0", uiwidget: UIWidgets.SpinBox, desc: "Sets the range in which Resource is sought.",params: "0.0 10000.0 1.0")]
537 protected float m_fResourceRange;
538
539 [Attribute("1", uiwidget: UIWidgets.SpinBox, params: string.Format("0.0 %1 1.0", float.MAX))]
540 protected float m_fBuyMultiplier;
541
542 [Attribute(uiwidget: UIWidgets.SpinBox, params: string.Format("0.0 %1 1.0", float.MAX))]
543 protected float m_fSellMultiplier;
544
545 [Attribute(uiwidget: UIWidgets.CheckBox)]
546 protected bool m_bIsIgnoringItself;
547
548 [Attribute(uiwidget: UIWidgets.Object)]
549 protected ref SCR_ResourceConsumerContainerQueue m_ContainerQueue;
550
551 protected bool m_bIsConsuming;
552 protected bool m_bIsExchanging;
553 protected ref ScriptInvoker m_OnResourceRangeChangedInvoker;
554 protected ref ScriptInvoker m_OnBuyMultiplierChangedInvoker;
555 protected ref ScriptInvoker m_OnSellMultiplierChangedInvoker;
556 protected ref ScriptInvoker m_OnConsumtionStateChangedInvoker; // TODO: Consumtion -> Consumption
557 protected ref ScriptInvoker m_OnExchangeStateChangedInvoker;
558
559 //------------------------------------------------------------------------------------------------
560 override float GetResourceGridRange()
561 {
562 return m_fResourceRange;
563 }
564
565 //------------------------------------------------------------------------------------------------
567 float GetResourceRange()
568 {
569 return m_fResourceRange;
570 }
571
572 //------------------------------------------------------------------------------------------------
574 float GetBuyMultiplier()
575 {
576 return m_fBuyMultiplier;
577 }
578
579 //------------------------------------------------------------------------------------------------
581 float GetSellMultiplier()
582 {
583 return m_fSellMultiplier;
584 }
585
586 //------------------------------------------------------------------------------------------------
587 override float GetAggregatedResourceValue()
588 {
589 if (!m_ContainerQueue)
590 return m_fAggregatedResourceValue;
591
592 return m_ContainerQueue.GetAggregatedResourceValue();
593 }
594
595 //------------------------------------------------------------------------------------------------
596 override float GetAggregatedMaxResourceValue()
597 {
598 if (!m_ContainerQueue)
599 return m_fAggregatedMaxResourceValue;
600
601 return m_ContainerQueue.GetAggregatedMaxResourceValue();
602 }
603
604 //------------------------------------------------------------------------------------------------
605 override int GetContainerCount()
606 {
607 if (!m_ContainerQueue)
608 return 0.0;
609
610 return m_ContainerQueue.GetContainerCount();
611 }
612
613 //------------------------------------------------------------------------------------------------
614 override SCR_ResourceContainerQueueBase GetContainerQueue()
615 {
616 return m_ContainerQueue;
617 }
618
619 //------------------------------------------------------------------------------------------------
621 ScriptInvoker GetOnResourceRangeChanged()
622 {
623 if (!m_OnResourceRangeChangedInvoker)
624 m_OnResourceRangeChangedInvoker = new ScriptInvoker();
625
626 return m_OnResourceRangeChangedInvoker;
627 }
628
629 //------------------------------------------------------------------------------------------------
631 ScriptInvoker GetOnBuyMultiplierChanged()
632 {
633 if (!m_OnBuyMultiplierChangedInvoker)
634 m_OnBuyMultiplierChangedInvoker = new ScriptInvoker();
635
636 return m_OnBuyMultiplierChangedInvoker;
637 }
638
639 //------------------------------------------------------------------------------------------------
641 ScriptInvoker GetOnSellMultiplierChanged()
642 {
643 if (!m_OnSellMultiplierChangedInvoker)
644 m_OnSellMultiplierChangedInvoker = new ScriptInvoker();
645
646 return m_OnSellMultiplierChangedInvoker;
647 }
648
649 //------------------------------------------------------------------------------------------------
651 // TODO: Consumtion -> Consumption
652 ScriptInvoker GetOnConsumtionStateChanged()
653 {
654 if (!m_OnConsumtionStateChangedInvoker)
655 m_OnConsumtionStateChangedInvoker = new ScriptInvoker();
656
657 return m_OnConsumtionStateChangedInvoker;
658 }
659
660 //------------------------------------------------------------------------------------------------
662 ScriptInvoker GetOnExchangeStateChanged()
663 {
664 if (!m_OnExchangeStateChangedInvoker)
665 m_OnExchangeStateChangedInvoker = new ScriptInvoker();
666
667 return m_OnExchangeStateChangedInvoker;
668 }
669
670 //------------------------------------------------------------------------------------------------
672 EResourceGeneratorID GetGeneratorIdentifier()
673 {
674 return m_eGeneratorIdentifier;
675 }
676
677 //------------------------------------------------------------------------------------------------
678 override EResourceGeneratorID GetIdentifier()
679 {
680 return m_eGeneratorIdentifier;
681 }
682
683 //------------------------------------------------------------------------------------------------
685 bool IsIgnoringItself()
686 {
687 return m_bIsIgnoringItself;
688 }
689
690 //------------------------------------------------------------------------------------------------
692 bool IsConsuming()
693 {
694 return m_bIsConsuming;
695 }
696
697 //------------------------------------------------------------------------------------------------
699 bool IsExchanging()
700 {
701 return m_bIsExchanging;
702 }
703
704 //------------------------------------------------------------------------------------------------
705 override bool ShouldUpdate()
706 {
707 return false;
708 }
709
710 //------------------------------------------------------------------------------------------------
711 override bool CanInteractWith(notnull SCR_ResourceContainer container)
712 {
713 return (!m_bIsIgnoringItself || container.GetOwner() != m_Owner) && super.CanInteractWith(container);
714 }
715
716 //------------------------------------------------------------------------------------------------
717 override int FindContainer(notnull SCR_ResourceContainer container)
718 {
719 if (!m_ContainerQueue)
720 return super.FindContainer(container);
721
722 return m_ContainerQueue.FindContainer(container);
723 }
724
725 //------------------------------------------------------------------------------------------------
729 bool SetResourceRange(float value, bool notifyChange = true)
730 {
731 float previousValue = m_fResourceRange;
732 m_fResourceRange = Math.Max(value, 0.0);
733
734 if (previousValue == m_fResourceRange)
735 return false;
736
737 if (notifyChange)
738 OnResourceRangeChanged(previousValue);
739
740 return true;
741 }
742
743 //------------------------------------------------------------------------------------------------
747 bool SetBuyMultiplier(float value, bool notifyChange = true)
748 {
749 float previousValue = m_fBuyMultiplier;
750 m_fBuyMultiplier = Math.Max(value, 0.0);
751
752 if (previousValue == m_fBuyMultiplier)
753 return false;
754
755 if (notifyChange)
756 OnBuyMultiplierChanged(previousValue);
757
758 return true;
759 }
760
761 //------------------------------------------------------------------------------------------------
765 bool SetSellMultiplier(float value, bool notifyChange = true)
766 {
767 float previousValue = m_fSellMultiplier;
768 m_fSellMultiplier = Math.Max(value, 0.0);
769
770 if (previousValue == m_fSellMultiplier)
771 return false;
772
773 if (notifyChange)
774 OnSellMultiplierChanged(previousValue);
775
776 return true;
777 }
778
779 //------------------------------------------------------------------------------------------------
784 // TODO: Consumtion -> Consumption
785 bool EnableConsumtion(bool shouldEnable, bool notifyChange = true)
786 {
787 bool previousValue = m_bIsConsuming;
788 m_bIsConsuming = shouldEnable;
789
790 if (shouldEnable == m_bIsConsuming)
791 return false;
792
793 if (notifyChange)
794 OnConsumtionStateChanged(previousValue);
795
796 return true;
797 }
798
799 //------------------------------------------------------------------------------------------------
804 bool EnableExchange(bool shouldEnable, bool notifyChange = true)
805 {
806 bool previousValue = m_bIsExchanging;
807 m_bIsExchanging = shouldEnable;
808
809 if (previousValue == m_bIsExchanging)
810 return false;
811
812 if (notifyChange)
813 OnExchangeStateChanged(previousValue);
814
815 return true;
816 }
817
818 //------------------------------------------------------------------------------------------------
819 override bool RegisterContainer(notnull SCR_ResourceContainer container)
820 {
821 if (CanInteractWith(container)
822 && m_ContainerQueue
823 && m_ContainerQueue.RegisterContainer(container) != SCR_ResourceContainerQueueBase.INVALID_CONTAINER_INDEX)
824 {
825 OnContainerRegistered(container);
826
827 return true;
828 }
829
830 return false;
831 }
832
833 //------------------------------------------------------------------------------------------------
834 override bool RegisterContainerForced(notnull SCR_ResourceContainer container)
835 {
836 if (m_ContainerQueue && m_ContainerQueue.RegisterContainer(container) != SCR_ResourceContainerQueueBase.INVALID_CONTAINER_INDEX)
837 {
838 OnContainerRegistered(container);
839
840 return true;
841 }
842
843 return false;
844 }
845
846 //------------------------------------------------------------------------------------------------
847 override bool UnregisterContainer(int containerIndex)
848 {
849 return m_ContainerQueue && m_ContainerQueue.PopContainerAt(containerIndex);
850 }
851
852 //------------------------------------------------------------------------------------------------
853 override bool UnregisterContainer(notnull SCR_ResourceContainer container)
854 {
855 return m_ContainerQueue && m_ContainerQueue.PopContainerAt(m_ContainerQueue.FindContainer(container));
856 }
857
858 //------------------------------------------------------------------------------------------------
863 SCR_ResourceConsumtionResponse RequestAvailability(float resourceCost, bool performFullQuery = false)
864 {
865 GetGame().GetResourceGrid().UpdateInteractor(this);
866
867 SCR_ResourceConsumtionResponse response = new SCR_ResourceConsumtionResponse(GetAggregatedResourceValue(), m_fBuyMultiplier, m_fResourceRange, EResourceReason.SUFFICIENT);
868
869 if (!m_bIsConsuming)
870 return response;
871
872 if (resourceCost > response.GetAvailableSupply())
873 {
874 response.SetReason(EResourceReason.INSUFICIENT);
875
876 return response;
877 }
878
879 if (resourceCost != 0.0 && response.GetAvailableSupply() == 0)
880 {
881 response.SetReason(EResourceReason.UNAVAILABLE);
882
883 return response;
884 }
885
886 return response;
887 }
888
889 //------------------------------------------------------------------------------------------------
893 // TODO: Consumtion -> Consumption
894 SCR_ResourceConsumtionResponse RequestConsumtion(float resourceCost)
895 {
896 SCR_ResourceConsumtionResponse response = RequestAvailability(resourceCost, true);
897
898 if (!m_bIsConsuming || response.GetReason() != EResourceReason.SUFFICIENT)
899 return response;
900
901 float resourceUsed;
902 SCR_ResourceContainer container;
903 SCR_ResourceEncapsulator encapsulator;
904
905 m_ContainerQueue.PerformSorting();
906
907 int containerCount = m_ContainerQueue.GetContainerCount();
908
909 for (int i = 0; i < containerCount && resourceCost > 0.0; i++)
910 {
911 container = m_ContainerQueue.GetContainerAt(i);
912 resourceUsed = Math.Min(resourceCost, container.GetResourceValue());
913 resourceCost -= resourceUsed;
914 encapsulator = container.GetResourceEncapsulator();
915
916 if (encapsulator)
917 encapsulator.RequestConsumtion(resourceUsed);
918 else
919 container.DecreaseResourceValue(resourceUsed);
920 }
921
922 m_ResourceComponent.Replicate();
923
924 return response;
925 }
926
927 //------------------------------------------------------------------------------------------------
929 void DebugDraw()
930 {
931 // TODO: Make it so that the nullity of these never happen in the first place.
933 return;
934
935 vector origin = GetOwnerOrigin();
936 Color color = Color.FromInt(m_ResourceComponent.GetDebugColor().PackToInt());
937
938 color.Scale(0.2);
939 color.SetA(1.0);
940 Shape.CreateSphere(m_ResourceComponent.GetDebugColor().PackToInt(), ShapeFlags.TRANSP | ShapeFlags.ONCE | ShapeFlags.DOUBLESIDE | ShapeFlags.NOZWRITE, origin, m_fResourceRange);
941 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());
942
943 if (m_ContainerQueue)
944 m_ContainerQueue.DebugDraw();
945 }
946
947 //------------------------------------------------------------------------------------------------
948 override void UpdateContainerResourceValue(SCR_ResourceContainer container, float previousValue)
949 {
951 m_ContainerQueue.UpdateContainerResourceValue(container.GetResourceValue(), previousValue)
952 );
953 }
954
955 //------------------------------------------------------------------------------------------------
956 override void UpdateContainerMaxResourceValue(SCR_ResourceContainer container, float previousValue)
957 {
958 OnMaxResourcesChanged(
959 m_ContainerQueue.UpdateContainerMaxResourceValue(container.GetMaxResourceValue(), previousValue)
960 );
961 }
962
963 //------------------------------------------------------------------------------------------------
964 protected void OnResourceRangeChanged(float previousValue)
965 {
966 if (m_OnResourceRangeChangedInvoker)
967 m_OnResourceRangeChangedInvoker.Invoke(this, previousValue);
968 }
969
970 //------------------------------------------------------------------------------------------------
971 protected void OnBuyMultiplierChanged(float previousValue)
972 {
973 if (m_OnBuyMultiplierChangedInvoker)
974 m_OnBuyMultiplierChangedInvoker.Invoke(this, previousValue);
975 }
976
977 //------------------------------------------------------------------------------------------------
978 protected void OnSellMultiplierChanged(float previousValue)
979 {
980 if (m_OnSellMultiplierChangedInvoker)
981 m_OnSellMultiplierChangedInvoker.Invoke(this, previousValue);
982 }
983
984 //------------------------------------------------------------------------------------------------
985 // TODO: Consumtion -> Consumption
986 protected void OnConsumtionStateChanged(float previousValue)
987 {
988 if (m_OnConsumtionStateChangedInvoker)
989 m_OnConsumtionStateChangedInvoker.Invoke(this, previousValue);
990 }
991
992 //------------------------------------------------------------------------------------------------
993 protected void OnExchangeStateChanged(float previousValue)
994 {
995 if (m_OnExchangeStateChangedInvoker)
996 m_OnExchangeStateChangedInvoker.Invoke(this, previousValue);
997 }
998
999 //------------------------------------------------------------------------------------------------
1000 override void Initialize(notnull IEntity owner)
1001 {
1002 super.Initialize(owner);
1003
1004 SCR_ResourceContainer container = m_ResourceComponent.GetContainer(m_eResourceType);
1005
1006 if (container && container.IsEncapsulated())
1007 return;
1008
1009 m_bIsConsuming = true;
1010
1011 if (m_ContainerQueue)
1012 m_ContainerQueue.Initialize(this);
1013
1014 if (container)
1015 RegisterContainer(container);
1016 }
1017
1018 //------------------------------------------------------------------------------------------------
1019 override void Clear()
1020 {
1021 super.Clear();
1022
1023 if (m_ContainerQueue)
1024 m_ContainerQueue.Clear();
1025
1026 GetGame().GetResourceSystemSubscriptionManager().OnResourceInteractorDeleted(this);
1027 }
1028
1029 //------------------------------------------------------------------------------------------------
1030 protected bool PropCompareNetworkedVariables(SSnapSerializerBase snapshot, ScriptCtx hint)
1031 {
1032 RplId componentRplId = Replication.FindItemId(m_ResourceComponent);
1033
1034 return snapshot.Compare(componentRplId, 4)
1035 && snapshot.Compare(m_fResourceRange, 4)
1036 && snapshot.Compare(m_fAggregatedResourceValue, 4)
1037 && snapshot.Compare(m_fAggregatedMaxResourceValue, 4)
1038 && snapshot.Compare(m_fBuyMultiplier, 4)
1039 && snapshot.Compare(m_fSellMultiplier, 4)
1040 && snapshot.Compare(m_bIsConsuming, 1)
1041 && snapshot.Compare(m_bIsExchanging, 1)
1042 && snapshot.Compare(m_eResourceRights, 4)
1043 && snapshot.Compare(m_eResourceType, 4)
1044 && snapshot.Compare(m_eGeneratorIdentifier, 4);
1045 }
1046
1047 //------------------------------------------------------------------------------------------------
1048 protected bool ExtractNetworkedVariables(SSnapSerializerBase snapshot, ScriptCtx hint)
1049 {
1050 float aggregatedResourceValue = GetAggregatedResourceValue();
1051 float aggregatedMaxResourceValue = GetAggregatedMaxResourceValue();
1052 RplId componentRplId = Replication.FindItemId(m_ResourceComponent);
1053
1054 snapshot.SerializeBytes(componentRplId, 4);
1055 snapshot.SerializeBytes(m_fResourceRange, 4);
1056 snapshot.SerializeBytes(aggregatedResourceValue, 4);
1057 snapshot.SerializeBytes(aggregatedMaxResourceValue, 4);
1058 snapshot.SerializeBytes(m_fBuyMultiplier, 4);
1059 snapshot.SerializeBytes(m_fSellMultiplier, 4);
1060 snapshot.SerializeBytes(m_bIsConsuming, 1);
1061 snapshot.SerializeBytes(m_bIsExchanging, 1);
1062 snapshot.SerializeBytes(m_eResourceRights, 4);
1063 snapshot.SerializeBytes(m_eResourceType, 4);
1064 snapshot.SerializeBytes(m_eGeneratorIdentifier, 4);
1065
1066 return true;
1067 }
1068
1069 //------------------------------------------------------------------------------------------------
1070 protected bool InjectNetworkedVariables(SSnapSerializerBase snapshot, ScriptCtx hint)
1071 {
1072 RplId componentRplId;
1073
1074 snapshot.SerializeBytes(componentRplId, 4);
1075 snapshot.SerializeBytes(m_fResourceRange, 4);
1076 snapshot.SerializeBytes(m_fAggregatedResourceValue, 4);
1077 snapshot.SerializeBytes(m_fAggregatedMaxResourceValue, 4);
1078 snapshot.SerializeBytes(m_fBuyMultiplier, 4);
1079 snapshot.SerializeBytes(m_fSellMultiplier, 4);
1080 snapshot.SerializeBytes(m_bIsConsuming, 1);
1081 snapshot.SerializeBytes(m_bIsExchanging, 1);
1082 snapshot.SerializeBytes(m_eResourceRights, 4);
1083 snapshot.SerializeBytes(m_eResourceType, 4);
1084 snapshot.SerializeBytes(m_eGeneratorIdentifier, 4);
1085
1086 m_ResourceComponent = SCR_ResourceComponent.Cast(Replication.FindItem(componentRplId));
1087
1089 return false;
1090
1091 m_Owner = m_ResourceComponent.GetOwner();
1092
1093 return true;
1094 }
1095
1096 //------------------------------------------------------------------------------------------------
1101 static void Encode(SSnapSerializerBase snapshot, ScriptCtx ctx, ScriptBitSerializer packet)
1102 {
1103 snapshot.Serialize(packet, SCR_ResourceConsumer.CODEC_CONSUMER_PACKET_BYTESIZE);
1104 }
1105
1106 //------------------------------------------------------------------------------------------------
1112 static bool Decode(ScriptBitSerializer packet, ScriptCtx ctx, SSnapSerializerBase snapshot)
1113 {
1114 return snapshot.Serialize(packet, SCR_ResourceConsumer.CODEC_CONSUMER_PACKET_BYTESIZE);
1115 }
1116
1117 //------------------------------------------------------------------------------------------------
1123 static bool SnapCompare(SSnapSerializerBase lhs, SSnapSerializerBase rhs , ScriptCtx ctx)
1124 {
1125 return lhs.CompareSnapshots(rhs, SCR_ResourceConsumer.CODEC_CONSUMER_PACKET_BYTESIZE);
1126 }
1127
1128 //------------------------------------------------------------------------------------------------
1134 static bool PropCompare(SCR_ResourceConsumer instance, SSnapSerializerBase snapshot, ScriptCtx ctx)
1135 {
1136 return instance.PropCompareNetworkedVariables(snapshot, ctx);
1137 }
1138
1139 //------------------------------------------------------------------------------------------------
1145 static bool Extract(SCR_ResourceConsumer instance, ScriptCtx ctx, SSnapSerializerBase snapshot)
1146 {
1147 return instance.ExtractNetworkedVariables(snapshot, ctx);
1148 }
1149
1150 //------------------------------------------------------------------------------------------------
1156 static bool Inject(SSnapSerializerBase snapshot, ScriptCtx ctx, SCR_ResourceConsumer instance)
1157 {
1158 return instance.InjectNetworkedVariables(snapshot, ctx);
1159 }
1160}
ArmaReforgerScripted GetGame()
Definition game.c:1398
SCR_AIAnimation_Loitering BaseContainerProps
Commanding menu commanding element class.
Get all prefabs that have the spawner the given labels and are valid in the editor mode param catalogType Type to catalog to get prefabs from param editorMode Editor mode to get valid entries from param faction Faction(Optional)
void SetGridUpdateId(int gridUpdateId)
void Replicate()
bool IsGridUpdateIdGreaterThan(int gridUpdateId)
int m_iGridUpdateId
int GetGridUpdateId()
void DebugDraw()
vector m_LastPosition
void ReplicateEx()
void UpdateLastPosition()
Updates the serial number for the current processing call of the resource grid onto this component.
vector GetLastPosition()
EResourceType m_eResourceType
void SCR_ResourceConsumtionResponse(float availableResource=0, float resourceMultiplier=0, float range=0, EResourceReason reasonCode=EResourceReason.UNAVAILABLE)
EResourceGeneratorID
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
void OnResourcesChanged(SCR_ResourceInteractor interactor, float previousSupplies)
EResourceType GetResourceType()
SCR_ResourceComponent m_ResourceComponent
enum EVehicleType IEntity
Definition Color.c:13
SCR_ResourceComponent m_ResourceComponent
EResourceType m_eResourceType
SCR_ResourceComponent GetComponent()
static const int CODEC_GENERATOR_PACKET_BYTESIZE
void UpdateInner(float timeslice)
void Update(float timeslice)
static const float UPDATE_PERIOD
void Update(WorldTimestamp timestamp)
EResourceRights GetResourceRight()
bool IsGridUpdateIdGreaterThan(int gridUpdateId)
void SetGridUpdateId(int gridUpdateId)
void SetResourceRights(EResourceRights rights)
EResourceRights m_eResourceRights
SCR_ResourceEncapsulator GetResourceEncapsulator()
bool DecreaseResourceValue(float value, bool notifyChange=true)
void RequestConsumtion(float resourceCost, bool notifyChange=true)
DebugTextFlags
ShapeFlags
Definition ShapeFlags.c:13
SCR_FieldOfViewSettings Attribute
int RplId
Definition EnNetwork.c:33
proto native void Clear()
Remove all calls from list.
ScriptInvokerBase< func > ScriptInvoker
Definition tools.c:134