Added Skynet, grayscale support (#7)

* Tried to add skynet

* Update tron

* Update tron

* Update tron

* Added grayscale support
This commit is contained in:
LDDestroier 2018-11-20 16:29:36 -05:00 committed by GitHub
parent a04d95be88
commit 5bde8f04f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 148 additions and 69 deletions

217
tron
View File

@ -1,14 +1,17 @@
--[[ --[[
TRON Light Cycle Game TRON Light Cycle Game
programmed by LDDestroier programmed by LDDestroier
wget https://raw.githubusercontent.com/LDDestroier/CC/master/tron wget https://raw.githubusercontent.com/LDDestroier/CC/master/tron
--]] --]]
local port = 701 local port = 701
local scr_x, scr_y = term.getSize() local scr_x, scr_y = term.getSize()
local isColor = term.isColor()
-- lower value = faster game. I'd reccommend 0.1 for SMP play.
local gameDelayInit = 0.1
local gameDelayInit = 0.1 -- lower value = faster game. I'd reccommend 0.1 for SMP play.
local initGrid = { local initGrid = {
x1 = -100, x1 = -100,
y1 = -100, y1 = -100,
@ -64,21 +67,47 @@ local resetPlayers = function()
} }
} }
end end
local tArg = {...}
local useSkynet = (tArg[1] or ""):lower() == "skynet"
local skynetPath = "skynet"
local skynetURL = "https://raw.githubusercontent.com/osmarks/skynet/master/client.lua"
local modem = peripheral.find("modem") local modem, skynet
if (not modem) and ccemux then if useSkynet then
ccemux.attach("top", "wireless_modem") if fs.exists(skynetPath) then
skynet = dofile(skynetPath)
skynet.open(port)
else
local prog = http.get(skynetURL)
if prog then
local file = fs.open(skynetPath, "w")
file.write(prog.readAll())
file.close()
skynet = dofile(skynetPath)
skynet.open(port)
else
error("Could not download Skynet.")
end
end
else
modem = peripheral.find("modem") modem = peripheral.find("modem")
if (not modem) and ccemux then
ccemux.attach("top", "wireless_modem")
modem = peripheral.find("modem")
end
if modem then
modem.open(port)
else
error("You should attach a modem.")
end
end end
local transmit = function(port, message) local transmit = function(port, message)
modem.transmit(port, port, message) if useSkynet then
end skynet.send(port, message)
else
if modem then modem.transmit(port, port, message)
modem.open(port) end
else
error("You should attach a modem.")
end end
local gamename = "" local gamename = ""
@ -86,12 +115,77 @@ local isHost
local squareGrid = true local squareGrid = true
local waitingForGame = true local waitingForGame = true
local toblit = {
[0] = " ",
[colors.white] = "0",
[colors.orange] = "1",
[colors.magenta] = "2",
[colors.lightBlue] = "3",
[colors.yellow] = "4",
[colors.lime] = "5",
[colors.pink] = "6",
[colors.gray] = "7",
[colors.lightGray] = "8",
[colors.cyan] = "9",
[colors.purple] = "a",
[colors.blue] = "b",
[colors.brown] = "c",
[colors.green] = "d",
[colors.red] = "e",
[colors.black] = "f"
}
local tograyCol, tograyBlit = {
[0] = 0,
[colors.white] = colors.white,
[colors.orange] = colors.lightGray,
[colors.magenta] = colors.lightGray,
[colors.lightBlue] = colors.white,
[colors.yellow] = colors.white,
[colors.lime] = colors.lightGray,
[colors.pink] = colors.lightGray,
[colors.gray] = colors.gray,
[colors.lightGray] = colors.lightGray,
[colors.cyan] = colors.lightGray,
[colors.purple] = colors.gray,
[colors.blue] = colors.gray,
[colors.brown] = colors.gray,
[colors.green] = colors.gray,
[colors.red] = colors.white,
[colors.black] = colors.black
}, {}
local termblit, termwrite, termclear = term.blit, term.write, term.clear local tocolors = {}
for k,v in pairs(toblit) do
tocolors[v] = k
end
for k,v in pairs(tograyCol) do
tograyBlit[toblit[k]] = toblit[v]
end
local termwrite, termclear = term.write, term.clear
local termsetCursorPos, termgetCursorPos = term.setCursorPos, term.getCursorPos local termsetCursorPos, termgetCursorPos = term.setCursorPos, term.getCursorPos
local tableunpack, tableremove = unpack, table.remove local tableunpack, tableremove = unpack, table.remove
local mathfloor, mathceil, mathcos, mathsin = math.floor, math.ceil, math.cos, math.sin local mathfloor, mathceil, mathcos, mathsin = math.floor, math.ceil, math.cos, math.sin
local termsetTextColor, termsetBackgroundColor = term.setTextColor, term.setBackgroundColor
local termsetTextColor = function(col)
return term.setTextColor(isColor and col or tograyCol[col])
end
local termsetBackgroundColor = function(col)
return term.setBackgroundColor(isColor and col or tograyCol[col])
end
local termblit = function(char, text, back)
if isColor then
return term.blit(char, text, back)
else
return term.blit(
char,
text:gsub(".", tograyBlit),
back:gsub(".", tograyBlit)
)
end
end
local tsv = function(visible) local tsv = function(visible)
if term.current().setVisible then if term.current().setVisible then
@ -250,48 +344,21 @@ local images = {
for k,v in pairs(images) do for k,v in pairs(images) do
v.x = #v[1][1] v.x = #v[1][1]
v.y = #v[1] v.y = #v[1]
--[[
for i = 2, #v do
for line = 1, #v[i] do
images[k][i][line] = v[i][line]:gsub("f", " ")
end
end
--]]
end
local tocolors = {}
local toblit = {
[0] = " ",
[colors.white] = "0",
[colors.orange] = "1",
[colors.magenta] = "2",
[colors.lightBlue] = "3",
[colors.yellow] = "4",
[colors.lime] = "5",
[colors.pink] = "6",
[colors.gray] = "7",
[colors.lightGray] = "8",
[colors.cyan] = "9",
[colors.purple] = "a",
[colors.blue] = "b",
[colors.brown] = "c",
[colors.green] = "d",
[colors.red] = "e",
[colors.black] = "f"
}
for k,v in pairs(toblit) do
tocolors[v] = k
end end
local drawImage = function(im, x, y) local drawImage = function(im, x, y)
local cx, cy = termgetCursorPos() local cx, cy = termgetCursorPos()
termsetBackgroundColor(tocolors[initGrid.voidcol]) termsetBackgroundColor( tocolors[initGrid.voidcol] )
termsetTextColor(tocolors[initGrid.voidcol]) termsetTextColor( tocolors[initGrid.voidcol] )
for iy = 1, #im[1] do for iy = 1, #im[1] do
for ix = 1, #im[1][iy] do for ix = 1, #im[1][iy] do
termsetCursorPos(x+(ix-1),y+(iy-1)) termsetCursorPos(x+(ix-1),y+(iy-1))
if not (im[2][iy]:sub(ix,ix) == " " and im[3][iy]:sub(ix,ix) == " ") then if not (im[2][iy]:sub(ix,ix) == " " and im[3][iy]:sub(ix,ix) == " ") then
termblit(im[1][iy]:sub(ix,ix), im[2][iy]:sub(ix,ix), im[3][iy]:sub(ix,ix)) termblit(
im[1][iy]:sub(ix,ix),
im[2][iy]:sub(ix,ix),
im[3][iy]:sub(ix,ix)
)
end end
end end
end end
@ -476,7 +543,11 @@ local drawGrid = function(x, y, onlyDrawGrid, useSetVisible)
end end
for sy = 1, scr_y do for sy = 1, scr_y do
termsetCursorPos(1,sy) termsetCursorPos(1,sy)
termblit(bg[1][sy], bg[2][sy], bg[3][sy]) termblit(
bg[1][sy],
bg[2][sy],
bg[3][sy]
)
end end
if useSetVisible then if useSetVisible then
tsv(true) tsv(true)
@ -499,11 +570,11 @@ local pleaseWait = function()
termsetBackgroundColor(colors.black) termsetBackgroundColor(colors.black)
termsetTextColor(colors.gray) termsetTextColor(colors.gray)
termclear() termclear()
local tID = os.startTimer(0.2) local tID = os.startTimer(0.2)
local evt local evt
local txt = "Waiting for game" local txt = "Waiting for game"
while true do while true do
termsetCursorPos(mathfloor(scr_x / 2 - (#txt + maxPeriods) / 2), scr_y - 2) termsetCursorPos(mathfloor(scr_x / 2 - (#txt + maxPeriods) / 2), scr_y - 2)
termwrite(txt .. ("."):rep(periods)) termwrite(txt .. ("."):rep(periods))
@ -797,12 +868,14 @@ local game = function()
local outcome local outcome
local p, np local p, np
while true do while true do
if not isHost then if isHost then
sleep(gameDelay)
else
os.pullEvent("move_tick") os.pullEvent("move_tick")
end end
p = player[you] p = player[you]
np = player[nou] np = player[nou]
if isHost then if isHost then
setDirection(p, nil, control[lastDirectionPressed]) setDirection(p, nil, control[lastDirectionPressed])
setDirection(np, nil, control[netLastDirectionPressed]) setDirection(np, nil, control[netLastDirectionPressed])
@ -811,7 +884,7 @@ local game = function()
setDirection(p, nil, control[lastDirectionPressed]) setDirection(p, nil, control[lastDirectionPressed])
isPuttingDown = not keysDown[control.release] isPuttingDown = not keysDown[control.release]
end end
if keysDown[control.lookLeft] then if keysDown[control.lookLeft] then
scrollAdjX = scrollAdjX - 2 scrollAdjX = scrollAdjX - 2
end end
@ -824,7 +897,7 @@ local game = function()
if keysDown[control.lookDown] then if keysDown[control.lookDown] then
scrollAdjY = scrollAdjY + 1.5 scrollAdjY = scrollAdjY + 1.5
end end
scrollAdjX = scrollAdjX * 0.8 scrollAdjX = scrollAdjX * 0.8
scrollAdjY = scrollAdjY * 0.8 scrollAdjY = scrollAdjY * 0.8
@ -841,9 +914,6 @@ local game = function()
scrollY = p.y - mathfloor(scr_y / 2) scrollY = p.y - mathfloor(scr_y / 2)
render() render()
if isHost then
sleep(gameDelay)
end
end end
end end
end end
@ -853,10 +923,13 @@ local decision
local networking = function() local networking = function()
local evt, side, channel, repchannel, msg, distance local evt, side, channel, repchannel, msg, distance
while true do while true do
evt, side, channel, repchannel, msg, distance = os.pullEvent("modem_message") if useSkynet then
if channel == port and repchannel == port and type(msg) == "table" then evt, channel, msg = os.pullEvent("skynet_message")
else
evt, side, channel, repchannel, msg, distance = os.pullEvent("modem_message")
end
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
if msg.new < os.time() then if msg.new < os.time() then
isHost = false isHost = false
@ -878,13 +951,15 @@ local networking = function()
waitingForGame = false waitingForGame = false
netKeysDown = {} netKeysDown = {}
os.queueEvent("new_game", gameID) os.queueEvent("new_game", gameID)
elseif msg.gameID == gamename then elseif msg.gameID == gamename then
if not isHost then if not isHost then
if type(msg.player) == "table" then if type(msg.player) == "table" then
player = msg.player player = msg.player
for i = 1, #msg.trail do if msg.trail then
putTrailXY(unpack(msg.trail[i])) for i = 1, #msg.trail do
putTrailXY(unpack(msg.trail[i]))
end
end end
deadGuys = msg.deadGuys deadGuys = msg.deadGuys
os.queueEvent("move_tick") os.queueEvent("move_tick")
@ -895,7 +970,7 @@ local networking = function()
player[nou].putTrail = msg.putTrail player[nou].putTrail = msg.putTrail
end end
end end
end end
end end
end end
@ -910,7 +985,7 @@ local helpScreen = function()
Move with arrow keys. Move with arrow keys.
Pan the camera with WASD. Pan the camera with WASD.
Hold SPACE to create gaps. Hold SPACE to create gaps.
That's basically it. That's basically it.
Press any key to go back. Press any key to go back.
]]) ]])
@ -935,7 +1010,7 @@ local main = function()
for i = 1, 32 do for i = 1, 32 do
gamename = gamename .. string.char(math.random(1,126)) gamename = gamename .. string.char(math.random(1,126))
end end
waitingForGame = true waitingForGame = true
transmit(port, { transmit(port, {
player = player, player = player,
@ -958,4 +1033,8 @@ local main = function()
end end
end end
main() if useSkynet then
parallel.waitForAny(main, skynet.listen)
else
main()
end