/////////////////////////////////////////////////////////////////////////////////
// Real Time Strategy - NWN - Voting Process
//===============================================================================
// By Deva Bryson Winblood.  03/09/2003
/////////////////////////////////////////////////////////////////////////////////

void fnClearVotes()
{ // set all votes to unvoted  1=YES, 2=NO
  object oPC=GetFirstPC();
  while(oPC!=OBJECT_INVALID)
  {
    SetLocalInt(oPC,"nRTSVote",0);
    oPC=GetNextPC();
  }
} // fnClearVotes()

void fnVotePasses(int nVoteType)
{ // the vote passed
  object oVoter=GetLocalObject(GetModule(),"oRTSVoter");
  string sID=GetLocalString(oVoter,"sTeamID");
  object oWP=GetWaypointByTag(sID+"_START");
  switch(nVoteType)
  { //vote switch
    case 1: { // forced end game
      DelayCommand(2.0,ExecuteScript("rts_forced_end",OBJECT_SELF));
      break;
    } // forced end game
    case 2: { // allow stuck player to return home
      AssignCommand(oVoter,ClearAllActions(TRUE));
      AssignCommand(oVoter,JumpToObject(oWP));
      break;
    } // allow stuck player to return home
    case 3: { // shut down all non-essential processes
      SetLocalInt(GetModule(),"nDisableNonEssential",TRUE);
      break;
    } // shut down all non-essential processes
    case 4: { // deep statistical dump
      ExecuteScript("deep_debug_dump",GetModule());
      break;
    } // deep statistical dump
    case 5: { // clean up common areas
      ExecuteScript("cleanup_common",GetModule());
      break;
    } // clean up common areas
    case 6: { // delete my units outside my lair
      ExecuteScript("delete_my_units",GetLocalObject(GetModule(),"oRTSVoteInitiate"));
      break;
    } // delete my units outside my lair
  } //vote switch
} // the vote passed



void fnVotingProcess()
{ // fnVotingProcess
  int nYea=0;
  int nNay=0;
  int nNoVote=0;
  int nVote;
  object oMod=GetModule();
  object oPC=GetFirstPC();
  while(oPC!=OBJECT_INVALID&&nNay==0)
  { // still PCs and no NAY vote
   nVote=GetLocalInt(oPC,"nRTSVote");
   if (nVote==1) nYea++;
   else if (nVote==2) nNay++;
   else
   { // has not voted
     nNoVote++;
     if (!IsInConversation(oPC))
     { // not in conversation
       AssignCommand(oPC,ActionStartConversation(oPC,"rts_vote",TRUE));
     } // not in conversation
   } // has not voted
   oPC=GetNextPC();
  } // still PCs and no NAY vote
  if (nNay==0&&nNoVote==0)
  { // motion passes
    fnVotePasses(GetLocalInt(oMod,"nRTSVoteType"));
    SetLocalInt(oMod,"nRTSVoteStart",FALSE);
  } // motion passes
  else if (nNay==0&&nNoVote!=0)
  { // still players who need to vote
    DelayCommand(5.0,fnVotingProcess()); // recursion later time
  } // still players who need to vote
  else
  {
    SendMessageToPC(OBJECT_SELF,"Your vote did not pass.");
    SetLocalInt(oMod,"nRTSVoteStart",FALSE);
  }
} // fnVotingProcess



void main()
{
   object oPC=OBJECT_SELF;
   AssignCommand(oPC,ClearAllActions());
   fnClearVotes();
   SetLocalInt(oPC,"nRTSVote",1);
   fnVotingProcess();
}