Author: sveinung
Date: Fri Mar  6 12:16:42 2015
New Revision: 28429

URL: http://svn.gna.org/viewcvs/freeciv?rev=28429&view=rev
Log:
Make it possible to directly kill a unit from Lua.

Introduce unit_kill() in the edit module and a kill() method on the Unit
class. This saves ruleset developers from the work of hacking together a
Lua unit killer by (ab)using the parts of the API that may kill a unit. It
also serves as a reminder for Freeciv developers that units may die during a
Lua callback.

See patch #5894

Modified:
    trunk/server/scripting/api_server_edit.c
    trunk/server/scripting/api_server_edit.h
    trunk/server/scripting/tolua_server.pkg

Modified: trunk/server/scripting/api_server_edit.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/scripting/api_server_edit.c?rev=28429&r1=28428&r2=28429&view=diff
==============================================================================
--- trunk/server/scripting/api_server_edit.c    (original)
+++ trunk/server/scripting/api_server_edit.c    Fri Mar  6 12:16:42 2015
@@ -182,6 +182,27 @@
 }
 
 /*****************************************************************************
+  Kill the unit.
+*****************************************************************************/
+void api_edit_unit_kill(lua_State *L, Unit *punit, const char *reason,
+                        Player *killer)
+{
+  enum unit_loss_reason loss_reason;
+
+  LUASCRIPT_CHECK_STATE(L);
+  LUASCRIPT_CHECK_ARG_NIL(L, punit, 2, Unit);
+  LUASCRIPT_CHECK_ARG_NIL(L, reason, 3, string);
+  LUASCRIPT_CHECK_ARG_NIL(L, killer, 4, Player);
+
+  loss_reason = unit_loss_reason_by_name(reason, fc_strcasecmp);
+
+  LUASCRIPT_CHECK_ARG(L, unit_loss_reason_is_valid(loss_reason), 3,
+                      "Invalid unit loss reason");
+
+  wipe_unit(punit, loss_reason, killer);
+}
+
+/*****************************************************************************
   Create a new city.
 *****************************************************************************/
 void api_edit_create_city(lua_State *L, Player *pplayer, Tile *ptile,

Modified: trunk/server/scripting/api_server_edit.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/scripting/api_server_edit.h?rev=28429&r1=28428&r2=28429&view=diff
==============================================================================
--- trunk/server/scripting/api_server_edit.h    (original)
+++ trunk/server/scripting/api_server_edit.h    Fri Mar  6 12:16:42 2015
@@ -39,6 +39,9 @@
 
 void api_edit_unit_turn(lua_State *L, Unit *punit, Direction dir);
 
+void api_edit_unit_kill(lua_State *L, Unit *punit, const char *reason,
+                        Player *killer);
+
 void api_edit_create_city(lua_State *L, Player *pplayer, Tile *ptile,
                           const char *name);
 Player *api_edit_create_player(lua_State *L, const char *username,

Modified: trunk/server/scripting/tolua_server.pkg
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/scripting/tolua_server.pkg?rev=28429&r1=28428&r2=28429&view=diff
==============================================================================
--- trunk/server/scripting/tolua_server.pkg     (original)
+++ trunk/server/scripting/tolua_server.pkg     Fri Mar  6 12:16:42 2015
@@ -98,6 +98,9 @@
                         int moves_left, int hp_left, Unit *ptransport);
   bool api_edit_unit_teleport
     @ unit_teleport(lua_State *L, Unit *self, Tile *dest);
+  void api_edit_unit_kill
+    @ unit_kill(lua_State *L, Unit *self, const char *reason,
+                Player *killer);
   void api_edit_create_city
     @ create_city (lua_State *L, Player *pplayer, Tile *ptile,
                    const char *name);
@@ -255,6 +258,10 @@
   edit.unit_turn(self, direction)
 end
 
+function Unit:kill(reason, killer)
+  edit.unit_kill(self, reason, killer)
+end
+
 function Unit:move(moveto, movecost)
   return edit.unit_move(self, moveto, movecost)
 end


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to