125 lines
5.9 KiB
C#
125 lines
5.9 KiB
C#
// ============================================================================
|
|
// RynthAi - RynthAiPlugin.DayNight.cs
|
|
// The Day/Night timer: day or night in Dereth now and the real time until the
|
|
// next dawn or dusk (DayNight/DayNightWindow.cs draws it, Plugins/Shared/DerethClock.cs
|
|
// has the math).
|
|
//
|
|
// It was RynthDefective's (2026-11-04). R-20261202-122606-0b16: /rdef daynight box
|
|
// answered "Unknown command: rdef" because that player had no RynthDefective loaded.
|
|
// 2026-12-02: it lives in RynthAi, so players don't need RynthDefective for this
|
|
// small piece. Same window title ("Day/Night"), so
|
|
// RynthCore keeps its saved place, size and the Settings > Interface rows;
|
|
// /ra daynight [show|hide|clear|box], and /rdef daynight hands over to it.
|
|
//
|
|
// The clock is read on the render thread, at most twice a second while the window
|
|
// draws, and only in world: its address isn't mapped before world entry, and a fault
|
|
// there can't be caught (ExpressionEngine.ReadGameClock has the same guard).
|
|
// ============================================================================
|
|
|
|
using System;
|
|
using System.Numerics;
|
|
using ImGuiNET;
|
|
using RynthCore.Plugin.RynthAi.DayNight;
|
|
using RynthCore.Plugin.Shared;
|
|
using RynthCore.PluginSdk;
|
|
|
|
namespace RynthCore.Plugin.RynthAi;
|
|
|
|
public sealed partial class RynthAiPlugin
|
|
{
|
|
internal const string DayNightTitle = DayNightWindow.Title, DayNightSurface = "window:daynight";
|
|
|
|
private DayNightWindow? _dayNightWindow;
|
|
private DayNightView _dayNightView = DayNightView.NotReady;
|
|
private long _nextDayNightReadMs;
|
|
private int _dayNightFailures;
|
|
|
|
private bool DayNightWindowWanted => Host.HasUiTheme && Host.HasSetPanelOpen;
|
|
|
|
/// <summary>
|
|
/// RynthPluginRenderEmbed "window:daynight": the timer, docked or popped out (the engine's current
|
|
/// context). 1 drawn, 0 not ready, -2 failed (three draws in a row threw). Render thread.
|
|
/// </summary>
|
|
internal int RenderDayNight(out Vector2 want)
|
|
{
|
|
want = default;
|
|
if (!_initialized || Host.ImGuiContext == IntPtr.Zero) return 0;
|
|
IntPtr previous = ImGui.GetCurrentContext();
|
|
ImGui.SetCurrentContext(EmbedContext());
|
|
try
|
|
{
|
|
long now = Environment.TickCount64;
|
|
if (now >= _nextDayNightReadMs)
|
|
{
|
|
_nextDayNightReadMs = now + 500;
|
|
_dayNightView = ReadDayNight(now);
|
|
}
|
|
_dayNightWindow ??= new DayNightWindow(Host);
|
|
float h = _dayNightWindow.Draw(_dayNightView, _dashboard?.Settings?.DayNightBox == true);
|
|
want = new Vector2(0, h);
|
|
_dayNightFailures = 0;
|
|
return 1;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Host.Log($"[RynthAi] day/night draw failed: {ex.GetType().Name}: {ex.Message}");
|
|
return ++_dayNightFailures >= 3 ? -2 : 0;
|
|
}
|
|
finally
|
|
{
|
|
ImGui.SetCurrentContext(previous);
|
|
}
|
|
}
|
|
|
|
/// <summary>Tests: the game clock's raw value instead of AC's memory (there is no AC in a test).</summary>
|
|
internal static Func<double>? GameClockOverride;
|
|
|
|
/// <summary>The clock now; NotReady out of world (never read then) or before the clock is set.</summary>
|
|
private DayNightView ReadDayNight(long now)
|
|
{
|
|
if (!_loginComplete || !Host.HasGetPlayerId || Host.GetPlayerId() == 0) return DayNightView.NotReady;
|
|
double raw = GameClockOverride?.Invoke()
|
|
?? BitConverter.Int64BitsToDouble(System.Runtime.InteropServices.Marshal.ReadInt64(new IntPtr(unchecked((int)DerethClock.ClockAddress))));
|
|
return DerethClock.IsUsable(raw) ? new DayNightView(true, DerethClock.At(raw), now) : DayNightView.NotReady;
|
|
}
|
|
|
|
/// <summary>"/ra daynight [show|hide|toggle|clear|box]": the timer shown or hidden (with the time now), or its background.</summary>
|
|
private void HandleDayNightCommand(string[] parts)
|
|
{
|
|
string verb = parts.Length > 2 ? parts[2].ToLowerInvariant() : "toggle";
|
|
string? look = verb switch
|
|
{
|
|
"clear" or "transparent" or "nobox" => "clear",
|
|
"box" or "background" => "box",
|
|
_ => null,
|
|
};
|
|
if (look != null)
|
|
{
|
|
var s = _dashboard?.Settings;
|
|
if (s == null) { ReplyFail("[RynthAi] Settings aren't loaded yet (log in first)."); return; }
|
|
s.DayNightBox = look == "box";
|
|
_dashboard?.SaveSettings();
|
|
ReplyOk(s.DayNightBox
|
|
? "[RynthAi] Day/Night timer: on its dark box (/ra daynight clear for no background)."
|
|
: "[RynthAi] Day/Night timer: on a clear background (/ra daynight box brings the dark box back).");
|
|
return;
|
|
}
|
|
RynthPanelOpen? mode = verb switch
|
|
{
|
|
"show" or "on" or "open" => RynthPanelOpen.Open,
|
|
"hide" or "off" or "close" => RynthPanelOpen.Close,
|
|
"" or "toggle" => RynthPanelOpen.Toggle,
|
|
_ => null,
|
|
};
|
|
if (mode == null) { ReplyFail($"[RynthAi] '{verb}' isn't show, hide, clear or box. Usage: /ra daynight [show|hide|clear|box]"); return; }
|
|
|
|
DayNightView clock = ReadDayNight(Environment.TickCount64);
|
|
string time = clock.Ready
|
|
? $" {(clock.Now.IsDay ? "Day" : "Night")} now ({clock.Now.HourName}); {clock.Now.Next.ToLowerInvariant()} in {DerethClock.Countdown(clock.Now.SecondsToChange)} of real time."
|
|
: "";
|
|
if (!Host.HasSetPanelOpen) { ReplyFail("[RynthAi] This RynthCore has no Day/Night timer window (it needs plugin windows)." + time); return; }
|
|
int state = Host.SetPanelOpen(DayNightTitle, mode.Value);
|
|
if (state < 0) { ReplyFail("[RynthAi] The Day/Night timer isn't there yet (RynthCore adds it a few seconds after login). Try again in a moment." + time); return; }
|
|
ReplyOk((state == 1 ? "[RynthAi] Day/Night timer shown." : "[RynthAi] Day/Night timer hidden.") + time);
|
|
}
|
|
}
|