Added a random number generator in CSimulation and functions to access it from JavaScript, which will be useful for things like projectile inaccuracy.

This was SVN commit r4183.
This commit is contained in:
Matei 2006-07-30 00:24:04 +00:00
parent 903b97c1a8
commit f007b1dc0c
3 changed files with 49 additions and 6 deletions

View File

@ -443,7 +443,6 @@ JSBool setTimeout( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval
}
}
// Request a callback be executed periodically.
// params: callback [fragment or function], initial delay in ms [int], period in ms [int]
// OR callback [fragment or function], period in ms [int] (initial delay = period)
@ -497,7 +496,6 @@ JSBool setInterval( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rva
}
}
// Cause all periodic functions registered via setInterval to
// no longer be called.
// params:
@ -513,7 +511,6 @@ JSBool cancelInterval( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval*
return( JS_TRUE );
}
// Cause the scheduled task (timeout or interval) with the given ID to
// no longer be called.
// params:
@ -538,6 +535,7 @@ JSBool cancelTimer( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rva
return( JS_TRUE );
}
//Set the simulation rate scalar-time becomes time * SimRate.
//Params: rate [float] : sets SimRate
JSBool setSimRate(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
@ -548,6 +546,28 @@ JSBool setSimRate(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
return JS_TRUE;
}
//Generate a random float in [0, 1) using the simulation's random generator
JSBool simRand(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
{
JSU_REQUIRE_NO_PARAMS();
*rval = ToJSVal( g_Game->GetSimulation()->RandFloat() );
return JS_TRUE;
}
//Generate a random float int between 0 and the given number - 1 using the simulation's RNG
JSBool simRandInt(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
{
JSU_REQUIRE_PARAMS(1);
JSU_ASSERT(JSVAL_IS_INT(argv[0]), "simRandInt(): first parameter must be an int");
*rval = ToJSVal( g_Game->GetSimulation()->RandInt(ToPrimitive<int>(argv[0])) );
return JS_TRUE;
}
// Script profiling functions: Begin timing a piece of code with startXTimer(num)
// and stop timing with stopXTimer(num). The results will be printed to stdout
// when the game exits.
static const uint MAX_XTIMERS = 20;
typedef TimerRdtsc XTimerImpl; // type must be kept in sync with timer.h TIMER_USE_RAW_TICKS
@ -1299,6 +1319,11 @@ JSFunctionSpec ScriptFunctionTable[] =
JS_FUNC(cancelTimer, cancelTimer, 0)
JS_FUNC(setSimRate, setSimRate, 1)
// Random number generator
JS_FUNC(simRand, simRand, 0)
JS_FUNC(simRandInt, simRandInt, 1)
// Profiling
JS_FUNC(startXTimer, startXTimer, 1)
JS_FUNC(stopXTimer, stopXTimer, 1)

View File

@ -35,12 +35,14 @@ using namespace std;
extern CConsole *g_Console;
CSimulation::CSimulation(CGame *pGame):
CSimulation::CSimulation(CGame *pGame, uint randomSeed):
m_pGame(pGame),
m_pWorld(pGame->GetWorld()),
m_pTurnManager((g_SinglePlayerTurnManager=new CSinglePlayerTurnManager())),
m_DeltaTime(0)
{}
{
m_Random.seed((unsigned long) randomSeed);
}
CSimulation::~CSimulation()
{

View File

@ -2,6 +2,7 @@
#define _Simulation_H
#include "lib/types.h"
#include <boost/random.hpp>
class CGame;
class CGameAttributes;
@ -16,6 +17,9 @@ class CSimulation
CTurnManager *m_pTurnManager;
double m_DeltaTime;
// Random number generator
boost::mt19937 m_Random;
// Simulate: move the deterministic simulation forward by one interval
void Simulate();
@ -25,7 +29,7 @@ class CSimulation
void Interpolate(double frameTime, double offset);
public:
CSimulation(CGame *pGame);
CSimulation(CGame *pGame, uint randomSeed=0);
~CSimulation();
inline void SetTurnManager(CTurnManager *pTurnManager)
@ -49,6 +53,18 @@ public:
static uint TranslateMessage(CNetMessage *, uint oldMask, void *userdata);
void QueueLocalCommand(CNetMessage *pMsg);
// Get a random integer between 0 and maxVal-1 from the simulation's random number generator
int RandInt(int maxVal)
{
return m_Random() % maxVal;
}
// Get a random float in [0, 1) from the simulation's random number generator
float RandFloat()
{
return float(m_Random()) * (1.0f/4294967296.0f);
}
};
#endif