Difference between revisions of "Valve Developer Community:Sandbox"

From Valve Developer Community
Jump to: navigation, search
(bots_vs_humans41.sma)
(bots_vs_humans.sma (v4.2))
Line 35: Line 35:
 
- Using "get_players".
 
- Using "get_players".
 
- Check bots again later if at least one of the bots is in the wrong team or hasn't picked a class (is_something_wrong).
 
- Check bots again later if at least one of the bots is in the wrong team or hasn't picked a class (is_something_wrong).
 
+
4.2 Replaced client_cmd with engclient_cmd.
 
*/
 
*/
  
Line 42: Line 42:
  
 
#define PLUGIN "Bots vs. Humans"
 
#define PLUGIN "Bots vs. Humans"
#define VERSION "4.1"
+
#define VERSION "4.2"
 
#define AUTHOR "pizzahut"
 
#define AUTHOR "pizzahut"
  
Line 55: Line 55:
 
g_bvh_bot_team = register_cvar("bvh_bot_team", "1") // Which team should bots join? (1=blue, 2=red, 3=yellow, 4=green)
 
g_bvh_bot_team = register_cvar("bvh_bot_team", "1") // Which team should bots join? (1=blue, 2=red, 3=yellow, 4=green)
 
g_bvh_human_team = register_cvar("bvh_human_team", "2") // Which team should humans join? (1=blue, 2=red, 3=yellow, 4=green)
 
g_bvh_human_team = register_cvar("bvh_human_team", "2") // Which team should humans join? (1=blue, 2=red, 3=yellow, 4=green)
register_clcmd("jointeam", "clcmd_jointeam") // Only humans use client commands.
+
register_clcmd("changeteam", "force_human_team") // Sometimes the changeteam command skips team selection.
 +
register_clcmd("jointeam", "force_human_team") // Only humans use client commands.
 
register_clcmd("spectate", "clcmd_spectate") // Spectators use the "spectate" command.
 
register_clcmd("spectate", "clcmd_spectate") // Spectators use the "spectate" command.
 
register_logevent("logevent", 3, "1=joined team") // Switch bots to correct team.
 
register_logevent("logevent", 3, "1=joined team") // Switch bots to correct team.
// set_task(1.0,"task_check_bot_team",_,_,_,"b")
 
 
}
 
}
  
Line 65: Line 65:
 
///////////////////////////////////////////
 
///////////////////////////////////////////
  
public clcmd_jointeam(id)
+
public force_human_team(id)
 
{
 
{
static Arg1[2], newteam
+
static human_team[2]
 
+
if(!get_pcvar_num(g_bvh_enabled)) // Plugin enabled?
if(!get_pcvar_num(g_bvh_enabled)) // Plugin enabled?
+
return PLUGIN_CONTINUE // No --> Do nothing.
return PLUGIN_CONTINUE // No --> Do nothing.
+
get_pcvar_string(g_bvh_human_team, human_team, charsmax(human_team))
 
+
engclient_cmd(id, "jointeam", human_team) // Change human player to human team.
read_argv(1, Arg1, charsmax(Arg1))
+
set_task(1.0, "task_adjust_bot_count") // Adjust bot count when a human player joins a team.
newteam = str_to_num(Arg1)
+
return PLUGIN_HANDLED // Block the changeteam and jointeam commands.
if(newteam != get_pcvar_num(g_bvh_human_team))
 
{
 
client_cmd(id, "jointeam %d", get_pcvar_num(g_bvh_human_team))
 
return PLUGIN_HANDLED // Block the jointeam command.
 
}
 
set_task(1.0, "task_adjust_bot_count") // Adjust bot count when a human player joins a team.
 
return PLUGIN_CONTINUE
 
 
}
 
}
  
 
public clcmd_spectate(id)
 
public clcmd_spectate(id)
 
{
 
{
set_task(1.0, "task_adjust_bot_count") // Adjust bot count if a human player changes to spectators.
+
if(get_pcvar_num(g_bvh_enabled)) // Plugin enabled?
return PLUGIN_CONTINUE
+
set_task(1.0, "task_adjust_bot_count") // Adjust bot count if a human player changes to spectators.
 +
return PLUGIN_CONTINUE // Allow the spectate command.
 
}
 
}
  
 
public client_disconnect(id)
 
public client_disconnect(id)
set_task(1.0, "task_adjust_bot_count") // Adjust bot count if a player disconnects.
+
{
 +
if(get_pcvar_num(g_bvh_enabled)) // Plugin enabled?
 +
set_task(1.0, "task_adjust_bot_count") // Adjust bot count if a player disconnects.
 +
}
  
 
//////////////////////
 
//////////////////////
Line 168: Line 165:
 
}
 
}
 
}
 
}
if(is_something_wrong)
+
if(is_something_wrong) // Check again later whether the bots actually switched team / class.
set_task(0.1, "task_check_bot_team", 33) // Make damn sure that bots switch to the correct team.
+
set_task(0.1, "task_check_bot_team", 33)
 
}
 
}

Revision as of 21:12, 3 April 2013

The Sandbox (Valve Developer Community:Sandbox) is a VDC namespace page designed for testing and experimenting with Wiki syntax. Feel free to try your skills at formatting here: click on edit, make your changes, and click 'Save page' when you are finished. Content added here will not stay permanently.

Please do not edit the "markup" text, at the top of the editing window, that reads "{{sandbox}}". /*

Bots vs. Humans plugin for FoxBot / Team Fortress Classic

Features

- Forces bots into one team, humans into another. - Maintains a chosen bot-to-human ratio.

CVARs

bvh_enabled (default=1) // 1 = plugin enabled, 0 = disabled bvh_ratio (default=4) // bot-to-human ratio, bot count = humans * ratio bvh_bot_team (default=1) // Which team should bots join? (1=blue, 2=red, 3=yellow, 4=green) bvh_human_team (default=2) // Which team should humans join? (1=blue, 2=red, 3=yellow, 4=green)

Requirements

==

FoxBot settings:

bot_bot_balance 0 bot_team_balance 0 min_bots -1 max_bots -1

History

=

4.0 Initial plugin submission 4.1 Changes: - Replaced looping bot check task in "plugin_init" with single task in "logevent". - Removing existing bot check tasks prior to setting up a new one. - Using "get_players". - Check bots again later if at least one of the bots is in the wrong team or hasn't picked a class (is_something_wrong). 4.2 Replaced client_cmd with engclient_cmd.

  • /
  1. include <amxmodx>
  2. include <engine>
  1. define PLUGIN "Bots vs. Humans"
  2. define VERSION "4.2"
  3. define AUTHOR "pizzahut"

new g_bot_cvar, g_bvh_enabled, g_bvh_ratio, g_bvh_bot_team, g_bvh_human_team

public plugin_init() { register_plugin(PLUGIN, VERSION, AUTHOR) g_bot_cvar = get_cvar_pointer("bot") // FoxBot cvar through which commands are passed to the bots g_bvh_enabled = register_cvar("bvh_enabled", "1") // 1 = plugin enabled, 0 = disabled g_bvh_ratio = register_cvar("bvh_ratio", "4") // bot-to-human ratio, bot count = humans * ratio g_bvh_bot_team = register_cvar("bvh_bot_team", "1") // Which team should bots join? (1=blue, 2=red, 3=yellow, 4=green) g_bvh_human_team = register_cvar("bvh_human_team", "2") // Which team should humans join? (1=blue, 2=red, 3=yellow, 4=green) register_clcmd("changeteam", "force_human_team") // Sometimes the changeteam command skips team selection. register_clcmd("jointeam", "force_human_team") // Only humans use client commands. register_clcmd("spectate", "clcmd_spectate") // Spectators use the "spectate" command. register_logevent("logevent", 3, "1=joined team") // Switch bots to correct team. }

/////////////////////////////////////////// // Make sure humans join the human team. // ///////////////////////////////////////////

public force_human_team(id) { static human_team[2] if(!get_pcvar_num(g_bvh_enabled)) // Plugin enabled? return PLUGIN_CONTINUE // No --> Do nothing. get_pcvar_string(g_bvh_human_team, human_team, charsmax(human_team)) engclient_cmd(id, "jointeam", human_team) // Change human player to human team. set_task(1.0, "task_adjust_bot_count") // Adjust bot count when a human player joins a team. return PLUGIN_HANDLED // Block the changeteam and jointeam commands. }

public clcmd_spectate(id) { if(get_pcvar_num(g_bvh_enabled)) // Plugin enabled? set_task(1.0, "task_adjust_bot_count") // Adjust bot count if a human player changes to spectators. return PLUGIN_CONTINUE // Allow the spectate command. }

public client_disconnect(id) { if(get_pcvar_num(g_bvh_enabled)) // Plugin enabled? set_task(1.0, "task_adjust_bot_count") // Adjust bot count if a player disconnects. }

////////////////////// // Adjust bot count // //////////////////////

public task_adjust_bot_count(task_id) { static HumanCount, i, playerCount, Players[32], SpecCount, teamnumber

if(!get_pcvar_num(g_bvh_enabled)) // Plugin enabled? return // No --> Do nothing.

HumanCount = 0 // How many humans in teams? SpecCount = 0 // How many spectating humans?

get_players(Players, playerCount, "c") // skip bots, don't skip the HLTV for(i = 0; i < playerCount; i++) { teamnumber = get_user_team(Players[i]) if((1 <= teamnumber) && (teamnumber <= 4)) HumanCount++ else SpecCount++ } if(HumanCount == 0) HumanCount = 1 // Add some bots when server is empty. (Pretend we have 1 human player.) static bot_cmd[12] format(bot_cmd, charsmax(bot_cmd), "max_bots %d", HumanCount * (get_pcvar_num(g_bvh_ratio) + 1) + SpecCount) set_pcvar_string(g_bot_cvar, bot_cmd) // Send a command to FoxBot on a dedicated server. server_cmd(bot_cmd) // Send a command to FoxBot on a listen server. }

////////////////////////////////// // Change bots to the bot team. // //////////////////////////////////

public logevent() { remove_task(33) // Remove already running tasks. set_task(0.1, "task_check_bot_team", 33) // Switch bots to correct team. }

public task_check_bot_team(task_id) { static bot_team[2], i, id, bool:is_something_wrong, playerCount, Players[32]

if(!get_pcvar_num(g_bvh_enabled)) return is_something_wrong = false get_players(Players, playerCount, "dh") // skip real players, skip HLTV for(i = 0; i < playerCount; i++) { id = Players[i] if(entity_get_int(id, EV_INT_playerclass) == 0) { is_something_wrong = true switch(random_num(1,10)) { case 1: engclient_cmd(id, "scout") case 2: engclient_cmd(id, "sniper") case 3: engclient_cmd(id, "soldier") case 4: engclient_cmd(id, "demoman") case 5: engclient_cmd(id, "medic") case 6: engclient_cmd(id, "hwguy") case 7: engclient_cmd(id, "pyro") case 8: engclient_cmd(id, "spy") case 9: engclient_cmd(id, "engineer") case 10: engclient_cmd(id, "randompc") } } if(get_user_team(id) != get_pcvar_num(g_bvh_bot_team)) { is_something_wrong = true get_pcvar_string(g_bvh_bot_team, bot_team, charsmax(bot_team)) engclient_cmd(id, "jointeam", bot_team) } } if(is_something_wrong) // Check again later whether the bots actually switched team / class. set_task(0.1, "task_check_bot_team", 33) }