ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

lua_v1_actor.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/foreach.h"
00024 
00025 #include "engines/grim/grim.h"
00026 #include "engines/grim/lua_v1.h"
00027 #include "engines/grim/actor.h"
00028 #include "engines/grim/costume.h"
00029 #include "engines/grim/set.h"
00030 #include "engines/grim/model.h"
00031 #include "engines/grim/gfx_base.h"
00032 
00033 #include "engines/grim/lua/lauxlib.h"
00034 
00035 namespace Grim {
00036 
00037 void Lua_V1::LoadActor() {
00038     lua_Object nameObj = lua_getparam(1);
00039     const char *name;
00040 
00041     if (lua_isnil(nameObj) || !lua_isstring(nameObj))
00042         name = "<unnamed>";
00043     else
00044         name = lua_getstring(nameObj);
00045     Actor *a = new Actor();
00046     a->setName(name);
00047     lua_pushusertag(a->getId(), MKTAG('A','C','T','R'));
00048 }
00049 
00050 void Lua_V1::GetActorTimeScale() {
00051     lua_Object actorObj = lua_getparam(1);
00052     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00053         return;
00054 
00055     Actor *actor = getactor(actorObj);
00056     lua_pushnumber(actor->getTimeScale());
00057 }
00058 
00059 void Lua_V1::SetSelectedActor() {
00060     lua_Object actorObj = lua_getparam(1);
00061     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00062         return;
00063     // TODO there is missing some check here: else lua_pushnil()
00064     g_grim->setSelectedActor(getactor(actorObj));
00065 }
00066 
00067 /* Get the currently selected actor, this is used in
00068  * "Camera-Relative" mode to handle the appropriate
00069  * actor for movement
00070  */
00071 void Lua_V1::GetCameraActor() {
00072     // TODO verify what is going on with selected actor
00073     Actor *actor = g_grim->getSelectedActor();
00074     lua_pushusertag(actor->getId(), MKTAG('A','C','T','R'));
00075 }
00076 
00077 void Lua_V1::SetActorTalkColor() {
00078     lua_Object actorObj = lua_getparam(1);
00079     lua_Object colorObj = lua_getparam(2);
00080 
00081     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00082         return;
00083     if (!lua_isuserdata(colorObj) && lua_tag(colorObj) != MKTAG('C','O','L','R'))
00084         return;
00085     Actor *actor = getactor(actorObj);
00086     actor->setTalkColor(getcolor(colorObj));
00087 }
00088 
00089 void Lua_V1::GetActorTalkColor() {
00090     lua_Object actorObj = lua_getparam(1);
00091     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R')) {
00092         lua_pushnil();
00093         return;
00094     }
00095     Actor *actor = getactor(actorObj);
00096     int32 cTag = actor->getTalkColor().toEncodedValue();
00097     if (g_grim->getGameType() == GType_MONKEY4)
00098         cTag |= (0xFF << 24);
00099     lua_pushusertag(cTag, MKTAG('C','O','L','R'));
00100 }
00101 
00102 void Lua_V1::SetActorRestChore() {
00103     lua_Object actorObj = lua_getparam(1);
00104     lua_Object choreObj = lua_getparam(2);
00105     lua_Object costumeObj = lua_getparam(3);
00106     Costume *costume;
00107     int chore;
00108 
00109     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R') ||
00110             (!lua_isnumber(choreObj) && !lua_isnil(choreObj))) {
00111         return;
00112     }
00113 
00114     Actor *actor = getactor(actorObj);
00115 
00116     if (lua_isnil(choreObj)) {
00117         chore = -1;
00118     } else {
00119         chore = (int)lua_getnumber(choreObj);
00120     }
00121     if (!findCostume(costumeObj, actor, &costume))
00122         return;
00123 
00124     actor->setRestChore(chore, costume);
00125 }
00126 
00127 void Lua_V1::SetActorWalkChore() {
00128     lua_Object actorObj = lua_getparam(1);
00129     lua_Object choreObj = lua_getparam(2);
00130     lua_Object costumeObj = lua_getparam(3);
00131     Costume *costume;
00132     int chore;
00133 
00134     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R') ||
00135             (!lua_isnumber(choreObj) && !lua_isnil(choreObj))) {
00136         return;
00137     }
00138 
00139     Actor *actor = getactor(actorObj);
00140 
00141     if (lua_isnil(choreObj)) {
00142         chore = -1;
00143     } else {
00144         chore = (int)lua_getnumber(choreObj);
00145     }
00146     if (!findCostume(costumeObj, actor, &costume))
00147         return;
00148 
00149     actor->setWalkChore(chore, costume);
00150 }
00151 
00152 void Lua_V1::SetActorTurnChores() {
00153     lua_Object actorObj = lua_getparam(1);
00154     lua_Object leftChoreObj = lua_getparam(2);
00155     lua_Object rightChoreObj = lua_getparam(3);
00156     lua_Object costumeObj = lua_getparam(4);
00157     Costume *costume;
00158 
00159     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R') ||
00160             (!lua_isnumber(leftChoreObj) && !lua_isnumber(rightChoreObj))) {
00161         return;
00162     }
00163 
00164     Actor *actor = getactor(actorObj);
00165     int leftChore = (int)lua_getnumber(leftChoreObj);
00166     int rightChore = (int)lua_getnumber(rightChoreObj);
00167 
00168     if (!findCostume(costumeObj, actor, &costume))
00169         return;
00170 
00171     actor->setTurnChores(leftChore, rightChore, costume);
00172 }
00173 
00174 void Lua_V1::SetActorTalkChore() {
00175     lua_Object actorObj = lua_getparam(1);
00176     lua_Object indexObj = lua_getparam(2);
00177     lua_Object choreObj = lua_getparam(3);
00178     lua_Object costumeObj = lua_getparam(4);
00179     Costume *costume;
00180     int chore;
00181 
00182     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R') ||
00183             !lua_isnumber(indexObj) || (!lua_isnumber(choreObj) && !lua_isnil(choreObj))) {
00184         return;
00185     }
00186 
00187     int index = (int)lua_getnumber(indexObj);
00188     if (index < 1 || index > 16)
00189         return;
00190 
00191     Actor *actor = getactor(actorObj);
00192 
00193     if (lua_isnil(choreObj)) {
00194         chore = -1;
00195     } else {
00196         chore = (int)lua_getnumber(choreObj);
00197     }
00198     if (!findCostume(costumeObj, actor, &costume))
00199         return;
00200 
00201     actor->setTalkChore(index, chore, costume);
00202 }
00203 
00204 void Lua_V1::SetActorMumblechore() {
00205     lua_Object actorObj = lua_getparam(1);
00206     lua_Object choreObj = lua_getparam(2);
00207     lua_Object costumeObj = lua_getparam(3);
00208     Costume *costume;
00209     int chore;
00210 
00211     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R') ||
00212             (!lua_isnumber(choreObj) && !lua_isnil(choreObj))) {
00213         return;
00214     }
00215 
00216     Actor *actor = getactor(actorObj);
00217 
00218     if (lua_isnil(choreObj)) {
00219         chore = -1;
00220     } else {
00221         chore = (int)lua_getnumber(choreObj);
00222     }
00223     if (!findCostume(costumeObj, actor, &costume))
00224         return;
00225 
00226     actor->setMumbleChore(chore, costume);
00227 }
00228 
00229 void Lua_V1::SetActorVisibility() {
00230     lua_Object actorObj = lua_getparam(1);
00231     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00232         return;
00233 
00234     Actor *actor = getactor(actorObj);
00235 
00236     bool val = getbool(2);
00237     actor->setVisibility(val);
00238 }
00239 
00240 void Lua_V1::SetActorScale() {
00241     lua_Object actorObj = lua_getparam(1);
00242     lua_Object scaleObj = lua_getparam(2);
00243 
00244     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00245         return;
00246 
00247     Actor *actor = getactor(actorObj);
00248     float scale = 1.f;
00249 
00250     if (lua_isnumber(scaleObj))
00251         scale = lua_getnumber(scaleObj);
00252 
00253     actor->setScale(scale);
00254 }
00255 
00256 void Lua_V1::SetActorTimeScale() {
00257     lua_Object actorObj = lua_getparam(1);
00258     lua_Object scaleObj = lua_getparam(2);
00259 
00260     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00261         return;
00262 
00263     Actor *actor = getactor(actorObj);
00264     float scale = 1.f;
00265 
00266     if (lua_isnumber(scaleObj))
00267         scale = lua_getnumber(scaleObj);
00268 
00269     actor->setTimeScale(scale);
00270 }
00271 
00272 void Lua_V1::SetActorCollisionMode() {
00273     lua_Object actorObj = lua_getparam(1);
00274     lua_Object modeObj  = lua_getparam(2);
00275 
00276     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R')) {
00277         lua_pushnil();
00278         return;
00279     }
00280 
00281     Actor *actor = getactor(actorObj);
00282     Actor::CollisionMode mode = (Actor::CollisionMode)(int)(lua_getnumber(modeObj));
00283 
00284     actor->setCollisionMode(mode);
00285 }
00286 
00287 void Lua_V1::SetActorCollisionScale() {
00288     lua_Object actorObj = lua_getparam(1);
00289     lua_Object scaleObj  = lua_getparam(2);
00290 
00291     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R')) {
00292         lua_pushnil();
00293         return;
00294     }
00295 
00296     Actor *actor = getactor(actorObj);
00297     float scale = lua_getnumber(scaleObj);
00298 
00299     actor->setCollisionScale(scale);
00300 }
00301 
00302 void Lua_V1::PutActorAt() {
00303     lua_Object actorObj = lua_getparam(1);
00304     lua_Object xObj = lua_getparam(2);
00305     lua_Object yObj = lua_getparam(3);
00306     lua_Object zObj = lua_getparam(4);
00307     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00308         return;
00309 
00310     if (!lua_isnumber(xObj) || !lua_isnumber(yObj) || !lua_isnumber(zObj)) {
00311         warning("PutActorAt called without valid target position");
00312         return;
00313     }
00314 
00315     Actor *actor = getactor(actorObj);
00316     float x = lua_getnumber(xObj);
00317     float y = lua_getnumber(yObj);
00318     float z = lua_getnumber(zObj);
00319     actor->setPos(Math::Vector3d(x, y, z));
00320 }
00321 
00322 void Lua_V1::GetActorPos() {
00323     lua_Object actorObj = lua_getparam(1);
00324 
00325     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00326         return;
00327 
00328     Actor *actor = getactor(actorObj);
00329     Math::Vector3d pos = actor->getPos();
00330     lua_pushnumber(pos.x());
00331     lua_pushnumber(pos.y());
00332     lua_pushnumber(pos.z());
00333 }
00334 
00335 void Lua_V1::SetActorRot() {
00336     lua_Object actorObj = lua_getparam(1);
00337 
00338     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00339         return;
00340 
00341     Actor *actor = getactor(actorObj);
00342     lua_Object p = lua_getparam(2);
00343     lua_Object y = lua_getparam(3);
00344     lua_Object r = lua_getparam(4);
00345     if (!lua_isnumber(p) || !lua_isnumber(y) || !lua_isnumber(r))
00346         return;
00347     float pitch = lua_getnumber(p);
00348     float yaw = lua_getnumber(y);
00349     float roll = lua_getnumber(r);
00350     if (getbool(5)) {
00351         actor->turnTo(pitch, yaw, roll, true);
00352     } else {
00353         actor->setRot(pitch, yaw, roll);
00354     }
00355 }
00356 
00357 void Lua_V1::GetActorRot() {
00358     lua_Object actorObj = lua_getparam(1);
00359 
00360     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00361         return;
00362 
00363     Actor *actor = getactor(actorObj);
00364     lua_pushnumber(actor->getPitch().getDegrees());
00365     lua_pushnumber(actor->getYaw().getDegrees());
00366     lua_pushnumber(actor->getRoll().getDegrees());
00367 }
00368 
00369 void Lua_V1::IsActorTurning() {
00370     lua_Object actorObj = lua_getparam(1);
00371 
00372     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00373         return;
00374 
00375     Actor *actor = getactor(actorObj);
00376     pushbool(actor->isTurning());
00377 }
00378 
00379 void Lua_V1::GetAngleBetweenActors() {
00380     lua_Object actor1Obj = lua_getparam(1);
00381     lua_Object actor2Obj = lua_getparam(2);
00382 
00383     if (!lua_isuserdata(actor1Obj) || lua_tag(actor1Obj) != MKTAG('A','C','T','R')) {
00384         lua_pushnil();
00385         return;
00386     }
00387     if (!lua_isuserdata(actor2Obj) || lua_tag(actor2Obj) != MKTAG('A','C','T','R')) {
00388         lua_pushnil();
00389         return;
00390     }
00391     Actor *actor1 = getactor(actor1Obj);
00392     Actor *actor2 = getactor(actor2Obj);
00393 
00394     if (!actor1 || !actor2) {
00395         lua_pushnil();
00396         return;
00397     }
00398 
00399     lua_pushnumber(actor1->getYawTo(actor2).getDegrees());
00400 }
00401 
00402 void Lua_V1::GetActorYawToPoint() {
00403     lua_Object actorObj = lua_getparam(1);
00404     lua_Object pointObj = lua_getparam(2);
00405     lua_Object xObj, yObj, zObj;
00406 
00407     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R')) {
00408         lua_pushnil();
00409         return;
00410     }
00411 
00412     Actor *actor = getactor(actorObj);
00413     if (lua_istable(pointObj)) {
00414         lua_pushobject(pointObj);
00415         lua_pushstring("x");
00416         xObj = lua_gettable();
00417         lua_pushobject(pointObj);
00418         lua_pushstring("y");
00419         yObj = lua_gettable();
00420         lua_pushobject(pointObj);
00421         lua_pushstring("z");
00422         zObj = lua_gettable();
00423     } else {
00424         xObj = pointObj;
00425         yObj = lua_getparam(3);
00426         zObj = lua_getparam(4);
00427     }
00428     float x = lua_getnumber(xObj);
00429     float y = lua_getnumber(yObj);
00430     float z = lua_getnumber(zObj);
00431 
00432     Math::Vector3d yawVector(x, y, z);
00433 
00434     lua_pushnumber(actor->getYawTo(yawVector).getDegrees());
00435 }
00436 
00437 /* Changes the set that an actor is associated with,
00438  * by changing the set to "nil" an actor is disabled
00439  * but should still not be destroyed.
00440  */
00441 void Lua_V1::PutActorInSet() {
00442     lua_Object actorObj = lua_getparam(1);
00443     lua_Object setObj = lua_getparam(2);
00444 
00445     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00446         return;
00447 
00448     Actor *actor = getactor(actorObj);
00449 
00450     if (!lua_isstring(setObj) && !lua_isnil(setObj))
00451         return;
00452 
00453     const char *set = lua_getstring(setObj);
00454 
00455     // FIXME verify adding actor to set
00456     if (!set)
00457         set = "";
00458     if (!actor->isInSet(set))
00459         actor->putInSet(set);
00460 }
00461 
00462 void Lua_V1::SetActorWalkRate() {
00463     lua_Object actorObj = lua_getparam(1);
00464     lua_Object rateObj = lua_getparam(2);
00465 
00466     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00467         return;
00468     if (!lua_isnumber(rateObj))
00469         return;
00470 
00471     Actor *actor = getactor(actorObj);
00472     float rate = lua_getnumber(rateObj);
00473     actor->setWalkRate(rate);
00474 }
00475 
00476 void Lua_V1::GetActorWalkRate() {
00477     lua_Object actorObj = lua_getparam(1);
00478     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00479         return;
00480 
00481     Actor *actor = getactor(actorObj);
00482     lua_pushnumber(actor->getWalkRate());
00483 }
00484 
00485 void Lua_V1::SetActorTurnRate() {
00486     lua_Object actorObj = lua_getparam(1);
00487     lua_Object rateObj = lua_getparam(2);
00488 
00489     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00490         return;
00491     if (!lua_isnumber(rateObj))
00492         return;
00493 
00494     Actor *actor = getactor(actorObj);
00495     float rate = lua_getnumber(rateObj); // FIXME verify negate values of rate
00496     actor->setTurnRate(rate);
00497 }
00498 
00499 void Lua_V1::WalkActorForward() {
00500     lua_Object actorObj = lua_getparam(1);
00501     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00502         return;
00503     Actor *actor = getactor(actorObj);
00504     actor->walkForward();
00505 }
00506 
00507 void Lua_V1::SetActorReflection() {
00508     lua_Object actorObj = lua_getparam(1);
00509     lua_Object angleObj = lua_getparam(2);
00510 
00511     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00512         return;
00513 
00514     Actor *actor = getactor(actorObj);
00515     float angle = lua_getnumber(angleObj);
00516     actor->setReflection(angle);
00517 }
00518 
00519 void Lua_V1::GetActorPuckVector() {
00520     lua_Object actorObj = lua_getparam(1);
00521     lua_Object addObj = lua_getparam(2);
00522 
00523     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R')) {
00524         lua_pushnil();
00525         return;
00526     }
00527 
00528     Actor *actor = getactor(actorObj);
00529     if (!actor) {
00530         lua_pushnil();
00531         return;
00532     }
00533 
00534     Math::Vector3d result = actor->getPuckVector();
00535     if (!lua_isnil(addObj))
00536         result += actor->getPos();
00537 
00538     lua_pushnumber(result.x());
00539     lua_pushnumber(result.y());
00540     lua_pushnumber(result.z());
00541 }
00542 
00543 void Lua_V1::ActorPuckOrient() {
00544     lua_Object  actorObj = lua_getparam(1);
00545 
00546     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R')) {
00547         return;
00548     }
00549 
00550     Actor *actor = getactor(actorObj);
00551     actor->setPuckOrient(getbool(2));
00552 }
00553 
00554 void Lua_V1::WalkActorTo() {
00555     lua_Object actorObj = lua_getparam(1);
00556     lua_Object xObj = lua_getparam(2);
00557     lua_Object yObj = lua_getparam(3);
00558     lua_Object zObj = lua_getparam(4);
00559 
00560     lua_Object txObj = lua_getparam(5);
00561     lua_Object tyObj = lua_getparam(6);
00562     lua_Object tzObj = lua_getparam(7);
00563 
00564     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00565         return;
00566 
00567     Math::Vector3d destVec;
00568     Actor *actor = getactor(actorObj);
00569     if (!lua_isnumber(xObj)) {
00570         if (!lua_isuserdata(xObj) || lua_tag(xObj) != MKTAG('A','C','T','R'))
00571             return;
00572         Actor *destActor = getactor(xObj);
00573         destVec = destActor->getPos();
00574     } else {
00575         float x = lua_getnumber(xObj);
00576         float y = lua_getnumber(yObj);
00577         float z = lua_getnumber(zObj);
00578         destVec.set(x, y, z);
00579     }
00580 
00581     // TODO figure out purpose this
00582     float tx = lua_getnumber(txObj);
00583     float ty = lua_getnumber(tyObj);
00584     float tz = lua_getnumber(tzObj);
00585     Math::Vector3d tVec(tx, ty, tz);
00586 
00587     actor->walkTo(destVec);
00588 }
00589 
00590 /* This draw an actor to an offscreen buffer.
00591  * The actors' buffers blit to the global buffer which then gets blitted to screen.
00592  * This double layer system is necessary for set at, where Glottis and the Albinizod
00593  * are drawn to the clean buffer but at different times.
00594  *
00595  * If you change the clean system check with the sets dd, at and with the trail of flowers
00596  * in sh or zw.
00597  */
00598 void Lua_V1::ActorToClean() {
00599     lua_Object actorObj = lua_getparam(1);
00600 
00601     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R')) {
00602         lua_pushnil();
00603         return;
00604     }
00605 
00606     Actor *actor = getactor(actorObj);
00607     actor->drawToCleanBuffer();
00608 }
00609 
00610 void Lua_V1::IsActorMoving() {
00611     lua_Object actorObj = lua_getparam(1);
00612     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00613         return;
00614 
00615     Actor *actor = getactor(actorObj);
00616     pushbool(actor->isWalking());
00617 }
00618 
00619 void Lua_V1::IsActorResting() {
00620     lua_Object actorObj = lua_getparam(1);
00621     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00622         return;
00623 
00624     Actor *actor = getactor(actorObj);
00625     // FIXME verify if exist one flag for resting mode
00626     pushbool(!(actor->isWalking() || actor->isTurning()));
00627 }
00628 
00629 /* Get the location of one of the actor's nodes, this is
00630  * used by Glottis to watch where Manny is located in
00631  * order to hand him the work order.  This function is
00632  * also important for when Velasco hands Manny the logbook
00633  * in Rubacava. It is also used for calculating many positions
00634  * passed to ActorLookAt.
00635  */
00636 void Lua_V1::GetActorNodeLocation() {
00637     lua_Object actorObj = lua_getparam(1);
00638     lua_Object nodeObj = lua_getparam(2);
00639 
00640     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00641         return;
00642 
00643     if (!lua_isnumber(nodeObj))
00644         return;
00645 
00646     Actor *actor = getactor(actorObj);
00647     if (!actor->getCurrentCostume() || !actor->getCurrentCostume()->getModelNodes())
00648         return;
00649 
00650     int nodeId = (int)lua_getnumber(nodeObj);
00651 
00652     ModelNode *allNodes = actor->getCurrentCostume()->getModelNodes();
00653     ModelNode *node = allNodes + nodeId;
00654 
00655     node->_needsUpdate = true;
00656     ModelNode *root = node;
00657     while (root->_parent) {
00658         root = root->_parent;
00659         root->_needsUpdate = true;
00660     }
00661 
00662     Math::Matrix4 matrix;
00663     matrix.setPosition(actor->getPos());
00664     matrix.buildFromEuler(actor->getYaw(), actor->getPitch(), actor->getRoll(), Math::EO_ZXY);
00665     root->setMatrix(matrix);
00666     root->update();
00667 
00668     Math::Vector3d pos(node->_pivotMatrix.getPosition());
00669     lua_pushnumber(pos.x());
00670     lua_pushnumber(pos.y());
00671     lua_pushnumber(pos.z());
00672 }
00673 
00674 void Lua_V1::SetActorWalkDominate() {
00675     lua_Object actorObj = lua_getparam(1);
00676     lua_Object modeObj = lua_getparam(2);
00677 
00678     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00679         return;
00680 
00681     bool mode = lua_isnil(modeObj) != 0;
00682     Actor *actor = getactor(actorObj);
00683     actor->setRunning(mode);
00684 }
00685 
00686 void Lua_V1::SetActorColormap() {
00687     lua_Object actorObj = lua_getparam(1);
00688     lua_Object nameObj = lua_getparam(2);
00689 
00690     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00691         return;
00692 
00693     Actor *actor = getactor(actorObj);
00694     if (lua_isstring(nameObj)) {
00695         const char *name = lua_getstring(nameObj);
00696         actor->setColormap(name);
00697     } else if (lua_isnil(nameObj)) {
00698         error("SetActorColormap: implement remove cmap");
00699         // remove ?
00700     }
00701 }
00702 
00703 void Lua_V1::TurnActor() {
00704     lua_Object actorObj = lua_getparam(1);
00705     lua_Object dirObj = lua_getparam(2);
00706 
00707     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00708         return;
00709 
00710     if (!lua_isnumber(dirObj))
00711         return;
00712 
00713     Actor *actor = getactor(actorObj);
00714     int dir = (int)lua_getnumber(dirObj);
00715     // TODO verify. need clear mode walking
00716     actor->turn(dir);
00717 }
00718 
00719 void Lua_V1::PushActorCostume() {
00720     lua_Object actorObj = lua_getparam(1);
00721     lua_Object nameObj = lua_getparam(2);
00722 
00723     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00724         return;
00725 
00726     if (!lua_isstring(nameObj))
00727         return;
00728 
00729     Actor *actor = getactor(actorObj);
00730     const char *costumeName = lua_getstring(nameObj);
00731     actor->pushCostume(costumeName);
00732 }
00733 
00734 void Lua_V1::SetActorCostume() {
00735     lua_Object actorObj = lua_getparam(1);
00736     lua_Object costumeObj = lua_getparam(2);
00737 
00738     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00739         return;
00740 
00741     Actor *actor = getactor(actorObj);
00742     if (lua_isnil(costumeObj)) {
00743         actor->clearCostumes();
00744         pushbool(true);
00745         return;
00746     }
00747     if (!lua_isstring(costumeObj)) {
00748         pushbool(false);
00749         return;
00750     }
00751 
00752     const char *costumeName = lua_getstring(costumeObj);
00753     actor->setCostume(costumeName);
00754     pushbool(true);
00755 }
00756 
00757 void Lua_V1::GetActorCostume() {
00758     lua_Object actorObj = lua_getparam(1);
00759     lua_Object costumeObj = lua_getparam(2);
00760 
00761     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R')) {
00762         lua_pushnil();
00763         return;
00764     }
00765 
00766     Actor *actor = getactor(actorObj);
00767     Costume *costume = actor->getCurrentCostume();
00768     if (lua_isnil(costumeObj)) {
00769         // dummy
00770     } else if (lua_isnumber(costumeObj)) {
00771 /*      int num = (int)lua_getnumber(costumeObj);*/
00772         error("GetActorCostume: implement number Id");
00773     } else
00774         return;
00775 
00776     if (costume)
00777         lua_pushstring(costume->getFilename().c_str());
00778     else
00779         lua_pushnil();
00780 }
00781 
00782 void Lua_V1::PopActorCostume() {
00783     lua_Object actorObj = lua_getparam(1);
00784     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00785         return;
00786 
00787     Actor *actor = getactor(actorObj);
00788     if (actor->getCurrentCostume()) {
00789         lua_pushstring(actor->getCurrentCostume()->getFilename().c_str());
00790         actor->popCostume();
00791     } else
00792         lua_pushnil();
00793 }
00794 
00795 void Lua_V1::GetActorCostumeDepth() {
00796     lua_Object actorObj = lua_getparam(1);
00797     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R')) {
00798         lua_pushnil();
00799         return;
00800     }
00801 
00802     Actor *actor = getactor(actorObj);
00803     lua_pushnumber(actor->getCostumeStackDepth());
00804 }
00805 
00806 void Lua_V1::PrintActorCostumes() {
00807     // dummy
00808 }
00809 
00810 void Lua_V1::LoadCostume() {
00811     lua_Object nameObj = lua_getparam(1);
00812     if (lua_isstring(nameObj)) {
00813         // FIXME disable loading costume due creating issue with colormap, this opcode is unknown purpose
00814         //const char *name = lua_getstring(nameObj);
00815         //g_resourceloader->loadCostume(name, NULL);
00816     } else
00817         lua_pushnil();
00818 }
00819 
00820 void Lua_V1::PlayActorChore() {
00821     lua_Object actorObj = lua_getparam(1);
00822     lua_Object choreObj = lua_getparam(2);
00823     lua_Object costumeObj = lua_getparam(3);
00824 
00825     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00826         return;
00827 
00828     Actor *actor = getactor(actorObj);
00829 
00830     Costume *costume;
00831     if (!findCostume(costumeObj, actor, &costume))
00832         return;
00833 
00834     if (!lua_isnumber(choreObj)) {
00835         lua_pushnil();
00836         return;
00837     }
00838     int chore = (int)lua_getnumber(choreObj);
00839 
00840     if (!costume) {
00841         costume = actor->getCurrentCostume();
00842     }
00843     if (!costume) {
00844         lua_pushnil();
00845         return;
00846     }
00847 
00848     costume->playChore(chore);
00849     pushbool(true);
00850 }
00851 
00852 void Lua_V1::CompleteActorChore() {
00853     lua_Object actorObj = lua_getparam(1);
00854     lua_Object choreObj = lua_getparam(2);
00855     lua_Object costumeObj = lua_getparam(3);
00856 
00857     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00858         return;
00859 
00860     Actor *actor = getactor(actorObj);
00861 
00862     Costume *costume;
00863     if (!findCostume(costumeObj, actor, &costume))
00864         return;
00865 
00866     if (!lua_isnumber(choreObj)) {
00867         lua_pushnil();
00868         return;
00869     }
00870     int chore = (int)lua_getnumber(choreObj);
00871 
00872     if (!costume) {
00873         costume = actor->getCurrentCostume();
00874     }
00875     if (!costume) {
00876         lua_pushnil();
00877         return;
00878     }
00879 
00880     costume->setChoreLastFrame(chore);
00881     pushbool(true);
00882 }
00883 
00884 void Lua_V1::PlayActorChoreLooping() {
00885     lua_Object actorObj = lua_getparam(1);
00886     lua_Object choreObj = lua_getparam(2);
00887     lua_Object costumeObj = lua_getparam(3);
00888 
00889     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00890         return;
00891 
00892     Actor *actor = getactor(actorObj);
00893 
00894     Costume *costume;
00895     if (!findCostume(costumeObj, actor, &costume))
00896         return;
00897 
00898     if (!lua_isnumber(choreObj)) {
00899         lua_pushnil();
00900         return;
00901     }
00902     int chore = (int)lua_getnumber(choreObj);
00903 
00904     if (!costume) {
00905         costume = actor->getCurrentCostume();
00906     }
00907     if (!costume) {
00908         lua_pushnil();
00909         return;
00910     }
00911 
00912     costume->playChoreLooping(chore);
00913     pushbool(true);
00914 }
00915 
00916 void Lua_V1::SetActorChoreLooping() {
00917     lua_Object actorObj = lua_getparam(1);
00918     lua_Object choreObj = lua_getparam(2);
00919     lua_Object costumeObj = lua_getparam(4);
00920 
00921     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00922         return;
00923 
00924     Actor *actor = getactor(actorObj);
00925 
00926     Costume *costume;
00927     if (!findCostume(costumeObj, actor, &costume))
00928         return;
00929 
00930     if (!costume) {
00931         costume = actor->getCurrentCostume();
00932     }
00933     if (!costume)
00934         return;
00935 
00936     if (lua_isnumber(choreObj)) {
00937         int chore = (int)lua_getnumber(choreObj);
00938         costume->setChoreLooping(chore, getbool(3));
00939     } else if (lua_isnil(choreObj)) {
00940         error("SetActorChoreLooping: implement nil case");
00941     }
00942 }
00943 
00944 void Lua_V1::StopActorChore() {
00945     lua_Object actorObj = lua_getparam(1);
00946     lua_Object choreObj = lua_getparam(2);
00947     lua_Object costumeObj = lua_getparam(3);
00948 
00949     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00950         return;
00951 
00952     Actor *actor = getactor(actorObj);
00953 
00954     Costume *costume;
00955     if (!findCostume(costumeObj, actor, &costume))
00956         return;
00957 
00958     if (!costume) {
00959         costume = actor->getCurrentCostume();
00960     }
00961     if (!costume)
00962         return;
00963 
00964     if (lua_isnumber(choreObj)) {
00965         int chore = (int)lua_getnumber(choreObj);
00966         costume->stopChore(chore);
00967     } else if (lua_isnil(choreObj)) {
00968         costume->stopChores();
00969     }
00970 }
00971 
00972 void Lua_V1::FadeOutChore() {
00973     lua_Object actorObj = lua_getparam(1);
00974     lua_Object costumeObj = lua_getparam(2);
00975     lua_Object choreObj = lua_getparam(3);
00976     lua_Object timeObj = lua_getparam(4);
00977 
00978     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00979         return;
00980 
00981     Actor *actor = getactor(actorObj);
00982 
00983     Costume *costume;
00984     if (!findCostume(costumeObj, actor, &costume))
00985         return;
00986 
00987     if (!costume) {
00988         costume = actor->getCurrentCostume();
00989     }
00990     if (!costume)
00991         return;
00992 
00993     if (lua_isnumber(choreObj)) {
00994         int chore = (int)lua_getnumber(choreObj);
00995         int time = (int)lua_getnumber(timeObj);
00996 
00997         costume->fadeChoreOut(chore, time);
00998     }
00999 }
01000 
01001 void Lua_V1::FadeInChore() {
01002     lua_Object actorObj = lua_getparam(1);
01003     lua_Object costumeObj = lua_getparam(2);
01004     lua_Object choreObj = lua_getparam(3);
01005     lua_Object timeObj = lua_getparam(4);
01006 
01007     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
01008         return;
01009 
01010     Actor *actor = getactor(actorObj);
01011 
01012     Costume *costume;
01013     if (!findCostume(costumeObj, actor, &costume))
01014         return;
01015 
01016     if (!costume) {
01017         costume = actor->getCurrentCostume();
01018     }
01019     if (!costume)
01020         return;
01021 
01022     if (lua_isnumber(choreObj)) {
01023         int chore = (int)lua_getnumber(choreObj);
01024         int time = (int)lua_getnumber(timeObj);
01025 
01026         costume->fadeChoreIn(chore, time);
01027     }
01028 }
01029 
01030 void Lua_V1::IsActorChoring() {
01031     lua_Object actorObj = lua_getparam(1);
01032     lua_Object choreObj = lua_getparam(2);
01033     lua_Object costumeObj = lua_getparam(4);
01034 
01035     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
01036         return;
01037 
01038     Actor *actor = getactor(actorObj);
01039 
01040     Costume *costume;
01041     if (!findCostume(costumeObj, actor, &costume))
01042         return;
01043 
01044     if (!costume) {
01045         costume = actor->getCurrentCostume();
01046     }
01047     if (!costume) {
01048         lua_pushnil();
01049         return;
01050     }
01051 
01052     bool excludeLoop = getbool(3);
01053     if (lua_isnumber(choreObj)) {
01054         int chore = (int)lua_getnumber(choreObj);
01055         if (costume->isChoring(chore, excludeLoop) != -1) {
01056             lua_pushobject(choreObj);
01057             pushbool(true);
01058         } else
01059             lua_pushnil();
01060         return;
01061     } else if (lua_isnil(choreObj)) {
01062         for (int i = 0; i < costume->getNumChores(); i++) {
01063             int chore = costume->isChoring(i, excludeLoop);
01064             if (chore != -1) {
01065                 // Ignore talk chores.
01066                 bool isTalk = false;
01067                 for (int j = 0; j < 10; j++) {
01068                     if (costume == actor->getTalkCostume(j) && actor->getTalkChore(j) == chore) {
01069                         isTalk = true;
01070                         break;
01071                     }
01072                 }
01073                 if (isTalk)
01074                     continue;
01075 
01076                 lua_pushnumber(chore);
01077                 pushbool(true);
01078                 return;
01079             }
01080         }
01081     }
01082 
01083     lua_pushnil();
01084 }
01085 
01086 void Lua_V1::ActorLookAt() {
01087     lua_Object actorObj = lua_getparam(1);
01088     lua_Object xObj = lua_getparam(2);
01089     lua_Object yObj = lua_getparam(3);
01090     lua_Object zObj = lua_getparam(4);
01091     lua_Object rateObj = lua_getparam(5);
01092 
01093     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
01094         return;
01095     Actor *actor = getactor(actorObj);
01096     if (!actor->getCurrentCostume())
01097         return;
01098 
01099     if (lua_isnumber(rateObj))
01100         actor->setLookAtRate(lua_getnumber(rateObj));
01101 
01102     // Look at nothing
01103     if (lua_isnil(xObj)) {
01104         if (actor->isLookAtVectorZero())
01105             return;
01106 
01107         actor->setLookAtVectorZero();
01108         actor->setLooking(false);
01109         // FIXME: When grabbing Chepito lua_getnumber(yObj) returns -3.50214
01110         // which doesn't make any sense. I suspect that is a bug in Lua, since
01111         // i couldn't find any call to manny:head_look_at(nil, -3.50214) while
01112         // there are some calls to glottis:setpos(-0.120987, -3.50214, 0).
01113         // The same number, strange indeed eh?
01114         if (lua_isnumber(yObj) && lua_getnumber(yObj) > 0)
01115             actor->setLookAtRate(lua_getnumber(yObj));
01116         return;
01117     } else if (lua_isnumber(xObj)) { // look at xyz
01118         float fY;
01119         float fZ;
01120 
01121         float fX = lua_getnumber(xObj);
01122 
01123         if (lua_isnumber(yObj))
01124             fY = lua_getnumber(yObj);
01125         else
01126             fY = 0.0f;
01127 
01128         if (lua_isnumber(zObj))
01129             fZ = lua_getnumber(zObj);
01130         else
01131             fZ = 0.0f;
01132 
01133         Math::Vector3d vector;
01134         vector.set(fX, fY, fZ);
01135         actor->setLookAtVector(vector);
01136 
01137         if (lua_isnumber(rateObj))
01138             actor->setLookAtRate(lua_getnumber(rateObj));
01139     } else if (lua_isuserdata(xObj) && lua_tag(xObj) == MKTAG('A','C','T','R')) { // look at another actor
01140         Actor *lookedAct = getactor(xObj);
01141         actor->setLookAtVector(lookedAct->getHeadPos());
01142 
01143         if (lua_isnumber(yObj))
01144             actor->setLookAtRate(lua_getnumber(yObj));
01145     } else {
01146         return;
01147     }
01148 
01149     actor->setLooking(true);
01150 }
01151 
01152 /* Turn the actor to a point specified in the 3D space,
01153  * this should not have the actor look toward the point
01154  * but should rotate the entire actor toward it.
01155  * It does not however make the actor slowly turn the next
01156  * times update() is called on it. It does make it turn
01157  * slightly, by the amount given by its turn rate, and its
01158  * effect is immediate.
01159  */
01160 void Lua_V1::TurnActorTo() {
01161     lua_Object actorObj = lua_getparam(1);
01162     lua_Object xObj = lua_getparam(2);
01163     lua_Object yObj = lua_getparam(3);
01164     lua_Object zObj = lua_getparam(4);
01165     float x, y, z;
01166 
01167     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R')) {
01168         lua_pushnil();
01169         return;
01170     }
01171     Actor *actor = getactor(actorObj);
01172 
01173     if (lua_isuserdata(xObj) && lua_tag(xObj) == MKTAG('A','C','T','R')) {
01174         Actor *destActor = getactor(xObj);
01175         const Math::Vector3d &pos = destActor->getWorldPos();
01176         x = pos.x();
01177         y = pos.y();
01178         z = pos.z();
01179     } else {
01180         x = lua_getnumber(xObj);
01181         y = lua_getnumber(yObj);
01182         z = lua_getnumber(zObj);
01183     }
01184 
01185     Math::Vector3d turnToVector(x, y, z);
01186     // Return true if the actor is still turning
01187     // This allows manny to have the right yaw when he exits the elevator in the garage
01188     pushbool(!actor->singleTurnTo(turnToVector));
01189 }
01190 
01191 void Lua_V1::PointActorAt() {
01192     lua_Object actorObj = lua_getparam(1);
01193     lua_Object xObj = lua_getparam(2);
01194     lua_Object yObj = lua_getparam(3);
01195     lua_Object zObj = lua_getparam(4);
01196     float x, y, z;
01197 
01198     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R')) {
01199         lua_pushnil();
01200         return;
01201     }
01202     Actor *actor = getactor(actorObj);
01203 
01204     if (lua_isuserdata(xObj) && lua_tag(xObj) == MKTAG('A','C','T','R')) {
01205         Actor *destActor = getactor(xObj);
01206         const Math::Vector3d &pos = destActor->getPos();
01207         x = pos.x();
01208         y = pos.y();
01209         z = pos.z();
01210     } else {
01211         x = lua_getnumber(xObj);
01212         y = lua_getnumber(yObj);
01213         z = lua_getnumber(zObj);
01214     }
01215 
01216     // Find the vector pointing from the actor to the desired location
01217     Math::Vector3d turnToVector(x, y, z);
01218     actor->setRot(turnToVector);
01219 }
01220 
01221 void Lua_V1::WalkActorVector() {
01222     lua_Object actorObj = lua_getparam(1);
01223     lua_Object actor2Obj = lua_getparam(2);
01224 //  lua_Object xObj = lua_getparam(3);
01225 //  lua_Object yObj = lua_getparam(4);
01226 //  lua_Object zObj = lua_getparam(5);
01227 //  lua_Object param6Obj = lua_getparam(6);
01228 
01229     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R') ||
01230             !lua_isuserdata(actor2Obj) || lua_tag(actor2Obj) != MKTAG('A','C','T','R'))
01231         return;
01232 
01233 //  Actor *actor = static_cast<Actor *>(lua_getuserdata(actorObj));
01234     Actor *actor2 = getactor(actor2Obj);
01235 
01236     // TODO whole below part need rewrote to much original
01237     float moveHoriz, moveVert;
01238 
01239     // Third option is the "left/right" movement
01240     moveHoriz = luaL_check_number(3);
01241     // Fourth Option is the "up/down" movement
01242     moveVert = luaL_check_number(4);
01243 
01244     // Get the direction the camera is pointing
01245     Math::Vector3d cameraVector = g_grim->getCurrSet()->getCurrSetup()->_interest - g_grim->getCurrSet()->getCurrSetup()->_pos;
01246     // find the angle the camera direction is around the unit circle
01247     Math::Angle cameraYaw = cameraVector.unitCircleAngle();
01248 
01249     // Handle the turning
01250     Math::Vector3d adjustVector(moveHoriz, moveVert, 0);
01251     // find the angle the adjust vector is around the unit circle
01252     Math::Angle adjustYaw = adjustVector.unitCircleAngle();
01253 
01254     Math::Angle yaw = cameraYaw + adjustYaw;
01255     // yaw is offset from forward by 180 degrees
01256     yaw -= 180.0f;
01257 
01258     // set the new direction or walk forward
01259     if (actor2->getYaw() != yaw)
01260         actor2->turnTo(0, yaw, 0);
01261     else
01262         actor2->walkForward();
01263 }
01264 
01265 /* Set the pitch of the actor to the requested value,
01266  * this will rotate an actor toward/away from the ground.
01267  * This is used when Glottis runs over the signpost in
01268  * the Petrified Forest
01269  */
01270 void Lua_V1::SetActorPitch() {
01271     lua_Object actorObj = lua_getparam(1);
01272     lua_Object pitchObj = lua_getparam(2);
01273 
01274     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
01275         return;
01276 
01277     Actor *actor = getactor(actorObj);
01278     float pitch = lua_getnumber(pitchObj);
01279     actor->setRot(pitch, actor->getYaw(), actor->getRoll());
01280 }
01281 
01282 void Lua_V1::SetActorLookRate() {
01283     lua_Object actorObj = lua_getparam(1);
01284     lua_Object rateObj = lua_getparam(2);
01285 
01286     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
01287         return;
01288 
01289     if (!lua_isnumber(rateObj))
01290         return;
01291 
01292     Actor *actor = getactor(actorObj);
01293     if (!actor->getCurrentCostume())
01294         return;
01295 
01296     float rate = lua_getnumber(rateObj);
01297     actor->setLookAtRate(rate);
01298 }
01299 
01300 void Lua_V1::GetActorLookRate() {
01301     lua_Object actorObj = lua_getparam(1);
01302 
01303     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
01304         return;
01305 
01306     Actor *actor = getactor(actorObj);
01307     if (!actor->getCurrentCostume())
01308         lua_pushnil();
01309     else
01310         lua_pushnumber(actor->getLookAtRate());
01311 }
01312 
01313 void Lua_V1::SetActorHead() {
01314     lua_Object actorObj = lua_getparam(1);
01315     lua_Object joint1Obj = lua_getparam(2);
01316     lua_Object joint2Obj = lua_getparam(3);
01317     lua_Object joint3Obj = lua_getparam(4);
01318     lua_Object maxRollObj = lua_getparam(5);
01319     lua_Object maxPitchObj = lua_getparam(6);
01320     lua_Object maxYawObj = lua_getparam(7);
01321 
01322     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
01323         return;
01324     if (!lua_isnumber(joint1Obj) || !lua_isnumber(joint2Obj) || !lua_isnumber(joint3Obj) ||
01325             !lua_isnumber(maxRollObj) || !lua_isnumber(maxPitchObj) || !lua_isnumber(maxYawObj))
01326         return;
01327 
01328     Actor *actor = getactor(actorObj);
01329     int joint1 = (int)lua_getnumber(joint1Obj);
01330     int joint2 = (int)lua_getnumber(joint2Obj);
01331     int joint3 = (int)lua_getnumber(joint3Obj);
01332     float maxRoll = lua_getnumber(maxRollObj);
01333     float maxPitch = lua_getnumber(maxPitchObj);
01334     float maxYaw = lua_getnumber(maxYawObj);
01335 
01336     actor->setHead(joint1, joint2, joint3, maxRoll, maxPitch, maxYaw);
01337 }
01338 
01339 void Lua_V1::PutActorAtInterest() {
01340     lua_Object actorObj = lua_getparam(1);
01341 
01342     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
01343         return;
01344 
01345     Actor *actor = getactor(actorObj);
01346     Set *scene = g_grim->getCurrSet();
01347     if (!scene)
01348         return;
01349 
01350     Math::Vector3d p = scene->getCurrSetup()->_interest;
01351     Math::Vector3d resultPt = p;
01352     float minDist = -1.f;
01353 
01354     for (int i = 0; i < scene->getSectorCount(); ++i) {
01355         Sector *sector = scene->getSectorBase(i);
01356         if (sector->getType() != Sector::WalkType || !sector->isVisible())
01357             continue;
01358 
01359         Math::Vector3d closestPt = sector->getClosestPoint(p);
01360         if (scene->findPointSector(closestPt, Sector::HotType))
01361             continue;
01362         float thisDist = (closestPt - p).getMagnitude();
01363         if (minDist < 0 || thisDist < minDist) {
01364             resultPt = closestPt;
01365             minDist = thisDist;
01366         }
01367     }
01368 
01369     actor->setPos(resultPt);
01370 }
01371 
01372 void Lua_V1::SetActorFollowBoxes() {
01373     lua_Object actorObj = lua_getparam(1);
01374 
01375     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
01376         return;
01377 
01378     Actor *actor = getactor(actorObj);
01379     actor->setFollowBoxes(getbool(2));
01380 }
01381 
01382 void Lua_V1::SetActorConstrain() {
01383     lua_Object actorObj = lua_getparam(1);
01384 //  lua_Object constrainObj = lua_getparam(2);
01385 
01386     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
01387         return;
01388 
01389 //  Actor *actor = static_cast<Actor *>(lua_getuserdata(actorObj));
01390 //  bool constrain = !lua_isnil(constrainObj);
01391 
01392     // FIXME that below should be enabled, but for now it's disabled realated to
01393     // above func SetActorFollowBoxes.
01394 //  actor->setConstrain(constrain);
01395 }
01396 
01397 void Lua_V1::GetVisibleThings() {
01398     lua_Object actorObj = lua_getparam(1);
01399     Actor *actor = nullptr;
01400     if (lua_isnil(actorObj)) {
01401         actor = g_grim->getSelectedActor();
01402         if (!actor)
01403             return;
01404     } else if (lua_isuserdata(actorObj) && lua_tag(actorObj) == MKTAG('A','C','T','R')) {
01405         actor = getactor(actorObj);
01406     }
01407     assert(actor);
01408 
01409     lua_Object result = lua_createtable();
01410 
01411     // TODO verify code below
01412     if (actor->isInSet(g_grim->getCurrSet()->getName())) {
01413         foreach (Actor *a, g_grim->getActiveActors()) {
01414             // Consider the active actor visible
01415             if (actor == a || actor->getYawTo(a) < 90) {
01416                 lua_pushobject(result);
01417                 lua_pushusertag(a->getId(), MKTAG('A','C','T','R'));
01418                 lua_pushnumber(1);
01419                 lua_settable();
01420             }
01421         }
01422     }
01423     lua_pushobject(result);
01424 }
01425 
01426 void Lua_V1::SetShadowColor() {
01427     int r = (int)lua_getnumber(lua_getparam(1));
01428     int g = (int)lua_getnumber(lua_getparam(2));
01429     int b = (int)lua_getnumber(lua_getparam(3));
01430 
01431     g_driver->setShadowColor(r, g, b);
01432 }
01433 
01434 void Lua_V1::KillActorShadows() {
01435     lua_Object actorObj = lua_getparam(1);
01436 
01437     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R')) {
01438         lua_pushnil();
01439         return;
01440     }
01441     Actor *actor = getactor(actorObj);
01442     actor->clearShadowPlanes();
01443 }
01444 
01445 void Lua_V1::SetActiveShadow() {
01446     lua_Object actorObj = lua_getparam(1);
01447     lua_Object shadowIdObj = lua_getparam(2);
01448 
01449     if (!lua_isuserdata(actorObj) || actorObj == LUA_NOOBJECT) {
01450         lua_pushnil();
01451         return;
01452     }
01453     Actor *actor = getactor(actorObj);
01454     int shadowId = (int)lua_getnumber(shadowIdObj);
01455     actor->setActiveShadow(shadowId);
01456 }
01457 
01458 void Lua_V1::SetActorShadowPoint() {
01459     lua_Object actorObj = lua_getparam(1);
01460     lua_Object xObj = lua_getparam(2);
01461     lua_Object yObj = lua_getparam(3);
01462     lua_Object zObj = lua_getparam(4);
01463 
01464     if (!lua_isuserdata(actorObj) || actorObj == LUA_NOOBJECT) {
01465         lua_pushnil();
01466         return;
01467     }
01468     Actor *actor = getactor(actorObj);
01469     float x = lua_getnumber(xObj);
01470     float y = lua_getnumber(yObj);
01471     float z = lua_getnumber(zObj);
01472 
01473     actor->setShadowPoint(Math::Vector3d(x, y, z));
01474 }
01475 
01476 void Lua_V1::SetActorShadowPlane() {
01477     lua_Object actorObj = lua_getparam(1);
01478     lua_Object nameObj = lua_getparam(2);
01479 
01480     if (!lua_isuserdata(actorObj) || actorObj == LUA_NOOBJECT) {
01481         lua_pushnil();
01482         return;
01483     }
01484     Actor *actor = getactor(actorObj);
01485     const char *name = lua_getstring(nameObj);
01486 
01487     actor->setShadowPlane(name);
01488 }
01489 
01490 void Lua_V1::AddShadowPlane() {
01491     lua_Object actorObj = lua_getparam(1);
01492     lua_Object nameObj = lua_getparam(2);
01493 
01494     if (!lua_isuserdata(actorObj) || actorObj == LUA_NOOBJECT) {
01495         lua_pushnil();
01496         return;
01497     }
01498     Actor *actor = getactor(actorObj);
01499     const char *name = lua_getstring(nameObj);
01500 
01501     actor->addShadowPlane(name);
01502 }
01503 
01504 void Lua_V1::ActivateActorShadow() {
01505     lua_Object actorObj = lua_getparam(1);
01506     lua_Object shadowIdObj = lua_getparam(2);
01507     lua_Object stateObj = lua_getparam(3);
01508 
01509     if (!lua_isuserdata(actorObj) || actorObj == LUA_NOOBJECT) {
01510         lua_pushnil();
01511         return;
01512     }
01513     Actor *actor = getactor(actorObj);
01514     int shadowId = (int)lua_getnumber(shadowIdObj);
01515     bool state = !lua_isnil(stateObj);
01516 
01517     actor->setActivateShadow(shadowId, state);
01518     g_grim->flagRefreshShadowMask(true);
01519 }
01520 
01521 void Lua_V1::SetActorShadowValid() {
01522     lua_Object actorObj = lua_getparam(1);
01523     lua_Object numObj = lua_getparam(2);
01524 
01525     if (!lua_isuserdata(actorObj) || actorObj == LUA_NOOBJECT) {
01526         lua_pushnil();
01527         return;
01528     }
01529     Actor *actor = getactor(actorObj);
01530     int valid = (int)lua_getnumber(numObj);
01531 
01532     warning("SetActorShadowValid(%d) unknown purpose", valid);
01533 
01534     actor->setShadowValid(valid);
01535 }
01536 
01537 void Lua_V1::GetActorRect() {
01538     // This function is only used in one place and it is only checked if the return value is nil or not.
01539     // Looking at how it is used I don't see any reason to ever return nil.
01540     // If it does not return nil, the light that comes out of Chepito's lantern move properly.
01541     lua_pushnumber(1);
01542 }
01543 
01544 } // end of namespace Grim


Generated on Sat Oct 19 2019 05:01:04 for ResidualVM by doxygen 1.7.1
curved edge   curved edge