]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client.h
Tune gravel and picks
[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 #include "utility.h" // For IntervalLimiter
32 #include "gamedef.h"
33
34 struct MeshMakeData;
35 class IGameDef;
36 class IWritableToolDefManager;
37 class IWritableNodeDefManager;
38
39 class ClientNotReadyException : public BaseException
40 {
41 public:
42         ClientNotReadyException(const char *s):
43                 BaseException(s)
44         {}
45 };
46
47 struct QueuedMeshUpdate
48 {
49         v3s16 p;
50         MeshMakeData *data;
51         bool ack_block_to_server;
52
53         QueuedMeshUpdate();
54         ~QueuedMeshUpdate();
55 };
56
57 /*
58         A thread-safe queue of mesh update tasks
59 */
60 class MeshUpdateQueue
61 {
62 public:
63         MeshUpdateQueue();
64
65         ~MeshUpdateQueue();
66         
67         /*
68                 peer_id=0 adds with nobody to send to
69         */
70         void addBlock(v3s16 p, MeshMakeData *data, bool ack_block_to_server);
71
72         // Returned pointer must be deleted
73         // Returns NULL if queue is empty
74         QueuedMeshUpdate * pop();
75
76         u32 size()
77         {
78                 JMutexAutoLock lock(m_mutex);
79                 return m_queue.size();
80         }
81         
82 private:
83         core::list<QueuedMeshUpdate*> m_queue;
84         JMutex m_mutex;
85 };
86
87 struct MeshUpdateResult
88 {
89         v3s16 p;
90         scene::SMesh *mesh;
91         bool ack_block_to_server;
92
93         MeshUpdateResult():
94                 p(-1338,-1338,-1338),
95                 mesh(NULL),
96                 ack_block_to_server(false)
97         {
98         }
99 };
100
101 class MeshUpdateThread : public SimpleThread
102 {
103 public:
104
105         MeshUpdateThread(IGameDef *gamedef):
106                 m_gamedef(gamedef)
107         {
108         }
109
110         void * Thread();
111
112         MeshUpdateQueue m_queue_in;
113
114         MutexedQueue<MeshUpdateResult> m_queue_out;
115
116         IGameDef *m_gamedef;
117 };
118
119 enum ClientEventType
120 {
121         CE_NONE,
122         CE_PLAYER_DAMAGE,
123         CE_PLAYER_FORCE_MOVE,
124         CE_DEATHSCREEN,
125 };
126
127 struct ClientEvent
128 {
129         ClientEventType type;
130         union{
131                 struct{
132                 } none;
133                 struct{
134                         u8 amount;
135                 } player_damage;
136                 struct{
137                         f32 pitch;
138                         f32 yaw;
139                 } player_force_move;
140                 struct{
141                         bool set_camera_point_target;
142                         f32 camera_point_target_x;
143                         f32 camera_point_target_y;
144                         f32 camera_point_target_z;
145                 } deathscreen;
146         };
147 };
148
149 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
150 {
151 public:
152         /*
153                 NOTE: Nothing is thread-safe here.
154         */
155
156         Client(
157                         IrrlichtDevice *device,
158                         const char *playername,
159                         std::string password,
160                         MapDrawControl &control,
161                         IWritableTextureSource *tsrc,
162                         IWritableToolDefManager *tooldef,
163                         IWritableNodeDefManager *nodedef
164         );
165         
166         ~Client();
167         /*
168                 The name of the local player should already be set when
169                 calling this, as it is sent in the initialization.
170         */
171         void connect(Address address);
172         /*
173                 returns true when
174                         m_con.Connected() == true
175                         AND m_server_ser_ver != SER_FMT_VER_INVALID
176                 throws con::PeerNotFoundException if connection has been deleted,
177                 eg. timed out.
178         */
179         bool connectedAndInitialized();
180         /*
181                 Stuff that references the environment is valid only as
182                 long as this is not called. (eg. Players)
183                 If this throws a PeerNotFoundException, the connection has
184                 timed out.
185         */
186         void step(float dtime);
187
188         // Called from updater thread
189         // Returns dtime
190         //float asyncStep();
191
192         void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
193         // Returns true if something was received
194         bool AsyncProcessPacket();
195         bool AsyncProcessData();
196         void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
197
198         // Pops out a packet from the packet queue
199         //IncomingPacket getPacket();
200
201         void groundAction(u8 action, v3s16 nodepos_undersurface,
202                         v3s16 nodepos_oversurface, u16 item);
203         void clickActiveObject(u8 button, u16 id, u16 item_i);
204
205         void sendSignNodeText(v3s16 p, std::string text);
206         void sendInventoryAction(InventoryAction *a);
207         void sendChatMessage(const std::wstring &message);
208         void sendChangePassword(const std::wstring oldpassword,
209                 const std::wstring newpassword);
210         void sendDamage(u8 damage);
211         void sendRespawn();
212         
213         // locks envlock
214         void removeNode(v3s16 p);
215         // locks envlock
216         void addNode(v3s16 p, MapNode n);
217         
218         void updateCamera(v3f pos, v3f dir, f32 fov);
219         
220         void renderPostFx();
221         
222         // Returns InvalidPositionException if not found
223         MapNode getNode(v3s16 p);
224         // Wrapper to Map
225         NodeMetadata* getNodeMetadata(v3s16 p);
226
227         LocalPlayer* getLocalPlayer();
228
229         void setPlayerControl(PlayerControl &control);
230
231         void selectPlayerItem(u16 item);
232
233         // Returns true if the inventory of the local player has been
234         // updated from the server. If it is true, it is set to false.
235         bool getLocalInventoryUpdated();
236         // Copies the inventory of the local player to parameter
237         void getLocalInventory(Inventory &dst);
238         
239         InventoryContext *getInventoryContext();
240
241         Inventory* getInventory(InventoryContext *c, std::string id);
242         void inventoryAction(InventoryAction *a);
243
244         // Gets closest object pointed by the shootline
245         // Returns NULL if not found
246         ClientActiveObject * getSelectedActiveObject(
247                         f32 max_d,
248                         v3f from_pos_f_on_map,
249                         core::line3d<f32> shootline_on_map
250         );
251
252         // Prints a line or two of info
253         void printDebugInfo(std::ostream &os);
254
255         u32 getDayNightRatio();
256
257         u16 getHP();
258
259         void setTempMod(v3s16 p, NodeMod mod);
260         void clearTempMod(v3s16 p);
261
262         float getAvgRtt()
263         {
264                 try{
265                         return m_con.GetPeerAvgRTT(PEER_ID_SERVER);
266                 } catch(con::PeerNotFoundException){
267                         return 1337;
268                 }
269         }
270
271         bool getChatMessage(std::wstring &message)
272         {
273                 if(m_chat_queue.size() == 0)
274                         return false;
275                 message = m_chat_queue.pop_front();
276                 return true;
277         }
278
279         void addChatMessage(const std::wstring &message)
280         {
281                 if (message[0] == L'/') {
282                         m_chat_queue.push_back(
283                                 (std::wstring)L"issued command: "+message);
284                         return;
285                 }
286
287                 //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
288                 LocalPlayer *player = m_env.getLocalPlayer();
289                 assert(player != NULL);
290                 std::wstring name = narrow_to_wide(player->getName());
291                 m_chat_queue.push_back(
292                                 (std::wstring)L"<"+name+L"> "+message);
293         }
294
295         u64 getMapSeed(){ return m_map_seed; }
296
297         void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false);
298         // Including blocks at appropriate edges
299         void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false);
300
301         // Get event from queue. CE_NONE is returned if queue is empty.
302         ClientEvent getClientEvent();
303         
304         inline bool accessDenied()
305         {
306                 return m_access_denied;
307         }
308
309         inline std::wstring accessDeniedReason()
310         {
311                 return m_access_denied_reason;
312         }
313
314         float getRTT(void);
315
316         // IGameDef interface
317         // Under envlock
318         virtual IToolDefManager* getToolDefManager();
319         virtual INodeDefManager* getNodeDefManager();
320         virtual ITextureSource* getTextureSource();
321
322 private:
323         
324         // Virtual methods from con::PeerHandler
325         void peerAdded(con::Peer *peer);
326         void deletingPeer(con::Peer *peer, bool timeout);
327         
328         void ReceiveAll();
329         void Receive();
330         
331         void sendPlayerPos();
332         // This sends the player's current name etc to the server
333         void sendPlayerInfo();
334         // Send the item number 'item' as player item to the server
335         void sendPlayerItem(u16 item);
336         
337         float m_packetcounter_timer;
338         float m_connection_reinit_timer;
339         float m_avg_rtt_timer;
340         float m_playerpos_send_timer;
341         float m_ignore_damage_timer; // Used after server moves player
342         IntervalLimiter m_map_timer_and_unload_interval;
343
344         IWritableTextureSource *m_tsrc;
345         IWritableToolDefManager *m_tooldef;
346         IWritableNodeDefManager *m_nodedef;
347         MeshUpdateThread m_mesh_update_thread;
348         ClientEnvironment m_env;
349         con::Connection m_con;
350         IrrlichtDevice *m_device;
351         // Server serialization version
352         u8 m_server_ser_ver;
353         // This is behind m_env_mutex.
354         bool m_inventory_updated;
355         core::map<v3s16, bool> m_active_blocks;
356         PacketCounter m_packetcounter;
357         // Received from the server. 0-23999
358         u32 m_time_of_day;
359         // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
360         //s32 m_daynight_i;
361         //u32 m_daynight_ratio;
362         Queue<std::wstring> m_chat_queue;
363         // The seed returned by the server in TOCLIENT_INIT is stored here
364         u64 m_map_seed;
365         std::string m_password;
366         bool m_access_denied;
367         std::wstring m_access_denied_reason;
368         InventoryContext m_inventory_context;
369         Queue<ClientEvent> m_client_event_queue;
370         friend class FarMesh;
371 };
372
373 #endif // !SERVER
374
375 #endif // !CLIENT_HEADER
376