Skip to content

Commit d51d0f3

Browse files
authoredSep 27, 2021
Various code improvements
* Camera: Fix division by 0 after view bobbing * Remove ignored constness * Connection: Improve window size range limits
1 parent 918fbe3 commit d51d0f3

11 files changed

+40
-47
lines changed
 

‎src/client/camera.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_r
378378
// Smoothen and invert the above
379379
fall_bobbing = sin(fall_bobbing * 0.5 * M_PI) * -1;
380380
// Amplify according to the intensity of the impact
381-
fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
381+
if (player->camera_impact > 0.0f)
382+
fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
382383

383384
fall_bobbing *= m_cache_fall_bobbing_amount;
384385
}

‎src/client/client.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,13 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
334334
// disconnect client when CSM failed.
335335
const std::string &accessDeniedReason() const { return m_access_denied_reason; }
336336

337-
const bool itemdefReceived() const
337+
bool itemdefReceived() const
338338
{ return m_itemdef_received; }
339-
const bool nodedefReceived() const
339+
bool nodedefReceived() const
340340
{ return m_nodedef_received; }
341-
const bool mediaReceived() const
341+
bool mediaReceived() const
342342
{ return !m_media_downloader; }
343-
const bool activeObjectsReceived() const
343+
bool activeObjectsReceived() const
344344
{ return m_activeobjects_received; }
345345

346346
u16 getProtoVersion()

‎src/client/gameui.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class GameUI
8484
void showTranslatedStatusText(const char *str);
8585
inline void clearStatusText() { m_statustext.clear(); }
8686

87-
const bool isChatVisible()
87+
bool isChatVisible()
8888
{
8989
return m_flags.show_chat && m_recent_chat_count != 0 && m_profiler_current_page == 0;
9090
}

‎src/gui/guiFormSpecMenu.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class GUIFormSpecMenu : public GUIModalMenu
229229
return m_selected_item;
230230
}
231231

232-
const u16 getSelectedAmount() const
232+
u16 getSelectedAmount() const
233233
{
234234
return m_selected_amount;
235235
}

‎src/inventory.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ const InventoryList *Inventory::getList(const std::string &name) const
995995
return m_lists[i];
996996
}
997997

998-
const s32 Inventory::getListIndex(const std::string &name) const
998+
s32 Inventory::getListIndex(const std::string &name) const
999999
{
10001000
for(u32 i=0; i<m_lists.size(); i++)
10011001
{

‎src/inventory.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ class Inventory
336336
}
337337
private:
338338
// -1 if not found
339-
const s32 getListIndex(const std::string &name) const;
339+
s32 getListIndex(const std::string &name) const;
340340

341341
std::vector<InventoryList*> m_lists;
342342
IItemDefManager *m_itemdef;

‎src/network/connection.cpp

+10-22
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ u16 Channel::getOutgoingSequenceNumber(bool& successful)
578578
// ugly cast but this one is required in order to tell compiler we
579579
// know about difference of two unsigned may be negative in general
580580
// but we already made sure it won't happen in this case
581-
if (((u16)(next_outgoing_seqnum - lowest_unacked_seqnumber)) > window_size) {
581+
if (((u16)(next_outgoing_seqnum - lowest_unacked_seqnumber)) > m_window_size) {
582582
successful = false;
583583
return 0;
584584
}
@@ -588,7 +588,7 @@ u16 Channel::getOutgoingSequenceNumber(bool& successful)
588588
// know about difference of two unsigned may be negative in general
589589
// but we already made sure it won't happen in this case
590590
if ((next_outgoing_seqnum + (u16)(SEQNUM_MAX - lowest_unacked_seqnumber)) >
591-
window_size) {
591+
m_window_size) {
592592
successful = false;
593593
return 0;
594594
}
@@ -666,7 +666,7 @@ void Channel::UpdateTimers(float dtime)
666666
//packet_too_late = current_packet_too_late;
667667
packets_successful = current_packet_successful;
668668

669-
if (current_bytes_transfered > (unsigned int) (window_size*512/2)) {
669+
if (current_bytes_transfered > (unsigned int) (m_window_size*512/2)) {
670670
reasonable_amount_of_data_transmitted = true;
671671
}
672672
current_packet_loss = 0;
@@ -681,37 +681,25 @@ void Channel::UpdateTimers(float dtime)
681681
if (packets_successful > 0) {
682682
successful_to_lost_ratio = packet_loss/packets_successful;
683683
} else if (packet_loss > 0) {
684-
window_size = std::max(
685-
(window_size - 10),
686-
MIN_RELIABLE_WINDOW_SIZE);
684+
setWindowSize(m_window_size - 10);
687685
done = true;
688686
}
689687

690688
if (!done) {
691-
if ((successful_to_lost_ratio < 0.01f) &&
692-
(window_size < MAX_RELIABLE_WINDOW_SIZE)) {
689+
if (successful_to_lost_ratio < 0.01f) {
693690
/* don't even think about increasing if we didn't even
694691
* use major parts of our window */
695692
if (reasonable_amount_of_data_transmitted)
696-
window_size = std::min(
697-
(window_size + 100),
698-
MAX_RELIABLE_WINDOW_SIZE);
699-
} else if ((successful_to_lost_ratio < 0.05f) &&
700-
(window_size < MAX_RELIABLE_WINDOW_SIZE)) {
693+
setWindowSize(m_window_size + 100);
694+
} else if (successful_to_lost_ratio < 0.05f) {
701695
/* don't even think about increasing if we didn't even
702696
* use major parts of our window */
703697
if (reasonable_amount_of_data_transmitted)
704-
window_size = std::min(
705-
(window_size + 50),
706-
MAX_RELIABLE_WINDOW_SIZE);
698+
setWindowSize(m_window_size + 50);
707699
} else if (successful_to_lost_ratio > 0.15f) {
708-
window_size = std::max(
709-
(window_size - 100),
710-
MIN_RELIABLE_WINDOW_SIZE);
700+
setWindowSize(m_window_size - 100);
711701
} else if (successful_to_lost_ratio > 0.1f) {
712-
window_size = std::max(
713-
(window_size - 50),
714-
MIN_RELIABLE_WINDOW_SIZE);
702+
setWindowSize(m_window_size - 50);
715703
}
716704
}
717705
}

‎src/network/connection.h

+17-13
Original file line numberDiff line numberDiff line change
@@ -420,34 +420,38 @@ class Channel
420420

421421
void UpdateTimers(float dtime);
422422

423-
const float getCurrentDownloadRateKB()
423+
float getCurrentDownloadRateKB()
424424
{ MutexAutoLock lock(m_internal_mutex); return cur_kbps; };
425-
const float getMaxDownloadRateKB()
425+
float getMaxDownloadRateKB()
426426
{ MutexAutoLock lock(m_internal_mutex); return max_kbps; };
427427

428-
const float getCurrentLossRateKB()
428+
float getCurrentLossRateKB()
429429
{ MutexAutoLock lock(m_internal_mutex); return cur_kbps_lost; };
430-
const float getMaxLossRateKB()
430+
float getMaxLossRateKB()
431431
{ MutexAutoLock lock(m_internal_mutex); return max_kbps_lost; };
432432

433-
const float getCurrentIncomingRateKB()
433+
float getCurrentIncomingRateKB()
434434
{ MutexAutoLock lock(m_internal_mutex); return cur_incoming_kbps; };
435-
const float getMaxIncomingRateKB()
435+
float getMaxIncomingRateKB()
436436
{ MutexAutoLock lock(m_internal_mutex); return max_incoming_kbps; };
437437

438-
const float getAvgDownloadRateKB()
438+
float getAvgDownloadRateKB()
439439
{ MutexAutoLock lock(m_internal_mutex); return avg_kbps; };
440-
const float getAvgLossRateKB()
440+
float getAvgLossRateKB()
441441
{ MutexAutoLock lock(m_internal_mutex); return avg_kbps_lost; };
442-
const float getAvgIncomingRateKB()
442+
float getAvgIncomingRateKB()
443443
{ MutexAutoLock lock(m_internal_mutex); return avg_incoming_kbps; };
444444

445-
const unsigned int getWindowSize() const { return window_size; };
445+
u16 getWindowSize() const { return m_window_size; };
446+
447+
void setWindowSize(long size)
448+
{
449+
m_window_size = (u16)rangelim(size, MIN_RELIABLE_WINDOW_SIZE, MAX_RELIABLE_WINDOW_SIZE);
450+
}
446451

447-
void setWindowSize(unsigned int size) { window_size = size; };
448452
private:
449453
std::mutex m_internal_mutex;
450-
int window_size = MIN_RELIABLE_WINDOW_SIZE;
454+
u16 m_window_size = MIN_RELIABLE_WINDOW_SIZE;
451455

452456
u16 next_incoming_seqnum = SEQNUM_INITIAL;
453457

@@ -765,7 +769,7 @@ class Connection
765769
Address GetPeerAddress(session_t peer_id);
766770
float getPeerStat(session_t peer_id, rtt_stat_type type);
767771
float getLocalStat(rate_stat_type type);
768-
const u32 GetProtocolID() const { return m_protocol_id; };
772+
u32 GetProtocolID() const { return m_protocol_id; };
769773
const std::string getDesc();
770774
void DisconnectPeer(session_t peer_id);
771775

‎src/network/networkpacket.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class NetworkPacket
4141
u32 getSize() const { return m_datasize; }
4242
session_t getPeerId() const { return m_peer_id; }
4343
u16 getCommand() { return m_command; }
44-
const u32 getRemainingBytes() const { return m_datasize - m_read_offset; }
44+
u32 getRemainingBytes() const { return m_datasize - m_read_offset; }
4545
const char *getRemainingString() { return getString(m_read_offset); }
4646

4747
// Returns a c-string without copying.

‎src/remoteplayer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef):
8484
}
8585

8686

87-
const RemotePlayerChatResult RemotePlayer::canSendChatMessage()
87+
RemotePlayerChatResult RemotePlayer::canSendChatMessage()
8888
{
8989
// Rate limit messages
9090
u32 now = time(NULL);

‎src/remoteplayer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class RemotePlayer : public Player
4747
PlayerSAO *getPlayerSAO() { return m_sao; }
4848
void setPlayerSAO(PlayerSAO *sao) { m_sao = sao; }
4949

50-
const RemotePlayerChatResult canSendChatMessage();
50+
RemotePlayerChatResult canSendChatMessage();
5151

5252
void setHotbarItemcount(s32 hotbar_itemcount)
5353
{

0 commit comments

Comments
 (0)
Please sign in to comment.