Freeciv21
Develop your civilization from humble roots to a global empire
api_common_utilities.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file
3  is part of Freeciv21. Freeciv21 is free software:
4 |\_/|,,_____,~~` you can redistribute it and/or modify it under the
5 (.".)~~ )`~}} terms of the GNU General Public License as published
6  \o/\ /---~\\ ~}} by the Free Software Foundation, either version 3 of
7  _// _// ~} the License, or (at your option) any later version.
8  You should have received a copy of the GNU General
9  Public License along with Freeciv21. If not, see
10  https://www.gnu.org/licenses/.
11  */
12 
13 #include <cmath>
14 
15 // Sol
16 #include "sol/sol.hpp"
17 
18 // utilities
19 #include "deprecations.h"
20 #include "fcintl.h"
21 #include "rand.h"
22 
23 // common
24 #include "map.h"
25 #include "version.h"
26 
27 /* common/scriptcore */
28 #include "luascript.h"
29 
30 #include "api_common_utilities.h"
31 
35 int api_utilities_random(int min, int max)
36 {
37  double roll;
38 
39  roll =
40  (static_cast<double>(fc_rand(MAX_UINT32) % MAX_UINT32) / MAX_UINT32);
41 
42  return (min + floor(roll * (max - min + 1)));
43 }
44 
49 
53 void api_utilities_log_base(sol::this_state s, int level,
54  const char *message)
55 {
56  auto fcl = luascript_get_fcl(s);
57 
58  LUASCRIPT_CHECK(s, fcl != nullptr, "Undefined Freeciv21 lua state!");
59 
60  luascript_log(fcl, QtMsgType(level), "%s", message);
61 }
62 
66 const Direction *api_utilities_str2dir(lua_State *L, const char *dir)
67 {
68  LUASCRIPT_CHECK_STATE(L, nullptr);
69  LUASCRIPT_CHECK_ARG_NIL(L, dir, 2, string, nullptr);
70 
71  return luascript_dir(direction8_by_name(dir, fc_strcasecmp));
72 }
73 
77 const Direction *api_utilities_dir_ccw(lua_State *L, Direction dir)
78 {
79  Direction new_dir = dir;
80 
81  LUASCRIPT_CHECK_STATE(L, nullptr);
82 
83  do {
84  new_dir = dir_ccw(new_dir);
85  } while (!is_valid_dir(new_dir));
86 
87  return luascript_dir(new_dir);
88 }
89 
93 const Direction *api_utilities_dir_cw(lua_State *L, Direction dir)
94 {
95  Direction new_dir = dir;
96 
97  LUASCRIPT_CHECK_STATE(L, nullptr);
98 
99  do {
100  new_dir = dir_cw(new_dir);
101  } while (!is_valid_dir(new_dir));
102 
103  return luascript_dir(new_dir);
104 }
105 
111 {
112  LUASCRIPT_CHECK_STATE(L, nullptr);
113 
114  return luascript_dir(opposite_direction(dir));
115 }
116 
120 void api_utilities_deprecation_warning(char *method, char *replacement,
121  char *deprecated_since)
122 {
123  /* TODO: Keep track which deprecations we have already warned about, and
124  * do not keep spamming about them. */
125  if (deprecated_since != nullptr && replacement != nullptr) {
126  qCWarning(
127  deprecations_category,
128  "Deprecated: lua construct \"%s\", deprecated since \"%s\", used. "
129  "Use \"%s\" instead",
130  method, deprecated_since, replacement);
131  } else if (replacement != nullptr) {
132  qCWarning(deprecations_category,
133  "Deprecated: lua construct \"%s\" used. "
134  "Use \"%s\" instead",
135  method, replacement);
136  } else {
137  qCWarning(deprecations_category,
138  "Deprecated: lua construct \"%s\" used.", method);
139  }
140 }
const Direction * api_utilities_dir_cw(lua_State *L, Direction dir)
Next (clockwise) valid direction.
void api_utilities_log_base(sol::this_state s, int level, const char *message)
One log message.
int api_utilities_random(int min, int max)
Generate random number.
const Direction * api_utilities_opposite_dir(lua_State *L, Direction dir)
Opposite direction - validity not checked, but it's valid iff original direction is.
const Direction * api_utilities_str2dir(lua_State *L, const char *dir)
Convert text describing direction into direction.
const char * api_utilities_fc_version()
Return the version of freeciv lua script.
void api_utilities_deprecation_warning(char *method, char *replacement, char *deprecated_since)
Lua script wants to warn about use of deprecated construct.
const Direction * api_utilities_dir_ccw(lua_State *L, Direction dir)
Previous (counter-clockwise) valid direction.
const Direction * luascript_dir(enum direction8 dir)
Returns a pointer to a given value of enum direction8 (always the same address for the same value),...
Definition: luascript.cpp:789
void luascript_log(struct fc_lua *fcl, QtMsgType level, const char *format,...)
Print a message to the selected output handle.
Definition: luascript.cpp:428
struct fc_lua * luascript_get_fcl(lua_State *L)
Get the freeciv lua struct from a lua state.
Definition: luascript.cpp:315
#define LUASCRIPT_CHECK_STATE(L,...)
Definition: luascript.h:110
#define LUASCRIPT_CHECK_ARG_NIL(L, value, narg, type,...)
Definition: luascript.h:131
#define LUASCRIPT_CHECK(L, check, msg,...)
Definition: luascript.h:117
enum direction8 Direction
enum direction8 opposite_direction(enum direction8 dir)
Return direction that is opposite to given one.
Definition: map.cpp:1589
enum direction8 dir_ccw(enum direction8 dir)
Returns the next direction counter-clock-wise.
Definition: map.cpp:1141
bool is_valid_dir(enum direction8 dir)
Returns TRUE iff the given direction is a valid one.
Definition: map.cpp:1199
enum direction8 dir_cw(enum direction8 dir)
Returns the next direction clock-wise.
Definition: map.cpp:1112
#define fc_rand(_size)
Definition: rand.h:16
static void static sol::state * fcl
Lua virtual machine state.
Definition: script_fcdb.cpp:48
struct setting_list * level[OLEVELS_NUM]
Definition: settings.cpp:167
#define MAX_UINT32
Definition: shared.h:73
int fc_strcasecmp(const char *str0, const char *str1)
Compare strings like strcmp(), but ignoring case.
Definition: support.cpp:89
const char * freeciv_name_version()
Return string containing both name of Freeciv21 and version.
Definition: version.cpp:34