Check for item in terrain

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
Enderlook
Posts: 38
Joined: January 22nd, 2018, 12:23 am

Check for item in terrain

Post by Enderlook »

I am trying to put a basic item in some tiles, and later I want to check if there are items in other tiles.
I know how to add an item:

Code: Select all

                [item]
                    x,y=$x1,$y1
                    image=items/flower4.png
                [/item]
Also I know how to remove items:

Code: Select all

                [remove_item]
                    x,y=$x1,$y1
                [/remove_item]
But I can't find a way to check if there is an item in the terrain. Do you know how check that?

The think I am trying to make is a farmer.
In the first turn, I want to iterate over all the farmers in a farmland and add an item to each item. Later, in the second turn, I want to iterate over all the farmes in a farmalnd and with an item to get gold and remove the item.
So my idea is iterate over all the farmers in a farmland and: if there isn't an item add it, and if there is an item, remove it and give gold. But I am stucked in check for the item.
Last edited by Enderlook on January 26th, 2018, 10:15 pm, edited 1 time in total.
User avatar
Ravana
Forum Moderator
Posts: 2965
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Check for item in terrain

Post by Ravana »

I think Lua keeps internal list of all items in order to restore them on loading game, but you are not expected to use that.

You should keep your items in array and iterate those only.
Enderlook
Posts: 38
Joined: January 22nd, 2018, 12:23 am

Re: Check for item in terrain

Post by Enderlook »

Thanks for your idea. I search in internet how to use arrays in wensoth and I found [store_locations].
If someone wants to see, here is my code:

Code: Select all

#define FARMER_EVENT
	[set_variables]
		name=farms
		mode=insert
	[/set_variables]

	[event]
		name=side_turn
		first_time_only=no
		
		[store_locations]
			[filter]
				ability=NE_farmer
				side=$side_number
				[filter_location]
					terrain=Gg^Gvs,Gs^Gvs,Gd^Gvs,Gll^Gvs,Re^Gvs,Rb^Gvs,Rd^Gvs,Ss^Gvs,Sm^Gvs
				[/filter_location]
			[/filter]
			[and]
				[not]
					find_in=farms[$side_number].harvesters
				[/not]
			[/and]
		    variable=farms[$side_number].planters
		[/store_locations]
		
		[foreach]
			array=farms[$side_number].planters
			[do]
				[item]
					x,y=$this_item.x,$this_item.y
					image=items/flower4.png
				[/item]
			[/do]
		[/foreach]
		
		[foreach]
			array=farms[$side_number].harvesters
			[do]
				[remove_item]
					x,y=$this_item.x,$this_item.y
					image=items/flower4.png
				[/remove_item]
			[/do]
		[/foreach]

		{CLEAR_VARIABLE farms[$side_number].harvesters}

		[store_locations]
			find_in=farms[$side_number].planters
			variable=farms[$side_number].harvesters
		[/store_locations]
		
		{CLEAR_VARIABLE farms[$side_number].planters}
	[/event]
#enddef
User avatar
WhiteWolf
Forum Moderator
Posts: 769
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

Re: Check for item in terrain

Post by WhiteWolf »

For this I use [store_items] on the tile, and then if there's an item there, then

Code: Select all

[variable]
    name=stored_items.image
    contains="png"
[/variable]
will be true, if there is no item there, it'll be false.
Main UMC campaigns: The Ravagers - now for 1.16, with new bugs!
Old UMC works: The Underness Series, consisting of 5 parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
Enderlook
Posts: 38
Joined: January 22nd, 2018, 12:23 am

Re: Check for item in terrain

Post by Enderlook »

Thanks also for your idea. Now I'll make something like this:

Code: Select all

#define FARMER_EVENT
	[set_variables]
		name=farms
		mode=insert
	[/set_variables]

	[event]
		name=side_turn
		first_time_only=no
		
		[store_items]
			variable=farms[$side_number].harvesters
			[filter]
				ability=NE_farmer
				side=$side_number
				[filter_location]
					terrain=Gg^Gvs,Gs^Gvs,Gd^Gvs,Gll^Gvs,Re^Gvs,Rb^Gvs,Rd^Gvs,Ss^Gvs,Sm^Gvs
				[/filter_location]
			[/filter]
		[/store_items]
		
		[store_locations]
			[filter]
				ability=NE_farmer
				side=$side_number
				[filter_location]
					terrain=Gg^Gvs,Gs^Gvs,Gd^Gvs,Gll^Gvs,Re^Gvs,Rb^Gvs,Rd^Gvs,Ss^Gvs,Sm^Gvs
				[/filter_location]
			[/filter]
		    variable=farms[$side_number].planters
		[/store_locations]
		
		[foreach]
			array=farms[$side_number].planters
			[do]
				[item]
					x,y=$this_item.x,$this_item.y
					image=items/flower4.png
				[/item]
			[/do]
		[/foreach]

		[foreach]
			array=farms[$side_number].harvesters
			[do]
				[remove_item]
					x,y=$this_item.x,$this_item.y
					image=items/flower4.png
				[/remove_item]
			[/do]
		[/foreach]
		
		{CLEAR_VARIABLE farms[$side_number].planters}
	[/event]
#enddef
User avatar
Ravana
Forum Moderator
Posts: 2965
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Check for item in terrain

Post by Ravana »

Consider that when you store items you might want to check what image there is. In this case filter is specific enough that it has very low chance of conflicts.
Enderlook
Posts: 38
Joined: January 22nd, 2018, 12:23 am

Re: Check for item in terrain

Post by Enderlook »

Ok, I'll take that in consideration. Thanks you.
Post Reply