Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_PlayerSupplyAllocationComponent.c
Go to the documentation of this file.
4
5class SCR_PlayerSupplyAllocationComponent : ScriptComponent
6{
7 [RplProp(onRplName: "MilitarySupplyAllocationReplicated", condition: RplCondition.OwnerOnly)]
8 protected int m_iPlayerMilitarySupplyAllocation;
9
10 [RplProp(onRplName: "AvailableAllocatedSuppliesReplicated", condition: RplCondition.OwnerOnly)]
12
14
16
18
20
22
23 protected bool m_bIsEnabled;
24
25 //------------------------------------------------------------------------------------------------
26 protected override void OnPostInit(IEntity owner)
27 {
28 if (!SetUpConfigData())
29 return;
30
32
33 if (m_bIsEnabled)
34 {
36 if (gameMode)
37 {
38 gameMode.GetOnPlayerConnected().Insert(OnPlayerConnected);
39 gameMode.GetOnPlayerKilled().Insert(OnPlayerKilled);
40 gameMode.GetOnPlayerDeleted().Insert(OnPlayerDeleted);
42 }
43
46 m_PlayerController.m_OnPossessed.Insert(OnCharacterPossessed);
47 }
48
49 #ifdef ENABLE_DIAG
51 DiagMenu.RegisterMenu(SCR_DebugMenuID.DEBUGUI_CAMPAIGN_MILITARY_SUPPLY_ALLOCATION, "Military Supply Allocation", "Conflict");
52 DiagMenu.RegisterBool(SCR_DebugMenuID.DEBUGUI_CAMPAIGN_MILITARY_SUPPLY_ALLOCATION_DEBUG, "", "Enable MSA debug", "Military Supply Allocation");
53 #endif
54 }
55
56 //------------------------------------------------------------------------------------------------
58 protected bool SetUpConfigData()
59 {
60 SCR_ArsenalManagerComponent arsenalManagerComponent;
61 SCR_ArsenalManagerComponent.GetArsenalManager(arsenalManagerComponent);
62
63 if (!arsenalManagerComponent)
64 return false;
65
66 m_MilitarySupplyAllocationConfig = arsenalManagerComponent.GetMilitarySupplyApplicationConfigData();
68 return false;
69
70 m_bIsEnabled = arsenalManagerComponent.IsMilitarySupplyAllocationEnabled();
71 arsenalManagerComponent.GetOnMilitarySupplyAllocationToggle().Insert(OnMilitarySupplyAllocationEnabledChanged);
72
73 return true;
74 }
75
76 //------------------------------------------------------------------------------------------------
84
85 //------------------------------------------------------------------------------------------------
93
94 //------------------------------------------------------------------------------------------------
100
101 //------------------------------------------------------------------------------------------------
103 {
105 m_MilitarySupplyAllocationChanged.Invoke(m_iPlayerMilitarySupplyAllocation);
106 }
107
108 //------------------------------------------------------------------------------------------------
110 {
111 return m_iPlayerMilitarySupplyAllocation;
112 }
113
114 //------------------------------------------------------------------------------------------------
119
120 //------------------------------------------------------------------------------------------------
125
126 //------------------------------------------------------------------------------------------------
131 {
132 if (!m_bIsEnabled || IsProxy())
133 return;
134
135 if (amount == 0)
136 return;
137
138 int newAvailableAllocatedSupplies = m_iPlayerAvailableAllocatedSupplies + amount;
139
140 m_iPlayerAvailableAllocatedSupplies = Math.ClampInt(newAvailableAllocatedSupplies, 0, m_iPlayerMilitarySupplyAllocation);
141
144
145 Replication.BumpMe();
146 }
147
148 //------------------------------------------------------------------------------------------------
153 {
154 if (!m_bIsEnabled || IsProxy())
155 return;
156
157 if (amount == m_iPlayerMilitarySupplyAllocation)
158 return;
159
160 int availableAllocatedSuppliesToAdd = amount - m_iPlayerMilitarySupplyAllocation;
161
162 m_iPlayerMilitarySupplyAllocation = amount;
163
165 m_MilitarySupplyAllocationChanged.Invoke(m_iPlayerMilitarySupplyAllocation);
166
167 AddPlayerAvailableAllocatedSupplies(availableAllocatedSuppliesToAdd);
168 }
169
170 //------------------------------------------------------------------------------------------------
174 {
175 return m_iPlayerAvailableAllocatedSupplies - amount >= 0;
176 }
177
178 //------------------------------------------------------------------------------------------------
180 {
181 if (!m_PlayerController.GetMainEntity())
182 return;
183
185 }
186
187 //------------------------------------------------------------------------------------------------
190 {
192 if (!playerXPHandlerComponent)
193 return;
194
195 int thresholdValue = m_MilitarySupplyAllocationConfig.GetAvailableAllocatedSuppliesReplenishmentThresholdValueAtRank(playerXPHandlerComponent.GetPlayerRankByXP());
196
197 if (m_iPlayerAvailableAllocatedSupplies < thresholdValue)
199
200 m_fAvailableAllocatedSuppliesReplenishmentTimer = m_MilitarySupplyAllocationConfig.GetAvailableAllocatedSuppliesReplenishmentTimer();
201 }
202
203 //------------------------------------------------------------------------------------------------
208 protected void OnPlayerDisconnected(int playerId, KickCauseCode cause, int timeout)
209 {
210 if (m_PlayerController.GetPlayerId() != playerId)
211 return;
212
214 if (playerXPHandler)
215 playerXPHandler.GetOnPlayerXPChanged().Remove(OnPlayerXPChanged);
216
217 IEntity playerCharacter = m_PlayerController.GetMainEntity();
218 if (!playerCharacter)
219 return;
220
222 if (!rankComp)
223 return;
224
225 rankComp.s_OnRankChanged.Remove(OnRankChanged);
226 }
227
228 //------------------------------------------------------------------------------------------------
232 protected void OnCharacterPossessed(IEntity character)
233 {
234 if (m_PlayerController.GetMainEntity() != character)
235 return;
236
237 if (SCR_Enum.HasPartialFlag(GetEventMask(), EntityEvent.FRAME))
238 return;
239
240 if (!m_bIsEnabled)
241 return;
242
244 m_fAvailableAllocatedSuppliesReplenishmentTimer = m_MilitarySupplyAllocationConfig.GetAvailableAllocatedSuppliesReplenishmentTimer();
245
247 if (!rankComp)
248 return;
249
250 rankComp.s_OnRankChanged.Insert(OnRankChanged);
251 }
252
253 //------------------------------------------------------------------------------------------------
256 protected void OnPlayerConnected(int playerId)
257 {
258 if (m_PlayerController.GetPlayerId() != playerId)
259 return;
260
262 if (!playerXPHandler)
263 return;
264
265 playerXPHandler.GetOnPlayerXPChanged().Insert(OnPlayerXPChanged);
266 }
267
268 //------------------------------------------------------------------------------------------------
274 protected void OnPlayerXPChanged(int playerId, int currentXP, int XPToAdd, SCR_EXPRewards rewardId)
275 {
276 if (m_PlayerController.GetPlayerId() != playerId)
277 return;
278
279 // Player receiving XP after reconnect, set Military Supply Allocation to his rank
280 if (rewardId == SCR_EXPRewards.UNDEFINED)
281 {
283 if (!playerXPHandler)
284 return;
285
287 }
288
289 // Replenish Available Allocated Supplies on Supply delivery
290 if (rewardId == SCR_EXPRewards.SUPPLIES_DELIVERED)
291 AddPlayerAvailableAllocatedSupplies(XPToAdd * m_MilitarySupplyAllocationConfig.GetAvailableAllocatedSuppliesReplenishmentOnSupplyDeliveryMultiplier());
292 }
293
294 //------------------------------------------------------------------------------------------------
295 protected void OnPlayerKilled(SCR_InstigatorContextData investigatorContextData)
296 {
297 const IEntity victim = investigatorContextData.GetVictimEntity();
298 if (m_PlayerController.GetMainEntity() == victim)
299 OnPlayerLost();
300 }
301
302 //------------------------------------------------------------------------------------------------
303 protected void OnPlayerDeleted(int playerId, IEntity player)
304 {
305 if (m_PlayerController.GetMainEntity() == player)
306 OnPlayerLost();
307 }
308
309 //------------------------------------------------------------------------------------------------
312 protected void OnPlayerLost()
313 {
314 if (!m_bIsEnabled)
315 return;
316
317 const IEntity player = m_PlayerController.GetMainEntity();
318 if (player)
319 {
321 if (rankComp)
322 rankComp.s_OnRankChanged.Remove(OnRankChanged);
323 }
324
327 AddPlayerAvailableAllocatedSupplies(m_iPlayerMilitarySupplyAllocation);
328 }
329
330 //------------------------------------------------------------------------------------------------
336 protected void OnRankChanged(SCR_ECharacterRank prevRank, SCR_ECharacterRank newRank, IEntity owner, bool silent)
337 {
338 if (m_PlayerController.GetMainEntity() != owner || prevRank == newRank)
339 return;
340
342 }
343
344 //------------------------------------------------------------------------------------------------
347 protected void SetSupplyAllocationValuesByRank(SCR_ECharacterRank playerRank = SCR_ECharacterRank.INVALID)
348 {
349 if (!m_bIsEnabled)
350 return;
351
352 if (playerRank == SCR_ECharacterRank.INVALID)
353 {
355 if (!playerXPHandlerComponent)
356 return;
357
358 SCR_FactionManager factionManager = SCR_FactionManager.Cast(GetGame().GetFactionManager());
359 if (!factionManager)
360 return;
361
362 SCR_RankContainer ranks = factionManager.GetFactionRanks(m_PlayerController.GetPlayerId());
363 playerRank = ranks.GetRankByXP(playerXPHandlerComponent.GetPlayerXP());
364 }
365
366 SetPlayerMilitarySupplyAllocation(m_MilitarySupplyAllocationConfig.GetMilitarySupplyAllocationValueAtRank(playerRank));
367 }
368
369 //------------------------------------------------------------------------------------------------
376 protected void OnArsenalRequestItem(notnull SCR_ResourceComponent resourceComponent, ResourceName resourceName, IEntity requesterEntity, notnull BaseInventoryStorageComponent inventoryStorageComponent, EResourceType resourceType, int itemCost)
377 {
378 if (!m_bIsEnabled || itemCost == 0 || resourceType != EResourceType.SUPPLIES)
379 return;
380
381 SCR_PlayerController playerController = SCR_PlayerController.Cast(requesterEntity);
382 if (!playerController || playerController != m_PlayerController)
383 return;
384
385 SCR_EntityCatalogManagerComponent entityCatalogManager = SCR_EntityCatalogManagerComponent.GetInstance();
386 if (!entityCatalogManager)
387 return;
388
389 IEntity resourcesOwner = resourceComponent.GetOwner();
390 if (!resourcesOwner)
391 return;
392
393 SCR_ArsenalComponent arsenalComponent = SCR_ArsenalComponent.FindArsenalComponent(resourcesOwner);
394 if (!arsenalComponent || !arsenalComponent.IsArsenalUsingSupplies())
395 return;
396
397 SCR_ChimeraCharacter buyer = SCR_ChimeraCharacter.Cast(playerController.GetControlledEntity());
398 if (!buyer)
399 return;
400
401 if (arsenalComponent.GetAssignedFaction() != buyer.GetFaction())
402 return;
403
404 SCR_ResourceConsumer consumer = resourceComponent.GetConsumer(EResourceGeneratorID.DEFAULT, EResourceType.SUPPLIES);
405 if (!consumer)
406 return;
407
408 SCR_ArsenalManagerComponent arsenalManager;
409 if (!SCR_ArsenalManagerComponent.GetArsenalManager(arsenalManager))
410 return;
411
412 int msarCost = arsenalManager.GetItemMilitarySupplyAllocationCost(resourceName, arsenalComponent, true);
413
414 AddPlayerAvailableAllocatedSupplies(-1 * msarCost * consumer.GetBuyMultiplier());
415 }
416
417 //------------------------------------------------------------------------------------------------
419 protected bool IsProxy()
420 {
421 RplComponent rpl = RplComponent.Cast(GetOwner().FindComponent(RplComponent));
422
423 return rpl && rpl.IsProxy();
424 }
425
426 //------------------------------------------------------------------------------------------------
428 {
429 if (m_bIsEnabled == enable)
430 return;
431
432 m_bIsEnabled = enable;
433 OnEnabled(enable);
434 }
435
436 //------------------------------------------------------------------------------------------------
439 protected void OnEnabled(bool enable)
440 {
441 IEntity controlledEntity = m_PlayerController.GetControlledEntity();
442 if (enable)
443 {
445
447 if (gameMode)
448 {
449 gameMode.GetOnPlayerConnected().Insert(OnPlayerConnected);
450 gameMode.GetOnPlayerKilled().Insert(OnPlayerKilled);
451 gameMode.GetOnPlayerDeleted().Insert(OnPlayerDeleted);
453 }
454
456
457 m_PlayerController.m_OnPossessed.Insert(OnCharacterPossessed);
458 if (!controlledEntity)
459 return;
460
462 if (!rankComp)
463 return;
464
465 rankComp.s_OnRankChanged.Insert(OnRankChanged);
466
468 m_fAvailableAllocatedSuppliesReplenishmentTimer = m_MilitarySupplyAllocationConfig.GetAvailableAllocatedSuppliesReplenishmentTimer();
469 }
470 else
471 {
474
476 if (gameMode)
477 {
478 gameMode.GetOnPlayerConnected().Remove(OnPlayerConnected);
479 gameMode.GetOnPlayerKilled().Remove(OnPlayerKilled);
480 gameMode.GetOnPlayerDeleted().Remove(OnPlayerDeleted);
482 }
483
485 m_PlayerController.m_OnPossessed.Remove(OnCharacterPossessed);
486
487 if (!controlledEntity)
488 return;
489
491 if (!rankComp)
492 return;
493
494 rankComp.s_OnRankChanged.Remove(OnRankChanged);
495 }
496 }
497
498 //------------------------------------------------------------------------------------------------
499 protected override void EOnFrame(IEntity owner, float timeSlice)
500 {
504 }
505
506 //------------------------------------------------------------------------------------------------
507 protected override void OnDelete(IEntity owner)
508 {
510 if (gameMode)
511 {
512 gameMode.GetOnPlayerConnected().Remove(OnPlayerConnected);
513 gameMode.GetOnPlayerKilled().Remove(OnPlayerKilled);
514 gameMode.GetOnPlayerDeleted().Remove(OnPlayerDeleted);
516 }
517
519 {
521 if (playerXPHandler)
522 {
523 playerXPHandler.GetOnPlayerXPChanged().Remove(OnPlayerXPChanged);
524 }
525
526 m_PlayerController.m_OnPossessed.Remove(OnCharacterPossessed);
527 }
528
530
531 SCR_ArsenalManagerComponent arsenalManagerComponent;
532 SCR_ArsenalManagerComponent.GetArsenalManager(arsenalManagerComponent);
533
534 if (arsenalManagerComponent)
535 arsenalManagerComponent.GetOnMilitarySupplyAllocationToggle().Remove(OnMilitarySupplyAllocationEnabledChanged);
536
537 #ifdef ENABLE_DIAG
539 #endif
540 }
541
542 #ifdef ENABLE_DIAG
543 //------------------------------------------------------------------------------------------------
544 override void EOnDiag(IEntity owner, float timeSlice)
545 {
546 if (!DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_CAMPAIGN_MILITARY_SUPPLY_ALLOCATION_DEBUG))
547 return;
548
549 if (!m_bIsEnabled)
550 {
551 DbgUI.Text("Military Supply Allocation is disabled.");
552 return;
553 }
554
555 DbgUI.Begin("SCR_PlayerSupplyAllocationComponent");
556 DbgUI.Text(string.Format("Player ID: %1", m_PlayerController.GetPlayerId()));
557 DbgUI.Text(string.Format("Available allocated supplies: %1 / %2", GetPlayerAvailableAllocatedSupplies(), GetPlayerMilitarySupplyAllocation()));
558
559 DbgUI.Text(string.Format("Replenishment timer: %1s", Math.Round(GetAvailableAllocatedSuppliesReplenishmentTimer())));
560 DbgUI.Spacer(8);
561 DbgUI.End();
562 }
563 #endif
564}
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition DebugMenuID.c:4
ArmaReforgerScripted GetGame()
Definition game.c:1398
ResourceName resourceName
Definition SCR_AIGroup.c:66
SCR_BaseGameMode GetGameMode()
SCR_CacheNoteComponentClass ScriptComponentClass RplProp()] protected ref array< string > m_aLines
void OnPlayerKilled(notnull SCR_InstigatorContextData instigatorContextData)
void SCR_CharacterRankComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
void OnRankChanged(SCR_ECharacterRank prevRank, SCR_ECharacterRank newRank, bool silent)
void SCR_FactionManager(IEntitySource src, IEntity parent)
SCR_FastTravelComponentClass m_PlayerController
int GetAvailableAllocatedSuppliesReplenishmentTimer()
void ReplenishAvailableAllocatedSupplies()
If player's Available Allocated Supplies are below the threshold value it gets replenished to thresho...
void OnEnabled(bool enable)
ref ScriptInvokerInt m_MilitarySupplyAllocationChanged
void SetPlayerMilitarySupplyAllocation(int amount)
int GetPlayerAvailableAllocatedSupplies()
void OnPlayerDeleted(int playerId, IEntity player)
int m_iPlayerAvailableAllocatedSupplies
bool HasPlayerEnoughAvailableAllocatedSupplies(int amount)
void AvailableAllocatedSuppliesReplicated()
ScriptInvokerInt GetOnMilitarySupplyAllocationChanged()
int GetPlayerMilitarySupplyAllocation()
void OnArsenalRequestItem(notnull SCR_ResourceComponent resourceComponent, ResourceName resourceName, IEntity requesterEntity, notnull BaseInventoryStorageComponent inventoryStorageComponent, EResourceType resourceType, int itemCost)
ref SCR_MilitarySupplyAllocationConfig m_MilitarySupplyAllocationConfig
void MilitarySupplyAllocationReplicated()
void OnCharacterPossessed(IEntity character)
void OnMilitarySupplyAllocationEnabledChanged(bool enable)
void SetSupplyAllocationValuesByRank(SCR_ECharacterRank playerRank=SCR_ECharacterRank.INVALID)
void SetSupplyAllocationOnReconnect(int amount)
ScriptInvokerInt GetOnAvailableAllocatedSuppliesChanged()
ref ScriptInvokerInt m_AvailableAllocatedSuppliesChanged
float m_fAvailableAllocatedSuppliesReplenishmentTimer
void AddPlayerAvailableAllocatedSupplies(int amount)
ScriptInvokerBase< OnPlayerXPChangedMethod > OnPlayerXPChanged
EResourceGeneratorID
ScriptInvokerBase< ScriptInvokerIntMethod > ScriptInvokerInt
enum EVehicleType IEntity
Diagnostic and developer menu system.
Definition DiagMenu.c:18
void DisconnectFromDiagSystem(IEntity owner)
proto external int ClearEventMask(notnull IEntity owner, int mask)
proto external int SetEventMask(notnull IEntity owner, int mask)
proto external GenericComponent FindComponent(typename typeName)
void ConnectToDiagSystem(IEntity owner)
proto external Managed FindComponent(typename typeName)
Definition Math.c:13
Main replication API.
Definition Replication.c:14
static SCR_ArsenalComponent FindArsenalComponent(notnull IEntity entity, bool getFromSlotted=true)
ScriptInvokerBase< SCR_BaseGameMode_OnPlayerDisconnected > GetOnPlayerDisconnected()
ScriptInvokerBase< SCR_BaseGameMode_PlayerIdAndEntity > GetOnPlayerDeleted()
ScriptInvokerBase< SCR_BaseGameMode_PlayerId > GetOnPlayerConnected()
ScriptInvokerBase< SCR_BaseGameMode_OnControllableDestroyed > GetOnPlayerKilled()
SCR_ECharacterRank GetRankByXP(int XP)
void EOnFrame(IEntity owner, float timeSlice)
void EOnDiag(IEntity owner, float timeSlice)
proto external GenericEntity GetOwner()
Get owner entity.
EntityEvent
Various entity events.
Definition EntityEvent.c:14
override void OnPlayerDisconnected(int playerId, KickCauseCode cause, int timeout)
void OnPlayerConnected(int playerId)
RplCondition
Conditional replication rule. Fine grained selection of receivers.