示例#1
0
        public static List <object> SerializeAttributes(GameEntity itemEntity, SenseType sense, SerializationContext context)
        {
            var canIdentify = itemEntity != null && sense.CanIdentify();

            if (!canIdentify)
            {
                return(new List <object>());
            }

            var manager        = itemEntity.Manager;
            var item           = itemEntity.Item;
            var template       = Item.Loader.Get(item.TemplateName);
            var physical       = itemEntity.Physical;
            var equipableSlots = manager.ItemUsageSystem.GetEquipableSlots(item, context.Observer.Physical.Size)
                                 .GetNonRedundantFlags(removeComposites: true)
                                 .Select(s => Serialize(s, context))
                                 .ToList();

            return(new List <object>(13)
            {
                context.Services.Language.GetString(item, item.GetQuantity(manager), sense),
                context.Services.Language.GetDescription(item.TemplateName, DescriptionCategory.Item),
                item.Type,
                (int)physical.Material,
                physical.Size,
                physical.Weight,
                item.Hindrance,
                template.Complexity,
                template.RequiredMight,
                template.RequiredSpeed,
                template.RequiredFocus,
                template.RequiredPerception,
                equipableSlots,
                manager.AbilitiesToAffectableRelationship[itemEntity.Id]
                .Where(a => a.Ability.IsUsable &&
                       a.Ability.Activation != ActivationType.Default &&
                       a.Ability.Activation != ActivationType.Always &&
                       a.Ability.Activation != ActivationType.WhilePossessed)
                .Select(a => AbilitySnapshot.SerializeAttributes(a, context.Observer, context)).ToList()
            });
        }
示例#2
0
        public static List <object> Serialize(
            GameEntity playerEntity,
            EntityState state,
            PlayerSnapshot snapshot,
            List <object> serializedLevel,
            SerializationContext context)
        {
            var           manager = context.Manager;
            var           player  = playerEntity.Player;
            var           being   = playerEntity.Being;
            List <object> properties;

            if (state == EntityState.Added)
            {
                properties = new List <object>(14)
                {
                    (int)state
                };

                properties.Add(player.EntityId);
                properties.Add(player.ProperName);
                properties.Add(player.Game.CurrentTick);
                properties.Add(serializedLevel);
                properties.Add(manager.RacesToBeingRelationship[playerEntity.Id].Values
                               .Select(r => RaceSnapshot.Serialize(r, null, context)).ToList());
                properties.Add(manager.EntityItemsToContainerRelationship[playerEntity.Id]
                               .Select(t => InventoryItemSnapshot.Serialize(t, null, null, context)).ToList());
                properties.Add(GetSlottedAbilities(playerEntity, manager)
                               .Select(a => AbilitySnapshot.Serialize(a, null, context)).ToList());
                // TODO: Group log entries for the same tick
                // TODO: Only send entries since last player turn
                properties.Add(GetLogEntries(player)
                               .Select(e => LogEntrySnapshot.Serialize(e, null, context)).ToList());
                properties.Add(player.NextActionTick);
                properties.Add(player.NextLevelXP);
                properties.Add(being.ExperiencePoints);
                properties.Add(being.HitPoints);
                properties.Add(being.HitPointMaximum);
                properties.Add(being.EnergyPoints);
                properties.Add(being.EnergyPointMaximum);
                properties.Add(being.ReservedEnergyPoints);
                return(properties);
            }

            properties = new List <object>(3)
            {
                (int)state,
                snapshot.SnapshotTick,
                player.Game.CurrentTick
            };
            var playerEntry = context.DbContext.Entry(player);

            var i = 2;

            if (serializedLevel != null)
            {
                properties.Add(i);
                properties.Add(serializedLevel);
            }

            i++;
            var serializedRaces = GameTransmissionProtocol.Serialize(
                manager.RacesToBeingRelationship[playerEntity.Id].Values,
                snapshot.RacesSnapshot,
                RaceSnapshot.Serialize,
                context);

            if (serializedRaces.Count > 0)
            {
                properties.Add(i);
                properties.Add(serializedRaces);
            }

            i++;
            var serializedItems = GameTransmissionProtocol.Serialize(
                manager.EntityItemsToContainerRelationship[playerEntity.Id],
                snapshot.ItemsSnapshot,
                InventoryItemSnapshot.Serialize,
                context);

            if (serializedItems.Count > 0)
            {
                properties.Add(i);
                properties.Add(serializedItems);
            }

            i++;
            var serializedAbilities = GameTransmissionProtocol.Serialize(
                GetSlottedAbilities(playerEntity, manager),
                snapshot.AbilitiesSnapshot,
                AbilitySnapshot.Serialize,
                context);

            if (serializedAbilities.Count > 0)
            {
                properties.Add(i);
                properties.Add(serializedAbilities);
            }

            i++;
            var serializedLog = GameTransmissionProtocol.Serialize(
                GetLogEntries(player),
                snapshot.LogEntriesSnapshot,
                LogEntrySnapshot.Serialize,
                context);

            if (serializedLog.Count > 0)
            {
                properties.Add(i);
                properties.Add(serializedLog);
            }

            i++;
            var nextActionTick = playerEntry.Property(nameof(PlayerComponent.NextActionTick));

            if (nextActionTick.IsModified)
            {
                properties.Add(i);
                properties.Add(player.NextActionTick);
            }

            i++;
            var nextLevelXP = playerEntry.Property(nameof(PlayerComponent.NextLevelXP));

            if (nextLevelXP.IsModified)
            {
                properties.Add(i);
                properties.Add(player.NextLevelXP);
            }

            var beingEntry = context.DbContext.Entry(being);

            if (beingEntry.State != EntityState.Unchanged)
            {
                i++;
                var xp = beingEntry.Property(nameof(BeingComponent.ExperiencePoints));
                if (xp.IsModified)
                {
                    properties.Add(i);
                    properties.Add(being.ExperiencePoints);
                }

                i++;
                var hitPoints = beingEntry.Property(nameof(BeingComponent.HitPoints));
                if (hitPoints.IsModified)
                {
                    properties.Add(i);
                    properties.Add(being.HitPoints);
                }

                i++;
                var hitPointMaximum = beingEntry.Property(nameof(BeingComponent.HitPointMaximum));
                if (hitPointMaximum.IsModified)
                {
                    properties.Add(i);
                    properties.Add(being.HitPointMaximum);
                }

                i++;
                var energyPoints = beingEntry.Property(nameof(BeingComponent.EnergyPoints));
                if (energyPoints.IsModified)
                {
                    properties.Add(i);
                    properties.Add(being.EnergyPoints);
                }

                i++;
                var energyPointMaximum = beingEntry.Property(nameof(BeingComponent.EnergyPointMaximum));
                if (energyPointMaximum.IsModified)
                {
                    properties.Add(i);
                    properties.Add(being.EnergyPointMaximum);
                }

                i++;
                var reservedEnergyPoints = beingEntry.Property(nameof(BeingComponent.ReservedEnergyPoints));
                if (reservedEnergyPoints.IsModified)
                {
                    properties.Add(i);
                    properties.Add(being.ReservedEnergyPoints);
                }
            }

            return(properties);
        }
示例#3
0
        public List <object> QueryGame(string playerName, int intQueryType, int[] arguments)
        {
            var queryType = (GameQueryType)intQueryType;
            var result    = new List <object> {
                intQueryType
            };

            var player = FindPlayer(playerName);

            if (player?.Entity.Being.IsAlive != true)
            {
                return(result);
            }

            var manager = player.Entity.Manager;
            var context = new SerializationContext(_dbContext, player.Entity, _gameServices);

            switch (queryType)
            {
            case GameQueryType.SlottableAbilities:
                var slot = arguments[0];
                result.Add(slot);
                var abilities = new List <object>();
                result.Add(abilities);

                foreach (var slottableAbilityEntity in manager.AbilitiesToAffectableRelationship[player.EntityId])
                {
                    var ability = slottableAbilityEntity.Ability;
                    if (!ability.IsUsable)
                    {
                        continue;
                    }

                    if (slot == AbilitySlottingSystem.DefaultMeleeAttackSlot)
                    {
                        if (ability.Template?.Type == AbilityType.DefaultAttack &&
                            ((WieldingAbility)ability.Template).ItemType == ItemType.WeaponMelee)
                        {
                            abilities.Add(AbilitySnapshot.Serialize(slottableAbilityEntity, null, context));
                        }
                    }
                    else if (slot == AbilitySlottingSystem.DefaultRangedAttackSlot)
                    {
                        if (ability.Template?.Type == AbilityType.DefaultAttack &&
                            ((WieldingAbility)ability.Template).ItemType == ItemType.WeaponRanged)
                        {
                            abilities.Add(AbilitySnapshot.Serialize(slottableAbilityEntity, null, context));
                        }
                    }
                    else if ((ability.Activation & ActivationType.Slottable) != 0 &&
                             ability.Template?.Type != AbilityType.DefaultAttack)
                    {
                        abilities.Add(AbilitySnapshot.Serialize(slottableAbilityEntity, null, context));
                    }
                }

                break;

            case GameQueryType.PlayerAttributes:
                result.Add(LevelActorSnapshot.SerializeAttributes(player.Entity, SenseType.Sight, context));
                break;

            case GameQueryType.PlayerAdaptations:
                result.Add(PlayerSnapshot.SerializeAdaptations(player.Entity, context));
                break;

            case GameQueryType.PlayerSkills:
                result.Add(PlayerSnapshot.SerializeSkills(player.Entity, context));
                break;

            case GameQueryType.ActorAttributes:
                var actorKnowledge = manager.FindEntity(arguments[0])?.Knowledge;

                result.Add(LevelActorSnapshot.SerializeAttributes(
                               actorKnowledge?.KnownEntity, actorKnowledge?.SensedType ?? SenseType.None, context));
                break;

            case GameQueryType.ItemAttributes:
                var itemEntity    = manager.FindEntity(arguments[0]);
                var item          = itemEntity?.Item;
                var itemKnowledge = itemEntity?.Knowledge;

                if (item != null)
                {
                    result.Add(InventoryItemSnapshot.SerializeAttributes(itemEntity, SenseType.Sight, context));
                }
                else
                {
                    result.Add(InventoryItemSnapshot.SerializeAttributes(
                                   itemKnowledge?.KnownEntity, itemKnowledge?.SensedType ?? SenseType.None, context));
                }

                break;

            case GameQueryType.AbilityAttributes:
                var abilityEntity = manager.FindEntity(arguments[0]);

                var ownerEntity     = abilityEntity.Ability.OwnerEntity;
                var activatorEntity = abilityEntity.Ability.OwnerEntity.HasComponent(EntityComponent.Item)
                        ? player.Entity
                        : ownerEntity;

                result.Add(AbilitySnapshot.SerializeAttributes(abilityEntity, activatorEntity, context));
                break;

            default:
                throw new InvalidOperationException($"Query type {intQueryType} not supported");
            }

            return(result);
        }
示例#4
0
        public static List <object> SerializeAttributes(GameEntity actorEntity, SenseType sense, SerializationContext context)
        {
            var canIdentify = actorEntity != null && sense.CanIdentify();

            if (!canIdentify)
            {
                return(new List <object>());
            }

            var being       = actorEntity.Being;
            var sensor      = actorEntity.Sensor;
            var physical    = actorEntity.Physical;
            var description = actorEntity.HasComponent(EntityComponent.Player)
                ? ""
                : context.Services.Language.GetDescription(
                actorEntity.Manager.RacesToBeingRelationship[actorEntity.Id].Values.First().Race.TemplateName,
                DescriptionCategory.Creature);
            var result = new List <object>(40)
            {
                context.Services.Language.GetActorName(actorEntity, sense),
                description,
                actorEntity.Position.MovementDelay,
                physical.Size,
                physical.Weight,
                sensor.PrimaryFOVQuadrants,
                sensor.PrimaryVisionRange,
                sensor.TotalFOVQuadrants,
                sensor.SecondaryVisionRange,
                sensor.Infravision,
                sensor.InvisibilityDetection,
                physical.Infravisible,
                being.Visibility,
                being.HitPoints,
                being.HitPointMaximum,
                being.EnergyPoints,
                being.EnergyPointMaximum,
                being.Might,
                being.Speed,
                being.Focus,
                being.Perception,
                being.Regeneration,
                being.EnergyRegeneration,
                being.Armor,
                being.Deflection,
                being.Evasion,
                being.PhysicalResistance,
                being.MagicResistance,
                being.BleedingResistance,
                being.AcidResistance,
                being.ColdResistance,
                being.ElectricityResistance,
                being.FireResistance,
                being.PsychicResistance,
                being.ToxinResistance,
                being.VoidResistance,
                being.SonicResistance,
                being.StunResistance,
                being.LightResistance,
                being.WaterResistance
            };

            if (!actorEntity.HasComponent(EntityComponent.Player))
            {
                result.Add(actorEntity.Manager.AbilitiesToAffectableRelationship[actorEntity.Id]
                           .Where(a => a.Ability.IsUsable &&
                                  a.Ability.Activation != ActivationType.Default &&
                                  a.Ability.Activation != ActivationType.Always &&
                                  a.Ability.Activation != ActivationType.OnMeleeAttack &&
                                  a.Ability.Activation != ActivationType.OnRangedAttack)  // Instead add the effects to the corresponding abilities
                           .Select(a => AbilitySnapshot.SerializeAttributes(a, actorEntity, context)).ToList());
            }

            return(result);
        }