Debian Codegraph @

src/prep.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (C) 1995 Ronny Wester
5 Copyright (C) 2003 Jeremy Chin
6 Copyright (C) 2003-2007 Lucas Martin-King
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 This file incorporates work covered by the following copyright and
23 permission notice:
24
25 Copyright (c) 2013-2018, 2020-2024 Cong Xu
26 All rights reserved.
27
28 Redistribution and use in source and binary forms, with or without
29 modification, are permitted provided that the following conditions are met:
30
31 Redistributions of source code must retain the above copyright notice, this
32 list of conditions and the following disclaimer.
33 Redistributions in binary form must reproduce the above copyright notice,
34 this list of conditions and the following disclaimer in the documentation
35 and/or other materials provided with the distribution.
36
37 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 POSSIBILITY OF SUCH DAMAGE.
48 */
49 #include "prep.h"
50
51 #include <assert.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55
56 #include <SDL_mouse.h>
57
58 #include <cdogs/actors.h>
59 #include <cdogs/blit.h>
60 #include <cdogs/config_io.h>
61 #include <cdogs/draw/draw.h>
62 #include <cdogs/files.h>
63 #include <cdogs/font.h>
64 #include <cdogs/grafx.h>
65 #include <cdogs/handle_game_events.h>
66 #include <cdogs/joystick.h>
67 #include <cdogs/keyboard.h>
68 #include <cdogs/log.h>
69 #include <cdogs/map_wolf.h>
70 #include <cdogs/music.h>
71 #include <cdogs/net_client.h>
72 #include <cdogs/net_server.h>
73 #include <cdogs/pic_manager.h>
74 #include <cdogs/sounds.h>
75 #include <cdogs/utils.h>
76
77 #include "briefing_screens.h"
78 #include "game.h"
79 #include "loading_screens.h"
80 #include "namegen.h"
81 #include "password.h"
82 #include "player_select_menus.h"
83
84 typedef struct
85 {
86 MenuSystem ms;
87 GameLoopResult (*checkFunc)(void *, LoopRunner *l);
88 void *data;
89 } ScreenWaitData;
90 static void ScreenWaitTerminate(GameLoopData *data);
91 static GameLoopResult ScreenWaitUpdate(GameLoopData *data, LoopRunner *l);
92 static void ScreenWaitDraw(GameLoopData *data);
93 static GameLoopData *ScreenWait(
94 const char *message, GameLoopResult (*checkFunc)(void *, LoopRunner *l),
95 void *data)
96 {
97 ScreenWaitData *swData;
98 CMALLOC(swData, sizeof *swData);
99 MenuSystemInit(
100 &swData->ms, &gEventHandlers, &gGraphicsDevice, svec2i_zero(),
101 gGraphicsDevice.cachedConfig.Res);
102 swData->ms.allowAborts = true;
103 swData->ms.root = swData->ms.current =
104 MenuCreateNormal("", message, MENU_TYPE_NORMAL, 0);
105 MenuAddExitType(&swData->ms, MENU_TYPE_RETURN);
106 swData->checkFunc = checkFunc;
107 swData->data = data;
108
109 return GameLoopDataNew(
110 swData, ScreenWaitTerminate, NULL, NULL, NULL, ScreenWaitUpdate,
111 ScreenWaitDraw);
112 }
113 static void ScreenWaitTerminate(GameLoopData *data)
114 {
115 ScreenWaitData *swData = data->Data;
116
117 MenuSystemTerminate(&swData->ms);
118 CFREE(swData->data);
119 CFREE(swData);
120 }
121 static GameLoopResult ScreenWaitUpdate(GameLoopData *data, LoopRunner *l)
122 {
123 ScreenWaitData *swData = data->Data;
124
125 const GameLoopResult result = swData->checkFunc(swData->data, l);
126 if (result != UPDATE_RESULT_DRAW)
127 {
128 return result;
129 }
130 const GameLoopResult menuResult = MenuUpdate(&swData->ms);
131 if (menuResult == UPDATE_RESULT_OK)
132 {
133 LoopRunnerPop(l);
134 }
135 return menuResult;
136 }
137 static void ScreenWaitDraw(GameLoopData *data)
138 {
139 const ScreenWaitData *swData = data->Data;
140
141 MenuDraw(&swData->ms);
142 }
143
144 static GameLoopResult CheckCampaignDefComplete(void *data, LoopRunner *l);
145 GameLoopData *ScreenWaitForCampaignDef(void)
146 {
147 char buf[256];
148 char ipbuf[64];
149 enet_address_get_host_ip(&gNetClient.peer->address, ipbuf, sizeof ipbuf);
150 sprintf(
151 buf, "Connecting to %s:%u...", ipbuf, gNetClient.peer->address.port);
152 return ScreenWait(buf, CheckCampaignDefComplete, NULL);
153 }
154 static GameLoopResult CheckCampaignDefComplete(void *data, LoopRunner *l)
155 {
156 UNUSED(data);
157 if (gCampaign.IsLoaded || gCampaign.IsError)
158 {
159 if (gCampaign.IsError)
160 {
161 // Failed to load campaign
162 CampaignUnload(&gCampaign);
163 }
164 else
165 {
166 CASSERT(gCampaign.IsClient, "campaign is not client");
167 }
168 LoopRunnerPop(l);
169 return UPDATE_RESULT_OK;
170 }
171 return UPDATE_RESULT_DRAW;
172 }
173
174 static void NumPlayersTerminate(GameLoopData *data);
175 static void NumPlayersOnEnter(GameLoopData *data);
176 static GameLoopResult NumPlayersUpdate(GameLoopData *data, LoopRunner *l);
177 static void NumPlayersDraw(GameLoopData *data);
178 GameLoopData *NumPlayersSelection(
179 GraphicsDevice *graphics, EventHandlers *handlers)
180 {
181 MenuSystem *ms;
182 CMALLOC(ms, sizeof *ms);
183 MenuSystemInit(
184 ms, handlers, graphics, svec2i_zero(), graphics->cachedConfig.Res);
185 ms->allowAborts = true;
186 ms->root = ms->current =
187 MenuCreateNormal("", "Select number of players", MENU_TYPE_NORMAL, 0);
188 MenuAddSubmenu(ms->current, MenuCreateReturn("(No local players)", 0));
189 for (int i = 0; i < MAX_LOCAL_PLAYERS; i++)
190 {
191 char buf[2];
192 sprintf(buf, "%d", i + 1);
193 MenuAddSubmenu(ms->current, MenuCreateReturn(buf, i + 1));
194 }
195 MenuAddExitType(ms, MENU_TYPE_RETURN);
196 // Select 1 player default
197 ms->current->u.normal.index = 1;
198
199 return GameLoopDataNew(
200 ms, NumPlayersTerminate, NumPlayersOnEnter, NULL, NULL,
201 NumPlayersUpdate, NumPlayersDraw);
202 }
203 static void NumPlayersTerminate(GameLoopData *data)
204 {
205 MenuSystem *ms = data->Data;
206 MenuSystemTerminate(ms);
207 CFREE(data->Data);
208 }
209 static void NumPlayersOnEnter(GameLoopData *data)
210 {
211 UNUSED(data);
212 MusicPlayFromChunk(
213 &gSoundDevice.music, MUSIC_BRIEFING,
214 &gCampaign.Setting.CustomSongs[MUSIC_BRIEFING]);
215 }
216 static GameLoopResult NumPlayersUpdate(GameLoopData *data, LoopRunner *l)
217 {
218 int numPlayers = 0;
219 GameLoopResult result = UPDATE_RESULT_DRAW;
220 bool hasAbort = false;
221 if (gEventHandlers.DemoQuitTimer > 0)
222 {
223 // Select random number of players for demo
224 numPlayers = RAND_INT(1, 5);
225 result = UPDATE_RESULT_OK;
226 }
227 else
228 {
229 MenuSystem *ms = data->Data;
230 result = MenuUpdate(ms);
231 numPlayers = ms->current->u.returnCode;
232 hasAbort = ms->hasAbort;
233 }
234
235 if (result == UPDATE_RESULT_OK)
236 {
237 if (hasAbort)
238 {
239 CampaignUnload(&gCampaign);
240 LoopRunnerPop(l);
241 }
242 else
243 {
244 CA_FOREACH(const PlayerData, p, gPlayerDatas)
245 CASSERT(!p->IsLocal, "unexpected local player");
246 CA_FOREACH_END()
247 // Add the players
248 for (int i = 0; i < numPlayers; i++)
249 {
250 GameEvent e = GameEventNew(GAME_EVENT_PLAYER_DATA);
251 e.u.PlayerData = PlayerDataDefault(i);
252 e.u.PlayerData.UID = gNetClient.FirstPlayerUID + i;
253 GameEventsEnqueue(&gGameEvents, e);
254 }
255 // Process the events to force add the players
256 HandleGameEvents(&gGameEvents, NULL, NULL, NULL, NULL);
257 // This also causes the client to send player data to the server
258
259 // Switch to player selection
260 LoopRunnerChange(l, PlayerSelection());
261 }
262 }
263 return result;
264 }
265 static void NumPlayersDraw(GameLoopData *data)
266 {
267 MenuDraw(data->Data);
268 }
269
270 static void AssignPlayerInputDevices(EventHandlers *handlers)
271 {
272 CA_FOREACH(PlayerData, p, gPlayerDatas)
273 if (!p->IsLocal)
274 {
275 continue;
276 }
277
278 // Try to assign devices to players
279 // For each unassigned player, check if any device has button 1 pressed
280 // If so, and that input device wasn't used, assign it to that player
281 for (int j = 0; j < MAX_KEYBOARD_CONFIGS; j++)
282 {
283 char buf[256];
284 sprintf(buf, "Input.PlayerCodes%d.button1", j);
285 if (KeyIsPressed(&handlers->keyboard, ConfigGetInt(&gConfig, buf)) &&
286 PlayerTrySetUnusedInputDevice(p, INPUT_DEVICE_KEYBOARD, j))
287 {
288 MenuPlaySound(MENU_SOUND_START);
289 break;
290 }
291 }
292 for (int j = 0; j < (int)handlers->joysticks.size; j++)
293 {
294 const Joystick *joy = CArrayGet(&handlers->joysticks, j);
295 if (JoyIsPressed(joy->id, CMD_BUTTON1) &&
296 PlayerTrySetUnusedInputDevice(p, INPUT_DEVICE_JOYSTICK, joy->id))
297 {
298 MenuPlaySound(MENU_SOUND_START);
299 break;
300 }
301 }
302 CA_FOREACH_END()
303 }
304
305 typedef struct
306 {
307 PlayerSelectMenu menus[MAX_LOCAL_PLAYERS];
308 NameGen g;
309 char prefixes[CDOGS_PATH_MAX];
310 char suffixes[CDOGS_PATH_MAX];
311 char suffixnames[CDOGS_PATH_MAX];
312 EventWaitResult waitResult;
313 int endCounter;
314 } PlayerSelectionData;
315 static void PlayerSelectionTerminate(GameLoopData *data);
316 static void PlayerSelectionOnExit(GameLoopData *data);
317 static GameLoopResult PlayerSelectionUpdate(GameLoopData *data, LoopRunner *l);
318 static void PlayerSelectionDraw(GameLoopData *data);
319 GameLoopData *PlayerSelection(void)
320 {
321 PlayerSelectionData *data;
322 CCALLOC(data, sizeof *data);
323 data->waitResult = EVENT_WAIT_CONTINUE;
324 GetDataFilePath(data->prefixes, "data/prefixes.txt");
325 GetDataFilePath(data->suffixes, "data/suffixes.txt");
326 GetDataFilePath(data->suffixnames, "data/suffixnames.txt");
327 NameGenInit(&data->g, data->prefixes, data->suffixes, data->suffixnames);
328
329 // Create selection menus for each local player
330 for (int i = 0, idx = 0; i < (int)gPlayerDatas.size; i++, idx++)
331 {
332 PlayerData *p = CArrayGet(&gPlayerDatas, i);
333 if (!p->IsLocal)
334 {
335 idx--;
336 continue;
337 }
338 PlayerSelectMenusCreate(
339 &data->menus[idx], GetNumPlayers(PLAYER_ANY, false, true), idx,
340 p->UID, &gEventHandlers, &gGraphicsDevice, &data->g);
341 }
342
343 return GameLoopDataNew(
344 data, PlayerSelectionTerminate, NULL, PlayerSelectionOnExit, NULL,
345 PlayerSelectionUpdate, PlayerSelectionDraw);
346 }
347 static void PlayerSelectionTerminate(GameLoopData *data)
348 {
349 PlayerSelectionData *pData = data->Data;
350
351 for (int i = 0; i < GetNumPlayers(PLAYER_ANY, false, true); i++)
352 {
353 MenuSystemTerminate(&pData->menus[i].ms);
354 }
355 NameGenTerminate(&pData->g);
356 CFREE(pData);
357 }
358 static void PlayerSelectionOnExit(GameLoopData *data)
359 {
360 PlayerSelectionData *pData = data->Data;
361 if (pData->waitResult == EVENT_WAIT_OK)
362 {
363 if (!gCampaign.IsClient)
364 {
365 gCampaign.MissionIndex = 0;
366 }
367 MapWolfN3DCheckAndLoadCustomQuiz(
368 gCampaign.Setting.Title, &gPlayerDatas);
369 }
370 else
371 {
372 CampaignUnload(&gCampaign);
373 }
374 }
375 static GameLoopResult PlayerSelectionUpdate(GameLoopData *data, LoopRunner *l)
376 {
377 PlayerSelectionData *pData = data->Data;
378
379 if (GetNumPlayers(PLAYER_ANY, false, true) == 0)
380 {
381 pData->waitResult = EVENT_WAIT_OK;
382 LoopRunnerPop(l);
383 return UPDATE_RESULT_OK;
384 }
385
386 // Check if anyone pressed escape
387 int cmds[MAX_LOCAL_PLAYERS];
388 memset(cmds, 0, sizeof cmds);
389 GetPlayerCmds(&gEventHandlers, &cmds);
390 if (EventIsEscape(
391 &gEventHandlers, cmds, GetMenuCmd(&gEventHandlers, false)))
392 {
393 pData->waitResult = EVENT_WAIT_CANCEL;
394 LoopRunnerPop(l);
395 return UPDATE_RESULT_OK;
396 }
397
398 int idx = 0;
399 if (pData->endCounter == 0)
400 {
401 // Menu input
402 const int localHumanPlayers = GetNumPlayers(PLAYER_ANY, true, true);
403 const bool useMenuCmd = (localHumanPlayers <= 1);
404 for (int i = 0; i < (int)gPlayerDatas.size; i++, idx++)
405 {
406 const PlayerData *p = CArrayGet(&gPlayerDatas, i);
407 if (!p->IsLocal)
408 {
409 idx--;
410 continue;
411 }
412 if (p->inputDevice != INPUT_DEVICE_UNSET)
413 {
414 MenuSystem *ms = &pData->menus[idx].ms;
415 if (ms->current->customPostUpdateFunc)
416 {
417 ms->current->customPostUpdateFunc(
418 ms->current, ms->current->customPostUpdateData);
419 }
420 MenuUpdateMouse(ms);
421 if (useMenuCmd)
422 {
423 cmds[idx] |=
424 GetMenuCmd(&gEventHandlers, ms->current->mouseHover);
425 }
426 if (!MenuIsExit(ms) && cmds[idx])
427 {
428 MenuProcessCmd(ms, cmds[idx]);
429 }
430 }
431 }
432 }
433
434 // Conditions for exit: at least one player has selected "Done",
435 // and no other players, if any, are still selecting their player
436 // The "players" with no input device are turned into AIs
437 // If in demo mode, all players are AI
438 bool hasAtLeastOneInput = gEventHandlers.DemoQuitTimer > 0;
439 bool isDone = true;
440 idx = 0;
441 for (int i = 0; i < (int)gPlayerDatas.size; i++, idx++)
442 {
443 const PlayerData *p = CArrayGet(&gPlayerDatas, i);
444 if (!p->IsLocal)
445 {
446 idx--;
447 continue;
448 }
449 if (p->inputDevice != INPUT_DEVICE_UNSET)
450 {
451 hasAtLeastOneInput = true;
452 if (strcmp(pData->menus[idx].ms.current->name, "Done") != 0)
453 {
454 isDone = false;
455 }
456 }
457 }
458 if (isDone && hasAtLeastOneInput)
459 {
460 // For any player slots not picked, turn them into AIs
461 bool hasAIPlayers = false;
462 idx = 0;
463 for (int i = 0; i < (int)gPlayerDatas.size; i++, idx++)
464 {
465 PlayerData *p = CArrayGet(&gPlayerDatas, i);
466 if (!p->IsLocal)
467 {
468 idx--;
469 continue;
470 }
471 if (p->inputDevice == INPUT_DEVICE_UNSET ||
472 p->inputDevice == INPUT_DEVICE_AI)
473 {
474 hasAIPlayers = true;
475 PlayerTrySetInputDevice(p, INPUT_DEVICE_AI, 0);
476 pData->menus[idx].ms.current =
477 MenuGetSubmenuByName(pData->menus[idx].ms.root, "Done");
478 }
479 }
480 if (hasAIPlayers && pData->endCounter < 70)
481 {
482 // If we have AI players, wait a bit before continuing
483 pData->endCounter++;
484 return UPDATE_RESULT_DRAW;
485 }
486 else
487 {
488 pData->waitResult = EVENT_WAIT_OK;
489 if (!gCampaign.IsClient && CanLevelSelect(gCampaign.Entry.Mode))
490 {
491 LoopRunnerChange(l, LevelSelection(&gGraphicsDevice));
492 }
493 else
494 {
495 LoopRunnerChange(l, GameOptions(gCampaign.Entry.Mode));
496 }
497 return UPDATE_RESULT_OK;
498 }
499 }
500
501 AssignPlayerInputDevices(&gEventHandlers);
502
503 return UPDATE_RESULT_DRAW;
504 }
505 static void PlayerSelectionDraw(GameLoopData *data)
506 {
507 const PlayerSelectionData *pData = data->Data;
508
509 BlitClearBuf(&gGraphicsDevice);
510 const int w = gGraphicsDevice.cachedConfig.Res.x;
511 const int h = gGraphicsDevice.cachedConfig.Res.y;
512 int idx = 0;
513 for (int i = 0; i < (int)gPlayerDatas.size; i++, idx++)
514 {
515 const PlayerData *p = CArrayGet(&gPlayerDatas, i);
516 if (!p->IsLocal)
517 {
518 idx--;
519 continue;
520 }
521 if (p->inputDevice != INPUT_DEVICE_UNSET)
522 {
523 MenuDisplay(&pData->menus[idx].ms);
524 }
525 else
526 {
527 struct vec2i center = svec2i_zero();
528 const char *prompt =
529 "Press Fire to choose input device\nand join...";
530 const struct vec2i offset =
531 svec2i_scale_divide(FontStrSize(prompt), -2);
532 switch (GetNumPlayers(false, false, true))
533 {
534 case 1:
535 // Center of screen
536 center = svec2i(w / 2, h / 2);
537 break;
538 case 2:
539 // Side by side
540 center = svec2i(idx * w / 2 + w / 4, h / 2);
541 break;
542 case 3:
543 case 4:
544 // Four corners
545 center = svec2i(
546 (idx & 1) * w / 2 + w / 4, (idx / 2) * h / 2 + h / 4);
547 break;
548 default:
549 CASSERT(false, "not implemented");
550 break;
551 }
552 FontStr(prompt, svec2i_add(center, offset));
553 }
554 }
555
556 BlitUpdateFromBuf(&gGraphicsDevice, gGraphicsDevice.screen);
557 }
558
559 typedef struct
560 {
561 MenuSystem ms;
562 const CArray *weapons;
563 CArray allowed; // of bool
564 } GameOptionsData;
565 static menu_t *MenuCreateAllowedWeapons(
566 const char *name, GameOptionsData *data);
567 static void GameOptionsTerminate(GameLoopData *data);
568 static void GameOptionsOnEnter(GameLoopData *data);
569 static GameLoopResult GameOptionsUpdate(GameLoopData *data, LoopRunner *l);
570 static void GameOptionsDraw(GameLoopData *data);
571 GameLoopData *GameOptions(const GameMode gm)
572 {
573 // Create selection menus
574 const int w = gGraphicsDevice.cachedConfig.Res.x;
575 const int h = gGraphicsDevice.cachedConfig.Res.y;
576 GameOptionsData *data;
577 CMALLOC(data, sizeof *data);
578 MenuSystem *ms = &data->ms;
579 data->weapons = &gMission.Weapons;
580 CArrayInit(&data->allowed, sizeof(bool));
581 for (int i = 0; i < (int)data->weapons->size; i++)
582 {
583 const bool f = true;
584 CArrayPushBack(&data->allowed, &f);
585 }
586 MenuSystemInit(
587 ms, &gEventHandlers, &gGraphicsDevice, svec2i_zero(), svec2i(w, h));
588 ms->align = MENU_ALIGN_CENTER;
589 ms->allowAborts = true;
590 ms->root = MenuCreateNormal("", "", MENU_TYPE_OPTIONS, 0);
591 #define I(_config) \
592 MenuAddConfigOptionsItem(ms->root, ConfigGet(&gConfig, _config));
593 switch (gm)
594 {
595 case GAME_MODE_NORMAL:
596 I("Game.Difficulty");
597 I("Game.EnemyDensity");
598 I("Game.NonPlayerHP");
599 if (gCampaign.Setting.PlayerMaxHP == 0)
600 {
601 I("Game.PlayerHP");
602 }
603 if (gCampaign.Setting.Lives == 0)
604 {
605 I("Game.Lives");
606 }
607 I("Game.HealthPickups");
608 I("Game.RandomSeed");
609 MenuAddSubmenu(ms->root, MenuCreateSeparator(""));
610 I("StartServer");
611 break;
612 case GAME_MODE_DOGFIGHT:
613 I("Dogfight.PlayerHP");
614 I("Dogfight.FirstTo");
615 I("Game.HealthPickups");
616 I("Game.RandomSeed");
617 MenuAddSubmenu(ms->root, MenuCreateAllowedWeapons("Weapons...", data));
618 MenuAddSubmenu(ms->root, MenuCreateSeparator(""));
619 I("StartServer");
620 break;
621 case GAME_MODE_DEATHMATCH:
622 if (gCampaign.Setting.PlayerMaxHP == 0)
623 {
624 I("Game.PlayerHP");
625 }
626 I("Deathmatch.Lives");
627 I("Game.HealthPickups");
628 I("Game.RandomSeed");
629 MenuAddSubmenu(ms->root, MenuCreateAllowedWeapons("Weapons...", data));
630 MenuAddSubmenu(ms->root, MenuCreateSeparator(""));
631 I("StartServer");
632 break;
633 case GAME_MODE_QUICK_PLAY:
634 I("QuickPlay.WallCount");
635 I("QuickPlay.WallLength");
636 I("QuickPlay.RoomCount");
637 I("QuickPlay.SquareCount");
638 I("QuickPlay.EnemyCount");
639 I("QuickPlay.EnemySpeed");
640 I("QuickPlay.EnemyHealth");
641 I("QuickPlay.EnemiesWithExplosives");
642 I("QuickPlay.ItemCount");
643 // Force start server to be false - we don't support multiplayer with
644 // this game mode
645 ConfigGet(&gConfig, "StartServer")->u.Bool.Value = false;
646 break;
647 default:
648 CASSERT(false, "unknown game mode");
649 break;
650 }
651 #undef I
652 MenuAddSubmenu(ms->root, MenuCreateSeparator(""));
653 MenuAddSubmenu(ms->root, MenuCreateReturn("Done", 0));
654 MenuAddExitType(ms, MENU_TYPE_RETURN);
655
656 return GameLoopDataNew(
657 ms, GameOptionsTerminate, GameOptionsOnEnter, NULL, NULL,
658 GameOptionsUpdate, GameOptionsDraw);
659 }
660 static void GameOptionsTerminate(GameLoopData *data)
661 {
662 GameOptionsData *gData = data->Data;
663
664 MenuSystemTerminate(&gData->ms);
665 CArrayTerminate(&gData->allowed);
666 CFREE(gData);
667 }
668 static void GameOptionsOnEnter(GameLoopData *data)
669 {
670 GameOptionsData *gData = data->Data;
671
672 if (!gCampaign.IsQuit)
673 {
674 MissionOptionsTerminate(&gMission);
675 CampaignAndMissionSetup(&gCampaign, &gMission);
676 }
677
678 gData->ms.current = gData->ms.root;
679 // Select "Done"
680 gData->ms.root->u.normal.index =
681 (int)gData->ms.root->u.normal.subMenus.size - 1;
682 }
683 static GameLoopResult GameOptionsUpdate(GameLoopData *data, LoopRunner *l)
684 {
685 GameOptionsData *gData = data->Data;
686
687 // Check end conditions:
688 // - Campaign not loaded
689 // - Campaign complete
690 // - Mission quit
691 // - No options needed
692 // - Demo mode
693 // - Menu complete
694 const GameLoopResult result = MenuUpdate(&gData->ms);
695 const bool isQuit = !gCampaign.IsLoaded || gCampaign.IsComplete ||
696 gCampaign.IsQuit || gData->ms.hasAbort ||
697 gMission.missionData == NULL;
698 const bool isDone = !IsGameOptionsNeeded(gCampaign.Entry.Mode) ||
699 result == UPDATE_RESULT_OK ||
700 gEventHandlers.DemoQuitTimer > 0;
701 if (isQuit || isDone)
702 {
703 if (isQuit)
704 {
705 MissionOptionsTerminate(&gMission);
706 CampaignUnload(&gCampaign);
707 LoopRunnerPop(l);
708 return UPDATE_RESULT_OK;
709 }
710 if (!ConfigApply(&gConfig, NULL))
711 {
712 LOG(LM_MAIN, LL_ERROR,
713 "Failed to apply config; reset to last used");
714 ConfigResetChanged(&gConfig);
715 }
716 else
717 {
718 // Save options for later
719 ConfigSave(&gConfig, GetConfigFilePath(CONFIG_FILE));
720 }
721
722 // Set allowed weapons
723 // First check if the player has unwittingly disabled all weapons
724 // if so, enable all weapons
725 bool allDisabled = true;
•726 for (int i = 0; i < (int)gData->allowed.size; i++)
727 {
728 const bool *allowed = CArrayGet(&gData->allowed, i);
729 if (*allowed)
730 {
731 allDisabled = false;
732 break;
733 }
734 }
735 if (!allDisabled)
736 {
737 for (int i = 0, j = 0; i < (int)gData->allowed.size; i++, j++)
738 {
739 const bool *allowed = CArrayGet(&gData->allowed, i);
740 if (!*allowed)
741 {
742 CArrayDelete(&gMission.Weapons, j);
743 j--;
744 }
745 }
746 }
747
748 gCampaign.OptionsSet = true;
749
750 // If enabled, start net server
751 if (!gCampaign.IsClient && ConfigGetBool(&gConfig, "StartServer"))
752 {
753 NetServerOpen(
754 &gNetServer, (uint16_t)ConfigGetInt(&gConfig, "ListenPort"));
755 }
756 LoopRunnerPush(
757 l, ScreenMissionBriefing(&gCampaign.Setting, &gMission));
758 return UPDATE_RESULT_OK;
759 }
760 return UPDATE_RESULT_DRAW;
761 }
762 static void GameOptionsDraw(GameLoopData *data)
763 {
764 const GameOptionsData *gData = data->Data;
765
766 MenuDraw(&gData->ms);
767 }
768 static menu_t *MenuCreateAllowedWeapons(
769 const char *name, GameOptionsData *data)
770 {
771 // Create a menu to choose allowable weapons for this map
772 // The weapons will be chosen from the available weapons
773 menu_t *m =
774 MenuCreateNormal(name, "Available Weapons:", MENU_TYPE_OPTIONS, 0);
775 m->customPostInputData = data;
776 for (int i = 0; i < (int)data->weapons->size; i++)
777 {
778 const WeaponClass **wc = CArrayGet(data->weapons, i);
779 MenuAddSubmenu(
780 m,
781 MenuCreateOptionToggle((*wc)->name, CArrayGet(&data->allowed, i)));
782 }
783 MenuAddSubmenu(m, MenuCreateSeparator(""));
784 MenuAddSubmenu(m, MenuCreateBack("Done"));
785 return m;
786 }
787
788 static GameLoopResult CheckGameStart(void *data, LoopRunner *l);
789 GameLoopData *ScreenWaitForGameStart(void)
790 {
791 return ScreenWait("Waiting for game start...", CheckGameStart, NULL);
792 }
793 static GameLoopResult CheckGameStart(void *data, LoopRunner *l)
794 {
795 UNUSED(data);
796 if (!gNetClient.Ready)
797 {
798 // Tell server we're ready
799 NetClientSendMsg(&gNetClient, GAME_EVENT_CLIENT_READY, NULL);
800 }
801 gNetClient.Ready = true;
802 if (!gCampaign.IsClient || gMission.HasStarted)
803 {
804 goto bail;
805 }
806 // Check disconnections
807 if (!NetClientIsConnected(&gNetClient))
808 {
809 CampaignUnload(&gCampaign);
810 goto bail;
811 }
812 return UPDATE_RESULT_DRAW;
813
814 bail:
815 if (!gCampaign.IsLoaded)
816 {
817 LoopRunnerPop(l);
818 return UPDATE_RESULT_OK;
819 }
820 LoopRunnerPush(
821 l, ScreenLoading(
822 "Starting game...", true, RunGame(&gCampaign, &gMission, &gMap),
823 true));
824 return UPDATE_RESULT_OK;
825 }