forked from bartvdbraak/blender
386122ada6
This commit extends the technique of dynamic linked list to the logic system to eliminate as much as possible temporaries, map lookup or full scan. The logic engine is now free of memory allocation, which is an important stability factor. The overhead of the logic system is reduced by a factor between 3 and 6 depending on the logic setup. This is the speed-up you can expect on a logic setup using simple bricks. Heavy bricks like python controllers and ray sensors will still take about the same time to execute so the speed up will be less important. The core of the logic engine has been much reworked but the functionality is still the same except for one thing: the priority system on the execution of controllers. The exact same remark applies to actuators but I'll explain for controllers only: Previously, it was possible, with the "executePriority" attribute to set a controller to run before any other controllers in the game. Other than that, the sequential execution of controllers, as defined in Blender was guaranteed by default. With the new system, the sequential execution of controllers is still guaranteed but only within the controllers of one object. the user can no longer set a controller to run before any other controllers in the game. The "executePriority" attribute controls the execution of controllers within one object. The priority is a small number starting from 0 for the first controller and incrementing for each controller. If this missing feature is a must, a special method can be implemented to set a controller to run before all other controllers. Other improvements: - Systematic use of reference in parameter passing to avoid unnecessary data copy - Use pre increment in iterator instead of post increment to avoid temporary allocation - Use const char* instead of STR_String whenever possible to avoid temporary allocation - Fix reference counting bugs (memory leak) - Fix a crash in certain cases of state switching and object deletion - Minor speed up in property sensor - Removal of objects during the game is a lot faster
246 lines
7.1 KiB
C++
246 lines
7.1 KiB
C++
/**
|
|
* Generate random pulses
|
|
*
|
|
* $Id$
|
|
*
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
*
|
|
* 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
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* 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,
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
*
|
|
* 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.
|
|
*
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
*/
|
|
|
|
#include "SCA_RandomSensor.h"
|
|
#include "SCA_EventManager.h"
|
|
#include "SCA_RandomEventManager.h"
|
|
#include "SCA_LogicManager.h"
|
|
#include "ConstExpr.h"
|
|
#include <iostream>
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include <config.h>
|
|
#endif
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
/* Native functions */
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
SCA_RandomSensor::SCA_RandomSensor(SCA_EventManager* eventmgr,
|
|
SCA_IObject* gameobj,
|
|
int startseed,
|
|
PyTypeObject* T)
|
|
: SCA_ISensor(gameobj,eventmgr, T)
|
|
{
|
|
// m_basegenerator is never deleted => memory leak
|
|
m_basegenerator = new SCA_RandomNumberGenerator(startseed);
|
|
Init();
|
|
}
|
|
|
|
|
|
|
|
SCA_RandomSensor::~SCA_RandomSensor()
|
|
{
|
|
/* Nothing to be done here. */
|
|
}
|
|
|
|
void SCA_RandomSensor::Init()
|
|
{
|
|
m_iteration = 0;
|
|
m_interval = 0;
|
|
m_lastdraw = false;
|
|
m_currentDraw = m_basegenerator->Draw();
|
|
}
|
|
|
|
|
|
CValue* SCA_RandomSensor::GetReplica()
|
|
{
|
|
CValue* replica = new SCA_RandomSensor(*this);
|
|
// replication copies m_basegenerator pointer => share same generator
|
|
// this will copy properties and so on...
|
|
replica->ProcessReplica();
|
|
|
|
return replica;
|
|
}
|
|
|
|
|
|
|
|
bool SCA_RandomSensor::IsPositiveTrigger()
|
|
{
|
|
return (m_invert !=m_lastdraw);
|
|
}
|
|
|
|
|
|
bool SCA_RandomSensor::Evaluate()
|
|
{
|
|
/* Random generator is the generator from Line 25 of Table 1 in */
|
|
/* [KNUTH 1981, The Art of Computer Programming Vol. 2 */
|
|
/* (2nd Ed.), pp102] */
|
|
/* It's a very simple max. length sequence generator. We can */
|
|
/* draw 32 bool values before having to generate the next */
|
|
/* sequence value. There are some theorems that will tell you */
|
|
/* this is a reasonable way of generating bools. Check Knuth. */
|
|
/* Furthermore, we only draw each <delay>-eth frame. */
|
|
|
|
bool evaluateResult = false;
|
|
|
|
if (++m_interval > m_pulse_frequency) {
|
|
bool drawResult = false;
|
|
m_interval = 0;
|
|
if (m_iteration > 31) {
|
|
m_currentDraw = m_basegenerator->Draw();
|
|
drawResult = (m_currentDraw & 0x1) == 0;
|
|
m_iteration = 1;
|
|
} else {
|
|
drawResult = ((m_currentDraw >> m_iteration) & 0x1) == 0;
|
|
m_iteration++;
|
|
}
|
|
evaluateResult = drawResult != m_lastdraw;
|
|
m_lastdraw = drawResult;
|
|
}
|
|
|
|
/* now pass this result to some controller */
|
|
return evaluateResult;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
/* Python functions */
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
/* Integration hooks ------------------------------------------------------- */
|
|
PyTypeObject SCA_RandomSensor::Type = {
|
|
#if (PY_VERSION_HEX >= 0x02060000)
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
#else
|
|
/* python 2.5 and below */
|
|
PyObject_HEAD_INIT( NULL ) /* required py macro */
|
|
0, /* ob_size */
|
|
#endif
|
|
"SCA_RandomSensor",
|
|
sizeof(PyObjectPlus_Proxy),
|
|
0,
|
|
py_base_dealloc,
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
py_base_repr,
|
|
0,0,0,0,0,0,
|
|
py_base_getattro,
|
|
py_base_setattro,
|
|
0,0,0,0,0,0,0,0,0,
|
|
Methods
|
|
};
|
|
|
|
PyParentObject SCA_RandomSensor::Parents[] = {
|
|
&SCA_RandomSensor::Type,
|
|
&SCA_ISensor::Type,
|
|
&SCA_ILogicBrick::Type,
|
|
&CValue::Type,
|
|
NULL
|
|
};
|
|
|
|
PyMethodDef SCA_RandomSensor::Methods[] = {
|
|
//Deprecated functions ----->
|
|
{"setSeed", (PyCFunction) SCA_RandomSensor::sPySetSeed, METH_VARARGS, (PY_METHODCHAR)SetSeed_doc},
|
|
{"getSeed", (PyCFunction) SCA_RandomSensor::sPyGetSeed, METH_NOARGS, (PY_METHODCHAR)GetSeed_doc},
|
|
{"getLastDraw", (PyCFunction) SCA_RandomSensor::sPyGetLastDraw, METH_NOARGS, (PY_METHODCHAR)GetLastDraw_doc},
|
|
//<----- Deprecated
|
|
{NULL,NULL} //Sentinel
|
|
};
|
|
|
|
PyAttributeDef SCA_RandomSensor::Attributes[] = {
|
|
KX_PYATTRIBUTE_BOOL_RO("lastDraw",SCA_RandomSensor,m_lastdraw),
|
|
KX_PYATTRIBUTE_RW_FUNCTION("seed", SCA_RandomSensor, pyattr_get_seed, pyattr_set_seed),
|
|
{NULL} //Sentinel
|
|
};
|
|
|
|
PyObject* SCA_RandomSensor::py_getattro(PyObject *attr) {
|
|
py_getattro_up(SCA_ISensor);
|
|
}
|
|
|
|
PyObject* SCA_RandomSensor::py_getattro_dict() {
|
|
py_getattro_dict_up(SCA_ISensor);
|
|
}
|
|
|
|
int SCA_RandomSensor::py_setattro(PyObject *attr, PyObject *value)
|
|
{
|
|
py_setattro_up(SCA_ISensor);
|
|
}
|
|
|
|
/* 1. setSeed */
|
|
const char SCA_RandomSensor::SetSeed_doc[] =
|
|
"setSeed(seed)\n"
|
|
"\t- seed: integer\n"
|
|
"\tSet the initial seed of the generator. Equal seeds produce\n"
|
|
"\tequal series. If the seed is 0, the generator will produce\n"
|
|
"\tthe same value on every call.\n";
|
|
PyObject* SCA_RandomSensor::PySetSeed(PyObject* args) {
|
|
ShowDeprecationWarning("setSeed()", "the seed property");
|
|
long seedArg;
|
|
if(!PyArg_ParseTuple(args, "i:setSeed", &seedArg)) {
|
|
return NULL;
|
|
}
|
|
|
|
m_basegenerator->SetSeed(seedArg);
|
|
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
/* 2. getSeed */
|
|
const char SCA_RandomSensor::GetSeed_doc[] =
|
|
"getSeed()\n"
|
|
"\tReturns the initial seed of the generator. Equal seeds produce\n"
|
|
"\tequal series.\n";
|
|
PyObject* SCA_RandomSensor::PyGetSeed() {
|
|
ShowDeprecationWarning("getSeed()", "the seed property");
|
|
return PyInt_FromLong(m_basegenerator->GetSeed());
|
|
}
|
|
|
|
/* 3. getLastDraw */
|
|
const char SCA_RandomSensor::GetLastDraw_doc[] =
|
|
"getLastDraw()\n"
|
|
"\tReturn the last value that was drawn.\n";
|
|
PyObject* SCA_RandomSensor::PyGetLastDraw() {
|
|
ShowDeprecationWarning("getLastDraw()", "the lastDraw property");
|
|
return PyInt_FromLong(m_lastdraw);
|
|
}
|
|
|
|
|
|
PyObject* SCA_RandomSensor::pyattr_get_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
|
|
{
|
|
SCA_RandomSensor* self= static_cast<SCA_RandomSensor*>(self_v);
|
|
return PyInt_FromLong(self->m_basegenerator->GetSeed());
|
|
}
|
|
|
|
int SCA_RandomSensor::pyattr_set_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
|
|
{
|
|
SCA_RandomSensor* self= static_cast<SCA_RandomSensor*>(self_v);
|
|
if (!PyInt_Check(value)) {
|
|
PyErr_SetString(PyExc_TypeError, "sensor.seed = int: Random Sensor, expected an integer");
|
|
return -1;
|
|
}
|
|
self->m_basegenerator->SetSeed(PyInt_AsLong(value));
|
|
return 0;
|
|
}
|
|
|
|
/* eof */
|