mirror of https://github.com/OpenMW/openmw.git
Update to latest Monster, and minor simplification of the scripts.
git-svn-id: https://openmw.svn.sourceforge.net/svnroot/openmw/trunk@83 ea6a568a-9f4f-0410-981a-c910a81bb256pull/7/head
parent
656007cf01
commit
6dcf6c565a
@ -0,0 +1,49 @@
|
||||
|
||||
// This module provides simple random number generation. Since this is
|
||||
// intended for game development, speed and simplicity is favored over
|
||||
// flexibility and random number quality.
|
||||
module monster.modules.random;
|
||||
|
||||
import monster.monster;
|
||||
import std.random;
|
||||
|
||||
const char[] moduleDef =
|
||||
"module random;
|
||||
|
||||
native uint rand(); // Return a number between 0 and uint.max, inclusive
|
||||
native float frand(); // Return a number between 0 and 1, inclusive
|
||||
|
||||
// Return a random number between a and b, inclusive. Allows negative
|
||||
// numbers, and works with a>b, a<b and a==b
|
||||
native int randInt(int a, int b);
|
||||
"; //"
|
||||
|
||||
const float _frandFactor = 1.0/uint.max;
|
||||
|
||||
// Return a random integer between a and b, inclusive.
|
||||
int randInt(int a, int b)
|
||||
out(result)
|
||||
{
|
||||
// Result must be in range m <= result <= M, where m=min(a,b) and M=max(a,b)
|
||||
if(b >= a) assert( (a <= result) && (result <= b) );
|
||||
else if(a > b) assert( (b <= result) && (result <= a) );
|
||||
}
|
||||
body
|
||||
{
|
||||
if(a>b) return cast(int)(rand() % (a-b+1)) + b;
|
||||
else if(b>a) return cast(int)(rand() % (b-a+1)) + a;
|
||||
else return a;
|
||||
}
|
||||
|
||||
void initRandomModule()
|
||||
{
|
||||
static MonsterClass mc;
|
||||
if(mc !is null) return;
|
||||
|
||||
mc = new MonsterClass(MC.String, moduleDef, "random");
|
||||
|
||||
mc.bind("rand", { stack.pushInt(rand()); });
|
||||
mc.bind("frand", { stack.pushFloat(rand()*_frandFactor); });
|
||||
mc.bind("randInt", { stack.pushInt(randInt(stack.popInt,
|
||||
stack.popInt)); });
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
module random;
|
||||
|
||||
native uint rand(); // Return a number between 0 and uint.max, inclusive
|
||||
native float frand(); // Return a number between 0 and 1, inclusive
|
||||
|
||||
// Return a random number between a and b, inclusive. Allows negative
|
||||
// numbers, and works with a>b, a<b and a==b
|
||||
native int randInt(int a, int b);
|
@ -1,30 +0,0 @@
|
||||
/*
|
||||
OpenMW - The completely unofficial reimplementation of Morrowind
|
||||
Copyright (C) 2008 Nicolay Korslund
|
||||
Email: < korslund@gmail.com >
|
||||
WWW: http://openmw.snaptoad.com/
|
||||
|
||||
This file (object.mn) is part of the OpenMW package.
|
||||
|
||||
OpenMW is distributed as free software: you can redistribute it
|
||||
and/or modify it under the terms of the GNU General Public License
|
||||
version 3, as published by the Free Software Foundation.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
version 3 along with this program. If not, see
|
||||
http://www.gnu.org/licenses/ .
|
||||
|
||||
*/
|
||||
|
||||
// This is the base class of all OpenMW Monster classes.
|
||||
class Object;
|
||||
|
||||
import io, timer;
|
||||
|
||||
// Get a random number between a and b (inclusive)
|
||||
native int randInt(int a, int b);
|
@ -1,155 +0,0 @@
|
||||
/*
|
||||
OpenMW - The completely unofficial reimplementation of Morrowind
|
||||
Copyright (C) 2008 Nicolay Korslund
|
||||
Email: < korslund@gmail.com >
|
||||
WWW: http://openmw.snaptoad.com/
|
||||
|
||||
This file (random.d) is part of the OpenMW package.
|
||||
|
||||
OpenMW is distributed as free software: you can redistribute it
|
||||
and/or modify it under the terms of the GNU General Public License
|
||||
version 3, as published by the Free Software Foundation.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
version 3 along with this program. If not, see
|
||||
http://www.gnu.org/licenses/ .
|
||||
|
||||
*/
|
||||
|
||||
module util.random;
|
||||
|
||||
private import std.date;
|
||||
private import std.random;
|
||||
|
||||
abstract class Random
|
||||
{
|
||||
static const double scale = 1.0/uint.max;
|
||||
|
||||
// Initialize from current time
|
||||
this() { initialize(); }
|
||||
|
||||
// Initialize from parameter
|
||||
this(long seed) { initialize(seed); }
|
||||
|
||||
// Reinitialize with seed
|
||||
abstract void initialize(long newSeed);
|
||||
|
||||
// Produce random numbers between 0 and uint.max
|
||||
abstract uint rand();
|
||||
|
||||
// Default is to initialize using current time as seed
|
||||
void initialize() { initialize(getUTCtime()); }
|
||||
|
||||
// Produce a uniform random number between 0 and 1
|
||||
double random()
|
||||
{
|
||||
return rand() * scale;
|
||||
}
|
||||
|
||||
// Return a uniform random number between a and b. Works for the
|
||||
// both the cases a < b and a > b.
|
||||
double random(double a, double b)
|
||||
in
|
||||
{
|
||||
// We only disallow nan parameters
|
||||
assert(a <>= b);
|
||||
}
|
||||
out(result)
|
||||
{
|
||||
// Result must be in range m < result < M, where m=min(a,b) and M=max(a,b)
|
||||
if(b > a) assert( (a < result) && (result < b) );
|
||||
else if(a > b) assert( (b < result) && (result < a) );
|
||||
}
|
||||
body
|
||||
{ return random()*(b - a) + a; }
|
||||
|
||||
// Return a random integer between a and b, inclusive.
|
||||
int randInt(int a, int b)
|
||||
out(result)
|
||||
{
|
||||
// Result must be in range m <= result <= M, where m=min(a,b) and M=max(a,b)
|
||||
if(b >= a) assert( (a <= result) && (result <= b) );
|
||||
else if(a > b) assert( (b <= result) && (result <= a) );
|
||||
}
|
||||
body
|
||||
{
|
||||
if(a>b) return cast(int)(rand() % (a-b+1)) + b;
|
||||
else if(b>a) return cast(int)(rand() % (b-a+1)) + a;
|
||||
else return a;
|
||||
}
|
||||
|
||||
// Allow using "function call" syntax:
|
||||
//
|
||||
// Random ran = new Random1;
|
||||
// double d = ran(); // Calls ran.random()
|
||||
// d = ran(a,b); // Calls ran.random(a,b);
|
||||
double opCall() { return random(); }
|
||||
double opCall(double a, double b) { return random(a, b); }
|
||||
|
||||
// Return the seed originally given the object
|
||||
long getSeed() {return origSeed;}
|
||||
|
||||
protected:
|
||||
long origSeed; // Value used to seed the generator
|
||||
}
|
||||
|
||||
// Uses the standard library generator
|
||||
class DRand : Random
|
||||
{
|
||||
// Initialize from current time
|
||||
this() { super(); }
|
||||
|
||||
// Initialize from parameter
|
||||
this(long seed) { super(seed); }
|
||||
|
||||
uint rand() { return std.random.rand(); }
|
||||
|
||||
void initialize(long newSeed)
|
||||
{
|
||||
origSeed = newSeed;
|
||||
rand_seed(cast(uint)newSeed, 0);
|
||||
}
|
||||
|
||||
alias Random.initialize initialize;
|
||||
|
||||
unittest
|
||||
{
|
||||
struct tmp { import std.stdio; }
|
||||
alias tmp.writef writef;
|
||||
alias tmp.writefln writefln;
|
||||
|
||||
writefln("Unittest for class DRand");
|
||||
|
||||
DRand ran = new DRand;
|
||||
writefln("Seed (from time) = ", ran.getSeed());
|
||||
|
||||
// Take a look at some numbers on screen
|
||||
writefln("Some random numbers in [0,1]:");
|
||||
for(int i=0; i<10; i++)
|
||||
writef(" ", ran());
|
||||
|
||||
ran = new DRand(0);
|
||||
writefln("\nNew seed (preset) = ", ran.getSeed());
|
||||
|
||||
// Take a look at some numbers on screen
|
||||
writefln("Some random numbers in [0,1]:");
|
||||
for(int i=0; i<10; i++)
|
||||
writef(" ", ran());
|
||||
|
||||
// Check that all interfaces work (compile time)
|
||||
ran();
|
||||
ran(1,2);
|
||||
ran.random();
|
||||
ran.random(3,4);
|
||||
ran.initialize();
|
||||
ran.initialize(10);
|
||||
ran.randInt(-3,5);
|
||||
|
||||
writefln("\nEnd of unittest for class DRand\n");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue