Page 1 of 1

Changing max. player health

PostPosted: Fri Jul 11, 2014 04:18
by Wuzzy
Hi.
I’m suggesting a Lua API feature which allows the modder to change the maximum health for players.

Re: Changing max. player health

PostPosted: Fri Jul 11, 2014 06:59
by Ragnar
I think it's possible to change the HP to be over the max (20) directly (i think player.hp )

Re: Changing max. player health

PostPosted: Fri Jul 11, 2014 12:42
by Wuzzy
No, this doesn’t work. There is no field “hp” in a player object; I checked it.

Re: Changing max. player health

PostPosted: Fri Jul 11, 2014 18:45
by Calinou
Reported here on GitHub.

Re: Changing max. player health

PostPosted: Mon Jul 14, 2014 20:26
by Wuzzy
It seems that there is no need for new Lua code. Instead, the old one should just be fixed. Actually, I found this in lua_api.txt:

object:set_properties(object_properties_table)

Players are objects. The object properties table includes a field called “hp_max”. So I would expect that

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
player:set_properties({hp_max=5})


sets the max HP to 5. But nothing happens. :/ If I have 1 HP, I can still heal myself up to full 20 HP.
I also noticed that
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
player:set_properties({makes_footstep_sound=false})

does not change anything.

is_visible=false does not work either. Yeah, it seems that some object properties can not be changed for players. Bug?

Actually, using a call like player:set_properties is not uncommon, it used serveral times in minetest_game/default/player.lua. But it is only used to set models and textures and stuff like that.

Re: Changing max. player health

PostPosted: Mon Jul 14, 2014 21:24
by DeepGaze
@Wuzzy the heart issue is AFAIK where the engine edits "player.txt" if you shut down and edit the file in notepad/gedit the 100 hearts are listed as your health but if you heal at 99 it sets it to 20.
the sounds I have no idea towards, this is just experience and not where i understand the code(I don't understand C)

Re: Changing max. player health

PostPosted: Tue Jul 15, 2014 21:41
by prestidigitator
Poking around in the source code, it appears you can set the "hp_max" property on any object, whether it is a player or not. However, the only time the "hp_max" property is actually used is when a new non-player object is first added to the environment. You can even set such an object's HPs above this "maximum" without any kind of checks at all. It's not really a maximum, it is an initial/default value. It should have been called "initial_hp" or something. In fact, seeing as there is apparently no way to get properties (what kind of API is this anyway?), not even Lua can use the "hp_max" property to do bounds checking.

As for player objects, they have a compile-time constant (#define'd even) PLAYER_MAX_HP with a value of 20 that serves as both an initial value for HPs and as an absolute maximum. The "hp_max" property exists for player objects, but isn't used at all (other than being initialized to PLAYER_MAX_HP when the object is created). The somewhat good news, I guess, is that it appears clients will respect whatever value is set on the server, so if you wanted to build a custom server with a new value for PLAYER_MAX_HP, or one that used the "hp_max" property instead even, existing clients should work fine. You're just out of luck trying to do anything from mod code.

By the way, changing the server code to use something more dynamic like the "hp_max" object property appears like it would be pretty darned trivial, for anyone who wants to try to get a pull request past the dev team (hint: PlayerSAO::setHP(s16) in content_sao.cpp and Server::RespawnPlayer(u16) in server.cpp).

Re: Changing max. player health

PostPosted: Wed Jul 16, 2014 20:59
by Wuzzy
Prestidigitator, I thank you for your insights. So, first of all, I stick to my initial suggestion, of course. :=)

Using name “hp_max” for something which isn’t actually the maximum HP is pretty stupid. Also putting initial HP and maximum HP into the same bucket is not the smartest move either. It’s annoying but not really serious, because it can be worked around by immediately calling set_hp after creating the object.
If I understood correctly, the problem here is that the max HP can not be changed after creation. But then the question arises why hp_max is even in set_properties then. It looks like some API cleanup is needed. The tricky part is the question how to do it without breaking anything.

prestidigitator wrote:what kind of API is this anyway?

Obviously an unfinished one. ;-)
As for your complaint about get_properties being not existant. I filed an issue on GitHub. I noticed that there are quite some missing getter functions.