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;
8 [
Attribute(defvalue:
string.Empty, uiwidget: UIWidgets.EditBox,
desc:
"Identifier for debug prints",
category:
"Debugging")]
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))]
17 protected SCR_ResourceComponent m_ResourceComponent;
18 protected IEntity m_Owner;
36 vector GetOwnerOrigin()
54 int GetDebugNameHash()
56 return m_sDebugName.Hash();
61 SCR_ResourceComponent GetComponent()
77 return m_eResourceRights;
98 bool IsGridUpdateIdGreaterThan(
int gridUpdateId)
107 m_eResourceRights = rights;
112 void SetGridUpdateId(
int gridUpdateId)
120 void Update(
float timeslice)
128 void UpdateInner(
float timeslice)
151 protected ref ScriptInvoker m_OnResourcesChangedInvoker;
152 protected ref ScriptInvoker m_OnMaxResourcesChangedInvoker;
155 protected float m_fAggregatedResourceValue = -1.0;
156 protected float m_fAggregatedMaxResourceValue = -1.0;
167 float GetResourceGridRange()
174 float GetAggregatedResourceValue()
181 float GetAggregatedMaxResourceValue()
188 int GetContainerCount()
204 return m_eResourceType;
216 ScriptInvoker GetOnResourcesChanged()
218 if (!m_OnResourcesChangedInvoker)
219 m_OnResourcesChangedInvoker =
new ScriptInvoker();
221 return m_OnResourcesChangedInvoker;
227 ScriptInvoker GetOnMaxResourcesChanged()
229 if (!m_OnMaxResourcesChangedInvoker)
230 m_OnMaxResourcesChangedInvoker =
new ScriptInvoker();
232 return m_OnMaxResourcesChangedInvoker;
254 if (container.GetResourceType() != m_eResourceType)
257 switch (m_eResourceRights)
263 return m_Owner == container.GetOwner();
275 FactionAffiliationComponent interactorFactionComponent =
m_ResourceComponent.GetFactionAffiliationComponent();
277 if (!interactorFactionComponent)
280 FactionAffiliationComponent containerrFactionComponent = container.GetComponent().GetFactionAffiliationComponent();
282 if (!containerrFactionComponent)
285 return interactorFactionComponent.GetAffiliatedFaction() == containerrFactionComponent.GetAffiliatedFaction();
298 bool IsAllowed(notnull IEntity entity,
EResourceType resourceType)
300 if (resourceType != m_eResourceType)
303 switch (m_eResourceRights)
314 FactionAffiliationComponent interactorFactionComponent =
m_ResourceComponent.GetFactionAffiliationComponent();
316 if (!interactorFactionComponent)
319 SCR_ResourceComponent containerResourceComponent = SCR_ResourceComponent.Cast(entity.FindComponent(SCR_ResourceComponent));
321 if (!containerResourceComponent)
326 if (!containerResourceComponent.GetContainer(resourceType, container))
332 FactionAffiliationComponent containerrFactionComponent = container.GetComponent().GetFactionAffiliationComponent();
334 if (!containerrFactionComponent)
337 return interactorFactionComponent.GetAffiliatedFaction() == containerrFactionComponent.GetAffiliatedFaction();
352 return IsAllowed(container) && container.IsAllowed(
this);
402 bool UnregisterContainer(
int containerIndex)
444 void OnResourcesChanged(
float previousValue)
446 m_fAggregatedResourceValue = GetAggregatedResourceValue();
448 if (m_OnResourcesChangedInvoker)
449 m_OnResourcesChangedInvoker.Invoke(
this, previousValue);
459 void OnMaxResourcesChanged(
float previousValue)
461 m_fAggregatedMaxResourceValue = GetAggregatedMaxResourceValue();
463 if (m_OnMaxResourcesChangedInvoker)
464 m_OnMaxResourcesChangedInvoker.Invoke(
this, previousValue);
476 container.LinkInteractor(
this);
483 container.UnlinkInteractor(
this);
492 m_ResourceComponent = SCR_ResourceComponent.Cast(owner.FindComponent(SCR_ResourceComponent));
496 override void Clear()
500 m_fAggregatedResourceValue = -1.0;
501 m_fAggregatedMaxResourceValue = -1.0;
506 class SCR_ResourceConsumer : SCR_ResourceInteractor
508 protected static const int CODEC_CONSUMER_PACKET_BYTESIZE = 38;
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;
516 [
Attribute(
"1", uiwidget: UIWidgets.SpinBox,
params:
string.Format(
"0.0 %1 1.0",
float.MAX))]
517 protected float m_fBuyMultiplier;
519 [
Attribute(uiwidget: UIWidgets.SpinBox,
params:
string.Format(
"0.0 %1 1.0",
float.MAX))]
520 protected float m_fSellMultiplier;
522 [
Attribute(uiwidget: UIWidgets.CheckBox)]
523 protected bool m_bIsIgnoringItself;
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;
534 protected ref ScriptInvoker m_OnExchangeStateChangedInvoker;
537 override float GetResourceGridRange()
539 return m_fResourceRange;
544 float GetResourceRange()
546 return m_fResourceRange;
551 float GetBuyMultiplier()
553 return m_fBuyMultiplier;
558 float GetSellMultiplier()
560 return m_fSellMultiplier;
564 override float GetAggregatedResourceValue()
566 if (!m_ContainerQueue)
567 return m_fAggregatedResourceValue;
569 return m_ContainerQueue.GetAggregatedResourceValue();
573 override float GetAggregatedMaxResourceValue()
575 if (!m_ContainerQueue)
576 return m_fAggregatedMaxResourceValue;
578 return m_ContainerQueue.GetAggregatedMaxResourceValue();
582 override int GetContainerCount()
584 if (!m_ContainerQueue)
587 return m_ContainerQueue.GetContainerCount();
593 return m_ContainerQueue;
598 ScriptInvoker GetOnResourceRangeChanged()
600 if (!m_OnResourceRangeChangedInvoker)
601 m_OnResourceRangeChangedInvoker =
new ScriptInvoker();
603 return m_OnResourceRangeChangedInvoker;
608 ScriptInvoker GetOnBuyMultiplierChanged()
610 if (!m_OnBuyMultiplierChangedInvoker)
611 m_OnBuyMultiplierChangedInvoker =
new ScriptInvoker();
613 return m_OnBuyMultiplierChangedInvoker;
618 ScriptInvoker GetOnSellMultiplierChanged()
620 if (!m_OnSellMultiplierChangedInvoker)
621 m_OnSellMultiplierChangedInvoker =
new ScriptInvoker();
623 return m_OnSellMultiplierChangedInvoker;
629 ScriptInvoker GetOnConsumtionStateChanged()
631 if (!m_OnConsumtionStateChangedInvoker)
632 m_OnConsumtionStateChangedInvoker =
new ScriptInvoker();
634 return m_OnConsumtionStateChangedInvoker;
639 ScriptInvoker GetOnExchangeStateChanged()
641 if (!m_OnExchangeStateChangedInvoker)
642 m_OnExchangeStateChangedInvoker =
new ScriptInvoker();
644 return m_OnExchangeStateChangedInvoker;
651 return m_eGeneratorIdentifier;
657 return m_eGeneratorIdentifier;
662 bool IsIgnoringItself()
664 return m_bIsIgnoringItself;
671 return m_bIsConsuming;
678 return m_bIsExchanging;
682 override bool ShouldUpdate()
690 return (!m_bIsIgnoringItself || container.GetOwner() !=
m_Owner) && super.CanInteractWith(container);
696 if (!m_ContainerQueue)
697 return super.FindContainer(container);
699 return m_ContainerQueue.FindContainer(container);
706 bool SetResourceRange(
float value,
bool notifyChange =
true)
708 float previousValue = m_fResourceRange;
709 m_fResourceRange = Math.Max(value, 0.0);
711 if (previousValue == m_fResourceRange)
715 OnResourceRangeChanged(previousValue);
724 bool SetBuyMultiplier(
float value,
bool notifyChange =
true)
726 float previousValue = m_fBuyMultiplier;
727 m_fBuyMultiplier = Math.Max(value, 0.0);
729 if (previousValue == m_fBuyMultiplier)
733 OnBuyMultiplierChanged(previousValue);
742 bool SetSellMultiplier(
float value,
bool notifyChange =
true)
744 float previousValue = m_fSellMultiplier;
745 m_fSellMultiplier = Math.Max(value, 0.0);
747 if (previousValue == m_fSellMultiplier)
751 OnSellMultiplierChanged(previousValue);
762 bool EnableConsumtion(
bool shouldEnable,
bool notifyChange =
true)
764 bool previousValue = m_bIsConsuming;
765 m_bIsConsuming = shouldEnable;
767 if (shouldEnable == m_bIsConsuming)
771 OnConsumtionStateChanged(previousValue);
781 bool EnableExchange(
bool shouldEnable,
bool notifyChange =
true)
783 bool previousValue = m_bIsExchanging;
784 m_bIsExchanging = shouldEnable;
786 if (previousValue == m_bIsExchanging)
790 OnExchangeStateChanged(previousValue);
798 if (CanInteractWith(container)
802 OnContainerRegistered(container);
815 OnContainerRegistered(container);
824 override bool UnregisterContainer(
int containerIndex)
826 return m_ContainerQueue && m_ContainerQueue.PopContainerAt(containerIndex);
832 return m_ContainerQueue && m_ContainerQueue.PopContainerAt(m_ContainerQueue.FindContainer(container));
842 GetGame().GetResourceGrid().UpdateInteractor(
this);
849 if (resourceCost > response.GetAvailableSupply())
856 if (resourceCost != 0.0 && response.GetAvailableSupply() == 0)
875 if (!response || response.GetReason() !=
EResourceReason.SUFFICIENT || !m_bIsConsuming)
882 m_ContainerQueue.PerformSorting();
884 int containerCount = m_ContainerQueue.GetContainerCount();
886 for (
int i = 0; i < containerCount && resourceCost > 0.0; i++)
888 container = m_ContainerQueue.GetContainerAt(i);
889 resourceUsed = Math.Min(resourceCost, container.GetResourceValue());
890 resourceCost -= resourceUsed;
891 encapsulator = container.GetResourceEncapsulator();
894 encapsulator.RequestConsumtion(resourceUsed);
896 container.DecreaseResourceValue(resourceUsed);
912 vector origin = GetOwnerOrigin();
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());
920 if (m_ContainerQueue)
921 m_ContainerQueue.DebugDraw();
928 m_ContainerQueue.UpdateContainerResourceValue(container.GetResourceValue(), previousValue)
935 OnMaxResourcesChanged(
936 m_ContainerQueue.UpdateContainerMaxResourceValue(container.GetMaxResourceValue(), previousValue)
941 protected void OnResourceRangeChanged(
float previousValue)
943 if (m_OnResourceRangeChangedInvoker)
944 m_OnResourceRangeChangedInvoker.Invoke(
this, previousValue);
948 protected void OnBuyMultiplierChanged(
float previousValue)
950 if (m_OnBuyMultiplierChangedInvoker)
951 m_OnBuyMultiplierChangedInvoker.Invoke(
this, previousValue);
955 protected void OnSellMultiplierChanged(
float previousValue)
957 if (m_OnSellMultiplierChangedInvoker)
958 m_OnSellMultiplierChangedInvoker.Invoke(
this, previousValue);
963 protected void OnConsumtionStateChanged(
float previousValue)
965 if (m_OnConsumtionStateChangedInvoker)
966 m_OnConsumtionStateChangedInvoker.Invoke(
this, previousValue);
970 protected void OnExchangeStateChanged(
float previousValue)
972 if (m_OnExchangeStateChangedInvoker)
973 m_OnExchangeStateChangedInvoker.Invoke(
this, previousValue);
977 override void Initialize(notnull IEntity owner)
979 super.Initialize(owner);
983 if (container && container.IsEncapsulated())
986 m_bIsConsuming =
true;
988 if (m_ContainerQueue)
989 m_ContainerQueue.Initialize(
this);
992 RegisterContainer(container);
996 override void Clear()
1000 if (m_ContainerQueue)
1001 m_ContainerQueue.Clear();
1003 GetGame().GetResourceSystemSubscriptionManager().OnResourceInteractorDeleted(
this);
1007 protected bool PropCompareNetworkedVariables(SSnapSerializerBase snapshot, ScriptCtx hint)
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);
1025 protected bool ExtractNetworkedVariables(SSnapSerializerBase snapshot, ScriptCtx hint)
1027 float aggregatedResourceValue = GetAggregatedResourceValue();
1028 float aggregatedMaxResourceValue = GetAggregatedMaxResourceValue();
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);
1047 protected bool InjectNetworkedVariables(SSnapSerializerBase snapshot, ScriptCtx hint)
1049 RplId componentRplId;
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);
1078 static void Encode(SSnapSerializerBase snapshot, ScriptCtx ctx, ScriptBitSerializer packet)
1080 snapshot.Serialize(packet, SCR_ResourceConsumer.CODEC_CONSUMER_PACKET_BYTESIZE);
1089 static bool Decode(ScriptBitSerializer packet, ScriptCtx ctx, SSnapSerializerBase snapshot)
1091 return snapshot.Serialize(packet, SCR_ResourceConsumer.CODEC_CONSUMER_PACKET_BYTESIZE);
1100 static bool SnapCompare(SSnapSerializerBase lhs, SSnapSerializerBase rhs , ScriptCtx ctx)
1102 return lhs.CompareSnapshots(rhs, SCR_ResourceConsumer.CODEC_CONSUMER_PACKET_BYTESIZE);
1111 static bool PropCompare(SCR_ResourceConsumer instance, SSnapSerializerBase snapshot, ScriptCtx ctx)
1113 return instance.PropCompareNetworkedVariables(snapshot, ctx);
1122 static bool Extract(SCR_ResourceConsumer instance, ScriptCtx ctx, SSnapSerializerBase snapshot)
1124 return instance.ExtractNetworkedVariables(snapshot, ctx);
1133 static bool Inject(SSnapSerializerBase snapshot, ScriptCtx ctx, SCR_ResourceConsumer instance)
1135 return instance.InjectNetworkedVariables(snapshot, ctx);