Merge pull request #103 from bbbradsmith/lua_zapper_set

zapper.set for lua
This commit is contained in:
zeromus 2020-05-31 16:13:29 -05:00 committed by GitHub
commit 74b11ae84c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 3 deletions

View File

@ -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;

View File

@ -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();

View File

@ -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.

View File

@ -418,8 +418,17 @@
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts37">The return table consists of 3 values: x, y, and fire. &nbsp;x and y are the x,y coordinates of the zapper target in terms of pixels. &nbsp;fire represents the zapper firing. &nbsp;0 = not firing, 1 = firing</span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts64">zapper.set(table input)</span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts37">Note: The zapper is always controller 2 on the NES so there is no player argument to this function.</span></p>
<p><span class="rvts37">Sets the zapper input state.</span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts37">Taple entries (nil or -1 to leave unaffected):</span></p>
<p><span class="rvts37">x &nbsp; &nbsp;- Forces the X position </span></p>
<p><span class="rvts37">y &nbsp; &nbsp;- Forces the Y position</span></p>
<p><span class="rvts37">fire - Forces trigger (true/1 on, false/0 off)</span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts37">Note: The zapper is always controller 2 on the NES so there is no player argument to these functions.</span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts71">Input Library</span></p>