| 1 |
/* |
| 2 |
* PROPRIETARY INFORMATION. This software is proprietary to POWDER |
| 3 |
* Development, and is not to be reproduced, transmitted, or disclosed |
| 4 |
* in any way without written permission. |
| 5 |
* |
| 6 |
* Produced by: Jeff Lait |
| 7 |
* |
| 8 |
* POWDER Development |
| 9 |
* |
| 10 |
* NAME: hiscore.cpp ( POWDER Library, C++ ) |
| 11 |
* |
| 12 |
* COMMENTS: |
| 13 |
* Manages high score table |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
#include "hiscore.h" |
| 18 |
#include "sramstream.h" |
| 19 |
#include "creature.h" |
| 20 |
#include "gfxengine.h" |
| 21 |
#include "map.h" |
| 22 |
#include "victory.h" |
| 23 |
#include "msg.h" |
| 24 |
|
| 25 |
#define MAX_SCORES 15 |
| 26 |
|
| 27 |
#define FLAG_WON 2 |
| 28 |
#define FLAG_HAVEHEART 1 |
| 29 |
#define FLAG_NOONE 4 |
| 30 |
|
| 31 |
// #pragma pack(push, 1) |
| 32 |
|
| 33 |
int glbVersion = 118; |
| 34 |
int glbSaveCount = 0; |
| 35 |
bool glbIsNewGame = false; |
| 36 |
bool glbRNGValid = false; |
| 37 |
int glbRNGSeed = 0; |
| 38 |
|
| 39 |
extern u8 *getGlobalActionStrip(); |
| 40 |
extern void saveOptions(SRAMSTREAM &is); |
| 41 |
extern void loadOptions(SRAMSTREAM &os); |
| 42 |
|
| 43 |
void |
| 44 |
saveActionStrip(SRAMSTREAM &os) |
| 45 |
{ |
| 46 |
// Save the memory on the GBA... |
| 47 |
#ifdef HAS_STYLUS |
| 48 |
u8 *astrip = getGlobalActionStrip(); |
| 49 |
|
| 50 |
os.writeRaw((char *) astrip, 50); |
| 51 |
#endif |
| 52 |
} |
| 53 |
|
| 54 |
void |
| 55 |
loadActionStrip(SRAMSTREAM &is) |
| 56 |
{ |
| 57 |
// Save the memory on the GBA... |
| 58 |
#ifdef HAS_STYLUS |
| 59 |
u8 *astrip = getGlobalActionStrip(); |
| 60 |
|
| 61 |
is.readRaw((char *) astrip, 50); |
| 62 |
#endif |
| 63 |
} |
| 64 |
|
| 65 |
struct HISCORE_ENTRY |
| 66 |
{ |
| 67 |
// score of this character |
| 68 |
u16 score; |
| 69 |
// Number of turns taken. |
| 70 |
u16 turns; |
| 71 |
// Up to 8 chars in name. |
| 72 |
char name[8]; |
| 73 |
// Max depth, death depth |
| 74 |
u8 maxdlevel, deathdlevel; |
| 75 |
// Magic die & Hit die |
| 76 |
s16 mp, hp; |
| 77 |
u8 mpdie, hitdie; |
| 78 |
// What version |
| 79 |
u8 version; |
| 80 |
// Flags for winning, etc. |
| 81 |
u8 deathflag; |
| 82 |
}; |
| 83 |
|
| 84 |
// #pragma pack(pop) |
| 85 |
|
| 86 |
HISCORE_ENTRY glbScoreList[MAX_SCORES]; |
| 87 |
|
| 88 |
int |
| 89 |
hiscore_savecount() |
| 90 |
{ |
| 91 |
return glbSaveCount; |
| 92 |
} |
| 93 |
|
| 94 |
void |
| 95 |
hiscore_setsavecount(int newval) |
| 96 |
{ |
| 97 |
glbSaveCount = newval; |
| 98 |
} |
| 99 |
|
| 100 |
void |
| 101 |
hiscore_bumpsavecount() |
| 102 |
{ |
| 103 |
if (hiscore_savecount() < 100) |
| 104 |
hiscore_setsavecount(hiscore_savecount()+1); |
| 105 |
} |
| 106 |
|
| 107 |
void |
| 108 |
hiscore_flagnewgame(bool isnewgame) |
| 109 |
{ |
| 110 |
glbIsNewGame = isnewgame; |
| 111 |
} |
| 112 |
|
| 113 |
bool |
| 114 |
hiscore_isnewgame() |
| 115 |
{ |
| 116 |
return glbIsNewGame; |
| 117 |
} |
| 118 |
|
| 119 |
bool |
| 120 |
hiscore_advancedmode() |
| 121 |
{ |
| 122 |
if (glbScoreList[MAX_SCORES-1].turns == 0) |
| 123 |
return false; |
| 124 |
else |
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
void |
| 129 |
hiscore_clearEntry(HISCORE_ENTRY *entry) |
| 130 |
{ |
| 131 |
strcpy(entry->name, "No One"); |
| 132 |
entry->maxdlevel = entry->deathdlevel = 0; |
| 133 |
entry->mp = entry->hp = 0; |
| 134 |
entry->mpdie = entry->hitdie = 0; |
| 135 |
entry->deathflag = FLAG_NOONE; |
| 136 |
entry->score = 0; |
| 137 |
entry->turns = 0; |
| 138 |
entry->version = glbVersion; |
| 139 |
} |
| 140 |
|
| 141 |
void |
| 142 |
hiscore_saveEntry(SRAMSTREAM &os, HISCORE_ENTRY *entry) |
| 143 |
{ |
| 144 |
os.writeRaw((char *)entry, sizeof(HISCORE_ENTRY)); |
| 145 |
} |
| 146 |
|
| 147 |
void |
| 148 |
hiscore_loadEntry(SRAMSTREAM &is, HISCORE_ENTRY *entry) |
| 149 |
{ |
| 150 |
is.readRaw((char *)entry, sizeof(HISCORE_ENTRY)); |
| 151 |
} |
| 152 |
|
| 153 |
void |
| 154 |
hiscore_formatScore(char *line1, char *line2, |
| 155 |
HISCORE_ENTRY *entry, bool highlight) |
| 156 |
{ |
| 157 |
char name[9]; |
| 158 |
const char *state; |
| 159 |
int i; |
| 160 |
|
| 161 |
// Null terminate name. |
| 162 |
memcpy(name, entry->name, 8); |
| 163 |
name[8] = 0; |
| 164 |
// Add spaces to name. |
| 165 |
for (i = 0; i < 8; i++) |
| 166 |
if (!name[i]) |
| 167 |
break; |
| 168 |
for (; i < 8; i++) |
| 169 |
name[i] = ' '; |
| 170 |
|
| 171 |
// Format: |
| 172 |
// 123456789012345678901234567890 |
| 173 |
// +Brask Hhit/hdMmag/md Ldd/md |
| 174 |
// Heart $65536 t:83323 v061 |
| 175 |
sprintf(line1, "%c%s%c%03d/%02d%c%03d/%02d %c%02d/%02d", |
| 176 |
(highlight ? '+' : ' '), |
| 177 |
name, |
| 178 |
SYMBOL_HEART, |
| 179 |
entry->hp, entry->hitdie, |
| 180 |
SYMBOL_MAGIC, |
| 181 |
entry->mp, entry->mpdie, |
| 182 |
SYMBOL_DLEVEL, |
| 183 |
entry->deathdlevel, entry->maxdlevel); |
| 184 |
|
| 185 |
if (entry->deathflag & FLAG_WON) |
| 186 |
state = "Won!"; |
| 187 |
else if (entry->deathflag & FLAG_HAVEHEART) |
| 188 |
state = "Heart"; |
| 189 |
else |
| 190 |
state = "Dead"; |
| 191 |
sprintf(line2, " %s $%d t:%d v%03d", state, entry->score, entry->turns, entry->version); |
| 192 |
} |
| 193 |
|
| 194 |
void |
| 195 |
hiscore_init() |
| 196 |
{ |
| 197 |
SRAMSTREAM is(false); |
| 198 |
// Load the hiscore, which triggers the appropriate rebuilding |
| 199 |
hiscore_load(is); |
| 200 |
} |
| 201 |
|
| 202 |
int |
| 203 |
hiscore_getversion() |
| 204 |
{ |
| 205 |
return glbVersion; |
| 206 |
} |
| 207 |
|
| 208 |
void |
| 209 |
hiscore_load(SRAMSTREAM &is) |
| 210 |
{ |
| 211 |
char prefix[6]; |
| 212 |
bool clearsave = false, keepscore = true; |
| 213 |
int i; |
| 214 |
u8 version; |
| 215 |
u8 data; |
| 216 |
|
| 217 |
is.readRaw(prefix, 6); |
| 218 |
if (memcmp(prefix, "POWDER", 6)) |
| 219 |
{ |
| 220 |
// This is an invalid memory chunk, rebuild! |
| 221 |
clearsave = true; |
| 222 |
keepscore = false; |
| 223 |
for (i = 0; i < MAX_SCORES; i++) |
| 224 |
{ |
| 225 |
hiscore_clearEntry(&glbScoreList[i]); |
| 226 |
} |
| 227 |
} |
| 228 |
else |
| 229 |
{ |
| 230 |
// Check the version number. |
| 231 |
is.readRaw((char *) &version, 1); |
| 232 |
if (version != hiscore_getversion()) |
| 233 |
{ |
| 234 |
// Invalid version, hiscore is cool, but wipe save. |
| 235 |
clearsave = true; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
// Read save count. |
| 240 |
is.readRaw((char *) &data, 1); |
| 241 |
if (clearsave) |
| 242 |
{ |
| 243 |
// Consider it invalid. |
| 244 |
glbSaveCount = 0; |
| 245 |
} |
| 246 |
else |
| 247 |
{ |
| 248 |
glbSaveCount = data; |
| 249 |
} |
| 250 |
|
| 251 |
// Read the score entries. |
| 252 |
HISCORE_ENTRY blank; |
| 253 |
for (i = 0; i < MAX_SCORES; i++) |
| 254 |
{ |
| 255 |
if (keepscore) |
| 256 |
hiscore_loadEntry(is, &glbScoreList[i]); |
| 257 |
else |
| 258 |
hiscore_loadEntry(is, &blank); |
| 259 |
} |
| 260 |
|
| 261 |
// Read random seed and action bars settings. |
| 262 |
// These are reset with every new version since we don't want |
| 263 |
// to guarantee that ACTION_NAMEs or SPELL_NAMEs are consistent. |
| 264 |
glbRNGValid = false; |
| 265 |
if (!clearsave) |
| 266 |
{ |
| 267 |
glbRNGValid = true; |
| 268 |
is.readRaw((char *) &glbRNGSeed, sizeof(glbRNGSeed)); |
| 269 |
|
| 270 |
loadActionStrip(is); |
| 271 |
loadOptions(is); |
| 272 |
} |
| 273 |
|
| 274 |
// If the memory was entirely corrupt, rewrite with the valid stuff. |
| 275 |
if (!keepscore) |
| 276 |
{ |
| 277 |
SRAMSTREAM os(false); |
| 278 |
|
| 279 |
hiscore_save(os); |
| 280 |
hamfake_endWritingSession(); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
void |
| 285 |
hiscore_save(SRAMSTREAM &os) |
| 286 |
{ |
| 287 |
int i; |
| 288 |
u8 data; |
| 289 |
|
| 290 |
os.writeRaw("POWDER", 6); |
| 291 |
data = hiscore_getversion(); |
| 292 |
os.writeRaw((char *) &data, 1); |
| 293 |
|
| 294 |
data = glbSaveCount; |
| 295 |
os.writeRaw((char *) &data, 1); |
| 296 |
|
| 297 |
for (i = 0; i < MAX_SCORES; i++) |
| 298 |
{ |
| 299 |
hiscore_saveEntry(os, &glbScoreList[i]); |
| 300 |
} |
| 301 |
// Write out the current RNG value. |
| 302 |
glbRNGSeed = rand_getseed(); |
| 303 |
os.writeRaw((const char *) &glbRNGSeed, sizeof(glbRNGSeed)); |
| 304 |
|
| 305 |
saveActionStrip(os); |
| 306 |
saveOptions(os); |
| 307 |
} |
| 308 |
|
| 309 |
void |
| 310 |
hiscore_display(HISCORE_ENTRY *bonus, bool ischeater) |
| 311 |
{ |
| 312 |
int i; |
| 313 |
char line1[100], line2[100]; |
| 314 |
bool bonuswon, curwon, hadbonus; |
| 315 |
int place = -1; |
| 316 |
|
| 317 |
hadbonus = false; |
| 318 |
if (bonus) |
| 319 |
hadbonus = true; |
| 320 |
bonuswon = false; |
| 321 |
if (bonus && (bonus->deathflag & FLAG_WON)) |
| 322 |
bonuswon = true; |
| 323 |
|
| 324 |
for (i = 0; i < MAX_SCORES; i++) |
| 325 |
{ |
| 326 |
// Check to see if we insert bonus. |
| 327 |
curwon = (glbScoreList[i].deathflag & FLAG_WON) ? true : false; |
| 328 |
|
| 329 |
// We only compare winners with other winners. A winner always |
| 330 |
// scores higher than a loser. |
| 331 |
if ((!ischeater && bonus) && |
| 332 |
((bonus->score >= glbScoreList[i].score && (curwon == bonuswon)) |
| 333 |
|| (bonuswon && !curwon) |
| 334 |
) |
| 335 |
) |
| 336 |
{ |
| 337 |
hiscore_formatScore(line1, line2, bonus, true); |
| 338 |
gfx_pager_addsingleline(line1); |
| 339 |
gfx_pager_addsingleline(line2); |
| 340 |
bonus = 0; |
| 341 |
place = i; |
| 342 |
} |
| 343 |
|
| 344 |
// Ignore empty entries. |
| 345 |
if (glbScoreList[i].deathflag & FLAG_NOONE) |
| 346 |
continue; |
| 347 |
|
| 348 |
hiscore_formatScore(line1, line2, &glbScoreList[i], false); |
| 349 |
gfx_pager_addsingleline(line1); |
| 350 |
gfx_pager_addsingleline(line2); |
| 351 |
} |
| 352 |
|
| 353 |
// If still no bonus, put at end. |
| 354 |
if (bonus) |
| 355 |
{ |
| 356 |
gfx_pager_addsingleline("... ..."); |
| 357 |
hiscore_formatScore(line1, line2, bonus, true); |
| 358 |
gfx_pager_addsingleline(line1); |
| 359 |
gfx_pager_addsingleline(line2); |
| 360 |
} |
| 361 |
|
| 362 |
// Inform user how they placed: |
| 363 |
if (hadbonus) |
| 364 |
{ |
| 365 |
BUF buf; |
| 366 |
|
| 367 |
if (place >= 0) |
| 368 |
{ |
| 369 |
BUF placename = gram_createplace(place+1); |
| 370 |
buf.sprintf("You came in %s place! ", placename.buffer()); |
| 371 |
} |
| 372 |
else if (ischeater) |
| 373 |
{ |
| 374 |
buf.reference("Your save scumming disqualified you from the list. "); |
| 375 |
} |
| 376 |
else |
| 377 |
{ |
| 378 |
buf.reference("You did not make the high score list. "); |
| 379 |
} |
| 380 |
|
| 381 |
msg_report(buf); |
| 382 |
} |
| 383 |
|
| 384 |
int center; |
| 385 |
|
| 386 |
if (place < 0) |
| 387 |
center = MAX_SCORES * 2; |
| 388 |
else |
| 389 |
center = place * 2; |
| 390 |
|
| 391 |
if (!hadbonus) |
| 392 |
center = 0; |
| 393 |
|
| 394 |
gfx_pager_display(center); |
| 395 |
} |
| 396 |
|
| 397 |
bool |
| 398 |
hiscore_addAndDisplayEntry(MOB *mob, bool haswon, bool ischeater) |
| 399 |
{ |
| 400 |
int score; |
| 401 |
int i, j; |
| 402 |
bool hasheart; |
| 403 |
HISCORE_ENTRY newentry; |
| 404 |
|
| 405 |
hiscore_clearEntry(&newentry); |
| 406 |
|
| 407 |
score = mob->calcScore(haswon); |
| 408 |
|
| 409 |
hasheart = mob->hasItem(ITEM_BLACKHEART); |
| 410 |
|
| 411 |
newentry.score = score; |
| 412 |
if (speed_gettime() >= 65535) |
| 413 |
newentry.turns = 65535; |
| 414 |
else |
| 415 |
newentry.turns = speed_gettime(); |
| 416 |
|
| 417 |
// Compute the current and final depths. |
| 418 |
MAP *map, *lastmap; |
| 419 |
int maxdepth, depth; |
| 420 |
|
| 421 |
depth = glbCurLevel->getDepth(); |
| 422 |
lastmap = glbCurLevel; |
| 423 |
for (map = glbCurLevel; map; map = map->getMapDown(false)) |
| 424 |
{ |
| 425 |
lastmap = map; |
| 426 |
} |
| 427 |
|
| 428 |
maxdepth = lastmap->getDepth(); |
| 429 |
// copy avatar name. |
| 430 |
memcpy(newentry.name, glbAvatarName, 8); |
| 431 |
|
| 432 |
newentry.deathdlevel = depth; |
| 433 |
newentry.maxdlevel = maxdepth; |
| 434 |
|
| 435 |
newentry.mp = mob->getMP(); |
| 436 |
newentry.hp = mob->getHP(); |
| 437 |
|
| 438 |
newentry.mpdie = mob->getMagicDie(); |
| 439 |
newentry.hitdie = mob->getHitDie(); |
| 440 |
|
| 441 |
newentry.version = hiscore_getversion(); |
| 442 |
newentry.deathflag = 0; |
| 443 |
if (haswon) |
| 444 |
newentry.deathflag |= FLAG_WON; |
| 445 |
if (hasheart) |
| 446 |
newentry.deathflag |= FLAG_HAVEHEART; |
| 447 |
|
| 448 |
hiscore_display(&newentry, ischeater); |
| 449 |
|
| 450 |
// Add in the new score. |
| 451 |
for (i = 0; i < MAX_SCORES; i++) |
| 452 |
{ |
| 453 |
bool curwon; |
| 454 |
|
| 455 |
// Don't add cheaters |
| 456 |
if (ischeater) |
| 457 |
break; |
| 458 |
|
| 459 |
curwon = (glbScoreList[i].deathflag & FLAG_WON) ? true : false; |
| 460 |
// We only compare winners with other winners. A winner always |
| 461 |
// scores higher than a loser. |
| 462 |
if ( (newentry.score >= glbScoreList[i].score && (curwon == haswon)) |
| 463 |
|| (haswon && !curwon) |
| 464 |
) |
| 465 |
{ |
| 466 |
// Replace this entry. |
| 467 |
for (j = MAX_SCORES-1; j > i; j--) |
| 468 |
{ |
| 469 |
memcpy(&glbScoreList[j], &glbScoreList[j-1], |
| 470 |
sizeof(HISCORE_ENTRY)); |
| 471 |
} |
| 472 |
|
| 473 |
memcpy(&glbScoreList[i], &newentry, |
| 474 |
sizeof(HISCORE_ENTRY)); |
| 475 |
|
| 476 |
SRAMSTREAM os(false); |
| 477 |
hiscore_save(os); |
| 478 |
|
| 479 |
hamfake_endWritingSession(); |
| 480 |
|
| 481 |
return true; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
// Even if no highscore achieved, still resave the high score |
| 486 |
// table so options are saved. |
| 487 |
|
| 488 |
SRAMSTREAM os(false); |
| 489 |
hiscore_save(os); |
| 490 |
|
| 491 |
hamfake_endWritingSession(); |
| 492 |
|
| 493 |
return false; |
| 494 |
} |