Imported some API-features from zr-dev. Added API test plugins in testsuite.

This commit is contained in:
Richard Helgeby
2010-11-14 15:17:19 +01:00
parent 772ed77beb
commit e7478e0a05
15 changed files with 1179 additions and 25 deletions

92
src/zr/api/api.inc Normal file
View File

@ -0,0 +1,92 @@
/*
* ============================================================================
*
* Zombie:Reloaded
*
* File: api.inc
* Type: Core
* Description: Native handlers for the ZR API.
*
* Copyright (C) 2009-2010 Greyscale, Richard Helgeby
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ============================================================================
*/
/**
* Application Programming Interface (API)
*
* To allow other plugins or extensions to interact directly with Zombie:Reloaded we need to implement
* an API. SourceMod allows us to do this by creating global "natives" or "forwards."
*
* Natives are basically functions that can be called from any plugin that includes its definition.
* Forwards are functions that are called on a given event. (Such as OnClientPutInServer)
* ZR's API files are located in sourcemod/scripting/include/zr/category.zr.inc. We chose to create multiple
* files simply for organization. Including zr.inc will automatically include the rest of the files as well.
*
* To better understand how natives and forwards are created, go here:
* http://wiki.alliedmods.net/Creating_Natives_(SourceMod_Scripting)
* http://wiki.alliedmods.net/Function_Calling_API_(SourceMod_Scripting)
*/
#include "zr/api/infect.api"
#include "zr/api/respawn.api"
/**
* Initializes all main natives and forwards.
*/
APIInit()
{
// Forward event to sub-modules.
APIInfectInit();
APIRespawnInit();
}
/**
* Validates a client index and when it fails, an error is thrown.
*
* @param client The client index to validate.
* @param alive Set to true to validate that the client is alive, false to ignore.
*
* @error Throws an error when the client isn't valid.
*/
stock APIValidateClientIndex(client, EligibleCondition:alive = Condition_Either)
{
// Verify client index.
if (client < 1 || client > MaxClients)
{
ThrowNativeError(SP_ERROR_NATIVE, "Invalid client index. (%d)", client);
}
// Verify that the client is connected.
if (!IsClientConnected(client))
{
ThrowNativeError(SP_ERROR_NATIVE, "Client %d is not connected.", client);
}
// Check if the player must be dead or alive.
new bool:bAlive = bool:alive;
if (alive != Condition_Either)
{
if (bAlive && !IsPlayerAlive(client))
{
ThrowNativeError(SP_ERROR_NATIVE, "Client %d must be alive.", client);
}
else if (!bAlive && IsPlayerAlive(client))
{
ThrowNativeError(SP_ERROR_NATIVE, "Client %d must be dead.", client);
}
}
}

229
src/zr/api/infect.api.inc Normal file
View File

@ -0,0 +1,229 @@
/*
* ============================================================================
*
* Zombie:Reloaded
*
* File: infect.api.inc
* Type: Core
* Description: Native handlers for the ZR API. (Infect module)
*
* Copyright (C) 2009-2010 Greyscale, Richard Helgeby
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ============================================================================
*/
/**
* @section Global forward handles.
*/
new Handle:g_hAPIFwdOnClientInfect = INVALID_HANDLE;
new Handle:g_hAPIFwdOnClientInfected = INVALID_HANDLE;
new Handle:g_hAPIFwdOnClientHuman = INVALID_HANDLE;
new Handle:g_hAPIFwdOnClientHumanPost = INVALID_HANDLE;
/**
* @endsection
*/
/**
* Initializes all natives and forwards related to infection.
*/
APIInfectInit()
{
// Infect module natives/forwards (infect.zr.inc)
// Natives
CreateNative("ZR_IsClientZombie", APIIsClientZombie);
CreateNative("ZR_IsClientHuman", APIIsClientHuman);
CreateNative("ZR_InfectClient", APIInfectClient);
CreateNative("ZR_HumanClient", APIHumanClient);
// Forwards
g_hAPIFwdOnClientInfect = CreateGlobalForward("ZR_OnClientInfect", ET_Hook, Param_CellByRef, Param_CellByRef, Param_CellByRef, Param_CellByRef, Param_CellByRef);
g_hAPIFwdOnClientInfected = CreateGlobalForward("ZR_OnClientInfected", ET_Ignore, Param_Cell, Param_Cell, Param_Cell, Param_Cell, Param_Cell);
g_hAPIFwdOnClientHuman = CreateGlobalForward("ZR_OnClientHuman", ET_Hook, Param_CellByRef, Param_CellByRef, Param_CellByRef);
g_hAPIFwdOnClientHumanPost = CreateGlobalForward("ZR_OnClientHumanPost", ET_Ignore, Param_Cell, Param_Cell, Param_Cell);
}
/**
* Native call function (ZR_IsClientInfected)
* Returns true if the client is a zombie, false if not.
*/
public APIIsClientZombie(Handle:plugin, numParams)
{
new client = GetNativeCell(1);
// Validate the client index.
APIValidateClientIndex(client, Condition_True);
return InfectIsClientInfected(client);
}
/**
* Native call function (ZR_IsClientHuman)
* Returns true if the client is a human, false if not.
*
* bool:InfectIsClientHuman(client)
*/
public APIIsClientHuman(Handle:plugin, numParams)
{
new client = GetNativeCell(1);
// Validate the client index.
APIValidateClientIndex(client, Condition_True);
return InfectIsClientHuman(client);
}
/**
* Native call function (ZR_InfectClient)
* Infects a client.
*
* native ZR_InfectClient(client, attacker = -1, bool:motherInfect = false, bool:respawnOverride = false, bool:respawn = false);
*/
public APIInfectClient(Handle:plugin, numParams)
{
new client = GetNativeCell(1);
new attacker = GetNativeCell(2);
new bool:motherInfect = bool:GetNativeCell(3);
new bool:respawnOverride = bool:GetNativeCell(4);
new bool:respawn = bool:GetNativeCell(5);
// Validate the client index.
APIValidateClientIndex(client, Condition_True);
InfectHumanToZombie(client, attacker, motherInfect, respawnOverride, respawn);
}
/**
* Native call function (ZR_HumanClient)
* Turns a zombie back into a human.
*
* native ZR_HumanClient(client, bool:respawn = false, bool:protect = false);
*/
public APIHumanClient(Handle:plugin, numParams)
{
new client = GetNativeCell(1);
new bool:respawn = bool:GetNativeCell(2);
new bool:protect = bool:GetNativeCell(2);
// Validate the client index.
APIValidateClientIndex(client, Condition_True);
InfectZombieToHuman(client, respawn, protect);
}
/**
* Called when a client is about to become a zombie.
*
* @param client The client to infect.
* @param attacker The attacker who did the infect.
* @param motherinfect Indicates a mother zombie infect.
* @param respawnoverride Set to true to override respawn cvar.
* @param respawn Value to override with.
*
* InfectHumanToZombie(client, attacker = -1, bool:motherinfect = false, bool:respawnoverride = false, bool:respawn = false)
*/
Action:APIOnClientInfect(&client, &attacker, &bool:motherinfect, &bool:respawnoverride, &bool:respawn)
{
// Start forward call.
Call_StartForward(g_hAPIFwdOnClientInfect);
// Push the parameters.
Call_PushCellRef(client);
Call_PushCellRef(attacker);
Call_PushCellRef(motherinfect);
Call_PushCellRef(respawnoverride);
Call_PushCellRef(respawn);
// Get what they returned.
new Action:result;
Call_Finish(result);
return result;
}
/**
* Called after a client has become a zombie.
*
* @param client The client to infect.
* @param attacker The attacker who did the infect.
* @param motherinfect Indicates a mother zombie infect.
* @param respawnoverride Set to true to override respawn cvar.
* @param respawn Value to override with.
*
* InfectHumanToZombie(client, attacker = -1, bool:motherinfect = false, bool:respawnoverride = false, bool:respawn = false)
*/
APIOnClientInfected(client, attacker, bool:motherinfect, bool:respawnoverride, bool:respawn)
{
// Start forward call.
Call_StartForward(g_hAPIFwdOnClientInfected);
// Push the parameters.
Call_PushCell(client);
Call_PushCell(attacker);
Call_PushCell(motherinfect);
Call_PushCell(respawnoverride);
Call_PushCell(respawn);
// Finish the call.
Call_Finish();
}
/**
* Called when a client is about to become a human. (Through either zr_human or admin menu)
*
* @param client The client index.
* @param respawn True if the client was respawned, false if not.
* @param protect True if the client spawn protected, false if not.
*
* InfectZombieToHuman(client, bool:respawn = false, bool:protect = false)
*/
Action:APIOnClientHuman(&client, &bool:respawn, &bool:protect)
{
// Start forward call.
Call_StartForward(g_hAPIFwdOnClientHuman);
// Push the parameters.
Call_PushCellRef(client);
Call_PushCellRef(respawn);
Call_PushCellRef(protect);
// Get what they returned.
new Action:result;
Call_Finish(result);
return result;
}
/**
* Called after a client has become a human. (Through either zr_human or admin menu)
*
* @param client The client index.
* @param respawn True if the client was respawned, false if not.
* @param protect True if the client spawn protected, false if not.
*
* InfectZombieToHuman(client, bool:respawn = false, bool:protect = false)
*/
APIOnClientHumanPost(client, bool:respawn, bool:protect)
{
// Start forward call.
Call_StartForward(g_hAPIFwdOnClientHumanPost);
// Push the parameters.
Call_PushCell(client);
Call_PushCell(respawn);
Call_PushCell(protect);
// Finish the call.
Call_Finish();
}

149
src/zr/api/respawn.api.inc Normal file
View File

@ -0,0 +1,149 @@
/*
* ============================================================================
*
* Zombie:Reloaded
*
* File: respawn.api.inc
* Type: Core
* Description: Native handlers for the ZR API. (Respawn module)
*
* Copyright (C) 2009-2010 Greyscale, Richard Helgeby
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ============================================================================
*/
/**
* @section Global forward handles.
*/
new Handle:g_hAPIFwdOnClientRespawn = INVALID_HANDLE;
new Handle:g_hAPIFwdOnClientRespawned = INVALID_HANDLE;
/**
* @endsection
*/
/**
* Initializes all natives and forwards related to respawn.
*/
APIRespawnInit()
{
// Respawn module natives/forwards (respawn.zr.inc)
// Natives
CreateNative("ZR_RespawnClient", APIRespawnClient);
CreateNative("ZR_SetKilledByWorld", APISetKilledByWorld);
CreateNative("ZR_GetKilledByWorld", APIGetKilledByWorld);
// Forwards
g_hAPIFwdOnClientRespawn = CreateGlobalForward("ZR_OnClientRespawn", ET_Hook, Param_CellByRef, Param_CellByRef);
g_hAPIFwdOnClientRespawned = CreateGlobalForward("ZR_OnClientRespawned", ET_Ignore, Param_Cell, Param_Cell);
}
/**
* Native call function (ZR_RespawnClient)
* Spawns a player into the round following rules set by cvars.
*
* RespawnSpawnClient(client, bool:zombie = false, bool:zombie_if_suicide = false)
*/
public APIRespawnClient(Handle:plugin, numParams)
{
new client = GetNativeCell(1);
// Validate index and that player isn't alive.
APIValidateClientIndex(client, Condition_False);
// Restore respawn condition.
new RespawnCondition:condition = RespawnCondition:GetNativeCell(2);
new bool:zombie;
new bool:zombieIfSuicide;
RespawnRestoreCondition(condition, zombie, zombieIfSuicide);
// Respawn the client.
RespawnSpawnClient(client, zombie, zombieIfSuicide);
}
/**
* Called right before ZR is about to respawn a player.
*
* @param client The client index.
* @param condition Respawn condition. See RespawnCondition for
* details.
*/
Action:APIOnClientRespawn(&client, &RespawnCondition:condition)
{
// Start forward call.
Call_StartForward(g_hAPIFwdOnClientRespawn);
// Push the parameters.
Call_PushCellRef(client);
Call_PushCellRef(condition);
// Get what they returned.
new Action:result;
Call_Finish(result);
return result;
}
/**
* Called after a player was respawned.
*
* @param client The client index.
* @param condition Current condition of the respawned player. See
* RespawnCondition for details.
*/
Action:APIOnClientRespawned(client, RespawnCondition:condition)
{
// Start forward call.
Call_StartForward(g_hAPIFwdOnClientRespawned);
// Push the parameters.
Call_PushCell(client);
Call_PushCell(condition);
// Finish the call.
Call_Finish();
}
/**
* Native call function (ZR_SetKilledByWorld)
* Override if the client died by a suicide.
*/
public APISetKilledByWorld(Handle:plugin, numParams)
{
new client = GetNativeCell(1);
// Validate the client index.
APIValidateClientIndex(client, Condition_Either);
new bool:suicide = bool:GetNativeCell(2);
// Override the old value.
bKilledByWorld[client] = suicide;
}
/**
* Native call function (ZR_GetKilledByWorld)
* Read the value of if the client died by a suicide.
*/
public APIGetKilledByWorld(Handle:plugin, numParams)
{
new client = GetNativeCell(1);
// Validate the client index.
APIValidateClientIndex(client, Condition_Either);
// Return the value.
return _:bKilledByWorld[client];
}