I am encountering a terrible bug. Please see the code below.
Notice that lobbies and factions are defined in almost exactly the same way, and the syntax used for saving and loading them from the save file is likewise identical. However, upon loading the first byte from the line {fread(&factionlist[f]->AbbreviatedName, sizeof(char), CREATURE_NAMELEN, h);}, the game immediately crashes. I cannot understand how this could be crashing.
Does anybody else have any idea of what could be going on here???
In Includes.h
class Lobby
{
public:
char LobbyName[CREATURE_NAMELEN];
int LobbyCreatures[CREATURENUM];
int LobbyLaws[LAWNUM];
int spectrum;
int alignment_max_liberal;
int alignment_max_conservative;
int alignment_current;
int influence;
};
...
class FactionMember
{
public:
char Name[CREATURE_NAMELEN];
int alive;
int gender;
int competence;
int stylemoviegenre;
};
class Faction
{
public:
char AbbreviatedName[CREATURE_NAMELEN];
char FullName[CREATURE_NAMELEN];
int FriendList[FACTIONALLIANCENUM];
int NeutralList[FACTIONALLIANCENUM];
int EnemyList[FACTIONALLIANCENUM];
int FactionAlignments[FACTIONALIGNNUM];
int RelevantLaws[LAWNUM];
int MaxGovernmentLiberalLaws[LAWNUM];
int MaxGovernmentConservativeLaws[LAWNUM];
int RelevantViews[VIEWNUM];
int RelevantLocations[SITENUM];
int RelevantCrimes[LAWFLAGNUM];
int RelevantGenres[GENRENUM];
int RelevantProtagonists[VIEWNUM];
int RelevantSettings[VIEWNUM];
int RelevantClubs[ACTIVITYNUM];
int healthmax;
int healthcurrent;
int governmentfunded; //1 for yes, 0 for no
int moviefunded; //-2 for conservative or Stalinist; -1 for conservative; 0 for no; 1 for liberal; 2 for liberal or Stalinist
int privatefunded; //1 for yes, 0 for no
int fundscurrent;
int basestrength;
int fundingrelevance;
int baseaggressiveness;
vector<FactionMember *> Members;
};
in saveload.c:
//Lobbies
int lobbynumber = lobbies.size();
fwrite(&lobbynumber, sizeof(int), 1, h);
for (int l = 0; l < lobbynumber; l++)
{
fwrite(&lobbies[l]->LobbyName, sizeof(char), CREATURE_NAMELEN, h);
fwrite(&lobbies[l]->LobbyCreatures, sizeof(int), CREATURENUM, h);
fwrite(&lobbies[l]->LobbyLaws, sizeof(int), LAWNUM, h);
fwrite(&lobbies[l]->spectrum, sizeof(int), 1, h);
fwrite(&lobbies[l]->alignment_max_liberal, sizeof(int), 1, h);
fwrite(&lobbies[l]->alignment_max_conservative, sizeof(int), 1, h);
fwrite(&lobbies[l]->alignment_current, sizeof(int), 1, h);
fwrite(&lobbies[l]->influence, sizeof(int), 1, h);
}
//Factions
fwrite(&global_violence_counter, sizeof(int), 1, h); //Number of violent events in the news related to politics
int factiontotal = factionlist.size();
fwrite(&factiontotal, sizeof(int), 1, h); //Number of factions
for (int f = 0; f < factiontotal; f++)
{
fwrite(&factionlist[f]->AbbreviatedName, sizeof(char), CREATURE_NAMELEN, h);
fwrite(&factionlist[f]->FullName, sizeof(char), CREATURE_NAMELEN, h);
fwrite(&factionlist[f]->FriendList, sizeof(int), FACTIONALLIANCENUM, h);
fwrite(&factionlist[f]->NeutralList, sizeof(int), FACTIONALLIANCENUM, h);
fwrite(&factionlist[f]->EnemyList, sizeof(int), FACTIONALLIANCENUM, h);
fwrite(&factionlist[f]->FactionAlignments, sizeof(int), FACTIONALIGNNUM, h);
fwrite(&factionlist[f]->RelevantLaws, sizeof(int), LAWNUM, h);
fwrite(&factionlist[f]->MaxGovernmentLiberalLaws, sizeof(int), LAWNUM, h);
fwrite(&factionlist[f]->MaxGovernmentConservativeLaws, sizeof(int), LAWNUM, h);
fwrite(&factionlist[f]->RelevantViews, sizeof(int), VIEWNUM, h);
fwrite(&factionlist[f]->RelevantLocations, sizeof(int), SITENUM, h);
fwrite(&factionlist[f]->RelevantCrimes, sizeof(int), LAWFLAGNUM, h);
fwrite(&factionlist[f]->RelevantGenres, sizeof(int), GENRENUM, h);
fwrite(&factionlist[f]->RelevantProtagonists, sizeof(int), VIEWNUM, h);
fwrite(&factionlist[f]->RelevantSettings, sizeof(int), VIEWNUM, h);
fwrite(&factionlist[f]->RelevantClubs, sizeof(int), ACTIVITYNUM, h);
fwrite(&factionlist[f]->healthmax, sizeof(int), 1, h);
fwrite(&factionlist[f]->healthcurrent, sizeof(int), 1, h);
fwrite(&factionlist[f]->governmentfunded, sizeof(int), 1, h);
fwrite(&factionlist[f]->moviefunded, sizeof(int), 1, h);
fwrite(&factionlist[f]->privatefunded, sizeof(int), 1, h);
fwrite(&factionlist[f]->fundscurrent, sizeof(int), 1, h);
fwrite(&factionlist[f]->basestrength, sizeof(int), 1, h);
fwrite(&factionlist[f]->fundingrelevance, sizeof(int), 1, h);
fwrite(&factionlist[f]->baseaggressiveness, sizeof(int), 1, h);
int factionmembers = factionlist[f]->Members.size();
fwrite(&factionmembers, sizeof(int), 1, h);
for (int m = 0; m < factionmembers; m++)
{
fwrite(&factionlist[f]->Members[m]->Name, sizeof(char), CREATURE_NAMELEN, h);
fwrite(&factionlist[f]->Members[m]->gender, sizeof(int), 1, h);
fwrite(&factionlist[f]->Members[m]->competence, sizeof(int), 1, h);
fwrite(&factionlist[f]->Members[m]->stylemoviegenre, sizeof(int), 1, h);
}
}
...
//Lobbies
fread(&dummy, sizeof(int), 1, h);
lobbies.resize(dummy);
for (int l = 0; l < len(lobbies); l++)
{
lobbies[l] = new Lobby;
fread(&lobbies[l]->LobbyName, sizeof(char), CREATURE_NAMELEN, h);
fread(&lobbies[l]->LobbyCreatures, sizeof(int), CREATURENUM, h);
fread(&lobbies[l]->LobbyLaws, sizeof(int), LAWNUM, h);
fread(&lobbies[l]->spectrum, sizeof(int), 1, h);
fread(&lobbies[l]->alignment_max_liberal, sizeof(int), 1, h);
fread(&lobbies[l]->alignment_max_conservative, sizeof(int), 1, h);
fread(&lobbies[l]->alignment_current, sizeof(int), 1, h);
fread(&lobbies[l]->influence, sizeof(int), 1, h);
}
//Factions
fread(&global_violence_counter, sizeof(int), 1, h); //global violence counter
fread(&dummy, sizeof(int), 1, h); //Number of factions
factionlist.resize(dummy);
for (int f = 0; f < len(factionlist); f++)
{
factionlist[f] = new Faction;
fread(&factionlist[f]->AbbreviatedName, sizeof(char), CREATURE_NAMELEN, h);
fread(&factionlist[f]->FullName, sizeof(char), CREATURE_NAMELEN, h);
fread(&factionlist[f]->FriendList, sizeof(int), FACTIONALLIANCENUM, h);
fread(&factionlist[f]->NeutralList, sizeof(int), FACTIONALLIANCENUM, h);
fread(&factionlist[f]->EnemyList, sizeof(int), FACTIONALLIANCENUM, h);
fread(&factionlist[f]->FactionAlignments, sizeof(int), FACTIONALIGNNUM, h);
fread(&factionlist[f]->RelevantLaws, sizeof(int), LAWNUM, h);
fread(&factionlist[f]->MaxGovernmentLiberalLaws, sizeof(int), LAWNUM, h);
fread(&factionlist[f]->MaxGovernmentConservativeLaws, sizeof(int), LAWNUM, h);
fread(&factionlist[f]->RelevantViews, sizeof(int), VIEWNUM, h);
fread(&factionlist[f]->RelevantLocations, sizeof(int), SITENUM, h);
fread(&factionlist[f]->RelevantCrimes, sizeof(int), LAWFLAGNUM, h);
fread(&factionlist[f]->RelevantGenres, sizeof(int), GENRENUM, h);
fread(&factionlist[f]->RelevantProtagonists, sizeof(int), VIEWNUM, h);
fread(&factionlist[f]->RelevantSettings, sizeof(int), VIEWNUM, h);
fread(&factionlist[f]->RelevantClubs, sizeof(int), ACTIVITYNUM, h);
fread(&factionlist[f]->healthmax, sizeof(int), 1, h);
fread(&factionlist[f]->healthcurrent, sizeof(int), 1, h);
fread(&factionlist[f]->governmentfunded, sizeof(int), 1, h);
fread(&factionlist[f]->moviefunded, sizeof(int), 1, h);
fread(&factionlist[f]->privatefunded, sizeof(int), 1, h);
fread(&factionlist[f]->fundscurrent, sizeof(int), 1, h);
fread(&factionlist[f]->basestrength, sizeof(int), 1, h);
fread(&factionlist[f]->fundingrelevance, sizeof(int), 1, h);
fread(&factionlist[f]->baseaggressiveness, sizeof(int), 1, h);
fread(&dummy, sizeof(int), 1, h); //faction members
factionlist[f]->Members.resize(dummy);
for (int m = 0; m < factionlist[f]->Members.size(); m++)
{
fread(&factionlist[f]->Members[m]->Name, sizeof(char), CREATURE_NAMELEN, h);
fread(&factionlist[f]->Members[m]->gender, sizeof(int), 1, h);
fread(&factionlist[f]->Members[m]->competence, sizeof(int), 1, h);
fread(&factionlist[f]->Members[m]->stylemoviegenre, sizeof(int), 1, h);
}
}