]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client.h
92375974ab659937f8a7d5908b56b719adf16a7b
[dragonfireclient.git] / src / client.h
1 #ifndef CLIENT_HEADER
2 #define CLIENT_HEADER
3
4 #include "connection.h"
5 #include "environment.h"
6 #include "common_irrlicht.h"
7 #include "jmutex.h"
8 #include <ostream>
9
10 class ClientNotReadyException : public BaseException
11 {
12 public:
13         ClientNotReadyException(const char *s):
14                 BaseException(s)
15         {}
16 };
17
18 class Client;
19
20 class ClientUpdateThread : public JThread
21 {
22         bool run;
23         JMutex run_mutex;
24
25         Client *m_client;
26
27 public:
28
29         ClientUpdateThread(Client *client) : JThread(), run(true), m_client(client)
30         {
31                 run_mutex.Init();
32         }
33
34         void * Thread();
35
36         bool getRun()
37         {
38                 run_mutex.Lock();
39                 bool run_cached = run;
40                 run_mutex.Unlock();
41                 return run_cached;
42         }
43         void setRun(bool a_run)
44         {
45                 run_mutex.Lock();
46                 run = a_run;
47                 run_mutex.Unlock();
48         }
49 };
50
51 struct IncomingPacket
52 {
53         IncomingPacket()
54         {
55                 m_data = NULL;
56                 m_datalen = 0;
57                 m_refcount = NULL;
58         }
59         IncomingPacket(const IncomingPacket &a)
60         {
61                 m_data = a.m_data;
62                 m_datalen = a.m_datalen;
63                 m_refcount = a.m_refcount;
64                 if(m_refcount != NULL)
65                         (*m_refcount)++;
66         }
67         IncomingPacket(u8 *data, u32 datalen)
68         {
69                 m_data = new u8[datalen];
70                 memcpy(m_data, data, datalen);
71                 m_datalen = datalen;
72                 m_refcount = new s32(1);
73         }
74         ~IncomingPacket()
75         {
76                 if(m_refcount != NULL){
77                         assert(*m_refcount > 0);
78                         (*m_refcount)--;
79                         if(*m_refcount == 0){
80                                 if(m_data != NULL)
81                                         delete[] m_data;
82                         }
83                 }
84         }
85         /*IncomingPacket & operator=(IncomingPacket a)
86         {
87                 m_data = a.m_data;
88                 m_datalen = a.m_datalen;
89                 m_refcount = a.m_refcount;
90                 (*m_refcount)++;
91                 return *this;
92         }*/
93         u8 *m_data;
94         u32 m_datalen;
95         s32 *m_refcount;
96 };
97
98 class LazyMeshUpdater
99 {
100 public:
101         LazyMeshUpdater(Environment *env)
102         {
103                 m_env = env;
104         }
105         ~LazyMeshUpdater()
106         {
107                 /*
108                         TODO: This could be optimized. It will currently
109                         double-update some blocks.
110                 */
111                 for(core::map<v3s16, bool>::Iterator
112                                 i = m_blocks.getIterator();
113                                 i.atEnd() == false; i++)
114                 {
115                         v3s16 p = i.getNode()->getKey();
116                         m_env->getMap().updateMeshes(p);
117                 }
118                 m_blocks.clear();
119         }
120         void add(v3s16 p)
121         {
122                 m_blocks.insert(p, true);
123         }
124 private:
125         Environment *m_env;
126         core::map<v3s16, bool> m_blocks;
127 };
128
129 class Client : public con::PeerHandler
130 {
131 public:
132         /*
133                 NOTE: Every public method should be thread-safe
134         */
135         Client(IrrlichtDevice *device, video::SMaterial *materials,
136                         float delete_unused_sectors_timeout,
137                         const char *playername);
138         ~Client();
139         /*
140                 The name of the local player should already be set when
141                 calling this, as it is sent in the initialization.
142         */
143         void connect(Address address);
144         /*
145                 returns true when
146                         m_con.Connected() == true
147                         AND m_server_ser_ver != SER_FMT_VER_INVALID
148                 throws con::PeerNotFoundException if connection has been deleted,
149                 eg. timed out.
150         */
151         bool connectedAndInitialized();
152         /*
153                 Stuff that references the environment is valid only as
154                 long as this is not called. (eg. Players)
155                 If this throws a PeerNotFoundException, the connection has
156                 timed out.
157         */
158         void step(float dtime);
159
160         // Called from updater thread
161         // Returns dtime
162         float asyncStep();
163
164         void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
165         // Returns true if something was received
166         bool AsyncProcessPacket(LazyMeshUpdater &mesh_updater);
167         bool AsyncProcessData();
168         void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
169
170         //TODO: Remove
171         bool isFetchingBlocks();
172
173         // Pops out a packet from the packet queue
174         IncomingPacket getPacket();
175
176         /*void removeNode(v3s16 nodepos);
177         void addNodeFromInventory(v3s16 nodepos, u16 i);*/
178         void clickGround(u8 button, v3s16 nodepos_undersurface,
179                         v3s16 nodepos_oversurface, u16 item);
180         void clickObject(u8 button, v3s16 blockpos, s16 id, u16 item);
181         void release(u8 button);
182
183         void sendSignText(v3s16 blockpos, s16 id, std::string text);
184         
185         void updateCamera(v3f pos, v3f dir);
186         
187         // Returns InvalidPositionException if not found
188         MapNode getNode(v3s16 p);
189         // Returns InvalidPositionException if not found
190         //f32 getGroundHeight(v2s16 p);
191         // Returns InvalidPositionException if not found
192         bool isNodeUnderground(v3s16 p);
193
194         // Note: The players should not be exposed outside
195         // Return value is valid until client is destroyed
196         //Player * getLocalPlayer();
197         // Return value is valid until step()
198         //core::list<Player*> getPlayers();
199         v3f getPlayerPosition();
200
201         void setPlayerControl(PlayerControl &control);
202         
203         // Returns true if the inventory of the local player has been
204         // updated from the server. If it is true, it is set to false.
205         bool getLocalInventoryUpdated();
206         // Copies the inventory of the local player to parameter
207         void getLocalInventory(Inventory &dst);
208         // TODO: Functions for sending inventory editing commands to
209         //       server
210         
211         // Gets closest object pointed by the shootline
212         // Returns NULL if not found
213         MapBlockObject * getSelectedObject(
214                         f32 max_d,
215                         v3f from_pos_f_on_map,
216                         core::line3d<f32> shootline_on_map
217         );
218
219         // Prints a line or two of info
220         void printDebugInfo(std::ostream &os);
221         
222 private:
223         
224         // Virtual methods from con::PeerHandler
225         void peerAdded(con::Peer *peer);
226         void deletingPeer(con::Peer *peer, bool timeout);
227         
228         void ReceiveAll();
229         void Receive();
230         
231         void sendPlayerPos();
232         // This sends the player's current name etc to the server
233         void sendPlayerInfo();
234
235         ClientUpdateThread m_thread;
236         
237         // NOTE: If connection and environment are both to be locked,
238         // environment shall be locked first.
239
240         Environment m_env;
241         JMutex m_env_mutex;
242         
243         con::Connection m_con;
244         JMutex m_con_mutex;
245
246         /*core::map<v3s16, float> m_fetchblock_history;
247         JMutex m_fetchblock_mutex;*/
248
249         core::list<IncomingPacket> m_incoming_queue;
250         JMutex m_incoming_queue_mutex;
251
252         IrrlichtDevice *m_device;
253
254         v3f camera_position;
255         v3f camera_direction;
256         
257         // Server serialization version
258         u8 m_server_ser_ver;
259
260         float m_step_dtime;
261         JMutex m_step_dtime_mutex;
262
263         float m_delete_unused_sectors_timeout;
264         
265         // This is behind m_env_mutex.
266         bool m_inventory_updated;
267
268         core::map<v3s16, bool> m_active_blocks;
269
270         PacketCounter m_packetcounter;
271 };
272
273 #endif
274