Better snow fall, finite liquid transform, leveled nodes api
authorproller <proller@github.com>
Sun, 28 Jul 2013 13:11:59 +0000 (17:11 +0400)
committerproller <proller@github.com>
Sun, 28 Jul 2013 13:14:31 +0000 (17:14 +0400)
builtin/falling.lua
doc/lua_api.txt
src/content_abm.cpp
src/map.cpp
src/mapnode.cpp
src/mapnode.h
src/script/lua_api/l_env.cpp
src/script/lua_api/l_env.h

index 91e732a4b1c07df160b84c8dd28b408e0db53473..605252b7b95151c58c2a7a8d2ce9f726d26a36d3 100644 (file)
@@ -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)
index cf1ab7f052ef74dd7310db1128b93c076148bfb1..e5fbd7a3a52b762102e3a529207a8cdad75e1b60 100644 (file)
@@ -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)
index 110ac1eea919d641591be89ff1635c4266f887bf..2501a3a321e088581fb07188da879e425f1429cf 100644 (file)
@@ -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));
index fa52a2f52abbaf0475b6f63084d0520eb190d747..de04e5fbddc1f34f7151d1b2a0d86aaf4a925a85 100644 (file)
@@ -1717,7 +1717,7 @@ void Map::transformLiquidsFinite(std::map<v3s16, MapBlock*> & 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<v3s16, MapBlock*> & 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<v3s16, MapBlock*> & modified_blocks)
                        << (int)liquid_levels_want[D_BOTTOM]<<std::endl;
                */
 
-               u8 changed = 0;
+               //u8 changed = 0;
                for (u16 i = 0; i < 7; i++) {
                        if (liquid_levels_want[i] < 0 || !neighbors[i].l) 
                                continue;
@@ -1881,7 +1881,7 @@ void Map::transformLiquidsFinite(std::map<v3s16, MapBlock*> & 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<v3s16, MapBlock*> & 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<v3s16, MapBlock*> & 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<v3s16, MapBlock*> & 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="
                        <<new_node_content<< " p2="<<(int)n0.param2<< " nl="
                        <<(int)new_node_level<<std::endl;
                        */
                        
-                       n0.setContent(new_node_content);
+                       n0.setContent(liquid_kind_flowing);
+                       n0.setLevel(nodemgr, new_node_level);
                        // Find out whether there is a suspect for this action
                        std::string suspect;
                        if(m_gamedef->rollback()){
index 4707978cbc2a4803fa651060404277648f296511..a47a48bc0c23871cae1cb5a655cedad24b437d79 100644 (file)
@@ -364,9 +364,7 @@ u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
 {
        const ContentFeatures &f = nodemgr->get(*this);
        // todo: after update in all games leave only if (f.param_type_2 ==
-       if(        f.liquid_type == LIQUID_SOURCE 
-               || f.liquid_type == LIQUID_FLOWING 
-               || f.param_type_2 == CPT2_FLOWINGLIQUID)
+       if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
                return LIQUID_LEVEL_MAX;
        if(f.leveled || f.param_type_2 == CPT2_LEVELED)
                return LEVELED_MAX;
@@ -392,12 +390,9 @@ u8 MapNode::getLevel(INodeDefManager *nodemgr) const
        return 0;
 }
 
-u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
+u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
 {
-       s8 level = getLevel(nodemgr);
        u8 rest = 0;
-       if (add == 0) level = 1;
-       level += add;
        if (level < 1) {
                setContent(CONTENT_AIR);
                return 0;
@@ -406,8 +401,8 @@ u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
        if (       f.param_type_2 == CPT2_FLOWINGLIQUID
                || f.liquid_type == LIQUID_FLOWING
                || f.liquid_type == LIQUID_SOURCE) {
-               if (level >= LIQUID_LEVEL_MAX) {
-                       rest = level - LIQUID_LEVEL_MAX;
+               if (level >= LIQUID_LEVEL_SOURCE) {
+                       rest = level - LIQUID_LEVEL_SOURCE;
                        setContent(nodemgr->getId(f.liquid_alternative_source));
                } else {
                        setContent(nodemgr->getId(f.liquid_alternative_flowing));
@@ -423,6 +418,31 @@ u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
        return rest;
 }
 
+u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
+{
+       s8 level = getLevel(nodemgr);
+       if (add == 0) level = 1;
+       level += add;
+       return setLevel(nodemgr, level);
+}
+
+void MapNode::freezeMelt(INodeDefManager *ndef) {
+       u8 level_was_max = this->getMaxLevel(ndef);
+       u8 level_was = this->getLevel(ndef);
+       this->setContent(ndef->getId(ndef->get(*this).freezemelt));
+       u8 level_now_max = this->getMaxLevel(ndef);
+       if (level_was_max && level_was_max != level_now_max) {
+               u8 want = (float)level_now_max / level_was_max * level_was;
+               if (!want)
+                       want = 1;
+               if (want != level_was)
+                       this->setLevel(ndef, want);
+               //errorstream<<"was="<<(int)level_was<<"/"<<(int)level_was_max<<" nowm="<<(int)want<<"/"<<(int)level_now_max<< " => "<<(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))
index fcff1707a898516fcd5ea86676945ef3219828a1..785537759a4ea2ee0b12891b080fe9c759dd53de 100644 (file)
@@ -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
index 52ea557175d6135b221dcd2a55b43cbf938ee241..47bc9baf7bcc43858d7fa42b358cbf5c7c1467b1 100644 (file)
@@ -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);
index eaef16180a78bb527e5ed736079674efdca840e0..4122fd037ebc5da868222fcdbe6ec93232312f0b 100644 (file)
@@ -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);