]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/client.h
comments
[dragonfireclient.git] / src / client.h
index 65b02a0e2245c0f8d979dff58745085032ff1492..00fd3a5edf72476735083fd7111e3a0bd8b841de 100644 (file)
@@ -20,6 +20,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #ifndef CLIENT_HEADER
 #define CLIENT_HEADER
 
+#ifndef SERVER
+
 #include "connection.h"
 #include "environment.h"
 #include "common_irrlicht.h"
@@ -36,35 +38,19 @@ class ClientNotReadyException : public BaseException
 
 class Client;
 
-class ClientUpdateThread : public JThread
+class ClientUpdateThread : public SimpleThread
 {
-       bool run;
-       JMutex run_mutex;
-
        Client *m_client;
 
 public:
 
-       ClientUpdateThread(Client *client) : JThread(), run(true), m_client(client)
+       ClientUpdateThread(Client *client):
+                       SimpleThread(),
+                       m_client(client)
        {
-               run_mutex.Init();
        }
 
        void * Thread();
-
-       bool getRun()
-       {
-               run_mutex.Lock();
-               bool run_cached = run;
-               run_mutex.Unlock();
-               return run_cached;
-       }
-       void setRun(bool a_run)
-       {
-               run_mutex.Lock();
-               run = a_run;
-               run_mutex.Unlock();
-       }
 };
 
 struct IncomingPacket
@@ -98,6 +84,7 @@ struct IncomingPacket
                        if(*m_refcount == 0){
                                if(m_data != NULL)
                                        delete[] m_data;
+                               delete m_refcount;
                        }
                }
        }
@@ -114,47 +101,18 @@ struct IncomingPacket
        s32 *m_refcount;
 };
 
-// TODO: Remove this. It is not used as supposed.
-class LazyMeshUpdater
-{
-public:
-       LazyMeshUpdater(Environment *env)
-       {
-               m_env = env;
-       }
-       ~LazyMeshUpdater()
-       {
-               /*
-                       TODO: This could be optimized. It will currently
-                       double-update some blocks.
-               */
-               for(core::map<v3s16, bool>::Iterator
-                               i = m_blocks.getIterator();
-                               i.atEnd() == false; i++)
-               {
-                       v3s16 p = i.getNode()->getKey();
-                       m_env->getMap().updateMeshes(p);
-               }
-               m_blocks.clear();
-       }
-       void add(v3s16 p)
-       {
-               m_blocks.insert(p, true);
-       }
-private:
-       Environment *m_env;
-       core::map<v3s16, bool> m_blocks;
-};
-
 class Client : public con::PeerHandler
 {
 public:
        /*
                NOTE: Every public method should be thread-safe
        */
-       Client(IrrlichtDevice *device, video::SMaterial *materials,
-                       float delete_unused_sectors_timeout,
-                       const char *playername);
+       Client(
+                       IrrlichtDevice *device,
+                       const char *playername,
+                       MapDrawControl &control
+                       );
+       
        ~Client();
        /*
                The name of the local player should already be set when
@@ -183,39 +141,36 @@ class Client : public con::PeerHandler
 
        void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
        // Returns true if something was received
-       bool AsyncProcessPacket(LazyMeshUpdater &mesh_updater);
+       bool AsyncProcessPacket();
        bool AsyncProcessData();
        void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
 
-       //TODO: Remove
-       bool isFetchingBlocks();
-
        // Pops out a packet from the packet queue
        IncomingPacket getPacket();
 
-       /*void removeNode(v3s16 nodepos);
-       void addNodeFromInventory(v3s16 nodepos, u16 i);*/
-       void clickGround(u8 button, v3s16 nodepos_undersurface,
+       void groundAction(u8 action, v3s16 nodepos_undersurface,
                        v3s16 nodepos_oversurface, u16 item);
        void clickObject(u8 button, v3s16 blockpos, s16 id, u16 item);
-       void release(u8 button);
 
        void sendSignText(v3s16 blockpos, s16 id, std::string text);
+       void sendInventoryAction(InventoryAction *a);
+       void sendChatMessage(const std::wstring &message);
+       
+       // locks envlock
+       void removeNode(v3s16 p);
+       // locks envlock
+       void addNode(v3s16 p, MapNode n);
        
        void updateCamera(v3f pos, v3f dir);
        
        // Returns InvalidPositionException if not found
        MapNode getNode(v3s16 p);
        // Returns InvalidPositionException if not found
-       //f32 getGroundHeight(v2s16 p);
+       //void setNode(v3s16 p, MapNode n);
+
        // Returns InvalidPositionException if not found
-       bool isNodeUnderground(v3s16 p);
+       //f32 getGroundHeight(v2s16 p);
 
-       // Note: The players should not be exposed outside
-       // Return value is valid until client is destroyed
-       //Player * getLocalPlayer();
-       // Return value is valid until step()
-       //core::list<Player*> getPlayers();
        v3f getPlayerPosition();
 
        void setPlayerControl(PlayerControl &control);
@@ -225,8 +180,6 @@ class Client : public con::PeerHandler
        bool getLocalInventoryUpdated();
        // Copies the inventory of the local player to parameter
        void getLocalInventory(Inventory &dst);
-       // TODO: Functions for sending inventory editing commands to
-       //       server
        
        // Gets closest object pointed by the shootline
        // Returns NULL if not found
@@ -238,7 +191,93 @@ class Client : public con::PeerHandler
 
        // Prints a line or two of info
        void printDebugInfo(std::ostream &os);
+
+       //s32 getDayNightIndex();
+       u32 getDayNightRatio();
+
+       //void updateSomeExpiredMeshes();
        
+       void setTempMod(v3s16 p, NodeMod mod)
+       {
+               JMutexAutoLock envlock(m_env_mutex);
+               assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
+
+               core::map<v3s16, MapBlock*> affected_blocks;
+               ((ClientMap&)m_env.getMap()).setTempMod(p, mod,
+                               &affected_blocks);
+
+               for(core::map<v3s16, MapBlock*>::Iterator
+                               i = affected_blocks.getIterator();
+                               i.atEnd() == false; i++)
+               {
+                       i.getNode()->getValue()->updateMesh(m_env.getDayNightRatio());
+               }
+       }
+       void clearTempMod(v3s16 p)
+       {
+               JMutexAutoLock envlock(m_env_mutex);
+               assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
+
+               core::map<v3s16, MapBlock*> affected_blocks;
+               ((ClientMap&)m_env.getMap()).clearTempMod(p,
+                               &affected_blocks);
+
+               for(core::map<v3s16, MapBlock*>::Iterator
+                               i = affected_blocks.getIterator();
+                               i.atEnd() == false; i++)
+               {
+                       i.getNode()->getValue()->updateMesh(m_env.getDayNightRatio());
+               }
+       }
+
+#if 0
+       void setTempMod(v3s16 p, NodeMod mod)
+       {
+               JMutexAutoLock envlock(m_env_mutex);
+               assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
+               bool changed = false;
+               v3s16 blockpos = ((ClientMap&)m_env.getMap()).setTempMod(p, mod, &changed);
+               if(changed)
+                       m_env.getMap().updateMeshes(blockpos, m_env.getDayNightRatio());
+       }
+       void clearTempMod(v3s16 p)
+       {
+               JMutexAutoLock envlock(m_env_mutex);
+               assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
+               bool changed = false;
+               v3s16 blockpos = ((ClientMap&)m_env.getMap()).clearTempMod(p, &changed);
+               if(changed)
+                       m_env.getMap().updateMeshes(blockpos, m_env.getDayNightRatio());
+       }
+#endif
+
+       float getAvgRtt()
+       {
+               JMutexAutoLock lock(m_con_mutex);
+               con::Peer *peer = m_con.GetPeerNoEx(PEER_ID_SERVER);
+               if(peer == NULL)
+                       return 0.0;
+               return peer->avg_rtt;
+       }
+
+       bool getChatMessage(std::wstring &message)
+       {
+               if(m_chat_queue.size() == 0)
+                       return false;
+               message = m_chat_queue.pop_front();
+               return true;
+       }
+
+       void addChatMessage(const std::wstring &message)
+       {
+               JMutexAutoLock envlock(m_env_mutex);
+               LocalPlayer *player = m_env.getLocalPlayer();
+               assert(player != NULL);
+               std::wstring name = narrow_to_wide(player->getName());
+               m_chat_queue.push_back(
+                               (std::wstring)L"<"+name+L"> "+message);
+       }
+
 private:
        
        // Virtual methods from con::PeerHandler
@@ -251,6 +290,12 @@ class Client : public con::PeerHandler
        void sendPlayerPos();
        // This sends the player's current name etc to the server
        void sendPlayerInfo();
+       
+       float m_packetcounter_timer;
+       float m_delete_unused_sectors_timer;
+       float m_connection_reinit_timer;
+       float m_avg_rtt_timer;
+       float m_playerpos_send_timer;
 
        ClientUpdateThread m_thread;
        
@@ -280,15 +325,24 @@ class Client : public con::PeerHandler
        float m_step_dtime;
        JMutex m_step_dtime_mutex;
 
-       float m_delete_unused_sectors_timeout;
-       
        // This is behind m_env_mutex.
        bool m_inventory_updated;
 
        core::map<v3s16, bool> m_active_blocks;
 
        PacketCounter m_packetcounter;
+       
+       // Received from the server. 0-23999
+       MutexedVariable<u32> m_time_of_day;
+       
+       // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
+       //s32 m_daynight_i;
+       //u32 m_daynight_ratio;
+
+       Queue<std::wstring> m_chat_queue;
 };
 
-#endif
+#endif // !SERVER
+
+#endif // !CLIENT_HEADER