movement cost and ai for a specific unit

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

Moderator: Forum Moderators

Post Reply
bejacomune
Posts: 11
Joined: February 20th, 2023, 8:38 am

movement cost and ai for a specific unit

Post by bejacomune »

Hello,
I wrote a code that make monsters spawns if certain conditions are met (it is inspired by many other coders) but in order to make it more adequate, I want to make the spawned units have special attributes.
First, I wanted to make certain units unable to walk outside certain type of terrain (the terrain they spawned on). The problem here is that I don't want to restrict the movement for a complete array (like flat, forest...) but a specific terrain id,
Second, I want to create a special AI applied only for certain spawned units to make them able to walk but avoid certain terrain

Code: Select all

#define SPAWN_MONSTERS SIDE
	[set_variable]
		name=spawning_category
		rand="forest,swamp,none"
	[/set_variable]

    [switch]
        variable=spawning_category
        # two types for testing 
        [case]
            value="forest"
            {VARIABLE count_max 5}
		   	[set_variable]
                name=type_spawner
                rand=Fire Dragon,Yeti
			[/set_variable]
            {VARIABLE terrain_spawner Rr}
		[/case]
        [case]
            value="swamp"
            {VARIABLE count_max 6}
            {VARIABLE_OP type_spawner rand (Giant Mudcrawler,Giant Scorpion)}
           	{VARIABLE terrain_spawner Ss}
		[/case]
	[/switch]
        [if]
            [not]
			[have_unit]
				side={SIDE}
				count=$count_max
			[/have_unit]
			[/not]
				[then]
					[set_variable]
						name=spawning_actually
						rand="1,1,0"
						# the value here is exaggerated for testing purposes
					[/set_variable]
						[if]
							[variable]
								name=spawning_actually
								equals=1
							[/variable]
								[then] 
									{SCATTER_UNITS 1 $type_spawner 0 (
										{EVERYWHERE}
											terrain=$terrain_spawner
											) (side,animate={SIDE},yes
											[modifications]
												[object]
													[effect]
														apply_to=movement_costs
														replace="true"
														[movement_costs]
															[not]
																id=$terrain_spawner
															[/not]
															terrain={UNREACHABLE}
# I think here the modifications for the movement will take place but I don't know what to do or how filter can be used
														[/movement_costs]
													[/effect]
												[/object]
											[/modifications] ) }
								[/then]
						[/if]
				[/then]
		[/if]
#enddef
	
#define MONSTER_SIDE SIDE
    [side]
        side={SIDE}
        controller=ai
        allow_player=no
        hidden=yes
        no_leader=yes
        color=brown
		[ai]
			agression=0.1
			caution=0.9		
		[/ai]
    [/side]
#enddef
	
# forest *^F*
# sand D*^*
# deep water Wo*^*
Shiki
Developer
Posts: 348
Joined: July 13th, 2015, 9:53 pm
Location: Germany

Re: movement cost and ai for a specific unit

Post by Shiki »

First,…
That is impossible. You can only set movement for terrain types, not for terrain ids.
However, you can tell the ai to avoid certain hexes. One way is to set parameters for the [ai] of one side – all units from that side will be affected by this.

Code: Select all

[side]
    …
    [ai]
        # The AI will not even move over terrain matching the filter inside the [avoid] tag.
        [avoid]
            # Standard Location Filter
            x,y=12,13
            radius=4
            [filter_radius]
                [not]
                    # You can use terrain IDs in the Standard Location Filter
                    terrain=*^F*
                [/not]
            [/filter_radius]
        [/avoid]
    [/ai]
[/side]
Second, …
MicroAIs can be used for that. The Lurker microAI can be set up to end moves only on specified hexes, while still being allowed to walk over others. Micro_Ais
Try out the dark board theme.
bejacomune
Posts: 11
Joined: February 20th, 2023, 8:38 am

Re: movement cost and ai for a specific unit

Post by bejacomune »

Shiki wrote: February 20th, 2023, 8:51 pm
First,…
That is impossible. You can only set movement for terrain types, not for terrain ids.
However, you can tell the ai to avoid certain hexes. One way is to set parameters for the [ai] of one side – all units from that side will be affected by this.

Code: Select all

[side]
    …
    [ai]
        # The AI will not even move over terrain matching the filter inside the [avoid] tag.
        [avoid]
            # Standard Location Filter
            x,y=12,13
            radius=4
            [filter_radius]
                [not]
                    # You can use terrain IDs in the Standard Location Filter
                    terrain=*^F*
                [/not]
            [/filter_radius]
        [/avoid]
    [/ai]
[/side]
Second, …
MicroAIs can be used for that. The Lurker microAI can be set up to end moves only on specified hexes, while still being allowed to walk over others. Micro_Ais
I undestand, that is unfortunate. Thank you.
However, can I set the [ai] to a certain type like we do to [time_of_day] instead of [micro_ai] (which is more complicated for me)?
Shiki
Developer
Posts: 348
Joined: July 13th, 2015, 9:53 pm
Location: Germany

Re: movement cost and ai for a specific unit

Post by Shiki »

No, micro AIs are something different. To do what you mean one would instead add a [micro_ai] which controls all units of the side.
The wiki article is quite long, but they can be straightforward. Here is an example:

Code: Select all

[event]
    name=start

    [micro_ai]
        # mandatory micro_ai stuff
        side=2
        ai_type=lurkers
        action=add

        # The meaning of this depends on the chosen micro AI
        [filter] # For all units except the leader
            canrecruit=no
        [/filter]
        [filter_location] # only ever move onto Forest hexes
            terrain=*^F*
            [not] # but not onto a Great Tree. As you can see, you use terrain IDs here.
                terrain=*^Fet
            [/not]
        [/filter_location]
    [/micro_ai]
[/event]
Try out the dark board theme.
bejacomune
Posts: 11
Joined: February 20th, 2023, 8:38 am

Re: movement cost and ai for a specific unit

Post by bejacomune »

Shiki wrote: February 21st, 2023, 10:25 pm No, micro AIs are something different. To do what you mean one would instead add a [micro_ai] which controls all units of the side.
The wiki article is quite long, but they can be straightforward. Here is an example:

Code: Select all

[event]
    name=start

    [micro_ai]
        # mandatory micro_ai stuff
        side=2
        ai_type=lurkers
        action=add

        # The meaning of this depends on the chosen micro AI
        [filter] # For all units except the leader
            canrecruit=no
        [/filter]
        [filter_location] # only ever move onto Forest hexes
            terrain=*^F*
            [not] # but not onto a Great Tree. As you can see, you use terrain IDs here.
                terrain=*^Fet
            [/not]
        [/filter_location]
    [/micro_ai]
[/event]

I see. But still, I was hoping that there is some kind of filter for both the terrain ID inside the movement_cost and the unit inside the ai (which could be very useful indeed).
Anyway. Thank you very much for the further explanation ^_^ ^_^
Post Reply