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
36
37/********************************************************************/
40int api_utilities_random(lua_State *L, int min, int max)
41{
42 double roll;
43
45
47
48 return (min + floor(roll * (max - min + 1)));
49}
50
51/********************************************************************/
56{
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/********************************************************************/
75
76/********************************************************************/
85
86/********************************************************************/
95
96/********************************************************************/
101 const char *ver1, const char *ver2)
102{
103 enum cvercmp_type result;
104
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/********************************************************************/
130{
131 struct fc_lua *fcl;
132
134 LUASCRIPT_CHECK_ARG_NIL(L, message, 3, string);
135
137
138 LUASCRIPT_CHECK(L, fcl != NULL, "Undefined Freeciv lua state!");
139
141}
142
143/*********************************************************************//***
144 Just return the direction as number
145**************************************************************************/
147{
149
150 return (int) dir;
151}
152
153/**********************************************************************//***
154 Get direction name
155***************************************************************************/
157{
159 LUASCRIPT_CHECK(L, is_valid_dir(dir), "Direction is invalid", NULL);
160
161 return direction8_name(dir);
162}
163
164/********************************************************************/
168{
170 LUASCRIPT_CHECK_ARG_NIL(L, dir, 2, string, NULL);
171
173}
174
175/********************************************************************/
179{
180 Direction new_dir = dir;
181
183
184 do {
186 } while (!is_valid_dir(new_dir));
187
188 return luascript_dir(new_dir);
189}
190
191/********************************************************************/
195{
196 Direction new_dir = dir;
197
199
200 do {
202 } while (!is_valid_dir(new_dir));
203
204 return luascript_dir(new_dir);
205}
206
207/********************************************************************/
217
218/********************************************************************/
227
228/********************************************************************/
232 char *replacement,
233 char *deprecated_since)
234{
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}
const Direction * api_utilities_dir_cw(lua_State *L, Direction dir)
const char * api_utilities_fc_version(lua_State *L)
int api_utilities_versions_compare(lua_State *L, const char *ver1, const char *ver2)
const char * api_utilities_dir2str(lua_State *L, Direction dir)
const char * api_utilities_comparable_version(lua_State *L)
int api_utilities_random(lua_State *L, int min, int max)
const Direction * api_utilities_opposite_dir(lua_State *L, Direction dir)
bool api_utilities_direction_is_cardinal(lua_State *L, Direction dir)
void api_utilities_log_base(lua_State *L, int level, const char *message)
const Direction * api_utilities_str2dir(lua_State *L, const char *dir)
const char * api_utilities_name_version(lua_State *L)
const char * api_utilities_version_string(lua_State *L)
void api_utilities_deprecation_warning(lua_State *L, char *method, char *replacement, char *deprecated_since)
int api_utilities_direction_id(lua_State *L, Direction dir)
const Direction * api_utilities_dir_ccw(lua_State *L, Direction dir)
char * incite_cost
Definition comments.c:75
bool are_deprecation_warnings_enabled(void)
#define log_deprecation_always(message,...)
#define log_deprecation(message,...)
#define fc_assert(condition)
Definition log.h:176
const Direction * luascript_dir(enum direction8 dir)
Definition luascript.c:789
void luascript_log(struct fc_lua *fcl, enum log_level level, const char *format,...)
Definition luascript.c:409
struct fc_lua * luascript_get_fcl(lua_State *L)
Definition luascript.c:366
#define LUASCRIPT_CHECK_STATE(L,...)
Definition luascript.h:117
#define LUASCRIPT_CHECK_ARG_NIL(L, value, narg, type,...)
Definition luascript.h:138
#define LUASCRIPT_CHECK(L, check, msg,...)
Definition luascript.h:124
enum direction8 Direction
enum direction8 opposite_direction(enum direction8 dir)
Definition map.c:1937
enum direction8 dir_ccw(enum direction8 dir)
Definition map.c:1203
bool is_cardinal_dir(enum direction8 dir)
Definition map.c:1316
bool is_valid_dir(enum direction8 dir)
Definition map.c:1260
enum direction8 dir_cw(enum direction8 dir)
Definition map.c:1174
#define fc_rand(_size)
Definition rand.h:56
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
#define MAX_UINT32
Definition shared.h:81
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
#define FALSE
Definition support.h:47
const char * fc_comparable_version(void)
Definition version.c:95
const char * freeciv_name_version(void)
Definition version.c:35
const char * freeciv_datafile_version(void)
Definition version.c:186