DungeonJob.EntityTableDunGen.cs 1.9 KB

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