void main()
{
    object OITEM = OBJECT_SELF;
    object OPC = GetLastUsedBy();

    // Check if the projector is currently active
    int isActive = GetLocalInt(OITEM, "PROJECTOR_ACTIVE");

    // If the projector is active, turn it off and delete NPCs
    if (isActive)
    {
        // Turn off the projector
        SetLocalInt(OITEM, "PROJECTOR_ACTIVE", 0);

        // Delete all NPCs with the tag CAS_NPC_PROJECTOR
        object oNPC = GetObjectByTag("CAS_NPC_PROJECTOR");
        while(GetIsObjectValid(oNPC))
        {
            DestroyObject(oNPC);
            oNPC = GetObjectByTag("CAS_NPC_PROJECTOR");
        }

        // Inform the player
        SendMessageToPC(OPC, "Projector turned off and NPCs deleted.");
        return; // Exit the script
    }

    // If the projector is not active, continue with the original script
    // Set the projector as active
    SetLocalInt(OITEM, "PROJECTOR_ACTIVE", 1);

    // Original script functionality starts here
    float FTIME = GetLocalFloat(OITEM, "P_1b66_FTIME");
    SendMessageToPC(OPC, "Script started. FTIME value is: " + FloatToString(FTIME));

    // Declare spawnDelay here
    float spawnDelay = 2.0;

    // Retrieve the tags for NPCs from local variables set on OITEM
    string tagNPC1 = GetLocalString(OITEM, "P_1B66_TAG_NPC1");
    string tagNPC2 = GetLocalString(OITEM, "P_1B66_TAG_NPC2");

    // Attempt to find NPCs by their tags
    object NPC1 = GetObjectByTag(tagNPC1, 0);
    object NPC2 = GetObjectByTag(tagNPC2, 0);

    // Initialize lastSpeaker to OBJECT_INVALID as default
    object lastSpeaker = OBJECT_INVALID;

    // Spawn NPC1 if not found
    if (NPC1 == OBJECT_INVALID)
    {
        string resRefNPC1 = GetLocalString(OITEM, "P_1B66_SPAWN_NPC1");
        NPC1 = CreateObject(OBJECT_TYPE_CREATURE, resRefNPC1, GetLocation(OITEM));
        SetTag(NPC1, "CAS_NPC_PROJECTOR");
        SendMessageToPC(OPC, "Spawned NPC1 with tag CAS_NPC_PROJECTOR");
        lastSpeaker = NPC1;
    }

    // Spawn NPC2 if not found
    if (NPC2 == OBJECT_INVALID)
    {
        string resRefNPC2 = GetLocalString(OITEM, "P_1B66_SPAWN_NPC2");
        NPC2 = CreateObject(OBJECT_TYPE_CREATURE, resRefNPC2, GetLocation(OITEM));
        SetTag(NPC2, "CAS_NPC_PROJECTOR");
        SendMessageToPC(OPC, "Spawned NPC2 with tag CAS_NPC_PROJECTOR");
        // Only set lastSpeaker to NPC2 if it's the first NPC spawned
        if (lastSpeaker == OBJECT_INVALID)
        {
            lastSpeaker = NPC2;
        }
    }

    // Additional spawning code for other NPCs as needed ...


    // Retrieve the title string from the local variable
    string titleVar = GetLocalString(OITEM, "P_1B66_STITLE");
    object titleSpeaker = OBJECT_INVALID;

    // Check for the title speaker among all NPCs in the vicinity
    if (titleVar != "")
    {
        object potentialTitleSpeaker = GetFirstObjectInShape(SHAPE_SPHERE, 100.0, GetLocation(OITEM), TRUE);
        while (potentialTitleSpeaker != OBJECT_INVALID)
        {
            if (GetLocalInt(potentialTitleSpeaker, "P_SPEAKTITLE") == 1)
            {
                titleSpeaker = potentialTitleSpeaker; // This NPC will speak the title
                break;
            }
            potentialTitleSpeaker = GetNextObjectInShape(SHAPE_SPHERE, 100.0, GetLocation(OITEM), TRUE);
        }

        // If a title speaker is found, command them to speak the title after the delay
        if (titleSpeaker != OBJECT_INVALID)
        {
            DelayCommand(spawnDelay, AssignCommand(titleSpeaker, ActionSpeakString(titleVar)));
            lastSpeaker = titleSpeaker; // Update lastSpeaker to the title speaker
        }
        else
        {
            SendMessageToPC(OPC, "No NPC found to speak the title.");
        }
    }

    // Process the P_1b66_STEXT variables
    array a = GetLocalVariablesOfTypeWithPrefix(OITEM, VARIABLE_TYPE_STRING, "P_1b66_STEXT");
    int n = GetArraySize(a);
    SendMessageToPC(OPC, "Number of P_1b66_STEXT variables found: " + IntToString(n));


    int i;
    for (i = 1; i <= n; i++)
    {
    string SVARNAME = "P_1b66_STEXT" + IntToString(i);
    string dialogueLine = GetLocalString(OITEM, SVARNAME);
    if (dialogueLine == "")
        continue; // Skip empty variables

    string firstLetter = GetStringLeft(dialogueLine, 1);
    object speaker = OBJECT_INVALID;

    // Find all NPCs in the vicinity that could potentially speak
    object potentialSpeaker = GetFirstObjectInShape(SHAPE_SPHERE, 100.0, GetLocation(OITEM), TRUE);
    while (potentialSpeaker != OBJECT_INVALID)
    {
        // Check if this NPC has a variable indicating it should speak lines starting with the current firstLetter
        string speaksVarName = "P_SPEAKS_" + firstLetter;
        int canSpeak = GetLocalInt(potentialSpeaker, speaksVarName);
        if (canSpeak != 0)
        {
            speaker = potentialSpeaker; // This NPC will speak the line
            break;
        }
        potentialSpeaker = GetNextObjectInShape(SHAPE_SPHERE, 100.0, GetLocation(OITEM), TRUE);
    }

    // If no specific NPC found, default to the last speaker
    if (speaker == OBJECT_INVALID && lastSpeaker != OBJECT_INVALID)
        speaker = lastSpeaker;
    else if (speaker != OBJECT_INVALID)
        lastSpeaker = speaker; // Update the last speaker

    // Make the NPC speak the line after a delay
    if (speaker != OBJECT_INVALID)
    {
        float FDELAY = GetLocalFloat(OITEM, "P_1b66_FTIME") + (45.0 * i);
        DelayCommand(FDELAY, AssignCommand(speaker, ActionSpeakString(dialogueLine)));
    }
    else
    {
        // If no NPC can be found to speak the line, log an error
        SendMessageToPC(OPC, "No NPC found to speak the line: " + dialogueLine);
    }
}

SendMessageToPC(OPC, "Script completed.");

}

