Skip to content

Commit

Permalink
Minor cleanup in the tour stop deserialization code
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed May 30, 2024
1 parent aa91ffb commit 0a60a0f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 90 deletions.
147 changes: 72 additions & 75 deletions src/common/WorldTourStop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,64 +451,61 @@ std::vector<bool> deserializeGMS(short gamemodeId, GameModeSettings& gmsSettings
} // namespace


TourStop * ParseTourStopLine(char * buffer, const Version& version, bool fIsWorld)
TourStop ParseTourStopLine(char* buffer, const Version& version, bool fIsWorld)
{
TourStop * ts = new TourStop();
ts->fUseSettings = false;
ts->iNumUsedSettings = 0;
TourStop ts;
ts.fUseSettings = false;
ts.iNumUsedSettings = 0;

char * pszTemp = strtok(buffer, ",\n");
char* pszTemp = strtok(buffer, ",\n");

short iStageType = 0;
ts.iStageType = 0;
if (fIsWorld) {
iStageType = atoi(pszTemp);
if (iStageType < 0 || iStageType > 1)
iStageType = 0;
ts.iStageType = atoi(pszTemp);
if (ts.iStageType < 0 || ts.iStageType > 1)
ts.iStageType = 0;

pszTemp = strtok(NULL, ",\n");
}

ts->iStageType = iStageType;
ts.szBonusText[0][0] = 0;
ts.szBonusText[1][0] = 0;
ts.szBonusText[2][0] = 0;
ts.szBonusText[3][0] = 0;
ts.szBonusText[4][0] = 0;

ts->szBonusText[0][0] = 0;
ts->szBonusText[1][0] = 0;
ts->szBonusText[2][0] = 0;
ts->szBonusText[3][0] = 0;
ts->szBonusText[4][0] = 0;

if (iStageType == 0) {
char * szMap = new char[strlen(pszTemp) + 1];
if (ts.iStageType == 0) {
char* szMap = new char[strlen(pszTemp) + 1];
strcpy(szMap, pszTemp);

pszTemp = strtok(NULL, ",\n");

if (pszTemp)
ts->iMode = atoi(pszTemp);
ts.iMode = atoi(pszTemp);
else
ts->iMode = -1;
ts.iMode = -1;

const bool isMinigame = ts.iMode == game_mode_pipe_minigame || ts.iMode == game_mode_boss_minigame || ts.iMode == game_mode_boxes_minigame;

//If this is 1.8.0.2 or earlier and we are playing a minigame, use the default map
if (version <= Version {1, 8, 0, 2} &&
(ts->iMode == game_mode_pipe_minigame || ts->iMode == game_mode_boss_minigame || ts->iMode == game_mode_boxes_minigame)) {
if (version <= Version {1, 8, 0, 2} && isMinigame) {
//Get a bogus map name so the mode will know to load the default map
ts->pszMapFile = maplist->GetUnknownMapName();
ts.pszMapFile = maplist->GetUnknownMapName();
} else {
//Using the maplist to cheat and find a map for us
maplist->SaveCurrent();

//If that map is not found
bool fMapFound = maplist->findexact(szMap, true);

if (!fMapFound) {
if (ts->iMode == game_mode_pipe_minigame || ts->iMode == game_mode_boss_minigame || ts->iMode == game_mode_boxes_minigame) {
if (isMinigame) {
//Get a bogus map name so the mode will know to load the default map
ts->pszMapFile = maplist->GetUnknownMapName();
ts.pszMapFile = maplist->GetUnknownMapName();
} else {
maplist->random(false);
ts->pszMapFile = maplist->currentShortmapname();
ts.pszMapFile = maplist->currentShortmapname();
}
} else {
ts->pszMapFile = maplist->currentShortmapname();
ts.pszMapFile = maplist->currentShortmapname();
}

maplist->ResumeCurrent();
Expand All @@ -519,46 +516,46 @@ TourStop * ParseTourStopLine(char * buffer, const Version& version, bool fIsWorl
//The pipe minigame was using the value 24 from version 1.8.0.0 to 1.8.0.2
//It was later switched to 1000 to accomodate new modes easily
if (version <= Version {1, 8, 0, 2}) {
if (ts->iMode == 24)
ts->iMode = game_mode_pipe_minigame;
if (ts.iMode == 24)
ts.iMode = game_mode_pipe_minigame;
}

//If a valid mode was not detected, then just choose a random mode
if (ts->iMode < 0 || (ts->iMode >= GAMEMODE_LAST && ts->iMode != game_mode_pipe_minigame && ts->iMode != game_mode_boss_minigame && ts->iMode != game_mode_boxes_minigame))
ts->iMode = RANDOM_INT(GAMEMODE_LAST);
if (ts.iMode < 0 || (ts.iMode >= GAMEMODE_LAST && !isMinigame))
ts.iMode = RANDOM_INT(GAMEMODE_LAST);

pszTemp = strtok(NULL, ",\n");

//This gets the closest game mode to what the tour has
ts->iGoal = -1;
ts.iGoal = -1;
if (pszTemp) {
//If it is commented out, this will allow things like 33 coins, 17 kill goals, etc.
//ts->iGoal = gamemodes[ts->iMode]->GetClosestGoal(atoi(pszTemp));
ts->iGoal = atoi(pszTemp);
//ts.iGoal = gamemodes[ts.iMode]->GetClosestGoal(atoi(pszTemp));
ts.iGoal = atoi(pszTemp);
}

//Default to a random goal if an invalid goal was used
if (ts->iGoal <= 0) {
if (ts->iMode < GAMEMODE_LAST)
ts->iGoal = gamemodes[ts->iMode]->GetOptions()[RANDOM_INT(GAMEMODE_NUM_OPTIONS - 1)].iValue;
if (ts.iGoal <= 0) {
if (ts.iMode < GAMEMODE_LAST)
ts.iGoal = gamemodes[ts.iMode]->GetOptions()[RANDOM_INT(GAMEMODE_NUM_OPTIONS - 1)].iValue;
else
ts->iGoal = 50;
ts.iGoal = 50;
}

if (version >= Version {1, 7, 0, 2}) {
pszTemp = strtok(NULL, ",\n");

//Read in point value for tour stop
if (pszTemp)
ts->iPoints = atoi(pszTemp);
ts.iPoints = atoi(pszTemp);
else
ts->iPoints = 1;
ts.iPoints = 1;

pszTemp = strtok(NULL, ",\n");

if (fIsWorld) {
ts->iBonusType = 0;
ts->iNumBonuses = 0;
ts.iBonusType = 0;
ts.iNumBonuses = 0;

char * pszStart = pszTemp;

Expand All @@ -574,9 +571,9 @@ TourStop * ParseTourStopLine(char * buffer, const Version& version, bool fIsWorl
else if (iWinnerPlace < 1 || iWinnerPlace > 4)
iWinnerPlace = 1;

strcpy(ts->wsbBonuses[ts->iNumBonuses].szBonusString, pszStart);
strcpy(ts.wsbBonuses[ts.iNumBonuses].szBonusString, pszStart);

ts->wsbBonuses[ts->iNumBonuses].iWinnerPlace = iWinnerPlace - 1;
ts.wsbBonuses[ts.iNumBonuses].iWinnerPlace = iWinnerPlace - 1;

short iPowerupOffset = 0;
if (pszStart[1] == 'w' || pszStart[1] == 'W')
Expand All @@ -588,9 +585,9 @@ TourStop * ParseTourStopLine(char * buffer, const Version& version, bool fIsWorl
if (iBonus < 0 || iBonus >= NUM_POWERUPS + NUM_WORLD_POWERUPS)
iBonus = 0;

ts->wsbBonuses[ts->iNumBonuses].iBonus = iBonus;
ts.wsbBonuses[ts.iNumBonuses].iBonus = iBonus;

if (++ts->iNumBonuses >= 10)
if (++ts.iNumBonuses >= 10)
break;

if (pszEnd)
Expand All @@ -600,23 +597,23 @@ TourStop * ParseTourStopLine(char * buffer, const Version& version, bool fIsWorl
}
} else {
if (pszTemp)
ts->iBonusType = atoi(pszTemp);
ts.iBonusType = atoi(pszTemp);
else
ts->iBonusType = 0;
ts.iBonusType = 0;
}

pszTemp = strtok(NULL, ",\n");

if (pszTemp) {
strncpy(ts->szName, pszTemp, 127);
ts->szName[127] = 0;
strncpy(ts.szName, pszTemp, 127);
ts.szName[127] = 0;
} else {
sprintf(ts->szName, "Tour Stop %d", game_values.tourstoptotal + 1);
sprintf(ts.szName, "Tour Stop %d", game_values.tourstoptotal + 1);
}
} else {
ts->iPoints = 1;
ts->iBonusType = 0;
sprintf(ts->szName, "Tour Stop %d", game_values.tourstoptotal + 1);
ts.iPoints = 1;
ts.iBonusType = 0;
sprintf(ts.szName, "Tour Stop %d", game_values.tourstoptotal + 1);
}

if (version >= Version {1, 8, 0, 0}) {
Expand All @@ -625,24 +622,24 @@ TourStop * ParseTourStopLine(char * buffer, const Version& version, bool fIsWorl
pszTemp = strtok(NULL, ",\n");

if (pszTemp)
ts->fEndStage = pszTemp[0] == '1';
ts.fEndStage = pszTemp[0] == '1';
else
ts->fEndStage = false;
ts.fEndStage = false;
}

//Copy in default values first
memcpy(&ts->gmsSettings, &game_values.gamemodemenusettings, sizeof(GameModeSettings));
memcpy(&ts.gmsSettings, &game_values.gamemodemenusettings, sizeof(GameModeSettings));

const std::vector<bool> usedSettings = deserializeGMS(ts->iMode, ts->gmsSettings);
ts->iNumUsedSettings = std::count(usedSettings.cbegin(), usedSettings.cend(), true);
ts->fUseSettings = !usedSettings.empty();
const std::vector<bool> usedSettings = deserializeGMS(ts.iMode, ts.gmsSettings);
ts.iNumUsedSettings = std::count(usedSettings.cbegin(), usedSettings.cend(), true);
ts.fUseSettings = !usedSettings.empty();
}
} else if (iStageType == 1) { //Bonus House
} else if (ts.iStageType == 1) { //Bonus House
if (pszTemp) {
strncpy(ts->szName, pszTemp, 127);
ts->szName[127] = 0;
strncpy(ts.szName, pszTemp, 127);
ts.szName[127] = 0;
} else {
sprintf(ts->szName, "Bonus House %d", game_values.tourstoptotal + 1);
sprintf(ts.szName, "Bonus House %d", game_values.tourstoptotal + 1);
}

pszTemp = strtok(NULL, ",\n");
Expand All @@ -651,31 +648,31 @@ TourStop * ParseTourStopLine(char * buffer, const Version& version, bool fIsWorl
if (iBonusOrdering < 0 || iBonusOrdering > 1)
iBonusOrdering = 0;

ts->iBonusType = iBonusOrdering;
ts.iBonusType = iBonusOrdering;

pszTemp = strtok(NULL, ",\n");

char * pszStart = pszTemp;

ts->iBonusTextLines = 0;
ts.iBonusTextLines = 0;
while (pszStart != NULL && pszStart[0] != '-') {
char * pszEnd = strstr(pszStart, "|");

if (pszEnd)
*pszEnd = 0;

strcpy(ts->szBonusText[ts->iBonusTextLines], pszStart);
strcpy(ts.szBonusText[ts.iBonusTextLines], pszStart);

if (++ts->iBonusTextLines >= 5 || !pszEnd)
if (++ts.iBonusTextLines >= 5 || !pszEnd)
break;

pszStart = pszEnd + 1;
}

ts->iNumBonuses = 0;
ts.iNumBonuses = 0;
pszTemp = strtok(NULL, ",\n");
while (pszTemp) {
strcpy(ts->wsbBonuses[ts->iNumBonuses].szBonusString, pszTemp);
strcpy(ts.wsbBonuses[ts.iNumBonuses].szBonusString, pszTemp);

short iPowerupOffset = 0;
if (pszTemp[0] == 'w' || pszTemp[0] == 'W')
Expand All @@ -689,10 +686,10 @@ TourStop * ParseTourStopLine(char * buffer, const Version& version, bool fIsWorl
if (iBonus < 0 || iBonus >= NUM_POWERUPS + NUM_WORLD_POWERUPS + NUM_WORLD_SCORE_BONUSES)
iBonus = 0;

ts->wsbBonuses[ts->iNumBonuses].iBonus = iBonus;
ts->wsbBonuses[ts->iNumBonuses].iWinnerPlace = -1;
ts.wsbBonuses[ts.iNumBonuses].iBonus = iBonus;
ts.wsbBonuses[ts.iNumBonuses].iWinnerPlace = -1;

if (++ts->iNumBonuses >= MAX_BONUS_CHESTS)
if (++ts.iNumBonuses >= MAX_BONUS_CHESTS)
break;

pszTemp = strtok(NULL, ",\n");
Expand Down
4 changes: 2 additions & 2 deletions src/common/WorldTourStop.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct WorldStageBonus {
};

struct TourStop {
const char * pszMapFile;
std::string pszMapFile;
short iMode;
short iGoal;
short iPoints;
Expand All @@ -36,6 +36,6 @@ struct TourStop {
};


TourStop* ParseTourStopLine(char* buffer, const Version& version, bool fIsWorld);
TourStop ParseTourStopLine(char* buffer, const Version& version, bool fIsWorld);
std::string WriteTourStopLine(const TourStop& ts, bool fIsWorld);
void ResetTourStops();
5 changes: 3 additions & 2 deletions src/smw/GSMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,7 @@ void MenuState::update()
if (game_values.gamemode->gamemode == game_mode_pipe_minigame ||
game_values.gamemode->gamemode == game_mode_boss_minigame ||
game_values.gamemode->gamemode == game_mode_boxes_minigame) {
fMiniGameMapFound = maplist->findexact(game_values.tourstops[game_values.tourstopcurrent]->pszMapFile, true);
fMiniGameMapFound = maplist->findexact(game_values.tourstops[game_values.tourstopcurrent]->pszMapFile.c_str(), true);

if (fMiniGameMapFound) {
g_map->loadMap(maplist->currentFilename(), read_type_full);
Expand Down Expand Up @@ -1502,7 +1502,8 @@ bool MenuState::ReadTourFile()
continue;
}

TourStop * ts = ParseTourStopLine(buffer, version, false);
TourStop* ts = new TourStop();
*ts = ParseTourStopLine(buffer, version, false);

game_values.tourstops.push_back(ts);
game_values.tourstoptotal++;
Expand Down
8 changes: 4 additions & 4 deletions src/smw/ui/MI_TourStop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ void MI_TourStop::Refresh(short iTourStop)
miPointsField->add(szTemp, 0, false);

if (tourstop->iMode == game_mode_pipe_minigame) {
bool fFound = miMapField->SetMap(tourstop->pszMapFile, true);
bool fFound = miMapField->SetMap(tourstop->pszMapFile.c_str(), true);

if (!fFound)
miMapField->SetSpecialMap("Pipe Minigame", "maps/special/two52_special_pipe_minigame.map");
} else if (tourstop->iMode == game_mode_boss_minigame) {
bool fFound = miMapField->SetMap(tourstop->pszMapFile, true);
bool fFound = miMapField->SetMap(tourstop->pszMapFile.c_str(), true);

if (!fFound) {
switch (tourstop->gmsSettings.boss.bosstype) {
Expand All @@ -184,12 +184,12 @@ void MI_TourStop::Refresh(short iTourStop)
}
}
} else if (tourstop->iMode == game_mode_boxes_minigame) {
bool fFound = miMapField->SetMap(tourstop->pszMapFile, true);
bool fFound = miMapField->SetMap(tourstop->pszMapFile.c_str(), true);

if (!fFound)
miMapField->SetSpecialMap("Boxes Minigame", "maps/special/two52_special_boxes_minigame.map");
} else {
miMapField->SetMap(tourstop->pszMapFile, true);
miMapField->SetMap(tourstop->pszMapFile.c_str(), true);
}

miTourStopMenuHeaderText->SetText(tourstop->szName);
Expand Down
3 changes: 2 additions & 1 deletion src/smw/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,8 @@ bool WorldMap::Load(short tilesize)

iReadType = iNumStages == 0 ? 12 : 11;
} else if (iReadType == 11) { //stage details
TourStop * ts = ParseTourStopLine(buffer, version, true);
TourStop* ts = new TourStop();
*ts = ParseTourStopLine(buffer, version, true);

game_values.tourstops.push_back(ts);
game_values.tourstoptotal++;
Expand Down
Loading

0 comments on commit 0a60a0f

Please sign in to comment.