-
Notifications
You must be signed in to change notification settings - Fork 801
Add Corpse Run enhancement mode #6790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jdperos
wants to merge
12
commits into
HarbourMasters:develop
Choose a base branch
from
jdperos:corpse-run
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bb63dcf
Corpse run mode first pass
jdperos 3851278
Casting double to float to avoid error with new libultraship
jdperos 5074d37
Don't activate corpse remnant until player respawn
jdperos 7ec944b
Save/Load for CorpseRun
jdperos bb94601
Cleaning up code a bit
jdperos 88b25e8
A little more cleanup - preventing unnecessary copy
jdperos 0ef16cc
Handling magic. Moving consumables to a shared descriptor table for l…
jdperos 252b4b2
Merge branch 'HarbourMasters:develop' into corpse-run
jdperos 8f68c1a
clang format
jdperos 8bd5ff5
Small cleanup
jdperos aeb30ff
Binding to OnActorInit instead of making OnPlayerInit
jdperos 6b2fccc
Merge remote-tracking branch 'upstream/develop' into corpse-run
jdperos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,304 @@ | ||
| #include "soh/Enhancements/game-interactor/GameInteractor_Hooks.h" | ||
| #include "soh/Enhancements/randomizer/draw.h" | ||
| #include "soh/SaveManager.h" | ||
| #include "soh/ShipInit.hpp" | ||
| #include "z64item.h" | ||
|
|
||
| extern "C" { | ||
| #include "functions.h" | ||
| #include "macros.h" | ||
| #include "variables.h" | ||
|
|
||
| extern PlayState* gPlayState; | ||
| } | ||
|
|
||
| static constexpr int32_t CVAR_CORPSE_RUN_DEFAULT = 0; | ||
| #define CVAR_CORPSE_RUN_NAME CVAR_ENHANCEMENT("CorpseRun") | ||
| #define CVAR_CORPSE_RUN_VALUE CVarGetInteger(CVAR_CORPSE_RUN_NAME, CVAR_CORPSE_RUN_DEFAULT) | ||
|
|
||
| namespace CorpseRun { | ||
|
|
||
| static constexpr int32_t INVALID_SAVE_SECTION_ID = -1; | ||
| static constexpr int32_t VERSION = 1; | ||
| static constexpr float COLLECT_DISTANCE = 40.0f; | ||
|
|
||
| struct Remnant { | ||
| // State | ||
| bool dropped = false; | ||
| bool active = false; | ||
|
|
||
| int16_t sceneNum = -1; | ||
| int8_t roomNum = -1; | ||
|
|
||
| Vec3f pos = {}; | ||
| int16_t yaw = 0; | ||
|
|
||
| // Resources | ||
| int32_t rupees = 0; | ||
| int32_t magic = 0; | ||
|
|
||
| // Consumables | ||
| int32_t bombs = 0; | ||
| int32_t arrows = 0; | ||
| int32_t sticks = 0; | ||
| int32_t nuts = 0; | ||
| int32_t seeds = 0; | ||
| int32_t bombchus = 0; | ||
|
|
||
| // TODO(jperos): Progression items and/or Equipment modes? | ||
|
|
||
| void Clear() { | ||
| *this = {}; | ||
| } | ||
| bool HasContents() const; | ||
| bool IsInRoom() const; | ||
| bool IsRecoverable() const { | ||
| return dropped && active; | ||
| } | ||
| bool IsPendingActivation() const { | ||
| return dropped && !active; | ||
| } | ||
| }; | ||
|
|
||
| static constexpr struct ConsumableDesc { | ||
| const char* name; | ||
| int32_t Remnant::*field; | ||
| int16_t itemId; | ||
| } CONSUMABLE_TABLE[] = { | ||
| // clang-format off | ||
| { "bombs", &Remnant::bombs, ITEM_BOMB }, | ||
| { "arrows", &Remnant::arrows, ITEM_BOW }, | ||
| { "sticks", &Remnant::sticks, ITEM_STICK }, | ||
| { "nuts", &Remnant::nuts, ITEM_NUT }, | ||
| { "seeds", &Remnant::seeds, ITEM_SLINGSHOT }, | ||
| { "bombchus", &Remnant::bombchus, ITEM_BOMBCHU } | ||
| // clang-format on | ||
| }; | ||
|
|
||
| static Remnant sRemnant; | ||
| static GetItemEntry sMysteryItem = GET_ITEM_MYSTERY; | ||
|
|
||
| // Remnant member helpers | ||
| bool Remnant::HasContents() const { | ||
| if (rupees > 0) | ||
| return true; | ||
| if (magic > 0) | ||
| return true; | ||
| for (const ConsumableDesc& desc : CONSUMABLE_TABLE) { | ||
| if (this->*desc.field > 0) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| bool Remnant::IsInRoom() const { | ||
| return gPlayState != nullptr && gPlayState->sceneNum == sceneNum && gPlayState->roomCtx.curRoom.num == roomNum; | ||
| } | ||
|
|
||
| // Save/Load | ||
| static void InitSave(bool isDebug) { | ||
| sRemnant.Clear(); | ||
| } | ||
|
|
||
| static void SaveFile() { | ||
| SaveManager::Instance->SaveFile(gSaveContext.fileNum); | ||
| } | ||
|
|
||
| static void LoadSave() { | ||
| sRemnant.Clear(); | ||
|
|
||
| SaveManager::Instance->LoadData("dropped", sRemnant.dropped, false); | ||
|
|
||
| if (!sRemnant.dropped) { | ||
| return; | ||
| } | ||
|
|
||
| // Loaded remnants should always be recoverable. | ||
| sRemnant.active = true; | ||
|
|
||
| SaveManager::Instance->LoadData("sceneNum", sRemnant.sceneNum, (int16_t)-1); | ||
| SaveManager::Instance->LoadData("roomNum", sRemnant.roomNum, (int8_t)-1); | ||
|
|
||
| SaveManager::Instance->LoadStruct("pos", []() { | ||
| SaveManager::Instance->LoadData("x", sRemnant.pos.x, 0.0f); | ||
| SaveManager::Instance->LoadData("y", sRemnant.pos.y, 0.0f); | ||
| SaveManager::Instance->LoadData("z", sRemnant.pos.z, 0.0f); | ||
| }); | ||
|
|
||
| SaveManager::Instance->LoadData("yaw", sRemnant.yaw, (int16_t)0); | ||
|
|
||
| SaveManager::Instance->LoadStruct("contents", []() { | ||
| SaveManager::Instance->LoadData("rupees", sRemnant.rupees, 0); | ||
| SaveManager::Instance->LoadData("magic", sRemnant.magic, 0); | ||
| for (const ConsumableDesc& desc : CONSUMABLE_TABLE) { | ||
| SaveManager::Instance->LoadData(desc.name, sRemnant.*desc.field, 0); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| static void Save(SaveContext* saveContext, int sectionID, bool fullSave) { | ||
| SaveManager::Instance->SaveData("dropped", sRemnant.dropped); | ||
|
|
||
| if (!sRemnant.dropped) { | ||
| return; | ||
| } | ||
|
|
||
| SaveManager::Instance->SaveData("sceneNum", sRemnant.sceneNum); | ||
| SaveManager::Instance->SaveData("roomNum", sRemnant.roomNum); | ||
|
|
||
| SaveManager::Instance->SaveStruct("pos", []() { | ||
| SaveManager::Instance->SaveData("x", sRemnant.pos.x); | ||
| SaveManager::Instance->SaveData("y", sRemnant.pos.y); | ||
| SaveManager::Instance->SaveData("z", sRemnant.pos.z); | ||
| }); | ||
|
|
||
| SaveManager::Instance->SaveData("yaw", sRemnant.yaw); | ||
|
|
||
| SaveManager::Instance->SaveStruct("contents", []() { | ||
| SaveManager::Instance->SaveData("rupees", sRemnant.rupees); | ||
| SaveManager::Instance->SaveData("magic", sRemnant.magic); | ||
| for (const ConsumableDesc& desc : CONSUMABLE_TABLE) { | ||
| SaveManager::Instance->SaveData(desc.name, sRemnant.*desc.field); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| static void RegisterSave() { | ||
| static int32_t sSaveSectionId = CorpseRun::INVALID_SAVE_SECTION_ID; | ||
|
|
||
| // Don't register twice | ||
| if (sSaveSectionId != CorpseRun::INVALID_SAVE_SECTION_ID) { | ||
| return; | ||
| } | ||
|
|
||
| SaveManager::Instance->AddInitFunction(InitSave); | ||
| SaveManager::Instance->AddLoadFunction("corpseRun", CorpseRun::VERSION, LoadSave); | ||
| sSaveSectionId = | ||
| SaveManager::Instance->AddSaveFunction("corpseRun", CorpseRun::VERSION, Save, true, SECTION_PARENT_NONE); | ||
| } | ||
|
|
||
| // Corpse Run Logic | ||
| static void Activate(Player* player, PlayState* playState, int32_t respawnFlag) { | ||
| if (!sRemnant.dropped) { | ||
| return; | ||
| } | ||
|
|
||
| sRemnant.active = true; | ||
| } | ||
|
|
||
| static void Drop() { | ||
| // Safety in case death hook fires more than once during the same death flow | ||
| // If a remnant has been dropped but not activated by Player_Init yet, this is probably the same death | ||
| if (sRemnant.IsPendingActivation()) { | ||
| return; | ||
| } | ||
|
|
||
| if (gPlayState == nullptr) { | ||
| return; | ||
| } | ||
|
|
||
| Player* player = GET_PLAYER(gPlayState); | ||
| if (player == nullptr) { | ||
| return; | ||
| } | ||
|
|
||
| // Destroy previous remnant | ||
| sRemnant.Clear(); | ||
|
|
||
| sRemnant.dropped = true; | ||
| sRemnant.active = false; | ||
| sRemnant.sceneNum = gPlayState->sceneNum; | ||
| sRemnant.roomNum = gPlayState->roomCtx.curRoom.num; | ||
| sRemnant.pos = player->actor.world.pos; | ||
| sRemnant.yaw = player->actor.shape.rot.y; | ||
|
|
||
| // v1 - consumables only | ||
| sRemnant.rupees = gSaveContext.rupees; | ||
| sRemnant.magic = gSaveContext.magic; | ||
| for (const ConsumableDesc& desc : CONSUMABLE_TABLE) { | ||
| sRemnant.*desc.field = AMMO(desc.itemId); | ||
| } | ||
|
|
||
| if (!sRemnant.HasContents()) { | ||
| // Dark Souls behavior: dying again still destroys the previous remnant, | ||
| // even if the new death has nothing worth dropping | ||
| sRemnant.Clear(); | ||
| SaveFile(); | ||
| return; | ||
| } | ||
|
|
||
| gSaveContext.rupees = 0; | ||
| Magic_Reset(gPlayState); | ||
| for (const ConsumableDesc& desc : CONSUMABLE_TABLE) { | ||
| AMMO(desc.itemId) = 0; | ||
| } | ||
|
|
||
| SaveFile(); | ||
| } | ||
|
|
||
| static void Recover() { | ||
| Rupees_ChangeBy(sRemnant.rupees); | ||
| Magic_RequestChange(gPlayState, sRemnant.magic, MAGIC_ADD); | ||
| for (const ConsumableDesc& desc : CONSUMABLE_TABLE) { | ||
| Inventory_ChangeAmmo(desc.itemId, sRemnant.*desc.field); | ||
| } | ||
|
|
||
| sRemnant.Clear(); | ||
|
|
||
| SaveFile(); | ||
| } | ||
|
|
||
| static void Update() { | ||
| if (!sRemnant.IsInRoom() || !sRemnant.IsRecoverable()) { | ||
| return; | ||
| } | ||
|
|
||
| Player* player = GET_PLAYER(gPlayState); | ||
| if (player == nullptr) { | ||
| return; | ||
| } | ||
|
|
||
| const float dx = player->actor.world.pos.x - sRemnant.pos.x; | ||
| const float dy = player->actor.world.pos.y - sRemnant.pos.y; | ||
| const float dz = player->actor.world.pos.z - sRemnant.pos.z; | ||
| const float distSq = dx * dx + dy * dy + dz * dz; | ||
|
|
||
| if (distSq < SQ(COLLECT_DISTANCE)) { | ||
| Recover(); | ||
| } | ||
| } | ||
|
|
||
| static void Draw() { | ||
| if (!sRemnant.IsInRoom() || !sRemnant.IsRecoverable()) { | ||
| return; | ||
| } | ||
|
|
||
| Matrix_Push(); | ||
|
|
||
| // Cheap bob/spin so it reads as collectible-ish | ||
| // Just kind of picking the values out here | ||
| const s16 bobAngle = static_cast<s16>(gPlayState->gameplayFrames * 0x400); | ||
| const s16 spinAngle = static_cast<s16>(gPlayState->gameplayFrames * 0x200); | ||
|
|
||
| const f32 bob = Math_SinS(bobAngle) * 8.0f; | ||
|
|
||
| Matrix_Translate(sRemnant.pos.x, sRemnant.pos.y + 40.0f + bob, sRemnant.pos.z, MTXMODE_NEW); | ||
| Matrix_RotateY(static_cast<f32>(BINANG_TO_RAD(spinAngle)), MTXMODE_APPLY); | ||
| Matrix_Scale(0.35f, 0.35f, 0.35f, MTXMODE_APPLY); | ||
|
|
||
| Randomizer_DrawMysteryItem(gPlayState, &sMysteryItem); | ||
|
|
||
| Matrix_Pop(); | ||
| } | ||
|
|
||
| static void Register() { | ||
| RegisterSave(); | ||
|
|
||
| COND_HOOK(OnPlayerInit, CVAR_CORPSE_RUN_VALUE, Activate); | ||
| COND_HOOK(OnPlayerDeath, CVAR_CORPSE_RUN_VALUE, Drop); | ||
| COND_HOOK(OnGameFrameUpdate, CVAR_CORPSE_RUN_VALUE, Update); | ||
| COND_HOOK(OnPlayDrawEnd, CVAR_CORPSE_RUN_VALUE, Draw); | ||
| } | ||
| } // namespace CorpseRun | ||
|
|
||
| static RegisterShipInitFunc initFunc(CorpseRun::Register, { CVAR_CORPSE_RUN_NAME }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.