Cossacks 3

Cossacks 3

OCMOD3
Crazy Cossack  [developer] 29 Oct, 2017 @ 1:09pm
Modding the AI
The question has been asked, can we do something about making the AI better? The answer is yes but it is a hard project. I am mainly a parameter modder which means I am not good at new programming. A certain amount can be done with parameter modding. Examples would be changing market sales and changing units produced by nations and doing this to suit the overall new balance in OCMOD3. For example, with a 5,000s resources start, Algeria can rush best in the current OCMOD3 by selling all remaining iron for stone after making blacksmith, all coal for wood and selling 4,000 gold (2,000 for stone and 2,000 for wood). This requires an early iron mine too. Then an AI can be programmed to spam light infantry which are good in OCMOD3.

However, those kind of changes only begin to scratch the surface of making a true AI. What I described above is a scripted AI. It follows a set script for resource starts, nations and units produced. The trouble with a set script is that it is unresponsive. It also does the same thing every time except that a few random parameters may be used to introduce a little variety. I mean like making pikes or muskets, heavy horse or dragoons.

What is really needed is to get away from running a scripted, non-responsive AI. How would we do that? A responsive AI would need to respond to three key things. One, it would respond to the resource start. That is to say it would make different decisions based on how many resources it started with. Two, it would respond to the mod it was in. That is to say it would figure out best builds based on the parameters a particular mod set or the vanilla for that matter. Three, it would respond to opponent actions intelligently by using scouting and not cheating. How could an AI be made to do these things?

To respond as above, the AI needs to be goal seeking in the way that a chess program, for example, is goal seeking. However, an RTS AI cannot rely on brute force searches of a move tree as chess programs mainly do. The RTS "tree" of branching possibilities is vastly bigger than the chess "tree". As a start, the C3 AI should use some basic heuristics (rules of thumb) and possibly some limited tree searches.

To get the AI project under way we would have to use the divide ad conquor approach. Divide the problem into sub-components. Try to solve these one at a time and then try to put the components together (which would make a system of sub-systems needing feedbacks between the sub-systems.

The first problem to solve is the economy problem. Let us consider a general rule. The bigger economy you make, the bigger military you can make. Thus, the first problem to solve is the economy problem. The idea would be to solve this problem in isolation first. In other words, make an experimental mod which only made economic units and structures. Then try to solve the problem of how to make the AI grow the economy as possible.

Here we can leverage a heuristic, a rule of thumb of all RTS. It is well known that you cannot grow your economy as fast as possible if you leave too many resources in store. The heuristic rule is "Convert all your resources into buildings and units as fast as possible." Do not have idle resources." However, this rule in turn can run afoul of the need (a bit later) to save resources for upgrades. Nevertheless, we would put this concern aside at first and just build the routines to run the "Spend!" heuristic as we might call it.

How can we make goal-seeking routines for the "Spend!" heuristic? The answer is to give a bonus to resources embodied in structures and units as compared to resources in store. First, you convert all resources to "gold equivalent" (easy with the flat market of OCMOD3). Then you can get a stored resource score which is the value of resources in store in gold equivalent. Then you give a bonus, probably simply of 2x (times two) to resources embodied in structures (and later in units). A tree search routine would search possible sales and builds and pick the highest scoring answer. Then it would follow that build path.

However, even this is probably too sophisticated for a first cut attempt. It would be easier to "one-step" the build by having the AI do this;

(1) Sell all resouces to a floor for each resource. (I will come back to the floor issue).
(2) Build the most valuable building available.
(3) build the next most valuable building available and so on.
(4) Return to Step 1.

Here, the most important thing would be the floors set on market resources. These floors would have to float and indeed rise as the game goes on. Currently, the vanilla AI has "don't starve" routines. These rely on more than market floors. They will reassign peasants to the field if I recall correctly. In our case, the AI would set market floors in special ways and these floors would rise as the game goes on. These floors would prevent selling a resource that is adequate for a "next needed building" and there are ways of determining that. These floors would keep reserves of food to reduce starvation risks. These floors would keep reserves of iron and coal, once these are needed for possible musket fights. These floors would allow for saving towards "next required upgrade".

To cut a long story short, the above are just ideas to get a smart economic system. Then there is the military build and actual military tactics and strategies. However, as a first start of learning the AI system, I could improve as follows;

(a) Improve the scripting. (The above would be a longer term project).
(b) Improve unit choices.
(c) Get the AI to use proper formations as OCMOD3 mandates formation use, if you want to win.

I have several projects on the go. I will look at this when I can.