Skip to content

Commit 054c5df

Browse files
sfan5nerzhul
authored andcommittedApr 11, 2020
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.
1 parent f105bc8 commit 054c5df

File tree

7 files changed

+72
-65
lines changed

7 files changed

+72
-65
lines changed
 

‎src/environment.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ float Environment::getTimeOfDayF()
8383
return m_time_of_day_f;
8484
}
8585

86+
bool Environment::line_of_sight(v3f pos1, v3f pos2, v3s16 *p)
87+
{
88+
// Iterate trough nodes on the line
89+
voxalgo::VoxelLineIterator iterator(pos1 / BS, (pos2 - pos1) / BS);
90+
do {
91+
MapNode n = getMap().getNode(iterator.m_current_node_pos);
92+
93+
// Return non-air
94+
if (n.param0 != CONTENT_AIR) {
95+
if (p)
96+
*p = iterator.m_current_node_pos;
97+
return false;
98+
}
99+
iterator.next();
100+
} while (iterator.m_current_index <= iterator.m_last_index);
101+
return true;
102+
}
103+
86104
/*
87105
Check if a node is pointable
88106
*/

‎src/environment.h

+10
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ class Environment
7676

7777
u32 getDayCount();
7878

79+
/*!
80+
* Returns false if the given line intersects with a
81+
* non-air node, true otherwise.
82+
* \param pos1 start of the line
83+
* \param pos2 end of the line
84+
* \param p output, position of the first non-air node
85+
* the line intersects
86+
*/
87+
bool line_of_sight(v3f pos1, v3f pos2, v3s16 *p = nullptr);
88+
7989
/*!
8090
* Gets the objects pointed by the shootline as
8191
* pointed things.

‎src/script/lua_api/l_env.cpp

+16-34
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ void LuaLBM::trigger(ServerEnvironment *env, v3s16 p, MapNode n)
139139

140140
int LuaRaycast::l_next(lua_State *L)
141141
{
142-
MAP_LOCK_REQUIRED;
143-
GET_ENV_PTR;
142+
GET_PLAIN_ENV_PTR;
144143

145144
bool csm = false;
146145
#ifndef SERVER
@@ -384,7 +383,7 @@ int ModApiEnvMod::l_get_node_or_nil(lua_State *L)
384383
// timeofday: nil = current time, 0 = night, 0.5 = day
385384
int ModApiEnvMod::l_get_node_light(lua_State *L)
386385
{
387-
GET_ENV_PTR;
386+
GET_PLAIN_ENV_PTR;
388387

389388
// Do it
390389
v3s16 pos = read_v3s16(L, 1);
@@ -488,10 +487,7 @@ int ModApiEnvMod::l_punch_node(lua_State *L)
488487
// pos = {x=num, y=num, z=num}
489488
int ModApiEnvMod::l_get_node_max_level(lua_State *L)
490489
{
491-
Environment *env = getEnv(L);
492-
if (!env) {
493-
return 0;
494-
}
490+
GET_PLAIN_ENV_PTR;
495491

496492
v3s16 pos = read_v3s16(L, 1);
497493
MapNode n = env->getMap().getNode(pos);
@@ -503,10 +499,7 @@ int ModApiEnvMod::l_get_node_max_level(lua_State *L)
503499
// pos = {x=num, y=num, z=num}
504500
int ModApiEnvMod::l_get_node_level(lua_State *L)
505501
{
506-
Environment *env = getEnv(L);
507-
if (!env) {
508-
return 0;
509-
}
502+
GET_PLAIN_ENV_PTR;
510503

511504
v3s16 pos = read_v3s16(L, 1);
512505
MapNode n = env->getMap().getNode(pos);
@@ -551,7 +544,7 @@ int ModApiEnvMod::l_add_node_level(lua_State *L)
551544
// find_nodes_with_meta(pos1, pos2)
552545
int ModApiEnvMod::l_find_nodes_with_meta(lua_State *L)
553546
{
554-
GET_ENV_PTR;
547+
GET_PLAIN_ENV_PTR;
555548

556549
std::vector<v3s16> positions = env->getMap().findNodesWithMetadata(
557550
check_v3s16(L, 1), check_v3s16(L, 2));
@@ -728,10 +721,7 @@ int ModApiEnvMod::l_set_timeofday(lua_State *L)
728721
// get_timeofday() -> 0...1
729722
int ModApiEnvMod::l_get_timeofday(lua_State *L)
730723
{
731-
Environment *env = getEnv(L);
732-
if (!env) {
733-
return 0;
734-
}
724+
GET_PLAIN_ENV_PTR;
735725

736726
// Do it
737727
int timeofday_mh = env->getTimeOfDay();
@@ -743,10 +733,7 @@ int ModApiEnvMod::l_get_timeofday(lua_State *L)
743733
// get_day_count() -> int
744734
int ModApiEnvMod::l_get_day_count(lua_State *L)
745735
{
746-
Environment *env = getEnv(L);
747-
if (!env) {
748-
return 0;
749-
}
736+
GET_PLAIN_ENV_PTR;
750737

751738
lua_pushnumber(L, env->getDayCount());
752739
return 1;
@@ -767,12 +754,9 @@ int ModApiEnvMod::l_get_gametime(lua_State *L)
767754
// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
768755
int ModApiEnvMod::l_find_node_near(lua_State *L)
769756
{
770-
Environment *env = getEnv(L);
771-
if (!env) {
772-
return 0;
773-
}
757+
GET_PLAIN_ENV_PTR;
774758

775-
const NodeDefManager *ndef = getGameDef(L)->ndef();
759+
const NodeDefManager *ndef = env->getGameDef()->ndef();
776760
v3s16 pos = read_v3s16(L, 1);
777761
int radius = luaL_checkinteger(L, 2);
778762
std::vector<content_t> filter;
@@ -815,20 +799,19 @@ int ModApiEnvMod::l_find_node_near(lua_State *L)
815799
// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
816800
int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
817801
{
818-
GET_ENV_PTR;
802+
GET_PLAIN_ENV_PTR;
819803

820804
v3s16 minp = read_v3s16(L, 1);
821805
v3s16 maxp = read_v3s16(L, 2);
822806
sortBoxVerticies(minp, maxp);
823807

808+
const NodeDefManager *ndef = env->getGameDef()->ndef();
809+
824810
#ifndef SERVER
825-
const NodeDefManager *ndef = getClient(L) ? getClient(L)->ndef() : getServer(L)->ndef();
826811
if (getClient(L)) {
827812
minp = getClient(L)->CSMClampPos(minp);
828813
maxp = getClient(L)->CSMClampPos(maxp);
829814
}
830-
#else
831-
const NodeDefManager *ndef = getServer(L)->ndef();
832815
#endif
833816

834817
v3s16 cube = maxp - minp + 1;
@@ -892,20 +875,19 @@ int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L)
892875
* TODO
893876
*/
894877

895-
GET_ENV_PTR;
878+
GET_PLAIN_ENV_PTR;
896879

897880
v3s16 minp = read_v3s16(L, 1);
898881
v3s16 maxp = read_v3s16(L, 2);
899882
sortBoxVerticies(minp, maxp);
900883

884+
const NodeDefManager *ndef = env->getGameDef()->ndef();
885+
901886
#ifndef SERVER
902-
const NodeDefManager *ndef = getClient(L) ? getClient(L)->ndef() : getServer(L)->ndef();
903887
if (getClient(L)) {
904888
minp = getClient(L)->CSMClampPos(minp);
905889
maxp = getClient(L)->CSMClampPos(maxp);
906890
}
907-
#else
908-
const NodeDefManager *ndef = getServer(L)->ndef();
909891
#endif
910892

911893
v3s16 cube = maxp - minp + 1;
@@ -1034,7 +1016,7 @@ int ModApiEnvMod::l_clear_objects(lua_State *L)
10341016
// line_of_sight(pos1, pos2) -> true/false, pos
10351017
int ModApiEnvMod::l_line_of_sight(lua_State *L)
10361018
{
1037-
GET_ENV_PTR;
1019+
GET_PLAIN_ENV_PTR;
10381020

10391021
// read position 1 from lua
10401022
v3f pos1 = checkFloatPos(L, 1);

‎src/script/lua_api/l_internal.h

+27-2
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,39 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3232
#define luamethod_aliased(class, name, alias) {#name, class::l_##name}, {#alias, class::l_##name}
3333
#define API_FCT(name) registerFunction(L, #name, l_##name, top)
3434

35-
#define MAP_LOCK_REQUIRED
36-
#define NO_MAP_LOCK_REQUIRED
35+
// For future use
36+
#define MAP_LOCK_REQUIRED ((void)0)
37+
#define NO_MAP_LOCK_REQUIRED ((void)0)
3738

39+
/* In debug mode ensure no code tries to retrieve the server env when it isn't
40+
* actually available (in CSM) */
41+
#if !defined(SERVER) && !defined(NDEBUG)
42+
#define DEBUG_ASSERT_NO_CLIENTAPI \
43+
FATAL_ERROR_IF(getClient(L) != nullptr, "Tried " \
44+
"to retrieve ServerEnvironment on client")
45+
#else
46+
#define DEBUG_ASSERT_NO_CLIENTAPI ((void)0)
47+
#endif
48+
49+
// Retrieve ServerEnvironment pointer as `env` (no map lock)
3850
#define GET_ENV_PTR_NO_MAP_LOCK \
51+
DEBUG_ASSERT_NO_CLIENTAPI; \
3952
ServerEnvironment *env = (ServerEnvironment *)getEnv(L); \
4053
if (env == NULL) \
4154
return 0
4255

56+
// Retrieve ServerEnvironment pointer as `env`
4357
#define GET_ENV_PTR \
4458
MAP_LOCK_REQUIRED; \
4559
GET_ENV_PTR_NO_MAP_LOCK
60+
61+
// Retrieve Environment pointer as `env` (no map lock)
62+
#define GET_PLAIN_ENV_PTR_NO_MAP_LOCK \
63+
Environment *env = (Environment *)getEnv(L); \
64+
if (env == NULL) \
65+
return 0
66+
67+
// Retrieve Environment pointer as `env`
68+
#define GET_PLAIN_ENV_PTR \
69+
MAP_LOCK_REQUIRED; \
70+
GET_PLAIN_ENV_PTR_NO_MAP_LOCK

‎src/script/lua_api/l_object.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ int ObjectRef::l_set_local_animation(lua_State *L)
533533
// get_local_animation(self)
534534
int ObjectRef::l_get_local_animation(lua_State *L)
535535
{
536-
NO_MAP_LOCK_REQUIRED
536+
NO_MAP_LOCK_REQUIRED;
537537
ObjectRef *ref = checkobject(L, 1);
538538
RemotePlayer *player = getplayer(ref);
539539
if (player == NULL)

‎src/serverenvironment.cpp

-18
Original file line numberDiff line numberDiff line change
@@ -545,24 +545,6 @@ bool ServerEnvironment::removePlayerFromDatabase(const std::string &name)
545545
return m_player_database->removePlayer(name);
546546
}
547547

548-
bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, v3s16 *p)
549-
{
550-
// Iterate trough nodes on the line
551-
voxalgo::VoxelLineIterator iterator(pos1 / BS, (pos2 - pos1) / BS);
552-
do {
553-
MapNode n = getMap().getNode(iterator.m_current_node_pos);
554-
555-
// Return non-air
556-
if (n.param0 != CONTENT_AIR) {
557-
if (p)
558-
*p = iterator.m_current_node_pos;
559-
return false;
560-
}
561-
iterator.next();
562-
} while (iterator.m_current_index <= iterator.m_last_index);
563-
return true;
564-
}
565-
566548
void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason,
567549
const std::string &str_reason, bool reconnect)
568550
{

‎src/serverenvironment.h

-10
Original file line numberDiff line numberDiff line change
@@ -334,16 +334,6 @@ class ServerEnvironment : public Environment
334334
// This makes stuff happen
335335
void step(f32 dtime);
336336

337-
/*!
338-
* Returns false if the given line intersects with a
339-
* non-air node, true otherwise.
340-
* \param pos1 start of the line
341-
* \param pos2 end of the line
342-
* \param p output, position of the first non-air node
343-
* the line intersects
344-
*/
345-
bool line_of_sight(v3f pos1, v3f pos2, v3s16 *p = NULL);
346-
347337
u32 getGameTime() const { return m_game_time; }
348338

349339
void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }

0 commit comments

Comments
 (0)
Please sign in to comment.