Exercises in Formula and Lua AI and AI-demos add-on feedback

Discussion of all aspects of the game engine, including development of new and existing features.

Moderators: Forum Moderators, Developers

Post Reply
mattsc
Developer
Posts: 1166
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Exercises in Formula and Lua AI

Post by mattsc »

Thanks, Anonymissimus. This is due to a change from 1.10 to 1.11 in how the weapon number is specified in 'ai.attack()'. Before 1.11 weapon numbers started at 0, which is inconsistent with any other numbering in Lua.

I will add a version checker to this part of the code to ensure that it works in both 1.10 and 1.11. I'll add an 'edit' to this post when it is done, until then this scenario will only work correctly in 1.10.

EDIT: Hmm, looks like there's a bug in trunk at the moment that doesn't let me fix this. I'll file a bug report, but until that is taken care of, I'm afraid that you'll have to use 1.10.

Derekkk
Posts: 64
Joined: April 25th, 2007, 5:43 pm

Re: Exercises in Formula and Lua AI

Post by Derekkk »

I just stumbled onto this thread and it led me to downloading the AI demo. This is some very impressive work! I have had my dabble in the wesnoth AI in the past, in particular before python AI became obsolete due to security issues. But Wesnoth might just a bit too complicated to have an elegant solution for an AI. Plus the fact that I found it difficult to analyse possible moves / look ahead without a facility to generate a mirror map on which to perform analysis. If someone builds this into the core engine, I would be very happy indeed...

Looking around your Lua code for Prune cart, there are some very nice ideas in there. I like how you consider several units at the same time and figure out if there is a suitable formation, something which is not really done by the old default AI. In the old default AR, every unit essentially fights for themselves and no real cooperation between units. Not sure how that has changed in the RCA AI though.

There seems to be quite a few bits of code in here which are fairly reusable. I wouldn't call the bit on formation easy-to-reuse (or understand :) ), but the other bits I am quite interested in having a play around to see what I can come up with.

mattsc
Developer
Posts: 1166
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Exercises in Formula and Lua AI

Post by mattsc »

Thanks, Derekkk! It would be great if you started using some of this and see what you can do with it. Since you have looked at the code, I probably don't have to point out that this is all still work in progress and not optimized at all (and that some of the code is still quite a mess). There are certainly things that need improvement or generalization (one simple example is that the code currently assumes that all units of a side have the same terrain defense stats). There also must be a few bugs that I haven't found yet, because sometimes the AI does moves that I cannot explain based on what I have coded.

I haven't done anything on this since my last post because I needed to do something else for a while (and I also was a bit frustrated with it). I will get back to it soon-ish. If you work on it, please let me know what you find, especially if there's something that could be improved. I have a long list of that myself, but it would be good to hear others' opinions.
Derekkk wrote:I wouldn't call the bit on formation easy-to-reuse (or understand :) )
Well, yeah, and I don't even know if it is a good way of doing this. But it seemed like a good starting point. :)

EDIT: Also, the code is not at all optimized for speed at this time. There's lots of duplication and things that can be done much faster in the end, but for now I am (mostly) only concerned with getting the behavior right.

mattsc
Developer
Posts: 1166
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Exercises in Formula and Lua AI

Post by mattsc »

mattsc wrote:Hmm, looks like there's a bug in trunk at the moment that doesn't let me fix this. I'll file a bug report, but until that is taken care of, I'm afraid that you'll have to use 1.10.
The bug's been fixed, so the 'Prune Cart' scenario can now be played with trunk. You need at least r54340 of Wesnoth and v0.6.7b of 'AI Demos'.

Saytiras
Posts: 1
Joined: June 4th, 2012, 1:50 am

Re: Exercises in Formula and Lua AI

Post by Saytiras »

Hi,

awesome work, really impressive :)

Sadly ive encountered a bug in the "Prune Cart" scenario with Wesnoth 1.10 and AI Demos v0.6.7b . Directly after the "This is WIP" notification the game crashes to desktop without any errors. Tested it on 3 different computers, Windows and Linux. Am I the only one with this problem?^^

mattsc
Developer
Posts: 1166
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Exercises in Formula and Lua AI

Post by mattsc »

Saytiras wrote:awesome work, really impressive :)
Hi, Saytiras. Thanks! :)
Saytiras wrote:Sadly ive encountered a bug in the "Prune Cart" scenario with Wesnoth 1.10 and AI Demos v0.6.7b . Directly after the "This is WIP" notification the game crashes to desktop without any errors. Tested it on 3 different computers, Windows and Linux. Am I the only one with this problem?^^
Hmm, strange. The next thing that happens after that message is a check for the Wesnoth version, but I thought that I have it set up so that it should work whether 'wesnoth.compare_versions()' exists or not. I've tried it in both 1.10.2 and 1.11.0-svn and don't get an error or crash (this is on a Mac).

Could you try to start the scenario from a terminal window with
/usr/local/share/wesnoth -l AI-demos-Prune\ Cart.gz
(note that the path to the Wesnoth executable might be different)
and tell me what error message you get then? (When the scenario crashes like that, it is almost certainly a Lua error, which should be displayed in the terminal window.)

Thanks much for the feedback!

EDIT: Just downloaded BfW 1.10.3 and tested AI Demos v0.6.7b. No problems with that either for me.

User avatar
lipk
Developer
Posts: 637
Joined: July 18th, 2011, 1:42 pm

Re: Exercises in Formula and Lua AI

Post by lipk »

Yeah, Prune Cart doesn't work for me either. I'm on Linux with Wesnoth 1.10.2. Here's the error message:

Code: Select all

20120604 13:46:19 error scripting/lua: ...1.10/data/add-ons/AI-demos/lua/prune-cart_engine.lua:8: bad argument #1 to 'require' (file not found)
stack traceback:
	[C]: in function 'require'
	...1.10/data/add-ons/AI-demos/lua/prune-cart_engine.lua:8: in main chunk
	[C]: in function 'require'
	[string "..."]:3: in main chunk
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
By the way, it's about time you updated your signature :P

mattsc
Developer
Posts: 1166
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Exercises in Formula and Lua AI

Post by mattsc »

lipk wrote:Yeah, Prune Cart doesn't work for me either. I'm on Linux with Wesnoth 1.10.2. Here's the error message:
Thanks. That was not at all what I had expected. It should be fixed in 0.6.7c.
[I am using Wesnoth Lua Pack for bebugging and had forgotten to comment out the line which loads the debug_utils library. Sorry for that! I should really make the loading dependent on whether WLP exists...]
lipk wrote:By the way, it's about time you updated your signature :P
:oops: Done.

Anonymissimus
Inactive Developer
Posts: 2460
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: Exercises in Formula and Lua AI

Post by Anonymissimus »

lipk wrote: PANIC: unprotected error in call to Lua API (attempt to index a nil value)
This line looks very frightening to me; the scenario failing or even being force-quitted to the titlescreen is expected behavior, but if the engine crashes to the desktop that's a bug. Please report.
I didn't get this, perhaps since I have the WLP locally ?
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml startersPlan Your Advancements: mp mod
The Earth's Gut: sp campaignSettlers of Wesnoth: mp scenarioWesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign

mattsc
Developer
Posts: 1166
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Exercises in Formula and Lua AI

Post by mattsc »

Anonymissimus wrote:This line looks very frightening to me; the scenario failing or even being force-quitted to the titlescreen is expected behavior, but if the engine crashes to the desktop that's a bug. Please report.
Will do. Wesnoth crashes on errors in Lua AI code happen reasonably frequently (although I do not remember off hand if I'm just making the same type of mistake over and over again). I thought that was known behavior (I thought I've read about it somewhere) and did therefore not report this. I'll do so from now on as I come across them.
Anonymissimus wrote:I didn't get this, perhaps since I have the WLP locally ?
Yes, that's also the reason why I did not notice it.

Edit: bug reported here.

mattsc
Developer
Posts: 1166
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Exercises in Formula and Lua AI

Post by mattsc »

AI-Demos v0.7.0 is out with a new scenario, demonstrating a whole bunch of different animal behaviors. This now brings the count to 10 scenarios and 25 different AIs -- although I am cheating a bit here, since I am counting the RCA AI in that (and a few custom routines by others), so there are really only about 20 of my own. ;) To quote from the scenario dialog:

This is just a fun little scenario with a bunch of different animal AIs, mostly for watching only. The animal AIs behave as follows:

General: These AI's are set up to simulate (to some extent) how these animals would act in real life. This includes that they are animals, meaning that they are not super smart. As an example, the wolves generally hunt in a pack, but are easily distracted by prey coming into range. They are also decent, but not great at cornering deer. For the most part, this is intentional. I'd be happy to help if you'd like somewhat modified behavior for your own scenarios - and, of course, if you see something that should be improved, please let me know.

Bears, Spiders and Yetis mostly just wander in their respective parts of the map. They stay out of each other's way (and out of the way of the dogs), but do attack each other if cornered. They will attack the other, weaker animals (deer etc.) if those are within range.

Wolves hunt in a pack of up to three. They actively go after the closest deer (as long as it stays in the forest) and try to corner it (not always super successfully), but are easily distracted by other prey coming into range. The wolves try to avoid getting into the range of bears, spiders, dogs and the yeti, except when they are going in for an attack. If you let them move for long enough, they will also learn that it is not healthy to attack the tusklets. When no deer is left, they wander randomly. Note that the wolf AI only works with 3 or fewer wolves.

Each deer wanders randomly on forest tiles, except when enemies get in its (the deer's) range, in which case it flees to the farthest point it can reach.

Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets. This enemy will then experience the full wrath of an irate boar.

Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.

Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close. They reappear out of their holes at the beginning of the turn, if it is safe.

Sheep dogs try to keep their sheep safe. This involves keeping them inside the area outlined by the path the dogs have worn into the meadow, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close. You might have to let the scenario play for quite some time before you get to see an interesting dog/wolf interaction. If no active herding or protecting move is needed, the dogs go to a random location on the path.

Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog. The dogs exploit this by positioning themselves on the outside of the sheep, if possible. The sheep, dogs and forest creatures (deer, tuskers, rabbits) have learned that they are no threat to each other and leave each other alone, demonstrating the enormous self control of a well trained sheep dog.

Boucman
Inactive Developer
Posts: 2119
Joined: March 31st, 2004, 1:04 pm

Re: Exercises in Formula and Lua AI

Post by Boucman »

This is awsome...
Fight key loggers: write some perl using vim

mattsc
Developer
Posts: 1166
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Exercises in Formula and Lua AI

Post by mattsc »

On request by a fellow Wesnothian, I have added another scenario, 'Wolves', with a different wolf pack behavior from 'Animals'. Because of how I want the packs to behave (e.g. all wolves in a pack move together, before we go on to another pack, even if not all of them do the same thing; or wolves being assigned to packs automatically), this turned out to be a bit more complex than I had expected, almost 500 lines of code for a "reasonably simple" AI. Point being, while I believe that I have done thorough bug testing, it's quite possible that I missed something and any comments concerning unwanted behavior would be much appreciated. Because of the complexity, I'll need a replay in order to track things down though.

Anyways, this is available in v0.8.0,
with the wolves behaving as follows (taken from the scenario dialog):
This scenario features a different kind of wolf behavior from 'Animals'. First, there can be an arbitrary number of wolf packs of up to three wolves each. At the beginning of the scenario, close wolves are grouped into packs of three (and packs of two or one for remaining wolves, if any). Wolves in a pack begin by joining each other on the map and stay together until only one wolf is left, which then tries to join up with a pack of two, or with other single wolves; as do individual wolves entering the map during the scenario.

Second, wolves do not actively hunt deer here, for the most part they just wander (often long distance). However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes. Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.

Two sides with seven wolves each fight each other in this scenario until only one side remains. To emphasize which wolf belongs to which pack, the pack number is displayed below each wolf.

It is possible to include a human-controlled Side 1, so that the action stops once every turn for looking around (or for messing with things in debug mode). In human-controlled mode, several options are available through the right-click menu, such as adding additional wolves to either side, taking wolves off the map, adding peasants to the human-controlled side or ending the scenario. This enables easy exploring of the wolf AI behavior under different circumstances.

mattsc
Developer
Posts: 1166
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Exercises in Formula and Lua AI

Post by mattsc »

And there's another new scenario, following the suggestion by yet another fellow BfW-er: 'Swarm'. Thanks for all the ideas, everybody!
Bats moving around in a swarm (or a flock, or colony, or whatever):
This scenario features bats moving around in a swarm. Without adjacent enemies, they simply try to stay together and at a certain distance from enemies. However, if an enemy unit is adjacent to any bat, the swarm scatters. This is particular fun to watch when one places an enemy unit in the middle of the swarm. After being scattered, the swarm members slowly rejoin, but not in a very organized way. Sub-swarms or individual bats might roam around for quite some time before they find their way back. It is also possible that individual bats (or small groups) split off from the larger swarm at times.

The player controls a side of gryphons, each of which is given 99 moves so that the reaction of the swarm to enemies can be tested easily. There are also several right-click options, for example for adding bats or gryphons or for taking units off the map.

User avatar
Simons Mith
Posts: 784
Joined: January 27th, 2005, 10:46 pm
Location: Twickenham
Contact:

Re: Exercises in Formula and Lua AI

Post by Simons Mith »

Hey mattsc, I found a description of a wolves' hunting algorithm in New Scientist 22/10/2011, pg11. In brief:

'Co-operation not vital to hunting in packs'

A virtual wolf pack may be possible with just two rules.
Each wolf 'moves towards prey until it reached a certain safe distance. It then moves away from any other wolves that had reached that distance.'

Simulated wolves (in a five-strong wolf pack) following these rules closed on prey and encircled it, and if the prey veered right or left, one of the wolves sometimes pulled away to maintain its distance from its fellows. That wolf ended up waiting in the prey's path, seemingly in ambush.

Further on in the same article, 'packs do not hunt more eficiently above a certain pack size, suggesting that larger packs enjoy little advantage in obtaining food.'

Thought you might find that interesting.
 

Post Reply