cssdm_callbacks.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Wed Oct 01 11:50:24 2014 -0400 (2014-10-01)
changeset 193 277c856152b0
parent 0 67dcd379a2d5
permissions -rw-r--r--
Update linix CS:GO gamedata
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * By David "BAILOPAN" Anderson
     6  * All rights reserved.
     7  * ===============================================================
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or (at
    12  * your option) any later version.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but
    15  * WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    17  * General Public License for more details.
    18  * 
    19  * You should have received a copy of the GNU General Public License
    20  * along with this program; see the file COPYING; if not, write to the
    21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
    22  * MA 02110-1301 USA
    23  * 
    24  * Version: $Id$
    25  */
    26 
    27 #include "cssdm_headers.h"
    28 #include "cssdm_callbacks.h"
    29 #include "cssdm_config.h"
    30 #include "cssdm_weapons.h"
    31 #include "cssdm_players.h"
    32 #include "cssdm_utils.h"
    33 
    34 IForward *g_pOnClientDeath = NULL;
    35 IForward *g_pOnClientSpawned = NULL;
    36 IForward *g_pOnClientPostSpawned = NULL;
    37 IForward *g_pOnSetSpawnMethod = NULL;
    38 IForward *g_pOnStartup = NULL;
    39 IForward *g_pOnShutdown = NULL;
    40 
    41 void DM_InitCallbacks()
    42 {
    43 	g_pOnClientDeath = forwards->CreateForward("DM_OnClientDeath", ET_Hook, 1, NULL, Param_Cell);
    44 	g_pOnClientSpawned = forwards->CreateForward("DM_OnClientSpawned", ET_Ignore, 1, NULL, Param_Cell);
    45 	g_pOnClientPostSpawned = forwards->CreateForward("DM_OnClientPostSpawned", ET_Ignore, 1, NULL, Param_Cell);
    46 	g_pOnSetSpawnMethod = forwards->CreateForward("DM_OnSetSpawnMethod", ET_Hook, 1, NULL, Param_String);
    47 	g_pOnStartup = forwards->CreateForward("DM_OnStartup", ET_Ignore, 0, NULL);
    48 	g_pOnShutdown = forwards->CreateForward("DM_OnShutdown", ET_Ignore, 0, NULL);
    49 }
    50 
    51 void DM_ShutdownCallbacks()
    52 {
    53 	forwards->ReleaseForward(g_pOnClientDeath);
    54 	forwards->ReleaseForward(g_pOnClientSpawned);
    55 	forwards->ReleaseForward(g_pOnClientPostSpawned);
    56 	forwards->ReleaseForward(g_pOnSetSpawnMethod);
    57 	forwards->ReleaseForward(g_pOnStartup);
    58 	forwards->ReleaseForward(g_pOnShutdown);
    59 }
    60 
    61 bool DM_OnClientDeath(int client)
    62 {
    63 	cell_t res = 0;
    64 	g_pOnClientDeath->PushCell(client);
    65 	g_pOnClientDeath->Execute(&res);
    66 
    67 	return (res == (cell_t)Pl_Continue);
    68 }
    69 
    70 void DM_OnClientSpawned(int client, bool wasDead)
    71 {
    72 	g_pOnClientSpawned->PushCell(client);
    73 	g_pOnClientSpawned->Execute(NULL);
    74 	g_pOnClientPostSpawned->PushCell(client);
    75 	g_pOnClientPostSpawned->Execute(NULL);
    76 }
    77 
    78 void DM_OnSetSpawnMethod(const char *method)
    79 {
    80 	g_pOnSetSpawnMethod->PushString(method);
    81 	g_pOnSetSpawnMethod->Execute(NULL);
    82 }
    83 
    84 void DM_OnStartup()
    85 {
    86 	g_pOnStartup->Execute(NULL);
    87 }
    88 
    89 void DM_OnShutdown()
    90 {
    91 	g_pOnShutdown->Execute(NULL);
    92 }
    93 
    94 static cell_t DMN_GetSpawnMethod(IPluginContext *pContext, const cell_t *params)
    95 {
    96 	pContext->StringToLocal(params[1], params[2], DM_GetSpawnMethod());
    97 	return 1;
    98 }
    99 
   100 static cell_t DMN_GetWeaponID(IPluginContext *pContext, const cell_t *params)
   101 {
   102 	char *str;
   103 	dm_weapon_t *weapon;
   104 	
   105 	pContext->LocalToString(params[1], &str);
   106 	if ((weapon = DM_FindWeapon(str)) == NULL)
   107 	{
   108 		return -1;
   109 	}
   110 
   111 	return weapon->id;
   112 }
   113 
   114 static cell_t DMN_GetWeaponType(IPluginContext *pContext, const cell_t *params)
   115 {
   116 	dm_weapon_t *weapon;
   117 
   118 	if ((weapon = DM_GetWeapon(params[1])) == NULL)
   119 	{
   120 		return pContext->ThrowNativeError("Invalid CS:S DM weapon id (%d)", params[1]);
   121 	}
   122 
   123 	return (cell_t)weapon->type;
   124 }
   125 
   126 static cell_t DMN_GetWeaponClassname(IPluginContext *pContext, const cell_t *params)
   127 {
   128 	dm_weapon_t *weapon;
   129 
   130 	if ((weapon = DM_GetWeapon(params[1])) == NULL)
   131 	{
   132 		return pContext->ThrowNativeError("Invalid CS:S DM weapon id (%d)", params[1]);
   133 	}
   134 
   135 	pContext->StringToLocal(params[2], params[3], weapon->classname);
   136 
   137 	return 1;
   138 }
   139 
   140 static cell_t DMN_GetWeaponName(IPluginContext *pContext, const cell_t *params)
   141 {
   142 	dm_weapon_t *weapon;
   143 
   144 	if ((weapon = DM_GetWeapon(params[1])) == NULL)
   145 	{
   146 		return pContext->ThrowNativeError("Invalid CS:S DM weapon id (%d)", params[1]);
   147 	}
   148 
   149 	pContext->StringToLocal(params[2], params[3], weapon->display);
   150 
   151 	return 1;
   152 }
   153 
   154 static cell_t DMN_StripBotItems(IPluginContext *pContext, const cell_t *params)
   155 {
   156 	if (params[1] < 1 || params[1] > gpGlobals->maxClients)
   157 	{
   158 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   159 	}
   160 
   161 	dm_player_t *player = DM_GetPlayer(params[1]);
   162 	if (!player->pEntity)
   163 	{
   164 		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
   165 	}
   166 
   167 	if (!player->pBot)
   168 	{
   169 		return pContext->ThrowNativeError("Client %d is not a bot", params[1]);
   170 	}
   171 
   172 	player->pBot->RemoveAllItems(false);
   173 
   174 	return 1;
   175 }
   176 
   177 static cell_t DMN_GetClientWeapon(IPluginContext *pContext, const cell_t *params)
   178 {
   179 	if (params[1] < 1 || params[1] > gpGlobals->maxClients)
   180 	{
   181 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   182 	}
   183 
   184 	dm_player_t *player = DM_GetPlayer(params[1]);
   185 	if (!player->pEntity)
   186 	{
   187 		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
   188 	}
   189 
   190 	CBaseEntity *pEntity = DM_GetWeaponFromSlot(player->pEntity, params[2]);
   191 	if (!pEntity)
   192 	{
   193 		return -1;
   194 	}
   195 
   196 	edict_t *pEdict = gameents->BaseEntityToEdict(pEntity);
   197 	if (!pEdict)
   198 	{
   199 		return -1;
   200 	}
   201 
   202 	return gamehelpers->IndexOfEdict(pEdict);
   203 }
   204 
   205 static cell_t DMN_DropWeapon(IPluginContext *pContext, const cell_t *params)
   206 {
   207 	if (params[1] < 1 || params[1] > gpGlobals->maxClients)
   208 	{
   209 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   210 	}
   211 
   212 	dm_player_t *player = DM_GetPlayer(params[1]);
   213 	if (!player->pEntity)
   214 	{
   215 		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
   216 	}
   217 
   218 	CBaseEntity *pWeapon = DM_GetBaseEntity(params[2]);
   219 	if (!pWeapon)
   220 	{
   221 		return pContext->ThrowNativeError("Entity %d is not a valid entity", params[2]);
   222 	}
   223 
   224 	DM_DropWeapon(player->pEntity, pWeapon);
   225 
   226 	return 1;
   227 }
   228 
   229 static cell_t DMN_IsRunning(IPluginContext *pContext, const cell_t *params)
   230 {
   231 	return g_IsRunning ? true : false;
   232 }
   233 
   234 static cell_t DMN_GetSpawnWaitTime(IPluginContext *pContext, const cell_t *params)
   235 {
   236 	float f = DM_GetRespawnWait();
   237 	return sp_ftoc(f);
   238 }
   239 
   240 static cell_t DMN_RespawnClient(IPluginContext *pContext, const cell_t *params)
   241 {
   242 	if (params[1] < 1 || params[1] > gpGlobals->maxClients)
   243 	{
   244 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   245 	}
   246 
   247 	dm_player_t *player = DM_GetPlayer(params[1]);
   248 	if (!player->pEntity)
   249 	{
   250 		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
   251 	}
   252 
   253 	player->is_spawned = params[2] ? false : true;
   254 
   255 	DM_RespawnPlayer(params[1]);
   256 
   257 	return 1;
   258 }
   259 
   260 static cell_t DMN_IsClientAlive(IPluginContext *pContext, const cell_t *params)
   261 {
   262 	if (params[1] < 1 || params[1] > gpGlobals->maxClients)
   263 	{
   264 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   265 	}
   266 
   267 	dm_player_t *player = DM_GetPlayer(params[1]);
   268 	if (!player->pEntity)
   269 	{
   270 		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
   271 	}
   272 
   273 	return DM_IsPlayerAlive(params[1]) ? 1 : 0;
   274 }
   275 
   276 static cell_t DMN_GiveAmmo(IPluginContext *pContext, const cell_t *params)
   277 {
   278 	if (params[1] < 1 || params[1] > gpGlobals->maxClients)
   279 	{
   280 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   281 	}
   282 
   283 	dm_player_t *player = DM_GetPlayer(params[1]);
   284 	if (!player->pEntity)
   285 	{
   286 		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
   287 	}
   288 
   289 	return DM_GiveAmmo(player->pEntity, params[2], params[3], params[4] ? true : false);
   290 }
   291 
   292 sp_nativeinfo_t g_BaseNatives[] = 
   293 {
   294 	{"DM_GetSpawnMethod",		DMN_GetSpawnMethod},
   295 	{"DM_GetWeaponID",			DMN_GetWeaponID},
   296 	{"DM_GetWeaponType",		DMN_GetWeaponType},
   297 	{"DM_StripBotItems",		DMN_StripBotItems},
   298 	{"DM_GetWeaponClassname",	DMN_GetWeaponClassname},
   299 	{"DM_GetClientWeapon",		DMN_GetClientWeapon},
   300 	{"DM_DropWeapon",			DMN_DropWeapon},
   301 	{"DM_GetWeaponName",		DMN_GetWeaponName},
   302 	{"DM_IsRunning",			DMN_IsRunning},
   303 	{"DM_GetSpawnWaitTime",		DMN_GetSpawnWaitTime},
   304 	{"DM_RespawnClient",		DMN_RespawnClient},
   305 	{"DM_IsClientAlive",		DMN_IsClientAlive},
   306 	{"DM_GiveClientAmmo",		DMN_GiveAmmo},
   307 	{NULL,						NULL},
   308 };