How to Wall with an AI

Article written by offwo200
Published on 08-03-2011; updated on 08-03-2011
Tags: AI

AI Walling

This guide on how to build walls with AIs is intended for those that know the basic structure of AI scripting but still have yet to learn how to use the whole range of facts, actions, and strategic numbers. An expert or beginner scripter will still learn from this article, though.

But I thought that AI walling was useless?

You are partially right, but I will prove you wrong. Certainly, an AI can’t go wandering around 5 minutes into the game, with palisade walls walling in every single building of the opponent and winning the game within 10 minutes. However, walls are still useful for defence, and I will show you some examples of (sort of) chokepoint walling. There is one catch, though. (Without the UserPatch) AI’s, when they cut down a tree that’s in the wall, can’t rebuild the wall there. It is annoying indeed, and this is why most scripts don’t wall.

Okay, so we’ve established the purpose of it, now what do I do first?

The first thing you must do while building a wall with an AI is to enable wall placement. Walls can be built on either of two perimeters 1 or 2. I’ll give you an example of how to do this:

(defrule
(true)
;Other conditions if you want
=>
(enable-wall-placement 1);put 2 here if you want to build ;round perimeter 2
(disable-self)
)

The CSPB recommends you do this at the start of the game, which will produce the least bugs. However, I have found that if you enable wall placement later, it allows you to incorporate buildings into the wall. However, this may produce some bugs and only do this if you know what you’re doing.

What are the differences between the two perimeters?

Perimeter 1 is closer to the town centre (or starting point of the player_land in a nomad), and perimeter 2 is further away. Perimeter 1 is usually between 10 and 20 tiles away from the TC, and tends to be the better one to build, though you might experience problems with villagers gathering resources. Perimeter 2 is between 18 and 30 tiles away from the TC, it is less useful and much more expensive, but you will have less problems with buildings and gathering resources. You can build a wall on both perimeters, but I don’t recommend this, since the gates don’t line up.

Okay, we’ve established which wall perimeter we’re using, and we’ve enabled this, but how do we build the actual wall?

It’s pretty simple. Here is a (very simple and unrealistic) example:

(defrule
(can-build-wall 1 stone-wall-line)
=>
(build-wall 1 stone-wall-line)
)

That was simple. Yes, you’ve guessed it, to do 2 you just change the "1"? for a "2"?. Simples! Also, notice that I used stone-wall-line to build a stone wall. "stone-wall-line" will allow us to build either stone walls or fortified walls, depending on whether we have fortified wall researched, so it's a lot more useful than just stone-wall. You can use also palisade-wall or fortified-wall (though this is useless) instead of stone-wall-line to build that kind of wall.

Okay, but I’ve just walled myself in!

Obviously we need to build gates. It is quite simple as well. Here is an example:

(defrule
(building-type-count-total stone-wall-line > 0)
(can-build-gate 1)
(building-type-count-total gate < 5)
=>
(build gate 1)
)

Yes, you wouldn’t need to build 5 gates as a human. But AI’s place their gates in random locations, and some may lead to dead ends, so I recommend at least 4. Also, villagers have sometimes an annoying habit to try to gather stuff through the wall, and having more walls diminished this effect.

Yay, my script now walls, but it does so even on Arabia...

If we take what we have so far:

(defrule
(true)
=>
(enable-wall-placement 1)
(disable-self)
)

(defrule
(can-build-wall 1 stone-wall-line)
=>
(build-wall 1 stone-wall-line)
)

;And gates

Your script will always wall, even when the map is unsuitable. However, now, if we do this...

(defrule
(wall-completed-percentage 1 > 66);Sneaky addition, this tells us that the wall perimeter 1
;is covered by more than 66% trees
=>
(enable-wall-placement 1)
)

(defrule
(wall-completed-percentage 1 > 66)
(can-build-wall 1 stone-wall-line)
=>
(build-wall 1 stone-wall-line)
)

Now, your script will wall perimeter 1 on maps like BF, but not on maps like Arabia. This is very useful for obviously reasons.

Walling parts?

You can (sort of) do this. You will have to add conditions like any other buildings e.g

(defrule
(building-type-count-total stone-wall-line < 10)
(wall-completed-percentage 1 > 66)
(can-build-wall 1 stone-wall-line)
=>
(build-wall 1 stone-wall-line)
)

This restricts the amount of wall pieces it can build to 10. Hopefully, the game will usually pick the smallest gap to wall with those 10 pieces. Don’t rely on this however.

And finally, a little trick using these facts

If you enable-wall-placement, and then use the fact wall-completed-percentage, you get the result 100 if it is a water map. Otherwise you get a normal result. This can be used to work out what kind of map you are on.

Conclusion

Walling with an AI is not useless. I hope you can make your scripts stronger by using this advice and you have learnt from this guide. Happy scripting!