#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);

    // --- Listener spawn logic ---
    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_glorylisten", // resref
                lSpawn,
                FALSE
            );

            // Store reference so we don?t double spawn.
            SetLocalObject(OBJECT_SELF, "SPAWNED_LISTENER", oListener);

            // Set conversation handler
            SetLocalString(oListener, "EVENT_AI_CONVERSATION", "cas_repeatspeech");

            // If this trigger represents being in the hole, set a routing var
            if (sVariableName == "I_AM_IN_HOLE" && nVariableValue == 1)
            {
                SetLocalString(oListener, "sSEND_TO", "HOLE_OTHER_SIDE");
                CasinoNotification("<cU˙˙>The gloryhole is now occupied...</c>");
            }
        }
    }
}
