private static void SpawnResources(NWArea area, AreaSpawn areaSpawn) { var dbArea = DataService.Area.GetByResref(area.Resref); if (dbArea.ResourceSpawnTableID <= 0 || !dbArea.AutoSpawnResources) { return; } var possibleSpawns = DataService.SpawnObject.GetAllBySpawnTableID(dbArea.ResourceSpawnTableID).ToList(); // 1024 size = 32x32 // 256 size = 16x16 // 64 size = 8x8 int size = area.Width * area.Height; int maxSpawns = 0; if (size <= 12) { maxSpawns = 2; } else if (size <= 32) { maxSpawns = 6; } else if (size <= 64) { maxSpawns = 10; } else if (size <= 256) { maxSpawns = 25; } else if (size <= 512) { maxSpawns = 40; } else if (size <= 1024) { maxSpawns = 50; } int[] weights = new int[possibleSpawns.Count()]; for (int x = 0; x < possibleSpawns.Count(); x++) { weights[x] = possibleSpawns.ElementAt(x).Weight; } for (int x = 1; x <= maxSpawns; x++) { int index = RandomService.GetRandomWeightedIndex(weights); var dbSpawn = possibleSpawns.ElementAt(index); Location location = GetRandomSpawnPoint(area); NWPlaceable plc = (CreateObject(OBJECT_TYPE_PLACEABLE, dbSpawn.Resref, location)); ObjectSpawn spawn = new ObjectSpawn(location, false, dbArea.ResourceSpawnTableID, 600.0f); spawn.Spawn = plc; ObjectVisibilityService.ApplyVisibilityForObject(plc); if (dbSpawn.NPCGroupID != null && dbSpawn.NPCGroupID > 0) { plc.SetLocalInt("NPC_GROUP", Convert.ToInt32(dbSpawn.NPCGroupID)); spawn.NPCGroupID = Convert.ToInt32(dbSpawn.NPCGroupID); } if (!string.IsNullOrWhiteSpace(dbSpawn.BehaviourScript) && string.IsNullOrWhiteSpace(plc.GetLocalString("BEHAVIOUR"))) { plc.SetLocalString("BEHAVIOUR", dbSpawn.BehaviourScript); spawn.BehaviourScript = dbSpawn.BehaviourScript; } if (!string.IsNullOrWhiteSpace(dbSpawn.SpawnRule)) { var rule = GetSpawnRule(dbSpawn.SpawnRule); rule.Run(plc); } areaSpawn.Placeables.Add(spawn); } }
private static void ProcessSpawn(ObjectSpawn spawn, int objectType, NWArea area, bool forceSpawn) { // Don't process anything that's valid. if (spawn.Spawn.IsValid) { return; } spawn.Timer += ObjectProcessingService.ProcessingTickInterval; // Time to respawn! if (spawn.Timer >= spawn.RespawnTime || forceSpawn) { string resref = spawn.Resref; int npcGroupID = spawn.NPCGroupID; int deathVFXID = spawn.DeathVFXID; string behaviour = spawn.BehaviourScript; NWLocation location = spawn.IsStaticSpawnPoint ? spawn.SpawnLocation : null; AIFlags aiFlags = spawn.AIFlags; spawn.HasSpawnedOnce = true; // Look for a spawn out of the database set. Update spawn data if one is found. if (string.IsNullOrWhiteSpace(resref)) { var dbSpawn = DataService.SpawnObject.GetAllBySpawnTableID(spawn.SpawnTableID) .OrderBy(o => Guid.NewGuid()).First(); resref = dbSpawn.Resref; npcGroupID = dbSpawn.NPCGroupID ?? 0; deathVFXID = dbSpawn.DeathVFXID; behaviour = dbSpawn.BehaviourScript; aiFlags = dbSpawn.AIFlags; if (!string.IsNullOrWhiteSpace(dbSpawn.SpawnRule)) { spawn.SpawnRule = dbSpawn.SpawnRule; } else { // Clear the saved spawn rule since we now have a new resref etc. spawn.SpawnRule = null; } } if (location == null) { location = GetRandomSpawnPoint(area); } spawn.Spawn = CreateObject(objectType, resref, location); if (!spawn.Spawn.IsValid) { Console.WriteLine("SPAWN SERVICE ERROR: Cannot locate object with resref " + resref + ". Error occurred in area " + area.Name + " (" + area.Resref + ")"); return; } if (npcGroupID > 0) { spawn.Spawn.SetLocalInt("NPC_GROUP", npcGroupID); } if (deathVFXID > 0) { spawn.Spawn.SetLocalInt("DEATH_VFX", deathVFXID); } if (!string.IsNullOrWhiteSpace(behaviour) && string.IsNullOrWhiteSpace(spawn.Spawn.GetLocalString("BEHAVIOUR"))) { spawn.Spawn.SetLocalString("BEHAVIOUR", behaviour); } spawn.Spawn.SetLocalInt("AI_FLAGS", (int)aiFlags); if (objectType == OBJECT_TYPE_CREATURE) { AssignScriptEvents(spawn.Spawn.Object); } if (!string.IsNullOrWhiteSpace(spawn.SpawnRule)) { var rule = GetSpawnRule(spawn.SpawnRule); rule.Run(spawn.Spawn); } ObjectVisibilityService.ApplyVisibilityForObject(spawn.Spawn); spawn.Timer = 0.0f; } }
private static void ProcessSpawn(ObjectSpawn spawn, ObjectType objectType, NWArea area, bool forceSpawn) { // Spawn expiry. if (spawn.Expires < DateTime.UtcNow) { NWPlaceable prop = spawn.Spawn.GetLocalObject("RESOURCE_PROP_OBJ"); if (prop.IsValid) { DestroyObject(prop); } DestroyObject(spawn.Spawn); forceSpawn = true; } // Don't process anything that's valid. Put this in an else branch as object destruction // happens after script completion. else if (spawn.Spawn.IsValid) { return; } spawn.Timer += ObjectProcessingService.ProcessingTickInterval; // Time to respawn! if (spawn.Timer >= spawn.RespawnTime || forceSpawn) { string resref = spawn.Resref; int npcGroupID = spawn.NPCGroupID; int deathVFXID = spawn.DeathVFXID; string behaviour = spawn.BehaviourScript; NWLocation location = spawn.IsStaticSpawnPoint ? spawn.SpawnLocation : null; AIFlags aiFlags = spawn.AIFlags; spawn.HasSpawnedOnce = true; // Look for a spawn out of the database set. Update spawn data if one is found. if (string.IsNullOrWhiteSpace(resref)) { var dbSpawn = DataService.SpawnObject.GetAllBySpawnTableID(spawn.SpawnTableID) .OrderBy(o => Guid.NewGuid()).First(); resref = dbSpawn.Resref; npcGroupID = dbSpawn.NPCGroupID ?? 0; deathVFXID = dbSpawn.DeathVFXID; behaviour = dbSpawn.BehaviourScript; aiFlags = dbSpawn.AIFlags; if (!string.IsNullOrWhiteSpace(dbSpawn.SpawnRule)) { spawn.SpawnRule = dbSpawn.SpawnRule; } else { // Clear the saved spawn rule since we now have a new resref etc. spawn.SpawnRule = null; } } if (location == null) { location = GetRandomSpawnPoint(area); } spawn.Spawn = CreateObject(objectType, resref, location); if (!spawn.Spawn.IsValid) { Console.WriteLine("SPAWN SERVICE ERROR: Cannot locate object with resref " + resref + ". Error occurred in area " + area.Name + " (" + area.Resref + ")"); return; } if (npcGroupID > 0) { spawn.Spawn.SetLocalInt("NPC_GROUP", npcGroupID); } if (deathVFXID > 0) { spawn.Spawn.SetLocalInt("DEATH_VFX", deathVFXID); } if (!string.IsNullOrWhiteSpace(behaviour) && string.IsNullOrWhiteSpace(spawn.Spawn.GetLocalString("BEHAVIOUR"))) { spawn.Spawn.SetLocalString("BEHAVIOUR", behaviour); } spawn.Spawn.SetLocalInt("AI_FLAGS", (int)aiFlags); // Set expiry time for spawn. Set creatures to 26 hours (i.e. longer than the server reset // cycle, so never). Set placeables to 1 hour, so blocking objects will move and resources // will reset. if (objectType == ObjectType.Creature) { AssignScriptEvents(spawn.Spawn.Object); spawn.Expires = DateTime.UtcNow.AddHours(26); } else { spawn.Expires = DateTime.UtcNow.AddHours(1); } if (!string.IsNullOrWhiteSpace(spawn.SpawnRule)) { var rule = GetSpawnRule(spawn.SpawnRule); rule.Run(spawn.Spawn); } ObjectVisibilityService.ApplyVisibilityForObject(spawn.Spawn); spawn.Timer = 0.0f; } }