plugins/randomcycle.sp
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plugins/randomcycle.sp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,119 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod Random Map Cycle Plugin
     1.8 + * Randomly picks a map from the mapcycle.
     1.9 + *
    1.10 + * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
    1.11 + * =============================================================================
    1.12 + *
    1.13 + * This program is free software; you can redistribute it and/or modify it under
    1.14 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.15 + * Free Software Foundation.
    1.16 + * 
    1.17 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.18 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.19 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.20 + * details.
    1.21 + *
    1.22 + * You should have received a copy of the GNU General Public License along with
    1.23 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.24 + *
    1.25 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.26 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.27 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.28 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.29 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.30 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.31 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.32 + * or <http://www.sourcemod.net/license.php>.
    1.33 + *
    1.34 + * Version: $Id$
    1.35 + */
    1.36 +
    1.37 +#pragma semicolon 1
    1.38 +#include <sourcemod>
    1.39 +
    1.40 +public Plugin:myinfo =
    1.41 +{
    1.42 +	name = "RandomCycle",
    1.43 +	author = "AlliedModders LLC",
    1.44 +	description = "Randomly chooses the next map.",
    1.45 +	version = SOURCEMOD_VERSION,
    1.46 +	url = "http://www.sourcemod.net/"
    1.47 +};
    1.48 +
    1.49 +new Handle:g_Cvar_Nextmap = INVALID_HANDLE;
    1.50 +new Handle:g_Cvar_ExcludeMaps = INVALID_HANDLE;
    1.51 +
    1.52 +new Handle:g_MapList = INVALID_HANDLE;
    1.53 +new Handle:g_OldMapList = INVALID_HANDLE;
    1.54 +new g_mapListSerial = -1;
    1.55 +
    1.56 +public OnPluginStart()
    1.57 +{
    1.58 +	new arraySize = ByteCountToCells(33);	
    1.59 +	g_MapList = CreateArray(arraySize);
    1.60 +	g_OldMapList = CreateArray(arraySize);
    1.61 +
    1.62 +	g_Cvar_ExcludeMaps = CreateConVar("sm_randomcycle_exclude", "5", "Specifies how many past maps to exclude from the vote.", _, true, 0.0);
    1.63 +	
    1.64 +	AutoExecConfig(true, "randomcycle");
    1.65 +}
    1.66 +
    1.67 +public OnConfigsExecuted()
    1.68 +{
    1.69 +	g_Cvar_Nextmap = FindConVar("sm_nextmap");
    1.70 +
    1.71 +	if (g_Cvar_Nextmap == INVALID_HANDLE)
    1.72 +	{
    1.73 +		LogError("FATAL: Cannot find sm_nextmap cvar. RandomCycle not loaded.");
    1.74 +		SetFailState("sm_nextmap not found");
    1.75 +	}
    1.76 +	
    1.77 +	if (ReadMapList(g_MapList, 
    1.78 +					g_mapListSerial, 
    1.79 +					"randomcycle", 
    1.80 +					MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER)
    1.81 +		== INVALID_HANDLE)
    1.82 +	{
    1.83 +		if (g_mapListSerial == -1)
    1.84 +		{
    1.85 +			LogError("Unable to create a valid map list.");
    1.86 +		}
    1.87 +	}
    1.88 +	
    1.89 +	CreateTimer(5.0, Timer_RandomizeNextmap); // Small delay to give Nextmap time to complete OnMapStart()
    1.90 +}
    1.91 +
    1.92 +public Action:Timer_RandomizeNextmap(Handle:timer)
    1.93 +{
    1.94 +	decl String:map[32];
    1.95 +
    1.96 +	new bool:oldMaps = false;
    1.97 +	if (GetConVarInt(g_Cvar_ExcludeMaps) && GetArraySize(g_MapList) > GetConVarInt(g_Cvar_ExcludeMaps))
    1.98 +	{
    1.99 +		oldMaps = true;
   1.100 +	}
   1.101 +	
   1.102 +	new b = GetRandomInt(0, GetArraySize(g_MapList) - 1);
   1.103 +	GetArrayString(g_MapList, b, map, sizeof(map));
   1.104 +
   1.105 +	while (oldMaps && FindStringInArray(g_OldMapList, map) != -1)
   1.106 +	{
   1.107 +		b = GetRandomInt(0, GetArraySize(g_MapList) - 1);
   1.108 +		GetArrayString(g_MapList, b, map, sizeof(map));
   1.109 +	}
   1.110 +	
   1.111 +	SetConVarString(g_Cvar_Nextmap, map);
   1.112 +	PushArrayString(g_OldMapList, map);
   1.113 +
   1.114 +	if (GetArraySize(g_OldMapList) > GetConVarInt(g_Cvar_ExcludeMaps))
   1.115 +	{
   1.116 +		RemoveFromArray(g_OldMapList, 0);
   1.117 +	}
   1.118 +
   1.119 +	LogMessage("RandomCycle has chosen %s for the nextmap.", map);	
   1.120 +
   1.121 +	return Plugin_Stop;
   1.122 +}
   1.123 \ No newline at end of file