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;
47 class ClientNotReadyException : public BaseException
50 ClientNotReadyException(const char *s):
55 struct QueuedMeshUpdate
59 bool ack_block_to_server;
66 A thread-safe queue of mesh update tasks
76 peer_id=0 adds with nobody to send to
78 void addBlock(v3s16 p, MeshMakeData *data,
79 bool ack_block_to_server, bool urgent);
81 // Returned pointer must be deleted
82 // Returns NULL if queue is empty
83 QueuedMeshUpdate * pop();
87 JMutexAutoLock lock(m_mutex);
88 return m_queue.size();
92 std::vector<QueuedMeshUpdate*> m_queue;
93 std::set<v3s16> m_urgents;
97 struct MeshUpdateResult
101 bool ack_block_to_server;
104 p(-1338,-1338,-1338),
106 ack_block_to_server(false)
111 class MeshUpdateThread : public SimpleThread
115 MeshUpdateThread(IGameDef *gamedef):
122 MeshUpdateQueue m_queue_in;
124 MutexedQueue<MeshUpdateResult> m_queue_out;
133 CE_PLAYER_FORCE_MOVE,
140 ClientEventType type;
152 bool set_camera_point_target;
153 f32 camera_point_target_x;
154 f32 camera_point_target_y;
155 f32 camera_point_target_z;
162 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
166 NOTE: Nothing is thread-safe here.
170 IrrlichtDevice *device,
171 const char *playername,
172 std::string password,
173 MapDrawControl &control,
174 IWritableTextureSource *tsrc,
175 IWritableItemDefManager *itemdef,
176 IWritableNodeDefManager *nodedef
181 The name of the local player should already be set when
182 calling this, as it is sent in the initialization.
184 void connect(Address address);
187 m_con.Connected() == true
188 AND m_server_ser_ver != SER_FMT_VER_INVALID
189 throws con::PeerNotFoundException if connection has been deleted,
192 bool connectedAndInitialized();
194 Stuff that references the environment is valid only as
195 long as this is not called. (eg. Players)
196 If this throws a PeerNotFoundException, the connection has
199 void step(float dtime);
201 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
202 // Returns true if something was received
203 bool AsyncProcessPacket();
204 bool AsyncProcessData();
205 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
207 void interact(u8 action, const PointedThing& pointed);
209 void sendSignNodeText(v3s16 p, std::string text);
210 void sendInventoryAction(InventoryAction *a);
211 void sendChatMessage(const std::wstring &message);
212 void sendChangePassword(const std::wstring oldpassword,
213 const std::wstring newpassword);
214 void sendDamage(u8 damage);
217 ClientEnvironment& getEnv()
220 // Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
221 void removeNode(v3s16 p);
222 void addNode(v3s16 p, MapNode n);
224 void setPlayerControl(PlayerControl &control);
226 void selectPlayerItem(u16 item);
227 u16 getPlayerItem() const
228 { return m_playeritem; }
230 // Returns true if the inventory of the local player has been
231 // updated from the server. If it is true, it is set to false.
232 bool getLocalInventoryUpdated();
233 // Copies the inventory of the local player to parameter
234 void getLocalInventory(Inventory &dst);
236 /* InventoryManager interface */
237 Inventory* getInventory(const InventoryLocation &loc);
238 void inventoryAction(InventoryAction *a);
240 // Gets closest object pointed by the shootline
241 // Returns NULL if not found
242 ClientActiveObject * getSelectedActiveObject(
244 v3f from_pos_f_on_map,
245 core::line3d<f32> shootline_on_map
248 // Prints a line or two of info
249 void printDebugInfo(std::ostream &os);
251 core::list<std::wstring> getConnectedPlayerNames();
253 float getAnimationTime();
256 void setCrack(int level, v3s16 pos);
258 u32 getDayNightRatio();
265 return m_con.GetPeerAvgRTT(PEER_ID_SERVER);
266 } catch(con::PeerNotFoundException){
271 bool getChatMessage(std::wstring &message);
272 void typeChatMessage(const std::wstring& message);
274 u64 getMapSeed(){ return m_map_seed; }
276 void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
277 // Including blocks at appropriate edges
278 void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
279 void addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server=false, bool urgent=false);
281 // Get event from queue. CE_NONE is returned if queue is empty.
282 ClientEvent getClientEvent();
285 { return m_access_denied; }
287 std::wstring accessDeniedReason()
288 { return m_access_denied_reason; }
290 float textureReceiveProgress()
291 { return m_texture_receive_progress; }
293 bool texturesReceived()
294 { return m_textures_received; }
295 bool itemdefReceived()
296 { return m_itemdef_received; }
297 bool nodedefReceived()
298 { return m_nodedef_received; }
300 void afterContentReceived();
304 // IGameDef interface
305 virtual IItemDefManager* getItemDefManager();
306 virtual INodeDefManager* getNodeDefManager();
307 virtual ICraftDefManager* getCraftDefManager();
308 virtual ITextureSource* getTextureSource();
309 virtual u16 allocateUnknownNodeId(const std::string &name);
313 // Virtual methods from con::PeerHandler
314 void peerAdded(con::Peer *peer);
315 void deletingPeer(con::Peer *peer, bool timeout);
320 void sendPlayerPos();
321 // This sends the player's current name etc to the server
322 void sendPlayerInfo();
323 // Send the item number 'item' as player item to the server
324 void sendPlayerItem(u16 item);
326 float m_packetcounter_timer;
327 float m_connection_reinit_timer;
328 float m_avg_rtt_timer;
329 float m_playerpos_send_timer;
330 float m_ignore_damage_timer; // Used after server moves player
331 IntervalLimiter m_map_timer_and_unload_interval;
333 IWritableTextureSource *m_tsrc;
334 IWritableItemDefManager *m_itemdef;
335 IWritableNodeDefManager *m_nodedef;
336 MeshUpdateThread m_mesh_update_thread;
337 ClientEnvironment m_env;
338 con::Connection m_con;
339 IrrlichtDevice *m_device;
340 // Server serialization version
343 bool m_inventory_updated;
344 Inventory *m_inventory_from_server;
345 float m_inventory_from_server_age;
346 core::map<v3s16, bool> m_active_blocks;
347 PacketCounter m_packetcounter;
348 // Block mesh animation parameters
349 float m_animation_time;
352 // Received from the server. 0-23999
354 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
356 //u32 m_daynight_ratio;
357 Queue<std::wstring> m_chat_queue;
358 // The seed returned by the server in TOCLIENT_INIT is stored here
360 std::string m_password;
361 bool m_access_denied;
362 std::wstring m_access_denied_reason;
363 Queue<ClientEvent> m_client_event_queue;
364 float m_texture_receive_progress;
365 bool m_textures_received;
366 bool m_itemdef_received;
367 bool m_nodedef_received;
368 friend class FarMesh;
373 #endif // !CLIENT_HEADER