Browse Source

Added selfhost - 1

tags/crossplatform
Madiwka 4 years ago
parent
commit
635aefa78d
3 changed files with 219 additions and 95 deletions
  1. +203
    -91
      main.lua
  2. +4
    -4
      src/AI.lua
  3. +12
    -0
      src/baseGame.lua

+ 203
- 91
main.lua View File

@@ -700,7 +700,7 @@ function startShake(duration, magnitude)
end end
function displayFPS() function displayFPS()
--love.window.setTitle(love.timer.getFPS()) --love.window.setTitle(love.timer.getFPS())
--love.window.setTitle(globalState .. " " .. gameState .. " " .. paddle_SPEED .. " " .. p1bonus .. " " .. player1.dy)
love.window.setTitle(globalState .. " " .. gameState .. " " .. paddle_SPEED .. " " .. p1bonus .. " " .. player1.dy)
if love.keyboard.isDown("space") then if love.keyboard.isDown("space") then
player1nukescore = 200 player1nukescore = 200
player1score = player1score + 0.2 player1score = player1score + 0.2
@@ -741,6 +741,16 @@ function love.update(dt)
end end
nettest(dt) nettest(dt)
end
if globalState == "selfhost" then
--print("Confcode: " .. confirmation)
if confirmation == "N" then
globalState = "nettest"
basegame(dt)
end
globalState = "selfhost"
selfHost(dt)
end end
if globalState == "clienttest" then if globalState == "clienttest" then
ts = ts + dt ts = ts + dt
@@ -1454,18 +1464,22 @@ function love.draw(dt)


baseDraw() baseDraw()
if (globalState == "nettest" or globalState == "clienttest") 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")
end end
if (globalState == "nettest" or globalState == "clienttest") and confirmation == "U" then
if (globalState == "nettest" or globalState == "clienttest" or globalState == "selfhost") and confirmation == "U" then
love.graphics.clear(50 / 255, 50 / 255, 50 / 255, 255) love.graphics.clear(50 / 255, 50 / 255, 50 / 255, 255)
love.graphics.printf("LOBBY FULL OR WRONG MODE CHOSEN", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center") love.graphics.printf("LOBBY FULL OR WRONG MODE CHOSEN", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center")
end end
if (globalState == "nettest" or globalState == "clienttest") and confirmation == "L" then
if (globalState == "nettest" or globalState == "clienttest" or globalState == "selfhost") and confirmation == "L" then
love.graphics.clear(50 / 255, 50 / 255, 50 / 255, 255) love.graphics.clear(50 / 255, 50 / 255, 50 / 255, 255)
love.graphics.printf("POOR CONNECTION TO SERVER", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center") love.graphics.printf("POOR CONNECTION TO SERVER", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center")
end end
if (globalState == "nettest" or globalState == "clienttest" or globalState == "selfhost") and confirmation == "S" then
love.graphics.clear(50 / 255, 50 / 255, 50 / 255, 255)
love.graphics.printf("INTERNAL SERVER WAITING", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center")
end
simpleScale.unSet() simpleScale.unSet()
end end
@@ -1698,111 +1712,209 @@ function checkCurrentServer(dt)


end end

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

udp:settimeout(0)
udp:setsockname('*', 12345)

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


for i = 1, maxBalls do
if ts > updaterate then
ts = 0
actualP1DATA = 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"
end
end
local data
local datanumtest = 0
local datawaspassed = false
if actualP2DATA then
datawaspassed = true
print("ReceivedINFO: " .. actualP2data)
confirmation = "N"
local p = split(actualP2data, '|')
if p[15] then
if tonumber(p[16]) > 90 then
confirmation = "L"
end
if p[15] ~= "CLIENT" then
confirmation = "U"
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
ball[1].disabled = false
print("illegal disabling")
end
if (ball[1].x > VIRTUAL_WIDTH/2) then
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("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)
lastSentKeyClient = p[1]
player2.y = tonumber(p[4])
end
end
if not datawaspassed then
datawaspassedtimer = datawaspassedtimer + 1
if datawaspassedtimer > 5 then
confirmation = "D"
datawaspassedtimer = 0
end
else
datawaspassedtimer = 0
end
else
confirmation = "S"
end
end end

+ 4
- 4
src/AI.lua View File

@@ -99,7 +99,7 @@ function recursiveCalculations(px, ex, ey, edx, edy, ifspecial)
--print("results: " .. bonus .. " " .. edx .. " " .. anstime .. " " .. (px-ex)) --print("results: " .. bonus .. " " .. edx .. " " .. anstime .. " " .. (px-ex))
-- if (ifspecial == 0) then -- if (ifspecial == 0) then
local answer = ey + bonus local answer = ey + bonus
love.window.setTitle(tostring(answer) .. "Basiccalc")
--love.window.setTitle(tostring(answer) .. "Basiccalc")
return ey + bonus return ey + bonus
-- else -- else
@@ -109,7 +109,7 @@ function recursiveCalculations(px, ex, ey, edx, edy, ifspecial)
local emulatedx = ex + distance local emulatedx = ex + distance
local emulatedy = VIRTUAL_HEIGHT-40 local emulatedy = VIRTUAL_HEIGHT-40
local answer = recursiveCalculations(px, emulatedx, emulatedy, edx, -edy, 0) local answer = recursiveCalculations(px, emulatedx, emulatedy, edx, -edy, 0)
love.window.setTitle(tostring(answer) .. "recursive calc bottom")
--love.window.setTitle(tostring(answer) .. "recursive calc bottom")
return answer return answer
end end
elseif edy == 0 then elseif edy == 0 then
@@ -129,7 +129,7 @@ function recursiveCalculations(px, ex, ey, edx, edy, ifspecial)
--print("results: " .. bonus .. " " .. edx .. " " .. anstime .. " " .. (px-ex)) --print("results: " .. bonus .. " " .. edx .. " " .. anstime .. " " .. (px-ex))
-- if (ifspecial == 0) then -- if (ifspecial == 0) then
local answer = ey + bonus local answer = ey + bonus
love.window.setTitle(tostring(answer) .. "Basiccalc")
--love.window.setTitle(tostring(answer) .. "Basiccalc")
return answer return answer
-- else -- else
-- return -1 -- return -1
@@ -139,7 +139,7 @@ function recursiveCalculations(px, ex, ey, edx, edy, ifspecial)
local emulatedy = 0 local emulatedy = 0
----print("results: " .. bonus .. " " .. edx .. " " .. anstime .. " " .. (VIRTUAL_WIDTH-ex)) ----print("results: " .. bonus .. " " .. edx .. " " .. anstime .. " " .. (VIRTUAL_WIDTH-ex))
local answer = recursiveCalculations(px, emulatedx, emulatedy, edx, -edy, 0) local answer = recursiveCalculations(px, emulatedx, emulatedy, edx, -edy, 0)
love.window.setTitle(tostring(answer) .. "recursivecalc")
--love.window.setTitle(tostring(answer) .. "recursivecalc")
return answer return answer
end end
end end

+ 12
- 0
src/baseGame.lua View File

@@ -688,6 +688,18 @@ function menuDraw()
end end
if gameState == "chooseIP" then if gameState == "chooseIP" then
IPselect = {} IPselect = {}
table.insert(
IPselect,
newButton(
"LANHost",
function()
globalState = "selfhost"
AGAINST_AI = 0
gameState = "1serve"
ball[1]:reset(1, 1)
end
)
)
if status == "offline" then if status == "offline" then
love.graphics.printf("UNABLE TO CONNECT", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center") love.graphics.printf("UNABLE TO CONNECT", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center")
elseif status == "nettest" then elseif status == "nettest" then


Loading…
Cancel
Save