Debian Codegraph @

src/password.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2013-2014, 2017, 2021-2022, 2024-2025 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "password.h"
29
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <cdogs/actors.h>
36 #include <cdogs/blit.h>
37 #include <cdogs/config.h>
38 #include <cdogs/defs.h>
39 #include <cdogs/font.h>
40 #include <cdogs/gamedata.h>
41 #include <cdogs/grafx.h>
42 #include <cdogs/grafx_bg.h>
43 #include <cdogs/joystick.h>
44 #include <cdogs/keyboard.h>
45 #include <cdogs/sounds.h>
46
47 #include "autosave.h"
48 #include "game_loop.h"
49 #include "hiscores.h"
50 #include "menu.h"
51 #include "prep.h"
52
53 typedef enum
54 {
55 RETURN_CODE_CONTINUE = -1,
56 RETURN_CODE_START = -2,
57 } ReturnCode;
58
59 typedef struct
60 {
61 MenuSystem ms;
62 const CampaignSave *save;
63 HighScoresData hData;
64 } LevelSelectionData;
65 static void LevelSelectionTerminate(GameLoopData *data);
66 static void LevelSelectionOnEnter(GameLoopData *data);
67 static GameLoopResult LevelSelectionUpdate(GameLoopData *data, LoopRunner *l);
68 static void LevelSelectionDraw(GameLoopData *data);
69 static void MenuCreateStart(LevelSelectionData *data);
70 GameLoopData *LevelSelection(GraphicsDevice *graphics)
71 {
72 LevelSelectionData *data;
73 CCALLOC(data, sizeof *data);
74 data->save = AutosaveGetCampaign(&gAutosave, gCampaign.Entry.Path);
75 MenuSystemInit(
76 &data->ms, &gEventHandlers, graphics, svec2i_zero(),
77 graphics->cachedConfig.Res);
78 MenuCreateStart(data);
79 return GameLoopDataNew(
80 data, LevelSelectionTerminate, LevelSelectionOnEnter, NULL, NULL,
81 LevelSelectionUpdate, LevelSelectionDraw);
82 }
83 static void MenuCreateLevelSelect(
84 menu_t *levelSelect, const Campaign *co, const int i)
85 {
86 const Mission *m = CArrayGet(&co->Setting.Missions, i);
87 char buf[CDOGS_FILENAME_MAX];
88 sprintf(buf, "%d: %s", i + 1, m->Title);
89 menu_t *l = MenuCreateReturn(buf, i);
90 MenuAddSubmenu(levelSelect, l);
91 }
92 static void HighScoresDrawFunc(
93 const menu_t *menu, GraphicsDevice *g, const struct vec2i pos,
94 const struct vec2i size, const void *data);
95 static void MenuCreateStart(LevelSelectionData *data)
96 {
97 MenuSystem *ms = &data->ms;
98 ms->root = ms->current = MenuCreateNormal("", "", MENU_TYPE_NORMAL, 0);
99
100 menu_t *menuContinue = MenuCreateReturn("Continue", RETURN_CODE_CONTINUE);
101 // Note: mission can be -1
102 menuContinue->isDisabled =
103 data->save == NULL || data->save->NextMission <= 0 ||
104 data->save->NextMission == (int)gCampaign.Setting.Missions.size;
105 MenuAddSubmenu(ms->root, menuContinue);
106
107 // Create level select menus
108 menu_t *levelSelect = MenuCreateNormal(
109 "Level select...", "Select Level", MENU_TYPE_NORMAL, 0);
110 levelSelect->u.normal.maxItems = 20;
111 if (data->save)
112 {
113 CArray levels;
114 CArrayInitFillZero(
115 &levels, sizeof(bool), gCampaign.Setting.Missions.size);
116 CA_FOREACH(const int, missionIndex, data->save->MissionsCompleted)
117 if (*missionIndex >= (int)gCampaign.Setting.Missions.size)
118 {
119 continue;
120 }
121 MenuCreateLevelSelect(levelSelect, &gCampaign, *missionIndex);
122 CArraySet(&levels, *missionIndex, &gTrue);
123 CA_FOREACH_END()
124 if (data->save->NextMission < (int)gCampaign.Setting.Missions.size &&
125 !*(bool *)CArrayGet(&levels, data->save->NextMission))
126 {
127 MenuCreateLevelSelect(
128 levelSelect, &gCampaign, data->save->NextMission);
129 }
130 CArrayTerminate(&levels);
131 }
132 levelSelect->isDisabled = data->save == NULL ||
133 data->save->MissionsCompleted.size == 0 ||
134 gCampaign.Setting.Missions.size == 1;
135 MenuAddSubmenu(ms->root, levelSelect);
136
137 MenuAddSubmenu(
138 ms->root, MenuCreateReturn("Start campaign", RETURN_CODE_START));
139
140 data->hData = HighScoresDataLoad(&gCampaign, &gGraphicsDevice);
141 menu_t *highScoresMenu = MenuCreateCustom(
142 "High Scores", HighScoresDrawFunc, NULL, &data->hData);
143 highScoresMenu->isDisabled = data->hData.scores.size == 0;
144 MenuAddSubmenu(ms->root, highScoresMenu);
145
146 MenuAddExitType(ms, MENU_TYPE_RETURN);
147 }
148 static void LevelSelectionTerminate(GameLoopData *data)
149 {
150 LevelSelectionData *pData = data->Data;
151
152 MenuSystemTerminate(&pData->ms);
153 HighScoresDataTerminate(&pData->hData);
154 CFREE(data->Data);
155 }
156 static void LevelSelectionOnEnter(GameLoopData *data)
157 {
158 LevelSelectionData *pData = data->Data;
159
160 // TODO: re-detect mission saves on enter
161 MenuReset(&pData->ms);
162 gCampaign.MissionIndex = 0;
163 }
164 static GameLoopResult LevelSelectionUpdate(GameLoopData *data, LoopRunner *l)
165 {
166 LevelSelectionData *pData = data->Data;
167
168 const GameLoopResult result = MenuUpdate(&pData->ms);
169 if (result == UPDATE_RESULT_OK)
170 {
171 if (pData->ms.hasAbort)
172 {
173 LoopRunnerPop(l);
174 }
175 else
176 {
177 // Check valid password
178 const int returnCode = pData->ms.current->u.returnCode;
179 switch (returnCode)
180 {
181 case RETURN_CODE_CONTINUE:
182 gCampaign.MissionIndex = pData->save->NextMission;
183 break;
184 case RETURN_CODE_START:
185 break;
186 default:
187 // Return code represents the mission to start on
188 CASSERT(
189 returnCode >= 0, "Invalid return code for password menu");
190 gCampaign.MissionIndex = returnCode;
191 break;
192 }
193
194 // Load autosaved player data
195 if (pData->save != NULL && gCampaign.MissionIndex > 0)
196 {
197 const Mission *m = CampaignGetCurrentMission(&gCampaign);
198 PlayerSavesApply(&pData->save->Players, m->WeaponPersist);
199 }
200 // IMPORTANT: change state after applying player save to avoid
201 // reading freed data
202 LoopRunnerChange(l, GameOptions(gCampaign.Entry.Mode));
203 }
204 }
205 return result;
206 }
207 static void LevelSelectionDraw(GameLoopData *data)
208 {
209 const LevelSelectionData *pData = data->Data;
210
211 MenuDraw(&pData->ms);
212 }
213
214 static void HighScoresDrawFunc(
215 const menu_t *menu, GraphicsDevice *g, const struct vec2i pos,
216 const struct vec2i size, const void *data)
217 {
218 UNUSED(menu);
219 UNUSED(g);
220 UNUSED(pos);
221 UNUSED(size);
222 const HighScoresData *hData = data;
223 HighScoresDraw(hData);
224 }