| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 50 |
|
| 51 |
|
| 52 |
bool hasMenu; |
| 53 |
bool showWinners; |
| 54 |
bool showLastMan; |
| 55 |
|
| 56 |
CArray playerUIDs; |
| 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 |
|
| 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 |
|
| 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 |
|
| 198 |
|
| 199 |
const PlayerList *pl = data; |
| 200 |
|
| 201 |
|
| 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 |
|
| 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 |
|
| 232 |
const color_t textColor = p->IsLocal ? colorPurple : colorWhite; |
| 233 |
|
| 234 |
|
| 235 |
DisplayCharacterAndName( |
| 236 |
svec2i(x + (i & 1) * 16, y + 4), &p->Char, DIRECTION_DOWN, p->name, |
| 237 |
textColor, p->guns[0]); |
| 238 |
|
| 239 |
|
| 240 |
x += 68; |
| 241 |
char buf[256]; |
| 242 |
sprintf(buf, "%d", p->Totals.Score); |
| 243 |
FontStrMask(buf, svec2i(x, y), textColor); |
| 244 |
|
| 245 |
|
| 246 |
x += 32; |
| 247 |
sprintf(buf, "%d", p->Totals.Kills); |
| 248 |
FontStrMask(buf, svec2i(x, y), textColor); |
| 249 |
|
| 250 |
|
| 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 |
|
| 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 |
|
| 267 |
FontStrMask("Last man standing!", svec2i(x, y), colorGreen); |
| 268 |
} |
| 269 |
|
| 270 |
y += PLAYER_LIST_ROW_HEIGHT; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 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 |
|
| 288 |
if (pl->drawFunc) |
| 289 |
{ |
| 290 |
pl->drawFunc(pl->data); |
| 291 |
} |
| 292 |
} |
| 293 |
static int PlayerListInput(int cmd, void *data) |
| 294 |
{ |
| 295 |
|
| 296 |
|
| 297 |
PlayerList *pl = data; |
| 298 |
|
| 299 |
|
| 300 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |