New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
move the carryover gold handling and dialog to lua #7610
base: master
Are you sure you want to change the base?
Conversation
@@ -0,0 +1,17 @@ | |||
#textdomain wesnoth-test | |||
# This series of tests checks that scenarios are ending as appropriate. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment seems inaccurate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
66f98fa
to
2adfb8d
Compare
@@ -1704,6 +1704,7 @@ int game_lua_kernel::impl_current_get(lua_State *L) | |||
return_int_attrib("turn", play_controller_.turn()); | |||
return_string_attrib("synced_state", synced_state()); | |||
return_bool_attrib("user_can_invoke_commands", !events::commands_disabled && gamedata().phase() == game_data::TURN_PLAYING); | |||
return_bool_attrib("is_replaying", play_controller_.is_replay()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The convention for attributes of wesnoth.current
is that reading it out makes sense as a phrase – "current map", "current schedule", "current event context", "current user can invoke commands", etc. This one doesn't fit that convention – "current is replaying" doesn't make sense.
Maybe user_is_replaying
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i can also move it somehere else, i wouldn't know where though. I'm not convinced that user_is_replaying
is a better name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm… wesnoth.scenario.is_replaying
doesn't really make sense… maybe you could put it inwesnoth.interface
as a function…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still needs to be addressed.
01171b9
to
0d6d6ce
Compare
The default
Using events for carryover looks good. However, putting the entire These events can then be handled by user-made code that mimicks the respective APIs of the default functions, and that calls ( |
Another event-based approach would rely on the other API mentioned in #7582 : aborting subsequent event handlers to the current event (maybe Either way, it seems the event system needs further improvements to be usable. |
While i agree that parts of the code could be designed to be a bit more reusable, i also don't think its worth to write a event based framework here, in particular if it makes it harder for the main usecase i had in mind when writing this, which are campaigns that want to completely replace the carryover gold mechanic, (no gold carried over at all, and probably reward quick finishes in another way (maybe items or free exp), or campaigns that have a score system) |
Understandable. Not sure what sort of API you have in mind. However, a baseline approach could be to make |
That similar to what i had in mind, i'm just unsure whether to put it, also i'm wondering whether i should make the helper functions available too, on the one hand i think they could be helpful, on he other hand making it an api would restrict us when changing their bahaviour in the future. |
data/lua/carryover_gold.lua
Outdated
id = "carryover_gold", | ||
first_time_only = false, | ||
--priority = -1000, | ||
action = function() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just action = do_carryover_gold
should suffice, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i was considering making the variable do_carryover_gold
non local (not sure where to put it yet), so that it could be replaced by umc. That's why i added this indirection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm… someone wanting to replace it entirely could just unregister this event. I guess what you say makes sense if they want to mildly alter the behaviour, maybe…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm i also wasn't sure how cleanly unregistering an event handler that was set in preload (and by preload i also mean toplevel lua) works, but i guess you just have to unregister them at preload aswell.
So i split the function into smaller parts, since the parts are then kinda public api, we now have to worry about making them pretty. |
data/lua/scenario_end_events.lua
Outdated
@@ -0,0 +1,11 @@ | |||
carryover = wesnoth.require("carryover_gold.lua") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be local.
data/lua/carryover_gold.lua
Outdated
@@ -47,83 +52,108 @@ local function get_popup_text_basic() | |||
return title, report | |||
end | |||
|
|||
local function do_carryover_gold() | |||
--- Calculates the amount of carryover gold. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make these documentation comments conform to the format of the documentation comments in other modules, for example location_set
or functional
. That means using @param
and @return
annotations in addition to 3 dashes for the brief description as you've done here.
return #(wesnoth.map.find{ gives_income = true }) | ||
end | ||
|
||
local function half_signed_value(num) | ||
--- Like tostring(num) but uses a prettier minus sign, to be used in the UI. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is clearly not true, it's just returning tostring(num)
directly.
Also, even if it were changed to be true, this seems like something that probably belongs in either the mathx
or stringx
module, not in here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tbh i don't even know why it's called that i just copied the name from the c++ function, but yeah the implementation is not there yet, idk whether it'd be really useful another places or whether other places usually want to use the normal minus sign.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's a C++ function, we could just make this a thin wrapper around the C++ function. We should at least consider changing the name though, as it doesn't make sense to me.
I also think it should return a t_string
, rather than a regular Lua string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still applies now.
data/lua/carryover_gold.lua
Outdated
|
||
else | ||
goldmsg = _("You will start the next scenario with the defined minimum starting gold.", | ||
"You will start the next scenario with the defined minimum starting gold.", side.carryover_gold) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a normal string, not a plural.
I know it's copying the C++, it's a bug there too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you think so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right now i see it
08c7516
to
8cea1b7
Compare
8cea1b7
to
0e28580
Compare
The idea is to make it easier for umc devs to implement their own carryover / early finish bonus mechanics. Maybe we can also make use of it in Worls Conquest.
And use it in carryover_gold.lua
0e28580
to
2401395
Compare
this way the functions can easily be reused for alternative carryover implementations.
We now use a modified default carryover calculation, the main advantage is it now uses the normal carryover message on endlevel.
2401395
to
0e592b9
Compare
Now that 1.18 is branched of i'll probably merge this the next days |
|
||
--- Returns true iff there is not a single side that is controlled by the local human player. | ||
---@return boolean | ||
function carryover.get_is_observer() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like something that should be moved into wesnoth.interface
.
end | ||
|
||
--- Show names when thre are multiple persistent teams. | ||
---@return integer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment explaining the return value, like this:
---@return integer #the number of things
Or change the main comment to include this information. (Speaking of which, the main comment seems to be incorrect.)
--- Calculates the amount of carryover gold. | ||
---@param side side | ||
---@param turns_left integer | ||
---@return number (finishing_bonus), number (finishing_bonus_per_turn) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to explain multiple return values, each one needs to be on its own line.
---@return number (finishing_bonus), number (finishing_bonus_per_turn) | |
---@return number #total finishing bonus | |
---@return number #finishing bonus per turn |
return finishing_bonus, finishing_bonus_per_turn | ||
end | ||
|
||
---@return boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment, either with #
at the end of this line or with ---
on a separate line above, that explains what get_report_visible
does.
function carryover.get_report_visible() | ||
local is_observer = carryover.get_is_observer() | ||
local is_replay = wesnoth.current.is_replaying | ||
--return true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably needs to be deleted.
return wesnoth.scenario.end_level_data.carryover_report and (not is_observer) and (not is_replay) | ||
end | ||
|
||
--- Whether to show the sides names in the scenario end report. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--- Whether to show the sides names in the scenario end report. | |
--- Whether to show the sides' names in the scenario end report. |
|
||
|
||
--- returns a stub to show in the scenario end dialog. | ||
---@return string (title), string (report) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
---@return string (title), string (report) | |
---@return string #title | |
---@return string #report |
|
||
-- returns the first part of the carryover mesage, explain remaining gold | ||
---@param side side | ||
---@param info table |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please specify the format of this table.
There are two ways to do this. You can do it inline like this:
{field1: type, field2: type}
Or on the lines above you can add a block of comments like this:
---@class some_descriptive_name_for_the_table
---@field field1 type explanation of this field
---@field field2 type explanation of this field
If there's no obvious name for the type, remaining_gold_options
or remaining_gold_info
should be fine.
Or, if this is no longer used, then it could also be removed. (Though maybe the point is that the function could be overridden with one that does use it?)
end | ||
|
||
---@param side side | ||
---@param info table |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please specify the format of this table too. It looks like it has 3 optional fields? See my previous comment for how. Also, add a comment explaining what the function does.
end | ||
|
||
---@param side side | ||
---@param info table |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please specify the format of this table. Also, add a comment explaining what the function does.
return title, report | ||
end | ||
|
||
-- returns the first part of the carryover mesage, explain remaining gold |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment isn't descriptive enough. "explain remaining gold" isn't an explanation, it's just restating the function name.
end | ||
|
||
---@param side side | ||
---@param info table |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please specify the format of this table. Also, add a comment explaining what the function does.
end | ||
--- returns the last part of the gold report for a single side, describing how much gold is added in the next scenario. | ||
---@param side side | ||
---@param info table |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please specify the format of this table. Also, add a line break before this comment block.
side.carryover_gold) | ||
end | ||
|
||
-- xgettext:no-c-format |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this actually needed? Does it even do anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pretty sure i coped it from the c++ code so not sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that's the case, remove it. xgetext is the tool we use to gather translatable strings from C++ files, but it's not used on Lua files.
|
||
--- returns the default gold report for a single side. | ||
---@param side side | ||
---@param info table |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please specify the format of this table. Also, the comment could be improved.
return report | ||
end | ||
|
||
---@return boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment indicating what this function does. I realize it may seem obvious from the name, but I'd like to make it explicit too.
end | ||
|
||
---@return table | ||
function carryover.turns_left() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This says it returns a table but it actually returns an integer.
return math.max(0, wesnoth.scenario.turns - wesnoth.current.turn) | ||
end | ||
|
||
--- sets the sides carryover gold and returns information that is used in the report message |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--- sets the sides carryover gold and returns information that is used in the report message | |
--- sets the side's carryover gold and returns information that is used in the report message |
|
||
--- sets the sides carryover gold and returns information that is used in the report message | ||
---@param side side | ||
---@return table |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please specify the format of this table.
} | ||
end | ||
|
||
function carryover.do_carryover_gold() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this function undocumented?
@@ -394,7 +394,7 @@ void playsingle_controller::do_end_level() | |||
} | |||
|
|||
persist_.end_transaction(); | |||
carryover_show_gold(gamestate(), is_observer() || is_replay(), is_observer(), saved_game_.classification().is_test()); | |||
// carryover_show_gold(gamestate(), is_observer() || is_replay(), is_observer(), saved_game_.classification().is_test()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please delete.
carryover_show_gold.?pp have nothing left in them now, so please delete them. I realize that means editing the Xcode project file, but it's honestly not that hard to remove a file from it – just delete all lines mentioning the file. Please fix at least everything I mentioned that's not related to documentation before merging. I'd prefer you to fix all the documentation issues too, though. |
This is still a wip, in particular im not yet sure what exactly the api for replacing it should be