1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Add reasons to on_dieplayer and on_hpchange

This commit is contained in:
Andrew Ward 2018-03-28 16:05:18 +01:00 committed by GitHub
parent 2323842dd3
commit dfc8198349
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 219 additions and 52 deletions

View file

@ -194,13 +194,14 @@ int ObjectRef::l_punch(lua_State *L)
// If the punched is a player, and its HP changed
if (src_original_hp != co->getHP() &&
co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co);
getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
}
// If the puncher is a player, and its HP changed
if (dst_origin_hp != puncher->getHP() &&
puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
getServer(L)->SendPlayerHPOrDie((PlayerSAO *)puncher);
getServer(L)->SendPlayerHPOrDie((PlayerSAO *)puncher,
PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, co));
}
return 0;
}
@ -226,17 +227,36 @@ int ObjectRef::l_right_click(lua_State *L)
int ObjectRef::l_set_hp(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
// Get Object
ObjectRef *ref = checkobject(L, 1);
luaL_checknumber(L, 2);
ServerActiveObject *co = getobject(ref);
if (co == NULL) return 0;
if (co == NULL)
return 0;
// Get HP
int hp = lua_tonumber(L, 2);
/*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
<<" hp="<<hp<<std::endl;*/
// Get Reason
PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
reason.from_mod = true;
if (lua_istable(L, 3)) {
lua_pushvalue(L, 3);
lua_getfield(L, -1, "type");
if (lua_isstring(L, -1) && !reason.setTypeFromString(lua_tostring(L, -1))) {
errorstream << "Bad type given!" << std::endl;
}
lua_pop(L, 1);
reason.lua_reference = luaL_ref(L, LUA_REGISTRYINDEX);
}
// Do it
co->setHP(hp);
co->setHP(hp, reason);
if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co);
getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
// Return
return 0;
@ -729,9 +749,10 @@ int ObjectRef::l_set_properties(lua_State *L)
return 0;
read_object_properties(L, 2, prop, getServer(L)->idef());
if (prop->hp_max < co->getHP()) {
co->setHP(prop->hp_max);
PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
co->setHP(prop->hp_max, reason);
if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co);
getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
}
co->notifyObjectPropertiesModified();
return 0;