diff --git a/tron b/tron index 94814a6..fb9b382 100644 --- a/tron +++ b/tron @@ -1,4 +1,8 @@ local scr_x, scr_y = term.getSize() +local modemID = 712 +local gamename = "" + +waitingForGame = true local grid = { x1 = -80, @@ -361,6 +365,7 @@ local evt local keysDown = {} local getInput = function() + os.pullEvent("new_game") while true do evt = {os.pullEvent()} if lockInput then @@ -459,6 +464,7 @@ end local game = function() local p = player[you] local outcome + os.pullEvent("new_game") while true do p.putTrail = not keysDown[control.release] if keysDown[control.left] and p.direction ~= 0 then @@ -488,6 +494,7 @@ local game = function() scrollAdjY = scrollAdjY * 0.8 outcome = moveTick() + sendInfo(gameID) if outcome == "end" then return @@ -502,6 +509,38 @@ local game = function() end local decision +local modem = peripheral.find("modem") +if modem then + modem.open(modemID) +else + error("You need a modem.") +end + +local sendInfo = function(gameID) + modem.transmit(modemID, modemID, { + player = player, + gameID = gameID + }) +end + +local networking = function() + local evt, side, channel, repchannel, msg, distance + while true do + evt, side, channel, repchannel, msg, distance = os.pullEvent("modem_message") + if channel == modemID and repchannel == modemID and type(msg) == "table" then + if type(msg.player) == "table" and type(msg.gameID) == "string" then + if waitingForGame and (type(msg.new) == "number") then + if msg.new < os.time() then + you, nou = you, nou + gamename = msg.gameID + end + waitingForGame = false + os.queueEvent("new_game", gameID) + end + end + end + end +end while true do decision = titleScreen() @@ -509,7 +548,16 @@ while true do if decision == "start" then trail = {} resetPlayers() - parallel.waitForAny(getInput, game) + gamename = "" + for i = 1, 32 do + gamename = gamename .. string.char(math.random(1,126)) + end + modem.transmit(modemID, modemID, { + player = player, + gameID = gamename, + new = os.time() + }) + parallel.waitForAny(getInput, game, networking) elseif decision == "demo" then parallel.waitForAny(getInput, gridDemo) elseif decision == "exit" then