private bool TryHandleDisconectOnSubsystem(ComponentEntityTuple <Subsystem, ItemStorage> subsystemTuple,
                                                   ComponentEntityTuple <Player, ItemStorage, CurrentLocation> playerTuple,
                                                   InventoryItemContainer inventoryItemContainer)
        {
            if (subsystemTuple.Component2.TryGetEmptyContainer(out var emptyContainer, out var containerIndex))
            {
                #region copied from DropItemCommandHandler
                // TODO: this should be replaced with an intent based approach, the command handler should abstract through the intent as well

                if (_itemMatcherGroup.TryGetMatchingEntity(inventoryItemContainer.Item.Value, out var itemTuple) &&
                    playerTuple.Component3.Value.HasValue &&
                    itemTuple.Component2.Value == playerTuple.Entity.Id)
                {
                    var inventory = playerTuple.Component2.Items[0] as InventoryItemContainer;
                    var target    = subsystemTuple.Component2.Items[containerIndex];
                    if (inventory != null &&
                        inventory.Item == itemTuple.Entity.Id &&
                        target != null &&
                        target.CanCapture(itemTuple.Entity.Id))
                    {
                        target.Item = itemTuple.Entity.Id;
                        itemTuple.Component3.Value = subsystemTuple.Entity.Id;
                        inventory.Item             = null;
                        itemTuple.Component2.Value = null;
                        return(true);
                    }
                }
                #endregion
            }
            return(false);
        }
Beispiel #2
0
        public void OnActivating(ComponentEntityTuple <Engine.Systems.Activation.Components.Activation, TransferActivator, CurrentLocation, Owner, TimedActivation> entityTuple, int currentTick)
        {
            if (entityTuple.Component3.Value.HasValue &&          // TODO: this should never fail so might be able to remove it
                _transferSystemMatcherGroup.TryGetMatchingEntity(entityTuple.Component3.Value.Value, out var localTransferSystemTuple) &&
                entityTuple.Component4.Value.HasValue)
            {
                var otherTransferSystemTuple = _transferSystemMatcherGroup.MatchingEntities.SingleOrDefault(t => t.Entity.Id != localTransferSystemTuple.Entity.Id);

                if (otherTransferSystemTuple != null &&
                    localTransferSystemTuple.Component3.TryGetItemContainer <TransferItemContainer>(out var localTransferItemContainer) &&
                    otherTransferSystemTuple.Component3.TryGetItemContainer <TransferItemContainer>(out var remoterTransferItemContainer) &&
                    otherTransferSystemTuple.Component3.TryGetItemContainer <TransferActivatorTargetItemContainer>(out var remoteTransferActivatorContainer) &&
                    remoteTransferActivatorContainer.Item.HasValue &&
                    _transferActivatorMatcherGroup.TryGetMatchingEntity(remoteTransferActivatorContainer.Item.Value, out var remoteTransferActivatorTuple))
                {
                    // Make sure items haven't recently been locked by another player.
                    if (localTransferItemContainer.Locked == false)
                    {
                        localTransferItemContainer.Locked   = true;
                        remoterTransferItemContainer.Locked = true;
                        remoteTransferActivatorTuple.Component1.SetState(ActivationState.Active, currentTick);
                        // TODO: find a better way of doing this
                        remoteTransferActivatorTuple.Component5.Synchronized             = true;
                        remoteTransferActivatorTuple.Component5.ActivationTicksRemaining = remoteTransferActivatorTuple.Component5.ActivationDuration;
                    }
                }
                else
                {
                    entityTuple.Component1.SetState(ActivationState.NotActive, currentTick);
                }
            }
Beispiel #3
0
 private void OnNotActive(ComponentEntityTuple <Engine.Systems.Activation.Components.Activation, Analyser, CurrentLocation, Owner> entityTuple, int currentTick)
 {
     if (entityTuple.Component3.Value.HasValue &&
         entityTuple.Component4.Value.HasValue)
     {
         entityTuple.Component4.Value = null;
     }
 }
Beispiel #4
0
 public void OnNotActive(ComponentEntityTuple <Engine.Systems.Activation.Components.Activation, TransferActivator, CurrentLocation, Owner, TimedActivation> entityTuple, int currentTick)
 {
     if (entityTuple.Component3.Value.HasValue &&
         entityTuple.Component4.Value.HasValue)
     {
         entityTuple.Component4.Value = null;
     }
 }
Beispiel #5
0
 private void OnActivating(ComponentEntityTuple <Engine.Systems.Activation.Components.Activation, GarbageDisposalActivator, CurrentLocation, Owner> entityTuple, int currentTick)
 {
     if (entityTuple.Component3.Value.HasValue &&
         _subsystemMatcherGroup.TryGetMatchingEntity(entityTuple.Component3.Value.Value, out var locationTuple) &&
         locationTuple.Component2.TryGetItemContainer <GarbageDisposalTargetItemContainer>(out var targetItemContainer) &&
         targetItemContainer.Item.HasValue)
     {
         targetItemContainer.Locked = true;
     }
 private void OnActive(ComponentEntityTuple <Engine.Systems.Activation.Components.Activation, TimedActivation, CurrentLocation> entityTuple)
 {
     if (entityTuple.Component3.Value.HasValue &&
         _subsystemMatcher.TryGetMatchingEntity(entityTuple.Component3.Value.Value, out var subsystemTuple))
     {
         var activationTickModifier = (1 - (subsystemTuple.Component2.Value / (decimal)subsystemTuple.Component2.Maximum) * CPURatioScalingFactor);
         entityTuple.Component2.ActivationTickModifier = RangeHelper.AssignWithinBounds(activationTickModifier, 0, Int32.MaxValue);
     }
 }
Beispiel #7
0
 private void OnActivating(ComponentEntityTuple <Engine.Systems.Activation.Components.Activation, Analyser, CurrentLocation, Owner> entityTuple, int currentTick)
 {
     if (entityTuple.Component3.Value.HasValue &&
         _subsystemMatcherGroup.TryGetMatchingEntity(entityTuple.Component3.Value.Value, out var locationTuple) &&
         locationTuple.Component2.TryGetItemContainer <AnalysisTargetItemContainer>(out var analysisTargetItemContainer) &&
         analysisTargetItemContainer.Item.HasValue &&
         _captureMatcherGroup.TryGetMatchingEntity(analysisTargetItemContainer.Item.Value, out var captureTuple) &&
         captureTuple.Component1.CapturedGenome != 0)
     {
         analysisTargetItemContainer.Locked = true;
     }
Beispiel #8
0
        private void OnDeactivating(ComponentEntityTuple <Activation, Components.Antivirus, CurrentLocation, Owner, ConsumableActivation> entityTuple)
        {
            if (entityTuple.Component3.Value.HasValue &&
                _visitorsMatcherGroup.TryGetMatchingEntity(entityTuple.Component3.Value.Value, out var locationTuple))
            {
                var malwareVisitors = locationTuple.Component1.Values
                                      .Join(_malwareMatcherGroup.MatchingEntities,
                                            k => k,
                                            k => k.Entity.Id,
                                            (o, i) => i)
                                      .ToArray();

                var @event = new AntivirusActivationEvent()
                {
                    PlayerEntityId    = entityTuple.Component4.Value.Value,
                    SubsystemEntityId = locationTuple.Entity.Id,
                    Uses = entityTuple.Component5.ActivationsRemaining - 1,
                };

                if (malwareVisitors.Length == 0)
                {
                    @event.ActivationResult = AntivirusActivationEvent.AntivirusActivationResult.NoVirusPresent;
                }

                // join the current locations list of visitors with all malware entities
                foreach (var malwareVisitor in malwareVisitors)
                {
                    if ((malwareVisitor.Component1.Value & entityTuple.Component2.TargetGenome) == malwareVisitor.Component1.Value)
                    {
                        @event.GenomeEradicated = malwareVisitor.Component1.Value;
                        @event.ActivationResult = AntivirusActivationEvent.AntivirusActivationResult.SoloExtermination;
                        malwareVisitor.Entity.Dispose();
                    }
                    else
                    {
                        @event.ActivationResult = AntivirusActivationEvent.AntivirusActivationResult.IncorrectGenome;
                    }
                }

                @event.MalwareCount = new System.Collections.Generic.Dictionary <int, int>();
                var infections = _malwareMatcherGroup.MatchingEntities.Join(_subsystemMatcherGroup.MatchingEntities,
                                                                            mw => mw.Component3.Value,
                                                                            ss => ss.Entity.Id,
                                                                            (mw, ss) => mw)
                                 .ToArray();

                @event.MalwareCount.Add(SimulationConstants.MalwareGeneRed, infections.Count(i => i.Component1.Value == SimulationConstants.MalwareGeneRed || i.Component1.Value == (SimulationConstants.MalwareGeneRed | SimulationConstants.MalwareGeneGreen) || i.Component1.Value == (SimulationConstants.MalwareGeneRed | SimulationConstants.MalwareGeneBlue) || i.Component1.Value == (SimulationConstants.MalwareGeneRed | SimulationConstants.MalwareGeneGreen | SimulationConstants.MalwareGeneBlue)));
                @event.MalwareCount.Add(SimulationConstants.MalwareGeneGreen, infections.Count(i => i.Component1.Value == SimulationConstants.MalwareGeneGreen || i.Component1.Value == (SimulationConstants.MalwareGeneRed | SimulationConstants.MalwareGeneGreen) || i.Component1.Value == (SimulationConstants.MalwareGeneGreen | SimulationConstants.MalwareGeneBlue) || i.Component1.Value == (SimulationConstants.MalwareGeneRed | SimulationConstants.MalwareGeneGreen | SimulationConstants.MalwareGeneBlue)));
                @event.MalwareCount.Add(SimulationConstants.MalwareGeneBlue, infections.Count(i => i.Component1.Value == SimulationConstants.MalwareGeneBlue || i.Component1.Value == (SimulationConstants.MalwareGeneRed | SimulationConstants.MalwareGeneBlue) || i.Component1.Value == (SimulationConstants.MalwareGeneGreen | SimulationConstants.MalwareGeneBlue) || i.Component1.Value == (SimulationConstants.MalwareGeneRed | SimulationConstants.MalwareGeneGreen | SimulationConstants.MalwareGeneBlue)));

                _eventSystem.Publish(@event);
            }
        }
Beispiel #9
0
 private void OnNotActive(ComponentEntityTuple <Activation, Components.Antivirus, CurrentLocation, Owner, ConsumableActivation> entityTuple, int currentTick)
 {
     if (entityTuple.Component3.Value.HasValue &&
         entityTuple.Component4.Value.HasValue)
     {
         entityTuple.Component4.Value = null;
         entityTuple.Component5.ActivationsRemaining -= 1;
         if (entityTuple.Component5.ActivationsRemaining == 0)
         {
             entityTuple.Entity.Dispose();
         }
     }
 }
Beispiel #10
0
        public void OnNewEntity(ComponentEntityTuple <AntivirusEnhancement, ItemStorage> tuple)
        {
            var itemStorage = tuple.Component2;

            itemStorage.Items[AnalysisTargetStorageLocation] = new AnalysisTargetItemContainer(_captureMatcherGroup);

            if (_entityFactoryProvider.TryCreateItem(AnalyserActivator.Archetype, tuple.Entity.Id, null, out var activatorEntityTuple) == false)
            {
                throw new SimulationException($"{AnalyserActivator.Archetype} archetype not registered");
            }
            itemStorage.Items[AnalysisActivatorStorageLocation] = new AnalysisActivatorItemContainer()
            {
                Item = activatorEntityTuple.Entity.Id,
            };
            itemStorage.Items[AnalysisOutputStorageLocation] = new AnalysisOutputItemContainer(_outputMatcherGroup);
        }
Beispiel #11
0
        public void OnNewEntity(ComponentEntityTuple <TransferEnhancement, ItemStorage> tuple)
        {
            var itemStorage = tuple.Component2;

            itemStorage.Items[TransferStorageLocation] = new TransferItemContainer();

            ComponentEntityTuple <CurrentLocation, Owner> activatorEntityTuple;

            if (_entityFactoryProvider.TryCreateItem(Archetypes.TransferActivator.Archetype.Name, tuple.Entity.Id, null, out activatorEntityTuple) == false)
            {
                throw new SimulationException($"{Archetypes.TransferActivator.Archetype.Name} archetype not registered");
            }
            itemStorage.Items[TransferActivatorStorageLocation] = new TransferActivatorTargetItemContainer()
            {
                Item = activatorEntityTuple.Entity.Id,
            };
        }
Beispiel #12
0
        public void OnNewEntity(ComponentEntityTuple <GarbageDisposalEnhancement, ItemStorage> tuple)
        {
            var itemStorage = tuple.Component2;

            itemStorage.Items[GarbageDisposalTargetStorageLocation] = new GarbageDisposalTargetItemContainer();

            if (_entityFactoryProvider.TryCreateItem(GarbageDisposalActivatorArchetypeName, tuple.Entity.Id, null, out var activatorEntityTuple))
            {
                itemStorage.Items[GarbageDisposalActivatorStorageLocation] = new GarbageDisposalActivatorItemContainer()
                {
                    Item = activatorEntityTuple.Entity.Id,
                };
            }
            else
            {
                throw new SimulationException($"{GarbageDisposalActivatorArchetypeName} archetype not registered");
            }
        }
Beispiel #13
0
 public static bool TryCreateItem(this IEntityFactoryProvider entityFactoryProvider,
                                  string archetype,
                                  int?currentLocationId,
                                  int?ownerId,
                                  out ComponentEntityTuple <CurrentLocation, Owner> entityTuple)
 {
     if (entityFactoryProvider.TryCreateEntityFromArchetype(archetype, out var item) &&
         item.TryGetComponent <CurrentLocation>(out var currentLocation) &&
         item.TryGetComponent <Owner>(out var owner))
     {
         currentLocation.Value = currentLocationId;
         owner.Value           = ownerId;
         entityTuple           = new ComponentEntityTuple <CurrentLocation, Owner>(item, currentLocation, owner);
         return(true);
     }
     item?.Dispose();
     entityTuple = null;
     return(false);
 }
Beispiel #14
0
        private void Propogate(ComponentEntityTuple <MalwareGenome, CurrentLocation, MalwarePropogation, MalwareVisibility> malwareTuple, ComponentEntityTuple <Subsystem, GraphNode> subsystemTuple)
        {
            if (_rngSystem.Next(0, 99) > malwareTuple.Component3.RollThreshold)
            {
                var exitNodes = subsystemTuple.Component2.ExitPositions.Keys.ToArray();

                // TODO: currently the direction to spread in is picked randomly by the PRNG - maybe have a more deterministic solution
                var propogateTo = exitNodes[_rngSystem.Next(0, exitNodes.Length)];

                if (_entityFactoryProvider.TryCreateEntityFromArchetype(malwareTuple.Entity.CreatedFromArchetype, out var entity) &&
                    _malwareMatcher.TryGetMatchingEntity(entity.Id, out var newMalwareTuple))
                {
                    newMalwareTuple.Component1.Value     = malwareTuple.Component1.Value;
                    newMalwareTuple.Component4.VisibleTo = malwareTuple.Component4.VisibleTo;

                    _movementSystem.AddVisitor(propogateTo, newMalwareTuple.Entity.Id);
                }
            }
        }
Beispiel #15
0
        private void OnDeactivating(ComponentEntityTuple <Activation, Scanner, CurrentLocation, Owner> entityTuple)
        {
            if (entityTuple.Component3.Value.HasValue &&
                _visitorsMatcherGroup.TryGetMatchingEntity(entityTuple.Component3.Value.Value, out var locationTuple) &&
                _playerMatcherGroup.TryGetMatchingEntity(entityTuple.Component4.Value.Value, out var playerTuple))
            {
                var malwareVisitors = locationTuple.Component2.Values
                                      .Join(_malwareMatcherGroup.MatchingEntities,
                                            k => k,
                                            k => k.Entity.Id,
                                            (o, i) => i)
                                      .ToArray();

                // join the current locations list of visitors with all malware entities
                foreach (var malwareVisitor in malwareVisitors)
                {
                    var @event = new ScannerActivationEvent {
                        PlayerEntityId    = entityTuple.Component4.Value.Value,
                        SubsystemEntityId = locationTuple.Entity.Id,
                        ActivationResult  = (malwareVisitor.Component2.VisibleTo == 0 || (malwareVisitor.Component2.VisibleTo & playerTuple.Component2.Value) == 0)
                                                        ? ScannerActivationEvent.ScannerActivationResult.VirusRevealed
                                                        : ScannerActivationEvent.ScannerActivationResult.VirusAlreadyVisisble,
                    };
                    _eventSystem.Publish(@event);

                    // add the visible gene
                    malwareVisitor.Component2.VisibleTo |= VisibleToAll
                                                ? MalwareVisibility.All
                                                : playerTuple.Component2.Value;
                }

                if (malwareVisitors.Length == 0)
                {
                    var @event = new ScannerActivationEvent
                    {
                        PlayerEntityId    = entityTuple.Component4.Value.Value,
                        SubsystemEntityId = locationTuple.Entity.Id,
                        ActivationResult  = ScannerActivationEvent.ScannerActivationResult.NoVirusPresent,
                    };
                    _eventSystem.Publish(@event);
                }
            }
        }
 private void OnNotActive(ComponentEntityTuple <Engine.Systems.Activation.Components.Activation, TimedActivation, CurrentLocation> entityTuple)
 {
     entityTuple.Component2.ActivationTickModifier = SimulationConstants.TimedActivationTickModifier;
 }
Beispiel #17
0
        private void CreateItem(string itemType, EntityConfig node, SimulationLifecycleManager lifecycleManager, out ComponentEntityTuple <IItemType, CurrentLocation> item, out int itemContainerIndex, out ItemContainer itemContainer)
        {
            // store current match
            var itemLocationMatcher        = lifecycleManager.ECSRoot.ECS.MatcherProvider.CreateMatcherGroup <IItemType, CurrentLocation>();
            var priorMatchignEntitiesTotal = itemLocationMatcher.MatchingEntities.Length;

            // Add Items to swap
            var createItem = new CreateItem(TutorialScanner.Archetype.Name,
                                            node);

            createItem.Execute(lifecycleManager.ECSRoot.ECS,
                               lifecycleManager.ECSRoot.Configuration);

            // Tick so commands get processed
            lifecycleManager.ECSRoot.ECS.Tick();

            item = itemLocationMatcher.MatchingEntities[priorMatchignEntitiesTotal];

            // Get the subsystem the item's current location is set to
            var systemWithStorageMatcher = lifecycleManager.ECSRoot.ECS.MatcherProvider.CreateMatcherGroup <Subsystem, ItemStorage>();

            systemWithStorageMatcher.TryGetMatchingEntity(item.Component2.Value.Value,
                                                          out var subsystemTuple);

            var itemLocal = item;

            itemContainerIndex = Array.FindIndex(subsystemTuple.Component2.Items,
                                                 ic => ic.Item == itemLocal.Entity.Id);

            itemContainer = subsystemTuple.Component2.Items[itemContainerIndex];
        }
Beispiel #18
0
        private void CreatePlayer(string name, EntityConfig node, SimulationLifecycleManager lifecycleManager, out ComponentEntityTuple <ITAlert.Simulation.Components.EntityTypes.Player> player)
        {
            var playerMatcher             = lifecycleManager.ECSRoot.ECS.MatcherProvider.CreateMatcherGroup <ITAlert.Simulation.Components.EntityTypes.Player>();
            var priorMatchingEntitesTotal = playerMatcher.MatchingEntities.Length;

            // Create a NPC to initiate the swap action
            var createNpc = new CreatePlayer(Player.Archetype,
                                             node,
                                             name);

            createNpc.Execute(lifecycleManager.ECSRoot.ECS,
                              lifecycleManager.ECSRoot.Configuration);

            // Tick so commands get processed
            lifecycleManager.ECSRoot.ECS.Tick();

            player = playerMatcher.MatchingEntities[priorMatchingEntitesTotal];
        }
Beispiel #19
0
        private bool GetSubsystemItemStorage(EntityConfig node, SimulationLifecycleManager lifecycleManager, out ComponentEntityTuple <Subsystem, ItemStorage> subsystemTuple)
        {
            var systemWithStorageMatcher = lifecycleManager.ECSRoot.ECS.MatcherProvider.CreateMatcherGroup <Subsystem, ItemStorage>();

            return(systemWithStorageMatcher.TryGetMatchingEntity(node.EntityId, out subsystemTuple));
        }
        private void OnDeactivating(ComponentEntityTuple <Engine.Systems.Activation.Components.Activation, Components.Antivirus, CurrentLocation, Owner> itemTuple, int currentTick)
        {
            if (itemTuple.Component3.Value.HasValue &&
                _locationMatcherGroup.TryGetMatchingEntity(itemTuple.Component3.Value.Value, out var locationTuple))
            {
                var activeAntivirus = locationTuple.Component2.Items.Where(ic => ic?.Item != null && ic.Item.Value != itemTuple.Entity.Id)
                                      .Join(_antivirusMatcherGroup.MatchingEntities.Where(av => av.Component1.ActivationState == ActivationState.Active &&
                                                                                          (AllowSamePlayerActivation || av.Component4.Value != itemTuple.Component4.Value)),
                                            k => k.Item.Value,
                                            k => k.Entity.Id,
                                            (o, i) => i)
                                      .ToArray();

                var otherPlayerIds = activeAntivirus.Where(av => av.Component4.Value.HasValue &&
                                                           av.Component4.Value != itemTuple.Component4.Value)
                                     .Select(av => av.Component4.Value)
                                     .Distinct()
                                     .ToArray();

                var combinedGenome = itemTuple.Component2.TargetGenome | activeAntivirus.Aggregate(0, (g, av) => g |= av.Component2.TargetGenome, g => g);
                if (combinedGenome != 0 && combinedGenome != itemTuple.Component2.TargetGenome)
                {
                    // join the current locations list of visitors with all malware entities
                    foreach (var malwareVisitor in locationTuple.Component3.Values
                             .Join(_malwareMatcherGroup.MatchingEntities,
                                   k => k,
                                   k => k.Entity.Id,
                                   (o, i) => i).ToArray())
                    {
                        if ((malwareVisitor.Component1.Value & combinedGenome) == malwareVisitor.Component1.Value)
                        {
                            foreach (var antivirus in activeAntivirus)
                            {
                                antivirus.Component1.SetState(ActivationState.NotActive, currentTick);
                            }

                            var malwareType = malwareVisitor.Component1.Value;
                            malwareVisitor.Entity.Dispose();

                            var @event = new AntivirusActivationEvent()
                            {
                                PlayerEntityId    = itemTuple.Component4.Value.Value,
                                ActivationResult  = AntivirusActivationEvent.AntivirusActivationResult.CoopExtermination,
                                SubsystemEntityId = locationTuple.Entity.Id,
                                GenomeEradicated  = malwareType,
                            };

                            var infectionCount = new System.Collections.Generic.Dictionary <int, int>();
                            var infections     = _malwareMatcherGroup.MatchingEntities.Join(_subsystemMatcherGroup.MatchingEntities,
                                                                                            mw => mw.Component3.Value,
                                                                                            ss => ss.Entity.Id,
                                                                                            (mw, ss) => mw)
                                                 .ToArray();

                            infectionCount.Add(SimulationConstants.MalwareGeneRed, infections.Count(i => i.Component1.Value == SimulationConstants.MalwareGeneRed || i.Component1.Value == (SimulationConstants.MalwareGeneRed | SimulationConstants.MalwareGeneGreen) || i.Component1.Value == (SimulationConstants.MalwareGeneRed | SimulationConstants.MalwareGeneBlue) || i.Component1.Value == (SimulationConstants.MalwareGeneRed | SimulationConstants.MalwareGeneGreen | SimulationConstants.MalwareGeneBlue)));
                            infectionCount.Add(SimulationConstants.MalwareGeneGreen, infections.Count(i => i.Component1.Value == SimulationConstants.MalwareGeneGreen || i.Component1.Value == (SimulationConstants.MalwareGeneRed | SimulationConstants.MalwareGeneGreen) || i.Component1.Value == (SimulationConstants.MalwareGeneGreen | SimulationConstants.MalwareGeneBlue) || i.Component1.Value == (SimulationConstants.MalwareGeneRed | SimulationConstants.MalwareGeneGreen | SimulationConstants.MalwareGeneBlue)));
                            infectionCount.Add(SimulationConstants.MalwareGeneBlue, infections.Count(i => i.Component1.Value == SimulationConstants.MalwareGeneBlue || i.Component1.Value == (SimulationConstants.MalwareGeneRed | SimulationConstants.MalwareGeneBlue) || i.Component1.Value == (SimulationConstants.MalwareGeneGreen | SimulationConstants.MalwareGeneBlue) || i.Component1.Value == (SimulationConstants.MalwareGeneRed | SimulationConstants.MalwareGeneGreen | SimulationConstants.MalwareGeneBlue)));
                            @event.MalwareCount = infectionCount;

                            _eventSystem.Publish(@event);

                            foreach (var otherPlayer in otherPlayerIds)
                            {
                                @event = new AntivirusActivationEvent()
                                {
                                    PlayerEntityId    = otherPlayer.Value,
                                    ActivationResult  = AntivirusActivationEvent.AntivirusActivationResult.CoopExtermination,
                                    SubsystemEntityId = locationTuple.Entity.Id,
                                    GenomeEradicated  = malwareType,
                                    MalwareCount      = infectionCount
                                };
                                _eventSystem.Publish(@event);
                            }
                        }
                    }
                }
            }
        }
        private void GetPlayerTuple(int playerId, SimulationLifecycleManager lifecycleManager, out ComponentEntityTuple <ITAlert.Simulation.Components.EntityTypes.Player, ItemStorage, CurrentLocation> playerTuple)
        {
            var itemLocationMatcher = lifecycleManager.ECSRoot.ECS.MatcherProvider.CreateMatcherGroup <ITAlert.Simulation.Components.EntityTypes.Player, ItemStorage, CurrentLocation>();

            itemLocationMatcher.TryGetMatchingEntity(playerId, out playerTuple);
        }
        private void AssignToPlayer(int playerId, int itemId, SimulationLifecycleManager lifecycleManager, out ComponentEntityTuple <ITAlert.Simulation.Components.EntityTypes.Player, ItemStorage, CurrentLocation> playerTuple)
        {
            var swapCommand = new PickupItemCommand
            {
                PlayerId = playerId,
                ItemId   = itemId
            };

            lifecycleManager.ECSRoot.ECS.EnqueueCommand(swapCommand);

            lifecycleManager.ECSRoot.ECS.Tick();

            GetPlayerTuple(playerId, lifecycleManager, out playerTuple);

            Assert.NotNull(playerTuple.Component2.Items[0].Item);
            Assert.AreEqual(playerTuple.Component2.Items[0].Item.Value, itemId);
        }
Beispiel #23
0
        protected override bool TryHandleCommand(SwapInventoryItemAndActivateCommand command, int currentTick, bool handlerEnabled)
        {
            ComponentEntityTuple <Item, Owner, CurrentLocation, IItemType> subsystemItemTuple = null;
            ComponentEntityTuple <Item, Owner, CurrentLocation, IItemType> inventoryItemTuple = null;

            if (_playerMatcherGroup.TryGetMatchingEntity(command.PlayerId,
                                                         out var playerTuple) &&
                (!command.SubsystemItemId.HasValue ||
                 _itemMatcherGroup.TryGetMatchingEntity(command.SubsystemItemId.Value,
                                                        out subsystemItemTuple)) &&
                _itemMatcherGroup.TryGetMatchingEntity(command.InventoryItemId,
                                                       out inventoryItemTuple)
                // Player must be on a subsystem
                && playerTuple.Component3.Value.HasValue &&
                _subsystemMatcherGroup.TryGetMatchingEntity(playerTuple.Component3.Value.Value,
                                                            out var subsystemTuple)
                // Must be same subsystem as when command was issued
                && subsystemTuple.Entity.Id == command.SubsystemId)
            {
                // No one must own the subsystem item, but the player must own the inventory item
                if (subsystemItemTuple?.Component2.Value == null && (inventoryItemTuple?.Component2.Value == playerTuple.Entity.Id || inventoryItemTuple?.Component2.Value == null))
                {
                    var inventory          = playerTuple.Component2.Items[0] as InventoryItemContainer;
                    var subsystemContainer = subsystemTuple.Component2.Items[command.ContainerId];

                    // Items must still be in same locations as when the command was issued
                    if (subsystemContainer.Item == command.SubsystemItemId &&
                        inventory.Item == command.InventoryItemId
                        // Must be enabled
                        && subsystemContainer.Enabled &&
                        inventory.Enabled
                        // Must be able to release items
                        && (subsystemContainer.CanRelease || subsystemContainer.Item == null) &&
                        (inventory.CanRelease || inventory.Item == null)
                        // Containers must be able to accept items
                        && (!command.SubsystemItemId.HasValue || inventory.CanContain(command.SubsystemItemId.Value)))
                    {
                        inventory.Item = command.SubsystemItemId;
                        if (inventoryItemTuple != null)
                        {
                            inventoryItemTuple.Component3.Value = subsystemTuple.Entity.Id;
                            inventoryItemTuple.Component2.Value = null;
                        }

                        subsystemContainer.Item = command.InventoryItemId;
                        if (subsystemItemTuple != null)
                        {
                            subsystemItemTuple.Component2.Value = playerTuple.Entity.Id;
                            subsystemItemTuple.Component3.Value = null;
                        }

                        if (_activationMatcherGroup.TryGetMatchingEntity(command.InventoryItemId, out var itemTuple))
                        {
                            var @event = new SwapInventoryItemAndActivateEvent()
                            {
                                InventoryItemId   = itemTuple.Entity.Id,
                                InventoryItemType = itemTuple.Component5.GetType().Name,
                                SubsystemItemId   = subsystemItemTuple.Entity.Id,
                                SubsystemItemType = subsystemItemTuple.Component4.GetType().Name,
                                PlayerEntityId    = command.PlayerId,
                                SubsystemEntityId = itemTuple.Component3.Value ?? -1
                            };

                            var itemNotActive     = itemTuple.Component2.ActivationState == ActivationState.NotActive;                                                                                           // item is not active
                            var playerCanActivate = (itemTuple.Component4.AllowAll || itemTuple.Component4.Value == null || itemTuple.Component4.Value == command.PlayerId);                                     // player can activate item
                            // TODO: should an item have to have a location to be activated?
                            var playerHasActive     = _activationMatcherGroup.MatchingEntities.Any(it => it.Component4.Value == command.PlayerId && it.Component2.ActivationState != ActivationState.NotActive); // player has no other active items
                            var subsystemCanContain = subsystemContainer.CanContain(command.InventoryItemId);
                            var target = subsystemTuple.Component2.Items[command.ContainerId];

                            @event.TargetContainerType = target.GetType().Name;

                            if (handlerEnabled &&
                                itemNotActive &&
                                playerCanActivate &&
                                subsystemCanContain &&
                                playerHasActive == false &&
                                itemTuple.Component3.Value.HasValue &&                                 // item is on a subsystem
                                _subsystemMatcherGroup.TryGetMatchingEntity(itemTuple.Component3.Value.Value, out var _))
                            {
                                itemTuple.Component2.SetState(ActivationState.Activating,
                                                              currentTick);
                                itemTuple.Component4.Value = command.PlayerId;

                                @event.Result = SwapInventoryItemAndActivateEvent.ActivationResult.Success;
                                _eventSystem.Publish(@event);
                                return(true);
                            }

                            inventory.Item = command.InventoryItemId;
                            if (inventoryItemTuple != null)
                            {
                                inventoryItemTuple.Component2.Value = playerTuple.Entity.Id;
                                inventoryItemTuple.Component3.Value = null;
                            }

                            subsystemContainer.Item = command.SubsystemItemId;
                            if (subsystemItemTuple != null)
                            {
                                subsystemItemTuple.Component3.Value = subsystemTuple.Entity.Id;
                                subsystemItemTuple.Component2.Value = null;
                            }

                            @event.Result = handlerEnabled
                                                                ? itemNotActive
                                                                        ? playerCanActivate
                                                                                ? subsystemCanContain
                                                                                        ? playerHasActive
                                                                                                ? SwapInventoryItemAndActivateEvent.ActivationResult.Failure_PlayerHasActive
                                                                                                : SwapInventoryItemAndActivateEvent.ActivationResult.Error
                                                                                        : SwapInventoryItemAndActivateEvent.ActivationResult.Failure_DestinationCannotCapture
                                                                                : SwapInventoryItemAndActivateEvent.ActivationResult.Failure_PlayerCannotActivate
                                                                        : SwapInventoryItemAndActivateEvent.ActivationResult.Failure_ItemAlreadyActive
                                                                : SwapInventoryItemAndActivateEvent.ActivationResult.Failure_CommandDisabled;
                            _eventSystem.Publish(@event);
                        }
                        return(false);
                    }
                }
            }

            return(false);
        }
Beispiel #24
0
        public void OnDeactivating(ComponentEntityTuple <Engine.Systems.Activation.Components.Activation, Capture, CurrentLocation, Owner> entityTuple)
        {
            if (entityTuple.Component3.Value.HasValue &&
                _subsystemMatcherGroup.TryGetMatchingEntity(entityTuple.Component3.Value.Value, out var locationTuple) &&
                _playerMatcherGroup.TryGetMatchingEntity(entityTuple.Component4.Value.Value, out var playerTuple))
            {
                // join the current locations list of visitors with all malware entities
                var malwareVisitor = locationTuple.Component2.Values
                                     .Join(_malwareMatcherGroup.MatchingEntities,
                                           k => k,
                                           k => k.Entity.Id,
                                           (o, i) => i)
                                     .FirstOrDefault();

                var @event = new CaptureActivationEvent()
                {
                    PlayerEntityId    = playerTuple.Entity.Id,
                    SubsystemEntityId = locationTuple.Entity.Id,
                };

                // TODO: probably need a better way of choosing the malware than selecting first, but this will do for now
                if (malwareVisitor != null &&
                    entityTuple.Component4.Value.HasValue &&
                    (malwareVisitor.Component2.VisibleTo & playerTuple.Component2.Value) == playerTuple.Component2.Value)
                {
                    // cyclical behaviour for capture on advanced genomes
                    int gene     = entityTuple.Component2.CapturedGenome;
                    int captured = 0;
                    do
                    {
                        switch (gene)
                        {
                        case 0:
                            gene = SimulationConstants.MalwareGeneRed;
                            break;

                        case SimulationConstants.MalwareGeneRed:
                            gene = SimulationConstants.MalwareGeneGreen;
                            break;

                        case SimulationConstants.MalwareGeneGreen:
                            gene = SimulationConstants.MalwareGeneBlue;
                            break;

                        case SimulationConstants.MalwareGeneBlue:
                            gene = SimulationConstants.MalwareGeneRed;
                            break;
                        }

                        if (HasGene(malwareVisitor.Component1.Value, gene))
                        {
                            captured = gene;
                        }
                    } while (HasGene(malwareVisitor.Component1.Value, gene) == false);

                    var genes = new []
                    {
                        SimulationConstants.MalwareGeneRed,
                        SimulationConstants.MalwareGeneGreen,
                        SimulationConstants.MalwareGeneBlue
                    }
                    .Count(g => HasGene(malwareVisitor.Component1.Value, g));

                    if (entityTuple.Component2.CapturedGenome != captured)
                    {
                        entityTuple.Component2.CapturedGenome = captured;
                        @event.ActivationResult = genes > 1
                                                        ? CaptureActivationEvent.CaptureActivationResult.ComplexGenomeCaptured
                                                        : CaptureActivationEvent.CaptureActivationResult.SimpleGenomeCaptured;
                    }
                    else
                    {
                        @event.ActivationResult = CaptureActivationEvent.CaptureActivationResult.GenomeAlreadyCaptured;
                    }
                }
                else
                {
                    @event.ActivationResult = CaptureActivationEvent.CaptureActivationResult.NoVirusPresent;
                }

                _eventSystem.Publish(@event);
            }
        }
        protected override bool TryHandleCommand(SwapInventoryItemCommand command, int currentTick, bool handlerEnabled)
        {
            ComponentEntityTuple <Item, Owner, CurrentLocation, IItemType> subsystemItemTuple = null;
            ComponentEntityTuple <Item, Owner, CurrentLocation, IItemType> inventoryItemTuple = null;

            if (_playerMatcherGroup.TryGetMatchingEntity(command.PlayerId,
                                                         out var playerTuple) &&
                (!command.SubsystemItemId.HasValue ||
                 _itemMatcherGroup.TryGetMatchingEntity(command.SubsystemItemId.Value,
                                                        out subsystemItemTuple)) &&
                (!command.InventoryItemId.HasValue ||
                 _itemMatcherGroup.TryGetMatchingEntity(command.InventoryItemId.Value,
                                                        out inventoryItemTuple))
                // Player must be on a subsystem
                && playerTuple.Component3.Value.HasValue &&
                _subsystemMatcherGroup.TryGetMatchingEntity(playerTuple.Component3.Value.Value,
                                                            out var subsystemTuple)
                // Must be same subsystem as when command was issued
                && subsystemTuple.Entity.Id == command.SubsystemId)
            {
                var @event = new SwapInventoryItemCommandEvent()
                {
                    InventoryItemId   = inventoryItemTuple.Entity.Id,
                    InventoryItemType = inventoryItemTuple.Component4.GetType().Name,
                    SubsystemItemId   = subsystemItemTuple.Entity.Id,
                    SubsystemItemType = subsystemItemTuple.Component4.GetType().Name,
                    PlayerEntityId    = command.PlayerId,
                    SubsystemEntityId = subsystemItemTuple.Component3.Value ?? -1
                };

                var target = subsystemTuple.Component2.Items[command.ContainerId];

                @event.TargetContainerType = target.GetType().Name;

                // No one must own the subsystem item, but the player must own the inventory item
                if (subsystemItemTuple?.Component2.Value == null)
                {
                    if (inventoryItemTuple?.Component2.Value == playerTuple.Entity.Id || inventoryItemTuple?.Component2.Value == null)
                    {
                        var inventory          = playerTuple.Component2.Items[0] as InventoryItemContainer;
                        var subsystemContainer = subsystemTuple.Component2.Items[command.ContainerId];

                        // Items must still be in same locations as when the command was issued
                        if (handlerEnabled &&
                            subsystemContainer.Item == command.SubsystemItemId &&
                            inventory.Item == command.InventoryItemId
                            // Must be enabled
                            && subsystemContainer.Enabled &&
                            inventory.Enabled
                            // Must be able to release items
                            && (subsystemContainer.CanRelease || subsystemContainer.Item == null) &&
                            (inventory.CanRelease || inventory.Item == null)
                            // Containers must be able to accept items
                            && (!command.SubsystemItemId.HasValue || inventory.CanContain(command.SubsystemItemId.Value)) &&
                            (!command.InventoryItemId.HasValue || subsystemContainer.CanContain(command.InventoryItemId.Value)))
                        {
                            inventory.Item = command.SubsystemItemId;
                            if (inventoryItemTuple != null)
                            {
                                inventoryItemTuple.Component3.Value = subsystemTuple.Entity.Id;
                                inventoryItemTuple.Component2.Value = null;
                            }

                            subsystemContainer.Item = command.InventoryItemId;
                            if (subsystemItemTuple != null)
                            {
                                subsystemItemTuple.Component2.Value = playerTuple.Entity.Id;
                                subsystemItemTuple.Component3.Value = null;
                            }

                            @event.Result = SwapInventoryItemCommandEvent.ActivationResult.Success;
                            _eventSystem.Publish(@event);
                            return(true);
                        }
                        else
                        {
                            @event.Result = handlerEnabled
                                                                                ? !command.InventoryItemId.HasValue || subsystemContainer.CanContain(command.InventoryItemId.Value)
                                                                        ? SwapInventoryItemCommandEvent.ActivationResult.Error
                                                                : SwapInventoryItemCommandEvent.ActivationResult.Failure_DestinationCannotCapture
                                                        : SwapInventoryItemCommandEvent.ActivationResult.Failure_CommandDisabled;
                            _eventSystem.Publish(@event);
                        }
                    }
                    else
                    {
                        @event.Result = SwapInventoryItemCommandEvent.ActivationResult.Error;
                        _eventSystem.Publish(@event);
                    }
                }
                else
                {
                    @event.Result = SwapInventoryItemCommandEvent.ActivationResult.Failure_ItemAlreadyActive;
                    _eventSystem.Publish(@event);
                }
            }

            return(false);
        }
 public void OnDeactivating(ComponentEntityTuple <Activation, Owner> entityTuple)
 {
     entityTuple.Component2.Value = null;
 }
Beispiel #27
0
        protected override bool TryHandleCommand(SwapSubsystemItemCommand command, int currentTick, bool handlerEnabled)
        {
            ComponentEntityTuple <Item, Owner, CurrentLocation, IItemType> toItemTuple = null;

            if (_playerMatcherGroup.TryGetMatchingEntity(command.PlayerId,
                                                         out var playerTuple) &&
                _itemMatcherGroup.TryGetMatchingEntity(command.FromItemId,
                                                       out var fromItemTuple) &&
                (!command.ToItemId.HasValue ||
                 _itemMatcherGroup.TryGetMatchingEntity(command.ToItemId.Value,
                                                        out toItemTuple))
                // Player must be on a subsystem
                && playerTuple.Component3.Value.HasValue &&
                _subsystemMatcherGroup.TryGetMatchingEntity(playerTuple.Component3.Value.Value,
                                                            out var subsystemTuple)
                // Must be same subsystem as when command was issued
                && subsystemTuple.Entity.Id == command.SubsystemId)
            {
                var @event = new SwapSubsystemItemCommandEvent()
                {
                    FromItemId        = fromItemTuple.Entity.Id,
                    FromItemType      = fromItemTuple.Component4.GetType().Name,
                    ToItemId          = toItemTuple?.Entity.Id ?? -1,
                    ToItemType        = toItemTuple?.Component4.GetType().Name,
                    PlayerEntityId    = command.PlayerId,
                    SubsystemEntityId = fromItemTuple.Component3.Value ?? -1
                };

                // No one must own either item
                if (handlerEnabled && fromItemTuple.Component2.Value == null && toItemTuple?.Component2.Value == null)
                {
                    var toContainer   = subsystemTuple.Component2.Items[command.ToContainerIndex];
                    var fromContainer = subsystemTuple.Component2.Items[command.FromContainerIndex];

                    @event.FromContainerType = fromContainer.GetType().Name;
                    @event.ToContainerType   = toContainer.GetType().Name;

                    // Items must still be in same locations as when the command was issued
                    if (fromContainer.Item == command.FromItemId &&
                        toContainer.Item == command.ToItemId
                        // Must be enabled
                        && fromContainer.Enabled &&
                        toContainer.Enabled
                        // Must be able to release items
                        && (fromContainer.CanRelease || fromContainer.Item == null) &&
                        (toContainer.CanRelease || toContainer.Item == null)
                        // Containers must be able to accept items
                        && toContainer.CanContain(command.FromItemId) &&
                        (!command.ToItemId.HasValue || fromContainer.CanContain(command.ToItemId.Value)))
                    {
                        toContainer.Item   = command.FromItemId;
                        fromContainer.Item = command.ToItemId;

                        @event.Result = SwapSubsystemItemCommandEvent.ActivationResult.Success;
                        _eventSystem.Publish(@event);
                        return(true);
                    }
                    else
                    {
                        if (toContainer.CanContain(command.FromItemId) &&
                            (!command.ToItemId.HasValue || fromContainer.CanContain(command.ToItemId.Value)))
                        {
                            @event.Result = SwapSubsystemItemCommandEvent.ActivationResult.Error;
                            _eventSystem.Publish(@event);
                        }
                        else
                        {
                            @event.Result = SwapSubsystemItemCommandEvent.ActivationResult.Failure_DestinationCannotCapture;
                            _eventSystem.Publish(@event);
                        }
                    }
                }
                else
                {
                    if (handlerEnabled)
                    {
                        @event.Result = SwapSubsystemItemCommandEvent.ActivationResult.Failure_ItemAlreadyActive;
                        _eventSystem.Publish(@event);
                    }
                    else
                    {
                        @event.Result = SwapSubsystemItemCommandEvent.ActivationResult.Failure_CommandDisabled;
                        _eventSystem.Publish(@event);
                    }
                }
            }

            return(false);
        }
Beispiel #28
0
 private void ItemStorage_EntityAdded(ComponentEntityTuple <ItemStorage> tuple)
 {
     InitializeItemContainers(tuple.Component1);
 }