Getting the AI to recruit custom units

Suppose for your custom civilization you want to replace Knights with Cobra Cars, and you want the default AI to be able to train Cobra Cars. To do that, you’ll need to a) use the Advance Genie Editor and b) edit unitlines.json.

Part 1: Advance Genie Editor
To make the Cobra Car trainable at the Stables with AGE:

  1. Go to the Units tab and navigate to the entry for Cobra Car (unit ID 748)
  2. Change Train Location to Stables (101) and Train Button to 2.

The Unit entry should now look something like this:

Unit

Now you need to create an effect that will trick the AI into treating the Cobra Car as a Knight-line unit:

  1. Create a new effect and add Disable tech effect commands for Cavaliers and Paladins (Techs 209 and 265 respectively).
  2. Add effect commands to upgrade Knights (38), Cavaliers (283) and Paladins (569) to Cobra Cars (748)
  3. Under the Techs tab, create a new tech. Set Repeatable to 1, Effect to the effect number of the new effect and Civilization to whatever civilization modifying. In my own testing, I set the civilization to Teutons, because that civ really likes to train Knights.

Your new Effect entry should look like this:
Effect

Your new Tech entry look something like this:

The default AI will now recruit Cobra Cars as if it was a Knight-line unit. However, the AI will not include Cobra Cars in its count of Knight-line unit. This probably isn’t an issue for the default AI, in that I think it uses the military-population parameter to control its army size. However, say that you’re using a custom AI and that custom AI has the following block of code:

(defrule
    (unit-type-count-total knight-line < 10)
    (can-train knight-line)
=>
    (train knight-line)
)

Because the AI does not include Cobra Cars in its count of Knight-line units, the AI will train way more than 10 Cobra Cars, because as far the AI is concerned, the Knight-line count is still 0. To get the AI to include Cobra Cars in its Knight-line count, you will need to edit unitlines.json.

Part 2: unitlines.json
Unitlines.json defines which unit belongs to which Line ID and is located inside AoE2DE\resources_common\dat. The only way to edit LIneIDs is by editing the original unitlines.json file; mod folders do not work. Therefore, you should backup unitlines.json before editing it.

Inside unitlines.json, search for the entry for “Knight Line.” It should look like this:

    {
      "Name": "Knight Line",
      "LineID": -287,
      "IDChain": [
        38,
        283,
        569
      ]
    },

As you may recall, 38 refers to Knights, 283 refers to Cavaliers and 569 refers to Paladins. To add Cobra Cars to this entry, add a comma after 569. Then create a new line and add 748, without a comma. The entry will now look like this:

    {
      "Name": "Knight Line",
      "LineID": -287,
      "IDChain": [
        38,
        283,
        569,
		748
      ]
    },

If we run that block of AI code again, the AI will recruit the correct number of Cobra Cars.

Special thanks to LeifEricson on the AI Scripting Discord for informing me of the existence of unitlines.json.

And here’s a video of the default AI training Cobra Cars:

3 Likes

After the Return of Rome update, the game should now be able to read custom unitlines.json scripts inside mod folders.

When you use the upgrade effect, the game will still treat the your new unit as if it was the old unit. In other words, any bonuses that applied to the original unit will be applied to the new unit, even if the new unit is in a different class compared to the old unit.

For example, suppose you want to replace Skirmishers with Huskarl. You first create new Huskarl and Elite Huskarl unit entries so that they can be trained at the Archery Range. Then you create an effect that upgrades units 6 (Skirmishers) and 7 (Elite Skirmishers) to Huskarls and Elite Huskarls respectively. Next, you start a match in Feudal Age and train some Huskarls from the Archery Range. And then you decide to research Fletching.

When you research that tech, you’ll notice the Huskarl suddenly has 1 range, despite Huskarls being an infantry unit. After researching Bodkin Arrow and Bracer, you’ll see that the Huskarl now has 3 range. You’ll also notice that Blacksmith infantry upgrades won’t apply to the Huskarl. This is because the game still considers the Huskarl to be a Skirmisher, which means archer attack upgrades will affect its attack range. Because archer attack upgrades only affect pierce damage, the Huskarl’s attack will not increase, because Huskarls only deal melee damage.

To stop archer attack upgrades from giving Huskarls extra range, we can change the Skirmisher’s unit class to an unused one, and create new effects and technologies that apply only to Skirmishers. Currently, Fletching/Bodkin Arrow/Bracer apply to all members of the Archer class (Class 0). If we change the Skirmishers’ classes to, say, a Raider class (Class 56), then Huskarls will stop receiving range upgrades. But this also means default Skirmishers won’t receive any range upgrades.

To make sure default Skirmishers can receive upgrades, we need to copy the four attribute modifier commands that affect Class 0, paste them three into three separate new effects, and change them so that they apply only to Skirmishers, Elite Skirmisher and Imperial Skirmishers; you should have 12 commands per effects. Note that Class should be set to -1 if you’re targeting specific units with effect commands, or else the command will affect all members of that class.

SkirmExample

Under the Padded Archer Armor, Leather Archer Armor and Ring Archer Armor Effects, copy the two existing existing Class 0 commands into new commands that will give armor upgrades to Class 56 units. Finally, under the Chemistry effect, once copy and paste the existing Class 0 command into a new command that will give Class 56 units +1 attack.

Switching to the Techs tab, we now need to create three new technologies that will apply the archer attack upgrades to default Skirmishers. For the first tech, which we will name Flethcing Skirm, set a required tech to flethcing (199), Min Req Tech to 1 and the effect to your flethcing skirmisher effect (make sure civilization is also set to -1). Repeat the same for the other two techs, keeping Min Req Tech to 1 but changing the required tech to Bodkin Arrow or Bracer.

Finally, for the civilization(s) that will be getting the Skirmisher-Huskarl upgrade, go into each civilization’s tech tree effect and create three Disable tech commands to disable those three new Skirmisher techs. By doing this, Flethcing, Bodkin Arrow and Bracer will now be applied exclusively to Skirmishers and not those custom Huskarls.