14 class SCR_ResourcePlayerControllerInventoryComponentClass : ScriptComponentClass
28 if (!m_OnPlayerInteractionInvoker)
31 return m_OnPlayerInteractionInvoker;
35 protected SCR_ResourceActor TryGetGenerationActor(notnull SCR_ResourceComponent resourceComponent,
EResourceType resourceType, out
float currentResourceValue, out
float maxResourceValue)
41 currentResourceValue = containerFrom.GetResourceValue();
42 maxResourceValue = containerFrom.GetMaxResourceValue();
51 currentResourceValue = generator.GetAggregatedResourceValue();
52 maxResourceValue = generator.GetAggregatedMaxResourceValue();
61 currentResourceValue = generator.GetAggregatedResourceValue();
62 maxResourceValue = generator.GetAggregatedMaxResourceValue();
71 protected SCR_ResourceActor TryGetConsumptionActor(notnull SCR_ResourceComponent resourceComponent,
EResourceType resourceType, out
float currentResourceValue, out
float maxResourceValue)
77 currentResourceValue = containerFrom.GetResourceValue();
78 maxResourceValue = containerFrom.GetMaxResourceValue();
83 SCR_ResourceConsumer consumer = resourceComponent.GetConsumer(
EResourceGeneratorID.DEFAULT_STORAGE, resourceType);
87 currentResourceValue = consumer.GetAggregatedResourceValue();
88 maxResourceValue = consumer.GetAggregatedMaxResourceValue();
97 currentResourceValue = consumer.GetAggregatedResourceValue();
98 maxResourceValue = consumer.GetAggregatedMaxResourceValue();
107 protected bool TryPerformResourceConsumption(notnull
SCR_ResourceActor actor,
EResourceType resourceType,
float resourceValue,
bool ignoreOnEmptyBehavior =
false)
116 if (ignoreOnEmptyBehavior)
120 encapsulator.RequestConsumtion(resourceValue);
122 container.DecreaseResourceValue(resourceValue);
124 if (ignoreOnEmptyBehavior)
125 container.SetOnEmptyBehavior(emptyBehavior);
130 SCR_ResourceConsumer consumer = SCR_ResourceConsumer.Cast(actor);
134 consumer.RequestConsumtion(resourceValue);
152 encapsulator.RequestGeneration(resourceValue);
154 container.IncreaseResourceValue(resourceValue);
163 generator.RequestGeneration(resourceValue);
179 Rpc(RpcAsk_RequestSubscription, resourceComponentRplId, interactorType.ToString(), resourceType, resourceIdentifier);
190 Rpc(RpcAsk_RequestUnsubscription, resourceComponentRplId, interactorType.ToString(), resourceType, resourceIdentifier);
199 [
RplRpc(RplChannel.Reliable, RplRcver.Server)]
202 if (!resourceComponentRplId.IsValid())
205 SCR_ResourceComponent resourceComponent = SCR_ResourceComponent.Cast(Replication.FindItem(resourceComponentRplId));
207 if (!resourceComponent)
210 SCR_ResourceInteractor interactor;
212 if (interactorType ==
"SCR_ResourceGenerator")
213 interactor = resourceComponent.GetGenerator(resourceIdentifier, resourceType);
214 else if (interactorType ==
"SCR_ResourceConsumer")
215 interactor = resourceComponent.GetConsumer(resourceIdentifier, resourceType);
219 GetGame().GetResourceSystemSubscriptionManager().SubscribeListener(Replication.FindId(
this), interactor);
228 [
RplRpc(RplChannel.Reliable, RplRcver.Server)]
231 if (!resourceComponentRplId.IsValid())
234 SCR_ResourceComponent resourceComponent = SCR_ResourceComponent.Cast(Replication.FindItem(resourceComponentRplId));
236 if (!resourceComponent)
239 SCR_ResourceInteractor interactor;
241 if (interactorType ==
"SCR_ResourceGenerator")
242 interactor = resourceComponent.GetGenerator(resourceIdentifier, resourceType);
243 else if (interactorType ==
"SCR_ResourceConsumer")
244 interactor = resourceComponent.GetConsumer(resourceIdentifier, resourceType);
248 GetGame().GetResourceSystemSubscriptionManager().UnsubscribeListener(Replication.FindId(
this), interactor);
258 [
RplRpc(RplChannel.Reliable, RplRcver.Owner)]
261 if (!rplIdResourceComponentFrom.IsValid())
264 PlayerController playerController = PlayerController.Cast(
GetOwner());
266 if (!playerController)
269 SCR_ResourceComponent resourceComponentFrom = SCR_ResourceComponent.Cast(Replication.FindItem(rplIdResourceComponentFrom));
271 if (!resourceComponentFrom)
274 SCR_ResourceComponent resourceComponentTo = SCR_ResourceComponent.Cast(Replication.FindItem(rplIdResourceComponentTo));
276 OnPlayerInteraction(interactionType, resourceComponentFrom, resourceComponentTo, resourceType, resourceValue);
286 [
RplRpc(RplChannel.Reliable, RplRcver.Server)]
287 void RpcAsk_ArsenalRequestItem(RplId rplIdResourceComponent, RplId rplIdInventoryManager, RplId rplIdStorageComponent, ResourceName resourceNameItem,
EResourceType resourceType)
289 if (!rplIdInventoryManager.IsValid())
292 SCR_InventoryStorageManagerComponent inventoryManagerComponent = SCR_InventoryStorageManagerComponent.Cast(Replication.FindItem(rplIdInventoryManager));
294 if (!inventoryManagerComponent)
298 if (!rplIdStorageComponent.IsValid())
301 BaseInventoryStorageComponent storageComponent = BaseInventoryStorageComponent.Cast(Replication.FindItem(rplIdStorageComponent));
303 if (!storageComponent)
306 if (!rplIdResourceComponent.IsValid())
309 SCR_ResourceComponent resourceComponent = SCR_ResourceComponent.Cast(Replication.FindItem(rplIdResourceComponent));
310 if (!resourceComponent)
313 SCR_ResourceConsumer consumer = resourceComponent.GetConsumer(
EResourceGeneratorID.DEFAULT, resourceType);
317 float resourceCost = 0;
320 SCR_EntityCatalogManagerComponent entityCatalogManager = SCR_EntityCatalogManagerComponent.GetInstance();
321 if (entityCatalogManager)
323 IEntity resourcesOwner = resourceComponent.GetOwner();
329 if ((arsenalComponent && arsenalComponent.IsArsenalUsingSupplies()) || !arsenalComponent)
332 if (arsenalComponent)
333 faction = arsenalComponent.GetAssignedFaction();
338 entry = entityCatalogManager.GetEntryWithPrefabFromFactionCatalog(
EEntityCatalogType.ITEM, resourceNameItem, faction);
340 entry = entityCatalogManager.GetEntryWithPrefabFromCatalog(
EEntityCatalogType.ITEM, resourceNameItem);
347 if (arsenalComponent)
348 resourceCost =
data.GetSupplyCost(arsenalComponent.GetSupplyCostType());
350 resourceCost =
data.GetSupplyCost(SCR_EArsenalSupplyCostType.DEFAULT);
360 inventoryManagerComponent.TrySpawnPrefabToStorage(resourceNameItem, storageComponent);
368 [
RplRpc(RplChannel.Reliable, RplRcver.Server)]
369 void RpcAsk_ArsenalRefundItem(RplId rplIdResourceComponent, RplId rplIdInventoryItem,
EResourceType resourceType)
371 if (!rplIdInventoryItem.IsValid())
376 if (!inventoryItemComponent)
379 if (!rplIdResourceComponent.IsValid())
382 SCR_ResourceComponent resourceComponent = SCR_ResourceComponent.Cast(Replication.FindItem(rplIdResourceComponent));
383 if (!resourceComponent)
390 IEntity inventoryItemEntity = inventoryItemComponent.GetOwner();
391 if (!inventoryItemEntity)
394 float resourceCost = 0;
397 SCR_EntityCatalogManagerComponent entityCatalogManager = SCR_EntityCatalogManagerComponent.GetInstance();
398 if (entityCatalogManager)
400 IEntity resourcesOwner = resourceComponent.GetOwner();
406 if ((arsenalComponent && arsenalComponent.IsArsenalUsingSupplies()) || !arsenalComponent)
409 if (arsenalComponent)
410 faction = arsenalComponent.GetAssignedFaction();
412 ResourceName resourceNameItem = inventoryItemEntity.GetPrefabData().GetPrefabName();
421 if (arsenalComponent)
422 resourceCost =
data.GetSupplyCost(arsenalComponent.GetSupplyCostType());
424 resourceCost =
data.GetSupplyCost(SCR_EArsenalSupplyCostType.DEFAULT);
433 if (resourceCost > 0)
440 IEntity parentEntity = inventoryItemEntity.GetParent();
441 SCR_InventoryStorageManagerComponent inventoryManagerComponent;
444 inventoryManagerComponent = SCR_InventoryStorageManagerComponent.Cast(parentEntity.FindComponent(SCR_InventoryStorageManagerComponent));
446 if (inventoryManagerComponent && !inventoryManagerComponent.TryDeleteItem(inventoryItemEntity))
448 else if (!inventoryManagerComponent)
449 RplComponent.DeleteRplEntity(inventoryItemEntity,
false);
459 [
RplRpc(RplChannel.Reliable, RplRcver.Server)]
460 void RpcAsk_MergeContainerWithContainer(RplId rplIdFrom, RplId rplIdTo,
EResourceType resourceType)
462 if (!rplIdFrom.IsValid() || !rplIdTo.IsValid())
465 SCR_ResourceComponent componentFrom = SCR_ResourceComponent.Cast(Replication.FindItem(rplIdFrom));
470 SCR_ResourceComponent componentTo = SCR_ResourceComponent.Cast(Replication.FindItem(rplIdTo));
475 float resourceValueCurrentFrom, resourceValueMaxFrom;
476 SCR_ResourceActor actorFrom = TryGetConsumptionActor(componentFrom, resourceType, resourceValueCurrentFrom, resourceValueMaxFrom);
478 float resourceValueCurrentTo, resourceValueMaxTo;
479 SCR_ResourceActor actorTo = TryGetGenerationActor(componentTo, resourceType, resourceValueCurrentTo, resourceValueMaxTo);
481 float resourceUsed = Math.Min(resourceValueCurrentFrom, resourceValueMaxTo - resourceValueCurrentTo);
483 if(TryPerformResourceConsumption(actorFrom, resourceType, resourceUsed) && TryPerformResourceGeneration(actorTo, resourceType, resourceUsed))
493 [
RplRpc(RplChannel.Reliable, RplRcver.Server)]
494 void RpcAsk_MergeContainerWithContainerPartial(RplId rplIdFrom, RplId rplIdTo,
EResourceType resourceType,
float requestedResources)
496 if (!rplIdFrom.IsValid() || !rplIdTo.IsValid())
499 SCR_ResourceComponent componentFrom = SCR_ResourceComponent.Cast(Replication.FindItem(rplIdFrom));
504 SCR_ResourceComponent componentTo = SCR_ResourceComponent.Cast(Replication.FindItem(rplIdTo));
509 float resourceValueCurrentFrom, resourceValueMaxFrom;
510 SCR_ResourceActor actorFrom = TryGetConsumptionActor(componentFrom, resourceType, resourceValueCurrentFrom, resourceValueMaxFrom);
512 float resourceValueCurrentTo, resourceValueMaxTo;
513 SCR_ResourceActor actorTo = TryGetGenerationActor(componentTo, resourceType, resourceValueCurrentTo, resourceValueMaxTo);
515 float resourceUsed = Math.Min(Math.Min(resourceValueCurrentFrom, resourceValueMaxTo - resourceValueCurrentTo), requestedResources);
517 if(TryPerformResourceConsumption(actorFrom, resourceType, resourceUsed) && TryPerformResourceGeneration(actorTo, resourceType, resourceUsed))
528 [
RplRpc(RplChannel.Reliable, RplRcver.Server)]
529 void RpcAsk_CreatePhysicalContainerWithContainer(RplId rplIdResourceComponent, RplId rplIdInventoryManager, RplId rplIdStorageComponent,
EResourceType resourceType,
float requestedResources)
531 if (!rplIdResourceComponent.IsValid())
534 SCR_ResourceComponent resourceComponentFrom = SCR_ResourceComponent.Cast(Replication.FindItem(rplIdResourceComponent));
536 if (!resourceComponentFrom)
539 float resourceValueCurrentFrom, resourceValueMaxFrom;
540 SCR_ResourceActor actorFrom = TryGetConsumptionActor(resourceComponentFrom, resourceType, resourceValueCurrentFrom, resourceValueMaxFrom);
542 float resourceValueCurrentTo, resourceValueMaxTo;
545 if (!rplIdInventoryManager.IsValid())
548 array<SCR_EntityCatalogEntry> entries = {};
549 array<SCR_BaseEntityCatalogData>
data = {};
552 int selectedEntryIdx;
556 for (selectedEntryIdx =
data.Count() - 1; selectedEntryIdx >= 0; --selectedEntryIdx)
559 resourceUsed = datum.GetMaxResourceValue();
561 if (resourceUsed >= requestedResources)
565 if (selectedEntryIdx < 0)
566 selectedEntryIdx = 0;
568 resourceUsed = Math.Min(resourceUsed, requestedResources);
570 if (!TryPerformResourceConsumption(actorFrom, resourceType, resourceUsed))
573 RandomGenerator randGenerator =
new RandomGenerator();
574 vector center = resourceComponentFrom.GetOwner().GetOrigin();
575 vector
position = vector.Up * center[1] + randGenerator.GenerateRandomPointInRadius(0.0, 2.5, center);
576 TraceParam param =
new TraceParam();
577 param.Start =
position +
"0.0 10.0 0.0";
579 param.Flags = TraceFlags.WORLD | TraceFlags.ENTS;
580 param.LayerMask = EPhysicsLayerDefs.Projectile;
582 float traced =
GetGame().GetWorld().TraceMove(param,
null);
585 position = (param.End - param.Start) * traced + param.Start;
587 EntitySpawnParams spawnParams =
new EntitySpawnParams();
588 spawnParams.TransformMode = ETransformMode.WORLD;
589 spawnParams.Transform[3] =
position;
590 IEntity newStorageEntity =
GetGame().SpawnEntityPrefab(Resource.Load(entries[selectedEntryIdx].GetPrefab()),
GetGame().GetWorld(), spawnParams);
591 SCR_ResourceComponent resourceComponentTo = SCR_ResourceComponent.FindResourceComponent(newStorageEntity);
592 actorConsumptionTo = TryGetConsumptionActor(resourceComponentTo, resourceType, resourceValueCurrentTo, resourceValueMaxTo);
593 actorGenerationTo = TryGetGenerationActor(resourceComponentTo, resourceType, resourceValueCurrentTo, resourceValueMaxTo);
595 TryPerformResourceConsumption(actorConsumptionTo, resourceType, resourceValueCurrentTo,
true);
597 if (TryPerformResourceGeneration(actorGenerationTo, resourceType, resourceUsed))
604 delete newStorageEntity;
609 SCR_InventoryStorageManagerComponent inventoryManagerComponent = SCR_InventoryStorageManagerComponent.Cast(Replication.FindItem(rplIdInventoryManager));
611 if (!inventoryManagerComponent)
614 if (!rplIdStorageComponent.IsValid())
617 BaseInventoryStorageComponent storageComponent = BaseInventoryStorageComponent.Cast(Replication.FindItem(rplIdStorageComponent));
619 if (!storageComponent)
622 IEntity resourcesOwner = resourceComponentFrom.GetOwner();
624 int selectedEntryIdx = -1;
626 array<SCR_EntityCatalogEntry> entries = {};
627 array<SCR_BaseEntityCatalogData>
data = {};
628 float maxResourceValueTo = 0.0;
634 if (inventoryManagerComponent.CanInsertResourceInStorage(entry.GetPrefab(), storageComponent))
636 selectedEntry = entry;
637 selectedEntryIdx = idx;
647 float maxStoredResources = Math.Min(resourceValueCurrentFrom, datum.GetMaxResourceValue());
648 float resourceUsed = Math.Min(requestedResources, maxStoredResources);
650 if (!TryPerformResourceConsumption(actorFrom, resourceType, resourceUsed))
653 EntitySpawnParams spawnParams =
new EntitySpawnParams();
654 spawnParams.TransformMode = ETransformMode.WORLD;
656 inventoryManagerComponent.GetOwner().GetTransform(spawnParams.Transform);
658 IEntity newStorageEntity =
GetGame().SpawnEntityPrefab(Resource.Load(selectedEntry.GetPrefab()),
GetGame().GetWorld(), spawnParams);
660 if (!newStorageEntity)
663 SCR_ResourceComponent resourceComponentTo = SCR_ResourceComponent.FindResourceComponent(newStorageEntity);
664 actorConsumptionTo = TryGetConsumptionActor(resourceComponentTo, resourceType, resourceValueCurrentTo, resourceValueMaxTo);
665 actorGenerationTo = TryGetGenerationActor(resourceComponentTo, resourceType, resourceValueCurrentTo, resourceValueMaxTo);
667 TryPerformResourceConsumption(actorConsumptionTo, resourceType, resourceValueCurrentTo,
true);
669 if (!TryPerformResourceGeneration(actorGenerationTo, resourceType, resourceUsed))
671 delete newStorageEntity;
676 inventoryManagerComponent.TryInsertItemInStorage(newStorageEntity, storageComponent);
689 PlayerController playerController = PlayerController.Cast(owner);
691 if (!playerController)
694 RplComponent rplComponent = RplComponent.Cast(owner.FindComponent(RplComponent));
696 if (!playerController)
699 if (rplComponent && !rplComponent.IsProxy() && !rplComponent.IsOwner())
700 Rpc(RpcAsk_OnPlayerInteraction, interactionType, Replication.FindId(resourceComponentFrom), Replication.FindId(resourceComponentTo), resourceType, resourceValue);
702 GetOnPlayerInteraction().Invoke(interactionType, owner, resourceComponentFrom, resourceComponentTo, resourceType, resourceValue);
708 override event protected void OnDelete(IEntity owner)
710 super.OnDelete(owner);
712 GetGame().GetResourceSystemSubscriptionManager().UnsubscribeListenerCompletely(Replication.FindId(
this));