[USER MADE FIX] Calculation of turns to next social policy is slightly off

lockstep

Prince
Joined
Jan 15, 2004
Messages
399
Location
Vienna, Austria
Probably a contender for the most harmless bug in Civ 5, but here goes:

Civilization - any but France; game speed - standard; turn 0 (after founding first city). Number of turns to first social policy should be 25, but is shown as 26 in the policy screen.

View attachment nextpolicy_a.bmp

Civilization - France (+2 culture per city); game speed - standard; turn 0. Number of turns to first policy is correctly shown as 9.

View attachment nextpolicy_b.bmp

Civilization - France; game speed - quick; turn 0. Number of turns to first policy should be 5, but is shown as 6.

View attachment nextpolicy_c.bmp

It seems that the number of turns is calculated from policy costs that are too high by 1 (so the result is still correct for France at standard speed).
 
Patch version is 1.0.0.20. Enclosed are three save games to reproduce the bug - just hit F5 to open the social policy screen (or hover over the culture display in the main screen).
 

Attachments

  • egypt_standard_t0.Civ5Save
    321.1 KB · Views: 64
  • france_standard_t0.Civ5Save
    336.6 KB · Views: 72
  • france_quick_t0.Civ5Save
    384.5 KB · Views: 65
It seems I've got a bugfix. :D

The (wrong) calculation of turns can be found in TopPanel.lua:

iTurns = iCultureNeeded / pPlayer:GetTotalJONSCulturePerTurn();
iTurns = iTurns + 1;
iTurns = math.floor(iTurns);

Replace these lines with

iTurns = iCultureNeeded / pPlayer:GetTotalJONSCulturePerTurn();
iTurns = math.ceil(iTurns);

Reasoning: Increasing the number of turns by 1 and then calculating the floor will yield the wrong result if the number of turns was an integer to begin with. (I didn't know anything about lua, but I can search the net for "lua math.ceil".)

Corrected version of TopPanel.lua enclosed.

EDIT: SocialPolicyPopup.lua needs to be corrected as well.
 

Attachments

  • TopPanel.zip
    6.7 KB · Views: 77
  • SocialPolicyPopup.zip
    6 KB · Views: 74
Wow, that's a pretty elementary programming, nay - math - mistake. Turns for next (anything) should always be calculated using the ceiling. So weird they added a iTurns = iTurns + 1 "fix" and then used the floor without even thinking in their head what that would do. They could have used a iTurns = iTurns + 0.9999 or something. Pointless, but it would work.

Thanks for posting your correction.
 
Great that someone puts so much effort into Civ5 :goodjob:.

I hope the devs will include it in the next patch :).

Wow, that's a pretty elementary programming, nay - math - mistake. Turns for next (anything) should always be calculated using the ceiling. So weird they added a iTurns = iTurns + 1 "fix" and then used the floor without even thinking in their head what that would do

I guess that's probably due to the unusual index behavior in Lua.
In Lua, indexes begin at 1, in most other languages they begin with 0.
I think someones has been confused here and added the +1 to fix that shift.
 
Haha, what? My last win on Deity where I steamrolled everyone on my 2nd playthrough wasn't totally legit??

Actually I think this was just a straight misunderstanding of the floor/ceiling concept and of rounding in general. There are no lists or series involved here, where you would expect indexing errors like The_J mentioned to be introduced. These are just simple floating point numbers converted to integers. It's like the programmer knew of the floor function but not the ceil function and tried to introduce a workaround that would approximate the ceiling concept.
 
Actually I think this was just a straight misunderstanding of the floor/ceiling concept and of rounding in general. There are no lists or series involved here, where you would expect indexing errors like The_J mentioned to be introduced. These are just simple floating point numbers converted to integers. It's like the programmer knew of the floor function but not the ceil function and tried to introduce a workaround that would approximate the ceiling concept.

Even if lua did only include a floor function, there would still be an exact solution for ceilings:

iTurns = iCultureNeeded / pPlayer:GetTotalJONSCulturePerTurn();
iTurns = iTurns * -1;
iTurns = math.floor(iTurns);
iTurns = iTurns * -1;
 
Top Bottom