///////////////////////////////////////////////////////////////////////////////
// rts_team_choose2 - Version 2.0 of the team choosing script
// By Deva Bryson Winblood.  05/21/2004
///////////////////////////////////////////////////////////////////////////////
#include "equip_h"
#include "lib_h_pccustom"

void fnSetFactionToFriendly(object oPC,string sID)
{ // Set faction that you control to friendly
  object oMember=GetObjectByTag(sID+"_PROXY");
  if (oMember!=OBJECT_INVALID)
  { // !OI
     AdjustReputation(oPC,oMember,100);
  } // !OI
  else
  {
    SendMessageToPC(oPC,"CRITICAL AREA: A creature placed with tag "+sID+"_PROXY could not be found and is required to set faction you control to friendly!!!");
  }
} // fnSetFactionToFriendly()


void fnSetFactionToEnemy(object oPC,string sID)
{ // Set faction that you control to friendly
  object oMember=GetObjectByTag(sID+"_PROXY");
  if (oMember!=OBJECT_INVALID)
  { // !OI
     AdjustReputation(oPC,oMember,-100);
  } // !OI
  else
  {
    SendMessageToPC(oPC,"CRITICAL AREA: A creature placed with tag "+sID+"_PROXY could not be found and is required to set faction you control to friendly!!!");
  }
} // fnSetFactionToEnemy()


void fnVisualTeam(object oPC)
{
  object oMod=GetModule();
  string sID=GetLocalString(oPC,"sTeamID");
  int bLeader=GetLocalInt(oPC,"bTeamLeader");
  effect eVFX;
  effect eLeader;
  if (GetLocalInt(oMod,"nInProgress")==FALSE&&GetStringLength(sID)>0)
  { // show visual effect until game starts
    if (sID=="SPID")
    { // spiders
      eVFX=EffectVisualEffect(VFX_DUR_GLOW_RED);
      eLeader=EffectVisualEffect(VFX_DUR_WEB);
    } // spiders
    else if (sID=="DWF")
    { // dwarves
      eVFX=EffectVisualEffect(VFX_DUR_GLOW_BLUE);
      eLeader=EffectVisualEffect(VFX_DUR_IOUNSTONE_BLUE);
    } // dwarves
    else if (sID=="UNC")
    { // unclean
      eVFX=EffectVisualEffect(VFX_DUR_GLOW_GREEN);
      eLeader=EffectVisualEffect(VFX_DUR_FLIES);
    } // unclean
    else if (sID=="UND")
    { // undead
      eVFX=EffectVisualEffect(VFX_DUR_GLOW_PURPLE);
      eLeader=EffectVisualEffect(VFX_DUR_TENTACLE);
    } // undead
    //ApplyEffectToObject(DURATION_TYPE_TEMPORARY,eVFX,oPC,4.0);
    if (bLeader==TRUE) ApplyEffectToObject(DURATION_TYPE_TEMPORARY,eLeader,oPC,4.0);
    if (bLeader==TRUE) DelayCommand(4.0,fnVisualTeam(oPC));
  } // show visual effect until game starts
} // fnVisualTeam()


void fnEquipTeamMember(object oPC)
{ // PURPOSE: To apply team specific equipment
  struct stEquipment stEq;
  stEq=GetInitialEquipment(oPC);
  DelayCommand(3.0,SwapOutEquipment(oPC,stEq));
} // fnEquipTeamMember()


void fnSetAppearance(object oPC)
{ // PURPOSE: Set the appearance of the PC to reflect team
    string sS;
    string sTeamID=GetLocalString(oPC,"sTeamID");
    int nR=GetRacialType(oPC);
    int nG=GetGender(oPC);
    if (!GetIsHeadValid(oPC))
    { // change head
        sS=GetTeamHeadNumbers(sTeamID,nR,nG);
        sS=StringParse(sS,"/");
        SetCreatureBodyPart(CREATURE_PART_HEAD,StringToInt(sS),oPC);
    } // change head
    if (!GetIsHairColorValid(oPC))
    { // change hair color
        sS=GetTeamHairColors(sTeamID,nR,nG);
        sS=StringParse(sS,"/");
        SetColor(oPC,COLOR_CHANNEL_HAIR,StringToInt(sS));
    } // change hair color
    if (!GetIsSkinColorValid(oPC))
    { // change skin color
        sS=GetTeamSkinColors(sTeamID,nR,nG);
        sS=StringParse(sS,"/");
        SetColor(oPC,COLOR_CHANNEL_SKIN,StringToInt(sS));
    } // change skin color
} // fnSetAppearance()

void main()
{
   object oPC=GetEnteringObject();
   string sTeamID=GetName(OBJECT_SELF);
   int nTeamNum=StringToInt(GetStringRight(sTeamID,1));
   object oMod=GetModule();
   object oTeamLead;
   string sStartTID=GetLocalString(oPC,"sTeamID");
   object oWP;
   int bLeader=GetLocalInt(oPC,"bTeamLeader");
   string sTeamName;
   object oDoor;
   int bAbort=FALSE;
   sTeamID=GetStringLeft(sTeamID,GetStringLength(sTeamID)-1);
   oTeamLead=GetLocalObject(oMod,"oTeamLead"+sTeamID);
   oWP=GetWaypointByTag("ABORT_"+sTeamID);
   sTeamName=GetLocalString(oPC,"sTeamID");
   oDoor=GetObjectByTag("DOOR_"+sTeamID);
   if (GetLocalInt(oMod,"b"+sTeamID+"Locked")) bAbort=TRUE;
   if (bLeader!=TRUE&&!bAbort)
   { // okay to switch teams or become leader
     if (oTeamLead==OBJECT_INVALID)
     { // you get to be leader
       SetLocalObject(oMod,"oTeamLead"+sTeamID,oPC);
       SetLocalInt(oPC,"bTeamLeader",TRUE);
       SendMessageToPC(oPC,"You have taken control of the team with the ID:"+sTeamID);
     } // you get to be leader
     SetLocalInt(oMod,"n"+sTeamID+"Num",nTeamNum);
     SetLocalInt(oPC,"nTeamNum",nTeamNum);
     SetLocalString(oPC,"sTeamID",sTeamID);
     fnSetFactionToFriendly(oPC,sTeamID);
     AddJournalQuestEntry(sTeamID,1,oPC,FALSE,FALSE);
     AddJournalQuestEntry(sTeamID+"UNITS",1,oPC,FALSE,FALSE);
     AssignCommand(oPC,ClearAllActions());
     AssignCommand(oPC,JumpToObject(oWP));
     AddJournalQuestEntry("STARTHELP",2,oPC,FALSE,FALSE);
     AssignCommand(oDoor,ActionCloseDoor(oDoor));
     if (GetStringLength(sStartTID)>0&&sStartTID!=sTeamID)
     { // remove settings of previous team
       fnSetFactionToEnemy(oPC,sStartTID);
       RemoveJournalQuestEntry(sStartTID,oPC,FALSE);
       RemoveJournalQuestEntry(sStartTID+"UNITS",oPC,FALSE);
     } // remove settings of previous team
     fnVisualTeam(oPC);
     ExecuteScript("adjust_alignment",oPC);
     DelayCommand(0.01,fnSetAppearance(oPC));
     DelayCommand(0.03,PurgeExistingEquipment(oPC));
     DelayCommand(0.3,fnEquipTeamMember(oPC));
   } // okay to switch teams or become leader
   else if (bLeader==TRUE)
   { // leaders cannot change teams
     SendMessageToPC(oPC,"Team Leaders cannot change teams!  You can appoint someone else a leader once the game has begun.");
     AssignCommand(oPC,ClearAllActions(TRUE));
     AssignCommand(oPC,JumpToObject(oWP));
   } // leaders cannot change teams
   else
   { // team is locked
     SendMessageToPC(oPC,"That team has been LOCKED in the game settings.  Players may not join it!");
     AssignCommand(oPC,ClearAllActions(TRUE));
     AssignCommand(oPC,JumpToObject(oWP));
   } // team is locked
}