As I wrote AI for turn-based strategy

Hello. I think that from the heading it is clear that we will talk about the creation of artificial intelligence (hereinafter simply AI), about what decisions were made and what ultimately happened. But first you need to get you up to speed.



The game is written in the Lua programming language, so I will give examples of code in this language.



I will describe some details of the game that are important for AI:



  1. The game is a turn-based strategy. First, the player walks, then the AI ​​does its actions for each country. AI only works when you click Next move and has no idea what happens at another time.
  2. The game has a map on which you can recruit / move / deploy troops. AI must analyze it and make the necessary decisions.
  3. In the game, you can make peace / declare war / sign a non-aggression pact / make and terminate alliances. AI must be able to cope with this.
  4. Technology and political institutions are available only to the player. AI bonuses do not change from the beginning of the game, unlike the player.


The function that is responsible for starting the next move looks like this:



function next_step() --     --   ,    for k, v in pairs(game_data.lands) do calc_ai(k) end --  End
      
      





You must have noticed the magic function calc_ai



, which is responsible for all the actions of bots. You can still see one interesting detail: the function is performed alternately for each country.



Now we will answer the question of what happens in calc_ai



.



 function calc_ai(land) if land == game_data.player_land or land == "Undeveloped_land" or not game_data.lands[land] then return end --    ,     for k, v in pairs(game_data.lands) do local option = math.random() --  ,       . if k == game_data.player_land then ai_data = fixed_ai_data[game_data.difficulty].player else ai_data = fixed_ai_data[game_data.difficulty] end end move_army( math.random(), land) --  balance_army( math.random(), land) --   end
      
      





Until we deeply understand how AI makes decisions in politics, we note the following:



  1. There is a check that the AI ​​does not accidentally go for the player or for the incomprehensible Undeveloped_land. It’s just land without a country (In fact, a province should always have a country that owns it, so Undeveloped_land is the designation primarily for the game).
  2. The AI ​​selects different tables for interacting with the country, depending on whether the player controls it or not.
  3. AI first moves the army, and only then hires.


fixed_ai_data



- the table itself. It is named so because it does not change (more precisely, the variable always refers to the root table).

ai_data



- the table itself, which changes depending on whether the bot deals with the player or not (the variable simply refers to the desired table).



In fact, the game has only one table related to AI, and it looks like this:



 local ai = { standard = { peace = { conquer = 0.0001, war = 0.0002, war_neighbour = 0.004, pact = 0.005, agree_pact = 0.018, alliance = 0.001, alliance_small = 0.002, agree_alliance = 0.002, kick = 0.0005, envy = 0.02 }, war = { agree_peace = 0.005, }, player = { peace = { war = 0.0002, war_neighbour = 0.002, pact = 0.008, agree_pact = 0.08, alliance = 0.001, alliance_small = 0.002, agree_alliance = 0.002, kick = 0.0005, support = 0.005, voluntary_support = 0.002, envy = 0.01 }, war = { agree_peace = 0.02, } }, bonuses = { population_increase = 1.4, money_increase = 1.5, upgrade_province = 0.005 } }, }
      
      





standard



- level of difficulty. There are several levels of difficulty, and the values ​​in different tables vary.



peace



, war



- state of the country. The peace



table is used for any action, the war



table is only for actions with the enemy.



The third player



table contains the peace



and war



tables with the changed values. As you already know, in the game the AI ​​can have a different attitude to the player and bot.



The values ​​in the tables, I think, are clear by name. But in any case, we will consider some of them.



In the meantime, we need to figure out how the AI ​​works in the game. Everything is simple here, depending on the state, the bot can select any value from the list. The numbers above are the probability of occurrence of each event. Consider an example:

pact = 0.005



- means that with a chance of 0.005 a country will offer another country to conclude a non-aggression pact. Yes, everything is simple. You can say: “How then can you play, knowing that AI does everything at random?”. Actually, it’s not quite so, and we will analyze this a bit later.



In the meantime, look at the following function:



 function get_answer(option,state) local sum_option = 0 for k, v in pairs(state) do sum_option = sum_option + v if option < sum_option then return k end end if sum_option > 1 then print("AI Error, sum_option > 1") end end
      
      





option



- Random number from 0 to 1

state



- State of the country

The get_answer



function simply returns a random action. Why is it needed, and why you can’t just check option



<chance of action, I think it’s not necessary to explain.



This function is checked in all possible actions for AI:



 if __ and get_answer(option, ai_data.peace) == "pact" then _ end
      
      





Everything is simple. This, it would seem, can be finished, but no.



Let’s clarify a few points:



  1. AI with different chance declares war on neighbors and non-neighbors ( war



    , war_neighbour



    )
  2. Small countries are more likely to join unions than large ones ( alliance



    , alliance_small



    )
  3. AI has envy. The name is not entirely clear, but this is a chance with which the country will declare war too strong by the standards of the game power.
  4. We forgot about the bonuses table. It stores values ​​that have nothing to do with attitude to other countries, for example, a bonus to population growth or a chance to improve the province.


The interesting did not end there (unless you were interested, of course). It is strange when a country declares war for no reason, and even more strange when it is not ready for this war. Therefore, the AI ​​in the game has strategies. I will explain that instead of a surprise attack, in most cases the country starts a strategy.



Each country in the save file has the following fields:





That is, if AI starts a strategy, then he:



  1. Reduces his army and saves money.
  2. Hires the highest possible army.
  3. Declares war on the player.






With a certain chance, a country can put forward an ultimatum, in the fulfillment of which the war is canceled. For example:

Reduce your army in the province of lipetsk to 5,000, otherwise we will declare war on you.



From the funny: there is one interesting bug or even a flaw when the country demands to reduce the army in the province, and the player manages to lose the ultimatum until it expires. AI does not take into account the province and, if the ultimatum is not respected, declares war.







There are two variables that are responsible for the chance to support the player (transfer gold to the player). The first one always works, except for the state of war with the country for which it is calculated. And the second, with great significance, is taken into account when there is a common enemy.



Various variables were also added, for example, fear (how much the enemy army should exceed its own in order for AI to start thinking about the world). All this is done in order to make the actions of AI seem more logical and understandable. But anyway, now we know that this is actually an ordinary disguised accident.



I also noticed that the players really endow the AI ​​with some features unusual for him. For example, if at a certain moment a player declares war on several countries, then he will think first of all that this is a conspiracy against him, and not just a coincidence. And if the player is sent a proposal to conclude a non-aggression pact, he will think that the neighbor is afraid for his land. But, unfortunately, this is not so. Reality is full of disappointments.



All Articles