Is there a way to make some of the player's units become AI controlled?

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
weewah
Posts: 101
Joined: October 31st, 2019, 7:11 pm

Is there a way to make some of the player's units become AI controlled?

Post by weewah »

Say I want to make a super berserker unit. While this unit is not near any enemies, it behaves just like any other unit.

But when an enemy in range, the super berserker will be controlled by the AI to run to the enemy and attack. So the super berserker is basically a unit that can never run from a fight. If he can attack an enemy, then he will, regardless of what the player wants.

I considered some alternatives but they don't quite work:
1) I could make the unit start its turn with no mp so it can't run. And then force move it to the enemy. But then the player can just choose to not attack with the unit.
2) I could make the unit start its turn by switching to an allied AI side if there are enemies in range. But this runs into a problem where I can have 3 berserkers and 2 enemies, and the AI kills 2 enemies with 2 berserkers and leaves 1 berserker with no enemy. Ideally the player would be controlling this leftover berserker after the 2 enemies die, but it is too late at this point because it would no longer be the player's turn.
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Re: Is there a way to make some of the player's units become AI controlled?

Post by vghetto »

Hmm, sounds like you want to use the droid command during the player's turn but limit it to specific units.

I don't know how to do that, but here's a possible workaround. (Similar to what you said in point 2)

Have side 1 controlled by the player and side 2 controlled by the AI, side 2 aggression should be 1 and caution should be 0. Side 1 and side 2 should have the same team name.

On the turn end of side 1, if the berserker unit still has attacks/moves left, then do a modify_unit and switch the unit side to 2 and try to retain the same number of moves and attacks. You should do that in the side 2 turn refresh event.

At end of side 2 turn, do modify_unit to all of its units and switch them back to 1.

There may be other better solutions out there :)

Edit: Oh, or switch around the sides if you want the crazy units to go first.
Side 1 is AI controlled, Side 2 is the human side.
Set the guardian status to the crazy units so they don't waste their mp and use them only to attack.
User avatar
octalot
General Code Maintainer
Posts: 783
Joined: July 17th, 2010, 7:40 pm
Location: Austria

Re: Is there a way to make some of the player's units become AI controlled?

Post by octalot »

weewah wrote: May 3rd, 2021, 7:11 am 1) I could make the unit start its turn with no mp so it can't run. And then force move it to the enemy. But then the player can just choose to not attack with the unit.
You can force it to [do_command][attack] too...
weewah
Posts: 101
Joined: October 31st, 2019, 7:11 pm

Re: Is there a way to make some of the player's units become AI controlled?

Post by weewah »

Oooh! I didn't know there was a way to make a unit attack.

It isn't quite what I wanted though, since it doesn't let me use any of the handy micro ais, but I suppose I can just manually code them. Or just have the units randomly move/attack I guess.
vghetto wrote: May 3rd, 2021, 8:05 am
Edit: Oh, or switch around the sides if you want the crazy units to go first.
Side 1 is AI controlled, Side 2 is the human side.
Set the guardian status to the crazy units so they don't waste their mp and use them only to attack.
This also works, but only for the super berserkers example.

I was also thinking of ideas like ranged arrows: whenever an archer unit runs out of mp, if an enemy is near, then the archer's "attacks left" is set to 0, an arrow unit spawns next to the archer, travels to the enemy, and attacks the enemy. I then transfer the arrow's xp to the archer and despawn the arrow.
Post Reply