Barbarians spawn logic

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 loop 1:
  {
    do loop 2:
    {
      random_x = from 0 to 12;
      random_y = from 0 to 12;
      x = city_x - 6 + random_x; //after 1st iteration, city_x and city_y are from previous city_B!
      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 = counter+1;
      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) //then show message
    {
      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);
      set unit visibility to (x,y) square visibility; //for example, if civs 0,2,4 explored this square -> then civs 0,2,4 will see the unit
      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.

Fixed June 9, 2020: actually city for next iteration is the closest city from previous iteration. So barbarians can spawn even not on continent with your city and also they can spawn further than 6 squares from your city (but not further than 6 squares from any city).
 
Last edited:
Great job! :clap:So the richer your civ is, the more likely they are to spawn. Makes sense. Also nice to see the logic behind chariot spawning.
 
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:
Back
Top Bottom