internal static void AddComponentToEntity(Entity parentEntity, Entity componentEntity) { Entity ownerFaction = parentEntity.GetDataBlob <OwnedDB>().OwnedByFaction; FactionOwnerDB ownerdb = ownerFaction.GetDataBlob <FactionOwnerDB>(); AddComponentToEntity(parentEntity, componentEntity, ownerFaction, ownerdb); }
/// <summary> /// Initializes a new instance of the <see cref="T:Pulsar4X.ECSLib.EntityChangeListnerDB"/> class. /// </summary> /// <param name="factionEntity">will listen for any entites added or removed that are owned by this entity</param> public EntityChangeListner(EntityManager manager, Entity factionEntity, List <int> datablobFilter) : base(manager) { ListenForFaction = factionEntity; _ownerDB = ListenForFaction.GetDataBlob <FactionOwnerDB>(); IncludeDBTypeIndexFilter = datablobFilter; bool include = false; foreach (var entityitem in manager.GetEntitiesByFaction(ListenForFaction.Guid)) { foreach (var item in IncludeDBTypeIndexFilter) { if (entityitem.HasDataBlob(item)) { include = true; } else { include = false; break; } } if (include) { ListningToEntites.Add(entityitem); } } }
internal void SetFactionOwner(FactionOwnerDB factionOwnerDB, [System.Runtime.CompilerServices.CallerMemberName] string callerName = null) { if (callerName != nameof(FactionOwnerDB.SetOwned)) { throw new Exception("SetFactionOwner should be called from FactionOwnerDB.SetOwned"); } _factionOwner = factionOwnerDB.OwningEntity; FactionOwnerDB = factionOwnerDB; }
void SetFaction(Entity factionEntity) { FactionEntity = factionEntity; FactionInfo = FactionEntity.GetDataBlob <FactionInfoDB>(); foreach (var itemGuid in FactionInfo.KnownSystems) { KnownSystems.Add(_game.Systems[itemGuid]); } _factionOwner = factionEntity.GetDataBlob <FactionOwnerDB>(); SystemMap.Initialise(null, _game.Systems[FactionInfo.KnownSystems[0]], factionEntity); }
private static void BatchJobItemComplete(Entity colonyEntity, CargoStorageDB storage, ConstructionJob batchJob, ComponentInfoDB designInfo) { var colonyConstruction = colonyEntity.GetDataBlob <ConstructionDB>(); batchJob.NumberCompleted++; batchJob.ProductionPointsLeft = designInfo.BuildPointCost; batchJob.MineralsRequired = designInfo.MinerialCosts; batchJob.MineralsRequired = designInfo.MaterialCosts; batchJob.MineralsRequired = designInfo.ComponentCosts; var factionInfo = colonyEntity.GetDataBlob <OwnedDB>().OwnedByFaction.GetDataBlob <FactionInfoDB>(); Entity designEntity = factionInfo.ComponentDesigns[batchJob.ItemGuid]; Entity factionOwner = colonyEntity.GetDataBlob <OwnedDB>().OwnedByFaction; FactionOwnerDB ownerdb = factionOwner.GetDataBlob <FactionOwnerDB>(); Entity specificComponent = ComponentInstanceFactory.NewInstanceFromDesignEntity(designEntity, factionOwner, ownerdb, colonyEntity.Manager); if (batchJob.InstallOn != null) { if (batchJob.InstallOn == colonyEntity || StorageSpaceProcessor.HasEntity(storage, colonyEntity.GetDataBlob <CargoAbleTypeDB>())) { EntityManipulation.AddComponentToEntity(batchJob.InstallOn, specificComponent); ReCalcProcessor.ReCalcAbilities(batchJob.InstallOn); } } else { StorageSpaceProcessor.AddCargo(storage, specificComponent.GetDataBlob <CargoAbleTypeDB>(), 1); } if (batchJob.NumberCompleted == batchJob.NumberOrdered) { colonyConstruction.JobBatchList.Remove(batchJob); if (batchJob.Auto) { colonyConstruction.JobBatchList.Add(batchJob); } } }
/// <summary> /// Initializes a new instance of the <see cref="T:Pulsar4X.ECSLib.EntityChangeListnerDB"/> class. /// </summary> /// <param name="factionEntity">will listen for any entites added or removed that are owned by this entity</param> internal EntityChangeListner(EntityManager manager, Entity factionEntity, List <int> datablobFilter) : base(manager) { ListenForFaction = factionEntity; _ownerDB = ListenForFaction.GetDataBlob <FactionOwnerDB>(); IncludeDBTypeIndexFilter = datablobFilter; if (!IncludeDBTypeIndexFilter.Contains(EntityManager.DataBlobTypes[typeof(OwnedDB)])) { IncludeDBTypeIndexFilter.Add(EntityManager.DataBlobTypes[typeof(OwnedDB)]); } bool include = false; foreach (var entityitem in manager.GetAllEntitiesWithDataBlob <OwnedDB>()) //TODO: this could maybe be made more efficent if GetAllEntiesWithDatablobs(mask) had some use instructions. { if (entityitem.GetDataBlob <OwnedDB>().OwnedByFaction == ListenForFaction) { foreach (var item in IncludeDBTypeIndexFilter) { if (entityitem.HasDataBlob(item)) { include = true; } else { include = false; break; } } } if (include) { ListningToEntites.Add(entityitem); } } }
/// <summary> /// This is for adding components and installations to ships and colonies. /// batch add is faster than single add as it recalcs only once. /// </summary> /// <param name="parentEntity">entity that contains an ComponentInstancesDB</param> /// <param name="componentEntitys">Can be either a design or instance entity</param> internal static void AddComponentToEntity(Entity parentEntity, List <Entity> componentEntitys, Entity faction, FactionOwnerDB owner) { Entity instance; foreach (var componentEntity in componentEntitys) { if (parentEntity.HasDataBlob <ComponentInstancesDB>()) { if (!componentEntity.HasDataBlob <ComponentInstanceInfoDB>()) { if (componentEntity.HasDataBlob <ComponentInfoDB>()) { instance = ComponentInstanceFactory.NewInstanceFromDesignEntity(componentEntity, faction, owner, parentEntity.Manager); } else { throw new Exception("componentEntity does not contain either a ComponentInfoDB or a ComponentInstanceInfoDB. Entity Not a ComponentDesign or ComponentInstance"); } } else { instance = componentEntity; } AddComponentInstanceToEntity(parentEntity, instance); } else { throw new Exception("parentEntiy does not contain a ComponentInstanceDB"); } } ReCalcProcessor.ReCalcAbilities(parentEntity); }
/// <summary> /// This is for adding components and installations to ships and colonies. /// </summary> /// <param name="parentEntity">entity that contains an ComponentInstancesDB</param> /// <param name="componentEntity">Can be either a design or instance entity</param> internal static void AddComponentToEntity(Entity parentEntity, Entity componentEntity, Entity ownerFaction, FactionOwnerDB ownerDB) { Entity instance; if (parentEntity.HasDataBlob <ComponentInstancesDB>()) { if (!componentEntity.HasDataBlob <ComponentInstanceInfoDB>()) { if (componentEntity.HasDataBlob <ComponentInfoDB>()) { instance = ComponentInstanceFactory.NewInstanceFromDesignEntity(componentEntity, ownerFaction, ownerDB, parentEntity.Manager); } else { throw new Exception("componentEntity does not contain either a ComponentInfoDB or a ComponentInstanceInfoDB. Entity Not a ComponentDesign or ComponentInstance"); } } else { instance = componentEntity; } AddComponentInstanceToEntity(parentEntity, instance); } else { throw new Exception("parentEntiy does not contain a ComponentInstanceDB"); } ObjectOwnershipDB parentOwner; if (!parentEntity.HasDataBlob <ObjectOwnershipDB>()) { //StarSystem starSys = parentEntity.GetDataBlob<PositionDB>().SystemGuid parentOwner = new ObjectOwnershipDB(); parentEntity.SetDataBlob(parentOwner); } else { parentOwner = parentEntity.GetDataBlob <ObjectOwnershipDB>(); } parentOwner.Children.Add(instance); ReCalcProcessor.ReCalcAbilities(parentEntity); }
public FactionOwnerDB(FactionOwnerDB db) { OwnedEntities = new Dictionary <Guid, Entity>(db.OwnedEntities); }
internal static Entity NewInstanceFromDesignEntity(Entity design, Entity faction, FactionOwnerDB ownerdb, EntityManager manager) { List <BaseDataBlob> blobs = new List <BaseDataBlob>(); ComponentInstanceInfoDB info = new ComponentInstanceInfoDB(design); blobs.Add(info); blobs.Add(new DesignInfoDB(design)); // Added because each component instance needs its own copy of this datablob //Components have a mass and volume. MassVolumeDB mvDB = new MassVolumeDB(); blobs.Add(mvDB); //TODO: this seems ugly, consider using an Interface on the datablobs for this? if (design.HasDataBlob <BeamFireControlAtbDB>()) { blobs.Add(new FireControlInstanceAbilityDB()); } if (design.HasDataBlob <BeamWeaponAtbDB>() || design.HasDataBlob <SimpleBeamWeaponAtbDB>()) { blobs.Add(new WeaponStateDB()); } if (design.HasDataBlob <SensorReceverAtbDB>()) { blobs.Add((SensorReceverAtbDB)design.GetDataBlob <SensorReceverAtbDB>().Clone()); } Entity newInstance = new Entity(manager, blobs); new OwnedDB(faction, newInstance); //the constructor of OwnedDB sets itself to the entity return(newInstance); }
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); }
public static Entity CreateNewShipClass(Game game, Entity faction, string className = null) { //check className before any to use it in NameDB constructor if (string.IsNullOrEmpty(className)) { ///< @todo source the class name from faction theme. className = "New Class"; // <- Hack for now. } // lets start by creating all the Datablobs that make up a ship class: TODO only need to add datablobs for compoents it has abilites for. var shipInfo = new ShipInfoDB(); var armor = new ArmorDB(); var buildCost = new BuildCostDB(); var cargotype = new CargoAbleTypeDB(); var crew = new CrewDB(); var damage = new DamageDB(); var maintenance = new MaintenanceDB(); var sensorProfile = new SensorProfileDB(); var name = new NameDB(className); name.SetName(faction.Guid, className); var componentInstancesDB = new ComponentInstancesDB(); var massVolumeDB = new MassVolumeDB(); // now lets create a list of all these datablobs so we can create our new entity: List <BaseDataBlob> shipDBList = new List <BaseDataBlob>() { shipInfo, armor, buildCost, cargotype, crew, damage, maintenance, sensorProfile, name, componentInstancesDB, massVolumeDB, }; // now lets create the ship class: Entity shipClassEntity = new Entity(game.GlobalManager, faction.Guid, shipDBList); FactionOwnerDB factionOwner = faction.GetDataBlob <FactionOwnerDB>(); factionOwner.SetOwned(shipClassEntity); // also gets factionDB: FactionInfoDB factionDB = faction.GetDataBlob <FactionInfoDB>(); // and add it to the faction: factionDB.ShipClasses.Add(shipClassEntity); // now lets set some ship info: shipInfo.ShipClassDefinition = Guid.Empty; // just make sure it is marked as a class and not a ship. // now lets add some components: ///< @todo Add ship components // -- basic armour of current faction tech level // -- minimum crew quaters defaulting to 3 months deployment time. // -- a bridge // -- an engineering space // -- a fuel tank // now update the ship system DBs to reflect the components: ///< @todo update ship to reflect added components return(shipClassEntity); }