File: api_server_base.c

package info (click to toggle)
freeciv 3.2.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 286,492 kB
  • sloc: ansic: 484,452; cpp: 37,766; sh: 10,374; makefile: 7,425; python: 2,938; xml: 652; sed: 11
file content (127 lines) | stat: -rw-r--r-- 3,823 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*****************************************************************************
 Freeciv - Copyright (C) 2005 - The Freeciv Project
   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"

/* server */
#include "score.h"
#include "settings.h"
#include "srv_main.h"

/* server/sqavegame */
#include "savemain.h"

/* server/scripting */
#include "script_server.h"

#include "api_server_base.h"

/**********************************************************************//**
  Return the civilization score (total) for player
**************************************************************************/
int api_server_player_civilization_score(lua_State *L, Player *pplayer)
{
  LUASCRIPT_CHECK_STATE(L, 0);
  LUASCRIPT_CHECK_SELF(L, pplayer, 0);

  return get_civ_score(pplayer);
}

/**********************************************************************//**
  Returns TRUE if the game was started.
**************************************************************************/
bool api_server_was_started(lua_State *L)
{
  LUASCRIPT_CHECK_STATE(L, FALSE);

  return game_was_started();
}

/**********************************************************************//**
  Save the game (a manual save is triggered).
**************************************************************************/
bool api_server_save(lua_State *L, const char *filename)
{
  LUASCRIPT_CHECK_STATE(L, FALSE);

  /* Limit the allowed characters in the filename. */
  if (filename != NULL && !is_safe_filename(filename)) {
    return FALSE;
  }

  save_game(filename, "User request (Lua)", FALSE);

  return TRUE;
}

/**********************************************************************//**
  Play music track for player
**************************************************************************/
bool api_play_music(lua_State *L, Player *pplayer, const char *tag)
{
  struct packet_play_music p;

  LUASCRIPT_CHECK_STATE(L, FALSE);
  LUASCRIPT_CHECK_SELF(L, pplayer, FALSE);
  LUASCRIPT_CHECK_ARG_NIL(L, tag, 3, API_TYPE_STRING, FALSE);

  sz_strlcpy(p.tag, tag);

  lsend_packet_play_music(pplayer->connections, &p);

  return TRUE;
}

/**********************************************************************//**
  Popup image to player
**************************************************************************/
bool api_popup_image(lua_State *L, Player *pplayer, const char *tag)
{
  struct packet_popup_image p;

  LUASCRIPT_CHECK_STATE(L, FALSE);
  LUASCRIPT_CHECK_SELF(L, pplayer, FALSE);
  LUASCRIPT_CHECK_ARG_NIL(L, tag, 3, API_TYPE_STRING, FALSE);

  sz_strlcpy(p.tag, tag);

  lsend_packet_popup_image(pplayer->connections, &p);

  return TRUE;
}

/**********************************************************************//**
  Return the formatted value of the setting or NULL if no such setting
  exists.
**************************************************************************/
const char *api_server_setting_get(lua_State *L, const char *sett_name)
{
  struct setting *pset;
  static char buf[512];

  LUASCRIPT_CHECK_STATE(L, NULL);
  LUASCRIPT_CHECK_ARG_NIL(L, sett_name, 2, API_TYPE_STRING, NULL);

  pset = setting_by_name(sett_name);

  if (!pset) {
    return NULL;
  }

  return setting_value_name(pset, FALSE, buf, sizeof(buf));
}