diff --git a/src/lua_hook.h b/src/lua_hook.h index 822edf79f..a8a0c1164 100644 --- a/src/lua_hook.h +++ b/src/lua_hook.h @@ -48,6 +48,7 @@ enum hook { hook_MobjMoveBlocked, hook_MapThingSpawn, hook_FollowMobj, + hook_MusicChange, hook_MAX // last hook }; @@ -87,5 +88,6 @@ boolean LUAh_HurtMsg(player_t *player, mobj_t *inflictor, mobj_t *source, UINT8 #define LUAh_MobjMoveBlocked(mo) LUAh_MobjHook(mo, hook_MobjMoveBlocked) // Hook for P_XYMovement (when movement is blocked) boolean LUAh_MapThingSpawn(mobj_t *mo, mapthing_t *mthing); // Hook for P_SpawnMapThing by mobj type boolean LUAh_FollowMobj(player_t *player, mobj_t *mo); // Hook for P_PlayerAfterThink Smiles mobj-following +boolean LUAh_MusicChange(const char *oldname, const char *newname, char *newmusic); // Hook for music changes #endif diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index 53886f7ba..9d532b928 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -59,6 +59,7 @@ const char *const hookNames[hook_MAX+1] = { "MobjMoveBlocked", "MapThingSpawn", "FollowMobj", + "MusicChange", NULL }; @@ -1191,5 +1192,41 @@ boolean LUAh_FollowMobj(player_t *player, mobj_t *mobj) lua_settop(gL, 0); return hooked; } +// Hook for music changes +boolean LUAh_MusicChange(const char *oldname, const char *newname, char *newmusic) // UINT16 mflags, boolean looping) +{ + hook_p hookp; + boolean hooked = false; + + strncpy(newmusic, newname, 7); + + if (!gL || !(hooksAvailable[hook_MusicChange/8] & (1<<(hook_MusicChange%8)))) + return false; + + lua_settop(gL, 0); + + for (hookp = roothook; hookp; hookp = hookp->next) + if (hookp->type == hook_MusicChange) + { + lua_pushfstring(gL, FMT_HOOKID, hookp->id); + lua_gettable(gL, LUA_REGISTRYINDEX); + lua_pushstring(gL, oldname); + lua_pushstring(gL, newname); + if (lua_pcall(gL, 2, 1, 0)) { + CONS_Alert(CONS_WARNING,"%s\n",lua_tostring(gL,-1)); + lua_pop(gL, 1); + continue; + } + if (lua_isboolean(gL, -1) && lua_toboolean(gL, -1)) + hooked = true; + else if (lua_isstring(gL, -1)) + strncpy(newmusic, lua_tostring(gL, -1), 7); + lua_pop(gL, 1); + } + + lua_settop(gL, 0); + newmusic[6] = 0; + return hooked; +} #endif diff --git a/src/s_sound.c b/src/s_sound.c index 680eed4ff..211950294 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -38,6 +38,10 @@ extern INT32 msg_id; #include "p_local.h" // camera info #include "fastcmp.h" +#ifdef HAVE_BLUA +#include "lua_hook.h" // MusicChange hook +#endif + #ifdef HW3SOUND // 3D Sound Interface #include "hardware/hw3sound.h" @@ -1380,19 +1384,28 @@ void S_ChangeMusic(const char *mmusic, UINT16 mflags, boolean looping) if ((nomidimusic || music_disabled) && (nodigimusic || digital_disabled)) return; + char newmusic[7]; +#ifdef HAVE_BLUA + if(LUAh_MusicChange(music_name, mmusic, newmusic)) // todo: mflags and looping? + return; +#else + strncpy(newmusic, mmusic, 7); +#endif + newmusic[6] = 0; + // No Music (empty string) - if (mmusic[0] == 0) + if (newmusic[0] == 0) { S_StopMusic(); return; } - if (strncmp(music_name, mmusic, 6)) + if (strncmp(music_name, newmusic, 6)) { S_StopMusic(); // shutdown old music - if (!S_DigMusic(mmusic, looping) && !S_MIDIMusic(mmusic, looping)) + if (!S_DigMusic(newmusic, looping) && !S_MIDIMusic(newmusic, looping)) { - CONS_Alert(CONS_ERROR, M_GetText("Music lump %.6s not found!\n"), mmusic); + CONS_Alert(CONS_ERROR, M_GetText("Music lump %.6s not found!\n"), newmusic); return; } }