From 7fb9204e976376ae2e926a212cdddb00c44a5a4d Mon Sep 17 00:00:00 2001 From: fgenesis Date: Sat, 20 Jul 2013 17:44:27 +0200 Subject: [PATCH] Minor changes & simplifications to Lua interface. 5 new functions. This changes all typical vector interpolation functions that get value, time, loopType, pingPong, ease as args from the Lua state; use unified functions for all value type occurances. This fixes the slightly annoying problem that it was not possible to pass a boolean true to pingPong or ease, because lua_tonumber() would always return 0 in that case. Also changed: - obj_getWorldPosition() now takes optional x,y-vector New: - obj_getWorldPositionAndRotation() is a more efficient shortcut, as often both position and rotation are retrieved together, and the underlying matrix chain operation is rather expensive. - entity_getEmitter() - entity_getNumEmitters() - setSceneColor2() - getSceneColor2() --- Aquaria/Entity.cpp | 4 +- Aquaria/Entity.h | 2 +- Aquaria/ScriptInterface.cpp | 163 ++++++++++++++++++++++++++---------- Aquaria/ScriptedEntity.cpp | 10 +++ Aquaria/ScriptedEntity.h | 2 + BBGE/RenderObject.cpp | 8 ++ BBGE/RenderObject.h | 1 + 7 files changed, 141 insertions(+), 49 deletions(-) diff --git a/Aquaria/Entity.cpp b/Aquaria/Entity.cpp index e394019..fda5d24 100644 --- a/Aquaria/Entity.cpp +++ b/Aquaria/Entity.cpp @@ -416,12 +416,12 @@ void Entity::doFriction(float dt) } } -void Entity::doFriction(float dt, int len) +void Entity::doFriction(float dt, float len) { Vector v = vel; if (!v.isZero()) { - v.setLength2D(dt * float(len)); + v.setLength2D(dt * len); vel -= v; } } diff --git a/Aquaria/Entity.h b/Aquaria/Entity.h index 70c7a51..71bcf70 100644 --- a/Aquaria/Entity.h +++ b/Aquaria/Entity.h @@ -356,7 +356,7 @@ public: void revive(float a); void setName(const std::string &name); void doFriction(float dt); - void doFriction(float dt, int len); + void doFriction(float dt, float len); bool isNormalLayer() const { diff --git a/Aquaria/ScriptInterface.cpp b/Aquaria/ScriptInterface.cpp index beb04c5..4c885ab 100644 --- a/Aquaria/ScriptInterface.cpp +++ b/Aquaria/ScriptInterface.cpp @@ -601,6 +601,50 @@ static bool looksLikeGlobal(const char *s) return true; } +inline float interpolateVec1(lua_State *L, InterpolatedVector& vec, int argOffs) +{ + return vec.interpolateTo( + lua_tonumber (L, argOffs ), // value + lua_tonumber (L, argOffs+1), // time + lua_tointeger(L, argOffs+2), // loopType + getBool (L, argOffs+3), // pingPong + getBool (L, argOffs+4)); // ease +} + +inline float interpolateVec1z(lua_State *L, InterpolatedVector& vec, int argOffs) +{ + return vec.interpolateTo( + Vector(0.0f, 0.0f, lua_tonumber (L, argOffs )), // value, last component + lua_tonumber (L, argOffs+1), // time + lua_tointeger(L, argOffs+2), // loopType + getBool (L, argOffs+3), // pingPong + getBool (L, argOffs+4)); // ease +} + +inline float interpolateVec2(lua_State *L, InterpolatedVector& vec, int argOffs) +{ + return vec.interpolateTo( + Vector(lua_tonumber (L, argOffs ), // x value + lua_tonumber (L, argOffs+1)), // y value + lua_tonumber (L, argOffs+2), // time + lua_tointeger(L, argOffs+3), // loopType + getBool(L, argOffs+4), // pingPong + getBool(L, argOffs+5)); // ease +} + +inline float interpolateVec3(lua_State *L, InterpolatedVector& vec, int argOffs) +{ + return vec.interpolateTo( + Vector(lua_tonumber (L, argOffs ), // x value + lua_tonumber (L, argOffs+1), // y value + lua_tonumber (L, argOffs+2)), // z value + lua_tonumber (L, argOffs+3), // time + lua_tointeger(L, argOffs+4), // loopType + getBool(L, argOffs+5), // pingPong + getBool(L, argOffs+6)); // ease +} + + //----------------------------------// #define luaFunc(func) static int l_##func(lua_State *L) @@ -781,7 +825,7 @@ luaFunc(loadfile_caseinsensitive) // ----- RenderObject common functions ----- -#define forwardCall(func) return l_##func(L); +#define forwardCall(func) l_##func(L) #define MakeTypeCheckFunc(fname, ty) luaFunc(fname) \ { ScriptObject *r = (ScriptObject*)lua_touserdata(L, 1); luaReturnBool(r ? r->isType(ty) : false); } @@ -812,8 +856,7 @@ luaFunc(obj_setPosition) if (r) { r->position.stop(); - r->position.interpolateTo(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3)), - lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7)); + interpolateVec2(L, r->position, 2); } luaReturnNil(); } @@ -824,8 +867,7 @@ luaFunc(obj_scale) if (r) { r->scale.stop(); - r->scale.interpolateTo(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3), 0), - lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7)); + interpolateVec2(L, r->scale, 2); } luaReturnNil(); } @@ -845,7 +887,7 @@ luaFunc(obj_alpha) if (r) { r->alpha.stop(); - r->alpha.interpolateTo(lua_tonumber(L, 2), lua_tonumber(L, 3), lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6)); + interpolateVec1(L, r->alpha, 2); } luaReturnNil(); } @@ -876,8 +918,7 @@ luaFunc(obj_color) if (r) { r->color.stop(); - r->color.interpolateTo(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3), lua_tonumber(L, 4)), - lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7), lua_tonumber(L, 8)); + interpolateVec3(L, r->color, 2); } luaReturnNil(); } @@ -898,8 +939,7 @@ luaFunc(obj_rotate) if (r) { r->rotation.stop(); - r->rotation.interpolateTo(Vector(0,0,lua_tonumber(L, 2)), - lua_tonumber(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5), lua_tointeger(L, 6)); + interpolateVec1z(L, r->rotation, 2); } luaReturnNil(); } @@ -910,8 +950,7 @@ luaFunc(obj_rotateOffset) if (r) { r->rotationOffset.stop(); - r->rotationOffset.interpolateTo(Vector(0,0,lua_tonumber(L, 2)), - lua_tonumber(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5), lua_tointeger(L, 6)); + interpolateVec1z(L, r->rotationOffset, 2); } luaReturnNil(); } @@ -934,8 +973,7 @@ luaFunc(obj_offset) if (r) { r->offset.stop(); - r->offset.interpolateTo(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3)), - lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7)); + interpolateVec2(L, r->offset, 2); } luaReturnNil(); } @@ -946,8 +984,7 @@ luaFunc(obj_internalOffset) if (r) { r->internalOffset.stop(); - r->internalOffset.interpolateTo(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3)), - lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7)); + interpolateVec2(L, r->internalOffset, 2); } luaReturnNil(); } @@ -1119,8 +1156,7 @@ luaFunc(obj_setInternalVel) if (r) { r->velocity.stop(); - r->velocity.interpolateTo(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3)), - lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7)); + interpolateVec2(L, r->velocity, 2); } luaReturnNil(); } @@ -1252,7 +1288,7 @@ luaFunc(obj_getWorldPosition) float x = 0, y = 0; if (b) { - Vector v = b->getWorldPosition(); + Vector v = b->getWorldCollidePosition(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3))); x = v.x; y = v.y; } @@ -1265,6 +1301,17 @@ luaFunc(obj_getWorldRotation) luaReturnNum(r ? r->getWorldRotation() : 0.0f); } +luaFunc(obj_getWorldPositionAndRotation) +{ + RenderObject *r = robj(L); + if (r) + { + Vector v = r->getWorldPositionAndRotation(); + luaReturnVec3(v.x, v.y, v.z); + } + luaReturnVec3(0.0f, 0.0f, 0.0f); +} + luaFunc(obj_moveToFront) { RenderObject *r = robj(L); @@ -1565,7 +1612,7 @@ luaFunc(quad_isRepeatTexture) luaFunc(prefix##_##suffix) \ { \ typecheckOnly(getter(L)); \ - forwardCall(base##_##suffix); \ + return forwardCall(base##_##suffix); \ } #define MK_ALIAS(prefix, suffix, alias) // not yet used here. defined to avoid warnings @@ -1621,6 +1668,7 @@ luaFunc(quad_isRepeatTexture) RO_FUNC(getter, prefix, update ) \ RO_FUNC(getter, prefix, getWorldPosition) \ RO_FUNC(getter, prefix, getWorldRotation) \ + RO_FUNC(getter, prefix, getWorldPositionAndRotation)\ RO_FUNC(getter, prefix, moveToFront ) \ RO_FUNC(getter, prefix, moveToBack ) \ RO_FUNC(getter, prefix, setLayer ) \ @@ -1780,7 +1828,7 @@ luaFunc(setActivePet) luaFunc(setWaterLevel) { - dsq->game->waterLevel.interpolateTo(lua_tonumber(L, 1), lua_tonumber(L, 2), lua_tointeger(L, 3), getBool(L, 4), getBool(L, 5)); + interpolateVec1(L, dsq->game->waterLevel, 1); luaReturnNum(dsq->game->waterLevel.x); } @@ -2733,7 +2781,7 @@ luaFunc(entity_getVectorToEntity) { typecheckOnly(entity(L)); typecheckOnly(entity(L, 2)); - forwardCall(obj_getVectorToObj); + return forwardCall(obj_getVectorToObj); } luaFunc(entity_setDropChance) @@ -3613,8 +3661,8 @@ luaFunc(cam_setPosition) float y = lua_tonumber(L, 2); float time = lua_tonumber(L, 3); int loopType = lua_tointeger(L, 4); - int pingPong = lua_tointeger(L, 5); - int ease = lua_tointeger(L, 6); + bool pingPong = getBool(L, 5); + bool ease = getBool(L, 6); Vector p = dsq->game->getCameraPositionFor(Vector(x,y)); @@ -3674,6 +3722,18 @@ luaFunc(entity_stopEmitter) luaReturnNil(); } +luaFunc(entity_getEmitter) +{ + ScriptedEntity *se = scriptedEntity(L); + luaReturnPtr(se ? se->getEmitter(lua_tointeger(L, 2)) : NULL); +} + +luaFunc(entity_getNumEmitters) +{ + ScriptedEntity *se = scriptedEntity(L); + luaReturnInt(se ? se->getNumEmitters() : 0); +} + luaFunc(entity_initStrands) { ScriptedEntity *e = scriptedEntity(L); @@ -3750,7 +3810,7 @@ luaFunc(entity_setAnimLayerTimeMult) AnimationLayer *l = e->skeletalSprite.getAnimationLayer(layer); if (l) { - l->timeMultiplier.interpolateTo(t, lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7)); + interpolateVec1(L, l->timeMultiplier, 3); } } luaReturnNum(t); @@ -3925,8 +3985,7 @@ luaFunc(beam_setPosition_override) Beam *b = beam(L); if (b) { - b->position.interpolateTo(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3)), - lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7)); + forwardCall(obj_setPosition); b->trace(); } luaReturnNil(); @@ -4021,7 +4080,7 @@ luaFunc(clearControlHint) luaFunc(setSceneColor) { - dsq->game->sceneColor3.interpolateTo(Vector(lua_tonumber(L, 1), lua_tonumber(L, 2), lua_tonumber(L, 3)), lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7)); + interpolateVec3(L, dsq->game->sceneColor3, 1); luaReturnNil(); } @@ -4031,6 +4090,18 @@ luaFunc(getSceneColor) luaReturnVec3(c.x, c.y, c.z); } +luaFunc(setSceneColor2) +{ + interpolateVec3(L, dsq->game->sceneColor2, 1); + luaReturnNil(); +} + +luaFunc(getSceneColor2) +{ + const Vector& c = dsq->game->sceneColor2; + luaReturnVec3(c.x, c.y, c.z); +} + luaFunc(setCameraLerpDelay) { dsq->game->cameraLerpDelay = lua_tonumber(L, 1); @@ -5366,7 +5437,7 @@ luaFunc(entity_setMaxSpeedLerp) { Entity *e = entity(L); if (e) - e->maxSpeedLerp.interpolateTo(lua_tonumber(L, 2), lua_tonumber(L, 3), lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6)); + interpolateVec1(L, e->maxSpeedLerp, 2); luaReturnNil(); } @@ -5515,7 +5586,7 @@ luaFunc(setGameSpeed) { dsq->gameSpeed.stop(); dsq->gameSpeed.stopPath(); - dsq->gameSpeed.interpolateTo(lua_tonumber(L, 1), lua_tonumber(L, 2), lua_tonumber(L, 3), lua_tonumber(L, 4), lua_tonumber(L, 5)); + interpolateVec1(L, dsq->gameSpeed, 1); luaReturnNil(); } @@ -7035,11 +7106,10 @@ luaFunc(entity_partAlpha) float start = lua_tonumber(L, 3); if (start != -1) r->alpha = start; - r->alpha.interpolateTo(lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tointeger(L, 6), lua_tointeger(L, 7), lua_tointeger(L, 8)); + interpolateVec1(L, r->alpha, 4); } } - - luaReturnNum(0); + luaReturnNil(); } luaFunc(entity_partBlendType) @@ -7047,7 +7117,7 @@ luaFunc(entity_partBlendType) ScriptedEntity *e = scriptedEntity(L); if (e) e->partMap[getString(L, 2)]->setBlendType(lua_tointeger(L, 3)); - luaReturnInt(0); + luaReturnNil(); } luaFunc(entity_partRotate) @@ -7057,12 +7127,9 @@ luaFunc(entity_partRotate) { RenderObject *r = e->partMap[getString(L, 2)]; if (r) - { - r->rotation.interpolateTo(Vector(0,0,lua_tointeger(L, 3)), lua_tonumber(L, 4), lua_tointeger(L, 5), lua_tointeger(L, 6), lua_tointeger(L, 7)); - } + interpolateVec1z(L, r->rotation, 3); } - - luaReturnNum(0); + luaReturnNil(); } luaFunc(entity_getStateTime) @@ -7199,11 +7266,11 @@ luaFunc(entity_initPart) { std::string partName(getString(L, 2)); std::string partTex(getString(L, 3)); - Vector partPosition(lua_tointeger(L, 4), lua_tointeger(L, 5)); - int renderAfter = lua_tointeger(L, 6); - bool partFlipH = lua_tointeger(L, 7); - bool partFlipV = lua_tointeger(L,8); - Vector offsetInterpolateTo(lua_tointeger(L, 9), lua_tointeger(L, 10)); + Vector partPosition(lua_tonumber(L, 4), lua_tonumber(L, 5)); + bool renderAfter = getBool(L, 6); + bool partFlipH = getBool(L, 7); + bool partFlipV = getBool(L,8); + Vector offsetInterpolateTo(lua_tonumber(L, 9), lua_tonumber(L, 10)); float offsetInterpolateTime = lua_tonumber(L, 11); @@ -7227,7 +7294,7 @@ luaFunc(entity_initPart) e->registerNewPart(q, partName); } - luaReturnNum(0); + luaReturnNil(); } luaFunc(entity_findTarget) @@ -7244,7 +7311,7 @@ luaFunc(entity_doFriction) Entity *e = entity(L); if (e) { - e->doFriction(lua_tonumber(L, 2), lua_tointeger(L, 3)); + e->doFriction(lua_tonumber(L, 2), lua_tonumber(L, 3)); } luaReturnNil(); } @@ -8411,6 +8478,8 @@ static const struct { luaRegister(entity_initEmitter), luaRegister(entity_startEmitter), luaRegister(entity_stopEmitter), + luaRegister(entity_getEmitter), + luaRegister(entity_getNumEmitters), luaRegister(entity_initPart), luaRegister(entity_initSegments), @@ -8859,6 +8928,8 @@ static const struct { luaRegister(setSceneColor), luaRegister(getSceneColor), + luaRegister(setSceneColor2), + luaRegister(getSceneColor2), luaRegister(entity_watchEntity), diff --git a/Aquaria/ScriptedEntity.cpp b/Aquaria/ScriptedEntity.cpp index 10f4c9e..f31a88f 100644 --- a/Aquaria/ScriptedEntity.cpp +++ b/Aquaria/ScriptedEntity.cpp @@ -175,6 +175,16 @@ void ScriptedEntity::stopEmitter(int emit) } } +ParticleEffect *ScriptedEntity::getEmitter(int emit) +{ + return (size_t(emit) < emitters.size()) ? emitters[emit] : NULL; +} + +int ScriptedEntity::getNumEmitters() const +{ + return emitters.size(); +} + void ScriptedEntity::registerNewPart(RenderObject *r, const std::string &name) { partMap[name] = r; diff --git a/Aquaria/ScriptedEntity.h b/Aquaria/ScriptedEntity.h index f943f6a..4118547 100644 --- a/Aquaria/ScriptedEntity.h +++ b/Aquaria/ScriptedEntity.h @@ -78,6 +78,8 @@ public: void initEmitter(int emit, const std::string &file); void startEmitter(int emit); void stopEmitter(int emit); + ParticleEffect *getEmitter(int emit); + int getNumEmitters() const; void shiftWorlds(WorldType lastWorld, WorldType worldType); void setAutoSkeletalUpdate(bool v); diff --git a/BBGE/RenderObject.cpp b/BBGE/RenderObject.cpp index 69b5e6d..6268eee 100644 --- a/BBGE/RenderObject.cpp +++ b/BBGE/RenderObject.cpp @@ -319,6 +319,14 @@ float RenderObject::getWorldRotation() return rot; } +Vector RenderObject::getWorldPositionAndRotation() +{ + Vector up = getWorldCollidePosition(Vector(0,1)); + Vector orig = getWorldPosition(); + MathFunctions::calculateAngleBetweenVectorsInDegrees(orig, up, orig.z); + return orig; +} + Vector RenderObject::getWorldCollidePosition(const Vector &vec) { #ifdef BBGE_BUILD_OPENGL diff --git a/BBGE/RenderObject.h b/BBGE/RenderObject.h index 3b4fec1..e755478 100644 --- a/BBGE/RenderObject.h +++ b/BBGE/RenderObject.h @@ -206,6 +206,7 @@ public: Vector getAbsoluteRotation(); float getWorldRotation(); + Vector getWorldPositionAndRotation(); // more efficient shortcut, returns rotation in vector z component Vector getNormal(); Vector getForward(); void setOverrideCullRadius(float ovr);