Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teleport noobs off Arrivals shuttle to spawn #17189

Merged
merged 6 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Content.Server/GameTicking/GameTicker.Spawning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,16 @@ private void SpawnPlayer(IPlayerSession player, HumanoidCharacterProfile charact
// We also want this message last.
if (lateJoin && _arrivals.Enabled)
{
_chatManager.DispatchServerMessage(player, Loc.GetString("latejoin-arrivals-direction"));
var arrival = _arrivals.NextShuttleArrival();
if (arrival == null)
{
_chatManager.DispatchServerMessage(player, Loc.GetString("latejoin-arrivals-direction"));
}
else
{
_chatManager.DispatchServerMessage(player, Loc.GetString("latejoin-arrivals-direction-time",
("time", $"{arrival:mm\\:ss}")));
}
}

// We raise this event directed to the mob, but also broadcast it so game rules can do something now.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ public sealed class ArrivalsShuttleComponent : Component

[DataField("nextTransfer", customTypeSerializer:typeof(TimeOffsetSerializer))]
public TimeSpan NextTransfer;

[DataField("nextArrivalsTime", customTypeSerializer:typeof(TimeOffsetSerializer))]
public TimeSpan NextArrivalsTime;

}
83 changes: 80 additions & 3 deletions Content.Server/Shuttles/Systems/ArrivalsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ public override void Shutdown()

private void OnArrivalsFTL(EntityUid shuttleUid, ArrivalsShuttleComponent component, ref FTLStartedEvent args)
{
if (!TryGetArrivals(out EntityUid arrivals) || !TryComp<TransformComponent>(arrivals, out var arrivalsXform))
return;

var arrivalsMapUid = Transform(arrivals).MapUid;
// Don't do anything here when leaving arrivals.
if (args.FromMapUid == arrivalsMapUid)
return;

// Any mob then yeet them off the shuttle.
if (!_cfgManager.GetCVar(CCVars.ArrivalsReturns) && args.FromMapUid != null)
{
Expand All @@ -166,13 +174,24 @@ private void OnArrivalsFTL(EntityUid shuttleUid, ArrivalsShuttleComponent compon

var pendingQuery = AllEntityQuery<PendingClockInComponent, TransformComponent>();

// Clock them in when they FTL
// We're heading from the station back to arrivals (if leaving arrivals, would have returned above).
// Process everyone who holds a PendingClockInComponent
// Note, due to way DumpChildren works, anyone who doesn't have a PendingClockInComponent gets left in space
// and will not warp. This is intended behavior.
while (pendingQuery.MoveNext(out var pUid, out _, out var xform))
{
// Cheaper to iterate pending arrivals than all children
if (xform.GridUid != shuttleUid)
if (xform.GridUid == shuttleUid)
{
// Warp all players who are still on this shuttle to a spawn point. This doesn't let them return to
// arrivals. It also ensures noobs, slow players or AFK players safely leave the shuttle.
TryTeleportToMapSpawn(pUid, component.Station, xform);
}

// Players who have remained at arrives keep their warp coupon (PendingClockInComponent) for now.
if (xform.MapUid == arrivalsMapUid)
continue;

// The player has successfully left arrivals and is also not on the shuttle. Remove their warp coupon.
RemCompDeferred<PendingClockInComponent>(pUid);
RemCompDeferred<AutoOrientComponent>(pUid);
}
Expand Down Expand Up @@ -241,6 +260,41 @@ private void OnPlayerSpawn(PlayerSpawningEvent ev)
}
}

private bool TryTeleportToMapSpawn(EntityUid player, EntityUid stationId, TransformComponent? transform = null)
{
if (!Resolve(player, ref transform))
return false;

var points = EntityQuery<SpawnPointComponent, TransformComponent>().ToList();
_random.Shuffle(points);

// Find a spawnpoint on the same map as the player is already on now.
foreach (var (spawnPoint, xform) in points)
{
if (spawnPoint.SpawnType != SpawnPointType.LateJoin || _station.GetOwningStation(spawnPoint.Owner, xform) != stationId)
continue;

// Move the player to the spawnpoint.
transform.Coordinates = xform.Coordinates;

return true;
}

// Spawn them at a passenger location
foreach (var (spawnPoint, xform) in points)
{
if (spawnPoint?.Job?.ID != "Passenger" || _station.GetOwningStation(spawnPoint.Owner, xform) != stationId)
continue;

// Move the player to the spawnpoint.
transform.Coordinates = xform.Coordinates;

return true;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All stations have latejoin spawn points already so there isn't really a need to have a hardcoded backup to passengers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any harm in doing so though? I'm coding defensively here I guess.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's unnecessary but moreover, hardcoding in a prototype ID is generally frowned upon.


return false;
}

private void OnShuttleStartup(EntityUid uid, ArrivalsShuttleComponent component, ComponentStartup args)
{
EnsureComp<PreventPilotComponent>(uid);
Expand All @@ -263,6 +317,20 @@ private bool TryGetArrivals(out EntityUid uid)
return false;
}

public TimeSpan? NextShuttleArrival()
{
var query = EntityQueryEnumerator<ArrivalsShuttleComponent>();
var time = TimeSpan.MaxValue;
while (query.MoveNext(out var uid, out var comp))
{
if (comp.NextArrivalsTime < time)
time = comp.NextArrivalsTime;
}

var duration = _ticker.RoundDuration();
return (time < duration) ? null : time - _ticker.RoundDuration();
}

public override void Update(float frameTime)
{
base.Update(frameTime);
Expand All @@ -286,11 +354,15 @@ public override void Update(float frameTime)
if (comp.NextTransfer > curTime || !TryComp<StationDataComponent>(comp.Station, out var data))
continue;

var tripTime = ShuttleSystem.DefaultTravelTime + ShuttleSystem.DefaultStartupTime ;

// Go back to arrivals source
if (xform.MapUid != arrivalsXform.MapUid)
{
if (arrivals.IsValid())
_shuttles.FTLTravel(uid, shuttle, arrivals, dock: true);

comp.NextArrivalsTime = _ticker.RoundDuration() + TimeSpan.FromSeconds(tripTime);
}
// Go to station
else
Expand All @@ -299,6 +371,11 @@ public override void Update(float frameTime)

if (targetGrid != null)
_shuttles.FTLTravel(uid, shuttle, targetGrid.Value, dock: true);

// The ArrivalsCooldown includes the trip there, so we only need to add the time taken for
// the trip back.
comp.NextArrivalsTime = _ticker.RoundDuration() +
TimeSpan.FromSeconds(_cfgManager.GetCVar(CCVars.ArrivalsCooldown) + tripTime);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be using RoundDuration and also += like NextTransfer is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed roundDuration. However it doesn't make sense to use += here because the time estimate is not based on the old time estimate.

}

comp.NextTransfer += TimeSpan.FromSeconds(_cfgManager.GetCVar(CCVars.ArrivalsCooldown));
Expand Down
5 changes: 5 additions & 0 deletions Content.Server/Spawners/Components/SpawnPointComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public sealed class SpawnPointComponent : Component
public SpawnPointType SpawnType { get; } = SpawnPointType.Unset;

public JobPrototype? Job => string.IsNullOrEmpty(_jobId) ? null : _prototypeManager.Index<JobPrototype>(_jobId);

public override string ToString()
{
return $"{_jobId} {SpawnType}";
}
}

public enum SpawnPointType
Expand Down
1 change: 1 addition & 0 deletions Resources/Locale/en-US/game-ticking/game-ticker.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ player-leave-message = Player {$name} left.
latejoin-arrival-announcement = {$character} ({$job}) has arrived at the station!
latejoin-arrival-sender = Station
latejoin-arrivals-direction = A shuttle transferring you to your station will arrive shortly.
latejoin-arrivals-direction-time = A shuttle transferring you to your station will arrive in {$time}.