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