Divide variable help

The place to post your WML questions and answers.

Moderators: Forum Moderators, Developers

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
User avatar
LordAwsomeness
Posts: 96
Joined: August 12th, 2013, 2:20 pm
Location: U.S.A.

Divide variable help

Post by LordAwsomeness » October 12th, 2019, 6:57 pm

So essentially im trying to use variables to give gold from a chest to all of the players in a team. so say there is 25 gold in the chest but it needs to be distributed to 4 players, I want to give the 6 gold to each player and then the remainder gold (the last 1 gold) to the player who stepped on the chest. I have the gold distribution set up and everything but the variables is where im stuck at. im using the divide and then round=floor to find the gold that will be given to everybody but I dont know how to find the remainder gold and how to give it to the person who stepped on the chest. here is a little sample equation that im on and where im stuck at:

Code: Select all

{VARIABLE gold 25}
{VARIABLE_OP gold divide 4}
{VARIABLE_OP gold round floor}
it comes out to 6 as would be expected. but I want to know how to take that last piece of gold and give it to the player who stepped on the chest.
- Been playing Wesnoth since 2004 and the 1.0.x versions.
- Creator of Undead Invasion MP Scenario Pack.
- Creator of Valeria MP Adventure

User avatar
Ravana
Forum Moderator
Posts: 2244
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Divide variable help

Post by Ravana » October 12th, 2019, 7:57 pm

modulo key.

User avatar
octalot
Developer
Posts: 407
Joined: July 17th, 2010, 7:40 pm

Re: Divide variable help

Post by octalot » October 12th, 2019, 7:59 pm

Code: Select all

        {VARIABLE share_gold "$(floor($chest[1].amount / $hero_leaders.length))"}
        {VARIABLE remainder "$($chest[1].amount % $hero_leaders.length)"}
More context:

Code: Select all

    [switch]
      variable=hero_leaders.length
      [case]
        value=0,1 # zero should never happen, there must be at least 1 player
        {VARIABLE share_gold $chest[1].amount}
        {VARIABLE remainder 0}
        [print]
          text="This chest contains $chest[1].amount| gold!"
          size=18
          duration=500
          red,green,blue=255,255,0
        [/print]
      [/case]
      [else]
        {VARIABLE share_gold "$(floor($chest[1].amount / $hero_leaders.length))"}
        {VARIABLE remainder "$($chest[1].amount % $hero_leaders.length)"}
        [if]
          {SXC_CMP remainder numerical_equals 0}
          [then]
            [print]
              text="This chest contains $chest[1].amount gold!
Every player gained $share_gold gold"
              size=18
              duration=500
              red,green,blue=255,255,0
            [/print]
          [/then]
          [else]
            [print]
              text="This chest contains $chest[1].amount gold!
Every player gained $share_gold gold
Remaining $remainder gained by player $side_number"
              size=18
              duration=500
              red,green,blue=255,255,0
            [/print]
          [/else]
        [/if]
      [/else]
    [/switch]

User avatar
LordAwsomeness
Posts: 96
Joined: August 12th, 2013, 2:20 pm
Location: U.S.A.

Re: Divide variable help

Post by LordAwsomeness » October 12th, 2019, 11:37 pm

this is the code I have been using:

Code: Select all

#define LA_SPOILS_SPLIT_CHEST X Y AMOUNT
{MOVETO_ACTION_WML_ONCE_ONLY_SIDE {X} {Y} "The chest held {AMOUNT} pieces of gold!" "items/chest-plain-closed.png" (
	{VARIABLE i 1}
	[store_side]
		team_name=Refugees
		variable=player_count
	[/store_side]
	[while]
		{VARIABLE_CONDITIONAL i less_than_equal_to $player_count.length}
	[do]
		[if]
			[have_unit]
				side=$i
				canrecruit=yes
			[/have_unit]
			[then]
			
			{VARIABLE la_temp_gold {AMOUNT}}
			{VARIABLE_OP la_temp_gold divide $player_count.length}
			{VARIABLE_OP la_temp_gold round floor}
				[gold]
					side=$i
					amount=$la_temp_gold
				[/gold]
			[store_unit]
				[filter]
					side=$i
					canrecruit=yes
				[/filter]
				variable=players
			[/store_unit]
			[unstore_unit]
				variable=players
				text=$la_temp_gold
				red,green,blue=255,255,0
			[/unstore_unit]
			{VARIABLE_OP la_shop_points add $la_temp_gold}
			{CLEAR_VARIABLE players}
			[/then]
			[/if]
			[sound]
				name=gold.ogg
			[/sound]
			{VARIABLE_OP i add 1}
		[/do]
	[/while]
	{CLEAR_VARIABLE i}
	{CLEAR_VARIABLE player_count}
		{REMOVE_IMAGE {X} {Y}}
		{PLACE_IMAGE "items/chest-plain-open.png" {X} {Y}}
		) $la_player_sides}
#enddef
Ill make some modifications from the survival xtreme snippet and see if it works. probably shoulda just done that from the start without bothering you guys but hey I figured there may be a quicker explaination. Thank you guys!


EDIT:
Ravana wrote:
October 12th, 2019, 7:57 pm
modulo key.
Thank you! this was exactly what I needed so that I didn't have to rewrite my code to match SXC :D
- Been playing Wesnoth since 2004 and the 1.0.x versions.
- Creator of Undead Invasion MP Scenario Pack.
- Creator of Valeria MP Adventure

Post Reply