Freeciv-3.2: /fast/freeciv/nightly/S3_2/common/scriptcore/api_common_utilities.c Source File

Freeciv-3.2

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/* dependencies/cvercmp */
21#include "cvercmp.h"
22
23/* utilities */
24#include "deprecations.h"
25#include "log.h"
26#include "rand.h"
27
28/* common */
29#include "map.h"
30#include "version.h"
31
32/* common/scriptcore */
33#include "luascript.h"
34
35#include "api_common_utilities.h"
36
37/********************************************************************/
40 int api_utilities_random(lua_State *L, int min, int max)
41{
42 double roll;
43
44 LUASCRIPT_CHECK_STATE(L, 0);
45
46 roll = ((double) (fc_rand(MAX_UINT32) % MAX_UINT32) / MAX_UINT32);
47
48 return (min + floor(roll * (max - min + 1)));
49}
50
51/********************************************************************/
55 const char *api_utilities_fc_version(lua_State *L)
56{
57 LUASCRIPT_CHECK_STATE(L, 0);
58
59 log_deprecation("Deprecated: lua construct \"fc_version\", "
60 "deprecated since \"3.2\", used. "
61 "Use \"name_version\" instead.");
62
63 return freeciv_name_version();
64}
65
66/********************************************************************/
69 const char *api_utilities_name_version(lua_State *L)
70{
71 LUASCRIPT_CHECK_STATE(L, 0);
72
73 return freeciv_name_version();
74}
75
76/********************************************************************/
79 const char *api_utilities_comparable_version(lua_State *L)
80{
81 LUASCRIPT_CHECK_STATE(L, 0);
82
83 return fc_comparable_version();
84}
85
86/********************************************************************/
89 const char *api_utilities_version_string(lua_State *L)
90{
91 LUASCRIPT_CHECK_STATE(L, 0);
92
93 return freeciv_datafile_version();
94}
95
96/********************************************************************/
100 int api_utilities_versions_compare(lua_State *L,
101 const char *ver1, const char *ver2)
102{
103 enum cvercmp_type result;
104
105 LUASCRIPT_CHECK_STATE(L, 0);
106 LUASCRIPT_CHECK_ARG_NIL(L, ver1, 2, string, 0);
107 LUASCRIPT_CHECK_ARG_NIL(L, ver2, 3, string, 0);
108
109 result = cvercmp_cmp(ver1, ver2);
110
111 switch (result) {
112 case CVERCMP_EQUAL:
113 return 0;
114 case CVERCMP_GREATER:
115 return 1;
116 case CVERCMP_LESSER:
117 return -1;
118 default:
119 fc_assert(result == CVERCMP_EQUAL
120 || result == CVERCMP_GREATER
121 || result == CVERCMP_LESSER);
122 return 0;
123 }
124}
125
126/********************************************************************/
129 void api_utilities_log_base(lua_State *L, int level, const char *message)
130{
131 struct fc_lua *fcl;
132
133 LUASCRIPT_CHECK_STATE(L);
134 LUASCRIPT_CHECK_ARG_NIL(L, message, 3, string);
135
136 fcl = luascript_get_fcl(L);
137
138 LUASCRIPT_CHECK(L, fcl != NULL, "Undefined Freeciv lua state!");
139
140 luascript_log(fcl, level, "%s", message);
141}
142
143/*********************************************************************//***
144 Just return the direction as number
145**************************************************************************/
146 int api_utilities_direction_id(lua_State *L, Direction dir)
147{
148 LUASCRIPT_CHECK_STATE(L, 0);
149
150 return (int) dir;
151}
152
153/**********************************************************************//***
154 Get direction name
155***************************************************************************/
156 const char *api_utilities_dir2str(lua_State *L, Direction dir)
157{
158 LUASCRIPT_CHECK_STATE(L, NULL);
159 LUASCRIPT_CHECK(L, is_valid_dir(dir), "Direction is invalid", NULL);
160
161 return direction8_name(dir);
162}
163
164/********************************************************************/
167 const Direction *api_utilities_str2dir(lua_State *L, const char *dir)
168{
169 LUASCRIPT_CHECK_STATE(L, NULL);
170 LUASCRIPT_CHECK_ARG_NIL(L, dir, 2, string, NULL);
171
172 return luascript_dir(direction8_by_name(dir, fc_strcasecmp));
173}
174
175/********************************************************************/
178 const Direction *api_utilities_dir_ccw(lua_State *L, Direction dir)
179{
180 Direction new_dir = dir;
181
182 LUASCRIPT_CHECK_STATE(L, NULL);
183
184 do {
185 new_dir = dir_ccw(new_dir);
186 } while (!is_valid_dir(new_dir));
187
188 return luascript_dir(new_dir);
189}
190
191/********************************************************************/
194 const Direction *api_utilities_dir_cw(lua_State *L, Direction dir)
195{
196 Direction new_dir = dir;
197
198 LUASCRIPT_CHECK_STATE(L, NULL);
199
200 do {
201 new_dir = dir_cw(new_dir);
202 } while (!is_valid_dir(new_dir));
203
204 return luascript_dir(new_dir);
205}
206
207/********************************************************************/
211 const Direction *api_utilities_opposite_dir(lua_State *L, Direction dir)
212{
213 LUASCRIPT_CHECK_STATE(L, NULL);
214
215 return luascript_dir(opposite_direction(dir));
216}
217
218/********************************************************************/
221 bool api_utilities_direction_is_cardinal(lua_State *L, Direction dir)
222{
223 LUASCRIPT_CHECK_STATE(L, FALSE);
224
225 return is_cardinal_dir(dir);
226}
227
228/********************************************************************/
231 void api_utilities_deprecation_warning(lua_State *L, char *method,
232 char *replacement,
233 char *deprecated_since)
234{
235 if (are_deprecation_warnings_enabled()) {
236 /* TODO: Keep track which deprecations we have already warned about, and do not keep spamming
237 * about them. */
238 if (deprecated_since != NULL && replacement != NULL) {
239 log_deprecation_always("Deprecated: lua construct \"%s\", deprecated since \"%s\", used. "
240 "Use \"%s\" instead", method, deprecated_since, replacement);
241 } else if (replacement != NULL) {
242 log_deprecation_always("Deprecated: lua construct \"%s\" used. "
243 "Use \"%s\" instead", method, replacement);
244 } else {
245 log_deprecation_always("Deprecated: lua construct \"%s\" used.",
246 method);
247 }
248 }
249}
api_utilities_dir_cw
const Direction * api_utilities_dir_cw(lua_State *L, Direction dir)
Definition api_common_utilities.c:194
api_utilities_fc_version
const char * api_utilities_fc_version(lua_State *L)
Definition api_common_utilities.c:55
api_utilities_versions_compare
int api_utilities_versions_compare(lua_State *L, const char *ver1, const char *ver2)
Definition api_common_utilities.c:100
api_utilities_dir2str
const char * api_utilities_dir2str(lua_State *L, Direction dir)
Definition api_common_utilities.c:156
api_utilities_comparable_version
const char * api_utilities_comparable_version(lua_State *L)
Definition api_common_utilities.c:79
api_utilities_random
int api_utilities_random(lua_State *L, int min, int max)
Definition api_common_utilities.c:40
api_utilities_opposite_dir
const Direction * api_utilities_opposite_dir(lua_State *L, Direction dir)
Definition api_common_utilities.c:211
api_utilities_direction_is_cardinal
bool api_utilities_direction_is_cardinal(lua_State *L, Direction dir)
Definition api_common_utilities.c:221
api_utilities_log_base
void api_utilities_log_base(lua_State *L, int level, const char *message)
Definition api_common_utilities.c:129
api_utilities_str2dir
const Direction * api_utilities_str2dir(lua_State *L, const char *dir)
Definition api_common_utilities.c:167
api_utilities_name_version
const char * api_utilities_name_version(lua_State *L)
Definition api_common_utilities.c:69
api_utilities_version_string
const char * api_utilities_version_string(lua_State *L)
Definition api_common_utilities.c:89
api_utilities_deprecation_warning
void api_utilities_deprecation_warning(lua_State *L, char *method, char *replacement, char *deprecated_since)
Definition api_common_utilities.c:231
api_utilities_direction_id
int api_utilities_direction_id(lua_State *L, Direction dir)
Definition api_common_utilities.c:146
api_utilities_dir_ccw
const Direction * api_utilities_dir_ccw(lua_State *L, Direction dir)
Definition api_common_utilities.c:178
api_common_utilities.h
incite_cost
char * incite_cost
Definition comments.c:74
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_deprecation
#define log_deprecation(message,...)
Definition deprecations.h:35
log.h
fc_assert
#define fc_assert(condition)
Definition log.h:176
luascript_dir
const Direction * luascript_dir(enum direction8 dir)
Definition luascript.c:790
luascript_log
void luascript_log(struct fc_lua *fcl, enum log_level level, const char *format,...)
Definition luascript.c:410
luascript_get_fcl
struct fc_lua * luascript_get_fcl(lua_State *L)
Definition luascript.c:367
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:59
opposite_direction
enum direction8 opposite_direction(enum direction8 dir)
Definition map.c:1937
dir_ccw
enum direction8 dir_ccw(enum direction8 dir)
Definition map.c:1203
is_cardinal_dir
bool is_cardinal_dir(enum direction8 dir)
Definition map.c:1316
is_valid_dir
bool is_valid_dir(enum direction8 dir)
Definition map.c:1260
dir_cw
enum direction8 dir_cw(enum direction8 dir)
Definition map.c:1174
map.h
rand.h
fc_rand
#define fc_rand(_size)
Definition rand.h:56
level
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
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
FALSE
#define FALSE
Definition support.h:47
fc_comparable_version
const char * fc_comparable_version(void)
Definition version.c:95
freeciv_name_version
const char * freeciv_name_version(void)
Definition version.c:35
freeciv_datafile_version
const char * freeciv_datafile_version(void)
Definition version.c:186
version.h