Debian Codegraph @

src/screens_end.c

raw symbols SCIP
1 /*
2 Copyright (c) 2013-2017, 2020 Cong Xu
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 POSSIBILITY OF SUCH DAMAGE.
25 */
26 #include "screens_end.h"
27
28 #include <cdogs/events.h>
29 #include <cdogs/font.h>
30 #include <cdogs/grafx_bg.h>
31
32 #include "hiscores.h"
33 #include "menu_utils.h"
34
35 // All ending screens will be drawn in a table/list format, with each player
36 // on a row. This is to support any number of players, since there could be
37 // many players especially from network multiplayer.
38
39 #define PLAYER_LIST_ROW_HEIGHT 16
40 typedef struct
41 {
42 MenuSystem ms;
43 struct vec2i pos;
44 struct vec2i size;
45 int scroll;
46 GameLoopResult (*updateFunc)(GameLoopData *, LoopRunner *);
47 void (*drawFunc)(void *);
48 void *data;
49 // Whether to use a confirmation "Finish" menu at the end
50 // Useful for final screens where we want to view the scores without
51 // accidentally quitting
52 bool hasMenu;
53 bool showWinners;
54 bool showLastMan;
55 // Store player UIDs so we can display the list in a certain order
56 CArray playerUIDs; // of int
57 } PlayerList;
58 static int ComparePlayerScores(const void *v1, const void *v2);
59 static PlayerList *PlayerListNew(
60 GameLoopResult (*updateFunc)(GameLoopData *, LoopRunner *),
61 void (*drawFunc)(void *), void *data, const bool hasMenu,
62 const bool showWinners)
63 {
64 PlayerList *pl;
65 CMALLOC(pl, sizeof *pl);
66 pl->pos = svec2i_zero();
67 pl->size = gGraphicsDevice.cachedConfig.Res;
68 pl->scroll = 0;
69 pl->updateFunc = updateFunc;
70 pl->drawFunc = drawFunc;
71 pl->data = data;
72 pl->hasMenu = hasMenu;
73 pl->showWinners = showWinners;
74 CArrayInit(&pl->playerUIDs, sizeof(int));
75 // Collect all players, then order by score descending
76 int playersAlive = 0;
77 CA_FOREACH(const PlayerData, p, gPlayerDatas)
78 CArrayPushBack(&pl->playerUIDs, &p->UID);
79 if (p->Lives > 0)
80 {
81 playersAlive++;
82 }
83 CA_FOREACH_END()
84 qsort(
85 pl->playerUIDs.data, pl->playerUIDs.size, pl->playerUIDs.elemSize,
86 ComparePlayerScores);
87 pl->showLastMan = playersAlive == 1;
88 return pl;
89 }
90 static int GetModeScore(const PlayerData *p)
91 {
92 // For deathmatch, we count kills instead of score
93 if (gCampaign.Entry.Mode == GAME_MODE_DEATHMATCH)
94 {
95 return p->Totals.Kills;
96 }
97 return p->Totals.Score;
98 }
99 static int ComparePlayerScores(const void *v1, const void *v2)
100 {
101 const PlayerData *p1 = PlayerDataGetByUID(*(const int *)v1);
102 const PlayerData *p2 = PlayerDataGetByUID(*(const int *)v2);
103 int p1s = GetModeScore(p1);
104 int p2s = GetModeScore(p2);
105 if (p1s > p2s)
106 {
107 return -1;
108 }
109 else if (p1s < p2s)
110 {
111 return 1;
112 }
113 return 0;
114 }
115 static void PlayerListCustomDraw(
116 const menu_t *menu, GraphicsDevice *g, const struct vec2i pos,
117 const struct vec2i size, const void *data);
118 static int PlayerListInput(int cmd, void *data);
119 static void PlayerListTerminate(GameLoopData *data);
120 static void PlayerListOnEnter(GameLoopData *data);
121 static void PlayerListOnExit(GameLoopData *data);
122 static void PlayerListDraw(GameLoopData *data);
123 static GameLoopData *PlayerListLoop(PlayerList *pl)
124 {
125 MenuSystemInit(
126 &pl->ms, &gEventHandlers, &gGraphicsDevice, pl->pos, pl->size);
127 menu_t *menuScores = MenuCreateCustom(
128 "View Scores", PlayerListCustomDraw, PlayerListInput, pl);
129 if (pl->hasMenu)
130 {
131 pl->ms.root = MenuCreateNormal("", "", MENU_TYPE_NORMAL, 0);
132 MenuAddSubmenu(pl->ms.root, menuScores);
133 MenuAddSubmenu(pl->ms.root, MenuCreateReturn("Finish", 0));
134 }
135 else
136 {
137 pl->ms.root = menuScores;
138 }
139 pl->ms.allowAborts = true;
140 MenuAddExitType(&pl->ms, MENU_TYPE_RETURN);
141 return GameLoopDataNew(
142 pl, PlayerListTerminate, PlayerListOnEnter, PlayerListOnExit, NULL,
143 pl->updateFunc, PlayerListDraw);
144 }
145 static void PlayerListTerminate(GameLoopData *data)
146 {
147 PlayerList *pl = data->Data;
148
149 CArrayTerminate(&pl->playerUIDs);
150 MenuSystemTerminate(&pl->ms);
151 CFREE(pl->data);
152 CFREE(pl);
153 }
154 static void PlayerListOnEnter(GameLoopData *data)
155 {
156 PlayerList *pl = data->Data;
157
158 if (pl->hasMenu)
159 {
160 pl->ms.current = MenuGetSubmenuByName(pl->ms.root, "View Scores");
161 }
162 else
163 {
164 pl->ms.current = pl->ms.root;
165 }
166 }
167 static void PlayerListOnExit(GameLoopData *data)
168 {
169 UNUSED(data);
170 MenuPlaySound(MENU_SOUND_SWITCH);
171 }
172 static GameLoopResult PlayerListUpdate(GameLoopData *data, LoopRunner *l)
173 {
174 PlayerList *pl = data->Data;
175
176 const GameLoopResult result = MenuUpdate(&pl->ms);
177 if (result == UPDATE_RESULT_OK)
178 {
179 LoopRunnerChange(l, HighScoresScreen(&gCampaign, &gGraphicsDevice));
180 }
181 return result;
182 }
183 static void PlayerListDraw(GameLoopData *data)
184 {
185 const PlayerList *pl = data->Data;
186
187 MenuDraw(&pl->ms);
188 }
189 static int PlayerListMaxScroll(const PlayerList *pl);
190 static int PlayerListMaxRows(const PlayerList *pl);
191 static void PlayerListCustomDraw(
192 const menu_t *menu, GraphicsDevice *g, const struct vec2i pos,
193 const struct vec2i size, const void *data)
194 {
195 UNUSED(menu);
196 UNUSED(g);
197 // Draw players starting from the index
198 // TODO: custom columns
199 const PlayerList *pl = data;
200
201 // First draw the headers
202 const int xStart = pos.x + 80 + (size.x - 320) / 2;
203 int x = xStart;
204 int y = pos.y;
205 FontStrMask("Player", svec2i(x, y), colorPurple);
206 x += 68;
207 FontStrMask("Score", svec2i(x, y), colorPurple);
208 x += 32;
209 FontStrMask("Kills", svec2i(x, y), colorPurple);
210 x += 32;
211 FontStrMask("Time", svec2i(x, y), colorPurple);
212 y += FontH() * 2 + PLAYER_LIST_ROW_HEIGHT + 4;
213 // Then draw the player list
214 int maxScore = -1;
215 for (int i = pl->scroll;
216 i < MIN((int)pl->playerUIDs.size, pl->scroll + PlayerListMaxRows(pl));
217 i++)
218 {
219 const int *playerUID = CArrayGet(&pl->playerUIDs, i);
220 PlayerData *p = PlayerDataGetByUID(*playerUID);
221 if (p == NULL)
222 {
223 continue;
224 }
225 if (maxScore < GetModeScore(p))
226 {
227 maxScore = GetModeScore(p);
228 }
229
230 x = xStart;
231 // Highlight local players using different coloured text
232 const color_t textColor = p->IsLocal ? colorPurple : colorWhite;
233
234 // Draw the players offset on alternate rows
235 DisplayCharacterAndName(
236 svec2i(x + (i & 1) * 16, y + 4), &p->Char, DIRECTION_DOWN, p->name,
237 textColor, p->guns[0]);
238
239 // Draw score
240 x += 68;
241 char buf[256];
242 sprintf(buf, "%d", p->Totals.Score);
243 FontStrMask(buf, svec2i(x, y), textColor);
244
245 // Draw kills
246 x += 32;
247 sprintf(buf, "%d", p->Totals.Kills);
248 FontStrMask(buf, svec2i(x, y), textColor);
249
250 // Draw time
251 x += 32;
252 const int timeSeconds = p->Totals.TimeTicks / FPS_FRAMELIMIT;
253 sprintf(buf, "%d:%02d", timeSeconds / 60, timeSeconds % 60);
254 FontStrMask(buf, svec2i(x, y), textColor);
255
256 // Draw winner/award text
257 x += 32;
258 if (pl->showWinners && GetModeScore(p) == maxScore)
259 {
260 FontStrMask("Winner!", svec2i(x, y), colorGreen);
261 }
262 else if (
263 pl->showLastMan && p->Lives > 0 &&
264 gCampaign.Entry.Mode == GAME_MODE_DEATHMATCH)
265 {
266 // Only show last man standing on deathmatch mode
267 FontStrMask("Last man standing!", svec2i(x, y), colorGreen);
268 }
269
270 y += PLAYER_LIST_ROW_HEIGHT;
271 }
272
273 // Draw indicator arrows if there's enough to scroll
274 if (pl->scroll > 0)
275 {
276 FontStr(
277 "^", svec2i(CENTER_X(pos, size, FontStrW("^")), pos.y + FontH()));
278 }
279 if (pl->scroll < PlayerListMaxScroll(pl))
280 {
281 FontStr(
282 "v",
283 svec2i(
284 CENTER_X(pos, size, FontStrW("v")), pos.y + size.y - FontH()));
285 }
286
287 // Finally draw any custom stuff
288 if (pl->drawFunc)
289 {
290 pl->drawFunc(pl->data);
291 }
292 }
293 static int PlayerListInput(int cmd, void *data)
294 {
295 // Input: up/down scrolls list
296 // CMD 1/2: exit
297 PlayerList *pl = data;
298
299 // Note: players can leave due to network disconnection
300 // Update our lists
301 CA_FOREACH(const int, playerUID, pl->playerUIDs)
302 const PlayerData *p = PlayerDataGetByUID(*playerUID);
303 if (p == NULL)
304 {
305 CArrayDelete(&pl->playerUIDs, _ca_index);
306 _ca_index--;
307 }
308 CA_FOREACH_END()
309
310 if (cmd == CMD_DOWN)
311 {
312 MenuPlaySound(MENU_SOUND_SWITCH);
313 pl->scroll++;
314 }
315 else if (cmd == CMD_UP)
316 {
317 MenuPlaySound(MENU_SOUND_SWITCH);
318 pl->scroll--;
319 }
320 else if (AnyButton(cmd))
321 {
322 MenuPlaySound(MENU_SOUND_BACK);
323 return 1;
324 }
325 // Scroll wrap-around
326 pl->scroll = CLAMP_OPPOSITE(pl->scroll, 0, PlayerListMaxScroll(pl));
327 return 0;
328 }
329 static int PlayerListMaxScroll(const PlayerList *pl)
330 {
331 return MAX((int)pl->playerUIDs.size - PlayerListMaxRows(pl), 0);
332 }
333 static int PlayerListMaxRows(const PlayerList *pl)
334 {
335 return (pl->size.y - FontH() * 3) / PLAYER_LIST_ROW_HEIGHT - 2;
336 }
337
338 typedef struct
339 {
340 const Campaign *Campaign;
341 const char *FinalWords;
342 } VictoryData;
343 static void VictoryDraw(void *data);
344 GameLoopData *ScreenVictory(Campaign *c)
345 {
346 SoundPlay(&gSoundDevice, StrSound("victory"));
347 VictoryData *data;
348 CMALLOC(data, sizeof *data);
349 data->Campaign = c;
350 const char *finalWordsSingle[] = {
351 "Ha, next time I'll use my good hand",
352 "Over already? I was just warming up...",
353 "There's just no good opposition to be found these days!",
354 "Well, maybe I'll just do my monthly reload then",
355 "Woof woof",
356 "I'll just bury the bones in the back yard, he-he",
357 "I just wish they'd let me try bare-handed",
358 "Rambo? Who's Rambo?",
359 "<in Austrian accent:> I'll be back",
360 "Gee, my trigger finger is sore",
361 "I need more practice. I think I missed a few shots at times"};
362 const char *finalWordsMulti[] = {
363 "United we stand, divided we conquer",
364 "Nothing like good teamwork, is there?",
365 "Which way is the camera?",
366 "We eat bullets for breakfast and have grenades as dessert",
367 "We're so cool we have to wear mittens",
368 };
369 if (GetNumPlayers(PLAYER_ANY, false, true) == 1)
370 {
371 const int numWords = sizeof finalWordsSingle / sizeof(char *);
372 data->FinalWords = finalWordsSingle[rand() % numWords];
373 }
374 else
375 {
376 const int numWords = sizeof finalWordsMulti / sizeof(char *);
377 data->FinalWords = finalWordsMulti[rand() % numWords];
378 }
379 PlayerList *pl =
380 PlayerListNew(PlayerListUpdate, VictoryDraw, data, true, false);
381 pl->pos.y = 75;
382 pl->size.y -= pl->pos.y;
383 MusicPlayFromChunk(
384 &gSoundDevice.music, MUSIC_VICTORY,
385 &gCampaign.Setting.CustomSongs[MUSIC_VICTORY]);
386 return PlayerListLoop(pl);
387 }
388 static void VictoryDraw(void *data)
389 {
390 const VictoryData *vd = data;
391
392 const int w = gGraphicsDevice.cachedConfig.Res.x;
393 FontOpts opts = FontOptsNew();
394 opts.HAlign = ALIGN_CENTER;
395 opts.Area = gGraphicsDevice.cachedConfig.Res;
396 int y = 30;
397
398 // Congratulations text
399 #define CONGRATULATIONS "Congratulations, you have completed "
400 FontStrOpt(CONGRATULATIONS, svec2i(0, y), opts);
401 y += 15;
402 opts.Mask = colorRed;
403 FontStrOpt(vd->Campaign->Setting.Title, svec2i(0, y), opts);
404 y += 15;
405
406 // Final words
407 struct vec2i pos = svec2i((w - FontStrW(vd->FinalWords)) / 2, y);
408 pos = FontChMask('"', pos, colorDarker);
409 pos = FontStrMask(vd->FinalWords, pos, colorPurple);
410 FontChMask('"', pos, colorDarker);
411 }
412
413 static GameLoopResult DogfightScoresUpdate(GameLoopData *data, LoopRunner *l);
414 GameLoopData *ScreenDogfightScores(void)
415 {
416 // Calculate PVP rounds won
417 int maxScore = 0;
418 CA_FOREACH(PlayerData, p, gPlayerDatas)
419 if (IsPlayerAlive(p))
420 {
421 p->Totals.Score++;
422 maxScore = MAX(maxScore, (int)p->Totals.Score);
423 }
424 CA_FOREACH_END()
425 gCampaign.IsComplete = maxScore == ModeMaxRoundsWon(gCampaign.Entry.Mode);
426 CASSERT(
427 maxScore <= ModeMaxRoundsWon(gCampaign.Entry.Mode),
428 "score exceeds max rounds won");
429
430 PlayerList *pl =
431 PlayerListNew(DogfightScoresUpdate, NULL, NULL, false, false);
432 pl->pos.y = 24;
433 pl->size.y -= pl->pos.y;
434 return PlayerListLoop(pl);
435 }
436 static GameLoopResult DogfightScoresUpdate(GameLoopData *data, LoopRunner *l)
437 {
438 PlayerList *pl = data->Data;
439
440 const GameLoopResult result = MenuUpdate(&pl->ms);
441 if (result == UPDATE_RESULT_OK && gCampaign.IsComplete)
442 {
443 LoopRunnerChange(l, ScreenDogfightFinalScores());
444 }
445 return result;
446 }
447
448 GameLoopData *ScreenDogfightFinalScores(void)
449 {
450 SoundPlay(&gSoundDevice, StrSound("victory"));
451 PlayerList *pl = PlayerListNew(PlayerListUpdate, NULL, NULL, true, true);
452 pl->pos.y = 24;
453 pl->size.y -= pl->pos.y;
454 return PlayerListLoop(pl);
455 }
456
457 GameLoopData *ScreenDeathmatchFinalScores(void)
458 {
459 SoundPlay(&gSoundDevice, StrSound("victory"));
460 PlayerList *pl = PlayerListNew(PlayerListUpdate, NULL, NULL, true, true);
461 pl->pos.y = 24;
462 pl->size.y -= pl->pos.y;
463 return PlayerListLoop(pl);
464 }