From 037e84d377e91f37026cb5814145bdceac135534 Mon Sep 17 00:00:00 2001 From: proller Date: Sun, 28 Jul 2013 17:11:59 +0400 Subject: [PATCH] Better snow fall, finite liquid transform, leveled nodes api --- builtin/falling.lua | 11 ++++++++++- doc/lua_api.txt | 2 ++ src/content_abm.cpp | 15 ++++++-------- src/map.cpp | 23 ++++++++++++++-------- src/mapnode.cpp | 38 +++++++++++++++++++++++++++--------- src/mapnode.h | 4 +++- src/script/lua_api/l_env.cpp | 20 ++++++++++++++++++- src/script/lua_api/l_env.h | 4 ++++ 8 files changed, 88 insertions(+), 29 deletions(-) diff --git a/builtin/falling.lua b/builtin/falling.lua index 91e732a4..605252b7 100644 --- a/builtin/falling.lua +++ b/builtin/falling.lua @@ -59,7 +59,14 @@ minetest.register_entity("__builtin:falling_node", { minetest.registered_nodes[bcn.name].walkable or (minetest.get_node_group(self.node.name, "float") ~= 0 and minetest.registered_nodes[bcn.name].liquidtype ~= "none") then - if minetest.registered_nodes[bcn.name].buildable_to and (minetest.get_node_group(self.node.name, "float") == 0 or minetest.registered_nodes[bcn.name].liquidtype == "none") then + if minetest.registered_nodes[bcn.name].leveled and bcn.name == self.node.name then + local addlevel = self.node.level + if addlevel == nil or addlevel <= 0 then addlevel = minetest.registered_nodes[bcn.name].leveled end + if minetest.env:add_node_level(bcp, addlevel) == 0 then + self.object:remove() + return + end + elseif minetest.registered_nodes[bcn.name].buildable_to and (minetest.get_node_group(self.node.name, "float") == 0 or minetest.registered_nodes[bcn.name].liquidtype == "none") then minetest.remove_node(bcp) return end @@ -152,11 +159,13 @@ function nodeupdate_single(p, delay) -- Note: walkable is in the node definition, not in item groups if minetest.registered_nodes[n_bottom.name] and (minetest.get_node_group(n.name, "float") == 0 or minetest.registered_nodes[n_bottom.name].liquidtype == "none") and + (n.name ~= n_bottom.name or (minetest.registered_nodes[n_bottom.name].leveled and minetest.env:get_node_level(p_bottom) < minetest.env:get_node_max_level(p_bottom))) and (not minetest.registered_nodes[n_bottom.name].walkable or minetest.registered_nodes[n_bottom.name].buildable_to) then if delay then minetest.after(0.1, nodeupdate_single, {x=p.x, y=p.y, z=p.z}, false) else + n.level = minetest.env:get_node_level(p) minetest.remove_node(p) spawn_falling_node(p, n) nodeupdate(p) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index cf1ab7f0..e5fbd7a3 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -1268,6 +1268,8 @@ minetest.get_node_max_level(pos) ^ get max available level for leveled node minetest.get_node_level(pos) ^ get level of leveled node (water, snow) +minetest.set_node_level(pos, level) +^ set level of leveled node, default level = 1, if totallevel > maxlevel returns rest (total-max). minetest.add_node_level(pos, level) ^ increase level of leveled node by level, default level = 1, if totallevel > maxlevel returns rest (total-max). can be negative for decreasing minetest.get_heat(pos) diff --git a/src/content_abm.cpp b/src/content_abm.cpp index 110ac1ee..2501a3a3 100644 --- a/src/content_abm.cpp +++ b/src/content_abm.cpp @@ -285,7 +285,7 @@ class LiquidFreeze : public ActiveBlockModifier { } } if (allow) { - n.setContent(ndef->getId(ndef->get(n).freezemelt)); + n.freezeMelt(ndef); map->addNodeWithEvent(p, n); } } @@ -316,9 +316,7 @@ class LiquidMeltWeather : public ActiveBlockModifier { float heat = map->getHeat(env, p); if (heat >= 1 && (heat >= 40 || ((myrand_range(heat, 40)) >= 20))) { - n.setContent(ndef->getId(ndef->get(n).freezemelt)); - if (!n.getLevel(ndef)) - n.addLevel(ndef); + n.freezeMelt(ndef); map->addNodeWithEvent(p, n); env->getScriptIface()->node_falling_update(p); } @@ -346,14 +344,13 @@ class LiquidMeltHot : public ActiveBlockModifier { virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n) { ServerMap *map = &env->getServerMap(); INodeDefManager *ndef = env->getGameDef()->ndef(); - n.setContent(ndef->getId(ndef->get(n).freezemelt)); - if (!n.getLevel(ndef)) - n.addLevel(ndef); + n.freezeMelt(ndef); map->addNodeWithEvent(p, n); env->getScriptIface()->node_falling_update(p); } }; +/* too buggy, later via liquid flow code class LiquidMeltAround : public LiquidMeltHot { public: LiquidMeltAround(ServerEnvironment *env, INodeDefManager *nodemgr) @@ -368,7 +365,7 @@ class LiquidMeltAround : public LiquidMeltHot { virtual u32 getTriggerChance() { return 60; } }; - +*/ void add_legacy_abms(ServerEnvironment *env, INodeDefManager *nodedef) { env->addActiveBlockModifier(new GrowGrassABM()); @@ -378,7 +375,7 @@ void add_legacy_abms(ServerEnvironment *env, INodeDefManager *nodedef) { env->addActiveBlockModifier(new LiquidFlowABM(env, nodedef)); env->addActiveBlockModifier(new LiquidDropABM(env, nodedef)); env->addActiveBlockModifier(new LiquidMeltHot(env, nodedef)); - env->addActiveBlockModifier(new LiquidMeltAround(env, nodedef)); + //env->addActiveBlockModifier(new LiquidMeltAround(env, nodedef)); if (g_settings->getBool("weather")) { env->addActiveBlockModifier(new LiquidFreeze(env, nodedef)); env->addActiveBlockModifier(new LiquidMeltWeather(env, nodedef)); diff --git a/src/map.cpp b/src/map.cpp index fa52a2f5..de04e5fb 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -1717,7 +1717,7 @@ void Map::transformLiquidsFinite(std::map & modified_blocks) if (liquid_kind == CONTENT_IGNORE) liquid_kind = nb.n.getContent(); if (nb.n.getContent() == liquid_kind) { - liquid_levels[i] = LIQUID_LEVEL_SOURCE; + liquid_levels[i] = nb.n.getLevel(nodemgr); //LIQUID_LEVEL_SOURCE; nb.l = 1; nb.i = (nb.n.param2 & LIQUID_INFINITY_MASK); } @@ -1731,7 +1731,7 @@ void Map::transformLiquidsFinite(std::map & modified_blocks) liquid_kind = nodemgr->getId( nodemgr->get(nb.n).liquid_alternative_source); if (nb.n.getContent() == liquid_kind_flowing) { - liquid_levels[i] = (nb.n.param2 & LIQUID_LEVEL_MASK); + liquid_levels[i] = nb.n.getLevel(nodemgr); //(nb.n.param2 & LIQUID_LEVEL_MASK); nb.l = 1; } break; @@ -1849,7 +1849,7 @@ void Map::transformLiquidsFinite(std::map & modified_blocks) << (int)liquid_levels_want[D_BOTTOM]< & modified_blocks) new_node_content = liquid_kind_flowing; else new_node_content = CONTENT_AIR; - + // last level must flow down on stairs if (liquid_levels_want[i] != liquid_levels[i] && liquid_levels[D_TOP] <= 0 && !neighbors[D_BOTTOM].l && @@ -1896,10 +1896,11 @@ void Map::transformLiquidsFinite(std::map & modified_blocks) check if anything has changed. if not, just continue with the next node. */ + /* if ( new_node_content == n0.getContent() && (nodemgr->get(n0.getContent()).liquid_type != LIQUID_FLOWING || - ((n0.param2 & LIQUID_LEVEL_MASK) == (u8)new_node_level + (n0.getLevel(nodemgr) == (u8)new_node_level //&& ((n0.param2 & LIQUID_FLOW_DOWN_MASK) == //LIQUID_FLOW_DOWN_MASK) == flowing_down )) @@ -1908,14 +1909,18 @@ void Map::transformLiquidsFinite(std::map & modified_blocks) (((n0.param2 & LIQUID_INFINITY_MASK) == LIQUID_INFINITY_MASK) == neighbors[i].i )) - ) { + )*/ + if (liquid_levels[i] == new_node_level) + { continue; } - ++changed; + + //++changed; /* update the current node */ + /* if (nodemgr->get(new_node_content).liquid_type == LIQUID_FLOWING) { // set level to last 3 bits, flowing down bit to 4th bit n0.param2 = (new_node_level & LIQUID_LEVEL_MASK); @@ -1923,13 +1928,15 @@ void Map::transformLiquidsFinite(std::map & modified_blocks) //n0.param2 = ~(LIQUID_LEVEL_MASK | LIQUID_FLOW_DOWN_MASK); n0.param2 = (neighbors[i].i ? LIQUID_INFINITY_MASK : 0x00); } + */ /* infostream << "set node i=" <<(int)i<<" "<< PP(p0)<< " nc=" < "<<(int)this->getLevel(ndef)<< std::endl; + } + if (this->getMaxLevel(ndef) && !this->getLevel(ndef)) + this->addLevel(ndef); +} + u32 MapNode::serializedLength(u8 version) { if(!ser_ver_supported(version)) diff --git a/src/mapnode.h b/src/mapnode.h index fcff1707..78553775 100644 --- a/src/mapnode.h +++ b/src/mapnode.h @@ -104,7 +104,7 @@ enum Rotation { #define LIQUID_INFINITY_MASK 0x80 //0b10000000 // mask for param2, now as for liquid -#define LEVELED_MASK 0x07 +#define LEVELED_MASK 0x3F #define LEVELED_MAX LEVELED_MASK /* @@ -229,7 +229,9 @@ struct MapNode /* Liquid helpers */ u8 getMaxLevel(INodeDefManager *nodemgr) const; u8 getLevel(INodeDefManager *nodemgr) const; + u8 setLevel(INodeDefManager *nodemgr, s8 level = 1); u8 addLevel(INodeDefManager *nodemgr, s8 add = 1); + void freezeMelt(INodeDefManager *nodemgr); /* Serialization functions diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 52ea5571..47bc9baf 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -287,9 +287,26 @@ int ModApiEnvMod::l_get_node_level(lua_State *L) return 1; } +// minetest.set_node_level(pos, level) +// pos = {x=num, y=num, z=num} +// level: 0..63 +int ModApiEnvMod::l_set_node_level(lua_State *L) +{ + GET_ENV_PTR; + + v3s16 pos = read_v3s16(L, 1); + u8 level = 1; + if(lua_isnumber(L, 2)) + level = lua_tonumber(L, 2); + MapNode n = env->getMap().getNodeNoEx(pos); + lua_pushnumber(L, n.setLevel(env->getGameDef()->ndef(), level)); + env->setNode(pos, n); + return 1; +} + // minetest.add_node_level(pos, level) // pos = {x=num, y=num, z=num} -// level: 0..8 +// level: 0..63 int ModApiEnvMod::l_add_node_level(lua_State *L) { GET_ENV_PTR; @@ -913,6 +930,7 @@ bool ModApiEnvMod::Initialize(lua_State *L,int top) retval &= API_FCT(punch_node); retval &= API_FCT(get_node_max_level); retval &= API_FCT(get_node_level); + retval &= API_FCT(set_node_level); retval &= API_FCT(add_node_level); retval &= API_FCT(add_entity); retval &= API_FCT(get_meta); diff --git a/src/script/lua_api/l_env.h b/src/script/lua_api/l_env.h index eaef1618..4122fd03 100644 --- a/src/script/lua_api/l_env.h +++ b/src/script/lua_api/l_env.h @@ -76,6 +76,10 @@ private: // pos = {x=num, y=num, z=num} static int l_get_node_level(lua_State *L); + // minetest.set_node_level(pos) + // pos = {x=num, y=num, z=num} + static int l_set_node_level(lua_State *L); + // minetest.add_node_level(pos) // pos = {x=num, y=num, z=num} static int l_add_node_level(lua_State *L); -- 2.30.2