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"
30 #include "clientobject.h"
31 #include "utility.h" // For IntervalLimiter
36 class IWritableTextureSource;
37 class IWritableToolDefManager;
38 class IWritableNodeDefManager;
40 class ClientNotReadyException : public BaseException
43 ClientNotReadyException(const char *s):
48 struct QueuedMeshUpdate
52 bool ack_block_to_server;
59 A thread-safe queue of mesh update tasks
69 peer_id=0 adds with nobody to send to
71 void addBlock(v3s16 p, MeshMakeData *data, bool ack_block_to_server);
73 // Returned pointer must be deleted
74 // Returns NULL if queue is empty
75 QueuedMeshUpdate * pop();
79 JMutexAutoLock lock(m_mutex);
80 return m_queue.size();
84 core::list<QueuedMeshUpdate*> m_queue;
88 struct MeshUpdateResult
92 bool ack_block_to_server;
97 ack_block_to_server(false)
102 class MeshUpdateThread : public SimpleThread
106 MeshUpdateThread(IGameDef *gamedef):
113 MeshUpdateQueue m_queue_in;
115 MutexedQueue<MeshUpdateResult> m_queue_out;
124 CE_PLAYER_FORCE_MOVE,
131 ClientEventType type;
143 bool set_camera_point_target;
144 f32 camera_point_target_x;
145 f32 camera_point_target_y;
146 f32 camera_point_target_z;
153 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
157 NOTE: Nothing is thread-safe here.
161 IrrlichtDevice *device,
162 const char *playername,
163 std::string password,
164 MapDrawControl &control,
165 IWritableTextureSource *tsrc,
166 IWritableToolDefManager *tooldef,
167 IWritableNodeDefManager *nodedef
172 The name of the local player should already be set when
173 calling this, as it is sent in the initialization.
175 void connect(Address address);
178 m_con.Connected() == true
179 AND m_server_ser_ver != SER_FMT_VER_INVALID
180 throws con::PeerNotFoundException if connection has been deleted,
183 bool connectedAndInitialized();
185 Stuff that references the environment is valid only as
186 long as this is not called. (eg. Players)
187 If this throws a PeerNotFoundException, the connection has
190 void step(float dtime);
192 // Called from updater thread
196 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
197 // Returns true if something was received
198 bool AsyncProcessPacket();
199 bool AsyncProcessData();
200 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
202 // Pops out a packet from the packet queue
203 //IncomingPacket getPacket();
205 void groundAction(u8 action, v3s16 nodepos_undersurface,
206 v3s16 nodepos_oversurface, u16 item);
207 void clickActiveObject(u8 button, u16 id, u16 item_i);
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);
218 void removeNode(v3s16 p);
220 void addNode(v3s16 p, MapNode n);
222 void updateCamera(v3f pos, v3f dir, f32 fov);
226 // Returns InvalidPositionException if not found
227 MapNode getNode(v3s16 p);
229 NodeMetadata* getNodeMetadata(v3s16 p);
231 LocalPlayer* getLocalPlayer();
233 void setPlayerControl(PlayerControl &control);
235 void selectPlayerItem(u16 item);
237 // Returns true if the inventory of the local player has been
238 // updated from the server. If it is true, it is set to false.
239 bool getLocalInventoryUpdated();
240 // Copies the inventory of the local player to parameter
241 void getLocalInventory(Inventory &dst);
243 InventoryContext *getInventoryContext();
245 Inventory* getInventory(InventoryContext *c, std::string id);
246 void inventoryAction(InventoryAction *a);
248 // Gets closest object pointed by the shootline
249 // Returns NULL if not found
250 ClientActiveObject * getSelectedActiveObject(
252 v3f from_pos_f_on_map,
253 core::line3d<f32> shootline_on_map
256 // Prints a line or two of info
257 void printDebugInfo(std::ostream &os);
259 u32 getDayNightRatio();
263 void setTempMod(v3s16 p, NodeMod mod);
264 void clearTempMod(v3s16 p);
269 return m_con.GetPeerAvgRTT(PEER_ID_SERVER);
270 } catch(con::PeerNotFoundException){
275 bool getChatMessage(std::wstring &message)
277 if(m_chat_queue.size() == 0)
279 message = m_chat_queue.pop_front();
283 void addChatMessage(const std::wstring &message)
285 if (message[0] == L'/') {
286 m_chat_queue.push_back(
287 (std::wstring)L"issued command: "+message);
291 //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
292 LocalPlayer *player = m_env.getLocalPlayer();
293 assert(player != NULL);
294 std::wstring name = narrow_to_wide(player->getName());
295 m_chat_queue.push_back(
296 (std::wstring)L"<"+name+L"> "+message);
299 u64 getMapSeed(){ return m_map_seed; }
301 void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false);
302 // Including blocks at appropriate edges
303 void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=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 textureReceiveProgress()
315 { return m_texture_receive_progress; }
317 bool texturesReceived()
318 { return m_textures_received; }
319 bool tooldefReceived()
320 { return m_tooldef_received; }
321 bool nodedefReceived()
322 { return m_nodedef_received; }
326 // IGameDef interface
327 virtual IToolDefManager* getToolDefManager();
328 virtual INodeDefManager* getNodeDefManager();
329 virtual ITextureSource* getTextureSource();
330 virtual u16 allocateUnknownNodeId(const std::string &name);
334 // Virtual methods from con::PeerHandler
335 void peerAdded(con::Peer *peer);
336 void deletingPeer(con::Peer *peer, bool timeout);
341 void sendPlayerPos();
342 // This sends the player's current name etc to the server
343 void sendPlayerInfo();
344 // Send the item number 'item' as player item to the server
345 void sendPlayerItem(u16 item);
347 float m_packetcounter_timer;
348 float m_connection_reinit_timer;
349 float m_avg_rtt_timer;
350 float m_playerpos_send_timer;
351 float m_ignore_damage_timer; // Used after server moves player
352 IntervalLimiter m_map_timer_and_unload_interval;
354 IWritableTextureSource *m_tsrc;
355 IWritableToolDefManager *m_tooldef;
356 IWritableNodeDefManager *m_nodedef;
357 MeshUpdateThread m_mesh_update_thread;
358 ClientEnvironment m_env;
359 con::Connection m_con;
360 IrrlichtDevice *m_device;
361 // Server serialization version
363 // This is behind m_env_mutex.
364 bool m_inventory_updated;
365 core::map<v3s16, bool> m_active_blocks;
366 PacketCounter m_packetcounter;
367 // Received from the server. 0-23999
369 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
371 //u32 m_daynight_ratio;
372 Queue<std::wstring> m_chat_queue;
373 // The seed returned by the server in TOCLIENT_INIT is stored here
375 std::string m_password;
376 bool m_access_denied;
377 std::wstring m_access_denied_reason;
378 InventoryContext m_inventory_context;
379 Queue<ClientEvent> m_client_event_queue;
380 float m_texture_receive_progress;
381 bool m_textures_received;
382 bool m_tooldef_received;
383 bool m_nodedef_received;
384 friend class FarMesh;
389 #endif // !CLIENT_HEADER