Skip to content

Commit

Permalink
Merge remote-tracking branch 'alex/main' into fix_worldmap_txt_reading
Browse files Browse the repository at this point in the history
  • Loading branch information
roginvs committed Apr 24, 2023
2 parents 058e4e7 + df3ac30 commit 04cf9e1
Show file tree
Hide file tree
Showing 37 changed files with 884 additions and 412 deletions.
28 changes: 5 additions & 23 deletions src/audio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static int audioSoundDecoderReadHandler(void* data, void* buffer, unsigned int s

// AudioOpen
// 0x41A2EC
int audioOpen(const char* fname, int flags)
int audioOpen(const char* fname, int* channels, int* sampleRate)
{
char path[80];
snprintf(path, sizeof(path), "%s", fname);
Expand All @@ -70,28 +70,7 @@ int audioOpen(const char* fname, int flags)
compression = 0;
}

char mode[4];
memset(mode, 0, 4);

// NOTE: Original implementation is slightly different, it uses separate
// variable to track index where to set 't' and 'b'.
char* pm = mode;
if (flags & 1) {
*pm++ = 'w';
} else if (flags & 2) {
*pm++ = 'w';
*pm++ = '+';
} else {
*pm++ = 'r';
}

if (flags & 0x100) {
*pm++ = 't';
} else if (flags & 0x200) {
*pm++ = 'b';
}

File* stream = fileOpen(path, mode);
File* stream = fileOpen(path, "rb");
if (stream == NULL) {
debugPrint("AudioOpen: Couldn't open %s for read\n", path);
return -1;
Expand Down Expand Up @@ -121,6 +100,9 @@ int audioOpen(const char* fname, int flags)
audioFile->flags |= AUDIO_COMPRESSED;
audioFile->soundDecoder = soundDecoderInit(audioSoundDecoderReadHandler, audioFile->stream, &(audioFile->channels), &(audioFile->sampleRate), &(audioFile->fileSize));
audioFile->fileSize *= 2;

*channels = audioFile->channels;
*sampleRate = audioFile->sampleRate;
} else {
audioFile->fileSize = fileGetSize(stream);
}
Expand Down
2 changes: 1 addition & 1 deletion src/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace fallout {

typedef bool(AudioQueryCompressedFunc)(char* filePath);

int audioOpen(const char* fname, int mode);
int audioOpen(const char* fname, int* channels, int* sampleRate);
int audioClose(int handle);
int audioRead(int handle, void* buffer, unsigned int size);
long audioSeek(int handle, long offset, int origin);
Expand Down
28 changes: 5 additions & 23 deletions src/audio_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static int audioFileSoundDecoderReadHandler(void* data, void* buffer, unsigned i
}

// 0x41A88C
int audioFileOpen(const char* fname, int flags)
int audioFileOpen(const char* fname, int* channels, int* sampleRate)
{
char path[COMPAT_MAX_PATH];
strcpy(path, fname);
Expand All @@ -69,28 +69,7 @@ int audioFileOpen(const char* fname, int flags)
compression = 0;
}

char mode[4];
memset(mode, '\0', 4);

// NOTE: Original implementation is slightly different, it uses separate
// variable to track index where to set 't' and 'b'.
char* pm = mode;
if (flags & 0x01) {
*pm++ = 'w';
} else if (flags & 0x02) {
*pm++ = 'w';
*pm++ = '+';
} else {
*pm++ = 'r';
}

if (flags & 0x0100) {
*pm++ = 't';
} else if (flags & 0x0200) {
*pm++ = 'b';
}

FILE* stream = compat_fopen(path, mode);
FILE* stream = compat_fopen(path, "rb");
if (stream == NULL) {
return -1;
}
Expand Down Expand Up @@ -119,6 +98,9 @@ int audioFileOpen(const char* fname, int flags)
audioFile->flags |= AUDIO_FILE_COMPRESSED;
audioFile->soundDecoder = soundDecoderInit(audioFileSoundDecoderReadHandler, audioFile->stream, &(audioFile->channels), &(audioFile->sampleRate), &(audioFile->fileSize));
audioFile->fileSize *= 2;

*channels = audioFile->channels;
*sampleRate = audioFile->sampleRate;
} else {
audioFile->fileSize = getFileSize(stream);
}
Expand Down
2 changes: 1 addition & 1 deletion src/audio_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace fallout {

typedef bool(AudioFileQueryCompressedFunc)(char* filePath);

int audioFileOpen(const char* fname, int flags);
int audioFileOpen(const char* fname, int* channels, int* sampleRate);
int audioFileClose(int handle);
int audioFileRead(int handle, void* buf, unsigned int size);
long audioFileSeek(int handle, long offset, int origin);
Expand Down
35 changes: 31 additions & 4 deletions src/combat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static bool _combat_call_display = false;
// Accuracy modifiers for hit locations.
//
// 0x510954
static const int _hit_location_penalty[HIT_LOCATION_COUNT] = {
static int hit_location_penalty_default[HIT_LOCATION_COUNT] = {
-40,
-30,
-30,
Expand All @@ -180,6 +180,8 @@ static const int _hit_location_penalty[HIT_LOCATION_COUNT] = {
0,
};

static int hit_location_penalty[HIT_LOCATION_COUNT];

// Critical hit tables for every kill type.
//
// 0x510978
Expand Down Expand Up @@ -2029,6 +2031,7 @@ int combatInit()
burstModInit();
unarmedInit();
damageModInit();
combat_reset_hit_location_penalty();

return 0;
}
Expand Down Expand Up @@ -2058,6 +2061,7 @@ void combatReset()

// SFALL
criticalsReset();
combat_reset_hit_location_penalty();
}

// 0x420E14
Expand Down Expand Up @@ -3831,7 +3835,7 @@ static int attackCompute(Attack* attack)
roll = _compute_spray(attack, accuracy, &ammoQuantity, &v26, anim);
} else {
int chance = critterGetStat(attack->attacker, STAT_CRITICAL_CHANCE);
roll = randomRoll(accuracy, chance - _hit_location_penalty[attack->defenderHitLocation], NULL);
roll = randomRoll(accuracy, chance - hit_location_penalty[attack->defenderHitLocation], NULL);
}

if (roll == ROLL_FAILURE) {
Expand Down Expand Up @@ -4417,9 +4421,9 @@ static int attackDetermineToHit(Object* attacker, int tile, Object* defender, in
}

if (isRangedWeapon) {
accuracy += _hit_location_penalty[hitLocation];
accuracy += hit_location_penalty[hitLocation];
} else {
accuracy += _hit_location_penalty[hitLocation] / 2;
accuracy += hit_location_penalty[hitLocation] / 2;
}

if (defender != NULL && (defender->flags & OBJECT_MULTIHEX) != 0) {
Expand Down Expand Up @@ -6798,4 +6802,27 @@ static void damageModCalculateYaam(DamageCalculationContext* context)
}
}

int combat_get_hit_location_penalty(int hit_location)
{
if (hit_location >= 0 && hit_location < HIT_LOCATION_COUNT) {
return hit_location_penalty[hit_location];
} else {
return 0;
}
}

void combat_set_hit_location_penalty(int hit_location, int penalty)
{
if (hit_location >= 0 && hit_location < HIT_LOCATION_COUNT) {
hit_location_penalty[hit_location] = penalty;
}
}

void combat_reset_hit_location_penalty()
{
for (int hit_location = 0; hit_location < HIT_LOCATION_COUNT; hit_location++) {
hit_location_penalty[hit_location] = hit_location_penalty_default[hit_location];
}
}

} // namespace fallout
3 changes: 3 additions & 0 deletions src/combat.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ int unarmedGetKickHitMode(bool isSecondary);
bool unarmedIsPenetrating(int hitMode);
bool damageModGetBonusHthDamageFix();
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();

static inline bool isInCombat()
{
Expand Down
2 changes: 1 addition & 1 deletion src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ bool configRead(Config* config, const char* filePath, bool isDb)
} else {
FILE* stream = compat_fopen(filePath, "rt");
if (stream != NULL) {
while (fgets(string, sizeof(string), stream) != NULL) {
while (compat_fgets(string, sizeof(string), stream) != NULL) {
configParseLine(config, string);
}

Expand Down
2 changes: 1 addition & 1 deletion src/dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ int dictionaryLoad(FILE* stream, Dictionary* dictionary, int a3)
return -1;
}

if (fgets(entry->key, keyLength + 1, stream) == NULL) {
if (compat_fgets(entry->key, keyLength + 1, stream) == NULL) {
return -1;
}

Expand Down
8 changes: 2 additions & 6 deletions src/game_sound.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ static int _gsound_speech_volume_get_set(int volume);
static void speechPause();
static void speechResume();
static void _gsound_bkg_proc();
static int gameSoundFileOpen(const char* fname, int access);
static int gameSoundFileOpen(const char* fname, int* channels, int* sampleRate);
static long _gsound_write_();
static long gameSoundFileTellNotImplemented(int handle);
static int gameSoundFileWrite(int handle, const void* buf, unsigned int size);
Expand Down Expand Up @@ -1548,12 +1548,8 @@ void _gsound_bkg_proc()
}

// 0x451A08
int gameSoundFileOpen(const char* fname, int flags)
int gameSoundFileOpen(const char* fname, int* channels, int* sampleRate)
{
if ((flags & 2) != 0) {
return -1;
}

File* stream = fileOpen(fname, "rb");
if (stream == NULL) {
return -1;
Expand Down
33 changes: 33 additions & 0 deletions src/interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2616,4 +2616,37 @@ static void sidePanelsDraw(const char* path, int win, bool isLeading)
internal_free(image);
}

// NOTE: Follows Sfall implementation of `GetCurrentAttackMode`. It slightly
// differs from `interfaceGetCurrentHitMode` (can return one of `reload` hit
// modes, the default is `punch`).
//
// 0x45EF6C
bool interface_get_current_attack_mode(int* hit_mode)
{
if (gInterfaceBarWindow == -1) {
return false;
}

switch (gInterfaceItemStates[gInterfaceCurrentHand].action) {
case INTERFACE_ITEM_ACTION_PRIMARY_AIMING:
case INTERFACE_ITEM_ACTION_PRIMARY:
*hit_mode = gInterfaceItemStates[gInterfaceCurrentHand].primaryHitMode;
break;
case INTERFACE_ITEM_ACTION_SECONDARY_AIMING:
case INTERFACE_ITEM_ACTION_SECONDARY:
*hit_mode = gInterfaceItemStates[gInterfaceCurrentHand].secondaryHitMode;
break;
case INTERFACE_ITEM_ACTION_RELOAD:
*hit_mode = gInterfaceCurrentHand == HAND_LEFT
? HIT_MODE_LEFT_WEAPON_RELOAD
: HIT_MODE_RIGHT_WEAPON_RELOAD;
break;
default:
*hit_mode = HIT_MODE_PUNCH;
break;
}

return true;
}

} // namespace fallout
1 change: 1 addition & 0 deletions src/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ void interfaceBarEndButtonsRenderRedLights();
int indicatorBarRefresh();
bool indicatorBarShow();
bool indicatorBarHide();
bool interface_get_current_attack_mode(int* hit_mode);

unsigned char* customInterfaceBarGetBackgroundImageData();

Expand Down
25 changes: 25 additions & 0 deletions src/interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3268,4 +3268,29 @@ bool ProgramValue::isEmpty()
return true;
}

// Matches Sfall implementation.
bool ProgramValue::isInt()
{
return opcode == VALUE_TYPE_INT;
}

// Matches Sfall implementation.
bool ProgramValue::isFloat()
{
return opcode == VALUE_TYPE_FLOAT;
}

// Matches Sfall implementation.
float ProgramValue::asFloat()
{
switch (opcode) {
case VALUE_TYPE_INT:
return static_cast<float>(integerValue);
case VALUE_TYPE_FLOAT:
return floatValue;
default:
return 0.0;
}
}

} // namespace fallout
3 changes: 3 additions & 0 deletions src/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ typedef struct ProgramValue {
};

bool isEmpty();
bool isInt();
bool isFloat();
float asFloat();
} ProgramValue;

typedef std::vector<ProgramValue> ProgramStack;
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter_extra.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2987,7 +2987,7 @@ static void opGetMessageString(Program* program)
int messageListIndex = programStackPopInteger(program);

char* string;
if (messageIndex >= 1) {
if (messageIndex >= 0) {
string = _scr_get_msg_str_speech(messageListIndex, messageIndex, 1);
if (string == NULL) {
debugPrint("\nError: No message file EXISTS!: index %d, line %d", messageListIndex, messageIndex);
Expand Down
Loading

0 comments on commit 04cf9e1

Please sign in to comment.