1 /***********************************************************************
2  Freeciv - Copyright (C) 1996-2015 - Freeciv Development Team
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; either version 2, or (at your option)
6    any later version.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 ***********************************************************************/
13 
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
17 
18 /* common/scriptcore */
19 #include "luascript.h"
20 
21 /* ai */
22 #include "aitraits.h" /* ai_trait_get_value() */
23 
24 /* server/scripting */
25 #include "script_server.h"
26 
27 #include "api_server_game_methods.h"
28 
29 /*****************************************************************************
30   Return the current value of an AI trait in force (base+mod)
31 *****************************************************************************/
api_methods_player_trait(lua_State * L,Player * pplayer,const char * tname)32 int api_methods_player_trait(lua_State *L, Player *pplayer,
33                              const char *tname)
34 {
35   enum trait tr;
36 
37   LUASCRIPT_CHECK_STATE(L, -1);
38   LUASCRIPT_CHECK_SELF(L, pplayer, -1);
39   LUASCRIPT_CHECK_ARG_NIL(L, tname, 3, string, 0);
40 
41   tr = trait_by_name(tname, fc_strcasecmp);
42 
43   LUASCRIPT_CHECK_ARG(L, trait_is_valid(tr), 3, "no such trait", 0);
44 
45   return ai_trait_get_value(tr, pplayer);
46 }
47 
48 /*****************************************************************************
49   Return the current base value of an AI trait (not including Lua mod)
50 *****************************************************************************/
api_methods_player_trait_base(lua_State * L,Player * pplayer,const char * tname)51 int api_methods_player_trait_base(lua_State *L, Player *pplayer,
52                                   const char *tname)
53 {
54   enum trait tr;
55 
56   LUASCRIPT_CHECK_STATE(L, -1);
57   LUASCRIPT_CHECK_SELF(L, pplayer, -1);
58   LUASCRIPT_CHECK_ARG_NIL(L, tname, 3, string, 0);
59 
60   tr = trait_by_name(tname, fc_strcasecmp);
61 
62   LUASCRIPT_CHECK_ARG(L, trait_is_valid(tr), 3, "no such trait", 0);
63 
64   return pplayer->ai_common.traits[tr].val;
65 }
66 
67 /*****************************************************************************
68   Return the current Lua increment to an AI trait
69   (can be changed with api_edit_trait_mod_set())
70 *****************************************************************************/
api_methods_player_trait_current_mod(lua_State * L,Player * pplayer,const char * tname)71 int api_methods_player_trait_current_mod(lua_State *L, Player *pplayer,
72                                          const char *tname)
73 {
74   enum trait tr;
75 
76   LUASCRIPT_CHECK_STATE(L, -1);
77   LUASCRIPT_CHECK_SELF(L, pplayer, -1);
78   LUASCRIPT_CHECK_ARG_NIL(L, tname, 3, string, 0);
79 
80   tr = trait_by_name(tname, fc_strcasecmp);
81 
82   LUASCRIPT_CHECK_ARG(L, trait_is_valid(tr), 3, "no such trait", 0);
83 
84   return pplayer->ai_common.traits[tr].mod;
85 }
86 
87 /*****************************************************************************
88   Return the minimum random trait value that will be allocated for a nation
89 *****************************************************************************/
api_methods_nation_trait_min(lua_State * L,Nation_Type * pnation,const char * tname)90 int api_methods_nation_trait_min(lua_State *L, Nation_Type *pnation,
91                                  const char *tname)
92 {
93   enum trait tr;
94 
95   LUASCRIPT_CHECK_STATE(L, -1);
96   LUASCRIPT_CHECK_SELF(L, pnation, -1);
97   LUASCRIPT_CHECK_ARG_NIL(L, tname, 3, string, 0);
98 
99   tr = trait_by_name(tname, fc_strcasecmp);
100 
101   LUASCRIPT_CHECK_ARG(L, trait_is_valid(tr), 3, "no such trait", 0);
102 
103   return pnation->server.traits[tr].min;
104 }
105 
106 /*****************************************************************************
107   Return the maximum random trait value that will be allocated for a nation
108 *****************************************************************************/
api_methods_nation_trait_max(lua_State * L,Nation_Type * pnation,const char * tname)109 int api_methods_nation_trait_max(lua_State *L, Nation_Type *pnation,
110                                  const char *tname)
111 {
112   enum trait tr;
113 
114   LUASCRIPT_CHECK_STATE(L, -1);
115   LUASCRIPT_CHECK_SELF(L, pnation, -1);
116   LUASCRIPT_CHECK_ARG_NIL(L, tname, 3, string, 0);
117 
118   tr = trait_by_name(tname, fc_strcasecmp);
119 
120   LUASCRIPT_CHECK_ARG(L, trait_is_valid(tr), 3, "no such trait", 0);
121 
122   return pnation->server.traits[tr].max;
123 }
124 
125 /*****************************************************************************
126   Return the default trait value that will be allocated for a nation
127 *****************************************************************************/
api_methods_nation_trait_default(lua_State * L,Nation_Type * pnation,const char * tname)128 int api_methods_nation_trait_default(lua_State *L, Nation_Type *pnation,
129                                      const char *tname)
130 {
131   enum trait tr;
132 
133   LUASCRIPT_CHECK_STATE(L, -1);
134   LUASCRIPT_CHECK_SELF(L, pnation, -1);
135   LUASCRIPT_CHECK_ARG_NIL(L, tname, 3, string, 0);
136 
137   tr = trait_by_name(tname, fc_strcasecmp);
138 
139   LUASCRIPT_CHECK_ARG(L, trait_is_valid(tr), 3, "no such trait", 0);
140 
141   return pnation->server.traits[tr].fixed;
142 }
143