]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client.h
Fix buttons not working for Lua-triggered formspecs
[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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 #include "connection.h"
24 #include "environment.h"
25 #include "irrlichttypes_extrabloated.h"
26 #include "jmutex.h"
27 #include <ostream>
28 #include <set>
29 #include <vector>
30 #include "clientobject.h"
31 #include "gamedef.h"
32 #include "inventorymanager.h"
33 #include "filesys.h"
34 #include "filecache.h"
35 #include "localplayer.h"
36 #include "server.h"
37 #include "util/pointedthing.h"
38
39 struct MeshMakeData;
40 class MapBlockMesh;
41 class IGameDef;
42 class IWritableTextureSource;
43 class IWritableShaderSource;
44 class IWritableItemDefManager;
45 class IWritableNodeDefManager;
46 //class IWritableCraftDefManager;
47 class ClientEnvironment;
48 struct MapDrawControl;
49 class MtEventManager;
50
51 class ClientNotReadyException : public BaseException
52 {
53 public:
54         ClientNotReadyException(const char *s):
55                 BaseException(s)
56         {}
57 };
58
59 struct QueuedMeshUpdate
60 {
61         v3s16 p;
62         MeshMakeData *data;
63         bool ack_block_to_server;
64
65         QueuedMeshUpdate();
66         ~QueuedMeshUpdate();
67 };
68
69 /*
70         A thread-safe queue of mesh update tasks
71 */
72 class MeshUpdateQueue
73 {
74 public:
75         MeshUpdateQueue();
76
77         ~MeshUpdateQueue();
78         
79         /*
80                 peer_id=0 adds with nobody to send to
81         */
82         void addBlock(v3s16 p, MeshMakeData *data,
83                         bool ack_block_to_server, bool urgent);
84
85         // Returned pointer must be deleted
86         // Returns NULL if queue is empty
87         QueuedMeshUpdate * pop();
88
89         u32 size()
90         {
91                 JMutexAutoLock lock(m_mutex);
92                 return m_queue.size();
93         }
94         
95 private:
96         std::vector<QueuedMeshUpdate*> m_queue;
97         std::set<v3s16> m_urgents;
98         JMutex m_mutex;
99 };
100
101 struct MeshUpdateResult
102 {
103         v3s16 p;
104         MapBlockMesh *mesh;
105         bool ack_block_to_server;
106
107         MeshUpdateResult():
108                 p(-1338,-1338,-1338),
109                 mesh(NULL),
110                 ack_block_to_server(false)
111         {
112         }
113 };
114
115 class MeshUpdateThread : public SimpleThread
116 {
117 public:
118
119         MeshUpdateThread(IGameDef *gamedef):
120                 m_gamedef(gamedef)
121         {
122         }
123
124         void * Thread();
125
126         MeshUpdateQueue m_queue_in;
127
128         MutexedQueue<MeshUpdateResult> m_queue_out;
129
130         IGameDef *m_gamedef;
131 };
132
133 class MediaFetchThread : public SimpleThread
134 {
135 public:
136
137         MediaFetchThread(IGameDef *gamedef):
138                 m_gamedef(gamedef)
139         {
140         }
141
142         void * Thread();
143
144         core::list<MediaRequest> m_file_requests;
145         MutexedQueue<std::pair<std::string, std::string> > m_file_data;
146         core::list<MediaRequest> m_failed;
147         std::string m_remote_url;
148         IGameDef *m_gamedef;
149 };
150
151 enum ClientEventType
152 {
153         CE_NONE,
154         CE_PLAYER_DAMAGE,
155         CE_PLAYER_FORCE_MOVE,
156         CE_DEATHSCREEN,
157         CE_TEXTURES_UPDATED,
158         CE_SHOW_FORMSPEC
159 };
160
161 struct ClientEvent
162 {
163         ClientEventType type;
164         union{
165                 struct{
166                 } none;
167                 struct{
168                         u8 amount;
169                 } player_damage;
170                 struct{
171                         f32 pitch;
172                         f32 yaw;
173                 } player_force_move;
174                 struct{
175                         bool set_camera_point_target;
176                         f32 camera_point_target_x;
177                         f32 camera_point_target_y;
178                         f32 camera_point_target_z;
179                 } deathscreen;
180                 struct{
181                         std::string* formspec;
182                         std::string* formname;
183                 } show_formspec;
184                 struct{
185                 } textures_updated;
186         };
187 };
188
189 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
190 {
191 public:
192         /*
193                 NOTE: Nothing is thread-safe here.
194         */
195
196         Client(
197                         IrrlichtDevice *device,
198                         const char *playername,
199                         std::string password,
200                         MapDrawControl &control,
201                         IWritableTextureSource *tsrc,
202                         IWritableShaderSource *shsrc,
203                         IWritableItemDefManager *itemdef,
204                         IWritableNodeDefManager *nodedef,
205                         ISoundManager *sound,
206                         MtEventManager *event
207         );
208         
209         ~Client();
210         /*
211                 The name of the local player should already be set when
212                 calling this, as it is sent in the initialization.
213         */
214         void connect(Address address);
215         /*
216                 returns true when
217                         m_con.Connected() == true
218                         AND m_server_ser_ver != SER_FMT_VER_INVALID
219                 throws con::PeerNotFoundException if connection has been deleted,
220                 eg. timed out.
221         */
222         bool connectedAndInitialized();
223         /*
224                 Stuff that references the environment is valid only as
225                 long as this is not called. (eg. Players)
226                 If this throws a PeerNotFoundException, the connection has
227                 timed out.
228         */
229         void step(float dtime);
230
231         void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
232         // Returns true if something was received
233         bool AsyncProcessPacket();
234         bool AsyncProcessData();
235         void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
236
237         void interact(u8 action, const PointedThing& pointed);
238
239         void sendNodemetaFields(v3s16 p, const std::string &formname,
240                         const std::map<std::string, std::string> &fields);
241         void sendInventoryFields(const std::string &formname,
242                         const std::map<std::string, std::string> &fields);
243         void sendInventoryAction(InventoryAction *a);
244         void sendChatMessage(const std::wstring &message);
245         void sendChangePassword(const std::wstring oldpassword,
246                         const std::wstring newpassword);
247         void sendDamage(u8 damage);
248         void sendRespawn();
249
250         ClientEnvironment& getEnv()
251         { return m_env; }
252         
253         // Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
254         void removeNode(v3s16 p);
255         void addNode(v3s16 p, MapNode n);
256         
257         void setPlayerControl(PlayerControl &control);
258
259         void selectPlayerItem(u16 item);
260         u16 getPlayerItem() const
261         { return m_playeritem; }
262
263         // Returns true if the inventory of the local player has been
264         // updated from the server. If it is true, it is set to false.
265         bool getLocalInventoryUpdated();
266         // Copies the inventory of the local player to parameter
267         void getLocalInventory(Inventory &dst);
268         
269         /* InventoryManager interface */
270         Inventory* getInventory(const InventoryLocation &loc);
271         void inventoryAction(InventoryAction *a);
272
273         // Gets closest object pointed by the shootline
274         // Returns NULL if not found
275         ClientActiveObject * getSelectedActiveObject(
276                         f32 max_d,
277                         v3f from_pos_f_on_map,
278                         core::line3d<f32> shootline_on_map
279         );
280
281         // Prints a line or two of info
282         void printDebugInfo(std::ostream &os);
283
284         core::list<std::wstring> getConnectedPlayerNames();
285
286         float getAnimationTime();
287
288         int getCrackLevel();
289         void setCrack(int level, v3s16 pos);
290
291         u16 getHP();
292
293         bool checkPrivilege(const std::string &priv)
294         { return (m_privileges.count(priv) != 0); }
295
296         bool getChatMessage(std::wstring &message);
297         void typeChatMessage(const std::wstring& message);
298
299         u64 getMapSeed(){ return m_map_seed; }
300
301         void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
302         // Including blocks at appropriate edges
303         void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
304         void addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server=false, bool urgent=false);
305
306         // Get event from queue. CE_NONE is returned if queue is empty.
307         ClientEvent getClientEvent();
308         
309         bool accessDenied()
310         { return m_access_denied; }
311
312         std::wstring accessDeniedReason()
313         { return m_access_denied_reason; }
314
315         float mediaReceiveProgress()
316         {
317                 if (!m_media_receive_started) return 0;
318                 return 1.0 * m_media_received_count / m_media_count;
319         }
320
321         bool texturesReceived()
322         { return m_media_receive_started && m_media_received_count == m_media_count; }
323         bool itemdefReceived()
324         { return m_itemdef_received; }
325         bool nodedefReceived()
326         { return m_nodedef_received; }
327         
328         void afterContentReceived();
329
330         float getRTT(void);
331
332         // IGameDef interface
333         virtual IItemDefManager* getItemDefManager();
334         virtual INodeDefManager* getNodeDefManager();
335         virtual ICraftDefManager* getCraftDefManager();
336         virtual ITextureSource* getTextureSource();
337         virtual IShaderSource* getShaderSource();
338         virtual u16 allocateUnknownNodeId(const std::string &name);
339         virtual ISoundManager* getSoundManager();
340         virtual MtEventManager* getEventManager();
341         virtual bool checkLocalPrivilege(const std::string &priv)
342         { return checkPrivilege(priv); }
343
344 private:
345         
346         // Insert a media file appropriately into the appropriate manager
347         bool loadMedia(const std::string &data, const std::string &filename);
348
349         void request_media(const core::list<MediaRequest> &file_requests);
350
351         // Virtual methods from con::PeerHandler
352         void peerAdded(con::Peer *peer);
353         void deletingPeer(con::Peer *peer, bool timeout);
354         
355         void ReceiveAll();
356         void Receive();
357         
358         void sendPlayerPos();
359         // This sends the player's current name etc to the server
360         void sendPlayerInfo();
361         // Send the item number 'item' as player item to the server
362         void sendPlayerItem(u16 item);
363         
364         float m_packetcounter_timer;
365         float m_connection_reinit_timer;
366         float m_avg_rtt_timer;
367         float m_playerpos_send_timer;
368         float m_ignore_damage_timer; // Used after server moves player
369         IntervalLimiter m_map_timer_and_unload_interval;
370
371         IWritableTextureSource *m_tsrc;
372         IWritableShaderSource *m_shsrc;
373         IWritableItemDefManager *m_itemdef;
374         IWritableNodeDefManager *m_nodedef;
375         ISoundManager *m_sound;
376         MtEventManager *m_event;
377
378         MeshUpdateThread m_mesh_update_thread;
379         core::list<MediaFetchThread*> m_media_fetch_threads;
380         ClientEnvironment m_env;
381         con::Connection m_con;
382         IrrlichtDevice *m_device;
383         // Server serialization version
384         u8 m_server_ser_ver;
385         u16 m_playeritem;
386         bool m_inventory_updated;
387         Inventory *m_inventory_from_server;
388         float m_inventory_from_server_age;
389         core::map<v3s16, bool> m_active_blocks;
390         PacketCounter m_packetcounter;
391         // Block mesh animation parameters
392         float m_animation_time;
393         int m_crack_level;
394         v3s16 m_crack_pos;
395         // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
396         //s32 m_daynight_i;
397         //u32 m_daynight_ratio;
398         Queue<std::wstring> m_chat_queue;
399         // The seed returned by the server in TOCLIENT_INIT is stored here
400         u64 m_map_seed;
401         std::string m_password;
402         bool m_access_denied;
403         std::wstring m_access_denied_reason;
404         Queue<ClientEvent> m_client_event_queue;
405         FileCache m_media_cache;
406         // Mapping from media file name to SHA1 checksum
407         core::map<std::string, std::string> m_media_name_sha1_map;
408         bool m_media_receive_started;
409         u32 m_media_count;
410         u32 m_media_received_count;
411         bool m_itemdef_received;
412         bool m_nodedef_received;
413         friend class FarMesh;
414
415         // time_of_day speed approximation for old protocol
416         bool m_time_of_day_set;
417         float m_last_time_of_day_f;
418         float m_time_of_day_update_timer;
419
420         // An interval for generally sending object positions and stuff
421         float m_recommended_send_interval;
422
423         // Sounds
424         float m_removed_sounds_check_timer;
425         // Mapping from server sound ids to our sound ids
426         std::map<s32, int> m_sounds_server_to_client;
427         // And the other way!
428         std::map<int, s32> m_sounds_client_to_server;
429         // And relations to objects
430         std::map<int, u16> m_sounds_to_objects;
431
432         // Privileges
433         std::set<std::string> m_privileges;
434
435         // Detached inventories
436         // key = name
437         std::map<std::string, Inventory*> m_detached_inventories;
438 };
439
440 #endif // !CLIENT_HEADER
441