////////////////////////////////////////////////////////////////////////////////
// rts_it_op16 - Bag of Tricks Artifact
// By Deva B. Winblood.  October 18th, 2008
////////////////////////////////////////////////////////////////////////////////


void fnWatch(object oNPC,float fDuration)
{ // PURPOSE: Follow and support master
    object oMaster=GetLocalObject(oNPC,"oMaster");
    if (GetIsObjectValid(oMaster)&&!GetIsDead(oMaster)&&fDuration>0.0&&GetCurrentAction(oMaster)!=ACTION_REST)
    { // follow
        if (GetArea(oMaster)==GetArea(oNPC))
        { // same area
            if (GetDistanceBetween(oMaster,oNPC)>15.0&&!GetIsDMPossessed(oNPC))
            { // move
                AssignCommand(oNPC,ClearAllActions(TRUE));
                AssignCommand(oNPC,ActionMoveToObject(oMaster,TRUE,5.0));
            } // move
            else if (GetDistanceBetween(oMaster,oNPC)>4.0)
            { // move but, already close
                if (!GetIsInCombat(oNPC)&&!GetIsDMPossessed(oNPC))
                { // move okay
                    object oEnemy=GetNearestCreature(CREATURE_TYPE_IS_ALIVE,TRUE,oNPC,CREATURE_TYPE_REPUTATION,REPUTATION_TYPE_ENEMY,CREATURE_TYPE_PERCEPTION,PERCEPTION_SEEN);
                    if (GetIsObjectValid(oEnemy)&&GetDistanceBetween(oEnemy,oMaster)<15.001)
                    { // attack
                        AssignCommand(oNPC,ActionAttack(oEnemy));
                    } // attack
                    else
                    { // move
                        AssignCommand(oNPC,ClearAllActions(TRUE));
                        AssignCommand(oNPC,ActionMoveToObject(oMaster,TRUE,3.0));
                    } // move
                } // move okay
            } // move but, already close
        } // same area
        else if (!GetIsDMPossessed(oNPC))
        { // different area
            AssignCommand(oNPC,ClearAllActions(TRUE));
            AssignCommand(oNPC,JumpToObject(oMaster));
        } // different area
        DelayCommand(3.0,fnWatch(oNPC,fDuration-3.0));
    } // follow
    else
    { // unsummon
        effect eVFX=EffectVisualEffect(VFX_IMP_UNSUMMON);
        ApplyEffectAtLocation(DURATION_TYPE_INSTANT,eVFX,GetLocation(oNPC));
        DelayCommand(1.0,DestroyObject(oNPC));
    } // unsummon
} // fnWatch()



///////////////////////////////////////////////////////////[ MAIN ]/////////////
void main()
{
   object oItem=GetItemActivated();
   object oPC=GetItemActivator();
   int nBeans=GetLocalInt(oPC,"nBeans");
   string sRR;
   int nR;
   effect eVFX;
   object oCr;

   if (nBeans==0)
   { // set beans
       nBeans=6+d20();
       SetLocalInt(oPC,"nBeans",nBeans);
   } // set beans
   nR=d100();
   nBeans=nBeans-1;
   SendMessageToPC(oPC,"You pull a bean from the bag and drop it onto the ground.");
   if (nBeans>0) SetLocalInt(oPC,"nBeans",nBeans);
   else
   { // last bean used
       DestroyObject(oItem);
       SendMessageToPC(oPC,"You use the last bean and the bag vanishes.");
   } // last bean used
   if (nR<26) sRR="sum1cave2";
   else if (nR<41) sRR="spidemergm2";
   else if (nR<51) sRR="spidemergm1";
   else if (nR<61) sRR="undemergm2";
   else if (nR<71) sRR="undemergm1";
   else if (nR<76) sRR="sum4le22";
   else if (nR<81) sRR="sum1bean";
   else if (nR<86) sRR="sum4bean";
   else if (nR<91) sRR="sum2bean";
   else if (nR<96) sRR="sum3bean";
   else if (nR<98) sRR="sum6ce2";
   else { sRR="sum6le2"; }
   eVFX = EffectVisualEffect(VFX_DUR_TENTACLE);
   ApplyEffectAtLocation(DURATION_TYPE_TEMPORARY,eVFX,GetItemActivatedTargetLocation(),3.0);
   oCr=CreateObject(OBJECT_TYPE_CREATURE,sRR,GetItemActivatedTargetLocation());
   string sID=GetLocalString(oPC,"sTeamID");
   object oProxy=GetObjectByTag(sID+"_PROXY");
   if (!GetIsObjectValid(oProxy)) SendMessageToPC(oPC,"Proxy "+sID+"_PROXY not found.");
   ChangeFaction(oCr,oProxy);
   SetIsTemporaryFriend(oCr,oPC);
   SetIsTemporaryFriend(oPC,oCr);
   SetLocalObject(oCr,"oMaster",oPC);
   DelayCommand(1.0,fnWatch(oCr,HoursToSeconds(10)));
}
///////////////////////////////////////////////////////////[ MAIN ]/////////////