Debian Codegraph @

scripts/menus/network.lua

raw
1 -- ____ _ __
2 -- / __ )____ _____ | | / /___ ___________
3 -- / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 -- / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 -- /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 --
7 -- A futuristic real-time strategy game.
8 -- This file is part of Bos Wars.
9 --
10 -- network.lua - The multiplayer UI.
11 --
12 -- (c) Copyright 2005-2010 by François Beerten
13 --
14 -- This program is free software; you can redistribute it and/or modify
15 -- it under the terms of the GNU General Public License as published by
16 -- the Free Software Foundation; only version 2 of the License.
17 --
18 -- This program is distributed in the hope that it will be useful,
19 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
20 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 -- GNU General Public License for more details.
22 --
23 -- You should have received a copy of the GNU General Public License
24 -- along with this program; if not, write to the Free Software
25 -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 -- 02111-1307, USA.
27 --
28 -- $Id: guichan.lua 305 2005-12-18 13:36:42Z feb $
29
30 -- TODO:
31 -- * lua cleanup
32 -- * abort (exit)
33 -- * network errors
34 -- * load and store LocalPlayerName from/in preferences2.lua
35
36 function bool2int(boolvalue)
37 if boolvalue == true then
38 return 1
39 else
40 return 0
41 end
42 end
43
44 function int2bool(int)
45 if int == 0 then
46 return false
47 else
48 return true
49 end
50 end
51
52 function ErrorMenu(errmsg)
53 local menu
54
55 menu = BosMenu(_("Error"))
56
57 local l = MultiLineLabel(errmsg)
58 l:setFont(Fonts["large"])
59 l:setAlignment(MultiLineLabel.CENTER)
60 l:setVerticalAlignment(MultiLineLabel.CENTER)
61 l:setLineWidth(340)
62 l:setWidth(340)
63 l:setHeight(200)
64 l:setBackgroundColor(dark)
65 menu:add(l, Video.Width / 2 - 170, Video.Height / 2 - 100)
66
67 menu:addButton(_("~!OK"), Video.Width / 2 - 100, Video.Height - 100,
68 function() menu:stop() end)
69
70 menu:run()
71 end
72
73 function addPlayersList(menu, numplayers)
74 local i
75 local players_name = {}
76 local players_state = {}
77 local sx = Video.Width / 20
78 local sy = Video.Height / 20
79 local numplayers_text
80
81 menu:writeLargeText(_("Players"), sx * 11, sy*3)
82 for i=1,8 do
83 players_name[i] = menu:writeText(_("Player")..i, sx * 11, sy*4 + i*18)
84 players_name[i]:setWidth(80)
85 players_state[i] = menu:writeText(_("Preparing"), sx * 11 + 85, sy*4 + i*18)
86 end
87 numplayers_text = menu:writeText(_("Open slots: ") .. numplayers - 1, sx *11, sy*4 + 162)
88
89 local function updatePlayers()
90 local connected_players = 0
91 local ready_players = 0
92 players_state[1]:setCaption(_("Creator"))
93 players_name[1]:setCaption(Hosts[0].PlyName)
94 for i=2,8 do
95 if Hosts[i-1].PlyName == "" then
96 players_name[i]:setCaption("")
97 players_state[i]:setCaption("")
98 else
99 connected_players = connected_players + 1
100 if ServerSetupState.Ready[i-1] == 1 then
101 ready_players = ready_players + 1
102 players_state[i]:setCaption(_("Ready"))
103 else
104 players_state[i]:setCaption(_("Preparing"))
105 end
106 players_name[i]:setCaption(Hosts[i-1].PlyName)
107 end
108 end
109 numplayers_text:setCaption(_("Open slots : ") .. numplayers - 1 - connected_players)
110 numplayers_text:adjustSize()
111 return (connected_players > 0 and ready_players == connected_players)
112 end
113
114 return updatePlayers
115 end
116
117
118 local joincounter = 0
119
120 function RunJoiningMapMenu(s)
121 local menu
122 local listener
123 local sx = Video.Width / 20
124 local sy = Video.Height / 20
125 local numplayers = 3
126 local state
127
128 menu = BosMenu(_("Joining game: Map"))
129
130 menu:writeLargeText(_("Map"), sx, sy*3)
131 menu:writeText(_("File:"), sx, sy*3+30)
132 maptext = menu:writeText(NetworkMapName, sx+50, sy*3+30)
133 maptext:setWidth(sx * 9 - 50 - 20)
134 menu:writeText(_("Players:"), sx, sy*3+50)
135 players = menu:writeText(numplayers, sx+70, sy*3+50)
136 menu:writeText(_("Description:"), sx, sy*3+70)
137 descr = menu:writeText(_("Unknown map"), sx+20, sy*3+90)
138 descr:setWidth(sx * 9 - 20 - 20)
139
140 local fow = menu:addCheckBox(_("Fog of war"), sx, sy*3+120, function() end)
141 fow:setMarked(true)
142 ServerSetupState.FogOfWar = 1
143 fow:setEnabled(false)
144 local revealmap = menu:addCheckBox(_("Reveal map"), sx, sy*3+150, function() end)
145 revealmap:setEnabled(false)
146
147 menu:writeText(_("Difficulty:"), sx, sy*11)
148 local difficulty = menu:addDropDown({_("easy"), _("normal"), _("hard")}, sx + 150, sy*11,
149 function(dd) end)
150 difficulty:setEnabled(false)
151 menu:writeText(_("Starting resources:"), sx, sy*11+25)
152 local resources = menu:addDropDown({_("high"), _("normal"), _("low")}, sx + 150, sy*11+25,
153 function(dd) end)
154 resources:setEnabled(false)
155 menu:writeText(_("Game type:"), sx, sy*11+50)
156 local gametype = menu:addDropDown({_("Map Default"), _("Melee"), _("Free For All"), _("Top vs Bottom"), _("Left vs Right"), _("Man vs Machine")}, sx + 150, sy*11+50,
157 function(dd) end)
158 gametype:setEnabled(false)
159 gametype:setSize(140, gametype:getHeight())
160
161 local OldPresentMap = PresentMap
162 PresentMap = function(description, nplayers, w, h, id)
163 players:setCaption(""..nplayers)
164 descr:setCaption(description)
165 numplayers = nplayers
166 OldPresentMap(description, nplayers, w, h, id)
167 end
168
169 -- Security: The map name is checked by the stratagus engine.
170 Load(NetworkMapName)
171 local function readycb(dd)
172 LocalSetupState.Ready[NetLocalHostsSlot] = bool2int(dd:isMarked())
173 end
174 menu:addCheckBox(_("~!Ready"), sx*11, sy*14, readycb)
175
176 menu:addButton(_("Cancel (~<Esc~>)"), sx * 10 - 100, Video.Height - 100,
177 function()
178 NetworkDetachFromServer() menu:stop() end
179 )
180
181 local updatePlayersList = addPlayersList(menu, numplayers)
182
183 joincounter = 0
184 local function listen()
185 NetworkProcessClientRequest()
186 fow:setMarked(int2bool(ServerSetupState.FogOfWar))
187 GameSettings.NoFogOfWar = not int2bool(ServerSetupState.FogOfWar)
188 revealmap:setMarked(int2bool(ServerSetupState.RevealMap))
189 GameSettings.RevealMap = ServerSetupState.RevealMap
190 difficulty:setSelected((5 - ServerSetupState.Difficulty) / 2)
191 GameSettings.Difficulty = ServerSetupState.Difficulty
192 resources:setSelected((5 - ServerSetupState.ResourcesOption) / 2)
193 GameSettings.Resources = ServerSetupState.ResourcesOption
194 if (ServerSetupState.GameTypeOption == 255) then
195 gametype:setSelected(0)
196 else
197 gametype:setSelected(ServerSetupState.GameTypeOption + 1)
198 end
199 GameSettings.GameType = ServerSetupState.GameTypeOption
200 updatePlayersList()
201 state = GetNetworkState()
202 -- FIXME: don't use numbers
203 if (state == 15) then -- ccs_started, server started the game
204 ThisPlayer = Players[1]
205 joincounter = joincounter + 1
206 if (joincounter == 30) then
207 SetFogOfWar(fow:isMarked())
208 if revealmap:isMarked() == true then
209 RevealMap()
210 end
211 NetworkGamePrepareGameSettings()
212 AllowAllUnits()
213 RunMap(NetworkMapName)
214 PresentMap = OldPresentMap
215 menu:stop()
216 end
217 elseif (state == 10) then -- ccs_unreachable
218 ErrorMenu(_("Cannot reach server"))
219 menu:stop(1)
220 end
221 end
222 listener = LuaActionListener(listen)
223 menu:addLogicCallback(listener)
224 menu:run()
225 end
226
227 function RunJoiningGameMenu(s)
228 local menu
229 local x = Video.Width/2 - 100
230 local listener
231 local state
232 local percent = 0
233
234 menu = BosMenu(_("Joining game"))
235
236 local sb = StatBoxWidget(300, 30)
237 sb:setCaption(_("Connecting ..."))
238 sb:setPercent(0)
239 menu:add(sb, x-50, Video.Height/2)
240 sb:setBackgroundColor(dark)
241
242 menu:addButton(_("Cancel (~<Esc~>)"), x, Video.Height - 100,
243 function() menu:stop() end)
244
245 local function checkconnection()
246 NetworkProcessClientRequest()
247 percent = percent + 100 / (24 * GetGameSpeed()) -- 24 seconds * fps
248 sb:setPercent(percent)
249 state = GetNetworkState()
250 -- FIXME: do not use numbers
251 if (state == 3) then -- ccs_mapinfo
252 -- got ICMMap => load map
253 RunJoiningMapMenu()
254 menu:stop()
255 elseif (state == 4) then -- ccs_badmap
256 ErrorMenu(_("Map not available"))
257 menu:stop(1)
258 elseif (state == 10) then -- ccs_unreachable
259 ErrorMenu(_("Cannot reach server"))
260 menu:stop(1)
261 elseif (state == 12) then -- ccs_nofreeslots
262 ErrorMenu(_("Server is full"))
263 menu:stop(1)
264 elseif (state == 13) then -- ccs_serverquits
265 ErrorMenu(_("Server gone"))
266 menu:stop(1)
267 elseif (state == 16) then -- ccs_incompatibleengine
268 ErrorMenu(_("Incompatible engine version"))
269 menu:stop(1)
270 elseif (state == 17) then -- ccs_incompatiblenetwork
271 ErrorMenu(_("Incompatible network version"))
272 menu:stop(1)
273 end
274 end
275 listener = LuaActionListener(checkconnection)
276 menu:addLogicCallback(listener)
277 menu:run()
278 end
279
280 function RunJoinIpMenu()
281 local menu
282 local server
283 local x = Video.Width/2 - 100
284
285 menu = BosMenu(_("Enter Server address"))
286 menu:writeText(_("IP or server name :"), x, Video.Height*8/20)
287 server = menu:addTextInputField("localhost", x + 60, Video.Height*9/20 + 4, 130)
288 menu:addButton(_("~!Join Game"), x, Video.Height*10/20,
289 function(s)
290 -- FIXME: allow port ("localhost:1234")
291 if (NetworkSetupServerAddress(server:getText()) ~= 0) then
292 ErrorMenu(_("Invalid server name"))
293 return
294 end
295 NetworkInitClientConnect()
296 if (RunJoiningGameMenu() ~= 0) then
297 -- connect failed, don't leave this menu
298 return
299 end
300 menu:stop()
301 end
302 )
303 menu:addButton(_("Cancel (~<Esc~>)"), x, Video.Height - 100,
304 function() menu:stop() end)
305 menu:run()
306 end
307
308 function RunServerMultiGameMenu(map, description, numplayers)
309 local menu
310 local sx = Video.Width / 20
311 local sy = Video.Height / 20
312 local startgame
313 local d
314
315 menu = BosMenu(_("Create MultiPlayer game"))
316
317 menu:writeLargeText(_("Map"), sx, sy*3)
318 menu:writeText(_("File:"), sx, sy*3+30)
319 maptext = menu:writeText(map, sx+50, sy*3+30)
320 maptext:setWidth(sx * 9 - 50 - 20)
321 menu:writeText(_("Players:"), sx, sy*3+50)
322 players = menu:writeText(numplayers, sx+70, sy*3+50)
323 menu:writeText(_("Description:"), sx, sy*3+70)
324 descr = menu:writeText(description, sx+20, sy*3+90)
325 descr:setWidth(sx * 9 - 20 - 20)
326
327 local function fowCb(dd)
328 ServerSetupState.FogOfWar = bool2int(dd:isMarked())
329 NetworkServerResyncClients()
330 GameSettings.NoFogOfWar = not dd:isMarked()
331 end
332 local fow = menu:addCheckBox(_("Fog of war"), sx, sy*3+120, fowCb)
333 fow:setMarked(true)
334 local function revealMapCb(dd)
335 ServerSetupState.RevealMap = bool2int(dd:isMarked())
336 NetworkServerResyncClients()
337 GameSettings.RevealMap = bool2int(dd:isMarked())
338 end
339 local revealmap = menu:addCheckBox(_("Reveal map"), sx, sy*3+150, revealMapCb)
340
341 menu:writeText(_("Difficulty:"), sx, sy*11)
342 d = menu:addDropDown({_("easy"), _("normal"), _("hard")}, sx + 150, sy*11,
343 function(dd)
344 GameSettings.Difficulty = 5 - dd:getSelected()*2
345 ServerSetupState.Difficulty = GameSettings.Difficulty
346 NetworkServerResyncClients()
347 end)
348 d:setSelected(1)
349 menu:writeText(_("Starting resources:"), sx, sy*11+25)
350 d = menu:addDropDown({_("high"), _("normal"), _("low")}, sx + 150, sy*11+25,
351 function(dd)
352 GameSettings.Resources = 5 - dd:getSelected()*2
353 ServerSetupState.ResourcesOption = GameSettings.Resources
354 NetworkServerResyncClients()
355 end)
356 d:setSelected(1)
357 menu:writeText(_("Game type:"), sx, sy*11+50)
358 d = menu:addDropDown({_("Map Default"), _("Melee"), _("Free For All"), _("Top vs Bottom"), _("Left vs Right"), _("Man vs Machine")}, sx + 150, sy*11+50,
359 function(dd)
360 GameSettings.GameType = dd:getSelected() - 1
361 ServerSetupState.GameTypeOption = GameSettings.GameType
362 NetworkServerResyncClients()
363 end)
364 d:setSelected(0)
365 d:setSize(140, d:getHeight())
366
367
368 local updatePlayers = addPlayersList(menu, numplayers)
369
370 NetworkMapName = map
371 NetworkInitServerConnect(numplayers)
372 ServerSetupState.FogOfWar = 1
373 ServerSetupState.Difficulty = 3
374 ServerSetupState.ResourcesOption = 3
375 ServerSetupState.GameTypeOption = SettingsGameTypeMapDefault
376
377 menu:addButton(_("Cancel (~<Esc~>)"), Video.Width / 2 - 250, Video.Height - 100,
378 function() menu:stop() end)
379
380 startgame = menu:addButton(_("~!Start Game"),
381 Video.Width / 2 + 50,
382 Video.Height - 100,
383 function(s)
384 SetFogOfWar(fow:isMarked())
385 if revealmap:isMarked() == true then
386 RevealMap()
387 end
388 NetworkServerStartGame()
389 NetworkGamePrepareGameSettings()
390 AllowAllUnits()
391 RunMap(map)
392 menu:stop()
393 end
394 )
395 startgame:setVisible(false)
396 local waitingtext = menu:writeText(_("Waiting for players"), sx*11, sy*14)
397 local function updateStartButton(ready)
398 startgame:setVisible(ready)
399 waitingtext:setVisible(not ready)
400 end
401
402 local listener = LuaActionListener(function(s) updateStartButton(updatePlayers()) end)
403 menu:addLogicCallback(listener)
404 menu:run()
405 end
406
407 function RunCreateMultiGameMenu(s)
408 local menu
409 local map = _("No Map")
410 local description = _("No map")
411 local mapfile = "maps/islandwar.map"
412 local numplayers = 3
413 local sx = Video.Width / 20
414 local sy = Video.Height / 20
415
416 menu = BosMenu(_("Create MultiPlayer game"))
417
418 menu:writeText(_("File:"), sx, sy*3+30)
419 maptext = menu:writeText(mapfile, sx+50, sy*3+30)
420 maptext:setWidth(sx * 9 - 50 - 20)
421 menu:writeText(_("Players:"), sx, sy*3+50)
422 players = menu:writeText(numplayers, sx+70, sy*3+50)
423 menu:writeText(_("Size:"), sx, sy*3+70)
424 mapsize = menu:writeText(" ", sx+70, sy*3+70)
425 menu:writeText(_("Description:"), sx, sy*3+90)
426 descr = menu:writeText(description, sx+20, sy*3+110)
427 descr:setWidth(sx * 9 - 20 - 20)
428
429 local OldPresentMap = PresentMap
430 PresentMap = function(desc, nplayers, w, h, id)
431 numplayers = nplayers
432 players:setCaption(""..numplayers)
433 players:adjustSize()
434 mapsize:setCaption(""..h.."x"..w)
435 description = desc
436 descr:setCaption(description)
437 OldPresentMap(description, nplayers, w, h, id)
438 end
439
440 Load(mapfile .. '/presentation.smp')
441 local browser = menu:addMapBrowser("maps/", sx*10, sy*2+20, sx*8, sy*11, mapfile)
442 local function cb(s)
443 mapfile = browser:getSelectedMap()
444 Load(mapfile)
445 maptext:setCaption(browser:getSelectedItem())
446 end
447 browser:setActionCallback(cb)
448
449 menu:addButton(_("Cancel (~<Esc~>)"), Video.Width / 2 - 250, Video.Height - 100,
450 function() menu:stop(1) end)
451 menu:addButton(_("Create ~!Game"), Video.Width / 2 + 50, Video.Height - 100,
452 function(s)
453 RunServerMultiGameMenu(browser:getSelectedMap(), description, numplayers)
454 menu:stop()
455 end
456 )
457 menu:run()
458 PresentMap = OldPresentMap
459 end
460
461 function RunMultiPlayerMenu(s)
462 local menu
463 local b
464 local x = Video.Width/2 - 100
465 local nick
466
467 menu = BosMenu(_("MultiPlayer"))
468
469 menu:writeText(_("Nickname :"), x, Video.Height*8/20)
470 nick = menu:addTextInputField(GetLocalPlayerName(), x + 90, Video.Height*8/20 + 4)
471 nick:setMaxLengthBytes(NetPlayerNameSize - 1)
472
473 ResetMapOptions()
474 InitNetwork1()
475 menu:addButton(_("~!Join Game"), x, Video.Height*11/20,
476 function(s)
477 -- If the player did not edit the name or undid edits, then do not
478 -- save the name as a preference. If preferences.PlayerName is nil,
479 -- the name may be a default name chosen by the engine; not saving
480 -- the default name as a preference means the engine will be free to
481 -- choose a different default on the next run.
482 if nick:getText() ~= GetLocalPlayerName() then
483 SetLocalPlayerName(nick:getText())
484 preferences.PlayerName = nick:getText()
485 SavePreferences()
486 end
487 RunJoinIpMenu()
488 menu:stop(1)
489 end)
490 menu:addButton(_("Create ~!Game"), x, Video.Height*12/20,
491 function(s)
492 if nick:getText() ~= GetLocalPlayerName() then
493 SetLocalPlayerName(nick:getText())
494 preferences.PlayerName = nick:getText()
495 SavePreferences()
496 end
497 RunCreateMultiGameMenu()
498 menu:stop(1)
499 end)
500
501 menu:addButton(_("Cancel (~<Esc~>)"), Video.Width / 2 - 100, Video.Height - 100,
502 function() menu:stop() end)
503 menu:run()
504 ExitNetwork1()
505 end
506