Commit Graph

195 Commits

Author SHA1 Message Date
Campbell Barton
1a16fb1953 BGE Py API
use PY_SET_ATTR_FAIL and PY_SET_ATTR_SUCCESS return values so the fake subclassing can know if a value failed to be set or if it was missing from the type. (with PY_SET_ATTR_MISSING)

Also noticed some other mistakes.
- KX_LightObject, setting the type didnt check for an int.
- KX_SoundActuator, didnt return an error when assigning an invalid orientation value
- KX_GameObject, worldOrientation didnt return an error value.
2009-05-19 07:16:40 +00:00
Benoit Bolsee
9a40f4d2a6 BGE: bookmark option on controller to make them run before all other controllers.
A new bookmark button is available on the controller UI. 
When set, the controller is guaranteed to execute before all
other non-bookmarked controllers, provided it is scheduled 
for execution. 

This is useful for initialization scripts that run once at startup or
scripts that must set some prerequisite for the other controllers at
the start of each logic frame.

This feature is also available at python level with the "bookmark"
attribute. It can be changed during the game.

Note that if several script are bookmarked, their relative order of 
execution is not guaranteed. Make sure they don't depend on each other.
2009-05-17 21:50:31 +00:00
Campbell Barton
41acd3b81c While testing YoFrankie with the new API attributes found some issues...
- corrections to docs
- disallow calling controller.activate(actuator) when the controller is not active. (Raise a SystemError)
- Added 2 new attributes, CValue.name - deprecates CValue.getName(), KX_GameObject.children deprecated KX_GameObject.getChildren(), (same for getChildrenRecursive()).
2009-05-17 16:30:18 +00:00
Benoit Bolsee
3ea1c1b4b6 BGE: new sensor object to generalize Near and Radar sensor, static-static collision capbility.
A new type of "Sensor" physics object is available in the GE for advanced
collision management. It's called Sensor for its similarities with the
physics objects that underlie the Near and Radar sensors.
Like the Near and Radar object it is:
- static and ghost
- invisible by default
- always active to ensure correct collision detection
- capable of detecting both static and dynamic objects
- ignoring collision with their parent
- capable of broadphase filtering based on:
  * Actor option: the collisioning object must have the Actor flag set to be detected
  * property/material: as specified in the collision sensors attached to it
  Broadphase filtering is important for performance reason: the collision points
  will be computed only for the objects that pass the broahphase filter.
- automatically removed from the simulation when no collision sensor is active on it

Unlike the Near and Radar object it can:
- take any shape, including triangle mesh
- be made visible for debugging (just use the Visible actuator)
- have multiple collision sensors using it

Other than that, the sensor objects are ordinary objects. You can move them
freely or parent them. When parented to a dynamic object, they can provide
advanced collision control to this object.

The type of collision capability depends on the shape:
- box, sphere, cylinder, cone, convex hull provide volume detection.
- triangle mesh provides surface detection but you can give some volume
  to the suface by increasing the margin in the Advanced Settings panel.
  The margin applies on both sides of the surface.

Performance tip:
- Sensor objects perform better than Near and Radar: they do less synchronizations
  because of the Scenegraph optimizations and they can have multiple collision sensors
  on them (with different property filtering for example).
- Always prefer simple shape (box, sphere) to complex shape whenever possible.
- Always use broadphase filtering (avoid collision sensor with empty propery/material)
- Use collision sensor only when you need them. When no collision sensor is active
  on the sensor object, it is removed from the simulation and consume no CPU.

Known limitations:
- When running Blender in debug mode, you will see one warning line of the console:
  "warning btCollisionDispatcher::needsCollision: static-static collision!"
  In release mode this message is not printed.
- Collision margin has no effect on sphere, cone and cylinder shape.

Other performance improvements:
- Remove unnecessary interpolation for Near and Radar objects and by extension
  sensor objects.
- Use direct matrix copy instead of quaternion to synchronize orientation.

Other bug fix:
- Fix Near/Radar position error on newly activated objects. This was causing
  several detection problems in YoFrankie
- Fix margin not passed correctly to gImpact shape.
- Disable force/velocity actions on static objects
2009-05-17 12:51:51 +00:00
Benoit Bolsee
ef8f92ffe6 BGE: fix a compatibility problem since logic patch with YoFrankie (and other games I guess). State actuators will now execute before any other actuators to make sure that the actuators link count are up to date when they execute. 2009-05-15 20:51:32 +00:00
Campbell Barton
b8657fd648 Name attributes added since 2.48a more consistently.
BL_ActionActuator::blendin -> blendIn
BL_ActionActuator::end -> frameEnd
BL_ActionActuator::property -> propName
BL_ActionActuator::start -> frameStart
BL_ActionActuator::type -> mode
BL_ShapeActionActuator::blendin -> blendIn
BL_ShapeActionActuator::end -> frameEnd
BL_ShapeActionActuator::frameProperty -> framePropName
BL_ShapeActionActuator::property -> propName
BL_ShapeActionActuator::start -> frameStart
BL_ShapeActionActuator::type -> mode
KX_CameraActuator::xy -> useXY
KX_ConstraintActuator::property -> propName
KX_GameActuator::file -> fileName
KX_GameObject::localScaling -> localScaling
KX_GameObject::worldScaling -> worldScaling
KX_IpoActuator::endFrame -> frameEnd
KX_IpoActuator::startFrame -> frameStart
KX_IpoActuator::type -> mode
KX_RaySensor::property -> propName
KX_SCA_DynamicActuator::operation -> mode
KX_Scene::objects_inactive -> objectsInactive
KX_SoundActuator::filename -> fileName
KX_SoundActuator::type -> mode
KX_TouchSensor::objectHit -> hitObject
KX_TouchSensor::objectHitList -> hitObjectList
KX_TouchSensor::property -> propName
KX_TouchSensor::pulseCollisions -> usePulseCollision
KX_VisibilityActuator::occlusion -> useOcclusion
KX_VisibilityActuator::recursion -> useRecursion
SCA_2DFilterActuator::passNb -> passNumber
SCA_PropertyActuator::property -> propName
SCA_PropertyActuator::type -> mode
SCA_PropertySensor::property -> propName
SCA_PropertySensor::type -> mode
SCA_RandomActuator::property -> propName
2009-05-15 03:26:53 +00:00
Campbell Barton
e8c0440660 minor change - mistake in joystick fix, util function for getting attrs from BGE types 2009-05-15 02:03:27 +00:00
Campbell Barton
a20a2ebb9f [#18749] BGE: Crash on joysticksensor.getAxisValue()
negative index would be used when the joystick was not found, crashing python api, initialize these as 0 now.
2009-05-14 21:06:48 +00:00
Diego Borghetti
0c6ec76a4c Fix Makefiles for gameengine. 2009-05-11 15:34:46 +00:00
Benoit Bolsee
89c2d80d32 Linux compilation problem, better fix 2009-05-10 21:30:30 +00:00
Campbell Barton
a6721e549d changes to get benoits logic updates building on scons with gcc 2009-05-10 21:22:25 +00:00
Benoit Bolsee
386122ada6 BGE performance, 4th round: logic
This commit extends the technique of dynamic linked list to the logic
system to eliminate as much as possible temporaries, map lookup or 
full scan. The logic engine is now free of memory allocation, which is
an important stability factor. 

The overhead of the logic system is reduced by a factor between 3 and 6
depending on the logic setup. This is the speed-up you can expect on 
a logic setup using simple bricks. Heavy bricks like python controllers
and ray sensors will still take about the same time to execute so the
speed up will be less important.

The core of the logic engine has been much reworked but the functionality
is still the same except for one thing: the priority system on the 
execution of controllers. The exact same remark applies to actuators but
I'll explain for controllers only:

Previously, it was possible, with the "executePriority" attribute to set
a controller to run before any other controllers in the game. Other than
that, the sequential execution of controllers, as defined in Blender was
guaranteed by default.

With the new system, the sequential execution of controllers is still 
guaranteed but only within the controllers of one object. the user can
no longer set a controller to run before any other controllers in the
game. The "executePriority" attribute controls the execution of controllers
within one object. The priority is a small number starting from 0 for the
first controller and incrementing for each controller.

If this missing feature is a must, a special method can be implemented
to set a controller to run before all other controllers.

Other improvements:
- Systematic use of reference in parameter passing to avoid unnecessary data copy
- Use pre increment in iterator instead of post increment to avoid temporary allocation
- Use const char* instead of STR_String whenever possible to avoid temporary allocation
- Fix reference counting bugs (memory leak)
- Fix a crash in certain cases of state switching and object deletion
- Minor speed up in property sensor
- Removal of objects during the game is a lot faster
2009-05-10 20:53:58 +00:00
Campbell Barton
136d4c34ba deprecate controller.getActuator(name) and controller.getSensor(name) for
controller.actuators[name] and controller.sensors[name]

Made a read-only sequence type for logic brick sensors and actuators which can access single items or be used like a list or dictionary.
We could use a python dictionary or CValueList but that would be slower to create.

So you can do...
 for s in controller.sensors: print s

 print controller.sensors["Sensor"]

 print controller.sensors[0]

 sensors = list(controller.sensors)

This sequence type keeps a reference to the proxy it came from and will raise an error on access if the proxy has been removed.
2009-05-10 01:48:14 +00:00
Campbell Barton
c1e1091f02 moved py controller functions from SCA_PythonController to SCA_IController - the base controller class so python can get the sensors & actuators from any controller (not just SCA_PythonController types)
also deprecated getActuators() and getSensors() for 'sensors' and 'actuators' attributes.

an example of getting every sensor connected to an object.
 all_sensors = [s for c in ob.controllers for s in c.sensors]
2009-05-06 09:12:08 +00:00
Campbell Barton
9e8261a13b Disable importing module python controller scripts at conversion time because it can run BGE functions and crash since the internal state isnt setup yet.
face-select mode wasnt working when used with projection paint.
2009-05-05 05:57:08 +00:00
Benoit Bolsee
be2c21bcdb BGE logic: new sensor "tap" option to generate automatically on/off pulses
When enabled, this option converts any positive trigger from the sensor
into a pair of positive+negative trigger, with the negative trigger sent
in the next frame. The negative trigger from the sensor are not passed
to the controller as the option automatically generates the negative triggers. 
From the controller point of view, the sensor is positive only for 1 frame, 
even if the underlying sensor state remains positive.

The option interacts with the other sensor option in this way:
- Level option: tap option is mutually exclusive with level option. Both
  cannot be enabled at the same time.
- Invert option: tap option operates on the negative trigger of the 
  sensor, which are converted to positive trigger by the invert option.
  Hence, the controller will see the sensor positive for 1 frame when 
  the underlying sensor state turns negative. 
- Positive pulse option: tap option adds a negative trigger after each
  repeated positive pulse, unless the frequency option is 0, in which case
  positive pulse are generated on every frame as before, as long as the
  underlying sensor state is positive.
- Negative pulse option: this option is not compatible with tap option
  and is ignored when tap option is enabled.

Notes:
- Keyboard "All keys" is handled specially when tap option is set:
  There will be one pair of positive/negative trigger for each new 
  key press, regardless on how many keys are already pressed and there
  is no trigger when keys are released, regardless if keys are still
  pressed. 
  In case two keys are pressed in succesive frames, there will
  be 2 positive triggers and 1 negative trigger in the following frame.
2009-05-04 22:21:02 +00:00
Benoit Bolsee
2aa3c932d0 BGE performance: use inline function as much as possible in scenegraph and logic to avoid function call. 2009-05-03 21:51:57 +00:00
Benoit Bolsee
20888482ae BGE: fix a crash with previous scene destruction speed up commit when sensors and controllers are cross connected between objects. 2009-05-03 21:42:39 +00:00
Campbell Barton
22b501c791 fixes for bugs submitted by BGE users, fixes by myself and Mitchell Stokes
- when the attribute check function failed it didnt set an error raising a SystemError instead
- Rasterizer.getMaterialMode would never return KX_BLENDER_MULTITEX_MATERIAL
- PropertySensor value attribute checking function was always returning a fail.
- Vertex Self Shadow python script didnt update for meshes with modifiers.
2009-05-03 09:21:58 +00:00
Campbell Barton
40fc1d2af7 BGE PyController module type.
- Added support for any number of attributes, this means packages are supported automatically.
 so as well as "myModule.myFunc" you can do "myPackage.myModule.myFunc", nested packages work too.
- pass the controller to the python function as an argument for functions that take 1 arg, this check is only done at startup so it wont slow things down.

added support for
2009-05-02 15:09:06 +00:00
Campbell Barton
ef20036b84 BGE Py API patch from Mitchell Stokes, adds extra attributes and docs 2009-05-02 02:40:11 +00:00
Benoit Bolsee
07abb9dee2 BGE performance:
- Vast performance increase when removing scene containing large number of 
  objects: the sensor/controller map was updated for each deleted object, 
  causing massive slow down when the number of objects was large (O(n^2)).
- Use reference when scanning the sensor map => avoid useless copy.
- Remove dynamically the object bounding box from the DBVT when the object
  is invisible => faster culling.
2009-05-01 20:34:23 +00:00
Campbell Barton
1782be5c72 Not sure why this INCREF is needed since making a replica wont use the m_bytecode so why should it add a user?
Removed in 19974 but that crashes YoFrankie so adding back in for now.
2009-04-30 00:51:11 +00:00
Campbell Barton
81dfdf8374 ifdef's for future py3 support, after this adding py3 can mostly be done with defines or batch renaming funcs (with the exception of CListValue slicing)
.
No changes for py2.x.
2009-04-29 16:54:45 +00:00
Campbell Barton
d6c525d624 attempt to fix for py2.3 2009-04-29 16:48:00 +00:00
Campbell Barton
f8656d3510 BGE alternative run mode for python controllers.
Option to run a function in a module rather then a script from a python controller, this has a number of advantages.

- No allocating and freeing the namespace dictionary for every time its triggered
  (hard to measure the overhead here, but in a test with calling 42240 scripts a second each defining 200 vars, using modules was ~25% faster)

- Ability to use external python scripts for game logic.

- Convenient debug option that lets you edit scripts while the game engine runs.
2009-04-29 12:43:09 +00:00
Campbell Barton
e8f5c75005 patch from Mitchell Stokes, comments only - KX_PYATTRIBUTE_TODO for missing attributes 2009-04-23 00:47:45 +00:00
Benoit Bolsee
f6d27e73ee BGE: some more cleanup, remove useless ReplicaSetName(), move code to ProcessReplica. 2009-04-22 16:58:04 +00:00
Campbell Barton
5553d2c014 BGE C++ API
PyObjectPlus::ProcessReplica() is now called when any of its subclasses are replicated.

This is important because PyObjectPlus::ProcessReplica() NULL's the 'm_proxy' python pointer I added recently.
Without this a replicated subclass of PyObjectPlus could have an invalid pointer (crashing the BGE).

This change also means CValue::AddDataToReplica() can be moved into CValue::ProcessReplica() since ProcessReplica is always called.
2009-04-22 14:42:00 +00:00
Campbell Barton
0145a93f24 Patch from Mitchell Stokes for KX_Light to use attrdef's
Added type access to the SCA_PropertyActuator, added missing function in library docs.
2009-04-21 23:15:18 +00:00
Campbell Barton
720f7a8b69 use PyList_SET_ITEM instead of PyList_SetItem,
PyList_SetItem does error checking and decref's existing values, PyList_SET_ITEM is a macro for direct assignment, only for use on new lists.
2009-04-20 23:27:53 +00:00
Campbell Barton
217bbb7800 BGE Python API
Separate getting a normal attribute and getting __dict__, was having to do too a check for __dict__ on each class (multiple times per getattro call from python) when its not used that often.
2009-04-20 23:17:52 +00:00
Campbell Barton
dee32d0b3f BGE Python API
- initialize pythons sys.argv in the blenderplayer
- ignore all arguments after a single " - " in the blenderplayer (like in blender), so args can be passed to the game.
- add a utility function PyOrientationTo() - to take a Py euler, quat or 3x3 matrix and convert into a C++ MT_Matrix3x3.
- add utility function ConvertPythonToMesh to get a RAS_MeshObject from a KX_MeshProxy or a name.
- Added error prefix arguments to ConvertPythonToGameObject, ConvertPythonToMesh and PyOrientationTo so the error messages can include what function they came from.
- deprecated brick.getOwner() for the "owner" attribute.
2009-04-20 09:13:59 +00:00
Campbell Barton
f5fc4ebdd8 BGE Python API
- More verbose error messages.
- BL_Shader wasnt setting error messages on some errors
- FilterNormal depth attribute was checking for float which is bad because scripts often expect ints assigned to float attributes.
- Added a check to PyVecTo for a tuple rather then always using a generic python sequence. On my system this is over 2x faster with an optmized build.
2009-04-19 21:01:12 +00:00
Campbell Barton
6bc162e679 BGE Python API
removed redundant (PyObject *self) argument from python functions that are not exposed to python directly.
2009-04-19 17:29:07 +00:00
Campbell Barton
7dbc9dc719 BGE Python API cleanup - no functionality changes
- comments to PyObjectPlus.h
- remove unused/commented junk.
- renamed PyDestructor to py_base_dealloc for consistency
- all the PyTypeObject's were still using the sizeof() their class, can use sizeof(PyObjectPlus_Proxy) now which is smaller too.
2009-04-19 14:57:52 +00:00
Campbell Barton
8d2cb5bea4 BGE Python API
This changes how the BGE classes and Python work together, which hasnt changed since blender went opensource.
The main difference is PyObjectPlus - the base class for most game engine classes, no longer inherit from PyObject, and cannot be cast to a PyObject.

This has the advantage that the BGE does not have to keep 2 reference counts valid for C++ and Python.

Previously C++ classes would never be freed while python held a reference, however this reference could be problematic eg: a GameObject that isnt in a scene anymore should not be used by python, doing so could even crash blender in some cases.

Instead PyObjectPlus has a member "PyObject *m_proxy" which is lazily initialized when python needs it. m_proxy reference counts are managed by python, though it should never be freed while the C++ class exists since it holds a reference to avoid making and freeing it all the time.
When the C++ class is free'd it sets the m_proxy reference to NULL, If python accesses this variable it will raise a RuntimeError, (check the isValid attribute to see if its valid without raising an error).
- This replaces the m_zombie bool and IsZombie() tests added recently.

In python return values that used to be..
 return value->AddRef();
Are now
 return value->GetProxy();
or...
 return value->NewProxy(true); // true means python owns this C++ value which will be deleted when the PyObject is freed
2009-04-19 12:46:39 +00:00
Campbell Barton
df8cf26404 Added m_zombie to the base python class (PyObjectPlus), when this is set all the subclasses will raise an error on access to their members.
Other small changes...
- KX_Camera and KX_Light didnt have get/setitem access in their PyType definition.
- CList.from_id() error checking for a long was checking for -1 against an unsigned value (own fault)
- CValue::SpecialRelease was incrementing an int for no reason.
- renamed m_attrlist to m_attr_dict since its a PyDict type.
- removed custom getattro/setattro functions for KX_Scene and KX_GameObject, use py_base_getattro, py_base_setattro for all subclasses of PyObjectPlus.
- lowercase windows.h in VideoBase.cpp for cross compiling.
2009-04-17 20:06:06 +00:00
Ton Roosendaal
1dd980768d Missing <stdlib.h> for abs() made this file not compile for me. 2009-04-17 10:24:44 +00:00
Campbell Barton
efb7dd86ff Fix for own recent reference count error.
- The armature weakref list was  being incref'd twice then decrefed twice (incref and decref were used incorrectly), now only once. My 'fix' broke this.
- In bpy_pydriver_create_dict the 2 refs added from running PyDict_SetItemString twice were undone when clearing the dictionary (added comment)
- changed Py_XDECREF to Py_DECREF int BPY_pyconstraint_update and BPY_pyconstraint_target, Py_XDECREF checs for NULL value which would have crashed blender before it got to Py_XDECREF anyway.
- after every error is reported (PyErr_Print), remove sys.last_traceback and clear the error, I found this fixed certain crashes (usually when starting the game engine or exiting blender), so best do this all the time.

- header_text.c, CcdPhysicsEnvironment.cpp, KX_CameraActuator.cpp - remove some warnings.
2009-04-15 04:34:27 +00:00
Campbell Barton
17f35293ee PropertyActuator toggle option didnt run when the Value field was empty. 2009-04-12 16:10:43 +00:00
Campbell Barton
55d2b184ec added "toggle" an option for the property actuator.
much less hassle then setting up a property sensor and 2 assignment actuators, or through python.
2009-04-12 07:24:04 +00:00
Campbell Barton
33170295c8 use long long rather then int for storing game logic properties.
There were also some problems with int to python conversion
- assigning a PyLong to a KX_GameObject from python would raise an error
- PyLong were coerced into floats when used with internal CValue arithmetic

Changes...
- PyLong is converted into CIntValue for coercing and assigning from python
- CValue's generic GetNumber() function returns a double rather then a float.
- Print an error when a PyType cant be coerced into a CValue

Tested with python, expressions and property sensor.
2009-04-12 06:41:01 +00:00
Andre Susano Pinto
2fff90bbb4 Added function name to many of the PyArg_ParseTuple calls in gameengine
This way python raises more useful messages.
2009-04-10 16:45:19 +00:00
Campbell Barton
ac45472a17 BGE Bugfix
The End key didn't work work at all for the keyboard sensor.
Removed getEventList() since it was added since 2.48a release.
2009-04-09 22:15:26 +00:00
Campbell Barton
4669fa48a8 Added GameKeys.EventToCharacter(event, is_shift) so you can get the character that would be types when pressing a key.
Last commit was made in the pydocs folder only, so this includes changes mentioned in rev 19620.
2009-04-09 16:00:45 +00:00
Campbell Barton
65f65a729a key logging didnt work for alphanum keys -=_+ and |\ since revision 2 and nobody noticed! 2009-04-09 10:28:14 +00:00
Campbell Barton
eacf5b5d6d BGE Text
- multi-line strings for bitmap text 
- keyboard sensor now logs return and pad enter as "\n"

BGE std::vector use in Value.cpp and RAS_MaterialBucket.cpp
The size of a new list is known before making them, reduce re-allocs, though probably not a noticeable speedup.
2009-04-09 09:50:17 +00:00
Benoit Bolsee
5b0d75e831 BGE API cleanup: 2DFilterActuator. 2009-04-08 21:40:55 +00:00
Campbell Barton
d012a222a2 Some users have odd joysticks with more then 8 axises, increased to 16 (so 4 joysticks)
Ideally there would be no limit but I dont think its worth the effort.
Also had a bug in last commit for the pytyhon api's "axisSingle" attribute, UI index starts at 1 not zero.
2009-04-07 19:21:48 +00:00