Giving all of a unit's attacks to another unit

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
Larceny
Posts: 9
Joined: September 23rd, 2011, 3:38 am

Giving all of a unit's attacks to another unit

Post by Larceny »

I am attempting to take all of the attacks away from a stored unit, and give it all of the attacks from another stored unit with slight modifications. The second stored unit is stored in the first stored unit in <unit>.variables.wereform. (Yes, it's a werewolf.)

All of this is within a FOREACH macro with other stuff: the first unit is MODIFY_UNIT_store[$MODIFY_UNIT_i]

Without further ado, I present my code:

Code: Select all

	[object]
		[filter]
			id=MODIFY_UNIT_store[$MODIFY_UNIT_i]
		[/filter]
		[effect]
			apply_to=remove_attacks
		[/effect]
		silent=yes
	[/object]
        [set_variables]
		name=currentAttacks
		to_variable=MODIFY_UNIT_store[$MODIFY_UNIT_i].variables.wereform.attack
        [/set_variables]

	{FOREACH currentAttacks attack_i}
		[object]
			[filter]
				id=MODIFY_UNIT_store[$MODIFY_UNIT_i]
			[/filter]
			[effect]
				apply_to=new_attack
				description=$currentAttacks[$attack_i].description
				name=$currentAttacks[$attack_i].name
				type=$currentAttacks[$attack_i].type
	
				[set_variables]
					name=currentSpecials
					to_variable=MODIFY_UNIT_store[$MODIFY_UNIT_i].variables.wereform.attack.currentAttacks[attack_i].specials
	 		       	[/set_variables]
				[specials]
					{FOREACH currentSpecials special_i}
					{currentSpecials[$special_i]}
					{NEXT $special_i}
				[/specials]
	
				icon=$currentAttacks[$attack_i].icon
				range=$currentAttacks[$attack_i].range
				damage=$($MODIFY_UNIT_store[$MODIFY_UNIT_i].level| * $currentAttacks[$attack_i].description|)
				number=$currentAttacks[$attack_i].number
				movement_used=$currentAttacks[$attack_i].movement_used
				attack_weight=$currentAttacks[$attack_i].attack_weight
				defense_weight=$currentAttacks[$attack_i].defense_weight
			[/effect]
			silent=yes
		[/object]
    	{NEXT attack_i}
Sadly, it does not even remove attacks
User avatar
Dugi
Posts: 4961
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Giving all of a unit's attacks to another unit

Post by Dugi »

Better paste the whole code, would allow somebody to test it.

I see a problem in this part:

Code: Select all

id=MODIFY_UNIT_store[$MODIFY_UNIT_i]
that should rather be

Code: Select all

id=MODIFY_UNIT_store[$MODIFY_UNIT_i].id
(it is twice in the code).
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: Giving all of a unit's attacks to another unit

Post by Sapient »

don't use MODIFY_UNIT_store as a variable name... that's reserved for the MODIFY_UNIT macro, as you can tell by it's name.
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
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: Giving all of a unit's attacks to another unit

Post by zookeeper »

Larceny wrote: id=MODIFY_UNIT_store[$MODIFY_UNIT_i]
No unit has such an id. Prefix with $.
Larceny wrote: to_variable=MODIFY_UNIT_store[$MODIFY_UNIT_i].variables.wereform.attack.currentAttacks[attack_i].specials
Again, attack_i should be $attack_i.
Larceny wrote: [specials]
{FOREACH currentSpecials special_i}
{currentSpecials[$special_i]}
{NEXT $special_i}
[/specials]
Action WML isn't valid in [specials]. And even if it was, the middle line is completely bogus and whatever you think it would do, it most certainly would not. Also, {NEXT $special_i} should have been {NEXT special_i}.

And those are just the immediate problems with the code itself. I haven't yet taken a look at what the best approach to the problem would be.
bismitch
Posts: 24
Joined: August 12th, 2005, 8:38 pm

Re: Giving all of a unit's attacks to another unit

Post by bismitch »

I tried to get something done similar to what you wanted and this basically works for player 1 but it doubles the attacks and doesn't work for other players. For my particular scenario I limited the attack stealing to just LEADER_SPAWN units, if that makes it clearer. Let it be known this is the first batch of wesnoth code from me.

Code: Select all

function absorb_abilities()
	wesnoth.message("you're doing something right")
	local combatants = {wesnoth.get_variable("unit"),wesnoth.get_variable("second_unit")}
	for i=1,2,1 do
		wesnoth.message(combatants[i].name)
		if combatants[i].name == "Leader" then
			wesnoth.message(combatants[i].name, "good job")
			wesnoth.message(combatants[i].hitpoints)
			if combatants[i].hitpoints <=0 then
				local champ = math.abs(i-3)
				wesnoth.message(combatants[i].name, "something might happen")
				for attack in helper.child_range(combatants[i], "attack") do
					local u = wesnoth.get_unit(combatants[champ].x, combatants[champ].y)
					wesnoth.add_modification( u, "object", {
						{ "effect", {
							apply_to="new_attack",
							name=attack.name,
							icon=attack.icon,
							type=attack.type,
							range=attack.range,
							damage=attack.damage,
							number=attack.number,
						}}}
					)
				end
			end
		end
	end
end
Attachments
foursquare.zip
(5.01 KiB) Downloaded 100 times
bismitch
Posts: 24
Joined: August 12th, 2005, 8:38 pm

Re: Giving all of a unit's attacks to another unit

Post by bismitch »

It seems the combat_mods.cfg is duplicated when it is included in the scenario file. Because its running everything in that file twice. How do I prevent it from doing that? Also why does absorb_abilities() work for player 1 but not the other players?
User avatar
Dugi
Posts: 4961
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Giving all of a unit's attacks to another unit

Post by Dugi »

If it is running twice, it means that the code is loaded twice. You have possibly written it somewhere inside the utils and then also into the scenario or something. Just remove it somewhere and it should execute only once (you probably don't know where is the code written for the second time, I have also a campaign where death messages are executed from I don't know where, but it works).

And it probably affects only player 1 because it is specified somewhere that it is side one, or unspecified and side 1 is default there.
bismitch
Posts: 24
Joined: August 12th, 2005, 8:38 pm

Re: Giving all of a unit's attacks to another unit

Post by bismitch »

Yeah I'm not really sure where its called besides the scenario file, but it isnt duplicating now. I figured out why other players would not work. Basically the wml doesnt like the name "Anonymous Local Player".
bismitch
Posts: 24
Joined: August 12th, 2005, 8:38 pm

Re: Giving all of a unit's attacks to another unit

Post by bismitch »

You know I played around with this code again and I found weirdest bug. I never did duplicate the inclusion of the combat_mods.cfg. I actually created residual lua code that is called for every map. I should play wesnoth more often.
Post Reply