From 7d3e8cf540203bcfcf795d5abe68f1373c98785c Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 12 May 2024 02:03:30 +0300 Subject: [PATCH 63/81] Lua: Add edit.unit_add_hitpoints() Requested by ihnatus See osdn #44771 Signed-off-by: Marko Lindqvist --- server/scripting/api_server_edit.c | 36 ++++++++++++++++++++++++++++++ server/scripting/api_server_edit.h | 2 ++ server/scripting/tolua_server.pkg | 3 +++ 3 files changed, 41 insertions(+) diff --git a/server/scripting/api_server_edit.c b/server/scripting/api_server_edit.c index f96f507ad5..1ca520bd87 100644 --- a/server/scripting/api_server_edit.c +++ b/server/scripting/api_server_edit.c @@ -572,6 +572,42 @@ void api_edit_unit_kill(lua_State *L, Unit *punit, const char *reason, wipe_unit(punit, loss_reason, killer); } +/**********************************************************************//** + Change unit hitpoints. Reason and killer are used if unit dies. +**************************************************************************/ +bool api_edit_unit_hitpoints(lua_State *L, Unit *self, int change, + const char *reason, Player *killer) +{ + LUASCRIPT_CHECK_STATE(L, TRUE); + LUASCRIPT_CHECK_ARG_NIL(L, self, 2, Unit, TRUE); + + self->hp += change; + + if (self->hp <= 0) { + enum unit_loss_reason loss_reason + = unit_loss_reason_by_name(reason, fc_strcasecmp); + + wipe_unit(self, loss_reason, killer); + + /* Intentionally only after wiping, so that unit is never left with + * zero or less hit points. */ + LUASCRIPT_CHECK_ARG(L, unit_loss_reason_is_valid(loss_reason), 4, + "Invalid unit loss reason", FALSE); + + return FALSE; + } else { + int max = unit_type_get(self)->hp; + + if (self->hp > max) { + self->hp = max; + } + } + + send_unit_info(NULL, self); + + return TRUE; +} + /**********************************************************************//** Change terrain on tile **************************************************************************/ diff --git a/server/scripting/api_server_edit.h b/server/scripting/api_server_edit.h index a62362297d..b325cf06cd 100644 --- a/server/scripting/api_server_edit.h +++ b/server/scripting/api_server_edit.h @@ -66,6 +66,8 @@ bool api_edit_unit_transform(lua_State *L, Unit *punit, Unit_Type *ptype, void api_edit_unit_kill(lua_State *L, Unit *punit, const char *reason, Player *killer); +bool api_edit_unit_hitpoints(lua_State *L, Unit *self, int change, + const char *reason, Player *killer); bool api_edit_change_terrain(lua_State *L, Tile *ptile, Terrain *pterr); diff --git a/server/scripting/tolua_server.pkg b/server/scripting/tolua_server.pkg index b33ce88bb1..57de42648b 100644 --- a/server/scripting/tolua_server.pkg +++ b/server/scripting/tolua_server.pkg @@ -132,6 +132,9 @@ module edit { void api_edit_unit_kill @ unit_kill(lua_State *L, Unit *self, const char *reason, Player *killer); + bool api_edit_unit_hitpoints + @ unit_add_hitpoints(lua_State *L, Unit *self, int change, + const char *reason, Player *killer); bool api_edit_change_terrain @ change_terrain(lua_State *L, Tile *ptile, Terrain *pterr); bool api_edit_create_city -- 2.43.0