#include "cas_casinoinc"

void CasinoNotification(string sMessage);

void main()
{
    object oEnteringPC = GetEnteringObject();
    if (!GetIsPC(oEnteringPC)) return;

    // Assign per-trigger variable to the entering PC
    string sVariableName = GetLocalString(OBJECT_SELF, "VariableName");
    if (sVariableName == "") return;

    int nVariableValue = GetLocalInt(OBJECT_SELF, "VariableValue");
    SetLocalInt(oEnteringPC, sVariableName, nVariableValue);

    // Optional: special case notification
    if (sVariableName == "I_AM_IN_HOLE" && nVariableValue == 1)
    {
        CasinoNotification("<cU˙˙>The gloryhole is now occupied...</c>");
    }

    // --- Listener spawn logic ---
    // If this trigger is flagged to spawn a listener, do so (once).
    if (GetLocalInt(OBJECT_SELF, "SPAWN_LISTENER") == 1)
    {
        object oExisting = GetLocalObject(OBJECT_SELF, "SPAWNED_LISTENER");
        if (!GetIsObjectValid(oExisting))
        {
            // Spawn the listener at the PC's location.
            location lSpawn = GetLocation(oEnteringPC);
            object oListener = CreateObject(
                OBJECT_TYPE_CREATURE,
                "cas_listener", // resref
                lSpawn,
                FALSE           // no appear anim
                // , ""          // optional new tag if you want one
            );

            // Remember it on the trigger so we don't double-spawn.
            SetLocalObject(OBJECT_SELF, "SPAWNED_LISTENER", oListener);

            // Give the engine a tick to finalize the spawn, then set OnConversation.
            // Uses your helper: void SetScript(object oObject, int nEvent, string sScriptName);
            DelayCommand(0.3f,
                SetScript(oListener, EVENT_SCRIPT_CREATURE_ON_DIALOGUE, "cas_repeatspeech")
            );
        }
    }
}
