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 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.
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.
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.
23 #include "connection.h"
24 #include "environment.h"
25 #include "common_irrlicht.h"
30 #include "clientobject.h"
31 #include "utility.h" // For IntervalLimiter
33 #include "inventorymanager.h"
35 #include "filecache.h"
36 #include "localplayer.h"
41 class IWritableTextureSource;
42 class IWritableItemDefManager;
43 class IWritableNodeDefManager;
44 //class IWritableCraftDefManager;
45 class ClientEnvironment;
46 struct MapDrawControl;
49 class ClientNotReadyException : public BaseException
52 ClientNotReadyException(const char *s):
57 struct QueuedMeshUpdate
61 bool ack_block_to_server;
68 A thread-safe queue of mesh update tasks
78 peer_id=0 adds with nobody to send to
80 void addBlock(v3s16 p, MeshMakeData *data,
81 bool ack_block_to_server, bool urgent);
83 // Returned pointer must be deleted
84 // Returns NULL if queue is empty
85 QueuedMeshUpdate * pop();
89 JMutexAutoLock lock(m_mutex);
90 return m_queue.size();
94 std::vector<QueuedMeshUpdate*> m_queue;
95 std::set<v3s16> m_urgents;
99 struct MeshUpdateResult
103 bool ack_block_to_server;
106 p(-1338,-1338,-1338),
108 ack_block_to_server(false)
113 class MeshUpdateThread : public SimpleThread
117 MeshUpdateThread(IGameDef *gamedef):
124 MeshUpdateQueue m_queue_in;
126 MutexedQueue<MeshUpdateResult> m_queue_out;
135 CE_PLAYER_FORCE_MOVE,
142 ClientEventType type;
154 bool set_camera_point_target;
155 f32 camera_point_target_x;
156 f32 camera_point_target_y;
157 f32 camera_point_target_z;
164 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
168 NOTE: Nothing is thread-safe here.
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
185 The name of the local player should already be set when
186 calling this, as it is sent in the initialization.
188 void connect(Address address);
191 m_con.Connected() == true
192 AND m_server_ser_ver != SER_FMT_VER_INVALID
193 throws con::PeerNotFoundException if connection has been deleted,
196 bool connectedAndInitialized();
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
203 void step(float dtime);
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);
211 void interact(u8 action, const PointedThing& pointed);
213 void sendSignNodeText(v3s16 p, std::string text);
214 void sendInventoryAction(InventoryAction *a);
215 void sendChatMessage(const std::wstring &message);
216 void sendChangePassword(const std::wstring oldpassword,
217 const std::wstring newpassword);
218 void sendDamage(u8 damage);
221 ClientEnvironment& getEnv()
224 // Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
225 void removeNode(v3s16 p);
226 void addNode(v3s16 p, MapNode n);
228 void setPlayerControl(PlayerControl &control);
230 void selectPlayerItem(u16 item);
231 u16 getPlayerItem() const
232 { return m_playeritem; }
234 // Returns true if the inventory of the local player has been
235 // updated from the server. If it is true, it is set to false.
236 bool getLocalInventoryUpdated();
237 // Copies the inventory of the local player to parameter
238 void getLocalInventory(Inventory &dst);
240 /* InventoryManager interface */
241 Inventory* getInventory(const InventoryLocation &loc);
242 void inventoryAction(InventoryAction *a);
244 // Gets closest object pointed by the shootline
245 // Returns NULL if not found
246 ClientActiveObject * getSelectedActiveObject(
248 v3f from_pos_f_on_map,
249 core::line3d<f32> shootline_on_map
252 // Prints a line or two of info
253 void printDebugInfo(std::ostream &os);
255 core::list<std::wstring> getConnectedPlayerNames();
257 float getAnimationTime();
260 void setCrack(int level, v3s16 pos);
264 bool checkPrivilege(const std::string &priv)
265 { return (m_privileges.count(priv) != 0); }
267 bool getChatMessage(std::wstring &message);
268 void typeChatMessage(const std::wstring& message);
270 u64 getMapSeed(){ return m_map_seed; }
272 void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
273 // Including blocks at appropriate edges
274 void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
275 void addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server=false, bool urgent=false);
277 // Get event from queue. CE_NONE is returned if queue is empty.
278 ClientEvent getClientEvent();
281 { return m_access_denied; }
283 std::wstring accessDeniedReason()
284 { return m_access_denied_reason; }
286 float mediaReceiveProgress()
287 { return m_media_receive_progress; }
289 bool texturesReceived()
290 { return m_media_received; }
291 bool itemdefReceived()
292 { return m_itemdef_received; }
293 bool nodedefReceived()
294 { return m_nodedef_received; }
296 void afterContentReceived();
300 // IGameDef interface
301 virtual IItemDefManager* getItemDefManager();
302 virtual INodeDefManager* getNodeDefManager();
303 virtual ICraftDefManager* getCraftDefManager();
304 virtual ITextureSource* getTextureSource();
305 virtual u16 allocateUnknownNodeId(const std::string &name);
306 virtual ISoundManager* getSoundManager();
307 virtual MtEventManager* getEventManager();
308 virtual bool checkLocalPrivilege(const std::string &priv)
309 { return checkPrivilege(priv); }
313 // Insert a media file appropriately into the appropriate manager
314 bool loadMedia(const std::string &data, const std::string &filename);
316 // Virtual methods from con::PeerHandler
317 void peerAdded(con::Peer *peer);
318 void deletingPeer(con::Peer *peer, bool timeout);
323 void sendPlayerPos();
324 // This sends the player's current name etc to the server
325 void sendPlayerInfo();
326 // Send the item number 'item' as player item to the server
327 void sendPlayerItem(u16 item);
329 float m_packetcounter_timer;
330 float m_connection_reinit_timer;
331 float m_avg_rtt_timer;
332 float m_playerpos_send_timer;
333 float m_ignore_damage_timer; // Used after server moves player
334 IntervalLimiter m_map_timer_and_unload_interval;
336 IWritableTextureSource *m_tsrc;
337 IWritableItemDefManager *m_itemdef;
338 IWritableNodeDefManager *m_nodedef;
339 ISoundManager *m_sound;
340 MtEventManager *m_event;
342 MeshUpdateThread m_mesh_update_thread;
343 ClientEnvironment m_env;
344 con::Connection m_con;
345 IrrlichtDevice *m_device;
346 // Server serialization version
349 bool m_inventory_updated;
350 Inventory *m_inventory_from_server;
351 float m_inventory_from_server_age;
352 core::map<v3s16, bool> m_active_blocks;
353 PacketCounter m_packetcounter;
354 // Block mesh animation parameters
355 float m_animation_time;
358 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
360 //u32 m_daynight_ratio;
361 Queue<std::wstring> m_chat_queue;
362 // The seed returned by the server in TOCLIENT_INIT is stored here
364 std::string m_password;
365 bool m_access_denied;
366 std::wstring m_access_denied_reason;
367 Queue<ClientEvent> m_client_event_queue;
368 FileCache m_media_cache;
369 // Mapping from media file name to SHA1 checksum
370 core::map<std::string, std::string> m_media_name_sha1_map;
371 float m_media_receive_progress;
372 bool m_media_received;
373 bool m_itemdef_received;
374 bool m_nodedef_received;
375 friend class FarMesh;
377 // time_of_day speed approximation for old protocol
378 bool m_time_of_day_set;
379 float m_last_time_of_day_f;
380 float m_time_of_day_update_timer;
383 float m_removed_sounds_check_timer;
384 // Mapping from server sound ids to our sound ids
385 std::map<s32, int> m_sounds_server_to_client;
386 // And the other way!
387 std::map<int, s32> m_sounds_client_to_server;
388 // And relations to objects
389 std::map<int, u16> m_sounds_to_objects;
392 std::set<std::string> m_privileges;
395 #endif // !CLIENT_HEADER