make a unit chase another unit

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
User avatar
bvanevery
Posts: 338
Joined: August 27th, 2009, 12:47 am
Location: Winston-Salem, NC

make a unit chase another unit

Post by bvanevery »

I have this psycho unit named Rashti. When she gets beaten up too badly, she splits in half. One half stays under player control, the other half joins the enemy side. I want the enemy half to mercilessly hunt down the player's leader. I can't seem to come up with AI tags that will actually accomplish that though. When I test the behavior in a game, I don't see any evidence of targeting. Even when the enemy half is right next to the player's leader, it prefers to attack other units.

Has anyone been able to implement reliable hunting behavior?
To Lands Unknown, an Arabesque adventure of stunning background art, mobile summoning, and strong storytelling.
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: make a unit chase another unit

Post by Anonymissimus »

With standard wml ai tags this is completely impossible. Maybe with new ai syntax.
You can perhaps do some "ai herding": Before the ai makes his decision (side turn event) make the target unit especially weak to the hunter's attack type. After it has attacked, revert (attack start event). Combine with an [ai][target] setting for a side containing only the chaser. (That setting only works if the chaser can't attack anyone else.)
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
User avatar
StDrake
Posts: 996
Joined: July 21st, 2009, 6:50 am

Re: make a unit chase another unit

Post by StDrake »

or you could make all other units hidden/allied for the duration of the evil halves' turn :)
it's a bit of cheating, and probably clumsy resourcewise, but might just work
Like cats? I've made a whole faction of them to kick ass with!
Don't like cats? I've made a whole faction of them to kick their asses! So everyone's happy :)
Felinian faction is part of the Beyond Southern Hells era
kitties need sprites! art topic here
User avatar
bvanevery
Posts: 338
Joined: August 27th, 2009, 12:47 am
Location: Winston-Salem, NC

Re: make a unit chase another unit

Post by bvanevery »

Anonymissimus wrote:With standard wml ai tags this is completely impossible. Maybe with new ai syntax.
I've been going by what's documented for AiWML. I see that "new" syntax is stuff from Wesnoth 1.8; I haven't looked at that section yet. Also the "old" documentation is not 100% complete, leaving me to guess at whether I need [filter][/filter] tags for subsections and so forth. The game didn't reject my WML though; I just wonder if it silently dismisses unworkable WML. I used the inspection tool to look at AI settings, but the information shown doesn't look like the concepts provided in WML. I guess I'll go read the AI forum for a bit.

The behavior I want to implement is really simple. Perhaps I should do it in Lua.
To Lands Unknown, an Arabesque adventure of stunning background art, mobile summoning, and strong storytelling.
User avatar
Alarantalara
Art Contributor
Posts: 786
Joined: April 23rd, 2010, 8:17 pm
Location: Canada

Re: make a unit chase another unit

Post by Alarantalara »

A solution using ForumlaAi.

Use the following code in the side definition for the side that will have the chasing unit.
Replace 2 with the actual side number
Replace hunter_id with the id of the unit that does the hunting
Replace target id with the id of the unit that is the target

The hunter unit will chase down the target and attack it if possible. It will only attack other units if it can't attack its target.

Code: Select all

[ai]
            {MODIFY_AI_ADD_CANDIDATE_ACTION 2 main_loop (
                [candidate_action]
                    id=escape_south
                    engine=fai
                    name=escape_south
                    type=movement
                    evaluation="if(me.id='hunter_id',{AI_CA_GOTO_SCORE}+10,0)"
                    action="move(me.loc, choose(unit_moves(me.loc),'mloc',-sum(map(simplest_path(mloc,find(units,id='target_id').loc,me.loc), 'path_location', movement_cost( me, path_location ) ))) )"
                [/candidate_action]
            )}
            {MODIFY_AI_ADD_CANDIDATE_ACTION 2 main_loop (
                    [candidate_action]
                        id=attack_gra
                        engine=fai
                        name=attack_gra
                        type=attack
                        evaluation="if(me.id = 'hunter_id' and target.id = 'target_id', {AI_CA_GOTO_SCORE} ,0)"
                        action="attack(me.loc, me.loc, target.loc)"
                    [/candidate_action]
               )}
        [/ai]
The ids and names are rather random since I copied some code I wrote for myself to get this working quickly. You probably want to change them to something meaningful.
User avatar
bigkahuna
Posts: 657
Joined: September 11th, 2010, 6:24 pm
Location: In your mind.

Re: make a unit chase another unit

Post by bigkahuna »

bvanevery wrote:I see that "new" syntax is stuff from Wesnoth 1.8; I haven't looked at that section yet.
It is 1.8+ and works fine on both versions.

{ai/aliases/stable_singleplayer.cfg}
Alarantalara wrote:A solution using ForumlaAi.
Some great stuff there, But you have to include this above the [ai] tag for it to work:

Code: Select all

{ai/aliases/stable_singleplayer.cfg}
Check out my campaign Sweet Revenge!
Join the new R2D forum!
Tet
Posts: 391
Joined: February 18th, 2009, 5:11 am

Re: make a unit chase another unit

Post by Tet »

There was allways a AI command to make all units attack the opposite leader. It is used in a lot of scenarios and was allways working. If you put your evil Rashti in a side of its own (But allied with the ather bad guys), it should work.

There is stuff like ignore bad combat and so on. Make sure to make the Rashti side agressive and focused on the leader.
My Temple Project: http://forums.wesnoth.org/viewtopic.php?f=23&t=29800
This is "must-play" campaign! Don´t read the thread, unless you need help. http://forums.wesnoth.org/viewtopic.php?f=8&t=31895
User avatar
Crab
Inactive Developer
Posts: 200
Joined: March 18th, 2009, 9:42 pm

Re: make a unit chase another unit

Post by Crab »

Alarantalara, note that there's next_hop formula function which makes the formula slightly less verbose.
it can be used like this:

Code: Select all

move(my_leader.loc,next_hop(my_leader.loc,loc(20,6)))
basically, it return the next 'this turn' destination for our unit if we know the end destination.
User avatar
Alarantalara
Art Contributor
Posts: 786
Joined: April 23rd, 2010, 8:17 pm
Location: Canada

Re: make a unit chase another unit

Post by Alarantalara »

I was actually copying a different suggestion you had made earlier. According to your post there, this more complicated code ensures that the unit still moves if the desired target is occupied (which this certainly is). Since next_hop existed then, I assumed this would remain a better option. Has the behaviour of next_hop changed? I'll certainly test it though.
User avatar
Alarantalara
Art Contributor
Posts: 786
Joined: April 23rd, 2010, 8:17 pm
Location: Canada

Re: make a unit chase another unit

Post by Alarantalara »

I finally got around to testing it and it turns out that there are some behaviour differences.
next_hop seems to try to go around enemy units, while the more verbose option tries to take the straight line route regardless of enemies with no backtracking.
For this specific instance, given that the unit is "psycho", I think the more verbose option may be the better choice here.
I'll definitely remember next_hop if I want more sane behaviour
Post Reply