diff --git a/Content.Server/_CP14/BiomeSpawner/Components/CP14BiomeSpawnerComponent.cs b/Content.Server/_CP14/BiomeSpawner/CP14BiomeSpawnerComponent.cs similarity index 82% rename from Content.Server/_CP14/BiomeSpawner/Components/CP14BiomeSpawnerComponent.cs rename to Content.Server/_CP14/BiomeSpawner/CP14BiomeSpawnerComponent.cs index ccdf909849..4607acac32 100644 --- a/Content.Server/_CP14/BiomeSpawner/Components/CP14BiomeSpawnerComponent.cs +++ b/Content.Server/_CP14/BiomeSpawner/CP14BiomeSpawnerComponent.cs @@ -5,11 +5,10 @@ * */ -using Content.Server._CP14.BiomeSpawner.EntitySystems; using Content.Shared.Parallax.Biomes; using Robust.Shared.Prototypes; -namespace Content.Server._CP14.BiomeSpawner.Components; +namespace Content.Server._CP14.BiomeSpawner; /// /// fills the tile in which it is located with the contents of the biome. Includes: tile, decals and entities diff --git a/Content.Server/_CP14/BiomeSpawner/CP14BiomeSpawnerSystem.cs b/Content.Server/_CP14/BiomeSpawner/CP14BiomeSpawnerSystem.cs new file mode 100644 index 0000000000..cb4a54b6e6 --- /dev/null +++ b/Content.Server/_CP14/BiomeSpawner/CP14BiomeSpawnerSystem.cs @@ -0,0 +1,95 @@ +/* + * All right reserved to CrystallPunk. + * + * BUT this file is sublicensed under MIT License + * + */ + +using System.Numerics; +using Content.Server.Decals; +using Content.Server.GameTicking; +using Content.Server.Parallax; +using Robust.Server.GameObjects; +using Robust.Shared.Map; +using Robust.Shared.Map.Components; +using Robust.Shared.Prototypes; +using Robust.Shared.Random; + +namespace Content.Server._CP14.BiomeSpawner; + +public sealed class CP14BiomeSpawnerSystem : EntitySystem +{ + [Dependency] private readonly IPrototypeManager _proto = default!; + [Dependency] private readonly BiomeSystem _biome = default!; + [Dependency] private readonly TransformSystem _transform = default!; + [Dependency] private readonly SharedMapSystem _maps = default!; + [Dependency] private readonly DecalSystem _decals = default!; + [Dependency] private readonly IEntityManager _entManager = default!; + [Dependency] private readonly IRobustRandom _random = default!; + + private int _seed = 27; + public override void Initialize() + { + base.Initialize(); + + SubscribeLocalEvent(OnRoundStartAttempt); + SubscribeLocalEvent(OnMapInit); + } + + private void OnRoundStartAttempt(RoundStartAttemptEvent ev) + { + _seed = _random.Next(100000); + } + + private void OnMapInit(Entity spawner, ref MapInitEvent args) + { + SpawnBiome(spawner); + QueueDel(spawner); + } + + private void SpawnBiome(Entity spawner) + { + var biome = _proto.Index(spawner.Comp.Biome); + var parent = _transform.GetParent(spawner); + + if (parent == null) + return; + + var gridUid = parent.Owner; + + if (!TryComp(gridUid, out var map)) + return; + + var v2i = _transform.GetGridOrMapTilePosition(spawner); + if (!_biome.TryGetTile(v2i, biome.Layers, _seed, map, out var tile)) + return; + + // Set new tile + _maps.SetTile(gridUid, map, v2i, tile.Value); + + // Remove old decals + var oldDecals = _decals.GetDecalsInRange(gridUid, v2i + new Vector2(0.5f, 0.5f)); + foreach (var (id, _) in oldDecals) + { + _decals.RemoveDecal(gridUid, id); + } + + //Add decals + if (_biome.TryGetDecals(v2i, biome.Layers, _seed, map, out var decals)) + { + foreach (var decal in decals) + { + _decals.TryAddDecal(decal.ID, new EntityCoordinates(gridUid, decal.Position), out _); + } + } + + //TODO maybe need remove anchored entities here + + //Add entities + if (_biome.TryGetEntity(v2i, biome.Layers, tile.Value, _seed, map, out var entityProto)) + { + var ent = _entManager.SpawnEntity(entityProto, + new EntityCoordinates(gridUid, v2i + map.TileSizeHalfVector)); + } + } +} diff --git a/Content.Server/_CP14/BiomeSpawner/EntitySystems/CP14BiomeSpawnerSystem.cs b/Content.Server/_CP14/BiomeSpawner/EntitySystems/CP14BiomeSpawnerSystem.cs deleted file mode 100644 index be66a52244..0000000000 --- a/Content.Server/_CP14/BiomeSpawner/EntitySystems/CP14BiomeSpawnerSystem.cs +++ /dev/null @@ -1,95 +0,0 @@ -/* - * All right reserved to CrystallPunk. - * - * BUT this file is sublicensed under MIT License - * - */ - -using System.Linq; -using Content.Server._CP14.BiomeSpawner.Components; -using Content.Server._CP14.RoundSeed; -using Content.Server.Decals; -using Content.Server.Parallax; -using Robust.Server.GameObjects; -using Robust.Shared.Map; -using Robust.Shared.Map.Components; -using Robust.Shared.Prototypes; - -namespace Content.Server._CP14.BiomeSpawner.EntitySystems; - -public sealed class CP14BiomeSpawnerSystem : EntitySystem -{ - [Dependency] private readonly IPrototypeManager _proto = default!; - [Dependency] private readonly BiomeSystem _biome = default!; - [Dependency] private readonly TransformSystem _transform = default!; - [Dependency] private readonly SharedMapSystem _maps = default!; - [Dependency] private readonly DecalSystem _decals = default!; - [Dependency] private readonly EntityLookupSystem _lookup = default!; - [Dependency] private readonly CP14RoundSeedSystem _roundSeed = default!; - - public override void Initialize() - { - SubscribeLocalEvent(OnMapInit); - } - - private void OnMapInit(Entity ent, ref MapInitEvent args) - { - SpawnBiome(ent); - QueueDel(ent); - - } - - private void SpawnBiome(Entity ent) - { - var biome = _proto.Index(ent.Comp.Biome); - var spawnerTransform = Transform(ent); - - var gridUid = spawnerTransform.ParentUid; - - if (!TryComp(gridUid, out var map)) - return; - - if (!_roundSeed.TryGetSeed(out var seed)) - { - Log.Warning("Missing RoundSeed. Seed set to 0"); - seed = 0; - } - - var vec = _transform.GetGridOrMapTilePosition(ent); - - if (!_biome.TryGetTile(vec, biome.Layers, seed.Value, map, out var tile)) - return; - - // Set new tile - _maps.SetTile(gridUid, map, vec, tile.Value); - - var tileCenterVec = vec + map.TileSizeHalfVector; - - // Remove old decals - var oldDecals = _decals.GetDecalsInRange(gridUid, tileCenterVec); - foreach (var (id, _) in oldDecals) - { - _decals.RemoveDecal(gridUid, id); - } - - //Add decals - if (_biome.TryGetDecals(vec, biome.Layers, seed.Value, map, out var decals)) - { - foreach (var decal in decals) - { - _decals.TryAddDecal(decal.ID, new EntityCoordinates(gridUid, decal.Position), out _); - } - } - - // Remove entities - var oldEntities = _lookup.GetEntitiesInRange(spawnerTransform.Coordinates, 0.48f); - // TODO: Replace this shit with GetEntitiesInBox2 - foreach (var entToRemove in oldEntities.Concat(new[] { ent.Owner })) // Do not remove self - { - QueueDel(entToRemove); - } - - if (_biome.TryGetEntity(vec, biome.Layers, tile.Value, seed.Value, map, out var entityProto)) - Spawn(entityProto, new EntityCoordinates(gridUid, tileCenterVec)); - } -} diff --git a/Content.Server/_CP14/RoundSeed/CP14RoundSeedComponent.cs b/Content.Server/_CP14/RoundSeed/CP14RoundSeedComponent.cs deleted file mode 100644 index ba03922114..0000000000 --- a/Content.Server/_CP14/RoundSeed/CP14RoundSeedComponent.cs +++ /dev/null @@ -1,21 +0,0 @@ -/* - * All right reserved to CrystallPunk. - * - * BUT this file is sublicensed under MIT License - * - */ - -namespace Content.Server._CP14.RoundSeed; - -/// -/// This is used for round seed -/// -[RegisterComponent, Access(typeof(CP14RoundSeedSystem))] -public sealed partial class CP14RoundSeedComponent : Component -{ - [ViewVariables] - public static int MaxValue = 10000; - - [ViewVariables] - public int Seed; -} diff --git a/Content.Server/_CP14/RoundSeed/CP14RoundSeedSystem.cs b/Content.Server/_CP14/RoundSeed/CP14RoundSeedSystem.cs deleted file mode 100644 index 80b209e356..0000000000 --- a/Content.Server/_CP14/RoundSeed/CP14RoundSeedSystem.cs +++ /dev/null @@ -1,44 +0,0 @@ -/* - * All right reserved to CrystallPunk. - * - * BUT this file is sublicensed under MIT License - * - */ - -using System.Diagnostics.CodeAnalysis; -using JetBrains.Annotations; -using Robust.Shared.Random; - -namespace Content.Server._CP14.RoundSeed; - -/// -/// Provides a round seed for another systems -/// -public sealed class CP14RoundSeedSystem : EntitySystem -{ - [Dependency] private readonly IRobustRandom _random = default!; - - public override void Initialize() - { - SubscribeLocalEvent(OnComponentStartup); - } - - private void OnComponentStartup(Entity ent, ref ComponentStartup args) - { - ent.Comp.Seed = _random.Next(CP14RoundSeedComponent.MaxValue); - } - - [PublicAPI] - public bool TryGetSeed([NotNullWhen(true)] out int? seed) - { - seed = null; - var query = EntityQuery(); - foreach (var comp in query) - { - seed = comp.Seed; - return true; - } - - return false; - } -} diff --git a/Resources/Prototypes/_CP14/GameRules/roundstart.yml b/Resources/Prototypes/_CP14/GameRules/roundstart.yml index 8851f7d35c..c3d63c6126 100644 --- a/Resources/Prototypes/_CP14/GameRules/roundstart.yml +++ b/Resources/Prototypes/_CP14/GameRules/roundstart.yml @@ -5,7 +5,6 @@ components: - type: GameRule cP14Allowed: true - - type: CP14RoundSeed - type: entity id: CP14ResourceHarvesting @@ -18,4 +17,4 @@ - CP14ExpeditionCollectObjectiveGroup CP14Mercenary: - CP14PersonalCurrencyCollectObjectiveGroup - # antags + # antags \ No newline at end of file