00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 }