mirror of
https://github.com/LDDestroier/CC/
synced 2025-04-30 14:23:10 +00:00
Added Skynet, grayscale support (#7)
* Tried to add skynet * Update tron * Update tron * Update tron * Added grayscale support
This commit is contained in:
parent
a04d95be88
commit
5bde8f04f5
195
tron
195
tron
@ -7,8 +7,11 @@
|
|||||||
|
|
||||||
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)
|
||||||
@ -797,7 +868,9 @@ 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]
|
||||||
@ -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
|
||||||
@ -883,8 +956,10 @@ local networking = function()
|
|||||||
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")
|
||||||
@ -958,4 +1033,8 @@ local main = function()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
main()
|
if useSkynet then
|
||||||
|
parallel.waitForAny(main, skynet.listen)
|
||||||
|
else
|
||||||
|
main()
|
||||||
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user