]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_env.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / script / lua_api / l_env.cpp
index 8a72acee1fa0e11cb802c302af6fc1d72ff3cdae..d6dc79ca89930029834a2e034b9a9f15ef85ac5c 100644 (file)
@@ -407,6 +407,46 @@ int ModApiEnvMod::l_get_node_light(lua_State *L)
        return 1;
 }
 
+
+// get_natural_light(pos, timeofday)
+// pos = {x=num, y=num, z=num}
+// timeofday: nil = current time, 0 = night, 0.5 = day
+int ModApiEnvMod::l_get_natural_light(lua_State *L)
+{
+       GET_ENV_PTR;
+
+       v3s16 pos = read_v3s16(L, 1);
+
+       bool is_position_ok;
+       MapNode n = env->getMap().getNode(pos, &is_position_ok);
+       if (!is_position_ok)
+               return 0;
+
+       // If the daylight is 0, nothing needs to be calculated
+       u8 daylight = n.param1 & 0x0f;
+       if (daylight == 0) {
+               lua_pushinteger(L, 0);
+               return 1;
+       }
+
+       u32 time_of_day;
+       if (lua_isnumber(L, 2)) {
+               time_of_day = 24000.0 * lua_tonumber(L, 2);
+               time_of_day %= 24000;
+       } else {
+               time_of_day = env->getTimeOfDay();
+       }
+       u32 dnr = time_to_daynight_ratio(time_of_day, true);
+
+       // If it's the same as the artificial light, the sunlight needs to be
+       // searched for because the value may not emanate from the sun
+       if (daylight == n.param1 >> 4)
+               daylight = env->findSunlight(pos);
+
+       lua_pushinteger(L, dnr * daylight / 1000);
+       return 1;
+}
+
 // place_node(pos, node)
 // pos = {x=num, y=num, z=num}
 int ModApiEnvMod::l_place_node(lua_State *L)
@@ -703,6 +743,31 @@ int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
        return 1;
 }
 
+// get_objects_in_area(pos, minp, maxp)
+int ModApiEnvMod::l_get_objects_in_area(lua_State *L)
+{
+       GET_ENV_PTR;
+       ScriptApiBase *script = getScriptApiBase(L);
+       
+       v3f minp = read_v3f(L, 1) * BS;
+       v3f maxp = read_v3f(L, 2) * BS;
+       aabb3f box(minp, maxp);
+       box.repair();
+       std::vector<ServerActiveObject *> objs;
+
+       auto include_obj_cb = [](ServerActiveObject *obj){ return !obj->isGone(); };
+       env->getObjectsInArea(objs, box, include_obj_cb);
+
+       int i = 0;
+       lua_createtable(L, objs.size(), 0);
+       for (const auto obj : objs) {
+               // Insert object reference into table
+               script->objectrefGetOrCreate(L, obj);
+               lua_rawseti(L, -2, ++i);
+       }
+       return 1;
+}
+
 // set_timeofday(val)
 // val = 0...1
 int ModApiEnvMod::l_set_timeofday(lua_State *L)
@@ -711,8 +776,9 @@ int ModApiEnvMod::l_set_timeofday(lua_State *L)
 
        // Do it
        float timeofday_f = readParam<float>(L, 1);
-       sanity_check(timeofday_f >= 0.0 && timeofday_f <= 1.0);
-       int timeofday_mh = (int)(timeofday_f * 24000.0);
+       luaL_argcheck(L, timeofday_f >= 0.0f && timeofday_f <= 1.0f, 1,
+               "value must be between 0 and 1");
+       int timeofday_mh = (int)(timeofday_f * 24000.0f);
        // This should be set directly in the environment but currently
        // such changes aren't immediately sent to the clients, so call
        // the server instead.
@@ -796,7 +862,6 @@ int ModApiEnvMod::l_find_node_near(lua_State *L)
                        v3s16 p = pos + i;
                        content_t c = map.getNode(p).getContent();
                        if (CONTAINS(filter, c)) {
-                               std::cout << p.X << " " << p.Y << " " << p.Z << std::endl;
                                push_v3s16(L, p);
                                return 1;
                        }
@@ -1366,7 +1431,7 @@ int ModApiEnvMod::l_delete_area(lua_State *L)
 //     max_jump, max_drop, algorithm) -> table containing path
 int ModApiEnvMod::l_find_path(lua_State *L)
 {
-       GET_ENV_PTR;
+       Environment *env = getEnv(L);
 
        v3s16 pos1                  = read_v3s16(L, 1);
        v3s16 pos2                  = read_v3s16(L, 2);
@@ -1383,8 +1448,8 @@ int ModApiEnvMod::l_find_path(lua_State *L)
                if (algorithm == "Dijkstra")
                        algo = PA_DIJKSTRA;
        }
-
-       std::vector<v3s16> path = get_path(&env->getServerMap(), env->getGameDef()->ndef(), pos1, pos2,
+       
+       std::vector<v3s16> path = get_path(&env->getMap(), env->getGameDef()->ndef(), pos1, pos2,
                searchdistance, max_jump, max_drop, algo);
 
        if (!path.empty()) {
@@ -1499,9 +1564,9 @@ int ModApiEnvMod::l_get_translated_string(lua_State * L)
        GET_ENV_PTR;
        std::string lang_code = luaL_checkstring(L, 1);
        std::string string = luaL_checkstring(L, 2);
-       getServer(L)->loadTranslationLanguage(lang_code);
-       string = wide_to_utf8(translate_string(utf8_to_wide(string),
-                       &(*g_server_translations)[lang_code]));
+
+       auto *translations = getServer(L)->getTranslationLanguage(lang_code);
+       string = wide_to_utf8(translate_string(utf8_to_wide(string), translations));
        lua_pushstring(L, string.c_str());
        return 1;
 }
@@ -1517,6 +1582,7 @@ void ModApiEnvMod::Initialize(lua_State *L, int top)
        API_FCT(get_node);
        API_FCT(get_node_or_nil);
        API_FCT(get_node_light);
+       API_FCT(get_natural_light);
        API_FCT(place_node);
        API_FCT(dig_node);
        API_FCT(punch_node);
@@ -1530,6 +1596,7 @@ void ModApiEnvMod::Initialize(lua_State *L, int top)
        API_FCT(get_node_timer);
        API_FCT(get_connected_players);
        API_FCT(get_player_by_name);
+       API_FCT(get_objects_in_area);
        API_FCT(get_objects_inside_radius);
        API_FCT(set_timeofday);
        API_FCT(get_timeofday);
@@ -1569,6 +1636,7 @@ void ModApiEnvMod::InitializeClient(lua_State *L, int top)
        API_FCT(find_nodes_near_under_air_except);
        API_FCT(find_nodes_in_area);
        API_FCT(find_nodes_in_area_under_air);
+       API_FCT(find_path);
        API_FCT(line_of_sight);
        API_FCT(raycast);
 }