Cleanup (some stuff went wrong when reverting 4-byte mapnodes); fix legacy_wallmounted
[oweals/minetest.git] / src / mapnode.h
index 7fe2054631fb31d1b8b1cd5449386bb2399ad5e6..5e066604bfc00ddc02bd23b7cd4b9739effb399d 100644 (file)
@@ -20,14 +20,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #ifndef MAPNODE_HEADER
 #define MAPNODE_HEADER
 
-#include <iostream>
 #include "irrlichttypes.h"
 #include "light.h"
-#include "exceptions.h"
-#include "serialization.h"
-#ifndef SERVER
-#include "tile.h"
-#endif
 
 class INodeDefManager;
 
@@ -38,8 +32,8 @@ class INodeDefManager;
        - Tile = TileSpec at some side of a node of some content type
 
        Content ranges:
-               0x000...0x07f: param2 is fully usable
-               0x800...0xfff: param2 lower 4 bytes are free
+         0x000...0x07f: param2 is fully usable
+         0x800...0xfff: param2 lower 4 bits are free
 */
 typedef u16 content_t;
 #define MAX_CONTENT 0xfff
@@ -62,36 +56,6 @@ typedef u16 content_t;
 */
 #define CONTENT_AIR 126
 
-#ifndef SERVER
-/*
-       Nodes make a face if contents differ and solidness differs.
-       Return value:
-               0: No face
-               1: Face uses m1's content
-               2: Face uses m2's content
-       equivalent: Whether the blocks share the same face (eg. water and glass)
-*/
-u8 face_contents(content_t m1, content_t m2, bool *equivalent,
-               INodeDefManager *nodemgr);
-#endif
-
-/*
-       Packs directions like (1,0,0), (1,-1,0) in six bits.
-       NOTE: This wastes way too much space for most purposes.
-*/
-u8 packDir(v3s16 dir);
-v3s16 unpackDir(u8 b);
-
-/*
-       facedir: CPT_FACEDIR_SIMPLE param1 value
-       dir: The face for which stuff is wanted
-       return value: The face from which the stuff is actually found
-
-       NOTE: Currently this uses 2 bits for Z-,X-,Z+,X+, should there be Y+
-             and Y- too?
-*/
-v3s16 facedir_rotate(u8 facedir, v3s16 dir);
-
 enum LightBank
 {
        LIGHTBANK_DAY,
@@ -128,7 +92,6 @@ struct MapNode
                  stored logarithmically from 0 to LIGHT_MAX.
                  Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
                  - Contains 2 values, day- and night lighting. Each takes 4 bits.
-               - Mineral content (should be removed from here)
                - Uhh... well, most blocks have light or nothing in here.
        */
        u8 param1;
@@ -153,6 +116,11 @@ struct MapNode
                // because this needs to override part of param2
                setContent(content);
        }
+       
+       // Create directly from a nodename
+       // If name is unknown, sets CONTENT_IGNORE
+       MapNode(INodeDefManager *ndef, const std::string &name,
+                       u8 a_param1=0, u8 a_param2=0);
 
        bool operator==(const MapNode &other)
        {
@@ -184,65 +152,95 @@ struct MapNode
                        param2 |= (c&0x0f)<<4;
                }
        }
+       u8 getParam1() const
+       {
+               return param1;
+       }
+       void setParam1(u8 p)
+       {
+               param1 = p;
+       }
+       u8 getParam2() const
+       {
+               if(param0 < 0x80)
+                       return param2;
+               else
+                       return param2 & 0x0f;
+       }
+       void setParam2(u8 p)
+       {
+               if(param0 < 0x80)
+                       param2 = p;
+               else{
+                       param2 &= 0xf0;
+                       param2 |= (p&0x0f);
+               }
+       }
        
        void setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr);
        u8 getLight(enum LightBank bank, INodeDefManager *nodemgr) const;
-       u8 getLightBanksWithSource(INodeDefManager *nodemgr) const;
+       bool getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const;
        
        // 0 <= daylight_factor <= 1000
        // 0 <= return value <= LIGHT_SUN
        u8 getLightBlend(u32 daylight_factor, INodeDefManager *nodemgr) const
        {
-               u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY, nodemgr)
-                       + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT, nodemgr))
-                       )/1000;
-               u8 max = LIGHT_MAX;
-               if(getLight(LIGHTBANK_DAY, nodemgr) == LIGHT_SUN)
-                       max = LIGHT_SUN;
-               if(l > max)
-                       l = max;
-               return l;
+               u8 lightday = 0;
+               u8 lightnight = 0;
+               getLightBanks(lightday, lightnight, nodemgr);
+               return blend_light(daylight_factor, lightday, lightnight);
        }
-       /*// 0 <= daylight_factor <= 1000
-       // 0 <= return value <= 255
-       u8 getLightBlend(u32 daylight_factor, INodeDefManager *nodemgr)
-       {
-               u8 daylight = decode_light(getLight(LIGHTBANK_DAY, nodemgr));
-               u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT, nodemgr));
-               u8 mix = ((daylight_factor * daylight
-                       + (1000-daylight_factor) * nightlight)
-                       )/1000;
-               return mix;
-       }*/
-
-       // In mapnode.cpp
-#ifndef SERVER
-       /*
-               Get tile of a face of the node.
-               dir: direction of face
-               Returns: TileSpec. Can contain miscellaneous texture coordinates,
-                        which must be obeyed so that the texture atlas can be used.
-       */
-       TileSpec getTile(v3s16 dir, ITextureSource *tsrc,
-                       INodeDefManager *nodemgr) const;
-#endif
-       
-       /*
-               Gets mineral content of node, if there is any.
-               MINERAL_NONE if doesn't contain or isn't able to contain mineral.
-       */
-       u8 getMineral(INodeDefManager *nodemgr) const;
-       
+
+       u8 getFaceDir(INodeDefManager *nodemgr) const;
+       u8 getWallMounted(INodeDefManager *nodemgr) const;
+       v3s16 getWallMountedDir(INodeDefManager *nodemgr) const;
+
        /*
                Serialization functions
        */
 
        static u32 serializedLength(u8 version);
        void serialize(u8 *dest, u8 version);
-       void deSerialize(u8 *source, u8 version, INodeDefManager *nodemgr);
+       void deSerialize(u8 *source, u8 version);
        
+       // Serializes or deserializes a list of nodes in bulk format (first the
+       // content of all nodes, then the param1 of all nodes, then the param2
+       // of all nodes).
+       //   version = serialization version. Must be >= 22
+       //   content_width = the number of bytes of content per node
+       //   params_width = the number of bytes of params per node
+       //   compressed = true to zlib-compress output
+       static void serializeBulk(std::ostream &os, int version,
+                       const MapNode *nodes, u32 nodecount,
+                       u8 content_width, u8 params_width, bool compressed);
+       static void deSerializeBulk(std::istream &is, int version,
+                       MapNode *nodes, u32 nodecount,
+                       u8 content_width, u8 params_width, bool compressed);
+
+private:
+       // Deprecated serialization methods
+       void serialize_pre22(u8 *dest, u8 version);
+       void deSerialize_pre22(u8 *source, u8 version);
 };
 
+
+/*
+       MapNode helpers for mesh making stuff
+*/
+
+#ifndef SERVER
+
+/*
+       Nodes make a face if contents differ and solidness differs.
+       Return value:
+               0: No face
+               1: Face uses m1's content
+               2: Face uses m2's content
+       equivalent: Whether the blocks share the same face (eg. water and glass)
+*/
+u8 face_contents(content_t m1, content_t m2, bool *equivalent,
+               INodeDefManager *nodemgr);
+
 /*
        Gets lighting value at face of node
        
@@ -264,3 +262,6 @@ u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
 
 #endif
 
+
+#endif
+