2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2002-10-12 11:37:38 +00:00
|
|
|
* Set random/camera stuff
|
|
|
|
*
|
|
|
|
*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2008-04-16 22:40:48 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): none yet.
|
|
|
|
*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-25 13:32:11 +00:00
|
|
|
/** \file gameengine/GameLogic/SCA_RandomActuator.cpp
|
|
|
|
* \ingroup gamelogic
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-01-27 00:02:25 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "BoolValue.h"
|
|
|
|
#include "IntValue.h"
|
|
|
|
#include "FloatValue.h"
|
|
|
|
#include "SCA_IActuator.h"
|
|
|
|
#include "SCA_RandomActuator.h"
|
|
|
|
#include "math.h"
|
|
|
|
#include "MT_Transform.h"
|
2002-11-25 15:29:57 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
/* Native functions */
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
SCA_RandomActuator::SCA_RandomActuator(SCA_IObject *gameobj,
|
|
|
|
long seed,
|
|
|
|
SCA_RandomActuator::KX_RANDOMACT_MODE mode,
|
|
|
|
float para1,
|
|
|
|
float para2,
|
2009-06-28 11:22:26 +00:00
|
|
|
const STR_String &propName)
|
2009-09-24 21:22:24 +00:00
|
|
|
: SCA_IActuator(gameobj, KX_ACT_RANDOM),
|
2002-10-12 11:37:38 +00:00
|
|
|
m_propname(propName),
|
|
|
|
m_parameter1(para1),
|
2004-03-22 22:02:18 +00:00
|
|
|
m_parameter2(para2),
|
|
|
|
m_distribution(mode)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
m_base = new SCA_RandomNumberGenerator(seed);
|
|
|
|
m_counter = 0;
|
|
|
|
enforceConstraints();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SCA_RandomActuator::~SCA_RandomActuator()
|
|
|
|
{
|
2009-06-08 20:08:19 +00:00
|
|
|
m_base->Release();
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CValue* SCA_RandomActuator::GetReplica()
|
|
|
|
{
|
|
|
|
SCA_RandomActuator* replica = new SCA_RandomActuator(*this);
|
2008-03-01 19:05:41 +00:00
|
|
|
// replication just copy the m_base pointer => common random generator
|
2002-10-12 11:37:38 +00:00
|
|
|
replica->ProcessReplica();
|
|
|
|
return replica;
|
|
|
|
}
|
|
|
|
|
2009-06-08 20:08:19 +00:00
|
|
|
void SCA_RandomActuator::ProcessReplica()
|
|
|
|
{
|
|
|
|
SCA_IActuator::ProcessReplica();
|
|
|
|
// increment reference count so that we can release the generator at the end
|
|
|
|
m_base->AddRef();
|
|
|
|
}
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
|
2004-10-16 11:41:50 +00:00
|
|
|
bool SCA_RandomActuator::Update()
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2005-03-09 19:45:59 +00:00
|
|
|
//bool result = false; /*unused*/
|
2002-10-12 11:37:38 +00:00
|
|
|
bool bNegativeEvent = IsNegativeEvent();
|
|
|
|
|
|
|
|
RemoveAllEvents();
|
|
|
|
|
|
|
|
|
2004-03-22 22:02:18 +00:00
|
|
|
CValue *tmpval = NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
if (bNegativeEvent)
|
|
|
|
return false; // do nothing on negative events
|
|
|
|
|
|
|
|
switch (m_distribution) {
|
|
|
|
case KX_RANDOMACT_BOOL_CONST: {
|
|
|
|
/* un petit peu filthy */
|
|
|
|
bool res = !(m_parameter1 < 0.5);
|
|
|
|
tmpval = new CBoolValue(res);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KX_RANDOMACT_BOOL_UNIFORM: {
|
|
|
|
/* flip a coin */
|
|
|
|
bool res;
|
|
|
|
if (m_counter > 31) {
|
|
|
|
m_previous = m_base->Draw();
|
|
|
|
res = ((m_previous & 0x1) == 0);
|
|
|
|
m_counter = 1;
|
|
|
|
} else {
|
|
|
|
res = (((m_previous >> m_counter) & 0x1) == 0);
|
|
|
|
m_counter++;
|
|
|
|
}
|
|
|
|
tmpval = new CBoolValue(res);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KX_RANDOMACT_BOOL_BERNOUILLI: {
|
|
|
|
/* 'percentage' */
|
|
|
|
bool res;
|
|
|
|
res = (m_base->DrawFloat() < m_parameter1);
|
|
|
|
tmpval = new CBoolValue(res);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KX_RANDOMACT_INT_CONST: {
|
|
|
|
/* constant */
|
|
|
|
tmpval = new CIntValue((int) floor(m_parameter1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KX_RANDOMACT_INT_UNIFORM: {
|
|
|
|
/* uniform (toss a die) */
|
|
|
|
int res;
|
|
|
|
/* The [0, 1] interval is projected onto the [min, max+1] domain, */
|
|
|
|
/* and then rounded. */
|
|
|
|
res = (int) floor( ((m_parameter2 - m_parameter1 + 1) * m_base->DrawFloat())
|
|
|
|
+ m_parameter1);
|
|
|
|
tmpval = new CIntValue(res);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KX_RANDOMACT_INT_POISSON: {
|
|
|
|
/* poisson (queues) */
|
|
|
|
/* If x_1, x_2, ... is a sequence of random numbers with uniform */
|
|
|
|
/* distribution between zero and one, k is the first integer for */
|
|
|
|
/* which the product x_1*x_2*...*x_k < exp(-\lamba). */
|
2011-08-27 03:25:02 +00:00
|
|
|
float a, b;
|
2002-10-12 11:37:38 +00:00
|
|
|
int res = 0;
|
|
|
|
/* The - sign is important here! The number to test for, a, must be */
|
|
|
|
/* between 0 and 1. */
|
|
|
|
a = exp(-m_parameter1);
|
|
|
|
/* a quickly reaches 0.... so we guard explicitly for that. */
|
|
|
|
if (a < FLT_MIN) a = FLT_MIN;
|
|
|
|
b = m_base->DrawFloat();
|
|
|
|
while (b >= a) {
|
|
|
|
b = b * m_base->DrawFloat();
|
|
|
|
res++;
|
|
|
|
};
|
|
|
|
tmpval = new CIntValue(res);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KX_RANDOMACT_FLOAT_CONST: {
|
|
|
|
/* constant */
|
|
|
|
tmpval = new CFloatValue(m_parameter1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KX_RANDOMACT_FLOAT_UNIFORM: {
|
|
|
|
float res = ((m_parameter2 - m_parameter1) * m_base->DrawFloat())
|
|
|
|
+ m_parameter1;
|
|
|
|
tmpval = new CFloatValue(res);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KX_RANDOMACT_FLOAT_NORMAL: {
|
|
|
|
/* normal (big numbers): para1 = mean, para2 = std dev */
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
070301 - nzc - Changed the termination condition. I think I
|
|
|
|
made a small mistake here, but it only affects distro's where
|
|
|
|
the seed equals 0. In that case, the algorithm locks. Let's
|
|
|
|
just guard that case separately.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
float x = 0.0, y = 0.0, s = 0.0, t = 0.0;
|
|
|
|
if (m_base->GetSeed() == 0) {
|
|
|
|
/*
|
|
|
|
|
|
|
|
070301 - nzc
|
|
|
|
Just taking the mean here seems reasonable.
|
|
|
|
|
|
|
|
*/
|
|
|
|
tmpval = new CFloatValue(m_parameter1);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
|
|
|
|
070301 - nzc
|
|
|
|
Now, with seed != 0, we will most assuredly get some
|
|
|
|
sensible values. The termination condition states two
|
|
|
|
things:
|
|
|
|
1. s >= 0 is not allowed: to prevent the distro from
|
2011-09-03 02:15:49 +00:00
|
|
|
getting a bias towards high values. This is a small
|
2002-10-12 11:37:38 +00:00
|
|
|
correction, really, and might also be left out.
|
|
|
|
2. s == 0 is not allowed: to prevent a division by zero
|
2011-09-03 02:15:49 +00:00
|
|
|
when renormalising the drawn value to the desired
|
2002-10-12 11:37:38 +00:00
|
|
|
distribution shape. As a side effect, the distro will
|
|
|
|
never yield the exact mean.
|
|
|
|
I am not sure whether this is consistent, since the error
|
|
|
|
cause by #2 is of the same magnitude as the one
|
|
|
|
prevented by #1. The error introduced into the SD will be
|
|
|
|
improved, though. By how much? Hard to say... If you like
|
|
|
|
the maths, feel free to analyse. Be aware that this is
|
|
|
|
one of the really old standard algorithms. I think the
|
|
|
|
original came in Fortran, was translated to Pascal, and
|
|
|
|
then someone came up with the C code. My guess it that
|
|
|
|
this will be quite sufficient here.
|
|
|
|
|
|
|
|
*/
|
|
|
|
do
|
|
|
|
{
|
|
|
|
x = 2.0 * m_base->DrawFloat() - 1.0;
|
|
|
|
y = 2.0 * m_base->DrawFloat() - 1.0;
|
|
|
|
s = x*x + y*y;
|
|
|
|
} while ( (s >= 1.0) || (s == 0.0) );
|
|
|
|
t = x * sqrt( (-2.0 * log(s)) / s);
|
|
|
|
tmpval = new CFloatValue(m_parameter1 + m_parameter2 * t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL: {
|
|
|
|
/* 1st order fall-off. I am very partial to using the half-life as */
|
|
|
|
/* controlling parameter. Using the 'normal' exponent is not very */
|
|
|
|
/* intuitive... */
|
|
|
|
/* tmpval = new CFloatValue( (1.0 / m_parameter1) */
|
|
|
|
tmpval = new CFloatValue( (m_parameter1)
|
|
|
|
* (-log(1.0 - m_base->DrawFloat())) );
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2004-03-22 22:02:18 +00:00
|
|
|
{
|
|
|
|
/* unknown distribution... */
|
|
|
|
static bool randomWarning = false;
|
|
|
|
if (!randomWarning) {
|
|
|
|
randomWarning = true;
|
|
|
|
std::cout << "RandomActuator '" << GetName() << "' has an unknown distribution." << std::endl;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Round up: assign it */
|
|
|
|
CValue *prop = GetParent()->GetProperty(m_propname);
|
|
|
|
if (prop) {
|
|
|
|
prop->SetValue(tmpval);
|
|
|
|
}
|
|
|
|
tmpval->Release();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SCA_RandomActuator::enforceConstraints() {
|
|
|
|
/* The constraints that are checked here are the ones fundamental to */
|
|
|
|
/* the various distributions. Limitations of the algorithms are checked */
|
|
|
|
/* elsewhere (or they should be... ). */
|
|
|
|
switch (m_distribution) {
|
|
|
|
case KX_RANDOMACT_BOOL_CONST:
|
|
|
|
case KX_RANDOMACT_BOOL_UNIFORM:
|
|
|
|
case KX_RANDOMACT_INT_CONST:
|
|
|
|
case KX_RANDOMACT_INT_UNIFORM:
|
|
|
|
case KX_RANDOMACT_FLOAT_UNIFORM:
|
|
|
|
case KX_RANDOMACT_FLOAT_CONST:
|
|
|
|
; /* Nothing to be done here. We allow uniform distro's to have */
|
|
|
|
/* 'funny' domains, i.e. max < min. This does not give problems. */
|
|
|
|
break;
|
|
|
|
case KX_RANDOMACT_BOOL_BERNOUILLI:
|
|
|
|
/* clamp to [0, 1] */
|
|
|
|
if (m_parameter1 < 0.0) {
|
|
|
|
m_parameter1 = 0.0;
|
|
|
|
} else if (m_parameter1 > 1.0) {
|
|
|
|
m_parameter1 = 1.0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KX_RANDOMACT_INT_POISSON:
|
|
|
|
/* non-negative */
|
|
|
|
if (m_parameter1 < 0.0) {
|
|
|
|
m_parameter1 = 0.0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KX_RANDOMACT_FLOAT_NORMAL:
|
|
|
|
/* standard dev. is non-negative */
|
|
|
|
if (m_parameter2 < 0.0) {
|
|
|
|
m_parameter2 = 0.0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL:
|
|
|
|
/* halflife must be non-negative */
|
|
|
|
if (m_parameter1 < 0.0) {
|
|
|
|
m_parameter1 = 0.0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
; /* unknown distribution... */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-31 04:11:39 +00:00
|
|
|
#ifdef WITH_PYTHON
|
2009-09-29 21:42:40 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
/* Python functions */
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Integration hooks ------------------------------------------------------- */
|
|
|
|
PyTypeObject SCA_RandomActuator::Type = {
|
2009-06-08 20:08:19 +00:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2002-10-12 11:37:38 +00:00
|
|
|
"SCA_RandomActuator",
|
2009-04-20 15:06:46 +00:00
|
|
|
sizeof(PyObjectPlus_Proxy),
|
2002-10-12 11:37:38 +00:00
|
|
|
0,
|
2009-04-20 15:06:46 +00:00
|
|
|
py_base_dealloc,
|
2002-10-12 11:37:38 +00:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2009-04-20 15:06:46 +00:00
|
|
|
py_base_repr,
|
2009-06-29 12:06:46 +00:00
|
|
|
0,0,0,0,0,0,0,0,0,
|
2009-06-28 11:22:26 +00:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
|
|
|
0,0,0,0,0,0,0,
|
|
|
|
Methods,
|
|
|
|
0,
|
|
|
|
0,
|
2009-06-29 12:06:46 +00:00
|
|
|
&SCA_IActuator::Type,
|
|
|
|
0,0,0,0,0,0,
|
|
|
|
py_base_new
|
2002-10-12 11:37:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
PyMethodDef SCA_RandomActuator::Methods[] = {
|
2009-04-20 15:06:46 +00:00
|
|
|
KX_PYMETHODTABLE(SCA_RandomActuator, setBoolConst),
|
|
|
|
KX_PYMETHODTABLE_NOARGS(SCA_RandomActuator, setBoolUniform),
|
|
|
|
KX_PYMETHODTABLE(SCA_RandomActuator, setBoolBernouilli),
|
|
|
|
|
|
|
|
KX_PYMETHODTABLE(SCA_RandomActuator, setIntConst),
|
|
|
|
KX_PYMETHODTABLE(SCA_RandomActuator, setIntUniform),
|
|
|
|
KX_PYMETHODTABLE(SCA_RandomActuator, setIntPoisson),
|
|
|
|
|
|
|
|
KX_PYMETHODTABLE(SCA_RandomActuator, setFloatConst),
|
|
|
|
KX_PYMETHODTABLE(SCA_RandomActuator, setFloatUniform),
|
|
|
|
KX_PYMETHODTABLE(SCA_RandomActuator, setFloatNormal),
|
|
|
|
KX_PYMETHODTABLE(SCA_RandomActuator, setFloatNegativeExponential),
|
2002-10-12 11:37:38 +00:00
|
|
|
{NULL,NULL} //Sentinel
|
|
|
|
};
|
|
|
|
|
2009-01-02 17:43:56 +00:00
|
|
|
PyAttributeDef SCA_RandomActuator::Attributes[] = {
|
|
|
|
KX_PYATTRIBUTE_FLOAT_RO("para1",SCA_RandomActuator,m_parameter1),
|
|
|
|
KX_PYATTRIBUTE_FLOAT_RO("para2",SCA_RandomActuator,m_parameter2),
|
|
|
|
KX_PYATTRIBUTE_ENUM_RO("distribution",SCA_RandomActuator,m_distribution),
|
2009-06-08 20:08:19 +00:00
|
|
|
KX_PYATTRIBUTE_STRING_RW_CHECK("propName",0,100,false,SCA_RandomActuator,m_propname,CheckProperty),
|
2009-04-20 15:06:46 +00:00
|
|
|
KX_PYATTRIBUTE_RW_FUNCTION("seed",SCA_RandomActuator,pyattr_get_seed,pyattr_set_seed),
|
2009-01-02 17:43:56 +00:00
|
|
|
{ NULL } //Sentinel
|
|
|
|
};
|
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
PyObject* SCA_RandomActuator::pyattr_get_seed(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
|
|
|
|
{
|
|
|
|
SCA_RandomActuator* act = static_cast<SCA_RandomActuator*>(self);
|
2009-06-29 02:25:54 +00:00
|
|
|
return PyLong_FromSsize_t(act->m_base->GetSeed());
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
int SCA_RandomActuator::pyattr_set_seed(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
|
2008-12-29 16:36:58 +00:00
|
|
|
{
|
2009-04-20 15:06:46 +00:00
|
|
|
SCA_RandomActuator* act = static_cast<SCA_RandomActuator*>(self);
|
2009-06-29 02:25:54 +00:00
|
|
|
if (PyLong_Check(value)) {
|
|
|
|
int ival = PyLong_AsSsize_t(value);
|
2009-04-20 15:06:46 +00:00
|
|
|
act->m_base->SetSeed(ival);
|
2009-06-08 20:08:19 +00:00
|
|
|
return PY_SET_ATTR_SUCCESS;
|
2009-04-20 15:06:46 +00:00
|
|
|
} else {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "actuator.seed = int: Random Actuator, expected an integer");
|
2009-06-08 20:08:19 +00:00
|
|
|
return PY_SET_ATTR_FAIL;
|
2008-12-29 16:36:58 +00:00
|
|
|
}
|
2009-04-20 15:06:46 +00:00
|
|
|
}
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/* 11. setBoolConst */
|
2009-04-20 15:06:46 +00:00
|
|
|
KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setBoolConst,
|
2002-10-12 11:37:38 +00:00
|
|
|
"setBoolConst(value)\n"
|
|
|
|
"\t- value: 0 or 1\n"
|
2009-04-20 15:06:46 +00:00
|
|
|
"\tSet this generator to produce a constant boolean value.\n")
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
int paraArg;
|
2009-04-20 15:06:46 +00:00
|
|
|
if(!PyArg_ParseTuple(args, "i:setBoolConst", ¶Arg)) {
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_distribution = KX_RANDOMACT_BOOL_CONST;
|
2008-12-29 16:36:58 +00:00
|
|
|
m_parameter1 = (paraArg) ? 1.0 : 0.0;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2009-02-21 12:43:24 +00:00
|
|
|
Py_RETURN_NONE;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
/* 12. setBoolUniform, */
|
2009-04-20 15:06:46 +00:00
|
|
|
KX_PYMETHODDEF_DOC_NOARGS(SCA_RandomActuator, setBoolUniform,
|
2002-10-12 11:37:38 +00:00
|
|
|
"setBoolUniform()\n"
|
2009-04-20 15:06:46 +00:00
|
|
|
"\tSet this generator to produce true and false, each with 50%% chance of occuring\n")
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
/* no args */
|
|
|
|
m_distribution = KX_RANDOMACT_BOOL_UNIFORM;
|
|
|
|
enforceConstraints();
|
2009-02-21 12:43:24 +00:00
|
|
|
Py_RETURN_NONE;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
/* 13. setBoolBernouilli, */
|
2009-04-20 15:06:46 +00:00
|
|
|
KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setBoolBernouilli,
|
2002-10-12 11:37:38 +00:00
|
|
|
"setBoolBernouilli(value)\n"
|
|
|
|
"\t- value: a float between 0 and 1\n"
|
2009-04-20 15:06:46 +00:00
|
|
|
"\tReturn false value * 100%% of the time.\n")
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
float paraArg;
|
2009-04-20 15:06:46 +00:00
|
|
|
if(!PyArg_ParseTuple(args, "f:setBoolBernouilli", ¶Arg)) {
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-06-04 03:00:13 +00:00
|
|
|
m_distribution = KX_RANDOMACT_BOOL_BERNOUILLI;
|
2002-10-12 11:37:38 +00:00
|
|
|
m_parameter1 = paraArg;
|
|
|
|
enforceConstraints();
|
2009-02-21 12:43:24 +00:00
|
|
|
Py_RETURN_NONE;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
/* 14. setIntConst,*/
|
2009-04-20 15:06:46 +00:00
|
|
|
KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setIntConst,
|
2002-10-12 11:37:38 +00:00
|
|
|
"setIntConst(value)\n"
|
|
|
|
"\t- value: integer\n"
|
2009-04-20 15:06:46 +00:00
|
|
|
"\tAlways return value\n")
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
int paraArg;
|
2009-04-20 15:06:46 +00:00
|
|
|
if(!PyArg_ParseTuple(args, "i:setIntConst", ¶Arg)) {
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_distribution = KX_RANDOMACT_INT_CONST;
|
|
|
|
m_parameter1 = paraArg;
|
|
|
|
enforceConstraints();
|
2009-02-21 12:43:24 +00:00
|
|
|
Py_RETURN_NONE;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
/* 15. setIntUniform,*/
|
2009-04-20 15:06:46 +00:00
|
|
|
KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setIntUniform,
|
2002-10-12 11:37:38 +00:00
|
|
|
"setIntUniform(lower_bound, upper_bound)\n"
|
|
|
|
"\t- lower_bound: integer\n"
|
|
|
|
"\t- upper_bound: integer\n"
|
|
|
|
"\tReturn a random integer between lower_bound and\n"
|
2009-04-20 15:06:46 +00:00
|
|
|
"\tupper_bound. The boundaries are included.\n")
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
int paraArg1, paraArg2;
|
2009-04-20 15:06:46 +00:00
|
|
|
if(!PyArg_ParseTuple(args, "ii:setIntUniform", ¶Arg1, ¶Arg2)) {
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_distribution = KX_RANDOMACT_INT_UNIFORM;
|
|
|
|
m_parameter1 = paraArg1;
|
|
|
|
m_parameter2 = paraArg2;
|
|
|
|
enforceConstraints();
|
2009-02-21 12:43:24 +00:00
|
|
|
Py_RETURN_NONE;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
/* 16. setIntPoisson, */
|
2009-04-20 15:06:46 +00:00
|
|
|
KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setIntPoisson,
|
2002-10-12 11:37:38 +00:00
|
|
|
"setIntPoisson(value)\n"
|
|
|
|
"\t- value: float\n"
|
|
|
|
"\tReturn a Poisson-distributed number. This performs a series\n"
|
|
|
|
"\tof Bernouilli tests with parameter value. It returns the\n"
|
2009-04-20 15:06:46 +00:00
|
|
|
"\tnumber of tries needed to achieve succes.\n")
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
float paraArg;
|
2009-04-20 15:06:46 +00:00
|
|
|
if(!PyArg_ParseTuple(args, "f:setIntPoisson", ¶Arg)) {
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_distribution = KX_RANDOMACT_INT_POISSON;
|
|
|
|
m_parameter1 = paraArg;
|
|
|
|
enforceConstraints();
|
2009-02-21 12:43:24 +00:00
|
|
|
Py_RETURN_NONE;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2009-04-20 15:06:46 +00:00
|
|
|
/* 17. setFloatConst */
|
|
|
|
KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setFloatConst,
|
2002-10-12 11:37:38 +00:00
|
|
|
"setFloatConst(value)\n"
|
|
|
|
"\t- value: float\n"
|
2009-04-20 15:06:46 +00:00
|
|
|
"\tAlways return value\n")
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
float paraArg;
|
2009-04-20 15:06:46 +00:00
|
|
|
if(!PyArg_ParseTuple(args, "f:setFloatConst", ¶Arg)) {
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_distribution = KX_RANDOMACT_FLOAT_CONST;
|
|
|
|
m_parameter1 = paraArg;
|
|
|
|
enforceConstraints();
|
2009-02-21 12:43:24 +00:00
|
|
|
Py_RETURN_NONE;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
/* 18. setFloatUniform, */
|
2009-04-20 15:06:46 +00:00
|
|
|
KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setFloatUniform,
|
2002-10-12 11:37:38 +00:00
|
|
|
"setFloatUniform(lower_bound, upper_bound)\n"
|
|
|
|
"\t- lower_bound: float\n"
|
|
|
|
"\t- upper_bound: float\n"
|
|
|
|
"\tReturn a random integer between lower_bound and\n"
|
2009-04-20 15:06:46 +00:00
|
|
|
"\tupper_bound.\n")
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
float paraArg1, paraArg2;
|
2009-04-20 15:06:46 +00:00
|
|
|
if(!PyArg_ParseTuple(args, "ff:setFloatUniform", ¶Arg1, ¶Arg2)) {
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_distribution = KX_RANDOMACT_FLOAT_UNIFORM;
|
|
|
|
m_parameter1 = paraArg1;
|
|
|
|
m_parameter2 = paraArg2;
|
|
|
|
enforceConstraints();
|
2009-02-21 12:43:24 +00:00
|
|
|
Py_RETURN_NONE;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
/* 19. setFloatNormal, */
|
2009-04-20 15:06:46 +00:00
|
|
|
KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setFloatNormal,
|
2002-10-12 11:37:38 +00:00
|
|
|
"setFloatNormal(mean, standard_deviation)\n"
|
|
|
|
"\t- mean: float\n"
|
|
|
|
"\t- standard_deviation: float\n"
|
|
|
|
"\tReturn normal-distributed numbers. The average is mean, and the\n"
|
2009-04-20 15:06:46 +00:00
|
|
|
"\tdeviation from the mean is characterized by standard_deviation.\n")
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
float paraArg1, paraArg2;
|
2009-04-20 15:06:46 +00:00
|
|
|
if(!PyArg_ParseTuple(args, "ff:setFloatNormal", ¶Arg1, ¶Arg2)) {
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_distribution = KX_RANDOMACT_FLOAT_NORMAL;
|
|
|
|
m_parameter1 = paraArg1;
|
|
|
|
m_parameter2 = paraArg2;
|
|
|
|
enforceConstraints();
|
2009-02-21 12:43:24 +00:00
|
|
|
Py_RETURN_NONE;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
/* 20. setFloatNegativeExponential, */
|
2009-04-20 15:06:46 +00:00
|
|
|
KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setFloatNegativeExponential,
|
2002-10-12 11:37:38 +00:00
|
|
|
"setFloatNegativeExponential(half_life)\n"
|
|
|
|
"\t- half_life: float\n"
|
|
|
|
"\tReturn negative-exponentially distributed numbers. The half-life 'time'\n"
|
2009-04-20 15:06:46 +00:00
|
|
|
"\tis characterized by half_life.\n")
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
float paraArg;
|
2009-04-20 15:06:46 +00:00
|
|
|
if(!PyArg_ParseTuple(args, "f:setFloatNegativeExponential", ¶Arg)) {
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_distribution = KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL;
|
|
|
|
m_parameter1 = paraArg;
|
|
|
|
enforceConstraints();
|
2009-02-21 12:43:24 +00:00
|
|
|
Py_RETURN_NONE;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2009-09-29 21:42:40 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/* eof */
|