Added basic class API: ZR_IsValidClassIndex, ZR_GetActiveClass, ZR_SelectClientClass, ZR_GetClassByName, ZR_GetClassDisplayName

This commit is contained in:
Richard Helgeby
2012-09-08 22:34:31 +02:00
parent 393044aa87
commit 8bec3be02d
10 changed files with 562 additions and 68 deletions

View File

@ -235,7 +235,7 @@ stock ClassGetGroup(index, String:buffer[], maxlen, cachetype = ZR_CLASS_CACHE_P
}
/**
* Gets the class name to be displayed in the class menu.
* Gets the class name displayed in the class menu.
*
* @param index Index of the class in a class cache or a client index,
* depending on the cache type specified.

View File

@ -340,77 +340,22 @@ ClassMenuSelect(client, teamid)
*/
public ClassMenuSelectHandle(Handle:menu, MenuAction:action, client, slot)
{
decl String:classname[MENU_LINE_REG_LENGTH];
new classindex;
new teamid;
decl String:className[MENU_LINE_REG_LENGTH];
new classIndex;
new bool:autoclose = GetConVarBool(g_hCvarsList[CVAR_CLASSES_MENU_AUTOCLOSE]);
new bool:iszombie = InfectIsClientInfected(client);
switch (action)
{
case MenuAction_Select:
{
// Get class name from the information string.
GetMenuItem(menu, slot, classname, sizeof(classname));
GetMenuItem(menu, slot, className, sizeof(className));
// Solve class index from the name.
classindex = ClassGetIndex(classname);
classIndex = ClassGetIndex(className);
// Solve teamid from the class index.
teamid = ClassGetTeamID(classindex, ZR_CLASS_CACHE_MODIFIED);
// Allow instant class change if enabled and both class and player is human.
if (ClassAllowInstantChange[client] && !iszombie && teamid == ZR_CLASS_TEAM_HUMANS)
{
// Directly change the selected class index.
ClassSelected[client][teamid] = classindex;
// Update cache and apply attributes.
ClassReloadPlayerCache(client, classindex);
ClassApplyAttributes(client);
}
else
{
// Check if the player is alive.
if (IsPlayerAlive(client))
{
// Set next spawn index if the player is changing the class on
// his active team.
if ((iszombie && teamid == ZR_CLASS_TEAM_ZOMBIES) ||
(!iszombie && teamid == ZR_CLASS_TEAM_HUMANS) ||
(ClassPlayerInAdminMode[client] && teamid == ZR_CLASS_TEAM_ADMINS))
{
// Check if player selected the same class that he already is.
if (ClassSelected[client][teamid] == classindex)
{
// Player is already the specified class. Disable
// next class for the specified team.
ClassSelectedNext[client][teamid] = -1;
}
else
{
// Set class to be used on next spawn.
ClassSelectedNext[client][teamid] = classindex;
}
}
else
{
// Directly change the selected class index.
ClassSelected[client][teamid] = classindex;
}
}
else
{
// Player isn't alive. The class can be directly changed.
ClassSelected[client][teamid] = classindex;
}
}
// Save selected class index in cookie if enabled.
if (GetConVarBool(g_hCvarsList[CVAR_CLASSES_SAVE]))
{
CookiesSetInt(client, g_hClassCookieClassSelected[teamid], classindex + 1);
}
// Select (and eventually apply) class.
ClassSelectClientClass(client, classIndex);
}
case MenuAction_Cancel:
{

View File

@ -587,12 +587,12 @@ stock ClassValidateEditableAttributes(attributes[ClassEditableAttributes])
/**
* Checks if the specified class index is a valid index.
*
* @param classindex The class index to validate.
* @param classIndex The class index to validate.
* @return True if the class exist, false otherwise.
*/
stock bool:ClassValidateIndex(classindex)
stock bool:ClassValidateIndex(classIndex)
{
if (classindex >= 0 && classindex < ClassCount)
if (classIndex >= 0 && classIndex < ClassCount)
{
return true;
}
@ -660,10 +660,10 @@ stock ClassGetIndex(const String:name[], cachetype = ZR_CLASS_CACHE_MODIFIED)
{
decl String:current_name[64];
// Check if there are no classes.
if (ClassCount == 0)
// Check if there are no classes, or reading from player cache.
if (ClassCount == 0 || cachetype == ZR_CLASS_CACHE_PLAYER)
{
return false;
return -1;
}
// Loop through all classes.

View File

@ -377,6 +377,16 @@ enum ClassSpeedMethods
ClassSpeed_Prop, /** Modifies players' max speed property(m_flMaxspeed). */
}
/**
* Results when selecting a class for a player.
*/
enum ClassSelectResult
{
ClassSelected_NoChange, /** No class change was necessary (class already selected). */
ClassSelected_Instant, /** Class was instantly changed. */
ClassSelected_NextSpawn /** Class will be used next spawn. */
}
/**
* Empty filter structure.
*/
@ -1196,6 +1206,86 @@ ClassClientSetDefaultIndexes(client = -1)
}
}
/**
* Selects a class for a player.
*
* Human class attribute may be instantly applied if player is alive, human and
* instant class change is enabled. Otherwise only the selected index will be
* updated for next spawn.
*
* Class selection will be saved in client cookies if enabled.
*
* @param client Client index.
* @param classIndex Class index.
* @param applyIfPossible Optional. Apply class attributes if conditions allow
* it. Default is true.
* @param saveIfEnabled Optional. Save class selection in client cookies if
* enabled. Default is true.
*
* @return Class selection result. See enum ClassSelectResult.
*/
ClassSelectResult:ClassSelectClientClass(client, classIndex, bool:applyIfPossible = true, bool:saveIfEnabled = true)
{
new bool:iszombie = InfectIsClientInfected(client);
new teamid = ClassGetTeamID(classIndex, ZR_CLASS_CACHE_MODIFIED);
new ClassSelectResult:selectResult = ClassSelected_NoChange;
// Allow instant class change if enabled and both class and player is human.
if (applyIfPossible &&
ClassAllowInstantChange[client] &&
!iszombie && teamid == ZR_CLASS_TEAM_HUMANS)
{
// Update selected class index.
ClassSelected[client][teamid] = classIndex;
// Update cache and apply attributes.
ClassReloadPlayerCache(client, classIndex);
ClassApplyAttributes(client);
selectResult = ClassSelected_Instant;
}
else
{
// Set next spawn index if the player is changing the class on
// his active team.
if (IsPlayerAlive(client) &&
(iszombie && teamid == ZR_CLASS_TEAM_ZOMBIES) ||
(!iszombie && teamid == ZR_CLASS_TEAM_HUMANS) ||
(ClassPlayerInAdminMode[client] && teamid == ZR_CLASS_TEAM_ADMINS))
{
// Check if selecting the same class that the player already is.
if (ClassSelected[client][teamid] == classIndex)
{
// Player is already the specified class. No need to change
// class next spawn.
ClassSelectedNext[client][teamid] = -1;
selectResult = ClassSelected_NoChange;
}
else
{
// Set class to be used on next spawn.
ClassSelectedNext[client][teamid] = classIndex;
selectResult = ClassSelected_NextSpawn;
}
}
else
{
// Directly change the selected class index.
ClassSelected[client][teamid] = classIndex;
selectResult = ClassSelected_NextSpawn;
}
}
// Save selected class index in cookie if enabled.
// Note: Saved indexes are increased by one.
if (saveIfEnabled && GetConVarBool(g_hCvarsList[CVAR_CLASSES_SAVE]))
{
CookiesSetInt(client, g_hClassCookieClassSelected[teamid], classIndex + 1);
}
return selectResult;
}
/**
* Dump class data into a string. String buffer length should be at about 2048
* cells.