Skip to content

Commit 0891975

Browse files
authoredMar 17, 2017
[CSM] Add core.get_timeofday & core.get_day_count env calls (#5401)
* [CSM] Add core.get_timeofday & core.get_day_count env calls * [CSM] Add core.get_node_level, core.get_node_max_level, core.find_node_near
1 parent b52f300 commit 0891975

13 files changed

+67
-25
lines changed
 

‎clientmods/preview/init.lua

+9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ core.after(2, function()
6464
preview_minimap()
6565
modstorage:set_string("current_mod", modname)
6666
print(modstorage:get_string("current_mod"))
67+
68+
print("[PREVIEW] Day count: " .. core.get_day_count() ..
69+
" time of day " .. core.get_timeofday())
70+
71+
print("[PREVIEW] Node level: " .. core.get_node_level({x=0, y=20, z=0}) ..
72+
" max level " .. core.get_node_max_level({x=0, y=20, z=0}))
73+
74+
print("[PREVIEW] Find node near: " .. dump(core.find_node_near({x=0, y=20, z=0}, 10,
75+
{"group:tree", "default:dirt", "default:stone"})))
6776
end)
6877

6978
core.register_on_dignode(function(pos, node)

‎src/client.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ Client::Client(
268268
m_modding_enabled = g_settings->getBool("enable_client_modding");
269269
m_script = new ClientScripting(this);
270270
m_env.setScript(m_script);
271+
m_script->setEnv(&m_env);
271272
}
272273

273274
void Client::initMods()

‎src/clientenvironment.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3838
ClientEnvironment::ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
3939
ITextureSource *texturesource, Client *client,
4040
IrrlichtDevice *irr):
41+
Environment(client),
4142
m_map(map),
4243
m_local_player(NULL),
4344
m_smgr(smgr),

‎src/environment.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2626
#include "daynightratio.h"
2727
#include "emerge.h"
2828

29-
Environment::Environment():
29+
Environment::Environment(IGameDef *gamedef):
3030
m_time_of_day_speed(0),
3131
m_time_of_day(9000),
3232
m_time_of_day_f(9000./24000),
3333
m_time_conversion_skew(0.0f),
3434
m_enable_day_night_ratio_override(false),
35-
m_day_night_ratio_override(0.0f)
35+
m_day_night_ratio_override(0.0f),
36+
m_gamedef(gamedef)
3637
{
3738
m_cache_enable_shaders = g_settings->getBool("enable_shaders");
3839
m_cache_active_block_mgmt_interval = g_settings->getFloat("active_block_mgmt_interval");

‎src/environment.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
4040
#include "threading/atomic.h"
4141
#include "network/networkprotocol.h" // for AccessDeniedCode
4242

43+
class IGameDef;
4344
class Map;
4445

4546
class Environment
4647
{
4748
public:
4849
// Environment will delete the map passed to the constructor
49-
Environment();
50+
Environment(IGameDef *gamedef);
5051
virtual ~Environment();
5152

5253
/*
@@ -77,6 +78,7 @@ class Environment
7778
// counter used internally when triggering ABMs
7879
u32 m_added_objects;
7980

81+
IGameDef* getGameDef() { return m_gamedef; }
8082
protected:
8183
GenericAtomic<float> m_time_of_day_speed;
8284

@@ -114,6 +116,7 @@ class Environment
114116
float m_cache_abm_interval;
115117
float m_cache_nodetimer_interval;
116118

119+
IGameDef *m_gamedef;
117120
private:
118121
Mutex m_time_lock;
119122

‎src/script/clientscripting.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2222
#include "client.h"
2323
#include "cpp_api/s_internal.h"
2424
#include "lua_api/l_client.h"
25+
#include "lua_api/l_env.h"
2526
#include "lua_api/l_minimap.h"
2627
#include "lua_api/l_storage.h"
2728
#include "lua_api/l_sound.h"
@@ -62,6 +63,7 @@ void ClientScripting::InitializeModApi(lua_State *L, int top)
6263
ModApiClient::Initialize(L, top);
6364
ModApiSound::Initialize(L, top);
6465
ModApiStorage::Initialize(L, top);
66+
ModApiEnvMod::InitializeClient(L, top);
6567

6668
LuaItemStack::Register(L);
6769
StorageRef::Register(L);

‎src/script/cpp_api/s_client.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,8 @@ bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
177177
bool blocked = lua_toboolean(L, -1);
178178
return blocked;
179179
}
180+
181+
void ScriptApiClient::setEnv(ClientEnvironment *env)
182+
{
183+
ScriptApiBase::setEnv(env);
184+
}

‎src/script/cpp_api/s_client.h

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2929
#include <cstdint>
3030
#endif
3131

32+
class ClientEnvironment;
33+
3234
class ScriptApiClient: virtual public ScriptApiBase
3335
{
3436
public:
@@ -47,5 +49,7 @@ class ScriptApiClient: virtual public ScriptApiBase
4749

4850
bool on_dignode(v3s16 p, MapNode node);
4951
bool on_punchnode(v3s16 p, MapNode node);
52+
53+
void setEnv(ClientEnvironment *env);
5054
};
5155
#endif

‎src/script/lua_api/l_env.cpp

+34-10
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,10 @@ int ModApiEnvMod::l_punch_node(lua_State *L)
347347
// pos = {x=num, y=num, z=num}
348348
int ModApiEnvMod::l_get_node_max_level(lua_State *L)
349349
{
350-
GET_ENV_PTR;
350+
Environment *env = getEnv(L);
351+
if (!env) {
352+
return 0;
353+
}
351354

352355
v3s16 pos = read_v3s16(L, 1);
353356
MapNode n = env->getMap().getNodeNoEx(pos);
@@ -359,7 +362,10 @@ int ModApiEnvMod::l_get_node_max_level(lua_State *L)
359362
// pos = {x=num, y=num, z=num}
360363
int ModApiEnvMod::l_get_node_level(lua_State *L)
361364
{
362-
GET_ENV_PTR;
365+
Environment *env = getEnv(L);
366+
if (!env) {
367+
return 0;
368+
}
363369

364370
v3s16 pos = read_v3s16(L, 1);
365371
MapNode n = env->getMap().getNodeNoEx(pos);
@@ -558,19 +564,25 @@ int ModApiEnvMod::l_set_timeofday(lua_State *L)
558564
// get_timeofday() -> 0...1
559565
int ModApiEnvMod::l_get_timeofday(lua_State *L)
560566
{
561-
GET_ENV_PTR;
567+
Environment *env = getEnv(L);
568+
if (!env) {
569+
return 0;
570+
}
562571

563572
// Do it
564573
int timeofday_mh = env->getTimeOfDay();
565-
float timeofday_f = (float)timeofday_mh / 24000.0;
574+
float timeofday_f = (float)timeofday_mh / 24000.0f;
566575
lua_pushnumber(L, timeofday_f);
567576
return 1;
568577
}
569578

570579
// get_day_count() -> int
571580
int ModApiEnvMod::l_get_day_count(lua_State *L)
572581
{
573-
GET_ENV_PTR;
582+
Environment *env = getEnv(L);
583+
if (!env) {
584+
return 0;
585+
}
574586

575587
lua_pushnumber(L, env->getDayCount());
576588
return 1;
@@ -591,9 +603,12 @@ int ModApiEnvMod::l_get_gametime(lua_State *L)
591603
// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
592604
int ModApiEnvMod::l_find_node_near(lua_State *L)
593605
{
594-
GET_ENV_PTR;
606+
Environment *env = getEnv(L);
607+
if (!env) {
608+
return 0;
609+
}
595610

596-
INodeDefManager *ndef = getServer(L)->ndef();
611+
INodeDefManager *ndef = getGameDef(L)->ndef();
597612
v3s16 pos = read_v3s16(L, 1);
598613
int radius = luaL_checkinteger(L, 2);
599614
std::set<content_t> filter;
@@ -611,13 +626,13 @@ int ModApiEnvMod::l_find_node_near(lua_State *L)
611626
ndef->getIds(lua_tostring(L, 3), filter);
612627
}
613628

614-
for(int d=1; d<=radius; d++){
629+
for (int d=1; d<=radius; d++){
615630
std::vector<v3s16> list = FacePositionCache::getFacePositions(d);
616-
for(std::vector<v3s16>::iterator i = list.begin();
631+
for (std::vector<v3s16>::iterator i = list.begin();
617632
i != list.end(); ++i){
618633
v3s16 p = pos + (*i);
619634
content_t c = env->getMap().getNodeNoEx(p).getContent();
620-
if(filter.count(c) != 0){
635+
if (filter.count(c) != 0){
621636
push_v3s16(L, p);
622637
return 1;
623638
}
@@ -1087,3 +1102,12 @@ void ModApiEnvMod::Initialize(lua_State *L, int top)
10871102
API_FCT(forceload_block);
10881103
API_FCT(forceload_free_block);
10891104
}
1105+
1106+
void ModApiEnvMod::InitializeClient(lua_State *L, int top)
1107+
{
1108+
API_FCT(get_timeofday);
1109+
API_FCT(get_day_count);
1110+
API_FCT(get_node_max_level);
1111+
API_FCT(get_node_level);
1112+
API_FCT(find_node_near);
1113+
}

‎src/script/lua_api/l_env.h

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class ModApiEnvMod : public ModApiBase {
173173

174174
public:
175175
static void Initialize(lua_State *L, int top);
176+
static void InitializeClient(lua_State *L, int top);
176177

177178
static struct EnumString es_ClearObjectsMode[];
178179
};

‎src/script/lua_api/l_internal.h

-10
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3737
#define MAP_LOCK_REQUIRED
3838
#define NO_MAP_LOCK_REQUIRED
3939

40-
/*
41-
#if (defined(WIN32) || defined(_WIN32_WCE))
42-
#define NO_MAP_LOCK_REQUIRED
43-
#else
44-
#include "profiler.h"
45-
#define NO_MAP_LOCK_REQUIRED \
46-
ScopeProfiler nolocktime(g_profiler,"Scriptapi: unlockable time",SPT_ADD)
47-
#endif
48-
*/
49-
5040
#define GET_ENV_PTR_NO_MAP_LOCK \
5141
ServerEnvironment *env = (ServerEnvironment *)getEnv(L); \
5242
if (env == NULL) \

‎src/serverenvironment.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ void ActiveBlockList::update(std::vector<v3s16> &active_positions,
353353

354354
ServerEnvironment::ServerEnvironment(ServerMap *map,
355355
ServerScripting *scriptIface, Server *server,
356-
const std::string &path_world) :
356+
const std::string &path_world):
357+
Environment(server),
357358
m_map(map),
358359
m_script(scriptIface),
359360
m_server(server),

‎src/serverenvironment.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class ServerEnvironment : public Environment
320320
//check if there's a line of sight between two positions
321321
bool line_of_sight(v3f pos1, v3f pos2, float stepsize=1.0, v3s16 *p=NULL);
322322

323-
u32 getGameTime() { return m_game_time; }
323+
u32 getGameTime() const { return m_game_time; }
324324

325325
void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }
326326
float getMaxLagEstimate() { return m_max_lag_estimate; }

0 commit comments

Comments
 (0)
Please sign in to comment.