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"
41 class IWritableTextureSource;
42 class IWritableItemDefManager;
43 class IWritableNodeDefManager;
44 //class IWritableCraftDefManager;
45 class ClientEnvironment;
46 struct MapDrawControl;
48 class ClientNotReadyException : public BaseException
51 ClientNotReadyException(const char *s):
56 struct QueuedMeshUpdate
60 bool ack_block_to_server;
67 A thread-safe queue of mesh update tasks
77 peer_id=0 adds with nobody to send to
79 void addBlock(v3s16 p, MeshMakeData *data,
80 bool ack_block_to_server, bool urgent);
82 // Returned pointer must be deleted
83 // Returns NULL if queue is empty
84 QueuedMeshUpdate * pop();
88 JMutexAutoLock lock(m_mutex);
89 return m_queue.size();
93 std::vector<QueuedMeshUpdate*> m_queue;
94 std::set<v3s16> m_urgents;
98 struct MeshUpdateResult
102 bool ack_block_to_server;
105 p(-1338,-1338,-1338),
107 ack_block_to_server(false)
112 class MeshUpdateThread : public SimpleThread
116 MeshUpdateThread(IGameDef *gamedef):
123 MeshUpdateQueue m_queue_in;
125 MutexedQueue<MeshUpdateResult> m_queue_out;
134 CE_PLAYER_FORCE_MOVE,
141 ClientEventType type;
153 bool set_camera_point_target;
154 f32 camera_point_target_x;
155 f32 camera_point_target_y;
156 f32 camera_point_target_z;
163 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
167 NOTE: Nothing is thread-safe here.
171 IrrlichtDevice *device,
172 const char *playername,
173 std::string password,
174 MapDrawControl &control,
175 IWritableTextureSource *tsrc,
176 IWritableItemDefManager *itemdef,
177 IWritableNodeDefManager *nodedef
182 The name of the local player should already be set when
183 calling this, as it is sent in the initialization.
185 void connect(Address address);
188 m_con.Connected() == true
189 AND m_server_ser_ver != SER_FMT_VER_INVALID
190 throws con::PeerNotFoundException if connection has been deleted,
193 bool connectedAndInitialized();
195 Stuff that references the environment is valid only as
196 long as this is not called. (eg. Players)
197 If this throws a PeerNotFoundException, the connection has
200 void step(float dtime);
202 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
203 // Returns true if something was received
204 bool AsyncProcessPacket();
205 bool AsyncProcessData();
206 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
208 void interact(u8 action, const PointedThing& pointed);
210 void sendSignNodeText(v3s16 p, std::string text);
211 void sendInventoryAction(InventoryAction *a);
212 void sendChatMessage(const std::wstring &message);
213 void sendChangePassword(const std::wstring oldpassword,
214 const std::wstring newpassword);
215 void sendDamage(u8 damage);
218 ClientEnvironment& getEnv()
221 // Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
222 void removeNode(v3s16 p);
223 void addNode(v3s16 p, MapNode n);
225 void setPlayerControl(PlayerControl &control);
227 void selectPlayerItem(u16 item);
228 u16 getPlayerItem() const
229 { return m_playeritem; }
231 // Returns true if the inventory of the local player has been
232 // updated from the server. If it is true, it is set to false.
233 bool getLocalInventoryUpdated();
234 // Copies the inventory of the local player to parameter
235 void getLocalInventory(Inventory &dst);
237 /* InventoryManager interface */
238 Inventory* getInventory(const InventoryLocation &loc);
239 void inventoryAction(InventoryAction *a);
241 // Gets closest object pointed by the shootline
242 // Returns NULL if not found
243 ClientActiveObject * getSelectedActiveObject(
245 v3f from_pos_f_on_map,
246 core::line3d<f32> shootline_on_map
249 // Prints a line or two of info
250 void printDebugInfo(std::ostream &os);
252 core::list<std::wstring> getConnectedPlayerNames();
254 float getAnimationTime();
257 void setCrack(int level, v3s16 pos);
264 return m_con.GetPeerAvgRTT(PEER_ID_SERVER);
265 } catch(con::PeerNotFoundException){
270 bool getChatMessage(std::wstring &message);
271 void typeChatMessage(const std::wstring& message);
273 u64 getMapSeed(){ return m_map_seed; }
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);
280 // Get event from queue. CE_NONE is returned if queue is empty.
281 ClientEvent getClientEvent();
284 { return m_access_denied; }
286 std::wstring accessDeniedReason()
287 { return m_access_denied_reason; }
289 float textureReceiveProgress()
290 { return m_texture_receive_progress; }
292 bool texturesReceived()
293 { return m_textures_received; }
294 bool itemdefReceived()
295 { return m_itemdef_received; }
296 bool nodedefReceived()
297 { return m_nodedef_received; }
299 void afterContentReceived();
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);
312 // Virtual methods from con::PeerHandler
313 void peerAdded(con::Peer *peer);
314 void deletingPeer(con::Peer *peer, bool timeout);
319 void sendPlayerPos();
320 // This sends the player's current name etc to the server
321 void sendPlayerInfo();
322 // Send the item number 'item' as player item to the server
323 void sendPlayerItem(u16 item);
325 float m_packetcounter_timer;
326 float m_connection_reinit_timer;
327 float m_avg_rtt_timer;
328 float m_playerpos_send_timer;
329 float m_ignore_damage_timer; // Used after server moves player
330 IntervalLimiter m_map_timer_and_unload_interval;
332 IWritableTextureSource *m_tsrc;
333 IWritableItemDefManager *m_itemdef;
334 IWritableNodeDefManager *m_nodedef;
335 MeshUpdateThread m_mesh_update_thread;
336 ClientEnvironment m_env;
337 con::Connection m_con;
338 IrrlichtDevice *m_device;
339 // Server serialization version
342 bool m_inventory_updated;
343 Inventory *m_inventory_from_server;
344 float m_inventory_from_server_age;
345 core::map<v3s16, bool> m_active_blocks;
346 PacketCounter m_packetcounter;
347 // Block mesh animation parameters
348 float m_animation_time;
351 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
353 //u32 m_daynight_ratio;
354 Queue<std::wstring> m_chat_queue;
355 // The seed returned by the server in TOCLIENT_INIT is stored here
357 std::string m_password;
358 bool m_access_denied;
359 std::wstring m_access_denied_reason;
360 Queue<ClientEvent> m_client_event_queue;
361 float m_texture_receive_progress;
362 bool m_textures_received;
363 bool m_itemdef_received;
364 bool m_nodedef_received;
365 friend class FarMesh;
367 // time_of_day speed approximation for old protocol
368 bool m_time_of_day_set;
369 float m_last_time_of_day_f;
370 float m_time_of_day_update_timer;
375 #endif // !CLIENT_HEADER