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
-- Reusable
local FILENAME = "leaderboard.txt"
local lb = {}
@ -27,9 +28,50 @@ local AFK_TIMEOUT = TICRATE * 5
local AFK_BALANCE = TICRATE * 60
local PREVENT_JOIN_TIME = START_TIME + TICRATE * 5
local GUI_OFF = 0x0
local GUI_SPLITS = 0x1
local GUI_ON = 0x2
-- patch caching function
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 id = tostring(map)
if flags & F_SPBATK then
@ -99,16 +141,18 @@ else
end
local function allowJoin(v)
local y
if v then
y = "yes"
hud.enable("freeplay")
else
y = "no"
hud.disable("freeplay")
end
if not cv_interrupt.value then
local y
if v then
y = "yes"
hud.enable("freeplay")
else
y = "no"
hud.disable("freeplay")
end
COM_BufInsertText(server, "allowteamchange " + y)
COM_BufInsertText(server, "allowteamchange " + y)
end
end
local function ingame()
@ -127,7 +171,8 @@ local function initLeaderboard(player)
else
disable = disable or ingame() > 1
end
player.afkTimeout = leveltime
disable = $ or not cv_enable.value
player.afkTime = leveltime
end
addHook("PlayerSpawn", initLeaderboard)
@ -157,6 +202,33 @@ local function exitlevel(player, ...)
end
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)
CONS_Printf(player, string.format("Map doesn't exist: %s", map:upper()))
end
@ -279,7 +351,9 @@ local function marquee(text, maxwidth)
return text:sub(pos, pos + maxwidth - 1)
end
-- Bats on ...
local bodium = {V_YELLOWMAP, V_GRAYMAP, V_BROWNMAP, 0}
local splitColor = {[true]=V_SKYMAP, [false]=V_REDMAP}
local splitSymbol = {[true]="-", [false]="+"}
@ -296,106 +370,115 @@ local function drawScoreboard(v, player)
return
end
for i, score in ipairs(m) do
local name = score["name"]
local skin = skins[score["skin"]]
if skin == nil then
skin = skins["sonic"]
end
local skinPatch = PATCH["FACERANK"][skin.name]
local gui = cv_gui.value
if leveltime < START_TIME or player.exiting or player.lives == 0 then
gui = GUI_ON
end
-- | 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(
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
)
if gui then
for i, score in ipairs(m) do
local name = score["name"]
local skin = skins[score["skin"]]
if skin == nil then
skin = skins["sonic"]
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(
v,
4 - 2,
h + skinPatch.height - 4,
h - 2,
scale,
PATCH["BIG"],
PATCH["SPB"],
V_HUDTRANS | VFLAGS
)
end
if score["flags"] & F_SPBJUS then
drawitem(
v,
skinPatch.width,
h + skinPatch.height - 4,
scale,
PATCH["HYUD"],
V_HUDTRANS | VFLAGS
)
end
end
-- Shorten long names
local stralign = "left"
local MAXWIDTH = 70
local px = 6
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)
if score["flags"] & F_SPBEXP then
drawitem(
v,
skinPatch.width,
h - 2,
scale,
PATCH["INV"][(leveltime / 4) % 6],
V_HUDTRANS | VFLAGS
)
end
if score["flags"] & F_SPBBIG then
drawitem(
v,
4 - 2,
h + skinPatch.height - 4,
scale,
PATCH["BIG"],
V_HUDTRANS | VFLAGS
)
end
if score["flags"] & F_SPBJUS then
drawitem(
v,
skinPatch.width,
h + skinPatch.height - 4,
scale,
PATCH["HYUD"],
V_HUDTRANS | VFLAGS
)
end
end
end
v.drawString(
px + skinPatch.width,
h + py,
name,
V_HUDTRANSHALF | V_ALLOWLOWERCASE | VFLAGS,
stralign
)
if gui == GUI_ON or (gui == GUI_SPLITS and showSplit)
-- Shorten long names
local stralign = "left"
local MAXWIDTH = 70
local px = 6
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
if showSplit > 0 and score["splits"][prevLap] != nil then
local split = splits[prevLap] - score["splits"][prevLap]
v.drawString(
px + skinPatch.width,
h + 8,
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
)
v.drawString(
px + skinPatch.width,
h + py,
name,
V_HUDTRANSHALF | V_ALLOWLOWERCASE | VFLAGS,
stralign
)
-- Draw splits
if showSplit and score["splits"][prevLap] != nil then
local split = splits[prevLap] - score["splits"][prevLap]
v.drawString(
px + skinPatch.width,
h + 8,
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
@ -533,13 +616,13 @@ local function think()
end
if disable then
if ingame() > 1 then
if cv_afk.value and ingame() > 1 then
for p in players.iterate do
if p.valid and not p.spectator and not p.exiting then
if p.cmd.buttons then
p.afkTimeout = leveltime
p.afkTime = leveltime
end
if p.afkTimeout + AFK_BALANCE < leveltime then
if p.afkTime + AFK_BALANCE < leveltime then
p.spectator = true
chatprint("\x89" + p.name + " was moved to the other team for game balance", true)
end
@ -548,7 +631,7 @@ local function think()
else
for p in players.iterate do
if p.valid and not p.spectator then
p.afkTimeout = leveltime
p.afkTime = leveltime
end
end
end
@ -567,7 +650,7 @@ local function think()
if ingame() == 1 then
if help then
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
else
help = true
@ -611,19 +694,22 @@ local function think()
end
end
end
if not (Flags & F_SPBATK) then
hud.enable("freeplay")
end
end
local cv_teamchange = CV_FindVar("allowteamchange")
if p then
if p.cmd.buttons then
p.afkTimeout = leveltime
p.afkTime = leveltime
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
allowJoin(false)
end
elseif p.afkTimeout + AFK_TIMEOUT < leveltime then
elseif p.afkTime + AFK_TIMEOUT < leveltime then
if not cv_teamchange.value then
allowJoin(true)
end
@ -653,5 +739,7 @@ addHook("IntermissionThinker", interThink)
local function netvars(net)
lb = net($)
splits = net($)
prevLap = net($)
end
addHook("NetVars", netvars)