LUAS3DLib.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <lua/LUAS3DLib.h>
00022 #include <lua/LUAScript.h>
00023 #include <lua/LUAUtil.h>
00024 #include <common/OptionEntry.h>
00025 #include <common/OptionsScorched.h>
00026 #include <common/Logger.h>
00027 #include <tank/TankContainer.h>
00028 #include <tank/TankState.h>
00029 #include <target/TargetLife.h>
00030 #include <landscapemap/LandscapeMaps.h>
00031 
00032 #define LUA_LIB
00033 
00034 #include "lua.h"
00035 
00036 #include "lauxlib.h"
00037 #include "lualib.h"
00038 
00039 static LUAScript *getScript(lua_State *L)
00040 {
00041         lua_getglobal(L, "s3d_script");
00042         LUAScript *script = (LUAScript *) lua_touserdata(L, -1);
00043         lua_pop(L, 1);
00044         return script;
00045 }
00046 
00047 static void addTank(lua_State *L, Tank *tank)
00048 {
00049         lua_newtable(L);
00050 
00051         lua_pushstring(L, "name");
00052         lua_pushstring(L, tank->getCStrName().c_str());
00053         lua_settable(L, -3);
00054 
00055         lua_pushstring(L, "id");
00056         lua_pushnumber(L, tank->getPlayerId());
00057         lua_settable(L, -3);
00058 
00059         lua_pushstring(L, "position");
00060         LUAUtil::addVectorToStack(L, tank->getLife().getCenterPosition());
00061         lua_settable(L, -3);
00062 
00063         lua_pushstring(L, "alive");
00064         lua_pushboolean(L, tank->getAlive()?1:0);
00065         lua_settable(L, -3);
00066 
00067         lua_pushstring(L, "team");
00068         lua_pushnumber(L, tank->getTeam() * FIXED_RESOLUTION);
00069         lua_settable(L, -3);
00070 }
00071 
00072 static int s3d_get_option(lua_State *L) 
00073 {
00074         LUAScript *wrapper = getScript(L);
00075 
00076         const char *optionName = luaL_checkstring(L, 1);
00077         OptionEntry *entry = OptionEntryHelper::getEntry(
00078                 wrapper->getContext()->getOptionsGame().getMainOptions().getOptions(),
00079                 optionName);
00080         if (entry) lua_pushstring(L, entry->getValueAsString());
00081         else 
00082         {
00083                 Logger::log(S3D::formatStringBuffer("s3d_get_option:Failed to an option named %s", optionName));
00084                 lua_pushstring(L, "");
00085         }
00086 
00087         return 1;
00088 }
00089 
00090 static int s3d_get_tank(lua_State *L) 
00091 {
00092         LUAScript *wrapper = getScript(L);
00093 
00094         int number = luaL_checknumber(L, 1);
00095         Tank *tank =
00096                 wrapper->getContext()->getTankContainer().getTankById((unsigned int) number);
00097         if (tank) addTank(L, tank);
00098         else
00099         {
00100                 Logger::log(S3D::formatStringBuffer("s3d_get_option:Failed to an tank id %u", 
00101                         (unsigned int) number));
00102                 lua_pushstring(L, "");
00103         }
00104 
00105         return 1;
00106 }
00107 
00108 static int s3d_get_tanks(lua_State *L) 
00109 {
00110         LUAScript *wrapper = getScript(L);
00111 
00112         std::map<unsigned int, Tank *> &tanks =
00113                 wrapper->getContext()->getTankContainer().getAllTanks();
00114         lua_newtable(L);
00115 
00116         std::map<unsigned int, Tank *>::iterator itor;
00117         for (itor = tanks.begin(); 
00118                 itor != tanks.end();
00119                 itor++)
00120         {
00121                 Tank *tank = itor->second;
00122                 lua_pushnumber(L, tank->getPlayerId());
00123                 addTank(L, tank);
00124                 lua_settable(L, -3);
00125         }
00126 
00127         return 1;
00128 }
00129 
00130 static int s3d_get_height(lua_State *L) 
00131 {
00132         LUAScript *wrapper = getScript(L);
00133 
00134         fixed x = fixed(true, luaL_checknumber(L, 1));
00135         fixed y = fixed(true, luaL_checknumber(L, 2));
00136 
00137         fixed result = wrapper->getContext()->getLandscapeMaps().getGroundMaps().getHeight(
00138                 x.asInt(), y.asInt());
00139         lua_pushnumber(L, result.getInternal());
00140 
00141         return 1;
00142 }
00143 
00144 static int s3d_get_arenawidth(lua_State *L) 
00145 {
00146         LUAScript *wrapper = getScript(L);
00147 
00148         fixed result(wrapper->getContext()->getLandscapeMaps().getGroundMaps().getArenaWidth());
00149         lua_pushnumber(L, result.getInternal());
00150 
00151         return 1;
00152 }
00153 
00154 static int s3d_get_arenaheight(lua_State *L) 
00155 {
00156         LUAScript *wrapper = getScript(L);
00157 
00158         fixed result(wrapper->getContext()->getLandscapeMaps().getGroundMaps().getArenaHeight());
00159         lua_pushnumber(L, result.getInternal());
00160 
00161         return 1;
00162 }
00163 
00164 static int s3d_get_landscapewidth(lua_State *L) 
00165 {
00166         LUAScript *wrapper = getScript(L);
00167 
00168         fixed result(wrapper->getContext()->getLandscapeMaps().getGroundMaps().getLandscapeWidth());
00169         lua_pushnumber(L, result.getInternal());
00170 
00171         return 1;
00172 }
00173 
00174 static int s3d_get_landscapeheight(lua_State *L) 
00175 {
00176         LUAScript *wrapper = getScript(L);
00177 
00178         fixed result(wrapper->getContext()->getLandscapeMaps().getGroundMaps().getLandscapeHeight());
00179         lua_pushnumber(L, result.getInternal());
00180 
00181         return 1;
00182 }
00183 
00184 static const luaL_Reg s3dlib[] = {
00185         {"get_option", s3d_get_option},
00186         {"get_tank", s3d_get_tank},
00187         {"get_tanks", s3d_get_tanks},
00188         {"get_height", s3d_get_height},
00189         {"get_arenawidth", s3d_get_arenawidth},
00190         {"get_arenaheight", s3d_get_arenaheight},
00191         {"get_landscapewidth", s3d_get_landscapewidth},
00192         {"get_landscapeheight", s3d_get_landscapeheight},
00193         {NULL, NULL}
00194 };
00195 
00196 LUALIB_API int luaopen_s3d (lua_State *L) {
00197         luaL_register(L, LUA_S3DLIBNAME, s3dlib);
00198         return 1;
00199 }

Generated on Mon Feb 16 15:14:51 2009 for Scorched3D by  doxygen 1.5.3