Reworked texture, material, mineral and whatever handling
[oweals/minetest.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
31 class ClientNotReadyException : public BaseException
32 {
33 public:
34         ClientNotReadyException(const char *s):
35                 BaseException(s)
36         {}
37 };
38
39 class Client;
40
41 class ClientUpdateThread : public SimpleThread
42 {
43         Client *m_client;
44
45 public:
46
47         ClientUpdateThread(Client *client):
48                         SimpleThread(),
49                         m_client(client)
50         {
51         }
52
53         void * Thread();
54 };
55
56 struct IncomingPacket
57 {
58         IncomingPacket()
59         {
60                 m_data = NULL;
61                 m_datalen = 0;
62                 m_refcount = NULL;
63         }
64         IncomingPacket(const IncomingPacket &a)
65         {
66                 m_data = a.m_data;
67                 m_datalen = a.m_datalen;
68                 m_refcount = a.m_refcount;
69                 if(m_refcount != NULL)
70                         (*m_refcount)++;
71         }
72         IncomingPacket(u8 *data, u32 datalen)
73         {
74                 m_data = new u8[datalen];
75                 memcpy(m_data, data, datalen);
76                 m_datalen = datalen;
77                 m_refcount = new s32(1);
78         }
79         ~IncomingPacket()
80         {
81                 if(m_refcount != NULL){
82                         assert(*m_refcount > 0);
83                         (*m_refcount)--;
84                         if(*m_refcount == 0){
85                                 if(m_data != NULL)
86                                         delete[] m_data;
87                                 delete m_refcount;
88                         }
89                 }
90         }
91         /*IncomingPacket & operator=(IncomingPacket a)
92         {
93                 m_data = a.m_data;
94                 m_datalen = a.m_datalen;
95                 m_refcount = a.m_refcount;
96                 (*m_refcount)++;
97                 return *this;
98         }*/
99         u8 *m_data;
100         u32 m_datalen;
101         s32 *m_refcount;
102 };
103
104 class Client : public con::PeerHandler
105 {
106 public:
107         /*
108                 NOTE: Every public method should be thread-safe
109         */
110         Client(
111                         IrrlichtDevice *device,
112                         const char *playername,
113                         MapDrawControl &control
114                         );
115         
116         ~Client();
117         /*
118                 The name of the local player should already be set when
119                 calling this, as it is sent in the initialization.
120         */
121         void connect(Address address);
122         /*
123                 returns true when
124                         m_con.Connected() == true
125                         AND m_server_ser_ver != SER_FMT_VER_INVALID
126                 throws con::PeerNotFoundException if connection has been deleted,
127                 eg. timed out.
128         */
129         bool connectedAndInitialized();
130         /*
131                 Stuff that references the environment is valid only as
132                 long as this is not called. (eg. Players)
133                 If this throws a PeerNotFoundException, the connection has
134                 timed out.
135         */
136         void step(float dtime);
137
138         // Called from updater thread
139         // Returns dtime
140         float asyncStep();
141
142         void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
143         // Returns true if something was received
144         bool AsyncProcessPacket();
145         bool AsyncProcessData();
146         void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
147
148         // Pops out a packet from the packet queue
149         IncomingPacket getPacket();
150
151         void groundAction(u8 action, v3s16 nodepos_undersurface,
152                         v3s16 nodepos_oversurface, u16 item);
153         void clickObject(u8 button, v3s16 blockpos, s16 id, u16 item);
154
155         void sendSignText(v3s16 blockpos, s16 id, std::string text);
156         void sendInventoryAction(InventoryAction *a);
157         void sendChatMessage(const std::wstring &message);
158         
159         // locks envlock
160         void removeNode(v3s16 p);
161         // locks envlock
162         void addNode(v3s16 p, MapNode n);
163         
164         void updateCamera(v3f pos, v3f dir);
165         
166         // Returns InvalidPositionException if not found
167         MapNode getNode(v3s16 p);
168         // Returns InvalidPositionException if not found
169         //void setNode(v3s16 p, MapNode n);
170
171         // Returns InvalidPositionException if not found
172         //f32 getGroundHeight(v2s16 p);
173
174         v3f getPlayerPosition();
175
176         void setPlayerControl(PlayerControl &control);
177         
178         // Returns true if the inventory of the local player has been
179         // updated from the server. If it is true, it is set to false.
180         bool getLocalInventoryUpdated();
181         // Copies the inventory of the local player to parameter
182         void getLocalInventory(Inventory &dst);
183         
184         // Gets closest object pointed by the shootline
185         // Returns NULL if not found
186         MapBlockObject * getSelectedObject(
187                         f32 max_d,
188                         v3f from_pos_f_on_map,
189                         core::line3d<f32> shootline_on_map
190         );
191
192         // Prints a line or two of info
193         void printDebugInfo(std::ostream &os);
194
195         //s32 getDayNightIndex();
196         u32 getDayNightRatio();
197
198         //void updateSomeExpiredMeshes();
199         
200         void setTempMod(v3s16 p, NodeMod mod)
201         {
202                 JMutexAutoLock envlock(m_env_mutex);
203                 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
204
205                 core::map<v3s16, MapBlock*> affected_blocks;
206                 ((ClientMap&)m_env.getMap()).setTempMod(p, mod,
207                                 &affected_blocks);
208
209                 for(core::map<v3s16, MapBlock*>::Iterator
210                                 i = affected_blocks.getIterator();
211                                 i.atEnd() == false; i++)
212                 {
213                         i.getNode()->getValue()->updateMesh(m_env.getDayNightRatio());
214                 }
215         }
216         void clearTempMod(v3s16 p)
217         {
218                 JMutexAutoLock envlock(m_env_mutex);
219                 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
220
221                 core::map<v3s16, MapBlock*> affected_blocks;
222                 ((ClientMap&)m_env.getMap()).clearTempMod(p,
223                                 &affected_blocks);
224
225                 for(core::map<v3s16, MapBlock*>::Iterator
226                                 i = affected_blocks.getIterator();
227                                 i.atEnd() == false; i++)
228                 {
229                         i.getNode()->getValue()->updateMesh(m_env.getDayNightRatio());
230                 }
231         }
232
233 #if 0
234         void setTempMod(v3s16 p, NodeMod mod)
235         {
236                 JMutexAutoLock envlock(m_env_mutex);
237                 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
238                 bool changed = false;
239                 v3s16 blockpos = ((ClientMap&)m_env.getMap()).setTempMod(p, mod, &changed);
240                 if(changed)
241                         m_env.getMap().updateMeshes(blockpos, m_env.getDayNightRatio());
242         }
243         void clearTempMod(v3s16 p)
244         {
245                 JMutexAutoLock envlock(m_env_mutex);
246                 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
247                 bool changed = false;
248                 v3s16 blockpos = ((ClientMap&)m_env.getMap()).clearTempMod(p, &changed);
249                 if(changed)
250                         m_env.getMap().updateMeshes(blockpos, m_env.getDayNightRatio());
251         }
252 #endif
253
254         float getAvgRtt()
255         {
256                 JMutexAutoLock lock(m_con_mutex);
257                 con::Peer *peer = m_con.GetPeerNoEx(PEER_ID_SERVER);
258                 if(peer == NULL)
259                         return 0.0;
260                 return peer->avg_rtt;
261         }
262
263         bool getChatMessage(std::wstring &message)
264         {
265                 if(m_chat_queue.size() == 0)
266                         return false;
267                 message = m_chat_queue.pop_front();
268                 return true;
269         }
270
271         void addChatMessage(const std::wstring &message)
272         {
273                 JMutexAutoLock envlock(m_env_mutex);
274                 LocalPlayer *player = m_env.getLocalPlayer();
275                 assert(player != NULL);
276                 std::wstring name = narrow_to_wide(player->getName());
277                 m_chat_queue.push_back(
278                                 (std::wstring)L"<"+name+L"> "+message);
279         }
280
281 private:
282         
283         // Virtual methods from con::PeerHandler
284         void peerAdded(con::Peer *peer);
285         void deletingPeer(con::Peer *peer, bool timeout);
286         
287         void ReceiveAll();
288         void Receive();
289         
290         void sendPlayerPos();
291         // This sends the player's current name etc to the server
292         void sendPlayerInfo();
293         
294         float m_packetcounter_timer;
295         float m_delete_unused_sectors_timer;
296         float m_connection_reinit_timer;
297         float m_avg_rtt_timer;
298         float m_playerpos_send_timer;
299
300         ClientUpdateThread m_thread;
301         
302         // NOTE: If connection and environment are both to be locked,
303         // environment shall be locked first.
304
305         Environment m_env;
306         JMutex m_env_mutex;
307         
308         con::Connection m_con;
309         JMutex m_con_mutex;
310
311         /*core::map<v3s16, float> m_fetchblock_history;
312         JMutex m_fetchblock_mutex;*/
313
314         core::list<IncomingPacket> m_incoming_queue;
315         JMutex m_incoming_queue_mutex;
316
317         IrrlichtDevice *m_device;
318
319         v3f camera_position;
320         v3f camera_direction;
321         
322         // Server serialization version
323         u8 m_server_ser_ver;
324
325         float m_step_dtime;
326         JMutex m_step_dtime_mutex;
327
328         // This is behind m_env_mutex.
329         bool m_inventory_updated;
330
331         core::map<v3s16, bool> m_active_blocks;
332
333         PacketCounter m_packetcounter;
334         
335         // Received from the server. 0-23999
336         MutexedVariable<u32> m_time_of_day;
337         
338         // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
339         //s32 m_daynight_i;
340         //u32 m_daynight_ratio;
341
342         Queue<std::wstring> m_chat_queue;
343 };
344
345 #endif // !SERVER
346
347 #endif // !CLIENT_HEADER
348