/////////////////////////////////////////////////////////////////////////////////
// mai_challenge - This is a more advanced AI routine for Computer Controlled Teams
// By Deva Bryson Winblood.  04/13/2004
// This is a modified version supporting custom settings by the game settings console
// Modifications created: 02/04/2005 By Deva Bryson Winblood.
////////////////////////////////////////////////////////////////////////////////
#include "ai_header"
////////////////////////
// PROTOTYPES
////////////////////////
int fnLevel(object oOb);
string fnReturnUnitRes(string sID,string sPos,int nLLevel);
int fnGetGold(string sID);
void fnSetGold(string sID,int nGold);
void fnSpendGold(string sID,string sPID);

void fnMAIReport(string sMsg)
{ // PURPOSE: Log the message to the file
  PrintString("(mai_challenge) report: "+sMsg);
} // fnMAIReport()

void fnBird(object oBird,string sTeamID,object oLeader)
{
    object oMod=GetModule();
    //SendMessageToPC(GetFirstPC(),"fnBird("+GetName(oBird)+","+sTeamID+","+GetName(oLeader)+")");
    SetLocalString(oBird,"sTeamID",sTeamID);
    SetLocalInt(oBird,"nMState",22);
    SetLocalInt(oBird,"nSState",0);
    ChangeFaction(oBird,oLeader);
    SetLocalObject(oMod,"oAISBird"+sTeamID,oBird);
    SetAILevel(oBird,AI_LEVEL_NORMAL);
}

void fnRaid(object oOb,string sRaid,int nParm)
{
  DeleteLocalInt(oOb,"nSState");
  SetLocalString(oOb,"sTeamToRaid",sRaid);
  SetLocalInt(oOb,"nParm",nParm); // attack
  SetLocalInt(oOb,"nMState",17);
  SetLocalInt(oOb,"nRun",TRUE);
  //AssignCommand(oOb,SpeakString("*raid "+sRaid+"*"));
}

void fnHireMerc(string sTeamID,string sPID)
{
  object oMod=GetModule();
  int nGold=fnGetGold(sTeamID);
  int nBase=0;
  int nEnd=0;
  int nCost=0;
  int nL=1;
  string sRaid="NA";
  object oOb;
  object oMerc;
  object oTMerc1=GetLocalObject(oMod,"oTMerc1"+sTeamID); // Team mercenary 1
  object oTMerc2=GetLocalObject(oMod,"oTMerc2"+sTeamID); // Team mercenary 2
    while(nL<4)
    { // check mercenary prices
      oOb=GetLocalObject(oMod,"oMerL"+IntToString(nL)+sTeamID);
      if (oOb!=OBJECT_INVALID)
      { // know this merchant
        if (GetLocalInt(oMod,"nMerLC"+IntToString(nL)+"_1"+sTeamID)<nGold)
        { // have enough gold
          if (GetLocalInt(oMod,"nMerLC"+IntToString(nL)+"_1"+sTeamID)>nCost)
          { nBase=nL; nEnd=1; nCost=GetLocalInt(oMod,"nMerLC"+IntToString(nL)+"_1"+sTeamID);}
        } // have enough gold
        if (GetLocalInt(oMod,"nMerLC"+IntToString(nL)+"_2"+sTeamID)<nGold)
        { // have enough gold
          if (GetLocalInt(oMod,"nMerLC"+IntToString(nL)+"_2"+sTeamID)>nCost)
          { nBase=nL; nEnd=2; nCost=GetLocalInt(oMod,"nMerLC"+IntToString(nL)+"_2"+sTeamID); }
        } // have enough gold
      } // know this merchant
      nL++;
    } // check mercenary prices
    if (nCost>0)
    { // can hire a merc
      sRaid=GetLocalString(oMod,"sMerL"+IntToString(nBase)+"_"+IntToString(nEnd)+sTeamID);
      oOb=GetLocalObject(oMod,"oMerL"+IntToString(nBase));
      if (oOb!=OBJECT_INVALID)
      { // create mercenary
        nGold=nGold-nCost;
        fnSetGold(sTeamID,nGold);
        oMerc=CreateObject(OBJECT_TYPE_CREATURE,sRaid,GetLocation(oOb));
        SetLocalString(oMerc,"sTeamID",sTeamID);
        SetLocalString(oMerc,"sTeamToRaid",sPID);
        oOb=GetWaypointByTag(sTeamID+"_RESOURCES");
        oOb=GetObjectByTag(sTeamID+"_PROXY");
        ChangeFaction(oMerc,oOb);
        SetLocalInt(oMerc,"nMState",17);
        SetLocalInt(oMerc,"nParm",1);
        if (oTMerc1==OBJECT_INVALID) SetLocalObject(oMod,"oTMerc1"+sTeamID,oMerc);
        else {SetLocalObject(oMod,"oTMerc2"+sTeamID,oMerc); }
      } // create mercenary
    } // can hire a merc
}
//int fnSpawnDelay(int nLevel);

///////////////////////////////////////////////////////////////////////// MAIN
void main()
{
  object oLeader=OBJECT_SELF;
  string sTeamID=GetLocalString(oLeader,"sTeamID");
  //SendMessageToPC(GetFirstPC(),"sTeamID="+sTeamID);
  object oMod=GetModule();
  int nPauseBetweenCreate=1;
  int nMVD=0; // mana vault guard
  int nTRD=0; // throne room guard
  int nLD1=0; // Lair Defense 1 Guard
  int nLD2=0; // Lair Defense 2 Guard
  int nHU=0;  // Harvest unit
  int nIC=0;  // Item Capture Unit
  int nRA=0;  // Raid Attack Unit
  int nRM=0;  // Raid Mana Unit
  int nRG=0;  // Raid Gold Unit
  int nPR=0;  // Power Reservoir capture unit
  int nRL=0;  // Level which raids are okay
  int nPRL=0; // Level at which power reservoir units are okay
  int nCPL=0; // Level at which control point capture is okay
  int nCPC=0; // Control point capture
  int nUCS=3; // Unit create speed - bigger = faster
  object oTMerc1=GetLocalObject(oMod,"oTMerc1"+sTeamID); // Team mercenary 1
  object oTMerc2=GetLocalObject(oMod,"oTMerc2"+sTeamID); // Team mercenary 2
  string sPID=""; // Primary enemy ID
  string sMVD="";
  string sTRD="";
  string sLD1="";
  string sLD2="";
  string sCPC="";
  string sHU="";
  string sIC="";
  string sRA="";
  string sRM="";
  string sRG="";
  string sPR="";
  int nAIMode=0;
  object oCP;
  object oLD1=GetWaypointByTag(sTeamID+"_LD1");
  object oLD2=GetWaypointByTag(sTeamID+"_LD2");
  object oMVD=GetWaypointByTag(sTeamID+"_VAULT");
  object oTRD=GetWaypointByTag(sTeamID+"_START");
  string sPre="oAdvAIUnit"+sTeamID;
  int nReturnState=13; // return mana or item state
  int nCN=0; // create number
  int nUN=0; // upgrade number
  int nUC=0; // units created (only 4 per tic)
  int nRC=0; // count of raid units waiting to go on raid
  int nUAL=0; // Units allowed
  int nL;
  int nBase;
  int nEnd;
  int nCost;
  int nGold;
  object oMerc;
  object oOb;
  string sRaid;
  int nLeaderLevel=fnLevel(oLeader);
  int nAIC=GetLocalInt(oMod,"nAIC"+sTeamID);
  object oEnemy=GetNearestCreature(CREATURE_TYPE_REPUTATION,REPUTATION_TYPE_ENEMY,oLeader,1,CREATURE_TYPE_PLAYER_CHAR,PLAYER_CHAR_IS_PC);
  int nSpawnRes=GetLocalInt(oMod,"nAISpawnRes"+sTeamID); // spawn resource amount
  /*object oBird=GetLocalObject(oMod,"oAISBird"+sTeamID);
  if (oBird==OBJECT_INVALID||GetIsDead(oBird)==TRUE)
  { // scavenger bird
    oBird=CreateObject(OBJECT_TYPE_CREATURE,"scavbird",GetLocation(oLD1));
    DelayCommand(1.0,fnBird(oBird,sTeamID,oLeader));
  } // scavenger bird */
  fnMAIReport(GetName(oLeader)+"  Level:"+IntToString(nLeaderLevel));
  fnMAIReport("   nAIC:"+IntToString(nAIC)+"  nSpawnRes:"+IntToString(nSpawnRes));
  nGold=fnGetGold(sTeamID);
  if (nGold>(nLeaderLevel*1000)) fnSetGold(sTeamID,nLeaderLevel*1000);
  if (oEnemy==OBJECT_INVALID)
  { // no enemies nearby
  if (GetLocalInt(oMod,"nAIPauseSetting")>0) nPauseBetweenCreate=GetLocalInt(oMod,"nAIPauseSetting");
  if (GetLocalInt(oMod,"nAIUnitCreateSpeed")>0) nUCS=GetLocalInt(oMod,"nAIUnitCreateSpeed");
  nAIC++;
  if (nAIC>=nPauseBetweenCreate)
  { // AI ok
  nSpawnRes=nSpawnRes+nUCS;
  nUAL=nSpawnRes/nLeaderLevel;
  if (nUAL>nUCS) nUAL=nUCS;
  if (nUAL>=1)
  { // enough resources to spawn
  DeleteLocalInt(oMod,"nAIC"+sTeamID);
  if (oLD1==OBJECT_INVALID) SendMessageToPC(GetFirstPC(),sTeamID+"_LD1 waypoint not found.");
  if (oLD2==OBJECT_INVALID) SendMessageToPC(GetFirstPC(),sTeamID+"_LD2 waypoint not found.");
  if (oMVD==OBJECT_INVALID) SendMessageToPC(GetFirstPC(),sTeamID+"_VAULT waypoint not found.");
  if (oTRD==OBJECT_INVALID) SendMessageToPC(GetFirstPC(),sTeamID+"_START waypoint not found.");
  nMVD=GetLocalInt(oMod,"n"+sTeamID+"_nCAIMVD");
  nTRD=GetLocalInt(oMod,"n"+sTeamID+"_nCAITRD");
  nLD1=GetLocalInt(oMod,"n"+sTeamID+"_nCAILD1");
  nLD2=GetLocalInt(oMod,"n"+sTeamID+"_nCAILD2");
  nCPC=GetLocalInt(oMod,"n"+sTeamID+"_nCAICPC");
  nHU=GetLocalInt(oMod,"n"+sTeamID+"_nCAIHU");
  nIC=GetLocalInt(oMod,"n"+sTeamID+"_nCAIIC");
  nRA=GetLocalInt(oMod,"n"+sTeamID+"_nCAIRA");
  nRM=GetLocalInt(oMod,"n"+sTeamID+"_nCAIRM");
  nRG=GetLocalInt(oMod,"n"+sTeamID+"_nCAIRG");
  nPR=GetLocalInt(oMod,"n"+sTeamID+"_nCAIPR");
  nAIMode=GetLocalInt(oMod,"n"+sTeamID+"_CAI_Setting");
  sPID=GetLocalString(oMod,"s"+sTeamID+"_CAI_PE");
  sMVD=fnReturnUnitRes(sTeamID,"MVD",nLeaderLevel);
  sTRD=fnReturnUnitRes(sTeamID,"TRD",nLeaderLevel);
  sLD1=fnReturnUnitRes(sTeamID,"LD1",nLeaderLevel);
  sLD2=fnReturnUnitRes(sTeamID,"LD2",nLeaderLevel);
  sCPC=fnReturnUnitRes(sTeamID,"CPC",nLeaderLevel);
  sHU=fnReturnUnitRes(sTeamID,"HU",nLeaderLevel);
  sIC=fnReturnUnitRes(sTeamID,"IC",nLeaderLevel);
  sRA=fnReturnUnitRes(sTeamID,"RA",nLeaderLevel);
  sRM=fnReturnUnitRes(sTeamID,"RM",nLeaderLevel);
  sRG=fnReturnUnitRes(sTeamID,"RG",nLeaderLevel);
  sPR=fnReturnUnitRes(sTeamID,"PR",nLeaderLevel);
  if (GetLocalInt(oMod,"bAIOFF8")!=TRUE&&GetLocalInt(oMod,"nMaxUnits")>40)
  { // scale the units
    nCost=GetLocalInt(oMod,"nMaxUnits");
    nL=1;
    if (nCost==80) nL=2;
    else if (nCost==100) nL=3;
    else if (nCost==120) nL=4;
    if (nL>1)
    { // scale the units
      nMVD=nMVD*nL;
      nTRD=nTRD*nL;
      nLD1=nLD1*nL;
      nLD2=nLD2*nL;
      nCPC=nCPC*nL;
      nRA=nRA*nL;
      nRM=nRM*nL;
      nRG=nRG*nL;
    } // scale the units
  } // scale the units
  if (GetLocalInt(oMod,"bAIOFF6")!=TRUE&&(oTMerc1==OBJECT_INVALID||oTMerc2==OBJECT_INVALID))
  { // hire mercenary if can - mercenaries ALWAYS attack raid primary target
    DelayCommand(2.0,fnHireMerc(sTeamID,sPID));
  } // hire mercenary if can - mercenaries ALWAYS attack raid primary target
  if (GetLocalInt(oMod,"bAIOFF7")!=TRUE)
  {
    DelayCommand(4.0,fnSpendGold(sTeamID,sPID));
  }
  if (GetStringLength(sMVD)>3)
  { // MVD
    nBase=1;
    nEnd=nBase+nMVD;
    nCN=0;
    nUN=0;
    nL=nBase;
    while(nL<nEnd)
    { // check units
      oOb=GetLocalObject(oMod,sPre+IntToString(nL));
      if (oOb==OBJECT_INVALID||GetIsDead(oOb)==TRUE) nCN=nL;
      else
      { // see if upgrade
        if(GetTag(oOb)!=sMVD&&GetArea(oOb)==GetArea(oTRD)) nUN=nL;
        //AssignCommand(oOb,SpeakString("*MVD*"));
        if (GetLocalInt(oOb,"nMState")!=21) { SetLocalInt(oOb,"nMState",21); SetLocalObject(oOb,"oDestWP",oMVD); }
      } // see if upgrade
      nL++;
    } // check units
    if (nCN!=0&&nUC<nUAL)
    { // create unit
      nSpawnRes=nSpawnRes-nLeaderLevel;
      oOb=fnAICreateUnit(sTeamID,sMVD,GetLocation(oMVD),nCN);
      fnMAIReport("  create MVD:"+GetName(oOb));
      if (oOb!=OBJECT_INVALID)
      { nCN=0; nUC++; SetLocalInt(oOb,"nMState",21); SetLocalObject(oOb,"oDestWP",oMVD);}
    } // create unit
    if (nUN!=0)
    { // upgrade unit
      oOb=GetLocalObject(oMod,sPre+IntToString(nUN));
      if (oOb!=OBJECT_INVALID) fnAIUpgradeUnit(oOb,sMVD);
      nUN=0;
    } // upgrade unit
  } // MVD
  if (GetStringLength(sTRD)>3)
  { // TRD
    nBase=nMVD+1;
    nEnd=nBase+nTRD;
    nCN=0;
    nUN=0;
    nL=nBase;
    while(nL<nEnd)
    { // check units
      oOb=GetLocalObject(oMod,sPre+IntToString(nL));
      if (oOb==OBJECT_INVALID||GetIsDead(oOb)==TRUE) nCN=nL;
      else
      { // see if upgrade
        if(GetTag(oOb)!=sTRD&&GetArea(oOb)==GetArea(oTRD)) nUN=nL;
        //AssignCommand(oOb,SpeakString("*TRD*"));
        if (GetLocalInt(oOb,"nMState")!=21) { SetLocalInt(oOb,"nMState",21); SetLocalObject(oOb,"oDestWP",oTRD); }
      } // see if upgrade
      nL++;
    } // check units
    if (nCN!=0&&nUC<nUAL)
    { // create unit
      nSpawnRes=nSpawnRes-nLeaderLevel;
      oOb=fnAICreateUnit(sTeamID,sTRD,GetLocation(oTRD),nCN);
      fnMAIReport("  create TRD:"+GetName(oOb));
      if (oOb!=OBJECT_INVALID)
      { nCN=0; nUC++; SetLocalInt(oOb,"nMState",21); SetLocalObject(oOb,"oDestWP",oTRD);}
    } // create unit
    if (nUN!=0)
    { // upgrade unit
      oOb=GetLocalObject(oMod,sPre+IntToString(nUN));
      if (oOb!=OBJECT_INVALID) fnAIUpgradeUnit(oOb,sTRD);
      nUN=0;
    } // upgrade unit
  } // TRD
  if (GetStringLength(sLD1)>3)
  { // LD1
    nBase=nMVD+nTRD+1;
    nEnd=nBase+nLD1;
    nCN=0;
    nUN=0;
    nL=nBase;
    while(nL<nEnd)
    { // check units
      oOb=GetLocalObject(oMod,sPre+IntToString(nL));
      if (oOb==OBJECT_INVALID||GetIsDead(oOb)==TRUE) nCN=nL;
      else
      { // see if upgrade
        if(GetTag(oOb)!=sLD1&&GetArea(oOb)==GetArea(oTRD)) nUN=nL;
        //AssignCommand(oOb,SpeakString("*LD1*"));
        if (GetLocalInt(oOb,"nMState")!=21) { SetLocalInt(oOb,"nMState",21); SetLocalObject(oOb,"oDestWP",oLD1); }
      } // see if upgrade
      nL++;
    } // check units
    if (nCN!=0&&nUC<nUAL)
    { // create unit
      nSpawnRes=nSpawnRes-nLeaderLevel;
      oOb=fnAICreateUnit(sTeamID,sLD1,GetLocation(oLD1),nCN);
      fnMAIReport("  create LD1:"+GetName(oOb));
      if (oOb!=OBJECT_INVALID)
      { nCN=0; nUC++; SetLocalInt(oOb,"nMState",21); SetLocalObject(oOb,"oDestWP",oLD1);}
    } // create unit
    if (nUN!=0)
    { // upgrade unit
      oOb=GetLocalObject(oMod,sPre+IntToString(nUN));
      if (oOb!=OBJECT_INVALID) fnAIUpgradeUnit(oOb,sLD1);
      nUN=0;
    } // upgrade unit
  } // LD1
  if (GetStringLength(sLD2)>3)
  { // LD2
    nBase=nMVD+nTRD+nLD1+1;
    nEnd=nBase+nLD2;
    nCN=0;
    nUN=0;
    nL=nBase;
    while(nL<nEnd)
    { // check units
      oOb=GetLocalObject(oMod,sPre+IntToString(nL));
      if (oOb==OBJECT_INVALID||GetIsDead(oOb)==TRUE) nCN=nL;
      else
      { // see if upgrade
        if(GetTag(oOb)!=sLD2&&GetArea(oOb)==GetArea(oTRD)) nUN=nL;
        //AssignCommand(oOb,SpeakString("*LD2*"));
        if (GetLocalInt(oOb,"nMState")!=21) { SetLocalInt(oOb,"nMState",21); SetLocalObject(oOb,"oDestWP",oLD2); }
      } // see if upgrade
      nL++;
    } // check units
    if (nCN!=0&&nUC<nUAL)
    { // create unit
      nSpawnRes=nSpawnRes-nLeaderLevel;
      oOb=fnAICreateUnit(sTeamID,sLD2,GetLocation(oLD2),nCN);
      fnMAIReport("  create LD2:"+GetName(oOb));
      if (oOb!=OBJECT_INVALID)
      { nCN=0; nUC++; SetLocalInt(oOb,"nMState",21); SetLocalObject(oOb,"oDestWP",oLD2);}
    } // create unit
    if (nUN!=0)
    { // upgrade unit
      oOb=GetLocalObject(oMod,sPre+IntToString(nUN));
      if (oOb!=OBJECT_INVALID) fnAIUpgradeUnit(oOb,sLD2);
      nUN=0;
    } // upgrade unit
  } // LD2
  if (GetStringLength(sCPC)>3&&GetLocalInt(oMod,"bAIOFF5")!=TRUE)
  { // CPC
    nBase=nMVD+nTRD+nLD1+nLD2+1;
    nEnd=nBase+nCPC;
    nCN=0;
    nUN=0;
    nL=nBase;
    while(nL<nEnd)
    { // check units
      oOb=GetLocalObject(oMod,sPre+IntToString(nL));
      if (oOb==OBJECT_INVALID||GetIsDead(oOb)==TRUE) nCN=nL;
      else
      { // see if upgrade
        if(GetTag(oOb)!=sCPC&&GetArea(oOb)==GetArea(oTRD)) nUN=nL;
        //AssignCommand(oOb,SpeakString("*CPC*"));
        if (GetLocalInt(oOb,"nMState")!=18) { SetLocalInt(oOb,"nMState",18); DeleteLocalInt(oOb,"nSState"); }
      } // see if upgrade
      nL++;
    } // check units
    if (nCN!=0&&nUC<nUAL)
    { // create unit
      nSpawnRes=nSpawnRes-nLeaderLevel;
      oOb=fnAICreateUnit(sTeamID,sCPC,GetLocation(oLD1),nCN);
      fnMAIReport("  create CPC:"+GetName(oOb));
      if (oOb!=OBJECT_INVALID)
      { nCN=0; nUC++; SetAILevel(oOb,AI_LEVEL_NORMAL); if (GetLocalInt(oOb,"nMState")!=18) { SetLocalInt(oOb,"nMState",18); DeleteLocalInt(oOb,"nSState"); }}
    } // create unit
    if (nUN!=0)
    { // upgrade unit
      oOb=GetLocalObject(oMod,sPre+IntToString(nUN));
      if (oOb!=OBJECT_INVALID) fnAIUpgradeUnit(oOb,sCPC);
      nUN=0;
    } // upgrade unit
  } // CPC
  if (GetStringLength(sHU)>3&&GetLocalInt(oMod,"bAIOFF1")!=TRUE)
  { // HU
    nBase=nMVD+nTRD+nLD1+nLD2+nCPC+1;
    nEnd=nBase+nHU;
    nCN=0;
    nUN=0;
    nL=nBase;
    while(nL<nEnd)
    { // check units
      oOb=GetLocalObject(oMod,sPre+IntToString(nL));
      if (oOb==OBJECT_INVALID||GetIsDead(oOb)==TRUE) nCN=nL;
      else
      { // see if upgrade
        if(GetTag(oOb)!=sHU&&GetArea(oOb)==GetArea(oTRD)&&GetLocalInt(oOb,"nSState")!=nReturnState) nUN=nL;
        //AssignCommand(oOb,SpeakString("*HU*"));
        if (GetLocalInt(oOb,"nMState")!=19) { SetLocalInt(oOb,"nMState",19); DeleteLocalInt(oOb,"nSState"); }
      } // see if upgrade
      nL++;
    } // check units
    if (nCN!=0&&nUC<nUAL)
    { // create unit
      nSpawnRes=nSpawnRes-nLeaderLevel;
      oOb=fnAICreateUnit(sTeamID,sHU,GetLocation(oTRD),nCN);
      fnMAIReport("  create HU:"+GetName(oOb));
      if (oOb!=OBJECT_INVALID)
      { nCN=0; nUC++;SetAILevel(oOb,AI_LEVEL_NORMAL); if (GetLocalInt(oOb,"nMState")!=19) { SetLocalInt(oOb,"nMState",19); DeleteLocalInt(oOb,"nSState"); }}
    } // create unit
    if (nUN!=0)
    { // upgrade unit
      oOb=GetLocalObject(oMod,sPre+IntToString(nUN));
      if (oOb!=OBJECT_INVALID) fnAIUpgradeUnit(oOb,sHU);
      nUN=0;
    } // upgrade unit
  } // HU
  if (GetStringLength(sIC)>3&&GetLocalInt(oMod,"nAIOFF2")!=TRUE)
  { // IC
    nBase=nMVD+nTRD+nLD1+nLD2+nCPC+nHU+1;
    nEnd=nBase+nIC;
    nCN=0;
    nUN=0;
    nL=nBase;
    while(nL<nEnd)
    { // check units
      oOb=GetLocalObject(oMod,sPre+IntToString(nL));
      if (oOb==OBJECT_INVALID||GetIsDead(oOb)==TRUE) nCN=nL;
      else
      { // see if upgrade
        if(GetTag(oOb)!=sIC&&GetArea(oOb)==GetArea(oTRD)&&GetLocalInt(oOb,"nSState")!=nReturnState) nUN=nL;
        //AssignCommand(oOb,SpeakString("*IC*"));
        if (GetLocalInt(oOb,"nMState")!=20) { SetLocalInt(oOb,"nMState",20); DeleteLocalInt(oOb,"nSState"); }
      } // see if upgrade
      nL++;
    } // check units
    if (nCN!=0&&nUC<nUAL)
    { // create unit
      nSpawnRes=nSpawnRes-nLeaderLevel;
      oOb=fnAICreateUnit(sTeamID,sIC,GetLocation(oTRD),nCN);
      fnMAIReport("  create IC:"+GetName(oOb));
      if (oOb!=OBJECT_INVALID)
      { nCN=0; nUC++;SetAILevel(oOb,AI_LEVEL_NORMAL); if (GetLocalInt(oOb,"nMState")!=20) { SetLocalInt(oOb,"nMState",20); DeleteLocalInt(oOb,"nSState"); }}
    } // create unit
    if (nUN!=0)
    { // upgrade unit
      oOb=GetLocalObject(oMod,sPre+IntToString(nUN));
      if (oOb!=OBJECT_INVALID) fnAIUpgradeUnit(oOb,sIC);
      nUN=0;
    } // upgrade unit
  } // IC
  if (GetStringLength(sRA)>3)
  { // RA
    nBase=nMVD+nTRD+nLD1+nLD2+nCPC+nHU+nIC+1;
    nEnd=nBase+nRA;
    nCN=0;
    nUN=0;
    nL=nBase;
    while(nL<nEnd)
    { // check units
      oOb=GetLocalObject(oMod,sPre+IntToString(nL));
      if (oOb==OBJECT_INVALID||GetIsDead(oOb)==TRUE) nCN=nL;
      else {
       if (GetArea(oOb)==GetArea(oLD1)&&GetLocalInt(oOb,"nMState")!=17) nRC++;
       // AssignCommand(oOb,SpeakString("*RA="+IntToString(nRC)+"*")); }
       //else { AssignCommand(oOb,SpeakString("*on raid*")); }
      }
      nL++;
    } // check units
    if (nCN!=0&&nUC<nUAL)
    { // create unit
      nSpawnRes=nSpawnRes-nLeaderLevel;
      oOb=fnAICreateUnit(sTeamID,sRA,GetLocation(oLD1),nCN);
      fnMAIReport("  create RA:"+GetName(oOb));
      if (oOb!=OBJECT_INVALID)
      { nCN=0; nUC++; SetAILevel(oOb,AI_LEVEL_NORMAL);}
    } // create unit
  } // RA
  if (GetStringLength(sRM)>3&&GetLocalInt(oMod,"bAIOFF3")!=TRUE)
  { // RM
    nBase=nMVD+nTRD+nLD1+nLD2+nCPC+nHU+nIC+nRA+1;
    nEnd=nBase+nRM;
    nCN=0;
    nUN=0;
    nL=nBase;
    while(nL<nEnd)
    { // check units
      oOb=GetLocalObject(oMod,sPre+IntToString(nL));
      if (oOb==OBJECT_INVALID||GetIsDead(oOb)==TRUE) nCN=nL;
      //else { AssignCommand(oOb,SpeakString("*RM*")); }
      nL++;
    } // check units
    if (nCN!=0&&nUC<4)
    { // create unit
      nSpawnRes=nSpawnRes-nLeaderLevel;
      oOb=fnAICreateUnit(sTeamID,sRM,GetLocation(oLD1),nCN);
      fnMAIReport("  create RM:"+GetName(oOb));
      if (oOb!=OBJECT_INVALID)
      { nCN=0; nUC++; SetAILevel(oOb,AI_LEVEL_NORMAL);}
    } // create unit
  } // RM
  if (GetStringLength(sRG)>3&&GetLocalInt(oMod,"bAIOFF4")!=TRUE)
  { // RG
    nBase=nMVD+nTRD+nLD1+nLD2+nCPC+nHU+nIC+nRA+nRM+1;
    nEnd=nBase+nRG;
    nCN=0;
    nUN=0;
    nL=nBase;
    while(nL<nEnd)
    { // check units
      oOb=GetLocalObject(oMod,sPre+IntToString(nL));
      if (oOb==OBJECT_INVALID||GetIsDead(oOb)==TRUE) nCN=nL;
      //else { AssignCommand(oOb,SpeakString("*RG*")); }
      nL++;
    } // check units
    if (nCN!=0&&nUC<nUAL)
    { // create unit
      nSpawnRes=nSpawnRes-nLeaderLevel;
      oOb=fnAICreateUnit(sTeamID,sRG,GetLocation(oLD1),nCN);
      fnMAIReport("  create RG:"+GetName(oOb));
      if (oOb!=OBJECT_INVALID)
      { nCN=0; nUC++; SetAILevel(oOb,AI_LEVEL_NORMAL);}
    } // create unit
  } // RG
  if (GetStringLength(sPR)>3&&nPR>0)
  { // PR
    nBase=nMVD+nTRD+nLD1+nLD2+nCPC+nHU+nIC+nRA+nRM+nRG+1;
    nEnd=nBase+nPR;
    nCN=0;
    nUN=0;
    nL=nBase;
    while(nL<nEnd)
    { // check units
      oOb=GetLocalObject(oMod,sPre+IntToString(nL));
      if (oOb==OBJECT_INVALID||GetIsDead(oOb)==TRUE) nCN=nL;
      nL++;
    } // check units
    if (nCN!=0&&nUC<nUAL)
    { // create unit
      nSpawnRes=nSpawnRes-nLeaderLevel;
      oOb=fnAICreateUnit(sTeamID,sPR,GetLocation(oTRD),nCN);
      fnMAIReport("  create PR:"+GetName(oOb));
      if (oOb!=OBJECT_INVALID)
      {
        nCN=0; nUC++;SetAILevel(oOb,AI_LEVEL_NORMAL);
        if (GetLocalInt(oOb,"nMState")!=23)
        {
          SetLocalInt(oOb,"nMState",23);
          DeleteLocalInt(oOb,"nSState");
          SetLocalString(oOb,"sTeamID",sTeamID);
          ChangeFaction(oOb,oLeader);
        }
      }
    } // create unit
  } // PR
  if (nRC>=(nRA/2))
  { // a raid needs to be initiated
    //SendMessageToPC(GetFirstPC(),"There are enough units for team "+sTeamID+" to raid.");
   fnMAIReport("  raid initiated!");
   sRaid=fnAIGetLargestTrespasser(sTeamID);
    //SendMessageToPC(GetFirstPC(),"Largest Trespasser "+sRaid);
    if (sRaid!="NA")
    { // Someone has trespassed
      sRaid=fnAIGetRandomTrespasser(sTeamID);
    } // Someone has trespassed
    else if (nAIMode!=0)
    { // no trespassers and I like to raid
      if (nAIMode==1) sRaid=sPID;
      else
      { // random raid target with emphasis on primary
        nL=d6();
        if (nL<3) sRaid=sPID;
        else if (nL==3) sRaid=="UNC";
        else if (nL==4) sRaid=="SPID";
        else if (nL==5) sRaid=="DWF";
        else if (nL==6) sRaid=="UND";
        while(sRaid==sTeamID)
        { // pick a different one
          nL=d6();
          if (nL<3) sRaid=sPID;
          else if (nL==3) sRaid=="UNC";
          else if (nL==4) sRaid=="SPID";
          else if (nL==5) sRaid=="DWF";
          else if (nL==6) sRaid=="UND";
        } // pick a different one
      } // random raid target with emphasis on primary
    } // no trespassers and I like to raid
    if (sRaid!="NA")
    { // initiate the raid
      //SendMessageToPC(GetFirstPC(),sTeamID+" initiates raid against "+sRaid+".");
      nBase=nMVD+nTRD+nLD1+nLD2+nCPC+nHU+nIC+1;
      nEnd=nBase+nRA;
      nL=nBase;
      while(nL<nEnd)
      { // check units
        oOb=GetLocalObject(oMod,sPre+IntToString(nL));
        if (oOb==OBJECT_INVALID) nCN=nL;
        else { // else
         if (GetArea(oOb)==GetArea(oLD1)&&GetLocalInt(oOb,"nMState")!=17)
         { // raid command
           DelayCommand(1.0,fnRaid(oOb,sRaid,1));
         } // raid command
        } // else
        nL++;
      } // check units
      nBase=nMVD+nTRD+nLD1+nLD2+nCPC+nHU+nIC+nRA+1;
      nEnd=nBase+nRM;
      nL=nBase;
      while(nL<nEnd)
      { // check units
        oOb=GetLocalObject(oMod,sPre+IntToString(nL));
        if (oOb==OBJECT_INVALID) nCN=nL;
        else {
         if (GetArea(oOb)==GetArea(oLD1)&&GetLocalInt(oOb,"nMState")!=17)
         { // raid command
           DelayCommand(1.5,fnRaid(oOb,sRaid,2));
         } // raid command
        }
        nL++;
      } // check units
      nBase=nMVD+nTRD+nLD1+nLD2+nCPC+nHU+nIC+nRA+nRM+1;
      nEnd=nBase+nRG;
      nL=nBase;
      while(nL<nEnd)
      { // check units
        oOb=GetLocalObject(oMod,sPre+IntToString(nL));
        if (oOb==OBJECT_INVALID) nCN=nL;
        else {
         if (GetArea(oOb)==GetArea(oLD1)&&GetLocalInt(oOb,"nMState")!=17)
         { // raid command
           DelayCommand(1.7,fnRaid(oOb,sRaid,3));
         } // raid command
        }
        nL++;
      } // check units
    } // initiate the raid
    //else if (nAIMode!=0) { SendMessageToPC(GetFirstPC(),"Raid target was bad for "+sTeamID+"."); }
  } // a raid needs to be initiated
  } // enough resources to spawn
  nAIC=0;
  SetLocalInt(oMod,"nAISpawnRes"+sTeamID,nSpawnRes);
  } // ai ok
  SetLocalInt(oMod,"nAIC"+sTeamID,nAIC);
 } // no enemies nearby
}
///////////////////////////////////////////////////////////////////////// MAIN

////////////////////////
// FUNCTIONS
////////////////////////
int fnLevel(object oOb)
{ // return level
  return GetLevelByPosition(1,oOb)+GetLevelByPosition(2,oOb)+GetLevelByPosition(3,oOb);
} // fnLevel()

string fnReturnUnitRes(string sID,string sPos,int nLevel)
{ // return resref of unit
  string sRes="";
  object oMod=GetModule();
  int nRL=GetLocalInt(oMod,"n"+sID+"_nCAIRL");
  int nCPL=GetLocalInt(oMod,"n"+sID+"_nCAICPL");
  int nPRL=GetLocalInt(oMod,"n"+sID+"_nCAIPRL");
  if (nLevel>0)
  { // level 1
    if (sPos=="MVD")
    { // mana guard
      if (sID=="SPID") sRes="SPID0";
      else if (sID=="UNC") sRes="UNC1";
      else if (sID=="UND") sRes="UND1";
      else if (sID=="DWF") sRes="DWF22";
    } // mana guard
    else if (sPos=="TRD")
    { // Throne room guard
      if (sID=="SPID") sRes="SPID0";
      else if (sID=="UNC") sRes="UNC1";
      else if (sID=="UND") sRes="UND1";
      else if (sID=="DWF") sRes="DWF12";
    } // Throne room guard
    else if (sPos=="LD1")
    { // Lair defense 1
      if (sID=="SPID") sRes="SPID29";
      else if (sID=="UNC") sRes="UNC1";
      else if (sID=="UND") sRes="UND1";
      else if (sID=="DWF") sRes="DWF1";
    } // Lair defense 1
    else if (sPos=="LD2")
    { // Lair defense 2
      if (sID=="SPID") sRes="SPID29";
      else if (sID=="UNC") sRes="UNC1";
      else if (sID=="UND") sRes="UND1";
      else if (sID=="DWF") sRes="DWF1";
    } // Lair defense 2
    else if (sPos=="HU")
    { // harvest unit
      if (sID=="SPID") sRes="SPID0";
      else if (sID=="UNC") sRes="UNC0";
      else if (sID=="UND") sRes="UND0";
      else if (sID=="DWF") sRes="DWF0";
    } // harvest unit
    else if (sPos=="CPC"&&nCPL==1)
    { // control point capture
      if (sID=="SPID") sRes="spid3ai";
      else if (sID=="UNC") sRes="UNC1";
      else if (sID=="UND") sRes="UND1";
      else if (sID=="DWF") sRes="DWF22";
    } // control point capture
    else if (sPos=="IC")
    { // item capture
      if (sID=="SPID") sRes="SPID29";
      else if (sID=="UNC") sRes="UNC1";
      else if (sID=="UND") sRes="UND0";
      else if (sID=="DWF") sRes="DWF1";
    } // item capture
    else if (sPos=="RA"&&nRL==1)
    { // raid attack
      if (sID=="SPID") sRes="spid3ai";
      else if (sID=="UNC") sRes="UNC1";
      else if (sID=="UND") sRes="UND1";
      else if (sID=="DWF") sRes="DWF22";
    } // raid attack
    else if (sPos=="RM"&&nRL==1)
    { // raid mana
    } // raid mana
    else if (sPos=="RG"&&nRL==1)
    { // raid gold
    } // raid gold
    else if (sPos=="PR"&&nPRL==1)
    { // power res
      if (sID=="SPID") sRes="SPID29";
      else if (sID=="UNC") sRes="UNC1";
      else if (sID=="UND") sRes="UND1";
      else if (sID=="DWF") sRes="DWF22";
    } // power res
  } // level 1
  if (nLevel>2)
  { // level 3
    if (sPos=="MVD")
    { // mana guard
      if (sID=="SPID") sRes="SPID29";
      else if (sID=="UNC") sRes="UNC2";
      else if (sID=="UND") sRes="UND2";
      else if (sID=="DWF") sRes="DWF3";
    } // mana guard
    else if (sPos=="TRD")
    { // Throne room guard
      if (sID=="SPID") sRes="SPID29";
      else if (sID=="UNC") sRes="UNC2";
      else if (sID=="UND") sRes="UND3";
      else if (sID=="DWF") sRes="DWF13";
    } // Throne room guard
    else if (sPos=="LD1")
    { // Lair defense 1
      if (sID=="UNC") sRes="UNC2";
      else if (sID=="UND") sRes="UND2";
      else if (sID=="DWF") sRes="DWF17";
    } // Lair defense 1
    else if (sPos=="LD2")
    { // Lair defense 2
      if (sID=="UNC") sRes="UNC2";
      else if (sID=="UND") sRes="UND2";
      else if (sID=="DWF") sRes="DWF17";
    } // Lair defense 2
    else if (sPos=="CPC"&&nCPL<4)
    { // control point capture
      if (sID=="SPID") sRes="spid16ai";
      else if (sID=="UNC") sRes="unc25";
      else if (sID=="UND") sRes="UND3";
      else if (sID=="DWF") sRes="DWF12";
    } // control point capture
    else if (sPos=="HU")
    { // harvest unit
    } // harvest unit
    else if (sPos=="IC")
    { // item capture
      if (sID=="UND") sRes="UND26";
    } // item capture
    else if (sPos=="RA"&&nRL<4)
    { // raid attack
      if (sID=="SPID") sRes="spid16ai";
      else if (sID=="UNC") sRes="UNC1";
      else if (sID=="UND") sRes="UND2";
      else if (sID=="DWF") sRes="DWF1";
    } // raid attack
    else if (sPos=="RM"&&nRL<4)
    { // raid mana
      if (sID=="UND") sRes="UND26";
    } // raid mana
    else if (sPos=="RG"&&nRL<4)
    { // raid gold
    } // raid gold
    else if (sPos=="PR"&&nPRL<4)
    { // power res
      if (sID=="SPID") sRes="SPID29";
      else if (sID=="UNC") sRes="UNC2";
      else if (sID=="UND") sRes="UND3";
      else if (sID=="DWF") sRes="DWF1";
    } // power res
  } // level 3
  if (nLevel>4)
  { // level 5
    if (sPos=="MVD")
    { // mana guard
      if (sID=="SPID") sRes="SPID6";
      else if (sID=="UNC") sRes="UNC4";
      else if (sID=="UND") sRes="UND12";
      else if (sID=="DWF") sRes="DWF5";
    } // mana guard
    else if (sPos=="TRD")
    { // Throne room guard
      if (sID=="SPID") sRes="SPID6";
      else if (sID=="UNC") sRes="UNC4";
      else if (sID=="UND") sRes="UND10";
      else if (sID=="DWF") sRes="DWF14";
    } // Throne room guard
    else if (sPos=="LD1")
    { // Lair defense 1
      if (sID=="SPID") sRes="SPID6";
      else if (sID=="UNC") sRes="UNC4";
      else if (sID=="UND") sRes="UND4";
      else if (sID=="DWF") sRes="DWF28";
    } // Lair defense 1
    else if (sPos=="LD2")
    { // Lair defense 2
      if (sID=="SPID") sRes="SPID6";
      else if (sID=="UNC") sRes="UNC4";
      else if (sID=="UND") sRes="UND10";
      else if (sID=="DWF") sRes="DWF28";
    } // Lair defense 2
    else if (sPos=="CPC"&&nCPL<6)
    { // control point capture
      if (sID=="SPID") sRes="SPID6";
      else if (sID=="UNC") sRes="UNC4";
      else if (sID=="UND") sRes="UND5";
      else if (sID=="DWF") sRes="DWF4";
    } // control point capture
    else if (sPos=="HU")
    { // harvest unit
    } // harvest unit
    else if (sPos=="IC")
    { // item capture
      if (sID=="UNC") sRes="UNC4";
      else if (sID=="DWF") sRes="DWF3";
    } // item capture
    else if (sPos=="RA"&&nRL<6)
    { // raid attack
      if (sID=="SPID") sRes="SPID6";
      else if (sID=="UNC") sRes="UNC25";
      else if (sID=="UND") sRes="UND5";
      else if (sID=="DWF") sRes="DWF2";
    } // raid attack
    else if (sPos=="RM"&&nRL<6)
    { // raid mana
    } // raid mana
    else if (sPos=="RG"&&nRL<6)
    { // raid gold
      if (sID=="UND") sRes="UND26";
    } // raid gold
    else if (sPos=="PR"&&nPRL<6)
    { // power res
      if (sID=="SPID") sRes="SPID29";
      else if (sID=="UNC") sRes="UNC4";
      else if (sID=="UND") sRes="UND5";
      else if (sID=="DWF") sRes="DWF2";
    } // power res
  } // level 5
  if (nLevel>6)
  { // level 7
    if (sPos=="MVD")
    { // mana guard
      if (sID=="UNC") sRes="UNC6";
      else if (sID=="DWF") sRes="DWF7";
    } // mana guard
    else if (sPos=="TRD")
    { // Throne room guard
      if (sID=="UNC") sRes="UNC6";
      else if (sID=="UND") sRes="UND8";
    } // Throne room guard
    else if (sPos=="LD1")
    { // Lair defense 1
      if (sID=="DWF") sRes="DWF18";
    } // Lair defense 1
    else if (sPos=="LD2")
    { // Lair defense 2
      if (sID=="DWF") sRes="DWF18";
    } // Lair defense 2
    else if (sPos=="CPC"&&nCPL<8)
    { // control point capture
      if (sID=="SPID") sRes="SPID6";
      else if (sID=="UNC") sRes="UNC24";
      else if (sID=="UND") sRes="UND5";
      else if (sID=="DWF") sRes="DWF13";
    } // control point capture
    else if (sPos=="HU")
    { // harvest unit
      if (sID=="SPID") sRes="SPID24";
      else if (sID=="UND") sRes="UND11";
    } // harvest unit
    else if (sPos=="IC")
    { // item capture
    } // item capture
    else if (sPos=="RA"&&nRL<8)
    { // raid attack
      if (sID=="SPID") sRes="SPID6";
      else if (sID=="UNC") sRes="UNC26";
      else if (sID=="UND") sRes="UND4";
      else if (sID=="DWF") sRes="DWF4";
    } // raid attack
    else if (sPos=="RM"&&nRL<8)
    { // raid mana
      if (sID=="SPID") sRes="SPID24";
    } // raid mana
    else if (sPos=="RG"&&nRL<8)
    { // raid gold
      if (sID=="SPID") sRes="SPID29";
      else if (sID=="DWF") sRes="DWF23";
    } // raid gold
    else if (sPos=="PR"&&nPRL<8)
    { // power res
      if (sID=="SPID") sRes="SPID24";
      else if (sID=="UNC") sRes="UNC24";
      else if (sID=="UND") sRes="UND10";
      else if (sID=="DWF") sRes="DWF5";
    } // power res
  } // level 7
  if (nLevel>8)
  { // level 9
    if (sPos=="MVD")
    { // mana guard
      if (sID=="DWF") sRes="DWF9";
    } // mana guard
    else if (sPos=="TRD")
    { // Throne room guard
      if (sID=="UNC") sRes="UNC10";
      else if (sID=="UND") sRes="UND14";
    } // Throne room guard
    else if (sPos=="LD1")
    { // Lair defense 1
      if (sID=="UNC") sRes="UNC12";
      else if (sID=="UND") sRes="UND18";
    } // Lair defense 1
    else if (sPos=="LD2")
    { // Lair defense 2
      if (sID=="UNC") sRes="UNC12";
      else if (sID=="UND") sRes="UND12";
    } // Lair defense 2
    else if (sPos=="CPC")
    { // control point capture
      if (sID=="UNC") sRes="UNC24";
      else if (sID=="UND") sRes="UND17";
    } // control point capture
    else if (sPos=="HU")
    { // harvest unit
    } // harvest unit
    else if (sPos=="IC")
    { // item capture
      if (sID=="UNC") sRes="UNC24";
      else if (sID=="DWF") sRes="DWF6";
    } // item capture
    else if (sPos=="RA")
    { // raid attack
      if (sID=="UNC") sRes="UNC4";
      else if (sID=="UND") sRes="UND17";
      else if (sID=="DWF") sRes="DWF6";
    } // raid attack
    else if (sPos=="RM")
    { // raid mana
      if (sID=="DWF") sRes="DWF23";
    } // raid mana
    else if (sPos=="RG")
    { // raid gold
      if (sID=="UNC") sRes="UNC27";
    } // raid gold
    else if (sPos=="PR")
    { // power res
      if (sID=="SPID") sRes="SPID24";
      else if (sID=="UNC") sRes="UNC7";
      else if (sID=="UND") sRes="UND12";
      else if (sID=="DWF") sRes="DWF9";
    } // power res
  } // level 9
  if (nLevel>10)
  { // level 11
    if (sPos=="MVD")
    { // mana guard
      if (sID=="SPID") sRes="SPID26";
      else if (sID=="UNC") sRes="UNC13";
      else if (sID=="UND") sRes="UND13";
      else if (sID=="DWF") sRes="DWF10";
    } // mana guard
    else if (sPos=="TRD")
    { // Throne room guard
      if (sID=="SPID") sRes="SPID19";
      else if (sID=="UNC") sRes="UNC11";
      else if (sID=="UND") sRes="UND19";
      else if (sID=="DWF") sRes="DWF15";
    } // Throne room guard
    else if (sPos=="LD1")
    { // Lair defense 1
      if (sID=="SPID") sRes="SPID19";
    } // Lair defense 1
    else if (sPos=="LD2")
    { // Lair defense 2
      if (sID=="SPID") sRes="SPID19";
    } // Lair defense 2
    else if (sPos=="CPC")
    { // control point capture
      if (sID=="DWF") sRes="DWF14";
    } // control point capture
    else if (sPos=="HU")
    { // harvest unit
    } // harvest unit
    else if (sPos=="IC")
    { // item capture
      if (sID=="DWF") sRes="DWF8";
    } // item capture
    else if (sPos=="RA")
    { // raid attack
      if (sID=="UNC") sRes="UNC24";
      else if (sID=="UND") sRes="UND18";
      else if (sID=="DWF") sRes="DWF18";
    } // raid attack
    else if (sPos=="RM")
    { // raid mana
      if (sID=="UNC") sRes="UNC6";
    } // raid mana
    else if (sPos=="RG")
    { // raid gold
    } // raid gold
    else if (sPos=="PR")
    { // power res
      if (sID=="SPID") sRes="SPID24";
      else if (sID=="UNC") sRes="UNC10";
      else if (sID=="UND") sRes="UND12";
      else if (sID=="DWF") sRes="DWF10";
    } // power res
  } // level 11
  if (nLevel>12)
  { // level 13
    if (sPos=="MVD")
    { // mana guard
    } // mana guard
    else if (sPos=="TRD")
    { // Throne room guard
      if (sID=="UND") sRes="UND20";
    } // Throne room guard
    else if (sPos=="LD1")
    { // Lair defense 1
      if (sID=="SPID") sRes="SPID26";
      else if (sID=="UNC") sRes="UNC14";
      else if (sID=="DWF") sRes="DWF24";
    } // Lair defense 1
    else if (sPos=="LD2")
    { // Lair defense 2
      if (sID=="SPID") sRes="SPID26";
      else if (sID=="UNC") sRes="UNC14";
      else if (sID=="DWF") sRes="DWF24";
    } // Lair defense 2
    else if (sPos=="CPC")
    { // control point capture
      if (sID=="UND") sRes="UND14";
    } // control point capture
    else if (sPos=="HU")
    { // harvest unit
    } // harvest unit
    else if (sPos=="IC")
    { // item capture
      if (sID=="SPID") sRes="SPID26";
      else if (sID=="UND") sRes="UND25";
      else if (sID=="DWF") sRes="DWF10";
    } // item capture
    else if (sPos=="RA")
    { // raid attack
      if (sID=="UND") sRes="UND14";
      else if (sID=="DWF") sRes="DWF8";
    } // raid attack
    else if (sPos=="RM")
    { // raid mana
      if (sID=="UND") sRes="UND25";
    } // raid mana
    else if (sPos=="RG")
    { // raid gold
      if (sID=="UNC") sRes="UNC13";
    } // raid gold
    else if (sPos=="PR")
    { // power res
      if (sID=="SPID") sRes="SPID23";
      else if (sID=="UNC") sRes="UNC11";
      else if (sID=="UND") sRes="UND12";
      else if (sID=="DWF") sRes="DWF15";
    } // power res
  } // level 13
  if (nLevel>14)
  { // level 15
    if (sPos=="MVD")
    { // mana guard
      if (sID=="DWF") sRes="DWF11";
    } // mana guard
    else if (sPos=="TRD")
    { // Throne room guard
      if (sID=="SPID") sRes="SPID26";
      else if (sID=="UNC") sRes="UNC14";
      else if (sID=="DWF") sRes="DWF16";
    } // Throne room guard
    else if (sPos=="LD1")
    { // Lair defense 1
      if (sID=="UND") sRes="UND13";
    } // Lair defense 1
    else if (sPos=="LD2")
    { // Lair defense 2
      if (sID=="UND") sRes="UND20";
    } // Lair defense 2
    else if (sPos=="CPC")
    { // control point capture
      if (sID=="UNC") sRes="UNC7";
      else if (sID=="DWF") sRes="DWF21";
    } // control point capture
    else if (sPos=="HU")
    { // harvest unit
    } // harvest unit
    else if (sPos=="IC")
    { // item capture
    } // item capture
    else if (sPos=="RA")
    { // raid attack
      if (sID=="UNC") sRes="UNC10";
      else if (sID=="UND") sRes="UND28";
      else if (sID=="DWF") sRes="DWF10";
    } // raid attack
    else if (sPos=="RM")
    { // raid mana
    } // raid mana
    else if (sPos=="RG")
    { // raid gold
    } // raid gold
    else if (sPos=="PR")
    { // power res
      if (sID=="SPID") sRes="SPID23";
      else if (sID=="UNC") sRes="UNC11";
      else if (sID=="UND") sRes="UND14";
      else if (sID=="DWF") sRes="DWF21";
    } // power res
  } // level 15
  if (nLevel>16)
  { // level 17
    if (sPos=="MVD")
    { // mana guard
      if (sID=="SPID") sRes="SPID25";
      else if (sID=="UNC") sRes="UNC15";
    } // mana guard
    else if (sPos=="TRD")
    { // Throne room guard
      if (sID=="SPID") sRes="SPID25";
    } // Throne room guard
    else if (sPos=="LD1")
    { // Lair defense 1
    } // Lair defense 1
    else if (sPos=="LD2")
    { // Lair defense 2
    } // Lair defense 2
    else if (sPos=="CPC")
    { // control point capture
    } // control point capture
    else if (sPos=="HU")
    { // harvest unit
    } // harvest unit
    else if (sPos=="IC")
    { // item capture
      if (sID=="UNC") sRes="UNC7";
      else if (sID=="DWF") sRes="DWF19";
    } // item capture
    else if (sPos=="RA")
    { // raid attack
      if (sID=="UNC") sRes="UNC11";
      else if (sID=="UND") sRes="UND15";
      else if (sID=="DWF") sRes="DWF24";
    } // raid attack
    else if (sPos=="RM")
    { // raid mana
    } // raid mana
    else if (sPos=="RG")
    { // raid gold
      if (sID=="UND") sRes="UND25";
    } // raid gold
    else if (sPos=="PR")
    { // power res
      if (sID=="SPID") sRes="SPIDPR4";
      else if (sID=="UNC") sRes="UNC11";
      else if (sID=="UND") sRes="UND25";
      else if (sID=="DWF") sRes="DWF19";
    } // power res
  } // level 17
  if (nLevel>18)
  { // level 19
    if (sPos=="MVD")
    { // mana guard
      if (sID=="UND") sRes="UND23";
    } // mana guard
    else if (sPos=="TRD")
    { // Throne room guard
      if (sID=="SPID") sRes="SPID27";
      else if (sID=="UND") sRes="UND23";
    } // Throne room guard
    else if (sPos=="LD1")
    { // Lair defense 1
      if (sID=="SPID") sRes="SPID27";
      else if (sID=="DWF") sRes="DWF25";
    } // Lair defense 1
    else if (sPos=="LD2")
    { // Lair defense 2
      if (sID=="SPID") sRes="SPID27";
      else if (sID=="DWF") sRes="DWF25";
    } // Lair defense 2
    else if (sPos=="CPC")
    { // control point capture
      if (sID=="SPID") sRes="SPID27";
      else if (sID=="UNC") sRes="UNC11";
      else if (sID=="UND") sRes="UND15";
    } // control point capture
    else if (sPos=="HU")
    { // harvest unit
    } // harvest unit
    else if (sPos=="IC")
    { // item capture
    } // item capture
    else if (sPos=="RA")
    { // raid attack
      if (sID=="UNC") sRes="UNC14";
      else if (sID=="DWF") sRes="DWF25";
    } // raid attack
    else if (sPos=="RM")
    { // raid mana
      if (sID=="SPID") sRes="SPID27";
    } // raid mana
    else if (sPos=="RG")
    { // raid gold
      if (sID=="SPID") sRes="SPID27";
      else if (sID=="UNC") sRes="UNC11";
    } // raid gold
    else if (sPos=="PR")
    { // power res
      if (sID=="SPID") sRes="SPID27";
      else if (sID=="UNC") sRes="UNCPR6";
      else if (sID=="UND") sRes="UND19";
      else if (sID=="DWF") sRes="DWF11";
    } // power res
  } // level 19
  if (nLevel>19)
  { // level 20
    if (sPos=="MVD")
    { // mana guard
      if (sID=="SPID") sRes="SPID28";
      else if (sID=="UNC") sRes="UNC16";
    } // mana guard
    else if (sPos=="TRD")
    { // Throne room guard
      if (sID=="UNC") sRes="UNC9";
    } // Throne room guard
    else if (sPos=="LD1")
    { // Lair defense 1
    } // Lair defense 1
    else if (sPos=="LD2")
    { // Lair defense 2
    } // Lair defense 2
    else if (sPos=="CPC")
    { // control point capture
      if (sID=="DWF") sRes="DWF16";
    } // control point capture
    else if (sPos=="HU")
    { // harvest unit
    } // harvest unit
    else if (sPos=="IC")
    { // item capture
      if (sID=="DWF") sRes="DWF20";
    } // item capture
    else if (sPos=="RA")
    { // raid attack
      if (sID=="SPID") sRes="SPID19";
      else if (sID=="UNC") sRes="UNC13";
      else if (sID=="UND") sRes="UND22";
      else if (sID=="DWF") sRes="DWF11";
    } // raid attack
    else if (sPos=="RM")
    { // raid mana
      if (sID=="UNC") sRes="UNC9";
    } // raid mana
    else if (sPos=="RG")
    { // raid gold
      if (sID=="DWF") sRes="DWF20";
    } // raid gold
    else if (sPos=="PR")
    { // power res
      if (sID=="SPID") sRes="SPID28";
      else if (sID=="UNC") sRes="UNC9";
      else if (sID=="UND") sRes="UND13";
      else if (sID=="DWF") sRes="DWF20";
    } // power res
  } // level 20
  return sRes;
} // fnReturnUnitRes()

/*int fnSpawnDelay(int nLevel)
{
  int nRet=0;
  if (nLevel>4) nRet=1;
  else if (nLevel>6) nRet=2;
  else if (nLevel>8) nRet=3;
  else if (nLevel>10) nRet=4;
  else if (nLevel>12) nRet=5;
  else if (nLevel>14) nRet=6;
  else if (nLevel>16) nRet=7;
  else if (nLevel>18) nRet=8;
  else if (nLevel>19) nRet=7;
  return nRet;
} // fnSpawnDelay()     */

int fnGetGold(string sID)
{
  object oChest=GetObjectByTag(sID+"_CHEST");
  int nG=0;
  object oItem=GetFirstItemInInventory(oChest);
  while(nG==0&&oItem!=OBJECT_INVALID)
  { // check chest inventory
    if (GetTag(oItem)=="NW_IT_GOLD001") nG=GetItemStackSize(oItem);
    oItem=GetNextItemInInventory(oChest);
  } // check chest inventory
  return nG;
} // fnGetGold()

void fnSetGold(string sID,int nGold)
{
  object oChest=GetObjectByTag(sID+"_CHEST");
  int nG=0;
  object oItem=GetFirstItemInInventory(oChest);
  while(oItem!=OBJECT_INVALID)
  { // count gold
    if (GetResRef(oItem)=="nw_it_gold001")
    { // gold
      DelayCommand(0.3,DestroyObject(oItem));
    } // gold
    oItem=GetNextItemInInventory(oChest);
  } // count gold
  CreateItemOnObject("nw_it_gold001",oChest,nGold);
} // set gold amount

void fnSpendGold(string sID,string sPID)
{ // spend the gold of the team on assassins, lair cleaners, invisible mercs,etc.
  object oMod=GetModule();
  int nGold=fnGetGold(sID);
  object oStart=GetWaypointByTag(sID+"_START");
  object oLC=GetLocalObject(oMod,"oAILairClean"+sID); // lair cleaner exists?
  object oAS=GetLocalObject(oMod,"oAIAssassin"+sID); // hired assassin
  object oMA=GetLocalObject(oMod,"oAIMageAssassin"+sID); // hired mage assassin
  object oLDM1=GetLocalObject(oMod,"oAILDM1"+sID); // lair defense see invis merc 1
  object oLDM2=GetLocalObject(oMod,"oAILDM2"+sID); // lair defense see invis merc 2
  object oLPID=GetLocalObject(oMod,"oTeamLead"+sPID);
  int nELVL=fnLevel(oLPID);
  int nMercCost=1000;
  int nMGCost;
  int nAGCost;
  string sMGRes;
  string sAGRes;
  object oWP;
  int nN;
  if ((oLC==OBJECT_INVALID||GetIsDead(oLC)==TRUE)&&nGold>99)
  { // create a lair cleaner
    fnMAIReport("  create lair cleaner (spend gold)");
    oLC=CreateObject(OBJECT_TYPE_CREATURE,"clean_"+sID,GetLocation(oStart));
    SetAILevel(oLC,AI_LEVEL_NORMAL);
    SetLocalObject(oMod,"oAILairClean"+sID,oLC);
    nGold=nGold-100;
    fnSetGold(sID,nGold);
  } // create a lair cleaner
  else if (GetIsPC(oLPID)==TRUE&&nGold>300)
  { // hire assassins
    fnMAIReport("  hire assassins (spend gold)");
    if (nELVL<5)
    { // apprentice
      nMGCost=300;
      sMGRes="mageass1";
      nAGCost=500;
      sAGRes="assassin1";
    } // apprentice
    else if (nELVL<8)
    { // journeyman
      nMGCost=500;
      sMGRes="mageass2";
      nAGCost=2000;
      sAGRes="assassin2";
    } // journeyman
    else if (nELVL<12)
    { // master
      nMGCost=1000;
      sMGRes="mageass3";
      nAGCost=4000;
      sAGRes="assassin3";
    } // master
    else
    { // the best
      nMGCost=5000;
      sMGRes="mageass4";
      nAGCost=4000;
      sAGRes="assassin3";
    } // the best
    // set target
    if (sPID=="UNC") nN=1;
    else if (sPID=="SPID") nN=2;
    else if (sPID=="UND") nN=3;
    else { nN=4; }
    if((oAS==OBJECT_INVALID||GetIsDead(oAS)==TRUE)&&nGold>=nAGCost)
    { // hire assassin
      nGold=nGold-nAGCost;
      fnSetGold(sID,nGold);
      oWP=GetWaypointByTag("ASS_SPAWN");
      oAS=CreateObject(OBJECT_TYPE_CREATURE,sAGRes,GetLocation(oWP));
      SetLocalInt(oAS,"nSpeed",8);
      SetAILevel(oAS,AI_LEVEL_NORMAL);
      SetLocalObject(oMod,"oAIAssassin"+sID,oAS);
      SetLocalInt(oAS,"nTarget",nN);
      ExecuteScript("cg_assassin",oAS);
    } // hire assassin
    if ((oMA==OBJECT_INVALID||GetIsDead(oMA)==TRUE)&&nGold>=nMGCost)
    { // hire mage assassin
      nGold=nGold-nMGCost;
      fnSetGold(sID,nGold);
      oWP=GetWaypointByTag("MAGEASS_SPAWN");
      oMA=CreateObject(OBJECT_TYPE_CREATURE,sMGRes,GetLocation(oWP));
      SetLocalInt(oMA,"nSpeed",8);
      SetAILevel(oMA,AI_LEVEL_NORMAL);
      SetLocalObject(oMod,"oAIMageAssassin"+sID,oMA);
      SetLocalInt(oMA,"nTarget",nN);
      ExecuteScript("mage_assassin",oMA);
    } // hire mage assassin
  } // hire assassins
  if (oLDM1==OBJECT_INVALID&&nGold>=nMercCost)
  { // hire lair defense merc 1
    fnMAIReport("   hire LD1 merc (spend gold)");
    nGold=nGold-nMercCost;
    fnSetGold(sID,nGold);
    oLDM1=CreateObject(OBJECT_TYPE_CREATURE,"watchermercenary",GetLocation(oStart));
    SetLocalString(oLDM1,"sTeamID",sID);
    ChangeFaction(oLDM1,OBJECT_SELF);
    SetLocalInt(oLDM1,"nMState",11); // guard lair
    SetLocalInt(oLDM1,"nSState",0);
    SetLocalObject(oMod,"oAILDM1"+sID,oLDM1);
  } // hire lair defense merc 1
  else if (oLDM2==OBJECT_INVALID&&nGold>=nMercCost)
  { // hire lair defense merc 2
    fnMAIReport("   hire LD2 merc (spend gold)");
    nGold=nGold-nMercCost;
    fnSetGold(sID,nGold);
    oLDM2=CreateObject(OBJECT_TYPE_CREATURE,"watchermercenary",GetLocation(oStart));
    SetLocalString(oLDM2,"sTeamID",sID);
    ChangeFaction(oLDM2,OBJECT_SELF);
    SetLocalInt(oLDM2,"nMState",11); // guard lair
    SetLocalInt(oLDM2,"nSState",0);
    SetLocalObject(oMod,"oAILDM2"+sID,oLDM2);
  } // hire lair defense merc 2
} // fnSpendGold()