Rework civ2civ3 ruleset help and documentation.
[freeciv.git] / client / luascript / script_client.c
blobaed729c3e187cf6c461b9a25345f435c5ee11ad2
1 /*****************************************************************************
2 Freeciv - Copyright (C) 2005 - The Freeciv Project
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.
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 *****************************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 #include <stdarg.h>
19 #include <stdlib.h>
20 #include <time.h>
22 /* dependencies/lua */
23 #include "lua.h"
24 #include "lualib.h"
26 /* dependencies/tolua */
27 #include "tolua.h"
29 /* utility */
30 #include "log.h"
32 /* common */
33 #include "featured_text.h"
35 /* common/scriptcore */
36 #include "api_game_specenum.h"
37 #include "luascript.h"
38 #include "tolua_common_a_gen.h"
39 #include "tolua_common_z_gen.h"
40 #include "tolua_game_gen.h"
41 #include "tolua_signal_gen.h"
43 /* client */
44 #include "luaconsole_common.h"
46 /* client/luascript */
47 #include "tolua_client_gen.h"
49 #include "script_client.h"
51 /*****************************************************************************
52 Lua virtual machine state.
53 *****************************************************************************/
54 static struct fc_lua *fcl = NULL;
56 /*****************************************************************************
57 Optional game script code (useful for scenarios).
58 *****************************************************************************/
59 static char *script_client_code = NULL;
61 static void script_client_vars_init(void);
62 static void script_client_vars_free(void);
63 static void script_client_vars_load(struct section_file *file);
64 static void script_client_vars_save(struct section_file *file);
65 static void script_client_code_init(void);
66 static void script_client_code_free(void);
67 static void script_client_code_load(struct section_file *file);
68 static void script_client_code_save(struct section_file *file);
70 static void script_client_output(struct fc_lua *fcl, enum log_level level,
71 const char *format, ...)
72 fc__attribute((__format__ (__printf__, 3, 4)));
74 static void script_client_signal_create(void);
76 /*****************************************************************************
77 Parse and execute the script in str
78 *****************************************************************************/
79 bool script_client_do_string(const char *str)
81 int status = luascript_do_string(fcl, str, "cmd");
82 return (status == 0);
85 /*****************************************************************************
86 Parse and execute the script at filename.
87 *****************************************************************************/
88 bool script_client_do_file(const char *filename)
90 int status = luascript_do_file(fcl, filename);
91 return (status == 0);
94 /*****************************************************************************
95 Invoke the 'callback_name' Lua function.
96 *****************************************************************************/
97 bool script_client_callback_invoke(const char *callback_name, int nargs,
98 enum api_types *parg_types, va_list args)
100 return luascript_callback_invoke(fcl, callback_name, nargs, parg_types,
101 args);
104 /*****************************************************************************
105 Mark any, if exported, full userdata representing 'object' in
106 the current script state as 'Nonexistent'.
107 This changes the type of the lua variable.
108 *****************************************************************************/
109 void script_client_remove_exported_object(void *object)
111 luascript_remove_exported_object(fcl, object);
114 /*****************************************************************************
115 Initialize the game script variables.
116 *****************************************************************************/
117 static void script_client_vars_init(void)
119 /* nothing */
122 /*****************************************************************************
123 Free the game script variables.
124 *****************************************************************************/
125 static void script_client_vars_free(void)
127 /* nothing */
130 /*****************************************************************************
131 Load the game script variables in file.
132 *****************************************************************************/
133 static void script_client_vars_load(struct section_file *file)
135 luascript_vars_load(fcl, file, "script.vars");
138 /*****************************************************************************
139 Save the game script variables to file.
140 *****************************************************************************/
141 static void script_client_vars_save(struct section_file *file)
143 luascript_vars_save(fcl, file, "script.vars");
146 /*****************************************************************************
147 Initialize the optional game script code (useful for scenarios).
148 *****************************************************************************/
149 static void script_client_code_init(void)
151 script_client_code = NULL;
154 /*****************************************************************************
155 Free the optional game script code (useful for scenarios).
156 *****************************************************************************/
157 static void script_client_code_free(void)
159 if (script_client_code) {
160 free(script_client_code);
161 script_client_code = NULL;
165 /*****************************************************************************
166 Load the optional game script code from file (useful for scenarios).
167 *****************************************************************************/
168 static void script_client_code_load(struct section_file *file)
170 if (!script_client_code) {
171 const char *code;
172 const char *section = "script.code";
174 code = secfile_lookup_str_default(file, "", "%s", section);
175 script_client_code = fc_strdup(code);
176 luascript_do_string(fcl, script_client_code, section);
180 /*****************************************************************************
181 Save the optional game script code to file (useful for scenarios).
182 *****************************************************************************/
183 static void script_client_code_save(struct section_file *file)
185 if (script_client_code) {
186 secfile_insert_str_noescape(file, script_client_code, "script.code");
190 /*****************************************************************************
191 Initialize the scripting state.
192 *****************************************************************************/
193 bool script_client_init(void)
195 if (fcl != NULL) {
196 fc_assert_ret_val(fcl->state != NULL, FALSE);
198 return TRUE;
201 fcl = luascript_new(script_client_output);
202 if (!fcl) {
203 luascript_destroy(fcl);
204 fcl = NULL;
206 return FALSE;
209 tolua_common_a_open(fcl->state);
210 api_specenum_open(fcl->state);
211 tolua_game_open(fcl->state);
212 tolua_signal_open(fcl->state);
213 tolua_client_open(fcl->state);
214 tolua_common_z_open(fcl->state);
216 script_client_code_init();
217 script_client_vars_init();
219 luascript_signal_init(fcl);
220 script_client_signal_create();
222 return TRUE;
225 /*****************************************************************************
226 Ouput a message on the client lua console.
227 *****************************************************************************/
228 static void script_client_output(struct fc_lua *fcl, enum log_level level,
229 const char *format, ...)
231 va_list args;
232 struct ft_color ftc_luaconsole = ftc_luaconsole_error;
234 switch (level) {
235 case LOG_FATAL:
236 /* Special case - will quit the client. */
238 char buf[1024];
240 va_start(args, format);
241 fc_vsnprintf(buf, sizeof(buf), format, args);
242 va_end(args);
244 log_fatal("%s", buf);
246 break;
247 case LOG_ERROR:
248 ftc_luaconsole = ftc_luaconsole_error;
249 break;
250 case LOG_NORMAL:
251 ftc_luaconsole = ftc_luaconsole_normal;
252 break;
253 case LOG_VERBOSE:
254 ftc_luaconsole = ftc_luaconsole_verbose;
255 break;
256 case LOG_DEBUG:
257 ftc_luaconsole = ftc_luaconsole_debug;
258 break;
261 va_start(args, format);
262 luaconsole_vprintf(ftc_luaconsole, format, args);
263 va_end(args);
266 /*****************************************************************************
267 Free the scripting data.
268 *****************************************************************************/
269 void script_client_free(void)
271 if (fcl) {
272 script_client_code_free();
273 script_client_vars_free();
275 luascript_signal_free(fcl);
277 luascript_destroy(fcl);
278 fcl = NULL;
282 /*****************************************************************************
283 Load the scripting state from file.
284 *****************************************************************************/
285 void script_client_state_load(struct section_file *file)
287 script_client_code_load(file);
289 /* Variables must be loaded after code is loaded and executed,
290 * so we restore their saved state properly */
291 script_client_vars_load(file);
294 /*****************************************************************************
295 Save the scripting state to file.
296 *****************************************************************************/
297 void script_client_state_save(struct section_file *file)
299 script_client_code_save(file);
300 script_client_vars_save(file);
303 /*****************************************************************************
304 Invoke all the callback functions attached to a given signal.
305 *****************************************************************************/
306 void script_client_signal_emit(const char *signal_name, int nargs, ...)
308 va_list args;
310 va_start(args, nargs);
311 luascript_signal_emit_valist(fcl, signal_name, nargs, args);
312 va_end(args);
315 /*****************************************************************************
316 Declare any new signal types you need here.
317 *****************************************************************************/
318 static void script_client_signal_create(void)
320 luascript_signal_create(fcl, "new_tech", 0);