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

lua_v1_set.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 "math/matrix3.h"
00024 
00025 #include "engines/grim/debug.h"
00026 #include "engines/grim/lua_v1.h"
00027 #include "engines/grim/actor.h"
00028 #include "engines/grim/grim.h"
00029 #include "engines/grim/set.h"
00030 
00031 #include "engines/grim/lua/lauxlib.h"
00032 
00033 namespace Grim {
00034 
00035 // Sector functions
00036 /* Find the sector (of any type) which contains
00037  * the requested coordinate (x,y,z).
00038  */
00039 void Lua_V1::GetPointSector() {
00040     lua_Object xObj = lua_getparam(1);
00041     lua_Object yObj = lua_getparam(2);
00042     lua_Object zObj = lua_getparam(3);
00043     lua_Object typeObj = lua_getparam(4);
00044     Sector::SectorType sectorType;
00045 
00046     if (!lua_isnumber(xObj) || !lua_isnumber(yObj) || !lua_isnumber(zObj)) {
00047         lua_pushnil();
00048         return;
00049     }
00050     if (lua_isnil(typeObj))
00051         sectorType = Sector::WalkType;
00052     else
00053         sectorType = (Sector::SectorType)(int)lua_getnumber(typeObj);
00054 
00055     float x = lua_getnumber(xObj);
00056     float y = lua_getnumber(yObj);
00057     float z = lua_getnumber(zObj);
00058 
00059     Math::Vector3d point(x, y, z);
00060     Sector *result = g_grim->getCurrSet()->findPointSector(point, sectorType);
00061     if (result) {
00062         lua_pushnumber(result->getSectorId());
00063         lua_pushstring(result->getName().c_str());
00064         lua_pushnumber(result->getType());
00065     } else {
00066         lua_pushnil();
00067     }
00068 }
00069 
00070 void Lua_V1::GetActorSector() {
00071     lua_Object actorObj = lua_getparam(1);
00072     lua_Object typeObj = lua_getparam(2);
00073 
00074     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00075         return;
00076     if (!lua_isnumber(typeObj))
00077         return;
00078 
00079     Actor *actor = getactor(actorObj);
00080     Sector::SectorType sectorType = (Sector::SectorType)(int)lua_getnumber(typeObj);
00081     Math::Vector3d pos = actor->getWorldPos();
00082     Sector *result = g_grim->getCurrSet()->findPointSector(pos, sectorType);
00083     if (result) {
00084         lua_pushnumber(result->getSectorId());
00085         lua_pushstring(result->getName().c_str());
00086         lua_pushnumber(result->getType());
00087     } else {
00088         lua_pushnil();
00089     }
00090 }
00091 
00092 void Lua_V1::IsActorInSector() {
00093     lua_Object actorObj = lua_getparam(1);
00094     lua_Object nameObj = lua_getparam(2);
00095     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00096         return;
00097     if (!lua_isstring(nameObj)) {
00098         lua_pushnil();
00099         return;
00100     }
00101 
00102     Actor *actor = getactor(actorObj);
00103     const char *name = lua_getstring(nameObj);
00104 
00105     Sector *sector;
00106     if (g_grim->getGameType() == GType_GRIM) {
00107         sector = g_grim->getCurrSet()->getSectorBySubstring(name, actor->getPos());
00108     } else {
00109         sector = g_grim->getCurrSet()->getSectorByName(name);
00110         if (!(sector && sector->isPointInSector(actor->getPos()))) {
00111             sector = nullptr;
00112         }
00113     }
00114 
00115     if (sector) {
00116         lua_pushnumber(sector->getSectorId());
00117         lua_pushstring(sector->getName().c_str());
00118         lua_pushnumber(sector->getType());
00119     } else {
00120         lua_pushnil();
00121     }
00122 }
00123 
00124 void Lua_V1::IsPointInSector() {
00125     lua_Object xObj = lua_getparam(1);
00126     lua_Object yObj = lua_getparam(2);
00127     lua_Object zObj = lua_getparam(3);
00128     lua_Object nameObj = lua_getparam(4);
00129 
00130     if (!lua_isstring(nameObj)) {
00131         lua_pushnil();
00132         return;
00133     }
00134 
00135     const char *name = lua_getstring(nameObj);
00136     float x = lua_getnumber(xObj);
00137     float y = lua_getnumber(yObj);
00138     float z = lua_getnumber(zObj);
00139     Math::Vector3d pos(x, y, z);
00140 
00141     Sector *sector = g_grim->getCurrSet()->getSectorBySubstring(name, pos);
00142     if (sector) {
00143         lua_pushnumber(sector->getSectorId());
00144         lua_pushstring(sector->getName().c_str());
00145         lua_pushnumber(sector->getType());
00146     } else {
00147         lua_pushnil();
00148     }
00149 }
00150 
00151 void Lua_V1::GetSectorOppositeEdge() {
00152     lua_Object actorObj = lua_getparam(1);
00153     lua_Object nameObj = lua_getparam(2);
00154 
00155     if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
00156         return;
00157 
00158     if (!lua_isstring(nameObj)) {
00159         lua_pushnil();
00160         return;
00161     }
00162 
00163     Actor *actor = getactor(actorObj);
00164     const char *name = lua_getstring(nameObj);
00165 
00166     Sector *sector = g_grim->getCurrSet()->getSectorBySubstring(name);
00167     if (sector) {
00168         if (sector->getNumVertices() != 4)
00169             warning("GetSectorOppositeEdge(): cheat box with %d (!= 4) edges!", sector->getNumVertices());
00170         Math::Vector3d *vertices = sector->getVertices();
00171         Sector::ExitInfo e;
00172 
00173         sector->getExitInfo(actor->getPos(), -actor->getPuckVector(), &e);
00174         float frac = (e.exitPoint - vertices[e.edgeVertex + 1]).getMagnitude() / e.edgeDir.getMagnitude();
00175         e.edgeVertex -= 2;
00176         if (e.edgeVertex < 0)
00177             e.edgeVertex += sector->getNumVertices();
00178         Math::Vector3d edge = vertices[e.edgeVertex + 1] - vertices[e.edgeVertex];
00179         Math::Vector3d p = vertices[e.edgeVertex] + edge * frac;
00180         lua_pushnumber(p.x());
00181         lua_pushnumber(p.y());
00182         lua_pushnumber(p.z());
00183     } else {
00184         lua_pushnil();
00185     }
00186 }
00187 
00188 void Lua_V1::MakeSectorActive() {
00189     lua_Object sectorObj = lua_getparam(1);
00190 
00191     if (!lua_isnumber(sectorObj) && !lua_isstring(sectorObj))
00192         return;
00193 
00194     // FIXME: This happens on initial load. Are we initting something in the wrong order?
00195     if (!g_grim->getCurrSet()) {
00196         warning("!!!! Trying to call MakeSectorActive without a scene");
00197         return;
00198     }
00199 
00200     bool visible = !lua_isnil(lua_getparam(2));
00201 
00202     if (lua_isnumber(sectorObj)) {
00203         int numSectors = g_grim->getCurrSet()->getSectorCount();
00204         int id = (int)lua_getnumber(sectorObj);
00205         for (int i = 0; i < numSectors; i++) {
00206             Sector *sector = g_grim->getCurrSet()->getSectorBase(i);
00207             if (sector->getSectorId() == id) {
00208                 sector->setVisible(visible);
00209                 return;
00210             }
00211         }
00212     } else if (lua_isstring(sectorObj)) {
00213         const char *name = lua_getstring(sectorObj);
00214         // a search by name here is needed for set bv, since it calls MakeSectorActive with sectors
00215         // "bw_gone" and "bw_gone2", and a substring search would return "bw_gone2" for both.
00216         Sector *sector = g_grim->getCurrSet()->getSectorByName(name);
00217         if (sector) {
00218             sector->setVisible(visible);
00219         }
00220     }
00221 }
00222 
00223 // Set functions
00224 void Lua_V1::LockSet() {
00225     lua_Object nameObj = lua_getparam(1);
00226     if (!lua_isstring(nameObj))
00227         return;
00228 
00229     const char *name = lua_getstring(nameObj);
00230     // TODO implement proper locking
00231     g_grim->setSetLock(name, true);
00232 }
00233 
00234 void Lua_V1::UnLockSet() {
00235     lua_Object nameObj = lua_getparam(1);
00236     if (!lua_isstring(nameObj))
00237         return;
00238 
00239     const char *name = lua_getstring(nameObj);
00240     // TODO implement proper unlocking
00241     g_grim->setSetLock(name, false);
00242 }
00243 
00244 void Lua_V1::MakeCurrentSet() {
00245     lua_Object nameObj = lua_getparam(1);
00246     if (!lua_isstring(nameObj)) {
00247         // TODO setting current set null
00248         warning("Lua_V1::MakeCurrentSet: implement missing case");
00249         return;
00250     }
00251 
00252     const char *name = lua_getstring(nameObj);
00253     Debug::debug(Debug::Engine, "Entered new scene '%s'.", name);
00254     g_grim->setSet(name);
00255 }
00256 
00257 void Lua_V1::MakeCurrentSetup() {
00258     lua_Object setupObj = lua_getparam(1);
00259     if (!lua_isnumber(setupObj))
00260         return;
00261 
00262     int num = (int)lua_getnumber(setupObj);
00263     g_grim->makeCurrentSetup(num);
00264 }
00265 
00266 /* Find the requested scene and return the current setup
00267  * id number.  This function cannot just use the current
00268  * scene or else when Manny opens his inventory information
00269  * gets lost, such as the position for the demon beavors
00270  * in the Petrified Forest.
00271  */
00272 void Lua_V1::GetCurrentSetup() {
00273     lua_Object nameObj = lua_getparam(1);
00274     if (!lua_isstring(nameObj))
00275         return;
00276 
00277     const char *name = lua_getstring(nameObj);
00278 
00279     // FIXME there are some big difference here !
00280     Set *scene = g_grim->loadSet(name);
00281     if (!scene) {
00282         warning("GetCurrentSetup() Requested scene (%s) is not loaded", name);
00283         lua_pushnil();
00284         return;
00285     }
00286     lua_pushnumber(scene->getSetup());
00287 }
00288 
00289 void Lua_V1::PreviousSetup() {
00290     int num = g_grim->getCurrSet()->getSetup() - 1;
00291     if (num < 0) {
00292         num = g_grim->getCurrSet()->getNumSetups() - 1;
00293     }
00294     g_grim->makeCurrentSetup(num);
00295 }
00296 
00297 void Lua_V1::NextSetup() {
00298     int num = g_grim->getCurrSet()->getSetup() + 1;
00299     if (num >= g_grim->getCurrSet()->getNumSetups()) {
00300         num = 0;
00301     }
00302     g_grim->makeCurrentSetup(num);
00303 }
00304 
00305 /* This function makes the walkplane sectors smaller by the
00306  * given size. This is used when manny is holding some big
00307  * thing, like his scythe, that is likely to clip with the
00308  * things around him. The sectors are still connected, but they
00309  * have a margin which prevents manny to go too close to the
00310  * sectors edges.
00311  */
00312 void Lua_V1::ShrinkBoxes() {
00313     lua_Object sizeObj = lua_getparam(1);
00314 
00315     if (lua_isnumber(sizeObj)) {
00316         float size = lua_getnumber(sizeObj);
00317         g_grim->getCurrSet()->shrinkBoxes(size);
00318     }
00319 }
00320 
00321 void Lua_V1::UnShrinkBoxes() {
00322     g_grim->getCurrSet()->unshrinkBoxes();
00323 }
00324 
00325 /* Given a position and a size this function calculates and pushes
00326  * the nearest point to that which will be valid if the boxes are
00327  * shrunk by the amount specified.
00328  */
00329 void Lua_V1::GetShrinkPos() {
00330     lua_Object xObj = lua_getparam(1);
00331     lua_Object yObj = lua_getparam(2);
00332     lua_Object zObj = lua_getparam(3);
00333     lua_Object rObj = lua_getparam(4);
00334 
00335     if (!lua_isnumber(xObj) || !lua_isnumber(yObj) || !lua_isnumber(zObj) || !lua_isnumber(rObj))
00336         return;
00337 
00338     float x = lua_getnumber(xObj);
00339     float y = lua_getnumber(yObj);
00340     float z = lua_getnumber(zObj);
00341     float r = lua_getnumber(rObj);
00342     Math::Vector3d pos;
00343     pos.set(x, y, z);
00344 
00345     Sector *sector;
00346     g_grim->getCurrSet()->shrinkBoxes(r);
00347     g_grim->getCurrSet()->findClosestSector(pos, &sector, &pos);
00348     g_grim->getCurrSet()->unshrinkBoxes();
00349 
00350     if (sector) {
00351         lua_pushnumber(pos.x());
00352         lua_pushnumber(pos.y());
00353         lua_pushnumber(pos.z());
00354     } else {
00355         lua_pushnil();
00356     }
00357 }
00358 
00359 void Lua_V1::NewObjectState() {
00360     int setupID = (int)lua_getnumber(lua_getparam(1));
00361     int val = (int)lua_getnumber(lua_getparam(2));
00362     ObjectState::Position pos = (ObjectState::Position)val;
00363     const char *bitmap = lua_getstring(lua_getparam(3));
00364     const char *zbitmap = nullptr;
00365     if (!lua_isnil(lua_getparam(4)))
00366         zbitmap = lua_getstring(lua_getparam(4));
00367     bool transparency = getbool(5);
00368 
00369     ObjectState *state = g_grim->getCurrSet()->addObjectState(setupID, pos, bitmap, zbitmap, transparency);
00370     lua_pushusertag(state->getId(), MKTAG('S','T','A','T'));
00371 }
00372 
00373 void Lua_V1::FreeObjectState() {
00374     lua_Object param = lua_getparam(1);
00375     if (!lua_isuserdata(param) || lua_tag(param) != MKTAG('S','T','A','T'))
00376         return;
00377     ObjectState *state = getobjectstate(param);
00378     g_grim->getCurrSet()->deleteObjectState(state);
00379     delete state;
00380 }
00381 
00382 void Lua_V1::SendObjectToBack() {
00383     lua_Object param = lua_getparam(1);
00384     if (lua_isuserdata(param) && lua_tag(param) == MKTAG('S','T','A','T')) {
00385         ObjectState *state =  getobjectstate(param);
00386         g_grim->getCurrSet()->moveObjectStateToBack(state);
00387     }
00388 }
00389 
00390 void Lua_V1::SendObjectToFront() {
00391     lua_Object param = lua_getparam(1);
00392     if (lua_isuserdata(param) && lua_tag(param) == MKTAG('S','T','A','T')) {
00393         ObjectState *state =  getobjectstate(param);
00394         g_grim->getCurrSet()->moveObjectStateToFront(state);
00395     }
00396 }
00397 
00398 void Lua_V1::SetObjectType() {
00399     lua_Object param = lua_getparam(1);
00400     if (!lua_isuserdata(param) || lua_tag(param) != MKTAG('S','T','A','T'))
00401         return;
00402     ObjectState *state =  getobjectstate(param);
00403     int val = (int)lua_getnumber(lua_getparam(2));
00404     ObjectState::Position pos = (ObjectState::Position)val;
00405     state->setPos(pos);
00406 }
00407 
00408 void Lua_V1::SetAmbientLight() {
00409     int mode = (int)lua_getnumber(lua_getparam(1));
00410     Set *set = g_grim->getCurrSet();
00411 
00412     if (set == nullptr)
00413         return;
00414 
00415     if (mode == 0) {
00416         set->setLightEnableState(true);
00417     } else if (mode == 1) {
00418         set->setLightEnableState(false);
00419     }
00420 }
00421 
00422 void Lua_V1::SetLightIntensity() {
00423     lua_Object lightObj = lua_getparam(1);
00424     lua_Object intensityObj = lua_getparam(2);
00425 
00426     if (!lua_isnumber(intensityObj))
00427         return;
00428 
00429     float intensity = lua_getnumber(intensityObj);
00430 
00431     if (lua_isnumber(lightObj)) {
00432         int light = (int)lua_getnumber(lightObj);
00433         g_grim->getCurrSet()->setLightIntensity(light, intensity);
00434     } else if (lua_isstring(lightObj)) {
00435         const char *light = lua_getstring(lightObj);
00436         g_grim->getCurrSet()->setLightIntensity(light, intensity);
00437     }
00438 }
00439 
00440 void Lua_V1::SetLightPosition() {
00441     lua_Object lightObj = lua_getparam(1);
00442     lua_Object xObj = lua_getparam(2);
00443     lua_Object yObj = lua_getparam(3);
00444     lua_Object zObj = lua_getparam(4);
00445 
00446     if (!lua_isnumber(xObj) || !lua_isnumber(yObj) || !lua_isnumber(zObj))
00447         return;
00448 
00449     float x = lua_getnumber(xObj);
00450     float y = lua_getnumber(yObj);
00451     float z = lua_getnumber(zObj);
00452     Math::Vector3d vec(x, y, z);
00453 
00454     if (lua_isnumber(lightObj)) {
00455         int light = (int)lua_getnumber(lightObj);
00456         g_grim->getCurrSet()->setLightPosition(light, vec);
00457     } else if (lua_isstring(lightObj)) {
00458         const char *light = lua_getstring(lightObj);
00459         g_grim->getCurrSet()->setLightPosition(light, vec);
00460     }
00461 }
00462 
00463 void Lua_V1::TurnLightOn() {
00464     lua_Object lightObj = lua_getparam(1);
00465 
00466     Set *scene = g_grim->getCurrSet();
00467     bool isOn = getbool(2);
00468     if (lua_isnumber(lightObj)) {
00469         scene->setLightEnabled((int)lua_getnumber(lightObj), isOn);
00470     } else if (lua_isstring(lightObj)) {
00471         scene->setLightEnabled(lua_getstring(lightObj), isOn);
00472     }
00473 }
00474 
00475 } // end of namespace Grim


Generated on Sat Feb 16 2019 05:00:56 for ResidualVM by doxygen 1.7.1
curved edge   curved edge