1 /************************************************************************
3 * Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
6 * voxelands - 3d voxel world sandbox game
7 * Copyright (C) Lisa 'darkrose' Milne 2014 <lisa@ltmnet.com>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 * See the GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>
22 * License updated from GPLv2 or later to GPLv3 or later by Lisa Milne
24 ************************************************************************/
31 #include "connection.h"
32 #include "environment.h"
33 #include "common_irrlicht.h"
36 #include "clientobject.h"
37 #include "particles.h"
38 #include "utility.h" // For IntervalLimiter
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
;
93 struct MeshUpdateResult
97 bool ack_block_to_server
;
100 p(-1338,-1338,-1338),
102 ack_block_to_server(false)
107 class MeshUpdateThread
: public SimpleThread
117 MeshUpdateQueue m_queue_in
;
119 MutexedQueue
<MeshUpdateResult
> m_queue_out
;
121 v3s16 m_camera_offset
;
122 ClientEnvironment
*m_env
;
131 CE_PLAYER_FORCE_MOVE
,
137 ClientEventType type
;
149 bool set_camera_point_target
;
150 f32 camera_point_target_x
;
151 f32 camera_point_target_y
;
152 f32 camera_point_target_z
;
157 class Client
: public con::PeerHandler
, public InventoryManager
161 NOTE: Nothing is thread-safe here.
165 IrrlichtDevice
*device
,
166 const char *playername
,
167 std::string password
,
168 MapDrawControl
&control
,
174 The name of the local player should already be set when
175 calling this, as it is sent in the initialization.
177 void connect(Address address
);
180 m_con.Connected() == true
181 AND m_server_ser_ver != SER_FMT_VER_INVALID
182 throws con::PeerNotFoundException if connection has been deleted,
185 bool connectedAndInitialized();
187 Stuff that references the environment is valid only as
188 long as this is not called. (eg. Players)
189 If this throws a PeerNotFoundException, the connection has
192 void step(float dtime
);
194 // Called from updater thread
198 void ProcessData(u8
*data
, u32 datasize
, u16 sender_peer_id
);
199 // Returns true if something was received
200 bool AsyncProcessPacket();
201 bool AsyncProcessData();
202 void Send(u16 channelnum
, SharedBuffer
<u8
> data
, bool reliable
);
204 // Pops out a packet from the packet queue
205 //IncomingPacket getPacket();
207 void groundAction(u8 action
, v3s16 nodepos_undersurface
,
208 v3s16 nodepos_oversurface
, u16 item
);
209 void throwItem(v3f dir
, u16 item
);
211 void clickActiveObject(u8 button
, u16 id
, u16 item_i
);
213 void sendNodemetaFields(v3s16 p
, const std::string
&formname
,
214 const std::map
<std::string
, std::wstring
> &fields
);
215 void sendInventoryAction(InventoryAction
*a
);
216 void sendChatMessage(const std::wstring
&message
);
217 void sendChangePassword(const std::wstring oldpassword
,
218 const std::wstring newpassword
);
219 void sendDamage(s8 damage
, s8 suffocate
, s8 hunger
);
220 void sendClothesWear(u16 wear
);
223 ClientEnvironment
& getEnv() { return m_env
; }
226 void removeNode(v3s16 p
);
228 void addNode(v3s16 p
, MapNode n
);
230 void updateCamera(v3f pos
, v3f dir
, f32 fov
, v3s16 camera_offset
);
234 // Returns InvalidPositionException if not found
235 MapNode
getNode(v3s16 p
);
237 NodeMetadata
* getNodeMetadata(v3s16 p
);
239 Player
* getPlayer(const char* name
) { return m_env
.getPlayer(name
);}
240 LocalPlayer
* getLocalPlayer();
242 void setPlayerControl(PlayerControl
&control
);
244 void selectPlayerItem(u16 item
);
246 // Returns true if the inventory of the local player has been
247 // updated from the server. If it is true, it is set to false.
248 bool getLocalInventoryUpdated();
249 // Copies the inventory of the local player to parameter
250 void getLocalInventory(Inventory
&dst
);
252 InventoryContext
*getInventoryContext();
254 Inventory
* getInventory(InventoryContext
*c
, std::string id
);
255 Inventory
* getInventory(const InventoryLocation
*loc
);
256 void inventoryAction(InventoryAction
*a
);
258 // Gets closest object pointed by the shootline
259 // Returns NULL if not found
260 ClientActiveObject
* getSelectedActiveObject(
262 v3f from_pos_f_on_map
,
263 core::line3d
<f32
> shootline_on_map
266 // Prints a line or two of info
267 void printDebugInfo(std::ostream
&os
);
274 bool getChatMessage(std::wstring
&message
)
276 if(m_chat_queue
.size() == 0)
278 message
= m_chat_queue
.pop_front();
282 void addChatMessage(const std::wstring
&message
)
284 if (message
[0] == L
'/') {
285 m_chat_queue
.push_back(
286 (std::wstring
)L
"issued command: "+message
);
290 //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
291 LocalPlayer
*player
= m_env
.getLocalPlayer();
292 assert(player
!= NULL
);
293 std::wstring name
= narrow_to_wide(player
->getName());
294 m_chat_queue
.push_back(
295 (std::wstring
)L
"<"+name
+L
"> "+message
);
298 uint64_t getMapSeed() {return m_map_seed
;}
299 MapGenType
getMapType() {return m_map_type
;}
301 void addUpdateMeshTask(v3s16 blockpos
, bool ack_to_server
=false, bool refresh_only
=false);
302 // Including blocks at appropriate edges
303 void addUpdateMeshTaskWithEdge(v3s16 blockpos
, bool ack_to_server
=false);
305 void updateCameraOffset(v3s16 camera_offset
){ m_mesh_update_thread
.m_camera_offset
= camera_offset
; }
307 // Get event from queue. CE_NONE is returned if queue is empty.
308 ClientEvent
getClientEvent();
310 inline bool accessDenied()
312 return m_access_denied
;
315 inline std::wstring
accessDeniedReason()
317 return m_access_denied_reason
;
321 virtual ISoundManager
* getSoundManager();
323 void playStepSound(int foot
);
324 void playDigSound(content_t c
);
325 void playPlaceSound(content_t c
);
326 void playSound(std::string
&name
, bool loop
);
327 void playSoundAt(std::string
&name
, v3f pos
, bool loop
);
329 void setPointedNode(v3s16 p
) {m_pointed_node
= p
;}
330 v3s16
getPointedNode() {return m_pointed_node
;}
332 void setPointedContent(content_t c
) {m_pointed_content
= c
;}
333 content_t
getPointedContent() {return m_pointed_content
;}
335 bool getServerDamage() {return m_server_damage
;}
336 bool getServerSuffocation() {return m_server_suffocation
;}
337 bool getServerHunger() {return m_server_hunger
;}
339 bool getFormState() {return m_form_open
;}
340 void setFormState(bool state
) {m_form_open
= state
;}
342 u8
getSleepAlpha() {return m_sleep_state
*255;}
346 // Virtual methods from con::PeerHandler
347 void peerAdded(con::Peer
*peer
);
348 void deletingPeer(con::Peer
*peer
, bool timeout
);
353 void sendPlayerPos();
354 // This sends the player's current name etc to the server
355 void sendPlayerInfo();
356 // Send the item number 'item' as player item to the server
357 void sendPlayerItem(u16 item
);
359 void setServerSettings(bool damage
, bool suffocation
, bool hunger
);
361 float m_packetcounter_timer
;
362 float m_connection_reinit_timer
;
363 float m_avg_rtt_timer
;
364 float m_playerpos_send_timer
;
365 float m_ignore_damage_timer
; // Used after server moves player
366 IntervalLimiter m_map_timer_and_unload_interval
;
368 MeshUpdateThread m_mesh_update_thread
;
370 ClientEnvironment m_env
;
372 // when connecting to a server it will give these via TOCLIENT_SERVERSETTINGS
373 bool m_server_damage
;
374 bool m_server_suffocation
;
375 bool m_server_hunger
;
377 con::Connection m_con
;
378 ISoundManager
*m_sound
;
380 IrrlichtDevice
*m_device
;
382 // Server serialization version
385 // This is behind m_env_mutex.
386 bool m_inventory_updated
;
388 core::map
<v3s16
, bool> m_active_blocks
;
389 v3s16 m_pointed_node
;
390 content_t m_pointed_content
;
392 PacketCounter m_packetcounter
;
396 Queue
<std::wstring
> m_chat_queue
;
398 // The seed returned by the server in TOCLIENT_INIT is stored here
400 MapGenType m_map_type
;
402 std::string m_password
;
403 bool m_access_denied
;
404 std::wstring m_access_denied_reason
;
406 InventoryContext m_inventory_context
;
408 Queue
<ClientEvent
> m_client_event_queue
;
410 // time_of_day speed approximation for old protocol
411 bool m_time_of_day_set
;
412 float m_last_time_of_day_f
;
413 float m_time_of_day_update_timer
;
423 #endif // !CLIENT_HEADER