Add boolean "set_amount" (or similar term) to [gold] tag

Brainstorm ideas of possible additions to the game. Read this before posting!

Moderator: Forum Moderators

Forum rules
Before posting a new idea, you must read the following:
Post Reply
User avatar
Spannerbag
Posts: 505
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Add boolean "set_amount" (or similar term) to [gold] tag

Post by Spannerbag »

Add boolean set_amount sub-tag (or some similar term) to [gold].

Function:
If set_amount=true then if after adding amount the side still has less than amount gold, the side gold is increased to amount.
If omitted, set_amount defaults to false and [gold] acts as is does at present.

Two examples with set_amount=true using the code below:

Code: Select all

[gold]
    side=1
    amount=100
    set_amount=true
[/gold]
1) If side 1 had 14 gold prior to execution of the above code it would have 114 afterwards (same as current behaviour, so set_amount made no difference).

2) If however side 1 had -14 gold prior to execution of the above code it would have 86 afterwards.
However set_amount=true so because side gold(=86) is less than amount(=100), side gold is increased to 100.

I happen to use this logic a lot in my UMC and wrote a macro to do the job.
However this change may, IMHO, slightly reduce WML bloat and should not impact existing WML.
I do not know lua but hope it would be an easy mod for someone who does?

No worries if it can't be done or isn't worth doing...

(Any queries, questions etc. please send me a private message if you need a quick response.)

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Re: Add boolean "set_amount" (or similar term) to [gold] tag

Post by vghetto »

Could you test this, please?
I called it [umc_gold]

Code: Select all

function wesnoth.wml_actions.umc_gold(cfg)
        local amount = tonumber(cfg.amount) or
                wml.error "[gold] missing required amount= attribute."
        local sides = wesnoth.sides.find(cfg)

        local set_amount = cfg.set_amount
        if (set_amount == nil) then set_amount = false end

        for index, team in ipairs(sides) do
                if set_amount and team.gold < 0 then
                        team.gold = amount
                else
                        team.gold = team.gold + amount
                end
        end
end
Oops, nevermind. I got the logic wrong :oops:

Edit: Ok, either someone edited my post or I'm losing my mind.
set_amount should default to false
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Re: Add boolean "set_amount" (or similar term) to [gold] tag

Post by vghetto »

I ended up integrating this idea into WF to reset the player's negative gold.
I tested it on 1.14 and glad to say that it works :) Haven't tried it on 1.15+ yet

This is how I'm resetting the negative gold.

Code: Select all

        [wf_gold]
            side=1
            amount=0
            reset=yes
        [/wf_gold]
The final lua code looks like this:

Code: Select all

function wesnoth.wml_actions.wf_gold(cfg)
-- if wesnoth version >= 1.15.0
if wesnoth.compare_versions(wesnoth.game_config.version, ">=", "1.15.0") then

        local amount = tonumber(cfg.amount) or
                wml.error "[wf_gold] missing required amount= attribute."
        local sides = wesnoth.sides.find(cfg)
        local reset = cfg.reset
        if (reset == nil) then reset = false end
        for index, team in ipairs(sides) do
                if reset and team.gold < 0 then
                        team.gold = amount
                else
                        team.gold = team.gold + amount
                end
        end

-- else wesnoth version < 1.15.0
else

        local amount = tonumber(cfg.amount) or
                helper.wml_error "[wf_gold] missing required amount= attribute."
        local sides = wesnoth.get_sides(cfg)
        local reset = cfg.reset
        if (reset == nil) then reset = false end
        for index, team in ipairs(sides) do
                if reset and team.gold < 0 then
                        team.gold = amount
                else
                        team.gold = team.gold + amount
                end
        end
end
-- end wesnoth version
end
User avatar
Spannerbag
Posts: 505
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Add boolean "set_amount" (or similar term) to [gold] tag

Post by Spannerbag »

vghetto wrote: October 2nd, 2021, 10:47 am I ended up integrating this idea into WF to reset the player's negative gold.
I tested it on 1.14 and glad to say that it works :) Haven't tried it on 1.15+ yet...
Cool 8)

Aaaah you reminded me of 2 things I forgot to do:
- update my version to 1.15 (I'm porting a half-done campaign from 1.14 to 1.15 and forgot to update the lua)
- post the final working 1.14 version as a maybe helpful widget.

I might pinch your Wesnoth version test logic :)

Bit late now, but FWIW here's the v14 version of my code:

Code: Select all

local helper = wesnoth.require "lua/helper"
function wesnoth.wml_actions.gold_min(cfg)
  local gm_amount = tonumber(cfg.amount) or helper.wml_error "[gold_min] missing required amount= attribute."
  local gm_min = tonumber(cfg.min) or gm_amount
  local sides = wesnoth.get_sides(cfg)
  for index, team in ipairs(sides) do
    team.gold = team.gold + gm_amount
    if team.gold < gm_min then team.gold = gm_min end
  end
end
... and here's the comments and implementation in _main.cfg

Code: Select all

# Custom tag [gold_min] used to (slightly) reduce WML bloat
#
# Keys
#	side:	Side(s) to modify gold for.  Can be a single side or a comma separated list.
#		Note: [filter_side] does not seem to work (all sides are included).
#
# 	amount:	Gold to be given to side(s) above.  Required.
#
#	min:	Minimum gold that side(s) must have.  Optional, if omitted min defaults to amount.
#
#
# Function
#
#	max( side gold+min, min ) for each side
#
#
# Example 1
# 
# [gold_min]
#   sides=1,2,3
#   amount=35
# [/gold_min]
#
# Side gold before/after execution of WML above:
# side 1=100/135, side 2=-23/35, side 3=0/35.
# Note: min omitted so defaults to amount (=35).
#
#
# Example 2
#
# Side gold before execution of WML below:
#
# [gold_min]
#   sides=1,2,4
#   amount=35
#   min=20
# [/gold_min]
#
# Side gold before/after execution of WML above:
# side 1=100/135, side 2=-23/20, side 3=0/35.
#
#
# Credits
#
# Many thanks to all those on the forum who did all the hard work for me.
# If you are interested, my various consufions and failures can be viewed on the forum:
# https://forums.wesnoth.org/viewtopic.php?f=58&t=54347
# https://forums.wesnoth.org/viewtopic.php?f=58&t=54540
# https://forums.wesnoth.org/viewtopic.php?f=12&t=54045
#
#
# Compatibility
#
# Works for Wesnoth v1.14
#
# Changes to lua code for v1.15:
#
# wesnoth.get_sides(cfg) -> wesnoth.sides.find(cfg)
# helper.wml_error	 -> wml.error

  [lua]
    code="wesnoth.require '~add-ons/LSB/lua/gold_min.lua'"
  [/lua]
Of course, we won't mention [modify_side] :-)

Cheers!
--Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Re: Add boolean "set_amount" (or similar term) to [gold] tag

Post by vghetto »

Spannerbag wrote: October 2nd, 2021, 2:10 pm I might pinch your Wesnoth version test logic :)
I think I pinched it from Iris. I don't remember.
Spannerbag wrote: October 2nd, 2021, 2:10 pm # [gold_min]
# sides=1,2,3
# amount=35
# [/gold_min]
should be side=1,2,3
Spannerbag wrote: October 2nd, 2021, 2:10 pm Of course, we won't mention [modify_side] :-)
Of course not, I don't want to reset if the gold is positive.
User avatar
Spannerbag
Posts: 505
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Add boolean "set_amount" (or similar term) to [gold] tag

Post by Spannerbag »

vghetto wrote: October 2nd, 2021, 3:18 pm
Spannerbag wrote: October 2nd, 2021, 2:10 pm # [gold_min]
# sides=1,2,3
# amount=35
# [/gold_min]
should be side=1,2,3
Ooops, quite right, fixed - thanks!

Cheers,
--Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
landofMordor
Posts: 6
Joined: August 19th, 2019, 2:01 pm

Re: Add boolean "set_amount" (or similar term) to [gold] tag

Post by landofMordor »

Spannerbag wrote: October 2nd, 2021, 2:10 pm
Bit late now, but FWIW here's the v14 version of my code:

... and here's the comments and implementation in _main.cfg
This is an awesome snippet of code. Could I borrow it for a UMC, attributing you in my _main.cfg?
User avatar
Spannerbag
Posts: 505
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Add boolean "set_amount" (or similar term) to [gold] tag

Post by Spannerbag »

Hi landofMordor,
landofMordor wrote: February 18th, 2022, 3:10 pm This is an awesome snippet of code. Could I borrow it for a UMC, attributing you in my _main.cfg?
Of course, you're very welcome :D

Here's the 1.6 version:

Code: Select all

-- Wesnoth 1.15+

function wesnoth.wml_actions.gold_min(cfg)
  local gm_amount = tonumber(cfg.amount) or wml.error "[gold_min] missing required amount= attribute."
  local gm_min = tonumber(cfg.min) or gm_amount
  local sides = wesnoth.sides.find(cfg)
  for index, team in ipairs(sides) do
    team.gold = team.gold + gm_amount
    if team.gold < gm_min then team.gold = gm_min end
  end
end
In _main.cfg within [campaign] you'll need to add
Edit: Ooops, forgot to edit the path below, now fixed to reflect general case with <<campaign_directory>> :roll:
So if your UMC was in a directory called Count_Marigold then replace "<<campaign_directory>"> with "Count_Marigold"

Code: Select all

  [lua]
    code="wesnoth.require '~add-ons/<<campaign_directory>>/lua/gold_min.lua'"
  [/lua]
However to be fair it was really the contributions of several people much cleverer than me who got the code working, I just stood on their shoulders :)

I've credited them in _main.cfg as part of the usage notes:

Code: Select all

# Custom tag [gold_min] used to (slightly) reduce WML bloat
#
# Keys
#	side:	Side(s) to modify gold for.  Can be a single side or a comma separated list.
#		Note: [filter_side] does not seem to work (all sides are included).
#
# 	amount:	Gold to be given to side(s) above.  Required.
#
#	min:	Minimum gold that side(s) must have.  Optional, if omitted min defaults to amount.
#
#
# Function
#
#	max( side gold+min, min ) for each side
#
#
# Example 1
# 
# [gold_min]
#   side=1,2,3
#   amount=35
# [/gold_min]
#
# Side gold before/after execution of WML above:
# side 1=100/135, side 2=-23/35, side 3=0/35.
# Note: min omitted so defaults to amount (=35).
#
#
# Example 2
#
# [gold_min]
#   side=1,2,4
#   amount=35
#   min=20
# [/gold_min]
#
# Side gold before/after execution of WML above:
# side 1=100/135, side 2=-23/20, side 3=0/35.
#
#
# Credits
#
# Many thanks to all those on the forum who did all the hard work for me:
# Luther, vghetto, Elvish_Hunter and Pentarctagon
# If you are interested, my various consufions and failures can be viewed on the forum:
# https://forums.wesnoth.org/viewtopic.php?f=58&t=54347
# https://forums.wesnoth.org/viewtopic.php?f=58&t=54540
# https://forums.wesnoth.org/viewtopic.php?f=12&t=54045

  [lua]
    code="wesnoth.require '~add-ons/LSB/lua/gold_min.lua'"
  [/lua]
Any problems feel free to get back to me ... and when I haven't a clue I'll ask on the forum :doh:

Cheers!
--Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Ravana
Forum Moderator
Posts: 2949
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Add boolean "set_amount" (or similar term) to [gold] tag

Post by Ravana »

[campaign] is not supposed to recognize [lua]. It might also not support reloading. Should be more reliable to use preload event to set up tags.
User avatar
Lord-Knightmare
Discord Moderator
Posts: 2340
Joined: May 24th, 2010, 5:26 pm
Location: Somewhere in the depths of Irdya, gathering my army to eventually destroy the known world.
Contact:

Re: Add boolean "set_amount" (or similar term) to [gold] tag

Post by Lord-Knightmare »

Spannerbag wrote: February 20th, 2022, 12:15 am Hi landofMordor,
landofMordor wrote: February 18th, 2022, 3:10 pm This is an awesome snippet of code. Could I borrow it for a UMC, attributing you in my _main.cfg?
Of course, you're very welcome :D

Here's the 1.6 version:

Code: Select all

-- Wesnoth 1.15+

function wesnoth.wml_actions.gold_min(cfg)
  local gm_amount = tonumber(cfg.amount) or wml.error "[gold_min] missing required amount= attribute."
  local gm_min = tonumber(cfg.min) or gm_amount
  local sides = wesnoth.sides.find(cfg)
  for index, team in ipairs(sides) do
    team.gold = team.gold + gm_amount
    if team.gold < gm_min then team.gold = gm_min end
  end
end
In _main.cfg within [campaign] you'll need to add
Edit: Ooops, forgot to edit the path below, now fixed to reflect general case with <<campaign_directory>> :roll:
So if your UMC was in a directory called Count_Marigold then replace "<<campaign_directory>"> with "Count_Marigold"

Code: Select all

  [lua]
    code="wesnoth.require '~add-ons/<<campaign_directory>>/lua/gold_min.lua'"
  [/lua]
However to be fair it was really the contributions of several people much cleverer than me who got the code working, I just stood on their shoulders :)

I've credited them in _main.cfg as part of the usage notes:

Code: Select all

# Custom tag [gold_min] used to (slightly) reduce WML bloat
#
# Keys
#	side:	Side(s) to modify gold for.  Can be a single side or a comma separated list.
#		Note: [filter_side] does not seem to work (all sides are included).
#
# 	amount:	Gold to be given to side(s) above.  Required.
#
#	min:	Minimum gold that side(s) must have.  Optional, if omitted min defaults to amount.
#
#
# Function
#
#	max( side gold+min, min ) for each side
#
#
# Example 1
# 
# [gold_min]
#   side=1,2,3
#   amount=35
# [/gold_min]
#
# Side gold before/after execution of WML above:
# side 1=100/135, side 2=-23/35, side 3=0/35.
# Note: min omitted so defaults to amount (=35).
#
#
# Example 2
#
# [gold_min]
#   side=1,2,4
#   amount=35
#   min=20
# [/gold_min]
#
# Side gold before/after execution of WML above:
# side 1=100/135, side 2=-23/20, side 3=0/35.
#
#
# Credits
#
# Many thanks to all those on the forum who did all the hard work for me:
# Luther, vghetto, Elvish_Hunter and Pentarctagon
# If you are interested, my various consufions and failures can be viewed on the forum:
# https://forums.wesnoth.org/viewtopic.php?f=58&t=54347
# https://forums.wesnoth.org/viewtopic.php?f=58&t=54540
# https://forums.wesnoth.org/viewtopic.php?f=12&t=54045

  [lua]
    code="wesnoth.require '~add-ons/LSB/lua/gold_min.lua'"
  [/lua]
Any problems feel free to get back to me ... and when I haven't a clue I'll ask on the forum :doh:

Cheers!
--Spannerbag
This is a good Lua code segment. Beneficial to campaigns where sides can accumulate negative gold and the gold bonus does nothing (since big_negative + positive = still_negative). Can you contribute it to the Wesnoth Lua Pack add-on to make it more accessible to more people?
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare | My Medium
User avatar
Spannerbag
Posts: 505
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Add boolean "set_amount" (or similar term) to [gold] tag

Post by Spannerbag »

Ravana wrote: February 20th, 2022, 10:51 pm [campaign] is not supposed to recognize [lua]. It might also not support reloading. Should be more reliable to use preload event to set up tags.
OK, would the most efficient way to make the lua available to every scenario be to have the following within [campaign] (or maybe the campaign's #ifdef)?

Code: Select all

[event]
    name=preload
    first_time_only=no
    [lua]
        code="wesnoth.require '~add-ons/LSB/lua/gold_min.lua'"
    [/lua]
[/event]
I've never used [modification] or [resource] but if they're optimal I can use them - and a real-world example would be much appreciated! :)

Cheers!
--Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Spannerbag
Posts: 505
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Add boolean "set_amount" (or similar term) to [gold] tag

Post by Spannerbag »

Lord-Knightmare wrote: February 20th, 2022, 11:02 pm This is a good Lua code segment. Beneficial to campaigns where sides can accumulate negative gold and the gold bonus does nothing (since big_negative + positive = still_negative). Can you contribute it to the Wesnoth Lua Pack add-on to make it more accessible to more people?
Thanks for the positive feedback - I use this logic a lot in my UMC (most of which is still in development :augh: ).

Wesnoth Lua Pack contribution post viewtopic.php?p=671753#p671753.

Cheers!
--Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
Post Reply