]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/mapblockobject.h
make it slightly less annoying :o)
[dragonfireclient.git] / src / mapblockobject.h
index 30fa797f4928253cbd34bf40c22e1150c449c53c..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
 
@@ -31,6 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #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
 
@@ -429,7 +432,7 @@ class SignObject : public MapBlockObject
                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;
@@ -453,7 +456,7 @@ class SignObject : public MapBlockObject
                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;
@@ -531,6 +534,11 @@ class SignObject : public MapBlockObject
                setBlockChanged();
        }
 
+       std::string getText()
+       {
+               return m_text;
+       }
+
        void setYaw(f32 yaw)
        {
                m_yaw = yaw;
@@ -555,7 +563,8 @@ class RatObject : public MovingObject
                                (-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;
@@ -624,8 +633,8 @@ class RatObject : public MovingObject
                        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);
                        }
                }
@@ -714,6 +723,182 @@ class RatObject : public MovingObject
        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.
 */
@@ -722,7 +907,8 @@ class PlayerObject : public MovingObject
 public:
        PlayerObject(MapBlock *block, s16 id, v3f pos):
                MovingObject(block, id, pos),
-               m_node(NULL)
+               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);