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)
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 *****************************************************************************/
15 #include <fc_config.h>
22 /* dependencies/lua */
26 /* dependencies/tolua */
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"
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");
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
);
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
,
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)
122 /*****************************************************************************
123 Free the game script variables.
124 *****************************************************************************/
125 static void script_client_vars_free(void)
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
) {
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)
196 fc_assert_ret_val(fcl
->state
!= NULL
, FALSE
);
201 fcl
= luascript_new(script_client_output
);
203 luascript_destroy(fcl
);
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();
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
, ...)
232 struct ft_color ftc_luaconsole
= ftc_luaconsole_error
;
236 /* Special case - will quit the client. */
240 va_start(args
, format
);
241 fc_vsnprintf(buf
, sizeof(buf
), format
, args
);
244 log_fatal("%s", buf
);
248 ftc_luaconsole
= ftc_luaconsole_error
;
251 ftc_luaconsole
= ftc_luaconsole_normal
;
254 ftc_luaconsole
= ftc_luaconsole_verbose
;
257 ftc_luaconsole
= ftc_luaconsole_debug
;
261 va_start(args
, format
);
262 luaconsole_vprintf(ftc_luaconsole
, format
, args
);
266 /*****************************************************************************
267 Free the scripting data.
268 *****************************************************************************/
269 void script_client_free(void)
272 script_client_code_free();
273 script_client_vars_free();
275 luascript_signal_free(fcl
);
277 luascript_destroy(fcl
);
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
, ...)
310 va_start(args
, nargs
);
311 luascript_signal_emit_valist(fcl
, signal_name
, nargs
, 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);