Random movement of units

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
Kal_Ultor
Posts: 17
Joined: April 28th, 2023, 12:01 pm
Location: Central Europe

Random movement of units

Post by Kal_Ultor »

I try to make a unit move randomly, so that no player or ai moves the unit but it will move 5 hex on a random path. I tried this:

Defining unit

Code: Select all

[event]
        name=prestart

        [unit]
            id=fireship
            type=boat
            x,y= 10,10
        [/unit]
    [/event]
Making an event when the ship is moved

Code: Select all

    #move ship randomly
    [event]
        name=turn end

        [move_unit]
            id=fireship
            check_passability=yes
            force_scroll=yes
            dir={MOVEMENT}
        [/move_unit]
    [/event]
The Macro

Code: Select all

#define MOVEMENT
[set_variables]
    name=dir_fireship
    [value]
        rand=N,NW,NE,E,W,S,SW,SE
    [/value]
    [value]
        rand=N,NW,NE,E,W,S,SW,SE
    [/value]
    [value]
        rand=N,NW,NE,E,W,S,SW,SE
    [/value]
    [value]
        rand=N,NW,NE,E,W,S,SW,SE
    [/value]
    [value]
        rand=N,NW,NE,E,W,S,SW,SE
    [/value]
[/set_variables]
#enddef
Any ideas, is my macro flowed (i guess but I don't know why/ how to do it better)
User avatar
Ravana
Forum Moderator
Posts: 3000
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Random movement of units

Post by Ravana »

MOVEMENT is called as value of dir key, so it should return some text value. Currently you have
dir=[set_variables] and later on try to close tag [/set_variables] which was never opened, since [set_variables] is value of dir, it is not treated as opening tag.

After solving syntax issues we get to

Code: Select all

[set_variables]
    name=dir_fireship
    [value]
        rand=N,NW,NE,E,W,S,SW,SE
    [/value]
    [value]
        rand=N,NW,NE,E,W,S,SW,SE
    [/value]
    [value]
        rand=N,NW,NE,E,W,S,SW,SE
    [/value]
    [value]
        rand=N,NW,NE,E,W,S,SW,SE
    [/value]
    [value]
        rand=N,NW,NE,E,W,S,SW,SE
    [/value]
[/set_variables]
[move_unit]
	id=fireship
	check_passability=yes
	force_scroll=yes
	dir=$dir_fireship[0].rand
[/move_unit]
However there is nothing about randomness so far. You could use [set_variable] with rand to get random data.
User avatar
Heindal
Posts: 1353
Joined: August 11th, 2011, 9:25 pm
Location: Germany, Karlsruhe
Contact:

Re: Random movement of units

Post by Heindal »

I've made something like that, but without a route. I'm using teleport to teleport the unit and the animate function of teleport that let it appear as if it is moving. Note - the unit I move has the id "Mover".

Code: Select all

[store_unit]
									variable=wanderermove
									[filter]
										id=Mover
									[/filter]
								[/store_unit]

								[set_variable]
									name=wandererchangex
									rand=-1..1
								[/set_variable]

								[set_variable]
									name=wandererchangey
									rand=-1..1
								[/set_variable]

								[set_variable]
									name=wanderx
									value=$wanderermove.x
									add=$wandererchangex
								[/set_variable]

								[set_variable]
									name=wandery
									value=$wanderermove.y
									add=$wandererchangey
								[/set_variable]

								[unstore_unit]
									find_vacant=no
									variable=wanderermove
								[/unstore_unit]

								[clear_variable]
									name=wanderermove
								[/clear_variable]

								[teleport]
									[filter]
										id=Mover
									[/filter]
									x=$wanderx
									y=$wandery
									animate=$movementsetting
								[/teleport]
								
You can combine it with a while loop to make the boat walk several steps or if the units are called Boat1 Boat2 and so on, you could make a while loop that moves several boats or units one after another.
The future belongs to those, who believe in the beauty of their dreams.
Developer of: Trapped, Five Fates, Strange Legacy, Epical, UR Epic Era
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses, Dwarf Dwarfson Dwarvenminer
Post Reply