Added new HOOK_SETGLOBALVAR (#34) (from Mr.Stalin)

This commit is contained in:
NovaRain
2018-05-02 11:14:02 +08:00
parent 31f42128e1
commit 371a299640
6 changed files with 72 additions and 35 deletions
+1
View File
@@ -49,6 +49,7 @@
#define HOOK_ADJUSTFID (26) #define HOOK_ADJUSTFID (26)
#define HOOK_COMBATTURN (27) #define HOOK_COMBATTURN (27)
#define HOOK_CARTRAVEL (28) #define HOOK_CARTRAVEL (28)
#define HOOK_SETGLOBALVAR (29)
//Valid arguments to list_begin //Valid arguments to list_begin
#define LIST_CRITTERS (0) #define LIST_CRITTERS (0)
+23 -12
View File
@@ -77,7 +77,7 @@ The defines to use for the hooktype are in sfall.h.
HOOK_TOHIT (hs_tohit.int) HOOK_TOHIT (hs_tohit.int)
Runs when fallout is calculating the chances of an attack striking a target Runs when Fallout is calculating the chances of an attack striking a target
Runs after the hit chance is fully calculated normally, including applying the 95% cap Runs after the hit chance is fully calculated normally, including applying the 95% cap
int arg1 - The unmodified hit chance int arg1 - The unmodified hit chance
@@ -94,7 +94,7 @@ int ret1 - the new hit chance
HOOK_AFTERHITROLL (hs_afterhitroll.int) HOOK_AFTERHITROLL (hs_afterhitroll.int)
Runs after fallout has decided if an attack will hit or miss Runs after Fallout has decided if an attack will hit or miss
int arg1 - If the attack will hit. (0 - critical miss, 1 - miss, 2 - hit, 3 - critical hit) int arg1 - If the attack will hit. (0 - critical miss, 1 - miss, 2 - hit, 3 - critical hit)
critter arg2 - The attacker critter arg2 - The attacker
@@ -110,22 +110,22 @@ critter ret3 - Override the target of the attack
HOOK_CALCAPCOST (hs_calcapcost.int) HOOK_CALCAPCOST (hs_calcapcost.int)
Runs whenever fallout is calculating the ap cost of using the weapon (or unarmed attack). Doesn't run for using other item types or moving. Runs whenever Fallout is calculating the AP cost of using the weapon (or unarmed attack). Doesn't run for using other item types or moving.
Note that the first time a game is loaded, this script doesn't run before the initial interface is drawn, so if the script effects the ap cost of whatever is in the players hands at the time the wrong ap cost will be shown. It will be fixed the next time the interface is redrawn. Note that the first time a game is loaded, this script doesn't run before the initial interface is drawn, so if the script effects the AP cost of whatever is in the players hands at the time the wrong AP cost will be shown. It will be fixed the next time the interface is redrawn.
You can get the weapon object by checking item slot based on attack type (ATKTYPE_LWEP1, ATKTYPE_LWEP2, etc) and then calling critter_inven_obj(). You can get the weapon object by checking item slot based on attack type (ATKTYPE_LWEP1, ATKTYPE_LWEP2, etc) and then calling critter_inven_obj().
critter arg1 - The critter performing the action critter arg1 - The critter performing the action
int arg2 - Attack Type (see ATKTYPE_* constants) int arg2 - Attack Type (see ATKTYPE_* constants)
int arg3 - Is aimed attack (1 or 0) int arg3 - Is aimed attack (1 or 0)
int arg4 - The normal ap cost int arg4 - The normal AP cost
int ret1 - The new ap cost int ret1 - The new AP cost
------------------------------------------- -------------------------------------------
HOOK_DEATHANIM1 (hs_deathanim1.int) HOOK_DEATHANIM1 (hs_deathanim1.int)
Runs before fallout tries to calculate the death animation. Lets you switch out which weapon fallout sees Runs before Fallout tries to calculate the death animation. Lets you switch out which weapon Fallout sees
int arg1 - The pid of the weapon performing the attack. (May be -1 if the attack is unarmed) int arg1 - The pid of the weapon performing the attack. (May be -1 if the attack is unarmed)
critter arg2 - The attacker critter arg2 - The attacker
@@ -138,14 +138,14 @@ int ret1 - The pid of an object to override the attacking weapon with
HOOK_DEATHANIM2 (hs_deathanim2.int) HOOK_DEATHANIM2 (hs_deathanim2.int)
Runs after fallout has calculated the death animation. Lets you set your own custom frame id, so more powerful than hs_deathanim1, but performs no validation. Runs after Fallout has calculated the death animation. Lets you set your own custom frame id, so more powerful than hs_deathanim1, but performs no validation.
When using critter_dmg function, this script will also run. In that case weapon pid will be -1 and target will point to an object with obj_art_fid == 0x20001F5. When using critter_dmg function, this script will also run. In that case weapon pid will be -1 and target will point to an object with obj_art_fid == 0x20001F5.
item arg1 - The pid of the weapon performing the attack. (May be -1 if the attack is unarmed) item arg1 - The pid of the weapon performing the attack. (May be -1 if the attack is unarmed)
critter arg2 - The attacker critter arg2 - The attacker
critter arg3 - The target critter arg3 - The target
int arg4 - The amount of damage int arg4 - The amount of damage
int arg5 - The death anim id calculated by fallout int arg5 - The death anim id calculated by Fallout
int ret1 - The death anim id to override with int ret1 - The death anim id to override with
@@ -271,13 +271,13 @@ int ret1 - the modified value of all of the goods
HOOK_MOVECOST (hs_movecost.int) HOOK_MOVECOST (hs_movecost.int)
Runs when calculating the ap cost of movement Runs when calculating the AP cost of movement
Critter arg1 - the critter doing the moving Critter arg1 - the critter doing the moving
int arg2 - the number of hexes being moved int arg2 - the number of hexes being moved
int arg3 - the original ap cost int arg3 - the original AP cost
int ret1 - the new ap cost int ret1 - the new AP cost
------------------------------------------- -------------------------------------------
@@ -474,3 +474,14 @@ int arg2 - vanilla fuel consumption (100 and below)
int ret1 - car speed override (pass -1 if you just want to override fuel consumption) int ret1 - car speed override (pass -1 if you just want to override fuel consumption)
int ret2 - fuel consumption override int ret2 - fuel consumption override
-------------------------------------------
HOOK_SETGLOBALVAR (hs_setglobalvar.int)
Runs when setting the value of a global variable.
int arg1 - the index number of the global variable being set
int arg2 - the set value of the global variable
int ret1 - overrides the value of the global variable
+1
View File
@@ -55,6 +55,7 @@ enum HookType
HOOK_ADJUSTFID = 26, HOOK_ADJUSTFID = 26,
HOOK_COMBATTURN = 27, HOOK_COMBATTURN = 27,
HOOK_CARTRAVEL = 28, HOOK_CARTRAVEL = 28,
HOOK_SETGLOBALVAR = 29,
HOOK_COUNT HOOK_COUNT
}; };
+37 -1
View File
@@ -1,6 +1,7 @@
#include "..\..\FalloutEngine\Fallout2.h" #include "..\..\FalloutEngine\Fallout2.h"
#include "..\..\SafeWrite.h" #include "..\..\SafeWrite.h"
#include "..\HookScripts.h" #include "..\HookScripts.h"
#include "..\Karma.h"
#include "Common.h" #include "Common.h"
#include "MiscHs.h" #include "MiscHs.h"
@@ -116,7 +117,6 @@ end:
} }
} }
static void __declspec(naked) UseSkillHook() { static void __declspec(naked) UseSkillHook() {
__asm { __asm {
hookbegin(4); hookbegin(4);
@@ -257,6 +257,38 @@ static void __declspec(naked) CarTravelHack() {
} }
} }
static int newGVarValue;
static void _stdcall GlobalVarHookScript(DWORD number, int value) {
int old = fo::var::game_global_vars[number];
BeginHook();
argCount = 2;
args[0] = number;
args[1] = value;
RunHookScript(HOOK_SETGLOBALVAR);
EndHook();
if (cRet == 1) value = rets[0];
if (number == fo::GVAR_PLAYER_REPUTATION && displayKarmaChanges) {
int diff = value - old;
if (diff != 0) Karma::DisplayKarma(diff);
}
newGVarValue = value;
}
static void __declspec(naked) SetGlobalVarHook() {
__asm {
pushad;
push edx; // value
push eax; // number
call GlobalVarHookScript;
popad;
mov edx, newGVarValue;
jmp fo::funcoffs::game_set_global_var_;
}
}
void InitMiscHookScripts() { void InitMiscHookScripts() {
LoadHookScript("hs_useobjon", HOOK_USEOBJON); LoadHookScript("hs_useobjon", HOOK_USEOBJON);
HookCalls(UseObjOnHook, { 0x49C606, 0x473619 }); HookCalls(UseObjOnHook, { 0x49C606, 0x473619 });
@@ -299,6 +331,10 @@ void InitMiscHookScripts() {
LoadHookScript("hs_cartravel", HOOK_CARTRAVEL); LoadHookScript("hs_cartravel", HOOK_CARTRAVEL);
MakeJump(0x4BFEF1, CarTravelHack); MakeJump(0x4BFEF1, CarTravelHack);
BlockCall(0x4BFF6E); // vanilla wnCarUseGas(100) call BlockCall(0x4BFF6E); // vanilla wnCarUseGas(100) call
LoadHookScript("hs_setglobalvar", HOOK_SETGLOBALVAR);
HookCall(0x455A6D, SetGlobalVarHook);
} }
} }
+7 -22
View File
@@ -38,6 +38,7 @@ static std::vector<KarmaFrmSetting> karmaFrms;
static std::string karmaGainMsg; static std::string karmaGainMsg;
static std::string karmaLossMsg; static std::string karmaLossMsg;
bool displayKarmaChanges;
static DWORD _stdcall DrawCardHook2() { static DWORD _stdcall DrawCardHook2() {
int reputation = fo::var::game_global_vars[fo::GVAR_PLAYER_REPUTATION]; int reputation = fo::var::game_global_vars[fo::GVAR_PLAYER_REPUTATION];
@@ -65,38 +66,22 @@ skip:
} }
} }
static void _stdcall SetKarma(int value) { void Karma::DisplayKarma(int value) {
int old = fo::var::game_global_vars[fo::GVAR_PLAYER_REPUTATION];
old = value - old;
char buf[128]; char buf[128];
if (old == 0) return; if (value > 0) {
if (old > 0) { sprintf_s(buf, karmaGainMsg.c_str(), value);
sprintf_s(buf, karmaGainMsg.c_str(), old);
} else { } else {
sprintf_s(buf, karmaLossMsg.c_str(), -old); sprintf_s(buf, karmaLossMsg.c_str(), -value);
} }
fo::func::display_print(buf); fo::func::display_print(buf);
} }
static void __declspec(naked) SetGlobalVarWrapper() {
__asm {
test eax, eax;
jnz end;
pushad;
push edx;
call SetKarma;
popad;
end:
jmp fo::funcoffs::game_set_global_var_;
}
}
void ApplyDisplayKarmaChangesPatch() { void ApplyDisplayKarmaChangesPatch() {
if (GetConfigInt("Misc", "DisplayKarmaChanges", 0)) { displayKarmaChanges = GetConfigInt("Misc", "DisplayKarmaChanges", 0) != 0;
if (displayKarmaChanges) {
dlog("Applying display karma changes patch.", DL_INIT); dlog("Applying display karma changes patch.", DL_INIT);
karmaGainMsg = Translate("sfall", "KarmaGain", "You gained %d karma."); karmaGainMsg = Translate("sfall", "KarmaGain", "You gained %d karma.");
karmaLossMsg = Translate("sfall", "KarmaLoss", "You lost %d karma."); karmaLossMsg = Translate("sfall", "KarmaLoss", "You lost %d karma.");
HookCall(0x455A6D, SetGlobalVarWrapper);
dlogr(" Done", DL_INIT); dlogr(" Done", DL_INIT);
} }
} }
+3
View File
@@ -26,7 +26,10 @@ namespace sfall
class Karma : public Module { class Karma : public Module {
public: public:
const char* name() { return "Karma"; } const char* name() { return "Karma"; }
static void DisplayKarma(int value);
void init(); void init();
}; };
extern bool displayKarmaChanges;
} }