Turning a whole number into a decimal

ripple01

Emperor
Joined
Mar 7, 2006
Messages
1,254
Location
New York City
Hi all,

Could someone tell me how hard it would be to take a value in the game that is a whole number (I am looking at the health and happiness values) and change it into a decimal (so I could have a level of 5.5 happiness in a city, for example.)

I know this would be done in the SDK, I just have no idea if this is something relatively simple or quite difficult.

Cheers,
ripple01
 
Mostly it is difficult to get things to work nicely in all the areas where it re-combines with other integer values. Happiness in cities is used in a LOT of areas, so you have to make minor changes in MANY areas of the code.

If you wanted to see a way to approach it, in Fall Further we have made Experience into a float (one of the code terms for a decimal, the other would be a double, which is like a float, but smaller. The same difference between an Int and a Short really). We also made happiness from features into a float, food consumed per population, and I think happiness from garrisoned units (though we only use it as an integer now anyway). I also have added quite a few float fields which affect existing integer fields. So if you download the mod (source is included with it) you can check out how to change a few existing INT into Float, and how to have Float interact with INT.
 
Something to consider if you choose to do this is whether to use a float or an int to hold your value. The game uses ints to hold values "times 100" in order to support two decimal places without float-representation and rounding issues. This ensures that 3.14 + 2.28 = 5.42 instead of 5.43.

It requires more work as you need to ensure you divide by 100 when displaying it as a decimal or comparing it to other ints that aren't handled this way.

You can see examples of this in CvCity::getCommerceRateTimes100().

One advantage to this I just thought of but haven't thought completely through is that you could leave the original functions and add new ones for the Times100 values. You'd have to change the code that handles the old functions, but any code that calls them should still continue to work since happyLevel() would continue to return 5 when happyLevelTimes100() returns 550 (for 5.5 in your example).
 
Top Bottom