Skip to content

Commit b8a2b42

Browse files
committed
dsda: add use callback
1 parent b798195 commit b8a2b42

File tree

10 files changed

+127
-55
lines changed

10 files changed

+127
-55
lines changed

Assets/dll/dsda.wbx.zst

-1.6 KB
Binary file not shown.

src/BizHawk.Client.Common/lua/INamedLuaFunction.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ public interface INamedLuaFunction
1313
MemoryCallbackDelegate MemCallback { get; }
1414

1515
/// <summary>for <c>doom.on_prandom</c>; single param: caller of RNG, per categories <see href="https://github.com/TASEmulators/dsda-doom/blob/7f03360ce0e9000c394fb99869d78adf4603ade5/prboom2/src/m_random.h#L63-L133">in source</see></summary>
16-
Action<int> RandomCallback { get; }
16+
Action<int> RandomCallback { get; }
17+
18+
/// <summary>for <c>doom.on_use</c>; single param: player who triggered the line special</summary>
19+
Action<int> UseCallback { get; }
1720

1821
string Name { get; }
1922

src/BizHawk.Client.Common/lua/LuaHelperLibs/DoomLuaLibrary.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,30 @@ public string OnPrandom(LuaFunction luaf, string name = null)
4848
nlf.OnRemove += () => callbacks.Remove(nlf.RandomCallback);
4949
return nlf.GuidStr;
5050
}
51+
52+
/// <exception cref="InvalidOperationException">loaded core is not DSDA-Doom</exception>
53+
#pragma warning disable MA0136 // multi-line string literals (passed to `[LuaMethodExample]`, which converts to host newlines)
54+
[LuaMethodExample("""
55+
local usesuccess_cb_id = doom.on_use(function(player)
56+
console.log("Use press succeeded for player "..player);
57+
end, "Use notifier");
58+
""")]
59+
#pragma warning restore MA0136
60+
[LuaMethod(
61+
name: "on_use",
62+
description: "Fires when P_UseSpecialLine() is called by one of the players via the Use button input. Your callback can have 1 parameter, which will be an integer identifying which player triggered it.")]
63+
public string OnUse(LuaFunction luaf, string name = null)
64+
{
65+
if (Emulator is not DSDA dsda)
66+
{
67+
throw new InvalidOperationException(ERR_MSG_UNSUPPORTED_CORE);
68+
}
69+
70+
var callbacks = dsda.UseCallbacks;
71+
var nlf = CreateAndRegisterNamedFunction(luaf, "OnUse", LogOutputCallback, CurrentFile, name: name);
72+
callbacks.Add(nlf.UseCallback);
73+
nlf.OnRemove += () => callbacks.Remove(nlf.UseCallback);
74+
return nlf.GuidStr;
75+
}
5176
}
5277
}

src/BizHawk.Client.Common/lua/NamedLuaFunction.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ public NamedLuaFunction(LuaFunction function, string theEvent, Action<string> lo
105105
luaLibraries.IsInInputOrMemoryCallback = false;
106106
}
107107
};
108+
UseCallback = player =>
109+
{
110+
luaLibraries.IsInInputOrMemoryCallback = true;
111+
try
112+
{
113+
Callback([player]);
114+
}
115+
finally
116+
{
117+
luaLibraries.IsInInputOrMemoryCallback = false;
118+
}
119+
};
108120
}
109121

110122
public void DetachFromScript()
@@ -139,6 +151,8 @@ public string GuidStr
139151

140152
public Action<int> RandomCallback { get; }
141153

154+
public Action<int> UseCallback { get; }
155+
142156
public void Call(string name = null)
143157
{
144158
LuaSandbox.Sandbox(LuaFile.Thread, () =>

src/BizHawk.Emulation.Cores/Computers/Doom/DSDA.IEmulator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ public bool FrameAdvance(IController controller, bool renderVideo, bool renderAu
258258
};
259259

260260
_core.dsda_set_random_callback(RandomCallbacks.Count > 0 ? _randomCallback : null);
261+
_core.dsda_set_use_callback(UseCallbacks.Count > 0 ? _useCallback : null);
261262

262263
IsLagFrame = _core.dsda_frame_advance(
263264
automapButtons,

src/BizHawk.Emulation.Cores/Computers/Doom/DSDA.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ public DSDA(CoreLoadParameters<DoomSettings, DoomSyncSettings> lp)
167167
foreach (var cb in RandomCallbacks) cb(pr_class);
168168
};
169169

170+
_useCallback = player =>
171+
{
172+
foreach (var cb in UseCallbacks) cb(player);
173+
};
174+
170175
_elf = new WaterboxHost(new WaterboxOptions
171176
{
172177
Path = PathUtils.DllDirectoryPath,
@@ -184,7 +189,7 @@ public DSDA(CoreLoadParameters<DoomSettings, DoomSyncSettings> lp)
184189
{
185190
var callingConventionAdapter = CallingConventionAdapters.MakeWaterbox(
186191
[
187-
_loadCallback, _randomCallback, _errorCallback
192+
_loadCallback, _randomCallback, _useCallback, _errorCallback
188193
], _elf);
189194

190195
using (_elf.EnterExit())
@@ -282,6 +287,7 @@ public DSDA(CoreLoadParameters<DoomSettings, DoomSyncSettings> lp)
282287
ControllerDefinition = CreateControllerDefinition(_syncSettings);
283288

284289
_core.dsda_set_random_callback(RandomCallbacks.Count > 0 ? _randomCallback : null);
290+
_core.dsda_set_use_callback(UseCallbacks.Count > 0 ? _useCallback : null);
285291
}
286292
catch
287293
{
@@ -386,9 +392,11 @@ private static bool PlayerPresent(DoomSyncSettings syncSettings, int port) =>
386392
private List<IRomAsset> _pwadFiles;
387393
private LibDSDA.GameMode _gameMode;
388394
private LibDSDA.random_cb _randomCallback;
395+
private LibDSDA.use_cb _useCallback;
389396
private LibDSDA.error_cb _errorCallback;
390397

391398
public List<Action<int>> RandomCallbacks = [ ];
399+
public List<Action<int>> UseCallbacks = [ ];
392400
public string RomDetails { get; } // IRomInfo
393401

394402
private void ErrorCallback(string error)

src/BizHawk.Emulation.Cores/Computers/Doom/LibDSDA.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ public abstract bool dsda_frame_advance(
153153
[BizImport(CallingConvention.Cdecl)]
154154
public abstract void dsda_set_random_callback(random_cb cb);
155155

156+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
157+
public delegate void use_cb(int player);
158+
[BizImport(CallingConvention.Cdecl)]
159+
public abstract void dsda_set_use_callback(use_cb cb);
160+
156161
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
157162
public delegate void error_cb(string error);
158163
[BizImport(CallingConvention.Cdecl)]

waterbox/dsda/BizhawkInterface.c

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,18 @@ ECL_EXPORT bool dsda_frame_advance(AutomapButtons buttons, struct PackedPlayerIn
421421
return !wipeDone;
422422
}
423423

424+
// P_UseSpecialLine() calls by player
425+
ECL_ENTRY void (*use_callback_cb)(int);
426+
void biz_use_callback(int player)
427+
{
428+
if (use_callback_cb)
429+
use_callback_cb(player);
430+
}
431+
ECL_EXPORT void dsda_set_use_callback(ECL_ENTRY void (*cb)(int))
432+
{
433+
use_callback_cb = cb;
434+
}
435+
424436
// P_Random() calls
425437
ECL_ENTRY void (*random_callback_cb)(int);
426438
void biz_random_callback(int pr_class)
@@ -572,67 +584,70 @@ ECL_EXPORT int dsda_add_wad_file(const char *filename, const int size, ECL_ENTRY
572584
// TODO: expose sectors and linedefs like xdre does (but better)
573585
ECL_EXPORT char dsda_read_memory_array(int type, uint32_t addr)
574586
{
575-
if (type == ARRAY_PLAYERS)
587+
switch (type)
576588
{
577-
if (addr >= g_maxplayers * MEMORY_PADDED_PLAYER)
578-
return MEMORY_OUT_OF_BOUNDS;
589+
case ARRAY_PLAYERS:
590+
{
591+
if (addr >= g_maxplayers * MEMORY_PADDED_PLAYER)
592+
return MEMORY_OUT_OF_BOUNDS;
579593

580-
int index = addr / MEMORY_PADDED_PLAYER;
581-
int offset = addr % MEMORY_PADDED_PLAYER;
594+
int index = addr / MEMORY_PADDED_PLAYER;
595+
int offset = addr % MEMORY_PADDED_PLAYER;
582596

583-
if (!playeringame[index] || offset >= sizeof(player_t))
584-
return MEMORY_NULL;
597+
if (!playeringame[index] || offset >= sizeof(player_t))
598+
return MEMORY_NULL;
585599

586-
player_t *player = &players[index];
600+
player_t *player = &players[index];
587601

588-
char *data = (char *)player + offset;
589-
return *data;
590-
}
591-
else if (type == ARRAY_THINGS)
592-
{
593-
if (addr >= thinker_count * MEMORY_PADDED_THING)
594-
return MEMORY_OUT_OF_BOUNDS;
602+
char *data = (char *)player + offset;
603+
return *data;
604+
}
605+
case ARRAY_THINGS:
606+
{
607+
if (addr >= thinker_count * MEMORY_PADDED_THING)
608+
return MEMORY_OUT_OF_BOUNDS;
595609

596-
int index = addr / MEMORY_PADDED_THING;
597-
int offset = addr % MEMORY_PADDED_THING;
598-
mobj_t *mobj = mobj_ptrs[index];
610+
int index = addr / MEMORY_PADDED_THING;
611+
int offset = addr % MEMORY_PADDED_THING;
612+
mobj_t *mobj = mobj_ptrs[index];
599613

600-
if (mobj == NULL || offset >= sizeof(mobj_t))
601-
return MEMORY_NULL;
614+
if (mobj == NULL || offset >= sizeof(mobj_t))
615+
return MEMORY_NULL;
602616

603-
char *data = (char *)mobj + offset;
604-
return *data;
605-
}
606-
else if (type == ARRAY_LINES)
607-
{
608-
if (addr >= numlines * MEMORY_PADDED_LINE)
609-
return MEMORY_OUT_OF_BOUNDS;
617+
char *data = (char *)mobj + offset;
618+
return *data;
619+
}
620+
case ARRAY_LINES:
621+
{
622+
if (addr >= numlines * MEMORY_PADDED_LINE)
623+
return MEMORY_OUT_OF_BOUNDS;
610624

611-
int index = addr / MEMORY_PADDED_LINE;
612-
int offset = addr % MEMORY_PADDED_LINE;
613-
line_t *line = &lines[index];
625+
int index = addr / MEMORY_PADDED_LINE;
626+
int offset = addr % MEMORY_PADDED_LINE;
627+
line_t *line = &lines[index];
614628

615-
if (line == NULL || offset >= sizeof(line_t))
616-
return MEMORY_NULL;
629+
if (line == NULL || offset >= sizeof(line_t))
630+
return MEMORY_NULL;
617631

618-
char *data = (char *)line + offset;
619-
return *data;
620-
}
621-
else if (type == ARRAY_SECTORS)
622-
{
623-
if (addr >= numsectors * MEMORY_PADDED_SECTOR)
624-
return MEMORY_OUT_OF_BOUNDS;
632+
char *data = (char *)line + offset;
633+
return *data;
634+
}
635+
case ARRAY_SECTORS:
636+
{
637+
if (addr >= numsectors * MEMORY_PADDED_SECTOR)
638+
return MEMORY_OUT_OF_BOUNDS;
625639

626-
int index = addr / MEMORY_PADDED_SECTOR;
627-
int offset = addr % MEMORY_PADDED_SECTOR;
628-
sector_t *sector = &sectors[index];
640+
int index = addr / MEMORY_PADDED_SECTOR;
641+
int offset = addr % MEMORY_PADDED_SECTOR;
642+
sector_t *sector = &sectors[index];
629643

630-
if (sector == NULL || offset >= sizeof(sector_t))
631-
return MEMORY_NULL;
644+
if (sector == NULL || offset >= sizeof(sector_t))
645+
return MEMORY_NULL;
632646

633-
char *data = (char *)sector + offset;
634-
return *data;
647+
char *data = (char *)sector + offset;
648+
return *data;
649+
}
650+
default:
651+
return MEMORY_OUT_OF_BOUNDS;
635652
}
636-
else
637-
return MEMORY_OUT_OF_BOUNDS;
638653
}

waterbox/dsda/BizhawkInterface.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,11 @@ enum HudMode
122122

123123
enum MemoryArrayType
124124
{
125-
ARRAY_PLAYERS = 0,
126-
ARRAY_THINGS = 1,
127-
ARRAY_LINES = 2,
128-
ARRAY_SECTORS = 3
125+
ARRAY_PLAYERS = 0,
126+
ARRAY_THINGS = 1,
127+
ARRAY_LINES = 2,
128+
ARRAY_SECTORS = 3,
129+
ARRAY_BLOCKMAP = 4,
129130
};
130131

131132
typedef union

waterbox/dsda/core

0 commit comments

Comments
 (0)