This tutorial shows how to create a simple plugin to allow players to vote on/off low gravity. It covers the following topics:
As with the previous tutorial, we will start with the standard template:
/*********************************************************
* Gravity Vote Plugin - Version 1.0 *
*********************************************************
* *
* Name: plugin_vote_gravity *
* Author: ravenousbugblatterbeast@hotmail.com *
* Released: 1st September 2002 *
* *
* Version 1.0: *
* *
* - Initial version *
* *
*********************************************************
*/
/* Includes */
#include <core>
#include <console>
#include <string>
#include <plugin>
#include <admin>
#include <adminlib>
/* Constants */
/* Global Variables */
new g_Version[]="1.0"; /* Plugin version number */
/* Function Declarations */
/* Event Handlers */
public plugin_init() {
plugin_registerinfo("Gravity Vote Plugin","Allows player to vote on/off low gravity",g_Version);
return PLUGIN_CONTINUE;
}
/* Command Handlers */
/* Support Functions */
The first thing we need to do is register a command admin_vote_gravity. We will register this with the same access level as admin_vote_map (ACCESS_VOTE_MAP) so that if server admin can use their their default_access setting in adminmod.cfg to determine if anyone on the server can start votes, or only selected players.
/* Event Handlers */
public plugin_init() {
plugin_registerinfo("Gravity Vote Plugin","Allows players to vote on/off low gravity",g_Version);
plugin_registercmd("admin_vote_gravity","AdminVoteGravity",ACCESS_VOTE_MAP,
"admin_vote_gravity: Starts a vote to change gravity setting.");
return PLUGIN_CONTINUE;
}
The command handler checks whether it is legal to vote at this time (allowable vote frequency is governed by the vote_freq setting in adminmod.cfg) and if so starts a vote using vote() function. NOTE: The last two arguments of the vote function are the name of the public function to call when the vote is complete (the vote handler) and a string to pass to the HLParam argument of the vote handler. In this particular case, there is no data that we need to pass. We return PLUGIN_HANDLED becase admin_vote_gravity is not a standard halflife command and therefore Admin Mod would produce an error message if we allowed it to continue.
public AdminVoteGravity(HLCommand,HLData,HLUserName,UserIndex) {
new Command[MAX_COMMAND_LENGTH];
new UserName[MAX_TEXT_LENGTH];
if (vote_allowed()!=1) {
selfmessage( "Vote not allowed at this time.");
return PLUGIN_HANDLED;
}
convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
convert_string(HLUserName,UserName,MAX_NAME_LENGTH);
say_command(User,Command,"");
vote("Choose a gravity setting:","Low Gravity","Normal Gravity","HandleGravityVote","");
return PLUGIN_HANDLED;
}
Like a command handler and an event handler, a vote handler must be a public function so Admin Mod can invoke it, and must have a predefined set of arguments:
public HandleGravityVote(WinningOption,HLParam,VoteCount,UserCount) {
We are going to run this vote on a simple majority, so we do not need to compare VoteCount and UserCount - we are only interested in WinningOption. If you would like an example of how the other arguments are used, check the source for admin_vote_kick / HandleKickVote in plugin_base.sma.
Our vote handler simply checks the winning option, then makes the appropriate gravity setting.
/* Handle a gravity vote's results. */
public HandleGravityVote(WinningOption,HLParam,VoteCount,UserCount) {
if (WinningOption == 1) {
say("Switching to low gravity due to vote.");
setvar("sv_gravity",200);
}
else {
say("Switching to normal gravity due to vote.");
setvar("sv_gravity",800);
}
return PLUGIN_HANDLED;
}
Copy the code below to a file plugin_vote_gravity, compile it and add it plugin.ini. You should then be able to perform gravity votes on your server.
/*********************************************************
* Gravity Vote Plugin - Version 1.0 *
*********************************************************
* *
* Name: plugin_vote_gravity *
* Author: ravenousbugblatterbeast@hotmail.com *
* Released: 1st September 2002 *
* *
* Version 1.0: *
* *
* - Initial version *
* *
*********************************************************
*/
/* Includes */
#include <core>
#include <console>
#include <string>
#include <plugin>
#include <admin>
#include <adminlib>
/* Constants */
/* Global Variables */
new g_Version[]="1.0"; /* Plugin version number */
/* Function Declarations */
forward AdminVoteGravity(HLCommand,HLData,HLUserName,UserIndex);
forward HandleGravityVote(WinningOption,HLParam,VoteCount,UserCount);
/* Event Handlers */
public plugin_init() {
plugin_registerinfo("Gravity Vote Plugin","Allows players to vote on/off low gravity",g_Version);
plugin_registercmd("admin_vote_gravity","AdminVoteGravity",ACCESS_VOTE_MAP,
"admin_vote_gravity: Starts a vote to change gravity setting.");
return PLUGIN_CONTINUE;
}
/* Command Handlers */
public AdminVoteGravity(HLCommand,HLData,HLUserName,UserIndex) {
new Command[MAX_COMMAND_LENGTH];
new UserName[MAX_TEXT_LENGTH];
if (vote_allowed()!=1) {
selfmessage( "Vote not allowed at this time.");
return PLUGIN_HANDLED;
}
convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
convert_string(HLUserName,UserName,MAX_NAME_LENGTH);
say_command(User,Command,"");
vote("Choose a gravity setting:","Low Gravity","Normal Gravity","HandleGravityVote","");
return PLUGIN_HANDLED;
}
/* Support Functions */
/* Handle a gravity vote's results. */
public HandleGravityVote(WinningOption,HLParam,VoteCount,UserCount) {
if (WinningOption == 1) {
say("Switching to low gravity due to vote.");
setvar("sv_gravity",200);
}
else {
say("Switching to normal gravity due to vote.");
setvar("sv_gravity",800);
}
return PLUGIN_HANDLED;
}