aboutsummaryrefslogtreecommitdiffstats
path: root/Gamestates/Multiplayer
diff options
context:
space:
mode:
Diffstat (limited to 'Gamestates/Multiplayer')
-rw-r--r--Gamestates/Multiplayer/InGame.lua201
-rw-r--r--Gamestates/Multiplayer/Internet.lua0
-rw-r--r--Gamestates/Multiplayer/Local.lua165
-rw-r--r--Gamestates/Multiplayer/Protocol.lua35
4 files changed, 357 insertions, 44 deletions
diff --git a/Gamestates/Multiplayer/InGame.lua b/Gamestates/Multiplayer/InGame.lua
new file mode 100644
index 0000000..f94be12
--- /dev/null
+++ b/Gamestates/Multiplayer/InGame.lua
@@ -0,0 +1,201 @@
+--[[
+ Gamestates/Multiplayer/InGame.lua
+ Multiplayer mode
+ by piernov
+]]--
+local GUI = { InGame = require("GUI/InGame")}
+local Game = require("Gamestates/Game")
+local Protocol = require("Gamestates/Multiplayer/Protocol")
+
+local InGame = {}
+
+function InGame:enter(Local) -- Initialize or reset variables when entering game
+ Previous = "Gamestates/Multiplayer/Local"
+ InGame.Keypressed = {{}} -- 2D array for storage of button pressed
+ InGame.Hints = {} -- Hints displayed for local player
+ InGame.opponentHints = {} -- Hints displayed for remote player
+ InGame.state = "playing" -- Default to "playing" but it doesn't mean anything
+ InGame.localLife = {}
+ InGame.remoteLife = {}
+
+ InGame.host = Local.host
+ InGame.client = Local.client
+
+ InGame.opponentAnswer = Game.genAnswer() -- Generate answer for the opponent
+
+ for o = 0, 9 do
+ table.insert(InGame.remoteLife, { Type = "line", LineWidth = 0.015, Position = { x1 = 0.135, y1 = 0.61+0.02*o, x2 = 0.185, y2 = 0.61+0.02*o}, Colors = {171, 0, 0, 255} }) -- Display life bar content
+ end
+
+ for o = 0, 9 do
+ table.insert(InGame.localLife, { Type = "line", LineWidth = 0.022, Position = { x1 = 0.535+0.0275*o, y1 = 0.66, x2 = 0.535+0.0275*o, y2 = 0.71}, Colors = {171, 0, 0, 255} }) -- Display life bar content
+ end
+
+ InGame:resize() -- Call the function filling the canvas
+
+ InGame.Musiclowlife = love.audio.newSource("Resources/BlueMind midlife.ogg")
+ InGame.Music = love.audio.newSource("Resources/BlueMind.ogg")
+ InGame.Music:setLooping(true)
+ InGame.Music:play()
+end
+
+function InGame:resize() -- Called in :enter() and when the window is resized
+ InGame.Display = love.graphics.newCanvas(Screen.width, Screen.height) -- Create the canvas containing base interface to avoid drawing it entirely each frame
+ love.graphics.setCanvas(InGame.Display)
+ love.graphics.setFont(Fonts[3]) -- Use font with size 3% by default
+ InGame.Display:clear()
+ for id, polygon in ipairs(GUI.InGame.Polygons) do
+ GUI.InGame.drawPolygon(polygon)
+ end
+ love.graphics.setCanvas()
+end
+
+function InGame:init(host, client)
+ Game.init()
+ GUI.InGame.loadInterface()
+ GUI.InGame.loadMultiplayerInterface()
+end
+
+function InGame:draw()
+ love.graphics.setColor(255, 255, 255, 255)
+ love.graphics.setBlendMode('premultiplied')
+ love.graphics.draw(InGame.Display)
+ love.graphics.setBlendMode('alpha')
+
+ for num, line in ipairs(InGame.Hints) do
+ for k, poly in ipairs(line) do
+ GUI.InGame.drawPolygon(poly)
+ end
+ end
+
+ for line, keys in ipairs(InGame.Keypressed) do
+ for id, num in ipairs(keys) do
+ GUI.InGame.drawPolygon({ Type = "rectangle", DrawMode = "fill", Position = { x = 0.50+(id-1)*0.075, y = 0.15+(line-1)*0.075-0.025}, Dimension = {width = 0.04, height = 0.04}, Colors = GUI.InGame.Colors[num] })
+ end
+ end
+
+ for num, poly in ipairs(InGame.localLife) do
+ GUI.InGame.drawPolygon(poly)
+ end
+
+ for num, poly in ipairs(InGame.remoteLife) do
+ GUI.InGame.drawPolygon(poly)
+ end
+
+ for id, num in ipairs(InGame.opponentAnswer) do
+ GUI.InGame.drawPolygon({ Type = "rectangle", DrawMode = "fill", Position = { x = 0.08+(id-1)*0.075+0.005, y = 0.12+0.005}, Dimension = {width = 0.035, height = 0.04}, Colors = GUI.InGame.Colors[num] }) -- Top left boxes content displaying opponent answer
+ end
+
+-- Display popup for win/loose and found answer
+ if InGame.state == "found" then
+ GUI.InGame.displayPopup("Found !")
+ elseif InGame.state == "not-found" then
+ GUI.InGame.displayPopup("Not found...")
+ elseif InGame.state == "win" then
+ GUI.InGame.displayPopup("You win !")
+ elseif InGame.state == "loose" then
+ GUI.InGame.displayPopup("You loose...")
+ end
+-- End
+
+ GUI.InGame.drawPolygon({ Type = "print", Text = InGame.client.name, Position = { x = 0.15, y = 0.06}, Colors = {0, 0, 171, 255} }) -- Display remote player's name
+ GUI.InGame.drawPolygon({ Type = "print", Text = Config.player_name.text, Position = { x = 0.60, y = 0.06}, Colors = {0, 0, 171, 255} }) -- Display local player's name
+end
+
+function InGame:update(dt)
+ local event = InGame.host:service()
+
+ while event ~= nil do -- Handle network events
+ Protocol.parseGlobalEvents(event)
+
+ if event.type == "receive" then
+ command, args = event.data:match("(.*): (.*)") -- Split received message : "<COMMAND>: <ARGS>"
+ if command == "ANSWER" then
+ local hints = table.concat(Game.checkKeys({args:match("(%d)(%d)(%d)(%d)")}, InGame.opponentAnswer)) -- Process received answers
+ if hints == "1111" then -- Only well-placed hints = answer found
+ event.peer:send("FOUND: TRUE", 0, "reliable")
+ table.remove(InGame.localLife, 1) -- Loss of 1 life point
+ if #InGame.localLife < 1 then -- Empty life
+ InGame.state = "loose"
+ else
+ if #InGame.localLife == 2 then
+ InGame.Music:stop()
+ InGame.Musiclowlife:setLooping(true)
+ InGame.Musiclowlife:play()
+ end
+ InGame.opponentAnswer = Game.genAnswer() -- Generate a new answer
+ InGame.opponentHints = {}
+ end
+ else
+ event.peer:send("HINTS: " .. hints, 0, "reliable")
+ end
+ elseif command == "FOUND" and args == "TRUE" then
+ table.remove(InGame.remoteLife, 1) -- Opponent loose 1 life point
+ if #InGame.remoteLife < 1 then -- Opponent's life is empty, we won
+ InGame.state = "win"
+ else
+ InGame.Keypressed = {{}}
+ InGame.Hints = {}
+ InGame.state = "found"
+ end
+ elseif command == "FOUND" and args == "FALSE" then
+ table.remove(InGame.remoteLife, 1) -- Opponent loose 1 life point
+ InGame.opponentAnswer = Game.genAnswer() -- Generate a new answer
+ InGame.opponentHints = {}
+ elseif command == "HINTS" then
+ table.insert(InGame.Hints, Game.addHints(args, #InGame.Keypressed))
+ InGame.Keypressed[#InGame.Keypressed+1] = {}
+ InGame.state = "playing"
+ if #InGame.Keypressed > 7 then
+ table.remove(InGame.localLife, 1) -- Loss of 1 life point
+ InGame.Keypressed = {{}}
+ InGame.Hints = {}
+ InGame.state = "not-found"
+ event.peer:send("FOUND: FALSE", 0, "reliable")
+ end
+ end
+ end
+ event = InGame.host:service() -- Process next event
+ end
+end
+
+function InGame:mousepressed(x, y, b) -- Handle mouse
+ if InGame.state == "found" or InGame.state == "not-found" then -- Click anywhere to hide the "found" popup message
+ InGame.state = "playing"
+ end
+
+ x = x/Screen.width -- relative coordinates
+ y = y/Screen.height
+ if 0.875 < y and y < 0.915 and InGame.state ~= "validating" then -- lower part of the screen, and don't do anything if we're waiting for hints from peer
+ if x > 0.45 then
+ for i = 0,5 do
+ if 0.50+i*0.075 < x and x < 0.50+i*0.075+0.04 then
+ if #InGame.Keypressed > 7 or #InGame.Keypressed[#InGame.Keypressed] >= 4 then -- Line already full
+ return
+ else
+ for k,v in pairs(InGame.Keypressed[#InGame.Keypressed]) do
+ if v == i+1 then
+ return -- Disallow multiple identical colors in same line
+ end
+ end
+
+ table.insert(InGame.Keypressed[#InGame.Keypressed], i+1) -- Click detected on a color-button
+ end
+ end
+ end
+ elseif 0.25 < x and x < 0.375 and #InGame.Keypressed <= 7 and #InGame.Keypressed[#InGame.Keypressed] == 4 then -- Clik on Ok button
+ local peer = InGame.host:get_peer(InGame.client.index)
+ InGame.state = "validating"
+ peer:send("ANSWER: " .. table.concat(InGame.Keypressed[#InGame.Keypressed]), 0, "reliable")
+ elseif #InGame.Keypressed <= 7 and 0.075 < x and x < 0.200 and #InGame.Keypressed[#InGame.Keypressed] > 0 then -- Click on reset button
+ InGame.Keypressed[#InGame.Keypressed] = {}
+ end
+ end
+end
+
+function InGame:leave()
+ InGame.Music:stop()
+ InGame.Musiclowlife:stop()
+end
+
+return InGame
diff --git a/Gamestates/Multiplayer/Internet.lua b/Gamestates/Multiplayer/Internet.lua
deleted file mode 100644
index e69de29..0000000
--- a/Gamestates/Multiplayer/Internet.lua
+++ /dev/null
diff --git a/Gamestates/Multiplayer/Local.lua b/Gamestates/Multiplayer/Local.lua
index aaa13f6..7158866 100644
--- a/Gamestates/Multiplayer/Local.lua
+++ b/Gamestates/Multiplayer/Local.lua
@@ -1,3 +1,11 @@
+--[[
+ Gamestates/Multiplayer/Local.lua
+ Local connect menu
+ by piernov
+
+ Comment: we really miss a Bonjour/Avahi Lua binding, only Android NSD is (barely) supported…
+]]--
+
local Local = {}
require "enet"
@@ -5,16 +13,20 @@ require "enet"
local Gui = require "Quickie"
local Utils = require "Utils"
+local Protocol = require "Gamestates/Multiplayer/Protocol"
+
local connect, connected = false, false
Local.server_host = { text = "" }
-Local.myservice = { name = "Mastermind", ip = "*", port = "5678" }
+Local.myservice = { name = "BlueMind", ip = "*", port = 5678 }
+Local.client = {}
-if love.system.getOS() == "Android" then
+-- Android NSD events callback
+if love.system.getOS() == "Android" then -- Won't work on other OS…
Local.services = {}
-
function love.handlers.serviceregistered(a,b,c,d)
SDL.log("ServiceRegistered: " .. a)
+ Local.myservice.name = a
end
function love.handlers.discoverystarted(a,b,c,d)
@@ -37,67 +49,128 @@ if love.system.getOS() == "Android" then
Local.services[a] = nil
end
end
+-- End
+
+function Local:enter()
+ Previous = "Gamestates/Multiplayer"
+ connect, connected = false, false
+end
function Local:init()
- Local.host = enet.host_create(Local.myservice.ip .. ":" .. Local.myservice.port)
+ Local.host = enet.host_create(Local.myservice.ip .. ":" .. Local.myservice.port) -- Open network socket
+ local i = Local.myservice.port
+ while not Local.host do -- unable to create socket: most likely port is already used
+ if i > Local.myservice.port+20 then
+ print("Error while creating socket")
+ return 1
+ end
+ Local.myservice.port = Local.myservice.port+1 -- try next port
+ Local.host = enet.host_create(Local.myservice.ip .. ":" .. Local.myservice.port)
+ end
+
+ Local.myservice.name = Config.player_name.text -- Get player_name from user config
- if love.system.getOS() == "Android" then
- love.android.registerService(Local.myservice.name, "_http._tcp.", Local.myservice.port)
- love.android.discoverServices("_http._tcp.")
+ if love.system.getOS() == "Android" then -- Start Android NSD discovery and publish
+ love.android.registerService(Local.myservice.name, "_bluemind._tcp.", Local.myservice.port)
+ love.android.discoverServices("_bluemind._tcp.")
end
end
function Local:update(dt)
- if not connect then
- Gui.group.push{grow = "right"}
+ Gui.group.push{grow = "right"}
- Gui.group.push{grow = "down", pos = {Utils.percentCoordinates(10, 0)}}
- if love.system.getOS() == "Android" then
- for name, host in pairs(Local.services) do
- if Gui.Button{text = name .. " " .. host.ip .. ":" .. host.port, size = {Utils.percentCoordinates(60, 10)}} then
- Local.server_host.text = host.ip.. ":" .. host.port
- connect = true
- return
+ Gui.group.push{grow = "down", pos = {Utils.percentCoordinates(10, 0)}}
+ if love.system.getOS() == "Android" then -- List discovered services
+ for name, host in pairs(Local.services) do
+ if Gui.Button{text = name .. " " .. host.ip .. ":" .. host.port, size = {Utils.percentCoordinates(60, 10)}} then
+ Local.server_host.text = host.ip.. ":" .. host.port
+ if connected then
+ Local.host:disconnect()
+ connected = false
end
+ connect = true
+ return
end
end
+ end
+
+-- Manually entering peer's address
+ Gui.Label{text = "Host", size = {Utils.percentCoordinates(10, 10)}}
+ Gui.Input{info = Local.server_host, size = {Utils.percentCoordinates(50, 10)}}
+ if Gui.Button{text = "Connect", size = {Utils.percentCoordinates(60, 10)}} then
+ connect = true
+ end
+-- End
+
+-- Handle connection and display a proposition message
+ if Local.client.name and not connect then
+ Gui.Label{text = args .. " connected to you. Play a game ?"}
+
+ Gui.group.push{grow = "right"}
+ if Gui.Button{text = "Yes", size = {Utils.percentCoordinates(10, 10)}} then
+ Protocol.acceptGame(Local.host, Local.client.index, true)
+ Gamestate.switch(require("Gamestates/Multiplayer/InGame"))
+ end
+ if Gui.Button{text = "No", size = {Utils.percentCoordinates(10, 10)}} then
+ Protocol.acceptGame(Local.host, Local.client.index, false)
+ Local.client = {}
+ end
+ Gui.group.pop{}
+ end
+-- End
- Gui.Label{text = "Host", size = {Utils.percentCoordinates(10, 10)}}
- Gui.Input{info = Local.server_host, size = {Utils.percentCoordinates(50, 10)}}
- if Gui.Button{text = "Connect", size = {Utils.percentCoordinates(60, 10)}} then
- connect = true
- end
- Gui.group.pop{}
- Gui.group.push{grow = "down", pos = {Utils.percentCoordinates(10, 0)}}
- Gui.Label{text = "My infos"}
- Gui.Label{text = "Name: " .. Local.myservice.name}
- Gui.Label{text = "Host: " .. Local.myservice.ip .. ":" .. Local.myservice.port}
- Gui.group.pop{}
+ Gui.group.pop{}
- Gui.group.pop{}
- elseif not connected then
+-- Show local player name and host
+ love.graphics.setFont(Fonts[3])
+ Gui.group.push{grow = "down", pos = {Utils.percentCoordinates(10, 0)}}
+ Gui.Label{text = "My infos"}
+ Gui.Label{text = "Name: " .. Local.myservice.name}
+ Gui.Label{text = "Host: " .. Local.myservice.ip .. ":" .. Local.myservice.port}
+ Gui.group.pop{}
+ love.graphics.setFont(Fonts[4])
+-- End
+
+ Gui.group.pop{}
+
+ if connect and not connected then
Local.host:connect(Local.server_host.text)
connected = true
- else
- local event = Local.host:service()
-
- while event ~= nil do
- if event.type == "receive" then
- print("Receive ", event.data, event.peer)
- elseif event.type == "connect" then
- if event.peer:index() > 1 then
- print("Already connected")
- else
- print("Local " .. tostring(event.peer) .. " " .. event.peer:state() .. " " .. tostring(event.peer:connect_id()))
+ end
+
+ local event = Local.host:service()
+
+ while event ~= nil do -- Process network events
+ Protocol.parseGlobalEvents(event)
+
+ if event.type == "receive" then
+ command, args = event.data:match("(.*): (.*)")
+ if command == "CONNECT" then
+ Local.client.name = args
+ event.peer:send("CONNECTED: ".. Local.myservice.name, 0, "reliable")
+ elseif command == "CONNECTED" then
+ Local.client.name = args
+ elseif command == "ACCEPT" then
+ if args == "YES" then
+ Gamestate.switch(require("Gamestates/Multiplayer/InGame"))
+ elseif args == "NO" then
+ event.peer:disconnect()
+ connect, connected = false, false
+ Local.client = {}
end
- elseif event.type == "disconnect" then
- print("Disconnect " .. tostring(event.peer))
end
-
- event = Local.host:service()
+ elseif event.type == "connect" then
+ if event.peer:index() <= 1 then
+ Local.client.index = event.peer:index()
+ end
+ if connected then
+ event.peer:send("CONNECT: ".. Local.myservice.name, 0, "reliable")
+ end
end
+
+ event = Local.host:service()
end
end
@@ -114,4 +187,8 @@ function love.textinput(str)
Gui.keyboard.textinput(str)
end
+function Local:leave()
+ love.keyboard.setTextInput(false)
+end
+
return Local
diff --git a/Gamestates/Multiplayer/Protocol.lua b/Gamestates/Multiplayer/Protocol.lua
new file mode 100644
index 0000000..6f3b739
--- /dev/null
+++ b/Gamestates/Multiplayer/Protocol.lua
@@ -0,0 +1,35 @@
+--[[
+ Gamestates/Multiplayer/Protocol.lua
+ Some functions involved in talking between 2 clients
+ by piernov
+
+ Comment: maybe useless.
+]]--
+local Protocol = {}
+
+function Protocol.acceptGame(host, peer_id, accept) -- Send an answer to a connection from another player
+ local peer = host:get_peer(peer_id)
+ if accept then
+ peer:send("ACCEPT: YES", 0, "reliable")
+ else
+ peer:send("ACCEPT: NO", 0, "reliable")
+ peer:disconnect_later()
+ end
+end
+
+function Protocol.parseGlobalEvents(event) -- Shared function between local menu and game which disallow multiple connections and handle disconnect
+ if event.type == "connect" then
+ if event.peer:index() > 1 then
+ print("Already connected")
+ event.peer:disconnect(2)
+ end
+ elseif event.type == "disconnect" then
+ print("Disconnect " .. tostring(event.peer))
+ if event.data == 2 then
+ print("Peer is busy")
+ event.peer:disconnect()
+ end
+ end
+end
+
+return Protocol