Skip to content

Commit

Permalink
Add checks as to whether a combat will be scouted or not.
Browse files Browse the repository at this point in the history
Add pre and post combat state checking and setting.

Nago quest now works correctly as a result.
  • Loading branch information
xavieran committed Jan 20, 2024
1 parent 124554b commit 7f58f6c
Show file tree
Hide file tree
Showing 17 changed files with 336 additions and 140 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ string(CONCAT CXX_IGNORES
"-Wno-unused-private-field "
"-Wno-unused-function "
"-Wno-deprecated-enum-enum-conversion "
# This is currently needed for glm
#glm/gtx/string_cast.inl:25:40: error: comparison of unsigned expression in ‘>= 0’ is always true [-Werror=type-limits]
# 25 | assert(strlen(message) >= 0 && strlen(message) < STRING_BUFFER);
"-Wno-type-limits "
)

#set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
Expand Down
2 changes: 2 additions & 0 deletions bak/dialogSources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ class DialogSources
static constexpr auto mGenericScoutedCombat = KeyTarget{0x2f};

static constexpr auto mCharacterFlavourDialog = BAK::KeyTarget{0x69};

static constexpr auto mAfterNagoCombatSetKeys = BAK::KeyTarget{0x1cfdf1};
};

}
2 changes: 1 addition & 1 deletion bak/gameData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ Location GameData::LoadLocation()
unsigned ypos = mBuffer.GetUint32LE();

mLogger.Info() << "Unknown: " << mBuffer.GetArray<5>() << "\n";
std::uint16_t heading = mBuffer.GetUint16LE() >> 8;
std::uint16_t heading = mBuffer.GetUint16LE();

mLogger.Info() << "Tile: " << xtile << "," << ytile << std::endl;
mLogger.Info() << "Pos: " << xpos << "," << ypos << std::endl;
Expand Down
13 changes: 10 additions & 3 deletions bak/gameState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,8 @@ class GameState

// Probably want to put this logic somewhere else...
// if eventPtr % 10 != 0
if (mGameData
&& std::get<1>(State::CalculateComplexEventOffset(choice.mEventPointer))
!= 0)
const auto [byteOffset, bitOffset] = State::CalculateComplexEventOffset(choice.mEventPointer);
if (mGameData && bitOffset != 0)
{
return (state >= choice.mXorMask) && (state <= choice.mMustEqualExpected);
}
Expand All @@ -582,17 +581,25 @@ class GameState
{
if (((state ^ choice.mXorMask) & choice.mExpected) == choice.mExpected
&& chapterMaskSatisfied)
{
return true;
}
else
{
return false;
}
}
else
{
if (((state ^ choice.mXorMask) & choice.mExpected) != 0
&& chapterMaskSatisfied)
{
return true;
}
else
{
return false;
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion bak/monster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ class MonsterNames
{
static constexpr std::string sInvalidMonster = "INVALID MONSTER";
public:
MonsterNames();
static const MonsterNames& Get()
{
static auto mnames = MonsterNames{};
return mnames;
}

struct Monster
{
Expand Down Expand Up @@ -64,6 +68,8 @@ class MonsterNames
auto size() const { return mMonsterPrefixes.size(); }

private:
MonsterNames();

std::vector<std::string> mMonsterNames;
std::vector<Monster> mMonsterPrefixes;
};
Expand Down
4 changes: 2 additions & 2 deletions bak/spells.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class SpellDatabase

void LoadSpellWeaknesses()
{
auto monsters = MonsterNames{};
auto monsters = MonsterNames::Get();
auto fb = FileBufferFactory::Get().CreateDataBuffer(sSpellWeaknessesFile);
unsigned entries = fb.GetUint16LE();
for (unsigned i = 0; i < entries; i++)
Expand All @@ -339,7 +339,7 @@ class SpellDatabase

void LoadSpellResistances()
{
auto monsters = MonsterNames{};
auto monsters = MonsterNames::Get();
auto fb = FileBufferFactory::Get().CreateDataBuffer(sSpellResistances);
unsigned entries = fb.GetUint16LE();
for (unsigned i = 0; i < entries; i++)
Expand Down
2 changes: 1 addition & 1 deletion bak/state/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void SetEventDialogAction(FileBuffer& fb, const SetFlag& setFlag)
^ setFlag.mAlwaysZero;
fb.Seek(offset);

Logging::LogSpam(__FUNCTION__) << std::hex <<
Logging::LogSpam(__FUNCTION__) << std::hex <<
" " << setFlag << " offset: " << offset
<< " data[" << +data << "] new[" << +newData <<"]\n" << std::dec;
fb.PutUint8(newData);
Expand Down
107 changes: 99 additions & 8 deletions bak/state/encounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bool CheckActive(
ZoneNumber zone)
{
const auto encounterIndex = encounter.GetIndex().mValue;
const bool alreadyEncountered = CheckUniqueEncounterStateFlagOffset(
const bool alreadyEncountered = CheckUniqueEncounterStateFlag(
fb,
zone,
encounter.GetTileIndex(),
Expand All @@ -41,7 +41,7 @@ bool CheckCombatActive(
ZoneNumber zone)
{
const auto encounterIndex = encounter.GetIndex().mValue;
const bool alreadyEncountered = CheckUniqueEncounterStateFlagOffset(
const bool alreadyEncountered = CheckUniqueEncounterStateFlag(
fb,
zone,
encounter.GetTileIndex(),
Expand Down Expand Up @@ -129,12 +129,12 @@ void SetPostEnableOrDisableEventFlags(

if (encounter.mUnknown2 != 0)
{
SetEventFlagTrue(
SetUniqueEncounterStateFlag(
fb,
CalculateUniqueEncounterStateFlagOffset(
zone,
encounter.GetTileIndex(),
encounter.GetIndex().mValue));
zone,
encounter.GetTileIndex(),
encounter.GetIndex().mValue,
true);
}
}

Expand All @@ -154,7 +154,7 @@ unsigned CalculateUniqueEncounterStateFlagOffset(
return offset + encounterStateOffset;
}

bool CheckUniqueEncounterStateFlagOffset(
bool CheckUniqueEncounterStateFlag(
FileBuffer& fb,
ZoneNumber zone,
std::uint8_t tileIndex,
Expand All @@ -168,6 +168,22 @@ bool CheckUniqueEncounterStateFlagOffset(
encounterIndex));
}

void SetUniqueEncounterStateFlag(
FileBuffer& fb,
ZoneNumber zone,
std::uint8_t tileIndex,
std::uint8_t encounterIndex,
bool value)
{
SetEventFlag(
fb,
CalculateUniqueEncounterStateFlagOffset(
zone,
tileIndex,
encounterIndex),
value);
}

unsigned CalculateCombatEncounterScoutedStateFlag(
std::uint8_t encounterIndex)
{
Expand Down Expand Up @@ -195,6 +211,19 @@ bool CheckCombatEncounterStateFlag(
CalculateCombatEncounterStateFlag(combatIndex));
}

void SetCombatEncounterState(
FileBuffer& fb,
unsigned combatIndex,
bool state)
{
constexpr auto alwaysTriggeredIndex = 0x3e8;
if (combatIndex >= alwaysTriggeredIndex)
{
return;
}
SetEventFlag(fb, CalculateCombatEncounterStateFlag(combatIndex), state);
}

void SetCombatEncounterScoutedState(
FileBuffer& fb,
std::uint8_t encounterIndex,
Expand Down Expand Up @@ -237,4 +266,66 @@ void ClearTileRecentEncounters(
SetEventFlagFalse(fb, CalculateCombatEncounterScoutedStateFlag(i));
}
}

void SetPostCombatCombatSpecificFlags(FileBuffer& fb, unsigned combatIndex)
{
switch (combatIndex)
{
case 74: return; // this runs dialog 1cfdf1 (nago combat flags)
// and is handled externally in my code
case 131: [[fallthrough]];
case 132: [[fallthrough]];
case 133: [[fallthrough]];
case 134: [[fallthrough]];
case 135:
if (ReadEventBool(fb, 0x14e7)
&& ReadEventBool(fb, 0x14e8)
&& ReadEventBool(fb, 0x14e9)
&& ReadEventBool(fb, 0x14ea)
&& ReadEventBool(fb, 0x14eb))
{
SetEventFlag(fb, 0xdb1c, 1);
}
return;
// The never-ending combats?
case 235: [[fallthrough]];
case 245: [[fallthrough]];
case 291: [[fallthrough]];
case 293: [[fallthrough]];
case 335: [[fallthrough]];
case 337: [[fallthrough]];
case 338: [[fallthrough]];
case 375: [[fallthrough]];
case 410: [[fallthrough]];
case 429: [[fallthrough]];
case 430:
// Zero State 0x190 UniqueEncounterStateFlag
// Zero state 0x1450 Recently encountered encounter
// Zero state 145a
// Zero state 1464
// Zero combat clicked...
break;
case 610: [[fallthrough]];
case 613: [[fallthrough]];
case 615: [[fallthrough]];
case 618: [[fallthrough]];
case 619: [[fallthrough]];
case 621:
if (ReadEventBool(fb, 0x16c6)
&& ReadEventBool(fb, 0x16c9)
&& ReadEventBool(fb, 0x16cb)
&& ReadEventBool(fb, 0x16ce)
&& ReadEventBool(fb, 0x16cf)
&& ReadEventBool(fb, 0x16d1))
{
SetEventFlag(fb, 0x1d17, 1);
}
default:break;
}
}

void ZeroCombatClicked(unsigned combatIndex)
{
auto offset = (combatIndex * 0xe) + 0x131f;
}
}
25 changes: 19 additions & 6 deletions bak/state/encounter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,36 +57,49 @@ unsigned CalculateUniqueEncounterStateFlagOffset(
std::uint8_t tileIndex,
std::uint8_t encounterIndex);

bool CheckUniqueEncounterStateFlagOffset(
bool CheckUniqueEncounterStateFlag(
FileBuffer&,
ZoneNumber zone,
std::uint8_t tileIndex,
std::uint8_t encounterIndex);

void SetUniqueEncounterStateFlag(
FileBuffer& fb,
ZoneNumber zone,
std::uint8_t tileIndex,
std::uint8_t encounterIndex,
bool value);

// 1450 is "recently encountered this encounter"
// should be cleared when we move to a new tile
// (or it will inhibit the events of the new tile)
unsigned CalculateRecentEncounterStateFlag(
std::uint8_t encounterIndex);

// 145a is combat scouted flag
// 1464 is combat completed flag
unsigned CalculateCombatEncounterStateFlag(
unsigned combatIndex);

// 1464 is combat completed flag
unsigned CalculateCombatEncounterScoutedStateFlag(
std::uint8_t encounterIndex);

bool CheckCombatEncounterStateFlag(
FileBuffer&,
unsigned combatIndex);

void SetCombatEncounterState(
FileBuffer& fb,
unsigned combatIndex,
bool state);

// 145a is combat scouted flag
unsigned CalculateCombatEncounterScoutedStateFlag(
std::uint8_t encounterIndex);

void SetCombatEncounterScoutedState(
FileBuffer&,
std::uint8_t encounterIndex, bool state);

void SetRecentlyEncountered(FileBuffer&, std::uint8_t encounterIndex);
bool CheckRecentlyEncountered(FileBuffer&, std::uint8_t encounterIndex);
void ClearTileRecentEncounters(FileBuffer&);
void SetPostCombatCombatSpecificFlags(FileBuffer& fb, unsigned combatIndex);

}
2 changes: 1 addition & 1 deletion bak/worldFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ZoneTextureStore::ZoneTextureStore(

mHorizonOffset = GetTextures().size();

const auto monsters = MonsterNames{};
const auto monsters = MonsterNames::Get();
for (unsigned i = 0; i < monsters.size(); i ++)
{
auto prefix = monsters.GetMonsterAnimationFile(MonsterIndex{i});
Expand Down
2 changes: 1 addition & 1 deletion bak/zone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Zone
item.GetName(),
BAK::ZoneItemToMeshObject(item, mZoneTextures, mPalette));

const auto monsters = MonsterNames{};
const auto monsters = MonsterNames::Get();
for (unsigned i = 0; i < monsters.size(); i++)
{
mObjects.AddObject(
Expand Down
37 changes: 37 additions & 0 deletions game/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,39 @@ struct Console : public std::streambuf
mGameState->SetChapter(BAK::Chapter{chapter});
}

void SetLogLevel(const std::vector<std::string>& words)
{
if (words.size() < 2)
{
std::stringstream ss{};
for (const auto& w : words)
ss << w << "|";
AddLog("[error] Usage: SET_LOG_LEVEL LEVEL (%s)", ss.str().c_str());
return;
}

if (words[1] == LevelToString(Logging::LogLevel::Spam))
{
Logging::LogState::SetLevel(Logging::LogLevel::Spam);
}
else if (words[1] == LevelToString(Logging::LogLevel::Debug))
{
Logging::LogState::SetLevel(Logging::LogLevel::Debug);
}
else if (words[1] == LevelToString(Logging::LogLevel::Info))
{
Logging::LogState::SetLevel(Logging::LogLevel::Info);
}
else if (words[1] == LevelToString(Logging::LogLevel::Fatal))
{
Logging::LogState::SetLevel(Logging::LogLevel::Fatal);
}
else
{
AddLog("[error] SET_LOG_LEVEL FAILED Invalid log level provided");
}
}

Console()
:
mStream{this},
Expand Down Expand Up @@ -402,6 +435,10 @@ struct Console : public std::streambuf
for (int i = first > 0 ? first : 0; i < mHistory.Size; i++)
AddLog("%3d: %s\n", i, mHistory[i]);
});

mCommands.push_back("SET_LOG_LEVEL");
mCommandActions.emplace_back([this](const auto& cmd){ SetLogLevel(cmd); });

mCommands.push_back("CLEAR");
mCommandActions.emplace_back([this](const auto& cmd)
{
Expand Down
Loading

0 comments on commit 7f58f6c

Please sign in to comment.