summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsfan5 <sfan5@live.de>2020-04-10 22:06:24 +0200
committerLoïc Blot <nerzhul@users.noreply.github.com>2020-04-11 13:12:51 +0200
commit054c5dfaa35dd79560a465ccc0ef214cb5f34c88 (patch)
tree7f9e2feefbee0708f3dc7371d3615767deba0048 /src
parentf105bc8dc2444d98a9cd74a2caa0013ce2e07008 (diff)
downloadminetest-054c5dfaa35dd79560a465ccc0ef214cb5f34c88.tar.gz
minetest-054c5dfaa35dd79560a465ccc0ef214cb5f34c88.tar.bz2
minetest-054c5dfaa35dd79560a465ccc0ef214cb5f34c88.zip
scriptapi: Sort out ServerEnvironment / Environment distinction properly
The API implementation is shared between CSM and SSM. Functions should retrieve a plain env when they do not need any server-specific functions.
Diffstat (limited to 'src')
-rw-r--r--src/environment.cpp18
-rw-r--r--src/environment.h10
-rw-r--r--src/script/lua_api/l_env.cpp50
-rw-r--r--src/script/lua_api/l_internal.h29
-rw-r--r--src/script/lua_api/l_object.cpp2
-rw-r--r--src/serverenvironment.cpp18
-rw-r--r--src/serverenvironment.h10
7 files changed, 72 insertions, 65 deletions
diff --git a/src/environment.cpp b/src/environment.cpp
index 906f35219..c997be3ff 100644
--- a/src/environment.cpp
+++ b/src/environment.cpp
@@ -83,6 +83,24 @@ float Environment::getTimeOfDayF()
return m_time_of_day_f;
}
+bool Environment::line_of_sight(v3f pos1, v3f pos2, v3s16 *p)
+{
+ // Iterate trough nodes on the line
+ voxalgo::VoxelLineIterator iterator(pos1 / BS, (pos2 - pos1) / BS);
+ do {
+ MapNode n = getMap().getNode(iterator.m_current_node_pos);
+
+ // Return non-air
+ if (n.param0 != CONTENT_AIR) {
+ if (p)
+ *p = iterator.m_current_node_pos;
+ return false;
+ }
+ iterator.next();
+ } while (iterator.m_current_index <= iterator.m_last_index);
+ return true;
+}
+
/*
Check if a node is pointable
*/
diff --git a/src/environment.h b/src/environment.h
index f568ba228..91c33ba15 100644
--- a/src/environment.h
+++ b/src/environment.h
@@ -77,6 +77,16 @@ public:
u32 getDayCount();
/*!
+ * Returns false if the given line intersects with a
+ * non-air node, true otherwise.
+ * \param pos1 start of the line
+ * \param pos2 end of the line
+ * \param p output, position of the first non-air node
+ * the line intersects
+ */
+ bool line_of_sight(v3f pos1, v3f pos2, v3s16 *p = nullptr);
+
+ /*!
* Gets the objects pointed by the shootline as
* pointed things.
* If this is a client environment, the local player
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp
index 3169fa4cf..31e582d3d 100644
--- a/src/script/lua_api/l_env.cpp
+++ b/src/script/lua_api/l_env.cpp
@@ -139,8 +139,7 @@ void LuaLBM::trigger(ServerEnvironment *env, v3s16 p, MapNode n)
int LuaRaycast::l_next(lua_State *L)
{
- MAP_LOCK_REQUIRED;
- GET_ENV_PTR;
+ GET_PLAIN_ENV_PTR;
bool csm = false;
#ifndef SERVER
@@ -384,7 +383,7 @@ int ModApiEnvMod::l_get_node_or_nil(lua_State *L)
// timeofday: nil = current time, 0 = night, 0.5 = day
int ModApiEnvMod::l_get_node_light(lua_State *L)
{
- GET_ENV_PTR;
+ GET_PLAIN_ENV_PTR;
// Do it
v3s16 pos = read_v3s16(L, 1);
@@ -488,10 +487,7 @@ int ModApiEnvMod::l_punch_node(lua_State *L)
// pos = {x=num, y=num, z=num}
int ModApiEnvMod::l_get_node_max_level(lua_State *L)
{
- Environment *env = getEnv(L);
- if (!env) {
- return 0;
- }
+ GET_PLAIN_ENV_PTR;
v3s16 pos = read_v3s16(L, 1);
MapNode n = env->getMap().getNode(pos);
@@ -503,10 +499,7 @@ int ModApiEnvMod::l_get_node_max_level(lua_State *L)
// pos = {x=num, y=num, z=num}
int ModApiEnvMod::l_get_node_level(lua_State *L)
{
- Environment *env = getEnv(L);
- if (!env) {
- return 0;
- }
+ GET_PLAIN_ENV_PTR;
v3s16 pos = read_v3s16(L, 1);
MapNode n = env->getMap().getNode(pos);
@@ -551,7 +544,7 @@ int ModApiEnvMod::l_add_node_level(lua_State *L)
// find_nodes_with_meta(pos1, pos2)
int ModApiEnvMod::l_find_nodes_with_meta(lua_State *L)
{
- GET_ENV_PTR;
+ GET_PLAIN_ENV_PTR;
std::vector<v3s16> positions = env->getMap().findNodesWithMetadata(
check_v3s16(L, 1), check_v3s16(L, 2));
@@ -728,10 +721,7 @@ int ModApiEnvMod::l_set_timeofday(lua_State *L)
// get_timeofday() -> 0...1
int ModApiEnvMod::l_get_timeofday(lua_State *L)
{
- Environment *env = getEnv(L);
- if (!env) {
- return 0;
- }
+ GET_PLAIN_ENV_PTR;
// Do it
int timeofday_mh = env->getTimeOfDay();
@@ -743,10 +733,7 @@ int ModApiEnvMod::l_get_timeofday(lua_State *L)
// get_day_count() -> int
int ModApiEnvMod::l_get_day_count(lua_State *L)
{
- Environment *env = getEnv(L);
- if (!env) {
- return 0;
- }
+ GET_PLAIN_ENV_PTR;
lua_pushnumber(L, env->getDayCount());
return 1;
@@ -767,12 +754,9 @@ int ModApiEnvMod::l_get_gametime(lua_State *L)
// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
int ModApiEnvMod::l_find_node_near(lua_State *L)
{
- Environment *env = getEnv(L);
- if (!env) {
- return 0;
- }
+ GET_PLAIN_ENV_PTR;
- const NodeDefManager *ndef = getGameDef(L)->ndef();
+ const NodeDefManager *ndef = env->getGameDef()->ndef();
v3s16 pos = read_v3s16(L, 1);
int radius = luaL_checkinteger(L, 2);
std::vector<content_t> filter;
@@ -815,20 +799,19 @@ int ModApiEnvMod::l_find_node_near(lua_State *L)
// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
{
- GET_ENV_PTR;
+ GET_PLAIN_ENV_PTR;
v3s16 minp = read_v3s16(L, 1);
v3s16 maxp = read_v3s16(L, 2);
sortBoxVerticies(minp, maxp);
+ const NodeDefManager *ndef = env->getGameDef()->ndef();
+
#ifndef SERVER
- const NodeDefManager *ndef = getClient(L) ? getClient(L)->ndef() : getServer(L)->ndef();
if (getClient(L)) {
minp = getClient(L)->CSMClampPos(minp);
maxp = getClient(L)->CSMClampPos(maxp);
}
-#else
- const NodeDefManager *ndef = getServer(L)->ndef();
#endif
v3s16 cube = maxp - minp + 1;
@@ -892,20 +875,19 @@ int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L)
* TODO
*/
- GET_ENV_PTR;
+ GET_PLAIN_ENV_PTR;
v3s16 minp = read_v3s16(L, 1);
v3s16 maxp = read_v3s16(L, 2);
sortBoxVerticies(minp, maxp);
+ const NodeDefManager *ndef = env->getGameDef()->ndef();
+
#ifndef SERVER
- const NodeDefManager *ndef = getClient(L) ? getClient(L)->ndef() : getServer(L)->ndef();
if (getClient(L)) {
minp = getClient(L)->CSMClampPos(minp);
maxp = getClient(L)->CSMClampPos(maxp);
}
-#else
- const NodeDefManager *ndef = getServer(L)->ndef();
#endif
v3s16 cube = maxp - minp + 1;
@@ -1034,7 +1016,7 @@ int ModApiEnvMod::l_clear_objects(lua_State *L)
// line_of_sight(pos1, pos2) -> true/false, pos
int ModApiEnvMod::l_line_of_sight(lua_State *L)
{
- GET_ENV_PTR;
+ GET_PLAIN_ENV_PTR;
// read position 1 from lua
v3f pos1 = checkFloatPos(L, 1);
diff --git a/src/script/lua_api/l_internal.h b/src/script/lua_api/l_internal.h
index bbedfe46e..a86eeaf79 100644
--- a/src/script/lua_api/l_internal.h
+++ b/src/script/lua_api/l_internal.h
@@ -32,14 +32,39 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define luamethod_aliased(class, name, alias) {#name, class::l_##name}, {#alias, class::l_##name}
#define API_FCT(name) registerFunction(L, #name, l_##name, top)
-#define MAP_LOCK_REQUIRED
-#define NO_MAP_LOCK_REQUIRED
+// For future use
+#define MAP_LOCK_REQUIRED ((void)0)
+#define NO_MAP_LOCK_REQUIRED ((void)0)
+/* In debug mode ensure no code tries to retrieve the server env when it isn't
+ * actually available (in CSM) */
+#if !defined(SERVER) && !defined(NDEBUG)
+#define DEBUG_ASSERT_NO_CLIENTAPI \
+ FATAL_ERROR_IF(getClient(L) != nullptr, "Tried " \
+ "to retrieve ServerEnvironment on client")
+#else
+#define DEBUG_ASSERT_NO_CLIENTAPI ((void)0)
+#endif
+
+// Retrieve ServerEnvironment pointer as `env` (no map lock)
#define GET_ENV_PTR_NO_MAP_LOCK \
+ DEBUG_ASSERT_NO_CLIENTAPI; \
ServerEnvironment *env = (ServerEnvironment *)getEnv(L); \
if (env == NULL) \
return 0
+// Retrieve ServerEnvironment pointer as `env`
#define GET_ENV_PTR \
MAP_LOCK_REQUIRED; \
GET_ENV_PTR_NO_MAP_LOCK
+
+// Retrieve Environment pointer as `env` (no map lock)
+#define GET_PLAIN_ENV_PTR_NO_MAP_LOCK \
+ Environment *env = (Environment *)getEnv(L); \
+ if (env == NULL) \
+ return 0
+
+// Retrieve Environment pointer as `env`
+#define GET_PLAIN_ENV_PTR \
+ MAP_LOCK_REQUIRED; \
+ GET_PLAIN_ENV_PTR_NO_MAP_LOCK
diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp
index 23ed1ffe0..5cd978d73 100644
--- a/src/script/lua_api/l_object.cpp
+++ b/src/script/lua_api/l_object.cpp
@@ -533,7 +533,7 @@ int ObjectRef::l_set_local_animation(lua_State *L)
// get_local_animation(self)
int ObjectRef::l_get_local_animation(lua_State *L)
{
- NO_MAP_LOCK_REQUIRED
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
RemotePlayer *player = getplayer(ref);
if (player == NULL)
diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp
index 2d3ee078e..0ccbd772b 100644
--- a/src/serverenvironment.cpp
+++ b/src/serverenvironment.cpp
@@ -545,24 +545,6 @@ bool ServerEnvironment::removePlayerFromDatabase(const std::string &name)
return m_player_database->removePlayer(name);
}
-bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, v3s16 *p)
-{
- // Iterate trough nodes on the line
- voxalgo::VoxelLineIterator iterator(pos1 / BS, (pos2 - pos1) / BS);
- do {
- MapNode n = getMap().getNode(iterator.m_current_node_pos);
-
- // Return non-air
- if (n.param0 != CONTENT_AIR) {
- if (p)
- *p = iterator.m_current_node_pos;
- return false;
- }
- iterator.next();
- } while (iterator.m_current_index <= iterator.m_last_index);
- return true;
-}
-
void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason,
const std::string &str_reason, bool reconnect)
{
diff --git a/src/serverenvironment.h b/src/serverenvironment.h
index 3c7b7d059..bf10e3ca8 100644
--- a/src/serverenvironment.h
+++ b/src/serverenvironment.h
@@ -334,16 +334,6 @@ public:
// This makes stuff happen
void step(f32 dtime);
- /*!
- * Returns false if the given line intersects with a
- * non-air node, true otherwise.
- * \param pos1 start of the line
- * \param pos2 end of the line
- * \param p output, position of the first non-air node
- * the line intersects
- */
- bool line_of_sight(v3f pos1, v3f pos2, v3s16 *p = NULL);
-
u32 getGameTime() const { return m_game_time; }
void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }