Quantcast
Channel: Hacker News
Viewing all articles
Browse latest Browse all 25817

Mathematics of Civilization V

$
0
0

BackArrowGreenBack to game concepts

This article presents some of the mathematics of Civilization V. It was originally published by alpaca at the CivFanatics forum. Notice that this article represents the state of the game before the December 2010 patch!

Updated on 6/13/2015: Unit Maintenance updated to reflect the actual formula in the DLL - monju125

Calculating CultureEdit

Border growthEdit

The cost of a border growth is, with t the number of tiles already claimed

20 + (10 (t - 1))^{1.1}

Formula for Social PoliciesEdit

The first thing we need to know when talking about anything culture-wise is how to calculate it. From the game core XML files and in-game observations, it works like this: You calculate a base cost that depends on the number of policies, then multiply with a factor that depends on your number of cities. As follows:

p_b(k): The base cost of policy k. Depends only on the number of policies

n: The number of cities in your empire

p_c(n): The cost factor depending on the number of cities in your empire

p(n,k): The total cost of policy k. Depends on your number of cities and policies already unlocked

The policy cost has to be modified depending on your difficulty setting, game speed and map size. I took the numbers from a post by Yamian. Let

p_m: The map size modifier (0.3 for normal size and below, 0.2 for large, 0.15 for huge)

p_d: The map difficulty coefficient (0.5 for settler, 0.67 for chieftain, 0.85 for warlord, 1 otherwise)

p_s: The game speed modifier (3 for marathon, 1.5 for epic, 1 for normal and 0.67 for quick)

p_b(k) = p_s p_d (25 + (6 k)^{1.7}) = p_s p_d (25 + 21.03k^{1.7})

p_c(n) = 1 + p_m (n - 1)

The policy cost scales linearly with the number of cities and something between linear and quadratic with the number of policies.

p(n,k) = p_b(k) p_c(n) rounded to the next multiple of 5

Cost for each policy on standard settings
k=0123456789101112131415161718192021222324252627282930
n=1254590160245345465595745905107512601460167018902120236526202885316034453745405043654690502553705725609064656845
230551202053204506057759701175140016401900217024602760307534053750411044804865526556756100653569857445792084058900
340701452553955557459551190144517252020233526703025339537854195462050605515599064806985751080458595916597451034510955
4458517530546566088511351415172020502400277531753595403545004980548560106550711576958295891595551021010885115751228013010
55510020535054076510251315164019902370278032153675416046705210577063506955758582408910960510325110601182012600134001422015065
6601152304006158701165149518652265269531603655417547305310592065557215790586209360101251092011730125701343514320152301616017115
77012526045069097513051675208525353020354040904680529559456630734080858855965510485113451223013140140801504516040170551810019170
8751402854957651080144518552310280533453915453051805865658573408130895098051069011610125601354014550155901666017755188852004021225
98515531554583511851585203525353080366542954970568064307220805089159815107551172512735137751485015955171001827019475207152198023280
1090170345595910129017252215276033503990467554056180700078608760970010685117051276013855149901616017365186051988521195225402392025335
111001803706409851395186523952985362043155055584566857570849594701049011550126501379514980162051747018775201152149522915243702586027390
12105195400690106015002005257532053895463554356285718581359135101801127512415136001483016105174201878020180216252311024630261952780029445
13115210425740113516052145275534304165496058156725768587059770108951206513280145501586517230186352009021590231352472026350280252974031495
141202254557851210171022852935365544405285619571608190927010410116051285014150155001690018350198502140023000246402633028070298503168033550
151302354858351280181524253115388047105610657576008690984011045123151363515015164501793519475210702271024405261502794529790316803362035605
1613525051088513551920257032954100498059306950804091901040511685130251442515880173951897020600222852402025815276602955531505335053556037660
1714526554093014302025271034754325525562557330847596901097512320137351521016750183452000521725235002533027220291703117033225353353750039715
18150280565980150521302850365545505525658077108915101951154012960144451599517615192952104022845247152664528630306753278034945371653944041770
191602905951030158022352990383547755795690580909355106951211013595151551678518480202452207523970259302795530040321853439536665389904138043825
201653056251075165023403130401550006070722584709795111951268014235158651757019345211952311025095271452926531445336953600538380408204332045880
2117532065011251725244532704195522063407550885010230117001324514870165751836020215221452414526220283603057532855352053762040100426454525547930
2218033568011751800255034104375544566157875923010670122001381515510172851914521080230902518027340295753188534265367103923041820444754719549985
2319034570512201875265535504555567068858200960511110127001438016145180001993021945240402621528465307903319535670382204084543540463004913552040
2419536073512701950276036904735589571558520998511550132001495016785187102072022815249902725029590320103450537080397304245545255481305107554095
25205375765132020252865383049156115743088451036511985137051551517420194202150523680259402828530715332253581538490412404407046975499605301556150
26210390790136520952970397050956340770091701074512425142051608518060201302229024545268902932031835344403712539895427504568048695517855495558205
27220405820141521703070411052756565797094951112512865147051665018695208402308025410278353035532960356553843541305442554729550410536155689560260
28225415845146522453175425054556790824598151150513300152101722019335215502386526280287853139034085368703974542710457654890552130554405883562315
292354308751510232032804390563570158515101401188513740157101779019970222602465527145297353242535210380854105544120472755052053850572706077564365
302404459051560239533854530581572358790104651226514180162101835520610229702544028010306853346036330393004237045530487855213055570590956271566420
312504609301610247034904670599574609060107901264014620167101892521245236802622528875316353449537455405154368046935502905374057285609256465568475
322554709601655254035954810617576859330111101302015055172151949021885243952701529745325853553038580417354499048345518005535559005627556659570530
332654859851705261537004950635579109605114351340015495177152006022520251052780030610335303656539700429504630049755533105696560725645806853572585
3427050010151755269038055090653581309875117601378015935182152062523160258152858531475344803760040825441654761051160548205858062445664107047574640
35280515104518002765391052306715835510145120851416016370187202119523795265252937532345354303863541950453804892052570563256019064160682357241576695
36285525107018502840401553706895858010420124051454016810192202176024435272353016033210363803967043075465955023053980578356180565880700657435578745
37295540110019002910412055107075880510690127301492017250197202233025070279453095034075373304070544195478105154055385593456341567600718907629580800
38300555112519452985422556507255903010960130551529517690202252289525710286553173534940382754173545320490255285056795608556503069320737207823582855
39310570115519953060433057907435925011235133801567518125207252346526345293653252035810392254277046445502405416058200623656664071035755458017084910
40315580118520453135443559307615947511505137001605518565212252403526985300753331036675401754380547570514555547059610638706825572755773758211086965
41325595121020903210454060707795970011780140251643519005217252460027620307903409537540411254484048690526755678061020653806986574475792058405089020
42330610124021403285464562157975992512050143501681519440222302517028260315003488038410420754587549815538905809562425668907148076195810308599091075
433406251265219033554750635581551014512320146701719519880227302573528895322103567039275430254691050940551055940563835684007309077910828608793093130
443456351295223534304855649583351037012595149951757520320232302630529535329203645540140439704794552065563206071565245699057470579630846858987095180
453556501325228535054960663585151059512865153201795520760237352687030170336303724041005449204898053185575356202566650714157631581350865159181097235
463606651350233535805065677586951082013135156451833021195242352744030810343403803041875458705001554310587506333568060729257793083065883409375099290
4737068013802380365551706915887511045134101596518710216352473528005314453505038815427404682051050554355996564645694657443579540847859017095690101345
4837569514052430372552757055905511265136801629019090220752523528575320853576039605436054777052085565606118065955708757594081150865059200097630103400
4938570514352480380053807195923511490139551661519470225152574029145327203647040390444754871553120576806239567265722857745082765882259382599570105455
50390720146525253875548573359415117151422516940198502295026240297103336037185411754534049665541555880563615685757369078960843758994095655101510107510

How fast do I acquire new policies?Edit

This is very simple, and I'll just list it here to introduce some variable names. It depends on the culture your empire yields. With

c: The total culture yield of your empire

p: The cost of your next social policy (I will omit the dependencies for readability)

t: The time in turns to unlock the next policy

we get the simple formula

t = p/c

if we assume we just unlocked a new policy. If we introduce

c_{acc}: The culture already accumulated in the culture bucket

we get

t = (p - c_{acc})/c

How do I calculate the total culture in my empire?Edit

This is shown in the UI but again to introduce some concepts. Let

{\bar c} = c/n : the average culture of each city in your empire

It makes sense to split c up into a part that depends on n and represents your "typical city culture", the culture each city you newly create will add to your empire, and a part that is constant in n and represents bonuses from wonders, landmarks and city states. This is exact if you immediately buy your typical culture buildings but normally it's an approximation (it represents an equilibrium you will not normally have reached)

c_t: The typical culture per city

c_c: The city state culture

c = n c_t + c_c = n {\bar c}

Before I continue let's look at the policy speed for large numbers

\begin{align}
t &= \frac{p_b(k) (1 + p_m (n - 1))}{n {\bar c}} \\
  &= \frac{p_b(k)}{\bar c}\left(\frac{1 - pm}{n} + pm\right) \\
\end{align}


\lim_{n \to \infty}t = \frac{p_m p_b}{\bar c}

Also:
\lim_{n \to \infty}{\bar c} = c_t

So for large n, the average amount of culture per city is a good measure for the policy speed.

Will expanding increase or decrease policy speed?Edit

This is the question Paeanblack and I discussed in said thread. To analyse this, we have to calculate the number of turns to the next policy and see how it's affected by going from n->n+1.

When we do the city number increase, both p and c change. Let p and c be the policy cost and culture yield before founding the new city and p' and c' be the respective numbers after the founding.

p' = p_b(k) (1 + p_m n) = p + p_m p_b(k)

c' = (n + 1) c_t + c_c = c + c_t

Then calculate t' and check when it gets smaller than t (this would signify an increased policy speed)

t' = p'/c' = \frac{1 + p_m n}{c_t n + c_t + c_c} < \frac{1 + p_m n - p_m}{c_t n + c_c}

(1 + p_m n) (c_t n + c_c) < (1 + p_m n) (c_t n + c_c) + (1 + p_m n) c_t - p_m (c_t n + c_t + c_c)

p_m c_t n + p_m c_t + p_m c_c < c_t + p_m n c_t

p_m (c_t + c_c) < c_t

r_c = \frac{c_c}{c_t} < \frac{1}{p_m} - 1 = \frac{1 - p_m}{p_m}

For standard-sized maps, p_m = 3/10 so

r_c < \frac{7}{3} = 2.\hat3

So if the base culture from city states is less than 7/3 times larger than the typical city culture (let's call this the culture ratio r_c), you will get an increased policy speed from expanding. If it's exactly equal to this, the speed will stay the same, and if it's more, policy speed will slow. It should be noted that, no matter the value of r_c, for large numbers of cities the increase or decrease for founding an additional city will be very small.

For larger maps, this will be a little different.

  • Large: r_c < 4
  • Huge: r_c < 17/3 =5.\hat6

NumbersEdit

Now we can plug in some example numbers into our calculations. I will discuss the results only for the standard map size

  1. Let's assume you only build a monument in each of your cities. This is equivalent to a value of cT = 2. So if cC is five or greater, for example because you have at least one cultural city state as a friend, expansion will slow down your social policy speed.
    Standard: cC > 4, Large: cC > 8, Huge: cC > 11
  2. Now assume we build a monument and a temple, or cT = 5. Then, cC <= 11 will still yield an increase in your social policy speed. A cC of 10 is still pretty low, though. You normally still get it later on if you have at least one cultural city state ally.
    Standard: cC > 11, Large: cC > 20, Huge: cC > 28
  3. Looking at France, with a monument cT = 4 (true also for Egypt with Monument and Burial Tomb) and with both monument and temple, cT = 7. The corresponding cC values are 9 and 16. For 9 the same as above is true, but for the case with temples, you will actually gain an increase in policy speed if you don't have at least two city state allies or a city state and a few wonders.
    Standard: cC > 9 or 16, Large: cC > 16 or 28, Huge: cC > 28 or 40
  4. The Songhai have the excellent Mud Pyramid, so they share the cT = 7 case with France. The same goes for adding two artists in each city.

To sum up, expanding will in almost all cases slow down your social progress. The only cases where it will speed it up are if you either aren't interested in city states and wonders, or if you play a civ with a culture bonus. The only somewhat realistic scenario where expansion could speed up your policy gain is in my opinion if you play Songhai because you'll really want the Mud Pyramid and a Monument isn't that expensive.

I'm not sure if city state bonuses scale with the map size but if they don't, expansion increasing your policy speed is a lot more likely on larger maps, probably happening at some time if you just have a monument and a temple or are playing France. If you play Songhai, or France with temples it will even happen pretty often in fact. This is another case where the game doesn't scale well (well in the sense of preserving the same effects on gameplay as on standard size) with map size.

Food costEdit

The food cost for a city to grow is calculated as follows

n: The number of citizens in the city

f: Amount of food to grow to size n+1

f(n) = 15 + 6 (n - 1) + (n - 1)^{1.8} rounded down to the next integer.

Here are some tabularized and plotted values:

Food required to grow to level n+1

Civ5 food graph

Code:

n12345678910111213141516171819202122232425262728293031323334353637383940
f1522304051637690105121138155174194214235258280304329354380407435464493523554585617650684719754790826863901940979
Integrated food values (total amount food it takes to grow to size n)

Code:

n234567891011121314151617181920212223242526272829303132333435363738394041
f15376710715822129738749261375190610801274148817231981226125652894324836284035447049345427595065047089770683569040975910513113031212912992138931483315812

What this means is that growing from size 5 to 6 costs 51 food, while growing from size 10 to 11 already costs 122 food. I would like to present a few ways of looking at this problem from different angles in the following.

Constant Food SurplusEdit

This simplification assumes that each new citizen will work a tile that's worth 2 food and you therefore have a constant amount of food surplus that is put into growth. For a normal city without any bonuses, the amount of turns you need to grow is simply given by

t(n) = g(n)/f

where n is the number of citizens, f the food surplus and t the number of turns until growth, assuming you start at 0 food. Let's look at the numbers for df = 8

Code:

n12345678910111213141516171819202122232425262728293031323334353637383940
t2345681011131517192224272932353841444851545862656973778186909499103108113118122

This kind of explains why growth feels so slow once you hit the teens: The number of turns cities need to grow becomes pretty large, even with a pretty decent food surplus. A city takes about the same time to grow from size 12 to 13 as from size 1 to 5.

Constant food per citizenEdit

Here, the assumption is that each citizen will yield a certain mean amount of food. To understand what happens there better, let's define another function, the average growth cost per citizen

ga(n) = g(n)/n = 9/n + 6 + (n - 1)^{1.8}/n

(In blue the exact function, in red the rounded one the game uses)

The last term is approximately n^0.8 and dominates for large n. As you can see, this function has a minimum at size 4 (about 3.75 in the continuous case) and then constantly grows a little less than linearly - but linear growth is an excellent approximation.

As you can see below, the function 0.41 n + 8.18 fits the data over this range perfectly but is much simpler (the functions agree at all integer places).

So how do you interpret this function? What it tells you is that, with a constant amount of food produced per citizen, your city will grow more quickly than before until it hits size 4, and then starts slowing down again. Or, to put it more simple, this function tells you the amount of food each citizen has to produce to let the city grow.

An alternative, but equivalent, point of view is that, if you're aiming for a constant city growth speed, each citizen has to become more efficient as your city grows, in an approximately linear function.

You should note that the amount of food generated per citizen will usually be larger for small cities due to the city square itself (especially with maritime CS).

Slightly more realisticEdit

Cities start with a center square that yields some food (this catches granary, maritime city-states and water mill food, too). So the assumption that the amount of food per citizen will stay the same isn't really completely applicable. So let's introduce a new variable, f_csq, the city square food amount.

The assumption of an average amount of food per citizen now makes sense if you leave the city square food out. So let's call the amount of food each citizen generates f_c. The total food per turn, f, is then given by

F = f_c * n + f_{csq}

More interesting for us is the food surplus. This is given by

f = F - 2*n = (f_c - 2)*n + f_{csq}

The amount of surplus food produced per citizen is then

fa = f/n = f_{csq}/n + f_c - 2

To get the number of turns we need for growth, we need to combine this with the food growth cost and get

t = g/f

Since this function doesn't read too pretty, I'll just plop in some numbers and give you a graphical representation. Let's say f_csq = 2, which is the case if you don't have city state allies. If the city is to grow in a reasonable amount of time, f_c should be greater than 2.5, as you can see below

Civ5 foodturns1

You will notice a local minimum evolving in the f_c = 4 case, which signifies the onset of the constant food per citizen approximation, which is the limiting case for f_csq = 0

Are granaries worth it?Edit

Ultimately, that's for you to decide. I can give you some information so you can make an educated decision, though. Granaries increase f_csq from 2 to 4, so let's look at what happens to t if we make that change. Dashed are the values without granary, the full lines are with a granary

Civ5 cs2

More useful to judge the granary's effects is looking at the difference between the case without a granary, and the case with a granary

Civ5 cs2diff

As you can see, the difference quickly becomes essentially constant at a city size of 10 or more for any case of f_c > 2. From this analysis, I'd say that for f_c values up to 3, a granary is generally worth it, because it saves you a turn or two per growth step. For f_c = 2.5 it's very much worth it, and this seems to be a more realistic case than the higher values because not every citizen will work a farm (the f_c = 4 case represents each citizen working a Civil Service/Fertilizer farm) and some won't produce any food at all, like specialists.

Other f_csq effectsEdit

We can continue this analysis by increasing f_csq in steps. For example, a maritime CS will yield 2 extra food, as will a water wheel. Let's see what happens if we go from f_csq = 4 to f_csq = 6. I will omit the more unrealistically high cases from now on for a better overview. Shown is again the total number of turns needed and the difference between 4 and 6. This time, f_csq = 4 is dashed

Civ5 csCiv5 csdiff

So getting the second city-state isn't as good as getting the first, and getting a granary when you already have a city-state isn't so great, either. It still shaves off a turn (or three in the f_c = 2.5 case), though.

The next increase step, from 6 to 8 (the "before" being dashed as usual)

Now things start becoming somewhat underwhelming. As I said, if things are worth it for you is up to you to decide, but I'd definitely not build that water mill if I already have a granary and a city-state ally because the difference will only be something like two turns in three growth steps or so, which isn't exactly a lot.

f_c values < 2Edit

After reading a comment from ehrgeix, I think it makes sense to extend the analysis to f_c values that are smaller than 2. The values greater than 2 are applicable if you want to let your city continue to grow for the rest of the game. Values smaller than 2 still make sense in transitionary periods, if you want a growth cap, or if you're fine with your growth speed slowing down even more than in the constant food surplus case discussed above.

Qualitatively, we can already see from looking at f = (f_c - 2)*n + f_{csq}, which occurs as the denominator in the formula for t, that these functions will have a pole at a finite n, because f_c - 2 becomes negative. The locus of this singularity is given by n = f_{csq}/(2 - f_c). This singularity signifies the number of citizens where the city stops growing.

In the following are some graphs in the same way as above. First, f_csq = 2

Civ5 fc2

If you increase f_csq you shift the position of the pole to the right, so the difference between t(csq = 2) - t(csq = 4) has a singularity at the same points as t. See the f_csq = 4 case below. Dashed is the "previous", in this case f_csq = 2

Civ5 fc4Civ5 fc4diff

f_csq = 4 -> f_csq = 6

Civ5 fc6Civ5 fc6diff

f_csq = 6 -> f_csq = 8

Civ5 fc8Civ5 fc8diff

Unit maintenanceEdit

As of 6/2015, the formula for unit maintenance as defined in all versions CvGameCoreDLL is calculated as follows:

  • b = Base unit cost (INITIAL_GOLD_PER_UNIT_TIMES_100)
  • f = Free units from handicap (GoldFreeUnits), specific unit types, or policies
  • u = Total paid units
  • n = max(0, u-f) = Number of actual paid units (if u-f is less than 0 then n = 0)
  • m = Multiplier (UNIT_MAINTENANCE_GAME_MULTIPLIER)
  • d = Divisor (UNIT_MAINTENANCE_GAME_EXPONENT_DIVISOR)
  • t = Current turn
  • e = Estimated end turn (based on all entries for the current GameSpeed in GameSpeed_Turns)
  • g = t/e = Game progress factor
final cost = (n*b(1+g*m)/100)(1+(g/d))

Unit cost modifiers from traits are applied before the exponent. Unit cost modifiers from policies, handicap, and AI difficulty level are applied to the final cost.

Since there's no easy "each unit in turn t will cost this much" here's a table you can use as a rough reference. The first row is the number of turns, the first column the number of units

Total unit maintenance costs
Turns
Units 1 20 50 100 150 200 250 300 350 400
422357911141720
845711151924303643
12681116233038475768
168101422314152647894
201013182839526682100121
2412162234476380100122148
2814182640567494118145176
32162130466485109136168204
36182434527396124155191232
402026375881108138174215261
442229416489119153193238291
482432457098131168212262321
5226354976107142184231287350
5628375382115154199251311381
6030405788124166214270335411
6432436194133177229290360442
68344564100141189245310385473
72364868106150201260330410504
76385172112159213276349435535
80405476118167225292370461567
84425680124176237307390486598
88445984130185249323410512630
92466288137194260339430537662
96486492143203273354451563694
100506795149211285370471589727

Here's an equivalent table detailing the cost per unit

Maintenance cost per unit
Turns
Units 1 20 50 100 150 200 250 300 350 400
40.500.500.751.31.82.32.83.54.35.0
80.500.630.881.41.92.43.03.84.55.4
120.500.670.921.31.92.53.23.94.85.7
160.500.630.881.41.92.63.34.04.95.9
200.500.650.901.42.02.63.34.15.06.1
240.500.670.921.42.02.63.34.25.16.2
280.500.640.931.42.02.63.44.25.26.3
320.500.660.941.42.02.73.44.35.36.4
360.500.670.941.42.02.73.44.35.36.4
400.500.650.931.52.02.73.54.45.46.5
440.500.660.931.52.02.73.54.45.46.6
480.500.670.941.52.02.73.54.45.56.7
520.500.670.941.52.12.73.54.45.56.7
560.500.660.951.52.12.83.64.55.66.8
600.500.670.951.52.12.83.64.55.66.9
640.500.670.951.52.12.83.64.55.66.9
680.500.660.941.52.12.83.64.65.77.0
720.500.670.941.52.12.83.64.65.77.0
760.500.670.951.52.12.83.64.65.77.0
800.500.680.951.52.12.83.74.65.87.1
840.500.670.951.52.12.83.74.65.87.1
880.500.670.951.52.12.83.74.75.87.2
920.500.670.961.52.12.83.74.75.87.2
960.500.670.961.52.12.83.74.75.97.2
1000.500.670.951.52.12.93.74.75.97.3

Viewing all articles
Browse latest Browse all 25817

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>