1function client_load()
2	CLIENT = true
3	print("===CLIENT===")
4	message( "Attempting Connection to " .. ip .. ":" .. port .. ".." )
5	client_createclient(ip, port)
6	updatetimer = 0
7	players = 2
8	client_controls()
9	netplay = true
10end
11
12function client_controls()
13	mouseowner = 2
14
15	controls = {}
16
17	local i = 2
18	controls[i] = {}
19	controls[i]["right"] = {"d"}
20	controls[i]["left"] = {"a"}
21	controls[i]["down"] = {"s"}
22	controls[i]["run"] = {"lshift"}
23	controls[i]["jump"] = {" "}
24	controls[i]["aimX"] = {} --mouse aiming, so no need
25	controls[i]["aimY"] = {}
26	controls[i]["portal1"] = {}
27	controls[i]["portal2"] = {}
28	controls[i]["reload"] = {"r"}
29	controls[i]["use"] = {"e"}
30
31	local i = 1 --unbind Player 1
32	controls[i] = {}
33	controls[i]["right"] = {""}
34	controls[i]["left"] = {""}
35	controls[i]["down"] = {""}
36	controls[i]["run"] = {""}
37	controls[i]["jump"] = {""}
38	controls[i]["aimX"] = {}
39	controls[i]["aimY"] = {}
40	controls[i]["portal1"] = {}
41	controls[i]["portal2"] = {}
42	controls[i]["reload"] = {""}
43	controls[i]["use"] = {""}
44end
45
46function client_createclient(ip, port)
47	pingtime = love.timer.getTime()
48	CLIENT = true
49	MyClient = lube.client("udp")
50	MyClient:setCallback(umsg.recv)
51	MyClient:setHandshake("bj")
52	MyClient:connect(ip, port)
53	MyClient:setPing(true, 5, "PING")
54
55	--umsg.hook( "string", function)
56	umsg.hook( "client_connectconfirm", client_connectconfirm)
57	umsg.hook( "client_playerlistget", client_playerlistget)
58	umsg.hook( "client_start", client_start)
59	umsg.hook( "client_synctest", client_synctest)
60	umsg.hook( "shootportal", client_shootportal)
61end
62
63function client_update(dt)
64	MyClient:update(dt)
65
66	updatetimer = updatetimer + dt
67	if updatetimer > updatedelay then
68		if objects then
69			local s = "ndummy~1"
70			for i, v in pairs(objects["player"][2]) do
71				if type(v) == "table" then
72
73				elseif type(v) == "number" then
74					s = s .. "~n" .. i .. "~" .. tostring(v)
75
76				elseif type(v) == "string" then
77					s = s .. "~s" .. i .. "~" .. tostring(v)
78
79				elseif type(v) == "boolean" then
80					s = s .. "~b" .. i .. "~" .. tostring(v)
81				else
82					s = s .. "~" .. i .. "~" .. tostring(v)
83				end
84			end
85			local um = usermessage:new("server_synctest", s)
86			um:send()
87		end
88
89		updatetimer = 0
90	end
91end
92
93function client_shootportal(args)
94	args = args:split("~")
95	shootportal(1, tonumber(args[1]), tonumber(args[2]), tonumber(args[3]), tonumber(args[4]))
96end
97
98function client_synctest(input)
99	if objects then
100		des = deserialize(input)
101		for i = 1, #des, 2 do
102			local t = string.sub(des[i], 1, 1)
103			local varname = string.sub(des[i], 2, string.len(des[i]))
104			local varvalue = des[i+1]
105			if t == "b" then
106				if varvalue == "true" then
107					objects["player"][1][varname] = true
108				else
109					objects["player"][1][varname] = false
110				end
111			elseif t == "s" then
112				objects["player"][1][varname] = varvalue
113			elseif t == "n" then
114				objects["player"][1][varname] = tonumber(varvalue)
115			end
116		end
117	end
118end
119
120function client_connectconfirm(s)
121	currenttime = love.timer.getTime()
122 	message("Successfully Connected! (" .. math.floor((currenttime - pingtime)*1000) .. "ms)")
123	message("MotD: " .. s)
124	message("Sending nickname..")
125
126	local um = usermessage:new( "nickname", localnick)
127	um:send()
128
129	message("Entering Lobby..")
130	message("Requesting playerlist..")
131	message("---------------------------")
132
133	local um = usermessage:new( "playerlistrequest" )
134	um:send()
135end
136
137function client_playerlistget(s)
138	playerlist = s:split("~")
139	message("Playerlist received:")
140	for i = 1, #playerlist do
141		message(i .. ": " .. playerlist[i])
142	end
143	message("---------------------------")
144end
145
146function client_start(s)
147	message("Starting the game!")
148	game_load()
149end
150
151function client_quit()
152	MyClient:disconnect()
153
154end