First, your total food production must be calculated so it can be added to your total food supply. For each building you own, it's food production is added to a running total. (Note that it is possible for a building other than an agridome to have food production here. The game just doesn't have any other buildings that produce food).
Once the food production is calculated, it is added to the total food stored. Then the game calculates how much food your colony will eat.
The amount of food each colonist eats depends on how many colonists you have.
If totalPopulation > 9 then populationFoodMultiplier = 9
otherwise
populationFoodMultiplier = 10
(the default value)
Then the amount of food required is calculated.
amountFoodRequired = (totalPopulation * populationFoodMultiplier) / 10
So under normal circumstances (totalPopulation > 9) each colonist eats one units of food. Otherwise, each colonist eats 9/10 of a unit of food. (Always round down after the division). If amountFoodRequired is greater than your new amount of food stored, then people will starve.
The amountFoodRequired is not the amount taken from storage however. Your newFoodStored is decreased by totalPopulation, provided you have that much food, otherwise it is decreased by the newFoodStored (to 0). In other words, the amount of food required to prevent starvation is less than the amount of food your colonists will actually eat, if it is available. This is done after the starvation conditions are checked for.
Now, provided starvation is occuring, this is how it's done.
(This part has no effect on the starvation code, but is done anyways. The amount of food you fall short by is calculated:
foodDeficit = amountFoodRequired - newFoodStored
)
If totalPopulation < 4 then numPeopleToStarve = 1
If totalPopulation >= 4 then numPeopleToStarve = (amountFoodRequired - newFoodStored) / 2
(If for some reason the number of people starving is negative, which should never happen, then add 1 before dividing by 2. Also, note that newFoodStored is oldFoodStored + totalFoodProduction, and does not include the amount subtracted from what people eat during this round).
Now that we know how many people will starve on this round, we need to select which types of people we will starve (kids, workers, scientists). For this we use a variable stored in the Player class, which I shall refer to as starvationCase. Each time someone is starved to death, starvationCase is incremented modulo 5. (It will have a value from 0-4). (If for some reason it's value is outside this range, it's value will be incremented and then taken modulo 5, and execution will continue normally).
As a quick check, if numPeopleToStarve > totalPopulation then numPeopleToStarve = totalPopulation. The game will then kill people off in a certain sequence until numPeopleToStarve have died. The game will cycle through the population classes in the following order: kid, scientist, scientist, worker, kid. This is based on the value of starvationCase. If starvationCase = 0, 4 then a kid is starved. If starvationCase = 1, 2 then a scientists is starved. If starvationCase = 3 then a worker is starved. Hence the cycle of 5 classes given above. If there is no colonists left in that class of colonists when it comes time to starve one, then it is skipped over and starvation continues in the next class.
Example: If 3 people were to starve in one round. And then 7 people were to starve in the next round, they would starve in the following order:
1st round: kid, scientist, scientist (1 kid, 2 scientists have died)
2nd round: worker, kid, (restart cycle here) kid, scientist, scientists, worker, kid (3 kids, 2 workers, 2 scientists have died)
Example: If there are no workers still alive, and 6 people need to be starved, it's done in the following way:
kid, scientist, scientist, (worker is skipped over) kid, kid, scientist (3 kids, 3 scientists have died)
It's kind of amusing to think how morbidly "fair" or "efficient" this game is at killing people off. Everyone gets their turn. :ph34r: Even if starvation occurs 1000 time marks apart, the game won't have forgotten whose turn it was. :ph34r: