Skip to content

Commit

Permalink
Refactored ASM code in Sound.cpp (from Mr.Stalin)
Browse files Browse the repository at this point in the history
Minor fix for "path_find_to" script function.
Implemented WRAP_WATCOM_FCALL# wrappers from Mr.Stalin.
Changed some code in Objects script handler for new wrapper functions.
Moved obj_under_cursor to Objects script handler.
Reformatted code in Karma.cpp/Worldmap.cpp.
Minor edits to documents.
  • Loading branch information
NovaRain committed Jan 8, 2019
1 parent c1107e1 commit 11ae4f5
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 166 deletions.
2 changes: 1 addition & 1 deletion artifacts/config_files/elevators.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
;Controls the elevators
;Image must match up with the image of an existing elevator (can be overrided in sfall 4.1.4 or newer)
;Image must match up with the image of an existing elevator (can be overrided in sfall 4.1.4/3.8.14 or newer)
;Make sure you specify the correct number of exit targets
;The maximum number of elevators is currently capped at 50
;Use the line number (0-indexed) of the corresponding FRM in intrface.lst to set the appearance of the elevator
Expand Down
2 changes: 1 addition & 1 deletion artifacts/scripting/arrays.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Example:
- if size is >= 0, creates list with given size
- if size == -1, creates map (associative array)
- if size == -1 and flags == 2, creates a "lookup" map in which the values of existing keys are read-only and can't be updated.
This type of array allows you to store a zero (0) value for a key
This type of array allows you to store a zero (0) key value
- NOTE: in earlier versions (up to 4.1.3/3.8.13) the second argument is not used, just use 0
- returns arrayID (valid until array is deleted)

Expand Down
87 changes: 74 additions & 13 deletions sfall/FalloutEngine/Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace func
WRAP_WATCOM_CALL2(offs, arg1, arg2)

#define WRAP_WATCOM_CALL4(offs, arg1, arg2, arg3, arg4) \
__asm mov ecx, arg4 \
__asm mov ecx, arg4 \
WRAP_WATCOM_CALL3(offs, arg1, arg2, arg3)

#define WRAP_WATCOM_CALL5(offs, arg1, arg2, arg3, arg4, arg5) \
Expand All @@ -59,19 +59,41 @@ namespace func
__asm push arg7 \
WRAP_WATCOM_CALL6(offs, arg1, arg2, arg3, arg4, arg5, arg6)

// defines wrappers for __fastcall
#define WRAP_WATCOM_FCALL1(offs, arg1) \
__asm mov eax, ecx \
WRAP_WATCOM_CALL0(offs)

#define WRAP_WATCOM_FCALL2(offs, arg1, arg2) \
WRAP_WATCOM_FCALL1(offs, arg1)

#define WRAP_WATCOM_FCALL3(offs, arg1, arg2, arg3) \
__asm mov ebx, arg3 \
WRAP_WATCOM_FCALL1(offs, arg1)

#define WRAP_WATCOM_FCALL4(offs, arg1, arg2, arg3, arg4) \
__asm mov eax, ecx \
__asm mov ebx, arg3 \
__asm mov ecx, arg4 \
WRAP_WATCOM_CALL0(offs)

#define WRAP_WATCOM_FCALL5(offs, arg1, arg2, arg3, arg4, arg5) \
__asm push arg5 \
WRAP_WATCOM_FCALL4(offs, arg1, arg2, arg3, arg4)

#define WRAP_WATCOM_FCALL6(offs, arg1, arg2, arg3, arg4, arg5, arg6) \
__asm push arg6 \
WRAP_WATCOM_FCALL5(offs, arg1, arg2, arg3, arg4, arg5)

#define WRAP_WATCOM_FCALL7(offs, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
__asm push arg7 \
WRAP_WATCOM_FCALL6(offs, arg1, arg2, arg3, arg4, arg5, arg6)


bool __stdcall art_exists(long artFid) {
WRAP_WATCOM_CALL1(art_exists_, artFid)
}

long __fastcall _word_wrap(const char* text, int maxWidth, DWORD* buf, BYTE* count) {
__asm {
mov eax, ecx;
mov ecx, count;
mov ebx, buf;
call fo::funcoffs::_word_wrap_;
}
}

// Returns the name of the critter
const char* __stdcall critter_name(GameObject* critter) {
WRAP_WATCOM_CALL1(critter_name_, critter)
Expand Down Expand Up @@ -284,6 +306,14 @@ long __stdcall message_exit(MessageList *msgList) {
WRAP_WATCOM_CALL1(message_exit_, msgList)
}

GameObject* __fastcall obj_blocking_at_wrapper(GameObject* obj, DWORD tile, DWORD elevation, void* func) {
__asm {
mov eax, ecx;
mov ebx, elevation;
call func;
}
}

GameObject* __stdcall obj_find_first_at_tile(long elevation, long tileNum) {
WRAP_WATCOM_CALL2(obj_find_first_at_tile_, elevation, tileNum)
}
Expand Down Expand Up @@ -424,9 +454,40 @@ void __fastcall DrawWinLine(int winRef, DWORD startXPos, DWORD endXPos, DWORD st
WRAP_WATCOM_CALL6(name##_, arg1, arg2, arg3, arg4, arg5, arg6) \
}

#define WRAP_WATCOM_FUNC7(retType, name, arg1t, arg1, arg2t, arg2, arg3t, arg3, arg4t, arg4, arg5t, arg5, arg6t, arg6, arg7t, arg7) \
retType __stdcall name(arg1t arg1, arg2t arg2, arg3t arg3, arg4t arg4, arg5t arg5, arg6t arg6, arg7t arg7) { \
WRAP_WATCOM_CALL7(name##_, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \

#define WRAP_WATCOM_FFUNC1(retType, name, arg1t, arg1) \
retType __fastcall name(arg1t arg1) { \
WRAP_WATCOM_FCALL1(name##_, arg1) \
}

#define WRAP_WATCOM_FFUNC2(retType, name, arg1t, arg1, arg2t, arg2) \
retType __fastcall name(arg1t arg1, arg2t arg2) { \
WRAP_WATCOM_FCALL2(name##_, arg1, arg2) \
}

#define WRAP_WATCOM_FFUNC3(retType, name, arg1t, arg1, arg2t, arg2, arg3t, arg3) \
retType __fastcall name(arg1t arg1, arg2t arg2, arg3t arg3) { \
WRAP_WATCOM_FCALL3(name##_, arg1, arg2, arg3) \
}

#define WRAP_WATCOM_FFUNC4(retType, name, arg1t, arg1, arg2t, arg2, arg3t, arg3, arg4t, arg4) \
retType __fastcall name(arg1t arg1, arg2t arg2, arg3t arg3, arg4t arg4) { \
WRAP_WATCOM_FCALL4(name##_, arg1, arg2, arg3, arg4) \
}

#define WRAP_WATCOM_FFUNC5(retType, name, arg1t, arg1, arg2t, arg2, arg3t, arg3, arg4t, arg4, arg5t, arg5) \
retType __fastcall name(arg1t arg1, arg2t arg2, arg3t arg3, arg4t arg4, arg5t arg5) { \
WRAP_WATCOM_FCALL5(name##_, arg1, arg2, arg3, arg4, arg5) \
}

#define WRAP_WATCOM_FFUNC6(retType, name, arg1t, arg1, arg2t, arg2, arg3t, arg3, arg4t, arg4, arg5t, arg5, arg6t, arg6) \
retType __fastcall name(arg1t arg1, arg2t arg2, arg3t arg3, arg4t arg4, arg5t arg5, arg6t arg6) { \
WRAP_WATCOM_FCALL6(name##_, arg1, arg2, arg3, arg4, arg5, arg6) \
}

#define WRAP_WATCOM_FFUNC7(retType, name, arg1t, arg1, arg2t, arg2, arg3t, arg3, arg4t, arg4, arg5t, arg5, arg6t, arg6, arg7t, arg7) \
retType __fastcall name(arg1t arg1, arg2t arg2, arg3t arg3, arg4t arg4, arg5t arg5, arg6t arg6, arg7t arg7) { \
WRAP_WATCOM_FCALL7(name##_, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
}

#include "Functions_def.h"
Expand Down
37 changes: 32 additions & 5 deletions sfall/FalloutEngine/Functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ namespace func

bool __stdcall art_exists(long artFid);

long __fastcall _word_wrap(const char* text, int maxWidth, DWORD* buf, BYTE* count);

// Returns the name of the critter
const char* __stdcall critter_name(GameObject* critter);

Expand Down Expand Up @@ -162,6 +160,8 @@ long __stdcall message_load(MessageList *msgList, const char *msgFilePath);
// destroys message list
long __stdcall message_exit(MessageList *msgList);

GameObject* __fastcall obj_blocking_at_wrapper(GameObject* obj, DWORD tile, DWORD elevation, void* func);

GameObject* __stdcall obj_find_first_at_tile(long elevation, long tileNum);

GameObject* __stdcall obj_find_next_at_tile();
Expand Down Expand Up @@ -221,8 +221,27 @@ void __stdcall DialogOut(const char* text);
#define WRAP_WATCOM_FUNC6(retType, name, arg1t, arg1, arg2t, arg2, arg3t, arg3, arg4t, arg4, arg5t, arg5, arg6t, arg6) \
retType __stdcall name(arg1t arg1, arg2t arg2, arg3t arg3, arg4t arg4, arg5t arg5, arg6t arg6);

#define WRAP_WATCOM_FUNC7(retType, name, arg1t, arg1, arg2t, arg2, arg3t, arg3, arg4t, arg4, arg5t, arg5, arg6t, arg6, arg7t, arg7) \
retType __stdcall name(arg1t arg1, arg2t arg2, arg3t arg3, arg4t arg4, arg5t arg5, arg6t arg6, arg7t arg7);

#define WRAP_WATCOM_FFUNC1(retType, name, arg1t, arg1) \
retType __fastcall name(arg1t arg1);

#define WRAP_WATCOM_FFUNC2(retType, name, arg1t, arg1, arg2t, arg2) \
retType __fastcall name(arg1t arg1, arg2t arg2);

#define WRAP_WATCOM_FFUNC3(retType, name, arg1t, arg1, arg2t, arg2, arg3t, arg3) \
retType __fastcall name(arg1t arg1, arg2t arg2, arg3t arg3);

#define WRAP_WATCOM_FFUNC4(retType, name, arg1t, arg1, arg2t, arg2, arg3t, arg3, arg4t, arg4) \
retType __fastcall name(arg1t arg1, arg2t arg2, arg3t arg3, arg4t arg4);

#define WRAP_WATCOM_FFUNC5(retType, name, arg1t, arg1, arg2t, arg2, arg3t, arg3, arg4t, arg4, arg5t, arg5) \
retType __fastcall name(arg1t arg1, arg2t arg2, arg3t arg3, arg4t arg4, arg5t arg5);

#define WRAP_WATCOM_FFUNC6(retType, name, arg1t, arg1, arg2t, arg2, arg3t, arg3, arg4t, arg4, arg5t, arg5, arg6t, arg6) \
retType __fastcall name(arg1t arg1, arg2t arg2, arg3t arg3, arg4t arg4, arg5t arg5, arg6t arg6);

#define WRAP_WATCOM_FFUNC7(retType, name, arg1t, arg1, arg2t, arg2, arg3t, arg3, arg4t, arg4, arg5t, arg5, arg6t, arg6, arg7t, arg7) \
retType __fastcall name(arg1t arg1, arg2t arg2, arg3t arg3, arg4t arg4, arg5t arg5, arg6t arg6, arg7t arg7);

#include "Functions_def.h"

Expand All @@ -233,7 +252,15 @@ void __stdcall DialogOut(const char* text);
#undef WRAP_WATCOM_FUNC4
#undef WRAP_WATCOM_FUNC5
#undef WRAP_WATCOM_FUNC6
#undef WRAP_WATCOM_FUNC7
//#undef WRAP_WATCOM_FUNC7

#undef WRAP_WATCOM_FFUNC1
#undef WRAP_WATCOM_FFUNC2
#undef WRAP_WATCOM_FFUNC3
#undef WRAP_WATCOM_FFUNC4
#undef WRAP_WATCOM_FFUNC5
#undef WRAP_WATCOM_FFUNC6
#undef WRAP_WATCOM_FFUNC7

}
}
10 changes: 8 additions & 2 deletions sfall/FalloutEngine/Functions_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
NOTES: be careful not to use reserved words, including ASM instructions (push, pop, mov, div, etc.)
*/

// For functions that have 3 or more arguments, it is preferable to use the fastcall calling convention
// because the compiler builds the better/optimized code when calling the engine functions
WRAP_WATCOM_FFUNC4(long, _word_wrap, const char*, text, int, maxWidth, DWORD*, buf, BYTE*, count)
WRAP_WATCOM_FFUNC7(long, createWindow, const char*, winName, long, x, long, y, long, width, long, height, long, bgColorIndex, long, flags)
WRAP_WATCOM_FFUNC7(void, make_straight_path_func, fo::GameObject*, objFrom, DWORD, tileFrom, DWORD, tileTo, void*, rotationPtr, DWORD*, result, long, flags, void*, func)
WRAP_WATCOM_FFUNC3(long, object_under_mouse, long, crSwitch, long, inclDude, long, elevation)

// stdcall
WRAP_WATCOM_FUNC1(AIcap*, ai_cap, GameObject*, critter)
WRAP_WATCOM_FUNC1(Program*, allocateProgram, const char*, filePath)
WRAP_WATCOM_FUNC0(void, art_flush)
Expand All @@ -26,7 +34,6 @@ WRAP_WATCOM_FUNC4(BYTE*, art_ptr_lock_data, long, frmId, long, frameNum, long, r
WRAP_WATCOM_FUNC4(BYTE*, art_lock, long, frmId, DWORD*, lockPtr, long*, widthOut, long*, heightOut)
WRAP_WATCOM_FUNC1(long, art_ptr_unlock, DWORD, lockId)
WRAP_WATCOM_FUNC2(long, barter_compute_value, GameObject*, source, GameObject*, target)
WRAP_WATCOM_FUNC7(long, createWindow, const char*, winName, long, x, long, y, long, width, long, height, long, bgColorIndex, long, flags)
WRAP_WATCOM_FUNC1(void*, dbase_open, const char*, fileName)
WRAP_WATCOM_FUNC1(void, dbase_close, void*, dbPtr)
WRAP_WATCOM_FUNC3(long, db_freadShortCount, DbFile*, file, WORD*, dest, long, count)
Expand Down Expand Up @@ -79,7 +86,6 @@ WRAP_WATCOM_FUNC2(long, obj_pid_new, fo::GameObject*, object, long, pid)
// checks/unjams jammed locks
WRAP_WATCOM_FUNC1(long, obj_lock_is_jammed, GameObject*, object)
WRAP_WATCOM_FUNC1(void, obj_unjam_lock, GameObject*, object)
WRAP_WATCOM_FUNC3(long, object_under_mouse, long, crSwitch, long, inclDude, long, elevation)
WRAP_WATCOM_FUNC6(long, pick_death, GameObject*, attacker, GameObject*, target, GameObject*, weapon, long, amount, long, anim, long, hitFromBack)
WRAP_WATCOM_FUNC0(void, proto_dude_update_gender)
WRAP_WATCOM_FUNC2(long, queue_find_first, GameObject*, object, long, qType)
Expand Down
22 changes: 11 additions & 11 deletions sfall/Modules/Karma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static std::string karmaGainMsg;
static std::string karmaLossMsg;
bool displayKarmaChanges;

static DWORD _stdcall DrawCardHook2() {
static DWORD _stdcall DrawCard() {
int reputation = fo::var::game_global_vars[fo::GVAR_PLAYER_REPUTATION];
for (auto& info : karmaFrms) {
if (reputation < info.points) {
Expand All @@ -51,19 +51,19 @@ static DWORD _stdcall DrawCardHook2() {
return karmaFrms.end()->frm;
}

static void __declspec(naked) DrawCardHook() {
static void __declspec(naked) DrawInfoWin_hook() {
__asm {
cmp ds : [FO_VAR_info_line], 10;
jne skip;
cmp eax, 0x30;
jne skip;
cmp ds:[FO_VAR_info_line], 10;
jne skip;
cmp eax, 0x30;
jne skip;
push ecx;
push edx;
call DrawCardHook2;
pop edx;
pop ecx;
call DrawCard;
pop edx;
pop ecx;
skip:
jmp fo::funcoffs::DrawCard_;
jmp fo::funcoffs::DrawCard_;
}
}

Expand Down Expand Up @@ -101,7 +101,7 @@ void ApplyKarmaFRMsPatch() {
? atoi(karmaPointsList[i].c_str())
: INT_MAX;
}
HookCall(0x4367A9, DrawCardHook);
HookCall(0x4367A9, DrawInfoWin_hook);

dlogr(" Done", DL_INIT);
}
Expand Down
2 changes: 1 addition & 1 deletion sfall/Modules/ScriptExtender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ static void RunScript(GlobalScript* script) {
*/
static void ResetStateAfterFrame() {
if (tempArrays.size()) {
for (std::set<DWORD>::iterator it = tempArrays.begin(); it != tempArrays.end(); ++it)
for (std::set<DWORD>::iterator it = tempArrays.begin(); it != tempArrays.end(); ++it)
FreeArray(*it);
tempArrays.clear();
}
Expand Down
2 changes: 2 additions & 0 deletions sfall/Modules/Scripting/Arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ static void ListSort(std::vector<T> &arr, int type) {

static void MapSort(sArrayVar& arr, int type) {
std::vector<std::pair<sArrayElement, sArrayElement>> map;
map.reserve(arr.val.size());

bool sortByValue = false;
if (type < ARRAY_ACTION_SHUFFLE) {
type += 4;
Expand Down
2 changes: 1 addition & 1 deletion sfall/Modules/Scripting/Handlers/Metarule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static const SfallMetarule metarules[] = {
{"item_weight", sf_item_weight, 1, 1, {ARG_OBJECT}},
{"lock_is_jammed", sf_lock_is_jammed, 1, 1, {ARG_OBJECT}},
{"loot_obj", sf_get_loot_object, 0, 0},
{"obj_under_cursor", sf_obj_under_cursor, 2, 2, {ARG_INT, ARG_INT}},
{"obj_under_cursor", sf_get_obj_under_cursor, 2, 2, {ARG_INT, ARG_INT}},
{"outlined_object", sf_outlined_object, 0, 0},
{"real_dude_obj", sf_real_dude_obj, 0, 0},
{"set_can_rest_on_map", sf_set_rest_on_map, 3, 3, {ARG_INT, ARG_INT, ARG_INT}},
Expand Down
4 changes: 0 additions & 4 deletions sfall/Modules/Scripting/Handlers/Misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1722,9 +1722,5 @@ void sf_get_ini_section(OpcodeContext& ctx) {
ctx.setReturn(arrayId);
}

void sf_obj_under_cursor(OpcodeContext& ctx) {
ctx.setReturn(fo::func::object_under_mouse(ctx.arg(0).asBool() ? 1 : -1, ctx.arg(1).rawValue(), fo::var::map_elevation));
}

}
}
2 changes: 0 additions & 2 deletions sfall/Modules/Scripting/Handlers/Misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,5 @@ void sf_get_ini_sections(OpcodeContext&);

void sf_get_ini_section(OpcodeContext&);

void sf_obj_under_cursor(OpcodeContext&);

}
}
Loading

0 comments on commit 11ae4f5

Please sign in to comment.