37 protected ref set<SCR_ResourceInteractor> m_aInteractors =
new set<SCR_ResourceInteractor>();
38 float debugControlOffset = 0.25;
43 [
Attribute(uiwidget: UIWidgets.SpinBox,
params:
string.Format(
"0.0 %1 1.0",
float.MAX))]
44 protected float m_fResourceValueCurrent;
46 [
Attribute(uiwidget: UIWidgets.SpinBox,
params:
string.Format(
"0.0 %1 1.0",
float.MAX))]
47 protected float m_fResourceValueMax;
50 protected bool m_bEnableResourceGain;
52 [
Attribute(uiwidget: UIWidgets.SpinBox,
params:
string.Format(
"0.0 %1 1.0",
float.MAX))]
53 protected float m_fResourceGain;
55 [
Attribute(uiwidget: UIWidgets.SpinBox,
params:
string.Format(
"0.0 %1 1.0",
float.MAX))]
56 protected float m_fResourceGainTickrate;
58 [
Attribute(uiwidget: UIWidgets.SpinBox,
params:
string.Format(
"0.0 %1 1.0",
float.MAX))]
59 protected float m_fResourceGainTimeout;
62 protected bool m_bEnableResourceDecay;
65 protected float m_fResourceDecay;
67 [
Attribute(uiwidget: UIWidgets.SpinBox,
params:
string.Format(
"0.0 %1 1.0",
float.MAX))]
68 protected float m_fResourceDecayTickrate;
70 [
Attribute(uiwidget: UIWidgets.SpinBox,
params:
string.Format(
"0.0 %1 1.0",
float.MAX))]
71 protected float m_fResourceDecayTimeout;
76 protected bool m_bIsEncapsulated;
78 protected float m_fResourceGainElapsedTime;
79 protected float m_fResourceDecayElapsedTime;
80 protected float m_fWeightMultiplier;
82 protected ref ScriptInvoker m_OnResourcesChangedInvoker;
83 protected ref ScriptInvoker m_OnMaxResourcesChangedInvoker;
84 protected ref ScriptInvoker m_OnResourcesDepletedInvoker;
85 protected ref ScriptInvoker m_OnResourcesMaxedOutInvoker;
86 protected ref ScriptInvoker m_OnGainChangedInvoker;
87 protected ref ScriptInvoker m_OnDecayChangedInvoker;
88 protected ref ScriptInvoker m_OnGainEnabledChangedInvoker;
89 protected ref ScriptInvoker m_OnDecayEnabledChangedInvoker;
94 float GetResourceValue()
96 return m_fResourceValueCurrent;
100 float GetMaxResourceValue()
102 return m_fResourceValueMax;
106 float GetWeightMultiplier()
108 return m_fWeightMultiplier;
112 float GetResourceGain()
114 return m_fResourceGain;
118 float GetResourceDecay()
120 return m_fResourceDecay;
124 float GetResourceGainTickrate()
126 return m_fResourceGainTickrate;
130 float GetResourceDecayTickrate()
132 return m_fResourceDecayTickrate;
136 void GetBoundingVolume(inout vector mins, inout vector maxs)
143 void GetAxisAlignedBoundingVolume(inout vector mins, inout vector maxs)
148 m_Owner.GetWorldBounds(mins, maxs);
156 return m_eStorageType;
162 return m_eResourceType;
168 return m_eOnEmptyBehavior;
174 return m_ResourceEncapsulator;
178 set<SCR_ResourceInteractor> GetLinkedInteractors()
180 return m_aInteractors;
184 array<SCR_ResourceInteractor> GetLinkedInteractorsCopy()
186 array<SCR_ResourceInteractor> result =
new array<SCR_ResourceInteractor>();
188 foreach (SCR_ResourceInteractor interactor: m_aInteractors)
190 result.Insert(interactor);
197 int GetLinkedInteractorsCount()
199 return m_aInteractors.Count();
203 SCR_ResourceInteractor GetLinkedInteractorAt(
int index)
205 return m_aInteractors[
index];
209 int GetLinkedInteractorIndex(notnull SCR_ResourceInteractor interactor)
211 return m_aInteractors.Find(interactor);
215 bool IsInteractorLinked(notnull SCR_ResourceInteractor interactor)
217 return m_aInteractors.Contains(interactor);
221 bool IsAllowed(notnull SCR_ResourceInteractor interactor)
223 if (interactor.GetResourceType() != m_eResourceType)
226 switch (m_eResourceRights)
231 return m_Owner == interactor.GetOwner();
236 FactionAffiliationComponent interactorFactionComponent = interactor.GetComponent().GetFactionAffiliationComponent();
238 if (!interactorFactionComponent)
241 FactionAffiliationComponent containerrFactionComponent =
m_ResourceComponent.GetFactionAffiliationComponent();
243 if (!containerrFactionComponent)
246 return interactorFactionComponent.GetAffiliatedFaction() == containerrFactionComponent.GetAffiliatedFaction();
255 override bool IsIsolated()
257 return m_bIsEncapsulated || super.IsIsolated();
261 bool IsInRange(vector origin,
float range)
267 return Math3D.IntersectionSphereAABB(origin -
m_Owner.GetOrigin(), range, mins, maxs);
271 bool IsResourceDecayEnabled()
273 return m_bEnableResourceDecay;
277 bool IsResourceGainEnabled()
279 return m_bEnableResourceGain;
283 bool IsEncapsulated()
285 return m_bIsEncapsulated;
289 override bool ShouldUpdate()
291 return super.ShouldUpdate() || m_bEnableResourceGain || m_bEnableResourceDecay;
295 bool IsAllowedToStoreResource()
301 bool CanStoreResource()
304 && (m_fResourceValueMax - m_fResourceValueCurrent);
308 ScriptInvoker GetOnResourcesChanged()
310 if (!m_OnResourcesChangedInvoker)
311 m_OnResourcesChangedInvoker =
new ScriptInvoker();
313 return m_OnResourcesChangedInvoker;
317 ScriptInvoker GetOnMaxResourcesChanged()
319 if (!m_OnMaxResourcesChangedInvoker)
320 m_OnMaxResourcesChangedInvoker =
new ScriptInvoker();
322 return m_OnMaxResourcesChangedInvoker;
326 ScriptInvoker GetOnResourcesDepleted()
328 if (!m_OnResourcesDepletedInvoker)
329 m_OnResourcesDepletedInvoker =
new ScriptInvoker();
331 return m_OnResourcesDepletedInvoker;
335 ScriptInvoker GetOnResourcesMaxedOut()
337 if (!m_OnResourcesMaxedOutInvoker)
338 m_OnResourcesMaxedOutInvoker =
new ScriptInvoker();
340 return m_OnResourcesMaxedOutInvoker;
344 ScriptInvoker GetOnGainChanged()
346 if (!m_OnGainChangedInvoker)
347 m_OnGainChangedInvoker =
new ScriptInvoker();
349 return m_OnGainChangedInvoker;
353 ScriptInvoker GetOnDecayChanged()
355 if (!m_OnDecayChangedInvoker)
356 m_OnDecayChangedInvoker =
new ScriptInvoker();
358 return m_OnDecayChangedInvoker;
362 ScriptInvoker GetOnGainEnabledChanged()
364 if (!m_OnGainEnabledChangedInvoker)
365 m_OnGainEnabledChangedInvoker =
new ScriptInvoker();
367 return m_OnGainEnabledChangedInvoker;
371 ScriptInvoker GetOnDecayEnabledChanged()
373 if (!m_OnDecayEnabledChangedInvoker)
374 m_OnDecayEnabledChangedInvoker =
new ScriptInvoker();
376 return m_OnDecayEnabledChangedInvoker;
387 bool SetResourceValue(
float value,
bool notifyChange =
true)
389 if (!m_ResourceEncapsulator)
390 return SetResourceValueUnsafe(value, notifyChange);
392 float previousValue = m_fResourceValueCurrent;
393 float newValue = Math.Clamp(value, 0.0, m_fResourceValueMax);
395 if (newValue > previousValue)
396 m_ResourceEncapsulator.RequestGeneration(newValue - previousValue, notifyChange);
397 else if (newValue < previousValue)
398 m_ResourceEncapsulator.RequestConsumtion(previousValue - newValue, notifyChange);
403 OnResourcesChanged(previousValue);
418 bool SetResourceValueUnsafe(
float value,
bool notifyChange =
true)
420 float previousValue = m_fResourceValueCurrent;
421 m_fResourceValueCurrent = Math.Clamp(value, 0.0, m_fResourceValueMax);
423 if (previousValue == m_fResourceValueCurrent)
427 OnResourcesChanged(previousValue);
433 bool SetMaxResourceValue(
float value,
bool notifyChange =
true)
435 float previousValue = m_fResourceValueMax;
436 m_fResourceValueMax = Math.Max(value, 0.0);
438 if (previousValue == m_fResourceValueMax)
442 OnMaxResourcesChanged(previousValue);
448 bool SetResourceGain(
float value,
bool notifyChange =
true)
450 float previousValue = m_fResourceGain;
451 m_fResourceGain = Math.Clamp(value, 0.0, m_fResourceValueMax);
453 if (previousValue == m_fResourceGain)
457 OnGainChanged(previousValue);
463 bool SetResourceDecay(
float value,
bool notifyChange =
true)
465 float previousValue = m_fResourceDecay;
466 m_fResourceDecay = Math.Clamp(value, 0.0, m_fResourceValueMax);
468 if (previousValue == m_fResourceDecay)
472 OnDecayChanged(previousValue);
478 void SetResourceGainTickrate(
float tickrate)
480 m_fResourceGainTickrate = tickrate;
484 void SetResourceDecayTickrate(
float tickrate)
486 m_fResourceDecayTickrate = tickrate;
490 void SetResourceGainTimeout(
float timeout)
492 m_fResourceGainTimeout = timeout;
496 void SetResourceDecayTimeout(
float timeout)
498 m_fResourceDecayTimeout = timeout;
504 m_ResourceEncapsulator = encapsulator;
508 bool IncreaseResourceValue(
float value,
bool notifyChange =
true)
510 return SetResourceValue(m_fResourceValueCurrent + value, notifyChange);
514 bool DecreaseResourceValue(
float value,
bool notifyChange =
true)
516 return SetResourceValue(m_fResourceValueCurrent - value, notifyChange);
520 bool MaxOutResourceValue(
bool notifyChange =
true)
522 return SetResourceValue(m_fResourceValueMax, notifyChange);
526 bool DepleteResourceValue(
bool notifyChange =
true)
528 return SetResourceValue(0.0, notifyChange);
534 m_eOnEmptyBehavior = behavior;
538 bool LinkInteractor(notnull SCR_ResourceInteractor interactor)
540 return m_aInteractors.Insert(interactor);
544 bool UnlinkInteractor(notnull SCR_ResourceInteractor interactor)
546 return m_aInteractors.RemoveItem(interactor);
550 void UnlinkEveryInteractor()
552 m_aInteractors.Clear();
556 bool EnableGain(
bool shouldEnable,
bool notifyChange =
true)
558 bool previousValue = m_bEnableResourceGain;
559 m_bEnableResourceGain = shouldEnable;
561 if (previousValue == m_bEnableResourceGain)
564 m_fResourceGainElapsedTime = 0.0;
567 OnGainEnabledChanged(previousValue);
579 if (m_bEnableResourceGain)
580 updateSystem.RegisterContainer(
this);
582 updateSystem.UnregisterContainer(
this);
588 bool EnableDecay(
bool shouldEnable,
bool notifyChange =
true)
590 bool previousValue = m_bEnableResourceDecay;
591 m_bEnableResourceDecay = shouldEnable;
593 if (previousValue == m_bEnableResourceDecay)
596 m_fResourceDecayElapsedTime = 0.0;
599 OnDecayEnabledChanged(previousValue);
611 if (m_bEnableResourceDecay)
612 updateSystem.RegisterContainer(
this);
614 updateSystem.UnregisterContainer(
this);
620 void SetIsEncapsulated(
bool shouldEnable)
622 m_bIsEncapsulated = shouldEnable;
624 if (!m_bIsEncapsulated)
627 SCR_ResourceInteractor interactor;
631 interactor = m_aInteractors[
index];
636 interactor.UnregisterContainer(
this);
647 void UpdateWeightMultiplier()
649 if (m_fResourceValueMax == 0.0)
652 m_fWeightMultiplier = m_fResourceValueCurrent / m_fResourceValueMax;
656 void DebugDraw(
bool shouldShowRange =
true)
664 if (m_bEnableResourceDecay)
665 decaying =
string.Format(
"\n Decaying %1 every %2s ", m_fResourceDecay, m_fResourceDecayTickrate);
667 if (m_bEnableResourceGain)
668 gaining =
string.Format(
"\n Gaining %1 every %2s ", m_fResourceGain, m_fResourceGainTickrate);
670 string infoText =
string.Format(
" Cur: %1 Max: %2 \n Storage: %3%4%5", GetResourceValue(), GetMaxResourceValue(),
SCR_Enum.GetEnumName(
EResourceContainerStorageType, m_eStorageType), decaying, gaining);
671 vector origin =
m_Owner.GetOrigin();
673 int textColor = 0xFFFFFFFF;
675 if (m_bIsEncapsulated)
676 textColor = 0xFF2222FF;
679 if (m_fResourceValueCurrent == 0.0)
680 textColor = 0xFFFF2222;
682 DebugTextWorldSpace.Create(
GetGame().GetWorld(), infoText, DebugTextFlags.CENTER | DebugTextFlags.FACE_CAMERA | DebugTextFlags.ONCE, origin[0], origin[1], origin[2], 10, textColor, 0xFF000000);
686 override void Update(
float timeslice)
688 float resourceValue = 0.0;
690 if (m_bEnableResourceGain)
691 ComputeResourceGain(timeslice, resourceValue);
693 if (m_bEnableResourceDecay)
694 ComputeResourceDecay(timeslice, resourceValue);
696 if (
m_ResourceComponent && resourceValue != 0.0 && SetResourceValue(m_fResourceValueCurrent + resourceValue))
701 float ComputeResourceDifference()
703 return m_fResourceValueMax - m_fResourceValueCurrent;
707 protected void ComputeResourceGain(
float timeslice, out
float resourceValue)
709 m_fResourceGainElapsedTime += timeslice;;
711 float resourceGainElapsedTimeRelative = m_fResourceGainElapsedTime - m_fResourceGainTimeout;
713 if (m_fResourceGainElapsedTime < m_fResourceGainTimeout
714 || resourceGainElapsedTimeRelative < m_fResourceGainTickrate
715 || m_fResourceGainTickrate <= 0.0)
718 resourceValue += m_fResourceGain * (
int)(resourceGainElapsedTimeRelative / m_fResourceGainTickrate);
719 m_fResourceGainElapsedTime = m_fResourceGainTimeout;
723 protected void ComputeResourceDecay(
float timeslice, out
float resourceValue)
725 m_fResourceDecayElapsedTime += timeslice;
727 float resourceDecayElapsedTimeRelative = m_fResourceDecayElapsedTime - m_fResourceDecayTimeout;
729 if (m_fResourceDecayElapsedTime < m_fResourceDecayTimeout
730 || resourceDecayElapsedTimeRelative < m_fResourceDecayTickrate
731 || m_fResourceDecayTickrate <= 0.0)
734 resourceValue -= m_fResourceDecay * (
int)(resourceDecayElapsedTimeRelative / m_fResourceDecayTickrate);
735 m_fResourceDecayElapsedTime = m_fResourceDecayTimeout;
739 protected void OnResourcesChanged(
float previousValue)
741 UpdateWeightMultiplier();
743 if (m_OnResourcesChangedInvoker)
744 m_OnResourcesChangedInvoker.Invoke(
this, previousValue);
746 foreach (SCR_ResourceInteractor interactor: m_aInteractors)
748 if (interactor && interactor != m_ResourceEncapsulator)
749 interactor.UpdateContainerResourceValue(
this, previousValue);
752 if (previousValue < m_fResourceValueCurrent)
753 OnResourcesIncreased(previousValue);
755 OnResourcesDecreased(previousValue);
766 editableEntity.SetHierarchyAsDirtyInParents();
770 protected void OnResourcesIncreased(
float previousValue)
773 if (m_fResourceValueCurrent == m_fResourceValueMax)
774 OnResourcesMaxedOut(previousValue);
776 if (previousValue != 0.0)
779 switch (m_eOnEmptyBehavior)
796 protected void OnResourcesDecreased(
float previousValue)
799 if (m_fResourceValueCurrent == 0.0)
800 OnResourcesDepleted(previousValue);
804 protected void OnResourcesDepleted(
float previousValue)
806 if (m_OnResourcesDepletedInvoker)
807 m_OnResourcesDepletedInvoker.Invoke(
this, previousValue);
809 switch (m_eOnEmptyBehavior)
819 RplComponent.DeleteRplEntity(
m_Owner,
false);
828 protected void OnResourcesMaxedOut(
float previousValue)
830 if (m_OnResourcesMaxedOutInvoker)
831 m_OnResourcesMaxedOutInvoker.Invoke(
this, previousValue);
835 protected void OnMaxResourcesChanged(
float previousValue)
837 UpdateWeightMultiplier();
839 if (m_OnMaxResourcesChangedInvoker)
840 m_OnMaxResourcesChangedInvoker.Invoke(
this, previousValue);
842 foreach (SCR_ResourceInteractor interactor: m_aInteractors)
845 interactor.UpdateContainerMaxResourceValue(
this, previousValue);
850 protected void OnGainChanged(
float previousValue)
852 if (m_OnGainChangedInvoker)
853 m_OnGainChangedInvoker.Invoke(
this, previousValue);
857 protected void OnDecayChanged(
float previousValue)
859 if (m_OnDecayChangedInvoker)
860 m_OnDecayChangedInvoker.Invoke(
this, previousValue);
864 protected void OnGainEnabledChanged(
float previousValue)
866 if (m_OnGainEnabledChangedInvoker)
867 m_OnGainEnabledChangedInvoker.Invoke(
this, previousValue);
871 protected void OnDecayEnabledChanged(
float previousValue)
873 if (m_OnDecayEnabledChangedInvoker)
874 m_OnDecayEnabledChangedInvoker.Invoke(
this, previousValue);
880 m_sDebugName = container.m_sDebugName;
881 m_eStorageType = container.m_eStorageType;
882 m_fResourceValueCurrent = container.m_fResourceValueCurrent;
883 m_fResourceValueMax = container.m_fResourceValueMax;
884 m_bEnableResourceGain = container.m_bEnableResourceGain;
885 m_fResourceGain = container.m_fResourceGain;
886 m_fResourceGainTickrate = container.m_fResourceGainTickrate;
887 m_bEnableResourceDecay = container.m_bEnableResourceDecay;
888 m_fResourceDecay = container.m_fResourceDecay;
889 m_fResourceDecayTickrate = container.m_fResourceDecayTickrate;
890 m_eResourceRights = container.m_eResourceRights;
891 m_eResourceType = container.m_eResourceType;
892 m_eOnEmptyBehavior = container.m_eOnEmptyBehavior;
899 m_ResourceComponent = SCR_ResourceComponent.Cast(owner.FindComponent(SCR_ResourceComponent));
900 IEntity parentEntity =
m_Owner.GetParent();
902 CopyFromContainer(container);
905 SCR_ResourceComponent parentResourceComponent;
916 parentResourceComponent = SCR_ResourceComponent.Cast(parentEntity.FindComponent(SCR_ResourceComponent));
918 if (parentResourceComponent)
919 encapsulator = parentResourceComponent.GetEncapsulator(m_eResourceType);
922 encapsulator.RegisterContainer(
this);
925 parentEntity = parentEntity.GetParent();
944 if (m_bEnableResourceGain || m_bEnableResourceDecay)
945 updateSystem.RegisterContainer(
this);
947 updateSystem.UnregisterContainer(
this);
954 override void Clear()
960 m_aInteractors[
index].UnregisterContainer(
this);
973 updateSystem.UnregisterContainer(
this);