1
0
mirror of https://github.com/LDDestroier/CC/ synced 2024-12-12 03:00:29 +00:00

Added P2 netlag assistance

Specifically, a gray circle will appear 3 spaces ahead of the red player to help give an idea of where they will turn, because of netlag.
This only shows up when using skynet, though.
P2 now also has a ping counter.
This commit is contained in:
LDDestroier 2019-02-15 00:53:32 -05:00 committed by GitHub
parent bff55611df
commit ebaec81a81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,7 +17,7 @@ local isColor = term.isColor()
-- lower value = faster game. I'd reccommend 0.1 for SMP play. -- lower value = faster game. I'd reccommend 0.1 for SMP play.
local gameDelayInit = 0.1 local gameDelayInit = 0.1
local doDrawPlayerNames = false local doDrawPlayerNames = false
local useSetVisible = true local useSetVisible = false
local initGrid = { local initGrid = {
x1 = -100, x1 = -100,
@ -204,6 +204,13 @@ local tsv = function(visible)
end end
end end
local round = function(num, places)
return math.floor(num * 10^places) / 10^places
end
-- used in skynet matches if you are player 2
local ping = 0
local copyTable local copyTable
copyTable = function(tbl, ...) copyTable = function(tbl, ...)
local output = {} local output = {}
@ -520,7 +527,7 @@ local drawGrid = function(x, y, onlyDrawGrid, useSetVisible)
local foreX, foreY local foreX, foreY
local backX, backY local backX, backY
local adjX, adjY local adjX, adjY
local trailChar, trailColor, trailAge, isPlayer local trailChar, trailColor, trailAge, isPlayer, isPredict
for sy = 1, scr_y do for sy = 1, scr_y do
bg[1][sy] = "" bg[1][sy] = ""
bg[2][sy] = "" bg[2][sy] = ""
@ -534,11 +541,18 @@ local drawGrid = function(x, y, onlyDrawGrid, useSetVisible)
backY = 1 + mathfloor(sy + (y / 2)) % #gridBack backY = 1 + mathfloor(sy + (y / 2)) % #gridBack
trailChar, trailColor, trailAge = getTrail(adjX, adjY) trailChar, trailColor, trailAge = getTrail(adjX, adjY)
isPlayer = false isPlayer = false
isPredict = false
if not onlyDrawGrid then if not onlyDrawGrid then
for i = 1, #player do for i = 1, #player do
if player[i].x == adjX and player[i].y == adjY then if player[i].x == adjX and player[i].y == adjY then
isPlayer = i isPlayer = i
break break
elseif (not isHost) and useSkynet and i == you and (
adjX == math.floor(player[i].x + (0.03 * round(ping, 0)) * math.cos(math.rad(player[i].direction * 90))) and
adjY == math.floor(player[i].y + (0.03 * round(ping, 0)) * math.sin(math.rad(player[i].direction * 90)))
) then
isPredict = i
break
end end
end end
end end
@ -546,6 +560,10 @@ local drawGrid = function(x, y, onlyDrawGrid, useSetVisible)
bg[1][sy] = bg[1][sy] .. dirArrow[player[isPlayer].direction] bg[1][sy] = bg[1][sy] .. dirArrow[player[isPlayer].direction]
bg[2][sy] = bg[2][sy] .. toblit[player[isPlayer].color[1]] bg[2][sy] = bg[2][sy] .. toblit[player[isPlayer].color[1]]
bg[3][sy] = bg[3][sy] .. grid.voidcol bg[3][sy] = bg[3][sy] .. grid.voidcol
elseif isPredict and (not onlyDrawGrid) and (not doesIntersectBorder(adjX, adjY)) then
bg[1][sy] = bg[1][sy] .. "o"
bg[2][sy] = bg[2][sy] .. grid.forecol
bg[3][sy] = bg[3][sy] .. grid.voidcol
else else
if (not onlyDrawGrid) and trailChar and trailColor then if (not onlyDrawGrid) and trailChar and trailColor then
trailColor = trailColor[1 + ((trailAge - 1) % #trailColor)] trailColor = trailColor[1 + ((trailAge - 1) % #trailColor)]
@ -609,13 +627,18 @@ local drawGrid = function(x, y, onlyDrawGrid, useSetVisible)
end end
end end
local render = function(useSetVisible) local render = function(useSetVisible, netTime)
local p = player[you] local p = player[you]
drawGrid(scrollX + scrollAdjX, scrollY + scrollAdjY, false, useSetVisible) drawGrid(scrollX + scrollAdjX, scrollY + scrollAdjY, false, useSetVisible)
termsetCursorPos(1,1) termsetCursorPos(1,1)
termsetTextColor(player[you].color[1]) termsetTextColor(player[you].color[1])
termsetBackgroundColor(tocolors[grid.voidcol]) termsetBackgroundColor(tocolors[grid.voidcol])
term.write("P" .. you) term.write("P" .. you)
term.setTextColor(colors.white)
if netTime and useSkynet then
ping = (os.epoch() - netTime) / round(72, 2)
term.write(" " .. tostring(ping) .. " ms")
end
if debugShowKeys then if debugShowKeys then
term.setCursorPos(1,2) term.setCursorPos(1,2)
term.write("dir = " .. player[you].direction .. " ") term.write("dir = " .. player[you].direction .. " ")
@ -921,6 +944,7 @@ local sendInfo = function(gameID)
name = player[you].name, name = player[you].name,
putTrail = isPuttingDown, putTrail = isPuttingDown,
gameID = gameID, gameID = gameID,
time = os.epoch(),
keysDown = isHost and nil or keysDown, keysDown = isHost and nil or keysDown,
trail = isHost and lastTrails or nil, trail = isHost and lastTrails or nil,
deadGuys = isHost and deadGuys or nil, deadGuys = isHost and deadGuys or nil,
@ -1016,19 +1040,28 @@ end
local setDirection = function(p, checkDir, lastDir) local setDirection = function(p, checkDir, lastDir)
if (lastDir == control.left) and (checkDir or p.direction) ~= 0 then if (lastDir == control.left) and (checkDir or p.direction) ~= 0 then
p.direction = 2 p.direction = 2
return true
elseif (lastDir == control.right) and (checkDir or p.direction) ~= 2 then elseif (lastDir == control.right) and (checkDir or p.direction) ~= 2 then
p.direction = 0 p.direction = 0
return true
elseif (lastDir == control.up) and (checkDir or p.direction) ~= 1 then elseif (lastDir == control.up) and (checkDir or p.direction) ~= 1 then
p.direction = -1 p.direction = -1
return true
elseif (lastDir == control.down) and (checkDir or p.direction) ~= -1 then elseif (lastDir == control.down) and (checkDir or p.direction) ~= -1 then
p.direction = 1 p.direction = 1
return true
elseif isPuttingDown == keysDown[control.release] then
return true
else
return false
end end
end end
local game = function() local game = function()
local outcome local outcome
local p, np, timeoutID, tID, evt local p, np, timeoutID, tID, evt, netTime
while true do while true do
netTime = nil
if isHost then if isHost then
sleep(gameDelay) sleep(gameDelay)
else else
@ -1040,6 +1073,8 @@ local game = function()
os.queueEvent("tron_complete", "timeout", isHost, player[nou].name) os.queueEvent("tron_complete", "timeout", isHost, player[nou].name)
parallel.waitForAny(function() imageAnim(images.timeout) end, waitForKey) parallel.waitForAny(function() imageAnim(images.timeout) end, waitForKey)
return return
elseif evt == "move_tick" then
netTime = tID
end end
end end
p = player[you] p = player[you]
@ -1052,8 +1087,10 @@ local game = function()
else else
setDirection(p, nil, control[lastDirectionPressed]) setDirection(p, nil, control[lastDirectionPressed])
isPuttingDown = not keysDown[control.release] isPuttingDown = not keysDown[control.release]
sendInfo(gamename)
end end
if keysDown[control.lookLeft] then if keysDown[control.lookLeft] then
scrollAdjX = scrollAdjX - 2 scrollAdjX = scrollAdjX - 2
end end
@ -1073,7 +1110,7 @@ local game = function()
if isHost then if isHost then
outcome = moveTick(true) outcome = moveTick(true)
else else
outcome = deadAnimation(true) outcome = deadAnimation(false)
end end
ageTrails() ageTrails()
if outcome == "end" then if outcome == "end" then
@ -1081,7 +1118,7 @@ local game = function()
else else
scrollX = p.x - mathfloor(scr_x / 2) scrollX = p.x - mathfloor(scr_x / 2)
scrollY = p.y - mathfloor(scr_y / 2) scrollY = p.y - mathfloor(scr_y / 2)
render(true) render(true, (not isHost) and netTime)
end end
end end
end end
@ -1097,6 +1134,8 @@ local networking = function()
if channel == port and type(msg) == "table" then if channel == port and type(msg) == "table" then
if type(msg.gameID) == "string" then if type(msg.gameID) == "string" then
if waitingForGame and (type(msg.new) == "number") then if waitingForGame and (type(msg.new) == "number") then
-- called while waiting for match
if msg.new < os.time() then if msg.new < os.time() then
isHost = false isHost = false
gamename = msg.gameID gamename = msg.gameID
@ -1113,13 +1152,17 @@ local networking = function()
player = player, player = player,
gameID = gamename, gameID = gamename,
new = isHost and (-math.huge) or (math.huge), new = isHost and (-math.huge) or (math.huge),
time = os.epoch(),
grid = initGrid grid = initGrid
}) })
waitingForGame = false waitingForGame = false
netKeysDown = {} netKeysDown = {}
os.queueEvent("new_game", gameID) os.queueEvent("new_game", gameID)
return gameID return gameID
elseif msg.gameID == gamename then elseif msg.gameID == gamename then
-- called during gameplay
if not isHost then if not isHost then
if type(msg.player) == "table" then if type(msg.player) == "table" then
player[nou].name = msg.name or player[nou].name player[nou].name = msg.name or player[nou].name
@ -1130,7 +1173,7 @@ local networking = function()
end end
end end
deadGuys = msg.deadGuys deadGuys = msg.deadGuys
os.queueEvent("move_tick") os.queueEvent("move_tick", msg.time)
end end
elseif type(msg.keysDown) == "table" then elseif type(msg.keysDown) == "table" then
netKeysDown = msg.keysDown netKeysDown = msg.keysDown
@ -1138,6 +1181,7 @@ local networking = function()
player[nou].putTrail = msg.putTrail player[nou].putTrail = msg.putTrail
player[nou].name = msg.name or player[nou].name player[nou].name = msg.name or player[nou].name
end end
end end
end end
end end
@ -1154,6 +1198,11 @@ local helpScreen = function()
Pan the camera with WASD. Pan the camera with WASD.
Hold SPACE to create gaps. Hold SPACE to create gaps.
If you're P2 (red), a gray
circle will indicate where
you'll turn, to help with
skynet's netlag.
That's basically it. That's basically it.
Press any key to go back. Press any key to go back.
]]) ]])
@ -1181,6 +1230,7 @@ local startGame = function()
gameID = gamename, gameID = gamename,
new = os.time(), new = os.time(),
gameDelay = gameDelayInit, gameDelay = gameDelayInit,
time = os.epoch(),
name = argumentName or player[you].name, name = argumentName or player[you].name,
grid = initGrid grid = initGrid
}) })