[CSM] Add core.get_timeofday & core.get_day_count env calls (#5401)
authorLoïc Blot <nerzhul@users.noreply.github.com>
Fri, 17 Mar 2017 06:48:29 +0000 (07:48 +0100)
committerGitHub <noreply@github.com>
Fri, 17 Mar 2017 06:48:29 +0000 (07:48 +0100)
* [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

13 files changed:
clientmods/preview/init.lua
src/client.cpp
src/clientenvironment.cpp
src/environment.cpp
src/environment.h
src/script/clientscripting.cpp
src/script/cpp_api/s_client.cpp
src/script/cpp_api/s_client.h
src/script/lua_api/l_env.cpp
src/script/lua_api/l_env.h
src/script/lua_api/l_internal.h
src/serverenvironment.cpp
src/serverenvironment.h

index bdda7fe4ebd7a4cc43e79e132f60c488ab300882..3f85d576d0536dab48f277d07d11791cc513be3d 100644 (file)
@@ -64,6 +64,15 @@ core.after(2, function()
        preview_minimap()
        modstorage:set_string("current_mod", modname)
        print(modstorage:get_string("current_mod"))
+
+       print("[PREVIEW] Day count: " .. core.get_day_count() ..
+               " time of day " .. core.get_timeofday())
+
+       print("[PREVIEW] Node level: " .. core.get_node_level({x=0, y=20, z=0}) ..
+               " max level " .. core.get_node_max_level({x=0, y=20, z=0}))
+
+       print("[PREVIEW] Find node near: " .. dump(core.find_node_near({x=0, y=20, z=0}, 10,
+               {"group:tree", "default:dirt", "default:stone"})))
 end)
 
 core.register_on_dignode(function(pos, node)
index 2491db7045ba5a0dc17c3f5d098e931d6357da87..b355fa61747fe382ab9915757ad2adfe2c4cf70e 100644 (file)
@@ -268,6 +268,7 @@ Client::Client(
        m_modding_enabled = g_settings->getBool("enable_client_modding");
        m_script = new ClientScripting(this);
        m_env.setScript(m_script);
+       m_script->setEnv(&m_env);
 }
 
 void Client::initMods()
index 7a74c897c875fd9572cbbe9895fbf0337fe064bd..29ecd2dfe97cd71aaa971495fa482bb216c5e13a 100644 (file)
@@ -38,6 +38,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 ClientEnvironment::ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
        ITextureSource *texturesource, Client *client,
        IrrlichtDevice *irr):
+       Environment(client),
        m_map(map),
        m_local_player(NULL),
        m_smgr(smgr),
index 737d93ecdba0c49dd6850973aa395a3681f03b11..d1ea5f8bba7a159a5fe12ad5eb1f090d742a2e9b 100644 (file)
@@ -26,13 +26,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "daynightratio.h"
 #include "emerge.h"
 
-Environment::Environment():
+Environment::Environment(IGameDef *gamedef):
        m_time_of_day_speed(0),
        m_time_of_day(9000),
        m_time_of_day_f(9000./24000),
        m_time_conversion_skew(0.0f),
        m_enable_day_night_ratio_override(false),
-       m_day_night_ratio_override(0.0f)
+       m_day_night_ratio_override(0.0f),
+       m_gamedef(gamedef)
 {
        m_cache_enable_shaders = g_settings->getBool("enable_shaders");
        m_cache_active_block_mgmt_interval = g_settings->getFloat("active_block_mgmt_interval");
index 5154bbdcb2e4472af78219807d6f3611607c0c17..52f36981785456f8cdf86bde42adc91c12e7ee4c 100644 (file)
@@ -40,13 +40,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "threading/atomic.h"
 #include "network/networkprotocol.h" // for AccessDeniedCode
 
+class IGameDef;
 class Map;
 
 class Environment
 {
 public:
        // Environment will delete the map passed to the constructor
-       Environment();
+       Environment(IGameDef *gamedef);
        virtual ~Environment();
 
        /*
@@ -77,6 +78,7 @@ public:
        // counter used internally when triggering ABMs
        u32 m_added_objects;
 
+       IGameDef* getGameDef() { return m_gamedef; }
 protected:
        GenericAtomic<float> m_time_of_day_speed;
 
@@ -114,6 +116,7 @@ protected:
        float m_cache_abm_interval;
        float m_cache_nodetimer_interval;
 
+       IGameDef *m_gamedef;
 private:
        Mutex m_time_lock;
 
index 8bf1b68b11631bea53b507e5234ea6d65f9eb938..df30a7253cacfdf46ea033ff976c04c636d45d9a 100644 (file)
@@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "client.h"
 #include "cpp_api/s_internal.h"
 #include "lua_api/l_client.h"
+#include "lua_api/l_env.h"
 #include "lua_api/l_minimap.h"
 #include "lua_api/l_storage.h"
 #include "lua_api/l_sound.h"
@@ -62,6 +63,7 @@ void ClientScripting::InitializeModApi(lua_State *L, int top)
        ModApiClient::Initialize(L, top);
        ModApiSound::Initialize(L, top);
        ModApiStorage::Initialize(L, top);
+       ModApiEnvMod::InitializeClient(L, top);
 
        LuaItemStack::Register(L);
        StorageRef::Register(L);
index 154dd61947dcc5395423937fffb99e24371b9779..666fd693d865281afd64f0ef6f9bd2074ae01afb 100644 (file)
@@ -177,3 +177,8 @@ bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
        bool blocked = lua_toboolean(L, -1);
        return blocked;
 }
+
+void ScriptApiClient::setEnv(ClientEnvironment *env)
+{
+       ScriptApiBase::setEnv(env);
+}
index 93e9558f23792bb852e622d96871b000ed112f90..2369efe3e731a29218e3a315d8b95423b66ae2de 100644 (file)
@@ -29,6 +29,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <cstdint>
 #endif
 
+class ClientEnvironment;
+
 class ScriptApiClient: virtual public ScriptApiBase
 {
 public:
@@ -47,5 +49,7 @@ public:
 
        bool on_dignode(v3s16 p, MapNode node);
        bool on_punchnode(v3s16 p, MapNode node);
+
+       void setEnv(ClientEnvironment *env);
 };
 #endif
index 14df558d311a16da9107d2a339ae19646fbb2f0a..4fad7b37ced316915696970c3be88f071d966bdc 100644 (file)
@@ -347,7 +347,10 @@ 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)
 {
-       GET_ENV_PTR;
+       Environment *env = getEnv(L);
+       if (!env) {
+               return 0;
+       }
 
        v3s16 pos = read_v3s16(L, 1);
        MapNode n = env->getMap().getNodeNoEx(pos);
@@ -359,7 +362,10 @@ 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)
 {
-       GET_ENV_PTR;
+       Environment *env = getEnv(L);
+       if (!env) {
+               return 0;
+       }
 
        v3s16 pos = read_v3s16(L, 1);
        MapNode n = env->getMap().getNodeNoEx(pos);
@@ -558,11 +564,14 @@ int ModApiEnvMod::l_set_timeofday(lua_State *L)
 // get_timeofday() -> 0...1
 int ModApiEnvMod::l_get_timeofday(lua_State *L)
 {
-       GET_ENV_PTR;
+       Environment *env = getEnv(L);
+       if (!env) {
+               return 0;
+       }
 
        // Do it
        int timeofday_mh = env->getTimeOfDay();
-       float timeofday_f = (float)timeofday_mh / 24000.0;
+       float timeofday_f = (float)timeofday_mh / 24000.0f;
        lua_pushnumber(L, timeofday_f);
        return 1;
 }
@@ -570,7 +579,10 @@ int ModApiEnvMod::l_get_timeofday(lua_State *L)
 // get_day_count() -> int
 int ModApiEnvMod::l_get_day_count(lua_State *L)
 {
-       GET_ENV_PTR;
+       Environment *env = getEnv(L);
+       if (!env) {
+               return 0;
+       }
 
        lua_pushnumber(L, env->getDayCount());
        return 1;
@@ -591,9 +603,12 @@ 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)
 {
-       GET_ENV_PTR;
+       Environment *env = getEnv(L);
+       if (!env) {
+               return 0;
+       }
 
-       INodeDefManager *ndef = getServer(L)->ndef();
+       INodeDefManager *ndef = getGameDef(L)->ndef();
        v3s16 pos = read_v3s16(L, 1);
        int radius = luaL_checkinteger(L, 2);
        std::set<content_t> filter;
@@ -611,13 +626,13 @@ int ModApiEnvMod::l_find_node_near(lua_State *L)
                ndef->getIds(lua_tostring(L, 3), filter);
        }
 
-       for(int d=1; d<=radius; d++){
+       for (int d=1; d<=radius; d++){
                std::vector<v3s16> list = FacePositionCache::getFacePositions(d);
-               for(std::vector<v3s16>::iterator i = list.begin();
+               for (std::vector<v3s16>::iterator i = list.begin();
                                i != list.end(); ++i){
                        v3s16 p = pos + (*i);
                        content_t c = env->getMap().getNodeNoEx(p).getContent();
-                       if(filter.count(c) != 0){
+                       if (filter.count(c) != 0){
                                push_v3s16(L, p);
                                return 1;
                        }
@@ -1087,3 +1102,12 @@ void ModApiEnvMod::Initialize(lua_State *L, int top)
        API_FCT(forceload_block);
        API_FCT(forceload_free_block);
 }
+
+void ModApiEnvMod::InitializeClient(lua_State *L, int top)
+{
+       API_FCT(get_timeofday);
+       API_FCT(get_day_count);
+       API_FCT(get_node_max_level);
+       API_FCT(get_node_level);
+       API_FCT(find_node_near);
+}
index 3229594113627d3ea5066e5cf7342f54840e5cb3..38b2282d7cf7e59eee34eb54dda06cc6ba6daa2b 100644 (file)
@@ -173,6 +173,7 @@ private:
 
 public:
        static void Initialize(lua_State *L, int top);
+       static void InitializeClient(lua_State *L, int top);
 
        static struct EnumString es_ClearObjectsMode[];
 };
index c610dc5a310e9448d4b991117308647201f0e007..b7627619e446713e0979e8f4cf2d0782e30aaed8 100644 (file)
@@ -37,16 +37,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #define MAP_LOCK_REQUIRED
 #define NO_MAP_LOCK_REQUIRED
 
-/*
-#if (defined(WIN32) || defined(_WIN32_WCE))
-       #define NO_MAP_LOCK_REQUIRED
-#else
-       #include "profiler.h"
-       #define NO_MAP_LOCK_REQUIRED \
-               ScopeProfiler nolocktime(g_profiler,"Scriptapi: unlockable time",SPT_ADD)
-#endif
-*/
-
 #define GET_ENV_PTR_NO_MAP_LOCK                              \
        ServerEnvironment *env = (ServerEnvironment *)getEnv(L); \
        if (env == NULL)                                         \
index ecc7c31501256b07af0c5ad3af767c8474f42dac..e09c7da16b4435860454a884f908a6cf661718c5 100644 (file)
@@ -353,7 +353,8 @@ void ActiveBlockList::update(std::vector<v3s16> &active_positions,
 
 ServerEnvironment::ServerEnvironment(ServerMap *map,
        ServerScripting *scriptIface, Server *server,
-       const std::string &path_world) :
+       const std::string &path_world):
+       Environment(server),
        m_map(map),
        m_script(scriptIface),
        m_server(server),
index b7796b5f149b42721faea14bab36440eb7c0df70..99110542a5c918175f379a723d9ac951ecde2b34 100644 (file)
@@ -320,7 +320,7 @@ public:
        //check if there's a line of sight between two positions
        bool line_of_sight(v3f pos1, v3f pos2, float stepsize=1.0, v3s16 *p=NULL);
 
-       u32 getGameTime() { return m_game_time; }
+       u32 getGameTime() const { return m_game_time; }
 
        void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }
        float getMaxLagEstimate() { return m_max_lag_estimate; }