]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client.h
commit before some more radical changes
[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         // Wrapper to Map
170         NodeMetadata* getNodeMetadataClone(v3s16 p);
171
172         v3f getPlayerPosition();
173
174         void setPlayerControl(PlayerControl &control);
175         
176         // Returns true if the inventory of the local player has been
177         // updated from the server. If it is true, it is set to false.
178         bool getLocalInventoryUpdated();
179         // Copies the inventory of the local player to parameter
180         void getLocalInventory(Inventory &dst);
181         
182         // Gets closest object pointed by the shootline
183         // Returns NULL if not found
184         MapBlockObject * getSelectedObject(
185                         f32 max_d,
186                         v3f from_pos_f_on_map,
187                         core::line3d<f32> shootline_on_map
188         );
189
190         // Prints a line or two of info
191         void printDebugInfo(std::ostream &os);
192
193         u32 getDayNightRatio();
194
195         //void updateSomeExpiredMeshes();
196         
197         void setTempMod(v3s16 p, NodeMod mod)
198         {
199                 JMutexAutoLock envlock(m_env_mutex);
200                 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
201
202                 core::map<v3s16, MapBlock*> affected_blocks;
203                 ((ClientMap&)m_env.getMap()).setTempMod(p, mod,
204                                 &affected_blocks);
205
206                 for(core::map<v3s16, MapBlock*>::Iterator
207                                 i = affected_blocks.getIterator();
208                                 i.atEnd() == false; i++)
209                 {
210                         i.getNode()->getValue()->updateMesh(m_env.getDayNightRatio());
211                 }
212         }
213         void clearTempMod(v3s16 p)
214         {
215                 JMutexAutoLock envlock(m_env_mutex);
216                 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
217
218                 core::map<v3s16, MapBlock*> affected_blocks;
219                 ((ClientMap&)m_env.getMap()).clearTempMod(p,
220                                 &affected_blocks);
221
222                 for(core::map<v3s16, MapBlock*>::Iterator
223                                 i = affected_blocks.getIterator();
224                                 i.atEnd() == false; i++)
225                 {
226                         i.getNode()->getValue()->updateMesh(m_env.getDayNightRatio());
227                 }
228         }
229
230         float getAvgRtt()
231         {
232                 JMutexAutoLock lock(m_con_mutex);
233                 con::Peer *peer = m_con.GetPeerNoEx(PEER_ID_SERVER);
234                 if(peer == NULL)
235                         return 0.0;
236                 return peer->avg_rtt;
237         }
238
239         bool getChatMessage(std::wstring &message)
240         {
241                 if(m_chat_queue.size() == 0)
242                         return false;
243                 message = m_chat_queue.pop_front();
244                 return true;
245         }
246
247         void addChatMessage(const std::wstring &message)
248         {
249                 JMutexAutoLock envlock(m_env_mutex);
250                 LocalPlayer *player = m_env.getLocalPlayer();
251                 assert(player != NULL);
252                 std::wstring name = narrow_to_wide(player->getName());
253                 m_chat_queue.push_back(
254                                 (std::wstring)L"<"+name+L"> "+message);
255         }
256
257         u64 getMapSeed(){ return m_map_seed; }
258
259 private:
260         
261         // Virtual methods from con::PeerHandler
262         void peerAdded(con::Peer *peer);
263         void deletingPeer(con::Peer *peer, bool timeout);
264         
265         void ReceiveAll();
266         void Receive();
267         
268         void sendPlayerPos();
269         // This sends the player's current name etc to the server
270         void sendPlayerInfo();
271         
272         float m_packetcounter_timer;
273         float m_delete_unused_sectors_timer;
274         float m_connection_reinit_timer;
275         float m_avg_rtt_timer;
276         float m_playerpos_send_timer;
277
278         ClientUpdateThread m_thread;
279         
280         // NOTE: If connection and environment are both to be locked,
281         // environment shall be locked first.
282
283         ClientEnvironment m_env;
284         JMutex m_env_mutex;
285         
286         con::Connection m_con;
287         JMutex m_con_mutex;
288
289         core::list<IncomingPacket> m_incoming_queue;
290         JMutex m_incoming_queue_mutex;
291
292         IrrlichtDevice *m_device;
293
294         v3f camera_position;
295         v3f camera_direction;
296         
297         // Server serialization version
298         u8 m_server_ser_ver;
299
300         float m_step_dtime;
301         JMutex m_step_dtime_mutex;
302
303         // This is behind m_env_mutex.
304         bool m_inventory_updated;
305
306         core::map<v3s16, bool> m_active_blocks;
307
308         PacketCounter m_packetcounter;
309         
310         // Received from the server. 0-23999
311         MutexedVariable<u32> m_time_of_day;
312         
313         // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
314         //s32 m_daynight_i;
315         //u32 m_daynight_ratio;
316
317         Queue<std::wstring> m_chat_queue;
318         
319         // The seed returned by the server in TOCLIENT_INIT is stored here
320         u64 m_map_seed;
321 };
322
323 #endif // !SERVER
324
325 #endif // !CLIENT_HEADER
326