Skip to content

Commit

Permalink
fixed many issues + added SAVE RANDO PROGRESS!
Browse files Browse the repository at this point in the history
  • Loading branch information
Sora-yx committed Oct 25, 2021
1 parent c916e8f commit e629e52
Show file tree
Hide file tree
Showing 25 changed files with 27,003 additions and 82 deletions.
6 changes: 5 additions & 1 deletion sadx-randomizer/35_TwinkleCircuit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ void TwinkleCircuitResult() {
else
LastStoryFlag = 0;

//CustomFlag++;
if (!IsAdventureComplete(SelectedCharacter))
CustomFlag++;

CustomFlagCheck();
Rando_SaveProgression();
Rings = 0;
GameMode = GameModes_Adventure_Field;
GameState = 0x5;
Expand Down
10 changes: 8 additions & 2 deletions sadx-randomizer/AISettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ ObjectMaster* LoadCharObj(int i)
return CurAI;
}

int AI_BannedLevel[16]{
int AI_BannedLevel[17]{

LevelIDs_EmeraldCoast, LevelIDs_TwinklePark, LevelIDs_SpeedHighway, LevelIDs_RedMountain,
LevelIDs_EmeraldCoast, LevelIDs_TwinklePark, LevelIDs_SpeedHighway, LevelIDs_RedMountain, LevelIDs_IceCap,
LevelIDs_SkyDeck, LevelIDs_LostWorld, LevelIDs_Casinopolis, LevelIDs_FinalEgg, LevelIDs_Chaos0,
LevelIDs_Chaos2, LevelIDs_Chaos6, LevelIDs_PerfectChaos, LevelIDs_EggWalker,
LevelIDs_EggViper, LevelIDs_SandHill, LevelIDs_E101R
Expand Down Expand Up @@ -74,6 +74,12 @@ int CheckTailsAI_R(void) { //restriction and bug fixes.
if (CurrentAct == 1)
return 0;
break;
case LevelIDs_IceCap:
if (CurrentStageVersion == SonicVersion)
{
return 0;
}
break;
case LevelIDs_FinalEgg: //cutscene issue
if (CurrentCharacter == Characters_Amy)
return 0;
Expand Down
156 changes: 133 additions & 23 deletions sadx-randomizer/AdventureFlags.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,124 @@
#include "stdafx.h"
#include <string>
#include "nlohmann/json.hpp"
#include <fstream>
#include <iostream>


using json = nlohmann::json;
using namespace std;

Trampoline* DeleteSave_t;
int CustomFlag = 0; //Used for progression story and credits
bool CreditCheck = false;
extern int musicCount;

void Rando_SaveProgression() {

const char* saveOK = "Successfully Saved Rando progression on seed %d \n";
string savePath = modpath + "\\SAVEDATA\\";

json j;

j["levelCount"] = levelCount;
j["customFlag"] = CustomFlag;
j["cutsceneCount"] = cutsceneAllowedCount;
j["seed"] = SeedCopy;
j["saveFile"] = CurrentSaveSlot;

string saveNumber = to_string(CurrentSaveSlot);

std::ofstream file(savePath + "RandoSave" + saveNumber + ".json");
file << j;
file.close();
PrintDebug(saveOK, SeedCopy);
return;
}

DataPointer(ObjectMaster*, FileSelTp, 0x3C5E8C0);

void Rando_LoadProgression() {

if (GameState != 21 && GameMode != 12 || !FileSelTp)
return;

const char* loadOK = "Successfully Loaded Rando Save on seed %d \n";
const char* loadFailed = "Save doesn't match the current seed, load progression cancelled. \n";
string savePath = modpath + "\\SAVEDATA\\";

string saveNumber = to_string(CurrentSaveSlot);
string saveFileFullPath = savePath + "RandoSave" + saveNumber + ".json";

json j;

if (checkIfFileExists(saveFileFullPath.c_str()))
{
std::ifstream file(saveFileFullPath);
file >> j;
file.close();

if (SeedCopy == (int64_t)j["seed"]) {

levelCount = (int64_t)j["levelCount"];
CustomFlag = (int64_t)j["customFlag"];
cutsceneAllowedCount = (int64_t)j["cutsceneCount"];
PrintDebug(loadOK, SeedCopy);
}
else {
PrintDebug(loadFailed, SeedCopy);
}
}
else {
levelCount = 0;
CustomFlag = 0;
cutsceneAllowedCount = 0;
}

return;
}

void LoadSave_r() {

j_LoadSave();
Rando_LoadProgression();
}


void Rando_DeleteProgression() {
if (GameState != 21 && GameMode != 12)
return;

const char* deleteMSG = "Deleted Rando progression. on save: %d \n";
string savePath = modpath + "\\SAVEDATA\\";

string saveNumber = to_string(CurrentSaveSlot);
string saveFileFullPath = savePath + "RandoSave" + saveNumber + ".json";
LPCSTR delSave = saveFileFullPath.c_str();

if (checkIfFileExists(saveFileFullPath.c_str()))
{
if (DeleteFileA(delSave))
{
CustomFlag = 0;
levelCount = 0;
PrintDebug(deleteMSG, CurrentSaveSlot);
}
}

return;
}

void DeleteSave_r() {

Rando_DeleteProgression();
auto original = reinterpret_cast<decltype(DeleteSave_r)*>(DeleteSave_t->Target());
original();
}


int GetCharaProgression() {


if (!CustomFlag)
{
ChaoGardenSSWarpOpen = 1;
Expand Down Expand Up @@ -74,12 +184,14 @@ void DeleteCustomFlag() {
return SomethingAboutFlag();
}


void AddCustomFlag() {

if (!IsAdventureComplete(SelectedCharacter))
CustomFlag++;

CustomFlagCheck();
Rando_SaveProgression();

return StartLevelCutscene(1);
}

Expand All @@ -100,7 +212,7 @@ void CustomFlagCheck() {
if (GameMode == 9 || GameMode == 10)
return;

if (SelectedCharacter == CharSonic)
if (SelectedCharacter == CharSonic)
{
switch (CustomFlag)
{
Expand Down Expand Up @@ -154,13 +266,12 @@ void CustomFlagCheck() {
EventFlagArray[EventFlags_Sonic_EggViperClear] = true;
EventFlagArray[EventFlags_SonicAdventureComplete] = true;
EventFlagArray[EventFlags_TailsUnlockedAdventure] = true;
credits();
}
break;
return;
}
}

if (SelectedCharacter == CharTails)
if (SelectedCharacter == CharTails)
{
switch (CustomFlag)
{
Expand Down Expand Up @@ -194,7 +305,6 @@ void CustomFlagCheck() {
EventFlagArray[EventFlags_Tails_EggWalkerClear] = true;
EventFlagArray[EventFlags_TailsAdventureComplete] = true;
EventFlagArray[EventFlags_KnucklesUnlockedAdventure] = true;
credits();
break;
default:
if (CustomFlag >= 7) {
Expand All @@ -203,13 +313,12 @@ void CustomFlagCheck() {
EventFlagArray[EventFlags_Tails_EggWalkerClear] = true;
EventFlagArray[EventFlags_TailsAdventureComplete] = true;
EventFlagArray[EventFlags_KnucklesUnlockedAdventure] = true;
credits();
}
break;
return;
}
}



if (SelectedCharacter == CharKnuckles) //Knuckles
{
Expand Down Expand Up @@ -240,17 +349,15 @@ void CustomFlagCheck() {
EventFlagArray[EventFlags_Knuckles_Chaos6Clear] = true;
EventFlagArray[EventFlags_KnucklesAdventureComplete] = true;
EventFlagArray[EventFlags_AmyUnlockedAdventure] = true;
credits();
break;
default:
if (CustomFlag >= 7) {
CreditCheck = true;
EventFlagArray[EventFlags_Knuckles_Chaos6Clear] = true;
EventFlagArray[EventFlags_KnucklesAdventureComplete] = true;
EventFlagArray[EventFlags_AmyUnlockedAdventure] = true;
credits();
}
break;
return;
}
}
if (SelectedCharacter == CharAmy) //Amy
Expand All @@ -273,17 +380,15 @@ void CustomFlagCheck() {
EventFlagArray[EventFlags_Amy_ZeroClear] = true;
EventFlagArray[EventFlags_AmyAdventureComplete] = true;
EventFlagArray[EventFlags_BigUnlockedAdventure] = true;
credits();
break;
default:
if (CustomFlag >= 5) {
CreditCheck = true;
EventFlagArray[EventFlags_Amy_ZeroClear] = true;
EventFlagArray[EventFlags_AmyAdventureComplete] = true;
EventFlagArray[EventFlags_BigUnlockedAdventure] = true;
credits();
}
break;
return;
}
}

Expand All @@ -309,17 +414,15 @@ void CustomFlagCheck() {
EventFlagArray[EventFlags_Big_Chaos6Clear] = true;
EventFlagArray[EventFlags_BigAdventureComplete] = true;
EventFlagArray[EventFlags_GammaUnlockedAdventure] = true;
credits();
break;
default:
if (CustomFlag >= 6) {
CreditCheck = true;
EventFlagArray[EventFlags_Big_Chaos6Clear] = true;
EventFlagArray[EventFlags_BigAdventureComplete] = true;
EventFlagArray[EventFlags_GammaUnlockedAdventure] = true;
credits();
}
break;
return;
}
}

Expand Down Expand Up @@ -348,27 +451,34 @@ void CustomFlagCheck() {
EventFlagArray[EventFlags_Gamma_E101mkIIClear] = true;
EventFlagArray[EventFlags_GammaAdventureComplete] = true;
EventFlagArray[EventFlags_SuperSonicUnlockedAdventure] = true;
credits();
break;
default:
if (CustomFlag >= 6) {
CreditCheck = true;
EventFlagArray[EventFlags_Gamma_E101mkIIClear] = true;
EventFlagArray[EventFlags_GammaAdventureComplete] = true;
EventFlagArray[EventFlags_SuperSonicUnlockedAdventure] = true;
credits();
}
break;
return;
}
}
if (SelectedCharacter == CharSuperSonic)
{
if (CustomFlag >= 1) {
CreditCheck = true;
EventFlagArray[EventFlags_SuperSonicAdventureComplete] = true;
credits();
}
}

return;
}

void init_FlagsProgression() {
WriteCall((void*)0x413368, DeleteCustomFlag); //Reset flags when you create a new savefile.
WriteCall((void*)0x42af3b, AddCustomFlag);

WriteCall((void*)0x503782, LoadSave_r);
WriteCall((void*)0x5037fc, LoadSave_r);
WriteJump((void*)0x5053DD, LoadSave_r);
DeleteSave_t = new Trampoline((int)DeleteSave, (int)DeleteSave + 0x6, DeleteSave_r);
}
2 changes: 1 addition & 1 deletion sadx-randomizer/BackRing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void __cdecl CheckLoadCapsule_r(ObjectMaster* a1) {
RaceResultCheck(a1);

//call original function (Capsule.)
if (CurrentStageVersion != KnucklesVersion && CurrentMission < Mission2_100Rings) {
if (CurrentStageVersion != KnucklesVersion && CurrentMission < Mission2_100Rings || CurrentLevel == LevelIDs_LostWorld && CurrentAct == 2) {
ObjectFunc(origin, Capsule_Load_T->Target());
origin(a1);
}
Expand Down
7 changes: 6 additions & 1 deletion sadx-randomizer/CharactersSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,14 @@ void fixCharacterSoundAfterReset() {

void CallStuffWhenLevelStart() {


TimeThing = 1; //activate the timer of the stage.

if (CurrentLevel > LevelIDs_E101R)
return;

ObjectMaster* P1 = GetCharacterObject(0);
char CurChara = P1->Data1->CharID;
TimeThing = 1; //activate the timer of the stage.

if (CurChara != Characters_Sonic)
{
Expand Down
3 changes: 1 addition & 2 deletions sadx-randomizer/Common_Startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ void __cdecl StartupMiscellaneous_Init(const char* path, const HelperFunctions&
//Stats & Value Reset
WriteCall((void*)0x42ca4f, SoftReset_R); //Reset value and stuff properly when you Soft Reset and quit.
WriteCall((void*)0x416b7a, TrialModeQuit_R); //Reset value and stuff properly when you Quit Trial Mode
WriteCall((void*)0x413368, DeleteCustomFlag); //Reset flags when you create a new savefile.
WriteCall((void*)0x42af3b, AddCustomFlag);

init_FlagsProgression();
Init_Subtitles();
Init_RandomCutscene();
Credits_StartupInit(path, helperFunctions);
Expand Down
4 changes: 2 additions & 2 deletions sadx-randomizer/Cutscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ void StartCutscene_r(int flag) {


bool CheckAndPlayRandomCutscene() {
if (cutsceneAllowedCount >= 2 && RNGCutscene)
if (cutsceneAllowedCount >= 2 && RNGCutscene && !CreditCheck)
{
MetalSonicFlag = 0;
SonicRand = 0;
Expand Down Expand Up @@ -351,7 +351,7 @@ void PlayRandomCutscene(long flag) {
DataPointer(int, CutsceneID, 0x3B2C570);

void CutsceneManager(ObjectMaster* obj) {
if (!IsIngame())
if (!IsIngame() || CreditCheck)
return;

EntityData1* data = obj->Data1;
Expand Down
8 changes: 5 additions & 3 deletions sadx-randomizer/E100-series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ void CheckAndHurtE100Bosses(ObjectMaster* obj) {
EntityData1* data1 = obj->Data1;
EntityData1* p1 = EntityData1Ptrs[0];

if (GetCollidingEntityA(data1) && p1->Status & Status_Attack && p1->CharID != Characters_Gamma && data1->Action > 0)
{
data1->Status |= Status_Hurt;
if (data1->Action > 0 && p1->CharID != Characters_Gamma) {
if (GetCollidingEntityA(data1) && (p1->Status & Status_Attack || p1->CharID == Characters_Tails && p1->Action == 20))
{
data1->Status |= Status_Hurt;
}
}
}

Expand Down
Loading

0 comments on commit e629e52

Please sign in to comment.