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.
25 #include "connection.h"
26 #include "environment.h"
27 #include "common_irrlicht.h"
32 #include "clientobject.h"
33 #include "utility.h" // For IntervalLimiter
35 #include "inventorymanager.h"
37 #include "filecache.h"
42 class IWritableTextureSource;
43 class IWritableItemDefManager;
44 class IWritableNodeDefManager;
45 //class IWritableCraftDefManager;
46 class ClientEnvironment;
47 struct MapDrawControl;
50 class ClientNotReadyException : public BaseException
53 ClientNotReadyException(const char *s):
58 struct QueuedMeshUpdate
62 bool ack_block_to_server;
69 A thread-safe queue of mesh update tasks
79 peer_id=0 adds with nobody to send to
81 void addBlock(v3s16 p, MeshMakeData *data,
82 bool ack_block_to_server, bool urgent);
84 // Returned pointer must be deleted
85 // Returns NULL if queue is empty
86 QueuedMeshUpdate * pop();
90 JMutexAutoLock lock(m_mutex);
91 return m_queue.size();
95 std::vector<QueuedMeshUpdate*> m_queue;
96 std::set<v3s16> m_urgents;
100 struct MeshUpdateResult
104 bool ack_block_to_server;
107 p(-1338,-1338,-1338),
109 ack_block_to_server(false)
114 class MeshUpdateThread : public SimpleThread
118 MeshUpdateThread(IGameDef *gamedef):
125 MeshUpdateQueue m_queue_in;
127 MutexedQueue<MeshUpdateResult> m_queue_out;
136 CE_PLAYER_FORCE_MOVE,
143 ClientEventType type;
155 bool set_camera_point_target;
156 f32 camera_point_target_x;
157 f32 camera_point_target_y;
158 f32 camera_point_target_z;
165 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
169 NOTE: Nothing is thread-safe here.
173 IrrlichtDevice *device,
174 const char *playername,
175 std::string password,
176 MapDrawControl &control,
177 IWritableTextureSource *tsrc,
178 IWritableItemDefManager *itemdef,
179 IWritableNodeDefManager *nodedef,
180 ISoundManager *sound,
181 MtEventManager *event
186 The name of the local player should already be set when
187 calling this, as it is sent in the initialization.
189 void connect(Address address);
192 m_con.Connected() == true
193 AND m_server_ser_ver != SER_FMT_VER_INVALID
194 throws con::PeerNotFoundException if connection has been deleted,
197 bool connectedAndInitialized();
199 Stuff that references the environment is valid only as
200 long as this is not called. (eg. Players)
201 If this throws a PeerNotFoundException, the connection has
204 void step(float dtime);
206 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
207 // Returns true if something was received
208 bool AsyncProcessPacket();
209 bool AsyncProcessData();
210 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
212 void interact(u8 action, const PointedThing& pointed);
214 void sendSignNodeText(v3s16 p, std::string text);
215 void sendInventoryAction(InventoryAction *a);
216 void sendChatMessage(const std::wstring &message);
217 void sendChangePassword(const std::wstring oldpassword,
218 const std::wstring newpassword);
219 void sendDamage(u8 damage);
222 ClientEnvironment& getEnv()
225 // Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
226 void removeNode(v3s16 p);
227 void addNode(v3s16 p, MapNode n);
229 void setPlayerControl(PlayerControl &control);
231 void selectPlayerItem(u16 item);
232 u16 getPlayerItem() const
233 { return m_playeritem; }
235 // Returns true if the inventory of the local player has been
236 // updated from the server. If it is true, it is set to false.
237 bool getLocalInventoryUpdated();
238 // Copies the inventory of the local player to parameter
239 void getLocalInventory(Inventory &dst);
241 /* InventoryManager interface */
242 Inventory* getInventory(const InventoryLocation &loc);
243 void inventoryAction(InventoryAction *a);
245 // Gets closest object pointed by the shootline
246 // Returns NULL if not found
247 ClientActiveObject * getSelectedActiveObject(
249 v3f from_pos_f_on_map,
250 core::line3d<f32> shootline_on_map
253 // Prints a line or two of info
254 void printDebugInfo(std::ostream &os);
256 core::list<std::wstring> getConnectedPlayerNames();
258 float getAnimationTime();
261 void setCrack(int level, v3s16 pos);
268 return m_con.GetPeerAvgRTT(PEER_ID_SERVER);
269 } catch(con::PeerNotFoundException){
274 bool getChatMessage(std::wstring &message);
275 void typeChatMessage(const std::wstring& message);
277 u64 getMapSeed(){ return m_map_seed; }
279 void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
280 // Including blocks at appropriate edges
281 void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
282 void addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server=false, bool urgent=false);
284 // Get event from queue. CE_NONE is returned if queue is empty.
285 ClientEvent getClientEvent();
288 { return m_access_denied; }
290 std::wstring accessDeniedReason()
291 { return m_access_denied_reason; }
293 float textureReceiveProgress()
294 { return m_texture_receive_progress; }
296 bool texturesReceived()
297 { return m_textures_received; }
298 bool itemdefReceived()
299 { return m_itemdef_received; }
300 bool nodedefReceived()
301 { return m_nodedef_received; }
303 void afterContentReceived();
307 // IGameDef interface
308 virtual IItemDefManager* getItemDefManager();
309 virtual INodeDefManager* getNodeDefManager();
310 virtual ICraftDefManager* getCraftDefManager();
311 virtual ITextureSource* getTextureSource();
312 virtual u16 allocateUnknownNodeId(const std::string &name);
313 virtual ISoundManager* getSoundManager();
314 virtual MtEventManager* getEventManager();
318 // Virtual methods from con::PeerHandler
319 void peerAdded(con::Peer *peer);
320 void deletingPeer(con::Peer *peer, bool timeout);
325 void sendPlayerPos();
326 // This sends the player's current name etc to the server
327 void sendPlayerInfo();
328 // Send the item number 'item' as player item to the server
329 void sendPlayerItem(u16 item);
331 float m_packetcounter_timer;
332 float m_connection_reinit_timer;
333 float m_avg_rtt_timer;
334 float m_playerpos_send_timer;
335 float m_ignore_damage_timer; // Used after server moves player
336 IntervalLimiter m_map_timer_and_unload_interval;
338 IWritableTextureSource *m_tsrc;
339 IWritableItemDefManager *m_itemdef;
340 IWritableNodeDefManager *m_nodedef;
341 ISoundManager *m_sound;
342 MtEventManager *m_event;
344 MeshUpdateThread m_mesh_update_thread;
345 ClientEnvironment m_env;
346 con::Connection m_con;
347 IrrlichtDevice *m_device;
348 // Server serialization version
351 bool m_inventory_updated;
352 Inventory *m_inventory_from_server;
353 float m_inventory_from_server_age;
354 core::map<v3s16, bool> m_active_blocks;
355 PacketCounter m_packetcounter;
356 // Block mesh animation parameters
357 float m_animation_time;
360 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
362 //u32 m_daynight_ratio;
363 Queue<std::wstring> m_chat_queue;
364 // The seed returned by the server in TOCLIENT_INIT is stored here
366 std::string m_password;
367 bool m_access_denied;
368 std::wstring m_access_denied_reason;
369 Queue<ClientEvent> m_client_event_queue;
370 FileCache m_texture_cache;
371 // a map of the name and SHA1 checksum of each texture;
372 // cleared after content has been recieved
373 core::map<std::string, std::string> m_texture_name_sha1_map;
374 float m_texture_receive_progress;
375 bool m_textures_received;
376 bool m_itemdef_received;
377 bool m_nodedef_received;
378 friend class FarMesh;
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;
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;
397 #endif // !CLIENT_HEADER