13 Commits

View File

@ -15,12 +15,27 @@ local EncoreInitial = nil
local cv_teamchange
local scoreTable
-- Text flash on finish
local FlashTics = 0
local FlashRate
local FlashVFlags
local YellowFlash = {
[0] = V_YELLOWMAP,
[1] = V_ORANGEMAP,
[2] = 0
}
local RedFlash = {
[0] = V_REDMAP,
[1] = 0
}
-- Tracks if stats have been written or not
local StatTrack = false
local UNCLAIMED = "Unclaimed Record"
local HELP_MESSAGE = "\x89Leaderboard Commands:\nretry exit findmap changelevel spba_clearcheats lb_gui rival scroll encore"
local FILENAME = "leaderboard.txt"
local FILENAME = "leaderboard"
-- Retry / changelevel map
local nextMap = nil
@ -135,15 +150,36 @@ local cv_interrupt = CV_RegisterVar({
end
})
local function djb2(message)
local digest = 5381
for c in message:gmatch(".") do
digest = (($ << 5) + $) + string.byte(c)
end
return digest
end
local function mapChecksum(mapnum)
local mh = mapheaderinfo[mapnum]
if not mh then
return nil
end
local digest = string.format("%04x", djb2(mh.lvlttl+mh.subttl+mh.zonttl))
return string.sub(digest, #digest - 3)
end
local function setST(t, map, flags, scoreTable)
local mode = flags & ST_SEP
local cks = mapChecksum(map)
t[mode] = t[mode] or {}
t[mode][map] = scoreTable
t[mode][tostring(map)..(cks or "")] = scoreTable
end
local function getST(t, map, flags)
local mode = flags & ST_SEP
return t[mode] and t[mode][map] or nil
local cks = mapChecksum(map)
return t[mode] and t[mode][tostring(map)..(cks or "")] or nil
end
local function setScoreTable(map, flags, scoreTable)
@ -207,7 +243,7 @@ local cv_spb_separate = CV_RegisterVar({
end
})
local function score_t(map, name, skin, color, time, splits, flags, stat)
local function score_t(map, name, skin, color, time, splits, flags, stat, checksum)
return {
["map"] = map,
["name"] = name,
@ -216,7 +252,8 @@ local function score_t(map, name, skin, color, time, splits, flags, stat)
["time"] = time,
["splits"] = splits,
["flags"] = flags,
["stat"] = stat
["stat"] = stat,
["checksum"] = checksum
}
end
@ -238,12 +275,47 @@ local function stat_str(stat)
return "0"
end
local function writeScore(fh, score)
fh:write(
score["map"], "\t",
score["name"], "\t",
score["skin"], "\t",
score["color"], "\t",
score["time"], "\t",
table.concat(score["splits"], " "), "\t",
score["flags"], "\t",
stat_str(score["stat"]), "\t",
score["checksum"], "\n"
)
end
local function saveScores()
local f = assert(io.open(FILENAME + ".txt", "w"))
if f == nil then
print("Failed to open file for writing: " + FILENAME + ".txt")
return
end
for _, tbl in pairs(lb) do
for _, scoreTable in pairs(tbl) do
for _, score in ipairs(scoreTable) do
writeScore(f, score)
end
end
end
f:close()
end
-- Read the leaderboard
local f = io.open(FILENAME, "r")
local f = io.open(FILENAME + ".txt", "r")
if f then
-- track if scores checksums have been written
local checksumWrite = false
for l in f:lines() do
-- Leaderboard is stored in the following tab separated format
-- mapnum, name, skin, color, time, splits, flags, stat
-- mapnum, name, skin, color, time, splits, flags, stat, map_checksum
local t = {}
for word in (l+"\t"):gmatch("(.-)\t") do
table.insert(t, word)
@ -272,17 +344,41 @@ if f then
end
end
local mapnum = tonumber(t[1])
-- Checksums
local cks = t[9]
if cks == nil then
checksumWrite = true
cks = mapChecksum(mapnum)
-- We have no previous recognition of this map
-- remove it
if cks == nil then
local rsfh = io.open(FILENAME + "_removed_scores.txt", "a")
if not rsfh then
print("Failed to open file: ", FILENAME + "_removed_scores.txt")
continue
end
rsfh:write(l, "\n")
rsfh:close()
continue
end
end
table.insert(
scoreTable,
score_t(
tonumber(t[1]),
mapnum,
t[2],
t[3],
t[4],
tonumber(t[5]),
spl,
flags,
stats
stats,
cks
)
)
@ -291,8 +387,13 @@ if f then
sortScores()
f:close()
-- save scores
if checksumWrite then
saveScores()
end
else
print("Failed to open file: ", FILENAME)
print("Failed to open file: ", FILENAME + ".txt")
end
function allowJoin(v)
@ -310,23 +411,27 @@ function allowJoin(v)
end
end
local function ingame()
-- Returns true if there is a single player ingame
local function singleplayer()
local n = 0
for p in players.iterate do
if p.valid and not p.spectator then
n = $ + 1
if n > 1 then
return false
end
end
return n
end
return true
end
local function initLeaderboard(player)
if disable and leveltime < START_TIME then
disable = ingame() > 1
disable = not singleplayer()
else
disable = disable or ingame() > 1
disable = disable or not singleplayer()
end
disable = $ or not cv_enable.value
disable = $ or not cv_enable.value or not (maptol & (TOL_SP | TOL_RACE))
-- Restore encore mode to initial value
if disable and EncoreInitial != nil then
@ -339,7 +444,7 @@ end
addHook("PlayerSpawn", initLeaderboard)
local function doyoudare(player)
if ingame() > 1 or player.spectator then
if not singleplayer() or player.spectator then
CONS_Printf(player, "How dare you")
return false
end
@ -366,24 +471,57 @@ COM_AddCommand("exit", exitlevel)
local function findMap(player, ...)
local search = ...
if search == nil then
return
local hell = "\x85HELL"
local tol = {
[TOL_SP] = "\x81Race\x80", -- Nuked race maps
[TOL_COOP] = "\x8D\Battle\x80", -- Nuked battle maps
[TOL_RACE] = "\x88Race\x80",
[TOL_MATCH] = "\x87\Battle\x80"
}
local lvltype, map, lvlttl
local zoneAct = function(zone, act, levelflags)
local z = ""
if zone != "" then
z = " " + zone
elseif not(levelflags & LF_NOZONE) then
z = " Zone"
end
if act != "" then
z = $ + " " + act
end
return z
end
for i = 1, #mapheaderinfo do
local map = mapheaderinfo[i]
map = mapheaderinfo[i]
if map == nil then
continue
end
if map.lvlttl:lower():find(search:lower()) then
lvlttl = map.lvlttl + zoneAct(map.zonttl, map.actnum, map.levelflags)
if not search or lvlttl:lower():find(search:lower()) then
-- Only care for up to TOL_MATCH (0x10)
lvltype = tol[map.typeoflevel & 0x1F] or map.typeoflevel
-- If not battle print numlaps
lvltype = (map.typeoflevel & (TOL_MATCH | TOL_COOP) and lvltype)
or string.format("%s \x82%-2d\x80", lvltype, map.numlaps)
CONS_Printf(
player,
string.format(
"%s - %s",
"%s %-9s %-30s - %s\t%s",
G_BuildMapName(i),
map.lvlttl
lvltype,
lvlttl,
map.subttl,
(map.menuflags & LF2_HIDEINMENU and hell) or ""
)
)
end
@ -395,8 +533,6 @@ local function mapNotExists(player, map)
CONS_Printf(player, string.format("Map doesn't exist: %s", map:upper()))
end
local ALPH = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
local function changelevel(player, ...)
if not doyoudare(player) then
return
@ -418,6 +554,8 @@ local function changelevel(player, ...)
end
local mapnum = 0
local A = string.byte("A")
if tonumber(p) != nil then
-- Non extended map numbers
if tonumber(q) == nil then
@ -427,11 +565,12 @@ local function changelevel(player, ...)
mapnum = tonumber(p) * 10 + tonumber(q)
else
--Extended map numbers
p = ALPH:find(p) - 1
p = string.byte(p) - A
local qn = tonumber(q)
if qn == nil then
qn = ALPH:find(q) + 9
qn = string.byte(q) - A + 10
end
mapnum = 36 * p + qn + 100
end
@ -440,6 +579,12 @@ local function changelevel(player, ...)
return
end
-- Verify valid race level
if not (mapheaderinfo[mapnum].typeoflevel & (TOL_SP | TOL_RACE)) then
CONS_Printf(player, "Battle maps are not supported")
return
end
nextMap = G_BuildMapName(mapnum)
end
COM_AddCommand("changelevel", changelevel)
@ -662,6 +807,7 @@ addHook("MapLoad", function()
drawState = DS_DEFAULT
scrollY = 50 * FRACUNIT
scrollAcc = 0
FlashTics = 0
allowJoin(true)
--printTable(lb)
@ -754,10 +900,11 @@ local FACERANK_DIM = 16
local FACERANK_SPC = FACERANK_DIM + 4
local function drawScore(v, player, pos, x, y, gui, faceRank, score, drawPos, textVFlags)
textVFlags = textVFlags or V_HUDTRANSHALF
local me = player.name == score["name"]
--draw Patch/chili
v.draw(x, y, faceRank, V_HUDTRANS | VFLAGS, v.getColormap("sonic", score["color"]))
if player.name == score["name"] then
if me then
v.draw(x, y, PATCH["CHILI"][(leveltime / 4) % 8], V_HUDTRANS | VFLAGS)
end
@ -769,7 +916,7 @@ local function drawScore(v, player, pos, x, y, gui, faceRank, score, drawPos, te
bob + (y + FACERANK_DIM / 2) * FRACUNIT,
FRACUNIT / 6,
PATCH["RUBY"],
V_HUDTRANS
V_HUDTRANS | VFLAGS
)
end
@ -853,11 +1000,16 @@ local function drawScore(v, player, pos, x, y, gui, faceRank, score, drawPos, te
end
end
local flashV = 0
if me and FlashTics > leveltime then
flashV = FlashVFlags[leveltime / FlashRate % (#FlashVFlags + 1)]
end
v.drawString(
x + FACERANK_DIM + px,
y + py,
name,
textVFlags | V_ALLOWLOWERCASE | VFLAGS,
textVFlags | V_ALLOWLOWERCASE | VFLAGS | flashV,
stralign
)
@ -875,7 +1027,7 @@ local function drawScore(v, player, pos, x, y, gui, faceRank, score, drawPos, te
x + px + FACERANK_DIM,
y + 8,
ticsToTime(score["time"], true),
textVFlags | bodium[min(pos, 4)] | VFLAGS
textVFlags | bodium[min(pos, 4)] | VFLAGS | flashV
)
end
end
@ -1063,7 +1215,8 @@ local function saveTime(player)
timeFinished,
splits,
Flags,
stat_t(player.HMRs or pskin.kartspeed, player.HMRw or pskin.kartweight)
stat_t(player.HMRs or pskin.kartspeed, player.HMRw or pskin.kartweight),
mapChecksum(gamemap)
)
-- Check if you beat your previous best
@ -1072,10 +1225,16 @@ local function saveTime(player)
if lbComp(newscore, scoreTable[i]) then
table.remove(scoreTable, i)
S_StartSound(nil, 130)
FlashTics = leveltime + TICRATE * 3
FlashRate = 1
FlashVFlags = YellowFlash
break
else
-- You suck lol
S_StartSound(nil, 201)
FlashTics = leveltime + TICRATE * 3
FlashRate = 3
FlashVFlags = RedFlash
scroll_to(player)
return
end
@ -1102,30 +1261,7 @@ local function saveTime(player)
StatTrack = true
end
local f = assert(io.open(FILENAME, "w"))
if f == nil then
print("Failed to open file for writing: " + FILENAME)
return
end
for _, tbl in pairs(lb) do
for _, scoreTable in pairs(tbl) do
for _, score in ipairs(scoreTable) do
f:write(
score["map"], "\t",
score["name"], "\t",
score["skin"], "\t",
score["color"], "\t",
score["time"], "\t",
table.concat(score["splits"], " "), "\t",
score["flags"], "\t",
stat_str(score["stat"]), "\n"
)
end
end
end
f:close()
saveScores()
end
-- DEBUGGING
@ -1152,15 +1288,17 @@ local function getGamer()
end
end
local function think()
if nextMap then
COM_BufInsertText(server, "map " + nextMap)
local function changeMap()
COM_BufInsertText(server, "map " + nextMap + " -force -gametype race")
nextMap = nil
end
end
local function think()
if nextMap then changeMap() end
if disable then
if AntiAFK then
if ingame() > 1 then
if not singleplayer() then
for p in players.iterate do
if p.valid and not p.spectator and not p.exiting and p.lives > 0 then
if p.cmd.buttons then
@ -1197,7 +1335,7 @@ local function think()
if leveltime < START_TIME then
-- Help message
if leveltime == START_TIME - TICRATE * 3 then
if ingame() == 1 then
if singleplayer() then
if help then
help = false
chatprint(HELP_MESSAGE, true)
@ -1309,10 +1447,7 @@ end
addHook("ThinkFrame", think)
local function interThink()
if nextMap then
COM_BufInsertText(server, "map " + nextMap)
nextMap = nil
end
if nextMap then changeMap() end
if not cv_teamchange then
cv_teamchange = CV_FindVar("allowteamchange")