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.
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 /* server */
22 #include "score.h"
23 #include "settings.h"
24 #include "srv_main.h"
25
26 /* server/scripting */
27 #include "script_server.h"
28
29 #include "api_server_base.h"
30
31 /*****************************************************************************
32 Return the civilization score (total) for player
33 *****************************************************************************/
api_server_player_civilization_score(lua_State * L,Player * pplayer)34 int api_server_player_civilization_score(lua_State *L, Player *pplayer)
35 {
36 LUASCRIPT_CHECK_STATE(L, 0);
37 LUASCRIPT_CHECK_SELF(L, pplayer, 0);
38
39 return get_civ_score(pplayer);
40 }
41
42 /*****************************************************************************
43 Returns TRUE if the game was started.
44 *****************************************************************************/
api_server_was_started(lua_State * L)45 bool api_server_was_started(lua_State *L)
46 {
47 LUASCRIPT_CHECK_STATE(L, FALSE);
48
49 return game_was_started();
50 }
51
52 /*****************************************************************************
53 Save the game (a manual save is triggered).
54 *****************************************************************************/
api_server_save(lua_State * L,const char * filename)55 bool api_server_save(lua_State *L, const char *filename)
56 {
57 LUASCRIPT_CHECK_STATE(L, FALSE);
58
59 /* Limit the allowed characters in the filename. */
60 if (filename != NULL && !is_safe_filename(filename)) {
61 return FALSE;
62 }
63
64 save_game(filename, "User request (Lua)", FALSE);
65 return TRUE;
66 }
67
68 /*****************************************************************************
69 Play music track for player
70 *****************************************************************************/
api_play_music(lua_State * L,Player * pplayer,const char * tag)71 bool api_play_music(lua_State *L, Player *pplayer, const char *tag)
72 {
73 struct packet_play_music p;
74
75 LUASCRIPT_CHECK_STATE(L, FALSE);
76 LUASCRIPT_CHECK_SELF(L, pplayer, FALSE);
77 LUASCRIPT_CHECK_ARG_NIL(L, tag, 3, API_TYPE_STRING, FALSE);
78
79 sz_strlcpy(p.tag, tag);
80
81 lsend_packet_play_music(pplayer->connections, &p);
82
83 return TRUE;
84 }
85
86 /*****************************************************************************
87 Return the formated value of the setting or NULL if no such setting exists,
88 *****************************************************************************/
api_server_setting_get(lua_State * L,const char * sett_name)89 const char *api_server_setting_get(lua_State *L, const char *sett_name)
90 {
91 struct setting *pset;
92 static char buf[512];
93
94 LUASCRIPT_CHECK_STATE(L, NULL);
95 LUASCRIPT_CHECK_ARG_NIL(L, sett_name, 2, API_TYPE_STRING, NULL);
96
97 pset = setting_by_name(sett_name);
98
99 if (!pset) {
100 return NULL;
101 }
102
103 return setting_value_name(pset, FALSE, buf, sizeof(buf));
104 }
105