]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client.h
Temporary commit; lots of test code and stuff
[dragonfireclient.git] / src / client.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef CLIENT_HEADER
21 #define CLIENT_HEADER
22
23 #ifndef SERVER
24
25 #include "connection.h"
26 #include "environment.h"
27 #include "common_irrlicht.h"
28 #include "jmutex.h"
29 #include <ostream>
30 #include "clientobject.h"
31
32 class ClientNotReadyException : public BaseException
33 {
34 public:
35         ClientNotReadyException(const char *s):
36                 BaseException(s)
37         {}
38 };
39
40 class Client;
41
42 class ClientUpdateThread : public SimpleThread
43 {
44         Client *m_client;
45
46 public:
47
48         ClientUpdateThread(Client *client):
49                         SimpleThread(),
50                         m_client(client)
51         {
52         }
53
54         void * Thread();
55 };
56
57 struct IncomingPacket
58 {
59         IncomingPacket()
60         {
61                 m_data = NULL;
62                 m_datalen = 0;
63                 m_refcount = NULL;
64         }
65         IncomingPacket(const IncomingPacket &a)
66         {
67                 m_data = a.m_data;
68                 m_datalen = a.m_datalen;
69                 m_refcount = a.m_refcount;
70                 if(m_refcount != NULL)
71                         (*m_refcount)++;
72         }
73         IncomingPacket(u8 *data, u32 datalen)
74         {
75                 m_data = new u8[datalen];
76                 memcpy(m_data, data, datalen);
77                 m_datalen = datalen;
78                 m_refcount = new s32(1);
79         }
80         ~IncomingPacket()
81         {
82                 if(m_refcount != NULL){
83                         assert(*m_refcount > 0);
84                         (*m_refcount)--;
85                         if(*m_refcount == 0){
86                                 if(m_data != NULL)
87                                         delete[] m_data;
88                                 delete m_refcount;
89                         }
90                 }
91         }
92         /*IncomingPacket & operator=(IncomingPacket a)
93         {
94                 m_data = a.m_data;
95                 m_datalen = a.m_datalen;
96                 m_refcount = a.m_refcount;
97                 (*m_refcount)++;
98                 return *this;
99         }*/
100         u8 *m_data;
101         u32 m_datalen;
102         s32 *m_refcount;
103 };
104
105 class Client : public con::PeerHandler
106 {
107 public:
108         /*
109                 NOTE: Every public method should be thread-safe
110         */
111         Client(
112                         IrrlichtDevice *device,
113                         const char *playername,
114                         MapDrawControl &control
115                         );
116         
117         ~Client();
118         /*
119                 The name of the local player should already be set when
120                 calling this, as it is sent in the initialization.
121         */
122         void connect(Address address);
123         /*
124                 returns true when
125                         m_con.Connected() == true
126                         AND m_server_ser_ver != SER_FMT_VER_INVALID
127                 throws con::PeerNotFoundException if connection has been deleted,
128                 eg. timed out.
129         */
130         bool connectedAndInitialized();
131         /*
132                 Stuff that references the environment is valid only as
133                 long as this is not called. (eg. Players)
134                 If this throws a PeerNotFoundException, the connection has
135                 timed out.
136         */
137         void step(float dtime);
138
139         // Called from updater thread
140         // Returns dtime
141         float asyncStep();
142
143         void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
144         // Returns true if something was received
145         bool AsyncProcessPacket();
146         bool AsyncProcessData();
147         void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
148
149         // Pops out a packet from the packet queue
150         IncomingPacket getPacket();
151
152         void groundAction(u8 action, v3s16 nodepos_undersurface,
153                         v3s16 nodepos_oversurface, u16 item);
154         void clickObject(u8 button, v3s16 blockpos, s16 id, u16 item);
155
156         void sendSignText(v3s16 blockpos, s16 id, std::string text);
157         void sendInventoryAction(InventoryAction *a);
158         void sendChatMessage(const std::wstring &message);
159         
160         // locks envlock
161         void removeNode(v3s16 p);
162         // locks envlock
163         void addNode(v3s16 p, MapNode n);
164         
165         void updateCamera(v3f pos, v3f dir);
166         
167         // Returns InvalidPositionException if not found
168         MapNode getNode(v3s16 p);
169
170         v3f getPlayerPosition();
171
172         void setPlayerControl(PlayerControl &control);
173         
174         // Returns true if the inventory of the local player has been
175         // updated from the server. If it is true, it is set to false.
176         bool getLocalInventoryUpdated();
177         // Copies the inventory of the local player to parameter
178         void getLocalInventory(Inventory &dst);
179         
180         // Gets closest object pointed by the shootline
181         // Returns NULL if not found
182         MapBlockObject * getSelectedObject(
183                         f32 max_d,
184                         v3f from_pos_f_on_map,
185                         core::line3d<f32> shootline_on_map
186         );
187
188         // Prints a line or two of info
189         void printDebugInfo(std::ostream &os);
190
191         u32 getDayNightRatio();
192
193         //void updateSomeExpiredMeshes();
194         
195         void setTempMod(v3s16 p, NodeMod mod)
196         {
197                 JMutexAutoLock envlock(m_env_mutex);
198                 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
199
200                 core::map<v3s16, MapBlock*> affected_blocks;
201                 ((ClientMap&)m_env.getMap()).setTempMod(p, mod,
202                                 &affected_blocks);
203
204                 for(core::map<v3s16, MapBlock*>::Iterator
205                                 i = affected_blocks.getIterator();
206                                 i.atEnd() == false; i++)
207                 {
208                         i.getNode()->getValue()->updateMesh(m_env.getDayNightRatio());
209                 }
210         }
211         void clearTempMod(v3s16 p)
212         {
213                 JMutexAutoLock envlock(m_env_mutex);
214                 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
215
216                 core::map<v3s16, MapBlock*> affected_blocks;
217                 ((ClientMap&)m_env.getMap()).clearTempMod(p,
218                                 &affected_blocks);
219
220                 for(core::map<v3s16, MapBlock*>::Iterator
221                                 i = affected_blocks.getIterator();
222                                 i.atEnd() == false; i++)
223                 {
224                         i.getNode()->getValue()->updateMesh(m_env.getDayNightRatio());
225                 }
226         }
227
228         float getAvgRtt()
229         {
230                 JMutexAutoLock lock(m_con_mutex);
231                 con::Peer *peer = m_con.GetPeerNoEx(PEER_ID_SERVER);
232                 if(peer == NULL)
233                         return 0.0;
234                 return peer->avg_rtt;
235         }
236
237         bool getChatMessage(std::wstring &message)
238         {
239                 if(m_chat_queue.size() == 0)
240                         return false;
241                 message = m_chat_queue.pop_front();
242                 return true;
243         }
244
245         void addChatMessage(const std::wstring &message)
246         {
247                 JMutexAutoLock envlock(m_env_mutex);
248                 LocalPlayer *player = m_env.getLocalPlayer();
249                 assert(player != NULL);
250                 std::wstring name = narrow_to_wide(player->getName());
251                 m_chat_queue.push_back(
252                                 (std::wstring)L"<"+name+L"> "+message);
253         }
254
255 private:
256         
257         // Virtual methods from con::PeerHandler
258         void peerAdded(con::Peer *peer);
259         void deletingPeer(con::Peer *peer, bool timeout);
260         
261         void ReceiveAll();
262         void Receive();
263         
264         void sendPlayerPos();
265         // This sends the player's current name etc to the server
266         void sendPlayerInfo();
267         
268         float m_packetcounter_timer;
269         float m_delete_unused_sectors_timer;
270         float m_connection_reinit_timer;
271         float m_avg_rtt_timer;
272         float m_playerpos_send_timer;
273
274         ClientUpdateThread m_thread;
275         
276         // NOTE: If connection and environment are both to be locked,
277         // environment shall be locked first.
278
279         ClientEnvironment m_env;
280         JMutex m_env_mutex;
281         
282         con::Connection m_con;
283         JMutex m_con_mutex;
284
285         core::list<IncomingPacket> m_incoming_queue;
286         JMutex m_incoming_queue_mutex;
287
288         IrrlichtDevice *m_device;
289
290         v3f camera_position;
291         v3f camera_direction;
292         
293         // Server serialization version
294         u8 m_server_ser_ver;
295
296         float m_step_dtime;
297         JMutex m_step_dtime_mutex;
298
299         // This is behind m_env_mutex.
300         bool m_inventory_updated;
301
302         core::map<v3s16, bool> m_active_blocks;
303
304         PacketCounter m_packetcounter;
305         
306         // Received from the server. 0-23999
307         MutexedVariable<u32> m_time_of_day;
308         
309         // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
310         //s32 m_daynight_i;
311         //u32 m_daynight_ratio;
312
313         Queue<std::wstring> m_chat_queue;
314 };
315
316 #endif // !SERVER
317
318 #endif // !CLIENT_HEADER
319