core/smn_halflife.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Mar 22 21:50:20 2007 +0000 (2007-03-22)
changeset 653 ef60870067ff
parent 591 86e97f3eda1f
child 656 351fca8ce6a5
permissions -rw-r--r--
More consistency of some sort...

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