Add some metarules

This commit is contained in:
Alexander Batalov 2023-09-03 16:26:52 +03:00
parent b706756ec9
commit b40bfcc64a
14 changed files with 483 additions and 2 deletions

View File

@ -275,6 +275,8 @@ target_sources(${EXECUTABLE_NAME} PUBLIC
"src/sfall_kb_helpers.h"
"src/sfall_lists.cc"
"src/sfall_lists.h"
"src/sfall_metarules.cc"
"src/sfall_metarules.h"
"src/sfall_opcodes.cc"
"src/sfall_opcodes.h"
"src/sfall_arrays.cc"

View File

@ -6825,4 +6825,9 @@ void combat_reset_hit_location_penalty()
}
}
Attack* combat_get_data()
{
return &_main_ctd;
}
} // namespace fallout

View File

@ -74,6 +74,7 @@ bool damageModGetDisplayBonusDamage();
int combat_get_hit_location_penalty(int hit_location);
void combat_set_hit_location_penalty(int hit_location, int penalty);
void combat_reset_hit_location_penalty();
Attack* combat_get_data();
static inline bool isInCombat()
{

View File

@ -2464,4 +2464,9 @@ void gameMouseRefreshImmediately()
renderPresent();
}
Object* gmouse_get_outlined_object()
{
return gGameMouseHighlightedItem;
}
} // namespace fallout

View File

@ -103,6 +103,7 @@ void gameMouseLoadItemHighlight();
void _gmouse_remove_item_outline(Object* object);
void gameMouseRefreshImmediately();
Object* gmouse_get_outlined_object();
} // namespace fallout

View File

@ -5981,4 +5981,9 @@ int _inven_set_timer(Object* a1)
return seconds;
}
Object* inven_get_current_target_obj()
{
return _target_stack[_target_curr_stack];
}
} // namespace fallout

View File

@ -27,6 +27,7 @@ int inventoryOpenLooting(Object* a1, Object* a2);
int inventoryOpenStealing(Object* a1, Object* a2);
void inventoryOpenTrade(int win, Object* a2, Object* a3, Object* a4, int a5);
int _inven_set_timer(Object* a1);
Object* inven_get_current_target_obj();
} // namespace fallout

View File

@ -145,4 +145,53 @@ bool sfall_ini_get_string(const char* triplet, char* value, size_t size)
return true;
}
bool sfall_ini_set_int(const char* triplet, int value)
{
char stringValue[20];
compat_itoa(value, stringValue, 10);
return sfall_ini_set_string(triplet, stringValue);
}
bool sfall_ini_set_string(const char* triplet, const char* value)
{
char fileName[kFileNameMaxSize];
char section[kSectionMaxSize];
const char* key = parse_ini_triplet(triplet, fileName, section);
if (key == nullptr) {
return false;
}
Config config;
if (!configInit(&config)) {
return false;
}
char path[COMPAT_MAX_PATH];
bool loaded = false;
if (basePath[0] != '\0' && !is_system_file_name(fileName)) {
// Attempt to load requested file in base directory.
snprintf(path, sizeof(path), "%s\\%s", basePath, fileName);
loaded = configRead(&config, path, false);
}
if (!loaded) {
// There was no base path set, requested file is a system config, or
// non-system config file was not found the base path - attempt to load
// from current working directory.
strcpy(path, fileName);
loaded = configRead(&config, path, false);
}
configSetString(&config, section, key, value);
bool saved = configWrite(&config, path, false);
configFree(&config);
return saved;
}
} // namespace fallout

View File

@ -14,6 +14,12 @@ bool sfall_ini_get_int(const char* triplet, int* value);
/// Reads string key identified by "fileName|section|key" triplet into `value`.
bool sfall_ini_get_string(const char* triplet, char* value, size_t size);
/// Writes integer key identified by "fileName|section|key" triplet.
bool sfall_ini_set_int(const char* triplet, int value);
/// Writes string key identified by "fileName|section|key" triplet.
bool sfall_ini_set_string(const char* triplet, const char* value);
} // namespace fallout
#endif /* FALLOUT_SFALL_INI_H_ */

289
src/sfall_metarules.cc Normal file
View File

@ -0,0 +1,289 @@
#include "sfall_metarules.h"
#include "combat.h"
#include "debug.h"
#include "game.h"
#include "game_dialog.h"
#include "game_mouse.h"
#include "interface.h"
#include "inventory.h"
#include "object.h"
#include "sfall_ini.h"
#include "text_font.h"
#include "tile.h"
#include "window.h"
#include "worldmap.h"
namespace fallout {
typedef void(MetaruleHandler)(Program* program, int args);
// Simplified cousin of `SfallMetarule` from Sfall.
typedef struct MetaruleInfo {
const char* name;
MetaruleHandler* handler;
int minArgs;
int maxArgs;
} MetaruleInfo;
static void mf_car_gas_amount(Program* program, int args);
static void mf_combat_data(Program* program, int args);
static void mf_critter_inven_obj2(Program* program, int args);
static void mf_dialog_obj(Program* program, int args);
static void mf_get_cursor_mode(Program* program, int args);
static void mf_get_flags(Program* program, int args);
static void mf_get_object_data(Program* program, int args);
static void mf_get_text_width(Program* program, int args);
static void mf_intface_redraw(Program* program, int args);
static void mf_loot_obj(Program* program, int args);
static void mf_metarule_exist(Program* program, int args);
static void mf_outlined_object(Program* program, int args);
static void mf_set_cursor_mode(Program* program, int args);
static void mf_set_flags(Program* program, int args);
static void mf_set_ini_setting(Program* program, int args);
static void mf_set_outline(Program* program, int args);
static void mf_show_window(Program* program, int args);
static void mf_tile_refresh_display(Program* program, int args);
constexpr MetaruleInfo kMetarules[] = {
{ "car_gas_amount", mf_car_gas_amount, 0, 0 },
{ "combat_data", mf_combat_data, 0, 0 },
{ "critter_inven_obj2", mf_critter_inven_obj2, 2, 2 },
{ "dialog_obj", mf_dialog_obj, 0, 0 },
{ "get_cursor_mode", mf_get_cursor_mode, 0, 0 },
{ "get_flags", mf_get_flags, 1, 1 },
{ "get_object_data", mf_get_object_data, 2, 2 },
{ "get_text_width", mf_get_text_width, 1, 1 },
{ "intface_redraw", mf_intface_redraw, 0, 1 },
{ "loot_obj", mf_loot_obj, 0, 0 },
{ "metarule_exist", mf_metarule_exist, 1, 1 },
{ "outlined_object", mf_outlined_object, 0, 0 },
{ "set_cursor_mode", mf_set_cursor_mode, 1, 1 },
{ "set_flags", mf_set_flags, 2, 2 },
{ "set_ini_setting", mf_set_ini_setting, 2, 2 },
{ "set_outline", mf_set_outline, 2, 2 },
{ "show_window", mf_show_window, 0, 1 },
{ "tile_refresh_display", mf_tile_refresh_display, 0, 0 },
};
constexpr int kMetarulesMax = sizeof(kMetarules) / sizeof(kMetarules[0]);
void mf_car_gas_amount(Program* program, int args)
{
programStackPushInteger(program, wmCarGasAmount());
}
void mf_combat_data(Program* program, int args)
{
if (isInCombat()) {
programStackPushPointer(program, combat_get_data());
} else {
programStackPushPointer(program, nullptr);
}
}
void mf_critter_inven_obj2(Program* program, int args)
{
int slot = programStackPopInteger(program);
Object* obj = static_cast<Object*>(programStackPopPointer(program));
switch (slot) {
case 0:
programStackPushPointer(program, critterGetArmor(obj));
break;
case 1:
programStackPushPointer(program, critterGetItem2(obj));
break;
case 2:
programStackPushPointer(program, critterGetItem1(obj));
break;
case -2:
programStackPushInteger(program, obj->data.inventory.length);
break;
default:
programFatalError("mf_critter_inven_obj2: invalid type");
}
}
void mf_dialog_obj(Program* program, int args)
{
if (GameMode::isInGameMode(GameMode::kDialog)) {
programStackPushPointer(program, gGameDialogSpeaker);
} else {
programStackPushPointer(program, nullptr);
}
}
void mf_get_cursor_mode(Program* program, int args)
{
programStackPushInteger(program, gameMouseGetMode());
}
void mf_get_flags(Program* program, int args)
{
Object* object = static_cast<Object*>(programStackPopPointer(program));
programStackPushInteger(program, object->flags);
}
void mf_get_object_data(Program* program, int args)
{
size_t offset = static_cast<size_t>(programStackPopInteger(program));
void* ptr = programStackPopPointer(program);
if (offset % 4 != 0) {
programFatalError("mf_get_object_data: bad offset %d", offset);
}
int value = *reinterpret_cast<int*>(reinterpret_cast<unsigned char*>(ptr) + offset);
programStackPushInteger(program, value);
}
void mf_get_text_width(Program* program, int args)
{
const char* string = programStackPopString(program);
programStackPushInteger(program, fontGetStringWidth(string));
}
void mf_intface_redraw(Program* program, int args)
{
if (args == 0) {
interfaceBarRefresh();
} else {
// TODO: Incomplete.
programFatalError("mf_intface_redraw: not implemented");
}
programStackPushInteger(program, -1);
}
void mf_loot_obj(Program* program, int args)
{
if (GameMode::isInGameMode(GameMode::kInventory)) {
programStackPushPointer(program, inven_get_current_target_obj());
} else {
programStackPushPointer(program, nullptr);
}
}
void mf_metarule_exist(Program* program, int args)
{
const char* metarule = programStackPopString(program);
for (int index = 0; index < kMetarulesMax; index++) {
if (strcmp(kMetarules[index].name, metarule) == 0) {
programStackPushInteger(program, 1);
return;
}
}
programStackPushInteger(program, 0);
}
void mf_outlined_object(Program* program, int args)
{
programStackPushPointer(program, gmouse_get_outlined_object());
}
void mf_set_cursor_mode(Program* program, int args)
{
int mode = programStackPopInteger(program);
gameMouseSetMode(mode);
programStackPushInteger(program, -1);
}
void mf_set_flags(Program* program, int args)
{
int flags = programStackPopInteger(program);
Object* object = static_cast<Object*>(programStackPopPointer(program));
object->flags = flags;
programStackPushInteger(program, -1);
}
void mf_set_ini_setting(Program* program, int args)
{
ProgramValue value = programStackPopValue(program);
const char* triplet = programStackPopString(program);
if (value.isString()) {
const char* stringValue = programGetString(program, value.opcode, value.integerValue);
if (!sfall_ini_set_string(triplet, stringValue)) {
debugPrint("set_ini_setting: unable to write '%s' to '%s'",
stringValue,
triplet);
}
} else {
int integerValue = value.asInt();
if (!sfall_ini_set_int(triplet, integerValue)) {
debugPrint("set_ini_setting: unable to write '%d' to '%s'",
integerValue,
triplet);
}
}
programStackPushInteger(program, -1);
}
void mf_set_outline(Program* program, int args)
{
int outline = programStackPopInteger(program);
Object* object = static_cast<Object*>(programStackPopPointer(program));
object->outline = outline;
programStackPushInteger(program, -1);
}
void mf_show_window(Program* program, int args)
{
if (args == 0) {
_windowShow();
} else if (args == 1) {
const char* windowName = programStackPopString(program);
if (!_windowShowNamed(windowName)) {
debugPrint("show_window: window '%s' is not found", windowName);
}
}
programStackPushInteger(program, -1);
}
void mf_tile_refresh_display(Program* program, int args)
{
tileWindowRefresh();
programStackPushInteger(program, -1);
}
void sfall_metarule(Program* program, int args)
{
static ProgramValue values[6];
for (int index = 0; index < args; index++) {
values[index] = programStackPopValue(program);
}
const char* metarule = programStackPopString(program);
for (int index = 0; index < args; index++) {
programStackPushValue(program, values[index]);
}
int metaruleIndex = -1;
for (int index = 0; index < kMetarulesMax; index++) {
if (strcmp(kMetarules[index].name, metarule) == 0) {
metaruleIndex = index;
break;
}
}
if (metaruleIndex == -1) {
programFatalError("op_sfall_func: '%s' is not implemented", metarule);
}
if (args < kMetarules[metaruleIndex].minArgs || args > kMetarules[metaruleIndex].maxArgs) {
programFatalError("op_sfall_func: '%s': invalid number of args", metarule);
}
kMetarules[metaruleIndex].handler(program, args);
}
} // namespace fallout

12
src/sfall_metarules.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef FALLOUT_SFALL_METARULES_H_
#define FALLOUT_SFALL_METARULES_H_
#include "interpreter.h"
namespace fallout {
void sfall_metarule(Program* program, int args);
} // namespace fallout
#endif /* FALLOUT_SFALL_METARULES_H_ */

View File

@ -26,6 +26,7 @@
#include "sfall_ini.h"
#include "sfall_kb_helpers.h"
#include "sfall_lists.h"
#include "sfall_metarules.h"
#include "stat.h"
#include "svga.h"
#include "tile.h"
@ -889,6 +890,48 @@ static void opArtExists(Program* program)
programStackPushInteger(program, artExists(fid));
}
// sfall_func0
static void op_sfall_func0(Program* program)
{
sfall_metarule(program, 0);
}
// sfall_func1
static void op_sfall_func1(Program* program)
{
sfall_metarule(program, 1);
}
// sfall_func2
static void op_sfall_func2(Program* program)
{
sfall_metarule(program, 2);
}
// sfall_func3
static void op_sfall_func3(Program* program)
{
sfall_metarule(program, 3);
}
// sfall_func4
static void op_sfall_func4(Program* program)
{
sfall_metarule(program, 4);
}
// sfall_func5
static void op_sfall_func5(Program* program)
{
sfall_metarule(program, 5);
}
// sfall_func6
static void op_sfall_func6(Program* program)
{
sfall_metarule(program, 6);
}
// div (/)
static void op_div(Program* program)
{
@ -986,6 +1029,13 @@ void sfallOpcodesInit()
interpreterRegisterOpcode(0x826F, op_obj_blocking_at);
interpreterRegisterOpcode(0x8271, opPartyMemberList);
interpreterRegisterOpcode(0x8274, opArtExists);
interpreterRegisterOpcode(0x8276, op_sfall_func0);
interpreterRegisterOpcode(0x8277, op_sfall_func1);
interpreterRegisterOpcode(0x8278, op_sfall_func2);
interpreterRegisterOpcode(0x8279, op_sfall_func3);
interpreterRegisterOpcode(0x827A, op_sfall_func4);
interpreterRegisterOpcode(0x827B, op_sfall_func5);
interpreterRegisterOpcode(0x827C, op_sfall_func6);
interpreterRegisterOpcode(0x827F, op_div);
}

View File

@ -70,6 +70,8 @@ typedef struct ManagedWindow {
typedef int (*INITVIDEOFN)();
static void redrawButton(ManagedButton* managedButton);
// 0x51DCAC
static int _holdTime = 250;
@ -663,6 +665,38 @@ void _setButtonGFX(int width, int height, unsigned char* normal, unsigned char*
}
}
// 0x4B75F4
static void redrawButton(ManagedButton* managedButton)
{
_win_register_button_image(managedButton->btn, managedButton->normal, managedButton->pressed, managedButton->hover, false);
}
// 0x4B7610
bool _windowHide()
{
ManagedWindow* managedWindow = &(gManagedWindows[gCurrentManagedWindowIndex]);
if (managedWindow->window == -1) {
return false;
}
windowHide(managedWindow->window);
return true;
}
// 0x4B7648
bool _windowShow()
{
ManagedWindow* managedWindow = &(gManagedWindows[gCurrentManagedWindowIndex]);
if (managedWindow->window == -1) {
return false;
}
windowShow(managedWindow->window);
return true;
}
// 0x4B7734
int _windowWidth()
{
@ -1714,7 +1748,8 @@ bool _windowAddButtonGfx(const char* buttonName, char* pressedFileName, char* no
buttonSetMask(managedButton->btn, managedButton->normal);
}
_win_register_button_image(managedButton->btn, managedButton->normal, managedButton->pressed, managedButton->hover, 0);
// NOTE: Uninline.
redrawButton(managedButton);
return true;
}
@ -1952,7 +1987,8 @@ bool _windowAddButtonTextWithOffsets(const char* buttonName, const char* text, i
buttonSetMask(managedButton->btn, managedButton->normal);
}
_win_register_button_image(managedButton->btn, managedButton->normal, managedButton->pressed, managedButton->hover, 0);
// NOTE: Uninline.
redrawButton(managedButton);
return true;
}
@ -2646,4 +2682,19 @@ void _fillBuf3x3(unsigned char* src, int srcWidth, int srcHeight, unsigned char*
destWidth);
}
bool _windowShowNamed(const char* windowName)
{
for (int index = 0; index < MANAGED_WINDOW_COUNT; index++) {
ManagedWindow* managedWindow = &(gManagedWindows[index]);
if (managedWindow->window != -1) {
if (compat_stricmp(managedWindow->name, windowName) == 0) {
windowShow(managedWindow->window);
return true;
}
}
}
return false;
}
} // namespace fallout

View File

@ -63,6 +63,8 @@ void _doRightButtonPress(int btn, int keyCode);
void sub_4B704C(int btn, int mouseEvent);
void _doRightButtonRelease(int btn, int keyCode);
void _setButtonGFX(int width, int height, unsigned char* normal, unsigned char* pressed, unsigned char* a5);
bool _windowHide();
bool _windowShow();
int _windowWidth();
int _windowHeight();
bool _windowDraw();
@ -127,6 +129,8 @@ void _drawScaledBuf(unsigned char* dest, int destWidth, int destHeight, unsigned
void _alphaBltBuf(unsigned char* src, int srcWidth, int srcHeight, int srcPitch, unsigned char* alphaWindowBuffer, unsigned char* alphaBuffer, unsigned char* dest, int destPitch);
void _fillBuf3x3(unsigned char* src, int srcWidth, int srcHeight, unsigned char* dest, int destWidth, int destHeight);
bool _windowShowNamed(const char* name);
} // namespace fallout
#endif /* WINDOW_H */