/* This plugin enables gravity votes by clients and auto-responses. 
 * Note: you might want to turn your vote_freq down to 15 or 30 in server.cfg if your going to have a lot of voting happening (FF, Grav, other ect.)
 */
 
/* $Id: plugin_gv.sma,v 1.0 2001/04/08 based on yensid's plugin_ff, Cut and pasted :) by BenTheMeek $ 
 * $Id: plugin_gv.sma,v 1.1 2001/04/18 Made script a bit more robust to handle fact admins may manually set the upper gravity to something other than 780.
 *                                     Also changed the text for the "gravity" check to indicate it is just a status.  Would trigger when someone would vote
 *                                     and was confusing.  Also indicates that a vote is not available if one is requested before vote_freq limit was reached.
 *                                     -[MayheM]-
 * $
*/
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
/* Change this to whatever ratio of players need to vote for a setting to change it. */
#define GV_VOTE_RATIO 50
 
/* Change to 1 to have a gravity vote start 30 seconds in to a map */
#define AUTO_GV_VOTE 0
 
#define ACCESS_VOTE_GV 1
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.0";
 
public GVVote() {
	new strDummy[10];
	vote("What Gravity?", "Normal Gravity", "Low Gravity", "HandleGVVote",strDummy);
}
 
public HandleGVVote(WinningOption,HLData,VoteCount,UserCount) {
	new Text[MAX_TEXT_LENGTH];
	new strNumber[MAX_NUMBER_LENGTH];
	new Ratio = GV_VOTE_RATIO;
	new strData[MAX_DATA_LENGTH];
	convert_string(HLData, strData,MAX_DATA_LENGTH);
 
	if (VoteCount >= Ratio*UserCount/100) {	
		if (WinningOption==1) {
			if(getvar("sv_gravity") > 600) {
				centersay("Vote over.  Normal Gravity will stay as is.",18,249,244,0);
			} else {
				centersay("Vote successful. Changed to Normal Gravity ^nSo Just don't fall!!!",18,249,244,0);
				exec("sv_gravity 780");
			}
		} else {
			if(getvar("sv_gravity") > 600) {
				centersay("OK Ok...You got your way!^nLow Gravity is On...^nJump away!",18,63,187,239);
				exec("sv_gravity 200");
			} else {
				centersay("Vote over.  Low gravity will remain.",18,63,187,239);
			}
		}
	} else {
		numtostr(Ratio*UserCount/100,strNumber);
		if(getvar("sv_gravity") > 600) {
			snprintf(Text, MAX_TEXT_LENGTH, "Map vote succeeded, but not enough votes for change (needed %s)^nNormal Gravity will remain.", strNumber);
		} else {
			snprintf(Text, MAX_TEXT_LENGTH, "Map vote succeeded, but not enough votes for change (needed %s)^nLowGravity will remain.", strNumber);
		}
		centersay(Text,18,63,187,239);
	}
}
 
public admin_vote_gravity(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
 
	if (vote_allowed() != 1) {
		selfmessage( "Vote not allowed at this time.");
		return PLUGIN_HANDLED;
	}
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	say_command(User,Command,Data);		
	GVVote();
	return PLUGIN_HANDLED;
}
 
public HandleSay(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	strstripquotes(Data);
	new Match = FALSE;
	if (strcasestr(Data, "gravity")!=-1) {
		Match = TRUE;
	}
	if (Match==TRUE) {
		if (getvar("sv_gravity") > 500){ 
			centersay (" Gravity Status: ^n Normal Gravity! Don't Fall! ^n To change say vote_gravity!",7,0,255,0); 
		} else {
			centersay (" Gravity Status: ^n Low Gravity is on. ^n To change say vote_gravity. ",7,0,255,0);
		}
	}
 
	if (strcasestr(Data, "vote_gravity")!=-1) { 
      if (vote_allowed()!=1) {
         centersay("^nVote not allowed at this time.",7,0,255,0);
      }
      else {
   		GVVote(); 
      }
	}
 
	return PLUGIN_CONTINUE;
}
 
 
public plugin_init() {
	plugin_registerinfo("Gravity Plugin","Enables Gravity vote normal/low and auto-responses.",STRING_VERSION);
	plugin_registercmd("admin_vote_gravity","admin_vote_gravity",ACCESS_VOTE_GV,"admin_vote_gravity : Starts a vote to enable normal/low gravity.");
	plugin_registercmd("say","HandleSay",ACCESS_ALL);
	plugin_registerhelp("say",ACCESS_ALL,"say gravity: Will give gravity status, Clients say vote_gravity will start vote.");
	plugin_registerhelp("say",ACCESS_ALL,"say vote_gravity: Will start gravity vote.");
 
#if AUTO_GV_VOTE==1	
	set_timer("GVVote",30,1);
#endif
 
	return PLUGIN_CONTINUE;
}