using System;
using System.Collections.Concurrent;
using System.Linq;
using MoonSharp.Interpreter;
namespace RynthCore.Plugin.RynthLua;
///
/// The v2 API root: the game global (UtilityBelt's shape), script chat commands,
/// the module registry for require, and the hooks each API area implements in its
/// own file (partial methods: an area that isn't built yet simply isn't there).
///
/// World GameWorld*.cs game.World, WorldObject
/// Character GameCharacter*.cs game.Character
/// Actions GameActions*.cs game.Actions, game.ActionQueue, action objects
/// Library Library*.cs require("storage"), "enums" (+ globals), "json", "rynth.ai"
///
/// Everything here runs on the plugin pump thread.
///
public sealed partial class RynthLuaPlugin
{
// ── Hooks the API areas implement ───────────────────────────────────────
// World
partial void RegisterWorldApi(Script s, ScriptContext ctx, Table game);
partial void WorldTick();
partial void WorldOnLogin();
partial void WorldOnLogout();
partial void WorldOnCreateObject(uint objectId);
partial void WorldOnDeleteObject(uint objectId);
partial void WorldOnChatText(string text, int chatType);
partial void WorldOnSelected(uint currentId, uint previousId);
partial void WorldOnVendor(uint vendorId, bool open);
// Character
partial void RegisterCharacterApi(Script s, ScriptContext ctx, Table game);
partial void CharacterTick();
partial void CharacterOnLogin();
partial void CharacterOnLogout();
partial void CharacterOnCombatModeChange(int currentMode, int previousMode);
partial void CharacterOnEnchantmentAdded(uint spellId, double durationSeconds);
partial void CharacterOnEnchantmentRemoved(uint enchantmentId);
partial void CharacterOnChatText(string text, int chatType);
partial void CharacterOnUpdateHealth(uint targetId, float ratio, uint current, uint max);
// Actions
partial void RegisterActionsApi(Script s, ScriptContext ctx, Table game);
partial void ActionsTick();
partial void ActionsOnChatText(string text, int chatType);
partial void ActionsOnLogout();
// Library modules and globals
partial void RegisterLibraryGlobals(Script s, ScriptContext ctx);
partial void ResolveLibraryModule(Script s, ScriptContext ctx, string name, ref DynValue? module);
// WorldObject: WrapObject makes the table (raw fields Id and __rynth_object = true);
// World fills in the properties (its __index metatable), Actions adds the action methods.
partial void DecorateObject(Script s, ScriptContext ctx, uint id, Table obj);
partial void DecorateObjectActions(Script s, ScriptContext ctx, uint id, Table obj);
///
/// The Lua object for world object in this script (nil for 0).
/// Every area hands out objects through this, so they all have the same shape:
/// a table with the raw field Id (LuaArgs.Id reads it), properties from World and
/// action methods from Actions.
///
internal DynValue WrapObject(Script s, ScriptContext ctx, uint id)
{
if (id == 0) return DynValue.Nil;
var t = new Table(s);
t["Id"] = (double)id;
t["__rynth_object"] = true;
DecorateObject(s, ctx, id, t);
DecorateObjectActions(s, ctx, id, t);
return DynValue.NewTable(t);
}
// ── Game state ──────────────────────────────────────────────────────────
/// "CharacterSelect", "InGame" or "LoggingOut".
internal string GameState { get; private set; } = "CharacterSelect";
internal long LastTick { get; private set; }
/// Script chat commands ("/name" → owning script), readable from AC's thread.
private readonly ConcurrentDictionary _commandOwners = new(StringComparer.OrdinalIgnoreCase);
private readonly ConcurrentQueue<(string Script, string Command, string Args)> _pendingScriptCommands = new();
private void SetGameState(string state)
{
if (GameState == state) return;
GameState = state;
FireAll("Game.OnStateChanged", s =>
{
var t = new Table(s);
t["NewState"] = state;
return DynValue.NewTable(t);
});
}
/// Fires a v2 event on every running script that listens for it.
internal void FireAll(string key, Func