New adt_array functions: CloneArray, FindStringInArray, FindCellInArray

Adjusted mapchooser/randomcycle/rockthevote and LoadMaps stock to use new array functions

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401670
This commit is contained in:
Michael McKoy
2007-11-09 23:46:05 +00:00
parent b3fa7316d5
commit daaf961c07
7 changed files with 165 additions and 66 deletions
+35 -16
View File
@@ -79,6 +79,17 @@ native Handle:CreateArray(blocksize=1, startsize=0);
*/
native ClearArray(Handle:array);
/**
* Clones an array, returning a new handle with the same size and data. This should NOT
* be confused with CloneHandle. This is a completely new handle with the same data but
* no relation to the original. You MUST close it.
*
* @param array Array handle to be cloned
* @return New handle to the cloned array object
* @error Invalid Handle
*/
native Handle:CloneArray(Handle:array);
/**
* Resizes an array. If the size is smaller than the current size,
* the array is truncated.
@@ -250,25 +261,33 @@ native RemoveFromArray(Handle:array, index);
native SwapArrayItems(Handle:array, index1, index2);
/**
* Returns true is the supplied string exists within the supplied array.
* Returns the index for the first occurance of the provided string. If the string
* cannot be located, -1 will be returned.
*
* @param array Array Handle.
* @param item String to search for
* @return True if found, false if not
* @error Invalid handle
* @return Array index, or -1 on failure
* @error Invalid Handle
*/
native FindStringInArray(Handle:array, const String:item[]);
/**
* Returns the index for the first occurance of the provided value. If the value
* cannot be located, -1 will be returned.
*
* @param array Array Handle.
* @param item Value to search for
* @return Array index, or -1 on failure
* @error Invalid Handle
*/
native FindValueInArray(Handle:array, item);
/**
* Backwards compatible stock - Use FindStringInArray()
* @deprecated Replaced by FindStringInArray()
*/
#pragma deprecated Use FindStringInArray() instead
stock bool:IsStringInArray(Handle:array, const String:item[])
{
decl String:curItem[64];
for (new i = 0; i < GetArraySize(array); i++)
{
GetArrayString(array, i, curItem, sizeof(curItem));
if(strcmp(item, curItem, false) == 0)
{
return true;
}
}
return false;
}
return (FindStringInArray(array, item) != -1);
}
+5
View File
@@ -275,6 +275,11 @@ stock FindTarget(client, const String:target[], bool:nobots = false, bool:immuni
{
continue;
}
if (FindStringInArray(array, buffer) != -1)
{
continue;
}
PushArrayString(array, buffer);
}
+19 -18
View File
@@ -83,9 +83,10 @@ public OnPluginStart()
{
LoadTranslations("mapchooser.phrases");
g_MapList = CreateArray(33);
g_OldMapList = CreateArray(33);
g_NextMapList = CreateArray(33);
new arraySize = ByteCountToCells(33);
g_MapList = CreateArray(arraySize);
g_OldMapList = CreateArray(arraySize);
g_NextMapList = CreateArray(arraySize);
g_TeamScores = CreateArray(2);
g_Cvar_StartTime = CreateConVar("sm_mapvote_start", "3.0", "Specifies when to start the vote based on time remaining.", _, true, 1.0);
@@ -512,31 +513,31 @@ CreateNextVote()
ClearArray(g_NextMapList);
}
new Handle:tempMaps = CreateArray(33);
decl String:map[32];
for (new i = 0; i < GetArraySize(g_MapList); i++)
{
GetArrayString(g_MapList, i, map, sizeof(map));
PushArrayString(tempMaps, map);
}
new Handle:tempMaps = CloneArray(g_MapList);
if (GetArraySize(tempMaps) > GetConVarInt(g_Cvar_ExcludeMaps))
if (GetConVarInt(g_Cvar_ExcludeMaps) && GetArraySize(tempMaps) > GetConVarInt(g_Cvar_ExcludeMaps))
{
for (new i = 0; i < GetArraySize(g_OldMapList); i++)
{
GetArrayString(g_OldMapList, i, map, sizeof(map));
for (new j = 0; j < GetArraySize(tempMaps); j++)
new index = FindStringInArray(tempMaps, map);
if (index != -1)
{
decl String:temp[32];
GetArrayString(tempMaps, j, temp, sizeof(temp));
if (strcmp(temp, map) == 0)
{
RemoveFromArray(tempMaps, j);
break;
}
RemoveFromArray(tempMaps, index);
}
}
}
else
{
// If we didn't check against ExcludeMaps, we have to remove the current map.
GetCurrentMap(map, sizeof(map));
new index = FindStringInArray(tempMaps, map);
if (index != -1)
{
RemoveFromArray(tempMaps, index);
}
}
new limit = (GetConVarInt(g_Cvar_IncludeMaps) < GetArraySize(tempMaps) ? GetConVarInt(g_Cvar_IncludeMaps) : GetArraySize(tempMaps));
for (new i = 0; i < limit; i++)
+5 -4
View File
@@ -53,8 +53,9 @@ new g_mapFileTime;
public OnPluginStart()
{
g_MapList = CreateArray(33);
g_OldMapList = CreateArray(33);
new arraySize = ByteCountToCells(33);
g_MapList = CreateArray(arraySize);
g_OldMapList = CreateArray(arraySize);
g_Cvar_Mapfile = CreateConVar("sm_randomcycle_file", "configs/maps.ini", "Map file to use. (Def sourcemod/configs/maps.ini)");
g_Cvar_ExcludeMaps = CreateConVar("sm_randomcycle_exclude", "5", "Specifies how many past maps to exclude from the vote.", _, true, 0.0);
@@ -83,7 +84,7 @@ public Action:Timer_RandomizeNextmap(Handle:timer)
decl String:map[32];
new bool:oldMaps = false;
if (GetArraySize(g_MapList) > GetConVarInt(g_Cvar_ExcludeMaps))
if (GetConVarInt(g_Cvar_ExcludeMaps) && GetArraySize(g_MapList) > GetConVarInt(g_Cvar_ExcludeMaps))
{
oldMaps = true;
}
@@ -91,7 +92,7 @@ public Action:Timer_RandomizeNextmap(Handle:timer)
new b = GetRandomInt(0, GetArraySize(g_MapList) - 1);
GetArrayString(g_MapList, b, map, sizeof(map));
while (oldMaps && IsStringInArray(g_OldMapList, map))
while (oldMaps && FindStringInArray(g_OldMapList, map) != -1)
{
b = GetRandomInt(0, GetArraySize(g_MapList) - 1);
GetArrayString(g_MapList, b, map, sizeof(map));
+14 -28
View File
@@ -71,8 +71,9 @@ public OnPluginStart()
LoadTranslations("common.phrases");
LoadTranslations("rockthevote.phrases");
g_MapList = CreateArray(33);
g_RTVMapList = CreateArray(33);
new arraySize = ByteCountToCells(33);
g_MapList = CreateArray(arraySize);
g_RTVMapList = CreateArray(arraySize);
g_Cvar_Needed = CreateConVar("sm_rtv_needed", "0.60", "Percentage of players needed to rockthevote (Def 60%)", 0, true, 0.05, true, 1.0);
g_Cvar_File = CreateConVar("sm_rtv_file", "configs/maps.ini", "Map file to use. (Def configs/maps.ini)");
@@ -161,23 +162,18 @@ public Action:Command_Addmap(client, args)
decl String:mapname[64];
GetCmdArg(1, mapname, sizeof(mapname));
if (!IsStringInArray(g_MapList, mapname))
if (FindStringInArray(g_MapList, mapname) == -1)
{
ReplyToCommand(client, "%t", "Map was not found", mapname);
return Plugin_Handled;
}
if (GetArraySize(g_RTVMapList) > 0)
{
for (new i = 0; i < GetArraySize(g_RTVMapList); i++)
{
decl String:nextmap[64];
GetArrayString(g_RTVMapList, i, nextmap, sizeof(nextmap));
if (strcmp(mapname, nextmap, false) == 0)
{
ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
return Plugin_Handled;
}
{
if (FindStringInArray(g_RTVMapList, mapname) != -1)
{
ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
return Plugin_Handled;
}
ShiftArrayUp(g_RTVMapList, 0);
@@ -336,13 +332,8 @@ public Action:Timer_StartRTV(Handle:timer)
new Handle:MapVoteMenu = CreateMenu(Handler_MapMapVoteMenu, MenuAction:MENU_ACTIONS_ALL);
SetMenuTitle(MapVoteMenu, "Rock The Vote");
new Handle:tempMaps = CreateArray(33);
new Handle:tempMaps = CloneArray(g_MapList);
decl String:map[32];
for (new i = 0; i < GetArraySize(g_MapList); i++)
{
GetArrayString(g_MapList, i, map, sizeof(map));
PushArrayString(tempMaps, map);
}
// We assume that g_RTVMapList is within the correct limits, based on the logic for nominations
for (new i = 0; i < GetArraySize(g_RTVMapList); i++)
@@ -350,15 +341,10 @@ public Action:Timer_StartRTV(Handle:timer)
GetArrayString(g_RTVMapList, i, map, sizeof(map));
AddMenuItem(MapVoteMenu, map, map);
for (new j = 0; j < GetArraySize(tempMaps); j++)
new index = FindStringInArray(tempMaps, map);
if (index != -1)
{
decl String:temp[32];
GetArrayString(tempMaps, j, temp, sizeof(temp));
if (strcmp(map, temp) == 0)
{
RemoveFromArray(tempMaps, j);
break;
}
RemoveFromArray(tempMaps, index);
}
}
@@ -489,7 +475,7 @@ public Handler_MapSelectMenu(Handle:menu, MenuAction:action, param1, param2)
decl String:map[64], String:name[64];
GetMenuItem(menu, param2, map, sizeof(map));
if (IsStringInArray(g_RTVMapList, map))
if (FindStringInArray(g_RTVMapList, map) != -1)
{
PrintToChat(param1, "[SM] %t", "Map Already Nominated");
return;