# Barbarians spawn logic

Discussion in 'Civ1 - General Discussions' started by SWY, Mar 18, 2018.

1. ### tupiChieftain

Joined:
Jun 25, 2011
Messages:
55
Location:
Russia
And here it goes: land spawning logic. It's a pretty big chunk. I tried to write it in preudocode for anybody to understand, dunno about how successful I was in achieving that. At least it can be useful for CivOne project.
Code:
```every turn:
random call: from 0 to 127
IF a city with such index exists AND it's a human player city
THEN
find city value = booty from hypothetical city capture: city_pop*civ_treasury/(civ_pop+1)
random call: from 0 to 99
IF its result is strictly less than city_value:
{
counter = 0;
do loop1:
{
do loop2: //you don't need 2 loops here, but I copy it from Ghidra decompiler as is
{
random_x = from 0 to 12;
random_y = from 0 to 12;
x = city_x - 6 + random_x;
y = city_y - 6 + random_y;
find closest city to (x,y); //it returns city id. let's call it city B...
find distance between city B and (x,y);
counter++;
if (counter >= 99)
finish the loops; //no barbarians for you today
}
while distance < 6
OR there's any non-barbarian unit on land (not ocean) in 8 squares adjacent to (x,y)
OR (x,y) is ocean
OR some unit in (x,y) //even if barbarian
}
while city_B and (x,y) are on different continents

IF (counter < 99)
{
IF civ of city B == human player civ
AND human player civ explored square (x,y) //only you! not comp. player!
{
change map view position of human civ to (city B x - 8, city B y - 6);

IF no civ knows INDUSTRIALIZATION
THEN
{
IF no civ knows GUNPOWDER
THEN
draw message 1; //barbarian uprising near city B!
ELSE
draw message 2; //native unrest
}
ELSE
draw message 3; //guerrilla

Var_C = random from 0 to cityB_size-1;
Var_C = Var_C/2;
IF Var_C < 2 THEN Var_C = 2;
IF Var_C > 99 THEN Var_C = 99;
counter = 0;
WHILE (counter <= Var_C)
{
IF no civ knows INDUSTRIALIZATION
THEN
{
IF 3rd bit of 'counter' variable == 0 //if (counter&4 == 0)
unit = CAVALRY;
ELSE
unit = CARRIOT;
}
ELSE
{
IF 3rd bit of 'counter' variable == 0
unit = MUSKETEERS;
ELSE
unit = CANNON;
}
if (counter == Var_C)
unit = DIPLOMAT;

spawn UNIT in (x,y);
if (city B civ == human_civ)
draw_unit in (x, y)
counter++;
}
}
}```
So, first 4 units are cavalry/musketeers and next 4 units are carriots/cannons and next 4 units are cavalry/musketeers again and so on. Last one is always diplomat.

I was wrong about techs though. Fixed it now for both land and sea spawning. There's only one list, but every entry is 2 bytes long, not 1. This list is started at 0x4b0. Actual techs are much more surprising for me, even if they still have some sense.

Last edited: Mar 21, 2020
Tristan_C and axx like this.
2. ### axxChieftain

Joined:
Feb 19, 2011
Messages:
93
Great job! So the richer your civ is, the more likely they are to spawn. Makes sense. Also nice to see the logic behind chariot spawning.

3. ### tupiChieftain

Joined:
Jun 25, 2011
Messages:
55
Location:
Russia
I fixed demanded civilization advances in listings (and some other small things). For sea barbs they are kinda surprising, but now I'm 100% sure about them.

Last edited: Mar 21, 2020