mirror of
https://github.com/LDDestroier/CC/
synced 2025-11-02 00:13:00 +00:00
Fixed ping indicator, added grid demo mode
My argument parser was added, which makes things much easier.
This commit is contained in:
324
tron.lua
324
tron.lua
@@ -89,10 +89,157 @@ local resetPlayers = function()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
local tArg = {...}
|
|
||||||
local useSkynet = (tArg[1] or ""):lower() == "skynet"
|
local function interpretArgs(tInput, tArgs)
|
||||||
local useOnce = (tArg[2] or tArg[1] or ""):lower() == "quick"
|
local output = {}
|
||||||
local argumentName = tArg[3] or tArg[2] or tArg[1] or nil
|
local errors = {}
|
||||||
|
local usedEntries = {}
|
||||||
|
for aName, aType in pairs(tArgs) do
|
||||||
|
output[aName] = false
|
||||||
|
for i = 1, #tInput do
|
||||||
|
if not usedEntries[i] then
|
||||||
|
if tInput[i] == aName and not output[aName] then
|
||||||
|
if aType then
|
||||||
|
usedEntries[i] = true
|
||||||
|
if type(tInput[i+1]) == aType or type(tonumber(tInput[i+1])) == aType then
|
||||||
|
usedEntries[i+1] = true
|
||||||
|
if aType == "number" then
|
||||||
|
output[aName] = tonumber(tInput[i+1])
|
||||||
|
else
|
||||||
|
output[aName] = tInput[i+1]
|
||||||
|
end
|
||||||
|
else
|
||||||
|
output[aName] = nil
|
||||||
|
errors[1] = errors[1] and (errors[1] + 1) or 1
|
||||||
|
errors[aName] = "expected " .. aType .. ", got " .. type(tInput[i+1])
|
||||||
|
end
|
||||||
|
else
|
||||||
|
usedEntries[i] = true
|
||||||
|
output[aName] = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
for i = 1, #tInput do
|
||||||
|
if not usedEntries[i] then
|
||||||
|
output[#output+1] = tInput[i]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return output, errors
|
||||||
|
end
|
||||||
|
|
||||||
|
local argData = {
|
||||||
|
["skynet"] = false, -- use Skynet HTTP multiplayer
|
||||||
|
["quick"] = false, -- start one game immediately
|
||||||
|
["griddemo"] = false, -- only move the grid
|
||||||
|
["--gridID"] = "number" -- grid ID to use
|
||||||
|
}
|
||||||
|
|
||||||
|
local gridFore, gridBack
|
||||||
|
local gridList = {
|
||||||
|
[1] = {
|
||||||
|
{
|
||||||
|
"+-------",
|
||||||
|
"| ",
|
||||||
|
"| ",
|
||||||
|
"| ",
|
||||||
|
"| "
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"+------------",
|
||||||
|
"| ",
|
||||||
|
"| ",
|
||||||
|
"| ",
|
||||||
|
"| ",
|
||||||
|
"| ",
|
||||||
|
"| ",
|
||||||
|
"| "
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[2] = {
|
||||||
|
{
|
||||||
|
" / ",
|
||||||
|
" / ",
|
||||||
|
" / ",
|
||||||
|
" / ",
|
||||||
|
"/__________"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
" / ",
|
||||||
|
" / ",
|
||||||
|
" / ",
|
||||||
|
" / ",
|
||||||
|
" / ",
|
||||||
|
" / ",
|
||||||
|
" / ",
|
||||||
|
"/_______________"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[3] = {
|
||||||
|
{
|
||||||
|
"+- -+------",
|
||||||
|
"| | ",
|
||||||
|
" | ",
|
||||||
|
". | ",
|
||||||
|
"+------+- --",
|
||||||
|
"| | ",
|
||||||
|
"| ",
|
||||||
|
"| . ",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"+- -+--------",
|
||||||
|
"| | ",
|
||||||
|
" | ",
|
||||||
|
" | ",
|
||||||
|
" | ",
|
||||||
|
"| | ",
|
||||||
|
"+--------+- -",
|
||||||
|
"| | ",
|
||||||
|
"| ",
|
||||||
|
"| ",
|
||||||
|
"| ",
|
||||||
|
"| | ",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[4] = {
|
||||||
|
{
|
||||||
|
" /\\ ",
|
||||||
|
" / \\ ",
|
||||||
|
" / \\ ",
|
||||||
|
"/ \\",
|
||||||
|
"\\ /",
|
||||||
|
" \\ / ",
|
||||||
|
" \\ / ",
|
||||||
|
" \\/ ",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
" /\\ ",
|
||||||
|
" / \\ ",
|
||||||
|
" / \\ ",
|
||||||
|
" / \\ ",
|
||||||
|
" / \\ ",
|
||||||
|
"/ \\",
|
||||||
|
"\\ /",
|
||||||
|
" \\ / ",
|
||||||
|
" \\ / ",
|
||||||
|
" \\ / ",
|
||||||
|
" \\ / ",
|
||||||
|
" \\/ ",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
local argList = interpretArgs({...}, argData)
|
||||||
|
|
||||||
|
local useSkynet = argList["skynet"]
|
||||||
|
local useOnce = argList["quick"]
|
||||||
|
local doGridDemo = argList["griddemo"]
|
||||||
|
if gridList[argList["--gridID"]] then
|
||||||
|
gridID = argList["--gridID"]
|
||||||
|
end
|
||||||
|
local argumentName = argList[1]
|
||||||
|
|
||||||
local skynetPath = "skynet"
|
local skynetPath = "skynet"
|
||||||
local skynetURL = "https://raw.githubusercontent.com/osmarks/skynet/master/client.lua"
|
local skynetURL = "https://raw.githubusercontent.com/osmarks/skynet/master/client.lua"
|
||||||
|
|
||||||
@@ -498,99 +645,6 @@ for k,v in pairs(control) do
|
|||||||
revControl[v] = k
|
revControl[v] = k
|
||||||
end
|
end
|
||||||
|
|
||||||
local gridFore, gridBack
|
|
||||||
local gridList = {
|
|
||||||
[1] = {
|
|
||||||
{
|
|
||||||
"+-------",
|
|
||||||
"| ",
|
|
||||||
"| ",
|
|
||||||
"| ",
|
|
||||||
"| "
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"+------------",
|
|
||||||
"| ",
|
|
||||||
"| ",
|
|
||||||
"| ",
|
|
||||||
"| ",
|
|
||||||
"| ",
|
|
||||||
"| ",
|
|
||||||
"| "
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[2] = {
|
|
||||||
{
|
|
||||||
" / ",
|
|
||||||
" / ",
|
|
||||||
" / ",
|
|
||||||
" / ",
|
|
||||||
"/__________"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
" / ",
|
|
||||||
" / ",
|
|
||||||
" / ",
|
|
||||||
" / ",
|
|
||||||
" / ",
|
|
||||||
" / ",
|
|
||||||
" / ",
|
|
||||||
"/_______________"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[3] = {
|
|
||||||
{
|
|
||||||
"+- -+------",
|
|
||||||
"| | ",
|
|
||||||
" | ",
|
|
||||||
". | ",
|
|
||||||
"+------+- --",
|
|
||||||
"| | ",
|
|
||||||
"| ",
|
|
||||||
"| . ",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"+- -+--------",
|
|
||||||
"| | ",
|
|
||||||
" | ",
|
|
||||||
" | ",
|
|
||||||
" | ",
|
|
||||||
"| | ",
|
|
||||||
"+--------+- -",
|
|
||||||
"| | ",
|
|
||||||
"| ",
|
|
||||||
"| ",
|
|
||||||
"| ",
|
|
||||||
"| | ",
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[4] = {
|
|
||||||
{
|
|
||||||
" /\\ ",
|
|
||||||
" / \\ ",
|
|
||||||
" / \\ ",
|
|
||||||
"/ \\",
|
|
||||||
"\\ /",
|
|
||||||
" \\ / ",
|
|
||||||
" \\ / ",
|
|
||||||
" \\/ ",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
" /\\ ",
|
|
||||||
" / \\ ",
|
|
||||||
" / \\ ",
|
|
||||||
" / \\ ",
|
|
||||||
" / \\ ",
|
|
||||||
"/ \\",
|
|
||||||
"\\ /",
|
|
||||||
" \\ / ",
|
|
||||||
" \\ / ",
|
|
||||||
" \\ / ",
|
|
||||||
" \\ / ",
|
|
||||||
" \\/ ",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
gridFore, gridBack = table.unpack(gridList[gridID])
|
gridFore, gridBack = table.unpack(gridList[gridID])
|
||||||
|
|
||||||
local dirArrow = {
|
local dirArrow = {
|
||||||
@@ -640,8 +694,8 @@ local drawGrid = function(x, y, onlyDrawGrid, useSetVisible)
|
|||||||
isPlayer = i
|
isPlayer = i
|
||||||
break
|
break
|
||||||
elseif (not isHost) and useSkynet and i == you and (
|
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
|
adjX == math.floor(player[i].x + (0.02 * 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)))
|
adjY == math.floor(player[i].y + (0.02 * round(ping, 0)) * math.sin(math.rad(player[i].direction * 90)))
|
||||||
) then
|
) then
|
||||||
isPredict = i
|
isPredict = i
|
||||||
break
|
break
|
||||||
@@ -744,7 +798,7 @@ local render = function(useSetVisible, netTime)
|
|||||||
end
|
end
|
||||||
term.setCursorPos(1,2)
|
term.setCursorPos(1,2)
|
||||||
if netTime and useSkynet then
|
if netTime and useSkynet then
|
||||||
ping = (os.epoch() - netTime) / round(72, 2)
|
ping = (os.epoch("utc") - netTime)
|
||||||
term.setTextColor(colors.white)
|
term.setTextColor(colors.white)
|
||||||
term.write(" " .. tostring(ping) .. " ms")
|
term.write(" " .. tostring(ping) .. " ms")
|
||||||
end
|
end
|
||||||
@@ -759,8 +813,12 @@ local pleaseWait = function()
|
|||||||
termclear()
|
termclear()
|
||||||
|
|
||||||
local tID = os.startTimer(0.2)
|
local tID = os.startTimer(0.2)
|
||||||
local evt
|
local evt, txt
|
||||||
local txt = "Waiting for game"
|
if useSkynet then
|
||||||
|
txt = "Waiting for Skynet game"
|
||||||
|
else
|
||||||
|
txt = "Waiting for modem game"
|
||||||
|
end
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
cwrite("(Press 'Q' to cancel)", 2)
|
cwrite("(Press 'Q' to cancel)", 2)
|
||||||
@@ -816,10 +874,18 @@ local makeMenu = function(x, y, options, doAnimate, scrollInfo, _cpos)
|
|||||||
local lastPos = cpos
|
local lastPos = cpos
|
||||||
if not doAnimate then
|
if not doAnimate then
|
||||||
drawImage(images.logo, mathceil(scr_x / 2 - images.logo.x / 2), 2)
|
drawImage(images.logo, mathceil(scr_x / 2 - images.logo.x / 2), 2)
|
||||||
|
if useSkynet then
|
||||||
|
term.setTextColor(colors.lightGray)
|
||||||
|
cwrite("Skynet Enabled", 2 + images.logo.y)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
local rend = function()
|
local rend = function()
|
||||||
if doAnimate then
|
if doAnimate then
|
||||||
drawImage(images.logo, mathceil(scr_x / 2 - images.logo.x / 2), 2)
|
drawImage(images.logo, mathceil(scr_x / 2 - images.logo.x / 2), 2)
|
||||||
|
if useSkynet then
|
||||||
|
term.setTextColor(colors.lightGray)
|
||||||
|
cwrite("Skynet Enabled", 2 + images.logo.y)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
for i = 1, #options do
|
for i = 1, #options do
|
||||||
if i == cpos then
|
if i == cpos then
|
||||||
@@ -1184,7 +1250,7 @@ local sendInfo = function(gameID)
|
|||||||
name = player[you].name,
|
name = player[you].name,
|
||||||
putTrail = isPuttingDown,
|
putTrail = isPuttingDown,
|
||||||
gameID = gameID,
|
gameID = gameID,
|
||||||
time = os.epoch(),
|
time = os.epoch("utc"),
|
||||||
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,
|
||||||
@@ -1407,7 +1473,7 @@ 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(),
|
time = os.epoch("utc"),
|
||||||
name = argumentName,
|
name = argumentName,
|
||||||
grid = initGrid
|
grid = initGrid
|
||||||
})
|
})
|
||||||
@@ -1492,7 +1558,7 @@ local startGame = function()
|
|||||||
gameID = gamename,
|
gameID = gamename,
|
||||||
new = os.time(),
|
new = os.time(),
|
||||||
gameDelay = gameDelayInit,
|
gameDelay = gameDelayInit,
|
||||||
time = os.epoch(),
|
time = os.epoch("utc"),
|
||||||
name = argumentName,
|
name = argumentName,
|
||||||
grid = initGrid
|
grid = initGrid
|
||||||
})
|
})
|
||||||
@@ -1524,19 +1590,43 @@ local main = function()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if useOnce then
|
if doGridDemo then
|
||||||
term.setCursorBlink(false)
|
parallel.waitForAny(function()
|
||||||
if useSkynet then
|
local step, gsX, gsY = 0, 0, 0
|
||||||
parallel.waitForAny(startGame, skynet.listen)
|
while true do
|
||||||
skynet.socket.close()
|
drawGrid(gsX, gsY, true)
|
||||||
else
|
step = step + 1
|
||||||
startGame()
|
if mathceil(step / 100) % 2 == 1 then
|
||||||
end
|
gsX = gsX + 1
|
||||||
|
else
|
||||||
|
gsY = gsY - 1
|
||||||
|
end
|
||||||
|
sleep(0.05)
|
||||||
|
end
|
||||||
|
end, function()
|
||||||
|
sleep(0.1)
|
||||||
|
local evt, key
|
||||||
|
repeat
|
||||||
|
evt, key = os.pullEvent("key")
|
||||||
|
until key == keys.q
|
||||||
|
sleep(0.1)
|
||||||
|
end)
|
||||||
else
|
else
|
||||||
if useSkynet then
|
if useOnce then
|
||||||
parallel.waitForAny(main, skynet.listen)
|
term.setCursorBlink(false)
|
||||||
skynet.socket.close()
|
if useSkynet then
|
||||||
|
parallel.waitForAny(startGame, skynet.listen)
|
||||||
|
skynet.socket.close()
|
||||||
|
else
|
||||||
|
startGame()
|
||||||
|
end
|
||||||
|
term.setCursorPos(1, scr_y)
|
||||||
else
|
else
|
||||||
main()
|
if useSkynet then
|
||||||
|
parallel.waitForAny(main, skynet.listen)
|
||||||
|
skynet.socket.close()
|
||||||
|
else
|
||||||
|
main()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user