Check units current defense

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.
User avatar
Vyncyn
Forum Regular
Posts: 515
Joined: April 6th, 2013, 5:51 pm

Check units current defense

Post by Vyncyn »

I want to have an unit with an ability to attack units over a larger range. I've already figured out how to use [set_menu_item] with [harm_unit] to do (nearly) what I want.
The thing is, it currently hits 100% of the time and I want it to act like a normal attack (hit at a chance specified by the terrain the defender is standing on).

Is there a way to access the defending units current defense?
If not, does any other way exist how I could achieve this?
Also, is it possible to check if the defender is invisible?

If it helps, here is my current code

Code: Select all


#define VR_RANGE_MENU_ITEM DAMAGE
	#event: select ballista -> store ballista
	[event]
		id=vr_select_range_ab
		name= select,moveto
		first_time_only=no
		[filter]
			side=$side_number
			ability=vr_range
			[filter_wml]
				attacks_left=1
			[/filter_wml]
		[/filter]
		[store_unit]
			variable=vr_ballista_unit
			[filter]
				x,y=$x1,$y1
			[/filter]
		[/store_unit]
	[/event]
	
	[set_menu_item]
		id=vr_range_menu
		description="Fire arrow"
		[show_if]
			[have_unit]
				[not]
					side=$side_number
				[/not]
				x,y=$x1,$y1
				[filter_owner]
					[enemy_of]
						side=$side_number
					[/enemy_of]
				[/filter_owner]
			[/have_unit]
			[and]
				[have_location]
					[and]
						x,y=$x1,$y1
						radius=2
					[/and]
					[and]
						[filter]
							find_in=vr_ballista_unit
							side=$side_number
							[filter_wml]
								attacks_left=1
							[/filter_wml]
						[/filter]
					[/and]
				[/have_location]
			[/and]
		[/show_if]

		[command]
			{VR_EASY_SET_VARIABLES vr_ballista_unit.attacks_left 0 value}
			{VR_EASY_SET_VARIABLES vr_ballista_unit.moves 0 value}
			[unstore_unit]
				variable=vr_ballista_unit
				find_vacant=no
			[/unstore_unit]

#hits 100%
			[harm_unit]
				[filter]
					x,y=$x1,$y1
				[/filter]
				[filter_second]
					find_in=vr_ballista_unit
				[/filter_second]
				amount= {DAMAGE}
				damage_type=pierce
				animate=yes
				experience=yes
				fire_event=yes
				[primary_attack]
					name=longshot
				[/primary_attack]
			[/harm_unit]
			{CLEAR_VARIABLE vr_ballista_unit}
          [/command]
     [/set_menu_item]
#enddef
what it does right now is:
store attacking unit, when selected (or moved)
when you right click on the unit you want to attack it checks if the attacking unit is in range
if it is in range you can click a "fire arrow" button. This deals the defender a preset ammount of pierce damage
The attacking unit looses all it's movement and both units get exp (like in a normal attack).
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: Check units current defense

Post by gfgtdf »

do you use wensoth 1.12 or 1.13 ?
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
Ravana
Forum Moderator
Posts: 3002
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Check units current defense

Post by Ravana »

Doable with Lua https://github.com/ProditorMagnus/Agele ... er.cfg#L81

There was discussion about making it visible from WML, but not sure if anything happened there.
User avatar
Vyncyn
Forum Regular
Posts: 515
Joined: April 6th, 2013, 5:51 pm

Re: Check units current defense

Post by Vyncyn »

gfgtdf wrote:do you use wensoth 1.12 or 1.13 ?
1.12
Ravana wrote:Doable with Lua https://github.com/ProditorMagnus/Agele ... er.cfg#L81

There was discussion about making it visible from WML, but not sure if anything happened there.
Thank you, I'll have a look at it. I'm not really good with Lua though.
Would be great if it will be implemented for WML.
User avatar
UnwiseOwl
Posts: 516
Joined: April 9th, 2010, 4:58 am

Re: Check units current defense

Post by UnwiseOwl »

Would be great if it will be implemented for WML.
Seconded. I was also trying to do this recently.
Maintainer of the Imperial Era and the campaigns Dreams of Urduk, Epic of Vaniyera, Up from Slavery, Fall of Silvium, Alfhelm the Wise and Gali's Contract.
But perhaps 'maintainer' is too strong a word.
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: Check units current defense

Post by gfgtdf »

note that your code which uses a select event to store data isn't mp safe and will casue OOS in when used in multiplayer. ow to fix this is unfortunateley differnt betwen 1.12 and 1.13:
for 1.12: add needs_select=yes, there are still some corner cases where this wont worn correctly though.
for 1.13: sync manually by using [sync_variable]

EDIT:
Vyncyn wrote:Would be great if it will be implemented for WML.
creating a wml tag that does this for your is quire easy, just add

Code: Select all

[lua]
code = <<
function wesnoth.wml_actions.store_unit_defense(cfg)
  local u = wesnoth.get_units(cfg)[1]
  local def = wesnoth.unit_defense(u, wesnoth.get_terrain(cfg.loc_x, cfg.loc_y))
  wesnoth.set_variable(cfg.variable or "terrain_defense", def)
end
>>
[/lua]
somehwere in a preload event and you have a [store_unit_defense] tag that takes id=, loc_y=, loc_x= and a variable= keys.

EDIT2: you prbabyl shodul name it differntly and prefix it with your addon abbivation to prevent naming clashes.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
Vyncyn
Forum Regular
Posts: 515
Joined: April 6th, 2013, 5:51 pm

Re: Check units current defense

Post by Vyncyn »

Sorry, I don't understand this. I put this code in the cfg file

Code: Select all

#define VR_LUA_BALLISTA_RANGE
	[event]
		id=vr_lua
		name= preload
		first_time_only=no
		[lua]
			code = <<
			function wesnoth.wml_actions.store_unit_defense(cfg)
			  local u = wesnoth.get_units(cfg)[1]
			  local def = wesnoth.unit_defense(u, wesnoth.get_terrain(cfg.loc_x, cfg.loc_y))
			  wesnoth.set_variable(cfg.variable or "terrain_defense", def)
			end
			>>
		[/lua]
	[/event]
#enddef
and added {VR_LUA_BALLISTA_RANGE} to the [era] and this code

Code: Select all

			[store_unit_defense]
				id=vr_ballista_defender_defense_id
				loc_x=$x1
				loc_y=$y1
				variable=vr_ballista_defender_defense_var
			[/store_unit_defense]
in the [command] tags. Now all it does it give my this error
lua-error.png
User avatar
Ravana
Forum Moderator
Posts: 3002
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Check units current defense

Post by Ravana »

Means your filter doesnt match any unit.
User avatar
Vyncyn
Forum Regular
Posts: 515
Joined: April 6th, 2013, 5:51 pm

Re: Check units current defense

Post by Vyncyn »

Ravana wrote:Means your filter doesnt match any unit.
Ok thanks.
I got rid of the error, but It still doesn't work. How do I access the value of the stored defense?
User avatar
Ravana
Forum Moderator
Posts: 3002
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Check units current defense

Post by Ravana »

This code creates normal wml variable, so with $.
User avatar
Vyncyn
Forum Regular
Posts: 515
Joined: April 6th, 2013, 5:51 pm

Re: Check units current defense

Post by Vyncyn »

Totally forgot that. Everything seems to work now, thanks.

Edit: Or not. For some reason it uses the defense the attacker would have if he stands on the defenders terrain or something like that.
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: Check units current defense

Post by Sapient »

Vyncyn wrote:For some reason it uses the defense the attacker would have if he stands on the defenders terrain or something like that.
Instead of using "$x1,$y1", you probably want to use the location of the vr_ballista_defender.
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
User avatar
Vyncyn
Forum Regular
Posts: 515
Joined: April 6th, 2013, 5:51 pm

Re: Check units current defense

Post by Vyncyn »

But $x1,$y1 should be the location of the defender. After all it's the same location as I have in [harm_unit]. I wouldn't know how to access the location otherwise.
User avatar
Ravana
Forum Moderator
Posts: 3002
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Check units current defense

Post by Ravana »

Make sure you are not confused by
(Note: it is a WML defense. So the higher it is, the weaker the unit is.)
User avatar
Vyncyn
Forum Regular
Posts: 515
Joined: April 6th, 2013, 5:51 pm

Re: Check units current defense

Post by Vyncyn »

I know. I tested the variable with [floating_text] and it seems to depend soley on where the defending unit is standig, not on their actual defense.
e.g I had a ship with 10% defense, a smallfoot unit and a mounted unit and the variable always gave back 40 on castle, 60 on land and 50 on forest; The attacking unit has movement_type=mounted, so I really don't understand where those values come from. Seems like it always uses smallfoot, but why?
Post Reply