3 * =============================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
33 #include <sh_string.h>
34 #include "extension.h"
35 #include "vcallbuilder.h"
39 #include "CellRecipientFilter.h"
40 #include <inetchannel.h>
44 SourceHook::List<ValveCall *> g_RegCalls;
45 SourceHook::List<ICallWrapper *> g_CallWraps;
47 inline void InitPass(ValvePassInfo &info, ValveType vtype, PassType type, unsigned int flags, unsigned int decflags=0)
49 info.decflags = decflags;
56 #define START_CALL() \
57 unsigned char *vptr = pCall->stk_get();
59 #define FINISH_CALL_SIMPLE(vret) \
60 pCall->call->Execute(vptr, vret); \
63 #define ENCODE_VALVE_PARAM(num, which, vnum) \
64 if (EncodeValveParam(pContext, \
67 &pCall->which[vnum], \
74 #define DECODE_VALVE_PARAM(num, which, vnum) \
75 if (DecodeValveParam(pContext, \
78 &pCall->which[vnum], \
85 bool CreateBaseCall(const char *name,
86 ValveCallType vcalltype,
87 const ValvePassInfo *retinfo,
88 const ValvePassInfo params[],
89 unsigned int numParams,
94 if (g_pGameConf->GetOffset(name, &offset))
96 call = CreateValveVCall(offset, vcalltype, retinfo, params, numParams);
99 g_RegCalls.push_back(call);
105 if (g_pGameConf->GetMemSig(name, &addr) && addr != NULL)
107 call = CreateValveCall(addr, vcalltype, retinfo, params, numParams);
110 g_RegCalls.push_back(call);
119 static cell_t RemovePlayerItem(IPluginContext *pContext, const cell_t *params)
121 static ValveCall *pCall = NULL;
124 ValvePassInfo pass[2];
125 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
126 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
127 if (!CreateBaseCall("RemovePlayerItem", ValveCall_Player, &pass[1], pass, 1, &pCall))
129 return pContext->ThrowNativeError("\"RemovePlayerItem\" not supported by this mod");
131 return pContext->ThrowNativeError("\"RemovePlayerItem\" wrapper failed to initialize");
137 DECODE_VALVE_PARAM(1, thisinfo, 0);
138 DECODE_VALVE_PARAM(2, vparams, 0);
139 FINISH_CALL_SIMPLE(&ret);
143 static cell_t GiveNamedItem(IPluginContext *pContext, const cell_t *params)
145 static ValveCall *pCall = NULL;
148 ValvePassInfo pass[3];
149 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
150 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
151 InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
152 if (!CreateBaseCall("GiveNamedItem", ValveCall_Player, &pass[2], pass, 2, &pCall))
154 return pContext->ThrowNativeError("\"GiveNamedItem\" not supported by this mod");
156 return pContext->ThrowNativeError("\"GiveNamedItem\" wrapper failed to initialize");
160 CBaseEntity *pEntity = NULL;
162 DECODE_VALVE_PARAM(1, thisinfo, 0);
163 DECODE_VALVE_PARAM(2, vparams, 0);
164 DECODE_VALVE_PARAM(3, vparams, 1);
165 FINISH_CALL_SIMPLE(&pEntity);
167 return gamehelpers->EntityToBCompatRef(pEntity);
170 static cell_t GetPlayerWeaponSlot(IPluginContext *pContext, const cell_t *params)
172 static ValveCall *pCall = NULL;
175 ValvePassInfo pass[2];
176 InitPass(pass[0], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
177 InitPass(pass[1], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
178 if (!CreateBaseCall("Weapon_GetSlot", ValveCall_Player, &pass[1], pass, 1, &pCall))
180 return pContext->ThrowNativeError("\"Weapon_GetSlot\" not supported by this mod");
182 return pContext->ThrowNativeError("\"Weapon_GetSlot\" wrapper failed to initialize");
186 CBaseEntity *pEntity;
188 DECODE_VALVE_PARAM(1, thisinfo, 0);
189 DECODE_VALVE_PARAM(2, vparams, 0);
190 FINISH_CALL_SIMPLE(&pEntity);
192 return gamehelpers->EntityToBCompatRef(pEntity);
195 #if SOURCE_ENGINE != SE_DARKMESSIAH
196 static cell_t IgniteEntity(IPluginContext *pContext, const cell_t *params)
198 static ValveCall *pCall = NULL;
201 ValvePassInfo pass[4];
202 InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
203 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
204 InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
205 InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
206 if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 4, &pCall))
208 return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
210 return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
215 DECODE_VALVE_PARAM(1, thisinfo, 0);
216 DECODE_VALVE_PARAM(2, vparams, 0);
217 DECODE_VALVE_PARAM(3, vparams, 1);
218 DECODE_VALVE_PARAM(4, vparams, 2);
219 DECODE_VALVE_PARAM(5, vparams, 3);
220 FINISH_CALL_SIMPLE(NULL);
225 /* Dark Messiah specific version */
226 static cell_t IgniteEntity(IPluginContext *pContext, const cell_t *params)
228 static ValveCall *pCall = NULL;
231 ValvePassInfo pass[6];
232 InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
233 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
234 InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
235 InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
236 InitPass(pass[4], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
237 InitPass(pass[5], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
238 if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 6, &pCall))
240 return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
242 return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
247 DECODE_VALVE_PARAM(1, thisinfo, 0);
248 DECODE_VALVE_PARAM(2, vparams, 0);
249 DECODE_VALVE_PARAM(3, vparams, 1);
250 DECODE_VALVE_PARAM(4, vparams, 2);
251 DECODE_VALVE_PARAM(5, vparams, 3);
252 /* Not sure what these params do, but they appear to be the default values */
253 *(int *)(vptr + 14) = 3;
254 *(int *)(vptr + 18) = 0;
255 FINISH_CALL_SIMPLE(NULL);
261 static cell_t ExtinguishEntity(IPluginContext *pContext, const cell_t *params)
263 static ValveCall *pCall = NULL;
266 if (!CreateBaseCall("Extinguish", ValveCall_Entity, NULL, NULL, 0, &pCall))
268 return pContext->ThrowNativeError("\"Extinguish\" not supported by this mod");
270 return pContext->ThrowNativeError("\"Extinguish\" wrapper failed to initialize");
275 DECODE_VALVE_PARAM(1, thisinfo, 0);
276 FINISH_CALL_SIMPLE(NULL);
281 static cell_t TeleportEntity(IPluginContext *pContext, const cell_t *params)
283 static ValveCall *pCall = NULL;
286 ValvePassInfo pass[3];
287 InitPass(pass[0], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
288 InitPass(pass[1], Valve_QAngle, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
289 InitPass(pass[2], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
290 if (!CreateBaseCall("Teleport", ValveCall_Entity, NULL, pass, 3, &pCall))
292 return pContext->ThrowNativeError("\"Teleport\" not supported by this mod");
294 return pContext->ThrowNativeError("\"Teleport\" wrapper failed to initialize");
299 DECODE_VALVE_PARAM(1, thisinfo, 0);
300 DECODE_VALVE_PARAM(2, vparams, 0);
301 DECODE_VALVE_PARAM(3, vparams, 1);
302 DECODE_VALVE_PARAM(4, vparams, 2);
303 FINISH_CALL_SIMPLE(NULL);
308 #if SOURCE_ENGINE >= SE_ORANGEBOX
309 /* :TODO: This is Team Fortress 2 specific */
310 static cell_t ForcePlayerSuicide(IPluginContext *pContext, const cell_t *params)
312 static ValveCall *pCall = NULL;
315 ValvePassInfo pass[2];
316 InitPass(pass[0], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
317 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
318 if (!CreateBaseCall("CommitSuicide", ValveCall_Player, NULL, pass, 2, &pCall))
320 return pContext->ThrowNativeError("\"CommitSuicide\" not supported by this mod");
324 return pContext->ThrowNativeError("\"CommitSuicide\" wrapper failed to initialize");
329 DECODE_VALVE_PARAM(1, thisinfo, 0);
330 *(bool *)(vptr + 4) = false;
331 *(bool *)(vptr + 5) = false;
332 FINISH_CALL_SIMPLE(NULL);
337 static cell_t ForcePlayerSuicide(IPluginContext *pContext, const cell_t *params)
339 static ValveCall *pCall = NULL;
342 if (!CreateBaseCall("CommitSuicide", ValveCall_Player, NULL, NULL, 0, &pCall))
344 return pContext->ThrowNativeError("\"CommitSuicide\" not supported by this mod");
346 return pContext->ThrowNativeError("\"CommitSuicide\" wrapper failed to initialize");
351 DECODE_VALVE_PARAM(1, thisinfo, 0);
352 FINISH_CALL_SIMPLE(NULL);
358 static cell_t SetClientViewEntity(IPluginContext *pContext, const cell_t *params)
360 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
363 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
365 if (!player->IsInGame())
367 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
370 edict_t *pEdict = PEntityOfEntIndex(gamehelpers->ReferenceToIndex(params[2]));
371 if (!pEdict || pEdict->IsFree())
373 return pContext->ThrowNativeError("Entity %d is not valid", params[2]);
376 engine->SetView(player->GetEdict(), pEdict);
381 static SourceHook::String *g_lightstyle[MAX_LIGHTSTYLES] = {NULL};
382 static cell_t SetLightStyle(IPluginContext *pContext, const cell_t *params)
384 int style = params[1];
385 if (style >= MAX_LIGHTSTYLES)
387 return pContext->ThrowNativeError("Light style %d is invalid (range: 0-%d)", style, MAX_LIGHTSTYLES - 1);
390 if (g_lightstyle[style] == NULL)
392 /* We allocate and never free this because the Engine wants to hold onto it :\
393 * in theory we could hook light style and know whether we're supposed to free
394 * this or not on shutdown, but for ~4K of memory MAX, it doesn't seem worth it yet.
397 g_lightstyle[style] = new SourceHook::String();
401 pContext->LocalToString(params[2], &str);
403 g_lightstyle[style]->assign(str);
405 engine->LightStyle(style, g_lightstyle[style]->c_str());
410 static cell_t SlapPlayer(IPluginContext *pContext, const cell_t *params)
412 static bool s_slap_supported = false;
413 static bool s_slap_setup = false;
414 static ICallWrapper *s_teleport = NULL;
415 static int s_health_offs = 0;
416 static int s_sound_count = 0;
417 static int s_frag_offs = 0;
425 if (IsTeleportSupported())
429 if (IsGetVelocitySupported())
435 if (g_pGameConf->GetOffset("m_iHealth", &s_health_offs) && s_health_offs)
442 s_slap_supported = true;
445 if ((key = g_pGameConf->GetKeyValue("SlapSoundCount")) != NULL)
447 s_sound_count = atoi(key);
452 if (!s_slap_supported)
454 return pContext->ThrowNativeError("This function is not supported on this mod");
457 /* First check if the client is valid */
458 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
461 return pContext->ThrowNativeError("Client %d is not valid", params[1]);
462 } else if (!player->IsInGame()) {
463 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
466 edict_t *pEdict = player->GetEdict();
467 CBaseEntity *pEntity = pEdict->GetUnknown()->GetBaseEntity();
469 /* See if we should be taking away health */
470 bool should_slay = false;
473 #if SOURCE_ENGINE != SE_DARKMESSIAH
474 int *health = (int *)((char *)pEntity + s_health_offs);
476 if (*health - params[2] <= 0)
481 *health -= params[2];
484 float *health = (float *)((char *)pEntity + s_health_offs);
486 if (*health - (float)params[2] <= 0)
491 *health -= (float)params[2];
496 /* Teleport in a random direction - thank you, Mani!*/
498 GetVelocity(pEntity, &velocity, NULL);
499 velocity.x += ((rand() % 180) + 50) * (((rand() % 2) == 1) ? -1 : 1);
500 velocity.y += ((rand() % 180) + 50) * (((rand() % 2) == 1) ? -1 : 1);
501 velocity.z += rand() % 200 + 100;
502 Teleport(pEntity, NULL, NULL, &velocity);
504 /* Play a random sound */
505 if (params[3] && s_sound_count > 0)
508 const char *sound_name;
509 cell_t player_list[256], total_players = 0;
510 int maxClients = playerhelpers->GetMaxClients();
512 int r = (rand() % s_sound_count) + 1;
513 snprintf(name, sizeof(name), "SlapSound%d", r);
515 if ((sound_name = g_pGameConf->GetKeyValue(name)) != NULL)
518 for (int i=1; i<=maxClients; i++)
520 other = playerhelpers->GetGamePlayer(i);
521 if (other->IsInGame())
523 player_list[total_players++] = i;
527 const Vector & pos = pEdict->GetCollideable()->GetCollisionOrigin();
528 CellRecipientFilter rf;
529 rf.SetToReliable(true);
530 rf.Initialize(player_list, total_players);
531 #if SOURCE_ENGINE == SE_ORANGEBOXVALVE || SOURCE_ENGINE == SE_CSS
532 engsound->EmitSound(rf, params[1], CHAN_AUTO, sound_name, VOL_NORM, ATTN_NORM, 0, PITCH_NORM, 0, &pos);
533 #elif SOURCE_ENGINE < SE_PORTAL2
534 engsound->EmitSound(rf, params[1], CHAN_AUTO, sound_name, VOL_NORM, ATTN_NORM, 0, PITCH_NORM, &pos);
536 engsound->EmitSound(rf, params[1], CHAN_AUTO, sound_name, -1, sound_name, VOL_NORM, ATTN_NORM, 0, 0, PITCH_NORM, &pos);
543 const char *frag_prop = g_pGameConf->GetKeyValue("m_iFrags");
546 datamap_t *pMap = gamehelpers->GetDataMap(pEntity);
547 typedescription_t *pType = gamehelpers->FindInDataMap(pMap, frag_prop);
550 s_frag_offs = GetTypeDescOffs(pType);
562 old_frags = *(int *)((char *)pEntity + s_frag_offs);
568 pluginhelpers->ClientCommand(pEdict, "kill\n");
573 *(int *)((char *)pEntity + s_frag_offs) = old_frags;
579 static cell_t GetClientEyePosition(IPluginContext *pContext, const cell_t *params)
581 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
584 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
586 if (!player->IsInGame())
588 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
592 serverClients->ClientEarPosition(player->GetEdict(), &pos);
595 pContext->LocalToPhysAddr(params[2], &addr);
596 addr[0] = sp_ftoc(pos.x);
597 addr[1] = sp_ftoc(pos.y);
598 addr[2] = sp_ftoc(pos.z);
603 static cell_t GetClientEyeAngles(IPluginContext *pContext, const cell_t *params)
605 IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(params[1]);
609 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
611 else if (!pPlayer->IsInGame())
613 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
616 edict_t *pEdict = pPlayer->GetEdict();
617 CBaseEntity *pEntity = pEdict->GetUnknown() ? pEdict->GetUnknown()->GetBaseEntity() : NULL;
619 /* We always set the angles for backwards compatibility --
620 * The original function had no return value.
623 bool got_angles = false;
627 got_angles = GetEyeAngles(pEntity, &angles);
631 pContext->LocalToPhysAddr(params[2], &addr);
632 addr[0] = sp_ftoc(angles.x);
633 addr[1] = sp_ftoc(angles.y);
634 addr[2] = sp_ftoc(angles.z);
636 return got_angles ? 1 : 0;
639 #if SOURCE_ENGINE >= SE_ORANGEBOX
640 static cell_t NativeFindEntityByClassname(IPluginContext *pContext, const cell_t *params)
643 CBaseEntity *pEntity;
647 pEntity = (CBaseEntity *)servertools->FirstEntity();
651 pEntity = gamehelpers->ReferenceToEntity(params[1]);
654 return pContext->ThrowNativeError("Entity %d (%d) is invalid",
655 gamehelpers->ReferenceToIndex(params[1]),
658 pEntity = (CBaseEntity *)servertools->NextEntity(pEntity);
661 // it's tough to find a good ent these days
667 pContext->LocalToString(params[2], &searchname);
669 const char *classname;
672 static int offset = -1;
675 offset = GetTypeDescOffs(
676 gamehelpers->FindInDataMap(gamehelpers->GetDataMap(pEntity),
685 if ((s = *(string_t *)((uint8_t *)pEntity + offset)) == NULL_STRING)
687 pEntity = (CBaseEntity *)servertools->NextEntity(pEntity);
691 classname = STRING(s);
693 lastletterpos = strlen(searchname) - 1;
694 if (searchname[lastletterpos] == '*')
696 if (strncasecmp(searchname, classname, lastletterpos) == 0)
698 return gamehelpers->EntityToBCompatRef(pEntity);
701 else if (strcasecmp(searchname, classname) == 0)
703 return gamehelpers->EntityToBCompatRef(pEntity);
706 pEntity = (CBaseEntity *)servertools->NextEntity(pEntity);
713 static cell_t FindEntityByClassname(IPluginContext *pContext, const cell_t *params)
715 static ValveCall *pCall = NULL;
716 static bool bProbablyNoFEBC = false;
718 #if SOURCE_ENGINE >= SE_ORANGEBOX
721 return NativeFindEntityByClassname(pContext, params);
727 ValvePassInfo pass[3];
728 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL|VDECODE_FLAG_ALLOWWORLD);
729 InitPass(pass[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
730 InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
734 if (!CreateBaseCall("FindEntityByClassname", ValveCall_EntityList, &pass[2], pass, 2, &pCall))
736 g_pSM->Format(error, sizeof(error), "\"FindEntityByClassname\" not supported by this mod");
738 g_pSM->Format(error, sizeof(error), "\"FindEntityByClassname\" wrapper failed to initialize");
741 if (error[0] != '\0')
743 #if SOURCE_ENGINE >= SE_ORANGEBOX
744 if (!bProbablyNoFEBC)
746 bProbablyNoFEBC = true;
747 g_pSM->LogError(myself, "%s, falling back to IServerTools method.", error);
749 return NativeFindEntityByClassname(pContext, params);
751 return pContext->ThrowNativeError("%s", error);
756 CBaseEntity *pEntity;
758 *(void **)vptr = g_EntList;
759 DECODE_VALVE_PARAM(1, vparams, 0);
760 DECODE_VALVE_PARAM(2, vparams, 1);
761 FINISH_CALL_SIMPLE(&pEntity);
763 return gamehelpers->EntityToBCompatRef(pEntity);
766 #if SOURCE_ENGINE >= SE_ORANGEBOX
767 static cell_t CreateEntityByName(IPluginContext *pContext, const cell_t *params)
769 if (!g_pSM->IsMapRunning())
771 return pContext->ThrowNativeError("Cannot create new entity when no map is running");
775 pContext->LocalToString(params[1], &classname);
776 CBaseEntity *pEntity = (CBaseEntity *)servertools->CreateEntityByName(classname);
777 return gamehelpers->EntityToBCompatRef(pEntity);
780 static cell_t CreateEntityByName(IPluginContext *pContext, const cell_t *params)
782 if (!g_pSM->IsMapRunning())
784 return pContext->ThrowNativeError("Cannot create new entity when no map is running");
787 static ValveCall *pCall = NULL;
790 ValvePassInfo pass[3];
791 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
792 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
793 InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
794 if (!CreateBaseCall("CreateEntityByName", ValveCall_Static, &pass[2], pass, 2, &pCall))
796 return pContext->ThrowNativeError("\"CreateEntityByName\" not supported by this mod");
798 return pContext->ThrowNativeError("\"CreateEntityByName\" wrapper failed to initialize");
802 CBaseEntity *pEntity = NULL;
804 DECODE_VALVE_PARAM(1, vparams, 0);
805 DECODE_VALVE_PARAM(2, vparams, 1);
806 FINISH_CALL_SIMPLE(&pEntity);
808 return gamehelpers->EntityToBCompatRef(pEntity);
812 #if SOURCE_ENGINE >= SE_ORANGEBOX
813 static cell_t DispatchSpawn(IPluginContext *pContext, const cell_t *params)
815 CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(params[1]);
818 return pContext->ThrowNativeError("Entity %d (%d) is invalid", gamehelpers->ReferenceToIndex(params[1]), params[1]);
821 servertools->DispatchSpawn(pEntity);
826 static cell_t DispatchSpawn(IPluginContext *pContext, const cell_t *params)
828 static ValveCall *pCall = NULL;
831 ValvePassInfo pass[2];
832 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
833 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
834 if (!CreateBaseCall("DispatchSpawn", ValveCall_Static, &pass[1], pass, 1, &pCall))
836 return pContext->ThrowNativeError("\"DispatchSpawn\" not supported by this mod");
838 return pContext->ThrowNativeError("\"DispatchSpawn\" wrapper failed to initialize");
844 DECODE_VALVE_PARAM(1, vparams, 0);
845 FINISH_CALL_SIMPLE(&ret);
847 return (ret == -1) ? 0 : 1;
851 #if SOURCE_ENGINE >= SE_ORANGEBOX
852 static cell_t DispatchKeyValue(IPluginContext *pContext, const cell_t *params)
854 CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(params[1]);
857 return pContext->ThrowNativeError("Entity %d (%d) is invalid", gamehelpers->ReferenceToIndex(params[1]), params[1]);
862 pContext->LocalToString(params[2], &key);
863 pContext->LocalToString(params[3], &value);
865 return (servertools->SetKeyValue(pEntity, key, value) ? 1 : 0);
868 static cell_t DispatchKeyValue(IPluginContext *pContext, const cell_t *params)
870 static ValveCall *pCall = NULL;
873 ValvePassInfo pass[3];
874 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
875 InitPass(pass[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
876 InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
877 if (!CreateBaseCall("DispatchKeyValue", ValveCall_Entity, &pass[2], pass, 2, &pCall))
879 return pContext->ThrowNativeError("\"DispatchKeyValue\" not supported by this mod");
881 return pContext->ThrowNativeError("\"DispatchKeyValue\" wrapper failed to initialize");
887 DECODE_VALVE_PARAM(1, thisinfo, 0);
888 DECODE_VALVE_PARAM(2, vparams, 0);
889 DECODE_VALVE_PARAM(3, vparams, 1);
890 FINISH_CALL_SIMPLE(&ret);
896 #if SOURCE_ENGINE >= SE_ORANGEBOX
897 static cell_t DispatchKeyValueFloat(IPluginContext *pContext, const cell_t *params)
899 CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(params[1]);
902 return pContext->ThrowNativeError("Entity %d (%d) is invalid", gamehelpers->ReferenceToIndex(params[1]), params[1]);
906 pContext->LocalToString(params[2], &key);
908 float value = sp_ctof(params[3]);
910 return (servertools->SetKeyValue(pEntity, key, value) ? 1 : 0);
913 static cell_t DispatchKeyValueFloat(IPluginContext *pContext, const cell_t *params)
915 static ValveCall *pCall = NULL;
918 ValvePassInfo pass[3];
919 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
920 InitPass(pass[1], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
921 InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
922 if (!CreateBaseCall("DispatchKeyValueFloat", ValveCall_Entity, &pass[2], pass, 2, &pCall))
924 return pContext->ThrowNativeError("\"DispatchKeyValueFloat\" not supported by this mod");
926 return pContext->ThrowNativeError("\"DispatchKeyValueFloat\" wrapper failed to initialize");
932 DECODE_VALVE_PARAM(1, thisinfo, 0);
933 DECODE_VALVE_PARAM(2, vparams, 0);
934 DECODE_VALVE_PARAM(3, vparams, 1);
935 FINISH_CALL_SIMPLE(&ret);
941 #if SOURCE_ENGINE >= SE_ORANGEBOX
942 static cell_t DispatchKeyValueVector(IPluginContext *pContext, const cell_t *params)
944 CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(params[1]);
947 return pContext->ThrowNativeError("Entity %d (%d) is invalid", gamehelpers->ReferenceToIndex(params[1]), params[1]);
951 pContext->LocalToString(params[2], &key);
954 pContext->LocalToPhysAddr(params[3], &vec);
956 const Vector *v = new Vector(
961 return (servertools->SetKeyValue(pEntity, key, *v) ? 1 : 0);
964 static cell_t DispatchKeyValueVector(IPluginContext *pContext, const cell_t *params)
966 static ValveCall *pCall = NULL;
969 ValvePassInfo pass[3];
970 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
971 #if SOURCE_ENGINE >= SE_ORANGEBOX
972 InitPass(pass[1], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL);
974 InitPass(pass[1], Valve_Vector, PassType_Object, PASSFLAG_BYVAL|PASSFLAG_OCTOR|PASSFLAG_OASSIGNOP);
976 InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
977 if (!CreateBaseCall("DispatchKeyValueVector", ValveCall_Entity, &pass[2], pass, 2, &pCall))
979 return pContext->ThrowNativeError("\"DispatchKeyValueVector\" not supported by this mod");
981 return pContext->ThrowNativeError("\"DispatchKeyValueVector\" wrapper failed to initialize");
987 DECODE_VALVE_PARAM(1, thisinfo, 0);
988 DECODE_VALVE_PARAM(2, vparams, 0);
989 DECODE_VALVE_PARAM(3, vparams, 1);
990 FINISH_CALL_SIMPLE(&ret);
996 static cell_t sm_GetClientAimTarget(IPluginContext *pContext, const cell_t *params)
998 IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(params[1]);
1002 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
1004 else if (!pPlayer->IsInGame())
1006 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
1009 return GetClientAimTarget(pPlayer->GetEdict(), params[2] ? true : false);
1012 static cell_t sm_SetEntityModel(IPluginContext *pContext, const cell_t *params)
1014 static ValveCall *pCall = NULL;
1017 ValvePassInfo pass[1];
1018 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
1019 if (!CreateBaseCall("SetEntityModel", ValveCall_Entity, NULL, pass, 1, &pCall))
1021 return pContext->ThrowNativeError("\"SetEntityModel\" not supported by this mod");
1022 } else if (!pCall) {
1023 return pContext->ThrowNativeError("\"SetEntityModel\" wrapper failed to initialize");
1028 DECODE_VALVE_PARAM(1, thisinfo, 0);
1029 DECODE_VALVE_PARAM(2, vparams, 0);
1030 FINISH_CALL_SIMPLE(NULL);
1035 static cell_t GetPlayerDecalFile(IPluginContext *pContext, const cell_t *params)
1037 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
1040 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
1042 if (!player->IsInGame())
1044 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
1050 if (!GetPlayerInfo(params[1], &info) || !info.customFiles[0])
1055 pContext->LocalToString(params[2], &buffer);
1056 Q_binarytohex((byte *)&info.customFiles[0], sizeof(info.customFiles[0]), buffer, params[3]);
1061 static cell_t GetServerNetStats(IPluginContext *pContext, const cell_t *params)
1063 if (iserver == NULL)
1065 return pContext->ThrowNativeError("IServer interface not supported, file a bug report.");
1071 pContext->LocalToPhysAddr(params[1], &pIn);
1072 pContext->LocalToPhysAddr(params[2], &pOut);
1073 iserver->GetNetStats(in, out);
1076 *pOut = sp_ftoc(out);
1081 static cell_t WeaponEquip(IPluginContext *pContext, const cell_t *params)
1083 static ValveCall *pCall = NULL;
1086 ValvePassInfo pass[1];
1087 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
1088 if (!CreateBaseCall("WeaponEquip", ValveCall_Player, NULL, pass, 1, &pCall))
1090 return pContext->ThrowNativeError("\"WeaponEquip\" not supported by this mod");
1091 } else if (!pCall) {
1092 return pContext->ThrowNativeError("\"WeaponEquip\" wrapper failed to initialize");
1097 DECODE_VALVE_PARAM(1, thisinfo, 0);
1098 DECODE_VALVE_PARAM(2, vparams, 0);
1099 FINISH_CALL_SIMPLE(NULL);
1104 static cell_t ActivateEntity(IPluginContext *pContext, const cell_t *params)
1106 static ValveCall *pCall = NULL;
1109 if (!CreateBaseCall("Activate", ValveCall_Entity, NULL, NULL, 0, &pCall))
1111 return pContext->ThrowNativeError("\"Activate\" not supported by this mod");
1115 return pContext->ThrowNativeError("\"Activate\" wrapper failed to initialize");
1120 DECODE_VALVE_PARAM(1, thisinfo, 0);
1121 FINISH_CALL_SIMPLE(NULL);
1126 static cell_t SetClientInfo(IPluginContext *pContext, const cell_t *params)
1128 if (iserver == NULL)
1130 return pContext->ThrowNativeError("IServer interface not supported, file a bug report.");
1133 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
1134 IClient *pClient = iserver->GetClient(params[1] - 1);
1136 if (player == NULL || pClient == NULL)
1138 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
1140 if (!player->IsConnected())
1142 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
1145 static ValveCall *pCall = NULL;
1148 ValvePassInfo params[2];
1149 InitPass(params[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
1150 InitPass(params[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
1152 if (!CreateBaseCall("SetUserCvar", ValveCall_Entity, NULL, params, 2, &pCall))
1154 return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
1158 return pContext->ThrowNativeError("\"SetUserCvar\" wrapper failed to initialize");
1162 /* TODO: Use UpdateUserSettings function for all engines */
1163 #if SOURCE_ENGINE == SE_DARKMESSIAH
1164 static ValveCall *pUpdateSettings = NULL;
1165 if (!pUpdateSettings)
1167 if (!CreateBaseCall("UpdateUserSettings", ValveCall_Entity, NULL, NULL, 0, &pUpdateSettings))
1169 return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
1171 else if (!pUpdateSettings)
1173 return pContext->ThrowNativeError("\"SetUserCvar\" wrapper failed to initialize");
1177 static int changedOffset = -1;
1179 if (changedOffset == -1)
1181 if (!g_pGameConf->GetOffset("InfoChanged", &changedOffset))
1183 return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
1188 unsigned char *CGameClient = (unsigned char *)pClient - 4;
1191 /* Not really a CBaseEntity* but this works */
1192 CBaseEntity **ebuf = (CBaseEntity **)vptr;
1193 *ebuf = (CBaseEntity *)CGameClient;
1194 DECODE_VALVE_PARAM(2, vparams, 0);
1195 DECODE_VALVE_PARAM(3, vparams, 1);
1196 FINISH_CALL_SIMPLE(NULL);
1198 #if SOURCE_ENGINE == SE_DARKMESSIAH
1199 unsigned char *args = pUpdateSettings->stk_get();
1200 *(void **)args = CGameClient;
1201 pUpdateSettings->call->Execute(args, NULL);
1202 pUpdateSettings->stk_put(args);
1204 uint8_t* changed = (uint8_t *)(CGameClient + changedOffset);
1211 static cell_t GetPlayerResourceEntity(IPluginContext *pContext, const cell_t *params)
1213 if (gamehelpers->GetHandleEntity(g_ResourceEntity) != NULL)
1215 return g_ResourceEntity.GetEntryIndex();
1221 sp_nativeinfo_t g_Natives[] =
1223 {"ExtinguishEntity", ExtinguishEntity},
1224 {"ForcePlayerSuicide", ForcePlayerSuicide},
1225 {"GivePlayerItem", GiveNamedItem},
1226 {"GetPlayerWeaponSlot", GetPlayerWeaponSlot},
1227 {"IgniteEntity", IgniteEntity},
1228 {"RemovePlayerItem", RemovePlayerItem},
1229 {"TeleportEntity", TeleportEntity},
1230 {"SetClientViewEntity", SetClientViewEntity},
1231 {"SetLightStyle", SetLightStyle},
1232 {"SlapPlayer", SlapPlayer},
1233 {"GetClientEyePosition", GetClientEyePosition},
1234 {"GetClientEyeAngles", GetClientEyeAngles},
1235 {"FindEntityByClassname", FindEntityByClassname},
1236 {"CreateEntityByName", CreateEntityByName},
1237 {"DispatchSpawn", DispatchSpawn},
1238 {"DispatchKeyValue", DispatchKeyValue},
1239 {"DispatchKeyValueFloat", DispatchKeyValueFloat},
1240 {"DispatchKeyValueVector", DispatchKeyValueVector},
1241 {"GetClientAimTarget", sm_GetClientAimTarget},
1242 {"SetEntityModel", sm_SetEntityModel},
1243 {"GetPlayerDecalFile", GetPlayerDecalFile},
1244 {"GetServerNetStats", GetServerNetStats},
1245 {"EquipPlayerWeapon", WeaponEquip},
1246 {"ActivateEntity", ActivateEntity},
1247 {"SetClientInfo", SetClientInfo},
1248 {"GetPlayerResourceEntity", GetPlayerResourceEntity},