Need assistance with Formula AI

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

Moderator: Forum Moderators

User avatar
ShikadiQueen
Posts: 74
Joined: October 4th, 2007, 6:12 pm
Location: Norsula IV
Contact:

Need assistance with Formula AI

Post by ShikadiQueen »

I basically want to write a custom AI for "Invasion from the Unknown". In scenarios 9 (the library), 20 (under the sands), 22B (Gauntlet), 23A (Interim), 23B (the lair) and 23C I use fixed spawn points for foes which have their own side, and are initially ai_special=guardian. This works fine for part of my purpose, which is to make them attack the player only when a player unit enters the line of view of these "drones". However, with the default AI I'm lacking the other part, which is to make them retreat to their initial positions when a player unit goes too far from the drones' spawn points.

I'd really appreciate guidance on making this approach possible with Formula AI; it'd be easier for me to learn rather than Python.

Thanks in advance.
I got better!
Dave
Founding Developer
Posts: 7071
Joined: August 17th, 2003, 5:07 am
Location: Seattle
Contact:

Re: Need assistance with Formula AI

Post by Dave »

Good question!

Basically you want a formula that can apply to specific units, and want to be able to store information (the spawn points) in each unit.

This is actually a little ugly to do in the formula AI at the moment, so I've asked some people interested in GSoC to implement it. Hopefully we'll have a nice implementation soon.

I'll get back to you when we do.

David
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming
Dave
Founding Developer
Posts: 7071
Joined: August 17th, 2003, 5:07 am
Location: Seattle
Contact:

Re: Need assistance with Formula AI

Post by Dave »

Thanks to Dragonking for some changes he made to make this relatively easy to do. :-)

To do this, set ai_algorithm=formula_ai in your unit's [side] tag. Then here is a unit that has behavior like what you're talking about:

Code: Select all

        [unit]
            x,y=8,5
            type="Orcish Archer"
            generate_description=yes
	     formula="if(attack, attack, move(me.loc, choose(unit_moves(me.loc), -distance_between(self, me.vars.guard_loc))))
			    where attack = choose(filter(attacks, units = [me.loc] and distance_between(me.vars.guard_loc, target) <= me.vars.guard_radius), avg_damage_inflicted)"
		[ai_vars]
			guard_radius=3
			guard_loc="loc(8,5)"
		[/ai_vars]
        [/unit]
This unit has been set to guard (8,5) (the 'guard_loc' attribute). It will guard a radius of three tiles around it (the 'guard_radius' attribute). The unit will remain on the tile. Whenever an enemy comes within three hexes, the unit will attack it. If the unit moves back outside the three tile radius, the unit will return to the tile it is guarding and continue to guard it.

Hope that helps! Let me know if you have any problems with it.

Naturally this works in current SVN HEAD, and no-where else. :-)

David
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: Need assistance with Formula AI

Post by Sapient »

meanwhile, in 1.4 you could do this using unit.variables.guard_x, guard_y and an ai turn that stores units matching a certain location filter then modifies their goto_x and goto_y attributes accordingly.
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
User avatar
ShikadiQueen
Posts: 74
Joined: October 4th, 2007, 6:12 pm
Location: Norsula IV
Contact:

Re: Need assistance with Formula AI

Post by ShikadiQueen »

Great, thanks. It'll take us a relatively large amount of time and work to migrate our campaign for SVN trunk, but I suppose we can deal with it with the help of the lord of wmllint. ;)

Sapient: will take it in mind, thanks.
I got better!
ckarai
Posts: 35
Joined: March 26th, 2008, 3:57 pm

Re: Need assistance with Formula AI

Post by ckarai »

Hi!

I intend to construct a woodchopper unit.

A woodchopper chops wood and gives money to the owner of the unit.
The main goal in this scenario would be to stop destroying the forest.

The AI should find the nearest forest-like terrain, and go there to chop the wood.

Can it be made with formula_ai?

I've read the docs ( http://www.wesnoth.org/wiki/FormulaAI ), and it seems to me, that formula_ai will not support this kind of behaviour. I've found a similar distance_to_nearest_unowned_village function, but I need distance_to_nearest_terrain.

I think, I could do it using a python script, but I've read that you intend to remove python scripts because of security reasons.

BTW: I'm developing a multiplayer campaign and I'll put it onto the campaign server as soon as I finish the first 3 scenarios.

Thanks,

Csaba
AI
Developer
Posts: 2396
Joined: January 31st, 2008, 8:38 pm

Re: Need assistance with Formula AI

Post by AI »

I doubt it can be done with formulaAI, but it might be possible with normal AI: event ai turn that sets goto_x and goto_y for a forest tile.

The chopping ability itself should be pretty easy.
ckarai
Posts: 35
Joined: March 26th, 2008, 3:57 pm

Re: Need assistance with Formula AI

Post by ckarai »

Thanks, I'll try it. I'm very new in developing WML.

My first impression of WML is, that it can be used for a lot of things, but it's quite hard to learn. As I am a software developer, I think, that an average computer user will never be able create his/her own campaign using WML.

The planned scenario editor would be a good solution to this problem.
AI
Developer
Posts: 2396
Joined: January 31st, 2008, 8:38 pm

Re: Need assistance with Formula AI

Post by AI »

Actually, the reason WML exists rather than a python or something is because non-programmers actually find it easier to use.
ckarai
Posts: 35
Joined: March 26th, 2008, 3:57 pm

Re: Need assistance with Formula AI

Post by ckarai »

I have a problem.

For chopping I have to find the nearest forest tile.

- store_locations can tell me every forest tiles

How can I get the minimum distance (with movement costs) from one forest tile to the chopper unit?

The filter has a radius parameter, but I think, that it doesn't compute the movement costs.
AI
Developer
Posts: 2396
Joined: January 31st, 2008, 8:38 pm

Re: Need assistance with Formula AI

Post by AI »

There is no way to do that in WML I think (other than checking the terrain for every tile and computing it all manually), but depending on the map, that might not be all that bad.

As for chopping the trees, I'd say the woodcutter should be inside the forest in order to cut it down, rather than next to it. (which also makes the coding easier)
ckarai
Posts: 35
Joined: March 26th, 2008, 3:57 pm

Re: Need assistance with Formula AI

Post by ckarai »

> There is no way to do that in WML I think (other than checking the terrain for every tile and computing it all manually), but depending on the map, that might not be all that bad.

It won't work. Imagine, that the following situation:

F = forest
E = enemy unit
C = chopper
O = other

F F O O
F F E O
F F E C
F F E O

The chopper should bypass the enemies to chop the trees. So I should also check the locations of the enemy units. Doing this for 22 chopper would cause huge performance problem.

It seems to me, that the python script could solve this problem, but I wonder how long it will be supported.

It would be nice to add a new WML command, which stores the distance between two locations in a variable. It's just a simple function call in the C++ side.
ckarai
Posts: 35
Joined: March 26th, 2008, 3:57 pm

Re: Need assistance with Formula AI

Post by ckarai »

It's very similar to the villages. The same algorithm can be used to locate the forest fields.

I think this feature would be important, as it makes resource collecting possible.
AI
Developer
Posts: 2396
Joined: January 31st, 2008, 8:38 pm

Re: Need assistance with Formula AI

Post by AI »

If you set the goto vars, the game should take care of the path by itself, though this won't fix the issue of the closest forest tile being surrounded (or occupied) by enemies.
ckarai
Posts: 35
Joined: March 26th, 2008, 3:57 pm

Re: Need assistance with Formula AI

Post by ckarai »

If I wrote a patch to WML, would you commit it?

1.

[distance_from_unit]
variable=variable_name
StandardUnitFilter
x, y
[/distance_from_unit]

Returns an array with the:
ret.id -> the id of the unit
ret.distance -> the distance from the location.

If the location is unreachable, 100 is returned.

2.

[store_locations]
effective_radius=movement costs
[/store_locations]

The effective_radius would be the radius, that a unit can reach in X steps.
- if there's no unit selected by the filter, an empty array is returned
- if there's more than one, then the array is or-ed
Post Reply