Function returning 0, I can't find out why.

Putmalk

Deity
Joined
Sep 26, 2010
Messages
2,652
Location
New York
Hi, this really shouldn't be returning 0, yet it is.

circumstance: GetPlayer() is the human here, eOtherPlayer is the AI.

Code:
/// Added by Putmalk - Version Three
// How must is a technology worth?
int CvDealAI::GetTechValue(TechTypes eTech, bool bFromMe, PlayerTypes eOtherPlayer, bool bUseEvenValue)
{
	int iItemValue = 0;

	int iTheyResearchedLeft = GET_TEAM( GET_PLAYER(eOtherPlayer).getTeam() ).GetTeamTechs()->GetResearchLeft(eTech);
	int iNumTechsTheyHave = GET_TEAM( GET_PLAYER(eOtherPlayer).getTeam() ).GetTeamTechs()->GetNumTechsKnown();
	int iNumTechsWeHave = GET_TEAM( GetPlayer()->getTeam() ).GetTeamTechs()->GetNumTechsKnown();
	int iEraModifier;

	// don't divide by zero, please
	if(iNumTechsWeHave == 0)
		iNumTechsWeHave = 1;
	if(iNumTechsTheyHave == 0)	
		iNumTechsTheyHave = 1;

	iItemValue = iTheyResearchedLeft;
	int iAdditionalValue = ((iNumTechsTheyHave * 100)/iNumTechsWeHave);
	if(iAdditionalValue == 0)
		iAdditionalValue = 1;
	iItemValue *= iAdditionalValue;
	iItemValue /= 100;

	// Modify formula based on era of tech
	EraTypes eMyEra = GET_TEAM(GetPlayer()->getTeam()).GetCurrentEra();
	CvTechEntry* pkTechInfo = GC.getTechInfo(eTech);
	
	int iTechEra = pkTechInfo->GetEra();

	iAdditionalValue = iItemValue * max(0,(int)(eMyEra - iTechEra));
	iItemValue += iAdditionalValue;


	// If multiple people have this tech its value decreased
	//fTechModifier *= ( (float) GC.getGame().countKnownTechNumTeams(eTech) / (float) GC.getGame().countCivPlayersEverAlive() );
	//fTechModifier *= 100;

	//if(fTechModifier = 0)
	//{
	//	fTechModifier = 100;
	//}

	//iItemValue *= (int) fTechModifier;
	//iItemValue /= 100;

	if(bFromMe)
	{
		// Approach is important
		switch(GetPlayer()->GetDiplomacyAI()->GetMajorCivApproach(eOtherPlayer, /*bHideTrueFeelings*/ true))
		{
			case MAJOR_CIV_APPROACH_HOSTILE:
				iItemValue *= 250;
				break;
			case MAJOR_CIV_APPROACH_GUARDED:
				iItemValue *= 130;
				break;
			case MAJOR_CIV_APPROACH_AFRAID:
				iItemValue *= 80;
				break;
			case MAJOR_CIV_APPROACH_FRIENDLY:
				iItemValue *= 100;
				break;
			case MAJOR_CIV_APPROACH_NEUTRAL:
				iItemValue *= 100;
				break;
			default:
				CvAssertMsg(false, "DEAL_AI: AI player has no valid Approach for Technology valuation.  Please send Jon this with your last 5 autosaves and what changelist # you're playing.")
				iItemValue *= 100;
				break;
		}
		iItemValue /= 100;
	}

	// Are we trying to find the middle point between what we think this item is worth and what another player thinks it's worth?
	if(bUseEvenValue)
	{
		iItemValue += GET_PLAYER(eOtherPlayer).GetDealAI()->GetTradeAgreementValue(!bFromMe, GetPlayer()->GetID(), /*bUseEvenValue*/ false);

		iItemValue /= 2;
	}

	return iItemValue;
}

Maybe I'm missing something there. Another pair of eyes could be useful.

Thank you.
 
Hey, I've been out of the c++ game for a while and am just circling back and working on some DLL stuff, but... dividing an int - can't that round to 0? (iItemValue /= 100)

You're right. I totally see my error. Thanks buddy!

iAdditionalValue is becoming 0, multiplied by iItemValue will be 0.

edit: Hang on, I multiplied by 100. ...it shouldn't be less than 1...even so, I added a check for that. Something else is wrong.
 
The error is here somewhere:

Code:
EraTypes eMyEra = GET_TEAM(GetPlayer()->getTeam()).GetCurrentEra();
	CvTechEntry* pkTechInfo = GC.getTechInfo(eTech);
	
	int iTechEra = pkTechInfo->GetEra();

	iAdditionalValue = iItemValue * max(0,(int)(eMyEra - iTechEra));
	iItemValue += iAdditionalValue;

How I know: I commented this out and everything works.
 
Out of curiosity why are you casting the subtraction of two ints to int? That shouldn't be the issue though

Just being safe. EraTypes is an enum and I don't want anything getting screwy. And I've changed it. I decided to do (int)(EraTypes - EraTypes) just like the research agreement code.

Well here's something. Pottery, Animal Husbandry, Archery, and Mining are costing 31 Science. The AI at Industrial values them at 0.

Sailing is 48, the AI values them at 30.

Okay, I guess that's not bad. I'm not seeing an issue here atm. I guess it's fixed...

Updated code...

Code:
/// Added by Putmalk - Version Three
// How must is a technology worth?
int CvDealAI::GetTechValue(TechTypes eTech, bool bFromMe, PlayerTypes eOtherPlayer, bool bUseEvenValue)
{
	int iItemValue = 0;

	int iTheyResearchedLeft = GET_TEAM( GET_PLAYER(eOtherPlayer).getTeam() ).GetTeamTechs()->GetResearchLeft(eTech);
	int iNumTechsTheyHave = GET_TEAM( GET_PLAYER(eOtherPlayer).getTeam() ).GetTeamTechs()->GetNumTechsKnown();
	int iNumTechsWeHave = GET_TEAM( GetPlayer()->getTeam() ).GetTeamTechs()->GetNumTechsKnown();
	int iEraModifier;

	// don't divide by zero, please
	if(iNumTechsWeHave == 0)
		iNumTechsWeHave = 1;
	if(iNumTechsTheyHave == 0)	
		iNumTechsTheyHave = 1;

	iItemValue = iTheyResearchedLeft;
	
	int iAdditionalValue = ((iNumTechsTheyHave * 100)/iNumTechsWeHave);
	if(iAdditionalValue == 0)
		iAdditionalValue = 1;
	iItemValue *= iAdditionalValue;
	iItemValue /= 100;

	// Modify formula based on era of tech
	EraTypes eMyEra = GET_TEAM(GetPlayer()->getTeam()).GetCurrentEra();
	CvTechEntry* pkTechInfo = GC.getTechInfo(eTech);
	
	int iTechEra = pkTechInfo->GetEra();

	iAdditionalValue = iItemValue * max(0,(int)(eMyEra - (EraTypes) iTechEra));
	iItemValue += iAdditionalValue;


	// If multiple people have this tech its value decreased
	//fTechModifier *= ( (float) GC.getGame().countKnownTechNumTeams(eTech) / (float) GC.getGame().countCivPlayersEverAlive() );
	//fTechModifier *= 100;

	//if(fTechModifier = 0)
	//{
	//	fTechModifier = 100;
	//}

	//iItemValue *= (int) fTechModifier;
	//iItemValue /= 100;

	if(bFromMe)
	{
		// Approach is important
		switch(GetPlayer()->GetDiplomacyAI()->GetMajorCivApproach(eOtherPlayer, /*bHideTrueFeelings*/ true))
		{
			case MAJOR_CIV_APPROACH_HOSTILE:
				iItemValue *= 250;
				break;
			case MAJOR_CIV_APPROACH_GUARDED:
				iItemValue *= 130;
				break;
			case MAJOR_CIV_APPROACH_AFRAID:
				iItemValue *= 80;
				break;
			case MAJOR_CIV_APPROACH_FRIENDLY:
				iItemValue *= 100;
				break;
			case MAJOR_CIV_APPROACH_NEUTRAL:
				iItemValue *= 100;
				break;
			default:
				CvAssertMsg(false, "DEAL_AI: AI player has no valid Approach for Technology valuation.  Please send Jon this with your last 5 autosaves and what changelist # you're playing.")
				iItemValue *= 100;
				break;
		}
		iItemValue /= 100;
	}

	// Are we trying to find the middle point between what we think this item is worth and what another player thinks it's worth?
	if(bUseEvenValue)
	{
		iItemValue += GET_PLAYER(eOtherPlayer).GetDealAI()->GetTradeAgreementValue(!bFromMe, GetPlayer()->GetID(), /*bUseEvenValue*/ false);

		iItemValue /= 2;
	}

	return iItemValue;
}
 
http://imgur.com/a/Cuhz5

Washington doesn't think much of Theology but likes Guilds.

The possibility exists Washington is almost done researching Theology, but I have no real way of knowing.

I'll have to run another playtest to see if this is an issue.
 
Found my bug! Look at my code again. You'll see it after you hit "// Are we trying to find the middle point between what we think this item is worth and what another player thinks it's worth?"
 
Back
Top Bottom