core/smn_halflife.cpp
author Scott Ehlert <ds@alliedmods.net>
Wed Jun 06 20:42:46 2007 +0000 (2007-06-06)
changeset 891 30343d215489
parent 874 91a057a8c782
child 892 0702fda305f0
permissions -rw-r--r--
Changed a few natives/stocks that should have used IsClientInGame() rather than IsClientConnected()
dvander@530
     1
/**
ds@653
     2
 * vim: set ts=4 :
dvander@530
     3
 * ===============================================================
dvander@530
     4
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
dvander@530
     5
 * ===============================================================
dvander@530
     6
 *
dvander@530
     7
 * This file is not open source and may not be copied without explicit
dvander@530
     8
 * written permission of AlliedModders LLC.  This file may not be redistributed 
dvander@530
     9
 * in whole or significant part.
dvander@530
    10
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
dvander@530
    11
 *
dvander@530
    12
 * Version: $Id$
dvander@530
    13
 */
dvander@530
    14
dvander@530
    15
#include "sm_globals.h"
dvander@532
    16
#include "sourcemod.h"
dvander@530
    17
#include "sourcemm_api.h"
ds@873
    18
#include "HandleSys.h"
dvander@591
    19
#include "PlayerManager.h"
ds@873
    20
#include "HalfLife2.h"
ds@874
    21
ds@874
    22
#define HUD_PRINTTALK		3
ds@874
    23
#define HUD_PRINTCENTER		4
faluco@682
    24
faluco@682
    25
IServerPluginCallbacks *g_VSP = NULL;
faluco@682
    26
faluco@682
    27
class HalfLifeNatives : public SMGlobalClass
faluco@682
    28
{
faluco@682
    29
public: //SMGlobalClass
faluco@682
    30
	void OnSourceModVSPReceived(IServerPluginCallbacks *iface)
faluco@682
    31
	{
faluco@682
    32
		g_VSP = iface;
faluco@682
    33
	}
faluco@682
    34
};
dvander@530
    35
dvander@530
    36
static cell_t SetRandomSeed(IPluginContext *pContext, const cell_t *params)
dvander@530
    37
{
dvander@530
    38
	engrandom->SetSeed(params[1]);
dvander@530
    39
dvander@530
    40
	return 1;
dvander@530
    41
}
dvander@530
    42
dvander@530
    43
static cell_t GetRandomFloat(IPluginContext *pContext, const cell_t *params)
dvander@530
    44
{
dvander@530
    45
	float fMin = sp_ctof(params[1]);
dvander@530
    46
	float fMax = sp_ctof(params[2]);
dvander@530
    47
dvander@530
    48
	float fRandom = engrandom->RandomFloat(fMin, fMax);
dvander@530
    49
dvander@530
    50
	return sp_ftoc(fRandom);
dvander@530
    51
}
dvander@530
    52
dvander@530
    53
static cell_t GetRandomInt(IPluginContext *pContext, const cell_t *params)
dvander@530
    54
{
dvander@530
    55
	return engrandom->RandomInt(params[1], params[2]);
dvander@530
    56
}
dvander@530
    57
dvander@532
    58
static cell_t IsMapValid(IPluginContext *pContext, const cell_t *params)
dvander@532
    59
{
dvander@532
    60
	char *map;
dvander@532
    61
	pContext->LocalToString(params[1], &map);
dvander@532
    62
dvander@532
    63
	return engine->IsMapValid(map);
dvander@532
    64
}
dvander@532
    65
dvander@532
    66
static cell_t IsDedicatedServer(IPluginContext *pContext, const cell_t *params)
dvander@532
    67
{
dvander@532
    68
	return engine->IsDedicatedServer();
dvander@532
    69
}
dvander@532
    70
dvander@532
    71
static cell_t GetEngineTime(IPluginContext *pContext, const cell_t *params)
dvander@532
    72
{
dvander@532
    73
	float fTime = engine->Time();
dvander@532
    74
	return sp_ftoc(fTime);
dvander@532
    75
}
dvander@532
    76
dvander@532
    77
static cell_t GetGameTime(IPluginContext *pContext, const cell_t *params)
dvander@532
    78
{
dvander@532
    79
	return sp_ftoc(gpGlobals->curtime);
dvander@532
    80
}
dvander@532
    81
dvander@532
    82
static cell_t CreateFakeClient(IPluginContext *pContext, const cell_t *params)
dvander@532
    83
{
dvander@532
    84
	char *netname;
dvander@532
    85
dvander@532
    86
	pContext->LocalToString(params[1], &netname);
dvander@532
    87
dvander@532
    88
	edict_t *pEdict = engine->CreateFakeClient(netname);
dvander@532
    89
dvander@532
    90
	/* :TODO: does the engine fire forwards for us and whatnot? no idea... */
dvander@532
    91
dvander@532
    92
	if (!pEdict)
dvander@532
    93
	{
dvander@532
    94
		return 0;
dvander@532
    95
	}
dvander@532
    96
dvander@532
    97
	return engine->IndexOfEdict(pEdict);
dvander@532
    98
}
dvander@532
    99
dvander@532
   100
static cell_t SetFakeClientConVar(IPluginContext *pContext, const cell_t *params)
dvander@532
   101
{
dvander@532
   102
	CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
dvander@532
   103
dvander@532
   104
	if (!pPlayer)
dvander@532
   105
	{
dvander@532
   106
		return pContext->ThrowNativeError("Player %d is not a valid player", params[1]);
dvander@532
   107
	}
dvander@532
   108
dvander@532
   109
	if (!pPlayer->IsConnected())
dvander@532
   110
	{
dvander@532
   111
		return pContext->ThrowNativeError("Player %d is not connected", params[1]);
dvander@532
   112
	}
dvander@532
   113
dvander@532
   114
	if (!pPlayer->IsFakeClient())
dvander@532
   115
	{
dvander@532
   116
		return pContext->ThrowNativeError("Player %d is not a fake client", params[1]);
dvander@532
   117
	}
dvander@532
   118
dvander@532
   119
	char *cvar, *value;
dvander@532
   120
dvander@532
   121
	pContext->LocalToString(params[2], &cvar);
dvander@532
   122
	pContext->LocalToString(params[3], &value);
dvander@532
   123
dvander@532
   124
	engine->SetFakeClientConVarValue(pPlayer->GetEdict(), cvar, value);
dvander@532
   125
dvander@532
   126
	return 1;
dvander@532
   127
}
dvander@532
   128
dvander@532
   129
static cell_t GetGameDescription(IPluginContext *pContext, const cell_t *params)
dvander@532
   130
{
dvander@532
   131
	const char *description;
dvander@532
   132
dvander@532
   133
	if (params[3])
dvander@532
   134
	{
dvander@532
   135
		description = gamedll->GetGameDescription();
dvander@532
   136
	} else {
dvander@532
   137
		description = SERVER_CALL(GetGameDescription)();
dvander@532
   138
	}
dvander@532
   139
dvander@532
   140
	size_t numBytes;
dvander@532
   141
	
dvander@532
   142
	pContext->StringToLocalUTF8(params[1], params[2], description, &numBytes);
dvander@532
   143
dvander@532
   144
	return numBytes;
dvander@532
   145
}
dvander@532
   146
ds@721
   147
static cell_t GetGameFolderName(IPluginContext *pContext, const cell_t *params)
ds@721
   148
{
ds@729
   149
	const char *name = g_SourceMod.GetGameFolderName();
ds@721
   150
	size_t numBytes;
ds@721
   151
ds@721
   152
	pContext->StringToLocalUTF8(params[1], params[2], name, &numBytes);
ds@721
   153
ds@721
   154
	return numBytes;
ds@721
   155
}
ds@721
   156
dvander@532
   157
static cell_t GetCurrentMap(IPluginContext *pContext, const cell_t *params)
dvander@532
   158
{
dvander@532
   159
	size_t bytes;
dvander@532
   160
	pContext->StringToLocalUTF8(params[1], params[2], STRING(gpGlobals->mapname), &bytes);
dvander@532
   161
	return bytes;
dvander@532
   162
}
dvander@532
   163
faluco@656
   164
static cell_t PrecacheModel(IPluginContext *pContext, const cell_t *params)
faluco@656
   165
{
faluco@656
   166
	char *model;
faluco@656
   167
	pContext->LocalToString(params[1], &model);
faluco@656
   168
faluco@656
   169
	return engine->PrecacheModel(model, params[2] ? true : false);
faluco@656
   170
}
faluco@656
   171
faluco@656
   172
static cell_t PrecacheSentenceFile(IPluginContext *pContext, const cell_t *params)
faluco@656
   173
{
faluco@656
   174
	char *sentencefile;
faluco@656
   175
	pContext->LocalToString(params[1], &sentencefile);
faluco@656
   176
faluco@656
   177
	return engine->PrecacheSentenceFile(sentencefile, params[2] ? true : false);
faluco@656
   178
}
faluco@656
   179
faluco@656
   180
static cell_t PrecacheDecal(IPluginContext *pContext, const cell_t *params)
faluco@656
   181
{
faluco@656
   182
	char *decal;
faluco@656
   183
	pContext->LocalToString(params[1], &decal);
faluco@656
   184
faluco@656
   185
	return engine->PrecacheDecal(decal, params[2] ? true : false);
faluco@656
   186
}
faluco@656
   187
faluco@656
   188
static cell_t PrecacheGeneric(IPluginContext *pContext, const cell_t *params)
faluco@656
   189
{
faluco@656
   190
	char *generic;
faluco@656
   191
	pContext->LocalToString(params[1], &generic);
faluco@656
   192
faluco@656
   193
	return engine->PrecacheGeneric(generic, params[2] ? true : false);
faluco@656
   194
}
faluco@656
   195
faluco@656
   196
static cell_t IsModelPrecached(IPluginContext *pContext, const cell_t *params)
faluco@656
   197
{
faluco@656
   198
	char *model;
faluco@656
   199
	pContext->LocalToString(params[1], &model);
faluco@656
   200
faluco@656
   201
	return engine->IsModelPrecached(model) ? 1 : 0;
faluco@656
   202
}
faluco@656
   203
faluco@656
   204
static cell_t IsDecalPrecached(IPluginContext *pContext, const cell_t *params)
faluco@656
   205
{
faluco@656
   206
	char *decal;
faluco@656
   207
	pContext->LocalToString(params[1], &decal);
faluco@656
   208
faluco@656
   209
	return engine->IsDecalPrecached(decal) ? 1 : 0;
faluco@656
   210
}
faluco@656
   211
faluco@656
   212
static cell_t IsGenericPrecached(IPluginContext *pContext, const cell_t *params)
faluco@656
   213
{
faluco@656
   214
	char *generic;
faluco@656
   215
	pContext->LocalToString(params[1], &generic);
faluco@656
   216
faluco@656
   217
	return engine->IsGenericPrecached(generic) ? 1 : 0;
faluco@656
   218
}
faluco@656
   219
faluco@656
   220
static cell_t PrecacheSound(IPluginContext *pContext, const cell_t *params)
faluco@656
   221
{
faluco@656
   222
	char *sample;
faluco@656
   223
	pContext->LocalToString(params[1], &sample);
faluco@656
   224
faluco@656
   225
	return enginesound->PrecacheSound(sample, params[2] ? true : false) ? 1 : 0;
faluco@656
   226
}
faluco@656
   227
faluco@656
   228
static cell_t IsSoundPrecached(IPluginContext *pContext, const cell_t *params)
faluco@656
   229
{
faluco@656
   230
	char *sample;
faluco@656
   231
	pContext->LocalToString(params[1], &sample);
faluco@656
   232
faluco@656
   233
	return enginesound->IsSoundPrecached(sample) ? 1 : 0;
faluco@656
   234
}
faluco@656
   235
faluco@659
   236
static cell_t FakeClientCommand(IPluginContext *pContext, const cell_t *params)
faluco@659
   237
{
faluco@659
   238
	CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
faluco@659
   239
faluco@659
   240
	if (!pPlayer)
faluco@659
   241
	{
faluco@659
   242
		return pContext->ThrowNativeError("Player %d is not a valid player", params[1]);
faluco@659
   243
	}
faluco@659
   244
faluco@659
   245
	if (!pPlayer->IsConnected())
faluco@659
   246
	{
faluco@659
   247
		return pContext->ThrowNativeError("Player %d is not connected", params[1]);
faluco@659
   248
	}
faluco@659
   249
faluco@659
   250
	char buffer[256];
ds@873
   251
	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
faluco@659
   252
faluco@659
   253
	serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), buffer);
faluco@659
   254
faluco@659
   255
	return 1;
faluco@659
   256
}
faluco@659
   257
faluco@682
   258
static cell_t smn_CreateDialog(IPluginContext *pContext, const cell_t *params)
faluco@682
   259
{
faluco@682
   260
	KeyValues *pKV;
faluco@682
   261
	HandleError herr;
faluco@682
   262
	Handle_t hndl = static_cast<Handle_t>(params[2]);
faluco@682
   263
	CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
faluco@682
   264
faluco@682
   265
	if (!pPlayer)
faluco@682
   266
	{
ds@891
   267
		return pContext->ThrowNativeError("Client %d is not valid", params[1]);
faluco@682
   268
	}
faluco@682
   269
ds@891
   270
	if (!pPlayer->IsInGame())
faluco@682
   271
	{
ds@891
   272
		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
faluco@682
   273
	}
faluco@682
   274
faluco@682
   275
	pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
faluco@682
   276
	if (herr != HandleError_None)
faluco@682
   277
	{
faluco@682
   278
		return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@682
   279
	}
faluco@682
   280
faluco@682
   281
	serverpluginhelpers->CreateMessage(pPlayer->GetEdict(), static_cast<DIALOG_TYPE>(params[3]), pKV, g_VSP);
faluco@682
   282
faluco@682
   283
	return 1;
faluco@682
   284
}
faluco@682
   285
ds@873
   286
static cell_t PrintToChat(IPluginContext *pContext, const cell_t *params)
ds@873
   287
{
ds@873
   288
	int client = params[1];
ds@873
   289
	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
ds@873
   290
ds@873
   291
	if (!pPlayer)
ds@873
   292
	{
ds@891
   293
		return pContext->ThrowNativeError("Client %d is not valid", params[1]);
ds@873
   294
	}
ds@873
   295
ds@891
   296
	if (!pPlayer->IsInGame())
ds@873
   297
	{
ds@891
   298
		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
ds@873
   299
	}
ds@873
   300
ds@873
   301
	char buffer[256];
ds@873
   302
	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
ds@873
   303
ds@873
   304
	g_HL2.TextMsg(client, HUD_PRINTTALK, buffer);
ds@873
   305
ds@873
   306
	return 1;
ds@873
   307
}
ds@873
   308
ds@873
   309
static cell_t PrintCenterText(IPluginContext *pContext, const cell_t *params)
ds@873
   310
{
ds@873
   311
	int client = params[1];
ds@873
   312
	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
ds@873
   313
ds@873
   314
	if (!pPlayer)
ds@873
   315
	{
ds@891
   316
		return pContext->ThrowNativeError("Client %d is not valid", params[1]);
ds@873
   317
	}
ds@873
   318
ds@891
   319
	if (!pPlayer->IsInGame())
ds@873
   320
	{
ds@891
   321
		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
ds@873
   322
	}
ds@873
   323
ds@873
   324
	char buffer[256];
ds@873
   325
	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
ds@873
   326
ds@873
   327
	g_HL2.TextMsg(client, HUD_PRINTCENTER, buffer);
ds@873
   328
ds@873
   329
	return 1;
ds@873
   330
}
ds@873
   331
faluco@682
   332
static HalfLifeNatives s_HalfLifeNatives;
faluco@682
   333
dvander@530
   334
REGISTER_NATIVES(halflifeNatives)
dvander@530
   335
{
dvander@532
   336
	{"CreateFakeClient",		CreateFakeClient},
dvander@532
   337
	{"GetCurrentMap",			GetCurrentMap},
dvander@532
   338
	{"GetEngineTime",			GetEngineTime},
dvander@532
   339
	{"GetGameDescription",		GetGameDescription},
ds@721
   340
	{"GetGameFolderName",		GetGameFolderName},
dvander@532
   341
	{"GetGameTime",				GetGameTime},
dvander@530
   342
	{"GetRandomFloat",			GetRandomFloat},
dvander@530
   343
	{"GetRandomInt",			GetRandomInt},
dvander@532
   344
	{"IsDedicatedServer",		IsDedicatedServer},
dvander@532
   345
	{"IsMapValid",				IsMapValid},
dvander@532
   346
	{"SetFakeClientConVar",		SetFakeClientConVar},
dvander@532
   347
	{"SetRandomSeed",			SetRandomSeed},
faluco@656
   348
	{"PrecacheModel",			PrecacheModel},
faluco@656
   349
	{"PrecacheSentenceFile",	PrecacheSentenceFile},
faluco@656
   350
	{"PrecacheDecal",			PrecacheDecal},
faluco@656
   351
	{"PrecacheGeneric",			PrecacheGeneric},
faluco@656
   352
	{"IsModelPrecached",		IsModelPrecached},
faluco@656
   353
	{"IsDecalPrecached",		IsDecalPrecached},
faluco@656
   354
	{"IsGenericPrecached",		IsGenericPrecached},
faluco@656
   355
	{"PrecacheSound",			PrecacheSound},
faluco@656
   356
	{"IsSoundPrecached",		IsSoundPrecached},
faluco@659
   357
	{"FakeClientCommand",		FakeClientCommand},
faluco@682
   358
	{"CreateDialog",			smn_CreateDialog},
ds@873
   359
	{"PrintToChat",				PrintToChat},
ds@873
   360
	{"PrintCenterText",			PrintCenterText},
dvander@530
   361
	{NULL,						NULL},
dvander@530
   362
};