Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion API/METHODS_UTIL.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Utility Methods
Utility methods created to help with various common scenarios

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

### util.BurnRagdoll(rag, burn_time, scorch)
Burns a player ragdoll, shows scorch marks, and automatically destroys the ragdoll unless it's been extinguished by water.\
Expand Down
3 changes: 2 additions & 1 deletion docs/api/methods_util.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h1>Utility Methods <span id="betaswitch"></span></h1>
<p>Utility methods created to help with various common scenarios</p>
</div>
<div class="contentbody panel">
<h3><a id="utilbitsrequirednum" href="#utilbitsrequirednum">util.BitsRequired(num)</a> <span data-text="Beta Only" class="betaonly betaicon tooltip">&nbsp;</span></h3>
<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>
<p>
Returns how many bits are required to transmit the given number over the network.<br>
<em>Realm:</em> Client and Server<br>
Expand All @@ -26,6 +26,7 @@ <h3><a id="utilbitsrequirednum" href="#utilbitsrequirednum">util.BitsRequired(nu
</p>
<ul>
<li><em>num</em> - The number being transmitted</li>
<li><em>signed</em> - Whether the number being transmitted is signed <em>(Added in 2.3.5)</em></li>
</ul>

<h3><a id="utilburnragdollrag-burn_time_scorch" href="#utilburnragdollrag-burn_time_scorch">util.BurnRagdoll(rag, burn_time, scorch)</a></h3>
Expand Down
27 changes: 16 additions & 11 deletions gamemodes/terrortown/gamemode/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,21 @@ function util.SimpleTime(seconds, fmt)
return StringFormat(fmt, m, s, ms)
end

-- Returns the number of bits required to network an integer
function util.BitsRequired(num, signed)
local bits, max = 0, 1
while max <= num do
bits = bits + 1
max = max + max
end

if signed then
bits = bits + 1
end

return bits
end

if SERVER then
function util.ExecFile(filePath, errorIfMissing)
if not FileExists(filePath, "GAME") then
Expand Down Expand Up @@ -554,20 +569,10 @@ function util.FormattedList(tbl, formatting)
return result
end

function util.BitsRequired(num)
local bits, max = 0, 1
while max <= num do
bits = bits + 1
max = max + max
end
return bits
end

local roleBits = nil
function util.RoleBits()
if not roleBits then
-- Add a bit to the required for this since we're sending it as signed to support ROLE_NONE (-1)
roleBits = math.max(8, util.BitsRequired(ROLE_MAX) + 1)
roleBits = math.max(8, util.BitsRequired(ROLE_MAX, true))
end

return roleBits
Expand Down
Loading