Fixed most of mapchooser.sp. May have issues with mp_winlimit on DoD. Timelimits may not function 100% in cstrike (Depends on something else, sorry)
authorMichael McKoy <ferret99gt@gmail.com>
Sat Sep 22 17:44:18 2007 +0000 (2007-09-22)
changeset 14528ea560a9915e
parent 1451 14d1cc64bd78
child 1453 034e43434794
Fixed most of mapchooser.sp. May have issues with mp_winlimit on DoD. Timelimits may not function 100% in cstrike (Depends on something else, sorry)
plugins/mapchooser.sp
     1.1 --- a/plugins/mapchooser.sp	Sat Sep 22 17:43:19 2007 +0000
     1.2 +++ b/plugins/mapchooser.sp	Sat Sep 22 17:44:18 2007 +0000
     1.3 @@ -57,24 +57,16 @@
     1.4  new Handle:g_NextMapList = INVALID_HANDLE;
     1.5  new Handle:g_VoteMenu = INVALID_HANDLE;
     1.6  
     1.7 -new g_MapCount;
     1.8 -new g_GameType;
     1.9  new bool:g_HasVoteStarted;
    1.10  new g_mapFileTime;
    1.11  
    1.12 -enum
    1.13 -{
    1.14 -	GAME_HL2 = 0,
    1.15 -	GAME_CSS,
    1.16 -	GAME_DOD
    1.17 -}
    1.18 -
    1.19  public OnPluginStart()
    1.20  {
    1.21  	LoadTranslations("mapchooser.phrases");
    1.22  	
    1.23  	g_MapList = CreateArray(33);
    1.24  	g_OldMapList = CreateArray(33);
    1.25 +	g_NextMapList = CreateArray(33);
    1.26  
    1.27  	g_Cvar_StartTime = CreateConVar("sm_mapvote_start", "3.0", "Specifies the time to start the vote when this much time remains.", _, true, 1.0);
    1.28  	g_Cvar_ExtendTimeMax = CreateConVar("sm_extendmap_maxtime", "90", "Specifies the maximum amount of time a map can be extended");
    1.29 @@ -87,20 +79,12 @@
    1.30  	decl String:FolderName[32];
    1.31  	GetGameFolderName(FolderName, sizeof(FolderName));
    1.32  
    1.33 -	if (strcmp(FolderName, "cstrike", false) == 0)
    1.34 -	{
    1.35 -		g_GameType = GAME_CSS;
    1.36 -		g_Cvar_Maxrounds = FindConVar("mp_maxrounds");
    1.37 -	}
    1.38 -	else if (strcmp(FolderName, "dod", false) == 0)
    1.39 +	g_Cvar_Winlimit = FindConVar("mp_winlimit");
    1.40 +	g_Cvar_Maxrounds = FindConVar("mp_maxrounds");
    1.41 +	
    1.42 +	if (g_Cvar_Winlimit != INVALID_HANDLE || g_Cvar_Maxrounds != INVALID_HANDLE)
    1.43  	{
    1.44 -		g_GameType = GAME_DOD;	
    1.45 -	}
    1.46 -	
    1.47 -	if (g_GameType)
    1.48 -	{
    1.49 -		g_Cvar_Winlimit = FindConVar("mp_winlimit");
    1.50 -		HookEvent("team_score", Event_TeamScore);
    1.51 +		HookEvent("round_end", Event_RoundEnd);
    1.52  	}
    1.53  }
    1.54  
    1.55 @@ -131,6 +115,7 @@
    1.56  public OnMapTimeLeftChanged()
    1.57  {
    1.58  	SetupTimeleftTimer();
    1.59 +	LogMessage("[MC] Timeleft changed, resetting timer.");
    1.60  }
    1.61  
    1.62  SetupTimeleftTimer()
    1.63 @@ -141,7 +126,7 @@
    1.64  		new startTime = GetConVarInt(g_Cvar_StartTime);
    1.65  		if (time - startTime < 0)
    1.66  		{
    1.67 -			InitiateVote();			
    1.68 +			InitiateVote();		
    1.69  		}
    1.70  		else
    1.71  		{
    1.72 @@ -149,7 +134,7 @@
    1.73  			{
    1.74  				KillTimer(g_VoteTimer);
    1.75  				g_VoteTimer = INVALID_HANDLE;
    1.76 -			}			
    1.77 +			}	
    1.78  			
    1.79  			g_VoteTimer = CreateTimer(float(time - startTime), Timer_StartMapVote, TIMER_FLAG_NO_MAPCHANGE);
    1.80  		}		
    1.81 @@ -158,11 +143,12 @@
    1.82  
    1.83  public Action:Timer_StartMapVote(Handle:timer)
    1.84  {
    1.85 -	if (!g_MapCount)
    1.86 +	if (!GetArraySize(g_MapList))
    1.87  	{
    1.88  		return Plugin_Stop;
    1.89  	}	
    1.90  	
    1.91 +	
    1.92  	if (timer == g_RetryTimer)
    1.93  	{
    1.94  		g_RetryTimer = INVALID_HANDLE;
    1.95 @@ -177,32 +163,44 @@
    1.96  	return Plugin_Stop;
    1.97  }
    1.98  
    1.99 -public Event_TeamScore(Handle:event, const String:name[], bool:dontBroadcast)
   1.100 +public Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
   1.101  {
   1.102 -	if (!g_MapCount)
   1.103 +	if (!GetArraySize(g_MapList))
   1.104  	{
   1.105  		return;
   1.106  	}
   1.107  
   1.108 -	static Score[2];
   1.109 -	new Team = GetEventInt(event, "teamid");
   1.110 -	Score[Team - 2] = GetEventInt(event, "score");
   1.111 +	new team = GetEventInt(event, "winner");
   1.112 +	if (team != 2 || team != 3)
   1.113 +	{
   1.114 +		return;
   1.115 +	}
   1.116 +	
   1.117 +	static score[2];
   1.118 +	
   1.119 +	score[team - 2]++;
   1.120  
   1.121 -	new winlimit = GetConVarInt(g_Cvar_Winlimit);
   1.122 -	if (winlimit)
   1.123 +	LogMessage("[MC] Team scored! %d - %d", score[0], score[1]);
   1.124 +	
   1.125 +	if (g_Cvar_Winlimit != INVALID_HANDLE)
   1.126  	{
   1.127 -		new Limit = winlimit - 2;
   1.128 -		if (Score[0] > Limit || Score[1] > Limit)
   1.129 +		new winlimit = GetConVarInt(g_Cvar_Winlimit);
   1.130 +		if (winlimit)
   1.131  		{
   1.132 -			InitiateVote();
   1.133 +			new Limit = winlimit - 2;
   1.134 +			if (score[0] > Limit || score[1] > Limit)
   1.135 +			{
   1.136 +				InitiateVote();
   1.137 +			}
   1.138  		}
   1.139  	}
   1.140 -	else if (g_GameType == GAME_CSS)
   1.141 +	
   1.142 +	if (g_Cvar_Maxrounds != INVALID_HANDLE)
   1.143  	{
   1.144  		new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
   1.145  		if (maxrounds)
   1.146  		{
   1.147 -			if ((Score[0] + Score[1]) > maxrounds - 2)
   1.148 +			if ((score[0] + score[1]) > maxrounds - 2)
   1.149  			{
   1.150  				InitiateVote();
   1.151  			}			
   1.152 @@ -228,35 +226,30 @@
   1.153  	g_VoteMenu = CreateMenu(Handler_MapVoteMenu, MenuAction:MENU_ACTIONS_ALL);
   1.154  	SetMenuTitle(g_VoteMenu, "Vote Nextmap");
   1.155  
   1.156 -	KvRewind(g_NextMapList);
   1.157 -	KvGotoFirstSubKey(g_NextMapList);
   1.158 -
   1.159  	decl String:map[32];
   1.160 -	do
   1.161 +	for (new i = 0; i < GetArraySize(g_NextMapList); i++)
   1.162  	{
   1.163 -		KvGetSectionName(g_NextMapList, map, sizeof(map));
   1.164 +		GetArrayString(g_NextMapList, i, map, sizeof(map));
   1.165  		AddMenuItem(g_VoteMenu, map, map);
   1.166  	}
   1.167 -	while (KvGotoNextKey(g_NextMapList));
   1.168  
   1.169 -	new bool:AllowExtend, time;
   1.170 +	new bool:allowExtend, time;
   1.171  	if (GetMapTimeLimit(time) && time > 0 && time < GetConVarInt(g_Cvar_ExtendTimeMax))
   1.172  	{
   1.173 -		AllowExtend = true;
   1.174 +		allowExtend = true;
   1.175  	}
   1.176  	
   1.177 -	if (g_GameType)
   1.178 +	if (g_Cvar_Winlimit != INVALID_HANDLE && GetConVarInt(g_Cvar_Winlimit) < GetConVarInt(g_Cvar_ExtendRoundMax))
   1.179  	{
   1.180 -		// Yes, I could short circuit this above. But I find it cleaner to break
   1.181 -		// it up into two if's
   1.182 -		if (GetConVarInt(g_Cvar_Maxrounds) < GetConVarInt(g_Cvar_ExtendRoundMax)
   1.183 -			|| GetConVarInt(g_Cvar_Winlimit) < GetConVarInt(g_Cvar_ExtendRoundMax))
   1.184 -		{
   1.185 -			AllowExtend = true;
   1.186 -		}
   1.187 +		allowExtend = true;
   1.188  	}	
   1.189 +	
   1.190 +	if (g_Cvar_Maxrounds != INVALID_HANDLE && GetConVarInt(g_Cvar_Maxrounds) < GetConVarInt(g_Cvar_ExtendRoundMax))
   1.191 +	{
   1.192 +		allowExtend = true;
   1.193 +	}
   1.194  
   1.195 -	if (AllowExtend)
   1.196 +	if (allowExtend)
   1.197  	{
   1.198  		AddMenuItem(g_VoteMenu, "##extend##", "Extend");
   1.199  	}
   1.200 @@ -338,22 +331,21 @@
   1.201  					}
   1.202  				}
   1.203  				
   1.204 -				if (g_GameType)
   1.205 +				if (g_Cvar_Winlimit != INVALID_HANDLE)
   1.206  				{
   1.207 -					new roundstep = GetConVarInt(g_Cvar_ExtendRoundStep);
   1.208  					new winlimit = GetConVarInt(g_Cvar_Winlimit);
   1.209 -					if (winlimit < GetConVarInt(g_Cvar_ExtendRoundMax))
   1.210 +					if (winlimit && winlimit < GetConVarInt(g_Cvar_ExtendRoundMax))
   1.211  					{
   1.212 -						SetConVarInt(g_Cvar_Winlimit, winlimit + roundstep);
   1.213 -					}
   1.214 -					
   1.215 -					if (g_GameType == GAME_CSS)
   1.216 +						SetConVarInt(g_Cvar_Winlimit, winlimit + GetConVarInt(g_Cvar_ExtendRoundStep));
   1.217 +					}					
   1.218 +				}
   1.219 +				
   1.220 +				if (g_Cvar_Maxrounds != INVALID_HANDLE)
   1.221 +				{
   1.222 +					new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
   1.223 +					if (maxrounds && maxrounds < GetConVarInt(g_Cvar_ExtendRoundMax))
   1.224  					{
   1.225 -						new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
   1.226 -						if (maxrounds < GetConVarInt(g_Cvar_ExtendRoundMax))
   1.227 -						{
   1.228 -							SetConVarInt(g_Cvar_Maxrounds, maxrounds + roundstep);
   1.229 -						}
   1.230 +						SetConVarInt(g_Cvar_Maxrounds, maxrounds + GetConVarInt(g_Cvar_ExtendRoundStep));
   1.231  					}
   1.232  				}
   1.233  
   1.234 @@ -382,42 +374,40 @@
   1.235  		RemoveFromArray(g_OldMapList, 0);
   1.236  	}
   1.237  		
   1.238 -	PrintToChatAll("[SM] %t", "Nextmap Voting Finished");
   1.239 +	PrintToChatAll("[SM] %t", "Nextmap Voting Finished", map);
   1.240  	LogMessage("Voting for next map has finished. Nextmap: %s.", map);	
   1.241  }
   1.242  
   1.243  CreateNextVote()
   1.244  {
   1.245 -	if(IsValidHandle(g_NextMapList))
   1.246 +	if(g_NextMapList != INVALID_HANDLE)
   1.247  	{
   1.248 -		CloseHandle(g_NextMapList);
   1.249 -	}
   1.250 +		ClearArray(g_NextMapList);
   1.251 +	}	
   1.252  	
   1.253 -	g_NextMapList = CreateKeyValues("MapChooser");
   1.254 +	decl String:map[32];
   1.255 +	new limit = (MAX_MAPS_SELECTION < GetArraySize(g_MapList) ? MAX_MAPS_SELECTION : GetArraySize(g_MapList));
   1.256  	
   1.257  	new bool:oldMaps = false;
   1.258 -	if (g_MapCount > GetConVarInt(g_Cvar_ExcludeMaps))
   1.259 +	if (limit > GetConVarInt(g_Cvar_ExcludeMaps))
   1.260  	{
   1.261 +		limit -= GetArraySize(g_OldMapList);
   1.262  		oldMaps = true;
   1.263  	}
   1.264  	
   1.265 -	decl String:map[32];
   1.266 -	for (new i = 0, b, count; i < g_MapCount; i++)
   1.267 +	for (new i = 0; i < limit; i++)
   1.268  	{
   1.269 -		b = GetRandomInt(0, g_MapCount - 1);
   1.270 +		new b = GetRandomInt(0, limit - 1);
   1.271  		GetArrayString(g_MapList, b, map, sizeof(map));
   1.272 -
   1.273 -		if (!IsMapSelected(map) && !(oldMaps && IsMapOld(map)))
   1.274 +		
   1.275 +		while (IsStringInArray(g_NextMapList, map) || (oldMaps && IsStringInArray(g_OldMapList, map)))
   1.276  		{
   1.277 -			
   1.278 -			KvJumpToKey(g_NextMapList, map, true);
   1.279 -			count++;
   1.280 +			b = GetRandomInt(0, limit - 1);
   1.281 +			GetArrayString(g_MapList, b, map, sizeof(map));
   1.282  		}
   1.283 -
   1.284 -		if (count == MAX_MAPS_SELECTION || count >= 9)
   1.285 -		{
   1.286 -			break;
   1.287 -		}
   1.288 +		
   1.289 +		LogMessage("[MC] Map was good, %s pushed on array.", map);
   1.290 +		PushArrayString(g_NextMapList, map);		
   1.291  	}
   1.292  }
   1.293  
   1.294 @@ -440,7 +430,6 @@
   1.295  	{
   1.296  		LogError("Unable to locate g_Cvar_Mapfile or mapcyclefile, no maps loaded.");
   1.297  		
   1.298 -		g_MapCount = 0;
   1.299  		if (g_MapList != INVALID_HANDLE)
   1.300  		{
   1.301  			ClearArray(g_MapList);
   1.302 @@ -454,19 +443,18 @@
   1.303  	new fileTime =  GetFileTime(mapPath, FileTime_LastChange);
   1.304  	if (g_mapFileTime == fileTime)
   1.305  	{
   1.306 -		return g_MapCount;
   1.307 +		return GetArraySize(g_MapList);
   1.308  	}
   1.309  	
   1.310  	g_mapFileTime = fileTime;
   1.311  	
   1.312  	// Reset the array
   1.313 -	g_MapCount = 0;
   1.314  	if (g_MapList != INVALID_HANDLE)
   1.315  	{
   1.316  		ClearArray(g_MapList);
   1.317  	}
   1.318  
   1.319 -	PrintToServer("[SM] Loading mapchooser map file [%s]", mapPath);
   1.320 +	LogMessage("[SM] Loading mapchooser map file [%s]", mapPath);
   1.321  
   1.322  	decl String:currentMap[32];
   1.323  	GetCurrentMap(currentMap, sizeof(currentMap));
   1.324 @@ -495,36 +483,13 @@
   1.325  		}
   1.326  
   1.327  		PushArrayString(g_MapList, buffer);
   1.328 -		g_MapCount++;
   1.329  	}
   1.330  
   1.331  	CloseHandle(file);
   1.332 -	return g_MapCount;
   1.333 +	return GetArraySize(g_MapList);
   1.334  }
   1.335  
   1.336  stock bool:IsValidCvarChar(c)
   1.337  {
   1.338  	return (c == '_' || IsCharAlpha(c) || IsCharNumeric(c));
   1.339 -}
   1.340 -
   1.341 -stock bool:IsMapSelected(const String:Map[])
   1.342 -{
   1.343 -	KvRewind(g_NextMapList);
   1.344 -	return KvJumpToKey(g_NextMapList, Map);
   1.345 -}
   1.346 -
   1.347 -stock bool:IsMapOld(const String:map[])
   1.348 -{
   1.349 -	decl String:oldMap[64];
   1.350 -	
   1.351 -	for (new i = 0; i < GetArraySize(g_OldMapList); i++)
   1.352 -	{
   1.353 -		GetArrayString(g_OldMapList, i, oldMap, sizeof(oldMap));
   1.354 -		if(strcmp(map, oldMap, false) == 0)
   1.355 -		{
   1.356 -			return false;
   1.357 -		}		
   1.358 -	}
   1.359 -
   1.360 -	return true;
   1.361  }
   1.362 \ No newline at end of file