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