@@ -29,6 +29,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
29
29
#include " content_sao.h"
30
30
#include " server.h"
31
31
#include " hud.h"
32
+ #include " settings.h"
33
+ #include " main.h"
32
34
33
35
34
36
struct EnumString es_HudElementType[] =
@@ -255,10 +257,10 @@ int ObjectRef::l_set_hp(lua_State *L)
255
257
ObjectRef *ref = checkobject (L, 1 );
256
258
luaL_checknumber (L, 2 );
257
259
ServerActiveObject *co = getobject (ref);
258
- if (co == NULL ) return 0 ;
260
+ if (co == NULL )
261
+ return 0 ;
259
262
int hp = lua_tonumber (L, 2 );
260
- /* infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
261
- <<" hp="<<hp<<std::endl;*/
263
+
262
264
// Do it
263
265
co->setHP (hp);
264
266
if (co->getType () == ACTIVEOBJECT_TYPE_PLAYER) {
@@ -289,6 +291,38 @@ int ObjectRef::l_get_hp(lua_State *L)
289
291
return 1 ;
290
292
}
291
293
294
+ // apply_damage(self, damage)
295
+ // damage = amount of damage to apply
296
+ // if damage is negative, heal the player
297
+ // returns: nil
298
+ int ObjectRef::l_apply_damage (lua_State *L)
299
+ {
300
+ NO_MAP_LOCK_REQUIRED;
301
+ ObjectRef *ref = checkobject (L, 1 );
302
+ luaL_checknumber (L, 2 );
303
+ ServerActiveObject *co = getobject (ref);
304
+ if (co == NULL )
305
+ return 0 ;
306
+
307
+ int damage = lua_tonumber (L, 2 );
308
+
309
+ // No damage, no heal => do nothing
310
+ if (damage == 0 )
311
+ return 0 ;
312
+
313
+ // If damage is positive (not healing) and damage is disabled, ignore
314
+ if (damage > 0 && g_settings->getBool (" enable_damage" ) == false )
315
+ return 0 ;
316
+
317
+ // Do damage/heal
318
+ co->setHP (co->getHP () - damage);
319
+ if (co->getType () == ACTIVEOBJECT_TYPE_PLAYER) {
320
+ getServer (L)->SendPlayerHPOrDie (((PlayerSAO*)co)->getPeerID (), co->getHP () == 0 );
321
+ }
322
+
323
+ return 0 ;
324
+ }
325
+
292
326
// get_inventory(self)
293
327
int ObjectRef::l_get_inventory (lua_State *L)
294
328
{
@@ -1345,6 +1379,7 @@ const luaL_reg ObjectRef::methods[] = {
1345
1379
luamethod (ObjectRef, right_click),
1346
1380
luamethod (ObjectRef, set_hp),
1347
1381
luamethod (ObjectRef, get_hp),
1382
+ luamethod (ObjectRef, apply_damage),
1348
1383
luamethod (ObjectRef, get_inventory),
1349
1384
luamethod (ObjectRef, get_wield_list),
1350
1385
luamethod (ObjectRef, get_wield_index),
0 commit comments