From c69cb0320ee66bbeed0ba72fd66654bad6b0a863 Mon Sep 17 00:00:00 2001 From: Gagarinten-Noverdo <71782778+noverd@users.noreply.github.com> Date: Mon, 29 Jul 2024 19:16:55 +0300 Subject: [PATCH] Biome spawner update. Round seed system (#359) * Biome spawner update. Round seed system * Format fix * Round seed M I T * Error to Warning * Test fix #2 * Test fix #3 * VV * Del out of body --- .../BiomeSpawner/CP14BiomeSpawnerSystem.cs | 95 ------------------- .../CP14BiomeSpawnerComponent.cs | 3 +- .../EntitySystems/CP14BiomeSpawnerSystem.cs | 95 +++++++++++++++++++ .../_CP14/RoundSeed/CP14RoundSeedComponent.cs | 21 ++++ .../_CP14/RoundSeed/CP14RoundSeedSystem.cs | 44 +++++++++ .../Prototypes/_CP14/GameRules/roundstart.yml | 3 +- 6 files changed, 164 insertions(+), 97 deletions(-) delete mode 100644 Content.Server/_CP14/BiomeSpawner/CP14BiomeSpawnerSystem.cs rename Content.Server/_CP14/BiomeSpawner/{ => Components}/CP14BiomeSpawnerComponent.cs (82%) create mode 100644 Content.Server/_CP14/BiomeSpawner/EntitySystems/CP14BiomeSpawnerSystem.cs create mode 100644 Content.Server/_CP14/RoundSeed/CP14RoundSeedComponent.cs create mode 100644 Content.Server/_CP14/RoundSeed/CP14RoundSeedSystem.cs diff --git a/Content.Server/_CP14/BiomeSpawner/CP14BiomeSpawnerSystem.cs b/Content.Server/_CP14/BiomeSpawner/CP14BiomeSpawnerSystem.cs deleted file mode 100644 index cb4a54b6e6..0000000000 --- a/Content.Server/_CP14/BiomeSpawner/CP14BiomeSpawnerSystem.cs +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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/CP14BiomeSpawnerComponent.cs b/Content.Server/_CP14/BiomeSpawner/Components/CP14BiomeSpawnerComponent.cs similarity index 82% rename from Content.Server/_CP14/BiomeSpawner/CP14BiomeSpawnerComponent.cs rename to Content.Server/_CP14/BiomeSpawner/Components/CP14BiomeSpawnerComponent.cs index 4607acac32..ccdf909849 100644 --- a/Content.Server/_CP14/BiomeSpawner/CP14BiomeSpawnerComponent.cs +++ b/Content.Server/_CP14/BiomeSpawner/Components/CP14BiomeSpawnerComponent.cs @@ -5,10 +5,11 @@ * */ +using Content.Server._CP14.BiomeSpawner.EntitySystems; using Content.Shared.Parallax.Biomes; using Robust.Shared.Prototypes; -namespace Content.Server._CP14.BiomeSpawner; +namespace Content.Server._CP14.BiomeSpawner.Components; /// /// 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/EntitySystems/CP14BiomeSpawnerSystem.cs b/Content.Server/_CP14/BiomeSpawner/EntitySystems/CP14BiomeSpawnerSystem.cs new file mode 100644 index 0000000000..be66a52244 --- /dev/null +++ b/Content.Server/_CP14/BiomeSpawner/EntitySystems/CP14BiomeSpawnerSystem.cs @@ -0,0 +1,95 @@ +/* + * 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 new file mode 100644 index 0000000000..ba03922114 --- /dev/null +++ b/Content.Server/_CP14/RoundSeed/CP14RoundSeedComponent.cs @@ -0,0 +1,21 @@ +/* + * 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 new file mode 100644 index 0000000000..80b209e356 --- /dev/null +++ b/Content.Server/_CP14/RoundSeed/CP14RoundSeedSystem.cs @@ -0,0 +1,44 @@ +/* + * 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 c3d63c6126..8851f7d35c 100644 --- a/Resources/Prototypes/_CP14/GameRules/roundstart.yml +++ b/Resources/Prototypes/_CP14/GameRules/roundstart.yml @@ -5,6 +5,7 @@ components: - type: GameRule cP14Allowed: true + - type: CP14RoundSeed - type: entity id: CP14ResourceHarvesting @@ -17,4 +18,4 @@ - CP14ExpeditionCollectObjectiveGroup CP14Mercenary: - CP14PersonalCurrencyCollectObjectiveGroup - # antags \ No newline at end of file + # antags