63 lines
2.1 KiB
C#
63 lines
2.1 KiB
C#
using System.Diagnostics;
|
|
using BSO.Net;
|
|
|
|
namespace BSO.Server;
|
|
|
|
/// <summary>
|
|
/// Fake-Mitspieler zum Alleine-Testen: faehrt als "Bus" Kreise (20 m Radius, ~30 km/h) um die Stelle, an der der erste
|
|
/// Spieler im Spiel ist. Entfernt sich der Spieler mehr als 300 m, zieht der Kreis zu ihm um.
|
|
/// </summary>
|
|
public class TestBot
|
|
{
|
|
const float Radius = 2000; // cm
|
|
const float SpeedCm = 30 / 0.036f; // 30 km/h in cm/s
|
|
const float Follow = 30000; // cm
|
|
static readonly TimeSpan SendInterval = TimeSpan.FromMilliseconds(100);
|
|
|
|
public int Id { get; }
|
|
public string Name => "Testbot";
|
|
public PlayerState State { get; private set; }
|
|
public bool HasState { get; private set; }
|
|
|
|
readonly Stopwatch clock = Stopwatch.StartNew();
|
|
TimeSpan lastSend;
|
|
float centerX, centerY, angle;
|
|
bool hasCenter;
|
|
|
|
public TestBot(int id) => Id = id;
|
|
|
|
/// <summary>Bewegt den Bot weiter; true = neuer Zustand ist faellig (10x pro Sekunde).</summary>
|
|
public bool Tick(IEnumerable<PlayerState> players)
|
|
{
|
|
TimeSpan now = clock.Elapsed;
|
|
if (now - lastSend < SendInterval) return false;
|
|
float dt = (float)(now - lastSend).TotalSeconds;
|
|
lastSend = now;
|
|
|
|
PlayerState? target = players.Where(p => p.Mode != PlayerMode.Menu).Cast<PlayerState?>().FirstOrDefault();
|
|
if (target == null) return false;
|
|
PlayerState t = target.Value;
|
|
float dx = t.X - centerX, dy = t.Y - centerY;
|
|
if (!hasCenter || dx * dx + dy * dy > Follow * Follow)
|
|
{
|
|
centerX = t.X;
|
|
centerY = t.Y;
|
|
hasCenter = true;
|
|
GameServer.Log($"Testbot faehrt jetzt Kreise um {centerX / 100:0} / {centerY / 100:0} m");
|
|
}
|
|
|
|
angle += SpeedCm / Radius * Math.Min(dt, 1);
|
|
State = new PlayerState
|
|
{
|
|
Mode = PlayerMode.InBus,
|
|
X = centerX + Radius * MathF.Cos(angle),
|
|
Y = centerY + Radius * MathF.Sin(angle),
|
|
Z = t.Z,
|
|
Yaw = (angle * 180 / MathF.PI + 90) % 360, // Fahrtrichtung = Tangente
|
|
Speed = SpeedCm
|
|
};
|
|
HasState = true;
|
|
return true;
|
|
}
|
|
}
|