Ejemplo n.º 1
0
        private static void UpdateOrbit(ProtoEntity entity, PositionDB parentPositionDB, DateTime toDate)
        {
            var entityOrbitDB  = entity.GetDataBlob <OrbitDB>(OrbitTypeIndex);
            var entityPosition = entity.GetDataBlob <PositionDB>(PositionTypeIndex);

            //if(toDate.Minute > entityOrbitDB.OrbitalPeriod.TotalMinutes)

            // Get our Parent-Relative coordinates.
            try
            {
                Vector3 newPosition = GetPosition_AU(entityOrbitDB, toDate);

                // Get our Absolute coordinates.
                entityPosition.AbsolutePosition_AU = parentPositionDB.AbsolutePosition_AU + newPosition;
            }
            catch (OrbitProcessorException e)
            {
                //Do NOT fail to the UI. There is NO data-corruption on this exception.
                // In this event, we did NOT update our position.
                Event evt = new Event(StaticRefLib.CurrentDateTime, "Non Critical Position Exception thrown in OrbitProcessor for EntityItem " + entity.Guid + " " + e.Message);
                evt.EventType = EventType.Opps;
                StaticRefLib.EventLog.AddEvent(evt);
            }

            // Update our children.
            foreach (Entity child in entityOrbitDB.Children)
            {
                // RECURSION!
                UpdateOrbit(child, entityPosition, toDate);
            }
        }
Ejemplo n.º 2
0
        public Entity Clone(EntityManager manager)
        {
            ProtoEntity clone = Clone();

            clone.Guid = Guid.NewGuid();
            return(new Entity(manager, clone));
        }
Ejemplo n.º 3
0
        public static Entity ImportEntity([NotNull] Game game, [NotNull] Stream inputStream, [NotNull] EntityManager manager)
        {
            if (manager == null)
            {
                throw new ArgumentNullException(nameof(manager));
            }


            var protoEntity = new ProtoEntity();

            protoEntity = Import(game, inputStream, protoEntity);

            //the block of code below is a somewhat hacky way of fixing a problem where an entity has a datablob that refers back to the entity.
            //eg a faction entitiy with a NameDB. in such cases the Import above creates an empty entity because of the NameDB's reference.
            //then Entity.Create below checks for an exsisting entity guid, finds it, then returns an entity with a new GUID.
            //this then gives us two entities, one with the correct guid but with no datablobs, and a second one with a new different guid and all the datablobs.
            //checking if the datablob count is 0 is a poor and not guarenteed way of seeing if the entity hasn't been created some other way previously.
            //I'm wondering if we shouldn't throw an exception if we try to add an entity with the same guid, instead of just changing the guid.
            Entity entity;

            if (manager.FindEntityByGuid(protoEntity.Guid, out entity) && entity.DataBlobs.Count == 0)
            {
                manager.RemoveEntity(entity);
            }



            entity = Entity.Create(manager, protoEntity);
            game.PostGameLoad();
            return(entity);
        }
Ejemplo n.º 4
0
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                //bool exists1 = Testing.manager.EntityExistsGlobaly(Testing.entityID);
                var protoEntity = new ProtoEntity();

                //StarObject (Entity)
                reader.Read();                                            // PropertyName ID
                reader.Read();                                            // Actual ID
                protoEntity.Guid = serializer.Deserialize <Guid>(reader); // Deserialize the ID
                //bool exists2 = Testing.manager.EntityExistsGlobaly(Testing.entityID);
                // Deserialize the dataBlobs
                reader.Read(); // PropertyName DATABLOB
                while (reader.TokenType == JsonToken.PropertyName)
                {
                    var  typestring   = "Pulsar4X.ECSLib." + (string)reader.Value;
                    Type dataBlobType = Type.GetType(typestring);
                    reader.Read();                                                                      // StartObject (dataBlob)
                    //bool exists3 = Testing.manager.EntityExistsGlobaly(Testing.entityID);
                    BaseDataBlob dataBlob = (BaseDataBlob)serializer.Deserialize(reader, dataBlobType); // EndObject (dataBlob)
                    //bool exists4 = Testing.manager.EntityExistsGlobaly(Testing.entityID);
                    protoEntity.SetDataBlob(dataBlob);
                    reader.Read(); // PropertyName DATABLOB OR EndObject (Entity)
                    //bool exists5 = Testing.manager.EntityExistsGlobaly(Testing.entityID);
                }
                //bool exists6 = Testing.manager.EntityExistsGlobaly(Testing.entityID);
                return(protoEntity);
            }
Ejemplo n.º 5
0
        private static void UpdateOrbit(ProtoEntity entity, PositionDB parentPositionDB, DateTime toDate)
        {
            var entityOrbitDB  = entity.GetDataBlob <OrbitDB>(OrbitTypeIndex);
            var entityPosition = entity.GetDataBlob <PositionDB>(PositionTypeIndex);


            // Get our Parent-Relative coordinates.
            try
            {
                Vector4 newPosition = GetPosition(entityOrbitDB, toDate);

                // Get our Absolute coordinates.
                entityPosition.AbsolutePosition = parentPositionDB.AbsolutePosition + newPosition;
            }
            catch (OrbitProcessorException e)
            {
                // TODO: Debug log this exception. Do NOT fail to the UI. There is NO data-corruption on this exception.
                // In this event, we did NOT update our position.
                throw new Exception("Position Exception thrown in OrbitProcessor");
            }

            // Update our children.
            foreach (Entity child in entityOrbitDB.Children)
            {
                // RECURSION!
                UpdateOrbit(child, entityPosition, toDate);
            }
        }
Ejemplo n.º 6
0
        private static ProtoEntity CreateSurveyPoint(double x, double y, int nameNumber)
        {
            // TODO: Rebalance "pointsRequired" here.
            // TODO: Load "pointsRequired" from GalaxyGen settings
            const int pointsRequired = 400;

            var surveyDB = new JPSurveyableDB(pointsRequired, new Dictionary <Entity, int>());
            var posDB    = new PositionDB(x, y, 0, Guid.Empty);
            var nameDB   = new NameDB($"Survey Point #{nameNumber}");

            return(ProtoEntity.Create(Guid.Empty, new BaseDataBlob[] { surveyDB, posDB, nameDB }));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// OBSOLETE
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="parentPositionDB"></param>
        /// <param name="game"></param>
        /// <param name="orbitsProcessed"></param>
        private static void UpdateOrbit(ProtoEntity entity, PositionDB parentPositionDB, Game game, ref int orbitsProcessed)
        {
            var entityOrbitDB  = entity.GetDataBlob <OrbitDB>(OrbitTypeIndex);
            var entityPosition = entity.GetDataBlob <PositionDB>(PositionTypeIndex);

            //TODO why are we not just removing the OrbitDB for these settings?

            if (!game.Settings.OrbitalMotionForPlanetsMoons ?? true) //if NOT orbital motion or NOT null
            {
                var systemBodyDB = entity.GetDataBlob <SystemBodyInfoDB>();
                if (systemBodyDB != null && systemBodyDB.BodyType == BodyType.Moon) //what were you trying to do here? if (systemBodyDB != null && systemBodyDB.Type != BodyType.Asteroid && systemBodyDB.Type != BodyType.Comet)
                {
                    // Do not process this planet or moon.
                    return;
                }
            }

            if (!game.Settings.OrbitalMotionForAsteroids ?? true) //if NOT orbital motion or NOT null
            {
                var systemBodyDB = entity.GetDataBlob <SystemBodyInfoDB>();
                if (systemBodyDB != null && systemBodyDB.BodyType == BodyType.Asteroid)
                {
                    // Do not process this asteroid
                    return;
                }
            }

            // Get our Parent-Relative coordinates.
            try
            {
                Vector4 newPosition = GetPosition(entityOrbitDB, game.CurrentDateTime);

                // Get our Absolute coordinates.
                entityPosition.AbsolutePosition = parentPositionDB.AbsolutePosition + newPosition;

                Interlocked.Increment(ref orbitsProcessed);
            }
            catch (OrbitProcessorException e)
            {
                // TODO: Debug log this exception. Do NOT fail to the UI. There is NO data-corruption on this exception.
                // In this event, we did NOT update our position.
                throw new Exception("Position Exception thrown in OrbitProcessor");
            }

            // Update our children.
            foreach (Entity child in entityOrbitDB.Children)
            {
                // RECURSION!
                UpdateOrbit(child, entityPosition, game, ref orbitsProcessed);
            }
        }
Ejemplo n.º 8
0
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            {
                ProtoEntity protoEntity = (ProtoEntity)value;

                writer.WriteStartObject();                      // Start the Entity.
                writer.WritePropertyName("Guid");               // Write the Guid PropertyName
                serializer.Serialize(writer, protoEntity.Guid); // Write the Entity's guid.
                foreach (BaseDataBlob dataBlob in protoEntity.DataBlobs.Where(dataBlob => dataBlob != null))
                {
                    writer.WritePropertyName(dataBlob.GetType().Name); // Write the PropertyName of the dataBlob as the dataBlob's type.
                    serializer.Serialize(writer, dataBlob);            // Serialize the dataBlob in this property.
                }
                writer.WriteEndObject();                               // End then Entity.
            }
Ejemplo n.º 9
0
        public static Entity ImportEntity([NotNull] Game game, [NotNull] Stream inputStream, [NotNull] EntityManager manager)
        {
            if (manager == null)
            {
                throw new ArgumentNullException(nameof(manager));
            }

            var protoEntity = new ProtoEntity();

            protoEntity = Import(game, inputStream, protoEntity);
            Entity entity = Entity.Create(manager, protoEntity);

            game.PostGameLoad();
            return(entity);
        }
Ejemplo n.º 10
0
        public static ProtoEntity Create(Guid guid, IEnumerable <BaseDataBlob> dataBlobs = null)
        {
            var protoEntity = new ProtoEntity
            {
                DataBlobs = EntityManager.BlankDataBlobList(),
                Guid      = guid
            };

            if (dataBlobs == null)
            {
                return(protoEntity);
            }
            foreach (BaseDataBlob dataBlob in dataBlobs)
            {
                protoEntity.SetDataBlob(dataBlob);
            }

            return(protoEntity);
        }
Ejemplo n.º 11
0
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                var protoEntity = new ProtoEntity();

                //StarObject (Entity)
                reader.Read();                                            // PropertyName Guid
                reader.Read();                                            // Actual Guid
                protoEntity.Guid = serializer.Deserialize <Guid>(reader); // Deserialize the Guid

                // Deserialize the dataBlobs
                reader.Read(); // PropertyName DATABLOB
                while (reader.TokenType == JsonToken.PropertyName)
                {
                    Type dataBlobType = Type.GetType("Pulsar4X.ECSLib." + (string)reader.Value);
                    reader.Read();                                                                      // StartObject (dataBlob)
                    BaseDataBlob dataBlob = (BaseDataBlob)serializer.Deserialize(reader, dataBlobType); // EndObject (dataBlob)
                    protoEntity.SetDataBlob(dataBlob);
                    reader.Read();                                                                      // PropertyName DATABLOB OR EndObject (Entity)
                }

                return(protoEntity);
            }
Ejemplo n.º 12
0
 public static void Export([NotNull] Game game, [NotNull] Stream outputStream, [NotNull] ProtoEntity entity, bool compress          = false) => ExportInternal(game, outputStream, entity, compress);
Ejemplo n.º 13
0
 public static void Export([NotNull] Game game, [NotNull] string filePath, [NotNull] ProtoEntity entity, bool compress = false) => ExportFile(game, filePath, entity, compress);
Ejemplo n.º 14
0
 public static string Export([NotNull] Game game, [NotNull] ProtoEntity entity, bool compress    = false) => Export <ProtoEntity>(game, entity, compress);
Ejemplo n.º 15
0
 /// <summary>
 /// Create an entity from specified manager, faction and protoEntity.
 /// </summary>
 /// <returns>The create.</returns>
 /// <param name="manager">Manager.</param>
 /// <param name="faction">the faction owner of this new entity. use ID.Empty for a non owned entity.</param>
 /// <param name="protoEntity">Proto entity.</param>
 public static Entity Create(EntityManager manager, Guid faction, ProtoEntity protoEntity)
 {
     return(new Entity(protoEntity.Guid, manager, faction, protoEntity.DataBlobs));
 }
Ejemplo n.º 16
0
        public static Entity CreateShip(Entity classEntity, EntityManager systemEntityManager, Entity ownerFaction, Vector3 pos, StarSystem starsys, string shipName = null)
        {
            // @todo replace ownerFaction with formationDB later. Now ownerFaction used just to add name
            // @todo: make sure each component design and component instance is unique, not duplicated
            ProtoEntity protoShip = classEntity.Clone();

            ShipInfoDB shipInfoDB = protoShip.GetDataBlob <ShipInfoDB>();

            shipInfoDB.ShipClassDefinition = classEntity.Guid;

            if (shipName == null)
            {
                shipName = "Ship Name";
            }

            NameDB nameDB = new NameDB(shipName);

            nameDB.SetName(ownerFaction.Guid, shipName);
            protoShip.SetDataBlob(nameDB);

            OrderableDB orderableDB = new OrderableDB();

            protoShip.SetDataBlob(orderableDB);

            PositionDB position = new PositionDB(pos, starsys.Guid);

            protoShip.SetDataBlob(position);


            protoShip.SetDataBlob(new DesignInfoDB(classEntity));

            //replace the ships references to the design's specific instances with shiny new specific instances

            ComponentInstancesDB classInstances = classEntity.GetDataBlob <ComponentInstancesDB>();


            Entity shipEntity = new Entity(systemEntityManager, ownerFaction.Guid, protoShip);

            shipEntity.RemoveDataBlob <ComponentInstancesDB>();
            shipEntity.SetDataBlob(new ComponentInstancesDB());
            if (shipEntity.HasDataBlob <FireControlAbilityDB>())
            {
                shipEntity.RemoveDataBlob <FireControlAbilityDB>();
            }

            foreach (var designKVP in classInstances.DesignsAndComponentCount)
            {
                for (int i = 0; i < designKVP.Value; i++)
                {
                    Entity newInstance = ComponentInstanceFactory.NewInstanceFromDesignEntity(designKVP.Key, ownerFaction.Guid, systemEntityManager);
                    EntityManipulation.AddComponentToEntity(shipEntity, newInstance);
                }
            }



            FactionOwnerDB factionOwner = ownerFaction.GetDataBlob <FactionOwnerDB>();

            factionOwner.SetOwned(shipEntity);
            ComponentInstancesDB shipComponentInstanceDB = shipEntity.GetDataBlob <ComponentInstancesDB>();

            //TODO: do this somewhere else, recalcprocessor maybe?
            foreach (var design in shipComponentInstanceDB.GetDesignsByType(typeof(SensorReceverAtbDB)))
            {
                foreach (var instance in shipComponentInstanceDB.GetComponentsBySpecificDesign(design.Guid))
                {
                    var      sensor       = design.GetDataBlob <SensorReceverAtbDB>();
                    DateTime nextDatetime = shipEntity.Manager.ManagerSubpulses.StarSysDateTime + TimeSpan.FromSeconds(sensor.ScanTime);
                    shipEntity.Manager.ManagerSubpulses.AddEntityInterupt(nextDatetime, new SensorScan().TypeName, instance.OwningEntity);
                }
            }


            ReCalcProcessor.ReCalcAbilities(shipEntity);
            return(shipEntity);
        }
Ejemplo n.º 17
0
 internal Entity([NotNull] EntityManager manager, Guid factionID, [NotNull] ProtoEntity protoEntity) : this(protoEntity.Guid, manager, factionID, protoEntity.DataBlobs)
 {
 }
Ejemplo n.º 18
0
 internal Entity([NotNull] EntityManager manager, [NotNull] ProtoEntity protoEntity) : this(protoEntity.Guid, manager, Guid.Empty, protoEntity.DataBlobs)
 {
 }
Ejemplo n.º 19
0
        public static Entity CreateShip(Entity classEntity, EntityManager systemEntityManager, Entity ownerFaction, Vector4 pos, StarSystem starsys, string shipName = null)
        {
            // @todo replace ownerFaction with formationDB later. Now ownerFaction used just to add name
            // @todo: make sure each component design and component instance is unique, not duplicated
            ProtoEntity protoShip = classEntity.Clone();

            ShipInfoDB shipInfoDB = protoShip.GetDataBlob <ShipInfoDB>();

            shipInfoDB.ShipClassDefinition = classEntity.Guid;

            if (shipName == null)
            {
                shipName = "Ship Name";
            }

            NameDB nameDB = new NameDB(shipName);

            protoShip.SetDataBlob(nameDB);

            var OwnedDB = new OwnedDB(ownerFaction);

            protoShip.SetDataBlob(OwnedDB);

            PositionDB position = new PositionDB(pos, starsys.Guid);

            protoShip.SetDataBlob(position);

            protoShip.SetDataBlob(new DesignInfoDB(classEntity));

            Entity shipEntity = new Entity(systemEntityManager, protoShip);

            //replace the ships references to the design's specific instances with shiny new specific instances
            ComponentInstancesDB componentInstances = shipEntity.GetDataBlob <ComponentInstancesDB>();
            var newSpecificInstances = new PrIwObsDict <Entity, PrIwObsList <Entity> >();

            foreach (var kvp in componentInstances.SpecificInstances)
            {
                newSpecificInstances.Add(kvp.Key, new PrIwObsList <Entity>());
                for (int i = 0; i < kvp.Value.Count; i++)
                {
                    newSpecificInstances[kvp.Key].Add(ComponentInstanceFactory.NewInstanceFromDesignEntity(kvp.Key, ownerFaction));
                }
            }
            componentInstances.SpecificInstances = newSpecificInstances;

            foreach (var componentType in shipEntity.GetDataBlob <ComponentInstancesDB>().SpecificInstances)
            {
                int numComponents = componentType.Value.Count;
                componentType.Value.Clear();

                for (int i = 0; i < numComponents; i++)
                {
                    EntityManipulation.AddComponentToEntity(shipEntity, componentType.Key);
                }

                foreach (var componentInstance in componentType.Value)
                {
                    // Set the parent/owning Entity to the shipEntity
                    AttributeToAbilityMap.AddAbility(shipEntity, componentType.Key, componentInstance);
                }
            }

            ReCalcProcessor.ReCalcAbilities(shipEntity);
            return(shipEntity);
        }
Ejemplo n.º 20
0
 public static Entity Create(EntityManager manager, ProtoEntity protoEntity)
 {
     return(new Entity(manager, protoEntity.Guid, protoEntity.DataBlobs));
 }
Ejemplo n.º 21
0
        public static Entity CreateShip(Entity classEntity, EntityManager systemEntityManager, Entity ownerFaction, Vector4 pos, StarSystem starsys, string shipName = null)
        {
            // @todo replace ownerFaction with formationDB later. Now ownerFaction used just to add name
            // @todo: make sure each component design and component instance is unique, not duplicated
            ProtoEntity protoShip = classEntity.Clone();

            ShipInfoDB shipInfoDB = protoShip.GetDataBlob <ShipInfoDB>();

            shipInfoDB.ShipClassDefinition = classEntity.Guid;

            if (shipName == null)
            {
                shipName = "Ship Name";
            }

            NameDB nameDB = new NameDB(shipName);

            nameDB.SetName(ownerFaction, shipName);
            protoShip.SetDataBlob(nameDB);

            OrderableDB orderableDB = new OrderableDB();

            protoShip.SetDataBlob(orderableDB);

            PositionDB position = new PositionDB(pos, starsys.Guid);

            protoShip.SetDataBlob(position);


            protoShip.SetDataBlob(new DesignInfoDB(classEntity));

            Entity shipEntity = new Entity(systemEntityManager, protoShip);

            new OwnedDB(ownerFaction, shipEntity);

            //replace the ships references to the design's specific instances with shiny new specific instances
            ComponentInstancesDB componentInstances = shipEntity.GetDataBlob <ComponentInstancesDB>();
            var newSpecificInstances = new PrIwObsDict <Entity, PrIwObsList <Entity> >();

            foreach (var kvp in componentInstances.SpecificInstances)
            {
                newSpecificInstances.Add(kvp.Key, new PrIwObsList <Entity>());
                for (int i = 0; i < kvp.Value.Count; i++)
                {
                    var ownerdb = ownerFaction.GetDataBlob <FactionOwnerDB>();
                    newSpecificInstances[kvp.Key].Add(ComponentInstanceFactory.NewInstanceFromDesignEntity(kvp.Key, ownerFaction, ownerdb, systemEntityManager));
                }
            }
            componentInstances.SpecificInstances = newSpecificInstances;

            foreach (var componentType in shipEntity.GetDataBlob <ComponentInstancesDB>().SpecificInstances)
            {
                int numComponents = componentType.Value.Count;
                componentType.Value.Clear();

                for (int i = 0; i < numComponents; i++)
                {
                    EntityManipulation.AddComponentToEntity(shipEntity, componentType.Key);
                }

                foreach (var componentInstance in componentType.Value)
                {
                    // Set the parent/owning Entity to the shipEntity
                    AttributeToAbilityMap.AddAbility(shipEntity, componentType.Key, componentInstance);

                    //TODO: do this somewhere else, recalcprocessor maybe?
                    if (componentInstance.HasDataBlob <SensorReceverAtbDB>())
                    {
                        var      sensor       = componentInstance.GetDataBlob <SensorReceverAtbDB>();
                        DateTime nextDatetime = shipEntity.Manager.ManagerSubpulses.SystemLocalDateTime + TimeSpan.FromSeconds(sensor.ScanTime);
                        shipEntity.Manager.ManagerSubpulses.AddEntityInterupt(nextDatetime, new SensorScan().TypeName, componentInstance);
                    }
                }
            }

            ReCalcProcessor.ReCalcAbilities(shipEntity);
            return(shipEntity);
        }