Freeciv-3.1: /fast/freeciv/nightly/S3_1/common/scriptcore/api_common_utilities.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
api_common_utilities.c
Go to the documentation of this file.
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#include <math.h>
19
20/* utilities */
21#include "deprecations.h"
22#include "log.h"
23#include "rand.h"
24
25/* common */
26#include "map.h"
27#include "version.h"
28
29/* common/scriptcore */
30#include "luascript.h"
31
32#include "api_common_utilities.h"
33
34/********************************************************************/
37 int api_utilities_random(lua_State *L, int min, int max)
38{
39 double roll;
40
41 LUASCRIPT_CHECK_STATE(L, 0);
42
43 roll = ((double) (fc_rand(MAX_UINT32) % MAX_UINT32) / MAX_UINT32);
44
45 return (min + floor(roll * (max - min + 1)));
46}
47
48/********************************************************************/
51 const char *api_utilities_fc_version(lua_State *L)
52{
53 return freeciv_name_version();
54}
55
56/********************************************************************/
59 void api_utilities_log_base(lua_State *L, int level, const char *message)
60{
61 struct fc_lua *fcl;
62
63 LUASCRIPT_CHECK_STATE(L);
64 LUASCRIPT_CHECK_ARG_NIL(L, message, 3, string);
65
66 fcl = luascript_get_fcl(L);
67
68 LUASCRIPT_CHECK(L, fcl != NULL, "Undefined Freeciv lua state!");
69
70 luascript_log(fcl, level, "%s", message);
71}
72
73/*********************************************************************//***
74 Just return the direction as number
75**************************************************************************/
76 int api_utilities_direction_id(lua_State *L, Direction dir)
77{
78 LUASCRIPT_CHECK_STATE(L, 0);
79
80 return (int) dir;
81}
82
83/**********************************************************************//***
84 Get direction name
85***************************************************************************/
86 const char *api_utilities_dir2str(lua_State *L, Direction dir)
87{
88 LUASCRIPT_CHECK_STATE(L, NULL);
89 LUASCRIPT_CHECK(L, is_valid_dir(dir), "Direction is invalid", NULL);
90
91 return direction8_name(dir);
92}
93
94/********************************************************************/
97 const Direction *api_utilities_str2dir(lua_State *L, const char *dir)
98{
99 LUASCRIPT_CHECK_STATE(L, NULL);
100 LUASCRIPT_CHECK_ARG_NIL(L, dir, 2, string, NULL);
101
102 return luascript_dir(direction8_by_name(dir, fc_strcasecmp));
103}
104
105/********************************************************************/
108 const Direction *api_utilities_dir_ccw(lua_State *L, Direction dir)
109{
110 Direction new_dir = dir;
111
112 LUASCRIPT_CHECK_STATE(L, NULL);
113
114 do {
115 new_dir = dir_ccw(new_dir);
116 } while (!is_valid_dir(new_dir));
117
118 return luascript_dir(new_dir);
119}
120
121/********************************************************************/
124 const Direction *api_utilities_dir_cw(lua_State *L, Direction dir)
125{
126 Direction new_dir = dir;
127
128 LUASCRIPT_CHECK_STATE(L, NULL);
129
130 do {
131 new_dir = dir_cw(new_dir);
132 } while (!is_valid_dir(new_dir));
133
134 return luascript_dir(new_dir);
135}
136
137/********************************************************************/
141 const Direction *api_utilities_opposite_dir(lua_State *L, Direction dir)
142{
143 LUASCRIPT_CHECK_STATE(L, NULL);
144
145 return luascript_dir(opposite_direction(dir));
146}
147
148/********************************************************************/
151 void api_utilities_deprecation_warning(lua_State *L, char *method,
152 char *replacement,
153 char *deprecated_since)
154{
155 if (are_deprecation_warnings_enabled()) {
156 /* TODO: Keep track which deprecations we have already warned about, and do not keep spamming
157 * about them. */
158 if (deprecated_since != NULL && replacement != NULL) {
159 log_deprecation_always("Deprecated: lua construct \"%s\", deprecated since \"%s\", used. "
160 "Use \"%s\" instead", method, deprecated_since, replacement);
161 } else if (replacement != NULL) {
162 log_deprecation_always("Deprecated: lua construct \"%s\" used. "
163 "Use \"%s\" instead", method, replacement);
164 } else {
165 log_deprecation_always("Deprecated: lua construct \"%s\" used.",
166 method);
167 }
168 }
169}
api_utilities_dir_cw
const Direction * api_utilities_dir_cw(lua_State *L, Direction dir)
Definition api_common_utilities.c:124
api_utilities_fc_version
const char * api_utilities_fc_version(lua_State *L)
Definition api_common_utilities.c:51
api_utilities_dir2str
const char * api_utilities_dir2str(lua_State *L, Direction dir)
Definition api_common_utilities.c:86
api_utilities_random
int api_utilities_random(lua_State *L, int min, int max)
Definition api_common_utilities.c:37
api_utilities_opposite_dir
const Direction * api_utilities_opposite_dir(lua_State *L, Direction dir)
Definition api_common_utilities.c:141
api_utilities_log_base
void api_utilities_log_base(lua_State *L, int level, const char *message)
Definition api_common_utilities.c:59
api_utilities_str2dir
const Direction * api_utilities_str2dir(lua_State *L, const char *dir)
Definition api_common_utilities.c:97
api_utilities_deprecation_warning
void api_utilities_deprecation_warning(lua_State *L, char *method, char *replacement, char *deprecated_since)
Definition api_common_utilities.c:151
api_utilities_direction_id
int api_utilities_direction_id(lua_State *L, Direction dir)
Definition api_common_utilities.c:76
api_utilities_dir_ccw
const Direction * api_utilities_dir_ccw(lua_State *L, Direction dir)
Definition api_common_utilities.c:108
api_common_utilities.h
are_deprecation_warnings_enabled
bool are_deprecation_warnings_enabled(void)
Definition deprecations.c:43
deprecations.h
log_deprecation_always
#define log_deprecation_always(message,...)
Definition deprecations.h:51
log.h
luascript_dir
const Direction * luascript_dir(enum direction8 dir)
Definition luascript.c:789
luascript_log
void luascript_log(struct fc_lua *fcl, enum log_level level, const char *format,...)
Definition luascript.c:409
luascript_get_fcl
struct fc_lua * luascript_get_fcl(lua_State *L)
Definition luascript.c:366
luascript.h
LUASCRIPT_CHECK_STATE
#define LUASCRIPT_CHECK_STATE(L,...)
Definition luascript.h:117
LUASCRIPT_CHECK_ARG_NIL
#define LUASCRIPT_CHECK_ARG_NIL(L, value, narg, type,...)
Definition luascript.h:138
LUASCRIPT_CHECK
#define LUASCRIPT_CHECK(L, check, msg,...)
Definition luascript.h:124
Direction
enum direction8 Direction
Definition luascript_types.h:57
opposite_direction
enum direction8 opposite_direction(enum direction8 dir)
Definition map.c:1740
dir_ccw
enum direction8 dir_ccw(enum direction8 dir)
Definition map.c:1201
is_valid_dir
bool is_valid_dir(enum direction8 dir)
Definition map.c:1258
dir_cw
enum direction8 dir_cw(enum direction8 dir)
Definition map.c:1172
map.h
rand.h
fc_rand
#define fc_rand(_size)
Definition rand.h:34
level
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:183
MAX_UINT32
#define MAX_UINT32
Definition shared.h:81
fc_lua
Definition luascript.h:44
message
Definition messagewin_common.h:29
fc_strcasecmp
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
freeciv_name_version
const char * freeciv_name_version(void)
Definition version.c:35
version.h