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
35 class ClientNotReadyException : public BaseException
38 ClientNotReadyException(const char *s):
43 struct QueuedMeshUpdate
47 bool ack_block_to_server;
54 A thread-safe queue of mesh update tasks
64 peer_id=0 adds with nobody to send to
66 void addBlock(v3s16 p, MeshMakeData *data, bool ack_block_to_server);
68 // Returned pointer must be deleted
69 // Returns NULL if queue is empty
70 QueuedMeshUpdate * pop();
74 JMutexAutoLock lock(m_mutex);
75 return m_queue.size();
79 core::list<QueuedMeshUpdate*> m_queue;
83 struct MeshUpdateResult
87 bool ack_block_to_server;
92 ack_block_to_server(false)
97 class MeshUpdateThread : public SimpleThread
107 MeshUpdateQueue m_queue_in;
109 MutexedQueue<MeshUpdateResult> m_queue_out;
116 CE_PLAYER_FORCE_MOVE,
122 ClientEventType type;
134 bool set_camera_point_target;
135 f32 camera_point_target_x;
136 f32 camera_point_target_y;
137 f32 camera_point_target_z;
142 class Client : public con::PeerHandler, public InventoryManager
146 NOTE: Nothing is thread-safe here.
150 IrrlichtDevice *device,
151 const char *playername,
152 std::string password,
153 MapDrawControl &control
158 The name of the local player should already be set when
159 calling this, as it is sent in the initialization.
161 void connect(Address address);
164 m_con.Connected() == true
165 AND m_server_ser_ver != SER_FMT_VER_INVALID
166 throws con::PeerNotFoundException if connection has been deleted,
169 bool connectedAndInitialized();
171 Stuff that references the environment is valid only as
172 long as this is not called. (eg. Players)
173 If this throws a PeerNotFoundException, the connection has
176 void step(float dtime);
178 // Called from updater thread
182 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
183 // Returns true if something was received
184 bool AsyncProcessPacket();
185 bool AsyncProcessData();
186 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
188 // Pops out a packet from the packet queue
189 //IncomingPacket getPacket();
191 void groundAction(u8 action, v3s16 nodepos_undersurface,
192 v3s16 nodepos_oversurface, u16 item);
193 void clickActiveObject(u8 button, u16 id, u16 item_i);
195 void sendSignNodeText(v3s16 p, std::string text);
196 void sendInventoryAction(InventoryAction *a);
197 void sendChatMessage(const std::wstring &message);
198 void sendChangePassword(const std::wstring oldpassword,
199 const std::wstring newpassword);
200 void sendDamage(u8 damage);
204 void removeNode(v3s16 p);
206 void addNode(v3s16 p, MapNode n);
208 void updateCamera(v3f pos, v3f dir, f32 fov);
212 // Returns InvalidPositionException if not found
213 MapNode getNode(v3s16 p);
215 NodeMetadata* getNodeMetadata(v3s16 p);
217 LocalPlayer* getLocalPlayer();
219 void setPlayerControl(PlayerControl &control);
221 void selectPlayerItem(u16 item);
223 // Returns true if the inventory of the local player has been
224 // updated from the server. If it is true, it is set to false.
225 bool getLocalInventoryUpdated();
226 // Copies the inventory of the local player to parameter
227 void getLocalInventory(Inventory &dst);
229 InventoryContext *getInventoryContext();
231 Inventory* getInventory(InventoryContext *c, std::string id);
232 void inventoryAction(InventoryAction *a);
234 // Gets closest object pointed by the shootline
235 // Returns NULL if not found
236 ClientActiveObject * getSelectedActiveObject(
238 v3f from_pos_f_on_map,
239 core::line3d<f32> shootline_on_map
242 // Prints a line or two of info
243 void printDebugInfo(std::ostream &os);
245 u32 getDayNightRatio();
249 void setTempMod(v3s16 p, NodeMod mod);
250 void clearTempMod(v3s16 p);
255 return m_con.GetPeerAvgRTT(PEER_ID_SERVER);
256 } catch(con::PeerNotFoundException){
261 bool getChatMessage(std::wstring &message)
263 if(m_chat_queue.size() == 0)
265 message = m_chat_queue.pop_front();
269 void addChatMessage(const std::wstring &message)
271 if (message[0] == L'/') {
272 m_chat_queue.push_back(
273 (std::wstring)L"issued command: "+message);
277 //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
278 LocalPlayer *player = m_env.getLocalPlayer();
279 assert(player != NULL);
280 std::wstring name = narrow_to_wide(player->getName());
281 m_chat_queue.push_back(
282 (std::wstring)L"<"+name+L"> "+message);
285 u64 getMapSeed(){ return m_map_seed; }
287 void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false);
288 // Including blocks at appropriate edges
289 void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false);
291 // Get event from queue. CE_NONE is returned if queue is empty.
292 ClientEvent getClientEvent();
294 inline bool accessDenied()
296 return m_access_denied;
299 inline std::wstring accessDeniedReason()
301 return m_access_denied_reason;
308 // Virtual methods from con::PeerHandler
309 void peerAdded(con::Peer *peer);
310 void deletingPeer(con::Peer *peer, bool timeout);
315 void sendPlayerPos();
316 // This sends the player's current name etc to the server
317 void sendPlayerInfo();
318 // Send the item number 'item' as player item to the server
319 void sendPlayerItem(u16 item);
321 float m_packetcounter_timer;
322 float m_connection_reinit_timer;
323 float m_avg_rtt_timer;
324 float m_playerpos_send_timer;
325 float m_ignore_damage_timer; // Used after server moves player
326 IntervalLimiter m_map_timer_and_unload_interval;
328 MeshUpdateThread m_mesh_update_thread;
330 ClientEnvironment m_env;
332 con::Connection m_con;
334 IrrlichtDevice *m_device;
336 // Server serialization version
339 // This is behind m_env_mutex.
340 bool m_inventory_updated;
342 core::map<v3s16, bool> m_active_blocks;
344 PacketCounter m_packetcounter;
346 // Received from the server. 0-23999
349 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
351 //u32 m_daynight_ratio;
353 Queue<std::wstring> m_chat_queue;
355 // The seed returned by the server in TOCLIENT_INIT is stored here
358 std::string m_password;
359 bool m_access_denied;
360 std::wstring m_access_denied_reason;
362 InventoryContext m_inventory_context;
364 Queue<ClientEvent> m_client_event_queue;
366 friend class FarMesh;
371 #endif // !CLIENT_HEADER