// I don't like prototyping.

float noexitdefault = 1;
float teamsdefault = 0;

float numplayers;
float exitTotal;    // don't laugh, there are no arrays or lists in QuakeC
float noexitTotal;
float teamtotal;
float noteamtotal;
float noexitvotetotal;
float noteamvotetotal;

/*************************************************************************/
// CountVotes - Self Explanatory
void() countvotes =                          
{
local string temps;
local entity ent;

 exitTotal=0;
 noexitTotal=0;
 teamtotal=0;
 noteamtotal=0;
 noexitvotetotal=0;
 noteamvotetotal=0;

 ent = find(world,classname,"player");

 while (ent != world)
  {
   if (ent.noexitvote == 1)
    noexitTotal=noexitTotal + 1;
   else if (ent.noexitvote == 0)
    exitTotal=exitTotal + 1;
   else
    noexitvotetotal=noexitvotetotal + 1;

   if (ent.teamplayvote == 1)
    teamtotal=teamtotal + 1;
   else if (ent.teamplayvote == 0)
    noteamtotal=noteamtotal + 1;
   else
    noteamvotetotal=noteamvotetotal + 1;

  ent = find(ent,classname,"player");
  }


 if ( teamtotal > (noteamtotal + noteamvotetotal))    //check for majority
  cvar_set("teamplay","1");                           //if none (tie), do
 else if ( noteamtotal > (teamtotal + noteamvotetotal))  //nothing.
  cvar_set("teamplay","0");

 if (exitTotal > (noexitTotal + noexitvotetotal))
  cvar_set("noexit","0");
 else if (noexitTotal > (exitTotal + noexitvotetotal))
  cvar_set("noexit","1");

 temps = ftos(noexitdefault);
 if ((exitTotal + noexitTotal) == 0)  //until someone votes, set to defaults.
  cvar_set("noexit",temps);
 temps = ftos(teamsdefault);
 if ((teamtotal + noteamtotal) == 0)
  cvar_set("teamplay",temps);

 numplayers=exitTotal+noexitTotal+noexitvotetotal; // not implemented

}; //countvotes


/*************************************************************************/
//VoteStats - Show vote stats
void(entity self) votestats =
        {
 local string tempstr;

         countvotes();

         sprint(self, "Allow level exit : ");
         tempstr = ftos(exitTotal);    // sprint(self,ftos(exitTotal)) doesnt 
         sprint(self, tempstr);        // seem to work.
         sprint(self, "\n");
         sprint(self, "Disallow level exit : ");
         tempstr = ftos(noexitTotal);
         sprint(self, tempstr);
         sprint(self, "\n");

         sprint(self, "Allow Friendly Fire : ");
         tempstr = ftos(noteamtotal);  
         sprint(self, tempstr);       
         sprint(self, "\n");
         sprint(self, "Disallow Friendly Fire : ");
         tempstr = ftos(teamtotal);
         sprint(self, tempstr);
         sprint(self, "\n");

        };

/************************************************************************/
// VoteExitYes - Vote for noexit = 0
void(entity self) voteexityes =
      {
       self.noexitvote = 0;
       countvotes();
      };

/************************************************************************/
//VoteExitNo - Vote for noexit = 1
void(entity self) voteexitno =
      {
       self.noexitvote = 1;
       countvotes();
      };
/********************************************************************/
// VoteTeamsNo - Vote for teamplay = 0
void(entity self) voteteamsno =
      {
       self.teamplayvote = 0;
       countvotes();
      };

/************************************************************************/
// VoteTeamsYes - Vote for teamplay = 1
void(entity self) voteteamsyes =
      {
       self.teamplayvote = 1;
       countvotes();
      };

/************************************************************************/

// RegVoter - Add new player to the voting booth.
void(entity self) regvoter =
{

self.noexitvote = 666;
self.teamplayvote = 666;
countvotes();

};  // regvoter

/***********************************************************************/
//VoteHelp - display command help
void() votehelp =
{
 sprint(self,"VoteStats : See voting status\nVoteExit : Vote for noexit = 0\nVoteNoExit : Vote for noexit = 1\nVoteTeams : Vote for teamplay = 1\nVoteNoTeams : Vote for teamplay = 0\n");
};
/***********************************************************************/
//VoteAliases - alias the impulse commands for user friendliness.
//FIXME:find a better place to call this.
//Note to QuakeC coders : this function doesn't seem to work when I do
// it as votealiases(entity self), which I like to do, even though it's
// unnecessary, just because it's logical. All the others seem to work
// whichever way I do them, but this one, when written and called as
// votealias(self), compiles, but on runtime gives some error "unknown
// command 'ishedalias'" (finishedalias?), which makes no sense.  If
// you can enlighten me, please do. (flame@pacbell.net)
void() votealiases =
{
 stuffcmd(self,"alias votestats \"impulse 12\"\n");
 stuffcmd(self,"alias votenoexit \"impulse 14\"\n");
 stuffcmd(self,"alias voteexit \"impulse 13\"\n");
 stuffcmd(self,"alias voteteams \"impulse 16\"\n");
 stuffcmd(self,"alias votenoteams \"impulse 17\"\n");
 stuffcmd(self,"alias votehelp \"impulse 18\"\n");
 stuffcmd(self,"alias vote \"impulse 18\"\n");

// votehelp();

};
/***************************************************************************/
//ConnectMessage - display a message on connect.
void() connectmessage =
{
 
 sprint(self,"This server is running Qvote v2.7, by Lunatik (flame@pacbell.net)\nServer settings Noexit and Teamplay can now be set democratically, via the following commands : \n");
 votealiases();
 votehelp();

};
