Author: jtn
Date: Wed Nov 11 00:05:50 2015
New Revision: 30528

URL: http://svn.gna.org/viewcvs/freeciv?rev=30528&view=rev
Log:
Lua functions for finding player and nation trait properties.

Also, existing trait_mod() function now throws error if given invalid
trait string.

New file api_server_game_methods.c added for server-only methods on
modules defined in tolua_game.pkg.

See gna patch #6557.

Added:
    branches/S2_6/server/scripting/api_server_game_methods.c
    branches/S2_6/server/scripting/api_server_game_methods.h
Modified:
    branches/S2_6/server/scripting/Makefile.am
    branches/S2_6/server/scripting/api_server_edit.c
    branches/S2_6/server/scripting/api_server_edit.h
    branches/S2_6/server/scripting/tolua_server.pkg

Modified: branches/S2_6/server/scripting/Makefile.am
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/server/scripting/Makefile.am?rev=30528&r1=30527&r2=30528&view=diff
==============================================================================
--- branches/S2_6/server/scripting/Makefile.am  (original)
+++ branches/S2_6/server/scripting/Makefile.am  Wed Nov 11 00:05:50 2015
@@ -9,6 +9,7 @@
        -I$(top_srcdir)/common \
        -I$(top_srcdir)/common/scriptcore \
        -I$(top_srcdir)/server \
+       -I$(top_srcdir)/ai \
        $(FCDB_MYSQL_CFLAGS) $(FCDB_POSTGRES_CFLAGS) $(FCDB_SQLITE_CFLAGS) \
        $(LUA_CFLAGS) $(LUASQL_CFLAGS) $(TOLUA_CFLAGS)
 
@@ -19,6 +20,8 @@
        api_server_base.h       \
        api_server_edit.c       \
        api_server_edit.h       \
+       api_server_game_methods.c       \
+       api_server_game_methods.h       \
        api_server_notify.c     \
        api_server_notify.h     \
        script_server.c         \

Modified: branches/S2_6/server/scripting/api_server_edit.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/server/scripting/api_server_edit.c?rev=30528&r1=30527&r2=30528&view=diff
==============================================================================
--- branches/S2_6/server/scripting/api_server_edit.c    (original)
+++ branches/S2_6/server/scripting/api_server_edit.c    Wed Nov 11 00:05:50 2015
@@ -325,14 +325,18 @@
 /*****************************************************************************
   Modify player's trait value.
 *****************************************************************************/
-bool api_edit_trait_mod(lua_State *L, Player *pplayer, const char *trait_name,
-                        const int mod)
-{
-  enum trait tr = trait_by_name(trait_name, fc_strcasecmp);
-
-  if (!trait_is_valid(tr)) {
-    return FALSE;
-  }
+bool api_edit_trait_mod_set(lua_State *L, Player *pplayer,
+                            const char *trait_name, const int mod)
+{
+  enum trait tr;
+
+  LUASCRIPT_CHECK_STATE(L, -1);
+  LUASCRIPT_CHECK_ARG_NIL(L, pplayer, 2, Player, FALSE);
+  LUASCRIPT_CHECK_ARG_NIL(L, trait_name, 3, string, FALSE);
+
+  tr = trait_by_name(trait_name, fc_strcasecmp);
+
+  LUASCRIPT_CHECK_ARG(L, trait_is_valid(tr), 3, "no such trait", 0);
 
   pplayer->ai_common.traits[tr].mod += mod;
 

Modified: branches/S2_6/server/scripting/api_server_edit.h
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/server/scripting/api_server_edit.h?rev=30528&r1=30527&r2=30528&view=diff
==============================================================================
--- branches/S2_6/server/scripting/api_server_edit.h    (original)
+++ branches/S2_6/server/scripting/api_server_edit.h    Wed Nov 11 00:05:50 2015
@@ -50,8 +50,8 @@
 Tech_Type *api_edit_give_technology(lua_State *L, Player *pplayer,
                                     Tech_Type *ptech, int cost,
                                     const char *reason);
-bool api_edit_trait_mod(lua_State *L, Player *pplayer, const char *trait_name,
-                        const int mod);
+bool api_edit_trait_mod_set(lua_State *L, Player *pplayer,
+                            const char *trait_name, const int mod);
 
 void api_edit_create_extra(lua_State *L, Tile *ptile, const char *name);
 void api_edit_create_base(lua_State *L, Tile *ptile, const char *name,

Added: branches/S2_6/server/scripting/api_server_game_methods.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/server/scripting/api_server_game_methods.c?rev=30528&view=auto
==============================================================================
--- branches/S2_6/server/scripting/api_server_game_methods.c    (added)
+++ branches/S2_6/server/scripting/api_server_game_methods.c    Wed Nov 11 
00:05:50 2015
@@ -0,0 +1,142 @@
+/**********************************************************************
+ Freeciv - Copyright (C) 1996-2015 - Freeciv Development Team
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <fc_config.h>
+#endif
+
+/* common/scriptcore */
+#include "luascript.h"
+
+/* ai */
+#include "aitraits.h" /* ai_trait_get_value() */
+
+/* server/scripting */
+#include "script_server.h"
+
+#include "api_server_game_methods.h"
+
+/*****************************************************************************
+  Return the current value of an AI trait in force (base+mod)
+*****************************************************************************/
+int api_methods_player_trait(lua_State *L, Player *pplayer,
+                             const char *trait_name)
+{
+  enum trait tr;
+
+  LUASCRIPT_CHECK_STATE(L, -1);
+  LUASCRIPT_CHECK_SELF(L, pplayer, -1);
+  LUASCRIPT_CHECK_ARG_NIL(L, trait_name, 3, string, 0);
+
+  tr = trait_by_name(trait_name, fc_strcasecmp);
+
+  LUASCRIPT_CHECK_ARG(L, trait_is_valid(tr), 3, "no such trait", 0);
+
+  return ai_trait_get_value(tr, pplayer);
+}
+
+/*****************************************************************************
+  Return the current base value of an AI trait (not including Lua mod)
+*****************************************************************************/
+int api_methods_player_trait_base(lua_State *L, Player *pplayer,
+                                  const char *trait_name)
+{
+  enum trait tr;
+
+  LUASCRIPT_CHECK_STATE(L, -1);
+  LUASCRIPT_CHECK_SELF(L, pplayer, -1);
+  LUASCRIPT_CHECK_ARG_NIL(L, trait_name, 3, string, 0);
+
+  tr = trait_by_name(trait_name, fc_strcasecmp);
+
+  LUASCRIPT_CHECK_ARG(L, trait_is_valid(tr), 3, "no such trait", 0);
+
+  return pplayer->ai_common.traits[tr].val;
+}
+
+/*****************************************************************************
+  Return the current Lua increment to an AI trait
+  (can be changed with api_edit_trait_mod_set())
+*****************************************************************************/
+int api_methods_player_trait_current_mod(lua_State *L, Player *pplayer,
+                                         const char *trait_name)
+{
+  enum trait tr;
+
+  LUASCRIPT_CHECK_STATE(L, -1);
+  LUASCRIPT_CHECK_SELF(L, pplayer, -1);
+  LUASCRIPT_CHECK_ARG_NIL(L, trait_name, 3, string, 0);
+
+  tr = trait_by_name(trait_name, fc_strcasecmp);
+
+  LUASCRIPT_CHECK_ARG(L, trait_is_valid(tr), 3, "no such trait", 0);
+
+  return pplayer->ai_common.traits[tr].mod;
+}
+
+/*****************************************************************************
+  Return the minimum random trait value that will be allocated for a nation
+*****************************************************************************/
+int api_methods_nation_trait_min(lua_State *L, Nation_Type *pnation,
+                                 const char *trait_name)
+{
+  enum trait tr;
+
+  LUASCRIPT_CHECK_STATE(L, -1);
+  LUASCRIPT_CHECK_SELF(L, pnation, -1);
+  LUASCRIPT_CHECK_ARG_NIL(L, trait_name, 3, string, 0);
+
+  tr = trait_by_name(trait_name, fc_strcasecmp);
+
+  LUASCRIPT_CHECK_ARG(L, trait_is_valid(tr), 3, "no such trait", 0);
+
+  return pnation->server.traits[tr].min;
+}
+
+/*****************************************************************************
+  Return the maximum random trait value that will be allocated for a nation
+*****************************************************************************/
+int api_methods_nation_trait_max(lua_State *L, Nation_Type *pnation,
+                                 const char *trait_name)
+{
+  enum trait tr;
+
+  LUASCRIPT_CHECK_STATE(L, -1);
+  LUASCRIPT_CHECK_SELF(L, pnation, -1);
+  LUASCRIPT_CHECK_ARG_NIL(L, trait_name, 3, string, 0);
+
+  tr = trait_by_name(trait_name, fc_strcasecmp);
+
+  LUASCRIPT_CHECK_ARG(L, trait_is_valid(tr), 3, "no such trait", 0);
+
+  return pnation->server.traits[tr].max;
+}
+
+/*****************************************************************************
+  Return the default trait value that will be allocated for a nation
+*****************************************************************************/
+int api_methods_nation_trait_default(lua_State *L, Nation_Type *pnation,
+                                     const char *trait_name)
+{
+  enum trait tr;
+
+  LUASCRIPT_CHECK_STATE(L, -1);
+  LUASCRIPT_CHECK_SELF(L, pnation, -1);
+  LUASCRIPT_CHECK_ARG_NIL(L, trait_name, 3, string, 0);
+
+  tr = trait_by_name(trait_name, fc_strcasecmp);
+
+  LUASCRIPT_CHECK_ARG(L, trait_is_valid(tr), 3, "no such trait", 0);
+
+  return pnation->server.traits[tr].fixed;
+}

Added: branches/S2_6/server/scripting/api_server_game_methods.h
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/server/scripting/api_server_game_methods.h?rev=30528&view=auto
==============================================================================
--- branches/S2_6/server/scripting/api_server_game_methods.h    (added)
+++ branches/S2_6/server/scripting/api_server_game_methods.h    Wed Nov 11 
00:05:50 2015
@@ -0,0 +1,40 @@
+/**********************************************************************
+ Freeciv - Copyright (C) 1996-2015 - Freeciv Development Team
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifndef FC__API_SERVER_GAME_METHODS_H
+#define FC__API_SERVER_GAME_METHODS_H
+
+/* common/scriptcore */
+#include "luascript_types.h"
+
+/* server/scripting */
+#include "api_server_edit.h"
+
+/* Server-only methods added to the modules defined in
+ * the common tolua_game.pkg. */
+
+int api_methods_player_trait(lua_State *L, Player *pplayer,
+                             const char *trait_name);
+int api_methods_player_trait_base(lua_State *L, Player *pplayer,
+                                  const char *trait_name);
+int api_methods_player_trait_current_mod(lua_State *L, Player *pplayer,
+                                         const char *trait_name);
+
+int api_methods_nation_trait_min(lua_State *L, Nation_Type *pnation,
+                                 const char *trait_name);
+int api_methods_nation_trait_max(lua_State *L, Nation_Type *pnation,
+                                 const char *trait_name);
+int api_methods_nation_trait_default(lua_State *L, Nation_Type *pnation,
+                                     const char *trait_name);
+
+#endif /* FC__API_SERVER_GAME_METHODS_H */

Modified: branches/S2_6/server/scripting/tolua_server.pkg
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/server/scripting/tolua_server.pkg?rev=30528&r1=30527&r2=30528&view=diff
==============================================================================
--- branches/S2_6/server/scripting/tolua_server.pkg     (original)
+++ branches/S2_6/server/scripting/tolua_server.pkg     Wed Nov 11 00:05:50 2015
@@ -33,6 +33,7 @@
 $#include "api_server_edit.h"
 $#include "api_server_base.h"
 $#include "api_server_notify.h"
+$#include "api_server_game_methods.h"
 $#include "script_server.h"
 
 /* Server module. */
@@ -134,7 +135,7 @@
   Tech_Type *api_edit_give_technology
     @ give_technology (lua_State *L, Player *pplayer, Tech_Type *ptech,
                        int cost, const char *reason);
-  bool api_edit_trait_mod
+  bool api_edit_trait_mod_set
     @ trait_mod (lua_State *L, Player *pplayer, const char *trait_name,
                  const int mod);
   bool api_edit_unleash_barbarians
@@ -303,3 +304,24 @@
 end
 
 $]
+
+/* Additions to common Player module. */
+module Player {
+  int api_methods_player_trait
+    @ trait (lua_State *L, Player *pplayer, const char *trait_name);
+  int api_methods_player_trait_base
+    @ trait_base (lua_State *L, Player *pplayer, const char *trait_name);
+  int api_methods_player_trait_current_mod
+    @ trait_current_mod (lua_State *L, Player *pplayer, const char 
*trait_name);
+}
+
+/* Additions to common Nation_Type module. */
+module Nation_Type {
+  int api_methods_nation_trait_min
+    @ trait_min (lua_State *L, Nation_Type *pnation, const char *trait_name);
+  int api_methods_nation_trait_max
+    @ trait_max (lua_State *L, Nation_Type *pnation, const char *trait_name);
+  int api_methods_nation_trait_default
+    @ trait_default (lua_State *L, Nation_Type *pnation,
+                     const char *trait_name);
+}


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

Reply via email to