LUAS3DWeaponLib.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/LUAS3DWeaponLib.h>
00022 #include <lua/LUAScript.h>
00023 #include <lua/LUAUtil.h>
00024 #include <weapons/AccessoryStore.h>
00025 #include <actions/Explosion.h>
00026 #include <actions/ExplosionParams.h>
00027 #include <actions/Napalm.h>
00028 #include <actions/Laser.h>
00029 #include <engine/ActionController.h>
00030 #include <common/Logger.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         DIALOG_ASSERT(!lua_isnil(L, -1));
00043         LUAScript *script = (LUAScript *) lua_touserdata(L, -1);
00044         lua_pop(L, 1);
00045         return script;
00046 }
00047 
00048 static int s3d_random(lua_State *L) 
00049 {
00050         LUAScript *wrapper = getScript(L);
00051         fixed result = wrapper->getContext()->
00052                 getActionController().getRandom().getRandFixed();
00053 
00054         lua_pushnumber(L, result.getInternal());
00055         return 1;
00056 }
00057 
00058 static int s3d_fire_weapon(lua_State *L) 
00059 {
00060         LUAScript *wrapper = getScript(L);
00061 
00062         const char *weaponName = luaL_checkstring(L, 1);
00063         int playerId = luaL_checknumber(L, 2);
00064         FixedVector position = LUAUtil::getVectorFromStack(L, 3);
00065         FixedVector velocity = LUAUtil::getVectorFromStack(L, 4);
00066 
00067         Accessory *accessory =
00068                 wrapper->getContext()->getAccessoryStore().findByPrimaryAccessoryName(weaponName);
00069         if (!accessory) 
00070         {
00071                 Logger::log(S3D::formatStringBuffer("Failed to find accessory named %s", weaponName));
00072                 return 0;
00073         }
00074 
00075         AccessoryPart *accessoryPart = accessory->getAction();
00076         if (!accessoryPart || accessoryPart->getType() != AccessoryPart::AccessoryWeapon)
00077         {
00078                 Logger::log(S3D::formatStringBuffer("Accessory named %s is not a weapons", weaponName));
00079                 return 0;
00080         }
00081 
00082         Weapon *weapon = (Weapon*) accessoryPart;
00083 
00084         WeaponFireContext weaponContext(playerId, 0);
00085         weapon->fireWeapon(*wrapper->getContext(), weaponContext, position, velocity);
00086 
00087         return 0;
00088 }
00089 
00090 static int s3d_explosion(lua_State *L) 
00091 {
00092         LUAScript *wrapper = getScript(L);
00093         ExplosionParams *explosionParams = new ExplosionParams();
00094 
00095         unsigned int playerId = (unsigned int) luaL_checknumber(L, 1);
00096         FixedVector position = LUAUtil::getVectorFromStack(L, 2);
00097         explosionParams->parseLUA(L, 3);        
00098 
00099         WeaponFireContext fireContext(playerId, 0);
00100         Explosion *explosion = new Explosion(
00101                 position, explosionParams, wrapper->getWeapon(), fireContext);
00102         wrapper->getContext()->getActionController().addAction(explosion);
00103 
00104         return 0;
00105 }
00106 
00107 static int s3d_napalm(lua_State *L) 
00108 {
00109         LUAScript *wrapper = getScript(L);
00110         NapalmParams *napalmParams = new NapalmParams();
00111 
00112         unsigned int playerId = (unsigned int) luaL_checknumber(L, 1);
00113         FixedVector position = LUAUtil::getVectorFromStack(L, 2);
00114         napalmParams->parseLUA(L, 3);   
00115 
00116         WeaponFireContext fireContext(playerId, 0);
00117         Napalm *napalm = new Napalm(
00118                 position[0].asInt(), position[1].asInt(),
00119                 wrapper->getWeapon(), napalmParams, fireContext);
00120         wrapper->getContext()->getActionController().addAction(napalm);
00121 
00122         return 0;
00123 }
00124 
00125 static int s3d_laser(lua_State *L) 
00126 {
00127         LUAScript *wrapper = getScript(L);
00128         LaserParams *laserParams = new LaserParams();
00129 
00130         unsigned int playerId = (unsigned int) luaL_checknumber(L, 1);
00131         FixedVector position = LUAUtil::getVectorFromStack(L, 2);
00132         FixedVector direction = LUAUtil::getVectorFromStack(L, 3);
00133         laserParams->parseLUA(L, 4);
00134 
00135         WeaponFireContext fireContext(playerId, 0);
00136         Laser *laser = new Laser(
00137                 wrapper->getWeapon(), laserParams,
00138                 position, direction,
00139                 fireContext);
00140         wrapper->getContext()->getActionController().addAction(laser);
00141 
00142         return 0;
00143 }
00144 
00145 static const luaL_Reg s3dweaponlib[] = {
00146         {"fire_weapon", s3d_fire_weapon}, 
00147         {"explosion", s3d_explosion},
00148         {"napalm", s3d_napalm},
00149         {"laser", s3d_laser},
00150         {"random", s3d_random},
00151         {NULL, NULL}
00152 };
00153 
00154 LUALIB_API int luaopen_s3dweapon (lua_State *L) {
00155         luaL_register(L, LUA_S3DWEAPONLIBNAME, s3dweaponlib);
00156         return 1;
00157 }

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