core/smn_halflife.cpp
author David Anderson <dvander@alliedmods.net>
Thu Mar 15 20:44:23 2007 +0000 (2007-03-15)
changeset 619 6934bf39171e
parent 591 86e97f3eda1f
child 653 ef60870067ff
permissions -rw-r--r--
random touchups that might not be so great
     1 /**
     2  * ===============================================================
     3  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ===============================================================
     5  *
     6  * This file is not open source and may not be copied without explicit
     7  * written permission of AlliedModders LLC.  This file may not be redistributed 
     8  * in whole or significant part.
     9  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #include "sm_globals.h"
    15 #include "sourcemod.h"
    16 #include "sourcemm_api.h"
    17 #include "PlayerManager.h"
    18 
    19 static cell_t SetRandomSeed(IPluginContext *pContext, const cell_t *params)
    20 {
    21 	engrandom->SetSeed(params[1]);
    22 
    23 	return 1;
    24 }
    25 
    26 static cell_t GetRandomFloat(IPluginContext *pContext, const cell_t *params)
    27 {
    28 	float fMin = sp_ctof(params[1]);
    29 	float fMax = sp_ctof(params[2]);
    30 
    31 	float fRandom = engrandom->RandomFloat(fMin, fMax);
    32 
    33 	return sp_ftoc(fRandom);
    34 }
    35 
    36 static cell_t GetRandomInt(IPluginContext *pContext, const cell_t *params)
    37 {
    38 	return engrandom->RandomInt(params[1], params[2]);
    39 }
    40 
    41 static cell_t IsMapValid(IPluginContext *pContext, const cell_t *params)
    42 {
    43 	char *map;
    44 	pContext->LocalToString(params[1], &map);
    45 
    46 	return engine->IsMapValid(map);
    47 }
    48 
    49 static cell_t IsDedicatedServer(IPluginContext *pContext, const cell_t *params)
    50 {
    51 	return engine->IsDedicatedServer();
    52 }
    53 
    54 static cell_t GetEngineTime(IPluginContext *pContext, const cell_t *params)
    55 {
    56 	float fTime = engine->Time();
    57 	return sp_ftoc(fTime);
    58 }
    59 
    60 static cell_t GetGameTime(IPluginContext *pContext, const cell_t *params)
    61 {
    62 	return sp_ftoc(gpGlobals->curtime);
    63 }
    64 
    65 static cell_t CreateFakeClient(IPluginContext *pContext, const cell_t *params)
    66 {
    67 	char *netname;
    68 
    69 	pContext->LocalToString(params[1], &netname);
    70 
    71 	edict_t *pEdict = engine->CreateFakeClient(netname);
    72 
    73 	/* :TODO: does the engine fire forwards for us and whatnot? no idea... */
    74 
    75 	if (!pEdict)
    76 	{
    77 		return 0;
    78 	}
    79 
    80 	return engine->IndexOfEdict(pEdict);
    81 }
    82 
    83 static cell_t SetFakeClientConVar(IPluginContext *pContext, const cell_t *params)
    84 {
    85 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
    86 
    87 	if (!pPlayer)
    88 	{
    89 		return pContext->ThrowNativeError("Player %d is not a valid player", params[1]);
    90 	}
    91 
    92 	if (!pPlayer->IsConnected())
    93 	{
    94 		return pContext->ThrowNativeError("Player %d is not connected", params[1]);
    95 	}
    96 
    97 	if (!pPlayer->IsFakeClient())
    98 	{
    99 		return pContext->ThrowNativeError("Player %d is not a fake client", params[1]);
   100 	}
   101 
   102 	char *cvar, *value;
   103 
   104 	pContext->LocalToString(params[2], &cvar);
   105 	pContext->LocalToString(params[3], &value);
   106 
   107 	engine->SetFakeClientConVarValue(pPlayer->GetEdict(), cvar, value);
   108 
   109 	return 1;
   110 }
   111 
   112 static cell_t GetGameDescription(IPluginContext *pContext, const cell_t *params)
   113 {
   114 	const char *description;
   115 
   116 	if (params[3])
   117 	{
   118 		description = gamedll->GetGameDescription();
   119 	} else {
   120 		description = SERVER_CALL(GetGameDescription)();
   121 	}
   122 
   123 	size_t numBytes;
   124 	
   125 	pContext->StringToLocalUTF8(params[1], params[2], description, &numBytes);
   126 
   127 	return numBytes;
   128 }
   129 
   130 static cell_t GetCurrentMap(IPluginContext *pContext, const cell_t *params)
   131 {
   132 	size_t bytes;
   133 	pContext->StringToLocalUTF8(params[1], params[2], STRING(gpGlobals->mapname), &bytes);
   134 	return bytes;
   135 }
   136 
   137 REGISTER_NATIVES(halflifeNatives)
   138 {
   139 	{"CreateFakeClient",		CreateFakeClient},
   140 	{"GetCurrentMap",			GetCurrentMap},
   141 	{"GetEngineTime",			GetEngineTime},
   142 	{"GetGameDescription",		GetGameDescription},
   143 	{"GetGameTime",				GetGameTime},
   144 	{"GetRandomFloat",			GetRandomFloat},
   145 	{"GetRandomInt",			GetRandomInt},
   146 	{"IsDedicatedServer",		IsDedicatedServer},
   147 	{"IsMapValid",				IsMapValid},
   148 	{"SetFakeClientConVar",		SetFakeClientConVar},
   149 	{"SetRandomSeed",			SetRandomSeed},
   150 	{NULL,						NULL},
   151 };