8 Commits

Author SHA1 Message Date
Not
db0696294b 4char padding, do proper string concat on map..checksum 2022-07-19 21:03:40 +02:00
Not
907c07a25d resave scores if checksums have been written 2022-07-19 20:31:03 +02:00
Not
00aa2060f3 use checksums to identify maps 2022-07-19 20:31:03 +02:00
Not
d7955feaf8 flash score on finish 2022-07-19 20:30:30 +02:00
Not
5a74a428b1 disable on battle maps 2022-07-08 19:58:34 +02:00
Not
4f138797a9 fix encore ruby icon 2022-07-08 19:22:10 +02:00
Not
22aa4ecd2f include zone and act in findmap 2022-05-27 15:07:57 +02:00
Not
1305fa2979 check only for max 2 ingame players 2022-05-25 19:00:55 +02:00

View File

@ -15,12 +15,27 @@ local EncoreInitial = nil
local cv_teamchange local cv_teamchange
local scoreTable 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 -- Tracks if stats have been written or not
local StatTrack = false local StatTrack = false
local UNCLAIMED = "Unclaimed Record" local UNCLAIMED = "Unclaimed Record"
local HELP_MESSAGE = "\x89Leaderboard Commands:\nretry exit findmap changelevel spba_clearcheats lb_gui rival scroll encore" 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 -- Retry / changelevel map
local nextMap = nil local nextMap = nil
@ -135,15 +150,36 @@ local cv_interrupt = CV_RegisterVar({
end 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 function setST(t, map, flags, scoreTable)
local mode = flags & ST_SEP local mode = flags & ST_SEP
local cks = mapChecksum(map)
t[mode] = t[mode] or {} t[mode] = t[mode] or {}
t[mode][map] = scoreTable t[mode][tostring(map)..(cks or "")] = scoreTable
end end
local function getST(t, map, flags) local function getST(t, map, flags)
local mode = flags & ST_SEP 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 end
local function setScoreTable(map, flags, scoreTable) local function setScoreTable(map, flags, scoreTable)
@ -207,7 +243,7 @@ local cv_spb_separate = CV_RegisterVar({
end 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 { return {
["map"] = map, ["map"] = map,
["name"] = name, ["name"] = name,
@ -216,7 +252,8 @@ local function score_t(map, name, skin, color, time, splits, flags, stat)
["time"] = time, ["time"] = time,
["splits"] = splits, ["splits"] = splits,
["flags"] = flags, ["flags"] = flags,
["stat"] = stat ["stat"] = stat,
["checksum"] = checksum
} }
end end
@ -238,12 +275,47 @@ local function stat_str(stat)
return "0" return "0"
end 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 -- Read the leaderboard
local f = io.open(FILENAME, "r") local f = io.open(FILENAME + ".txt", "r")
if f then if f then
-- track if scores checksums have been written
local checksumWrite = false
for l in f:lines() do for l in f:lines() do
-- Leaderboard is stored in the following tab separated format -- 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 = {} local t = {}
for word in (l+"\t"):gmatch("(.-)\t") do for word in (l+"\t"):gmatch("(.-)\t") do
table.insert(t, word) table.insert(t, word)
@ -272,17 +344,41 @@ if f then
end end
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( table.insert(
scoreTable, scoreTable,
score_t( score_t(
tonumber(t[1]), mapnum,
t[2], t[2],
t[3], t[3],
t[4], t[4],
tonumber(t[5]), tonumber(t[5]),
spl, spl,
flags, flags,
stats stats,
cks
) )
) )
@ -291,8 +387,13 @@ if f then
sortScores() sortScores()
f:close() f:close()
-- save scores
if checksumWrite then
saveScores()
end
else else
print("Failed to open file: ", FILENAME) print("Failed to open file: ", FILENAME + ".txt")
end end
function allowJoin(v) function allowJoin(v)
@ -310,23 +411,27 @@ function allowJoin(v)
end end
end end
local function ingame() -- Returns true if there is a single player ingame
local function singleplayer()
local n = 0 local n = 0
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
n = $ + 1 n = $ + 1
if n > 1 then
return false
end end
end end
return n end
return true
end end
local function initLeaderboard(player) local function initLeaderboard(player)
if disable and leveltime < START_TIME then if disable and leveltime < START_TIME then
disable = ingame() > 1 disable = not singleplayer()
else else
disable = disable or ingame() > 1 disable = disable or not singleplayer()
end 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 -- Restore encore mode to initial value
if disable and EncoreInitial != nil then if disable and EncoreInitial != nil then
@ -339,7 +444,7 @@ end
addHook("PlayerSpawn", initLeaderboard) addHook("PlayerSpawn", initLeaderboard)
local function doyoudare(player) local function doyoudare(player)
if ingame() > 1 or player.spectator then if not singleplayer() or player.spectator then
CONS_Printf(player, "How dare you") CONS_Printf(player, "How dare you")
return false return false
end end
@ -366,10 +471,6 @@ COM_AddCommand("exit", exitlevel)
local function findMap(player, ...) local function findMap(player, ...)
local search = ... local search = ...
if search == nil then
return
end
local hell = "\x85HELL" local hell = "\x85HELL"
local tol = { local tol = {
@ -378,7 +479,22 @@ local function findMap(player, ...)
[TOL_RACE] = "\x88Race\x80", [TOL_RACE] = "\x88Race\x80",
[TOL_MATCH] = "\x87\Battle\x80" [TOL_MATCH] = "\x87\Battle\x80"
} }
local lvltype, map 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 for i = 1, #mapheaderinfo do
map = mapheaderinfo[i] map = mapheaderinfo[i]
@ -386,20 +502,24 @@ local function findMap(player, ...)
continue continue
end 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) -- Only care for up to TOL_MATCH (0x10)
lvltype = tol[map.typeoflevel & 0x1F] or map.typeoflevel lvltype = tol[map.typeoflevel & 0x1F] or map.typeoflevel
-- If not battle print numlaps -- If not battle print numlaps
lvltype = (map.typeoflevel & (TOL_MATCH | TOL_COOP) and lvltype) or string.format("%s \x82%-2d\x80", lvltype, map.numlaps) lvltype = (map.typeoflevel & (TOL_MATCH | TOL_COOP) and lvltype)
or string.format("%s \x82%-2d\x80", lvltype, map.numlaps)
CONS_Printf( CONS_Printf(
player, player,
string.format( string.format(
"%s %-9s %-25s - %s\t%s", "%s %-9s %-30s - %s\t%s",
G_BuildMapName(i), G_BuildMapName(i),
lvltype, lvltype,
map.lvlttl, lvlttl,
map.subttl, map.subttl,
(map.menuflags & LF2_HIDEINMENU and hell) or "" (map.menuflags & LF2_HIDEINMENU and hell) or ""
) )
@ -687,6 +807,7 @@ addHook("MapLoad", function()
drawState = DS_DEFAULT drawState = DS_DEFAULT
scrollY = 50 * FRACUNIT scrollY = 50 * FRACUNIT
scrollAcc = 0 scrollAcc = 0
FlashTics = 0
allowJoin(true) allowJoin(true)
--printTable(lb) --printTable(lb)
@ -779,10 +900,11 @@ local FACERANK_DIM = 16
local FACERANK_SPC = FACERANK_DIM + 4 local FACERANK_SPC = FACERANK_DIM + 4
local function drawScore(v, player, pos, x, y, gui, faceRank, score, drawPos, textVFlags) local function drawScore(v, player, pos, x, y, gui, faceRank, score, drawPos, textVFlags)
textVFlags = textVFlags or V_HUDTRANSHALF textVFlags = textVFlags or V_HUDTRANSHALF
local me = player.name == score["name"]
--draw Patch/chili --draw Patch/chili
v.draw(x, y, faceRank, V_HUDTRANS | VFLAGS, v.getColormap("sonic", score["color"])) 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) v.draw(x, y, PATCH["CHILI"][(leveltime / 4) % 8], V_HUDTRANS | VFLAGS)
end end
@ -794,7 +916,7 @@ local function drawScore(v, player, pos, x, y, gui, faceRank, score, drawPos, te
bob + (y + FACERANK_DIM / 2) * FRACUNIT, bob + (y + FACERANK_DIM / 2) * FRACUNIT,
FRACUNIT / 6, FRACUNIT / 6,
PATCH["RUBY"], PATCH["RUBY"],
V_HUDTRANS V_HUDTRANS | VFLAGS
) )
end end
@ -878,11 +1000,16 @@ local function drawScore(v, player, pos, x, y, gui, faceRank, score, drawPos, te
end end
end end
local flashV = 0
if me and FlashTics > leveltime then
flashV = FlashVFlags[leveltime / FlashRate % (#FlashVFlags + 1)]
end
v.drawString( v.drawString(
x + FACERANK_DIM + px, x + FACERANK_DIM + px,
y + py, y + py,
name, name,
textVFlags | V_ALLOWLOWERCASE | VFLAGS, textVFlags | V_ALLOWLOWERCASE | VFLAGS | flashV,
stralign stralign
) )
@ -900,7 +1027,7 @@ local function drawScore(v, player, pos, x, y, gui, faceRank, score, drawPos, te
x + px + FACERANK_DIM, x + px + FACERANK_DIM,
y + 8, y + 8,
ticsToTime(score["time"], true), ticsToTime(score["time"], true),
textVFlags | bodium[min(pos, 4)] | VFLAGS textVFlags | bodium[min(pos, 4)] | VFLAGS | flashV
) )
end end
end end
@ -1088,7 +1215,8 @@ local function saveTime(player)
timeFinished, timeFinished,
splits, splits,
Flags, 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 -- Check if you beat your previous best
@ -1097,10 +1225,16 @@ local function saveTime(player)
if lbComp(newscore, scoreTable[i]) then if lbComp(newscore, scoreTable[i]) then
table.remove(scoreTable, i) table.remove(scoreTable, i)
S_StartSound(nil, 130) S_StartSound(nil, 130)
FlashTics = leveltime + TICRATE * 3
FlashRate = 1
FlashVFlags = YellowFlash
break break
else else
-- You suck lol -- You suck lol
S_StartSound(nil, 201) S_StartSound(nil, 201)
FlashTics = leveltime + TICRATE * 3
FlashRate = 3
FlashVFlags = RedFlash
scroll_to(player) scroll_to(player)
return return
end end
@ -1127,30 +1261,7 @@ local function saveTime(player)
StatTrack = true StatTrack = true
end end
local f = assert(io.open(FILENAME, "w")) saveScores()
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()
end end
-- DEBUGGING -- DEBUGGING
@ -1187,7 +1298,7 @@ local function think()
if disable then if disable then
if AntiAFK then if AntiAFK then
if ingame() > 1 then if not singleplayer() then
for p in players.iterate do for p in players.iterate do
if p.valid and not p.spectator and not p.exiting and p.lives > 0 then if p.valid and not p.spectator and not p.exiting and p.lives > 0 then
if p.cmd.buttons then if p.cmd.buttons then
@ -1224,7 +1335,7 @@ local function think()
if leveltime < START_TIME then if leveltime < START_TIME then
-- Help message -- Help message
if leveltime == START_TIME - TICRATE * 3 then if leveltime == START_TIME - TICRATE * 3 then
if ingame() == 1 then if singleplayer() then
if help then if help then
help = false help = false
chatprint(HELP_MESSAGE, true) chatprint(HELP_MESSAGE, true)