diff options
author | Dalai Felinto <dfelinto@gmail.com> | 2009-05-11 22:26:47 +0400 |
---|---|---|
committer | Dalai Felinto <dfelinto@gmail.com> | 2009-05-11 22:26:47 +0400 |
commit | b8ba7c2be521a19ca952f4033d39f8323f494abe (patch) | |
tree | 6c3fe995ef593667a6b2d202c72bc7a0220605e0 /source/gameengine/GameLogic | |
parent | c3fbb17e030c77cf94d1646dabd0e5af4150a02b (diff) | |
parent | 0c6ec76a4c7de61e84cc4dddbbda1698b7bf2a4b (diff) |
merge -r 19528-HEAD (20156)
starting dome test period again :) (i.e. not over committing in the trunk)
Diffstat (limited to 'source/gameengine/GameLogic')
70 files changed, 2066 insertions, 1699 deletions
diff --git a/source/gameengine/GameLogic/CMakeLists.txt b/source/gameengine/GameLogic/CMakeLists.txt index a7519196d50..449aae3ac84 100644 --- a/source/gameengine/GameLogic/CMakeLists.txt +++ b/source/gameengine/GameLogic/CMakeLists.txt @@ -31,6 +31,7 @@ SET(INC ../../../source/kernel/gen_system ../../../intern/string ../../../source/gameengine/Expressions + ../../../source/gameengine/Scenegraph ../../../intern/moto/include ../../../source/gameengine/Rasterizer ${PYTHON_INC} diff --git a/source/gameengine/GameLogic/Joystick/Makefile b/source/gameengine/GameLogic/Joystick/Makefile index 7016f1ed16f..02def1cec62 100644 --- a/source/gameengine/GameLogic/Joystick/Makefile +++ b/source/gameengine/GameLogic/Joystick/Makefile @@ -40,5 +40,5 @@ CPPFLAGS += -I$(NAN_STRING)/include CPPFLAGS += -I$(NAN_MOTO)/include CPPFLAGS += -I$(NAN_PYTHON)/include/python$(NAN_PYTHON_VERSION) CPPFLAGS += $(NAN_SDLCFLAGS) - +CPPFLAGS += -I../../SceneGraph CPPFLAGS += -I../../../kernel/gen_system diff --git a/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp b/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp index c21e5db1410..c300baa9bd4 100644 --- a/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp +++ b/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp @@ -29,6 +29,7 @@ #endif #include <stdio.h> +#include <stdlib.h> #include "SCA_Joystick.h" #include "SCA_JoystickPrivate.h" @@ -36,21 +37,19 @@ SCA_Joystick::SCA_Joystick(short int index) : m_joyindex(index), - m_axis10(0), - m_axis11(0), - m_axis20(0), - m_axis21(0), m_prec(3200), m_buttonnum(-2), m_axismax(-1), - m_hatdir(-2), m_buttonmax(-1), m_hatmax(-1), + m_hatdir(-2), m_isinit(0), m_istrig_axis(0), m_istrig_button(0), m_istrig_hat(0) { + for(int i=0; i<JOYAXIS_MAX; i++) + m_axis_array[i]= 0; #ifndef DISABLE_SDL m_private = new PrivateData(); #endif @@ -125,47 +124,30 @@ void SCA_Joystick::cSetPrecision(int val) } -bool SCA_Joystick::aAnyAxisIsPositive(int axis) +bool SCA_Joystick::aAxisPairIsPositive(int axis) { - bool result; - int res = pAxisTest(axis); - res > m_prec? result = true: result = false; - return result; + return (pAxisTest(axis) > m_prec) ? true:false; } -bool SCA_Joystick::aRightAxisIsPositive(int axis) +bool SCA_Joystick::aAxisPairDirectionIsPositive(int axis, int dir) { - bool result; - int res = pGetAxis(axis,1); - res > m_prec? result = true: result = false; - return result; -} - -bool SCA_Joystick::aUpAxisIsPositive(int axis) -{ - bool result; - int res = pGetAxis(axis,0); - res < -m_prec? result = true : result = false; - return result; + int res; + + if (dir==JOYAXIS_UP || dir==JOYAXIS_DOWN) + res = pGetAxis(axis, 1); + else /* JOYAXIS_LEFT || JOYAXIS_RIGHT */ + res = pGetAxis(axis, 0); + + if (dir==JOYAXIS_DOWN || dir==JOYAXIS_RIGHT) + return (res > m_prec) ? true : false; + else /* JOYAXIS_UP || JOYAXIS_LEFT */ + return (res < -m_prec) ? true : false; } - -bool SCA_Joystick::aLeftAxisIsPositive(int axis) +bool SCA_Joystick::aAxisIsPositive(int axis_single) { - bool result; - int res = pGetAxis(axis,1); - res < -m_prec ? result = true : result = false; - return result; -} - - -bool SCA_Joystick::aDownAxisIsPositive(int axis) -{ - bool result; - int res = pGetAxis(axis,0); - res > m_prec ? result = true:result = false; - return result; + return abs(m_axis_array[axis_single]) > m_prec ? true:false; } bool SCA_Joystick::aAnyButtonPressIsPositive(void) @@ -255,8 +237,12 @@ bool SCA_Joystick::CreateJoystickDevice(void) /* must run after being initialized */ m_axismax = SDL_JoystickNumAxes(m_private->m_joystick); + if (m_axismax > JOYAXIS_MAX) m_axismax= JOYAXIS_MAX; /* very unlikely */ + m_buttonmax = SDL_JoystickNumButtons(m_private->m_joystick); m_hatmax = SDL_JoystickNumHats(m_private->m_joystick); + + } return true; #endif @@ -288,17 +274,8 @@ int SCA_Joystick::Connected(void) void SCA_Joystick::pFillAxes() { #ifndef DISABLE_SDL - if(m_axismax == 1){ - m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0); - m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1); - }else if(m_axismax > 1){ - m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0); - m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1); - m_axis20 = SDL_JoystickGetAxis(m_private->m_joystick, 2); - m_axis21 = SDL_JoystickGetAxis(m_private->m_joystick, 3); - }else{ - m_axis10 = m_axis11 = m_axis20 = m_axis21 = 0; - } + for(int i=0; i<m_axismax; i++) + m_axis_array[i]= SDL_JoystickGetAxis(m_private->m_joystick, i); #endif } @@ -306,10 +283,7 @@ void SCA_Joystick::pFillAxes() int SCA_Joystick::pGetAxis(int axisnum, int udlr) { #ifndef DISABLE_SDL - if(axisnum == 1 && udlr == 1)return m_axis10; //u/d - if(axisnum == 1 && udlr == 0)return m_axis11; //l/r - if(axisnum == 2 && udlr == 0)return m_axis20; //... - if(axisnum == 2 && udlr == 1)return m_axis21; + return m_axis_array[(axisnum*2)+udlr]; #endif return 0; } @@ -317,13 +291,9 @@ int SCA_Joystick::pGetAxis(int axisnum, int udlr) int SCA_Joystick::pAxisTest(int axisnum) { #ifndef DISABLE_SDL - short i1,i2; - if(axisnum == 1) { - i1 = m_axis10; i2 = m_axis11; - } - else if(axisnum == 2) { - i1 = m_axis20; i2 = m_axis21; - } + short i1= m_axis_array[(axisnum*2)]; + short i2= m_axis_array[(axisnum*2)+1]; + /* long winded way to do * return MAX2(abs(i1), abs(i2)) * avoid abs from math.h */ @@ -335,4 +305,3 @@ int SCA_Joystick::pAxisTest(int axisnum) return 0; #endif } - diff --git a/source/gameengine/GameLogic/Joystick/SCA_Joystick.h b/source/gameengine/GameLogic/Joystick/SCA_Joystick.h index 8335d5538ad..53cd65cd495 100644 --- a/source/gameengine/GameLogic/Joystick/SCA_Joystick.h +++ b/source/gameengine/GameLogic/Joystick/SCA_Joystick.h @@ -55,10 +55,8 @@ class SCA_Joystick /* *support for 2 axes */ - - int m_axis10,m_axis11; - int m_axis20,m_axis21; - + int m_axis_array[JOYAXIS_MAX]; + /* * Precision or range of the axes */ @@ -120,8 +118,11 @@ class SCA_Joystick void OnButtonUp(SDL_Event *sdl_event); void OnButtonDown(SDL_Event *sdl_event); void OnNothing(SDL_Event *sdl_event); +#if 0 /* not used yet */ void OnBallMotion(SDL_Event *sdl_event){} #endif + +#endif /* * Open the joystick */ @@ -139,12 +140,12 @@ class SCA_Joystick void pFillButtons(void); /* - * returns m_axis10,m_axis11... + * returns m_axis_array */ int pAxisTest(int axisnum); /* - * returns m_axis10,m_axis11... + * returns m_axis_array */ int pGetAxis(int axisnum, int udlr); @@ -166,11 +167,9 @@ public: /* */ - bool aAnyAxisIsPositive(int axis); - bool aUpAxisIsPositive(int axis); - bool aDownAxisIsPositive(int axis); - bool aLeftAxisIsPositive(int axis); - bool aRightAxisIsPositive(int axis); + bool aAxisPairIsPositive(int axis); + bool aAxisPairDirectionIsPositive(int axis, int dir); /* function assumes joysticks are in axis pairs */ + bool aAxisIsPositive(int axis_single); /* check a single axis only */ bool aAnyButtonPressIsPositive(void); bool aAnyButtonReleaseIsPositive(void); @@ -184,24 +183,10 @@ public: void cSetPrecision(int val); - int GetAxis10(void){ - - return m_axis10; - - } - - int GetAxis11(void){ - return m_axis11; - } - - int GetAxis20(void){ - return m_axis20; + int GetAxisPosition(int index){ + return m_axis_array[index]; } - - int GetAxis21(void){ - return m_axis21; - } - + int GetButton(void){ return m_buttonnum; } diff --git a/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h b/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h index 73ffe1406d9..636c4dd5a42 100644 --- a/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h +++ b/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h @@ -39,5 +39,11 @@ #endif #define JOYINDEX_MAX 8 +#define JOYAXIS_MAX 16 + +#define JOYAXIS_RIGHT 0 +#define JOYAXIS_UP 1 +#define JOYAXIS_DOWN 3 +#define JOYAXIS_LEFT 2 #endif diff --git a/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp b/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp index 73ca288861d..8e190060e95 100644 --- a/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp +++ b/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp @@ -68,7 +68,7 @@ void SCA_Joystick::OnButtonUp(SDL_Event* sdl_event) void SCA_Joystick::OnButtonDown(SDL_Event* sdl_event) { - if(sdl_event->jbutton.button >= 0 || sdl_event->jbutton.button <= m_buttonmax) + if(sdl_event->jbutton.button <= m_buttonmax) /* unsigned int so always above 0 */ { m_istrig_button = 1; m_buttonnum = sdl_event->jbutton.button; @@ -111,9 +111,11 @@ void SCA_Joystick::HandleEvents(void) case SDL_JOYBUTTONDOWN: SCA_Joystick::m_instance[sdl_event.jbutton.which]->OnButtonDown(&sdl_event); break; +#if 0 /* Not used yet */ case SDL_JOYBALLMOTION: SCA_Joystick::m_instance[sdl_event.jball.which]->OnBallMotion(&sdl_event); break; +#endif default: printf("SCA_Joystick::HandleEvents, Unknown SDL event, this should not happen\n"); break; diff --git a/source/gameengine/GameLogic/Makefile b/source/gameengine/GameLogic/Makefile index 355ece6e8bd..a1794a60452 100644 --- a/source/gameengine/GameLogic/Makefile +++ b/source/gameengine/GameLogic/Makefile @@ -39,6 +39,7 @@ include nan_compile.mk CCFLAGS += $(LEVEL_1_CPP_WARNINGS) CPPFLAGS += -I../Expressions +CPPFLAGS += -I../SceneGraph CPPFLAGS += -I../Rasterizer CPPFLAGS += -I$(NAN_STRING)/include CPPFLAGS += -I$(NAN_MOTO)/include diff --git a/source/gameengine/GameLogic/SCA_2DFilterActuator.cpp b/source/gameengine/GameLogic/SCA_2DFilterActuator.cpp index 9d4dc1f33d6..7682f6755ef 100644 --- a/source/gameengine/GameLogic/SCA_2DFilterActuator.cpp +++ b/source/gameengine/GameLogic/SCA_2DFilterActuator.cpp @@ -1,5 +1,29 @@ -#include "SCA_IActuator.h" +/** + * SCA_2DFilterActuator.cpp + * + * $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. + * + * + * ***** END GPL LICENSE BLOCK ***** + */ +#include "SCA_IActuator.h" #include "SCA_2DFilterActuator.h" #ifdef HAVE_CONFIG_H @@ -22,7 +46,7 @@ SCA_2DFilterActuator::SCA_2DFilterActuator( PyTypeObject* T) : SCA_IActuator(gameobj, T), m_type(type), - m_flag(flag), + m_disableMotionBlur(flag), m_float_arg(float_arg), m_int_arg(int_arg), m_rasterizer(rasterizer), @@ -35,19 +59,11 @@ SCA_2DFilterActuator::SCA_2DFilterActuator( } } -void SCA_2DFilterActuator::SetShaderText(STR_String text) -{ - m_shaderText = text; -} - - CValue* SCA_2DFilterActuator::GetReplica() { SCA_2DFilterActuator* replica = new SCA_2DFilterActuator(*this); replica->ProcessReplica(); - CValue::AddDataToReplica(replica); - return replica; } @@ -63,7 +79,7 @@ bool SCA_2DFilterActuator::Update() if( m_type == RAS_2DFilterManager::RAS_2DFILTER_MOTIONBLUR ) { - if(!m_flag) + if(!m_disableMotionBlur) m_rasterizer->EnableMotionBlur(m_float_arg); else m_rasterizer->DisableMotionBlur(); @@ -79,13 +95,30 @@ bool SCA_2DFilterActuator::Update() } +void SCA_2DFilterActuator::SetShaderText(const char *text) +{ + m_shaderText = text; +} + +/* ------------------------------------------------------------------------- */ +/* Python functions */ +/* ------------------------------------------------------------------------- */ + + + +/* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_2DFilterActuator::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_2DFilterActuator", - sizeof(SCA_2DFilterActuator), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -109,14 +142,30 @@ PyParentObject SCA_2DFilterActuator::Parents[] = { PyMethodDef SCA_2DFilterActuator::Methods[] = { - /* add python functions to deal with m_msg... */ + /* add python functions to deal with m_msg... */ {NULL,NULL} }; PyAttributeDef SCA_2DFilterActuator::Attributes[] = { + KX_PYATTRIBUTE_STRING_RW("shaderText", 0, 64000, false, SCA_2DFilterActuator, m_shaderText), + KX_PYATTRIBUTE_SHORT_RW("disableMotionBlur", 0, 1, true, SCA_2DFilterActuator, m_disableMotionBlur), + KX_PYATTRIBUTE_ENUM_RW("type",RAS_2DFilterManager::RAS_2DFILTER_ENABLED,RAS_2DFilterManager::RAS_2DFILTER_NUMBER_OF_FILTERS,false,SCA_2DFilterActuator,m_type), + KX_PYATTRIBUTE_INT_RW("passNb", 0, 100, true, SCA_2DFilterActuator, m_int_arg), + KX_PYATTRIBUTE_FLOAT_RW("value", 0.0, 100.0, SCA_2DFilterActuator, m_float_arg), { NULL } //Sentinel }; -PyObject* SCA_2DFilterActuator::py_getattro(PyObject *attr) { +PyObject* SCA_2DFilterActuator::py_getattro(PyObject *attr) +{ py_getattro_up(SCA_IActuator); } + +PyObject* SCA_2DFilterActuator::py_getattro_dict() { + py_getattro_dict_up(SCA_IActuator); +} + +int SCA_2DFilterActuator::py_setattro(PyObject *attr, PyObject* value) +{ + py_setattro_up(SCA_IActuator); +} + diff --git a/source/gameengine/GameLogic/SCA_2DFilterActuator.h b/source/gameengine/GameLogic/SCA_2DFilterActuator.h index f69c680b774..13b9997a010 100644 --- a/source/gameengine/GameLogic/SCA_2DFilterActuator.h +++ b/source/gameengine/GameLogic/SCA_2DFilterActuator.h @@ -1,3 +1,30 @@ +/** + * SCA_2DFilterActuator.h + * + * $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. + * + * ***** END GPL LICENSE BLOCK ***** + */ + #ifndef __SCA_2DFILETRACTUATOR_H__ #define __SCA_2DFILETRACTUATOR_H__ @@ -13,7 +40,7 @@ private: vector<STR_String> m_propNames; void * m_gameObj; RAS_2DFilterManager::RAS_2DFILTER_MODE m_type; - short m_flag; + short m_disableMotionBlur; float m_float_arg; int m_int_arg; STR_String m_shaderText; @@ -33,12 +60,19 @@ public: PyTypeObject* T=&Type ); - void SetShaderText(STR_String text); + void SetShaderText(const char *text); virtual ~SCA_2DFilterActuator(); virtual bool Update(); virtual CValue* GetReplica(); + + /* --------------------------------------------------------------------- */ + /* Python interface ---------------------------------------------------- */ + /* --------------------------------------------------------------------- */ + virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); + virtual int py_setattro(PyObject *attr, PyObject* value); }; #endif diff --git a/source/gameengine/GameLogic/SCA_ANDController.cpp b/source/gameengine/GameLogic/SCA_ANDController.cpp index cb62e2b5a1d..87f7c612e7c 100644 --- a/source/gameengine/GameLogic/SCA_ANDController.cpp +++ b/source/gameengine/GameLogic/SCA_ANDController.cpp @@ -66,26 +66,19 @@ void SCA_ANDController::Trigger(SCA_LogicManager* logicmgr) !(is==m_linkedsensors.end());is++) { SCA_ISensor* sensor = *is; - if (!sensor->IsPositiveTrigger()) + if (!sensor->GetState()) { sensorresult = false; break; } } - CValue* newevent = new CBoolValue(sensorresult); - for (vector<SCA_IActuator*>::const_iterator i=m_linkedactuators.begin(); !(i==m_linkedactuators.end());i++) { - SCA_IActuator* actua = *i;//m_linkedactuators.at(i); - logicmgr->AddActiveActuator(actua,newevent); + SCA_IActuator* actua = *i; + logicmgr->AddActiveActuator(actua,sensorresult); } - - // every actuator that needs the event, has a it's own reference to it now so - // release it (so to be clear: if there is no actuator, it's deleted right now) - newevent->Release(); - } @@ -94,7 +87,7 @@ CValue* SCA_ANDController::GetReplica() { CValue* replica = new SCA_ANDController(*this); // this will copy properties and so on... - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); return replica; } @@ -107,12 +100,17 @@ CValue* SCA_ANDController::GetReplica() /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_ANDController::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_ANDController", - sizeof(SCA_ANDController), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -145,4 +143,8 @@ PyObject* SCA_ANDController::py_getattro(PyObject *attr) { py_getattro_up(SCA_IController); } +PyObject* SCA_ANDController::py_getattro_dict() { + py_getattro_dict_up(SCA_IController); +} + /* eof */ diff --git a/source/gameengine/GameLogic/SCA_ANDController.h b/source/gameengine/GameLogic/SCA_ANDController.h index fdb93d0fc42..9a359d57cb4 100644 --- a/source/gameengine/GameLogic/SCA_ANDController.h +++ b/source/gameengine/GameLogic/SCA_ANDController.h @@ -49,6 +49,7 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); }; diff --git a/source/gameengine/GameLogic/SCA_ActuatorEventManager.cpp b/source/gameengine/GameLogic/SCA_ActuatorEventManager.cpp index 768a3a45937..a80b2af55c8 100644 --- a/source/gameengine/GameLogic/SCA_ActuatorEventManager.cpp +++ b/source/gameengine/GameLogic/SCA_ActuatorEventManager.cpp @@ -51,17 +51,19 @@ SCA_ActuatorEventManager::~SCA_ActuatorEventManager() void SCA_ActuatorEventManager::NextFrame() { // check for changed actuator - for (set<SCA_ISensor*>::const_iterator it = m_sensors.begin();!(it==m_sensors.end());it++) + SG_DList::iterator<SCA_ISensor> it(m_sensors); + for (it.begin();!it.end();++it) { - (*it)->Activate(m_logicmgr,NULL); + (*it)->Activate(m_logicmgr); } } void SCA_ActuatorEventManager::UpdateFrame() { // update the state of actuator before executing them - for (set<SCA_ISensor*>::const_iterator it = m_sensors.begin();!(it==m_sensors.end());it++) + SG_DList::iterator<SCA_ActuatorSensor> it(m_sensors); + for (it.begin();!it.end();++it) { - ((SCA_ActuatorSensor*)(*it))->Update(); + (*it)->Update(); } }
\ No newline at end of file diff --git a/source/gameengine/GameLogic/SCA_ActuatorSensor.cpp b/source/gameengine/GameLogic/SCA_ActuatorSensor.cpp index ed7aa66d04b..4dad65c5a25 100644 --- a/source/gameengine/GameLogic/SCA_ActuatorSensor.cpp +++ b/source/gameengine/GameLogic/SCA_ActuatorSensor.cpp @@ -60,7 +60,7 @@ CValue* SCA_ActuatorSensor::GetReplica() { SCA_ActuatorSensor* replica = new SCA_ActuatorSensor(*this); // m_range_expr must be recalculated on replica! - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); replica->Init(); return replica; @@ -89,7 +89,7 @@ SCA_ActuatorSensor::~SCA_ActuatorSensor() -bool SCA_ActuatorSensor::Evaluate(CValue* event) +bool SCA_ActuatorSensor::Evaluate() { if (m_actuator) { @@ -122,12 +122,17 @@ void SCA_ActuatorSensor::Update() /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_ActuatorSensor::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_ActuatorSensor", - sizeof(SCA_ActuatorSensor), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -162,10 +167,15 @@ PyAttributeDef SCA_ActuatorSensor::Attributes[] = { }; PyObject* SCA_ActuatorSensor::py_getattro(PyObject *attr) { - PyObject* object = py_getattro_self(Attributes, this, attr); - if (object != NULL) - return object; - py_getattro_up(SCA_ISensor); /* implicit return! */ + py_getattro_up(SCA_ISensor); +} + +PyObject* SCA_ActuatorSensor::py_getattro_dict() { + py_getattro_dict_up(SCA_ISensor); +} + +int SCA_ActuatorSensor::py_setattro(PyObject *attr, PyObject *value) { + py_setattro_up(SCA_ISensor); } int SCA_ActuatorSensor::CheckActuator(void *self, const PyAttributeDef*) @@ -180,18 +190,11 @@ int SCA_ActuatorSensor::CheckActuator(void *self, const PyAttributeDef*) return 1; } -int SCA_ActuatorSensor::py_setattro(PyObject *attr, PyObject *value) { - int ret = py_setattro_self(Attributes, this, attr, value); - if (ret >= 0) - return ret; - return SCA_ISensor::py_setattro(attr, value); -} - /* 3. getActuator */ const char SCA_ActuatorSensor::GetActuator_doc[] = "getActuator()\n" "\tReturn the Actuator with which the sensor operates.\n"; -PyObject* SCA_ActuatorSensor::PyGetActuator(PyObject* self) +PyObject* SCA_ActuatorSensor::PyGetActuator() { ShowDeprecationWarning("getActuator()", "the actuator property"); return PyString_FromString(m_checkactname); @@ -203,14 +206,14 @@ const char SCA_ActuatorSensor::SetActuator_doc[] = "\t- name: string\n" "\tSets the Actuator with which to operate. If there is no Actuator\n" "\tof this name, the call is ignored.\n"; -PyObject* SCA_ActuatorSensor::PySetActuator(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_ActuatorSensor::PySetActuator(PyObject* args) { ShowDeprecationWarning("setActuator()", "the actuator property"); /* We should query whether the name exists. Or should we create a prop */ /* on the fly? */ char *actNameArg = NULL; - if (!PyArg_ParseTuple(args, "s", &actNameArg)) { + if (!PyArg_ParseTuple(args, "s:setActuator", &actNameArg)) { return NULL; } diff --git a/source/gameengine/GameLogic/SCA_ActuatorSensor.h b/source/gameengine/GameLogic/SCA_ActuatorSensor.h index 9bc873e4ee1..6655e08dc70 100644 --- a/source/gameengine/GameLogic/SCA_ActuatorSensor.h +++ b/source/gameengine/GameLogic/SCA_ActuatorSensor.h @@ -52,7 +52,7 @@ public: virtual ~SCA_ActuatorSensor(); virtual CValue* GetReplica(); virtual void Init(); - virtual bool Evaluate(CValue* event); + virtual bool Evaluate(); virtual bool IsPositiveTrigger(); virtual void ReParent(SCA_IObject* parent); void Update(); @@ -62,10 +62,11 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual int py_setattro(PyObject *attr, PyObject *value); /* 3. setProperty */ - KX_PYMETHOD_DOC(SCA_ActuatorSensor,SetActuator); + KX_PYMETHOD_DOC_VARARGS(SCA_ActuatorSensor,SetActuator); /* 4. getProperty */ KX_PYMETHOD_DOC_NOARGS(SCA_ActuatorSensor,GetActuator); diff --git a/source/gameengine/GameLogic/SCA_AlwaysEventManager.cpp b/source/gameengine/GameLogic/SCA_AlwaysEventManager.cpp index 4cd2dfba994..dd3b55abcc9 100644 --- a/source/gameengine/GameLogic/SCA_AlwaysEventManager.cpp +++ b/source/gameengine/GameLogic/SCA_AlwaysEventManager.cpp @@ -51,9 +51,10 @@ SCA_AlwaysEventManager::SCA_AlwaysEventManager(class SCA_LogicManager* logicmgr) void SCA_AlwaysEventManager::NextFrame() { - for (set<class SCA_ISensor*>::const_iterator i= m_sensors.begin();!(i==m_sensors.end());i++) + SG_DList::iterator<SCA_ISensor> it(m_sensors); + for (it.begin();!it.end();++it) { - (*i)->Activate(m_logicmgr, NULL); + (*it)->Activate(m_logicmgr); } } diff --git a/source/gameengine/GameLogic/SCA_AlwaysSensor.cpp b/source/gameengine/GameLogic/SCA_AlwaysSensor.cpp index a7b0e5a14d2..ff02680f191 100644 --- a/source/gameengine/GameLogic/SCA_AlwaysSensor.cpp +++ b/source/gameengine/GameLogic/SCA_AlwaysSensor.cpp @@ -72,7 +72,7 @@ CValue* SCA_AlwaysSensor::GetReplica() { CValue* replica = new SCA_AlwaysSensor(*this);//m_float,GetName()); // this will copy properties and so on... - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); return replica; } @@ -86,7 +86,7 @@ bool SCA_AlwaysSensor::IsPositiveTrigger() -bool SCA_AlwaysSensor::Evaluate(CValue* event) +bool SCA_AlwaysSensor::Evaluate() { /* Nice! :) */ //return true; @@ -105,12 +105,17 @@ bool SCA_AlwaysSensor::Evaluate(CValue* event) /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_AlwaysSensor::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_AlwaysSensor", - sizeof(SCA_AlwaysSensor), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -143,4 +148,8 @@ PyObject* SCA_AlwaysSensor::py_getattro(PyObject *attr) { py_getattro_up(SCA_ISensor); } +PyObject* SCA_AlwaysSensor::py_getattro_dict() { + py_getattro_dict_up(SCA_ISensor); +} + /* eof */ diff --git a/source/gameengine/GameLogic/SCA_AlwaysSensor.h b/source/gameengine/GameLogic/SCA_AlwaysSensor.h index 87949babf59..0f85a641ef1 100644 --- a/source/gameengine/GameLogic/SCA_AlwaysSensor.h +++ b/source/gameengine/GameLogic/SCA_AlwaysSensor.h @@ -43,7 +43,7 @@ public: PyTypeObject* T =&Type); virtual ~SCA_AlwaysSensor(); virtual CValue* GetReplica(); - virtual bool Evaluate(CValue* event); + virtual bool Evaluate(); virtual bool IsPositiveTrigger(); virtual void Init(); @@ -53,7 +53,7 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); - + virtual PyObject* py_getattro_dict(); }; diff --git a/source/gameengine/GameLogic/SCA_DelaySensor.cpp b/source/gameengine/GameLogic/SCA_DelaySensor.cpp index 5082caacfd5..dcdae0b4e75 100644 --- a/source/gameengine/GameLogic/SCA_DelaySensor.cpp +++ b/source/gameengine/GameLogic/SCA_DelaySensor.cpp @@ -77,7 +77,7 @@ CValue* SCA_DelaySensor::GetReplica() { CValue* replica = new SCA_DelaySensor(*this); // this will copy properties and so on... - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); return replica; } @@ -89,7 +89,7 @@ bool SCA_DelaySensor::IsPositiveTrigger() return (m_invert ? !m_lastResult : m_lastResult); } -bool SCA_DelaySensor::Evaluate(CValue* event) +bool SCA_DelaySensor::Evaluate() { bool trigger = false; bool result; @@ -131,12 +131,17 @@ bool SCA_DelaySensor::Evaluate(CValue* event) /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_DelaySensor::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_DelaySensor", - sizeof(SCA_DelaySensor), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -179,17 +184,15 @@ PyAttributeDef SCA_DelaySensor::Attributes[] = { }; PyObject* SCA_DelaySensor::py_getattro(PyObject *attr) { - PyObject* object = py_getattro_self(Attributes, this, attr); - if (object != NULL) - return object; py_getattro_up(SCA_ISensor); } +PyObject* SCA_DelaySensor::py_getattro_dict() { + py_getattro_dict_up(SCA_ISensor); +} + int SCA_DelaySensor::py_setattro(PyObject *attr, PyObject *value) { - int ret = py_setattro_self(Attributes, this, attr, value); - if (ret >= 0) - return ret; - return SCA_ISensor::py_setattro(attr, value); + py_setattro_up(SCA_ISensor); } @@ -198,12 +201,12 @@ const char SCA_DelaySensor::SetDelay_doc[] = "\t- delay: length of the initial OFF period as number of frame\n" "\t 0 for immediate trigger\n" "\tSet the initial delay before the positive trigger\n"; -PyObject* SCA_DelaySensor::PySetDelay(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_DelaySensor::PySetDelay(PyObject* args) { ShowDeprecationWarning("setDelay()", "the delay property"); int delay; - if(!PyArg_ParseTuple(args, "i", &delay)) { + if(!PyArg_ParseTuple(args, "i:setDelay", &delay)) { return NULL; } if (delay < 0) { @@ -220,12 +223,12 @@ const char SCA_DelaySensor::SetDuration_doc[] = "\t 0 for no ON period\n" "\tSet the duration of the ON pulse after initial delay.\n" "\tIf > 0, a negative trigger is fired at the end of the ON pulse.\n"; -PyObject* SCA_DelaySensor::PySetDuration(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_DelaySensor::PySetDuration(PyObject* args) { ShowDeprecationWarning("setDuration()", "the duration property"); int duration; - if(!PyArg_ParseTuple(args, "i", &duration)) { + if(!PyArg_ParseTuple(args, "i:setDuration", &duration)) { return NULL; } if (duration < 0) { @@ -241,12 +244,12 @@ const char SCA_DelaySensor::SetRepeat_doc[] = "\t- repeat: 1 if the initial OFF-ON cycle should be repeated indefinately\n" "\t 0 if the initial OFF-ON cycle should run only once\n" "\tSet the sensor repeat mode\n"; -PyObject* SCA_DelaySensor::PySetRepeat(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_DelaySensor::PySetRepeat(PyObject* args) { ShowDeprecationWarning("setRepeat()", "the repeat property"); int repeat; - if(!PyArg_ParseTuple(args, "i", &repeat)) { + if(!PyArg_ParseTuple(args, "i:setRepeat", &repeat)) { return NULL; } m_repeat = (repeat != 0); @@ -256,7 +259,7 @@ PyObject* SCA_DelaySensor::PySetRepeat(PyObject* self, PyObject* args, PyObject* const char SCA_DelaySensor::GetDelay_doc[] = "getDelay()\n" "\tReturn the delay parameter value\n"; -PyObject* SCA_DelaySensor::PyGetDelay(PyObject* self) +PyObject* SCA_DelaySensor::PyGetDelay() { ShowDeprecationWarning("getDelay()", "the delay property"); return PyInt_FromLong(m_delay); @@ -265,7 +268,7 @@ PyObject* SCA_DelaySensor::PyGetDelay(PyObject* self) const char SCA_DelaySensor::GetDuration_doc[] = "getDuration()\n" "\tReturn the duration parameter value\n"; -PyObject* SCA_DelaySensor::PyGetDuration(PyObject* self) +PyObject* SCA_DelaySensor::PyGetDuration() { ShowDeprecationWarning("getDuration()", "the duration property"); return PyInt_FromLong(m_duration); @@ -274,7 +277,7 @@ PyObject* SCA_DelaySensor::PyGetDuration(PyObject* self) const char SCA_DelaySensor::GetRepeat_doc[] = "getRepeat()\n" "\tReturn the repeat parameter value\n"; -PyObject* SCA_DelaySensor::PyGetRepeat(PyObject* self) +PyObject* SCA_DelaySensor::PyGetRepeat() { ShowDeprecationWarning("getRepeat()", "the repeat property"); return BoolToPyArg(m_repeat); diff --git a/source/gameengine/GameLogic/SCA_DelaySensor.h b/source/gameengine/GameLogic/SCA_DelaySensor.h index f9e3d619198..5ccb33f8a16 100644 --- a/source/gameengine/GameLogic/SCA_DelaySensor.h +++ b/source/gameengine/GameLogic/SCA_DelaySensor.h @@ -51,7 +51,7 @@ public: PyTypeObject* T =&Type); virtual ~SCA_DelaySensor(); virtual CValue* GetReplica(); - virtual bool Evaluate(CValue* event); + virtual bool Evaluate(); virtual bool IsPositiveTrigger(); virtual void Init(); @@ -61,12 +61,13 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual int py_setattro(PyObject *attr, PyObject *value); /* setProperty */ - KX_PYMETHOD_DOC(SCA_DelaySensor,SetDelay); - KX_PYMETHOD_DOC(SCA_DelaySensor,SetDuration); - KX_PYMETHOD_DOC(SCA_DelaySensor,SetRepeat); + KX_PYMETHOD_DOC_VARARGS(SCA_DelaySensor,SetDelay); + KX_PYMETHOD_DOC_VARARGS(SCA_DelaySensor,SetDuration); + KX_PYMETHOD_DOC_VARARGS(SCA_DelaySensor,SetRepeat); /* getProperty */ KX_PYMETHOD_DOC_NOARGS(SCA_DelaySensor,GetDelay); KX_PYMETHOD_DOC_NOARGS(SCA_DelaySensor,GetDuration); diff --git a/source/gameengine/GameLogic/SCA_EventManager.cpp b/source/gameengine/GameLogic/SCA_EventManager.cpp index e4fd0379597..d1301541a0a 100644 --- a/source/gameengine/GameLogic/SCA_EventManager.cpp +++ b/source/gameengine/GameLogic/SCA_EventManager.cpp @@ -28,6 +28,7 @@ #include <assert.h> #include "SCA_EventManager.h" +#include "SCA_ISensor.h" #ifdef HAVE_CONFIG_H #include <config.h> @@ -43,16 +44,18 @@ SCA_EventManager::SCA_EventManager(EVENT_MANAGER_TYPE mgrtype) SCA_EventManager::~SCA_EventManager() { + // all sensors should be removed + assert(m_sensors.Empty()); } void SCA_EventManager::RegisterSensor(class SCA_ISensor* sensor) { - m_sensors.insert(sensor); + m_sensors.AddBack(sensor); } void SCA_EventManager::RemoveSensor(class SCA_ISensor* sensor) { - m_sensors.erase(sensor); + sensor->Delink(); } void SCA_EventManager::NextFrame(double curtime, double fixedtime) diff --git a/source/gameengine/GameLogic/SCA_EventManager.h b/source/gameengine/GameLogic/SCA_EventManager.h index 9dbb5a6d24f..5ff55849bfe 100644 --- a/source/gameengine/GameLogic/SCA_EventManager.h +++ b/source/gameengine/GameLogic/SCA_EventManager.h @@ -33,11 +33,14 @@ #include <set> #include <algorithm> +#include "SG_DList.h" + class SCA_EventManager { protected: // use a set to speed-up insertion/removal - std::set <class SCA_ISensor*> m_sensors; + //std::set <class SCA_ISensor*> m_sensors; + SG_DList m_sensors; public: enum EVENT_MANAGER_TYPE { diff --git a/source/gameengine/GameLogic/SCA_ExpressionController.cpp b/source/gameengine/GameLogic/SCA_ExpressionController.cpp index 8ed46beb7f3..8e044b89c71 100644 --- a/source/gameengine/GameLogic/SCA_ExpressionController.cpp +++ b/source/gameengine/GameLogic/SCA_ExpressionController.cpp @@ -70,7 +70,7 @@ CValue* SCA_ExpressionController::GetReplica() replica->m_exprText = m_exprText; replica->m_exprCache = NULL; // this will copy properties and so on... - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); return replica; } @@ -109,43 +109,20 @@ void SCA_ExpressionController::Trigger(SCA_LogicManager* logicmgr) printf(value->GetText()); } else { - float num = value->GetNumber(); + float num = (float)value->GetNumber(); expressionresult = !MT_fuzzyZero(num); } value->Release(); } - //m_exprCache->Release(); - //m_exprCache = NULL; } - /* - - for (vector<SCA_ISensor*>::const_iterator is=m_linkedsensors.begin(); - !(is==m_linkedsensors.end());is++) - { - SCA_ISensor* sensor = *is; - if (!sensor->IsPositiveTrigger()) - { - sensorresult = false; - break; - } - } - - */ - - CValue* newevent = new CBoolValue(expressionresult); - for (vector<SCA_IActuator*>::const_iterator i=m_linkedactuators.begin(); !(i==m_linkedactuators.end());i++) { SCA_IActuator* actua = *i; - logicmgr->AddActiveActuator(actua,newevent); + logicmgr->AddActiveActuator(actua,expressionresult); } - //printf("expr %d.",expressionresult); - // every actuator that needs the event, has a it's own reference to it now so - // release it (so to be clear: if there is no actuator, it's deleted right now) - newevent->Release(); } @@ -161,7 +138,7 @@ CValue* SCA_ExpressionController::FindIdentifier(const STR_String& identifiernam SCA_ISensor* sensor = *is; if (sensor->GetName() == identifiername) { - identifierval = new CBoolValue(sensor->IsPositiveTrigger()); + identifierval = new CBoolValue(sensor->GetState()); //identifierval = sensor->AddRef(); } diff --git a/source/gameengine/GameLogic/SCA_ExpressionController.h b/source/gameengine/GameLogic/SCA_ExpressionController.h index 2936742be19..6a34d7b2dff 100644 --- a/source/gameengine/GameLogic/SCA_ExpressionController.h +++ b/source/gameengine/GameLogic/SCA_ExpressionController.h @@ -60,6 +60,7 @@ public: /* --------------------------------------------------------------------- */ // virtual PyObject* py_getattro(PyObject *attr); +// virtual PyObject* py_getattro_dict(); }; diff --git a/source/gameengine/GameLogic/SCA_IActuator.cpp b/source/gameengine/GameLogic/SCA_IActuator.cpp index 309f3108418..5f71bb3f9e4 100644 --- a/source/gameengine/GameLogic/SCA_IActuator.cpp +++ b/source/gameengine/GameLogic/SCA_IActuator.cpp @@ -37,53 +37,13 @@ using namespace std; SCA_IActuator::SCA_IActuator(SCA_IObject* gameobj, PyTypeObject* T) : SCA_ILogicBrick(gameobj,T), - m_links(0) + m_links(0), + m_posevent(false), + m_negevent(false) { // nothing to do } - - -void SCA_IActuator::AddEvent(CValue* event) -{ - m_events.push_back(event); -} - - - -void SCA_IActuator::RemoveAllEvents() -{ // remove event queue! - for (vector<CValue*>::iterator i=m_events.begin(); !(i==m_events.end());i++) - { - (*i)->Release(); - } - m_events.clear(); -} - - - - - -bool SCA_IActuator::IsNegativeEvent() const -{ - bool bPositiveEvent(false); - bool bNegativeEvent(false); - - for (vector<CValue*>::const_iterator i=m_events.begin(); i!=m_events.end();++i) - { - if ((*i)->GetNumber() == 0.0f) - { - bNegativeEvent = true; - } else { - bPositiveEvent = true; - } - } - - // if at least 1 positive event, return false - - return !bPositiveEvent && bNegativeEvent; -} - bool SCA_IActuator::Update(double curtime, bool frame) { if (frame) @@ -100,7 +60,9 @@ bool SCA_IActuator::Update() void SCA_IActuator::ProcessReplica() { - m_events.clear(); + SCA_ILogicBrick::ProcessReplica(); + RemoveAllEvents(); + m_linkedcontrollers.clear(); } @@ -119,3 +81,36 @@ void SCA_IActuator::DecLink() m_links = 0; } } + +void SCA_IActuator::LinkToController(SCA_IController* controller) +{ + m_linkedcontrollers.push_back(controller); +} + +void SCA_IActuator::UnlinkController(SCA_IController* controller) +{ + std::vector<class SCA_IController*>::iterator contit; + for (contit = m_linkedcontrollers.begin();!(contit==m_linkedcontrollers.end());++contit) + { + if ((*contit) == controller) + { + *contit = m_linkedcontrollers.back(); + m_linkedcontrollers.pop_back(); + return; + } + } + printf("Missing link from actuator %s:%s to controller %s:%s\n", + m_gameobj->GetName().ReadPtr(), GetName().ReadPtr(), + controller->GetParent()->GetName().ReadPtr(), controller->GetName().ReadPtr()); +} + +void SCA_IActuator::UnlinkAllControllers() +{ + std::vector<class SCA_IController*>::iterator contit; + for (contit = m_linkedcontrollers.begin();!(contit==m_linkedcontrollers.end());++contit) + { + (*contit)->UnlinkActuator(this); + } + m_linkedcontrollers.clear(); +} + diff --git a/source/gameengine/GameLogic/SCA_IActuator.h b/source/gameengine/GameLogic/SCA_IActuator.h index 51bd6454d92..3055e1d946f 100644 --- a/source/gameengine/GameLogic/SCA_IActuator.h +++ b/source/gameengine/GameLogic/SCA_IActuator.h @@ -29,17 +29,33 @@ #ifndef __KX_IACTUATOR #define __KX_IACTUATOR -#include "SCA_ILogicBrick.h" +#include "SCA_IController.h" #include <vector> +/* + * Use of SG_DList : element of actuator being deactivated + * Head: SCA_LogicManager::m_removedActuators + * Use of SG_QList : element of activated actuator list of their owner + * Head: SCA_IObject::m_activeActuators + */ class SCA_IActuator : public SCA_ILogicBrick { friend class SCA_LogicManager; protected: int m_links; // number of active links to controllers // when 0, the actuator is automatically stopped - std::vector<CValue*> m_events; - void RemoveAllEvents(); + //std::vector<CValue*> m_events; + bool m_posevent; + bool m_negevent; + + std::vector<class SCA_IController*> m_linkedcontrollers; + + void RemoveAllEvents() + { + m_posevent = false; + m_negevent = false; + } + public: /** @@ -75,7 +91,15 @@ public: /** * Add an event to an actuator. */ - void AddEvent(CValue* event); + //void AddEvent(CValue* event) + void AddEvent(bool event) + { + if (event) + m_posevent = true; + else + m_negevent = true; + } + virtual void ProcessReplica(); /** @@ -84,9 +108,38 @@ public: * not immediately clear. But usually refers to key-up events * or events where no action is required. */ - bool IsNegativeEvent() const; + bool IsNegativeEvent() const + { + return !m_posevent && m_negevent; + } + virtual ~SCA_IActuator(); + /** + * remove this actuator from the list of active actuators + */ + void Deactivate() + { + if (QDelink()) + // the actuator was in the active list + if (m_gameobj->m_activeActuators.QEmpty()) + // the owner object has no more active actuators, remove it from the global list + m_gameobj->m_activeActuators.Delink(); + } + + void Activate(SG_DList& head) + { + if (QEmpty()) + { + InsertActiveQList(m_gameobj->m_activeActuators); + head.AddBack(&m_gameobj->m_activeActuators); + } + } + + void LinkToController(SCA_IController* controller); + void UnlinkController(class SCA_IController* cont); + void UnlinkAllControllers(); + void ClrLink() { m_links=0; } void IncLink() { m_links++; } void DecLink(); diff --git a/source/gameengine/GameLogic/SCA_IController.cpp b/source/gameengine/GameLogic/SCA_IController.cpp index f9c192cae5c..f8b081ef050 100644 --- a/source/gameengine/GameLogic/SCA_IController.cpp +++ b/source/gameengine/GameLogic/SCA_IController.cpp @@ -30,6 +30,8 @@ #include "SCA_LogicManager.h" #include "SCA_IActuator.h" #include "SCA_ISensor.h" +#include "PyObjectPlus.h" +#include "../Ketsji/KX_PythonSeq.h" /* not nice, only need for KX_PythonSeq_CreatePyObject */ #ifdef HAVE_CONFIG_H #include <config.h> @@ -39,7 +41,8 @@ SCA_IController::SCA_IController(SCA_IObject* gameobj, PyTypeObject* T) : SCA_ILogicBrick(gameobj,T), - m_statemask(0) + m_statemask(0), + m_justActivated(false) { } @@ -47,19 +50,19 @@ SCA_IController::SCA_IController(SCA_IObject* gameobj, SCA_IController::~SCA_IController() { - UnlinkAllActuators(); + //UnlinkAllActuators(); } -const std::vector<class SCA_ISensor*>& SCA_IController::GetLinkedSensors() +std::vector<class SCA_ISensor*>& SCA_IController::GetLinkedSensors() { return m_linkedsensors; } -const std::vector<class SCA_IActuator*>& SCA_IController::GetLinkedActuators() +std::vector<class SCA_IActuator*>& SCA_IController::GetLinkedActuators() { return m_linkedactuators; } @@ -68,13 +71,14 @@ const std::vector<class SCA_IActuator*>& SCA_IController::GetLinkedActuators() void SCA_IController::UnlinkAllSensors() { - if (IsActive()) + std::vector<class SCA_ISensor*>::iterator sensit; + for (sensit = m_linkedsensors.begin();!(sensit==m_linkedsensors.end());++sensit) { - std::vector<class SCA_ISensor*>::iterator sensit; - for (sensit = m_linkedsensors.begin();!(sensit==m_linkedsensors.end());++sensit) + if (IsActive()) { (*sensit)->DecLink(); } + (*sensit)->UnlinkController(this); } m_linkedsensors.clear(); } @@ -83,34 +87,18 @@ void SCA_IController::UnlinkAllSensors() void SCA_IController::UnlinkAllActuators() { - if (IsActive()) + std::vector<class SCA_IActuator*>::iterator actit; + for (actit = m_linkedactuators.begin();!(actit==m_linkedactuators.end());++actit) { - std::vector<class SCA_IActuator*>::iterator actit; - for (actit = m_linkedactuators.begin();!(actit==m_linkedactuators.end());++actit) + if (IsActive()) { (*actit)->DecLink(); } + (*actit)->UnlinkController(this); } m_linkedactuators.clear(); } - - -/* -void SCA_IController::Trigger(SCA_LogicManager* logicmgr) -{ - //for (int i=0;i<m_linkedactuators.size();i++) - for (vector<SCA_IActuator*>::const_iterator i=m_linkedactuators.begin(); - !(i==m_linkedactuators.end());i++) - { - SCA_IActuator* actua = *i;//m_linkedactuators.at(i); - - logicmgr->AddActiveActuator(actua); - } - -} -*/ - void SCA_IController::LinkToActuator(SCA_IActuator* actua) { m_linkedactuators.push_back(actua); @@ -127,18 +115,18 @@ void SCA_IController::UnlinkActuator(class SCA_IActuator* actua) { if ((*actit) == actua) { - break; - } - - } - if (!(actit==m_linkedactuators.end())) - { - if (IsActive()) - { - (*actit)->DecLink(); + if (IsActive()) + { + (*actit)->DecLink(); + } + *actit = m_linkedactuators.back(); + m_linkedactuators.pop_back(); + return; } - m_linkedactuators.erase(actit); } + printf("Missing link from controller %s:%s to actuator %s:%s\n", + m_gameobj->GetName().ReadPtr(), GetName().ReadPtr(), + actua->GetParent()->GetName().ReadPtr(), actua->GetName().ReadPtr()); } void SCA_IController::LinkToSensor(SCA_ISensor* sensor) @@ -157,20 +145,21 @@ void SCA_IController::UnlinkSensor(class SCA_ISensor* sensor) { if ((*sensit) == sensor) { - break; - } - - } - if (!(sensit==m_linkedsensors.end())) - { - if (IsActive()) - { - (*sensit)->DecLink(); + if (IsActive()) + { + sensor->DecLink(); + } + *sensit = m_linkedsensors.back(); + m_linkedsensors.pop_back(); + return; } - m_linkedsensors.erase(sensit); } + printf("Missing link from controller %s:%s to sensor %s:%s\n", + m_gameobj->GetName().ReadPtr(), GetName().ReadPtr(), + sensor->GetParent()->GetName().ReadPtr(), sensor->GetName().ReadPtr()); } + void SCA_IController::ApplyState(unsigned int state) { std::vector<class SCA_IActuator*>::iterator actit; @@ -185,13 +174,13 @@ void SCA_IController::ApplyState(unsigned int state) { (*actit)->IncLink(); } + for (sensit = m_linkedsensors.begin();!(sensit==m_linkedsensors.end());++sensit) { (*sensit)->IncLink(); - // remember that this controller just activated that sensor - (*sensit)->AddNewController(this); } SetActive(true); + m_justActivated = true; } } else if (IsActive()) { @@ -204,6 +193,167 @@ void SCA_IController::ApplyState(unsigned int state) (*sensit)->DecLink(); } SetActive(false); + m_justActivated = false; } } +/* Python api */ + +PyTypeObject SCA_IController::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_IController", + 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_IController::Parents[] = { + &SCA_IController::Type, + &CValue::Type, + NULL +}; + +PyMethodDef SCA_IController::Methods[] = { + //Deprecated functions ------> + {"getSensor", (PyCFunction) SCA_IController::sPyGetSensor, METH_O}, + {"getActuator", (PyCFunction) SCA_IController::sPyGetActuator, METH_O}, + {"getSensors", (PyCFunction) SCA_IController::sPyGetSensors, METH_NOARGS}, + {"getActuators", (PyCFunction) SCA_IController::sPyGetActuators, METH_NOARGS}, + {"getState", (PyCFunction) SCA_IController::sPyGetState, METH_NOARGS}, + //<----- Deprecated + {NULL,NULL} //Sentinel +}; + +PyAttributeDef SCA_IController::Attributes[] = { + KX_PYATTRIBUTE_RO_FUNCTION("state", SCA_IController, pyattr_get_state), + KX_PYATTRIBUTE_RO_FUNCTION("sensors", SCA_IController, pyattr_get_sensors), + KX_PYATTRIBUTE_RO_FUNCTION("actuators", SCA_IController, pyattr_get_actuators), + { NULL } //Sentinel +}; + +PyObject* SCA_IController::py_getattro(PyObject *attr) +{ + py_getattro_up(SCA_ILogicBrick); +} + +PyObject* SCA_IController::py_getattro_dict() { + py_getattro_dict_up(SCA_ILogicBrick); +} + +int SCA_IController::py_setattro(PyObject *attr, PyObject *value) +{ + py_setattro_up(SCA_ILogicBrick); +} + + + +PyObject* SCA_IController::PyGetActuators() +{ + ShowDeprecationWarning("getActuators()", "the actuators property"); + + PyObject* resultlist = PyList_New(m_linkedactuators.size()); + for (unsigned int index=0;index<m_linkedactuators.size();index++) + { + PyList_SET_ITEM(resultlist,index, m_linkedactuators[index]->GetProxy()); + } + + return resultlist; +} + +PyObject* SCA_IController::PyGetSensor(PyObject* value) +{ + ShowDeprecationWarning("getSensor(string)", "the sensors[string] property"); + + char *scriptArg = PyString_AsString(value); + if (scriptArg==NULL) { + PyErr_SetString(PyExc_TypeError, "controller.getSensor(string): Python Controller, expected a string (sensor name)"); + return NULL; + } + + for (unsigned int index=0;index<m_linkedsensors.size();index++) + { + SCA_ISensor* sensor = m_linkedsensors[index]; + STR_String& realname = sensor->GetName(); + if (realname == scriptArg) + { + return sensor->GetProxy(); + } + } + + PyErr_Format(PyExc_AttributeError, "controller.getSensor(string): Python Controller, unable to find requested sensor \"%s\"", scriptArg); + return NULL; +} + +PyObject* SCA_IController::PyGetActuator(PyObject* value) +{ + ShowDeprecationWarning("getActuator(string)", "the actuators[string] property"); + + char *scriptArg = PyString_AsString(value); + if (scriptArg==NULL) { + PyErr_SetString(PyExc_TypeError, "controller.getActuator(string): Python Controller, expected a string (actuator name)"); + return NULL; + } + + for (unsigned int index=0;index<m_linkedactuators.size();index++) + { + SCA_IActuator* actua = m_linkedactuators[index]; + if (actua->GetName() == scriptArg) + { + return actua->GetProxy(); + } + } + + PyErr_Format(PyExc_AttributeError, "controller.getActuator(string): Python Controller, unable to find requested actuator \"%s\"", scriptArg); + return NULL; +} + +PyObject* SCA_IController::PyGetSensors() +{ + ShowDeprecationWarning("getSensors()", "the sensors property"); + + PyObject* resultlist = PyList_New(m_linkedsensors.size()); + for (unsigned int index=0;index<m_linkedsensors.size();index++) + { + PyList_SET_ITEM(resultlist,index, m_linkedsensors[index]->GetProxy()); + } + + return resultlist; +} + +PyObject* SCA_IController::PyGetState() +{ + ShowDeprecationWarning("getState()", "the state property"); + return PyInt_FromLong(m_statemask); +} + +PyObject* SCA_IController::pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + SCA_IController* self= static_cast<SCA_IController*>(self_v); + return PyInt_FromLong(self->m_statemask); +} + +PyObject* SCA_IController::pyattr_get_sensors(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + return KX_PythonSeq_CreatePyObject((static_cast<SCA_IController*>(self_v))->m_proxy, KX_PYGENSEQ_CONT_TYPE_SENSORS); +} + +PyObject* SCA_IController::pyattr_get_actuators(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + return KX_PythonSeq_CreatePyObject((static_cast<SCA_IController*>(self_v))->m_proxy, KX_PYGENSEQ_CONT_TYPE_ACTUATORS); +} diff --git a/source/gameengine/GameLogic/SCA_IController.h b/source/gameengine/GameLogic/SCA_IController.h index f67c0942eb4..1b9d8fb0d2b 100644 --- a/source/gameengine/GameLogic/SCA_IController.h +++ b/source/gameengine/GameLogic/SCA_IController.h @@ -30,28 +30,75 @@ #define __KX_ICONTROLLER #include "SCA_ILogicBrick.h" +#include "PyObjectPlus.h" +/* + * Use of SG_DList element: none + * Use of SG_QList element: build ordered list of activated controller on the owner object + * Head: SCA_IObject::m_activeControllers + */ class SCA_IController : public SCA_ILogicBrick { + Py_Header; protected: std::vector<class SCA_ISensor*> m_linkedsensors; std::vector<class SCA_IActuator*> m_linkedactuators; unsigned int m_statemask; + bool m_justActivated; public: SCA_IController(SCA_IObject* gameobj,PyTypeObject* T); virtual ~SCA_IController(); virtual void Trigger(class SCA_LogicManager* logicmgr)=0; void LinkToSensor(SCA_ISensor* sensor); void LinkToActuator(SCA_IActuator*); - const std::vector<class SCA_ISensor*>& GetLinkedSensors(); - const std::vector<class SCA_IActuator*>& GetLinkedActuators(); + std::vector<class SCA_ISensor*>& GetLinkedSensors(); + std::vector<class SCA_IActuator*>& GetLinkedActuators(); + void ReserveActuator(int num) + { + m_linkedactuators.reserve(num); + } void UnlinkAllSensors(); void UnlinkAllActuators(); void UnlinkActuator(class SCA_IActuator* actua); void UnlinkSensor(class SCA_ISensor* sensor); void SetState(unsigned int state) { m_statemask = state; } void ApplyState(unsigned int state); + void Deactivate() + { + // the controller can only be part of a sensor m_newControllers list + Delink(); + } + bool IsJustActivated() + { + return m_justActivated; + } + void ClrJustActivated() + { + m_justActivated = false; + } + + void Activate(SG_DList& head) + { + if (QEmpty()) + { + InsertActiveQList(m_gameobj->m_activeControllers); + head.AddBack(&m_gameobj->m_activeControllers); + } + } + virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); + virtual int py_setattro(PyObject *attr, PyObject *value); + + KX_PYMETHOD_NOARGS(SCA_IController,GetSensors); + KX_PYMETHOD_NOARGS(SCA_IController,GetActuators); + KX_PYMETHOD_O(SCA_IController,GetSensor); + KX_PYMETHOD_O(SCA_IController,GetActuator); + KX_PYMETHOD_NOARGS(SCA_IController,GetState); + + static PyObject* pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); + static PyObject* pyattr_get_sensors(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); + static PyObject* pyattr_get_actuators(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); }; diff --git a/source/gameengine/GameLogic/SCA_ILogicBrick.cpp b/source/gameengine/GameLogic/SCA_ILogicBrick.cpp index 45ebd874ea5..2dc80f54568 100644 --- a/source/gameengine/GameLogic/SCA_ILogicBrick.cpp +++ b/source/gameengine/GameLogic/SCA_ILogicBrick.cpp @@ -71,13 +71,6 @@ void SCA_ILogicBrick::SetUeberExecutePriority(int execute_Priority) -SCA_IObject* SCA_ILogicBrick::GetParent() -{ - return m_gameobj; -} - - - void SCA_ILogicBrick::ReParent(SCA_IObject* parent) { m_gameobj = parent; @@ -123,40 +116,24 @@ const STR_String& SCA_ILogicBrick::GetText() -float SCA_ILogicBrick::GetNumber() +double SCA_ILogicBrick::GetNumber() { return -1; } -STR_String SCA_ILogicBrick::GetName() +STR_String& SCA_ILogicBrick::GetName() { return m_name; } -void SCA_ILogicBrick::SetName(STR_String name) -{ - m_name = name; -} - - - -void SCA_ILogicBrick::ReplicaSetName(STR_String name) +void SCA_ILogicBrick::SetName(const char *name) { m_name = name; } - - - -bool SCA_ILogicBrick::IsActive() -{ - return m_bActive; -} - - bool SCA_ILogicBrick::LessComparedTo(SCA_ILogicBrick* other) { @@ -165,22 +142,6 @@ bool SCA_ILogicBrick::LessComparedTo(SCA_ILogicBrick* other) (this->m_Execute_Priority < other->m_Execute_Priority)); } - - -void SCA_ILogicBrick::SetActive(bool active) -{ - m_bActive=active; - if (active) - { - //m_gameobj->SetDebugColor(GetDrawColor()); - } else - { - //m_gameobj->ResetDebugColor(); - } -} - - - void SCA_ILogicBrick::RegisterEvent(CValue* eventval) { if (m_eventval) @@ -217,12 +178,17 @@ CValue* SCA_ILogicBrick::GetEvent() /* python stuff */ PyTypeObject SCA_ILogicBrick::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_ILogicBrick", - sizeof(SCA_ILogicBrick), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -246,8 +212,8 @@ PyParentObject SCA_ILogicBrick::Parents[] = { PyMethodDef SCA_ILogicBrick::Methods[] = { + // --> Deprecated {"getOwner", (PyCFunction) SCA_ILogicBrick::sPyGetOwner, METH_NOARGS}, - // --> Deprecated {"getExecutePriority", (PyCFunction) SCA_ILogicBrick::sPyGetExecutePriority, METH_NOARGS}, {"setExecutePriority", (PyCFunction) SCA_ILogicBrick::sPySetExecutePriority, METH_VARARGS}, // <-- Deprecated @@ -255,7 +221,9 @@ PyMethodDef SCA_ILogicBrick::Methods[] = { }; PyAttributeDef SCA_ILogicBrick::Attributes[] = { - KX_PYATTRIBUTE_INT_RW("executePriority",0,100000,false,SCA_ILogicBrick,m_Execute_Ueber_Priority), + KX_PYATTRIBUTE_RO_FUNCTION("owner", SCA_ILogicBrick, pyattr_get_owner), + KX_PYATTRIBUTE_INT_RW("executePriority",0,100000,false,SCA_ILogicBrick,m_Execute_Priority), + KX_PYATTRIBUTE_STRING_RO("name", SCA_ILogicBrick, m_name), {NULL} //Sentinel }; @@ -277,31 +245,29 @@ int SCA_ILogicBrick::CheckProperty(void *self, const PyAttributeDef *attrdef) return 0; } -PyObject* -SCA_ILogicBrick::py_getattro(PyObject *attr) +PyObject* SCA_ILogicBrick::py_getattro(PyObject *attr) { - PyObject* object = py_getattro_self(Attributes, this, attr); - if (object != NULL) - return object; py_getattro_up(CValue); } +PyObject* SCA_ILogicBrick::py_getattro_dict() { + py_getattro_dict_up(CValue); +} + int SCA_ILogicBrick::py_setattro(PyObject *attr, PyObject *value) { - int ret = py_setattro_self(Attributes, this, attr, value); - if (ret >= 0) - return ret; - return CValue::py_setattro(attr, value); + py_setattro_up(CValue); } -PyObject* SCA_ILogicBrick::PyGetOwner(PyObject* self) +PyObject* SCA_ILogicBrick::PyGetOwner() { + ShowDeprecationWarning("getOwner()", "the owner property"); + CValue* parent = GetParent(); if (parent) { - parent->AddRef(); - return parent; + return parent->GetProxy(); } printf("ERROR: Python scriptblock without owner\n"); @@ -310,29 +276,40 @@ PyObject* SCA_ILogicBrick::PyGetOwner(PyObject* self) -PyObject* SCA_ILogicBrick::PySetExecutePriority(PyObject* self, - PyObject* args, - PyObject* kwds) +PyObject* SCA_ILogicBrick::PySetExecutePriority(PyObject* args) { ShowDeprecationWarning("setExecutePriority()", "the executePriority property"); int priority=0; - if (!PyArg_ParseTuple(args, "i", &priority)) { + if (!PyArg_ParseTuple(args, "i:setExecutePriority", &priority)) { return NULL; } - m_Execute_Ueber_Priority = priority; + m_Execute_Priority = priority; Py_RETURN_NONE; } -PyObject* SCA_ILogicBrick::PyGetExecutePriority(PyObject* self) +PyObject* SCA_ILogicBrick::PyGetExecutePriority() { ShowDeprecationWarning("getExecutePriority()", "the executePriority property"); - return PyInt_FromLong(m_Execute_Ueber_Priority); + return PyInt_FromLong(m_Execute_Priority); +} + + +/*Attribute functions */ +PyObject* SCA_ILogicBrick::pyattr_get_owner(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + SCA_ILogicBrick* self= static_cast<SCA_ILogicBrick*>(self_v); + CValue* parent = self->GetParent(); + + if (parent) + return parent->GetProxy(); + + Py_RETURN_NONE; } @@ -347,8 +324,6 @@ bool SCA_ILogicBrick::PyArgToBool(int boolArg) } } - - PyObject* SCA_ILogicBrick::BoolToPyArg(bool boolarg) { return PyInt_FromLong(boolarg? KX_TRUE: KX_FALSE); diff --git a/source/gameengine/GameLogic/SCA_ILogicBrick.h b/source/gameengine/GameLogic/SCA_ILogicBrick.h index c098f9dfd8a..90881c0536f 100644 --- a/source/gameengine/GameLogic/SCA_ILogicBrick.h +++ b/source/gameengine/GameLogic/SCA_ILogicBrick.h @@ -59,7 +59,8 @@ public: void SetExecutePriority(int execute_Priority); void SetUeberExecutePriority(int execute_Priority); - SCA_IObject* GetParent(); + SCA_IObject* GetParent() { return m_gameobj; } + virtual void ReParent(SCA_IObject* parent); virtual void Relink(GEN_Map<GEN_HashedPtr, void*> *obj_map); virtual void Delete() { Release(); } @@ -69,17 +70,32 @@ public: virtual CValue* CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val); virtual const STR_String & GetText(); - virtual float GetNumber(); - virtual STR_String GetName(); - virtual void SetName(STR_String name); - virtual void ReplicaSetName(STR_String name); + virtual double GetNumber(); + virtual STR_String& GetName(); + virtual void SetName(const char *); - bool IsActive(); - void SetActive(bool active) ; + bool IsActive() + { + return m_bActive; + } + + void SetActive(bool active) + { + m_bActive=active; + } + + // insert in a QList at position corresponding to m_Execute_Priority + void InsertActiveQList(SG_QList& head) + { + SG_QList::iterator<SCA_ILogicBrick> it(head); + for(it.begin(); !it.end() && m_Execute_Priority > (*it)->m_Execute_Priority; ++it); + it.add_back(this); + } virtual bool LessComparedTo(SCA_ILogicBrick* other); virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual int py_setattro(PyObject *attr, PyObject *value); static class SCA_LogicManager* m_sCurrentLogicManager; @@ -88,8 +104,10 @@ public: // python methods KX_PYMETHOD_NOARGS(SCA_ILogicBrick,GetOwner); - KX_PYMETHOD(SCA_ILogicBrick,SetExecutePriority); + KX_PYMETHOD_VARARGS(SCA_ILogicBrick,SetExecutePriority); KX_PYMETHOD_NOARGS(SCA_ILogicBrick,GetExecutePriority); + + static PyObject* pyattr_get_owner(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); // check that attribute is a property static int CheckProperty(void *self, const PyAttributeDef *attrdef); diff --git a/source/gameengine/GameLogic/SCA_IObject.cpp b/source/gameengine/GameLogic/SCA_IObject.cpp index d1ce377316b..8962c8e8580 100644 --- a/source/gameengine/GameLogic/SCA_IObject.cpp +++ b/source/gameengine/GameLogic/SCA_IObject.cpp @@ -70,7 +70,7 @@ SCA_IObject::~SCA_IObject() } for (ita = m_actuators.begin(); !(ita==m_actuators.end()); ++ita) { - ((CValue*)(*ita))->Release(); + (*ita)->Delete(); } //T_InterpolatorList::iterator i; @@ -79,29 +79,6 @@ SCA_IObject::~SCA_IObject() //} } - - -SCA_ControllerList& SCA_IObject::GetControllers() -{ - return m_controllers; -} - - - -SCA_SensorList& SCA_IObject::GetSensors() -{ - return m_sensors; -} - - - -SCA_ActuatorList& SCA_IObject::GetActuators() -{ - return m_actuators; -} - - - void SCA_IObject::AddSensor(SCA_ISensor* act) { act->AddRef(); @@ -133,7 +110,7 @@ void SCA_IObject::RegisterActuator(SCA_IActuator* act) void SCA_IObject::UnregisterActuator(SCA_IActuator* act) { SCA_ActuatorList::iterator ita; - for (ita = m_registeredActuators.begin(); ita != m_registeredActuators.end(); ita++) + for (ita = m_registeredActuators.begin(); ita != m_registeredActuators.end(); ++ita) { if ((*ita) == act) { (*ita) = m_registeredActuators.back(); @@ -143,20 +120,6 @@ void SCA_IObject::UnregisterActuator(SCA_IActuator* act) } } -void SCA_IObject::SetIgnoreActivityCulling(bool b) -{ - m_ignore_activity_culling = b; -} - - - -bool SCA_IObject::GetIgnoreActivityCulling() -{ - return m_ignore_activity_culling; -} - - - void SCA_IObject::ReParentLogic() { SCA_ActuatorList& oldactuators = GetActuators(); @@ -208,7 +171,7 @@ SCA_ISensor* SCA_IObject::FindSensor(const STR_String& sensorname) { SCA_ISensor* foundsensor = NULL; - for (SCA_SensorList::iterator its = m_sensors.begin();!(its==m_sensors.end());its++) + for (SCA_SensorList::iterator its = m_sensors.begin();!(its==m_sensors.end());++its) { if ((*its)->GetName() == sensorname) { @@ -225,7 +188,7 @@ SCA_IController* SCA_IObject::FindController(const STR_String& controllername) { SCA_IController* foundcontroller = NULL; - for (SCA_ControllerList::iterator itc = m_controllers.begin();!(itc==m_controllers.end());itc++) + for (SCA_ControllerList::iterator itc = m_controllers.begin();!(itc==m_controllers.end());++itc) { if ((*itc)->GetName() == controllername) { @@ -242,7 +205,7 @@ SCA_IActuator* SCA_IObject::FindActuator(const STR_String& actuatorname) { SCA_IActuator* foundactuator = NULL; - for (SCA_ActuatorList::iterator ita = m_actuators.begin();!(ita==m_actuators.end());ita++) + for (SCA_ActuatorList::iterator ita = m_actuators.begin();!(ita==m_actuators.end());++ita) { if ((*ita)->GetName() == actuatorname) { @@ -256,14 +219,6 @@ SCA_IActuator* SCA_IObject::FindActuator(const STR_String& actuatorname) -void SCA_IObject::SetCurrentTime(float currentTime) { - //T_InterpolatorList::iterator i; - //for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) { - // (*i)->Execute(currentTime); - //} -} - - #if 0 const MT_Point3& SCA_IObject::ConvertPythonPylist(PyObject* pylist) { @@ -317,7 +272,7 @@ void SCA_IObject::Suspend() SCA_SensorList::iterator i = m_sensors.begin(); while (i != m_sensors.end()) { (*i)->Suspend(); - i++; + ++i; } } } @@ -332,7 +287,7 @@ void SCA_IObject::Resume(void) SCA_SensorList::iterator i = m_sensors.begin(); while (i != m_sensors.end()) { (*i)->Resume(); - i++; + ++i; } } } @@ -352,7 +307,7 @@ void SCA_IObject::SetState(unsigned int state) if (tmpstate != m_state) { // update the status of the controllers - for (contit = m_controllers.begin(); contit != m_controllers.end(); contit++) + for (contit = m_controllers.begin(); contit != m_controllers.end(); ++contit) { (*contit)->ApplyState(tmpstate); } @@ -360,7 +315,7 @@ void SCA_IObject::SetState(unsigned int state) m_state = state; if (m_state != tmpstate) { - for (contit = m_controllers.begin(); contit != m_controllers.end(); contit++) + for (contit = m_controllers.begin(); contit != m_controllers.end(); ++contit) { (*contit)->ApplyState(m_state); } @@ -375,12 +330,17 @@ void SCA_IObject::SetState(unsigned int state) /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_IObject::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_IObject", - sizeof(SCA_IObject), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -418,3 +378,6 @@ PyObject* SCA_IObject::py_getattro(PyObject *attr) { py_getattro_up(CValue); } +PyObject* SCA_IObject::py_getattro_dict() { + py_getattro_dict_up(CValue); +} diff --git a/source/gameengine/GameLogic/SCA_IObject.h b/source/gameengine/GameLogic/SCA_IObject.h index 44ed3c8f3fe..281c72ecd46 100644 --- a/source/gameengine/GameLogic/SCA_IObject.h +++ b/source/gameengine/GameLogic/SCA_IObject.h @@ -52,10 +52,24 @@ class SCA_IObject : public CValue Py_Header; protected: + friend class SCA_IActuator; + friend class SCA_IController; SCA_SensorList m_sensors; SCA_ControllerList m_controllers; SCA_ActuatorList m_actuators; SCA_ActuatorList m_registeredActuators; // actuators that use a pointer to this object + + // SG_Dlist: element of objects with active actuators + // Head: SCA_LogicManager::m_activeActuators + // SG_QList: Head of active actuators list on this object + // Elements: SCA_IActuator + SG_QList m_activeActuators; + // SG_Dlist: element of objects with active controllers + // Head: SCA_LogicManager::m_activeControllers + // SG_QList: Head of active controller list on this object + // Elements: SCA_IController + SG_QList m_activeControllers; + static class MT_Point3 m_sDummy; /** @@ -83,13 +97,38 @@ public: SCA_IObject(PyTypeObject* T=&Type); virtual ~SCA_IObject(); - SCA_ControllerList& GetControllers(); - SCA_SensorList& GetSensors(); - SCA_ActuatorList& GetActuators(); + SCA_ControllerList& GetControllers() + { + return m_controllers; + } + SCA_SensorList& GetSensors() + { + return m_sensors; + } + SCA_ActuatorList& GetActuators() + { + return m_actuators; + } + SG_QList& GetActiveActuators() + { + return m_activeActuators; + } void AddSensor(SCA_ISensor* act); + void ReserveSensor(int num) + { + m_sensors.reserve(num); + } void AddController(SCA_IController* act); + void ReserveController(int num) + { + m_controllers.reserve(num); + } void AddActuator(SCA_IActuator* act); + void ReserveActuator(int num) + { + m_actuators.reserve(num); + } void RegisterActuator(SCA_IActuator* act); void UnregisterActuator(SCA_IActuator* act); @@ -97,20 +136,26 @@ public: SCA_IActuator* FindActuator(const STR_String& actuatorname); SCA_IController* FindController(const STR_String& controllername); - void SetCurrentTime(float currentTime); + void SetCurrentTime(float currentTime) {} void ReParentLogic(); /** * Set whether or not to ignore activity culling requests */ - void SetIgnoreActivityCulling(bool b); + void SetIgnoreActivityCulling(bool b) + { + m_ignore_activity_culling = b; + } /** * Set whether or not this object wants to ignore activity culling * requests */ - bool GetIgnoreActivityCulling(); + bool GetIgnoreActivityCulling() + { + return m_ignore_activity_culling; + } /** * Suspend all progress. @@ -146,6 +191,7 @@ public: // here come the python forwarded methods virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual int GetGameObjectType() {return -1;} diff --git a/source/gameengine/GameLogic/SCA_IScene.cpp b/source/gameengine/GameLogic/SCA_IScene.cpp index 9fbeb706910..86b176a38b0 100644 --- a/source/gameengine/GameLogic/SCA_IScene.cpp +++ b/source/gameengine/GameLogic/SCA_IScene.cpp @@ -50,7 +50,7 @@ SCA_IScene::SCA_IScene() void SCA_IScene::RemoveAllDebugProperties() { for (std::vector<SCA_DebugProp*>::iterator it = m_debugList.begin(); - !(it==m_debugList.end());it++) + !(it==m_debugList.end());++it) { delete (*it); } diff --git a/source/gameengine/GameLogic/SCA_IScene.h b/source/gameengine/GameLogic/SCA_IScene.h index d18778a37c2..b641efc6ee1 100644 --- a/source/gameengine/GameLogic/SCA_IScene.h +++ b/source/gameengine/GameLogic/SCA_IScene.h @@ -52,7 +52,7 @@ public: int lifespan=0)=0; virtual void RemoveObject(class CValue* gameobj)=0; virtual void DelayedRemoveObject(class CValue* gameobj)=0; - virtual void DelayedReleaseObject(class CValue* gameobj)=0; + //virtual void DelayedReleaseObject(class CValue* gameobj)=0; virtual void ReplaceMesh(class CValue* gameobj, void* meshobj)=0; diff --git a/source/gameengine/GameLogic/SCA_ISensor.cpp b/source/gameengine/GameLogic/SCA_ISensor.cpp index 8a40c0c35f3..2783bf14600 100644 --- a/source/gameengine/GameLogic/SCA_ISensor.cpp +++ b/source/gameengine/GameLogic/SCA_ISensor.cpp @@ -45,26 +45,28 @@ void SCA_ISensor::ReParent(SCA_IObject* parent) SCA_ILogicBrick::ReParent(parent); // will be done when the sensor is activated //m_eventmgr->RegisterSensor(this); - this->SetActive(false); + //this->SetActive(false); } SCA_ISensor::SCA_ISensor(SCA_IObject* gameobj, class SCA_EventManager* eventmgr, PyTypeObject* T ) : - SCA_ILogicBrick(gameobj,T), - m_triggered(false) + SCA_ILogicBrick(gameobj,T) { m_links = 0; m_suspended = false; m_invert = false; m_level = false; + m_tap = false; m_reset = false; m_pos_ticks = 0; m_neg_ticks = 0; m_pos_pulsemode = false; m_neg_pulsemode = false; m_pulse_frequency = 0; + m_state = false; + m_prev_state = false; m_eventmgr = eventmgr; } @@ -75,6 +77,12 @@ SCA_ISensor::~SCA_ISensor() // intentionally empty } +void SCA_ISensor::ProcessReplica() +{ + SCA_ILogicBrick::ProcessReplica(); + m_linkedcontrollers.clear(); +} + bool SCA_ISensor::IsPositiveTrigger() { bool result = false; @@ -104,9 +112,13 @@ void SCA_ISensor::SetLevel(bool lvl) { m_level = lvl; } +void SCA_ISensor::SetTap(bool tap) { + m_tap = tap; +} -float SCA_ISensor::GetNumber() { - return IsPositiveTrigger(); + +double SCA_ISensor::GetNumber() { + return GetState(); } void SCA_ISensor::Suspend() { @@ -143,27 +155,80 @@ void SCA_ISensor::RegisterToManager() { // sensor is just activated, initialize it Init(); - m_newControllers.erase(m_newControllers.begin(), m_newControllers.end()); + m_state = false; m_eventmgr->RegisterSensor(this); } +void SCA_ISensor::LinkToController(SCA_IController* controller) +{ + m_linkedcontrollers.push_back(controller); +} + +void SCA_ISensor::UnlinkController(SCA_IController* controller) +{ + std::vector<class SCA_IController*>::iterator contit; + for (contit = m_linkedcontrollers.begin();!(contit==m_linkedcontrollers.end());++contit) + { + if ((*contit) == controller) + { + *contit = m_linkedcontrollers.back(); + m_linkedcontrollers.pop_back(); + return; + } + } + printf("Missing link from sensor %s:%s to controller %s:%s\n", + m_gameobj->GetName().ReadPtr(), GetName().ReadPtr(), + controller->GetParent()->GetName().ReadPtr(), controller->GetName().ReadPtr()); +} + +void SCA_ISensor::UnlinkAllControllers() +{ + std::vector<class SCA_IController*>::iterator contit; + for (contit = m_linkedcontrollers.begin();!(contit==m_linkedcontrollers.end());++contit) + { + (*contit)->UnlinkSensor(this); + } + m_linkedcontrollers.clear(); +} + void SCA_ISensor::UnregisterToManager() { m_eventmgr->RemoveSensor(this); + m_links = 0; } -void SCA_ISensor::Activate(class SCA_LogicManager* logicmgr, CValue* event) +void SCA_ISensor::ActivateControllers(class SCA_LogicManager* logicmgr) +{ + for(vector<SCA_IController*>::const_iterator c= m_linkedcontrollers.begin(); + c!=m_linkedcontrollers.end();++c) + { + SCA_IController* contr = *c; + if (contr->IsActive()) + logicmgr->AddTriggeredController(contr, this); + } +} + +void SCA_ISensor::Activate(class SCA_LogicManager* logicmgr) { // calculate if a __triggering__ is wanted // don't evaluate a sensor that is not connected to any controller if (m_links && !m_suspended) { - bool result = this->Evaluate(event); + bool result = this->Evaluate(); + // store the state for the rest of the logic system + m_prev_state = m_state; + m_state = this->IsPositiveTrigger(); if (result) { - logicmgr->AddActivatedSensor(this); - // reset these counters so that pulse are synchronized with transition - m_pos_ticks = 0; - m_neg_ticks = 0; + // the sensor triggered this frame + if (m_state || !m_tap) { + ActivateControllers(logicmgr); + // reset these counters so that pulse are synchronized with transition + m_pos_ticks = 0; + m_neg_ticks = 0; + } else + { + result = false; + } } else { /* First, the pulsing behaviour, if pulse mode is @@ -172,41 +237,55 @@ void SCA_ISensor::Activate(class SCA_LogicManager* logicmgr, CValue* event) if (m_pos_pulsemode) { m_pos_ticks++; if (m_pos_ticks > m_pulse_frequency) { - if ( this->IsPositiveTrigger() ) + if ( m_state ) { - logicmgr->AddActivatedSensor(this); + ActivateControllers(logicmgr); + result = true; } m_pos_ticks = 0; } } - - if (m_neg_pulsemode) + // negative pulse doesn't make sense in tap mode, skip + if (m_neg_pulsemode && !m_tap) { m_neg_ticks++; if (m_neg_ticks > m_pulse_frequency) { - if (!this->IsPositiveTrigger() ) + if (!m_state ) { - logicmgr->AddActivatedSensor(this); + ActivateControllers(logicmgr); + result = true; } m_neg_ticks = 0; } } } - if (!m_newControllers.empty()) + if (m_tap) { - if (!IsActive() && m_level) + // in tap mode: we send always a negative pulse immediately after a positive pulse + if (!result) { - // This level sensor is connected to at least one controller that was just made - // active but it did not generate an event yet, do it now to those controllers only - for (std::vector<SCA_IController*>::iterator ci=m_newControllers.begin(); - ci != m_newControllers.end(); ci++) + // the sensor did not trigger on this frame + if (m_prev_state) { - logicmgr->AddTriggeredController(*ci, this); + // but it triggered on previous frame => send a negative pulse + ActivateControllers(logicmgr); + result = true; } + // in any case, absence of trigger means sensor off + m_state = false; + } + } + if (!result && m_level) + { + // This level sensor is connected to at least one controller that was just made + // active but it did not generate an event yet, do it now to those controllers only + for(vector<SCA_IController*>::const_iterator c= m_linkedcontrollers.begin(); + c!=m_linkedcontrollers.end();++c) + { + SCA_IController* contr = *c; + if (contr->IsJustActivated()) + logicmgr->AddTriggeredController(contr, this); } - // clear the list. Instead of using clear, which also release the memory, - // use erase, which keeps the memory available for next time. - m_newControllers.erase(m_newControllers.begin(), m_newControllers.end()); } } } @@ -218,17 +297,17 @@ void SCA_ISensor::Activate(class SCA_LogicManager* logicmgr, CValue* event) const char SCA_ISensor::IsPositive_doc[] = "isPositive()\n" "\tReturns whether the sensor is in an active state.\n"; -PyObject* SCA_ISensor::PyIsPositive(PyObject* self) +PyObject* SCA_ISensor::PyIsPositive() { ShowDeprecationWarning("isPositive()", "the read-only positive property"); - int retval = IsPositiveTrigger(); + int retval = GetState(); return PyInt_FromLong(retval); } const char SCA_ISensor::IsTriggered_doc[] = "isTriggered()\n" "\tReturns whether the sensor has triggered the current controller.\n"; -PyObject* SCA_ISensor::PyIsTriggered(PyObject* self) +PyObject* SCA_ISensor::PyIsTriggered() { ShowDeprecationWarning("isTriggered()", "the read-only triggered property"); // check with the current controller @@ -244,7 +323,7 @@ PyObject* SCA_ISensor::PyIsTriggered(PyObject* self) const char SCA_ISensor::GetUsePosPulseMode_doc[] = "getUsePosPulseMode()\n" "\tReturns whether positive pulse mode is active.\n"; -PyObject* SCA_ISensor::PyGetUsePosPulseMode(PyObject* self) +PyObject* SCA_ISensor::PyGetUsePosPulseMode() { ShowDeprecationWarning("getUsePosPulseMode()", "the usePosPulseMode property"); return BoolToPyArg(m_pos_pulsemode); @@ -258,11 +337,11 @@ const char SCA_ISensor::SetUsePosPulseMode_doc[] = "\t - pulse? : Pulse when a positive event occurs?\n" "\t (KX_TRUE, KX_FALSE)\n" "\tSet whether to do pulsing when positive pulses occur.\n"; -PyObject* SCA_ISensor::PySetUsePosPulseMode(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_ISensor::PySetUsePosPulseMode(PyObject* args) { ShowDeprecationWarning("setUsePosPulseMode()", "the usePosPulseMode property"); int pyarg = 0; - if(!PyArg_ParseTuple(args, "i", &pyarg)) { return NULL; } + if(!PyArg_ParseTuple(args, "i:setUsePosPulseMode", &pyarg)) { return NULL; } m_pos_pulsemode = PyArgToBool(pyarg); Py_RETURN_NONE; } @@ -273,7 +352,7 @@ PyObject* SCA_ISensor::PySetUsePosPulseMode(PyObject* self, PyObject* args, PyOb const char SCA_ISensor::GetFrequency_doc[] = "getFrequency()\n" "\tReturns the frequency of the updates in pulse mode.\n" ; -PyObject* SCA_ISensor::PyGetFrequency(PyObject* self) +PyObject* SCA_ISensor::PyGetFrequency() { ShowDeprecationWarning("getFrequency()", "the frequency property"); return PyInt_FromLong(m_pulse_frequency); @@ -287,12 +366,12 @@ const char SCA_ISensor::SetFrequency_doc[] = "\t- pulse_frequency: The frequency of the updates in pulse mode (integer)" "\tSet the frequency of the updates in pulse mode.\n" "\tIf the frequency is negative, it is set to 0.\n" ; -PyObject* SCA_ISensor::PySetFrequency(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_ISensor::PySetFrequency(PyObject* args) { ShowDeprecationWarning("setFrequency()", "the frequency property"); int pulse_frequencyArg = 0; - if(!PyArg_ParseTuple(args, "i", &pulse_frequencyArg)) { + if(!PyArg_ParseTuple(args, "i:setFrequency", &pulse_frequencyArg)) { return NULL; } @@ -310,7 +389,7 @@ PyObject* SCA_ISensor::PySetFrequency(PyObject* self, PyObject* args, PyObject* const char SCA_ISensor::GetInvert_doc[] = "getInvert()\n" "\tReturns whether or not pulses from this sensor are inverted.\n" ; -PyObject* SCA_ISensor::PyGetInvert(PyObject* self) +PyObject* SCA_ISensor::PyGetInvert() { ShowDeprecationWarning("getInvert()", "the invert property"); return BoolToPyArg(m_invert); @@ -320,11 +399,11 @@ const char SCA_ISensor::SetInvert_doc[] = "setInvert(invert?)\n" "\t- invert?: Invert the event-values? (KX_TRUE, KX_FALSE)\n" "\tSet whether to invert pulses.\n"; -PyObject* SCA_ISensor::PySetInvert(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_ISensor::PySetInvert(PyObject* args) { ShowDeprecationWarning("setInvert()", "the invert property"); int pyarg = 0; - if(!PyArg_ParseTuple(args, "i", &pyarg)) { return NULL; } + if(!PyArg_ParseTuple(args, "i:setInvert", &pyarg)) { return NULL; } m_invert = PyArgToBool(pyarg); Py_RETURN_NONE; } @@ -336,7 +415,7 @@ const char SCA_ISensor::GetLevel_doc[] = "\tA level detector will immediately generate a pulse, negative or positive\n" "\tdepending on the sensor condition, as soon as the state is activated.\n" "\tA edge detector will wait for a state change before generating a pulse.\n"; -PyObject* SCA_ISensor::PyGetLevel(PyObject* self) +PyObject* SCA_ISensor::PyGetLevel() { ShowDeprecationWarning("getLevel()", "the level property"); return BoolToPyArg(m_level); @@ -346,11 +425,11 @@ const char SCA_ISensor::SetLevel_doc[] = "setLevel(level?)\n" "\t- level?: Detect level instead of edge? (KX_TRUE, KX_FALSE)\n" "\tSet whether to detect level or edge transition when entering a state.\n"; -PyObject* SCA_ISensor::PySetLevel(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_ISensor::PySetLevel(PyObject* args) { ShowDeprecationWarning("setLevel()", "the level property"); int pyarg = 0; - if(!PyArg_ParseTuple(args, "i", &pyarg)) { return NULL; } + if(!PyArg_ParseTuple(args, "i:setLevel", &pyarg)) { return NULL; } m_level = PyArgToBool(pyarg); Py_RETURN_NONE; } @@ -358,7 +437,7 @@ PyObject* SCA_ISensor::PySetLevel(PyObject* self, PyObject* args, PyObject* kwds const char SCA_ISensor::GetUseNegPulseMode_doc[] = "getUseNegPulseMode()\n" "\tReturns whether negative pulse mode is active.\n"; -PyObject* SCA_ISensor::PyGetUseNegPulseMode(PyObject* self) +PyObject* SCA_ISensor::PyGetUseNegPulseMode() { ShowDeprecationWarning("getUseNegPulseMode()", "the useNegPulseMode property"); return BoolToPyArg(m_neg_pulsemode); @@ -369,11 +448,11 @@ const char SCA_ISensor::SetUseNegPulseMode_doc[] = "\t - pulse? : Pulse when a negative event occurs?\n" "\t (KX_TRUE, KX_FALSE)\n" "\tSet whether to do pulsing when negative pulses occur.\n"; -PyObject* SCA_ISensor::PySetUseNegPulseMode(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_ISensor::PySetUseNegPulseMode(PyObject* args) { ShowDeprecationWarning("setUseNegPulseMode()", "the useNegPulseMode property"); int pyarg = 0; - if(!PyArg_ParseTuple(args, "i", &pyarg)) { return NULL; } + if(!PyArg_ParseTuple(args, "i:setUseNegPulseMode", &pyarg)) { return NULL; } m_neg_pulsemode = PyArgToBool(pyarg); Py_RETURN_NONE; } @@ -385,6 +464,7 @@ KX_PYMETHODDEF_DOC_NOARGS(SCA_ISensor, reset, "\tThe sensor is put in its initial state as if it was just activated.\n") { Init(); + m_prev_state = false; Py_RETURN_NONE; } @@ -393,12 +473,17 @@ KX_PYMETHODDEF_DOC_NOARGS(SCA_ISensor, reset, /* ----------------------------------------------- */ PyTypeObject SCA_ISensor::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_ISensor", - sizeof(SCA_ISensor), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -453,41 +538,59 @@ PyAttributeDef SCA_ISensor::Attributes[] = { KX_PYATTRIBUTE_BOOL_RW("useNegPulseMode",SCA_ISensor,m_neg_pulsemode), KX_PYATTRIBUTE_INT_RW("frequency",0,100000,true,SCA_ISensor,m_pulse_frequency), KX_PYATTRIBUTE_BOOL_RW("invert",SCA_ISensor,m_invert), - KX_PYATTRIBUTE_BOOL_RW("level",SCA_ISensor,m_level), - // make these properties read-only in _setaddr, must still implement them in py_getattro - KX_PYATTRIBUTE_DUMMY("triggered"), - KX_PYATTRIBUTE_DUMMY("positive"), + KX_PYATTRIBUTE_BOOL_RW_CHECK("level",SCA_ISensor,m_level,pyattr_check_level), + KX_PYATTRIBUTE_BOOL_RW_CHECK("tap",SCA_ISensor,m_tap,pyattr_check_tap), + KX_PYATTRIBUTE_RO_FUNCTION("triggered", SCA_ISensor, pyattr_get_triggered), + KX_PYATTRIBUTE_RO_FUNCTION("positive", SCA_ISensor, pyattr_get_positive), + //KX_PYATTRIBUTE_TODO("links"), + //KX_PYATTRIBUTE_TODO("posTicks"), + //KX_PYATTRIBUTE_TODO("negTicks"), { NULL } //Sentinel }; -PyObject* -SCA_ISensor::py_getattro(PyObject *attr) +PyObject* SCA_ISensor::py_getattro(PyObject *attr) { - PyObject* object = py_getattro_self(Attributes, this, attr); - if (object != NULL) - return object; - - char *attr_str= PyString_AsString(attr); - if (!strcmp(attr_str, "triggered")) - { - int retval = 0; - if (SCA_PythonController::m_sCurrentController) - retval = SCA_PythonController::m_sCurrentController->IsTriggered(this); - return PyInt_FromLong(retval); - } - if (!strcmp(attr_str, "positive")) - { - int retval = IsPositiveTrigger(); - return PyInt_FromLong(retval); - } py_getattro_up(SCA_ILogicBrick); } +PyObject* SCA_ISensor::py_getattro_dict() { + py_getattro_dict_up(SCA_ILogicBrick); +} + int SCA_ISensor::py_setattro(PyObject *attr, PyObject *value) { - int ret = py_setattro_self(Attributes, this, attr, value); - if (ret >= 0) - return ret; - return SCA_ILogicBrick::py_setattro(attr, value); + py_setattro_up(SCA_ILogicBrick); +} + +PyObject* SCA_ISensor::pyattr_get_triggered(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + SCA_ISensor* self= static_cast<SCA_ISensor*>(self_v); + int retval = 0; + if (SCA_PythonController::m_sCurrentController) + retval = SCA_PythonController::m_sCurrentController->IsTriggered(self); + return PyInt_FromLong(retval); +} + +PyObject* SCA_ISensor::pyattr_get_positive(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + SCA_ISensor* self= static_cast<SCA_ISensor*>(self_v); + return PyInt_FromLong(self->GetState()); } + +int SCA_ISensor::pyattr_check_level(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + SCA_ISensor* self= static_cast<SCA_ISensor*>(self_v); + if (self->m_level) + self->m_tap = false; + return 0; +} + +int SCA_ISensor::pyattr_check_tap(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + SCA_ISensor* self= static_cast<SCA_ISensor*>(self_v); + if (self->m_tap) + self->m_level = false; + return 0; +} + /* eof */ diff --git a/source/gameengine/GameLogic/SCA_ISensor.h b/source/gameengine/GameLogic/SCA_ISensor.h index ce7b66df1cd..9aeda728caf 100644 --- a/source/gameengine/GameLogic/SCA_ISensor.h +++ b/source/gameengine/GameLogic/SCA_ISensor.h @@ -32,18 +32,22 @@ #ifndef __SCA_ISENSOR #define __SCA_ISENSOR -#include "SCA_ILogicBrick.h" +#include "SCA_IController.h" #include <vector> /** * Interface Class for all logic Sensors. Implements - * pulsemode,pulsefrequency */ + * pulsemode,pulsefrequency + * Use of SG_DList element: link sensors to their respective event manager + * Head: SCA_EventManager::m_sensors + * Use of SG_QList element: not used + */ class SCA_ISensor : public SCA_ILogicBrick { Py_Header; +protected: class SCA_EventManager* m_eventmgr; - bool m_triggered; /** Pulse positive pulses? */ bool m_pos_pulsemode; @@ -66,6 +70,9 @@ class SCA_ISensor : public SCA_ILogicBrick /** detect level instead of edge*/ bool m_level; + /** tap mode */ + bool m_tap; + /** sensor has been reset */ bool m_reset; @@ -75,8 +82,13 @@ class SCA_ISensor : public SCA_ILogicBrick /** number of connections to controller */ int m_links; - /** list of controllers that have just activated this sensor because of a state change */ - std::vector<class SCA_IController*> m_newControllers; + /** current sensor state */ + bool m_state; + + /** previous state (for tap option) */ + bool m_prev_state; + + std::vector<class SCA_IController*> m_linkedcontrollers; public: SCA_ISensor(SCA_IObject* gameobj, @@ -89,8 +101,8 @@ public: /* an implementation on this level. It requires an evaluate on the lower */ /* level of individual sensors. Mapping the old activate()s is easy. */ /* The IsPosTrig() also has to change, to keep things consistent. */ - void Activate(class SCA_LogicManager* logicmgr,CValue* event); - virtual bool Evaluate(CValue* event) = 0; + void Activate(class SCA_LogicManager* logicmgr); + virtual bool Evaluate() = 0; virtual bool IsPositiveTrigger(); virtual void Init(); @@ -109,11 +121,22 @@ public: void SetInvert(bool inv); /** set the level detection on or off */ void SetLevel(bool lvl); + void SetTap(bool tap); virtual void RegisterToManager(); virtual void UnregisterToManager(); + void ReserveController(int num) + { + m_linkedcontrollers.reserve(num); + } + void LinkToController(SCA_IController* controller); + void UnlinkController(SCA_IController* controller); + void UnlinkAllControllers(); + void ActivateControllers(class SCA_LogicManager* logicmgr); - virtual float GetNumber(); + virtual void ProcessReplica(); + + virtual double GetNumber(); /** Stop sensing for a while. */ void Suspend(); @@ -121,11 +144,15 @@ public: /** Is this sensor switched off? */ bool IsSuspended(); + /** get the state of the sensor: positive or negative */ + bool GetState() + { + return m_state; + } + /** Resume sensing. */ void Resume(); - void AddNewController(class SCA_IController* controller) - { m_newControllers.push_back(controller); } void ClrLink() { m_links = 0; } void IncLink() @@ -137,23 +164,29 @@ public: /* Python functions: */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual int py_setattro(PyObject *attr, PyObject *value); //Deprecated functions -----> KX_PYMETHOD_DOC_NOARGS(SCA_ISensor,IsPositive); KX_PYMETHOD_DOC_NOARGS(SCA_ISensor,IsTriggered); KX_PYMETHOD_DOC_NOARGS(SCA_ISensor,GetUsePosPulseMode); - KX_PYMETHOD_DOC(SCA_ISensor,SetUsePosPulseMode); + KX_PYMETHOD_DOC_VARARGS(SCA_ISensor,SetUsePosPulseMode); KX_PYMETHOD_DOC_NOARGS(SCA_ISensor,GetFrequency); - KX_PYMETHOD_DOC(SCA_ISensor,SetFrequency); + KX_PYMETHOD_DOC_VARARGS(SCA_ISensor,SetFrequency); KX_PYMETHOD_DOC_NOARGS(SCA_ISensor,GetUseNegPulseMode); - KX_PYMETHOD_DOC(SCA_ISensor,SetUseNegPulseMode); + KX_PYMETHOD_DOC_VARARGS(SCA_ISensor,SetUseNegPulseMode); KX_PYMETHOD_DOC_NOARGS(SCA_ISensor,GetInvert); - KX_PYMETHOD_DOC(SCA_ISensor,SetInvert); + KX_PYMETHOD_DOC_VARARGS(SCA_ISensor,SetInvert); KX_PYMETHOD_DOC_NOARGS(SCA_ISensor,GetLevel); - KX_PYMETHOD_DOC(SCA_ISensor,SetLevel); + KX_PYMETHOD_DOC_VARARGS(SCA_ISensor,SetLevel); //<------ KX_PYMETHOD_DOC_NOARGS(SCA_ISensor,reset); + + static PyObject* pyattr_get_triggered(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); + static PyObject* pyattr_get_positive(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); + static int pyattr_check_level(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); + static int pyattr_check_tap(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); }; #endif //__SCA_ISENSOR diff --git a/source/gameengine/GameLogic/SCA_JoystickManager.cpp b/source/gameengine/GameLogic/SCA_JoystickManager.cpp index f3ce549a637..ff8f3b1c81f 100644 --- a/source/gameengine/GameLogic/SCA_JoystickManager.cpp +++ b/source/gameengine/GameLogic/SCA_JoystickManager.cpp @@ -59,20 +59,21 @@ SCA_JoystickManager::~SCA_JoystickManager() void SCA_JoystickManager::NextFrame(double curtime,double deltatime) { - if (m_sensors.size()==0) { + if (m_sensors.Empty()) { return; } else { - set<SCA_ISensor*>::iterator it; + ; #ifndef DISABLE_SDL SCA_Joystick::HandleEvents(); /* Handle all SDL Joystick events */ #endif - for (it = m_sensors.begin(); it != m_sensors.end(); it++) + SG_DList::iterator<SCA_JoystickSensor> it(m_sensors); + for (it.begin();!it.end();++it) { - SCA_JoystickSensor* joysensor = (SCA_JoystickSensor*)(*it); + SCA_JoystickSensor* joysensor = *it; if(!joysensor->IsSuspended()) { - joysensor->Activate(m_logicmgr, NULL); + joysensor->Activate(m_logicmgr); } } } diff --git a/source/gameengine/GameLogic/SCA_JoystickSensor.cpp b/source/gameengine/GameLogic/SCA_JoystickSensor.cpp index 0cfd6843c1b..906d454b728 100644 --- a/source/gameengine/GameLogic/SCA_JoystickSensor.cpp +++ b/source/gameengine/GameLogic/SCA_JoystickSensor.cpp @@ -87,7 +87,7 @@ CValue* SCA_JoystickSensor::GetReplica() { SCA_JoystickSensor* replica = new SCA_JoystickSensor(*this); // this will copy properties and so on... - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); replica->Init(); return replica; } @@ -102,7 +102,7 @@ bool SCA_JoystickSensor::IsPositiveTrigger() } -bool SCA_JoystickSensor::Evaluate(CValue* event) +bool SCA_JoystickSensor::Evaluate() { SCA_Joystick *js = m_pJoystickMgr->GetJoystickDevice(m_joyindex); bool result = false; @@ -117,11 +117,15 @@ bool SCA_JoystickSensor::Evaluate(CValue* event) case KX_JOYSENSORMODE_AXIS: { /* what is what! - m_axisf == 0 == right + m_axisf == JOYAXIS_RIGHT, JOYAXIS_UP, JOYAXIS_DOWN, JOYAXIS_LEFT m_axisf == 1 == up m_axisf == 2 == left m_axisf == 3 == down - numberof== m_axis -- max 2 + + numberof== m_axis (1-4), range is half of JOYAXIS_MAX since + it assumes the axis joysticks are axis parirs (0,1), (2,3), etc + also note that this starts at 1 where functions its used + with expect a zero index. */ if (!js->IsTrigAxis() && !reset) /* No events from SDL? - dont bother */ @@ -129,7 +133,7 @@ bool SCA_JoystickSensor::Evaluate(CValue* event) js->cSetPrecision(m_precision); if (m_bAllEvents) { - if(js->aAnyAxisIsPositive(m_axis)){ + if(js->aAxisPairIsPositive(m_axis-1)){ /* use zero based axis index internally */ m_istrig = 1; result = true; }else{ @@ -139,8 +143,8 @@ bool SCA_JoystickSensor::Evaluate(CValue* event) } } } - else if(m_axisf == 1){ - if(js->aUpAxisIsPositive(m_axis)){ + else { + if(js->aAxisPairDirectionIsPositive(m_axis-1, m_axisf)){ /* use zero based axis index internally */ m_istrig = 1; result = true; }else{ @@ -150,41 +154,28 @@ bool SCA_JoystickSensor::Evaluate(CValue* event) } } } - else if(m_axisf == 3){ - if(js->aDownAxisIsPositive(m_axis)){ - m_istrig = 1; - result = true; - }else{ - if(m_istrig){ - m_istrig = 0; - result = true; - } - } - } - else if(m_axisf == 2){ - if(js->aLeftAxisIsPositive(m_axis)){ - m_istrig = 1; - result = true; - }else{ - if(m_istrig){ - m_istrig = 0; - result = true; - } - } - } - else if(m_axisf == 0){ - if(js->aRightAxisIsPositive(m_axis)){ - m_istrig = 1; + break; + } + case KX_JOYSENSORMODE_AXIS_SINGLE: + { + /* Like KX_JOYSENSORMODE_AXIS but dont pair up axis */ + if (!js->IsTrigAxis() && !reset) /* No events from SDL? - dont bother */ + return false; + + /* No need for 'm_bAllEvents' check here since were only checking 1 axis */ + js->cSetPrecision(m_precision); + if(js->aAxisIsPositive(m_axis-1)){ /* use zero based axis index internally */ + m_istrig = 1; + result = true; + }else{ + if(m_istrig){ + m_istrig = 0; result = true; - }else{ - if(m_istrig){ - m_istrig = 0; - result = true; - } } } break; } + case KX_JOYSENSORMODE_BUTTON: { /* what is what! @@ -275,12 +266,17 @@ bool SCA_JoystickSensor::isValid(SCA_JoystickSensor::KX_JOYSENSORMODE m) /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_JoystickSensor::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_JoystickSensor", - sizeof(SCA_JoystickSensor), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -333,50 +329,28 @@ PyAttributeDef SCA_JoystickSensor::Attributes[] = { KX_PYATTRIBUTE_INT_RW("button",0,100,false,SCA_JoystickSensor,m_button), KX_PYATTRIBUTE_INT_LIST_RW_CHECK("axis",0,3,true,SCA_JoystickSensor,m_axis,2,CheckAxis), KX_PYATTRIBUTE_INT_LIST_RW_CHECK("hat",0,12,true,SCA_JoystickSensor,m_hat,2,CheckHat), - // dummy attributes will just be read-only in py_setattro - // you still need to defined them in py_getattro - KX_PYATTRIBUTE_DUMMY("axisPosition"), - KX_PYATTRIBUTE_DUMMY("numAxis"), - KX_PYATTRIBUTE_DUMMY("numButtons"), - KX_PYATTRIBUTE_DUMMY("numHats"), - KX_PYATTRIBUTE_DUMMY("connected"), + KX_PYATTRIBUTE_RO_FUNCTION("axisValues", SCA_JoystickSensor, pyattr_get_axis_values), + KX_PYATTRIBUTE_RO_FUNCTION("axisSingle", SCA_JoystickSensor, pyattr_get_axis_single), + KX_PYATTRIBUTE_RO_FUNCTION("numAxis", SCA_JoystickSensor, pyattr_get_num_axis), + KX_PYATTRIBUTE_RO_FUNCTION("numButtons", SCA_JoystickSensor, pyattr_get_num_buttons), + KX_PYATTRIBUTE_RO_FUNCTION("numHats", SCA_JoystickSensor, pyattr_get_num_hats), + KX_PYATTRIBUTE_RO_FUNCTION("connected", SCA_JoystickSensor, pyattr_get_connected), + //KX_PYATTRIBUTE_TODO("events"), { NULL } //Sentinel }; -PyObject* SCA_JoystickSensor::py_getattro(PyObject *attr) { - SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); - char *attr_str= PyString_AsString(attr); - - if (!strcmp(attr_str, "axisPosition")) { - if(joy) - return Py_BuildValue("[iiii]", joy->GetAxis10(), joy->GetAxis11(), joy->GetAxis20(), joy->GetAxis21()); - else - return Py_BuildValue("[iiii]", 0, 0, 0, 0); - } - if (!strcmp(attr_str, "numAxis")) { - return PyInt_FromLong( joy ? joy->GetNumberOfAxes() : 0 ); - } - if (!strcmp(attr_str, "numButtons")) { - return PyInt_FromLong( joy ? joy->GetNumberOfButtons() : 0 ); - } - if (!strcmp(attr_str, "numHats")) { - return PyInt_FromLong( joy ? joy->GetNumberOfHats() : 0 ); - } - if (!strcmp(attr_str, "connected")) { - return PyBool_FromLong( joy ? joy->Connected() : 0 ); - } - PyObject* object = py_getattro_self(Attributes, this, attr); - if (object != NULL) - return object; +PyObject* SCA_JoystickSensor::py_getattro(PyObject *attr) +{ py_getattro_up(SCA_ISensor); } +PyObject* SCA_JoystickSensor::py_getattro_dict() { + py_getattro_dict_up(SCA_ISensor); +} + int SCA_JoystickSensor::py_setattro(PyObject *attr, PyObject *value) { - int ret = py_setattro_self(Attributes, this, attr, value); - if (ret >= 0) - return ret; - return SCA_ISensor::py_setattro(attr, value); + py_setattro_up(SCA_ISensor); } @@ -384,7 +358,7 @@ int SCA_JoystickSensor::py_setattro(PyObject *attr, PyObject *value) const char SCA_JoystickSensor::GetIndex_doc[] = "getIndex\n" "\tReturns the joystick index to use.\n"; -PyObject* SCA_JoystickSensor::PyGetIndex( PyObject* self ) { +PyObject* SCA_JoystickSensor::PyGetIndex( ) { ShowDeprecationWarning("getIndex()", "the index property"); return PyInt_FromLong(m_joyindex); } @@ -394,7 +368,7 @@ PyObject* SCA_JoystickSensor::PyGetIndex( PyObject* self ) { const char SCA_JoystickSensor::SetIndex_doc[] = "setIndex\n" "\tSets the joystick index to use.\n"; -PyObject* SCA_JoystickSensor::PySetIndex( PyObject* self, PyObject* value ) { +PyObject* SCA_JoystickSensor::PySetIndex( PyObject* value ) { ShowDeprecationWarning("setIndex()", "the index property"); int index = PyInt_AsLong( value ); /* -1 on error, will raise an error in this case */ if (index < 0 || index >= JOYINDEX_MAX) { @@ -410,7 +384,7 @@ PyObject* SCA_JoystickSensor::PySetIndex( PyObject* self, PyObject* value ) { const char SCA_JoystickSensor::GetAxis_doc[] = "getAxis\n" "\tReturns the current axis this sensor reacts to.\n"; -PyObject* SCA_JoystickSensor::PyGetAxis( PyObject* self) { +PyObject* SCA_JoystickSensor::PyGetAxis( ) { ShowDeprecationWarning("getAxis()", "the axis property"); return Py_BuildValue("[ii]",m_axis, m_axisf); } @@ -420,11 +394,11 @@ PyObject* SCA_JoystickSensor::PyGetAxis( PyObject* self) { const char SCA_JoystickSensor::SetAxis_doc[] = "setAxis\n" "\tSets the current axis this sensor reacts to.\n"; -PyObject* SCA_JoystickSensor::PySetAxis( PyObject* self, PyObject* args ) { +PyObject* SCA_JoystickSensor::PySetAxis( PyObject* args ) { ShowDeprecationWarning("setAxis()", "the axis property"); int axis,axisflag; - if(!PyArg_ParseTuple(args, "ii", &axis, &axisflag)){ + if(!PyArg_ParseTuple(args, "ii:setAxis", &axis, &axisflag)){ return NULL; } m_axis = axis; @@ -437,13 +411,18 @@ PyObject* SCA_JoystickSensor::PySetAxis( PyObject* self, PyObject* args ) { const char SCA_JoystickSensor::GetAxisValue_doc[] = "getAxisValue\n" "\tReturns a list of the values for the current state of each axis.\n"; -PyObject* SCA_JoystickSensor::PyGetAxisValue( PyObject* self) { +PyObject* SCA_JoystickSensor::PyGetAxisValue( ) { ShowDeprecationWarning("getAxisValue()", "the axisPosition property"); SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); - if(joy) - return Py_BuildValue("[iiii]", joy->GetAxis10(), joy->GetAxis11(), joy->GetAxis20(), joy->GetAxis21()); - else - return Py_BuildValue("[iiii]", 0, 0, 0, 0); + + int axis_index= joy->GetNumberOfAxes(); + PyObject *list= PyList_New(axis_index); + + while(axis_index--) { + PyList_SET_ITEM(list, axis_index, PyInt_FromLong(joy->GetAxisPosition(axis_index))); + } + + return list; } @@ -451,7 +430,7 @@ PyObject* SCA_JoystickSensor::PyGetAxisValue( PyObject* self) { const char SCA_JoystickSensor::GetThreshold_doc[] = "getThreshold\n" "\tReturns the threshold of the axis.\n"; -PyObject* SCA_JoystickSensor::PyGetThreshold( PyObject* self) { +PyObject* SCA_JoystickSensor::PyGetThreshold( ) { ShowDeprecationWarning("getThreshold()", "the threshold property"); return PyInt_FromLong(m_precision); } @@ -461,10 +440,10 @@ PyObject* SCA_JoystickSensor::PyGetThreshold( PyObject* self) { const char SCA_JoystickSensor::SetThreshold_doc[] = "setThreshold\n" "\tSets the threshold of the axis.\n"; -PyObject* SCA_JoystickSensor::PySetThreshold( PyObject* self, PyObject* args ) { +PyObject* SCA_JoystickSensor::PySetThreshold( PyObject* args ) { ShowDeprecationWarning("setThreshold()", "the threshold property"); int thresh; - if(!PyArg_ParseTuple(args, "i", &thresh)){ + if(!PyArg_ParseTuple(args, "i:setThreshold", &thresh)){ return NULL; } m_precision = thresh; @@ -475,7 +454,7 @@ PyObject* SCA_JoystickSensor::PySetThreshold( PyObject* self, PyObject* args ) { const char SCA_JoystickSensor::GetButton_doc[] = "getButton\n" "\tReturns the current button this sensor is checking.\n"; -PyObject* SCA_JoystickSensor::PyGetButton( PyObject* self) { +PyObject* SCA_JoystickSensor::PyGetButton( ) { ShowDeprecationWarning("getButton()", "the button property"); return PyInt_FromLong(m_button); } @@ -484,7 +463,7 @@ PyObject* SCA_JoystickSensor::PyGetButton( PyObject* self) { const char SCA_JoystickSensor::SetButton_doc[] = "setButton\n" "\tSets the button the sensor reacts to.\n"; -PyObject* SCA_JoystickSensor::PySetButton( PyObject* self, PyObject* value ) { +PyObject* SCA_JoystickSensor::PySetButton( PyObject* value ) { ShowDeprecationWarning("setButton()", "the button property"); int button = PyInt_AsLong(value); if(button==-1 && PyErr_Occurred()) { @@ -499,16 +478,16 @@ PyObject* SCA_JoystickSensor::PySetButton( PyObject* self, PyObject* value ) { const char SCA_JoystickSensor::GetButtonValue_doc[] = "getButtonValue\n" "\tReturns a list containing the indicies of the current pressed state of each button.\n"; -PyObject* SCA_JoystickSensor::PyGetButtonValue( PyObject* self) { +PyObject* SCA_JoystickSensor::PyGetButtonValue( ) { ShowDeprecationWarning("getButtonValue()", "getButtonActiveList"); - return PyGetButtonActiveList(self); + return PyGetButtonActiveList( ); } /* get button active list -------------------------------------------------- */ const char SCA_JoystickSensor::GetButtonActiveList_doc[] = "getButtonActiveList\n" "\tReturns a list containing the indicies of the button currently pressed.\n"; -PyObject* SCA_JoystickSensor::PyGetButtonActiveList( PyObject* self) { +PyObject* SCA_JoystickSensor::PyGetButtonActiveList( ) { SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); PyObject *ls = PyList_New(0); PyObject *value; @@ -530,12 +509,11 @@ PyObject* SCA_JoystickSensor::PyGetButtonActiveList( PyObject* self) { const char SCA_JoystickSensor::GetButtonStatus_doc[] = "getButtonStatus(buttonIndex)\n" "\tReturns a bool of the current pressed state of the specified button.\n"; -PyObject* SCA_JoystickSensor::PyGetButtonStatus( PyObject* self, PyObject* args ) { +PyObject* SCA_JoystickSensor::PyGetButtonStatus( PyObject* args ) { SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); - PyObject *value; int index; - if(!PyArg_ParseTuple(args, "i", &index)){ + if(!PyArg_ParseTuple(args, "i:getButtonStatus", &index)){ return NULL; } if(joy && index >= 0 && index < joy->GetNumberOfButtons()) { @@ -548,7 +526,7 @@ PyObject* SCA_JoystickSensor::PyGetButtonStatus( PyObject* self, PyObject* args const char SCA_JoystickSensor::GetHat_doc[] = "getHat\n" "\tReturns the current direction of the hat.\n"; -PyObject* SCA_JoystickSensor::PyGetHat( PyObject* self ) { +PyObject* SCA_JoystickSensor::PyGetHat( ) { ShowDeprecationWarning("getHat()", "the hat property"); return Py_BuildValue("[ii]",m_hat, m_hatf); } @@ -558,10 +536,10 @@ PyObject* SCA_JoystickSensor::PyGetHat( PyObject* self ) { const char SCA_JoystickSensor::SetHat_doc[] = "setHat\n" "\tSets the hat the sensor reacts to.\n"; -PyObject* SCA_JoystickSensor::PySetHat( PyObject* self, PyObject* args ) { +PyObject* SCA_JoystickSensor::PySetHat( PyObject* args ) { ShowDeprecationWarning("setHat()", "the hat property"); int hat,hatflag; - if(!PyArg_ParseTuple(args, "ii", &hat, &hatflag)){ + if(!PyArg_ParseTuple(args, "ii:setHat", &hat, &hatflag)){ return NULL; } m_hat = hat; @@ -574,7 +552,7 @@ PyObject* SCA_JoystickSensor::PySetHat( PyObject* self, PyObject* args ) { const char SCA_JoystickSensor::NumberOfAxes_doc[] = "getNumAxes\n" "\tReturns the number of axes .\n"; -PyObject* SCA_JoystickSensor::PyNumberOfAxes( PyObject* self ) { +PyObject* SCA_JoystickSensor::PyNumberOfAxes( ) { ShowDeprecationWarning("getNumAxes()", "the numAxis property"); SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); // when the joystick is null their is 0 exis still. dumb but scripters should use isConnected() @@ -585,7 +563,7 @@ PyObject* SCA_JoystickSensor::PyNumberOfAxes( PyObject* self ) { const char SCA_JoystickSensor::NumberOfButtons_doc[] = "getNumButtons\n" "\tReturns the number of buttons .\n"; -PyObject* SCA_JoystickSensor::PyNumberOfButtons( PyObject* self ) { +PyObject* SCA_JoystickSensor::PyNumberOfButtons( ) { ShowDeprecationWarning("getNumButtons()", "the numButtons property"); SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); return PyInt_FromLong( joy ? joy->GetNumberOfButtons() : 0 ); @@ -595,7 +573,7 @@ PyObject* SCA_JoystickSensor::PyNumberOfButtons( PyObject* self ) { const char SCA_JoystickSensor::NumberOfHats_doc[] = "getNumHats\n" "\tReturns the number of hats .\n"; -PyObject* SCA_JoystickSensor::PyNumberOfHats( PyObject* self ) { +PyObject* SCA_JoystickSensor::PyNumberOfHats( ) { ShowDeprecationWarning("getNumHats()", "the numHats property"); SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); return PyInt_FromLong( joy ? joy->GetNumberOfHats() : 0 ); @@ -604,8 +582,65 @@ PyObject* SCA_JoystickSensor::PyNumberOfHats( PyObject* self ) { const char SCA_JoystickSensor::Connected_doc[] = "getConnected\n" "\tReturns True if a joystick is connected at this joysticks index.\n"; -PyObject* SCA_JoystickSensor::PyConnected( PyObject* self ) { +PyObject* SCA_JoystickSensor::PyConnected( ) { ShowDeprecationWarning("getConnected()", "the connected property"); SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); return PyBool_FromLong( joy ? joy->Connected() : 0 ); } + + +PyObject* SCA_JoystickSensor::pyattr_get_axis_values(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v); + SCA_Joystick *joy = self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex); + + int axis_index= joy->GetNumberOfAxes(); + PyObject *list= PyList_New(axis_index); + + while(axis_index--) { + PyList_SET_ITEM(list, axis_index, PyInt_FromLong(joy->GetAxisPosition(axis_index))); + } + + return list; +} + +PyObject* SCA_JoystickSensor::pyattr_get_axis_single(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v); + SCA_Joystick *joy = self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex); + + if(self->m_joymode != KX_JOYSENSORMODE_AXIS_SINGLE) { + PyErr_SetString(PyExc_TypeError, "val = sensor.axisSingle: Joystick Sensor, not 'Single Axis' type"); + return NULL; + } + + return PyInt_FromLong(joy->GetAxisPosition(self->m_axis-1)); +} + +PyObject* SCA_JoystickSensor::pyattr_get_num_axis(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v); + SCA_Joystick *joy = self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex); + return PyInt_FromLong( joy ? joy->GetNumberOfAxes() : 0 ); +} + +PyObject* SCA_JoystickSensor::pyattr_get_num_buttons(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v); + SCA_Joystick *joy = self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex); + return PyInt_FromLong( joy ? joy->GetNumberOfButtons() : 0 ); +} + +PyObject* SCA_JoystickSensor::pyattr_get_num_hats(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v); + SCA_Joystick *joy = self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex); + return PyInt_FromLong( joy ? joy->GetNumberOfHats() : 0 ); +} + +PyObject* SCA_JoystickSensor::pyattr_get_connected(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v); + SCA_Joystick *joy = self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex); + return PyBool_FromLong( joy ? joy->Connected() : 0 ); +} diff --git a/source/gameengine/GameLogic/SCA_JoystickSensor.h b/source/gameengine/GameLogic/SCA_JoystickSensor.h index ccdd2107b21..e8185d1911e 100644 --- a/source/gameengine/GameLogic/SCA_JoystickSensor.h +++ b/source/gameengine/GameLogic/SCA_JoystickSensor.h @@ -93,6 +93,7 @@ class SCA_JoystickSensor :public SCA_ISensor KX_JOYSENSORMODE_AXIS, KX_JOYSENSORMODE_BUTTON, KX_JOYSENSORMODE_HAT, + KX_JOYSENSORMODE_AXIS_SINGLE, KX_JOYSENSORMODE_MAX }; bool isValid(KX_JOYSENSORMODE); @@ -109,7 +110,7 @@ public: virtual ~SCA_JoystickSensor(); virtual CValue* GetReplica(); - virtual bool Evaluate(CValue* event); + virtual bool Evaluate(); virtual bool IsPositiveTrigger(); virtual void Init(); @@ -122,6 +123,7 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual int py_setattro(PyObject *attr, PyObject *value); /* Joystick Index */ @@ -148,6 +150,14 @@ public: KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,NumberOfHats); KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,Connected); + static PyObject* pyattr_get_axis_values(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); + static PyObject* pyattr_get_axis_single(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); + static PyObject* pyattr_get_num_axis(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); + static PyObject* pyattr_get_num_buttons(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); + static PyObject* pyattr_get_num_hats(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); + static PyObject* pyattr_get_connected(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); + + /* attribute check */ static int CheckAxis(void *self, const PyAttributeDef*) { diff --git a/source/gameengine/GameLogic/SCA_KeyboardManager.cpp b/source/gameengine/GameLogic/SCA_KeyboardManager.cpp index 6a96442b124..279adab94d8 100644 --- a/source/gameengine/GameLogic/SCA_KeyboardManager.cpp +++ b/source/gameengine/GameLogic/SCA_KeyboardManager.cpp @@ -62,12 +62,11 @@ void SCA_KeyboardManager::NextFrame() { //const SCA_InputEvent& event = GetEventValue(SCA_IInputDevice::KX_EnumInputs inputcode)=0; // cerr << "SCA_KeyboardManager::NextFrame"<< endl; - set<SCA_ISensor*>::iterator it; - for (it=m_sensors.begin(); it != m_sensors.end(); it++) + SG_DList::iterator<SCA_ISensor> it(m_sensors); + for (it.begin();!it.end();++it) { - (*it)->Activate(m_logicmanager,NULL); + (*it)->Activate(m_logicmanager); } - } bool SCA_KeyboardManager::IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode) diff --git a/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp b/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp index fc1b5be3540..f8ee8ed8b41 100644 --- a/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp +++ b/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp @@ -85,7 +85,7 @@ CValue* SCA_KeyboardSensor::GetReplica() { SCA_KeyboardSensor* replica = new SCA_KeyboardSensor(*this); // this will copy properties and so on... - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); replica->Init(); return replica; } @@ -118,7 +118,7 @@ bool SCA_KeyboardSensor::TriggerOnAllKeys() -bool SCA_KeyboardSensor::Evaluate(CValue* eventval) +bool SCA_KeyboardSensor::Evaluate() { bool result = false; bool reset = m_reset && m_level; @@ -148,7 +148,7 @@ bool SCA_KeyboardSensor::Evaluate(CValue* eventval) bool justreleased = false; bool active = false; - for (int i=SCA_IInputDevice::KX_BEGINKEY ; i< SCA_IInputDevice::KX_ENDKEY;i++) + for (int i=SCA_IInputDevice::KX_BEGINKEY ; i<= SCA_IInputDevice::KX_ENDKEY;i++) { const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i); switch (inevent.m_status) @@ -195,6 +195,9 @@ bool SCA_KeyboardSensor::Evaluate(CValue* eventval) } } } + if (m_tap) + // special case for tap mode: only generate event for new activation + result = false; } @@ -352,149 +355,8 @@ void SCA_KeyboardSensor::AddToTargetProp(int keyIndex) } } - -/** - * Determine whether this character can be printed. We cannot use - * the library functions here, because we need to test our own - * keycodes. */ -bool SCA_KeyboardSensor::IsPrintable(int keyIndex) -{ - /* only print - * - numerals: KX_ZEROKEY to KX_NINEKEY - * - alphas: KX_AKEY to KX_ZKEY. - * - specials: KX_RETKEY, KX_PADASTERKEY, KX_PADCOMMAKEY to KX_PERIODKEY, - * KX_TABKEY , KX_SEMICOLONKEY to KX_RIGHTBRACKETKEY, - * KX_PAD2 to KX_PADPLUSKEY - * - delete and backspace: also printable in the sense that they modify - * the string - * - retkey: should this be printable? - * - virgule: prints a space... don't know which key that's supposed - * to be... - */ - if ( ((keyIndex >= SCA_IInputDevice::KX_ZEROKEY) - && (keyIndex <= SCA_IInputDevice::KX_NINEKEY)) - || ((keyIndex >= SCA_IInputDevice::KX_AKEY) - && (keyIndex <= SCA_IInputDevice::KX_ZKEY)) - || (keyIndex == SCA_IInputDevice::KX_SPACEKEY) -/* || (keyIndex == KX_RETKEY) */ - || (keyIndex == SCA_IInputDevice::KX_PADASTERKEY) - || (keyIndex == SCA_IInputDevice::KX_TABKEY) - || ((keyIndex >= SCA_IInputDevice::KX_COMMAKEY) - && (keyIndex <= SCA_IInputDevice::KX_PERIODKEY)) - || ((keyIndex >= SCA_IInputDevice::KX_SEMICOLONKEY) - && (keyIndex <= SCA_IInputDevice::KX_RIGHTBRACKETKEY)) - || ((keyIndex >= SCA_IInputDevice::KX_PAD2) - && (keyIndex <= SCA_IInputDevice::KX_PADPLUSKEY)) - || (keyIndex == SCA_IInputDevice::KX_DELKEY) - || (keyIndex == SCA_IInputDevice::KX_BACKSPACEKEY) - ) - { - return true; - } else { - return false; - } -} - -// this code looks ugly, please use an ordinary hashtable - -char SCA_KeyboardSensor::ToCharacter(int keyIndex, bool shifted) -{ - /* numerals */ - if ( (keyIndex >= SCA_IInputDevice::KX_ZEROKEY) - && (keyIndex <= SCA_IInputDevice::KX_NINEKEY) ) { - if (shifted) { - char numshift[] = ")!@#$%^&*("; - return numshift[keyIndex - '0']; - } else { - return keyIndex - SCA_IInputDevice::KX_ZEROKEY + '0'; - } - } - - /* letters... always lowercase... is that desirable? */ - if ( (keyIndex >= SCA_IInputDevice::KX_AKEY) - && (keyIndex <= SCA_IInputDevice::KX_ZKEY) ) { - if (shifted) { - return keyIndex - SCA_IInputDevice::KX_AKEY + 'A'; - } else { - return keyIndex - SCA_IInputDevice::KX_AKEY + 'a'; - } - } - - if (keyIndex == SCA_IInputDevice::KX_SPACEKEY) { - return ' '; - } - -/* || (keyIndex == SCA_IInputDevice::KX_RETKEY) */ - - if (keyIndex == SCA_IInputDevice::KX_PADASTERKEY) { - return '*'; - } - - if (keyIndex == SCA_IInputDevice::KX_TABKEY) { - return '\t'; - } - - /* comma to period */ - char commatoperiod[] = ",-."; - char commatoperiodshifted[] = "<_>"; - if (keyIndex == SCA_IInputDevice::KX_COMMAKEY) { - if (shifted) { - return commatoperiodshifted[0]; - } else { - return commatoperiod[0]; - } - } - if (keyIndex == SCA_IInputDevice::KX_MINUSKEY) { - if (shifted) { - return commatoperiodshifted[1]; - } else { - return commatoperiod[1]; - } - } - if (keyIndex == SCA_IInputDevice::KX_PERIODKEY) { - if (shifted) { - return commatoperiodshifted[2]; - } else { - return commatoperiod[2]; - } - } - - /* semicolon to rightbracket */ - char semicolontorightbracket[] = ";\'` /\\=[]"; - char semicolontorightbracketshifted[] = ":\"~ \?|+{}"; - if ((keyIndex >= SCA_IInputDevice::KX_SEMICOLONKEY) - && (keyIndex <= SCA_IInputDevice::KX_RIGHTBRACKETKEY)) { - if (shifted) { - return semicolontorightbracketshifted[keyIndex - SCA_IInputDevice::KX_SEMICOLONKEY]; - } else { - return semicolontorightbracket[keyIndex - SCA_IInputDevice::KX_SEMICOLONKEY]; - } - } - - /* keypad2 to padplus */ - char pad2topadplus[] = "246813579. 0- +"; - if ((keyIndex >= SCA_IInputDevice::KX_PAD2) - && (keyIndex <= SCA_IInputDevice::KX_PADPLUSKEY)) { - return pad2topadplus[keyIndex - SCA_IInputDevice::KX_PAD2]; - } - - return '!'; -} /** - * Tests whether this is a delete key. - */ -bool SCA_KeyboardSensor::IsDelete(int keyIndex) -{ - if ( (keyIndex == SCA_IInputDevice::KX_DELKEY) - || (keyIndex == SCA_IInputDevice::KX_BACKSPACEKEY) ) { - return true; - } else { - return false; - } -} - -/** * Tests whether shift is pressed */ bool SCA_KeyboardSensor::IsShifted(void) @@ -528,7 +390,7 @@ void SCA_KeyboardSensor::LogKeystrokes(void) int index = 0; /* Check on all keys whether they were pushed. This does not * untangle the ordering, so don't type too fast :) */ - for (int i=SCA_IInputDevice::KX_BEGINKEY ; i< SCA_IInputDevice::KX_ENDKEY;i++) + for (int i=SCA_IInputDevice::KX_BEGINKEY ; i<= SCA_IInputDevice::KX_ENDKEY;i++) { const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i); if (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED) //NO_INPUTSTATUS) @@ -553,7 +415,7 @@ void SCA_KeyboardSensor::LogKeystrokes(void) const char SCA_KeyboardSensor::GetKey_doc[] = "getKey()\n" "\tReturn the code of the key this sensor is listening to.\n" ; -PyObject* SCA_KeyboardSensor::PyGetKey(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_KeyboardSensor::PyGetKey() { ShowDeprecationWarning("getKey()", "the key property"); return PyInt_FromLong(m_hotkey); @@ -564,12 +426,12 @@ const char SCA_KeyboardSensor::SetKey_doc[] = "setKey(keycode)\n" "\t- keycode: any code from GameKeys\n" "\tSet the key this sensor should listen to.\n" ; -PyObject* SCA_KeyboardSensor::PySetKey(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_KeyboardSensor::PySetKey(PyObject* args) { ShowDeprecationWarning("setKey()", "the key property"); int keyCode; - if(!PyArg_ParseTuple(args, "i", &keyCode)) { + if(!PyArg_ParseTuple(args, "i:setKey", &keyCode)) { return NULL; } @@ -585,7 +447,7 @@ const char SCA_KeyboardSensor::GetHold1_doc[] = "getHold1()\n" "\tReturn the code of the first key modifier to the key this \n" "\tsensor is listening to.\n" ; -PyObject* SCA_KeyboardSensor::PyGetHold1(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_KeyboardSensor::PyGetHold1() { ShowDeprecationWarning("getHold1()", "the hold1 property"); return PyInt_FromLong(m_qual); @@ -596,12 +458,12 @@ const char SCA_KeyboardSensor::SetHold1_doc[] = "setHold1(keycode)\n" "\t- keycode: any code from GameKeys\n" "\tSet the first modifier to the key this sensor should listen to.\n" ; -PyObject* SCA_KeyboardSensor::PySetHold1(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_KeyboardSensor::PySetHold1(PyObject* args) { ShowDeprecationWarning("setHold1()", "the hold1 property"); int keyCode; - if(!PyArg_ParseTuple(args, "i", &keyCode)) { + if(!PyArg_ParseTuple(args, "i:setHold1", &keyCode)) { return NULL; } @@ -617,7 +479,7 @@ const char SCA_KeyboardSensor::GetHold2_doc[] = "getHold2()\n" "\tReturn the code of the second key modifier to the key this \n" "\tsensor is listening to.\n" ; -PyObject* SCA_KeyboardSensor::PyGetHold2(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_KeyboardSensor::PyGetHold2() { ShowDeprecationWarning("getHold2()", "the hold2 property"); return PyInt_FromLong(m_qual2); @@ -628,12 +490,12 @@ const char SCA_KeyboardSensor::SetHold2_doc[] = "setHold2(keycode)\n" "\t- keycode: any code from GameKeys\n" "\tSet the first modifier to the key this sensor should listen to.\n" ; -PyObject* SCA_KeyboardSensor::PySetHold2(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_KeyboardSensor::PySetHold2(PyObject* args) { ShowDeprecationWarning("setHold2()", "the hold2 property"); int keyCode; - if(!PyArg_ParseTuple(args, "i", &keyCode)) { + if(!PyArg_ParseTuple(args, "i:setHold2", &keyCode)) { return NULL; } @@ -649,9 +511,9 @@ const char SCA_KeyboardSensor::GetPressedKeys_doc[] = "getPressedKeys()\n" "\tGet a list of pressed keys that have either been pressed, or just released this frame.\n" ; -PyObject* SCA_KeyboardSensor::PyGetPressedKeys(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_KeyboardSensor::PyGetPressedKeys() { - ShowDeprecationWarning("getPressedKeys()", "getEventList()"); + ShowDeprecationWarning("getPressedKeys()", "events"); SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice(); @@ -663,26 +525,25 @@ PyObject* SCA_KeyboardSensor::PyGetPressedKeys(PyObject* self, PyObject* args, P int index = 0; - for (int i=SCA_IInputDevice::KX_BEGINKEY ; i< SCA_IInputDevice::KX_ENDKEY;i++) + for (int i=SCA_IInputDevice::KX_BEGINKEY ; i<= SCA_IInputDevice::KX_ENDKEY;i++) { const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i); if ((inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED) || (inevent.m_status == SCA_InputEvent::KX_JUSTRELEASED)) { - if (index < num) - { - PyObject* keypair = PyList_New(2); - PyList_SetItem(keypair,0,PyInt_FromLong(i)); - PyList_SetItem(keypair,1,PyInt_FromLong(inevent.m_status)); - PyList_SetItem(resultlist,index,keypair); - index++; - } + PyObject* keypair = PyList_New(2); + PyList_SET_ITEM(keypair,0,PyInt_FromLong(i)); + PyList_SET_ITEM(keypair,1,PyInt_FromLong(inevent.m_status)); + PyList_SET_ITEM(resultlist,index,keypair); + index++; + + if (index >= num) /* should not happen */ + break; } - } - if (index>0) return resultlist; + } } - Py_RETURN_NONE; + return resultlist; } @@ -691,11 +552,11 @@ const char SCA_KeyboardSensor::GetCurrentlyPressedKeys_doc[] = "getCurrentlyPressedKeys()\n" "\tGet a list of keys that are currently pressed.\n" ; -PyObject* SCA_KeyboardSensor::PyGetCurrentlyPressedKeys(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_KeyboardSensor::PyGetCurrentlyPressedKeys() { -ShowDeprecationWarning("getCurrentlyPressedKeys()", "getEventList()"); + ShowDeprecationWarning("getCurrentlyPressedKeys()", "events"); -SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice(); + SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice(); int num = inputdev->GetNumActiveEvents(); PyObject* resultlist = PyList_New(num); @@ -704,73 +565,49 @@ SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice(); { int index = 0; - for (int i=SCA_IInputDevice::KX_BEGINKEY ; i< SCA_IInputDevice::KX_ENDKEY;i++) + for (int i=SCA_IInputDevice::KX_BEGINKEY ; i<= SCA_IInputDevice::KX_ENDKEY;i++) { const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i); if ( (inevent.m_status == SCA_InputEvent::KX_ACTIVE) || (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED)) { - if (index < num) - { - PyObject* keypair = PyList_New(2); - PyList_SetItem(keypair,0,PyInt_FromLong(i)); - PyList_SetItem(keypair,1,PyInt_FromLong(inevent.m_status)); - PyList_SetItem(resultlist,index,keypair); - index++; - } + PyObject* keypair = PyList_New(2); + PyList_SET_ITEM(keypair,0,PyInt_FromLong(i)); + PyList_SET_ITEM(keypair,1,PyInt_FromLong(inevent.m_status)); + PyList_SET_ITEM(resultlist,index,keypair); + index++; + + if (index >= num) /* should never happen */ + break; } } - - /* why?*/ - if (index > 0) return resultlist; } - Py_RETURN_NONE; -} -//<---- Deprecated - -KX_PYMETHODDEF_DOC_NOARGS(SCA_KeyboardSensor, getEventList, -"getEventList()\n" -"\tGet the list of the keyboard events in this frame.\n") -{ - SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice(); - - PyObject* resultlist = PyList_New(0); - - for (int i=SCA_IInputDevice::KX_BEGINKEY ; i< SCA_IInputDevice::KX_ENDKEY;i++) - { - const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i); - if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS) - { - PyObject* keypair = PyList_New(2); - PyList_SetItem(keypair,0,PyInt_FromLong(i)); - PyList_SetItem(keypair,1,PyInt_FromLong(inevent.m_status)); - PyList_Append(resultlist,keypair); - } - } return resultlist; } +//<---- Deprecated + KX_PYMETHODDEF_DOC_O(SCA_KeyboardSensor, getKeyStatus, "getKeyStatus(keycode)\n" "\tGet the given key's status (KX_NO_INPUTSTATUS, KX_JUSTACTIVATED, KX_ACTIVE or KX_JUSTRELEASED).\n") { - if (PyInt_Check(value)) - { - int keycode = PyInt_AsLong(value); - - if ((keycode < SCA_IInputDevice::KX_BEGINKEY) - || (keycode > SCA_IInputDevice::KX_ENDKEY)){ - PyErr_SetString(PyExc_AttributeError, "invalid keycode specified!"); - return NULL; - } - - SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice(); - const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) keycode); - return PyInt_FromLong(inevent.m_status); + if (!PyInt_Check(value)) { + PyErr_SetString(PyExc_ValueError, "sensor.getKeyStatus(int): Keyboard Sensor, expected an int"); + return NULL; } - Py_RETURN_NONE; + int keycode = PyInt_AsLong(value); + + if ((keycode < SCA_IInputDevice::KX_BEGINKEY) + || (keycode > SCA_IInputDevice::KX_ENDKEY)){ + PyErr_SetString(PyExc_AttributeError, "sensor.getKeyStatus(int): Keyboard Sensor, invalid keycode specified!"); + return NULL; + } + + SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice(); + const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) keycode); + return PyInt_FromLong(inevent.m_status); } /* ------------------------------------------------------------------------- */ @@ -778,12 +615,17 @@ KX_PYMETHODDEF_DOC_O(SCA_KeyboardSensor, getKeyStatus, /* ------------------------------------------------------------------------- */ PyTypeObject SCA_KeyboardSensor::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_KeyboardSensor", - sizeof(SCA_KeyboardSensor), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -806,21 +648,21 @@ PyParentObject SCA_KeyboardSensor::Parents[] = { PyMethodDef SCA_KeyboardSensor::Methods[] = { //Deprecated functions ------> - {"getKey", (PyCFunction) SCA_KeyboardSensor::sPyGetKey, METH_VARARGS, (PY_METHODCHAR)GetKey_doc}, + {"getKey", (PyCFunction) SCA_KeyboardSensor::sPyGetKey, METH_NOARGS, (PY_METHODCHAR)GetKey_doc}, {"setKey", (PyCFunction) SCA_KeyboardSensor::sPySetKey, METH_VARARGS, (PY_METHODCHAR)SetKey_doc}, - {"getHold1", (PyCFunction) SCA_KeyboardSensor::sPyGetHold1, METH_VARARGS, (PY_METHODCHAR)GetHold1_doc}, + {"getHold1", (PyCFunction) SCA_KeyboardSensor::sPyGetHold1, METH_NOARGS, (PY_METHODCHAR)GetHold1_doc}, {"setHold1", (PyCFunction) SCA_KeyboardSensor::sPySetHold1, METH_VARARGS, (PY_METHODCHAR)SetHold1_doc}, - {"getHold2", (PyCFunction) SCA_KeyboardSensor::sPyGetHold2, METH_VARARGS, (PY_METHODCHAR)GetHold2_doc}, + {"getHold2", (PyCFunction) SCA_KeyboardSensor::sPyGetHold2, METH_NOARGS, (PY_METHODCHAR)GetHold2_doc}, {"setHold2", (PyCFunction) SCA_KeyboardSensor::sPySetHold2, METH_VARARGS, (PY_METHODCHAR)SetHold2_doc}, - {"getPressedKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetPressedKeys, METH_VARARGS, (PY_METHODCHAR)GetPressedKeys_doc}, - {"getCurrentlyPressedKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetCurrentlyPressedKeys, METH_VARARGS, (PY_METHODCHAR)GetCurrentlyPressedKeys_doc}, + {"getPressedKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetPressedKeys, METH_NOARGS, (PY_METHODCHAR)GetPressedKeys_doc}, + {"getCurrentlyPressedKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetCurrentlyPressedKeys, METH_NOARGS, (PY_METHODCHAR)GetCurrentlyPressedKeys_doc}, //<----- Deprecated - KX_PYMETHODTABLE_NOARGS(SCA_KeyboardSensor, getEventList), KX_PYMETHODTABLE_O(SCA_KeyboardSensor, getKeyStatus), {NULL,NULL} //Sentinel }; PyAttributeDef SCA_KeyboardSensor::Attributes[] = { + KX_PYATTRIBUTE_RO_FUNCTION("events", SCA_KeyboardSensor, pyattr_get_events), KX_PYATTRIBUTE_BOOL_RW("useAllKeys",SCA_KeyboardSensor,m_bAllKeys), KX_PYATTRIBUTE_INT_RW("key",0,SCA_IInputDevice::KX_ENDKEY,true,SCA_KeyboardSensor,m_hotkey), KX_PYATTRIBUTE_SHORT_RW("hold1",0,SCA_IInputDevice::KX_ENDKEY,true,SCA_KeyboardSensor,m_qual), @@ -830,19 +672,188 @@ PyAttributeDef SCA_KeyboardSensor::Attributes[] = { { NULL } //Sentinel }; -PyObject* -SCA_KeyboardSensor::py_getattro(PyObject *attr) +PyObject* SCA_KeyboardSensor::py_getattro(PyObject *attr) { - PyObject* object = py_getattro_self(Attributes, this, attr); - if (object != NULL) - return object; py_getattro_up(SCA_ISensor); } +PyObject* SCA_KeyboardSensor::py_getattro_dict() { + py_getattro_dict_up(SCA_ISensor); +} + int SCA_KeyboardSensor::py_setattro(PyObject *attr, PyObject *value) { - int ret = py_setattro_self(Attributes, this, attr, value); - if (ret >= 0) - return ret; - return SCA_ISensor::py_setattro(attr, value); + py_setattro_up(SCA_ISensor); +} + + +PyObject* SCA_KeyboardSensor::pyattr_get_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + SCA_KeyboardSensor* self= static_cast<SCA_KeyboardSensor*>(self_v); + + SCA_IInputDevice* inputdev = self->m_pKeyboardMgr->GetInputDevice(); + + PyObject* resultlist = PyList_New(0); + + for (int i=SCA_IInputDevice::KX_BEGINKEY ; i<= SCA_IInputDevice::KX_ENDKEY;i++) + { + const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i); + if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS) + { + PyObject* keypair = PyList_New(2); + PyList_SET_ITEM(keypair,0,PyInt_FromLong(i)); + PyList_SET_ITEM(keypair,1,PyInt_FromLong(inevent.m_status)); + PyList_Append(resultlist,keypair); + } + } + return resultlist; +} + + +/* Accessed from python */ + +// this code looks ugly, please use an ordinary hashtable + +char ToCharacter(int keyIndex, bool shifted) +{ + /* numerals */ + if ( (keyIndex >= SCA_IInputDevice::KX_ZEROKEY) + && (keyIndex <= SCA_IInputDevice::KX_NINEKEY) ) { + if (shifted) { + char numshift[] = ")!@#$%^&*("; + return numshift[keyIndex - '0']; + } else { + return keyIndex - SCA_IInputDevice::KX_ZEROKEY + '0'; + } + } + + /* letters... always lowercase... is that desirable? */ + if ( (keyIndex >= SCA_IInputDevice::KX_AKEY) + && (keyIndex <= SCA_IInputDevice::KX_ZKEY) ) { + if (shifted) { + return keyIndex - SCA_IInputDevice::KX_AKEY + 'A'; + } else { + return keyIndex - SCA_IInputDevice::KX_AKEY + 'a'; + } + } + + if (keyIndex == SCA_IInputDevice::KX_SPACEKEY) { + return ' '; + } + if (keyIndex == SCA_IInputDevice::KX_RETKEY || keyIndex == SCA_IInputDevice::KX_PADENTER) { + return '\n'; + } + + + if (keyIndex == SCA_IInputDevice::KX_PADASTERKEY) { + return '*'; + } + + if (keyIndex == SCA_IInputDevice::KX_TABKEY) { + return '\t'; + } + + /* comma to period */ + char commatoperiod[] = ",-."; + char commatoperiodshifted[] = "<_>"; + if (keyIndex == SCA_IInputDevice::KX_COMMAKEY) { + if (shifted) { + return commatoperiodshifted[0]; + } else { + return commatoperiod[0]; + } + } + if (keyIndex == SCA_IInputDevice::KX_MINUSKEY) { + if (shifted) { + return commatoperiodshifted[1]; + } else { + return commatoperiod[1]; + } + } + if (keyIndex == SCA_IInputDevice::KX_PERIODKEY) { + if (shifted) { + return commatoperiodshifted[2]; + } else { + return commatoperiod[2]; + } + } + + /* semicolon to rightbracket */ + char semicolontorightbracket[] = ";\'`/\\=[]"; + char semicolontorightbracketshifted[] = ":\"~\?|+{}"; + if ((keyIndex >= SCA_IInputDevice::KX_SEMICOLONKEY) + && (keyIndex <= SCA_IInputDevice::KX_RIGHTBRACKETKEY)) { + if (shifted) { + return semicolontorightbracketshifted[keyIndex - SCA_IInputDevice::KX_SEMICOLONKEY]; + } else { + return semicolontorightbracket[keyIndex - SCA_IInputDevice::KX_SEMICOLONKEY]; + } + } + + /* keypad2 to padplus */ + char pad2topadplus[] = "246813579. 0- +"; + if ((keyIndex >= SCA_IInputDevice::KX_PAD2) + && (keyIndex <= SCA_IInputDevice::KX_PADPLUSKEY)) { + return pad2topadplus[keyIndex - SCA_IInputDevice::KX_PAD2]; + } + + return '!'; +} + + + +/** + * Determine whether this character can be printed. We cannot use + * the library functions here, because we need to test our own + * keycodes. */ +bool IsPrintable(int keyIndex) +{ + /* only print + * - numerals: KX_ZEROKEY to KX_NINEKEY + * - alphas: KX_AKEY to KX_ZKEY. + * - specials: KX_RETKEY, KX_PADASTERKEY, KX_PADCOMMAKEY to KX_PERIODKEY, + * KX_TABKEY , KX_SEMICOLONKEY to KX_RIGHTBRACKETKEY, + * KX_PAD2 to KX_PADPLUSKEY + * - delete and backspace: also printable in the sense that they modify + * the string + * - retkey: should this be printable? + * - virgule: prints a space... don't know which key that's supposed + * to be... + */ + if ( ((keyIndex >= SCA_IInputDevice::KX_ZEROKEY) + && (keyIndex <= SCA_IInputDevice::KX_NINEKEY)) + || ((keyIndex >= SCA_IInputDevice::KX_AKEY) + && (keyIndex <= SCA_IInputDevice::KX_ZKEY)) + || (keyIndex == SCA_IInputDevice::KX_SPACEKEY) + || (keyIndex == SCA_IInputDevice::KX_RETKEY) + || (keyIndex == SCA_IInputDevice::KX_PADENTER) + || (keyIndex == SCA_IInputDevice::KX_PADASTERKEY) + || (keyIndex == SCA_IInputDevice::KX_TABKEY) + || ((keyIndex >= SCA_IInputDevice::KX_COMMAKEY) + && (keyIndex <= SCA_IInputDevice::KX_PERIODKEY)) + || ((keyIndex >= SCA_IInputDevice::KX_SEMICOLONKEY) + && (keyIndex <= SCA_IInputDevice::KX_RIGHTBRACKETKEY)) + || ((keyIndex >= SCA_IInputDevice::KX_PAD2) + && (keyIndex <= SCA_IInputDevice::KX_PADPLUSKEY)) + || (keyIndex == SCA_IInputDevice::KX_DELKEY) + || (keyIndex == SCA_IInputDevice::KX_BACKSPACEKEY) + ) + { + return true; + } else { + return false; + } +} + +/** + * Tests whether this is a delete key. + */ +bool IsDelete(int keyIndex) +{ + if ( (keyIndex == SCA_IInputDevice::KX_DELKEY) + || (keyIndex == SCA_IInputDevice::KX_BACKSPACEKEY) ) { + return true; + } else { + return false; + } } diff --git a/source/gameengine/GameLogic/SCA_KeyboardSensor.h b/source/gameengine/GameLogic/SCA_KeyboardSensor.h index c579b6a82f8..033225cd9be 100644 --- a/source/gameengine/GameLogic/SCA_KeyboardSensor.h +++ b/source/gameengine/GameLogic/SCA_KeyboardSensor.h @@ -82,22 +82,6 @@ class SCA_KeyboardSensor : public SCA_ISensor void AddToTargetProp(int keyIndex); /** - * Determine whether this character can be printed. We cannot use - * the library functions here, because we need to test our own - * keycodes. */ - bool IsPrintable(int keyIndex); - - /** - * Transform keycodes to something printable. - */ - char ToCharacter(int keyIndex, bool shifted); - - /** - * Tests whether this is a delete key. - */ - bool IsDelete(int keyIndex); - - /** * Tests whether shift is pressed. */ bool IsShifted(void); @@ -118,7 +102,7 @@ public: short int GetHotkey(); - virtual bool Evaluate(CValue* event); + virtual bool Evaluate(); virtual bool IsPositiveTrigger(); bool TriggerOnAllKeys(); @@ -127,32 +111,55 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual int py_setattro(PyObject *attr, PyObject *value); //Deprecated functions -----> /** 1. GetKey : check which key this sensor looks at */ - KX_PYMETHOD_DOC(SCA_KeyboardSensor,GetKey); + KX_PYMETHOD_DOC_NOARGS(SCA_KeyboardSensor,GetKey); /** 2. SetKey: change the key to look at */ - KX_PYMETHOD_DOC(SCA_KeyboardSensor,SetKey); + KX_PYMETHOD_DOC_VARARGS(SCA_KeyboardSensor,SetKey); /** 3. GetHold1 : set the first bucky bit */ - KX_PYMETHOD_DOC(SCA_KeyboardSensor,GetHold1); + KX_PYMETHOD_DOC_NOARGS(SCA_KeyboardSensor,GetHold1); /** 4. SetHold1: change the first bucky bit */ - KX_PYMETHOD_DOC(SCA_KeyboardSensor,SetHold1); + KX_PYMETHOD_DOC_VARARGS(SCA_KeyboardSensor,SetHold1); /** 5. GetHold2 : set the second bucky bit */ - KX_PYMETHOD_DOC(SCA_KeyboardSensor,GetHold2); + KX_PYMETHOD_DOC_NOARGS(SCA_KeyboardSensor,GetHold2); /** 6. SetHold2: change the second bucky bit */ - KX_PYMETHOD_DOC(SCA_KeyboardSensor,SetHold2); + KX_PYMETHOD_DOC_VARARGS(SCA_KeyboardSensor,SetHold2); /** 9. GetPressedKeys: */ - KX_PYMETHOD_DOC(SCA_KeyboardSensor,GetPressedKeys); + KX_PYMETHOD_DOC_NOARGS(SCA_KeyboardSensor,GetPressedKeys); /** 9. GetCurrrentlyPressedKeys: */ - KX_PYMETHOD_DOC(SCA_KeyboardSensor,GetCurrentlyPressedKeys); + KX_PYMETHOD_DOC_NOARGS(SCA_KeyboardSensor,GetCurrentlyPressedKeys); // <------ // KeyEvents: KX_PYMETHOD_DOC_NOARGS(SCA_KeyboardSensor,getEventList); // KeyStatus: KX_PYMETHOD_DOC_O(SCA_KeyboardSensor,getKeyStatus); + + static PyObject* pyattr_get_events(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef); }; + +/** + * Transform keycodes to something printable. + */ +char ToCharacter(int keyIndex, bool shifted); + +/** + * Determine whether this character can be printed. We cannot use + * the library functions here, because we need to test our own + * keycodes. */ +bool IsPrintable(int keyIndex); + +/** + * Tests whether this is a delete key. + */ +bool IsDelete(int keyIndex); + + #endif //__KX_KEYBOARDSENSOR + + diff --git a/source/gameengine/GameLogic/SCA_LogicManager.cpp b/source/gameengine/GameLogic/SCA_LogicManager.cpp index b584b37180f..74370f89cf2 100644 --- a/source/gameengine/GameLogic/SCA_LogicManager.cpp +++ b/source/gameengine/GameLogic/SCA_LogicManager.cpp @@ -49,37 +49,14 @@ SCA_LogicManager::SCA_LogicManager() SCA_LogicManager::~SCA_LogicManager() { - /* AddRef() is not used when the objects are added to m_mapStringToGameObjects - so Release() should not be used either. The memory leak big is fixed - in BL_ConvertBlenderObjects() - - int numgameobj = m_mapStringToGameObjects.size(); - for (int i = 0; i < numgameobj; i++) - { - CValue** gameobjptr = m_mapStringToGameObjects.at(i); - assert(gameobjptr); - if (gameobjptr) - (*gameobjptr)->Release(); - - } - */ - /*for (int i=0;i<m_sensorcontrollermap.size();i++) - { - vector<SCA_IController*>* controllerarray = *(m_sensorcontrollermap[i]); - delete controllerarray; - } - */ - for (vector<SCA_EventManager*>::iterator it = m_eventmanagers.begin();!(it==m_eventmanagers.end());it++) + for (vector<SCA_EventManager*>::iterator it = m_eventmanagers.begin();!(it==m_eventmanagers.end());++it) { delete (*it); } m_eventmanagers.clear(); - m_sensorcontrollermapje.clear(); - m_removedActuators.clear(); - m_activeActuators.clear(); + assert(m_activeActuators.Empty()); } - /* // this kind of fixes bug 398 but breakes games, so better leave it out for now. // a removed object's gameobject (and logicbricks and stuff) didn't get released @@ -171,12 +148,7 @@ void* SCA_LogicManager::FindBlendObjByGameMeshName(const STR_String& gamemeshnam void SCA_LogicManager::RemoveSensor(SCA_ISensor* sensor) { - controllerlist contlist = m_sensorcontrollermapje[sensor]; - for (controllerlist::const_iterator c= contlist.begin();!(c==contlist.end());c++) - { - (*c)->UnlinkSensor(sensor); - } - m_sensorcontrollermapje.erase(sensor); + sensor->UnlinkAllControllers(); sensor->UnregisterToManager(); } @@ -184,35 +156,22 @@ void SCA_LogicManager::RemoveController(SCA_IController* controller) { controller->UnlinkAllSensors(); controller->UnlinkAllActuators(); - std::map<SCA_ISensor*,controllerlist>::iterator sit; - for (sit = m_sensorcontrollermapje.begin();!(sit==m_sensorcontrollermapje.end());++sit) - { - (*sit).second.remove(controller); - } + controller->Deactivate(); } -void SCA_LogicManager::RemoveDestroyedActuator(SCA_IActuator* actuator) +void SCA_LogicManager::RemoveActuator(SCA_IActuator* actuator) { - m_removedActuators.push_back(SmartActuatorPtr(actuator,0)); - // take care that no controller can use this actuator again ! - - std::map<SCA_ISensor*,controllerlist>::const_iterator sit; - for (sit = m_sensorcontrollermapje.begin();!(sit==m_sensorcontrollermapje.end());++sit) - { - controllerlist contlist = (*sit).second; - for (list<SCA_IController*>::const_iterator c= contlist.begin();!(c==contlist.end());c++) - { - (*c)->UnlinkActuator(actuator); - } - } + actuator->UnlinkAllControllers(); + actuator->Deactivate(); + actuator->SetActive(false); } void SCA_LogicManager::RegisterToSensor(SCA_IController* controller,SCA_ISensor* sensor) { - m_sensorcontrollermapje[sensor].push_back(controller); + sensor->LinkToController(controller); controller->LinkToSensor(sensor); } @@ -220,6 +179,7 @@ void SCA_LogicManager::RegisterToSensor(SCA_IController* controller,SCA_ISensor* void SCA_LogicManager::RegisterToActuator(SCA_IController* controller,SCA_IActuator* actua) { + actua->LinkToController(controller); controller->LinkToActuator(actua); } @@ -230,88 +190,60 @@ void SCA_LogicManager::BeginFrame(double curtime, double fixedtime) for (vector<SCA_EventManager*>::const_iterator ie=m_eventmanagers.begin(); !(ie==m_eventmanagers.end()); ie++) (*ie)->NextFrame(curtime, fixedtime); - // for this frame, look up for activated sensors, and build the collection of triggered controllers - // int numsensors = this->m_activatedsensors.size(); /*unused*/ - - for (vector<SCA_ISensor*>::const_iterator is=m_activatedsensors.begin(); - !(is==m_activatedsensors.end());is++) + for(SG_QList* obj = (SG_QList*)m_triggeredControllerSet.Remove(); + obj != NULL; + obj = (SG_QList*)m_triggeredControllerSet.Remove()) { - SCA_ISensor* sensor = *is; - controllerlist contlist = m_sensorcontrollermapje[sensor]; - for (list<SCA_IController*>::const_iterator c= contlist.begin(); - !(c==contlist.end());c++) + for(SCA_IController* contr = (SCA_IController*)obj->QRemove(); + contr != NULL; + contr = (SCA_IController*)obj->QRemove()) { - SCA_IController* contr = *c;//controllerarray->at(c); - if (contr->IsActive()) - { - m_triggeredControllerSet.insert(SmartControllerPtr(contr,0)); - // So that the controller knows which sensor has activited it. - // Only needed for the python controller though. - if (contr->GetType() == &SCA_PythonController::Type) - { - SCA_PythonController* pythonController = (SCA_PythonController*)contr; - pythonController->AddTriggeredSensor(sensor); - } - } + contr->Trigger(this); + contr->ClrJustActivated(); } - //sensor->SetActive(false); } - - - // int numtriggered = triggeredControllerSet.size(); /*unused*/ - for (set<SmartControllerPtr>::iterator tit=m_triggeredControllerSet.begin(); - !(tit==m_triggeredControllerSet.end());tit++) - { - (*tit)->Trigger(this); - } - m_triggeredControllerSet.clear(); } void SCA_LogicManager::UpdateFrame(double curtime, bool frame) { - vector<SmartActuatorPtr>::iterator ra; - for (ra = m_removedActuators.begin(); !(ra == m_removedActuators.end()); ra++) - { - m_activeActuators.erase(*ra); - (*ra)->SetActive(false); - } - m_removedActuators.clear(); - - // About to run actuators, but before update the sensors for those which depends on actuators for (vector<SCA_EventManager*>::const_iterator ie=m_eventmanagers.begin(); !(ie==m_eventmanagers.end()); ie++) (*ie)->UpdateFrame(); - for (set<SmartActuatorPtr>::iterator ia = m_activeActuators.begin();!(ia==m_activeActuators.end());ia++) + SG_DList::iterator<SG_QList> io(m_activeActuators); + for (io.begin(); !io.end(); ++io) { - //SCA_IActuator* actua = *ia; - if (!(*ia)->Update(curtime, frame)) - { - //*ia = m_activeactuators.back(); - m_removedActuators.push_back(*ia); - - (*ia)->SetActive(false); - //m_activeactuators.pop_back(); - } else if ((*ia)->IsNoLink()) + SG_QList::iterator<SCA_IActuator> ia(*(*io)); + for (ia.begin(); !ia.end(); ++ia) { - // This actuator has no more links but it still active - // make sure it will get a negative event on next frame to stop it - // Do this check after Update() rather than before to make sure - // that all the actuators that are activated at same time than a state - // actuator have a chance to execute. - CValue* event = new CBoolValue(false); - (*ia)->RemoveAllEvents(); - (*ia)->AddEvent(event); + SCA_IActuator* actua = *ia; + if (!actua->Update(curtime, frame)) + { + // cannot deactive the actuator now as it will disturb the list + m_removedActuators.AddBack(actua); + actua->SetActive(false); + } else if (actua->IsNoLink()) + { + // This actuator has no more links but it still active + // make sure it will get a negative event on next frame to stop it + // Do this check after Update() rather than before to make sure + // that all the actuators that are activated at same time than a state + // actuator have a chance to execute. + bool event = false; + actua->RemoveAllEvents(); + actua->AddEvent(event); + } } } - - for ( ra = m_removedActuators.begin(); !(ra == m_removedActuators.end()); ra++) + + for (SCA_IActuator* act = (SCA_IActuator*)m_removedActuators.Remove(); + act != NULL; + act = (SCA_IActuator*)m_removedActuators.Remove()) { - m_activeActuators.erase(*ra); - (*ra)->SetActive(false); + act->Deactivate(); + act->SetActive(false); } - m_removedActuators.clear(); } @@ -360,39 +292,17 @@ void SCA_LogicManager::RegisterActionName(const STR_String& actname,void* action void SCA_LogicManager::EndFrame() { - for (vector<SCA_ISensor*>::const_iterator is=m_activatedsensors.begin(); - !(is==m_activatedsensors.end());is++) - { - SCA_ISensor* sensor = *is; - sensor->SetActive(false); - } - m_activatedsensors.clear(); - for (vector<SCA_EventManager*>::const_iterator ie=m_eventmanagers.begin(); !(ie==m_eventmanagers.end());ie++) { (*ie)->EndFrame(); } - - } - -void SCA_LogicManager::AddActivatedSensor(SCA_ISensor* sensor) -{ - // each frame, only add sensor once, and to avoid a seek, or bloated container - // hold a flag in each sensor, with the 'framenr' - if (!sensor->IsActive()) - { - sensor->SetActive(true); - m_activatedsensors.push_back(sensor); - } -} - void SCA_LogicManager::AddTriggeredController(SCA_IController* controller, SCA_ISensor* sensor) { - m_triggeredControllerSet.insert(SmartControllerPtr(controller,0)); + controller->Activate(m_triggeredControllerSet); // so that the controller knows which sensor has activited it // only needed for python controller if (controller->GetType() == &SCA_PythonController::Type) @@ -403,14 +313,11 @@ void SCA_LogicManager::AddTriggeredController(SCA_IController* controller, SCA_I } -void SCA_LogicManager::AddActiveActuator(SCA_IActuator* actua,CValue* event) +void SCA_LogicManager::AddActiveActuator(SCA_IActuator* actua,bool event) { - if (!actua->IsActive()) - { - actua->SetActive(true); - m_activeActuators.insert(SmartActuatorPtr(actua,0)); - } - actua->AddEvent(event->AddRef()); + actua->SetActive(true); + actua->Activate(m_activeActuators); + actua->AddEvent(event); } @@ -432,109 +339,3 @@ SCA_EventManager* SCA_LogicManager::FindEventManager(int eventmgrtype) } return eventmgr; } - - - -SmartActuatorPtr::SmartActuatorPtr(const SmartActuatorPtr& other) -{ - this->m_actuator = other.m_actuator; - this->m_actuator->AddRef(); -} - - - -SmartActuatorPtr::SmartActuatorPtr(SCA_IActuator* actua,int dummy) -: m_actuator(actua) -{ - actua->AddRef(); -} - - - -SmartActuatorPtr::~SmartActuatorPtr() -{ - m_actuator->Release(); -} - - - -bool SmartActuatorPtr::operator <(const SmartActuatorPtr& other) const -{ - - return m_actuator->LessComparedTo(*other); -} - - - -bool SmartActuatorPtr::operator ==(const SmartActuatorPtr& other) const -{ - bool result2 = other->LessComparedTo(m_actuator); - return (m_actuator->LessComparedTo(*other) && result2); -} - - - -SCA_IActuator* SmartActuatorPtr::operator->() const -{ - return m_actuator; -} - - - -SCA_IActuator* SmartActuatorPtr::operator*() const -{ - return m_actuator; -} - - - -SmartControllerPtr::SmartControllerPtr(const SmartControllerPtr& copy) -{ - this->m_controller = copy.m_controller; - this->m_controller->AddRef(); -} - - - -SmartControllerPtr::SmartControllerPtr(SCA_IController* contr,int dummy) -: m_controller(contr) -{ - m_controller->AddRef(); -} - - - -SmartControllerPtr::~SmartControllerPtr() -{ - m_controller->Release(); -} - - - -bool SmartControllerPtr::operator <(const SmartControllerPtr& other) const -{ - return m_controller->LessComparedTo(*other); -} - - - -bool SmartControllerPtr::operator ==(const SmartControllerPtr& other) const -{ - return (m_controller->LessComparedTo(*other) && other->LessComparedTo(m_controller)); -} - - - -SCA_IController* SmartControllerPtr::operator->() const -{ - return m_controller; -} - - - -SCA_IController* SmartControllerPtr::operator*() const -{ - return m_controller; -} - - diff --git a/source/gameengine/GameLogic/SCA_LogicManager.h b/source/gameengine/GameLogic/SCA_LogicManager.h index 50383879d8f..0d610c9cc46 100644 --- a/source/gameengine/GameLogic/SCA_LogicManager.h +++ b/source/gameengine/GameLogic/SCA_LogicManager.h @@ -43,11 +43,13 @@ #include "GEN_Map.h" #include "STR_HashedString.h" #include "Value.h" +#include "SG_QList.h" #include "KX_HashedPtr.h" using namespace std; -typedef list<class SCA_IController*> controllerlist; +typedef std::list<class SCA_IController*> controllerlist; +typedef std::map<class SCA_ISensor*,controllerlist > sensormap_t; /** * This manager handles sensor, controllers and actuators. @@ -64,44 +66,17 @@ typedef list<class SCA_IController*> controllerlist; #include "SCA_ILogicBrick.h" -// todo: make this into a template, but first I want to think about what exactly to put in -class SmartActuatorPtr -{ - SCA_IActuator* m_actuator; -public: - SmartActuatorPtr(SCA_IActuator* actua,int dummy); - SmartActuatorPtr(const SmartActuatorPtr& other); - virtual ~SmartActuatorPtr(); - bool operator <(const SmartActuatorPtr& other) const; - bool operator ==(const SmartActuatorPtr& other) const; - SCA_IActuator* operator->() const; - SCA_IActuator* operator*() const; - -}; - -class SmartControllerPtr -{ - SCA_IController* m_controller; -public: - SmartControllerPtr(const SmartControllerPtr& copy); - SmartControllerPtr(SCA_IController* contr,int dummy); - virtual ~SmartControllerPtr(); - bool operator <(const SmartControllerPtr& other) const; - bool operator ==(const SmartControllerPtr& other) const; - SCA_IController* operator->() const; - SCA_IController* operator*() const; - -}; class SCA_LogicManager { vector<class SCA_EventManager*> m_eventmanagers; - vector<class SCA_ISensor*> m_activatedsensors; - set<class SmartActuatorPtr> m_activeActuators; - set<class SmartControllerPtr> m_triggeredControllerSet; - - map<SCA_ISensor*,controllerlist > m_sensorcontrollermapje; + // SG_DList: Head of objects having activated actuators + // element: SCA_IObject::m_activeActuators + SG_DList m_activeActuators; + // SG_DList: Head of objects having activated controllers + // element: SCA_IObject::m_activeControllers + SG_DList m_triggeredControllerSet; // need to find better way for this // also known as FactoryManager... @@ -112,10 +87,12 @@ class SCA_LogicManager GEN_Map<STR_HashedString,void*> m_map_gamemeshname_to_blendobj; GEN_Map<CHashedPtr,void*> m_map_blendobj_to_gameobj; - vector<SmartActuatorPtr> m_removedActuators; + // head of actuators being deactivated during the logic update + SG_DList m_removedActuators; public: SCA_LogicManager(); virtual ~SCA_LogicManager(); + //void SetKeyboardManager(SCA_KeyboardManager* keyboardmgr) { m_keyboardmgr=keyboardmgr;} void RegisterEventManager(SCA_EventManager* eventmgr); void RegisterToSensor(SCA_IController* controller, @@ -126,8 +103,7 @@ public: void BeginFrame(double curtime, double fixedtime); void UpdateFrame(double curtime, bool frame); void EndFrame(); - void AddActivatedSensor(SCA_ISensor* sensor); - void AddActiveActuator(SCA_IActuator* sensor,class CValue* event); + void AddActiveActuator(SCA_IActuator* sensor,bool event); void AddTriggeredController(SCA_IController* controller, SCA_ISensor* sensor); SCA_EventManager* FindEventManager(int eventmgrtype); @@ -138,7 +114,7 @@ public: */ void RemoveSensor(SCA_ISensor* sensor); void RemoveController(SCA_IController* controller); - void RemoveDestroyedActuator(SCA_IActuator* actuator); + void RemoveActuator(SCA_IActuator* actuator); // for the scripting... needs a FactoryManager later (if we would have time... ;) diff --git a/source/gameengine/GameLogic/SCA_MouseManager.cpp b/source/gameengine/GameLogic/SCA_MouseManager.cpp index ca875dad07c..d407647cec3 100644 --- a/source/gameengine/GameLogic/SCA_MouseManager.cpp +++ b/source/gameengine/GameLogic/SCA_MouseManager.cpp @@ -75,8 +75,8 @@ void SCA_MouseManager::NextFrame() { if (m_mousedevice) { - set<SCA_ISensor*>::iterator it; - for (it=m_sensors.begin(); it!=m_sensors.end(); it++) + SG_DList::iterator<SCA_ISensor> it(m_sensors); + for (it.begin();!it.end();++it) { SCA_MouseSensor* mousesensor = (SCA_MouseSensor*)(*it); // (0,0) is the Upper Left corner in our local window @@ -93,7 +93,7 @@ void SCA_MouseManager::NextFrame() mousesensor->setX(mx); mousesensor->setY(my); - mousesensor->Activate(m_logicmanager,NULL); + mousesensor->Activate(m_logicmanager); } } } diff --git a/source/gameengine/GameLogic/SCA_MouseSensor.cpp b/source/gameengine/GameLogic/SCA_MouseSensor.cpp index c5f6fdabbe8..c5e1c3c0441 100644 --- a/source/gameengine/GameLogic/SCA_MouseSensor.cpp +++ b/source/gameengine/GameLogic/SCA_MouseSensor.cpp @@ -111,7 +111,7 @@ CValue* SCA_MouseSensor::GetReplica() { SCA_MouseSensor* replica = new SCA_MouseSensor(*this); // this will copy properties and so on... - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); replica->Init(); return replica; @@ -144,7 +144,7 @@ SCA_IInputDevice::KX_EnumInputs SCA_MouseSensor::GetHotKey() -bool SCA_MouseSensor::Evaluate(CValue* event) +bool SCA_MouseSensor::Evaluate() { bool result = false; bool reset = m_reset && m_level; @@ -252,9 +252,7 @@ const char SCA_MouseSensor::GetXPosition_doc[] = "\tReturns the x-coordinate of the mouse sensor, in frame coordinates.\n" "\tThe lower-left corner is the origin. The coordinate is given in\n" "\tpixels\n"; -PyObject* SCA_MouseSensor::PyGetXPosition(PyObject* self, - PyObject* args, - PyObject* kwds) { +PyObject* SCA_MouseSensor::PyGetXPosition() { ShowDeprecationWarning("getXPosition()", "the position property"); return PyInt_FromLong(m_x); } @@ -265,9 +263,7 @@ const char SCA_MouseSensor::GetYPosition_doc[] = "\tReturns the y-coordinate of the mouse sensor, in frame coordinates.\n" "\tThe lower-left corner is the origin. The coordinate is given in\n" "\tpixels\n"; -PyObject* SCA_MouseSensor::PyGetYPosition(PyObject* self, - PyObject* args, - PyObject* kwds) { +PyObject* SCA_MouseSensor::PyGetYPosition() { ShowDeprecationWarning("getYPosition()", "the position property"); return PyInt_FromLong(m_y); } @@ -275,7 +271,7 @@ PyObject* SCA_MouseSensor::PyGetYPosition(PyObject* self, KX_PYMETHODDEF_DOC_O(SCA_MouseSensor, getButtonStatus, "getButtonStatus(button)\n" -"\tGet the given button's status (KX_NO_INPUTSTATUS, KX_JUSTACTIVATED, KX_ACTIVE or KX_JUSTRELEASED).\n") +"\tGet the given button's status (KX_INPUT_NONE, KX_INPUT_NONE, KX_INPUT_JUST_ACTIVATED, KX_INPUT_ACTIVE, KX_INPUT_JUST_RELEASED).\n") { if (PyInt_Check(value)) { @@ -283,7 +279,7 @@ KX_PYMETHODDEF_DOC_O(SCA_MouseSensor, getButtonStatus, if ((button < SCA_IInputDevice::KX_LEFTMOUSE) || (button > SCA_IInputDevice::KX_RIGHTMOUSE)){ - PyErr_SetString(PyExc_ValueError, "invalid button specified!"); + PyErr_SetString(PyExc_ValueError, "sensor.getButtonStatus(int): Mouse Sensor, invalid button specified!"); return NULL; } @@ -300,12 +296,17 @@ KX_PYMETHODDEF_DOC_O(SCA_MouseSensor, getButtonStatus, /* ------------------------------------------------------------------------- */ PyTypeObject SCA_MouseSensor::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_MouseSensor", - sizeof(SCA_MouseSensor), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -343,18 +344,16 @@ PyAttributeDef SCA_MouseSensor::Attributes[] = { PyObject* SCA_MouseSensor::py_getattro(PyObject *attr) { - PyObject* object = py_getattro_self(Attributes, this, attr); - if (object != NULL) - return object; py_getattro_up(SCA_ISensor); } +PyObject* SCA_MouseSensor::py_getattro_dict() { + py_getattro_dict_up(SCA_ISensor); +} + int SCA_MouseSensor::py_setattro(PyObject *attr, PyObject *value) { - int ret = py_setattro_self(Attributes, this, attr, value); - if (ret >= 0) - return ret; - return SCA_ISensor::py_setattro(attr, value); + py_setattro_up(SCA_ISensor); } /* eof */ diff --git a/source/gameengine/GameLogic/SCA_MouseSensor.h b/source/gameengine/GameLogic/SCA_MouseSensor.h index 73410569cc2..6d6302b514a 100644 --- a/source/gameengine/GameLogic/SCA_MouseSensor.h +++ b/source/gameengine/GameLogic/SCA_MouseSensor.h @@ -97,7 +97,7 @@ class SCA_MouseSensor : public SCA_ISensor virtual ~SCA_MouseSensor(); virtual CValue* GetReplica(); - virtual bool Evaluate(CValue* event); + virtual bool Evaluate(); virtual void Init(); virtual bool IsPositiveTrigger(); short int GetModeKey(); @@ -110,13 +110,14 @@ class SCA_MouseSensor : public SCA_ISensor /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual int py_setattro(PyObject *attr, PyObject *value); //Deprecated functions -----> /* read x-coordinate */ - KX_PYMETHOD_DOC(SCA_MouseSensor,GetXPosition); + KX_PYMETHOD_DOC_NOARGS(SCA_MouseSensor,GetXPosition); /* read y-coordinate */ - KX_PYMETHOD_DOC(SCA_MouseSensor,GetYPosition); + KX_PYMETHOD_DOC_NOARGS(SCA_MouseSensor,GetYPosition); //<----- deprecated // get button status diff --git a/source/gameengine/GameLogic/SCA_NANDController.cpp b/source/gameengine/GameLogic/SCA_NANDController.cpp index bddd5f4d3ab..d27aea5e6f7 100644 --- a/source/gameengine/GameLogic/SCA_NANDController.cpp +++ b/source/gameengine/GameLogic/SCA_NANDController.cpp @@ -66,26 +66,19 @@ void SCA_NANDController::Trigger(SCA_LogicManager* logicmgr) !(is==m_linkedsensors.end());is++) { SCA_ISensor* sensor = *is; - if (!sensor->IsPositiveTrigger()) + if (!sensor->GetState()) { sensorresult = true; break; } } - CValue* newevent = new CBoolValue(sensorresult); - for (vector<SCA_IActuator*>::const_iterator i=m_linkedactuators.begin(); !(i==m_linkedactuators.end());i++) { - SCA_IActuator* actua = *i;//m_linkedactuators.at(i); - logicmgr->AddActiveActuator(actua,newevent); + SCA_IActuator* actua = *i; + logicmgr->AddActiveActuator(actua,sensorresult); } - - // every actuator that needs the event, has a it's own reference to it now so - // release it (so to be clear: if there is no actuator, it's deleted right now) - newevent->Release(); - } @@ -94,7 +87,7 @@ CValue* SCA_NANDController::GetReplica() { CValue* replica = new SCA_NANDController(*this); // this will copy properties and so on... - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); return replica; } @@ -107,12 +100,17 @@ CValue* SCA_NANDController::GetReplica() /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_NANDController::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_NANDController", - sizeof(SCA_NANDController), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -145,4 +143,8 @@ PyObject* SCA_NANDController::py_getattro(PyObject *attr) { py_getattro_up(SCA_IController); } +PyObject* SCA_NANDController::py_getattro_dict() { + py_getattro_dict_up(SCA_IController); +} + /* eof */ diff --git a/source/gameengine/GameLogic/SCA_NANDController.h b/source/gameengine/GameLogic/SCA_NANDController.h index 11600914a1a..0ae0ff19745 100644 --- a/source/gameengine/GameLogic/SCA_NANDController.h +++ b/source/gameengine/GameLogic/SCA_NANDController.h @@ -49,6 +49,7 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); }; diff --git a/source/gameengine/GameLogic/SCA_NORController.cpp b/source/gameengine/GameLogic/SCA_NORController.cpp index 3ee073523c3..6c9141636b2 100644 --- a/source/gameengine/GameLogic/SCA_NORController.cpp +++ b/source/gameengine/GameLogic/SCA_NORController.cpp @@ -66,26 +66,19 @@ void SCA_NORController::Trigger(SCA_LogicManager* logicmgr) !(is==m_linkedsensors.end());is++) { SCA_ISensor* sensor = *is; - if (sensor->IsPositiveTrigger()) + if (sensor->GetState()) { sensorresult = false; break; } } - CValue* newevent = new CBoolValue(sensorresult); - for (vector<SCA_IActuator*>::const_iterator i=m_linkedactuators.begin(); !(i==m_linkedactuators.end());i++) { - SCA_IActuator* actua = *i;//m_linkedactuators.at(i); - logicmgr->AddActiveActuator(actua,newevent); + SCA_IActuator* actua = *i; + logicmgr->AddActiveActuator(actua,sensorresult); } - - // every actuator that needs the event, has a it's own reference to it now so - // release it (so to be clear: if there is no actuator, it's deleted right now) - newevent->Release(); - } @@ -94,7 +87,7 @@ CValue* SCA_NORController::GetReplica() { CValue* replica = new SCA_NORController(*this); // this will copy properties and so on... - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); return replica; } @@ -107,12 +100,17 @@ CValue* SCA_NORController::GetReplica() /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_NORController::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_NORController", - sizeof(SCA_NORController), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -145,4 +143,8 @@ PyObject* SCA_NORController::py_getattro(PyObject *attr) { py_getattro_up(SCA_IController); } +PyObject* SCA_NORController::py_getattro_dict() { + py_getattro_dict_up(SCA_IController); +} + /* eof */ diff --git a/source/gameengine/GameLogic/SCA_NORController.h b/source/gameengine/GameLogic/SCA_NORController.h index fc814e28d37..06cbb70a489 100644 --- a/source/gameengine/GameLogic/SCA_NORController.h +++ b/source/gameengine/GameLogic/SCA_NORController.h @@ -49,6 +49,7 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); }; diff --git a/source/gameengine/GameLogic/SCA_ORController.cpp b/source/gameengine/GameLogic/SCA_ORController.cpp index 91d5e56d4f3..42c0a67d657 100644 --- a/source/gameengine/GameLogic/SCA_ORController.cpp +++ b/source/gameengine/GameLogic/SCA_ORController.cpp @@ -60,7 +60,7 @@ CValue* SCA_ORController::GetReplica() { CValue* replica = new SCA_ORController(*this); // this will copy properties and so on... - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); return replica; } @@ -76,21 +76,16 @@ void SCA_ORController::Trigger(SCA_LogicManager* logicmgr) while ( (!sensorresult) && (!(is==m_linkedsensors.end())) ) { sensor = *is; - if (sensor->IsPositiveTrigger()) sensorresult = true; + if (sensor->GetState()) sensorresult = true; is++; } - CValue* newevent = new CBoolValue(sensorresult); - for (vector<SCA_IActuator*>::const_iterator i=m_linkedactuators.begin(); !(i==m_linkedactuators.end());i++) { - SCA_IActuator* actua = *i;//m_linkedactuators.at(i); - logicmgr->AddActiveActuator(actua,newevent); + SCA_IActuator* actua = *i; + logicmgr->AddActiveActuator(actua,sensorresult); } - - - newevent->Release(); } /* ------------------------------------------------------------------------- */ @@ -99,12 +94,17 @@ void SCA_ORController::Trigger(SCA_LogicManager* logicmgr) /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_ORController::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_ORController", - sizeof(SCA_ORController), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -138,4 +138,8 @@ PyObject* SCA_ORController::py_getattro(PyObject *attr) { py_getattro_up(SCA_IController); } +PyObject* SCA_ORController::py_getattro_dict() { + py_getattro_dict_up(SCA_IController); +} + /* eof */ diff --git a/source/gameengine/GameLogic/SCA_ORController.h b/source/gameengine/GameLogic/SCA_ORController.h index fdc81486e74..66f772c739e 100644 --- a/source/gameengine/GameLogic/SCA_ORController.h +++ b/source/gameengine/GameLogic/SCA_ORController.h @@ -50,6 +50,7 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); }; #endif //__KX_ORCONTROLLER diff --git a/source/gameengine/GameLogic/SCA_PropertyActuator.cpp b/source/gameengine/GameLogic/SCA_PropertyActuator.cpp index e1f303430ec..33dc83506f3 100644 --- a/source/gameengine/GameLogic/SCA_PropertyActuator.cpp +++ b/source/gameengine/GameLogic/SCA_PropertyActuator.cpp @@ -77,11 +77,25 @@ bool SCA_PropertyActuator::Update() CParser parser; parser.SetContext( propowner->AddRef()); - CExpression* userexpr = parser.ProcessText(m_exprtxt); - if (userexpr) + CExpression* userexpr= NULL; + + if (m_type==KX_ACT_PROP_TOGGLE) { - - + /* dont use */ + CValue* newval; + CValue* oldprop = propowner->GetProperty(m_propname); + if (oldprop) + { + newval = new CBoolValue((oldprop->GetNumber()==0.0) ? true:false); + oldprop->SetValue(newval); + } else + { /* as not been assigned, evaluate as false, so assign true */ + newval = new CBoolValue(true); + propowner->SetProperty(m_propname,newval); + } + newval->Release(); + } + else if ((userexpr = parser.ProcessText(m_exprtxt))) { switch (m_type) { @@ -135,6 +149,7 @@ bool SCA_PropertyActuator::Update() } break; } + /* case KX_ACT_PROP_TOGGLE: */ /* accounted for above, no need for userexpr */ default: { @@ -171,11 +186,6 @@ GetReplica() { SCA_PropertyActuator* replica = new SCA_PropertyActuator(*this); replica->ProcessReplica(); - - // this will copy properties and so on... - - CValue::AddDataToReplica(replica); - return replica; }; @@ -218,12 +228,17 @@ void SCA_PropertyActuator::Relink(GEN_Map<GEN_HashedPtr, void*> *obj_map) /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_PropertyActuator::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_PropertyActuator", - sizeof(SCA_PropertyActuator), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -257,21 +272,20 @@ PyMethodDef SCA_PropertyActuator::Methods[] = { PyAttributeDef SCA_PropertyActuator::Attributes[] = { KX_PYATTRIBUTE_STRING_RW_CHECK("property",0,100,false,SCA_PropertyActuator,m_propname,CheckProperty), KX_PYATTRIBUTE_STRING_RW("value",0,100,false,SCA_PropertyActuator,m_exprtxt), + KX_PYATTRIBUTE_INT_RW("type", KX_ACT_PROP_NODEF+1, KX_ACT_PROP_MAX-1, false, SCA_PropertyActuator, m_type), /* ATTR_TODO add constents to game logic dict */ { NULL } //Sentinel }; PyObject* SCA_PropertyActuator::py_getattro(PyObject *attr) { - PyObject* object = py_getattro_self(Attributes, this, attr); - if (object != NULL) - return object; py_getattro_up(SCA_IActuator); } +PyObject* SCA_PropertyActuator::py_getattro_dict() { + py_getattro_dict_up(SCA_IActuator); +} + int SCA_PropertyActuator::py_setattro(PyObject *attr, PyObject *value) { - int ret = py_setattro_self(Attributes, this, attr, value); - if (ret >= 0) - return ret; - return SCA_IActuator::py_setattro(attr, value); + py_setattro_up(SCA_IActuator); } /* 1. setProperty */ @@ -280,12 +294,12 @@ const char SCA_PropertyActuator::SetProperty_doc[] = "\t- name: string\n" "\tSet the property on which to operate. If there is no property\n" "\tof this name, the call is ignored.\n"; -PyObject* SCA_PropertyActuator::PySetProperty(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_PropertyActuator::PySetProperty(PyObject* args, PyObject* kwds) { ShowDeprecationWarning("setProperty()", "the 'property' property"); /* Check whether the name exists first ! */ char *nameArg; - if (!PyArg_ParseTuple(args, "s", &nameArg)) { + if (!PyArg_ParseTuple(args, "s:setProperty", &nameArg)) { return NULL; } @@ -305,7 +319,7 @@ PyObject* SCA_PropertyActuator::PySetProperty(PyObject* self, PyObject* args, Py const char SCA_PropertyActuator::GetProperty_doc[] = "getProperty(name)\n" "\tReturn the property on which the actuator operates.\n"; -PyObject* SCA_PropertyActuator::PyGetProperty(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_PropertyActuator::PyGetProperty(PyObject* args, PyObject* kwds) { ShowDeprecationWarning("getProperty()", "the 'property' property"); return PyString_FromString(m_propname); @@ -318,11 +332,11 @@ const char SCA_PropertyActuator::SetValue_doc[] = "\tSet the value with which the actuator operates. If the value\n" "\tis not compatible with the type of the property, the subsequent\n" "\t action is ignored.\n"; -PyObject* SCA_PropertyActuator::PySetValue(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_PropertyActuator::PySetValue(PyObject* args, PyObject* kwds) { ShowDeprecationWarning("setValue()", "the value property"); char *valArg; - if(!PyArg_ParseTuple(args, "s", &valArg)) { + if(!PyArg_ParseTuple(args, "s:setValue", &valArg)) { return NULL; } @@ -335,7 +349,7 @@ PyObject* SCA_PropertyActuator::PySetValue(PyObject* self, PyObject* args, PyObj const char SCA_PropertyActuator::GetValue_doc[] = "getValue()\n" "\tReturns the value with which the actuator operates.\n"; -PyObject* SCA_PropertyActuator::PyGetValue(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_PropertyActuator::PyGetValue(PyObject* args, PyObject* kwds) { ShowDeprecationWarning("getValue()", "the value property"); return PyString_FromString(m_exprtxt); diff --git a/source/gameengine/GameLogic/SCA_PropertyActuator.h b/source/gameengine/GameLogic/SCA_PropertyActuator.h index 6a975716ed0..a8df08dfc6e 100644 --- a/source/gameengine/GameLogic/SCA_PropertyActuator.h +++ b/source/gameengine/GameLogic/SCA_PropertyActuator.h @@ -43,6 +43,7 @@ class SCA_PropertyActuator : public SCA_IActuator KX_ACT_PROP_ASSIGN, KX_ACT_PROP_ADD, KX_ACT_PROP_COPY, + KX_ACT_PROP_TOGGLE, KX_ACT_PROP_MAX }; @@ -86,6 +87,7 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual int py_setattro(PyObject *attr, PyObject *value); // python wrapped methods diff --git a/source/gameengine/GameLogic/SCA_PropertyEventManager.cpp b/source/gameengine/GameLogic/SCA_PropertyEventManager.cpp index e5e3f9cced5..764465309df 100644 --- a/source/gameengine/GameLogic/SCA_PropertyEventManager.cpp +++ b/source/gameengine/GameLogic/SCA_PropertyEventManager.cpp @@ -50,8 +50,9 @@ SCA_PropertyEventManager::~SCA_PropertyEventManager() void SCA_PropertyEventManager::NextFrame() { // check for changed properties - for (set<SCA_ISensor*>::const_iterator it = m_sensors.begin();!(it==m_sensors.end());it++) + SG_DList::iterator<SCA_ISensor> it(m_sensors); + for (it.begin();!it.end();++it) { - (*it)->Activate(m_logicmgr,NULL); + (*it)->Activate(m_logicmgr); } } diff --git a/source/gameengine/GameLogic/SCA_PropertySensor.cpp b/source/gameengine/GameLogic/SCA_PropertySensor.cpp index 659823f6fba..2632cbd3dac 100644 --- a/source/gameengine/GameLogic/SCA_PropertySensor.cpp +++ b/source/gameengine/GameLogic/SCA_PropertySensor.cpp @@ -37,6 +37,7 @@ #include "StringValue.h" #include "SCA_EventManager.h" #include "SCA_LogicManager.h" +#include "BoolValue.h" #ifdef HAVE_CONFIG_H #include <config.h> @@ -111,7 +112,7 @@ CValue* SCA_PropertySensor::GetReplica() { SCA_PropertySensor* replica = new SCA_PropertySensor(*this); // m_range_expr must be recalculated on replica! - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); replica->Init(); replica->m_range_expr = NULL; @@ -152,7 +153,7 @@ SCA_PropertySensor::~SCA_PropertySensor() -bool SCA_PropertySensor::Evaluate(CValue* event) +bool SCA_PropertySensor::Evaluate() { bool result = CheckPropertyCondition(); bool reset = m_reset && m_level; @@ -182,17 +183,14 @@ bool SCA_PropertySensor::CheckPropertyCondition() CValue* orgprop = GetParent()->FindIdentifier(m_checkpropname); if (!orgprop->IsError()) { - STR_String testprop = orgprop->GetText(); + const STR_String& testprop = orgprop->GetText(); // Force strings to upper case, to avoid confusion in // bool tests. It's stupid the prop's identity is lost // on the way here... - if ((testprop == "TRUE") || (testprop == "FALSE")) { - STR_String checkprop = m_checkpropval; - checkprop.Upper(); - result = (testprop == checkprop); - } else { - result = (orgprop->GetText() == m_checkpropval); + if ((&testprop == &CBoolValue::sTrueString) || (&testprop == &CBoolValue::sFalseString)) { + m_checkpropval.Upper(); } + result = (testprop == m_checkpropval); } orgprop->Release(); @@ -232,8 +230,8 @@ bool SCA_PropertySensor::CheckPropertyCondition() CValue* vallie = m_range_expr->Calculate(); if (vallie) { - STR_String errtext = vallie->GetText(); - if (errtext == "TRUE") + const STR_String& errtext = vallie->GetText(); + if (&errtext == &CBoolValue::sTrueString) { result = true; } else @@ -295,9 +293,8 @@ CValue* SCA_PropertySensor::FindIdentifier(const STR_String& identifiername) int SCA_PropertySensor::validValueForProperty(void *self, const PyAttributeDef*) { - bool result = true; /* There is no type checking at this moment, unfortunately... */ - return result; + return 0; } /* ------------------------------------------------------------------------- */ @@ -306,12 +303,17 @@ int SCA_PropertySensor::validValueForProperty(void *self, const PyAttributeDef*) /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_PropertySensor::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_PropertySensor", - sizeof(SCA_PropertySensor), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -334,11 +336,11 @@ PyParentObject SCA_PropertySensor::Parents[] = { PyMethodDef SCA_PropertySensor::Methods[] = { //Deprecated functions ------> - {"getType", (PyCFunction) SCA_PropertySensor::sPyGetType, METH_VARARGS, (PY_METHODCHAR)GetType_doc}, + {"getType", (PyCFunction) SCA_PropertySensor::sPyGetType, METH_NOARGS, (PY_METHODCHAR)GetType_doc}, {"setType", (PyCFunction) SCA_PropertySensor::sPySetType, METH_VARARGS, (PY_METHODCHAR)SetType_doc}, - {"getProperty", (PyCFunction) SCA_PropertySensor::sPyGetProperty, METH_VARARGS, (PY_METHODCHAR)GetProperty_doc}, + {"getProperty", (PyCFunction) SCA_PropertySensor::sPyGetProperty, METH_NOARGS, (PY_METHODCHAR)GetProperty_doc}, {"setProperty", (PyCFunction) SCA_PropertySensor::sPySetProperty, METH_VARARGS, (PY_METHODCHAR)SetProperty_doc}, - {"getValue", (PyCFunction) SCA_PropertySensor::sPyGetValue, METH_VARARGS, (PY_METHODCHAR)GetValue_doc}, + {"getValue", (PyCFunction) SCA_PropertySensor::sPyGetValue, METH_NOARGS, (PY_METHODCHAR)GetValue_doc}, {"setValue", (PyCFunction) SCA_PropertySensor::sPySetValue, METH_VARARGS, (PY_METHODCHAR)SetValue_doc}, //<----- Deprecated {NULL,NULL} //Sentinel @@ -353,24 +355,22 @@ PyAttributeDef SCA_PropertySensor::Attributes[] = { PyObject* SCA_PropertySensor::py_getattro(PyObject *attr) { - PyObject* object = py_getattro_self(Attributes, this, attr); - if (object != NULL) - return object; - py_getattro_up(SCA_ISensor); /* implicit return! */ + py_getattro_up(SCA_ISensor); +} + +PyObject* SCA_PropertySensor::py_getattro_dict() { + py_getattro_dict_up(SCA_ISensor); } int SCA_PropertySensor::py_setattro(PyObject *attr, PyObject *value) { - int ret = py_setattro_self(Attributes, this, attr, value); - if (ret >= 0) - return ret; - return SCA_ISensor::py_setattro(attr, value); + py_setattro_up(SCA_ISensor); } /* 1. getType */ const char SCA_PropertySensor::GetType_doc[] = "getType()\n" "\tReturns the type of check this sensor performs.\n"; -PyObject* SCA_PropertySensor::PyGetType(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_PropertySensor::PyGetType() { ShowDeprecationWarning("getType()", "the type property"); return PyInt_FromLong(m_checktype); @@ -383,12 +383,12 @@ const char SCA_PropertySensor::SetType_doc[] = "\t KX_PROPSENSOR_INTERVAL, KX_PROPSENSOR_CHANGED,\n" "\t or KX_PROPSENSOR_EXPRESSION.\n" "\tSet the type of check to perform.\n"; -PyObject* SCA_PropertySensor::PySetType(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_PropertySensor::PySetType(PyObject* args) { ShowDeprecationWarning("setType()", "the type property"); int typeArg; - if (!PyArg_ParseTuple(args, "i", &typeArg)) { + if (!PyArg_ParseTuple(args, "i:setType", &typeArg)) { return NULL; } @@ -404,7 +404,7 @@ PyObject* SCA_PropertySensor::PySetType(PyObject* self, PyObject* args, PyObject const char SCA_PropertySensor::GetProperty_doc[] = "getProperty()\n" "\tReturn the property with which the sensor operates.\n"; -PyObject* SCA_PropertySensor::PyGetProperty(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_PropertySensor::PyGetProperty() { ShowDeprecationWarning("getProperty()", "the 'property' property"); return PyString_FromString(m_checkpropname); @@ -416,14 +416,14 @@ const char SCA_PropertySensor::SetProperty_doc[] = "\t- name: string\n" "\tSets the property with which to operate. If there is no property\n" "\tof this name, the call is ignored.\n"; -PyObject* SCA_PropertySensor::PySetProperty(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_PropertySensor::PySetProperty(PyObject* args) { ShowDeprecationWarning("setProperty()", "the 'property' property"); /* We should query whether the name exists. Or should we create a prop */ /* on the fly? */ char *propNameArg = NULL; - if (!PyArg_ParseTuple(args, "s", &propNameArg)) { + if (!PyArg_ParseTuple(args, "s:setProperty", &propNameArg)) { return NULL; } @@ -441,7 +441,7 @@ PyObject* SCA_PropertySensor::PySetProperty(PyObject* self, PyObject* args, PyOb const char SCA_PropertySensor::GetValue_doc[] = "getValue()\n" "\tReturns the value with which the sensor operates.\n"; -PyObject* SCA_PropertySensor::PyGetValue(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_PropertySensor::PyGetValue() { ShowDeprecationWarning("getValue()", "the value property"); return PyString_FromString(m_checkpropval); @@ -454,19 +454,19 @@ const char SCA_PropertySensor::SetValue_doc[] = "\tSet the value with which the sensor operates. If the value\n" "\tis not compatible with the type of the property, the subsequent\n" "\t action is ignored.\n"; -PyObject* SCA_PropertySensor::PySetValue(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_PropertySensor::PySetValue(PyObject* args) { ShowDeprecationWarning("setValue()", "the value property"); /* Here, we need to check whether the value is 'valid' for this property.*/ /* We know that the property exists, or is NULL. */ char *propValArg = NULL; - if(!PyArg_ParseTuple(args, "s", &propValArg)) { + if(!PyArg_ParseTuple(args, "s:setValue", &propValArg)) { return NULL; } STR_String oldval = m_checkpropval; m_checkpropval = propValArg; - if (validValueForProperty(self, NULL)) { + if (validValueForProperty(m_proxy, NULL)) { m_checkpropval = oldval; return NULL; } diff --git a/source/gameengine/GameLogic/SCA_PropertySensor.h b/source/gameengine/GameLogic/SCA_PropertySensor.h index 076c1ae51ec..538ecd65949 100644 --- a/source/gameengine/GameLogic/SCA_PropertySensor.h +++ b/source/gameengine/GameLogic/SCA_PropertySensor.h @@ -81,7 +81,7 @@ public: void PrecalculateRangeExpression(); bool CheckPropertyCondition(); - virtual bool Evaluate(CValue* event); + virtual bool Evaluate(); virtual bool IsPositiveTrigger(); virtual CValue* FindIdentifier(const STR_String& identifiername); @@ -90,20 +90,21 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual int py_setattro(PyObject *attr, PyObject *value); /* 1. getType */ - KX_PYMETHOD_DOC(SCA_PropertySensor,GetType); + KX_PYMETHOD_DOC_NOARGS(SCA_PropertySensor,GetType); /* 2. setType */ - KX_PYMETHOD_DOC(SCA_PropertySensor,SetType); + KX_PYMETHOD_DOC_VARARGS(SCA_PropertySensor,SetType); /* 3. setProperty */ - KX_PYMETHOD_DOC(SCA_PropertySensor,SetProperty); + KX_PYMETHOD_DOC_VARARGS(SCA_PropertySensor,SetProperty); /* 4. getProperty */ - KX_PYMETHOD_DOC(SCA_PropertySensor,GetProperty); + KX_PYMETHOD_DOC_NOARGS(SCA_PropertySensor,GetProperty); /* 5. getValue */ - KX_PYMETHOD_DOC(SCA_PropertySensor,GetValue); + KX_PYMETHOD_DOC_NOARGS(SCA_PropertySensor,GetValue); /* 6. setValue */ - KX_PYMETHOD_DOC(SCA_PropertySensor,SetValue); + KX_PYMETHOD_DOC_VARARGS(SCA_PropertySensor,SetValue); /** * Test whether this is a sensible value (type check) */ diff --git a/source/gameengine/GameLogic/SCA_PythonController.cpp b/source/gameengine/GameLogic/SCA_PythonController.cpp index 2d200e0a238..212366e6526 100644 --- a/source/gameengine/GameLogic/SCA_PythonController.cpp +++ b/source/gameengine/GameLogic/SCA_PythonController.cpp @@ -48,12 +48,18 @@ SCA_PythonController* SCA_PythonController::m_sCurrentController = NULL; SCA_PythonController::SCA_PythonController(SCA_IObject* gameobj, + int mode, PyTypeObject* T) : SCA_IController(gameobj, T), m_bytecode(NULL), + m_function(NULL), + m_function_argc(0), m_bModified(true), + m_debug(false), + m_mode(mode), m_pythondictionary(NULL) { + } /* @@ -74,15 +80,12 @@ int SCA_PythonController::Release() SCA_PythonController::~SCA_PythonController() { - if (m_bytecode) - { - // - //printf("released python byte script\n"); - Py_DECREF(m_bytecode); - } + //printf("released python byte script\n"); - if (m_pythondictionary) - { + Py_XDECREF(m_bytecode); + Py_XDECREF(m_function); + + if (m_pythondictionary) { // break any circular references in the dictionary PyDict_Clear(m_pythondictionary); Py_DECREF(m_pythondictionary); @@ -94,8 +97,12 @@ SCA_PythonController::~SCA_PythonController() CValue* SCA_PythonController::GetReplica() { SCA_PythonController* replica = new SCA_PythonController(*this); - // Copy the compiled bytecode if possible. + + /* why is this needed at all??? - m_bytecode is NULL'd below so this doesnt make sense + * but removing it crashes blender (with YoFrankie). so leave in for now - Campbell */ Py_XINCREF(replica->m_bytecode); + + Py_XINCREF(replica->m_function); // this is ok since its not set to NULL replica->m_bModified = replica->m_bytecode == NULL; // The replica->m_pythondictionary is stolen - replace with a copy. @@ -110,7 +117,7 @@ CValue* SCA_PythonController::GetReplica() */ // this will copy properties and so on... - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); return replica; } @@ -150,14 +157,15 @@ int SCA_PythonController::IsTriggered(class SCA_ISensor* sensor) return 0; } -#if 0 -static const char* sPyGetCurrentController__doc__; -#endif - - -PyObject* SCA_PythonController::sPyGetCurrentController(PyObject* self) +/* warning, self is not the SCA_PythonController, its a PyObjectPlus_Proxy */ +PyObject* SCA_PythonController::sPyGetCurrentController(PyObject *self) { - return m_sCurrentController->AddRef(); + if(m_sCurrentController==NULL) + { + PyErr_SetString(PyExc_SystemError, "GameLogic.getCurrentController(), this function is being run outside the python controllers context, or blenders internal state is corrupt."); + return NULL; + } + return m_sCurrentController->GetProxy(); } SCA_IActuator* SCA_PythonController::LinkedActuatorFromPy(PyObject *value) @@ -170,16 +178,16 @@ SCA_IActuator* SCA_PythonController::LinkedActuatorFromPy(PyObject *value) if (PyString_Check(value)) { /* get the actuator from the name */ char *name= PyString_AsString(value); - for(it = lacts.begin(); it!= lacts.end(); it++) { + for(it = lacts.begin(); it!= lacts.end(); ++it) { if( name == (*it)->GetName() ) { return *it; } } } - else { - /* Expecting an actuator type */ - for(it = lacts.begin(); it!= lacts.end(); it++) { - if( static_cast<SCA_IActuator*>(value) == (*it) ) { + else if (BGE_PROXY_CHECK_TYPE(value)) { + PyObjectPlus *value_plus= BGE_PROXY_REF(value); + for(it = lacts.begin(); it!= lacts.end(); ++it) { + if( static_cast<SCA_IActuator*>(value_plus) == (*it) ) { return *it; } } @@ -187,49 +195,46 @@ SCA_IActuator* SCA_PythonController::LinkedActuatorFromPy(PyObject *value) /* set the exception */ PyObject *value_str = PyObject_Repr(value); /* new ref */ - PyErr_Format(PyExc_ValueError, "'%s' not in this controllers actuator list", PyString_AsString(value_str)); + PyErr_Format(PyExc_ValueError, "'%s' not in this python controllers actuator list", PyString_AsString(value_str)); Py_DECREF(value_str); return false; } -#if 0 -static const char* sPyAddActiveActuator__doc__; -#endif - -PyObject* SCA_PythonController::sPyAddActiveActuator( - - PyObject* self, - PyObject* args) +/* warning, self is not the SCA_PythonController, its a PyObjectPlus_Proxy */ +PyObject* SCA_PythonController::sPyAddActiveActuator(PyObject* self, PyObject* args) { + ShowDeprecationWarning("GameLogic.addActiveActuator(act, bool)", "controller.activate(act) or controller.deactivate(act)"); PyObject* ob1; int activate; - if (!PyArg_ParseTuple(args, "Oi", &ob1,&activate)) + if (!PyArg_ParseTuple(args, "Oi:addActiveActuator", &ob1,&activate)) return NULL; SCA_IActuator* actu = LinkedActuatorFromPy(ob1); if(actu==NULL) return NULL; - CValue* boolval = new CBoolValue(activate!=0); + bool boolval = (activate!=0); m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)actu,boolval); - boolval->Release(); Py_RETURN_NONE; } - const char* SCA_PythonController::sPyGetCurrentController__doc__ = "getCurrentController()"; const char* SCA_PythonController::sPyAddActiveActuator__doc__= "addActiveActuator(actuator,bool)"; -const char SCA_PythonController::GetActuators_doc[] = "getActuator"; PyTypeObject SCA_PythonController::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_PythonController", - sizeof(SCA_PythonController), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -252,57 +257,128 @@ PyMethodDef SCA_PythonController::Methods[] = { {"activate", (PyCFunction) SCA_PythonController::sPyActivate, METH_O}, {"deactivate", (PyCFunction) SCA_PythonController::sPyDeActivate, METH_O}, - {"getActuators", (PyCFunction) SCA_PythonController::sPyGetActuators, METH_NOARGS, (PY_METHODCHAR)SCA_PythonController::GetActuators_doc}, - {"getActuator", (PyCFunction) SCA_PythonController::sPyGetActuator, METH_O, (PY_METHODCHAR)SCA_PythonController::GetActuator_doc}, - {"getSensors", (PyCFunction) SCA_PythonController::sPyGetSensors, METH_NOARGS, (PY_METHODCHAR)SCA_PythonController::GetSensors_doc}, - {"getSensor", (PyCFunction) SCA_PythonController::sPyGetSensor, METH_O, (PY_METHODCHAR)SCA_PythonController::GetSensor_doc}, //Deprecated functions ------> {"setScript", (PyCFunction) SCA_PythonController::sPySetScript, METH_O}, {"getScript", (PyCFunction) SCA_PythonController::sPyGetScript, METH_NOARGS}, - {"getState", (PyCFunction) SCA_PythonController::sPyGetState, METH_NOARGS}, //<----- Deprecated {NULL,NULL} //Sentinel }; PyAttributeDef SCA_PythonController::Attributes[] = { + KX_PYATTRIBUTE_RW_FUNCTION("script", SCA_PythonController, pyattr_get_script, pyattr_set_script), { NULL } //Sentinel }; -bool SCA_PythonController::Compile() +void SCA_PythonController::ErrorPrint(const char *error_msg) { + // didn't compile, so instead of compile, complain + // something is wrong, tell the user what went wrong + printf("%s - controller \"%s\":\n", error_msg, GetName().Ptr()); + //PyRun_SimpleString(m_scriptText.Ptr()); + PyErr_Print(); + + /* Added in 2.48a, the last_traceback can reference Objects for example, increasing + * their user count. Not to mention holding references to wrapped data. + * This is especially bad when the PyObject for the wrapped data is free'd, after blender + * has alredy dealocated the pointer */ + PySys_SetObject( (char *)"last_traceback", NULL); + PyErr_Clear(); /* just to be sure */ +} + +bool SCA_PythonController::Compile() +{ //printf("py script modified '%s'\n", m_scriptName.Ptr()); + m_bModified= false; // if a script already exists, decref it before replace the pointer to a new script - if (m_bytecode) - { + if (m_bytecode) { Py_DECREF(m_bytecode); m_bytecode=NULL; } + // recompile the scripttext into bytecode m_bytecode = Py_CompileString(m_scriptText.Ptr(), m_scriptName.Ptr(), Py_file_input); - m_bModified=false; - if (m_bytecode) - { - + if (m_bytecode) { return true; + } else { + ErrorPrint("Python error compiling script"); + return false; } - else { - // didn't compile, so instead of compile, complain - // something is wrong, tell the user what went wrong - printf("Python compile error from controller \"%s\": \n", GetName().Ptr()); - //PyRun_SimpleString(m_scriptText.Ptr()); - PyErr_Print(); - - /* Added in 2.48a, the last_traceback can reference Objects for example, increasing - * their user count. Not to mention holding references to wrapped data. - * This is especially bad when the PyObject for the wrapped data is free'd, after blender - * has alredy dealocated the pointer */ - PySys_SetObject( (char *)"last_traceback", Py_None); - PyErr_Clear(); /* just to be sure */ +} + +bool SCA_PythonController::Import() +{ + //printf("py module modified '%s'\n", m_scriptName.Ptr()); + m_bModified= false; + + /* incase we re-import */ + Py_XDECREF(m_function); + m_function= NULL; + + vector<STR_String> py_function_path = m_scriptText.Explode('.'); + + if(py_function_path.size() < 2) { + printf("Python module name formatting error \"%s\":\n\texpected \"SomeModule.Func\", got \"%s\"\n", GetName().Ptr(), m_scriptText.Ptr()); + return false; + } + + PyObject *mod = PyImport_ImportModule((char *)py_function_path[0].Ptr()); + if(mod && m_debug) { + Py_DECREF(mod); /* getting a new one so dont hold a ref to the old one */ + mod= PyImport_ReloadModule(mod); + } + + if(mod==NULL) { + ErrorPrint("Python module not found"); + return false; + } + /* 'mod' will be DECREF'd as 'base' + * 'm_function' will be left holding a reference that the controller owns */ + + PyObject *base= mod; + + for(unsigned int i=1; i < py_function_path.size(); i++) { + m_function = PyObject_GetAttrString(base, py_function_path[i].Ptr()); + Py_DECREF(base); + base = m_function; /* for the next loop if there is on */ + if(m_function==NULL) { + PyErr_Clear(); /* print our own error below */ + break; + } + } + + if(m_function==NULL) { + printf("Python module error \"%s\":\n \"%s\" module found but function missing\n", GetName().Ptr(), m_scriptText.Ptr()); return false; } + + if(!PyCallable_Check(m_function)) { + Py_DECREF(m_function); + printf("Python module function error \"%s\":\n \"%s\" not callable\n", GetName().Ptr(), m_scriptText.Ptr()); + return false; + } + + m_function_argc = 0; /* rare cases this could be a function that isnt defined in python, assume zero args */ + if (PyFunction_Check(m_function)) { + PyObject *py_arg_count = PyObject_GetAttrString(PyFunction_GET_CODE(m_function), "co_argcount"); + if(py_arg_count) { + m_function_argc = PyLong_AsLong(py_arg_count); + Py_DECREF(py_arg_count); + } + else { + PyErr_Clear(); /* unlikely to fail but just incase */ + } + } + + if(m_function_argc > 1) { + Py_DECREF(m_function); + printf("Python module function has \"%s\":\n \"%s\" takes %d args, should be zero or 1 controller arg\n", GetName().Ptr(), m_scriptText.Ptr(), m_function_argc); + return false; + } + + return true; } void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr) @@ -310,16 +386,18 @@ void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr) m_sCurrentController = this; m_sCurrentLogicManager = logicmgr; - if (m_bModified) + PyObject *excdict= NULL; + PyObject* resultobj= NULL; + + switch(m_mode) { + case SCA_PYEXEC_SCRIPT: { - if (Compile()==false) // sets m_bModified to false + if (m_bModified) + if (Compile()==false) // sets m_bModified to false + return; + if (!m_bytecode) return; - } - if (!m_bytecode) { - return; - } - /* * This part here with excdict is a temporary patch * to avoid python/gameengine crashes when python @@ -338,10 +416,36 @@ void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr) * should always ensure excdict is cleared). */ - PyObject *excdict= PyDict_Copy(m_pythondictionary); - PyObject* resultobj = PyEval_EvalCode((PyCodeObject*)m_bytecode, - excdict, excdict); - + excdict= PyDict_Copy(m_pythondictionary); + resultobj = PyEval_EvalCode((PyCodeObject*)m_bytecode, excdict, excdict); + /* PyRun_SimpleString(m_scriptText.Ptr()); */ + break; + } + case SCA_PYEXEC_MODULE: + { + if (m_bModified || m_debug) + if (Import()==false) // sets m_bModified to false + return; + if (!m_function) + return; + + PyObject *args= NULL; + + if(m_function_argc==1) { + args = PyTuple_New(1); + PyTuple_SET_ITEM(args, 0, GetProxy()); + } + + resultobj = PyObject_CallObject(m_function, args); + Py_XDECREF(args); + break; + } + + } /* end switch */ + + + + /* Free the return value and print the error */ if (resultobj) { Py_DECREF(resultobj); @@ -349,24 +453,26 @@ void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr) else { // something is wrong, tell the user what went wrong - printf("Python script error from controller \"%s\": \n", GetName().Ptr()); + printf("Python script error from controller \"%s\":\n", GetName().Ptr()); PyErr_Print(); /* Added in 2.48a, the last_traceback can reference Objects for example, increasing * their user count. Not to mention holding references to wrapped data. * This is especially bad when the PyObject for the wrapped data is free'd, after blender * has alredy dealocated the pointer */ - PySys_SetObject( (char *)"last_traceback", Py_None); + PySys_SetObject( (char *)"last_traceback", NULL); PyErr_Clear(); /* just to be sure */ - - //PyRun_SimpleString(m_scriptText.Ptr()); } - - // clear after PyErrPrint - seems it can be using - // something in this dictionary and crash? - PyDict_Clear(excdict); - Py_DECREF(excdict); - m_triggeredSensors.erase(m_triggeredSensors.begin(), m_triggeredSensors.end()); + + if(excdict) /* Only for SCA_PYEXEC_SCRIPT types */ + { + /* clear after PyErrPrint - seems it can be using + * something in this dictionary and crash? */ + PyDict_Clear(excdict); + Py_DECREF(excdict); + } + + m_triggeredSensors.clear(); m_sCurrentController = NULL; } @@ -374,155 +480,47 @@ void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr) PyObject* SCA_PythonController::py_getattro(PyObject *attr) { - char *attr_str= PyString_AsString(attr); - if (!strcmp(attr_str,"state")) { - return PyInt_FromLong(m_statemask); - } - if (!strcmp(attr_str,"script")) { - return PyString_FromString(m_scriptText); - } py_getattro_up(SCA_IController); } +PyObject* SCA_PythonController::py_getattro_dict() { + py_getattro_dict_up(SCA_IController); +} + int SCA_PythonController::py_setattro(PyObject *attr, PyObject *value) { - char *attr_str= PyString_AsString(attr); - if (!strcmp(attr_str,"state")) { - PyErr_SetString(PyExc_AttributeError, "state is read only"); - return 1; - } - if (!strcmp(attr_str,"script")) { - char *scriptArg = PyString_AsString(value); - - if (scriptArg==NULL) { - PyErr_SetString(PyExc_TypeError, "expected a string (script name)"); - return -1; - } - - /* set scripttext sets m_bModified to true, - so next time the script is needed, a reparse into byte code is done */ - this->SetScriptText(scriptArg); - - return 1; - } - return SCA_IController::py_setattro(attr, value); + py_setattro_up(SCA_IController); } -PyObject* SCA_PythonController::PyActivate(PyObject* self, PyObject *value) +PyObject* SCA_PythonController::PyActivate(PyObject *value) { SCA_IActuator* actu = LinkedActuatorFromPy(value); if(actu==NULL) return NULL; - CValue* boolval = new CBoolValue(true); - m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)actu, boolval); - boolval->Release(); + m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)actu, true); Py_RETURN_NONE; } -PyObject* SCA_PythonController::PyDeActivate(PyObject* self, PyObject *value) +PyObject* SCA_PythonController::PyDeActivate(PyObject *value) { SCA_IActuator* actu = LinkedActuatorFromPy(value); if(actu==NULL) return NULL; - CValue* boolval = new CBoolValue(false); - m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)actu, boolval); - boolval->Release(); + m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)actu, false); Py_RETURN_NONE; } -PyObject* SCA_PythonController::PyGetActuators(PyObject* self) -{ - PyObject* resultlist = PyList_New(m_linkedactuators.size()); - for (unsigned int index=0;index<m_linkedactuators.size();index++) - { - PyList_SET_ITEM(resultlist,index,m_linkedactuators[index]->AddRef()); - } - - return resultlist; -} - -const char SCA_PythonController::GetSensor_doc[] = -"GetSensor (char sensorname) return linked sensor that is named [sensorname]\n"; -PyObject* -SCA_PythonController::PyGetSensor(PyObject* self, PyObject* value) -{ - - char *scriptArg = PyString_AsString(value); - if (scriptArg==NULL) { - PyErr_SetString(PyExc_TypeError, "expected a string (sensor name)"); - return NULL; - } - - for (unsigned int index=0;index<m_linkedsensors.size();index++) - { - SCA_ISensor* sensor = m_linkedsensors[index]; - STR_String realname = sensor->GetName(); - if (realname == scriptArg) - { - return sensor->AddRef(); - } - } - - char emsg[96]; - PyOS_snprintf( emsg, sizeof( emsg ), "Unable to find requested sensor \"%s\"", scriptArg ); - PyErr_SetString(PyExc_AttributeError, emsg); - return NULL; -} - - - -const char SCA_PythonController::GetActuator_doc[] = -"GetActuator (char sensorname) return linked actuator that is named [actuatorname]\n"; -PyObject* -SCA_PythonController::PyGetActuator(PyObject* self, PyObject* value) -{ - - char *scriptArg = PyString_AsString(value); - if (scriptArg==NULL) { - PyErr_SetString(PyExc_TypeError, "expected a string (actuator name)"); - return NULL; - } - - for (unsigned int index=0;index<m_linkedactuators.size();index++) - { - SCA_IActuator* actua = m_linkedactuators[index]; - if (actua->GetName() == scriptArg) - { - return actua->AddRef(); - } - } - - char emsg[96]; - PyOS_snprintf( emsg, sizeof( emsg ), "Unable to find requested actuator \"%s\"", scriptArg ); - PyErr_SetString(PyExc_AttributeError, emsg); - return NULL; -} - - -const char SCA_PythonController::GetSensors_doc[] = "getSensors returns a list of all attached sensors"; -PyObject* -SCA_PythonController::PyGetSensors(PyObject* self) -{ - PyObject* resultlist = PyList_New(m_linkedsensors.size()); - for (unsigned int index=0;index<m_linkedsensors.size();index++) - { - PyList_SET_ITEM(resultlist,index,m_linkedsensors[index]->AddRef()); - } - - return resultlist; -} - /* 1. getScript */ -PyObject* SCA_PythonController::PyGetScript(PyObject* self) +PyObject* SCA_PythonController::PyGetScript() { ShowDeprecationWarning("getScript()", "the script property"); return PyString_FromString(m_scriptText); } /* 2. setScript */ -PyObject* SCA_PythonController::PySetScript(PyObject* self, PyObject* value) +PyObject* SCA_PythonController::PySetScript(PyObject* value) { char *scriptArg = PyString_AsString(value); @@ -541,11 +539,29 @@ PyObject* SCA_PythonController::PySetScript(PyObject* self, PyObject* value) Py_RETURN_NONE; } -/* 1. getScript */ -PyObject* SCA_PythonController::PyGetState(PyObject* self) +PyObject* SCA_PythonController::pyattr_get_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) { - ShowDeprecationWarning("getState()", "the state property"); - return PyInt_FromLong(m_statemask); + SCA_PythonController* self= static_cast<SCA_PythonController*>(self_v); + return PyString_FromString(self->m_scriptText); } +int SCA_PythonController::pyattr_set_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value) +{ + SCA_PythonController* self= static_cast<SCA_PythonController*>(self_v); + + char *scriptArg = PyString_AsString(value); + + if (scriptArg==NULL) { + PyErr_SetString(PyExc_TypeError, "controller.script = string: Python Controller, expected a string script text"); + return -1; + } + + /* set scripttext sets m_bModified to true, + so next time the script is needed, a reparse into byte code is done */ + self->SetScriptText(scriptArg); + + return 0; +} + + /* eof */ diff --git a/source/gameengine/GameLogic/SCA_PythonController.h b/source/gameengine/GameLogic/SCA_PythonController.h index 3348071c00f..0c2af79c3a3 100644 --- a/source/gameengine/GameLogic/SCA_PythonController.h +++ b/source/gameengine/GameLogic/SCA_PythonController.h @@ -42,23 +42,37 @@ class SCA_IObject; class SCA_PythonController : public SCA_IController { Py_Header; - struct _object * m_bytecode; + struct _object * m_bytecode; /* SCA_PYEXEC_SCRIPT only */ + PyObject* m_function; /* SCA_PYEXEC_MODULE only */ + int m_function_argc; bool m_bModified; + bool m_debug; /* use with SCA_PYEXEC_MODULE for reloading every logic run */ + int m_mode; + protected: STR_String m_scriptText; STR_String m_scriptName; - PyObject* m_pythondictionary; + PyObject* m_pythondictionary; /* for SCA_PYEXEC_SCRIPT only */ + PyObject* m_pythonfunction; /* for SCA_PYEXEC_MODULE only */ + std::vector<class SCA_ISensor*> m_triggeredSensors; + + public: + enum SCA_PyExecMode + { + SCA_PYEXEC_SCRIPT = 0, + SCA_PYEXEC_MODULE, + SCA_PYEXEC_MAX + }; - public: static SCA_PythonController* m_sCurrentController; // protected !!! //for debugging //virtual CValue* AddRef(); //virtual int Release(); // Release a reference to this value (when reference count reaches 0, the value is removed from the heap) - SCA_PythonController(SCA_IObject* gameobj,PyTypeObject* T = &Type); + SCA_PythonController(SCA_IObject* gameobj, int mode, PyTypeObject* T = &Type); virtual ~SCA_PythonController(); virtual CValue* GetReplica(); @@ -67,10 +81,14 @@ class SCA_PythonController : public SCA_IController void SetScriptText(const STR_String& text); void SetScriptName(const STR_String& name); void SetDictionary(PyObject* pythondictionary); + void SetDebug(bool debug) { m_debug = debug; } void AddTriggeredSensor(class SCA_ISensor* sensor) { m_triggeredSensors.push_back(sensor); } int IsTriggered(class SCA_ISensor* sensor); bool Compile(); + bool Import(); + void ErrorPrint(const char *error_msg); + static const char* sPyGetCurrentController__doc__; static PyObject* sPyGetCurrentController(PyObject* self); @@ -78,19 +96,20 @@ class SCA_PythonController : public SCA_IController static PyObject* sPyAddActiveActuator(PyObject* self, PyObject* args); static SCA_IActuator* LinkedActuatorFromPy(PyObject *value); + virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual int py_setattro(PyObject *attr, PyObject *value); KX_PYMETHOD_O(SCA_PythonController,Activate); KX_PYMETHOD_O(SCA_PythonController,DeActivate); - KX_PYMETHOD_DOC_NOARGS(SCA_PythonController,GetSensors); - KX_PYMETHOD_DOC_NOARGS(SCA_PythonController,GetActuators); - KX_PYMETHOD_DOC_O(SCA_PythonController,GetSensor); - KX_PYMETHOD_DOC_O(SCA_PythonController,GetActuator); KX_PYMETHOD_O(SCA_PythonController,SetScript); KX_PYMETHOD_NOARGS(SCA_PythonController,GetScript); - KX_PYMETHOD_NOARGS(SCA_PythonController,GetState); + + + static PyObject* pyattr_get_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); + static int pyattr_set_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value); }; diff --git a/source/gameengine/GameLogic/SCA_RandomActuator.cpp b/source/gameengine/GameLogic/SCA_RandomActuator.cpp index 7af116fad99..2db871c77cf 100644 --- a/source/gameengine/GameLogic/SCA_RandomActuator.cpp +++ b/source/gameengine/GameLogic/SCA_RandomActuator.cpp @@ -78,8 +78,6 @@ CValue* SCA_RandomActuator::GetReplica() SCA_RandomActuator* replica = new SCA_RandomActuator(*this); // replication just copy the m_base pointer => common random generator replica->ProcessReplica(); - CValue::AddDataToReplica(replica); - return replica; } @@ -312,12 +310,17 @@ void SCA_RandomActuator::enforceConstraints() { /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_RandomActuator::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_RandomActuator", - sizeof(SCA_RandomActuator), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -341,12 +344,12 @@ PyParentObject SCA_RandomActuator::Parents[] = { PyMethodDef SCA_RandomActuator::Methods[] = { //Deprecated functions ------> {"setSeed", (PyCFunction) SCA_RandomActuator::sPySetSeed, METH_VARARGS, (PY_METHODCHAR)SetSeed_doc}, - {"getSeed", (PyCFunction) SCA_RandomActuator::sPyGetSeed, METH_VARARGS, (PY_METHODCHAR)GetSeed_doc}, - {"getPara1", (PyCFunction) SCA_RandomActuator::sPyGetPara1, METH_VARARGS, (PY_METHODCHAR)GetPara1_doc}, - {"getPara2", (PyCFunction) SCA_RandomActuator::sPyGetPara2, METH_VARARGS, (PY_METHODCHAR)GetPara2_doc}, - {"getDistribution", (PyCFunction) SCA_RandomActuator::sPyGetDistribution, METH_VARARGS, (PY_METHODCHAR)GetDistribution_doc}, + {"getSeed", (PyCFunction) SCA_RandomActuator::sPyGetSeed, METH_NOARGS, (PY_METHODCHAR)GetSeed_doc}, + {"getPara1", (PyCFunction) SCA_RandomActuator::sPyGetPara1, METH_NOARGS, (PY_METHODCHAR)GetPara1_doc}, + {"getPara2", (PyCFunction) SCA_RandomActuator::sPyGetPara2, METH_NOARGS, (PY_METHODCHAR)GetPara2_doc}, + {"getDistribution", (PyCFunction) SCA_RandomActuator::sPyGetDistribution, METH_NOARGS, (PY_METHODCHAR)GetDistribution_doc}, {"setProperty", (PyCFunction) SCA_RandomActuator::sPySetProperty, METH_VARARGS, (PY_METHODCHAR)SetProperty_doc}, - {"getProperty", (PyCFunction) SCA_RandomActuator::sPyGetProperty, METH_VARARGS, (PY_METHODCHAR)GetProperty_doc}, + {"getProperty", (PyCFunction) SCA_RandomActuator::sPyGetProperty, METH_NOARGS, (PY_METHODCHAR)GetProperty_doc}, //<----- Deprecated KX_PYMETHODTABLE(SCA_RandomActuator, setBoolConst), KX_PYMETHODTABLE_NOARGS(SCA_RandomActuator, setBoolUniform), @@ -386,24 +389,22 @@ int SCA_RandomActuator::pyattr_set_seed(void *self, const struct KX_PYATTRIBUTE_ act->m_base->SetSeed(ival); return 0; } else { - PyErr_SetString(PyExc_TypeError, "expected an integer"); + PyErr_SetString(PyExc_TypeError, "actuator.seed = int: Random Actuator, expected an integer"); return 1; } } PyObject* SCA_RandomActuator::py_getattro(PyObject *attr) { - PyObject* object = py_getattro_self(Attributes, this, attr); - if (object != NULL) - return object; py_getattro_up(SCA_IActuator); } +PyObject* SCA_RandomActuator::py_getattro_dict() { + py_getattro_dict_up(SCA_IActuator); +} + int SCA_RandomActuator::py_setattro(PyObject *attr, PyObject *value) { - int ret = py_setattro_self(Attributes, this, attr, value); - if (ret >= 0) - return ret; - return SCA_IActuator::py_setattro(attr, value); + py_setattro_up(SCA_IActuator); } /* 1. setSeed */ @@ -413,10 +414,10 @@ const char SCA_RandomActuator::SetSeed_doc[] = "\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_RandomActuator::PySetSeed(PyObject* self, PyObject* args, PyObject* kwds) { +PyObject* SCA_RandomActuator::PySetSeed(PyObject* args) { ShowDeprecationWarning("setSeed()", "the seed property"); long seedArg; - if(!PyArg_ParseTuple(args, "i", &seedArg)) { + if(!PyArg_ParseTuple(args, "i:setSeed", &seedArg)) { return NULL; } @@ -429,7 +430,8 @@ const char SCA_RandomActuator::GetSeed_doc[] = "getSeed()\n" "\tReturns the initial seed of the generator. Equal seeds produce\n" "\tequal series.\n"; -PyObject* SCA_RandomActuator::PyGetSeed(PyObject* self, PyObject* args, PyObject* kwds) { +PyObject* SCA_RandomActuator::PyGetSeed() +{ ShowDeprecationWarning("getSeed()", "the seed property"); return PyInt_FromLong(m_base->GetSeed()); } @@ -440,7 +442,8 @@ const char SCA_RandomActuator::GetPara1_doc[] = "\tReturns the first parameter of the active distribution. Refer\n" "\tto the documentation of the generator types for the meaning\n" "\tof this value."; -PyObject* SCA_RandomActuator::PyGetPara1(PyObject* self, PyObject* args, PyObject* kwds) { +PyObject* SCA_RandomActuator::PyGetPara1() +{ ShowDeprecationWarning("getPara1()", "the para1 property"); return PyFloat_FromDouble(m_parameter1); } @@ -451,7 +454,8 @@ const char SCA_RandomActuator::GetPara2_doc[] = "\tReturns the first parameter of the active distribution. Refer\n" "\tto the documentation of the generator types for the meaning\n" "\tof this value."; -PyObject* SCA_RandomActuator::PyGetPara2(PyObject* self, PyObject* args, PyObject* kwds) { +PyObject* SCA_RandomActuator::PyGetPara2() +{ ShowDeprecationWarning("getPara2()", "the para2 property"); return PyFloat_FromDouble(m_parameter2); } @@ -460,7 +464,8 @@ PyObject* SCA_RandomActuator::PyGetPara2(PyObject* self, PyObject* args, PyObjec const char SCA_RandomActuator::GetDistribution_doc[] = "getDistribution()\n" "\tReturns the type of the active distribution.\n"; -PyObject* SCA_RandomActuator::PyGetDistribution(PyObject* self, PyObject* args, PyObject* kwds) { +PyObject* SCA_RandomActuator::PyGetDistribution() +{ ShowDeprecationWarning("getDistribution()", "the distribution property"); return PyInt_FromLong(m_distribution); } @@ -471,10 +476,10 @@ const char SCA_RandomActuator::SetProperty_doc[] = "\t- name: string\n" "\tSet the property to which the random value is assigned. If the \n" "\tgenerator and property types do not match, the assignment is ignored.\n"; -PyObject* SCA_RandomActuator::PySetProperty(PyObject* self, PyObject* args, PyObject* kwds) { +PyObject* SCA_RandomActuator::PySetProperty(PyObject* args) { ShowDeprecationWarning("setProperty()", "the 'property' property"); char *nameArg; - if (!PyArg_ParseTuple(args, "s", &nameArg)) { + if (!PyArg_ParseTuple(args, "s:setProperty", &nameArg)) { return NULL; } @@ -494,7 +499,8 @@ const char SCA_RandomActuator::GetProperty_doc[] = "getProperty(name)\n" "\tReturn the property to which the random value is assigned. If the \n" "\tgenerator and property types do not match, the assignment is ignored.\n"; -PyObject* SCA_RandomActuator::PyGetProperty(PyObject* self, PyObject* args, PyObject* kwds) { +PyObject* SCA_RandomActuator::PyGetProperty() +{ ShowDeprecationWarning("getProperty()", "the 'property' property"); return PyString_FromString(m_propname); } @@ -506,7 +512,7 @@ KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setBoolConst, "\tSet this generator to produce a constant boolean value.\n") { int paraArg; - if(!PyArg_ParseTuple(args, "i", ¶Arg)) { + if(!PyArg_ParseTuple(args, "i:setBoolConst", ¶Arg)) { return NULL; } @@ -532,7 +538,7 @@ KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setBoolBernouilli, "\tReturn false value * 100%% of the time.\n") { float paraArg; - if(!PyArg_ParseTuple(args, "f", ¶Arg)) { + if(!PyArg_ParseTuple(args, "f:setBoolBernouilli", ¶Arg)) { return NULL; } @@ -548,7 +554,7 @@ KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setIntConst, "\tAlways return value\n") { int paraArg; - if(!PyArg_ParseTuple(args, "i", ¶Arg)) { + if(!PyArg_ParseTuple(args, "i:setIntConst", ¶Arg)) { return NULL; } @@ -566,7 +572,7 @@ KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setIntUniform, "\tupper_bound. The boundaries are included.\n") { int paraArg1, paraArg2; - if(!PyArg_ParseTuple(args, "ii", ¶Arg1, ¶Arg2)) { + if(!PyArg_ParseTuple(args, "ii:setIntUniform", ¶Arg1, ¶Arg2)) { return NULL; } @@ -585,7 +591,7 @@ KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setIntPoisson, "\tnumber of tries needed to achieve succes.\n") { float paraArg; - if(!PyArg_ParseTuple(args, "f", ¶Arg)) { + if(!PyArg_ParseTuple(args, "f:setIntPoisson", ¶Arg)) { return NULL; } @@ -601,7 +607,7 @@ KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setFloatConst, "\tAlways return value\n") { float paraArg; - if(!PyArg_ParseTuple(args, "f", ¶Arg)) { + if(!PyArg_ParseTuple(args, "f:setFloatConst", ¶Arg)) { return NULL; } @@ -619,7 +625,7 @@ KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setFloatUniform, "\tupper_bound.\n") { float paraArg1, paraArg2; - if(!PyArg_ParseTuple(args, "ff", ¶Arg1, ¶Arg2)) { + if(!PyArg_ParseTuple(args, "ff:setFloatUniform", ¶Arg1, ¶Arg2)) { return NULL; } @@ -638,7 +644,7 @@ KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setFloatNormal, "\tdeviation from the mean is characterized by standard_deviation.\n") { float paraArg1, paraArg2; - if(!PyArg_ParseTuple(args, "ff", ¶Arg1, ¶Arg2)) { + if(!PyArg_ParseTuple(args, "ff:setFloatNormal", ¶Arg1, ¶Arg2)) { return NULL; } @@ -656,7 +662,7 @@ KX_PYMETHODDEF_DOC_VARARGS(SCA_RandomActuator, setFloatNegativeExponential, "\tis characterized by half_life.\n") { float paraArg; - if(!PyArg_ParseTuple(args, "f", ¶Arg)) { + if(!PyArg_ParseTuple(args, "f:setFloatNegativeExponential", ¶Arg)) { return NULL; } diff --git a/source/gameengine/GameLogic/SCA_RandomActuator.h b/source/gameengine/GameLogic/SCA_RandomActuator.h index fbafbb69c01..310e8a7fbf9 100644 --- a/source/gameengine/GameLogic/SCA_RandomActuator.h +++ b/source/gameengine/GameLogic/SCA_RandomActuator.h @@ -97,19 +97,20 @@ class SCA_RandomActuator : public SCA_IActuator /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual int py_setattro(PyObject *attr, PyObject *value); static PyObject* pyattr_get_seed(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef); static int pyattr_set_seed(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value); // Deprecated methods -----> - KX_PYMETHOD_DOC(SCA_RandomActuator,SetSeed); - KX_PYMETHOD_DOC(SCA_RandomActuator,GetSeed); - KX_PYMETHOD_DOC(SCA_RandomActuator,GetPara1); - KX_PYMETHOD_DOC(SCA_RandomActuator,GetPara2); - KX_PYMETHOD_DOC(SCA_RandomActuator,GetDistribution); - KX_PYMETHOD_DOC(SCA_RandomActuator,SetProperty); - KX_PYMETHOD_DOC(SCA_RandomActuator,GetProperty); + KX_PYMETHOD_DOC_VARARGS(SCA_RandomActuator,SetSeed); + KX_PYMETHOD_DOC_NOARGS(SCA_RandomActuator,GetSeed); + KX_PYMETHOD_DOC_NOARGS(SCA_RandomActuator,GetPara1); + KX_PYMETHOD_DOC_NOARGS(SCA_RandomActuator,GetPara2); + KX_PYMETHOD_DOC_NOARGS(SCA_RandomActuator,GetDistribution); + KX_PYMETHOD_DOC_VARARGS(SCA_RandomActuator,SetProperty); + KX_PYMETHOD_DOC_NOARGS(SCA_RandomActuator,GetProperty); // <----- KX_PYMETHOD_DOC_VARARGS(SCA_RandomActuator, setBoolConst); diff --git a/source/gameengine/GameLogic/SCA_RandomEventManager.cpp b/source/gameengine/GameLogic/SCA_RandomEventManager.cpp index 156478d866d..976597aa812 100644 --- a/source/gameengine/GameLogic/SCA_RandomEventManager.cpp +++ b/source/gameengine/GameLogic/SCA_RandomEventManager.cpp @@ -50,9 +50,10 @@ SCA_RandomEventManager::SCA_RandomEventManager(class SCA_LogicManager* logicmgr) void SCA_RandomEventManager::NextFrame() { - for (set<class SCA_ISensor*>::const_iterator i= m_sensors.begin();!(i==m_sensors.end());i++) + SG_DList::iterator<SCA_ISensor> it(m_sensors); + for (it.begin();!it.end();++it) { - (*i)->Activate(m_logicmgr, NULL); + (*it)->Activate(m_logicmgr); } } diff --git a/source/gameengine/GameLogic/SCA_RandomSensor.cpp b/source/gameengine/GameLogic/SCA_RandomSensor.cpp index 84a9ef95e84..1581a29480e 100644 --- a/source/gameengine/GameLogic/SCA_RandomSensor.cpp +++ b/source/gameengine/GameLogic/SCA_RandomSensor.cpp @@ -76,7 +76,7 @@ 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... - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); return replica; } @@ -89,7 +89,7 @@ bool SCA_RandomSensor::IsPositiveTrigger() } -bool SCA_RandomSensor::Evaluate(CValue* event) +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 */ @@ -127,12 +127,17 @@ bool SCA_RandomSensor::Evaluate(CValue* event) /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_RandomSensor::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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(SCA_RandomSensor), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -154,46 +159,31 @@ PyParentObject SCA_RandomSensor::Parents[] = { }; PyMethodDef SCA_RandomSensor::Methods[] = { + //Deprecated functions -----> {"setSeed", (PyCFunction) SCA_RandomSensor::sPySetSeed, METH_VARARGS, (PY_METHODCHAR)SetSeed_doc}, - {"getSeed", (PyCFunction) SCA_RandomSensor::sPyGetSeed, METH_VARARGS, (PY_METHODCHAR)GetSeed_doc}, - {"getLastDraw", (PyCFunction) SCA_RandomSensor::sPyGetLastDraw, METH_VARARGS, (PY_METHODCHAR)GetLastDraw_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) { - PyObject* object = py_getattro_self(Attributes, this, attr); - if (object != NULL) - return object; - - char *attr_str= PyString_AsString(attr); - if (!strcmp(attr_str,"seed")) { - return PyInt_FromLong(m_basegenerator->GetSeed()); - } 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) { - int ret = py_setattro_self(Attributes, this, attr, value); - if (ret >= 0) - return ret; - char *attr_str= PyString_AsString(attr); - if (!strcmp(attr_str,"seed")) { - if (PyInt_Check(value)) { - int ival = PyInt_AsLong(value); - m_basegenerator->SetSeed(ival); - return 0; - } else { - PyErr_SetString(PyExc_TypeError, "expected an integer"); - return 1; - } - } - return SCA_ISensor::py_setattro(attr, value); + py_setattro_up(SCA_ISensor); } /* 1. setSeed */ @@ -203,10 +193,10 @@ const char SCA_RandomSensor::SetSeed_doc[] = "\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* self, PyObject* args, PyObject* kwds) { +PyObject* SCA_RandomSensor::PySetSeed(PyObject* args) { ShowDeprecationWarning("setSeed()", "the seed property"); long seedArg; - if(!PyArg_ParseTuple(args, "i", &seedArg)) { + if(!PyArg_ParseTuple(args, "i:setSeed", &seedArg)) { return NULL; } @@ -220,7 +210,7 @@ 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(PyObject* self, PyObject* args, PyObject* kwds) { +PyObject* SCA_RandomSensor::PyGetSeed() { ShowDeprecationWarning("getSeed()", "the seed property"); return PyInt_FromLong(m_basegenerator->GetSeed()); } @@ -229,9 +219,27 @@ PyObject* SCA_RandomSensor::PyGetSeed(PyObject* self, PyObject* args, PyObject* const char SCA_RandomSensor::GetLastDraw_doc[] = "getLastDraw()\n" "\tReturn the last value that was drawn.\n"; -PyObject* SCA_RandomSensor::PyGetLastDraw(PyObject* self, PyObject* args, PyObject* kwds) { +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 */ diff --git a/source/gameengine/GameLogic/SCA_RandomSensor.h b/source/gameengine/GameLogic/SCA_RandomSensor.h index 39d072dd316..27b41841f0b 100644 --- a/source/gameengine/GameLogic/SCA_RandomSensor.h +++ b/source/gameengine/GameLogic/SCA_RandomSensor.h @@ -52,7 +52,7 @@ public: PyTypeObject* T=&Type); virtual ~SCA_RandomSensor(); virtual CValue* GetReplica(); - virtual bool Evaluate(CValue* event); + virtual bool Evaluate(); virtual bool IsPositiveTrigger(); virtual void Init(); @@ -61,14 +61,18 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual int py_setattro(PyObject *attr, PyObject *value); /* 1. setSeed */ - KX_PYMETHOD_DOC(SCA_RandomSensor,SetSeed); + KX_PYMETHOD_DOC_VARARGS(SCA_RandomSensor,SetSeed); /* 2. getSeed */ - KX_PYMETHOD_DOC(SCA_RandomSensor,GetSeed); - /* 3. getSeed */ - KX_PYMETHOD_DOC(SCA_RandomSensor,GetLastDraw); + KX_PYMETHOD_DOC_NOARGS(SCA_RandomSensor,GetSeed); + /* 3. getLastDraw */ + KX_PYMETHOD_DOC_NOARGS(SCA_RandomSensor,GetLastDraw); + + static PyObject* pyattr_get_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); + static int pyattr_set_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value); }; diff --git a/source/gameengine/GameLogic/SCA_TimeEventManager.cpp b/source/gameengine/GameLogic/SCA_TimeEventManager.cpp index b7fadd3d62c..911ea772bef 100644 --- a/source/gameengine/GameLogic/SCA_TimeEventManager.cpp +++ b/source/gameengine/GameLogic/SCA_TimeEventManager.cpp @@ -52,7 +52,7 @@ SCA_TimeEventManager::SCA_TimeEventManager(SCA_LogicManager* logicmgr) SCA_TimeEventManager::~SCA_TimeEventManager() { for (vector<CValue*>::iterator it = m_timevalues.begin(); - !(it == m_timevalues.end()); it++) + !(it == m_timevalues.end()); ++it) { (*it)->Release(); } @@ -80,7 +80,7 @@ void SCA_TimeEventManager::NextFrame(double curtime, double fixedtime) // update sensors, but ... need deltatime ! for (vector<CValue*>::iterator it = m_timevalues.begin(); - !(it == m_timevalues.end()); it++) + !(it == m_timevalues.end()); ++it) { float newtime = (*it)->GetNumber() + fixedtime; floatval->SetFloat(newtime); @@ -104,7 +104,7 @@ void SCA_TimeEventManager::AddTimeProperty(CValue* timeval) void SCA_TimeEventManager::RemoveTimeProperty(CValue* timeval) { for (vector<CValue*>::iterator it = m_timevalues.begin(); - !(it == m_timevalues.end()); it++) + !(it == m_timevalues.end()); ++it) { if ((*it) == timeval) { diff --git a/source/gameengine/GameLogic/SCA_XNORController.cpp b/source/gameengine/GameLogic/SCA_XNORController.cpp index 10757e1c935..aee8e26c21a 100644 --- a/source/gameengine/GameLogic/SCA_XNORController.cpp +++ b/source/gameengine/GameLogic/SCA_XNORController.cpp @@ -66,7 +66,7 @@ void SCA_XNORController::Trigger(SCA_LogicManager* logicmgr) !(is==m_linkedsensors.end());is++) { SCA_ISensor* sensor = *is; - if (sensor->IsPositiveTrigger()) + if (sensor->GetState()) { if (sensorresult == false) { @@ -77,19 +77,12 @@ void SCA_XNORController::Trigger(SCA_LogicManager* logicmgr) } } - CValue* newevent = new CBoolValue(sensorresult); - for (vector<SCA_IActuator*>::const_iterator i=m_linkedactuators.begin(); !(i==m_linkedactuators.end());i++) { - SCA_IActuator* actua = *i;//m_linkedactuators.at(i); - logicmgr->AddActiveActuator(actua,newevent); + SCA_IActuator* actua = *i; + logicmgr->AddActiveActuator(actua,sensorresult); } - - // every actuator that needs the event, has a it's own reference to it now so - // release it (so to be clear: if there is no actuator, it's deleted right now) - newevent->Release(); - } @@ -98,7 +91,7 @@ CValue* SCA_XNORController::GetReplica() { CValue* replica = new SCA_XNORController(*this); // this will copy properties and so on... - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); return replica; } @@ -111,12 +104,17 @@ CValue* SCA_XNORController::GetReplica() /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_XNORController::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_XNORController", - sizeof(SCA_XNORController), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -149,4 +147,8 @@ PyObject* SCA_XNORController::py_getattro(PyObject *attr) { py_getattro_up(SCA_IController); } +PyObject* SCA_XNORController::py_getattro_dict() { + py_getattro_dict_up(SCA_IController); +} + /* eof */ diff --git a/source/gameengine/GameLogic/SCA_XNORController.h b/source/gameengine/GameLogic/SCA_XNORController.h index c992d5f1834..4aad5763cb0 100644 --- a/source/gameengine/GameLogic/SCA_XNORController.h +++ b/source/gameengine/GameLogic/SCA_XNORController.h @@ -49,6 +49,7 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); }; diff --git a/source/gameengine/GameLogic/SCA_XORController.cpp b/source/gameengine/GameLogic/SCA_XORController.cpp index d2290fe207a..5afb3a750f5 100644 --- a/source/gameengine/GameLogic/SCA_XORController.cpp +++ b/source/gameengine/GameLogic/SCA_XORController.cpp @@ -66,7 +66,7 @@ void SCA_XORController::Trigger(SCA_LogicManager* logicmgr) !(is==m_linkedsensors.end());is++) { SCA_ISensor* sensor = *is; - if (sensor->IsPositiveTrigger()) + if (sensor->GetState()) { if (sensorresult == true) { @@ -77,19 +77,12 @@ void SCA_XORController::Trigger(SCA_LogicManager* logicmgr) } } - CValue* newevent = new CBoolValue(sensorresult); - for (vector<SCA_IActuator*>::const_iterator i=m_linkedactuators.begin(); !(i==m_linkedactuators.end());i++) { - SCA_IActuator* actua = *i;//m_linkedactuators.at(i); - logicmgr->AddActiveActuator(actua,newevent); + SCA_IActuator* actua = *i; + logicmgr->AddActiveActuator(actua,sensorresult); } - - // every actuator that needs the event, has a it's own reference to it now so - // release it (so to be clear: if there is no actuator, it's deleted right now) - newevent->Release(); - } @@ -98,7 +91,7 @@ CValue* SCA_XORController::GetReplica() { CValue* replica = new SCA_XORController(*this); // this will copy properties and so on... - CValue::AddDataToReplica(replica); + replica->ProcessReplica(); return replica; } @@ -111,12 +104,17 @@ CValue* SCA_XORController::GetReplica() /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_XORController::Type = { - PyObject_HEAD_INIT(NULL) - 0, +#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_XORController", - sizeof(SCA_XORController), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -149,4 +147,8 @@ PyObject* SCA_XORController::py_getattro(PyObject *attr) { py_getattro_up(SCA_IController); } +PyObject* SCA_XORController::py_getattro_dict() { + py_getattro_dict_up(SCA_IController); +} + /* eof */ diff --git a/source/gameengine/GameLogic/SCA_XORController.h b/source/gameengine/GameLogic/SCA_XORController.h index 065b31fd901..feb9f2ed07c 100644 --- a/source/gameengine/GameLogic/SCA_XORController.h +++ b/source/gameengine/GameLogic/SCA_XORController.h @@ -49,6 +49,7 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); }; diff --git a/source/gameengine/GameLogic/SConscript b/source/gameengine/GameLogic/SConscript index ebf225f728f..aadc4154298 100644 --- a/source/gameengine/GameLogic/SConscript +++ b/source/gameengine/GameLogic/SConscript @@ -5,7 +5,7 @@ sources = env.Glob('*.cpp') + env.Glob('Joystick/*.cpp') incs = '. #/source/kernel/gen_system #/intern/string' incs += ' #/source/gameengine/Expressions #/intern/moto/include' -incs += ' #/source/gameengine/Rasterizer' +incs += ' #/source/gameengine/Rasterizer #/source/gameengine/SceneGraph' incs += ' ' + env['BF_PYTHON_INC'] |