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
33 #include "inventorymanager.h"
37 class IWritableTextureSource;
38 class IWritableToolDefManager;
39 class IWritableNodeDefManager;
40 //class IWritableCraftDefManager;
41 class IWritableCraftItemDefManager;
43 class ClientNotReadyException : public BaseException
46 ClientNotReadyException(const char *s):
51 struct QueuedMeshUpdate
55 bool ack_block_to_server;
62 A thread-safe queue of mesh update tasks
72 peer_id=0 adds with nobody to send to
74 void addBlock(v3s16 p, MeshMakeData *data, bool ack_block_to_server);
76 // Returned pointer must be deleted
77 // Returns NULL if queue is empty
78 QueuedMeshUpdate * pop();
82 JMutexAutoLock lock(m_mutex);
83 return m_queue.size();
87 core::list<QueuedMeshUpdate*> m_queue;
91 struct MeshUpdateResult
95 bool ack_block_to_server;
100 ack_block_to_server(false)
105 class MeshUpdateThread : public SimpleThread
109 MeshUpdateThread(IGameDef *gamedef):
116 MeshUpdateQueue m_queue_in;
118 MutexedQueue<MeshUpdateResult> m_queue_out;
127 CE_PLAYER_FORCE_MOVE,
134 ClientEventType type;
146 bool set_camera_point_target;
147 f32 camera_point_target_x;
148 f32 camera_point_target_y;
149 f32 camera_point_target_z;
156 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
160 NOTE: Nothing is thread-safe here.
164 IrrlichtDevice *device,
165 const char *playername,
166 std::string password,
167 MapDrawControl &control,
168 IWritableTextureSource *tsrc,
169 IWritableToolDefManager *tooldef,
170 IWritableNodeDefManager *nodedef,
171 IWritableCraftItemDefManager *craftitemdef
176 The name of the local player should already be set when
177 calling this, as it is sent in the initialization.
179 void connect(Address address);
182 m_con.Connected() == true
183 AND m_server_ser_ver != SER_FMT_VER_INVALID
184 throws con::PeerNotFoundException if connection has been deleted,
187 bool connectedAndInitialized();
189 Stuff that references the environment is valid only as
190 long as this is not called. (eg. Players)
191 If this throws a PeerNotFoundException, the connection has
194 void step(float dtime);
196 // Called from updater thread
200 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
201 // Returns true if something was received
202 bool AsyncProcessPacket();
203 bool AsyncProcessData();
204 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
206 // Pops out a packet from the packet queue
207 //IncomingPacket getPacket();
209 void interact(u8 action, const PointedThing& pointed);
211 void sendSignNodeText(v3s16 p, std::string text);
212 void sendInventoryAction(InventoryAction *a);
213 void sendChatMessage(const std::wstring &message);
214 void sendChangePassword(const std::wstring oldpassword,
215 const std::wstring newpassword);
216 void sendDamage(u8 damage);
220 void removeNode(v3s16 p);
222 void addNode(v3s16 p, MapNode n);
224 void updateCamera(v3f pos, v3f dir, f32 fov);
228 // Returns InvalidPositionException if not found
229 MapNode getNode(v3s16 p);
231 NodeMetadata* getNodeMetadata(v3s16 p);
233 LocalPlayer* getLocalPlayer();
235 void setPlayerControl(PlayerControl &control);
237 void selectPlayerItem(u16 item);
238 u16 getPlayerItem() const
239 { return m_playeritem; }
241 // Returns true if the inventory of the local player has been
242 // updated from the server. If it is true, it is set to false.
243 bool getLocalInventoryUpdated();
244 // Copies the inventory of the local player to parameter
245 void getLocalInventory(Inventory &dst);
247 InventoryContext *getInventoryContext();
249 /* InventoryManager interface */
250 Inventory* getInventory(const InventoryLocation &loc);
251 //Inventory* getInventory(InventoryContext *c, std::string id);
252 void inventoryAction(InventoryAction *a);
254 // Gets closest object pointed by the shootline
255 // Returns NULL if not found
256 ClientActiveObject * getSelectedActiveObject(
258 v3f from_pos_f_on_map,
259 core::line3d<f32> shootline_on_map
262 // Prints a line or two of info
263 void printDebugInfo(std::ostream &os);
265 u32 getDayNightRatio();
269 void setTempMod(v3s16 p, NodeMod mod);
270 void clearTempMod(v3s16 p);
275 return m_con.GetPeerAvgRTT(PEER_ID_SERVER);
276 } catch(con::PeerNotFoundException){
281 bool getChatMessage(std::wstring &message)
283 if(m_chat_queue.size() == 0)
285 message = m_chat_queue.pop_front();
289 void addChatMessage(const std::wstring &message)
291 if (message[0] == L'/') {
292 m_chat_queue.push_back(
293 (std::wstring)L"issued command: "+message);
297 //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
298 LocalPlayer *player = m_env.getLocalPlayer();
299 assert(player != NULL);
300 std::wstring name = narrow_to_wide(player->getName());
301 m_chat_queue.push_back(
302 (std::wstring)L"<"+name+L"> "+message);
305 u64 getMapSeed(){ return m_map_seed; }
307 void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false);
308 // Including blocks at appropriate edges
309 void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false);
311 // Get event from queue. CE_NONE is returned if queue is empty.
312 ClientEvent getClientEvent();
315 { return m_access_denied; }
317 std::wstring accessDeniedReason()
318 { return m_access_denied_reason; }
320 float textureReceiveProgress()
321 { return m_texture_receive_progress; }
323 bool texturesReceived()
324 { return m_textures_received; }
325 bool tooldefReceived()
326 { return m_tooldef_received; }
327 bool nodedefReceived()
328 { return m_nodedef_received; }
329 bool craftitemdefReceived()
330 { return m_craftitemdef_received; }
334 // IGameDef interface
335 virtual IToolDefManager* getToolDefManager();
336 virtual INodeDefManager* getNodeDefManager();
337 virtual ICraftDefManager* getCraftDefManager();
338 virtual ICraftItemDefManager* getCraftItemDefManager();
339 virtual ITextureSource* getTextureSource();
340 virtual u16 allocateUnknownNodeId(const std::string &name);
344 // Virtual methods from con::PeerHandler
345 void peerAdded(con::Peer *peer);
346 void deletingPeer(con::Peer *peer, bool timeout);
351 void sendPlayerPos();
352 // This sends the player's current name etc to the server
353 void sendPlayerInfo();
354 // Send the item number 'item' as player item to the server
355 void sendPlayerItem(u16 item);
357 float m_packetcounter_timer;
358 float m_connection_reinit_timer;
359 float m_avg_rtt_timer;
360 float m_playerpos_send_timer;
361 float m_ignore_damage_timer; // Used after server moves player
362 IntervalLimiter m_map_timer_and_unload_interval;
364 IWritableTextureSource *m_tsrc;
365 IWritableToolDefManager *m_tooldef;
366 IWritableNodeDefManager *m_nodedef;
367 IWritableCraftItemDefManager *m_craftitemdef;
368 MeshUpdateThread m_mesh_update_thread;
369 ClientEnvironment m_env;
370 con::Connection m_con;
371 IrrlichtDevice *m_device;
372 // Server serialization version
375 bool m_inventory_updated;
376 core::map<v3s16, bool> m_active_blocks;
377 PacketCounter m_packetcounter;
378 // Received from the server. 0-23999
380 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
382 //u32 m_daynight_ratio;
383 Queue<std::wstring> m_chat_queue;
384 // The seed returned by the server in TOCLIENT_INIT is stored here
386 std::string m_password;
387 bool m_access_denied;
388 std::wstring m_access_denied_reason;
389 InventoryContext m_inventory_context;
390 Queue<ClientEvent> m_client_event_queue;
391 float m_texture_receive_progress;
392 bool m_textures_received;
393 bool m_tooldef_received;
394 bool m_nodedef_received;
395 bool m_craftitemdef_received;
396 friend class FarMesh;
401 #endif // !CLIENT_HEADER