Add boolean "set_amount" (or similar term) to [gold] tag
Moderator: Forum Moderators
Forum rules
Before posting a new idea, you must read the following:
Before posting a new idea, you must read the following:
- Spannerbag
- Posts: 535
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Add boolean "set_amount" (or similar term) to [gold] tag
Add boolean
Function:
If
If omitted,
Two examples with
1) If side 1 had
2) If however side 1 had
However
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
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]
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
Re: Add boolean "set_amount" (or similar term) to [gold] tag
Could you test this, please?
I called it [umc_gold]
Oops, nevermind. I got the logic wrong
Edit: Ok, either someone edited my post or I'm losing my mind.
set_amount should default to false
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
Edit: Ok, either someone edited my post or I'm losing my mind.
set_amount should default to false
Re: Add boolean "set_amount" (or similar term) to [gold] tag
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.
The final lua code looks like this:
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]
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
- Spannerbag
- Posts: 535
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Add boolean "set_amount" (or similar term) to [gold] tag
Cool
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
_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]
[modify_side]
Cheers!
--Spannerbag
Re: Add boolean "set_amount" (or similar term) to [gold] tag
I think I pinched it from Iris. I don't remember.
should be
side=1,2,3
Of course not, I don't want to reset if the gold is positive.
- Spannerbag
- Posts: 535
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Add boolean "set_amount" (or similar term) to [gold] tag
-
- Posts: 6
- Joined: August 19th, 2019, 2:01 pm
Re: Add boolean "set_amount" (or similar term) to [gold] tag
This is an awesome snippet of code. Could I borrow it for a UMC, attributing you in my _main.cfg?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
- Spannerbag
- Posts: 535
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Add boolean "set_amount" (or similar term) to [gold] tag
Hi landofMordor,
Here's the 1.6 version:
In
Edit: Ooops, forgot to edit the path below, now fixed to reflect general case with <<campaign_directory>>
So if your UMC was in a directory called Count_Marigold then replace "<<campaign_directory>"> with "Count_Marigold"
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
Any problems feel free to get back to me ... and when I haven't a clue I'll ask on the forum
Cheers!
--Spannerbag
Of course, you're very welcomelandofMordor 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?
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
_main.cfg
within [campaign]
you'll need to addEdit: Ooops, forgot to edit the path below, now fixed to reflect general case with <<campaign_directory>>
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]
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]
Cheers!
--Spannerbag
Re: Add boolean "set_amount" (or similar term) to [gold] tag
[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.
- Lord-Knightmare
- Discord Moderator
- Posts: 2364
- 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
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?Spannerbag wrote: ↑February 20th, 2022, 12:15 am Hi landofMordor,
Of course, you're very welcomelandofMordor 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?
Here's the 1.6 version:
InCode: 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
_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>>
So if your UMC was in a directory called Count_Marigold then replace "<<campaign_directory>"> with "Count_Marigold"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 shouldersCode: Select all
[lua] code="wesnoth.require '~add-ons/<<campaign_directory>>/lua/gold_min.lua'" [/lua]
I've credited them in_main.cfg
as part of the usage notes:Any problems feel free to get back to me ... and when I haven't a clue I'll ask on the forumCode: 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]
Cheers!
--Spannerbag
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare | My Medium
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare | My Medium
- Spannerbag
- Posts: 535
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Add boolean "set_amount" (or similar term) to [gold] tag
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]
[modification]
or [resource]
but if they're optimal I can use them - and a real-world example would be much appreciated! Cheers!
--Spannerbag
- Spannerbag
- Posts: 535
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Add boolean "set_amount" (or similar term) to [gold] tag
Thanks for the positive feedback - I use this logic a lot in my UMC (most of which is still in development ).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?
Wesnoth Lua Pack contribution post viewtopic.php?p=671753#p671753.
Cheers!
--Spannerbag