Skip to content

Commit f251464

Browse files
authored
Merge pull request #887 from Malivil/dev
Added "signed" parameter to "util.BitsRequired" from base TTT
2 parents c4e487c + ecdb853 commit f251464

3 files changed

Lines changed: 20 additions & 13 deletions

File tree

API/METHODS_UTIL.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Utility Methods
22
Utility methods created to help with various common scenarios
33

4-
### util.BitsRequired(num)
4+
### util.BitsRequired(num, signed)
55
Returns how many bits are required to transmit the given number over the network.\
66
*Realm:* Client and Server\
77
*Added in:* 2.3.3\
88
*Parameters:*
99
- *num* - The number being transmitted
10+
- *signed* - Whether the number being transmitted is signed *(Added in 2.3.5)*
1011

1112
### util.BurnRagdoll(rag, burn_time, scorch)
1213
Burns a player ragdoll, shows scorch marks, and automatically destroys the ragdoll unless it's been extinguished by water.\

docs/api/methods_util.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ <h1>Utility Methods <span id="betaswitch"></span></h1>
1717
<p>Utility methods created to help with various common scenarios</p>
1818
</div>
1919
<div class="contentbody panel">
20-
<h3><a id="utilbitsrequirednum" href="#utilbitsrequirednum">util.BitsRequired(num)</a> <span data-text="Beta Only" class="betaonly betaicon tooltip">&nbsp;</span></h3>
20+
<h3><a id="utilbitsrequirednum-signed" href="#utilbitsrequirednum-signed">util.BitsRequired(num, signed)</a> <span data-text="Beta Only" class="betaonly betaicon tooltip">&nbsp;</span></h3>
2121
<p>
2222
Returns how many bits are required to transmit the given number over the network.<br>
2323
<em>Realm:</em> Client and Server<br>
@@ -26,6 +26,7 @@ <h3><a id="utilbitsrequirednum" href="#utilbitsrequirednum">util.BitsRequired(nu
2626
</p>
2727
<ul>
2828
<li><em>num</em> - The number being transmitted</li>
29+
<li><em>signed</em> - Whether the number being transmitted is signed <em>(Added in 2.3.5)</em></li>
2930
</ul>
3031

3132
<h3><a id="utilburnragdollrag-burn_time_scorch" href="#utilburnragdollrag-burn_time_scorch">util.BurnRagdoll(rag, burn_time, scorch)</a></h3>

gamemodes/terrortown/gamemode/util.lua

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,21 @@ function util.SimpleTime(seconds, fmt)
472472
return StringFormat(fmt, m, s, ms)
473473
end
474474

475+
-- Returns the number of bits required to network an integer
476+
function util.BitsRequired(num, signed)
477+
local bits, max = 0, 1
478+
while max <= num do
479+
bits = bits + 1
480+
max = max + max
481+
end
482+
483+
if signed then
484+
bits = bits + 1
485+
end
486+
487+
return bits
488+
end
489+
475490
if SERVER then
476491
function util.ExecFile(filePath, errorIfMissing)
477492
if not FileExists(filePath, "GAME") then
@@ -554,20 +569,10 @@ function util.FormattedList(tbl, formatting)
554569
return result
555570
end
556571

557-
function util.BitsRequired(num)
558-
local bits, max = 0, 1
559-
while max <= num do
560-
bits = bits + 1
561-
max = max + max
562-
end
563-
return bits
564-
end
565-
566572
local roleBits = nil
567573
function util.RoleBits()
568574
if not roleBits then
569-
-- Add a bit to the required for this since we're sending it as signed to support ROLE_NONE (-1)
570-
roleBits = math.max(8, util.BitsRequired(ROLE_MAX) + 1)
575+
roleBits = math.max(8, util.BitsRequired(ROLE_MAX, true))
571576
end
572577

573578
return roleBits

0 commit comments

Comments
 (0)