diff --git a/artifacts/scripting/headers/sfall.h b/artifacts/scripting/headers/sfall.h
index 42076785..eacee997 100644
--- a/artifacts/scripting/headers/sfall.h
+++ b/artifacts/scripting/headers/sfall.h
@@ -76,6 +76,8 @@
#define HOOK_ROLLCHECK (46)
#define HOOK_BESTWEAPON (47)
#define HOOK_CANUSEWEAPON (48)
+// RESERVED 49..60
+#define HOOK_BUILDSFXWEAPON (61)
// Valid arguments to list_begin
#define LIST_CRITTERS (0)
diff --git a/artifacts/scripting/hooks.yml b/artifacts/scripting/hooks.yml
index 787309ce..2efdfb86 100644
--- a/artifacts/scripting/hooks.yml
+++ b/artifacts/scripting/hooks.yml
@@ -818,4 +818,18 @@
int arg3 - original result of engine function: 1 - can use, 0 - cannot use
int ret0 - overrides the result of engine function. Any non-zero value allows using the weapon
+
+
+- name: BuildSfxWeapon
+ id: HOOK_BUILDSFXWEAPON
+ doc: |
+ Runs before each weapon sound effect is played or put in the animation queue to determine the name of sound effect file based on the weapon, target and action being performed.
+
+ ```
+ int arg0 - weapon sound effect type: 0 - ready/reload, 1 - attack, 2 - out of ammo, 3 - flying (for projectile weapons), 4 - hit
+ Item arg1 - the weapon being used
+ int arg2 - attack type (see ATKTYPE_* constants)
+ Obj arg3 - target of the attack (can be 0)
+
+ String ret0 - the new sound file name to use, without extension (relative to "sound\sfx" path)
```
diff --git a/artifacts/scripting/hookscripts.md b/artifacts/scripting/hookscripts.md
index d0656d27..bfd0355c 100644
--- a/artifacts/scripting/hookscripts.md
+++ b/artifacts/scripting/hookscripts.md
@@ -936,3 +936,18 @@ int arg3 - original result of engine function: 1 - can use, 0 - cannot use
int ret0 - overrides the result of engine function. Any non-zero value allows using the weapon
```
+
+-------------------------------------------
+
+#### `HOOK_BUILDSFXWEAPON (hs_buildsfxweapon.int)`
+
+Runs before each weapon sound effect is played or put in the animation queue to determine the name of sound effect file based on the weapon, target and action being performed.
+
+```
+int arg0 - weapon sound effect type: 0 - ready/reload, 1 - attack, 2 - out of ammo, 3 - flying (for projectile weapons), 4 - hit
+Item arg1 - the weapon being used
+int arg2 - attack type (see ATKTYPE_* constants)
+Obj arg3 - target of the attack (can be 0)
+
+String ret0 - the new sound file name to use, without extension (relative to "sound\sfx" path)
+```
diff --git a/sfall/Modules/HookScripts.cpp b/sfall/Modules/HookScripts.cpp
index 9ed79a47..2dd422c0 100644
--- a/sfall/Modules/HookScripts.cpp
+++ b/sfall/Modules/HookScripts.cpp
@@ -31,6 +31,7 @@
#include "HookScripts\InventoryHs.h"
#include "HookScripts\ObjectHs.h"
#include "HookScripts\MiscHs.h"
+#include "HookScripts\SoundHs.h"
#include "HookScripts.h"
@@ -110,6 +111,19 @@ static HooksInjectInfo injectHooks[] = {
{HOOK_ROLLCHECK, Inject_RollCheckHook, 0},
{HOOK_BESTWEAPON, Inject_BestWeaponHook, 0},
{HOOK_CANUSEWEAPON, Inject_CanUseWeaponHook, 0},
+ {-1}, // RESERVED
+ {-1}, // RESERVED
+ {-1}, // HOOK_MOUSEWHEEL
+ {-1}, // RESERVED
+ {-1}, // RESERVED
+ {-1}, // RESERVED
+ {-1}, // HOOK_COMBATATTACK
+ {-1}, // RESERVED
+ {-1}, // RESERVED
+ {-1}, // RESERVED
+ {-1}, // RESERVED
+ {-1}, // RESERVED
+ {HOOK_BUILDSFXWEAPON, Inject_BuildSfxWeaponHook, 0},
};
void HookScripts::InjectingHook(int hookId) {
@@ -216,6 +230,7 @@ void HookScripts::LoadHookScripts() {
InitInventoryHookScripts();
InitObjectHookScripts();
InitMiscHookScripts();
+ InitSoundHookScripts();
HookScripts::LoadHookScript("hs_keypress", HOOK_KEYPRESS);
HookScripts::LoadHookScript("hs_mouseclick", HOOK_MOUSECLICK);
diff --git a/sfall/Modules/HookScripts.h b/sfall/Modules/HookScripts.h
index a8c5b755..d184fe04 100644
--- a/sfall/Modules/HookScripts.h
+++ b/sfall/Modules/HookScripts.h
@@ -75,6 +75,8 @@ enum HookType
HOOK_ROLLCHECK = 46,
HOOK_BESTWEAPON = 47,
HOOK_CANUSEWEAPON = 48,
+ // RESERVED 49 to 60
+ HOOK_BUILDSFXWEAPON = 61,
HOOK_COUNT
};
diff --git a/sfall/Modules/HookScripts/SoundHs.cpp b/sfall/Modules/HookScripts/SoundHs.cpp
new file mode 100644
index 00000000..094d70aa
--- /dev/null
+++ b/sfall/Modules/HookScripts/SoundHs.cpp
@@ -0,0 +1,75 @@
+#include "..\..\FalloutEngine\Fallout2.h"
+#include "..\..\SafeWrite.h"
+#include "..\HookScripts.h"
+#include "Common.h"
+
+#include "SoundHs.h"
+
+using namespace sfall::script;
+
+// Object hook scripts
+namespace sfall
+{
+
+static DWORD __fastcall BuildSfxNameHook_Script(long effectType, fo::GameObject* weapon, long hitMode, fo::GameObject* target) {
+ BeginHook();
+ allowNonIntReturn = true;
+ argCount = 4;
+
+ args[0] = effectType;
+ args[1] = (DWORD)weapon;
+ args[2] = (DWORD)hitMode;
+ args[3] = (DWORD)target;
+
+ RunHookScript(HOOK_BUILDSFXWEAPON);
+
+ DWORD textPtr = cRet > 0 && retTypes[0] == DataType::STR
+ ? rets[0]
+ : 0;
+ EndHook();
+
+ return textPtr; // -1 - default handler
+}
+
+static __declspec(naked) void gsnd_build_weapon_sfx_name_hook() {
+ __asm {
+ pushadc; // save state
+ push ebx;
+ push ecx; // target
+ push ebx; // hitMode
+ // edx - weapon
+ mov ecx, eax; // effectType
+ call BuildSfxNameHook_Script;
+ test eax, eax; // pointer to text
+ pop ebx; // restore state
+ pop ecx;
+ pop edx;
+ jnz skip;
+ pop eax;
+ jmp fo::funcoffs::gsnd_build_weapon_sfx_name_;
+skip:
+ add esp, 4;
+ retn;
+ }
+}
+
+
+void Inject_BuildSfxWeaponHook() {
+ HookCalls(gsnd_build_weapon_sfx_name_hook, {
+ 0x410DB3, // show_damage_to_object
+ 0x411397, 0x411538, // action_melee
+ 0x411787, 0x41196C, 0x411A96, 0x411B82, // action_ranged
+ 0x4268F5, // combat_attack_this
+ 0x42A9B4, 0x42AA92, 0x42AAF0, // ai_try_attack
+ 0x42AF4C, // cai_attempt_w_reload
+ 0x45BD31, // op_sfx_build_weapon_name
+ 0x460B87, // intface_item_reload
+ 0x476629, // drop_ammo_into_weapon
+ });
+}
+
+void InitSoundHookScripts() {
+ HookScripts::LoadHookScript("hs_buildsfxweapon", HOOK_BUILDSFXWEAPON);
+}
+
+}
diff --git a/sfall/Modules/HookScripts/SoundHs.h b/sfall/Modules/HookScripts/SoundHs.h
new file mode 100644
index 00000000..27aa0b7f
--- /dev/null
+++ b/sfall/Modules/HookScripts/SoundHs.h
@@ -0,0 +1,10 @@
+#pragma once
+
+namespace sfall
+{
+
+void InitSoundHookScripts();
+
+void Inject_BuildSfxWeaponHook();
+
+}
diff --git a/sfall/Modules/Sound.cpp b/sfall/Modules/Sound.cpp
index 040364c1..16f549e0 100644
--- a/sfall/Modules/Sound.cpp
+++ b/sfall/Modules/Sound.cpp
@@ -1061,6 +1061,7 @@ void Sound::init() {
if (IniReader::GetConfigInt("Sound", "AutoSearchSFX", 1)) {
HookCalls(sfxl_init_hook, {0x4A9999, 0x4A9B34});
SafeWrite8(0x4A9B3F, 0xA9); // jz 0x4A9BEC (skip error message)
+ SafeWrite8(0x4AA060, 127); // fix crash when ACM file has name longer than 12 symbols
}
if (IniReader::GetConfigString("Sound", "MainMenuMusic", "", mainMenuMusic, 9)) {
diff --git a/sfall/ddraw.vcxproj b/sfall/ddraw.vcxproj
index 8b480592..f8bcdba0 100644
--- a/sfall/ddraw.vcxproj
+++ b/sfall/ddraw.vcxproj
@@ -402,6 +402,7 @@
+
@@ -544,6 +545,7 @@
+
diff --git a/sfall/ddraw.vcxproj.filters b/sfall/ddraw.vcxproj.filters
index 527ec8b6..82dc79d0 100644
--- a/sfall/ddraw.vcxproj.filters
+++ b/sfall/ddraw.vcxproj.filters
@@ -460,6 +460,7 @@
Modules
+
@@ -840,6 +841,7 @@
Modules
+