Space Kudzu (#5472)

This commit is contained in:
Moony
2021-11-28 20:25:36 -06:00
committed by GitHub
parent 30c87ca6b2
commit 9075cf6163
30 changed files with 617 additions and 42 deletions

View File

@@ -1,4 +1,5 @@
using Content.Server.Atmos.Components;
using Content.Server.Kudzu;
using Content.Shared.Atmos;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
@@ -13,6 +14,7 @@ namespace Content.Server.Atmos.EntitySystems
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
[Dependency] private readonly SpreaderSystem _spreaderSystem = default!;
public override void Initialize()
{
@@ -43,6 +45,7 @@ namespace Content.Server.Atmos.EntitySystems
SetAirblocked(airtight, false);
InvalidatePosition(airtight.LastPosition.Item1, airtight.LastPosition.Item2, airtight.FixVacuum);
RaiseLocalEvent(new AirtightChanged(airtight));
}
private void OnMapInit(EntityUid uid, AirtightComponent airtight, MapInitEvent args)
@@ -69,12 +72,14 @@ namespace Content.Server.Atmos.EntitySystems
airtight.CurrentAirBlockedDirection = (int) Rotate((AtmosDirection)airtight.InitialAirBlockedDirection, ev.NewRotation);
UpdatePosition(airtight);
RaiseLocalEvent(uid, new AirtightChanged(airtight));
}
public void SetAirblocked(AirtightComponent airtight, bool airblocked)
{
airtight.AirBlocked = airblocked;
UpdatePosition(airtight);
RaiseLocalEvent(airtight.OwnerUid, new AirtightChanged(airtight));
}
public void UpdatePosition(AirtightComponent airtight)
@@ -119,4 +124,14 @@ namespace Content.Server.Atmos.EntitySystems
return newAirBlockedDirs;
}
}
public class AirtightChanged : EntityEventArgs
{
public AirtightComponent Airtight;
public AirtightChanged(AirtightComponent airtight)
{
Airtight = airtight;
}
}
}

View File

@@ -0,0 +1,17 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Kudzu;
[RegisterComponent]
public class GrowingKudzuComponent : Component
{
public override string Name => "GrowingKudzu";
[DataField("growthLevel")]
public int GrowthLevel = 1;
[DataField("growthTickSkipChance")]
public float GrowthTickSkipChange = 0.0f;
}

View File

@@ -0,0 +1,56 @@
using Content.Shared.Kudzu;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Random;
namespace Content.Server.Kudzu;
public class GrowingKudzuSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _robustRandom = default!;
private float _accumulatedFrameTime = 0.0f;
public override void Initialize()
{
SubscribeLocalEvent<GrowingKudzuComponent, ComponentAdd>(SetupKudzu);
}
private void SetupKudzu(EntityUid uid, GrowingKudzuComponent component, ComponentAdd args)
{
if (!EntityManager.TryGetComponent<AppearanceComponent>(uid, out var appearance))
{
return;
}
appearance.SetData(KudzuVisuals.Variant, _robustRandom.Next(1, 3));
appearance.SetData(KudzuVisuals.GrowthLevel, 1);
}
public override void Update(float frameTime)
{
_accumulatedFrameTime += frameTime;
if (!(_accumulatedFrameTime >= 0.5f))
return;
_accumulatedFrameTime -= 0.5f;
foreach (var (kudzu, appearance) in EntityManager.EntityQuery<GrowingKudzuComponent, AppearanceComponent>())
{
if (kudzu.GrowthLevel >= 3 || !_robustRandom.Prob(kudzu.GrowthTickSkipChange)) continue;
kudzu.GrowthLevel += 1;
if (kudzu.GrowthLevel == 3 &&
EntityManager.TryGetComponent<SpreaderComponent>(kudzu.OwnerUid, out var spreader))
{
// why cache when you can simply cease to be? Also saves a bit of memory/time.
EntityManager.RemoveComponent<GrowingKudzuComponent>(kudzu.OwnerUid);
}
appearance.SetData(KudzuVisuals.GrowthLevel, kudzu.GrowthLevel);
}
}
}

View File

@@ -0,0 +1,32 @@
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Kudzu;
/// <summary>
/// Component for rapidly spreading objects, like Kudzu.
/// ONLY USE THIS FOR ANCHORED OBJECTS. An error will be logged if not anchored/static.
/// Currently does not support growing in space.
/// </summary>
[RegisterComponent, Friend(typeof(SpreaderSystem))]
public class SpreaderComponent : Component
{
public override string Name => "Spreader";
/// <summary>
/// Chance for it to grow on any given tick, after the normal growth rate-limit (if it doesn't grow, SpreaderSystem will pick another one.).
/// </summary>
[DataField("chance", required: true)]
public float Chance;
/// <summary>
/// Prototype spawned on growth success.
/// </summary>
[DataField("growthResult", required: true)]
public string GrowthResult = default!;
[DataField("enabled")]
public bool Enabled = true;
}

View File

@@ -0,0 +1,133 @@
using System.Collections.Generic;
using System.Linq;
using Content.Server.Atmos.Components;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Temperature.Systems;
using Content.Shared.Atmos;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Random;
namespace Content.Server.Kudzu;
// Future work includes making the growths per interval thing not global, but instead per "group"
public class SpreaderSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
/// <summary>
/// Maximum number of edges that can grow out every interval.
/// </summary>
private const int GrowthsPerInterval = 1;
private float _accumulatedFrameTime = 0.0f;
private readonly HashSet<EntityUid> _edgeGrowths = new ();
public override void Initialize()
{
SubscribeLocalEvent<SpreaderComponent, ComponentAdd>(SpreaderAddHandler);
SubscribeLocalEvent<AirtightChanged>(e => UpdateNearbySpreaders(e.Airtight.OwnerUid, e.Airtight));
}
private void SpreaderAddHandler(EntityUid uid, SpreaderComponent component, ComponentAdd args)
{
if (component.Enabled)
_edgeGrowths.Add(uid); // ez
}
public void UpdateNearbySpreaders(EntityUid blocker, AirtightComponent comp)
{
if (!EntityManager.TryGetComponent<TransformComponent>(blocker, out var transform))
return; // how did we get here?
if (!_mapManager.TryGetGrid(transform.GridID, out var grid)) return;
for (var i = 0; i < Atmospherics.Directions; i++)
{
var direction = (AtmosDirection) (1 << i);
if (!comp.AirBlockedDirection.IsFlagSet(direction)) continue;
foreach (var ent in grid.GetInDir(transform.Coordinates, direction.ToDirection()))
{
if (EntityManager.TryGetComponent<SpreaderComponent>(ent, out var s) && s.Enabled)
_edgeGrowths.Add(ent);
}
}
}
public override void Update(float frameTime)
{
_accumulatedFrameTime += frameTime;
if (!(_accumulatedFrameTime >= 1.0f))
return;
_accumulatedFrameTime -= 1.0f;
var growthList = _edgeGrowths.ToList();
_robustRandom.Shuffle(growthList);
var successes = 0;
foreach (var entity in growthList)
{
if (!TryGrow(entity)) continue;
successes += 1;
if (successes >= GrowthsPerInterval)
break;
}
}
private bool TryGrow(EntityUid ent, TransformComponent? transform = null, SpreaderComponent? spreader = null)
{
if (!Resolve(ent, ref transform, ref spreader, false))
return false;
if (spreader.Enabled == false) return false;
if (!_mapManager.TryGetGrid(transform.GridID, out var grid)) return false;
var didGrow = false;
for (var i = 0; i < 4; i++)
{
var direction = (DirectionFlag) (1 << i);
var coords = transform.Coordinates.Offset(direction.AsDir().ToVec());
if (grid.GetTileRef(coords).Tile.IsEmpty || _robustRandom.Prob(spreader.Chance)) continue;
var ents = grid.GetLocal(coords);
if (ents.Any(x => IsTileBlockedFrom(x, direction))) continue;
// Ok, spawn a plant
didGrow = true;
EntityManager.SpawnEntity(spreader.GrowthResult, transform.Coordinates.Offset(direction.AsDir().ToVec()));
}
return didGrow;
}
public void EnableSpreader(EntityUid ent, SpreaderComponent? component = null)
{
if (!Resolve(ent, ref component))
return;
component.Enabled = true;
_edgeGrowths.Add(ent);
}
private bool IsTileBlockedFrom(EntityUid ent, DirectionFlag dir)
{
if (EntityManager.TryGetComponent<SpreaderComponent>(ent, out _))
return true;
if (!EntityManager.TryGetComponent<AirtightComponent>(ent, out var airtight))
return false;
var oppositeDir = dir.AsDir().GetOpposite().ToAtmosDirection();
return airtight.AirBlocked && airtight.AirBlockedDirection.IsFlagSet(oppositeDir);
}
}

View File

@@ -92,7 +92,7 @@ namespace Content.Server.StationEvents.Events
base.Startup();
// Essentially we'll pick out a target amount of gas to leak, then a rate to leak it at, then work out the duration from there.
if (TryFindRandomTile(out _targetTile))
if (TryFindRandomTile(out _targetTile, out _targetStation, out _targetGrid, out _targetCoords))
{
_foundTile = true;
@@ -168,40 +168,5 @@ namespace Content.Server.StationEvents.Events
SoundSystem.Play(Filter.Pvs(_targetCoords), "/Audio/Effects/sparks4.ogg", _targetCoords);
}
}
private bool TryFindRandomTile(out Vector2i tile)
{
tile = default;
_targetStation = _robustRandom.Pick(_entityManager.EntityQuery<StationComponent>().ToArray()).Station;
var possibleTargets = _entityManager.EntityQuery<StationComponent>()
.Where(x => x.Station == _targetStation).ToArray();
_targetGrid = _robustRandom.Pick(possibleTargets).Owner;
if (!_entityManager.TryGetComponent<IMapGridComponent>(_targetGrid!.Uid, out var gridComp))
return false;
var grid = gridComp.Grid;
var atmosphereSystem = EntitySystem.Get<AtmosphereSystem>();
var found = false;
var gridBounds = grid.WorldBounds;
var gridPos = grid.WorldPosition;
for (var i = 0; i < 10; i++)
{
var randomX = _robustRandom.Next((int) gridBounds.Left, (int) gridBounds.Right);
var randomY = _robustRandom.Next((int) gridBounds.Bottom, (int) gridBounds.Top);
tile = new Vector2i(randomX - (int) gridPos.X, randomY - (int) gridPos.Y);
if (atmosphereSystem.IsTileSpace(grid, tile) || atmosphereSystem.IsTileAirBlocked(grid, tile)) continue;
found = true;
_targetCoords = grid.GridTileToLocal(tile);
break;
}
if (!found) return false;
return true;
}
}
}

View File

@@ -0,0 +1,55 @@
using Content.Shared.Station;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Random;
namespace Content.Server.StationEvents.Events;
public class KudzuGrowth : StationEvent
{
[Dependency] private IRobustRandom _robustRandom = default!;
[Dependency] private IEntityManager _entityManager = default!;
public override string Name => "KudzuGrowth";
public override string? StartAnnouncement =>
Loc.GetString("station-event-kudzu-growth-start-announcement");
public override string? StartAudio => "/Audio/Announcements/bloblarm.ogg";
public override int EarliestStart => 15;
public override int MinimumPlayers => 15;
// Get players to scatter a bit looking for it.
protected override float StartAfter => 50f;
// Give crew at least 9 minutes to either have it gone, or to suffer another event. Kudzu is not actually required to be dead for another event to roll.
protected override float EndAfter => 60*4;
private IEntity? _targetGrid;
private Vector2i _targetTile;
private EntityCoordinates _targetCoords;
public override void Startup()
{
base.Startup();
// Pick a place to plant the kudzu.
if (TryFindRandomTile(out _targetTile, out _, out _targetGrid, out _targetCoords, _robustRandom, _entityManager))
{
_entityManager.SpawnEntity("Kudzu", _targetCoords);
Logger.InfoS("stationevents", $"Spawning a Kudzu at {_targetTile} on {_targetGrid}");
}
// If the kudzu tile selection fails we just let the announcement happen anyways because it's funny and people
// will be hunting the non-existent, dangerous plant.
}
}

View File

@@ -1,11 +1,18 @@
using System.Linq;
using Content.Server.Administration.Logs;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Chat.Managers;
using Content.Server.Station;
using Content.Shared.Administration.Logs;
using Content.Shared.Station;
using Content.Shared.Database;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Player;
using Robust.Shared.Random;
namespace Content.Server.StationEvents.Events
{
@@ -178,5 +185,45 @@ namespace Content.Server.StationEvents.Events
Running = false;
}
}
public static bool TryFindRandomTile(out Vector2i tile, out StationId targetStation, out IEntity? targetGrid, out EntityCoordinates targetCoords, IRobustRandom? robustRandom = null, IEntityManager? entityManager = null)
{
tile = default;
robustRandom ??= IoCManager.Resolve<IRobustRandom>();
entityManager ??= IoCManager.Resolve<IEntityManager>();
targetCoords = EntityCoordinates.Invalid;
targetStation = robustRandom.Pick(entityManager.EntityQuery<StationComponent>().ToArray()).Station;
var t = targetStation; // thanks C#
var possibleTargets = entityManager.EntityQuery<StationComponent>()
.Where(x => x.Station == t).ToArray();
targetGrid = robustRandom.Pick(possibleTargets).Owner;
if (!entityManager.TryGetComponent<IMapGridComponent>(targetGrid!.Uid, out var gridComp))
return false;
var grid = gridComp.Grid;
var atmosphereSystem = EntitySystem.Get<AtmosphereSystem>();
var found = false;
var gridBounds = grid.WorldBounds;
var gridPos = grid.WorldPosition;
for (var i = 0; i < 10; i++)
{
var randomX = robustRandom.Next((int) gridBounds.Left, (int) gridBounds.Right);
var randomY = robustRandom.Next((int) gridBounds.Bottom, (int) gridBounds.Top);
tile = new Vector2i(randomX - (int) gridPos.X, randomY - (int) gridPos.Y);
if (atmosphereSystem.IsTileSpace(grid, tile) || atmosphereSystem.IsTileAirBlocked(grid, tile)) continue;
found = true;
targetCoords = grid.GridTileToLocal(tile);
break;
}
if (!found) return false;
return true;
}
}
}

View File

@@ -45,7 +45,7 @@ namespace Content.Server.Temperature.Components
{
get
{
if (Owner.TryGetComponent<IPhysBody>(out var physics))
if (Owner.TryGetComponent<IPhysBody>(out var physics) && physics.Mass != 0)
{
return SpecificHeat * physics.Mass;
}