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;
121 ClientEventType type;
135 class Client : public con::PeerHandler, public InventoryManager
139 NOTE: Nothing is thread-safe here.
143 IrrlichtDevice *device,
144 const char *playername,
145 std::string password,
146 MapDrawControl &control
151 The name of the local player should already be set when
152 calling this, as it is sent in the initialization.
154 void connect(Address address);
157 m_con.Connected() == true
158 AND m_server_ser_ver != SER_FMT_VER_INVALID
159 throws con::PeerNotFoundException if connection has been deleted,
162 bool connectedAndInitialized();
164 Stuff that references the environment is valid only as
165 long as this is not called. (eg. Players)
166 If this throws a PeerNotFoundException, the connection has
169 void step(float dtime);
171 // Called from updater thread
175 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
176 // Returns true if something was received
177 bool AsyncProcessPacket();
178 bool AsyncProcessData();
179 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
181 // Pops out a packet from the packet queue
182 //IncomingPacket getPacket();
184 void groundAction(u8 action, v3s16 nodepos_undersurface,
185 v3s16 nodepos_oversurface, u16 item);
186 void clickObject(u8 button, v3s16 blockpos, s16 id, u16 item);
187 void clickActiveObject(u8 button, u16 id, u16 item);
189 void sendSignText(v3s16 blockpos, s16 id, std::string text);
190 void sendSignNodeText(v3s16 p, std::string text);
191 void sendInventoryAction(InventoryAction *a);
192 void sendChatMessage(const std::wstring &message);
193 void sendChangePassword(const std::wstring oldpassword,
194 const std::wstring newpassword);
195 void sendDamage(u8 damage);
198 void removeNode(v3s16 p);
200 void addNode(v3s16 p, MapNode n);
202 void updateCamera(v3f pos, v3f dir);
206 // Returns InvalidPositionException if not found
207 MapNode getNode(v3s16 p);
209 NodeMetadata* getNodeMetadata(v3s16 p);
211 LocalPlayer* getLocalPlayer();
213 v3f getPlayerPosition(v3f *eye_position);
215 void setPlayerControl(PlayerControl &control);
217 void selectPlayerItem(u16 item);
219 // Returns true if the inventory of the local player has been
220 // updated from the server. If it is true, it is set to false.
221 bool getLocalInventoryUpdated();
222 // Copies the inventory of the local player to parameter
223 void getLocalInventory(Inventory &dst);
225 InventoryContext *getInventoryContext();
227 Inventory* getInventory(InventoryContext *c, std::string id);
228 void inventoryAction(InventoryAction *a);
230 // Gets closest object pointed by the shootline
231 // Returns NULL if not found
232 MapBlockObject * getSelectedObject(
234 v3f from_pos_f_on_map,
235 core::line3d<f32> shootline_on_map
238 // Gets closest object pointed by the shootline
239 // Returns NULL if not found
240 ClientActiveObject * getSelectedActiveObject(
242 v3f from_pos_f_on_map,
243 core::line3d<f32> shootline_on_map
246 // Prints a line or two of info
247 void printDebugInfo(std::ostream &os);
249 u32 getDayNightRatio();
253 void setTempMod(v3s16 p, NodeMod mod);
254 void clearTempMod(v3s16 p);
258 //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
259 con::Peer *peer = m_con.GetPeerNoEx(PEER_ID_SERVER);
262 return peer->avg_rtt;
265 bool getChatMessage(std::wstring &message)
267 if(m_chat_queue.size() == 0)
269 message = m_chat_queue.pop_front();
273 void addChatMessage(const std::wstring &message)
275 if (message[0] == L'/') {
276 m_chat_queue.push_back(
277 (std::wstring)L"issued command: "+message);
281 //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
282 LocalPlayer *player = m_env.getLocalPlayer();
283 assert(player != NULL);
284 std::wstring name = narrow_to_wide(player->getName());
285 m_chat_queue.push_back(
286 (std::wstring)L"<"+name+L"> "+message);
289 u64 getMapSeed(){ return m_map_seed; }
291 void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false);
292 // Including blocks at appropriate edges
293 void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false);
295 // Get event from queue. CE_NONE is returned if queue is empty.
296 ClientEvent getClientEvent();
298 inline bool accessDenied()
300 return m_access_denied;
303 inline std::wstring accessDeniedReason()
305 return m_access_denied_reason;
310 // Virtual methods from con::PeerHandler
311 void peerAdded(con::Peer *peer);
312 void deletingPeer(con::Peer *peer, bool timeout);
317 void sendPlayerPos();
318 // This sends the player's current name etc to the server
319 void sendPlayerInfo();
320 // Send the item number 'item' as player item to the server
321 void sendPlayerItem(u16 item);
323 float m_packetcounter_timer;
324 float m_connection_reinit_timer;
325 float m_avg_rtt_timer;
326 float m_playerpos_send_timer;
327 float m_ignore_damage_timer; // Used after server moves player
328 IntervalLimiter m_map_timer_and_unload_interval;
330 MeshUpdateThread m_mesh_update_thread;
332 ClientEnvironment m_env;
334 con::Connection m_con;
336 IrrlichtDevice *m_device;
338 // Server serialization version
341 // This is behind m_env_mutex.
342 bool m_inventory_updated;
344 core::map<v3s16, bool> m_active_blocks;
346 PacketCounter m_packetcounter;
348 // Received from the server. 0-23999
351 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
353 //u32 m_daynight_ratio;
355 Queue<std::wstring> m_chat_queue;
357 // 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;
364 InventoryContext m_inventory_context;
366 Queue<ClientEvent> m_client_event_queue;
368 friend class FarMesh;
373 #endif // !CLIENT_HEADER