diff --git a/src/drivers/win/input.cpp b/src/drivers/win/input.cpp index edded8ea..3420e8b9 100644 --- a/src/drivers/win/input.cpp +++ b/src/drivers/win/input.cpp @@ -38,6 +38,10 @@ #include "sound.h" #include "keyscan.h" +#ifdef _S9XLUA_H +#include "fceulua.h" +#endif + LPDIRECTINPUT7 lpDI=0; void InitInputPorts(bool fourscore); @@ -71,6 +75,13 @@ static void PresetImport(int preset); static uint32 MouseData[3]; static int32 MouseRelative[3]; +#ifdef _S9XLUA_H +static uint32 LuaMouseData[3]; +#else +static uint32* const LuaMouseData = MouseData; +#endif + + //force the input types suggested by the game void ParseGIInput(FCEUGI *gi) { @@ -505,7 +516,13 @@ void FCEUD_UpdateInput() if(joy) UpdateGamepad(false); - if (mouse) GetMouseData(MouseData); + if (mouse) + { + GetMouseData(MouseData); + #ifdef _S9XLUA_H + FCEU_LuaReadZapper(MouseData, LuaMouseData); + #endif + } if (mouse_relative) GetMouseRelative(MouseRelative); } } @@ -553,7 +570,7 @@ void InitInputPorts(bool fourscore) InputDPtr=MouseData; break; case SI_ZAPPER: - InputDPtr=MouseData; + InputDPtr=LuaMouseData; break; case SI_MOUSE: InputDPtr=MouseRelative; diff --git a/src/fceulua.h b/src/fceulua.h index 4a0933d2..08b8a879 100644 --- a/src/fceulua.h +++ b/src/fceulua.h @@ -72,6 +72,7 @@ void FCEU_ReloadLuaCode(); void FCEU_LuaStop(); int FCEU_LuaRunning(); +void FCEU_LuaReadZapper(const uint32* mouse_in, uint32* mouse_out); uint8 FCEU_LuaReadJoypad(int,uint8); // HACK - Function needs controller input int FCEU_LuaSpeed(); int FCEU_LuaFrameskip(); diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index 1a5a7020..76f99cb3 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -208,6 +208,11 @@ static int wasPaused = FALSE; // Transparency strength. 255=opaque, 0=so transparent it's invisible static int transparencyModifier = 255; +// Our zapper. +static int luazapperx = -1; +static int luazappery = -1; +static int luazapperfire = -1; + // Our joypads. static uint8 luajoypads1[4]= { 0xFF, 0xFF, 0xFF, 0xFF }; //x1 static uint8 luajoypads2[4]= { 0x00, 0x00, 0x00, 0x00 }; //0x @@ -278,6 +283,9 @@ static const char* toCString(lua_State* L, int idx=0); static void FCEU_LuaOnStop() { luaRunning = FALSE; + luazapperx = -1; + luazappery = -1; + luazapperfire = -1; for (int i = 0 ; i < 4 ; i++ ){ luajoypads1[i]= 0xFF; // Set these back to pass-through luajoypads2[i]= 0x00; @@ -2536,7 +2544,37 @@ static int zapper_read(lua_State *L){ return 1; } +// zapper.set(table state) +// +// Sets the zapper state for the next frame advance. +static int zapper_set(lua_State* L) { + luaL_checktype(L, 1, LUA_TTABLE); + + luazapperx = -1; + luazappery = -1; + luazapperfire = -1; + + lua_getfield(L, 1, "x"); + if (!lua_isnil(L, -1)) luazapperx = lua_tointeger(L, -1); + lua_pop(L, 1); + + lua_getfield(L, 1, "y"); + if (!lua_isnil(L, -1)) luazappery = lua_tointeger(L, -1); + lua_pop(L, 1); + + lua_getfield(L, 1, "fire"); + if (!lua_isnil(L, -1)) + { + if (lua_toboolean(L, -1)) // True or string + luazapperfire = 1; + if (lua_toboolean(L, -1) == 0 || lua_isstring(L, -1)) // False or string + luazapperfire = 0; + } + lua_pop(L, 1); + + return 0; +} // table joypad.read(int which = 1) // @@ -5815,6 +5853,7 @@ static const struct luaL_reg joypadlib[] = { static const struct luaL_reg zapperlib[] = { {"read", zapper_read}, + {"set", zapper_set}, {NULL,NULL} }; @@ -6309,6 +6348,15 @@ int FCEU_LuaRunning() { return (int) (L != NULL); // should return true if callback functions are active. } +/** + * Applies zapper.set overrides to zapper input. + */ +void FCEU_LuaReadZapper(const uint32* mouse_in, uint32* mouse_out) +{ + mouse_out[0] = luazapperx >= 0 ? luazapperx : mouse_in[0]; + mouse_out[1] = luazappery >= 0 ? luazappery : mouse_in[1]; + mouse_out[2] = luazapperfire >= 0 ? (luazapperfire | (mouse_in[2] & ~1)) : mouse_in[2]; +} /** * Returns true if Lua would like to steal the given joypad control. diff --git a/web/help/LuaFunctionsList.html b/web/help/LuaFunctionsList.html index 44b06457..120bb3e8 100644 --- a/web/help/LuaFunctionsList.html +++ b/web/help/LuaFunctionsList.html @@ -418,8 +418,17 @@


The return table consists of 3 values: x, y, and fire.  x and y are the x,y coordinates of the zapper target in terms of pixels.  fire represents the zapper firing.  0 = not firing, 1 = firing


+

zapper.set(table input)


-

Note: The zapper is always controller 2 on the NES so there is no player argument to this function.

+

Sets the zapper input state.

+


+

Taple entries (nil or -1 to leave unaffected):

+

x    - Forces the X position

+

y    - Forces the Y position

+

fire - Forces trigger (true/1 on, false/0 off)

+


+


+

Note: The zapper is always controller 2 on the NES so there is no player argument to these functions.



Input Library