4 static const int FIRST_CONTAINER_INDEX = 0;
5 static const int INVALID_CONTAINER_INDEX = -1;
6 static const int INVALID_CONTAINER_COUNT = -1;
8 protected SCR_ResourceComponent m_ResourceComponent;
9 protected ref array<SCR_ResourceContainer> m_aRegisteredContainers =
new array<SCR_ResourceContainer>();
10 protected float m_fAggregatedResourceValue;
11 protected float m_fAggregatedMaxResourceValue;
20 float GetAggregatedResourceValue()
22 #ifdef SANDBOX_RESOURCES_DEBUG
25 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_fAggregatedResourceValue is lower than SCR_ResourceActor.RESOURCES_LOWER_LIMIT.",
"The current aggregated resources can not be lower than SCR_ResourceActor.RESOURCES_LOWER_LIMIT.");
30 return m_fAggregatedResourceValue;
32 return m_fAggregatedResourceValue;
43 float GetAggregatedMaxResourceValue()
45 #ifdef SANDBOX_RESOURCES_DEBUG
48 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_fAggregatedMaxResourceValue is lower than SCR_ResourceActor.RESOURCES_LOWER_LIMIT.",
"The current maximum aggregated resources can not be lower than SCR_ResourceActor.RESOURCES_LOWER_LIMIT.");
53 return m_fAggregatedMaxResourceValue;
55 return m_fAggregatedMaxResourceValue;
71 #ifdef SANDBOX_RESOURCES_DEBUG
72 if (!m_aRegisteredContainers)
74 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_aRegisteredContainers is null.",
"The array of registered containers has to be present.");
79 if (m_aRegisteredContainers.IsEmpty())
81 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_aRegisteredContainers is empty.",
"The method should not be called if the array of registered containers is empty.");
90 Debug.Error2(
"SCR_ResourceContainerQueueBase::GetFirstContainer found null instead of a resource container.",
"There should never be null values in the registered containers array.");
112 #ifdef SANDBOX_RESOURCES_DEBUG
113 if (!m_aRegisteredContainers)
115 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_aRegisteredContainers is null.",
"The array of registered containers has to be present.");
120 if (m_aRegisteredContainers.IsEmpty())
122 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_aRegisteredContainers is empty.",
"The method should not be called if the array of registered containers is empty.");
127 if (!m_aRegisteredContainers.IsIndexValid(
index))
129 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_aRegisteredContainers has no element at the provided index.",
"The method should not be called if the array of registered containers does not contain an element at the provided index.");
138 Debug.Error2(
"SCR_ResourceContainerQueueBase::GetContainerAt found null instead of a resource container.",
"There should never be null values in the registered containers array.");
145 return m_aRegisteredContainers[
index];
155 int GetContainerCount()
157 #ifdef SANDBOX_RESOURCES_DEBUG
158 if (!m_aRegisteredContainers)
160 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_aRegisteredContainers is null.",
"The array of registered containers has to be present.");
165 return m_aRegisteredContainers.Count();
167 return m_aRegisteredContainers.Count();
182 #ifdef SANDBOX_RESOURCES_DEBUG
184 if (!m_aRegisteredContainers)
186 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_aRegisteredContainers is null.",
"The array of registered containers has to be present.");
191 if (m_aRegisteredContainers.Contains(container))
193 Debug.Error2(
"SCR_ResourceContainerQueueBase::RegisterContainer tried to register an already registered resource container.",
"A resource container should not be registered more than once.");
198 return m_aRegisteredContainers.Insert(container);
200 return m_aRegisteredContainers.Insert(container);
214 #ifdef SANDBOX_RESOURCES_DEBUG
215 if (!m_aRegisteredContainers)
217 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_aRegisteredContainers is null.",
"The array of registered containers has to be present.");
222 return m_aRegisteredContainers.Find(container);
224 return m_aRegisteredContainers.Find(container);
237 #ifdef SANDBOX_RESOURCES_DEBUG
238 if (!m_aRegisteredContainers)
240 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_aRegisteredContainers is null.",
"The array of registered containers has to be present.");
245 if (m_aRegisteredContainers.IsEmpty())
247 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_aRegisteredContainers is empty.",
"The method should not be called if the array of registered containers is empty.");
277 #ifdef SANDBOX_RESOURCES_DEBUG
278 if (!m_aRegisteredContainers)
280 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_aRegisteredContainers is null.",
"The array of registered containers has to be present.");
285 if (m_aRegisteredContainers.IsEmpty())
287 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_aRegisteredContainers is empty.",
"The method should not be called if the array of registered containers is empty.");
292 if (!m_aRegisteredContainers.IsIndexValid(
index))
294 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_aRegisteredContainers has no element at the provided index.",
"The method should not be called if the array of registered containers does not contain an element at the provided index.");
303 Debug.Error2(
"SCR_ResourceContainerQueueBase::PopContainerAt found null instead of a resource container.",
"There should never be null values in the registered containers array.");
308 m_aRegisteredContainers.RemoveOrdered(
index);
312 if (!m_aRegisteredContainers.IsIndexValid(
index))
317 m_aRegisteredContainers.RemoveOrdered(
index);
331 void SetAggregatedResourceValue(
float value)
333 #ifdef SANDBOX_RESOURCES_DEBUG
336 Debug.Error2(
"SCR_ResourceContainerQueueBase::SetAggregatedResourceValue The value is lower than SCR_ResourceActor.RESOURCES_LOWER_LIMIT.",
"The current aggregated resources can not be lower than SCR_ResourceActor.RESOURCES_LOWER_LIMIT.");
341 if (value > m_fAggregatedMaxResourceValue)
343 Debug.Error2(
"SCR_ResourceContainerQueueBase::SetAggregatedResourceValue The value is greater than the current maximum aggregated resources value.",
"The current aggregated resources can not be greater than than the current maximum aggregated resources value.");
348 m_fAggregatedResourceValue = value;
350 m_fAggregatedResourceValue = value;
363 void SetAggregatedMaxResourceValue(
float value)
365 #ifdef SANDBOX_RESOURCES_DEBUG
368 Debug.Error2(
"SCR_ResourceContainerQueueBase::SetAggregatedMaxResourceValue The value is lower than SCR_ResourceActor.RESOURCES_LOWER_LIMIT.",
"The current maximum aggregated resources can not be lower than SCR_ResourceActor.RESOURCES_LOWER_LIMIT.");
373 if (value < m_fAggregatedResourceValue)
375 Debug.Error2(
"SCR_ResourceContainerQueueBase::SetAggregatedMaxResourceValue The value is lower than the current aggregated resources value.",
"The current maximum aggregated resources can not be lower than than the current aggregated resources value.");
380 m_fAggregatedMaxResourceValue = value;
382 m_fAggregatedMaxResourceValue = value;
395 void IncreaseAggregatedResourceValue(
float increment)
397 #ifdef SANDBOX_RESOURCES_DEBUG
400 Debug.Error2(
"SCR_ResourceContainerQueueBase::IncreaseAggregatedResourceValue The increment is lower than 0.0.",
"The increment has to be a positive number.");
405 SetAggregatedResourceValue(m_fAggregatedResourceValue + increment);
407 m_fAggregatedResourceValue += increment;
420 void DecreaseAggregatedResourceValue(
float decrement)
422 #ifdef SANDBOX_RESOURCES_DEBUG
425 Debug.Error2(
"SCR_ResourceContainerQueueBase::DecreaseAggregatedResourceValue The decrement is lower than 0.0.",
"The decrement has to be a positive number.");
430 SetAggregatedResourceValue(m_fAggregatedResourceValue - decrement);
432 m_fAggregatedResourceValue -= decrement;
446 void IncreaseAggregatedMaxResourceValue(
float increment)
448 #ifdef SANDBOX_RESOURCES_DEBUG
451 Debug.Error2(
"SCR_ResourceContainerQueueBase::IncreaseAggregatedMaxResourceValue The increment is lower than 0.0.",
"The increment has to be a positive number.");
456 SetAggregatedMaxResourceValue(m_fAggregatedResourceValue + increment);
458 m_fAggregatedMaxResourceValue += increment;
471 void DecreaseAggregatedMaxResourceValue(
float decrement)
473 #ifdef SANDBOX_RESOURCES_DEBUG
476 Debug.Error2(
"SCR_ResourceContainerQueueBase::DecreaseAggregatedMaxResourceValue The decrement is lower than 0.0.",
"The decrement has to be a positive number.");
481 SetAggregatedMaxResourceValue(m_fAggregatedResourceValue - decrement);
483 m_fAggregatedMaxResourceValue -= decrement;
491 array<SCR_ResourceContainer> Clear()
493 #ifdef SANDBOX_RESOURCES_DEBUG
494 array<SCR_ResourceContainer> clearedContainers = {};
496 if (!m_aRegisteredContainers)
498 Debug.Error2(
"SCR_ResourceContainerQueueBase::m_aRegisteredContainers is null.",
"The array of registered containers has to be present.");
500 return clearedContainers;
503 for (
int index = m_aRegisteredContainers.Count() - 1;
index >= 0; --
index)
505 if (!m_aRegisteredContainers[
index])
507 Debug.Error2(
"SCR_ResourceContainerQueueBase::Clear found null instead of a resource container.",
"There should never be null values in the registered containers array.");
509 return clearedContainers;
513 clearedContainers.Insert(PopContainerAt(
index));
516 m_aRegisteredContainers.Clear();
520 return clearedContainers;
522 array<SCR_ResourceContainer> clearedContainers = {};
524 for (
int index = m_aRegisteredContainers.Count() - 1;
index >= 0; --
index)
527 clearedContainers.Insert(PopContainerAt(
index));
530 m_aRegisteredContainers.Clear();
535 return clearedContainers;
540 void PerformSorting();
543 void Initialize(notnull SCR_ResourceInteractor interactor)
555 float UpdateContainerResourceValue(
float currentValue,
float previousValue)
561 float UpdateContainerMaxResourceValue(
float currentValue,
float previousValue)
585 protected ResourceInteractorType m_Interactor;
588 protected ref array<ref SCR_ResourceStoragePolicyBase<ResourceInteractorType>> m_StoragePolicies;
592 protected int GetStoragePolicyCount()
594 return m_StoragePolicies.Count();
605 bool shouldIncrementOffset;
606 SCR_ResourceContainerStorageQueue<ResourceInteractorType> storageQueue;
610 foreach (SCR_ResourceStoragePolicyBase<ResourceInteractorType> policy: m_StoragePolicies)
612 storageQueue = policy.GetStorageQueue();
614 if (shouldIncrementOffset)
616 storageQueue.IncrementOffset(1);
620 resourceValue = container.GetResourceValue();
622 if (!policy.IsStorageTypeValid(storageType))
625 m_fAggregatedResourceValue += resourceValue;
626 m_fAggregatedMaxResourceValue += container.GetMaxResourceValue();
628 shouldIncrementOffset =
true;
629 position = storageQueue.RegisterContainer(container, m_Interactor);
638 bool shouldDecrementOffset;
639 SCR_ResourceContainerStorageQueue<ResourceInteractorType> storageQueue;
642 foreach (SCR_ResourceStoragePolicyBase<ResourceInteractorType> policy: m_StoragePolicies)
644 storageQueue = policy.GetStorageQueue();
646 if (storageQueue.IsEmpty())
649 if (shouldDecrementOffset)
651 storageQueue.DecrementOffset(1);
655 container = m_aRegisteredContainers[0];
656 shouldDecrementOffset =
true;
660 m_fAggregatedResourceValue -= container.GetResourceValue();
661 m_fAggregatedMaxResourceValue -= container.GetMaxResourceValue();
663 m_Interactor.OnContainerUnregistered(container);
666 storageQueue.UnregisterFirstContainer();
675 if (!m_aRegisteredContainers.IsIndexValid(
index))
679 bool shouldDecrementOffset;
680 SCR_ResourceContainerStorageQueue<ResourceInteractorType> storageQueue;
683 foreach (SCR_ResourceStoragePolicyBase<ResourceInteractorType> policy: m_StoragePolicies)
685 storageQueue = policy.GetStorageQueue();
687 if (storageQueue.IsEmpty())
690 if (shouldDecrementOffset)
692 storageQueue.DecrementOffset(1);
696 offsetPosition = storageQueue.GetOffsetPosition();
698 if (index < offsetPosition || index > offsetPosition + storageQueue.GetContainerCount())
701 container = m_aRegisteredContainers[
index];
702 shouldDecrementOffset =
true;
704 storageQueue.UnregisterContainerAt(
index - offsetPosition);
708 m_fAggregatedResourceValue -= container.GetResourceValue();
709 m_fAggregatedMaxResourceValue -= container.GetMaxResourceValue();
711 m_Interactor.OnContainerUnregistered(container);
718 override void PerformSorting()
720 array<SCR_ResourceContainer> registeredContainers =
Clear();
727 m_Interactor.RegisterContainer(container);
733 override array<SCR_ResourceContainer>
Clear()
735 array<SCR_ResourceContainer> clearedContainers = {};
736 clearedContainers.Copy(m_aRegisteredContainers);
738 for (
int index = m_aRegisteredContainers.Count() - 1;
index >= 0; --
index)
740 m_Interactor.UnregisterContainer(
index);
743 m_aRegisteredContainers.Clear();
748 foreach (SCR_ResourceStoragePolicyBase<ResourceInteractorType> policy: m_StoragePolicies)
750 policy.ResetStorageQueue();
753 return clearedContainers;
757 override void Initialize(notnull SCR_ResourceInteractor interactor)
759 SCR_ResourceContainerStorageQueue<ResourceInteractorType> storageQueue;
760 m_Interactor = ResourceInteractorType.Cast(interactor);
762 foreach (
int idx, SCR_ResourceStoragePolicyBase<ResourceInteractorType> policy: m_StoragePolicies)
764 storageQueue = policy.GetStorageQueue();
769 storageQueue =
new SCR_ResourceContainerStorageQueue<ResourceInteractorType>();
771 policy.SetStorageQueue(storageQueue);
773 Print(
string.Format(
"%1:\n\tPolicy at index %2 does not have a defined storage queue.\n\tUsing default storage queue instead.", Type().ToString(), idx), LogLevel.WARNING);
776 storageQueue.Initialize(m_aRegisteredContainers);
781 override float UpdateContainerResourceValue(
float currentValue,
float previousValue)
783 float previousAggregatedValue = m_fAggregatedResourceValue;
784 m_fAggregatedResourceValue += currentValue - previousValue;
786 return previousAggregatedValue;
790 override float UpdateContainerMaxResourceValue(
float currentValue,
float previousValue)
792 float previousAggregatedMaxValue = m_fAggregatedMaxResourceValue;
793 m_fAggregatedMaxResourceValue += currentValue - previousValue;
795 return previousAggregatedMaxValue;