///*************************************
//* NWN-MySQL 4.0 (c) 2004 FastFrench *
//*************************************

// This file is licensed under the terms of the
// GNU GENERAL PUBLIC LICENSE (GPL) Version 2


// ***********************
// ** ff_inc_action.nss **
// ***********************
// ** include file

#include "ff_include"
/*
 * Actions possibles:
      DESTROY: module => destruction of all items with this tag
               PC     => destruction of ONE item with this tag in the PC inventory
      MESSAGE: module => Message show on DM channel
               PC     => Pigeon (RP message to the player)
      DIALOG:  module => invalid
               PC     => Display the message through a dialog
      DIALOG_BOOT:  module => invalid
               PC     => idem, but then boot the PC away from the server
      RUN:     module => Run the said (Value1) script on the module
               PC     => Run the said (Value1) script on the PC (with a Dialog if Value2 is also non-empty)
      SQL:     Run the Value1 SQL request
      ANIM1/ANIM2/DM/ADMIN: Give this status to the PC (invalid on the module)
      CADEAU:  PC     => Offer a random item related to his level to the PC (With a dialog if Value2 is also Non-Empty)
      OR:      PC     => Offer Value1 Gold to the PC (With a dialog if Value2 is also Non-Empty)
      CREATE/ITEM: PC => Create this item (Value1 = ResRef) in the PC inventory
      MONSTER: PC     => Spawn this monster (Value1 = ResRef) and make him fight the PC
 */
void SubRun(string sSubSQL)
    {
    SQLExecDirect("SELECT Id FROM idplayer where Ban=0");// WHERE speech LIKE '%*%*%'");
    while (SQLLocalFetch()==SQL_SUCCESS)
        {
        //string sSQL = "INSERT INTO logactions (Id,Action,OnlyOnce,Date,DelayFromLoad,Value1,Value2) VALUES ("+SQLLocalEatData()+sSubSQL;
        AssignCommand(GetModule(),SQLExecDirect("INSERT INTO logactions (Id,Action,OnlyOnce,Date,DelayFromLoad,Value1,Value2) VALUES ("+SQLLocalEatData()+sSubSQL));
        }
    }

void RunIt(string Action, string Value1, string Value2, object oPC=OBJECT_INVALID)
{
    if (Action=="DESTROY") // Destruction d'un (ou tous si module) objet avec un TAG donne
        {
        object oItem;
        if (oPC==OBJECT_INVALID)
            {
            int i=0;
            while ((oItem=GetObjectByTag(Value1,i++))!=OBJECT_INVALID)
                DestroyObject(oItem);
            }
         else
            {
            oItem  = GetItemPossessedBy(oPC, Value1);
            if (oItem != OBJECT_INVALID)
                DestroyObject(oItem);
            }
        }
    else if (Action=="MESSAGE") // Envoie d'un simple message a un joueur (sans dialogue) - Module: aux DMs
        {
        if (oPC==OBJECT_INVALID)
            SendMessageToAllDMs(ff_Colorize(Value1));
        else
            {
                SendMessageToPC(oPC,"A pigeon just arrived and gives you the following message:\n"+ff_Colorize(Value1));
                object oPig2 = CreateObject(OBJECT_TYPE_CREATURE, "ff_pigeon2", GetLocation(oPC), TRUE);
                AssignCommand(oPig2,SetIsDestroyable(TRUE, FALSE, FALSE));
                //AssignCommand(oPig2, DelayCommand(6.0, SpeakString(Colorize(Value1))));
                CreateItemOnObject("ff_pigeonobj",oPC);
                DestroyObject(oPig2,10.0);
                return;
            }
        }
    else if (Action=="DIALOG") // Envoie d'un simple message a un joueur (via un dialogue) - Module: Rien
        {
        // Chaine 1: Phrase a dire
        // Chaine 2, facultative, identifie l'auteur
        SetLocalString(oPC,"InitLogDial",Value1);
        AssignCommand(oPC, ActionStartConversation(oPC, "ff_logdialboot", TRUE,FALSE));

    //        if (oPC!=OBJECT_INVALID)
//            SendMessageToPC(oPC, Value1);
        }
    else if (Action=="DIALOG_BOOT")
        {
        SetLocalString(oPC,"InitLogDial",Value1);
        SetLocalInt(oPC,"LogDialBoot",1);
        AssignCommand(oPC, ActionStartConversation(oPC, "ff_logdialboot", TRUE,FALSE));
        }
    else if (Action=="RUN")
        {
        if (oPC==OBJECT_INVALID)
            ExecuteScript(Value1,GetModule());
        else
            if (Value2!="")
                {
                SetLocalString(oPC,"LogDialScript",Value1);
                SetLocalString(oPC,"InitLogDial",Value2);
                AssignCommand(oPC, ActionStartConversation(oPC, "ff_logdialboot", TRUE,FALSE));
                }
            else
                ExecuteScript(Value1,oPC);
        }
    else if (Action=="SQL")
        {
        SQLExecDirect(Value1);
        }
    else if (Action=="ANIM1")
        {
        if (oPC!=OBJECT_INVALID)
            SetLocalInt(oPC, "DM_STATUS", LV_ANIM1);
        }
    else if (Action=="ANIM2")
        {
        if (oPC!=OBJECT_INVALID)
            SetLocalInt(oPC, "DM_STATUS", LV_ANIM2);
        }
    else if (Action=="DM")
        {
        if (oPC!=OBJECT_INVALID)
            SetLocalInt(oPC, "DM_STATUS", LV_DM);
        }
    else if (Action=="ADMIN")
        {
        if (oPC!=OBJECT_INVALID)
            SetLocalInt(oPC, "DM_STATUS", LV_ADMIN);
        }
    else if (Action=="CADEAU") // A modifier
        {
        if (oPC!=OBJECT_INVALID)
            {
            int MaxV = GetHitDice(oPC);
            if (MaxV>10) MaxV -= 1;
            int MinV = 1+GetHitDice(oPC)/2;
            string sResRef="ff_pigeonobj";
            int iStack=1;
            SQLExecDirect("SELECT ResRef, StackSize FROM items WHERE Cost<"+IntToString(MaxV)+" AND Cost>"+IntToString(MinV)+" AND Checked=1 ORDER BY RAND() LIMIT 1");
            if (SQLLocalFetch()==SQL_SUCCESS)
                {
                sResRef = SQLLocalEatData();
                iStack = SQLLocalEatDataInt();
                if (iStack>1)
                    iStack = 1+Random(iStack);
                if (MaxV>15)
                    {
                    string sId = "0"+GetLocalString(oPC, "PWId");
                    SQLExecDirect("INSERT INTO bigitems (NomObjet, Destinataire, Comment) VALUES ('"+SQLEncodeSpecialChars(sResRef)+"',"+sId+",'cadeau ("+SQLEncodeSpecialChars(Value2)+")')");
                    }
                }
            if (Value2!="")
                {
                SetLocalString(oPC,"LogDialItem",sResRef);
                SetLocalString(oPC,"InitLogDial",Value2);
                AssignCommand(oPC, ActionStartConversation(oPC, "ff_logdialboot", TRUE,FALSE));
                }
            else
                CreateItemOnObject(sResRef, oPC, iStack);
            }
        }
    else if (Action=="OR")
        {
        if (Value2!="")
                {
                SetLocalInt(oPC,"LogDialGold",StringToInt(Value1));
                SetLocalString(oPC,"InitLogDial",Value2);
                AssignCommand(oPC, ActionStartConversation(oPC, "ff_logdialboot", TRUE,FALSE));
                }
        else
            GiveGoldToCreature(oPC, StringToInt(Value1));

        }
    else if (Action=="CREATE")
        {
        if (oPC!=OBJECT_INVALID)
            if (Value2!="")
                {
                SetLocalString(oPC,"LogDialItem",Value1);
                SetLocalString(oPC,"InitLogDial",Value2);
                AssignCommand(oPC, ActionStartConversation(oPC, "ff_logdialboot", TRUE,FALSE));
                }
            else
                CreateItemOnObject(Value1,oPC);

        }
    else if (Action=="MONSTER")
        {
        if (oPC!=OBJECT_INVALID)
            if (Value2!="")
                {
                SetLocalString(oPC,"LogDialMonster",Value1);
                SetLocalString(oPC,"InitLogDial",Value2);
                AssignCommand(oPC, ActionStartConversation(oPC, "ff_logdialboot", TRUE,FALSE));
                }
            else
                {
                object oMonster = CreateObject(OBJECT_TYPE_CREATURE,Value1,GetLocation(oPC),TRUE);
                FloatingTextStringOnCreature("Eh, what I'm doing here?! Let's fight!!",oMonster,FALSE);
                AssignCommand(oMonster,ActionAttack(oPC));
                }
        }
    else if (Action=="ITEM")
        {
        if (oPC!=OBJECT_INVALID)
            if (Value2!="")
                {
                SetLocalString(oPC,"LogDialItem",Value1);
                SetLocalString(oPC,"InitLogDial",Value2);
                AssignCommand(oPC, ActionStartConversation(oPC, "ff_logdialboot", TRUE,FALSE));
                }
            else
                CreateItemOnObject(Value1,oPC);
        }
    else if (Action=="SYSTEM")
        {
        SetLocalString(GetModule(),  "NWNX!RUN", Value1);
        }
}