comment update
[oweals/minetest.git] / src / mapblockobject.h
index c479bc959bfa07d40b2c37de8f36c4fe96111678..804494715608312699518e90d77ee65e9c205ed9 100644 (file)
@@ -17,6 +17,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+// This file contains the DEPRECATED MapBlockObject system
+
 #ifndef MAPBLOCKOBJECT_HEADER
 #define MAPBLOCKOBJECT_HEADER
 
@@ -28,10 +30,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "constants.h"
 #include "debug.h"
 
-#define MAPBLOCKOBJECT_TYPE_TEST 0
-#define MAPBLOCKOBJECT_TYPE_TEST2 1
+#define MAPBLOCKOBJECT_TYPE_PLAYER 0
 #define MAPBLOCKOBJECT_TYPE_SIGN 2
 #define MAPBLOCKOBJECT_TYPE_RAT 3
+#define MAPBLOCKOBJECT_TYPE_ITEM 4
 // Used for handling selecting special stuff
 //#define MAPBLOCKOBJECT_TYPE_PSEUDO 1000
 
@@ -430,8 +432,9 @@ public:
                buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
                //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
                buf->getMaterial().setTexture
-                               (0, driver->getTexture("../data/sign.png"));
+                               (0, driver->getTexture(getTexturePath("sign.png").c_str()));
                buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
+               buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
                buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
                // Add to mesh
                mesh->addMeshBuffer(buf);
@@ -453,8 +456,9 @@ public:
                buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
                //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
                buf->getMaterial().setTexture
-                               (0, driver->getTexture("../data/sign_back.png"));
+                               (0, driver->getTexture(getTexturePath("sign_back.png").c_str()));
                buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
+               buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
                buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
                // Add to mesh
                mesh->addMeshBuffer(buf);
@@ -530,6 +534,11 @@ public:
                setBlockChanged();
        }
 
+       std::string getText()
+       {
+               return m_text;
+       }
+
        void setYaw(f32 yaw)
        {
                m_yaw = yaw;
@@ -554,7 +563,8 @@ public:
                                (-BS*0.3,-BS*.25,-BS*0.3, BS*0.3,BS*0.25,BS*0.3);
                m_selection_box = new core::aabbox3d<f32>
                                (-BS*0.3,-BS*.25,-BS*0.3, BS*0.3,BS*0.25,BS*0.3);
-
+               
+               m_yaw = 0;
                m_counter1 = 0;
                m_counter2 = 0;
                m_age = 0;
@@ -623,8 +633,8 @@ public:
                        m_counter2 -= dtime;
                        if(m_counter2 < 0.0)
                        {
-                               m_counter2 += (float)(rand()%100)/100*3.0;
-                               m_yaw += ((float)(rand()%200)-100)/100*180;
+                               m_counter2 += (float)(myrand()%100)/100*3.0;
+                               m_yaw += ((float)(myrand()%200)-100)/100*180;
                                m_yaw = wrapDegrees(m_yaw);
                        }
                }
@@ -710,10 +720,306 @@ protected:
 
        float m_counter1;
        float m_counter2;
-       v3f m_oldpos;
        float m_age;
 };
 
+/*
+       An object on the map that represents an inventory item
+*/
+
+class InventoryItem;
+
+class ItemObject : public MapBlockObject
+{
+public:
+       // The constructor of every MapBlockObject should be like this
+       ItemObject(MapBlock *block, s16 id, v3f pos):
+               MapBlockObject(block, id, pos),
+               m_node(NULL)
+       {
+               /*m_selection_box = new core::aabbox3d<f32>
+                               (-BS*0.4,-BS*0.5,-BS*0.4, BS*0.4,BS*0.5,BS*0.4);*/
+               m_selection_box = new core::aabbox3d<f32>
+                               (-BS/3,-BS/2,-BS/3, BS/3,-BS/2+BS*2/3,BS/3);
+               m_yaw = 0.0;
+       }
+       virtual ~ItemObject()
+       {
+               delete m_selection_box;
+       }
+       
+       /*
+               Implementation interface
+       */
+       virtual u16 getTypeId() const
+       {
+               return MAPBLOCKOBJECT_TYPE_ITEM;
+       }
+       virtual void serialize(std::ostream &os, u8 version)
+       {
+               serializeBase(os, version);
+               u8 buf[2];
+
+               // Write text length
+               writeU16(buf, m_itemstring.size());
+               os.write((char*)buf, 2);
+               
+               // Write text
+               os.write(m_itemstring.c_str(), m_itemstring.size());
+       }
+       virtual void update(std::istream &is, u8 version)
+       {
+               u8 buf[2];
+
+               // Read text length
+               is.read((char*)buf, 2);
+               u16 size = readU16(buf);
+
+               // Read text
+               std::string old_itemstring = m_itemstring;
+               m_itemstring.clear();
+               for(u16 i=0; i<size; i++)
+               {
+                       is.read((char*)buf, 1);
+                       m_itemstring += buf[0];
+               }
+               
+#ifndef SERVER
+               if(m_itemstring != old_itemstring && m_node)
+               {
+                       /*
+                               Update texture
+                       */
+                       video::ITexture *texture = getItemImage();
+                       scene::IMesh *mesh = m_node->getMesh();
+                       if(mesh->getMeshBufferCount() >= 1)
+                       {
+                               scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
+                               //dstream<<"Setting texture "<<texture<<std::endl;
+                               buf->getMaterial().setTexture(0, texture);
+                       }
+               }
+               
+               updateSceneNode();
+#endif
+       }
+
+       virtual bool serverStep(float dtime, u32 daynight_ratio)
+       {
+               return false;
+       }
+
+#ifndef SERVER
+       virtual void clientStep(float dtime)
+       {
+               m_yaw += dtime * 60;
+               if(m_yaw >= 360.)
+                       m_yaw -= 360.;
+
+               updateSceneNode();
+       }
+       
+       virtual void addToScene(scene::ISceneManager *smgr);
+       
+       virtual void removeFromScene()
+       {
+               if(m_node != NULL)
+               {
+                       m_node->remove();
+                       m_node = NULL;
+               }
+       }
+       virtual void updateLight(u8 light_at_pos)
+       {
+               if(m_node == NULL)
+                       return;
+
+               u8 li = decode_light(light_at_pos);
+               video::SColor color(255,li,li,li);
+
+               scene::IMesh *mesh = m_node->getMesh();
+               
+               u16 mc = mesh->getMeshBufferCount();
+               for(u16 j=0; j<mc; j++)
+               {
+                       scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
+                       video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
+                       u16 vc = buf->getVertexCount();
+                       for(u16 i=0; i<vc; i++)
+                       {
+                               vertices[i].Color = color;
+                       }
+               }
+       }
+#endif
+
+       virtual std::string infoText()
+       {
+               return std::string("\"") + m_itemstring + "\"";
+       }
+
+       virtual std::string getInventoryString()
+       {
+               return std::string("ItemObj ")+m_itemstring;
+       }
+
+       /*
+               Special methods
+       */
+
+       InventoryItem * createInventoryItem();
+       
+#ifndef SERVER
+       video::ITexture * getItemImage();
+
+       void updateSceneNode()
+       {
+               if(m_node != NULL)
+               {
+                       m_node->setPosition(getAbsolutePos());
+                       m_node->setRotation(v3f(0, m_yaw, 0));
+               }
+       }
+#endif
+
+       void setItemString(std::string inventorystring)
+       {
+               m_itemstring = inventorystring;
+               setBlockChanged();
+       }
+
+       std::string getItemString()
+       {
+               return m_itemstring;
+       }
+
+protected:
+       scene::IMeshSceneNode *m_node;
+       std::string m_itemstring;
+       f32 m_yaw;
+};
+
+/*
+       NOTE: Not used.
+*/
+class PlayerObject : public MovingObject
+{
+public:
+       PlayerObject(MapBlock *block, s16 id, v3f pos):
+               MovingObject(block, id, pos),
+               m_node(NULL),
+               m_yaw(0)
+       {
+               m_collision_box = new core::aabbox3d<f32>
+                               (-BS*0.3,-BS*.25,-BS*0.3, BS*0.3,BS*0.25,BS*0.3);
+               /*m_selection_box = new core::aabbox3d<f32>
+                               (-BS*0.3,-BS*.25,-BS*0.3, BS*0.3,BS*0.25,BS*0.3);*/
+       }
+       virtual ~PlayerObject()
+       {
+               if(m_collision_box)
+                       delete m_collision_box;
+               if(m_selection_box)
+                       delete m_selection_box;
+       }
+       
+       /*
+               Implementation interface
+       */
+       virtual u16 getTypeId() const
+       {
+               return MAPBLOCKOBJECT_TYPE_PLAYER;
+       }
+       virtual void serialize(std::ostream &os, u8 version)
+       {
+               // Object data is generated from actual player
+       }
+       virtual void update(std::istream &is, u8 version)
+       {
+               MovingObject::update(is, version);
+               u8 buf[2];
+               
+               // Read yaw * 10
+               is.read((char*)buf, 2);
+               s16 yaw_i = readS16(buf);
+               m_yaw = (f32)yaw_i / 10;
+
+               updateNodePos();
+       }
+
+       virtual bool serverStep(float dtime, u32 daynight_ratio)
+       {
+               // Player is handled elsewhere.
+               // Die.
+               //return true;
+               // Actually, fail very loudly:
+               assert(0);
+       }
+
+#ifndef SERVER
+       virtual void clientStep(float dtime)
+       {
+               MovingObject::simpleMove(dtime);
+
+               updateNodePos();
+       }
+       
+       virtual void addToScene(scene::ISceneManager *smgr);
+
+       virtual void removeFromScene()
+       {
+               if(m_node == NULL)
+                       return;
+
+               m_node->remove();
+               m_node = NULL;
+       }
+
+       virtual void updateLight(u8 light_at_pos)
+       {
+               if(m_node == NULL)
+                       return;
+
+               u8 li = decode_light(light_at_pos);
+               video::SColor color(255,li,li,li);
+
+               scene::IMesh *mesh = m_node->getMesh();
+               
+               u16 mc = mesh->getMeshBufferCount();
+               for(u16 j=0; j<mc; j++)
+               {
+                       scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
+                       video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
+                       u16 vc = buf->getVertexCount();
+                       for(u16 i=0; i<vc; i++)
+                       {
+                               vertices[i].Color = color;
+                       }
+               }
+       }
+       
+#endif
+
+       /*
+               Special methods
+       */
+       
+       void updateNodePos()
+       {
+               if(m_node == NULL)
+                       return;
+
+               m_node->setPosition(getAbsoluteShowPos());
+               m_node->setRotation(v3f(0, -m_yaw+180, 0));
+       }
+       
+protected:
+       scene::IMeshSceneNode *m_node;
+       float m_yaw;
+
+       v3f m_oldpos;
+};
+
 struct DistanceSortedObject
 {
        DistanceSortedObject(MapBlockObject *a_obj, f32 a_d)