Procgen asteroids for salvage magnet (#17547)

* Procgen asteroids for salvage magnet

* big roids, remove min offset radius

* i fuck with configs a bit more

* some shit i guess

* actually this makes more sense
This commit is contained in:
Nemanja
2023-06-25 13:50:41 -04:00
committed by GitHub
parent c82d1b437e
commit 7cdfaa09d5
7 changed files with 261 additions and 187 deletions

View File

@@ -9,6 +9,6 @@
/// <summary> /// <summary>
/// The magnet that spawned this grid. /// The magnet that spawned this grid.
/// </summary> /// </summary>
public SalvageMagnetComponent? SpawnerMagnet; public EntityUid? SpawnerMagnet;
} }
} }

View File

@@ -1,4 +1,5 @@
using Content.Shared.Radio; using Content.Shared.Radio;
using Content.Shared.Random;
using Content.Shared.Salvage; using Content.Shared.Salvage;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
@@ -12,33 +13,19 @@ namespace Content.Server.Salvage
[Access(typeof(SalvageSystem))] [Access(typeof(SalvageSystem))]
public sealed class SalvageMagnetComponent : SharedSalvageMagnetComponent public sealed class SalvageMagnetComponent : SharedSalvageMagnetComponent
{ {
/// <summary>
/// Offset relative to magnet used as centre of the placement circle.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("offset")]
public Vector2 Offset = Vector2.Zero; // TODO: Maybe specify a direction, and find the nearest edge of the magnets grid the salvage can fit at
/// <summary>
/// Minimum distance from the offset position that will be used as a salvage's spawnpoint.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("offsetRadiusMin")]
public float OffsetRadiusMin = 24f;
/// <summary> /// <summary>
/// Maximum distance from the offset position that will be used as a salvage's spawnpoint. /// Maximum distance from the offset position that will be used as a salvage's spawnpoint.
/// </summary> /// </summary>
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
[DataField("offsetRadiusMax")] [DataField("offsetRadiusMax")]
public float OffsetRadiusMax = 48f; public float OffsetRadiusMax = 32;
/// <summary> /// <summary>
/// The entity attached to the magnet /// The entity attached to the magnet
/// </summary> /// </summary>
[ViewVariables(VVAccess.ReadOnly)] [ViewVariables(VVAccess.ReadOnly)]
[DataField("attachedEntity")] [DataField("attachedEntity")]
public EntityUid? AttachedEntity = null; public EntityUid? AttachedEntity;
/// <summary> /// <summary>
/// Current state of this magnet /// Current state of this magnet
@@ -95,18 +82,34 @@ namespace Content.Server.Salvage
/// <summary> /// <summary>
/// Current how much charge the magnet currently has /// Current how much charge the magnet currently has
/// </summary> /// </summary>
[DataField("chargeRemaining")]
public int ChargeRemaining = 5; public int ChargeRemaining = 5;
/// <summary> /// <summary>
/// How much capacity the magnet can hold /// How much capacity the magnet can hold
/// </summary> /// </summary>
[DataField("chargeCapacity")]
public int ChargeCapacity = 5; public int ChargeCapacity = 5;
/// <summary> /// <summary>
/// Used as a guard to prevent spamming the appearance system /// Used as a guard to prevent spamming the appearance system
/// </summary> /// </summary>
[DataField("previousCharge")]
public int PreviousCharge = 5; public int PreviousCharge = 5;
/// <summary>
/// The chance that a random procgen asteroid will be
/// generated rather than a static salvage prototype.
/// </summary>
[DataField("asteroidChance"), ViewVariables(VVAccess.ReadWrite)]
public float AsteroidChance = 0.6f;
/// <summary>
/// A weighted random prototype corresponding to
/// what asteroid entities will be generated.
/// </summary>
[DataField("asteroidPool", customTypeSerializer: typeof(PrototypeIdSerializer<WeightedRandomPrototype>)), ViewVariables(VVAccess.ReadWrite)]
public string AsteroidPool = "RandomAsteroidPool";
} }
[CopyByRef, DataRecord] [CopyByRef, DataRecord]

View File

@@ -178,14 +178,14 @@ public sealed partial class SalvageSystem
// Handle payout after expedition has finished // Handle payout after expedition has finished
if (expedition.Completed) if (expedition.Completed)
{ {
_sawmill.Debug($"Completed mission {expedition.MissionParams.MissionType} with seed {expedition.MissionParams.Seed}"); Log.Debug($"Completed mission {expedition.MissionParams.MissionType} with seed {expedition.MissionParams.Seed}");
component.NextOffer = _timing.CurTime + TimeSpan.FromSeconds(_cooldown); component.NextOffer = _timing.CurTime + TimeSpan.FromSeconds(_cooldown);
Announce(uid, Loc.GetString("salvage-expedition-mission-completed")); Announce(uid, Loc.GetString("salvage-expedition-mission-completed"));
GiveRewards(expedition); GiveRewards(expedition);
} }
else else
{ {
_sawmill.Debug($"Failed mission {expedition.MissionParams.MissionType} with seed {expedition.MissionParams.Seed}"); Log.Debug($"Failed mission {expedition.MissionParams.MissionType} with seed {expedition.MissionParams.Seed}");
component.NextOffer = _timing.CurTime + TimeSpan.FromSeconds(_failedCooldown); component.NextOffer = _timing.CurTime + TimeSpan.FromSeconds(_failedCooldown);
Announce(uid, Loc.GetString("salvage-expedition-mission-failed")); Announce(uid, Loc.GetString("salvage-expedition-mission-failed"));
} }

View File

@@ -1,28 +1,30 @@
using System.Linq;
using Content.Server.Construction; using Content.Server.Construction;
using Content.Server.GameTicking; using Content.Server.GameTicking;
using Content.Server.Radio.Components;
using Content.Server.Radio.EntitySystems; using Content.Server.Radio.EntitySystems;
using Content.Shared.CCVar;
using Content.Shared.Examine; using Content.Shared.Examine;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Popups; using Content.Shared.Popups;
using Content.Shared.Radio; using Content.Shared.Radio;
using Content.Shared.Salvage; using Content.Shared.Salvage;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Server.Maps;
using Robust.Shared.Configuration; using Robust.Shared.Configuration;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Player; using Robust.Shared.Player;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Random; using Robust.Shared.Random;
using Robust.Shared.Utility; using Robust.Shared.Utility;
using System.Linq;
using Content.Server.Chat.Managers; using Content.Server.Chat.Managers;
using Content.Server.Chat.Systems;
using Content.Server.Parallax; using Content.Server.Parallax;
using Content.Server.Procedural; using Content.Server.Procedural;
using Content.Server.Shuttles.Systems; using Content.Server.Shuttles.Systems;
using Content.Server.Station.Systems; using Content.Server.Station.Systems;
using Content.Server.Worldgen.Systems;
using Content.Shared.CCVar;
using Content.Shared.Random;
using Content.Shared.Random.Helpers;
using Robust.Server.Maps;
using Robust.Shared.Map.Components;
using Robust.Shared.Timing; using Robust.Shared.Timing;
namespace Content.Server.Salvage namespace Content.Server.Salvage
@@ -49,18 +51,15 @@ namespace Content.Server.Salvage
[Dependency] private readonly StationSystem _station = default!; [Dependency] private readonly StationSystem _station = default!;
[Dependency] private readonly UserInterfaceSystem _ui = default!; [Dependency] private readonly UserInterfaceSystem _ui = default!;
private static readonly int SalvageLocationPlaceAttempts = 16; private const int SalvageLocationPlaceAttempts = 16;
// TODO: This is probably not compatible with multi-station // TODO: This is probably not compatible with multi-station
private readonly Dictionary<EntityUid, SalvageGridState> _salvageGridStates = new(); private readonly Dictionary<EntityUid, SalvageGridState> _salvageGridStates = new();
private ISawmill _sawmill = default!;
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
_sawmill = Logger.GetSawmill("salvage");
SubscribeLocalEvent<SalvageMagnetComponent, InteractHandEvent>(OnInteractHand); SubscribeLocalEvent<SalvageMagnetComponent, InteractHandEvent>(OnInteractHand);
SubscribeLocalEvent<SalvageMagnetComponent, RefreshPartsEvent>(OnRefreshParts); SubscribeLocalEvent<SalvageMagnetComponent, RefreshPartsEvent>(OnRefreshParts);
SubscribeLocalEvent<SalvageMagnetComponent, UpgradeExamineEvent>(OnUpgradeExamine); SubscribeLocalEvent<SalvageMagnetComponent, UpgradeExamineEvent>(OnUpgradeExamine);
@@ -105,24 +104,21 @@ namespace Content.Server.Salvage
if (!Resolve(uid, ref component, false)) if (!Resolve(uid, ref component, false))
return; return;
int timeLeft = Convert.ToInt32(component.MagnetState.Until.TotalSeconds - currentTime.TotalSeconds); var timeLeft = Convert.ToInt32(component.MagnetState.Until.TotalSeconds - currentTime.TotalSeconds);
if (component.MagnetState.StateType == MagnetStateType.Inactive)
component.ChargeRemaining = 5; component.ChargeRemaining = component.MagnetState.StateType switch
else if (component.MagnetState.StateType == MagnetStateType.Holding)
{ {
component.ChargeRemaining = (timeLeft / (Convert.ToInt32(component.HoldTime.TotalSeconds) / component.ChargeCapacity)) + 1; MagnetStateType.Inactive => 5,
} MagnetStateType.Holding => timeLeft / (Convert.ToInt32(component.HoldTime.TotalSeconds) / component.ChargeCapacity) + 1,
else if (component.MagnetState.StateType == MagnetStateType.Detaching) MagnetStateType.Detaching => 0,
component.ChargeRemaining = 0; MagnetStateType.CoolingDown => component.ChargeCapacity - timeLeft / (Convert.ToInt32(component.CooldownTime.TotalSeconds) / component.ChargeCapacity) - 1,
else if (component.MagnetState.StateType == MagnetStateType.CoolingDown) _ => component.ChargeRemaining
{ };
component.ChargeRemaining = component.ChargeCapacity - (timeLeft / (Convert.ToInt32(component.CooldownTime.TotalSeconds) / component.ChargeCapacity)) - 1;
} if (component.PreviousCharge == component.ChargeRemaining)
if (component.PreviousCharge != component.ChargeRemaining) return;
{ _appearanceSystem.SetData(uid, SalvageMagnetVisuals.ChargeState, component.ChargeRemaining);
_appearanceSystem.SetData(uid, SalvageMagnetVisuals.ChargeState, component.ChargeRemaining); component.PreviousCharge = component.ChargeRemaining;
component.PreviousCharge = component.ChargeRemaining;
}
} }
private void OnGridRemoval(GridRemovalEvent ev) private void OnGridRemoval(GridRemovalEvent ev)
@@ -130,34 +126,37 @@ namespace Content.Server.Salvage
// If we ever want to give magnets names, and announce them individually, we would need to loop this, before removing it. // If we ever want to give magnets names, and announce them individually, we would need to loop this, before removing it.
if (_salvageGridStates.Remove(ev.EntityUid)) if (_salvageGridStates.Remove(ev.EntityUid))
{ {
if (EntityManager.TryGetComponent<SalvageGridComponent>(ev.EntityUid, out var salvComp) && salvComp.SpawnerMagnet != null) if (TryComp<SalvageGridComponent>(ev.EntityUid, out var salvComp) &&
Report(salvComp.SpawnerMagnet.Owner, salvComp.SpawnerMagnet.SalvageChannel, "salvage-system-announcement-spawn-magnet-lost"); TryComp<SalvageMagnetComponent>(salvComp.SpawnerMagnet, out var magnet))
Report(salvComp.SpawnerMagnet.Value, magnet.SalvageChannel, "salvage-system-announcement-spawn-magnet-lost");
// For the very unlikely possibility that the salvage magnet was on a salvage, we will not return here // For the very unlikely possibility that the salvage magnet was on a salvage, we will not return here
} }
foreach(var gridState in _salvageGridStates) foreach(var gridState in _salvageGridStates)
{ {
foreach(var magnet in gridState.Value.ActiveMagnets) foreach(var magnet in gridState.Value.ActiveMagnets)
{ {
if (magnet.AttachedEntity == ev.EntityUid) if (!TryComp<SalvageMagnetComponent>(magnet, out var magnetComponent))
{ continue;
magnet.AttachedEntity = null;
magnet.MagnetState = MagnetState.Inactive; if (magnetComponent.AttachedEntity != ev.EntityUid)
return; continue;
} magnetComponent.AttachedEntity = null;
magnetComponent.MagnetState = MagnetState.Inactive;
return;
} }
} }
} }
private void OnMagnetRemoval(EntityUid uid, SalvageMagnetComponent component, ComponentShutdown args) private void OnMagnetRemoval(EntityUid uid, SalvageMagnetComponent component, ComponentShutdown args)
{ {
if (component.MagnetState.StateType == MagnetStateType.Inactive) return; if (component.MagnetState.StateType == MagnetStateType.Inactive)
var magnetTranform = EntityManager.GetComponent<TransformComponent>(component.Owner);
if (!(magnetTranform.GridUid is EntityUid gridId) || !_salvageGridStates.TryGetValue(gridId, out var salvageGridState))
{
return; return;
}
salvageGridState.ActiveMagnets.Remove(component); var magnetTranform = Transform(uid);
if (magnetTranform.GridUid is not { } gridId || !_salvageGridStates.TryGetValue(gridId, out var salvageGridState))
return;
salvageGridState.ActiveMagnets.Remove(uid);
Report(uid, component.SalvageChannel, "salvage-system-announcement-spawn-magnet-lost"); Report(uid, component.SalvageChannel, "salvage-system-announcement-spawn-magnet-lost");
if (component.AttachedEntity.HasValue) if (component.AttachedEntity.HasValue)
{ {
@@ -169,6 +168,7 @@ namespace Content.Server.Salvage
{ {
Report(uid, component.SalvageChannel, "salvage-system-announcement-spawn-no-debris-available"); Report(uid, component.SalvageChannel, "salvage-system-announcement-spawn-no-debris-available");
} }
component.MagnetState = MagnetState.Inactive; component.MagnetState = MagnetState.Inactive;
} }
@@ -187,13 +187,13 @@ namespace Content.Server.Salvage
private void OnExamined(EntityUid uid, SalvageMagnetComponent component, ExaminedEvent args) private void OnExamined(EntityUid uid, SalvageMagnetComponent component, ExaminedEvent args)
{ {
var gotGrid = false;
var remainingTime = TimeSpan.Zero;
if (!args.IsInDetailsRange) if (!args.IsInDetailsRange)
return; return;
if (Transform(uid).GridUid is EntityUid gridId && var gotGrid = false;
var remainingTime = TimeSpan.Zero;
if (Transform(uid).GridUid is { } gridId &&
_salvageGridStates.TryGetValue(gridId, out var salvageGridState)) _salvageGridStates.TryGetValue(gridId, out var salvageGridState))
{ {
remainingTime = component.MagnetState.Until - salvageGridState.CurrentTime; remainingTime = component.MagnetState.Until - salvageGridState.CurrentTime;
@@ -201,8 +201,9 @@ namespace Content.Server.Salvage
} }
else else
{ {
Logger.WarningS("salvage", "Failed to load salvage grid state, can't display remaining time"); Log.Warning("Failed to load salvage grid state, can't display remaining time");
} }
switch (component.MagnetState.StateType) switch (component.MagnetState.StateType)
{ {
case MagnetStateType.Inactive: case MagnetStateType.Inactive:
@@ -223,7 +224,7 @@ namespace Content.Server.Salvage
args.PushMarkup(Loc.GetString("salvage-system-magnet-examined-active", ("timeLeft", Math.Ceiling(remainingTime.TotalSeconds)))); args.PushMarkup(Loc.GetString("salvage-system-magnet-examined-active", ("timeLeft", Math.Ceiling(remainingTime.TotalSeconds))));
break; break;
default: default:
throw new NotImplementedException("Unexpected magnet state type"); throw new ArgumentOutOfRangeException();
} }
} }
@@ -232,57 +233,56 @@ namespace Content.Server.Salvage
if (args.Handled) if (args.Handled)
return; return;
args.Handled = true; args.Handled = true;
StartMagnet(component, args.User); StartMagnet(uid, component, args.User);
UpdateAppearance(uid, component); UpdateAppearance(uid, component);
} }
private void StartMagnet(SalvageMagnetComponent component, EntityUid user) private void StartMagnet(EntityUid uid, SalvageMagnetComponent component, EntityUid user)
{ {
switch (component.MagnetState.StateType) switch (component.MagnetState.StateType)
{ {
case MagnetStateType.Inactive: case MagnetStateType.Inactive:
ShowPopup("salvage-system-report-activate-success", component, user); ShowPopup(uid, "salvage-system-report-activate-success", user);
SalvageGridState? gridState; var magnetTransform = Transform(uid);
var magnetTransform = EntityManager.GetComponent<TransformComponent>(component.Owner); var gridId = magnetTransform.GridUid ?? throw new InvalidOperationException("Magnet had no grid associated");
EntityUid gridId = magnetTransform.GridUid ?? throw new InvalidOperationException("Magnet had no grid associated"); if (!_salvageGridStates.TryGetValue(gridId, out var gridState))
if (!_salvageGridStates.TryGetValue(gridId, out gridState))
{ {
gridState = new SalvageGridState(); gridState = new SalvageGridState();
_salvageGridStates[gridId] = gridState; _salvageGridStates[gridId] = gridState;
} }
gridState.ActiveMagnets.Add(component); gridState.ActiveMagnets.Add(uid);
component.MagnetState = new MagnetState(MagnetStateType.Attaching, gridState.CurrentTime + component.AttachingTime); component.MagnetState = new MagnetState(MagnetStateType.Attaching, gridState.CurrentTime + component.AttachingTime);
RaiseLocalEvent(new SalvageMagnetActivatedEvent(component.Owner)); RaiseLocalEvent(new SalvageMagnetActivatedEvent(uid));
Report(component.Owner, component.SalvageChannel, "salvage-system-report-activate-success"); Report(uid, component.SalvageChannel, "salvage-system-report-activate-success");
break; break;
case MagnetStateType.Attaching: case MagnetStateType.Attaching:
case MagnetStateType.Holding: case MagnetStateType.Holding:
ShowPopup("salvage-system-report-already-active", component, user); ShowPopup(uid, "salvage-system-report-already-active", user);
break; break;
case MagnetStateType.Detaching: case MagnetStateType.Detaching:
case MagnetStateType.CoolingDown: case MagnetStateType.CoolingDown:
ShowPopup("salvage-system-report-cooling-down", component, user); ShowPopup(uid, "salvage-system-report-cooling-down", user);
break; break;
default: default:
throw new NotImplementedException("Unexpected magnet state type"); throw new ArgumentOutOfRangeException();
} }
} }
private void ShowPopup(string messageKey, SalvageMagnetComponent component, EntityUid user) private void ShowPopup(EntityUid uid, string messageKey, EntityUid user)
{ {
_popupSystem.PopupEntity(Loc.GetString(messageKey), component.Owner, user); _popupSystem.PopupEntity(Loc.GetString(messageKey), uid, user);
} }
private void SafeDeleteSalvage(EntityUid salvage) private void SafeDeleteSalvage(EntityUid salvage)
{ {
if(!EntityManager.TryGetComponent<TransformComponent>(salvage, out var salvageTransform)) if(!EntityManager.TryGetComponent<TransformComponent>(salvage, out var salvageTransform))
{ {
Logger.ErrorS("salvage", "Salvage entity was missing transform component"); Log.Error("Salvage entity was missing transform component");
return; return;
} }
if (salvageTransform.GridUid == null) if (salvageTransform.GridUid == null)
{ {
Logger.ErrorS("salvage", "Salvage entity has no associated grid?"); Log.Error( "Salvage entity has no associated grid?");
return; return;
} }
@@ -296,125 +296,124 @@ namespace Content.Server.Salvage
// Salvage mobs are NEVER immune (even if they're from a different salvage, they shouldn't be here) // Salvage mobs are NEVER immune (even if they're from a different salvage, they shouldn't be here)
continue; continue;
} }
Transform(playerEntityUid).AttachParent(salvageTransform.ParentUid); _transform.SetParent(playerEntityUid, salvageTransform.ParentUid);
} }
} }
// Deletion has to happen before grid traversal re-parents players. // Deletion has to happen before grid traversal re-parents players.
EntityManager.DeleteEntity(salvage); Del(salvage);
} }
private void TryGetSalvagePlacementLocation(SalvageMagnetComponent component, out MapCoordinates coords, out Angle angle) private bool TryGetSalvagePlacementLocation(EntityUid uid, SalvageMagnetComponent component, Box2 bounds, out MapCoordinates coords, out Angle angle)
{ {
coords = MapCoordinates.Nullspace; var xform = Transform(uid);
angle = Angle.Zero; angle = Angle.Zero;
var tsc = Transform(component.Owner); coords = new EntityCoordinates(uid, new Vector2(0, -component.OffsetRadiusMax)).ToMap(EntityManager, _transform);
coords = new EntityCoordinates(component.Owner, component.Offset).ToMap(EntityManager);
if (_mapManager.TryGetGrid(tsc.GridUid, out var magnetGrid) && TryComp<TransformComponent>(magnetGrid.Owner, out var gridXform)) if (xform.GridUid is not null)
angle = _transform.GetWorldRotation(Transform(xform.GridUid.Value));
for (var i = 0; i < SalvageLocationPlaceAttempts; i++)
{ {
angle = gridXform.WorldRotation; var randomRadius = _random.NextFloat(component.OffsetRadiusMax);
var randomOffset = _random.NextAngle().ToWorldVec() * randomRadius;
var finalCoords = coords.Offset(randomOffset);
var box2 = Box2.CenteredAround(finalCoords.Position, bounds.Size);
var box2Rot = new Box2Rotated(box2, angle, finalCoords.Position);
// This doesn't stop it from spawning on top of random things in space
// Might be better like this, ghosts could stop it before
if (_mapManager.FindGridsIntersecting(finalCoords.MapId, box2Rot).Any())
continue;
coords = finalCoords;
return true;
} }
return false;
} }
private IEnumerable<SalvageMapPrototype> GetAllSalvageMaps() => private bool SpawnSalvage(EntityUid uid, SalvageMagnetComponent component)
_prototypeManager.EnumeratePrototypes<SalvageMapPrototype>();
private bool SpawnSalvage(SalvageMagnetComponent component)
{ {
TryGetSalvagePlacementLocation(component, out var spl, out var spAngle); var salvMap = _mapManager.CreateMap();
var forcedSalvage = _configurationManager.GetCVar(CCVars.SalvageForced); Box2 bounds;
List<SalvageMapPrototype> allSalvageMaps; EntityUid? salvageEnt = null;
if (string.IsNullOrWhiteSpace(forcedSalvage)) SalvageMapPrototype? salvageProto = null;
if (_random.Prob(component.AsteroidChance))
{ {
allSalvageMaps = GetAllSalvageMaps().ToList(); var asteroidProto = _prototypeManager.Index<WeightedRandomPrototype>(component.AsteroidPool).Pick(_random);
salvageEnt = Spawn(asteroidProto, new MapCoordinates(0, 0, salvMap));
bounds = Comp<MapGridComponent>(salvageEnt.Value).LocalAABB;
} }
else else
{ {
allSalvageMaps = new(); var forcedSalvage = _configurationManager.GetCVar(CCVars.SalvageForced);
if (_prototypeManager.TryIndex<SalvageMapPrototype>(forcedSalvage, out var forcedMap)) salvageProto = string.IsNullOrWhiteSpace(forcedSalvage)
{ ? _random.Pick(_prototypeManager.EnumeratePrototypes<SalvageMapPrototype>().ToList())
allSalvageMaps.Add(forcedMap); : _prototypeManager.Index<SalvageMapPrototype>(forcedSalvage);
}
else bounds = salvageProto.Bounds;
{
Logger.ErrorS("c.s.salvage", $"Unable to get forced salvage map prototype {forcedSalvage}");
}
} }
SalvageMapPrototype? map = null; if (!TryGetSalvagePlacementLocation(uid, component, bounds, out var spawnLocation, out var spawnAngle))
Vector2 spawnLocation = Vector2.Zero;
for (var i = 0; i < allSalvageMaps.Count; i++)
{ {
SalvageMapPrototype attemptedMap = _random.PickAndTake(allSalvageMaps); Report(uid, component.SalvageChannel, "salvage-system-announcement-spawn-no-debris-available");
for (var attempt = 0; attempt < SalvageLocationPlaceAttempts; attempt++) _mapManager.DeleteMap(salvMap);
{
var randomRadius = _random.NextFloat(component.OffsetRadiusMin, component.OffsetRadiusMax);
var randomOffset = _random.NextAngle().ToWorldVec() * randomRadius;
spawnLocation = spl.Position + randomOffset;
var box2 = Box2.CenteredAround(spawnLocation + attemptedMap.Bounds.Center, attemptedMap.Bounds.Size);
var box2rot = new Box2Rotated(box2, spAngle, spawnLocation);
// This doesn't stop it from spawning on top of random things in space
// Might be better like this, ghosts could stop it before
if (!_mapManager.FindGridsIntersecting(spl.MapId, box2rot).Any())
{
map = attemptedMap;
break;
}
}
if (map != null)
{
break;
}
}
if (map == null)
{
Report(component.Owner, component.SalvageChannel, "salvage-system-announcement-spawn-no-debris-available");
return false; return false;
} }
var opts = new MapLoadOptions if (salvageEnt is { } ent)
{ {
Offset = spawnLocation var salvXForm = Transform(ent);
}; _transform.SetParent(ent, salvXForm, _mapManager.GetMapEntityId(spawnLocation.MapId));
_transform.SetWorldPosition(salvXForm, spawnLocation.Position);
var salvageEntityId = _map.LoadGrid(spl.MapId, map.MapPath.ToString(), opts);
if (salvageEntityId == null)
{
Report(component.Owner, component.SalvageChannel, "salvage-system-announcement-spawn-debris-disintegrated");
return false;
} }
component.AttachedEntity = salvageEntityId; else if (salvageProto != null)
var gridcomp = EntityManager.EnsureComponent<SalvageGridComponent>(salvageEntityId.Value); {
gridcomp.SpawnerMagnet = component; var opts = new MapLoadOptions
{
Offset = spawnLocation.Position,
Rotation = spawnAngle
};
var pulledTransform = EntityManager.GetComponent<TransformComponent>(salvageEntityId.Value); if (!_map.TryLoad(spawnLocation.MapId, salvageProto.MapPath.ToString(), out var roots, opts) ||
pulledTransform.WorldRotation = spAngle; roots.FirstOrNull() is not { } root)
{
Report(uid, component.SalvageChannel, "salvage-system-announcement-spawn-debris-disintegrated");
_mapManager.DeleteMap(salvMap);
return false;
}
Report(component.Owner, component.SalvageChannel, "salvage-system-announcement-arrived", ("timeLeft", component.HoldTime.TotalSeconds)); salvageEnt = root;
}
else
{
throw new InvalidOperationException("No asteroid generated and no salvage prototype present.");
}
component.AttachedEntity = salvageEnt;
var gridcomp = EnsureComp<SalvageGridComponent>(salvageEnt.Value);
gridcomp.SpawnerMagnet = uid;
_transform.SetWorldRotation(salvageEnt.Value, spawnAngle);
Report(uid, component.SalvageChannel, "salvage-system-announcement-arrived", ("timeLeft", component.HoldTime.TotalSeconds));
_mapManager.DeleteMap(salvMap);
return true; return true;
} }
private void Report(EntityUid source, string channelName, string messageKey, params (string, object)[] args) private void Report(EntityUid source, string channelName, string messageKey, params (string, object)[] args)
{ {
if (!TryComp<IntrinsicRadioReceiverComponent>(source, out var radio)) return;
var message = args.Length == 0 ? Loc.GetString(messageKey) : Loc.GetString(messageKey, args); var message = args.Length == 0 ? Loc.GetString(messageKey) : Loc.GetString(messageKey, args);
var channel = _prototypeManager.Index<RadioChannelPrototype>(channelName); var channel = _prototypeManager.Index<RadioChannelPrototype>(channelName);
_radioSystem.SendRadioMessage(source, message, channel, source); _radioSystem.SendRadioMessage(source, message, channel, source);
} }
private void Transition(SalvageMagnetComponent magnet, TimeSpan currentTime) private void Transition(EntityUid uid, SalvageMagnetComponent magnet, TimeSpan currentTime)
{ {
switch (magnet.MagnetState.StateType) switch (magnet.MagnetState.StateType)
{ {
case MagnetStateType.Attaching: case MagnetStateType.Attaching:
if (SpawnSalvage(magnet)) if (SpawnSalvage(uid, magnet))
{ {
magnet.MagnetState = new MagnetState(MagnetStateType.Holding, currentTime + magnet.HoldTime); magnet.MagnetState = new MagnetState(MagnetStateType.Holding, currentTime + magnet.HoldTime);
} }
@@ -424,7 +423,7 @@ namespace Content.Server.Salvage
} }
break; break;
case MagnetStateType.Holding: case MagnetStateType.Holding:
Report(magnet.Owner, magnet.SalvageChannel, "salvage-system-announcement-losing", ("timeLeft", magnet.DetachingTime.TotalSeconds)); Report(uid, magnet.SalvageChannel, "salvage-system-announcement-losing", ("timeLeft", magnet.DetachingTime.TotalSeconds));
magnet.MagnetState = new MagnetState(MagnetStateType.Detaching, currentTime + magnet.DetachingTime); magnet.MagnetState = new MagnetState(MagnetStateType.Detaching, currentTime + magnet.DetachingTime);
break; break;
case MagnetStateType.Detaching: case MagnetStateType.Detaching:
@@ -434,41 +433,42 @@ namespace Content.Server.Salvage
} }
else else
{ {
Logger.ErrorS("salvage", "Salvage detaching was expecting attached entity but it was null"); Log.Error("Salvage detaching was expecting attached entity but it was null");
} }
Report(magnet.Owner, magnet.SalvageChannel, "salvage-system-announcement-lost"); Report(uid, magnet.SalvageChannel, "salvage-system-announcement-lost");
magnet.MagnetState = new MagnetState(MagnetStateType.CoolingDown, currentTime + magnet.CooldownTime); magnet.MagnetState = new MagnetState(MagnetStateType.CoolingDown, currentTime + magnet.CooldownTime);
break; break;
case MagnetStateType.CoolingDown: case MagnetStateType.CoolingDown:
magnet.MagnetState = MagnetState.Inactive; magnet.MagnetState = MagnetState.Inactive;
break; break;
} }
UpdateAppearance(magnet.Owner, magnet); UpdateAppearance(uid, magnet);
UpdateChargeStateAppearance(magnet.Owner, currentTime, magnet); UpdateChargeStateAppearance(uid, currentTime, magnet);
} }
public override void Update(float frameTime) public override void Update(float frameTime)
{ {
var secondsPassed = TimeSpan.FromSeconds(frameTime); var secondsPassed = TimeSpan.FromSeconds(frameTime);
// Keep track of time, and state per grid // Keep track of time, and state per grid
foreach (var gridIdAndState in _salvageGridStates) foreach (var (uid, state) in _salvageGridStates)
{ {
var state = gridIdAndState.Value;
if (state.ActiveMagnets.Count == 0) continue; if (state.ActiveMagnets.Count == 0) continue;
var gridId = gridIdAndState.Key;
// Not handling the case where the salvage we spawned got paused // Not handling the case where the salvage we spawned got paused
// They both need to be paused, or it doesn't make sense // They both need to be paused, or it doesn't make sense
if (MetaData(gridId).EntityPaused) continue; if (MetaData(uid).EntityPaused) continue;
state.CurrentTime += secondsPassed; state.CurrentTime += secondsPassed;
var deleteQueue = new RemQueue<SalvageMagnetComponent>(); var deleteQueue = new RemQueue<EntityUid>();
foreach(var magnet in state.ActiveMagnets) foreach(var magnet in state.ActiveMagnets)
{ {
UpdateChargeStateAppearance(magnet.Owner, state.CurrentTime, magnet); if (!TryComp<SalvageMagnetComponent>(magnet, out var magnetComp))
if (magnet.MagnetState.Until > state.CurrentTime) continue; continue;
Transition(magnet, state.CurrentTime);
if (magnet.MagnetState.StateType == MagnetStateType.Inactive) UpdateChargeStateAppearance(magnet, state.CurrentTime, magnetComp);
if (magnetComp.MagnetState.Until > state.CurrentTime) continue;
Transition(magnet, magnetComp, state.CurrentTime);
if (magnetComp.MagnetState.StateType == MagnetStateType.Inactive)
{ {
deleteQueue.Add(magnet); deleteQueue.Add(magnet);
} }
@@ -488,7 +488,7 @@ namespace Content.Server.Salvage
public sealed class SalvageGridState public sealed class SalvageGridState
{ {
public TimeSpan CurrentTime { get; set; } public TimeSpan CurrentTime { get; set; }
public List<SalvageMagnetComponent> ActiveMagnets { get; } = new(); public List<EntityUid> ActiveMagnets { get; } = new();
} }
} }

View File

@@ -21,7 +21,11 @@ public sealed class LocalityLoaderSystem : BaseWorldSystem
while (e.MoveNext(out var uid, out var loadable, out var xform)) while (e.MoveNext(out var uid, out var loadable, out var xform))
{ {
if (!controllerQuery.TryGetComponent(xform.MapUid, out var controller)) if (!controllerQuery.TryGetComponent(xform.MapUid, out var controller))
return; {
RaiseLocalEvent(uid, new LocalStructureLoadedEvent());
RemCompDeferred<LocalityLoaderComponent>(uid);
continue;
}
var coords = GetChunkCoords(uid, xform); var coords = GetChunkCoords(uid, xform);
var done = false; var done = false;

View File

@@ -71,8 +71,14 @@
description: Locates salvage. description: Locates salvage.
components: components:
- type: SalvageMagnet - type: SalvageMagnet
offsetRadiusMin: 12 offsetRadiusMax: 32
offsetRadiusMax: 48
- type: ApcPowerReceiver - type: ApcPowerReceiver
powerLoad: 1000 powerLoad: 1000
- type: weightedRandom
id: RandomAsteroidPool
weights:
AsteroidSalvageSmall: 3
AsteroidSalvageMedium: 7
AsteroidSalvageLarge: 5
AsteroidSalvageHuge: 3

View File

@@ -16,18 +16,31 @@
FloorAsteroidCoarseSand0: FloorAsteroidCoarseSand0:
- id: WallRock - id: WallRock
prob: 0.5 prob: 0.5
- id: WallRockGold orGroup: rock
prob: 0.01
- id: WallRockSilver
prob: 0.04
- id: WallRockPlasma
prob: 0.09
- id: WallRockTin - id: WallRockTin
prob: 0.2 prob: 0.15
- id: WallRockUranium orGroup: rock
prob: 0.07
- id: WallRockQuartz - id: WallRockQuartz
prob: 0.2 prob: 0.15
orGroup: rock
- id: WallRockGold
prob: 0.05
orGroup: rock
- id: WallRockSilver
prob: 0.05
orGroup: rock
- id: WallRockPlasma
prob: 0.05
orGroup: rock
- id: WallRockUranium
prob: 0.02
orGroup: rock
- id: WallRockBananium
prob: 0.02
orGroup: rock
- id: WallRockArtifactFragment
prob: 0.01
orGroup: rock
- type: GCAbleObject - type: GCAbleObject
queue: SpaceDebris queue: SpaceDebris
- type: IFF - type: IFF
@@ -74,3 +87,51 @@
- type: BlobFloorPlanBuilder - type: BlobFloorPlanBuilder
radius: 12 radius: 12
floorPlacements: 36 floorPlacements: 36
- type: entity
id: AsteroidSalvageSmall
parent: BaseAsteroidDebris
name: Salvage Asteroid Small
noSpawn: true
components:
- type: MapGrid
- type: BlobFloorPlanBuilder
blobDrawProb: 0.66
radius: 15
floorPlacements: 100
- type: entity
id: AsteroidSalvageMedium
parent: BaseAsteroidDebris
name: Salvage Asteroid Medium
noSpawn: true
components:
- type: MapGrid
- type: BlobFloorPlanBuilder
blobDrawProb: 0.66
radius: 17
floorPlacements: 150
- type: entity
id: AsteroidSalvageLarge
parent: BaseAsteroidDebris
name: Salvage Asteroid Large
noSpawn: true
components:
- type: MapGrid
- type: BlobFloorPlanBuilder
blobDrawProb: 0.66
radius: 20
floorPlacements: 200
- type: entity
id: AsteroidSalvageHuge
parent: BaseAsteroidDebris
name: Salvage Asteroid Huge
noSpawn: true
components:
- type: MapGrid
- type: BlobFloorPlanBuilder
blobDrawProb: 0.66
radius: 23
floorPlacements: 250