3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
23 #include "connection.h"
24 #include "environment.h"
25 #include "irrlichttypes_extrabloated.h"
30 #include "clientobject.h"
32 #include "inventorymanager.h"
34 #include "filecache.h"
35 #include "localplayer.h"
37 #include "util/pointedthing.h"
42 class IWritableTextureSource;
43 class IWritableShaderSource;
44 class IWritableItemDefManager;
45 class IWritableNodeDefManager;
46 //class IWritableCraftDefManager;
47 class ClientEnvironment;
48 struct MapDrawControl;
51 class ClientNotReadyException : public BaseException
54 ClientNotReadyException(const char *s):
59 struct QueuedMeshUpdate
63 bool ack_block_to_server;
70 A thread-safe queue of mesh update tasks
80 peer_id=0 adds with nobody to send to
82 void addBlock(v3s16 p, MeshMakeData *data,
83 bool ack_block_to_server, bool urgent);
85 // Returned pointer must be deleted
86 // Returns NULL if queue is empty
87 QueuedMeshUpdate * pop();
91 JMutexAutoLock lock(m_mutex);
92 return m_queue.size();
96 std::vector<QueuedMeshUpdate*> m_queue;
97 std::set<v3s16> m_urgents;
101 struct MeshUpdateResult
105 bool ack_block_to_server;
108 p(-1338,-1338,-1338),
110 ack_block_to_server(false)
115 class MeshUpdateThread : public SimpleThread
119 MeshUpdateThread(IGameDef *gamedef):
126 MeshUpdateQueue m_queue_in;
128 MutexedQueue<MeshUpdateResult> m_queue_out;
133 class MediaFetchThread : public SimpleThread
137 MediaFetchThread(IGameDef *gamedef):
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;
155 CE_PLAYER_FORCE_MOVE,
163 ClientEventType type;
175 bool set_camera_point_target;
176 f32 camera_point_target_x;
177 f32 camera_point_target_y;
178 f32 camera_point_target_z;
181 std::string* formspec;
188 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
192 NOTE: Nothing is thread-safe here.
196 IrrlichtDevice *device,
197 const char *playername,
198 std::string password,
199 MapDrawControl &control,
200 IWritableTextureSource *tsrc,
201 IWritableShaderSource *shsrc,
202 IWritableItemDefManager *itemdef,
203 IWritableNodeDefManager *nodedef,
204 ISoundManager *sound,
205 MtEventManager *event
210 The name of the local player should already be set when
211 calling this, as it is sent in the initialization.
213 void connect(Address address);
216 m_con.Connected() == true
217 AND m_server_ser_ver != SER_FMT_VER_INVALID
218 throws con::PeerNotFoundException if connection has been deleted,
221 bool connectedAndInitialized();
223 Stuff that references the environment is valid only as
224 long as this is not called. (eg. Players)
225 If this throws a PeerNotFoundException, the connection has
228 void step(float dtime);
230 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
231 // Returns true if something was received
232 bool AsyncProcessPacket();
233 bool AsyncProcessData();
234 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
236 void interact(u8 action, const PointedThing& pointed);
238 void sendNodemetaFields(v3s16 p, const std::string &formname,
239 const std::map<std::string, std::string> &fields);
240 void sendInventoryFields(const std::string &formname,
241 const std::map<std::string, std::string> &fields);
242 void sendInventoryAction(InventoryAction *a);
243 void sendChatMessage(const std::wstring &message);
244 void sendChangePassword(const std::wstring oldpassword,
245 const std::wstring newpassword);
246 void sendDamage(u8 damage);
249 ClientEnvironment& getEnv()
252 // Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
253 void removeNode(v3s16 p);
254 void addNode(v3s16 p, MapNode n);
256 void setPlayerControl(PlayerControl &control);
258 void selectPlayerItem(u16 item);
259 u16 getPlayerItem() const
260 { return m_playeritem; }
262 // Returns true if the inventory of the local player has been
263 // updated from the server. If it is true, it is set to false.
264 bool getLocalInventoryUpdated();
265 // Copies the inventory of the local player to parameter
266 void getLocalInventory(Inventory &dst);
268 /* InventoryManager interface */
269 Inventory* getInventory(const InventoryLocation &loc);
270 void inventoryAction(InventoryAction *a);
272 // Gets closest object pointed by the shootline
273 // Returns NULL if not found
274 ClientActiveObject * getSelectedActiveObject(
276 v3f from_pos_f_on_map,
277 core::line3d<f32> shootline_on_map
280 // Prints a line or two of info
281 void printDebugInfo(std::ostream &os);
283 core::list<std::wstring> getConnectedPlayerNames();
285 float getAnimationTime();
288 void setCrack(int level, v3s16 pos);
292 bool checkPrivilege(const std::string &priv)
293 { return (m_privileges.count(priv) != 0); }
295 bool getChatMessage(std::wstring &message);
296 void typeChatMessage(const std::wstring& message);
298 u64 getMapSeed(){ return m_map_seed; }
300 void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
301 // Including blocks at appropriate edges
302 void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
303 void addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server=false, bool urgent=false);
305 // Get event from queue. CE_NONE is returned if queue is empty.
306 ClientEvent getClientEvent();
309 { return m_access_denied; }
311 std::wstring accessDeniedReason()
312 { return m_access_denied_reason; }
314 float mediaReceiveProgress()
316 if (!m_media_receive_started) return 0;
317 return 1.0 * m_media_received_count / m_media_count;
320 bool texturesReceived()
321 { return m_media_receive_started && m_media_received_count == m_media_count; }
322 bool itemdefReceived()
323 { return m_itemdef_received; }
324 bool nodedefReceived()
325 { return m_nodedef_received; }
327 void afterContentReceived();
331 // IGameDef interface
332 virtual IItemDefManager* getItemDefManager();
333 virtual INodeDefManager* getNodeDefManager();
334 virtual ICraftDefManager* getCraftDefManager();
335 virtual ITextureSource* getTextureSource();
336 virtual IShaderSource* getShaderSource();
337 virtual u16 allocateUnknownNodeId(const std::string &name);
338 virtual ISoundManager* getSoundManager();
339 virtual MtEventManager* getEventManager();
340 virtual bool checkLocalPrivilege(const std::string &priv)
341 { return checkPrivilege(priv); }
345 // Insert a media file appropriately into the appropriate manager
346 bool loadMedia(const std::string &data, const std::string &filename);
348 void request_media(const core::list<MediaRequest> &file_requests);
350 // Virtual methods from con::PeerHandler
351 void peerAdded(con::Peer *peer);
352 void deletingPeer(con::Peer *peer, bool timeout);
357 void sendPlayerPos();
358 // This sends the player's current name etc to the server
359 void sendPlayerInfo();
360 // Send the item number 'item' as player item to the server
361 void sendPlayerItem(u16 item);
363 float m_packetcounter_timer;
364 float m_connection_reinit_timer;
365 float m_avg_rtt_timer;
366 float m_playerpos_send_timer;
367 float m_ignore_damage_timer; // Used after server moves player
368 IntervalLimiter m_map_timer_and_unload_interval;
370 IWritableTextureSource *m_tsrc;
371 IWritableShaderSource *m_shsrc;
372 IWritableItemDefManager *m_itemdef;
373 IWritableNodeDefManager *m_nodedef;
374 ISoundManager *m_sound;
375 MtEventManager *m_event;
377 MeshUpdateThread m_mesh_update_thread;
378 core::list<MediaFetchThread*> m_media_fetch_threads;
379 ClientEnvironment m_env;
380 con::Connection m_con;
381 IrrlichtDevice *m_device;
382 // Server serialization version
385 bool m_inventory_updated;
386 Inventory *m_inventory_from_server;
387 float m_inventory_from_server_age;
388 core::map<v3s16, bool> m_active_blocks;
389 PacketCounter m_packetcounter;
390 // Block mesh animation parameters
391 float m_animation_time;
394 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
396 //u32 m_daynight_ratio;
397 Queue<std::wstring> m_chat_queue;
398 // The seed returned by the server in TOCLIENT_INIT is stored here
400 std::string m_password;
401 bool m_access_denied;
402 std::wstring m_access_denied_reason;
403 Queue<ClientEvent> m_client_event_queue;
404 FileCache m_media_cache;
405 // Mapping from media file name to SHA1 checksum
406 core::map<std::string, std::string> m_media_name_sha1_map;
407 bool m_media_receive_started;
409 u32 m_media_received_count;
410 bool m_itemdef_received;
411 bool m_nodedef_received;
412 friend class FarMesh;
414 // time_of_day speed approximation for old protocol
415 bool m_time_of_day_set;
416 float m_last_time_of_day_f;
417 float m_time_of_day_update_timer;
419 // An interval for generally sending object positions and stuff
420 float m_recommended_send_interval;
423 float m_removed_sounds_check_timer;
424 // Mapping from server sound ids to our sound ids
425 std::map<s32, int> m_sounds_server_to_client;
426 // And the other way!
427 std::map<int, s32> m_sounds_client_to_server;
428 // And relations to objects
429 std::map<int, u16> m_sounds_to_objects;
432 std::set<std::string> m_privileges;
434 // Detached inventories
436 std::map<std::string, Inventory*> m_detached_inventories;
439 #endif // !CLIENT_HEADER