1
0

DungeonJob.MobDunGen.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Threading.Tasks;
  2. using Content.Server.Ghost.Roles.Components;
  3. using Content.Server.NPC.Components;
  4. using Content.Server.NPC.Systems;
  5. using Content.Shared.Physics;
  6. using Content.Shared.Procedural;
  7. using Content.Shared.Procedural.DungeonLayers;
  8. using Content.Shared.Storage;
  9. using Robust.Shared.Collections;
  10. using Robust.Shared.Random;
  11. namespace Content.Server.Procedural.DungeonJob;
  12. public sealed partial class DungeonJob
  13. {
  14. private async Task PostGen(
  15. MobsDunGen gen,
  16. Dungeon dungeon,
  17. Random random)
  18. {
  19. var availableRooms = new ValueList<DungeonRoom>();
  20. availableRooms.AddRange(dungeon.Rooms);
  21. var availableTiles = new ValueList<Vector2i>(dungeon.AllTiles);
  22. var entities = EntitySpawnCollection.GetSpawns(gen.Groups, random);
  23. var count = random.Next(gen.MinCount, gen.MaxCount + 1);
  24. var npcs = _entManager.System<NPCSystem>();
  25. for (var i = 0; i < count; i++)
  26. {
  27. while (availableTiles.Count > 0)
  28. {
  29. var tile = availableTiles.RemoveSwap(random.Next(availableTiles.Count));
  30. if (!_anchorable.TileFree(_grid, tile, (int) CollisionGroup.MachineLayer,
  31. (int) CollisionGroup.MachineLayer))
  32. {
  33. continue;
  34. }
  35. foreach (var ent in entities)
  36. {
  37. var uid = _entManager.SpawnAtPosition(ent, _maps.GridTileToLocal(_gridUid, _grid, tile));
  38. _entManager.RemoveComponent<GhostRoleComponent>(uid);
  39. _entManager.RemoveComponent<GhostTakeoverAvailableComponent>(uid);
  40. npcs.SleepNPC(uid);
  41. }
  42. break;
  43. }
  44. await SuspendDungeon();
  45. if (!ValidateResume())
  46. return;
  47. }
  48. }
  49. }