public void Spawn(Xml.SpawnGroup spawnGroup) { foreach (var map in spawnGroup.Maps) { if (map.Disabled) { continue; } try { var spawnMap = Game.World.WorldData.Get <Map>(map.Id); GameLog.SpawnDebug("Spawn: calculating {0}", spawnMap.Name); var monsterList = spawnMap.Objects.OfType <Monster>().ToList(); var monsterCount = monsterList.Count; // If there is no limit specified, we want a reasonable limit, which we consider to be 1/10th of total // number of map tiles var spawnLimit = map.Limit == 0 ? (spawnMap.X * spawnMap.Y) / 10 : map.Limit; if (monsterCount > spawnLimit) { if (spawnMap.SpawnDebug) { GameLog.SpawnInfo($"Spawn: {map.Name}: not spawning, mob count is {monsterCount}, limit is {spawnLimit}"); } continue; } var since = DateTime.Now - map.LastSpawn; if (since.TotalSeconds < map.Interval) { if (spawnMap.SpawnDebug) { GameLog.SpawnInfo($"Spawn: {map.Name}: not spawning, last spawn was {since.TotalSeconds} ago, interval {map.Interval}"); } continue; } map.LastSpawn = DateTime.Now; var thisSpawn = _random.Next(map.MinSpawn, map.MaxSpawn + 1); GameLog.SpawnInfo($"Spawn: {map.Name}: spawning {thisSpawn} mobs "); for (var i = 0; i < thisSpawn; i++) { var spawn = spawnGroup.Spawns.PickRandom(true); if (spawn == null) { GameLog.SpawnError("Spawngroup empty, skipping"); break; } var creature = _creatures.FirstOrDefault(x => x.Name == spawn.Base); if (creature is default(Xml.Creature)) { GameLog.SpawnError($"Base monster {spawn.Base} not found"); break; } var newSpawnLoot = LootBox.CalculateLoot(spawn); if (spawnMap.SpawnDebug) { GameLog.SpawnInfo("Spawn {name}, map {map}: {Xp} xp, {Gold} gold, items {Items}", spawn.Base, map.Name, newSpawnLoot.Xp, newSpawnLoot.Gold, string.Join(',', newSpawnLoot.Items)); } var baseMob = new Monster(creature, spawn, map.Id, newSpawnLoot); var mob = (Monster)baseMob.Clone(); var xcoord = 0; var ycoord = 0; if (map.Coordinates.Count > 0) { // TODO: optimize / improve foreach (var coord in map.Coordinates) { if (spawnMap.EntityTree.GetObjects(new System.Drawing.Rectangle(coord.X, coord.Y, 1, 1)).Where(e => e is Creature).Count() == 0) { xcoord = coord.X; ycoord = coord.Y; break; } } } else { do { xcoord = _random.Next(0, spawnMap.X); ycoord = _random.Next(0, spawnMap.Y); } while (spawnMap.IsWall[xcoord, ycoord]); } mob.X = (byte)xcoord; mob.Y = (byte)ycoord; if (spawnMap.SpawnDebug) { GameLog.SpawnInfo($"Spawn: spawning {mob.Name} on {spawnMap.Name}"); } SpawnMonster(mob, spawnMap); } } catch (Exception e) { Game.ReportException(e); GameLog.SpawnError(e, "Spawngroup {Filename}: disabled map {Name} due to error", spawnGroup.Filename, map.Name); map.Disabled = true; continue; } } }
public void Spawn(Xml.SpawnGroup spawnGroup) { foreach (var map in spawnGroup.Maps) { if (map.Disabled) { continue; } try { var spawnMap = Game.World.WorldData.Get <Map>(map.Id); GameLog.SpawnDebug("Spawn: calculating {0}", spawnMap.Name); var monsterList = spawnMap.Objects.OfType <Monster>().ToList(); var monsterCount = monsterList.Count; // If there is no limit specified, we want a reasonable limit, which we consider to be 1/10th of total // number of map tiles var spawnLimit = map.Limit == 0 ? (spawnMap.X * spawnMap.Y) / 10 : map.Limit; if (monsterCount > spawnLimit) { if (spawnMap.SpawnDebug) { GameLog.SpawnInfo($"Spawn: {map.Name}: not spawning, mob count is {monsterCount}, limit is {spawnLimit}"); } continue; } var since = DateTime.Now - map.LastSpawn; if (since.TotalSeconds < map.Interval) { if (spawnMap.SpawnDebug) { GameLog.SpawnInfo($"Spawn: {map.Name}: not spawning, last spawn was {since.TotalSeconds} ago, interval {map.Interval}"); } continue; } map.LastSpawn = DateTime.Now; var thisSpawn = _random.Next(map.MinSpawn, map.MaxSpawn); GameLog.SpawnInfo($"Spawn: {map.Name}: spawning {thisSpawn} mobs "); for (var i = 0; i < thisSpawn; i++) { var idx = _random.Next(0, spawnGroup.Spawns.Count - 1); var spawn = spawnGroup.Spawns[idx]; var creature = _creatures.Single(x => x.Name == spawn.Base); var newSpawnLoot = LootBox.CalculateLoot(spawn); if (spawnMap.SpawnDebug) { GameLog.SpawnInfo("Spawn {name}, map {map}: {Xp} xp, {Gold} gold, items {Items}", spawn.Base, map.Name, newSpawnLoot.Xp, newSpawnLoot.Gold, string.Join(',', newSpawnLoot.Items)); } var baseMob = new Monster(creature, spawn, map.Id, newSpawnLoot); var mob = (Monster)baseMob.Clone(); var xcoord = 0; var ycoord = 0; do { xcoord = _random.Next(0, spawnMap.X - 1); ycoord = _random.Next(0, spawnMap.Y - 1); } while (spawnMap.IsWall[xcoord, ycoord]); mob.X = (byte)xcoord; mob.Y = (byte)ycoord; if (spawnMap.SpawnDebug) { GameLog.SpawnInfo($"Spawn: spawning {mob.Name} on {spawnMap.Name}"); } SpawnMonster(mob, spawnMap); } } catch (Exception e) { GameLog.SpawnError(e, "Spawngroup {Filename}: disabled map {Name} due to error", spawnGroup.Filename, map.Name); map.Disabled = true; continue; } } }