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

Clean up l_object.cpp #10512

Merged
merged 12 commits into from
Oct 22, 2020
Merged

Clean up l_object.cpp #10512

merged 12 commits into from
Oct 22, 2020

Conversation

Zughy
Copy link
Member

@Zughy Zughy commented Oct 17, 2020

So, I've decided cleaning up MT code on the go. I think a consistent ordered code is vital both for core devs and for possible contributors. This is, in short, what I did:

  • consistent names
    • ObjectRef = obj
    • ServerActiveObject = sao
    • LuaEntitySAO = entitysao
    • PlayerSAO = playersao
    • RemotePlayer = player
  • consistent structure: check for object, variables declaration, execution+return. These sections are separated by an empty line. When functions were too long, the execution got split up accordingly.
  • removed all NULL checks. Now there are nullptrs instead, as I find it more straightforward than !something
  • removed useless comments, e.g. //Do it
  • removed is_player_connected, a deprecated method that wasn't even listed in the API
  • descriptions above functions now match the API
  • removed info about parameters in the functions description: they're already explained in the API
  • edited the API where needed
  • simplified a few methods to make them more readable e.g. set_bone_position
  • added missing Lua checks ( partially solves Many Lua C functions are missing type checks #1794 )

Things I'm not sure about, core dev help needed:

  • how to name p in set_sprite(...)
  • the old -1 value of HUD IDs, returned when ID was not a number. I had a look at the code and it doesn't look to me was meaningful at all so I simply removed it, but a double check is always better
  • debug lines at 2249 and 2257. Do you want to keep them?
  • TODO at 483: has it been solved?

To do

This PR is Ready for Review.

How to test

Well: check the code. Also, I might have made a few mistakes as I'm human, even if I double-checked my edits twice. So idk, take a solid game, play it a little bit and see if it all works accordingly.

Lastly, I want to underline how, imho, this should be high priority: I've seen things which were pretty messed up, like edits pushed a couple of weeks ago going completely against the guidelines (the minimap), or useless 8 year old comments who nobody apparently decided to remove. A tidy environment helps a lot. And I've actually had fun doing it, it was relaxing

@paramat paramat added Code quality Maintenance Tasks to keep the codebase and related parts in order, including architectural improvements labels Oct 17, 2020
doc/lua_api.txt Outdated Show resolved Hide resolved
@paramat
Copy link
Contributor

paramat commented Oct 17, 2020

how to name p in set_sprite(...)

set_sprite(p, num_frames, framelength, select_x_by_camera)
p: {x=column number, y=row number}, the coordinate of the first frame default: {x=0, y=0}

How about 'first_frame' or 'start_frame'?
A comma should be added in the docs after 'the coordinate of the first frame'.

@Zughy
Copy link
Member Author

Zughy commented Oct 18, 2020

as a non native speaker, start_frame is more intuitive to me. I'll wait for more feedback though

@sfan5
Copy link
Member

sfan5 commented Oct 18, 2020

how to name p in set_sprite(...)

frame_pos or sprite_pos

debug lines at 2249 and 2257. Do you want to keep them?

No, anyone can write them again if they need them.

 

Regarding the renaming: I think it's useful to change the variable if it's misnamed (e.g. ServerActiveObject *co), but changing
o to obj in a short function is not an improvement.
Also I don't think !player to player == nullptr is an improvement.

doc/lua_api.txt Outdated Show resolved Hide resolved
@Zughy
Copy link
Member Author

Zughy commented Oct 18, 2020

Also I don't think !player to player == nullptr is an improvement.

It was simply a matter of choosing one or the other, as the code featured every variation of it (a lot of them also missed the new line for the body). Relevant discussion: #10480 (comment). As someone who's quite a beginner in C++, nullptr helps me understand better all the pointers thing, that's why I opted for it

doc/lua_api.txt Outdated Show resolved Hide resolved
@sfan5
Copy link
Member

sfan5 commented Oct 18, 2020

It was simply a matter of choosing one or the other, as the code featured every variation of it

My point here is that changing bad style (x == NULL) to good style (e.g. x == nullptr) is useful, but changing good style (!x) to a different good style is not useful.

As someone who's quite a beginner in C++, nullptr helps me understand better all the pointers thing, that's why I opted for it

...just like this wouldn't justify changing !foo to foo == nullptr everywhere in Minetest.

@Zughy
Copy link
Member Author

Zughy commented Oct 18, 2020

I understand your point now, I'll avoid doing that in the future, I'm sorry (but please don't make me revert the ones I've already edited, it's... a lot)

@v-rob
Copy link
Member

v-rob commented Oct 19, 2020

Reverting shouldn't be necessary at this point; it's not like it's a very hard change to review, even if there are a lot.

Also, I think start_frame or start_pos is pretty good. It doesn't matter a whole lot, any of the given suggestions are better than p.

Copy link
Member

@v-rob v-rob left a comment

Choose a reason for hiding this comment

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

Looks pretty good. I don't like the weird type checks everywhere, but that's better solved with better Lua-interfacing functions, like extending readParam and adding something like paramIsType. But that's for another PR, so I won't press inconsistent type checking here.

doc/lua_api.txt Outdated Show resolved Hide resolved
src/script/lua_api/l_object.cpp Outdated Show resolved Hide resolved
src/script/lua_api/l_object.cpp Outdated Show resolved Hide resolved
src/script/lua_api/l_object.cpp Outdated Show resolved Hide resolved
if (isNaN(L, 2))
throw LuaError("ObjectRef::set_yaw: NaN value is not allowed.");

float yaw = readParam<float>(L, 2) * core::RADTODEG;
co->setRotation(v3f(0, yaw, 0));

entitysao->setRotation(v3f(0, yaw, 0));
Copy link
Member

Choose a reason for hiding this comment

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

Question (not related to this cleanup, just a general question), but doesn't setting the other two to 0 reset the pitch and roll of ObjectRef:set_rotation? If that's so, sounds like a bug.

Copy link
Member

@rubenwardy rubenwardy Oct 21, 2020

Choose a reason for hiding this comment

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

I have a feeling that doing this any other way may mess with XYZ/ZYX ordering - ie: the yaw won't turn out correctly

An issue should probably be created from this

@Zughy
Copy link
Member Author

Zughy commented Oct 19, 2020

Thanks v-rob for the in-depth review, all problems have been addressed, and p is now start_frame. I'd like to remind the other 2 doubts of mine:

  • the old -1 value of HUD IDs, returned when ID was not a number. I had a look at the code and it doesn't look to me was meaningful at all so I simply removed it, but a double check is always better
  • TODO at 483: has it been solved?

Also, I noticed readParam<bool> and readParam<std::string> take an optional default value, while others don't. Is it normal? (helper.cpp)

doc/lua_api.txt Outdated Show resolved Hide resolved
Copy link
Member

@v-rob v-rob 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'm glad to see cleanups and code consistency.

@rubenwardy rubenwardy changed the title l_object.cpp clean up Clean up l_object.cpp Oct 22, 2020
@rubenwardy rubenwardy merged commit 33b2c5f into minetest:master Oct 22, 2020
@Zughy Zughy deleted the refactor branch October 23, 2020 16:32
@Lejo1
Copy link
Contributor

Lejo1 commented Oct 28, 2020

As I just recolonized this PR removes the pretty well used player:is_player_connected function and breaks some mods on the way.
What's the replace for it? Is it minetest.is_player?
If yes I think it should be clear that it only returns true if the player is online.

@sfan5
Copy link
Member

sfan5 commented Oct 28, 2020

is_player_connected has been marked deprecated since 5.2.0 (released 9 months ago) and there has been a deprecation warning the whole time.
Where exactly is it well used?

@Lejo1
Copy link
Contributor

Lejo1 commented Oct 28, 2020

Where exactly is it well used?

That might sound silly. It's just I always used this functions like any other so I expected it to be official. I learned minetest modding mostly by doing so I must have seen the function somewhere else.
Anyhow such changes are needed. Is minetest.is_player now the official replacement?
EDIT: Or is it just completely removed and my maybe dirty way of storing it using minetst.after and using it later is not anymore supported?

@sfan5
Copy link
Member

sfan5 commented Oct 29, 2020

Is minetest.is_player now the official replacement?

is_player does not do the same thing.
If you absolutely have to determine if an ObjectRef is valid you can use e.g. get_yaw() ~= nil, but you should really rework your code to pass along the player name to minetest.after and then retrieve the player again.

Or is it just completely removed and my maybe dirty way of storing it using minetst.after and using it later is not anymore supported?

See here: (section was added with 5.2.0)

minetest/doc/lua_api.txt

Lines 6107 to 6118 in 68cd93b

### Advice on handling `ObjectRefs`
When you receive an `ObjectRef` as a callback argument or from another API
function, it is possible to store the reference somewhere and keep it around.
It will keep functioning until the object is unloaded or removed.
However, doing this is **NOT** recommended as there is (intentionally) no method
to test if a previously acquired `ObjectRef` is still valid.
Instead, `ObjectRefs` should be "let go" of as soon as control is returned from
Lua back to the engine.
Doing so is much less error-prone and you will never need to wonder if the
object you are working with still exists.

std::vector<MinimapMode> modes;
s16 selected_mode = luaL_checkint(L, 3);
Copy link
Member

Choose a reason for hiding this comment

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

"2" is a valid number but fails in luaL_checkint.

Copy link
Member

Choose a reason for hiding this comment

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

(Missing type coercion)

return 0;

u32 id = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : -1;
u32 id = luaL_checkint(L, 2);
Copy link
Member

Choose a reason for hiding this comment

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

Assuming Krock's related comment is correct, the issue with luaL_checkint applies here too (strings are valid numbers, but the function won't accept them).

Copy link
Member Author

Choose a reason for hiding this comment

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

(assuming he's right) why exactly someone should pass a string instead of a number though? The API says nothing about it

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 called type coercion. Lua should convert strings to numbers when used in a numeric context - although, the documents are unclear as to whether this is just with arithmetic or with other contexts too

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 mean, that's useful for things like print("this is a number: " .. 55) but I still don't see why someone should pass a string into this function (hud_change). Anyways, a solution could be to have a readParam<int>(...) checking for NaN, applying type coercion and then casting into an int (if it's a decimal).
Also, the previous version was wrong too, because I could have passed a float, and HUDs take no floats

Copy link
Member

Choose a reason for hiding this comment

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

I don't know how important this really is (do other functions consistently take strings as numbers?), especially since you're not meant to do funny things to HUD IDs.

return 0;

u32 id = lua_tonumber(L, -1);
u32 id = luaL_checkint(L, 2);
Copy link
Member

Choose a reason for hiding this comment

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

same

HybridDog pushed a commit to HybridDog/minetest that referenced this pull request Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code quality Maintenance Tasks to keep the codebase and related parts in order, including architectural improvements >= Two approvals ✅ ✅
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants