[DLL] Building Deletion Bug

Joined
Jul 31, 2014
Messages
836
Hi all,


Background: I implemented a function to give a building X yields of faith dependent on the number of mountains, with the code in CvPlayer.cpp looking something like

Spoiler :

Code:
#ifdef Imperial_Num_Nearby_Mountain										
						int iNumNearbyMountain = 0;
						int FaithPerMountain = 0;
						int FaithFromMountains = 0;
						iNumNearbyMountain = pLoopCity->GetNumMountain();
#endif
#ifdef Imperial_NearbyMountainYield			
						FaithPerMountain = pBuildingInfo->FaithPerNearbyMountain();
						FaithFromMountains = FaithPerMountain * iNumNearbyMountain;
						Imperial_Logging(iNumNearbyMountain, FaithPerMountain); // logging.						
						pLoopCity->ChangeFaithPerTurnFromBuildings(FaithFromMountains);
#endif


However, the issue I'm finding is that when I sell the building back, the game would act as if I had just added an additional building (i.e. the yield bonus would increase), and this could be repeated (theoretically, I only got my faith bonus to 1,000~ or so before I got bored) an infinite number of times.

As the custom logging file I set up showed the number of mountains detected around the city to be constant (x5), I am wondering if there was something like a delete-cache function that I forgot to implement?
 
As you've discovered ChangeFaithPerTurnFromBuildings() is cumulative. No there is no cache of values, you'll need to implement your own (which will require persisting data). You'll need to track both the number of buildings in a city giving faith from mountains and the number of mountains around the city - both can decrease as well as increase (the former if a building is sold or destroyed during razing, the latter from a culture bomb). When either change (up or down) you'll need to calculate the delta from the previous stored/cached values and call ChangeFaithPerTurnFromBuildings() with an appropriate positive or negative value.

Edit: Another way to approach this would be to implement a <Building_PlotYieldChanges> table. All the code for this table already exists as part of the Unified Yields project
 
Ah, I see (so that's why you guys went with that approach in your UY proj :lol:). Thanks!

I wonder however, does
Code:
pLoopCity->ChangeBaseYieldRateFromBuildings

work properly for yield faith? Seemed strange at first glance that it would be separated like this (although I can understand for culture since it also needs to feed into city plot acquisition algorithms).
 
I wonder however, does
Code:
pLoopCity->ChangeBaseYieldRateFromBuildings
work properly for yield faith?
Probably not

Seemed strange at first glance that it would be separated like this (although I can understand for culture since it also needs to feed into city plot acquisition algorithms).
Nothing to do with plot acquisition ... in the beginning there were four yields - Food, Production, Gold and Science. Culture was a nasty hack (you can work out by whom as they left their name all over the culture API) with dedicated columns in some tables. Then it was hacked even more to make it look like a yield for G&K - but it was never fully integrated, just what Firaxis needed for the base game. Then came Faith, which is a complete rehack of Culture - with some dedicated columns and the same partially implemented pseudo-yield code.

While the base four yields are pretty easy to follow in the code, culture and faith are true spaghetti.

Have fun!
 
Thankyou for the insight Whoward, it is much appreciated.

Looks like my issues were ultimately caused by sloppy coding/understanding of the structures: not inserting a break; statement (inside an additional conditional struct for the entire thing - this seems to prevent all buildings from the faithfromnearbymountains from being cumulative for each and every building in the city), along with not multiplying the number passed to ChangeFaithPerTurnFromBuildings with the BuildingCount and iChange integers inside CvPlayer:: processBuilding.

Time to find out what I'll trip over next :lol:.
 
Back
Top Bottom