Skip to content
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

Mai parms tests #6802

Merged
merged 4 commits into from Jun 28, 2022
Merged

Mai parms tests #6802

merged 4 commits into from Jun 28, 2022

Conversation

mattsc
Copy link
Member

@mattsc mattsc commented Jun 25, 2022

This adds checks for the types of [micro_ai] tag parameters, addressing #6408. It also adds warning messages if a parameter specified in a [micro_ai] tag is not valid for that MAI.

And while I was at it, I also added a couple bug fixes that I came across while working on this.

I am not planning to backport this to 1.16, as the risk of screwing something up in the stable series is too high.

@github-actions github-actions bot added the AI Issues with the AI engine, including micro AIs. label Jun 25, 2022
Copy link
Member

@CelticMinstrel CelticMinstrel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I think my main complaints would be:

  1. warning is new in Lua 5.4, and I think this is a great place to use it.
  2. We probably need a deprecation message and support for MicroAIs that have not been updated to the new style, since add-ons may contain their own custom MicroAIs.

if (number ~= math.floor(number)) then
return false
elseif (math.type(number) ~= 'integer') then
std_print("Warning: [micro_ai] tag (" .. ai_type .. ") parameter '" .. name .. "' must be an integer. It has an integer representation, but is provided in floating-point format.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might make sense to use warning instead of std_print here? I believe that adds it to the Lua console.

is_wrong_type = not is_number(k_value, false, ai_type, k_name)
elseif (k_type == 'integer') then
is_wrong_type = not is_number(k_value, true, ai_type, k_name)
elseif (k_type == 'csv_integer') then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I'm not entirely sold on the name here. I wonder if "int_list" or "integer_list" would be easier to understand?

  2. Maybe this is a low priority, but is there any demand for a list of real numbers?

for k_name,k_type in pairs(required_keys) do
if (k_type == 'tag') then
-- Check that this is not a scalar parameter
if cfg[k_name] then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably fine in the vast majority of cases, but keep in mind that it's possible to have a tag and a key with the same name. I think in that case it makes more sense to issue a warning rather than an error, if that's not too much work.

I guess it's not a priority though.

v = v:sub(2,-2)
for child in wml.child_range(cfg, v) do
table.insert(CA_cfg, T[v](child))
for k_name,k_type in pairs(optional_keys) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, sooo… I think this system (both loops) possibly counts as an API, which means this change should go through a deprecation cycle? What this means is essentially that you'd:

  1. Add an if type(k_name) = 'number' case which uses the original code (where k_type is actually the key and k_name is ignored).
  2. Add a one-time deprecation message in this case. Since this isn't a place where deprecate_api is possible, you'd probably do something like (pseudo-code) if not showed_message then showed_message = true show_message() end. The showed_message variable would be a file-level local variable.

@mattsc
Copy link
Member Author

mattsc commented Jun 27, 2022

@CelticMinstrel Replying to all you comments in one message:

I did not like csv_integer either, changed it to integer_list. I don't know if there will ever be a need for a float_list, but since it was trivial, I added support for it.

The stack trace of Lua's new warning function is nice, but I think that there needs to be a message in the log also, not just in the Lua console. So right now I have both, with the log message pointing to the Lua console for more information (although the stack trace just points to the [micro_ai] tag code, so it is not actually all that useful.

I did not add anything about there being a key and a tag with the same name right now. I think this is so unlikely that anybody actually does that that we do not need to support it. And the current error should be clear enough for people to know what to do about it.

Deprecating the old syntax: meh - I guess since it is possible to set up your own Micro AI using the default [micro_ai] tag, we need to do that.

I've committed all those in separate commits, so that it is easy to see what I did. I'll squash those before merging.

@@ -262,7 +299,8 @@ function micro_ai_helper.micro_ai_setup(cfg, CA_parms, required_keys, optional_k

-- Check whether there are invalid keys in the [micro_ai] tag
for k in pairs(cfg) do
if (k ~= 'side') and (k ~= 'ai_type') and (k ~= 'action')
if (not showed_deprecation_message) -- otherwise this produces false positives
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this check isn't what you want it to be. It's true if there has ever been a deprecation message raised for any Micro AI, not whether there has been a deprecation message for the specific Micro AI being considered.

I'm not quite sure what the best approach is here. You could just skip numeric keys (no wait, you already are? why doesn't that work?), or you could change the deprecation message to be once per Micro AI (so showed_deprecation_message becomes showed_deprecation_message[ai_type]).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure what the best approach is here. You could just skip numeric keys (no wait, you already are? why doesn't that work?),

Because this is the loop over the [micro_ai] tag config, not over required_key or optional_keys.

or you could change the deprecation message to be once per Micro AI (so showed_deprecation_message becomes showed_deprecation_message[ai_type]).

Hmm. I actually figured that getting a deprecation message only once overall was good enough. It gets reset at each reload, after all, and deprecation messages are for UMC authors while they work on their add-ons, not something you need to worry about while you use an add-on.

However, there's a related (bigger?) issue with this, in that if one UMC creator does not bother dealing with deprecation messages, that might disable these warnings for another author. So maybe making it MAI type specific is the way to go.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, it shows false positives because the code within this loop doesn't handle the old required/optional keys format? Is that the reasoning?

If that's the case, it feels like it'd be better to put the "if not showed_deprecation_message" outside the loop… though that adds another level of indentation…

Copy link
Member Author

@mattsc mattsc Jun 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, it shows false positives because the code within this loop doesn't handle the old required/optional keys format? Is that the reasoning?

Yes.

If that's the case, it feels like it'd be better to put the "if not showed_deprecation_message" outside the loop… though that adds another level of indentation…

Yeah, I know. I did it this way because if I put it around the outside, then that whole chunk of code has to have its indentation changed whenever this is removed in the future, even though it is just two little pieces of conditional that need to be removed and it will look like a much bigger change than it is. And it's not like this is a performance issue. But it is a little "less clean" this way, so I can move it outside if you prefer. It's admittedly not a very good reason to do it the way I did.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you have a good point there. Fair enough, we can leave it like this.

It's not a good reason to do it the way you did it, but in my opinion it is a good reason not to bother with the effort of rearranging it now that it's done.

@mattsc
Copy link
Member Author

mattsc commented Jun 28, 2022

Okay, sounds good.
I just squashed things. I'll go over it one more time tomorrow and then merge it unless there are further comments.

@mattsc mattsc merged commit 07d8a55 into wesnoth:master Jun 28, 2022
@mattsc mattsc deleted the mai_parms_tests branch June 28, 2022 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AI Issues with the AI engine, including micro AIs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants