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