Here are formulas that are used by Freeciv to calculate some things, presented in a form better for mathematicians.
Score of a player Edit
Shown in F3 tab, equals to sum of
- Number of citizens (the sum of city sizes)
- Number of known techs * 2
- Number of known future techs * 5
- Number of wonders * 5 (the big ones)
- Spaceship score (100 points for every 10.000 citizens on the spaceship multiplied by success rate)
- Number of units built / 10
- Number of units killed / 3
DemographicsEdit
Population | The sum of the population in each of your cities |
Land Area | The sum of every tile of your territory (including Ocean) on the map multiplied by 1,000 in "square miles" |
Settled Area | The sum of every tile that is being currently worked by a city including the "city tile" AND plus any units who can improve terrain not on tiles worked by cities or in cities themselves, on the map multiplied by 1,000 in "square miles" |
Research Speed | The total amount of "bulbs" being produced by you per turn |
Literacy | The percentage of your civilization's population that live inside a city that is close, beyond or at a 100% output bonus in science. Under the default ruleset for example, a library provides a +100% bonus to science if every city had one - your civilization would have 100% literacy. |
Production | The sum of the surplus production after being spent on units and waste in each of your cities in "M tons" |
Economics | The sum of the surplus trade after loses to corruption in each of your cities in "M goods" |
Military Service | $ \frac{Number of Military Units \times 5000}{10 + [Total Population/1000]} $ --- in months |
Pollution | The sum of the total pollution being produced by each of your cities in "tons" |
Tile output Edit
Tiles produce three types of output: food, shields (for production), trade. To calculate the output of a tile:
- Start with the base output for the terrain type.
- Add output from the special resource, if the tile has any.
- Add the point bonuses from terrain improvements:
- Add bonus to food, if the tile has some irrigation or is the center of a city.
- Add bonus to shields, if the tile has a mine. (desert, glacier, hills or mountains)
- Add bonus to trade, if the tile has a road. (grassland, plains or desert)
- Add bonus to trade, if the tile has a river.
- Add to shields, the railroad bonus [50% in default ruleset], if the tile has a railroad.
- Add point bonus from effect "Output_Add_Tile" [as from Harbor or Offshore Platform].
- Add point bonus from effect "Output_Inc_Tile_Celebrate", if the tile already has some output of the same type, and the city is celebrating [like the +1 trade bonus for Monarchy or Communism]
- Add point bonus from effect "Output_Inc_Tile", if the tile already has some output of the same type [like the +1 trade bonus for Republic or Democracy].
- Add percentage bonus from effect "Output_Per_Tile" [as from Superhighways or Supermarket].
- Subtract 1 point if the output exceeds the value of effect "Output_Penalty_Tile" [like the penalty for exceeding 2 points during Anarchy or Despotism].
- Subtract pollution penalty [50% in default ruleset], if the tile has pollution.
- Subtract fallout penalty [50% in default ruleset], if the tile has nuclear fallout.
- Apply minimum values [1/1/0 in default ruleset], if the tile is the center of a city.
See get_worked_tile_output() and city_tile_output() in common/city.c.
City output (food/production/trade) Edit
Cities produce six types of output: food, shields (for production), trade, gold, luxury and science. To calculate the output of any single city:
- Start with the base citizen output from all citizens who are working tiles, or who are specialists.
- If the city has any trade routes, then add the bonus from these routes to trade.
- If the city pays tithes (like from Fundamentalism in civ2 ruleset), then add the tithes to gold.
- Calculate waste and corruption.
- Apply tax rates to trade. Add result to gold, luxury and science.
- Apply city bonus effects (like 50% shield bonus from Factory, and the 100% science bonus from Library.)
References:
- enum output_type_id in common/fc_types.h
- output_type_iterate() in common/city.h
- set_city_production() in common/city.c
Food box Edit
The amount of food needed to fill the food box is a function of the game.ruleset values granary_food_ini and granary_food_inc. The server option foodbox adjusts the amount.
- Recursive formula:
- $ city\_granary\_size = f(city\_size) \times \frac{foodbox}{100} $
- $ f(city\_size) = \begin{cases} granary\_food\_ini[city\_size] & \mbox{if } city\_size \le num\_inis \\ f(city\_size - 1) + granary\_food\_inc & \mbox{if } city\_size > num\_inis \\ \end{cases} $
- Where:
- city_size is the current size of the city.
- num_inis is the number of values in granary_food_ini (which is a list).
When you add a settler the food in the foodbox is kept. With a granary the foodbox will be half full when the city is decreased because of starvation, but when you grow a settler the foodbox will be empty(?). When you add a settler you will not gain half a foodbox
References:
- load_ruleset_game in server/ruleset.c
- city_granary_size in common/city.c
Production Edit
Gold needed to finish production ($ T $ - shields left to produce):
- For units: $ 2T+\frac{T^2}{20} $
- For buildings: $ 2T $
- For wonders: $ 4T $
Multiply these values by 2 if there is no shield in the city stock ($ C $ - cost in shields):
- For units: $ 4C+\frac{C^2}{10} $
- For buildings: $ 4C $
- For wonders: $ 8C $
The cost of upgrading units is the unit's buy cost had the unit been disbanded (contributing half of its shields) and then bought.
References:
- impr_buy_gold_cost in common/improvement.c
- utype_buy_gold_cost in common/unittype.c
Trade routes Edit
Freeciv 2.1 uses the following formulas:
Trade route revenue generated every turn in cities: ($ T_1 $ = trade production in the city 1, $ T_2 $ = trade production in the city 2. These values do not include the extra trade revenues from other trade routes )
- With yourself, on the same continent: $ \frac{(T_1 + T_2 + 4)}{16} $
- With another player, on the same continent: $ \frac{(T_1 + T_2 + 4)}{8} $
- With yourself, on another continent: $ \frac{(T_1 + T_2 + 4)}{8} $
- With another player, on another continent: $ \frac{(T_1 + T_2 + 4)}{4} $
- With experimental trade route revenues (on Warserver): $ \frac{(T_1 + T_2 + 4)}{4} $
Freeciv 2.2 uses the following formulas:
Trade route revenue generated every turn in cities: ( D = distance between city 1 and city 2, all directions counting as 1, S1 = size of city 1, S2 = size of city 2 )
- With yourself, on the same continent: ( D + S1 + S2 ) / 12
- With another player, on the same continent: ( D + S1 + S2 ) / 6
- With yourself, on another continent: ( D + S1 + S2 ) / 6
- With another player, on another continent: ( D + S1 + S2 ) / 3
Caravan bonus, the gold and science you get when establishing a new trade route: ($ T_1 $ = trade production (including trade routes revenues) in the city 1, $ T_2 $ = trade production in the city 2, $ D $ = real distance between the cities):
- $ \frac{(D + 10) (T_1 + T_2)}{8} $
The possession of the technologies Railroad and Flight each reduces the bonus by $ \frac{1}{3} $. Then:
- You have one of this technologies: $ \frac{(D + 10) (T_1 + T_2)}{12} $
- You have two of this technologies: $ \frac{(D + 10) (T_1 + T_2)}{18} $
- With experimental caravan bonus style (on Warserver): $ (2 \log(D + 20 + T_1 + T_2)) ^ 2 $
Bonus from a caravan, when it enter in a existent trade place, you get only $ \frac{1}{3} $ of the bonus: ($ T_1 $ - trade production in the city 1, $ T_2 $ - trade production in the city 2, $ D $ - real distance between the cities):
- $ \frac{(D + 10) (T_1 + T_2)}{24} $
The possession of the technologies Railroad and Flight each reduces the bonus by $ \frac{1}{3} $. Then:
- You have one of this technologies: $ \frac{(D + 10) (T_1 + T_2)}{36} $
- You have two of this technologies: $ \frac{(D + 10) (T_1 + T_2)}{54} $
- With experimental caravan bonus style (on Warserver): $ \frac{(2 \log(D + 20 + T_1 + T_2)) ^ 2}{3} $
These formulas are the base of the calculation. Different governments output different amounts of trade in each city. Republic and Democracy get a +1 trade bonus in tiles that are generating trade, so usually, you will make a lot more profit under these governments. Monarchy and Communism get the +1 trade bonus only if they are celebrating.
Taxation of trade Edit
For each city, multiply the trade output (after subtracting corruption) by the tax rates to yield the gold/luxury/science output taxed from trade. (See add_tax_income() in common/city.c.) The problem is how round the result to exact integers, without increasing or decreasing the trade output (which must exactly equal the sum of the three tax outputs).
From distribute() in utility/distribute.c:
/* * Distribution of a number of items into a number of groups with a given * ratio. This follows a modified Hare/Niemeyer algorithm (also known * as "Hamilton's Method"): * * 1) distribute the whole-numbered part of the targets * 2) sort the remaining fractions (called rest[]) * 3) divide the remaining source among the targets starting with the * biggest fraction. (If two targets have the same fraction the * target with the smaller whole-numbered value is chosen. If two * values are still equal it is the _first_ group which will be given * the item.) */
The order of the three groups in Freeciv is science, gold, luxury.
In Wikipedia, this method is the Largest remainder method using the Hare quota. (Wikipedia discusses elections, so pretend that the trade points are senate seats, and that the tax rates are 100 votes for the Gold, Luxury, or Science party lists.)
Example 1 Edit
- Trade surplus: +2
- Tax rates: 60% science, 40% gold
Initial multiplication:
- $ science = +2 \times 60\% = 1 + \frac{20}{100} $
- $ gold = +2 \times 40\% = 0 + \frac{80}{100} $
This gives 1 science and 0 gold, but uses only 1 of 2 trade points. Gold has the highest remainder (80 > 20), so Freeciv gives the other point to gold.
Final result:
- 1 science, 50% of +2
- 1 gold, 50% of +2
Example 2 Edit
- Trade surplus: +17
- Tax rates: 30% science, 30% gold, 40% luxury
Initial multiplication:
- $ science = +17 \times 30\% = 5 + \frac{10}{100} $
- $ gold = +17 \times 30\% = 5 + \frac{10}{100} $
- $ luxury = +17 \times 40\% = 6 + \frac{80}{100} $
This uses 5 + 5 + 6 = 16 trade points. Luxury has the highest remainder (80 > 10) and receives the 17th point.
Final result:
- 5 science, ≈29.4% of +17
- 5 gold, ≈29.4% of +17
- 7 luxury, ≈41.2% of +17
Example 3 Edit
- Trade surplus: +18
- Tax rates: 30% science, 30% gold, 40% luxury
Initial multiplication:
- $ science = +18 \times 30\% = 5 + \frac{40}{100} $
- $ gold = +18 \times 30\% = 5 + \frac{40}{100} $
- $ luxury = +18 \times 40\% = 7 + \frac{20}{100} $
This uses 5 + 5 + 7 = 17 trade points. Science and gold tie for the highest remainder. To break the tie, Freeciv would give the 18th point to the group with fewer points, but both groups have 5 points. In this case, Freeciv always prefers science to gold, and gives the 18th point to science.
Final result:
- 6 science, ≈33.3% of +18
- 5 gold, ≈27.8% of +18
- 7 luxury, ≈38.9% of +18
Example 4 Edit
- Trade surplus: +24
- Tax rates: 60% science, 10% gold, 30% luxury
Initial multiplication:
- $ science = +24 \times 60\% = 14 + \frac{40}{100} $
- $ gold = +24 \times 10\% = 2 + \frac{40}{100} $
- $ luxury = +24 \times 30\% = 7 + \frac{20}{100} $
This uses 14 + 2 + 7 = 23 points. Science and gold again tie for the highest remainder, but now gold has fewer points. To break the tie, Freeciv gives the 24th point to gold.
Final result:
- 14 science, ≈58.3% of +24
- 3 gold, 12.5% of +24
- 7 luxury, ≈29.2% of +24
City health Edit
Settings Edit
The health of a city is defined by the possibility of an illness (plague). The following ruleset options are available in game.ruleset, section [civstyle]:
; Whether plagues (illness) are possible illness_on = 0 ; the base factor for illness (of percent) illness_base_factor = 25 ; minimum city size for illness illness_min_size = 3 ; factor for how much trading with a plagued city increases our city's ; chance for plague (in percent) illness_trade_infection = 50 ; factor for how much pollution within a city increases its chance for ; plague (in percent) illness_pollution_factor = 50
Equations Edit
The possibility of an illness is given by a base value, defined in the game.ruleset file (illness_base_factor: maximum propability for illness in percent). Illness due to trade infection and illness due to pollution are added to this value. The illness values calculated below are given in tenths of percent.
The base illness value depends on the city size reduced by the minimum size for illness (illness_min_size) and is given by
$ illness\_size = (1- \exp(- \frac{size - illness\_min\_size}{10})) \times 10 \times illness\_base\_factor $
This function starts at 0 for $ size - illness\_min\_size = 0 $ and goes asymptotic to its maximum value (illness_base_factor).
If a trade city had an illness within the last 5 turns the illness value is increased by
$ illness\_trade = \frac{illness\_trade\_infection}{100} \times \sqrt{size \times trade\_city\_size} $
Pollution also increases the illness value:
$ illness\_pollution = \frac{illness\_pollution\_factor}{100} \times pollution $
Remember: The illness value is the illness in tenth of percent, i.e. a value of 56 means 5.6% propability for an illness within the city.
Example Edit
Taking into account a city size of 1 to 50, a pollution level which equals six times the city size (pollution = 6 * size) and four trade cities of size 10 the illness possibility of the city is given in the following table (in percent).
# | (illness) | aqueduct | aqueduct + sewer system | ||||||
---|---|---|---|---|---|---|---|---|---|
base | +trade | +pollution | base | +trade | +pollution | base | +trade | +pollution | |
5 | 9.800 | 11.200 | 12.700 | 4.900 | 5.600 | 6.350 | 1.960 | 2.240 | 2.540 |
10 | 15.800 | 17.800 | 20.800 | 7.900 | 8.900 | 10.400 | 3.160 | 3.560 | 4.160 |
15 | 19.400 | 21.800 | 26.300 | 9.700 | 10.900 | 13.150 | 3.880 | 4.360 | 5.260 |
20 | 21.600 | 24.400 | 30.400 | 10.800 | 12.200 | 15.200 | 4.320 | 4.880 | 6.080 |
25 | 22.900 | 26.100 | 33.600 | 11.450 | 13.050 | 16.800 | 4.580 | 5.220 | 6.720 |
30 | 23.800 | 27.300 | 36.300 | 11.900 | 13.650 | 18.150 | 4.760 | 5.460 | 7.260 |
35 | 24.200 | 27.900 | 38.400 | 12.100 | 13.950 | 19.200 | 4.840 | 5.580 | 7.680 |
40 | 24.500 | 28.500 | 40.500 | 12.250 | 14.250 | 20.250 | 4.900 | 5.700 | 8.100 |
45 | 24.700 | 28.900 | 42.400 | 12.350 | 14.450 | 21.200 | 4.940 | 5.780 | 8.480 |
50 | 24.800 | 29.300 | 44.300 | 12.400 | 14.650 | 22.150 | 4.960 | 5.860 | 8.860 |
Health effect Edit
Furthermore for the aqueduct and the sewer system a health effect is defined.
[effect_aqueduct_health] name = "Health_Pct" value = 50 reqs = { "type", "name", "range" "Building", "Aqueduct", "City" }
[effect_sewer_system_health] name = "Health_Pct" value = 30 reqs = { "type", "name", "range" "Building", "Aqueduct", "City" "Building", "Sewer System", "City" }
The possibility of an illness is reduced by <value> percent defined by the effect Health_Pct (i.e aqueduct + sewer system = Health effect of 80; an base illness possibility of 10% is reduced by 80% to 2%).
Migration Edit
Settings Edit
Migration between cities is controlled by the following game settings. They can be changed by using the set server command. help <setting> will give you more information.
- migration - Whether to enable citizen migration (default: 0 = off)
- mgr_turninterval - Number of turns between migrations from a city (default: 5; min:1; max: 100)
- mgr_foodneeded - Whether migration is limited by food (default: 1 = on)
- mgr_distance - Maximum distance citizens may migrate (default: 3; min: 1; max: 7)
- mgr_nationchance - Percent probability for migration within the same nation (default: 50; min: 0; max: 100)
- mgr_worldchance- Percent probability for migration between foreign cities (default: 10; min:0; max: 100)
Equations Edit
Helper function to calculate a "score" of a city. The score is used to get an estimate of the "migration desirability" of the city. The higher the score the more likely citizens will migrate to it.
The score depends on the city size, the feeling of its citizens, the cost of all buildings in the city, and the surplus of trade, luxury and science.
- base formula: $ score = (city\_size + feeling) \times \prod factors $
- feeling of the citizens: $ feeling = 1.00 \times [happy~citizens] + 0.00 \times [content~citizens] - 0.25 \times [unhappy~citizens] - 0.50 \times [angry~citizens] $
- factors
- if the city has at least one wonder a factor of 1.25 is added
- for the capital an additional factor of 1.25 is used
- an additional factor is given by the effect 'Migration_Pct': $ f = 1 + \frac{\sum_{effect} Migration\_Pct}{100} $
- the build costs of all buildings: $ f = \left(1 + \frac{1 - \exp\left(- \frac{[build~shield~cost] }{1000}\right)}{5}\right) $
- the trade of the city: $ f = \left(1 + \frac{1 - \exp\left(- \frac{[city~surplus~trade] }{100 }\right)}{5}\right) $
- the luxury within the city: $ f = \left(1 + \frac{1 - \exp\left(- \frac{[city~surplus~luxury] }{100 }\right)}{5}\right) $
- the science within the city: $ f = \left(1 + \frac{1 - \exp\left(- \frac{[city~surplus~science]}{100 }\right)}{5}\right) $
- the food available in the city: $ f = \left(1 + [city~surpuls~food] / 10\right) $
The first four factors f have values between 1 and 1.2 while the factor for food is limited between 0.9 (10 food) and 1.2 (+20 food). The overall factor will be between 1.0 (smaller cities) and 2.0 (bigger cities)
[build shield cost], [city surplus trade], [city surplus luxury] and [city surplus science] must be >= 0! [city surplus food] can be below zero.
The score of the source city is multiplied by 3 to take into account the persistence of the citizens.
$ score\_source = city\_score * 3 $
The score of the target city is weighted by the distance between both cities. The weight factor is calculated as
$ weight = \frac{[maximal~distance] + 1 - dist}{[maximal~distance] + 1} $
$ score\_target = city\_score * weight $
Example Edit
For the following three city the migration score is calculated:
city characteristics | |||
---|---|---|---|
city 1 | city 2 | city 3 | |
city size | 17 | 5 | 3 |
citizens (happy/content/unhappy/angry) | 7/9/1/0 | 1/4/0/0 | 0/3/0/0 |
city has a wonder | yes | yes | no |
city is capital | yes | no | no |
effect Migration_Pct | 30 | 0 | 0 |
build shild cost | 2500 | 1000 | 250 |
city surplus (trade/luxury/science) | 62/16/15 | 10/2/5 | 6/0/6 |
distance | |||
city 1 | - | 3 | 5 |
city 2 | 3 | - | 3 |
city 3 | 5 | 3 | - |
calculated values | |||
city 1 | city 2 | city 3 | |
city size | 17.000 | 5.000 | 3.000 |
feeling | 6.750 | 1.000 | 0.000 |
factor: wonder | 1.250 | 1.250 | 1.000 |
factor: capital | 1.250 | 1.000 | 1.000 |
factor: effect Migration_Pct | 1.300 | 1.000 | 1.000 |
factor: build shield cost | 1.184 | 1.126 | 1.044 |
factor: surplus trade | 1.092 | 1.019 | 1.012 |
factor: surplus luxery | 1.030 | 1.004 | 1.000 |
factor: surplus science | 1.028 | 1.010 | 1.012 |
basic city score | 66.044 | 8.726 | 3.208 |
weight: distance city 1 | - | 0.625 | 0.375 |
weight: distance city 2 | 0.625 | - | 0.625 |
weight: distance city 3 | 0.375 | 0.625 | - |
migration checks | |||
source city / target city | city 1 (source) | city 2 (source) | city 3 (source) |
city 1 (target) | - | 26.179 / 41.2773 | 9.623 / 24.7664 |
city 2 (target) | 198.131 / 5.4539 | - | 9.623 / 5.4539 |
city 3 (target) | 198.131 / 2.0048 | 26.179 / 3.2076 | - |
Migration will take place from city 2 to city 1 and from city 3 to city 1 with the possibility given in the corresponding game settings (mgr_nationchance and mgr_worldchance).
Migration Effect Edit
TODO: exact description of the effect; increase / decrease the possibility of migration (i.e for different governments)
Science Edit
Technology cost formula depends on several game settings: techcoststyle, researchcost, techcostdoubleyear, techleackage, techleackagerate. There might be differences between calculated values and real-game ones due to rounding to integers in several places.
- Future techs: calculated like common techs with techcoststyle=0.
- Common techs:
- If techcoststyle=2, ruleset might set its own values, not calculated by Freeciv. For the techs not overrided by the ruleset, cost is calculated like if techcoststyle=1.
- If techcoststyle=1 (the default case), $ \textrm{basecost}=\frac12 \times (n+2)^{1.5} \times \textrm{researchcost} $, but not less than researchcost. $ n $ is number of all (including already researched) technologies required by given tech.
- If techcoststyle=0, to discover nth technology you need $ \textrm{basecost}=(n-1)\times\textrm{researchcost} $ (like in Civilization I/II games).
Additional rules.
- If techcostdoubleyear parameter is not zero, cost doubles starting from this year.
- If techleakage parameter is not zero, you get bonus when other players already have developed the tech. The formula is:$ \textrm{bonus}= \textrm{basecost}\times \left( 1-\frac{\textrm{techleakagerate}}{100}\times\frac{p_2}{p_1} \right) $, where $ p_1 $ is total number of players and $ p_2 $ is:
- number of players that know the technology and you have embassy, when techleakage=1,
- number of players that know the technology, when techleakage=2,
- number of players that know the technology, excluding barbarian players, when techleakage=3 (in this case total number of players is calculated without barbarians too).
- Note: $ \textrm{techleakagerate} $ is a Warserver setting. On standard server, $ \textrm{techleakagerate} = 100 $.
- For AI players there might be a penalty/bonus depending on AI skill.
- If the calculated cost is zero, the real cost is 1 bulb.
Getting tech without research Edit
You can get techs, when you conquer a city, use a diplomat, from Great Library, by completing traderoutes, or by Darwin's Voyage wonder or discovery of Philosophy. The chances and costs of this is decided by server settings conquercost, diplochance, freecost, by disallowing traderoutes before the start of each game, etc.
Research Edit
You can research either by science out put from your trade income, or by using Specialists when your cities are at size 5 or above.
Specialists | Default output | With library | With university |
---|---|---|---|
1 scientists | 3 bulbs | 6 bulbs | 10 bulbs |
2 scientists | 6 bulbs | 12 bulbs | 21 bulbs |
Probability of winning a fightEdit
- Main page: /Battle outcome
The chances of winning a fight are sometimes counter-intuitive. E.g., what would you say are your chances of winning in the following situation (after taking into account the land advantages and other modifiers): you have an attack of 2 against a defense of 3, and the same number of hit points? Considering that the probability of winning each round is 2/5, you might think that this will give you with a reasonable chance of victory, something like 40%. Lost. You only have 19% chances of winning. The fact that the battle lasts several rounds tends to give a larger advantage to the biggest power than one could think beforehand.
Supposing the reader is aware of the rules of the fight (for each round, take a random number, decide who wins that round, etc.), let's try to find a formula representing the chances of victory.
Notations and basic computationsEdit
Consider a unit having a strength (all modifications, field, city, unit experience of combat, etc. being applied) $ s $ and an enemy unit having a defense $ r $. The number of hit points of the defending unit is written $ d_\text{hp} $, the damages per victorious rounds from the attacking unit is $ a_\text{fp} $. Let us note $ k $ the number of victorious rounds required to win the fight. And $ l $ is the number of rounds that, if lost, implies a defeat ($ l $ is for "losing allowance").
When the attacking unit cause one damage per victorious rounds ($ a_\text{fp}=1 $), $ k $ is the number of hit points of the defending unit ($ d_\text{hp} $). More generally, $ k = \lfloor\frac{d_\text{hp} + a_\text{fp} - 1}{a_\text{fp}}\rfloor $. $ l $ may be computed similarily.
The chance of winning each round, let us note it $ p $, is $ p = \frac{s}{s+r} $. (Thus the chance of losing a round is $ 1-p $.)
ReasoningEdit
Now let us suppose that the fight takes $ n $ rounds to achieve, and let us compute the probability of victory. Note that we do not know $ n $ in advance but we suppose for now that it is given. For a given $ n $, the probability of victory is the probability that $ k $ rounds have been won over these $ n $ tries and less than $ l $ rounds have been lost. If $ n < k + l $, then the probability of victory is simply the probability that $ k $ rounds have been won over these $ n $ tries (as this implies that less than $ l $ rounds have been lost). In case of victory, the last round must be the final hit (otherwise the fight would have been stopped earlier). Apart from that, there must be $ k - 1 $ hits among the $ n - 1 $ other rounds. That probability is given by a binomial $ B(n-1,k-1) $ and is $ {n-1 \choose k-1} p^{k-1} (1-p)^{n-k} $. As the last round must be won, we multiply by $ p $ and get a probability of victory with $ n $ rounds of $ {n-1 \choose k-1} p^k (1-p)^{n-k} $.
For the attacking unit to win the fight, we must have $ k \leq n < k + l $. Thus finally the probability of winning equals:
$ \sum_{n = k}^{k+l-1} {n-1 \choose k-1} p^k (1-p)^{n-k} $.
Alternative reasoningEdit
The freeciv source code uses a slightly different reasoning (look at ./common/combat.c:unit_win_chance() and ./common/combat.c:win_chance()). Let us note $ d $ the number of rounds lost, which must be smaller than $ l $ for a victorious attack. Then we have the following probability of victory.
$ \sum_{d = 0}^{l - 1} {k - 1 + d \choose d} p^{k-1} (1-p)^d p $.
We get the initial formula by using $ n = k + d $ and $ {n-1 \choose n-k} = {n-1 \choose k-1} $. In the code, $ k $ is def_N_lose, $ l $ is att_N_lose, $ p $ is def_P_lose1, $ (1-p) $ is att_P_lose1, $ d $ is lr.
Global Warming and Nuclear Winter Edit
The two global catastrophes are driven by the same engine, update_environmental_upset() in srv_main.c, but are independent of each other.
There isn't very much configurable about this mechanism. The various magic numbers mentioned below are hardcoded. About all you can do in rulesets currently is to change how likely tiles are to get polluted in the first place (base_pollution). Since 3.0, two new server settings globalwarming_percent and nuclearwinter_percent are introduced to balance some games better.
Each turn, the chance of catastrophe is affected by the current count of polluted tiles (for GW) or tiles with fallout (for NW) on the world map. (Pollution in cities doesn't directly affect global warming, beyond being the means by which tiles become polluted.)
However, the chance is not purely a function of that number; it also depends on the past.
The chance of catastrophe "accum" is calculated each turn as follows:
$ accum_t = \max(0,accum_{t-1} + current - level)\\ current = \lfloor count\times percent/100\rfloor $
where the variables are:
Variable | Specific name | Description | |
---|---|---|---|
Global Warming | Nuclear Winter | ||
count | - | - | Number of polluted tiles this turn |
current | game.info.heating | game.info.cooling | Number of polluted tiles this turn, in newer versions multiplied on the percentage. Applied as a delta (hence name "heating"/"cooling"). (Only stored for display purposes, not carried from one turn to the next.) |
percent | game.server.global_warming_percent | game.server.nuclear_winter_percent | The server setting, goes from 1 to 10000, defaults to 100 |
level | game.info.warminglevel | game.info.coolinglevel | A constant (but see below): critical level of pollution (same units as "current") |
accum | game.info.globalwarming | game.info.nuclearwinter | The chance of catastrophe each turn is proportional to "accum". Carried from one turn to the next. |
Thus:
- $ count = level\times\frac{100}{percent} $ represents a critical number of polluted tiles at which the chance of catastrophe remains constant over time. Depending on the past, that chance could be zero or it could be a high level.
- If current is below level, then the chance of catastrophe will decline and eventually become zero.
- If current is above level, then the chance of catastrophe will increase.
Thus, the effect of polluted tiles is cumulative. If there's currently an elevated risk of catastrophe, even if you then clean up all existing polluted tiles in the world in a single turn with Engineers, the risk won't necessarily drop to zero next turn; it will gradually reduce over several turns until it hits zero.
For both kinds of catastrophe, level is initially based on the map size intiles: $ level = \left \lceil \frac{1}{500} \times map\_num\_tiles() \right \rceil $. Since accum starts at zero, this effectively means that bigger worlds can take more punishment before there is any risk of catastrophe: 0.2% of a world's tiles must become polluted before the risk starts to increase at all, if percent is default. Nevertheless, Longturners have complained on too quick pollution. Changing the multiplier, one can make this number up to 100 times bigger or smaller.
The actual chance of catastrophe is also affected by the map size in tiles: ignoring rounding, $ P(catastrophe) = \frac{20 \times accum}{map\_num\_tiles()} $
Client display Edit
The client lets you see quantities like the following:
Global warming chance: 6% (-1%/turn) Nuclear winter chance: 0% (-2%/turn)
Shows the rate of global warming: Pollution rate: -1% Chance of catastrophic warming each turn: 6%
(Up to 2.1.11/2.2.0, due to a bug -- GNA#15563 -- the quantities displayed are only really percentages for the default map size of 4000 tiles. For bigger maps, the percentage displayed will be too big, and a displayed chance of 100% will correspond to a $ P(catastrophe) $ of less than 1.0. This is fixed as of 2.2.1.)
The displayed "chance" is proportional to "accum". The "rate" is proportional to "current - level". They have the same units; in the first example, you can expect the displayed "Global warming chance" to drop to (approximately) 5% next turn (assuming the number of polluted tiles doesn't change wildly). Examples:
- When "chance" is 0% and "rate" is negative, you're comfortably below the critical level of pollution and "chance" will remain zero.
- When "rate" is 0%, you are around the critical level of pollution.
When catastrophe strikes Edit
Each turn, dice are rolled for each kind of catastrophe to determine whether it occurs, based on "accum" as described above.
If it does occur, then the magnitude of the catastrophe also depends on "accum", as well as the map's dimensions -- approximately, the number of tiles changed to another type of terrain increases with "accum". So there's a double effect: a higher risk of catastrophe, in addition to being more likely to occur each turn (by definition), will also lead to more severe effects when it does occur.
After a given catastrophe has actually occurred:
- The appropriate "level" is increased, so that it takes more pollution for that kind of catastrophe to occur again, and recovery will happen quicker once pollution is cleaned. "level" is increased by $ \left \lceil \frac{1}{1000} \times map\_num\_tiles() \right \rceil $; for the first catastrophe, this increases the critical level by 1.5×.
- The appropriate "accum" is zeroed, so that the risk of further catastrophe is reduced to zero. (Of course, if the pollution remains above even the new "level", then the risk will start to increase again.)