Browse Source

Added LAN multiplayer - FINISHED

tags/crossplatform
Madiwka 4 years ago
parent
commit
e0b1af6260
2 changed files with 185 additions and 170 deletions
  1. +184
    -169
      main.lua
  2. +1
    -1
      src/baseGame.lua

+ 184
- 169
main.lua View File

@@ -750,6 +750,7 @@ function love.update(dt)
end end
globalState = "selfhost" globalState = "selfhost"
selfHost(dt) selfHost(dt)
IP = "127.0.0.1"
end end
if globalState == "clienttest" then if globalState == "clienttest" then
@@ -773,6 +774,7 @@ function love.textinput(t)
end end
end end
function nettest(dt) function nettest(dt)
print("nettest running")
if serverinit == false then if serverinit == false then
local socket = require "socket" local socket = require "socket"
local address, port = IP, 12345 local address, port = IP, 12345
@@ -834,11 +836,12 @@ function nettest(dt)
confirmation = "U" confirmation = "U"
end end


if p[15] then
if ball[1].disabled and ball[1].x > 20 and ball[1].x < VIRTUAL_WIDTH - 20 then if ball[1].disabled and ball[1].x > 20 and ball[1].x < VIRTUAL_WIDTH - 20 then
ball[1].disabled = false ball[1].disabled = false
print("illegal disabling") print("illegal disabling")
end end
if gameState ~= "1serve" then
if (ball[1].x > VIRTUAL_WIDTH/2) then if (ball[1].x > VIRTUAL_WIDTH/2) then
if tonumber(p[9]) > VIRTUAL_WIDTH/2 then if tonumber(p[9]) > VIRTUAL_WIDTH/2 then
die = tonumber(p[2]) die = tonumber(p[2])
@@ -882,7 +885,8 @@ function nettest(dt)
player2.y = tonumber(p[4]) player2.y = tonumber(p[4])
end end
end end
end
end
end end
until not data until not data
if not datawaspassed then if not datawaspassed then
@@ -1462,8 +1466,13 @@ end
function love.draw(dt) function love.draw(dt)
simpleScale.set() simpleScale.set()


if globalState == "selfhost" then
baseDraw() globalState = "nettest"
baseDraw()
globalState = "selfhost"
else
baseDraw()
end
if (globalState == "nettest" or globalState == "clienttest" or globalState == "selfhost") and confirmation == "D" then if (globalState == "nettest" or globalState == "clienttest" or globalState == "selfhost") and confirmation == "D" then
love.graphics.clear(50 / 255, 50 / 255, 50 / 255, 255) love.graphics.clear(50 / 255, 50 / 255, 50 / 255, 255)
love.graphics.printf("WAIT FOR OPPONENT", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center") love.graphics.printf("WAIT FOR OPPONENT", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center")
@@ -1624,6 +1633,8 @@ function resettinggenius()
nuclearanimation = 3 nuclearanimation = 3
timeIsSlow = false timeIsSlow = false
timeIsSlow2 = false timeIsSlow2 = false
serverinit = false
ts = 0
originalSpeed = 200 originalSpeed = 200
gameState = "menu" gameState = "menu"
globalState = "menu" globalState = "menu"
@@ -1714,177 +1725,152 @@ end
end end


end end
actualP2DATA = nil local gts = 0
actualP1DATA = nil hostinit = false
running = false player1ip = "127.0.0.1"
player1port = "12345"
player2ip = "none"
player2port = nil
local p1ping = 0 local p1ping = 0
local p2ping = 0 local p2ping = 0
local requesterip local requesterip
local requresterport local requresterport
local player1ip, player2ip, player1port, player2port = "127.0.0.1", "none", nil, nil
local gts = 0
function selfHost(dt) function selfHost(dt)
ts = ts + dt print("Server running")
gts = gts + dt if not hostinit then
if serverinit == false then
local socket = require('socket') local socket = require('socket')
udp = socket.udp() udp = socket.udp()
udp:setsockname('*', 12345) udp:setsockname('*', 12345)
udp:settimeout(0) udp:settimeout(0)
serverinit = true hostinit = true
end else
gts = gts + dt
if gts > 0.015 then if gts > 0.015 then
gts = 0 local data, msg_or_ip, port_or_nil
data, msg_or_ip, port_or_nil = udp:receivefrom() local p1data, p2data
if not running then repeat
print("launching server")
running = true
local socket = require 'socket'
local udp = socket.udp()


udp:settimeout(0) data, msg_or_ip, port_or_nil = udp:receivefrom()
udp:setsockname('*', 12345) if data then


end if data == "HELLO" then
print("Acting as server") requesterip = msg_or_ip
actualP2DATA = nil requesterport = port_or_nil
local data, msg_or_ip, port_or_nil else
local p1data, p2data print(string.sub(data,1,1) .. "Playerlist: " .. player1ip .. " " .. player2ip)
repeat if player2ip == msg_or_ip then
p2data = data .. '|' .. p2ping
data, msg_or_ip, port_or_nil = udp:receivefrom() p2ping = 0
if data then else
if player2ip == "none" and msg_or_ip ~= player1ip then
if data == "HELLO" then player2ip = msg_or_ip
requesterip = msg_or_ip p2data = data .. '|' .. p2ping
requesterport = port_or_nil p2ping = 0
else player2port = port_or_nil
print(string.sub(data,1,1) .. "Playerlist: " .. player1ip .. " " .. player2ip) print("CONNECTED: PLAYER 2 FROM: " .. player2ip)
if player2ip == msg_or_ip then elseif (player1ip ~= msg_or_ip and player2ip ~= msg_or_ip) then
p2data = data print("Lobby Full!" .. player1ip .. player2ip)
p2ping = 0 end
else
if player2ip == "none" and msg_or_ip ~= player1ip then
player2ip = msg_or_ip
p2data = data
p2ping = 0
player2port = port_or_nil
print("CONNECTED: PLAYER 2 FROM: " .. player2ip)
elseif (player1ip ~= msg_or_ip and player2ip ~= msg_or_ip) then
print("Lobby Full!" .. player1ip .. player2ip)
end
end
end
end
until not data
if player2ip ~= "none" then
p2ping = p2ping + 1
if p2ping > 100 then
if p1data then
udp:sendto(p1data .. '|' .. p2ping, player2ip, player2port)
end
print("PLAYER 2 DISCONNECTED")
p2data = nil
player2ip = "none"
player2port = nil
end
end
if p1data and player2port then
udp:sendto(p1data .. '|' .. p2ping, player2ip, player2port)
print("SENT TO " .. player2ip .. ":" .. player2port .. " : " .. string.sub(p1data,1,1))
end
if p2data and player1port then
actualP2DATA = p2data
--print("SENT TO " .. player1ip .. ":" .. player1port .. " : " .. string.sub(p2data,1,1))
--print("1::" .. p1data)
--print("2::" .. p2data)
--print("SENT1: " .. player2ip .. " " .. player2port .. " " .. p1data)
--print("SENT2: " .. player1ip .. " " .. player1port .. " " .. p2data)
end
if requesterip then
print("getting pnged!")
if player2ip == "none" then
udp:sendto("clienttest", requesterip, requesterport)
print("clienttest av to: " .. requesterip)
else
udp:sendto("full", requesterip, requesterport)
print("full to: " .. msg_or_ip)
end
requesterip, requesterport = nil
end end
end
print("Integrating nettest")


for i = 1, maxBalls do end
end
if ts > updaterate then until not data
ts = 0 if player1ip ~= "none" then
actualP1DATA = tostring(lastSentKey) .. p1ping = p1ping + 1
'|' .. tostring(ball[1].dy) .. end
'|' .. tostring(player2.y) .. if player2ip == "none" then
'|' .. tostring(player1.y) .. confirmation = "S"
'|' .. tostring(player1score) .. else
'|' .. tostring(player2score) .. print("Player2: " .. player2ip)
'|' .. tostring(player1nukescore) .. p2ping = p2ping + 1
'|' .. tostring(player2nukescore) .. if p2ping > 100 then
'|' .. tostring(ball[1].x) .. for i = 1, maxBalls do
'|' .. tostring(ball[1].y) .. ts = ts + dt
'|' .. gameState .. if ts > updaterate then
'|' .. tostring(ball[1].dx) .. udp:sendto(tostring(lastSentKey) ..
'|' .. tostring(ballSpeed) .. '|' .. tostring(ball[1].dy) ..
'|' .. tostring(paddle_SPEED) .. '|' .. tostring(player2.y) ..
"|HOST" '|' .. tostring(player1.y) ..
'|' .. tostring(player1score) ..
'|' .. tostring(player2score) ..
'|' .. tostring(player1nukescore) ..
'|' .. tostring(player2nukescore) ..
'|' .. tostring(ball[1].x) ..
'|' .. tostring(ball[1].y) ..
'|' .. gameState ..
'|' .. tostring(ball[1].dx) ..
'|' .. tostring(ballSpeed) ..
'|' .. tostring(paddle_SPEED) ..
"|HOST|".. p2ping, player2ip, player2port)
ts = 0
end
end
print("PLAYER 2 DISCONNECTED")
p2data = nil
player2ip = "none"
player2port = nil
end
end
if player2port then
for i = 1, maxBalls do
ts = ts + dt
if ts > updaterate then
udp:sendto(tostring(lastSentKey) ..
'|' .. tostring(ball[1].dy) ..
'|' .. tostring(player2.y) ..
'|' .. tostring(player1.y) ..
'|' .. tostring(player1score) ..
'|' .. tostring(player2score) ..
'|' .. tostring(player1nukescore) ..
'|' .. tostring(player2nukescore) ..
'|' .. tostring(ball[1].x) ..
'|' .. tostring(ball[1].y) ..
'|' .. gameState ..
'|' .. tostring(ball[1].dx) ..
'|' .. tostring(ballSpeed) ..
'|' .. tostring(paddle_SPEED) ..
"|HOST|".. p2ping, player2ip, player2port)
ts = 0
end
end end
end print("SENT TO " .. player2ip .. ":" .. player2port .. " : " ..lastSentKey)
end
local data
local datanumtest = 0 local datanumtest = 0
local datawaspassed = false local datawaspassed = false
if actualP2DATA then if p2data and player1port then
datawaspassed = true datawaspassed = true
print("ReceivedINFO: " .. actualP2data) print("ReceivedINFO: " .. p2data)
confirmation = "N" confirmation = "N"
local p = split(actualP2data, '|') local p = split(p2data, '|')
if p[15] then if p[15] then
if tonumber(p[16]) > 90 then if tonumber(p[16]) > 90 then
confirmation = "L" confirmation = "L"
end end
if p[15] ~= "CLIENT" then if p[15] ~= "CLIENT" then
confirmation = "U"
end
elseif p[1] == "RESPONSE" then
if p[2] == "1" then
elseif p[2] == "2" then
elseif p[2] == "3" then
end
else
confirmation = "U" confirmation = "U"
end end
elseif p[1] == "RESPONSE" then
else
confirmation = "U"
end
if ball[1].disabled and ball[1].x > 20 and ball[1].x < VIRTUAL_WIDTH - 20 then if p[15] then
ball[1].disabled = false if ball[1].disabled and ball[1].x > 20 and ball[1].x < VIRTUAL_WIDTH - 20 then
print("illegal disabling") ball[1].disabled = false
end print("illegal disabling")
if (ball[1].x > VIRTUAL_WIDTH/2) then end
if tonumber(p[9]) > VIRTUAL_WIDTH/2 then if gameState ~= "1serve" then
die = tonumber(p[2]) if (ball[1].x > VIRTUAL_WIDTH/2) then
lastSentKeyClient, if tonumber(p[9]) > VIRTUAL_WIDTH/2 then
ball[1].dy,
player2.y,
player1score,
player2score,
player1nukescore,
player2nukescore,
ball[1].x,
ball[1].y,
gameState,
ball[1].dx,
ballSpeed,
paddle_SPEED = p[1], die, tonumber(p[4]), tonumber(p[5]), tonumber(p[6]), tonumber(p[7]), tonumber(p[8]), tonumber(p[9]), tonumber(p[10]), p[11], tonumber(p[12]), tonumber(p[13]), tonumber(p[14])
print("ACCEPTED")
else
print("DECLINED")
end
else
if tonumber(p[9]) > VIRTUAL_WIDTH/2 then
die = tonumber(p[2]) die = tonumber(p[2])
lastSentKeyClient, lastSentKeyClient,
ball[1].dy, ball[1].dy,
@@ -1901,22 +1887,51 @@ function selfHost(dt)
paddle_SPEED = p[1], die, tonumber(p[4]), tonumber(p[5]), tonumber(p[6]), tonumber(p[7]), tonumber(p[8]), tonumber(p[9]), tonumber(p[10]), p[11], tonumber(p[12]), tonumber(p[13]), tonumber(p[14]) paddle_SPEED = p[1], die, tonumber(p[4]), tonumber(p[5]), tonumber(p[6]), tonumber(p[7]), tonumber(p[8]), tonumber(p[9]), tonumber(p[10]), p[11], tonumber(p[12]), tonumber(p[13]), tonumber(p[14])
print("ACCEPTED") print("ACCEPTED")
else else
print("DECLINED")
end
else
if tonumber(p[9]) > VIRTUAL_WIDTH/2 then
die = tonumber(p[2])
lastSentKeyClient,
ball[1].dy,
player2.y,
player1score,
player2score,
player1nukescore,
player2nukescore,
ball[1].x,
ball[1].y,
gameState,
ball[1].dx,
ballSpeed,
paddle_SPEED = p[1], die, tonumber(p[4]), tonumber(p[5]), tonumber(p[6]), tonumber(p[7]), tonumber(p[8]), tonumber(p[9]), tonumber(p[10]), p[11], tonumber(p[12]), tonumber(p[13]), tonumber(p[14])
print("ACCEPTED")
else
print("ENFORCED" .. ball[1].x .. " " .. ball[1].dx) print("ENFORCED" .. ball[1].x .. " " .. ball[1].dx)
lastSentKeyClient = p[1] lastSentKeyClient = p[1]
player2.y = tonumber(p[4]) player2.y = tonumber(p[4])
end end
end end
end
if not datawaspassed then end
datawaspassedtimer = datawaspassedtimer + 1 print("SENT TO " .. player1ip .. ":" .. player1port .. " : " .. string.sub(p2data,1,1))
if datawaspassedtimer > 5 then --print("1::" .. p1data)
confirmation = "D" --print("2::" .. p2data)
datawaspassedtimer = 0 --print("SENT1: " .. player2ip .. " " .. player2port .. " " .. p1data)
end --print("SENT2: " .. player1ip .. " " .. player1port .. " " .. p2data)
else end
datawaspassedtimer = 0 if requesterip then
print("getting pnged!")
if player2ip == "none" then
udp:sendto("clienttest", requesterip, requesterport)
print("clienttest av to: " .. requesterip)
else
udp:sendto("full", requesterip, requesterport)
print("full to: " .. msg_or_ip)
end end
else requesterip, requesterport = nil
confirmation = "S"
end end
end gts = 0
end
end
end

+ 1
- 1
src/baseGame.lua View File

@@ -697,6 +697,7 @@ function menuDraw()
AGAINST_AI = 0 AGAINST_AI = 0
gameState = "1serve" gameState = "1serve"
ball[1]:reset(1, 1) ball[1]:reset(1, 1)
end
) )
) )
table.insert( table.insert(
@@ -1311,6 +1312,5 @@ function clientsBaseGame(dt)
goalManager() goalManager()
powerAvailability() powerAvailability()
player1:update(dt) player1:update(dt)
love.event.quit()
player2:update(dt) player2:update(dt)
end end

||||||
x
 
000:0
Loading…
Cancel
Save