/// <summary> /// Create WorldObjects by ObjectType from a generator. /// </summary> /// <param name="generatorObject"></param> /// <returns>List of created WorldObjects</returns> public static List <WorldObject> CreateWorldObjectsFromGenerator(AceObject generator) { List <WorldObject> results = new List <WorldObject>(); DerethDateTime currentTime = new DerethDateTime(WorldManager.PortalYearTicks); Position pos = null; Random random = new Random((int)DateTime.UtcNow.Ticks); // Check if the current generator is meant to spawn objects at this time of the day switch (generator.GeneratorTimeType) { case (int)GeneratorTimeType.Day: if (currentTime.IsNight) { return(null); } break; case (int)GeneratorTimeType.Night: if (currentTime.IsDaytime) { return(null); } break; } // Check the probability of this generator spawning something at all if (random.Next(1, 100) >= generator.GeneratorProbability) { return(null); } // Generate objects from this generator #MaxGeneratedObjects times for (int i = 0; i < generator.MaxGeneratedObjects; i++) { switch (generator.GeneratorType) { // Use the position of the generator as a static position case (int)GeneratorType.Absolute: pos = generator.Location.InFrontOf(2.0); pos.PositionZ = pos.PositionZ - 0.5f; break; // Generate a random position inside the landblock case (int)GeneratorType.Relative: pos = GetRandomLocInLandblock(random, generator.Location.Cell); break; } // If this generator has linked generators use those for spawning objects if (generator.ActivationCreateClass == 0) { // Spawn this generator if it's not the top-level generator if (generator.GeneratorIID != null) { results.Add(new Generator(GuidManager.NewGeneratorGuid(), generator)); generator.GeneratorEnteredWorld = true; } // Get a random generator from the weighted list of linked generators and read it's AceObject from the DB if (generator.GeneratorLinks.Count == 0) { return(null); } uint linkId = GetRandomGeneratorIdFromGeneratorList(random, generator.GeneratorLinks); AceObject newGen = DatabaseManager.World.GetAceObjectByWeenie(linkId); // The linked generator is at the same location as the top generator and references its parent newGen.Location = pos; newGen.GeneratorIID = generator.AceObjectId; newGen.GeneratorEnteredWorld = true; // Recursively call this method again with the just read generatorObject List <WorldObject> objectList = CreateWorldObjectsFromGenerator(newGen); objectList?.ForEach(o => results.Add(o)); } // else spawn the objects directly from this generator else { WorldObject wo = WorldObjectFactory.CreateWorldObject((uint)generator.ActivationCreateClass); if (wo != null) { wo.Location = pos; if (wo.WeenieType == WeenieType.Creature || wo.WeenieType == WeenieType.Cow) { wo.Guid = GuidManager.NewNonStaticGuid(); } else { wo.Guid = GuidManager.NewItemGuid(); } wo.GeneratorId = generator.AceObjectId; results.Add(wo); } } } return(results); }
public static List <WorldObject> CreateWorldObjects(List <AceObject> sourceObjects) { var results = new List <WorldObject>(); var linkSourceResults = new List <AceObject>(); var linkResults = new Dictionary <int, List <AceObject> >(); foreach (var aceO in sourceObjects) { if (aceO.LinkSlot > 0) { if (aceO.LinkSource ?? false) { linkSourceResults.Add(aceO); continue; } if (!linkResults.ContainsKey((int)aceO.LinkSlot)) { linkResults.Add((int)aceO.LinkSlot, new List <AceObject>()); } linkResults[(int)aceO.LinkSlot].Add(aceO); continue; } if (aceO.Location != null) { WorldObject wo; if (aceO.AceObjectId > 0) { wo = CreateWorldObject(aceO); } else { // Here we're letting the server assign the guid (We're not doing a clone because the object will never be saved back to db) aceO.AceObjectId = GuidManager.NewGeneratorGuid().Full; wo = CreateWorldObject(aceO); } if (wo != null) { results.Add(wo); } } } foreach (var aceO in linkSourceResults) { int linkSlot = (int)aceO.LinkSlot; WorldObject wo; if (aceO.AceObjectId > 0) { wo = CreateWorldObject(aceO); } else { // Here we're letting the server assign the guid (We're not doing a clone because the object will never be saved back to db) aceO.AceObjectId = GuidManager.NewGeneratorGuid().Full; wo = CreateWorldObject(aceO); } if (linkResults.ContainsKey(linkSlot) && wo.GeneratorProfiles.Count > 0) { var profileTemplate = wo.GeneratorProfiles[0]; foreach (var link in linkResults[linkSlot]) { var profile = (AceObjectGeneratorProfile)profileTemplate.Clone(); profile.WeenieClassId = link.WeenieClassId; profile.LandblockRaw = link.Location.Cell; profile.PositionX = link.Location.PositionX; profile.PositionY = link.Location.PositionY; profile.PositionZ = link.Location.PositionZ; profile.RotationW = link.Location.RotationW; profile.RotationX = link.Location.RotationX; profile.RotationY = link.Location.RotationY; profile.RotationZ = link.Location.RotationZ; profile.Probability = Math.Abs(profile.Probability); wo.GeneratorProfiles.Add(profile); } wo.SelectGeneratorProfiles(); wo.UpdateGeneratorInts(); wo.QueueGenerator(); } if (wo != null) { results.Add(wo); } } return(results); }