diff --git a/core/msvc8/sourcemod_mm.vcproj b/core/msvc8/sourcemod_mm.vcproj index 477f1b5ba..e79697fde 100644 --- a/core/msvc8/sourcemod_mm.vcproj +++ b/core/msvc8/sourcemod_mm.vcproj @@ -665,6 +665,10 @@ RelativePath="..\smn_float.cpp" > + + diff --git a/core/smn_halflife.cpp b/core/smn_halflife.cpp new file mode 100644 index 000000000..32d7a6cb8 --- /dev/null +++ b/core/smn_halflife.cpp @@ -0,0 +1,45 @@ +/** + * =============================================================== + * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. + * =============================================================== + * + * This file is not open source and may not be copied without explicit + * written permission of AlliedModders LLC. This file may not be redistributed + * in whole or significant part. + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php + * + * Version: $Id$ + */ + +#include "sm_globals.h" +#include "sourcemm_api.h" + +static cell_t SetRandomSeed(IPluginContext *pContext, const cell_t *params) +{ + engrandom->SetSeed(params[1]); + + return 1; +} + +static cell_t GetRandomFloat(IPluginContext *pContext, const cell_t *params) +{ + float fMin = sp_ctof(params[1]); + float fMax = sp_ctof(params[2]); + + float fRandom = engrandom->RandomFloat(fMin, fMax); + + return sp_ftoc(fRandom); +} + +static cell_t GetRandomInt(IPluginContext *pContext, const cell_t *params) +{ + return engrandom->RandomInt(params[1], params[2]); +} + +REGISTER_NATIVES(halflifeNatives) +{ + {"SetRandomSeed", SetRandomSeed}, + {"GetRandomFloat", GetRandomFloat}, + {"GetRandomInt", GetRandomInt}, + {NULL, NULL}, +}; diff --git a/core/sourcemm_api.cpp b/core/sourcemm_api.cpp index fedd22e80..10f003b03 100644 --- a/core/sourcemm_api.cpp +++ b/core/sourcemm_api.cpp @@ -24,6 +24,7 @@ ISmmPluginManager *g_pMMPlugins = NULL; CGlobalVars *gpGlobals = NULL; ICvar *icvar = NULL; IGameEventManager2 *gameevents = NULL; +IUniformRandomStream *engrandom = NULL; CallClass *enginePatch = NULL; PLUGIN_EXPOSE(SourceMod, g_SourceMod_Core); @@ -37,6 +38,7 @@ bool SourceMod_Core::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen GET_V_IFACE_CURRENT(serverFactory, serverClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS); GET_V_IFACE_CURRENT(engineFactory, icvar, ICvar, VENGINE_CVAR_INTERFACE_VERSION); GET_V_IFACE_CURRENT(engineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2); + GET_V_IFACE_CURRENT(engineFactory, engrandom, IUniformRandomStream, VENGINE_SERVER_RANDOM_INTERFACE_VERSION); if ((g_pMMPlugins = (ISmmPluginManager *)g_SMAPI->MetaFactory(MMIFACE_PLMANAGER, NULL, NULL)) == NULL) { diff --git a/core/sourcemm_api.h b/core/sourcemm_api.h index 5acbfbe7a..6a64614ac 100644 --- a/core/sourcemm_api.h +++ b/core/sourcemm_api.h @@ -19,6 +19,7 @@ #include #include #include +#include /** * @file Contains wrappers around required Metamod:Source API exports @@ -52,6 +53,7 @@ extern ISmmPluginManager *g_pMMPlugins; extern CGlobalVars *gpGlobals; extern IGameEventManager2 *gameevents; extern SourceHook::CallClass *enginePatch; +extern IUniformRandomStream *engrandom; #define ENGINE_CALL(func) SH_CALL(enginePatch, &IVEngineServer::func) diff --git a/plugins/include/sourcemod.inc b/plugins/include/sourcemod.inc index e6ad901b5..b0004cb0b 100644 --- a/plugins/include/sourcemod.inc +++ b/plugins/include/sourcemod.inc @@ -340,5 +340,32 @@ native LogMessage(const String:format[], {Handle,Float,String,_}:...); */ native LogError(const String:format[], {Handle,Float,String,_}:...); +/** + * Sets the seed value for the global Half-Life 2 Random Stream + * + * @param seed Seed value. + * @noreturn + */ +native SetRandomSeed(seed); + +/** + * Returns a random floating point number from the Half-Life 2 Random Stream + * + * @param fMin Minimum random bound. + * @param fMax Maximum random bound. + * @return A random number between (inclusive) fMin and fMax. + */ +native Float:GetRandomFloat(Float:fMin=0.0, Float:fMax=1.0); + +/** + * Returns a random number from the Half-Life 2 Random Stream + * + * @param nmin Minimum random bound. + * @param nmax Maximum random bound. + * @return A random number between (inclusive) nmin and nmax. + */ +native GetRandomInt(nmin, nmax); + + #include #include