This commit is contained in:
Not 2022-03-28 13:14:30 +02:00
parent 70cd579079
commit b040fdd933
1 changed files with 194 additions and 106 deletions

View File

@ -1,4 +1,5 @@
-- Leaderboards written by Not -- Leaderboards written by Not
-- Reusable
local FILENAME = "leaderboard.txt" local FILENAME = "leaderboard.txt"
local lb = {} local lb = {}
@ -27,9 +28,50 @@ local AFK_TIMEOUT = TICRATE * 5
local AFK_BALANCE = TICRATE * 60 local AFK_BALANCE = TICRATE * 60
local PREVENT_JOIN_TIME = START_TIME + TICRATE * 5 local PREVENT_JOIN_TIME = START_TIME + TICRATE * 5
local GUI_OFF = 0x0
local GUI_SPLITS = 0x1
local GUI_ON = 0x2
-- patch caching function -- patch caching function
local cachePatches local cachePatches
local cv_gui = CV_RegisterVar({
name = "lb_gui",
defaultvalue = GUI_ON,
flags = 0,
PossibleValue = {Off = GUI_OFF, Splits = GUI_SPLITS, On = GUI_ON}
})
local cv_afk = CV_RegisterVar({
name = "lb_afk",
defaultvalue = 1,
flags = CV_NETVAR,
PossibleValue = CV_OnOff
})
local cv_enable = CV_RegisterVar({
name = "lb_enable",
defaultvalue = 1,
flags = CV_NETVAR | CV_CALL,
PossibleValue = CV_OnOff,
func = function(v)
disable = $ or not v.value
end
})
local cv_interrupt = CV_RegisterVar({
name = "lb_interrupt",
defaultvalue = 0,
flags = CV_NETVAR | CV_CALL,
PossibleValue = CV_OnOff,
func = function(v)
if v.value then
COM_BufInsertText(server, "allowteamchange yes")
end
end
})
local function lbID(map, flags) local function lbID(map, flags)
local id = tostring(map) local id = tostring(map)
if flags & F_SPBATK then if flags & F_SPBATK then
@ -99,16 +141,18 @@ else
end end
local function allowJoin(v) local function allowJoin(v)
local y if not cv_interrupt.value then
if v then local y
y = "yes" if v then
hud.enable("freeplay") y = "yes"
else hud.enable("freeplay")
y = "no" else
hud.disable("freeplay") y = "no"
end hud.disable("freeplay")
end
COM_BufInsertText(server, "allowteamchange " + y) COM_BufInsertText(server, "allowteamchange " + y)
end
end end
local function ingame() local function ingame()
@ -127,7 +171,8 @@ local function initLeaderboard(player)
else else
disable = disable or ingame() > 1 disable = disable or ingame() > 1
end end
player.afkTimeout = leveltime disable = $ or not cv_enable.value
player.afkTime = leveltime
end end
addHook("PlayerSpawn", initLeaderboard) addHook("PlayerSpawn", initLeaderboard)
@ -157,6 +202,33 @@ local function exitlevel(player, ...)
end end
COM_AddCommand("exit", exitlevel) COM_AddCommand("exit", exitlevel)
local function findMap(player, ...)
local search = ...
if search == nil then
return
end
for i = 1, #mapheaderinfo do
local map = mapheaderinfo[i]
if map == nil then
continue
end
if map.lvlttl:lower():find(search:lower()) then
CONS_Printf(
player,
string.format(
"%s - %s",
G_BuildMapName(i),
map.lvlttl
)
)
end
end
end
COM_AddCommand("findmap", findMap)
local function mapNotExists(player, map) local function mapNotExists(player, map)
CONS_Printf(player, string.format("Map doesn't exist: %s", map:upper())) CONS_Printf(player, string.format("Map doesn't exist: %s", map:upper()))
end end
@ -279,7 +351,9 @@ local function marquee(text, maxwidth)
return text:sub(pos, pos + maxwidth - 1) return text:sub(pos, pos + maxwidth - 1)
end end
-- Bats on ...
local bodium = {V_YELLOWMAP, V_GRAYMAP, V_BROWNMAP, 0} local bodium = {V_YELLOWMAP, V_GRAYMAP, V_BROWNMAP, 0}
local splitColor = {[true]=V_SKYMAP, [false]=V_REDMAP} local splitColor = {[true]=V_SKYMAP, [false]=V_REDMAP}
local splitSymbol = {[true]="-", [false]="+"} local splitSymbol = {[true]="-", [false]="+"}
@ -296,106 +370,115 @@ local function drawScoreboard(v, player)
return return
end end
for i, score in ipairs(m) do local gui = cv_gui.value
local name = score["name"] if leveltime < START_TIME or player.exiting or player.lives == 0 then
local skin = skins[score["skin"]] gui = GUI_ON
if skin == nil then end
skin = skins["sonic"]
end
local skinPatch = PATCH["FACERANK"][skin.name]
-- | OFFSET | + | PADDING | * |INDEX| if gui then
local h = ((200 / 4) + 4) + (skinPatch.height + 4) * (i - 1) for i, score in ipairs(m) do
local name = score["name"]
v.draw(4, h, skinPatch, V_HUDTRANS | VFLAGS, v.getColormap("sonic", score["color"])) local skin = skins[score["skin"]]
if player.name == name then if skin == nil then
v.draw(4, h, PATCH["CHILI"][(leveltime / 4) % 8], V_HUDTRANS | VFLAGS) skin = skins["sonic"]
end
-- SPB
if score["flags"] & F_SPBATK then
local scale = FRACUNIT / 4
drawitem(
v,
4 - 2,
h - 2,
scale,
PATCH["SPB"],
V_HUDTRANS | VFLAGS
)
if score["flags"] & F_SPBEXP then
drawitem(
v,
skinPatch.width,
h - 2,
scale,
PATCH["INV"][(leveltime / 4) % 6],
V_HUDTRANS | VFLAGS
)
end end
if score["flags"] & F_SPBBIG then local skinPatch = PATCH["FACERANK"][skin.name]
-- | OFFSET | + | PADDING | * |INDEX|
local h = ((200 / 4) + 4) + (skinPatch.height + 4) * (i - 1)
v.draw(4, h, skinPatch, V_HUDTRANS | VFLAGS, v.getColormap("sonic", score["color"]))
if player.name == name then
v.draw(4, h, PATCH["CHILI"][(leveltime / 4) % 8], V_HUDTRANS | VFLAGS)
end
-- SPB
if score["flags"] & F_SPBATK then
local scale = FRACUNIT / 4
drawitem( drawitem(
v, v,
4 - 2, 4 - 2,
h + skinPatch.height - 4, h - 2,
scale, scale,
PATCH["BIG"], PATCH["SPB"],
V_HUDTRANS | VFLAGS V_HUDTRANS | VFLAGS
) )
end if score["flags"] & F_SPBEXP then
if score["flags"] & F_SPBJUS then drawitem(
drawitem( v,
v, skinPatch.width,
skinPatch.width, h - 2,
h + skinPatch.height - 4, scale,
scale, PATCH["INV"][(leveltime / 4) % 6],
PATCH["HYUD"], V_HUDTRANS | VFLAGS
V_HUDTRANS | VFLAGS )
) end
end if score["flags"] & F_SPBBIG then
end drawitem(
v,
-- Shorten long names 4 - 2,
local stralign = "left" h + skinPatch.height - 4,
local MAXWIDTH = 70 scale,
local px = 6 PATCH["BIG"],
local py = 0 V_HUDTRANS | VFLAGS
if v.stringWidth(name) > MAXWIDTH then )
stralign = "thin" end
py = -1 if score["flags"] & F_SPBJUS then
if v.stringWidth(name, 0, "thin") > MAXWIDTH then drawitem(
stralign = "small" v,
py = 2 skinPatch.width,
if v.stringWidth(name, 0, "small") > MAXWIDTH then h + skinPatch.height - 4,
name = marquee(name, 15) scale,
PATCH["HYUD"],
V_HUDTRANS | VFLAGS
)
end end
end end
end
v.drawString( if gui == GUI_ON or (gui == GUI_SPLITS and showSplit)
px + skinPatch.width, -- Shorten long names
h + py, local stralign = "left"
name, local MAXWIDTH = 70
V_HUDTRANSHALF | V_ALLOWLOWERCASE | VFLAGS, local px = 6
stralign local py = 0
) if v.stringWidth(name) > MAXWIDTH then
stralign = "thin"
py = -1
if v.stringWidth(name, 0, "thin") > MAXWIDTH then
stralign = "small"
py = 2
if v.stringWidth(name, 0, "small") > MAXWIDTH then
name = marquee(name, 15)
end
end
end
-- Draw splits v.drawString(
if showSplit > 0 and score["splits"][prevLap] != nil then px + skinPatch.width,
local split = splits[prevLap] - score["splits"][prevLap] h + py,
v.drawString( name,
px + skinPatch.width, V_HUDTRANSHALF | V_ALLOWLOWERCASE | VFLAGS,
h + 8, stralign
splitSymbol[split < 0] + ticsToTime(abs(split)), )
V_HUDTRANSHALF | splitColor[split < 0] | VFLAGS
) -- Draw splits
else if showSplit and score["splits"][prevLap] != nil then
v.drawString( local split = splits[prevLap] - score["splits"][prevLap]
px + skinPatch.width, v.drawString(
h + 8, px + skinPatch.width,
ticsToTime(score["time"]), h + 8,
V_HUDTRANSHALF | bodium[min(i, 4)] | VFLAGS splitSymbol[split < 0] + ticsToTime(abs(split)),
) V_HUDTRANSHALF | splitColor[split < 0] | VFLAGS
)
else
v.drawString(
px + skinPatch.width,
h + 8,
ticsToTime(score["time"]),
V_HUDTRANSHALF | bodium[min(i, 4)] | VFLAGS
)
end
end
end end
end end
end end
@ -533,13 +616,13 @@ local function think()
end end
if disable then if disable then
if ingame() > 1 then if cv_afk.value and ingame() > 1 then
for p in players.iterate do for p in players.iterate do
if p.valid and not p.spectator and not p.exiting then if p.valid and not p.spectator and not p.exiting then
if p.cmd.buttons then if p.cmd.buttons then
p.afkTimeout = leveltime p.afkTime = leveltime
end end
if p.afkTimeout + AFK_BALANCE < leveltime then if p.afkTime + AFK_BALANCE < leveltime then
p.spectator = true p.spectator = true
chatprint("\x89" + p.name + " was moved to the other team for game balance", true) chatprint("\x89" + p.name + " was moved to the other team for game balance", true)
end end
@ -548,7 +631,7 @@ local function think()
else else
for p in players.iterate do for p in players.iterate do
if p.valid and not p.spectator then if p.valid and not p.spectator then
p.afkTimeout = leveltime p.afkTime = leveltime
end end
end end
end end
@ -567,7 +650,7 @@ local function think()
if ingame() == 1 then if ingame() == 1 then
if help then if help then
help = false help = false
chatprint("\x89Leaderboard Commands:\nretry exit findmap changelevel spba_clearcheats", true) chatprint("\x89Leaderboard Commands:\nretry exit findmap changelevel spba_clearcheats lb_gui", true)
end end
else else
help = true help = true
@ -611,19 +694,22 @@ local function think()
end end
end end
end end
if not (Flags & F_SPBATK) then
hud.enable("freeplay")
end
end end
local cv_teamchange = CV_FindVar("allowteamchange") local cv_teamchange = CV_FindVar("allowteamchange")
if p then if p then
if p.cmd.buttons then if p.cmd.buttons then
p.afkTimeout = leveltime p.afkTime = leveltime
end end
if leveltime > PREVENT_JOIN_TIME and p.afkTimeout + AFK_TIMEOUT > leveltime then if leveltime > PREVENT_JOIN_TIME and p.afkTime + AFK_TIMEOUT > leveltime then
if cv_teamchange.value then if cv_teamchange.value then
allowJoin(false) allowJoin(false)
end end
elseif p.afkTimeout + AFK_TIMEOUT < leveltime then elseif p.afkTime + AFK_TIMEOUT < leveltime then
if not cv_teamchange.value then if not cv_teamchange.value then
allowJoin(true) allowJoin(true)
end end
@ -653,5 +739,7 @@ addHook("IntermissionThinker", interThink)
local function netvars(net) local function netvars(net)
lb = net($) lb = net($)
splits = net($)
prevLap = net($)
end end
addHook("NetVars", netvars) addHook("NetVars", netvars)