Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Optimizon test #113

Merged
merged 4 commits into from
May 3, 2024
Merged
Changes from all 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
63 changes: 49 additions & 14 deletions Content.Server/Physics/Controllers/MoverController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using Content.Server.Shuttles.Components;
Expand All @@ -19,7 +21,7 @@ public sealed class MoverController : SharedMoverController
[Dependency] private readonly ThrusterSystem _thruster = default!;
[Dependency] private readonly SharedTransformSystem _xformSystem = default!;

private Dictionary<EntityUid, (ShuttleComponent, List<(EntityUid, PilotComponent, InputMoverComponent, TransformComponent)>)> _shuttlePilots = new();
private Dictionary<EntityUid, (ShuttleComponent, HashSet<(EntityUid, PilotComponent, InputMoverComponent, TransformComponent)>)> _shuttlePilots = new();

public override void Initialize()
{
Expand Down Expand Up @@ -61,8 +63,12 @@ public override void UpdateBeforeSolve(bool prediction, float frameTime)
{
base.UpdateBeforeSolve(prediction, frameTime);

var inputQueryEnumerator = AllEntityQuery<InputMoverComponent>();
// Existing code to update pilots and handle shuttle movement
UpdatePilots();
HandleShuttleMovement(frameTime);

// Existing code to handle mob movement
var inputQueryEnumerator = AllEntityQuery<InputMoverComponent>();
while (inputQueryEnumerator.MoveNext(out var uid, out var mover))
{
var physicsUid = uid;
Expand Down Expand Up @@ -93,17 +99,11 @@ public override void UpdateBeforeSolve(bool prediction, float frameTime)
continue;
}

HandleMobMovement(uid,
mover,
physicsUid,
body,
xformMover,
frameTime);
HandleMobMovement(uid, mover, physicsUid, body, xformMover, frameTime);
}

HandleShuttleMovement(frameTime);
}


public (Vector2 Strafe, float Rotation, float Brakes) GetPilotVelocityInput(PilotComponent component)
{
if (!Timing.InSimulation)
Expand Down Expand Up @@ -146,7 +146,41 @@ private void ResetSubtick(PilotComponent component)
component.LastInputTick = Timing.CurTick;
component.LastInputSubTick = 0;
}

// Corvax start
private void UpdatePilots()
{
var activePilotQuery = EntityQueryEnumerator<PilotComponent, InputMoverComponent>();
var shuttleQuery = GetEntityQuery<ShuttleComponent>();
var newPilots = new Dictionary<EntityUid, (ShuttleComponent Shuttle, HashSet<(EntityUid PilotUid, PilotComponent Pilot, InputMoverComponent Mover, TransformComponent ConsoleXform)>)>();

while (activePilotQuery.MoveNext(out var uid, out var pilot, out var mover))
{
if (!IsPilotActive(pilot)) continue;

var consoleEnt = pilot.Console;
if (!TryComp<TransformComponent>(consoleEnt, out var xform)) continue;

var gridId = xform.GridUid;
if (!shuttleQuery.TryGetComponent(gridId, out var shuttleComponent) || !shuttleComponent.Enabled) continue;

if (!newPilots.TryGetValue(gridId!.Value, out var pilots))
pilots = (shuttleComponent, new HashSet<(EntityUid, PilotComponent, InputMoverComponent, TransformComponent)>());

pilots.Item2.Add((uid, pilot, mover, xform));
newPilots[gridId.Value] = pilots;
}

_shuttlePilots = newPilots;
}

private bool IsPilotActive(PilotComponent pilot)
{
// Determine if the pilot is currently active based on input.
return pilot.HeldButtons != ShuttleButtons.None;
}
// Corvax end

protected override void HandleShuttleInput(EntityUid uid, ShuttleButtons button, ushort subTick, bool state)
{
if (!TryComp<PilotComponent>(uid, out var pilot) || pilot.Console == null)
Expand Down Expand Up @@ -248,19 +282,20 @@ private Vector2 ObtainMaxVel(Vector2 vel, ShuttleComponent shuttle)

var horizIndex = vel.X > 0 ? 1 : 3; // east else west
var vertIndex = vel.Y > 0 ? 2 : 0; // north else south
var horizComp = vel.X != 0 ? MathF.Pow(Vector2.Dot(vel, new (shuttle.BaseLinearThrust[horizIndex] / shuttle.LinearThrust[horizIndex], 0f)), 2) : 0;
var vertComp = vel.Y != 0 ? MathF.Pow(Vector2.Dot(vel, new (0f, shuttle.BaseLinearThrust[vertIndex] / shuttle.LinearThrust[vertIndex])), 2) : 0;
var horizComp = vel.X != 0 ? MathF.Pow(Vector2.Dot(vel, new Vector2(shuttle.BaseLinearThrust[horizIndex] / shuttle.LinearThrust[horizIndex], 0f)), 2) : 0;
var vertComp = vel.Y != 0 ? MathF.Pow(Vector2.Dot(vel, new Vector2(0f, shuttle.BaseLinearThrust[vertIndex] / shuttle.LinearThrust[vertIndex])), 2) : 0;

return shuttle.BaseMaxLinearVelocity * vel * MathF.ReciprocalSqrtEstimate(horizComp + vertComp);
}

private void HandleShuttleMovement(float frameTime)
{
var newPilots = new Dictionary<EntityUid, (ShuttleComponent Shuttle, List<(EntityUid PilotUid, PilotComponent Pilot, InputMoverComponent Mover, TransformComponent ConsoleXform)>)>();
var newPilots = new Dictionary<EntityUid, (ShuttleComponent Shuttle, HashSet<(EntityUid PilotUid, PilotComponent Pilot, InputMoverComponent Mover, TransformComponent ConsoleXform)>)>();

// We just mark off their movement and the shuttle itself does its own movement
var activePilotQuery = EntityQueryEnumerator<PilotComponent, InputMoverComponent>();
var shuttleQuery = GetEntityQuery<ShuttleComponent>();

while (activePilotQuery.MoveNext(out var uid, out var pilot, out var mover))
{
var consoleEnt = pilot.Console;
Expand All @@ -282,7 +317,7 @@ private void HandleShuttleMovement(float frameTime)

if (!newPilots.TryGetValue(gridId!.Value, out var pilots))
{
pilots = (shuttleComponent, new List<(EntityUid, PilotComponent, InputMoverComponent, TransformComponent)>());
pilots = (shuttleComponent, new HashSet<(EntityUid, PilotComponent, InputMoverComponent, TransformComponent)>());
newPilots[gridId.Value] = pilots;
}

Expand Down
Loading