Skip to content

Commit 467fc0d

Browse files
committedMar 18, 2015
Add a Lua call to do damages / heals
1 parent 8f2e9bf commit 467fc0d

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed
 

Diff for: ‎doc/lua_api.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2336,6 +2336,7 @@ This is basically a reference to a C++ `ServerActiveObject`
23362336
* `right_click(clicker)`; `clicker` is another `ObjectRef`
23372337
* `get_hp()`: returns number of hitpoints (2 * number of hearts)
23382338
* `set_hp(hp)`: set number of hitpoints (2 * number of hearts)
2339+
* `apply_damage(damage)`: set amount of damage to object. If damage < 0, heal the target
23392340
* `get_inventory()`: returns an `InvRef`
23402341
* `get_wield_list()`: returns the name of the inventory list the wielded item is in
23412342
* `get_wield_index()`: returns the index of the wielded item

Diff for: ‎src/script/lua_api/l_object.cpp

+38-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2929
#include "content_sao.h"
3030
#include "server.h"
3131
#include "hud.h"
32+
#include "settings.h"
33+
#include "main.h"
3234

3335

3436
struct EnumString es_HudElementType[] =
@@ -255,10 +257,10 @@ int ObjectRef::l_set_hp(lua_State *L)
255257
ObjectRef *ref = checkobject(L, 1);
256258
luaL_checknumber(L, 2);
257259
ServerActiveObject *co = getobject(ref);
258-
if(co == NULL) return 0;
260+
if(co == NULL)
261+
return 0;
259262
int hp = lua_tonumber(L, 2);
260-
/*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
261-
<<" hp="<<hp<<std::endl;*/
263+
262264
// Do it
263265
co->setHP(hp);
264266
if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
@@ -289,6 +291,38 @@ int ObjectRef::l_get_hp(lua_State *L)
289291
return 1;
290292
}
291293

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+
292326
// get_inventory(self)
293327
int ObjectRef::l_get_inventory(lua_State *L)
294328
{
@@ -1345,6 +1379,7 @@ const luaL_reg ObjectRef::methods[] = {
13451379
luamethod(ObjectRef, right_click),
13461380
luamethod(ObjectRef, set_hp),
13471381
luamethod(ObjectRef, get_hp),
1382+
luamethod(ObjectRef, apply_damage),
13481383
luamethod(ObjectRef, get_inventory),
13491384
luamethod(ObjectRef, get_wield_list),
13501385
luamethod(ObjectRef, get_wield_index),

Diff for: ‎src/script/lua_api/l_object.h

+6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ class ObjectRef : public ModApiBase {
8383
// 0 if not applicable to this type of object
8484
static int l_get_hp(lua_State *L);
8585

86+
// apply_damage(self, damage)
87+
// damage = amount of damage to apply
88+
// if damage is negative, heal the player
89+
// returns: nil
90+
static int l_apply_damage(lua_State *L);
91+
8692
// get_inventory(self)
8793
static int l_get_inventory(lua_State *L);
8894

0 commit comments

Comments
 (0)
Please sign in to comment.