// rts_it_op17pl - Simulacrum Vat Placeable Heartbeat
#include "rtsh_multiplay"


void fnCloneAI(object oClone)
{ // PURPOSE: Handle clone movement and behavior
    object oEnemy;
    if (!GetIsInCombat(oClone))
    { // move
        oEnemy=GetNearestCreature(CREATURE_TYPE_IS_ALIVE,TRUE,oClone,1,CREATURE_TYPE_PERCEPTION,PERCEPTION_SEEN,CREATURE_TYPE_REPUTATION,REPUTATION_TYPE_ENEMY);
        if (!GetIsObjectValid(oEnemy))
        { // move still
            if (GetCurrentAction(oClone)!=ACTION_MOVETOPOINT)
            { // move
                oEnemy=GetNearestObject(OBJECT_TYPE_WAYPOINT,oClone,d20(2));
                if (GetIsObjectValid(oEnemy))
                { // waypoint found
                    AssignCommand(oClone,ClearAllActions());
                    AssignCommand(oClone,ActionForceMoveToObject(oEnemy,FALSE,4.0,60.0));
                } // waypoint found
            } // move
        } // move still
    } // move
    DelayCommand(4.0,fnCloneAI(oClone));
} // fnCloneAI()


void main()
{
   object oMe=OBJECT_SELF;
   string sID=GetLocalString(oMe,"sTeamID");
   object oClone=GetLocalObject(oMe,"oClone");
   object oLeader=GetLocalObject(GetModule(),"oTeamLead"+sID);
   int nMana;
   int nLevel;
   if (GetIsObjectValid(oLeader))
   { // leader exists
       nMana=fnGetTeamMana(oLeader);
       nLevel=GetLevelByPosition(1,oLeader)+GetLevelByPosition(2,oLeader)+GetLevelByPosition(3,oLeader);
       if (!GetIsObjectValid(oClone))
       { // no clone exists
           nLevel=100+(nLevel*10);
           if (nMana>=nLevel)
           { // have enough mana
               nMana=nMana-nLevel;
               fnSetTeamMana(oLeader,nMana);
               oClone=CopyObject(oLeader,GetLocation(oMe),OBJECT_INVALID,sID+"_CLONE");
               SetName(oClone,GetName(oLeader));
               SetLocalObject(oClone,"oOriginal",oLeader);
               SetLocalObject(oLeader,"oClone",oClone);
               SetLocalObject(oMe,"oClone",oClone);
               oLeader=GetObjectByTag(sID+"_PROXY");
               if (GetIsObjectValid(oLeader)) ChangeFaction(oClone,oLeader);
               AssignCommand(oClone,ClearAllActions(TRUE));
               AssignCommand(oClone,fnCloneAI(oClone));
           } // have enough mana
       } // no clone exists
       else
       { // clone exists
           if (GetLocalObject(oClone,"oOriginal")!=oLeader)
           { // leader has changed - destroy clone
               DestroyObject(oClone);
           } // leader has changed - destroy clone
       } // clone exists
   } // leader exists
}