Why isn't this becoming false?

phungus420

Deity
Joined
Mar 1, 2003
Messages
6,296
I've been trying to fix an introduced bug caused by the new limited religions game option. Basically founding religions in an advanced start game isn't working correctly, so I wrote a new function to deal with founding religions in the first few turns of a game when a custom game is set to start up after players already have some the techs to found some religions. Debugging it, I'm getting this really odd behavior I do not understand. Here is the full code of the function:
Spoiler :
Code:
void CvGame::doHolyCityGameStart()
{
	int iLoopTeam;
	int iLoopReligion;
	int iLoopPlayer;
	TeamTypes eLoopTeam;
	ReligionTypes eLoopReligion;
	PlayerTypes eLoopPlayer;
	int iLotteryIncrement = MAX_TEAMS * GC.getNumReligionInfos();


	bool *pb_ReligionCanFoundNow = new bool[GC.getNumReligionInfos()];
	for(iLoopReligion = 0; iLoopReligion < GC.getNumReligionInfos(); ++iLoopReligion)
	{
		pb_ReligionCanFoundNow[iLoopReligion] = false;

		eLoopReligion = ReligionTypes(iLoopReligion);
		if(eLoopReligion != NO_RELIGION)
		{
			CvReligionInfo& kLoopReligion = GC.getReligionInfo(eLoopReligion);

			for(iLoopTeam = 0; iLoopTeam < MAX_TEAMS; iLoopTeam++)
			{
				eLoopTeam = TeamTypes(iLoopTeam);
				if(eLoopTeam != NO_TEAM)
				{
					CvTeam& kLoopTeam = GET_TEAM(eLoopTeam);
					if(kLoopTeam.isHasTech(TechTypes(kLoopReligion.getTechPrereq())))
					{
						if(!(isReligionSlotTaken(eLoopReligion)))
						{
							pb_ReligionCanFoundNow[eLoopReligion] = true;
							break;
						}
					}
				}
			}
		}
	}


	bool *pb_teamCanFound = new bool[MAX_TEAMS];
	for(iLoopTeam = 0; iLoopTeam < MAX_TEAMS; ++iLoopTeam)
	{
		bool bValid = false;
		eLoopTeam = TeamTypes(iLoopTeam);
		if(eLoopTeam != NO_TEAM && eLoopTeam != BARBARIAN_TEAM)
		{
			CvTeam& kLoopTeam = GET_TEAM(eLoopTeam);

			if(kLoopTeam.isAlive())
			{

				for (iLoopPlayer = 0; iLoopPlayer < MAX_PLAYERS; iLoopPlayer++)
				{
					eLoopPlayer = PlayerTypes(iLoopPlayer);
					if(eLoopPlayer != NO_PLAYER && eLoopPlayer != BARBARIAN_PLAYER)
					{
						CvPlayer& kLoopPlayer = GET_PLAYER(eLoopPlayer);

						if(kLoopPlayer.getTeam() == eLoopTeam && kLoopPlayer.isAlive())
						{
							FAssert(kLoopPlayer.isPlayable());
							if(kLoopPlayer.canFoundReligion())
							{
								pb_teamCanFound[eLoopTeam] = true;
								bValid = true;
								break;
							}
						}
					}
				}
				if(bValid)
				{
					continue;
				}
			}
		}

		pb_teamCanFound[eLoopTeam] = false;
	}


	int iTeamsLeft = countCivTeamsAlive();

	for(iLoopReligion = 0; iLoopReligion < GC.getNumReligionInfos(); ++iLoopReligion)
	{
		if(iTeamsLeft == 0)
		{
			break;
		}

		eLoopReligion = ReligionTypes(iLoopReligion); 
		int iScore = MAX_INT;
		int iTempScore;
		TeamTypes eBestTeam = NO_TEAM;

		if(eLoopReligion != NO_RELIGION)
		{
			CvReligionInfo& kLoopReligion = GC.getReligionInfo(eLoopReligion);

			if(pb_ReligionCanFoundNow[eLoopReligion])
			{

				for(iLoopTeam = 0; iLoopTeam < MAX_TEAMS; iLoopTeam++)
				{
					eLoopTeam = TeamTypes(iLoopTeam);
					if(eLoopTeam != NO_TEAM && eLoopTeam != BARBARIAN_TEAM)
					{
						CvTeam& kLoopTeam = GET_TEAM(eLoopTeam);

						if( pb_teamCanFound[eLoopTeam] && kLoopTeam.isHasTech(TechTypes(kLoopReligion.getTechPrereq())) )
						{
							iTempScore = getSorenRandNum(iLotteryIncrement, "Early Religion Lottery");
							for (iLoopPlayer = 0; iLoopPlayer < MAX_PLAYERS; iLoopPlayer++)
							{
								eLoopPlayer = PlayerTypes(iLoopPlayer);
								if(eLoopPlayer != NO_PLAYER)
								{
									CvPlayer& kLoopPlayer = GET_PLAYER(eLoopPlayer);

									if(kLoopPlayer.getTeam() == eLoopTeam)
									{
										int iILoopReligion;
										for(iILoopReligion = 0; iILoopReligion < GC.getNumReligionInfos(); iILoopReligion++)
										{
											if(kLoopPlayer.hasHolyCity(ReligionTypes(iILoopReligion)))
											{
												iTempScore += iLotteryIncrement;
											}
										}
									}
								}
							}
							if( pb_teamCanFound[eLoopTeam])
							{
								bool Stop = true;
							}

							if(iTempScore < iScore)
							{
								iScore = iTempScore;
								eBestTeam = eLoopTeam;
							}
						}
					}
				}

				if(eBestTeam != NO_TEAM && eBestTeam != BARBARIAN_TEAM)
				{
					pb_teamCanFound[eBestTeam] = false;
					bool TestVariable;
					TestVariable = pb_teamCanFound[eBestTeam];
					doHolyCityGameStartPlayer(eBestTeam, eLoopReligion);
					iTeamsLeft--;
				}
				else
				{
					bool NotGood = true;
				}
			}
		}
	}


	if(pb_ReligionCanFoundNow)
	{
		delete [] pb_ReligionCanFoundNow;
	}

	if(pb_teamCanFound)
	{
		delete [] pb_teamCanFound;
	}
}

The thing I don't understand is here, I threw in some debugging code so I could see what's going on:
Code:
				if(eBestTeam != NO_TEAM && eBestTeam != BARBARIAN_TEAM)
				{
					pb_teamCanFound[eBestTeam] = false;
					bool TestVariable;
					TestVariable = pb_teamCanFound[eBestTeam];
					doHolyCityGameStartPlayer(eBestTeam, eLoopReligion);
					iTeamsLeft--;
				}

TestVariable is true. WTF? How, I'm telling pb_teamCanFound[eBestTeam] to false, yet it stays true. Why, and how is this possible? It doesn't make any sense to me, what am I missing here?
 
I don't know what was up, but I just deleted it and retyped it and it worked :dunno:
 
:lol: I've had that happen on a few occasions myself.
 
I don't know what was up, but I just deleted it and retyped it and it worked :dunno:

Obviously that "e" should have been an "e". And if you replace that "if" with an "if" it will run over 9,000 times faster! Aren't computers awesome? :rolleyes:
 
This was actually really frustrating, and I still have no clue what was going on. Tried a bunch of things and that bTestVariable kept being true in the call stack when running the debugger. Finally just deleted the portion of code that sets that point in the array to false, and retyped it, and it worked... Wasn't the problem though, to be honest I still don't know what the problem was, but I just added a !isBarbarian() check to the function CvPlayer::canFoundReligion that I added for the limited religions game option, and that worked. What's wierd about that though, is that while debugging I tried to catch the barbarian civ founding a religion, but could never find where it was happening... but it wasn't from this added function. Really confused me, what I thought would be something relatively simple to code took me many more hours of debugging to get to work, and I still don't know why. I guess that's just how it goes sometimes though.
 
I was experiencing something similar yesterday and it consumed much of my day. A class instance was registering as "None type" at one key juncture. I never did figure out why or how that could be, but I ended up using a completely different approach (with lesser functionality) - I just didn't have time to figure it out... (This was Python by the way, not C++.)
 
Top Bottom