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
33
34/********************************************************************/
37int api_utilities_random(lua_State *L, int min, int max)
38{
39 double roll;
40
42
43 roll = ((double) (fc_rand(MAX_UINT32) % MAX_UINT32) / MAX_UINT32);
44
45 return (min + floor(roll * (max - min + 1)));
46}
47
48/********************************************************************/
51const char *api_utilities_fc_version(lua_State *L)
52{
53 return freeciv_name_version();
54}
55
56/********************************************************************/
59void api_utilities_log_base(lua_State *L, int level, const char *message)
60{
61 struct fc_lua *fcl;
62
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**************************************************************************/
77{
79
80 return (int) dir;
81}
82
83/**********************************************************************//***
84 Get direction name
85***************************************************************************/
86const 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/********************************************************************/
97const 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/********************************************************************/
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/********************************************************************/
124const 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/********************************************************************/
142{
143 LUASCRIPT_CHECK_STATE(L, NULL);
144
146}
147
148/********************************************************************/
151void api_utilities_deprecation_warning(lua_State *L, char *method,
152 char *replacement,
153 char *deprecated_since)
154{
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}
const Direction * api_utilities_dir_cw(lua_State *L, Direction dir)
const char * api_utilities_fc_version(lua_State *L)
const char * api_utilities_dir2str(lua_State *L, Direction dir)
int api_utilities_random(lua_State *L, int min, int max)
const Direction * api_utilities_opposite_dir(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)
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)
bool are_deprecation_warnings_enabled(void)
#define log_deprecation_always(message,...)
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:1740
enum direction8 dir_ccw(enum direction8 dir)
Definition map.c:1201
bool is_valid_dir(enum direction8 dir)
Definition map.c:1258
enum direction8 dir_cw(enum direction8 dir)
Definition map.c:1172
#define fc_rand(_size)
Definition rand.h:34
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:183
#define MAX_UINT32
Definition shared.h:81
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
const char * freeciv_name_version(void)
Definition version.c:35