Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_ResourceSystemHelper.c
Go to the documentation of this file.
2{
3 //~ How many decimals are displayed when showing supplies. Use SCR_ResourceSystemHelper.SuppliesToString(supplies) to ignore any decimals ending in 0
4 static const int DECIMALS_SUPPLIES = 1;
5
6 //------------------------------------------------------------------------------------------------
9 static bool IsGlobalResourceTypeEnabled(EResourceType resourceType = EResourceType.SUPPLIES)
10 {
12 if (!baseGameMode)
13 return true;
14
15 return baseGameMode.IsResourceTypeEnabled(resourceType);
16 }
17
18 //------------------------------------------------------------------------------------------------
26 static bool GetStoredResources(notnull SCR_ResourceComponent resourceComponent, out float storedRescources, EResourceType resourceType = EResourceType.SUPPLIES)
27 {
28 SCR_ResourceConsumer consumer;
29 storedRescources = 0;
30
31 if (resourceComponent.GetConsumer(EResourceGeneratorID.VEHICLE_UNLOAD, resourceType, consumer))
32 {
33 storedRescources = consumer.GetAggregatedResourceValue();
34 return true;
35 }
36
37 SCR_ResourceGenerator generator;
38 if (resourceComponent.GetGenerator(EResourceGeneratorID.VEHICLE_LOAD, resourceType, generator))
39 {
40 storedRescources = generator.GetAggregatedResourceValue();
41 return true;
42 }
43
44 if (resourceComponent.GetConsumer(EResourceGeneratorID.DEFAULT_STORAGE, resourceType, consumer))
45 {
46 storedRescources = consumer.GetAggregatedResourceValue();
47 return true;
48 }
49
50 return false;
51 }
52
53 //------------------------------------------------------------------------------------------------
62 static EResourceReason ConsumeResources(notnull SCR_ResourceComponent resourceComponent, float resourcesToConsume, bool failIfNotEnoughResources, EResourceType resourceType = EResourceType.SUPPLIES)
63 {
64 //~ If it does not fail if there are not enough resources than make sure to remove the max amount possible
65 if (!failIfNotEnoughResources)
66 {
67 float available;
68 GetAvailableResources(resourceComponent, available, resourceType);
69
70 if (available < resourcesToConsume)
71 resourcesToConsume = available;
72 }
73
74 SCR_ResourceConsumer consumer = GetAvailableResourceConsumer(resourceComponent, resourceType);
75 if (!consumer)
76 return EResourceReason.UNAVAILABLE;
77
78 return consumer.RequestConsumtion(resourcesToConsume).GetReason();
79 }
80
81 //------------------------------------------------------------------------------------------------
87 static string SuppliesToString(float supplies)
88 {
89 return SCR_FormatHelper.FloatToStringNoZeroDecimalEndings(supplies, DECIMALS_SUPPLIES);
90 }
91
92 //------------------------------------------------------------------------------------------------
101 static bool GetStoredAndMaxResources(notnull SCR_ResourceComponent resourceComponent, out float totalResources, out float maxResources, EResourceType resourceType = EResourceType.SUPPLIES)
102 {
103 SCR_ResourceConsumer consumer;
104
105 totalResources = 0;
106 maxResources = 0;
107
108 if (resourceComponent.GetConsumer(EResourceGeneratorID.VEHICLE_UNLOAD, resourceType, consumer))
109 {
110 maxResources = consumer.GetAggregatedMaxResourceValue();
111 totalResources = consumer.GetAggregatedResourceValue();
112
113 if (maxResources > 0)
114 return true;
115 }
116
117 SCR_ResourceGenerator generator;
118 if (resourceComponent.GetGenerator(EResourceGeneratorID.VEHICLE_LOAD, resourceType, generator))
119 {
120 maxResources = generator.GetAggregatedMaxResourceValue();
121 totalResources = generator.GetAggregatedResourceValue();
122
123 if (maxResources > 0)
124 return true;
125 }
126
127 if (resourceComponent.GetConsumer(EResourceGeneratorID.DEFAULT_STORAGE, resourceType, consumer))
128 {
129 maxResources = consumer.GetAggregatedMaxResourceValue();
130 totalResources = consumer.GetAggregatedResourceValue();
131
132 if (maxResources > 0)
133 return true;
134 }
135
136 //~ Reset again
137 totalResources = 0;
138 maxResources = 0;
139
140 return false;
141 }
142
143 //------------------------------------------------------------------------------------------------
152 static bool GetAvailableResources(notnull SCR_ResourceComponent resourceComponent, out float availableResources, EResourceGeneratorID resourceID = EResourceGeneratorID.DEFAULT, EResourceType resourceType = EResourceType.SUPPLIES)
153 {
154 SCR_ResourceConsumer consumer;
155 availableResources = 0;
156
157 if (resourceComponent.GetConsumer(resourceID, resourceType, consumer))
158 {
159 availableResources = consumer.GetAggregatedResourceValue();
160 return true;
161 }
162
163
164 return false;
165 }
166
167 //------------------------------------------------------------------------------------------------
174 static SCR_ResourceConsumer GetStorageConsumer(notnull SCR_ResourceComponent resourceComponent, EResourceType resourceType = EResourceType.SUPPLIES)
175 {
176 SCR_ResourceConsumer consumer = resourceComponent.GetConsumer(EResourceGeneratorID.DEFAULT_STORAGE, resourceType);
177 if (consumer)
178 return consumer;
179
180 consumer = resourceComponent.GetConsumer(EResourceGeneratorID.VEHICLE_UNLOAD, resourceType);
181 if (consumer)
182 return consumer;
183
184 return null;
185 }
186
187 //------------------------------------------------------------------------------------------------
194 static SCR_ResourceConsumer GetAvailableResourceConsumer(notnull SCR_ResourceComponent resourceComponent, EResourceType resourceType = EResourceType.SUPPLIES)
195 {
196 return resourceComponent.GetConsumer(EResourceGeneratorID.DEFAULT, resourceType);
197 }
198
199 //------------------------------------------------------------------------------------------------
206 static SCR_ResourceConsumer GetFirstValidConsumer(notnull SCR_ResourceComponent resourceComponent, EResourceType resourceType = EResourceType.SUPPLIES)
207 {
208 SCR_ResourceConsumer consumer = resourceComponent.GetConsumer(EResourceGeneratorID.DEFAULT_STORAGE, resourceType);
209 if (consumer)
210 return consumer;
211
212 consumer = resourceComponent.GetConsumer(EResourceGeneratorID.VEHICLE_UNLOAD, resourceType);
213 if (consumer)
214 return consumer;
215
216 consumer = resourceComponent.GetConsumer(EResourceGeneratorID.DEFAULT, resourceType);
217 if (consumer)
218 return consumer;
219
220 return null;
221 }
222
223 //------------------------------------------------------------------------------------------------
230 static SCR_ResourceGenerator GetFirstValidGenerator(notnull SCR_ResourceComponent resourceComponent, EResourceType resourceType = EResourceType.SUPPLIES)
231 {
232 SCR_ResourceGenerator generator = resourceComponent.GetGenerator(EResourceGeneratorID.DEFAULT_STORAGE, resourceType);
233 if (generator)
234 return generator;
235
236 generator = resourceComponent.GetGenerator(EResourceGeneratorID.VEHICLE_LOAD, resourceType);
237 if (generator)
238 return generator;
239
240 generator = resourceComponent.GetGenerator(EResourceGeneratorID.DEFAULT, resourceType);
241 if (generator)
242 return generator;
243
244 return null;
245 }
246
247 //------------------------------------------------------------------------------------------------
250 static float RoundRefundSupplyAmount(float refundCost)
251 {
252 return Math.Ceil(refundCost);
253 }
254
255 //------------------------------------------------------------------------------------------------
262 static EResourceReason SimpleResourceTransfer(notnull SCR_ResourceComponent resourceComponentFrom, notnull SCR_ResourceComponent resourceComponentTo, float transferAmount, bool allowPartialTransfer = true, EResourceType resourceType = EResourceType.SUPPLIES)
263 {
264 SCR_ResourceConsumer resourceConsumerFrom = GetFirstValidConsumer(resourceComponentFrom, resourceType);
265 if (!resourceConsumerFrom)
266 return EResourceReason.UNAVAILABLE;
267
268 SCR_ResourceGenerator resourceGeneratorTo = GetFirstValidGenerator(resourceComponentTo, resourceType);
269 if (!resourceGeneratorTo)
270 return EResourceReason.UNAVAILABLE;
271
272 float availableResources = resourceConsumerFrom.GetAggregatedResourceValue();
273 float actualTransferAmount = transferAmount;
274 if (actualTransferAmount > availableResources)
275 {
276 if (!allowPartialTransfer)
277 return EResourceReason.INSUFICIENT;
278
279 actualTransferAmount = transferAmount - availableResources;
280 }
281
282 float availableStorage = resourceGeneratorTo.GetAggregatedMaxResourceValue() - resourceGeneratorTo.GetAggregatedResourceValue();
283 if (availableStorage < actualTransferAmount)
284 {
285 if (!allowPartialTransfer)
286 return EResourceReason.INSUFICIENT;
287
288 actualTransferAmount = availableStorage;
289 }
290
291 resourceConsumerFrom.RequestConsumtion(actualTransferAmount);
292 resourceGeneratorTo.RequestGeneration(actualTransferAmount);
293 return EResourceReason.SUFFICIENT;
294 }
295}
ArmaReforgerScripted GetGame()
Definition game.c:1398
SCR_BaseGameMode GetGameMode()
EResourceGeneratorID
Definition Math.c:13
bool IsResourceTypeEnabled(EResourceType resourceType=EResourceType.SUPPLIES)
override float GetAggregatedResourceValue()
SCR_ResourceGenerationResponse RequestGeneration(float resourceAmount, SCR_ResourceGenerator generator=null)
override float GetAggregatedMaxResourceValue()