[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [15306] trunk/blender: BGE patch: add state engine support in the logic bricks.
Benoit Bolsee
benoit.bolsee at online.be
Sun Jun 22 16:24:55 CEST 2008
Revision: 15306
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15306
Author: ben2610
Date: 2008-06-22 16:23:57 +0200 (Sun, 22 Jun 2008)
Log Message:
-----------
BGE patch: add state engine support in the logic bricks.
This patch introduces a simple state engine system with the logic bricks. This system features full
backward compatibility, multiple active states, multiple state transitions, automatic disabling of
sensor and actuators, full GUI support and selective display of sensors and actuators.
Note: Python API is available but not documented yet. It will be added asap.
State internals
===============
The state system is object based. The current state mask is stored in the object as a 32 bit value;
each bit set in the mask is an active state. The controllers have a state mask too but only one bit
can be set: a controller belongs to a single state. The game engine will only execute controllers
that belong to active states. Sensors and actuators don't have a state mask but are effectively
attached to states via their links to the controllers. Sensors and actuators can be connected to more
than one state. When a controller becomes inactive because of a state change, its links to sensors
and actuators are temporarily broken (until the state becomes active again). If an actuator gets isolated,
i.e all the links to controllers are broken, it is automatically disabled. If a sensor gets isolated,
the game engine will stop calling it to save CPU. It will also reset the sensor internal state so that
it can react as if the game just started when it gets reconnected to an active controller. For example,
an Always sensor in no pulse mode that is connected to a single state (i.e connected to one or more
controllers of a single state) will generate a pulse each time the state becomes active. This feature is
not available on all sensors, see the notes below.
GUI
===
This system system is fully configurable through the GUI: the object state mask is visible under the
object bar in the controller's colum as an array of buttons just like the 3D view layer mask.
Click on a state bit to only display the controllers of that state. You can select more than one state
with SHIFT-click. The All button sets all the bits so that you can see all the controllers of the object.
The Ini button sets the state mask back to the object default state. You can change the default state
of object by first selecting the desired state mask and storing using the menu under the State button.
If you define a default state mask, it will be loaded into the object state make when you load the blend
file or when you run the game under the blenderplayer. However, when you run the game under Blender,
the current selected state mask will be used as the startup state for the object. This allows you to test
specific state during the game design.
The controller display the state they belong to with a new button in the controller header. When you add
a new controller, it is added by default in the lowest enabled state. You can change the controller state
by clicking on the button and selecting another state. If more than one state is enabled in the object
state mask, controllers are grouped by state for more readibility.
The new Sta button in the sensor and actuator column header allows you to display only the sensors and
actuators that are linked to visible controllers.
A new state actuator is available to modify the state during the game. It defines a bit mask and
the operation to apply on the current object state mask:
Cpy: the bit mask is copied to the object state mask.
Add: the bits that set in the bit mask will be turned on in the object state mask.
Sub: the bits that set in the bit mask will be turned off in the object state mask.
Inv: the bits that set in the bit mask will be inverted in the objecyy state mask.
Notes
=====
- Although states have no name, a simply convention consists in using the name of the first controller
of the state as the state name. The GUI will support that convention by displaying as a hint the name
of the first controller of the state when you move the mouse over a state bit of the object state mask
or of the state actuator bit mask.
- Each object has a state mask and each object can have a state engine but if several objects are
part of a logical group, it is recommended to put the state engine only in the main object and to
link the controllers of that object to the sensors and actuators of the different objects.
- When loading an old blend file, the state mask of all objects and controllers are initialized to 1
so that all the controllers belong to this single state. This ensures backward compatibility with
existing game.
- When the state actuator is activated at the same time as other actuators, these actuators are
guaranteed to execute before being eventually disabled due to the state change. This is useful for
example to send a message or update a property at the time of changing the state.
- Sensors that depend on underlying resource won't reset fully when they are isolated. By the time they
are acticated again, they will behave as follow:
* keyboard sensor: keys already pressed won't be detected. The keyboard sensor is only sensitive
to new key press.
* collision sensor: objects already colliding won't be detected. Only new collisions are
detected.
* near and radar sensor: same as collision sensor.
Modified Paths:
--------------
trunk/blender/projectfiles_vc7/gameengine/ketsji/KX_ketsji.vcproj
trunk/blender/source/blender/blenkernel/intern/sca.c
trunk/blender/source/blender/blenloader/intern/readfile.c
trunk/blender/source/blender/blenloader/intern/writefile.c
trunk/blender/source/blender/include/BIF_butspace.h
trunk/blender/source/blender/include/BIF_interface.h
trunk/blender/source/blender/include/butspace.h
trunk/blender/source/blender/makesdna/DNA_actuator_types.h
trunk/blender/source/blender/makesdna/DNA_controller_types.h
trunk/blender/source/blender/makesdna/DNA_object_types.h
trunk/blender/source/blender/makesdna/DNA_sensor_types.h
trunk/blender/source/blender/src/buttons_logic.c
trunk/blender/source/blender/src/interface.c
trunk/blender/source/gameengine/Converter/BL_BlenderDataConversion.cpp
trunk/blender/source/gameengine/Converter/KX_ConvertActuators.cpp
trunk/blender/source/gameengine/Converter/KX_ConvertControllers.cpp
trunk/blender/source/gameengine/GameLogic/SCA_AlwaysSensor.cpp
trunk/blender/source/gameengine/GameLogic/SCA_AlwaysSensor.h
trunk/blender/source/gameengine/GameLogic/SCA_IActuator.cpp
trunk/blender/source/gameengine/GameLogic/SCA_IActuator.h
trunk/blender/source/gameengine/GameLogic/SCA_IController.cpp
trunk/blender/source/gameengine/GameLogic/SCA_IController.h
trunk/blender/source/gameengine/GameLogic/SCA_IObject.cpp
trunk/blender/source/gameengine/GameLogic/SCA_IObject.h
trunk/blender/source/gameengine/GameLogic/SCA_ISensor.cpp
trunk/blender/source/gameengine/GameLogic/SCA_ISensor.h
trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.cpp
trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.h
trunk/blender/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp
trunk/blender/source/gameengine/GameLogic/SCA_KeyboardSensor.h
trunk/blender/source/gameengine/GameLogic/SCA_LogicManager.cpp
trunk/blender/source/gameengine/GameLogic/SCA_MouseSensor.cpp
trunk/blender/source/gameengine/GameLogic/SCA_MouseSensor.h
trunk/blender/source/gameengine/GameLogic/SCA_PropertySensor.cpp
trunk/blender/source/gameengine/GameLogic/SCA_PropertySensor.h
trunk/blender/source/gameengine/GameLogic/SCA_RandomSensor.cpp
trunk/blender/source/gameengine/GameLogic/SCA_RandomSensor.h
trunk/blender/source/gameengine/Ketsji/KXNetwork/KX_NetworkMessageSensor.cpp
trunk/blender/source/gameengine/Ketsji/KXNetwork/KX_NetworkMessageSensor.h
trunk/blender/source/gameengine/Ketsji/KX_MouseFocusSensor.cpp
trunk/blender/source/gameengine/Ketsji/KX_MouseFocusSensor.h
trunk/blender/source/gameengine/Ketsji/KX_RadarSensor.cpp
trunk/blender/source/gameengine/Ketsji/KX_RaySensor.cpp
trunk/blender/source/gameengine/Ketsji/KX_RaySensor.h
trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp
trunk/blender/source/gameengine/Ketsji/KX_TouchSensor.cpp
trunk/blender/source/gameengine/Ketsji/KX_TouchSensor.h
Added Paths:
-----------
trunk/blender/source/gameengine/Ketsji/KX_StateActuator.cpp
trunk/blender/source/gameengine/Ketsji/KX_StateActuator.h
Modified: trunk/blender/projectfiles_vc7/gameengine/ketsji/KX_ketsji.vcproj
===================================================================
--- trunk/blender/projectfiles_vc7/gameengine/ketsji/KX_ketsji.vcproj 2008-06-22 02:08:46 UTC (rev 15305)
+++ trunk/blender/projectfiles_vc7/gameengine/ketsji/KX_ketsji.vcproj 2008-06-22 14:23:57 UTC (rev 15306)
@@ -475,6 +475,9 @@
RelativePath="..\..\..\source\gameengine\Ketsji\KX_SoundActuator.cpp">
</File>
<File
+ RelativePath="..\..\..\source\gameengine\Ketsji\KX_StateActuator.cpp">
+ </File>
+ <File
RelativePath="..\..\..\source\gameengine\Ketsji\KX_TrackToActuator.cpp">
</File>
<File
@@ -696,6 +699,9 @@
RelativePath="..\..\..\source\gameengine\Ketsji\KX_SoundActuator.h">
</File>
<File
+ RelativePath="..\..\..\source\gameengine\Ketsji\KX_StateActuator.h">
+ </File>
+ <File
RelativePath="..\..\..\source\gameengine\Ketsji\KX_TrackToActuator.h">
</File>
<File
Modified: trunk/blender/source/blender/blenkernel/intern/sca.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/sca.c 2008-06-22 02:08:46 UTC (rev 15305)
+++ trunk/blender/source/blender/blenkernel/intern/sca.c 2008-06-22 14:23:57 UTC (rev 15306)
@@ -465,6 +465,9 @@
case ACT_PARENT:
act->data = MEM_callocN(sizeof( bParentActuator ), "parent act");
break;
+ case ACT_STATE:
+ act->data = MEM_callocN(sizeof( bStateActuator ), "state act");
+ break;
default:
; /* this is very severe... I cannot make any memory for this */
/* logic brick... */
Modified: trunk/blender/source/blender/blenloader/intern/readfile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/readfile.c 2008-06-22 02:08:46 UTC (rev 15305)
+++ trunk/blender/source/blender/blenloader/intern/readfile.c 2008-06-22 14:23:57 UTC (rev 15306)
@@ -3011,6 +3011,9 @@
bParentActuator *parenta = act->data;
parenta->ob = newlibadr(fd, ob->id.lib, parenta->ob);
}
+ else if(act->type==ACT_STATE) {
+ /* bStateActuator *statea = act->data; */
+ }
act= act->next;
}
@@ -3307,11 +3310,19 @@
direct_link_constraints(fd, &ob->constraints);
link_glob_list(fd, &ob->controllers);
+ if (ob->init_state) {
+ /* if a known first state is specified, set it so that the game will start ok */
+ ob->state = ob->init_state;
+ } else if (!ob->state) {
+ ob->state = 1;
+ }
cont= ob->controllers.first;
while(cont) {
cont->data= newdataadr(fd, cont->data);
cont->links= newdataadr(fd, cont->links);
test_pointer_array(fd, (void **)&cont->links);
+ if (cont->state_mask == 0)
+ cont->state_mask = 1;
cont= cont->next;
}
@@ -7635,6 +7646,7 @@
}
}
+
/* WATCH IT!!!: pointers from libdata have not been converted yet here! */
/* WATCH IT 2!: Userdef struct init has to be in src/usiblender.c! */
Modified: trunk/blender/source/blender/blenloader/intern/writefile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/writefile.c 2008-06-22 02:08:46 UTC (rev 15305)
+++ trunk/blender/source/blender/blenloader/intern/writefile.c 2008-06-22 14:23:57 UTC (rev 15306)
@@ -715,6 +715,9 @@
case ACT_PARENT:
writestruct(wd, DATA, "bParentActuator", 1, act->data);
break;
+ case ACT_STATE:
+ writestruct(wd, DATA, "bStateActuator", 1, act->data);
+ break;
default:
; /* error: don't know how to write this file */
}
Modified: trunk/blender/source/blender/include/BIF_butspace.h
===================================================================
--- trunk/blender/source/blender/include/BIF_butspace.h 2008-06-22 02:08:46 UTC (rev 15305)
+++ trunk/blender/source/blender/include/BIF_butspace.h 2008-06-22 14:23:57 UTC (rev 15306)
@@ -99,6 +99,8 @@
#define BUTS_ACT_SEL 64
#define BUTS_ACT_ACT 128
#define BUTS_ACT_LINK 256
+#define BUTS_SENS_STATE 512
+#define BUTS_ACT_STATE 1024
/* buttons grid */
Modified: trunk/blender/source/blender/include/BIF_interface.h
===================================================================
--- trunk/blender/source/blender/include/BIF_interface.h 2008-06-22 02:08:46 UTC (rev 15305)
+++ trunk/blender/source/blender/include/BIF_interface.h 2008-06-22 14:23:57 UTC (rev 15306)
@@ -185,6 +185,7 @@
void uiGetMouse(int win, short *adr);
void uiComposeLinks(uiBlock *block);
void uiSetButLock(int val, char *lockstr);
+uiBut *uiFindInlink(uiBlock *block, void *poin);
void uiClearButLock(void);
int uiDoBlocks(struct ListBase *lb, int event, int movemouse_quit);
void uiSetCurFont(uiBlock *block, int index);
Modified: trunk/blender/source/blender/include/butspace.h
===================================================================
--- trunk/blender/source/blender/include/butspace.h 2008-06-22 02:08:46 UTC (rev 15305)
+++ trunk/blender/source/blender/include/butspace.h 2008-06-22 14:23:57 UTC (rev 15306)
@@ -52,6 +52,8 @@
#define BUTS_ACT_SEL 64
#define BUTS_ACT_ACT 128
#define BUTS_ACT_LINK 256
+#define BUTS_SENS_STATE 512
+#define BUTS_ACT_STATE 1024
/* internal */
@@ -583,6 +585,8 @@
#define B_SETACTOR 2715
#define B_SETMAINACTOR 2716
#define B_SETDYNA 2717
+#define B_SET_STATE_BIT 2718
+#define B_INIT_STATE_BIT 2719
/* *********************** */
#define B_FPAINTBUTS 2900
Modified: trunk/blender/source/blender/makesdna/DNA_actuator_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_actuator_types.h 2008-06-22 02:08:46 UTC (rev 15305)
+++ trunk/blender/source/blender/makesdna/DNA_actuator_types.h 2008-06-22 14:23:57 UTC (rev 15306)
@@ -208,6 +208,11 @@
struct Object *ob;
} bParentActuator;
+typedef struct bStateActuator {
+ int type; /* 0=Set, 1=Add, 2=Rem, 3=Chg */
+ unsigned int mask; /* the bits to change */
+} bStateActuator;
+
typedef struct bActuator {
struct bActuator *next, *prev, *mynew;
short type;
@@ -279,11 +284,14 @@
#define ACT_2DFILTER 19
#define ACT_PARENT 20
#define ACT_SHAPEACTION 21
+#define ACT_STATE 22
/* actuator flag */
#define ACT_SHOW 1
#define ACT_DEL 2
#define ACT_NEW 4
+#define ACT_LINKED 8
+#define ACT_VISIBLE 16
/* link codes */
#define LINK_SENSOR 0
Modified: trunk/blender/source/blender/makesdna/DNA_controller_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_controller_types.h 2008-06-22 02:08:46 UTC (rev 15305)
+++ trunk/blender/source/blender/makesdna/DNA_controller_types.h 2008-06-22 14:23:57 UTC (rev 15306)
@@ -57,7 +57,7 @@
struct bSensor **slinks;
short val, valo;
- int pad5;
+ unsigned int state_mask;
} bController;
@@ -71,6 +71,7 @@
#define CONT_SHOW 1
#define CONT_DEL 2
#define CONT_NEW 4
+#define CONT_MASK 8
#endif
Modified: trunk/blender/source/blender/makesdna/DNA_object_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_object_types.h 2008-06-22 02:08:46 UTC (rev 15305)
+++ trunk/blender/source/blender/makesdna/DNA_object_types.h 2008-06-22 14:23:57 UTC (rev 15306)
@@ -216,7 +216,9 @@
struct DerivedMesh *derivedDeform, *derivedFinal;
int lastDataMask; /* the custom data layer mask that was last used to calculate derivedDeform and derivedFinal */
- int pad;
+ unsigned int state; /* bit masks of game controllers that are active */
+ unsigned int init_state; /* bit masks of initial state as recorded by the users */
+ int pad2;
/*#ifdef WITH_VERSE*/
void *vnode; /* pointer at object VerseNode */
@@ -440,6 +442,8 @@
#define OB_ADDCONT 512
#define OB_ADDACT 1024
#define OB_SHOWCONT 2048
+#define OB_SETSTBIT 4096
+#define OB_INITSTBIT 8192
/* ob->restrictflag */
#define OB_RESTRICT_VIEW 1
Modified: trunk/blender/source/blender/makesdna/DNA_sensor_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_sensor_types.h 2008-06-22 02:08:46 UTC (rev 15305)
+++ trunk/blender/source/blender/makesdna/DNA_sensor_types.h 2008-06-22 14:23:57 UTC (rev 15306)
@@ -202,6 +202,7 @@
#define SENS_DEL 2
#define SENS_NEW 4
#define SENS_NOT 8
+#define SENS_VISIBLE 16
/* sensor->pulse */
#define SENS_PULSE_CONT 0
Modified: trunk/blender/source/blender/src/buttons_logic.c
===================================================================
--- trunk/blender/source/blender/src/buttons_logic.c 2008-06-22 02:08:46 UTC (rev 15305)
+++ trunk/blender/source/blender/src/buttons_logic.c 2008-06-22 14:23:57 UTC (rev 15306)
@@ -88,6 +88,7 @@
#include "mydevice.h"
#include "nla.h" /* For __NLA : Important, do not remove */
#include "butspace.h" // own module
+#include "interface.h"
/* internals */
void buttons_enji(uiBlock *, Object *);
@@ -228,7 +229,7 @@
bSensor *sens_to_delete= datav;
int val;
Base *base;
- bSensor *sens;
+ bSensor *sens, *tmp;
val= pupmenu("Move up%x1|Move down %x2");
@@ -245,12 +246,24 @@
if(sens) {
if( val==1 && sens->prev) {
- BLI_remlink(&base->object->sensors, sens);
- BLI_insertlinkbefore(&base->object->sensors, sens->prev, sens);
+ for (tmp=sens->prev; tmp; tmp=tmp->prev) {
+ if (tmp->flag & SENS_VISIBLE)
+ break;
+ }
+ if (tmp) {
+ BLI_remlink(&base->object->sensors, sens);
+ BLI_insertlinkbefore(&base->object->sensors, tmp, sens);
+ }
}
else if( val==2 && sens->next) {
- BLI_remlink(&base->object->sensors, sens);
- BLI_insertlink(&base->object->sensors, sens->next, sens);
+ for (tmp=sens->next; tmp; tmp=tmp->next) {
+ if (tmp->flag & SENS_VISIBLE)
+ break;
+ }
+ if (tmp) {
+ BLI_remlink(&base->object->sensors, sens);
+ BLI_insertlink(&base->object->sensors, tmp, sens);
+ }
}
BIF_undo_push("Move sensor");
allqueue(REDRAWBUTSLOGIC, 0);
@@ -267,7 +280,7 @@
bController *controller_to_del= datav;
int val;
Base *base;
- bController *cont;
+ bController *cont, *tmp;
val= pupmenu("Move up%x1|Move down %x2");
@@ -284,12 +297,27 @@
if(cont) {
if( val==1 && cont->prev) {
- BLI_remlink(&base->object->controllers, cont);
- BLI_insertlinkbefore(&base->object->controllers, cont->prev, cont);
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list