| 1 |
#charset "us-ascii" |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (c) 2000, 2006 by Michael J. Roberts. All Rights Reserved. |
| 5 |
* |
| 6 |
* TADS 3 Library - scoring |
| 7 |
* |
| 8 |
* This module defines objects related to keeping track of the player's |
| 9 |
* score, which indicates the player's progress through the game. |
| 10 |
*/ |
| 11 |
|
| 12 |
/* include the library header */ |
| 13 |
#include "adv3.h" |
| 14 |
|
| 15 |
|
| 16 |
/* ------------------------------------------------------------------------ */ |
| 17 |
/* |
| 18 |
* An Achievement is an object used to award points in the score. For |
| 19 |
* most purposes, an achievement can be described simply by a string, |
| 20 |
* but the Achievement object provides more flexibility in describing |
| 21 |
* combined scores when a set of similar achievements are to be grouped. |
| 22 |
* |
| 23 |
* There are two ways to use the scoring system. |
| 24 |
* |
| 25 |
* 1. You can use a mix of string names and Achievement objects for |
| 26 |
* scoring items; each time you award a scoring item, you call the |
| 27 |
* function addToScore() to specify the achievement (by name or by |
| 28 |
* Achievement object) and the number of points to award. You can also |
| 29 |
* call the method addToScoreOnce() on an Achievement object to award |
| 30 |
* the scoring item, ensuring that the item is only awarded once in the |
| 31 |
* entire game (saving you the trouble of checking to see if the event |
| 32 |
* that triggered the scoring item has happened before already in the |
| 33 |
* same game). If you do this, you MUST set the property |
| 34 |
* gameMain.maxScore to reflect the maximum score possible in the game. |
| 35 |
* |
| 36 |
* 2. You can use EXCLUSIVELY Achievement objects to represents scoring |
| 37 |
* items, and give each Achievement object a 'points' property |
| 38 |
* indicating the number of points it's worth. To award a scoring item, |
| 39 |
* you call the method awardPoints() on an Achievement object. If you |
| 40 |
* use this style of scoring, the library AUTOMATICALLY computes the |
| 41 |
* gameMain.maxScore value, by adding up the 'points' values of all of |
| 42 |
* the Achievement objects in the game. For this to work properly, you |
| 43 |
* have to obey the following rules: |
| 44 |
* |
| 45 |
*. - use ONLY Achievement objects (never strings) to award points; |
| 46 |
*. - set the 'points' property of each Achievement to its score; |
| 47 |
*. - define Achievement objects statically only (never use 'new' to |
| 48 |
*. create an Achievement dynamically) |
| 49 |
*. - if an Achievement can be awarded more than once, you must override |
| 50 |
*. its 'maxPoints' property to reflect the total number of points it |
| 51 |
*. will be worth when it is awarded the maximum number of times; |
| 52 |
*. - always award an Achievement through its awardPoints() or |
| 53 |
*. awardPointsOnce() method; |
| 54 |
*. - there exists at least one solution of the game in which every |
| 55 |
*. Achievement object is awarded |
| 56 |
*/ |
| 57 |
class Achievement: object |
| 58 |
/* |
| 59 |
* The number of points this Achievement scores individually. By |
| 60 |
* default, we set this to nil. If you use the awardPoints() or |
| 61 |
* awardPointsOnce() methods, you MUST set this to a non-nil value. |
| 62 |
* |
| 63 |
* If you set this to a non-nil value, the library will use it |
| 64 |
* pre-compute the maximum possible score in the game, saving you the |
| 65 |
* trouble of figuring out the maximum score by hand. |
| 66 |
*/ |
| 67 |
points = nil |
| 68 |
|
| 69 |
/* |
| 70 |
* The MAXIMUM number of points this Achievement can award. This is |
| 71 |
* by default just our 'points' value, on the assumption that the |
| 72 |
* achievement is scored only once. The library uses this value |
| 73 |
* during pre-initialization to compute the maximum possible score in |
| 74 |
* the game. |
| 75 |
*/ |
| 76 |
maxPoints = (points) |
| 77 |
|
| 78 |
/* |
| 79 |
* Describe the achievement - this must display a string explaining |
| 80 |
* the reason the points associated with this achievement were |
| 81 |
* awarded. |
| 82 |
* |
| 83 |
* Note that this description can make use of the scoreCount |
| 84 |
* information to show different descriptions depending on how many |
| 85 |
* times the item has scored. For example, an achievement for |
| 86 |
* finding various treasure items might want to display "finding a |
| 87 |
* treasure" if only one treasure was found and "finding five |
| 88 |
* treasures" if five were found. |
| 89 |
* |
| 90 |
* In some cases, it might be desirable to keep track of additional |
| 91 |
* custom information, and use that information in generating the |
| 92 |
* description. For example, the game might keep a list of |
| 93 |
* treasures found with the achievement, adding to the list each |
| 94 |
* time the achievement is scored, and displaying the contents of |
| 95 |
* the list when the description is shown. |
| 96 |
*/ |
| 97 |
desc = "" |
| 98 |
|
| 99 |
/* show myself in a full-score listing */ |
| 100 |
listFullScoreItem() |
| 101 |
{ |
| 102 |
/* show the number of points I'm worth */ |
| 103 |
gLibMessages.fullScoreItemPoints(totalPoints); |
| 104 |
|
| 105 |
/* show my description */ |
| 106 |
desc; |
| 107 |
} |
| 108 |
|
| 109 |
/* |
| 110 |
* The number of times the achievement has been awarded. Each time |
| 111 |
* the achievement is passed to addToScore(), this is incremented. |
| 112 |
* Note that this is distinct from the number of points. |
| 113 |
*/ |
| 114 |
scoreCount = 0 |
| 115 |
|
| 116 |
/* |
| 117 |
* the number of points awarded for the achievement; if this |
| 118 |
* achievement has been accomplished multiple times, this reflects |
| 119 |
* the aggregate number of points awarded for all of the times it |
| 120 |
* has been accomplished |
| 121 |
*/ |
| 122 |
totalPoints = 0 |
| 123 |
|
| 124 |
/* |
| 125 |
* Add this achievement to the score one time only, awarding the |
| 126 |
* given number of points. This can be used to score an achievement |
| 127 |
* without separately tracking whether or not the achievement has |
| 128 |
* been accomplished previously. If the achievement has already been |
| 129 |
* scored before, this will do nothing at all; otherwise, it'll score |
| 130 |
* the achievement with the given number of points. Returns true if |
| 131 |
* we do award the points, nil if not (because we've awarded them |
| 132 |
* before). |
| 133 |
*/ |
| 134 |
addToScoreOnce(points) |
| 135 |
{ |
| 136 |
/* if I've never been scored before, score me now */ |
| 137 |
if (scoreCount == 0) |
| 138 |
{ |
| 139 |
/* add the points to the score */ |
| 140 |
addToScore(points, self); |
| 141 |
|
| 142 |
/* tell the caller we awarded the points as requested */ |
| 143 |
return true; |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
/* tell the caller we didn't do anything */ |
| 148 |
return nil; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/* |
| 153 |
* Award this Achievement's score, using the score value specified in |
| 154 |
* my 'points' property. |
| 155 |
*/ |
| 156 |
awardPoints() |
| 157 |
{ |
| 158 |
/* add me to the score, using my 'points' property */ |
| 159 |
addToScore(points, self); |
| 160 |
} |
| 161 |
|
| 162 |
/* |
| 163 |
* Award this Achievement's score, but ensure that we're never |
| 164 |
* awarded more than one time. If this Achievement has already been |
| 165 |
* awarded, this does nothing at all. Returns true if we do award |
| 166 |
* the points, nil if not (because we've awarded them before). |
| 167 |
*/ |
| 168 |
awardPointsOnce() |
| 169 |
{ |
| 170 |
/* award my 'points' value only if we haven't score before */ |
| 171 |
return addToScoreOnce(points); |
| 172 |
} |
| 173 |
; |
| 174 |
|
| 175 |
/* |
| 176 |
* Generic text achievement. When we add an achievement to the full |
| 177 |
* score list and the achievement is a simple string description, we'll |
| 178 |
* create one of these to encapsulate the achievement. |
| 179 |
*/ |
| 180 |
class SimpleAchievement: Achievement |
| 181 |
/* create dynamically with a given string as our description */ |
| 182 |
construct(str) { desc_ = str; } |
| 183 |
|
| 184 |
/* show my description */ |
| 185 |
desc { say(desc_); } |
| 186 |
|
| 187 |
/* my description string */ |
| 188 |
desc_ = '' |
| 189 |
; |
| 190 |
|
| 191 |
/* |
| 192 |
* List interface for showing the full score list |
| 193 |
*/ |
| 194 |
fullScoreLister: Lister |
| 195 |
showListPrefixTall(itemCount, pov, parent) |
| 196 |
{ |
| 197 |
/* showt he full score list intro message */ |
| 198 |
gLibMessages.showFullScorePrefix; |
| 199 |
} |
| 200 |
|
| 201 |
/* every achievement is listed */ |
| 202 |
isListed(obj) { return true; } |
| 203 |
|
| 204 |
/* each item counts as a singular object grammatically */ |
| 205 |
listCardinality(obj) { return 1; } |
| 206 |
|
| 207 |
/* achievements have no containment hierarchy */ |
| 208 |
getContents(obj) { return []; } |
| 209 |
getListedContents(obj, infoTab) { return []; } |
| 210 |
showContentsList(pov, obj, options, indent, infoTab) { } |
| 211 |
showInlineContentsList(pov, obj, options, indent, infoTab) { } |
| 212 |
|
| 213 |
/* show an item */ |
| 214 |
showListItem(obj, options, pov, infoTab) { obj.listFullScoreItem(); } |
| 215 |
; |
| 216 |
|
| 217 |
|
| 218 |
/* |
| 219 |
* Score notification daemon handler. We'll receive a |
| 220 |
* checkNotification() call each turn; we'll display a notification |
| 221 |
* message each time the score has changed since the last time we ran. |
| 222 |
*/ |
| 223 |
scoreNotifier: PreinitObject |
| 224 |
/* the score as it was the last time we displayed a notification */ |
| 225 |
lastScore = static (libScore.totalScore) |
| 226 |
|
| 227 |
/* we've never generated a notification about the score before */ |
| 228 |
everNotified = nil |
| 229 |
|
| 230 |
/* daemon entrypoint */ |
| 231 |
checkNotification() |
| 232 |
{ |
| 233 |
/* |
| 234 |
* if the score has changed since the last time we checked, |
| 235 |
* possibly generate a notification |
| 236 |
*/ |
| 237 |
if (libScore.totalScore != lastScore) |
| 238 |
{ |
| 239 |
/* only show a message if we're allowed to */ |
| 240 |
if (libScore.scoreNotify.isOn) |
| 241 |
{ |
| 242 |
local delta; |
| 243 |
|
| 244 |
/* calculate the change since the last notification */ |
| 245 |
delta = libScore.totalScore - lastScore; |
| 246 |
|
| 247 |
/* |
| 248 |
* generate the first or non-first notification, as |
| 249 |
* appropriate |
| 250 |
*/ |
| 251 |
if (everNotified) |
| 252 |
gLibMessages.scoreChange(delta); |
| 253 |
else |
| 254 |
gLibMessages.firstScoreChange(delta); |
| 255 |
|
| 256 |
/* |
| 257 |
* note that we've ever generated a score change |
| 258 |
* notification, so that we don't generate the more |
| 259 |
* verbose first-time message on subsequent |
| 260 |
* notifications |
| 261 |
*/ |
| 262 |
everNotified = true; |
| 263 |
} |
| 264 |
|
| 265 |
/* |
| 266 |
* Remember the current score, so that we don't generate |
| 267 |
* another notification until the score has changed again. |
| 268 |
* Note that we note the new score even if we aren't |
| 269 |
* displaying a message this time, because we don't want to |
| 270 |
* generate a message upon re-enabling notifications. |
| 271 |
*/ |
| 272 |
lastScore = libScore.totalScore; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
/* execute pre-initialization */ |
| 277 |
execute() |
| 278 |
{ |
| 279 |
/* initialize the score change notification daemon */ |
| 280 |
new PromptDaemon(self, &checkNotification); |
| 281 |
} |
| 282 |
; |
| 283 |
|
| 284 |
/* |
| 285 |
* Add points to the total score. This is a convenience function that |
| 286 |
* simply calls libScore.addToScore_(). |
| 287 |
*/ |
| 288 |
addToScore(points, desc) |
| 289 |
{ |
| 290 |
/* simply call the libScore method to handle it */ |
| 291 |
libScore.addToScore_(points, desc); |
| 292 |
} |
| 293 |
|
| 294 |
/* |
| 295 |
* The main game score object. |
| 296 |
*/ |
| 297 |
libScore: PreinitObject |
| 298 |
/* |
| 299 |
* Add to the score. 'points' is the number of points to add to the |
| 300 |
* score, and 'desc' is a string describing the reason the points |
| 301 |
* are being awarded, or an Achievement object describing the points. |
| 302 |
* |
| 303 |
* We keep a list of each unique achievement. If 'desc' is already |
| 304 |
* in this list, we'll simply add the given number of points to the |
| 305 |
* existing entry for the same description. |
| 306 |
* |
| 307 |
* Note that, if 'desc' is an Achievement object, it will match a |
| 308 |
* previous item only if it's exactly the same Achievement instance. |
| 309 |
*/ |
| 310 |
addToScore_(points, desc) |
| 311 |
{ |
| 312 |
local idx; |
| 313 |
|
| 314 |
/* |
| 315 |
* if the description is a string, encapsulate it in a |
| 316 |
* SimpleAchievement object |
| 317 |
*/ |
| 318 |
if (dataType(desc) == TypeSString) |
| 319 |
{ |
| 320 |
local newDesc; |
| 321 |
|
| 322 |
/* |
| 323 |
* look for an existing SimpleAchievement in our list with |
| 324 |
* the same descriptive text - if we find one, reuse it, |
| 325 |
* since this is another instance of the same group of |
| 326 |
* achievements and thus can be combined into the same |
| 327 |
* achievement object |
| 328 |
*/ |
| 329 |
newDesc = fullScoreList.valWhich( |
| 330 |
{ x: x.ofKind(SimpleAchievement) && x.desc_ == desc }); |
| 331 |
|
| 332 |
/* |
| 333 |
* if we didn't find it, create a new simple achievement to |
| 334 |
* wrap the descriptive text |
| 335 |
*/ |
| 336 |
if (newDesc == nil) |
| 337 |
newDesc = new SimpleAchievement(desc); |
| 338 |
|
| 339 |
/* |
| 340 |
* for the rest of our processing, use the wrapper simple |
| 341 |
* achievement object instead of the original text string |
| 342 |
*/ |
| 343 |
desc = newDesc; |
| 344 |
} |
| 345 |
|
| 346 |
/* increase the use count for the achievement */ |
| 347 |
desc.scoreCount++; |
| 348 |
|
| 349 |
/* add the points to the total */ |
| 350 |
totalScore += points; |
| 351 |
|
| 352 |
/* try to find a match in our list of past achievements */ |
| 353 |
idx = fullScoreList.indexOf(desc); |
| 354 |
|
| 355 |
/* if we didn't find it, add it to the list */ |
| 356 |
if (idx == nil) |
| 357 |
fullScoreList.append(desc); |
| 358 |
|
| 359 |
/* |
| 360 |
* combine the points awarded this time into the total for this |
| 361 |
* achievement |
| 362 |
*/ |
| 363 |
desc.totalPoints += points; |
| 364 |
} |
| 365 |
|
| 366 |
/* |
| 367 |
* Explicitly run the score notification daemon. |
| 368 |
*/ |
| 369 |
runScoreNotifier() |
| 370 |
{ |
| 371 |
/* explicitly run the notification */ |
| 372 |
scoreNotifier.checkNotification(); |
| 373 |
} |
| 374 |
|
| 375 |
/* |
| 376 |
* Show the simple score |
| 377 |
*/ |
| 378 |
showScore() |
| 379 |
{ |
| 380 |
/* |
| 381 |
* Show the basic score statistics. Use the appropriate form of |
| 382 |
* the message, depending on whether or not there's a maximum |
| 383 |
* score value. |
| 384 |
*/ |
| 385 |
if (gameMain.maxScore != nil) |
| 386 |
gLibMessages.showScoreMessage(totalScore, |
| 387 |
gameMain.maxScore, |
| 388 |
libGlobal.totalTurns); |
| 389 |
else |
| 390 |
gLibMessages.showScoreNoMaxMessage(totalScore, |
| 391 |
libGlobal.totalTurns); |
| 392 |
|
| 393 |
/* show the score ranking */ |
| 394 |
showScoreRank(totalScore); |
| 395 |
} |
| 396 |
|
| 397 |
/* |
| 398 |
* show the score rank message |
| 399 |
*/ |
| 400 |
showScoreRank(points) |
| 401 |
{ |
| 402 |
local idx; |
| 403 |
local tab = gameMain.scoreRankTable; |
| 404 |
|
| 405 |
/* if there's no rank table, skip the ranking */ |
| 406 |
if (tab == nil) |
| 407 |
return; |
| 408 |
|
| 409 |
/* |
| 410 |
* find the last item for which our score is at least the |
| 411 |
* minimum - the table is in ascending order of minimum score, |
| 412 |
* so we want the last item for which our score is sufficient |
| 413 |
*/ |
| 414 |
idx = tab.lastIndexWhich({x: points >= x[1]}); |
| 415 |
|
| 416 |
/* if we didn't find an item, use the first by default */ |
| 417 |
if (idx == nil) |
| 418 |
idx = 1; |
| 419 |
|
| 420 |
/* show the description from the item we found */ |
| 421 |
gLibMessages.showScoreRankMessage(tab[idx][2]); |
| 422 |
} |
| 423 |
|
| 424 |
/* |
| 425 |
* Display the full score. 'explicit' is true if the player asked |
| 426 |
* for the full score explicitly, as with a FULL SCORE command; if |
| 427 |
* we're showing the full score automatically in the course of some |
| 428 |
* other action, 'explicit' should be nil. |
| 429 |
*/ |
| 430 |
showFullScore() |
| 431 |
{ |
| 432 |
/* show the basic score statistics */ |
| 433 |
showScore(); |
| 434 |
|
| 435 |
/* list the achievements in 'tall' mode */ |
| 436 |
fullScoreLister.showListAll(fullScoreList.toList(), |
| 437 |
ListTall, 0); |
| 438 |
} |
| 439 |
|
| 440 |
/* |
| 441 |
* Vector for the full score achievement list. This is a list of |
| 442 |
* all of the Achievement objects awarded for accomplishments so |
| 443 |
* far. |
| 444 |
*/ |
| 445 |
fullScoreList = static new Vector(32) |
| 446 |
|
| 447 |
/* the total number of points scored so far */ |
| 448 |
totalScore = 0 |
| 449 |
|
| 450 |
/* |
| 451 |
* current score notification status - if on, we'll show a message at |
| 452 |
* the end of each turn where the score changes, otherwise we won't |
| 453 |
* mention anything |
| 454 |
*/ |
| 455 |
scoreNotify = scoreNotifySettingsItem |
| 456 |
|
| 457 |
/* |
| 458 |
* Compute the sum of the maximum point values of the Achievement |
| 459 |
* objects in the game. Point values are optional in Achievement |
| 460 |
* objects; if there are no Achievement objects with non-nil point |
| 461 |
* values, this will simply return nil. |
| 462 |
*/ |
| 463 |
calcMaxScore() |
| 464 |
{ |
| 465 |
local sum; |
| 466 |
local found; |
| 467 |
|
| 468 |
/* start with a running total of zero */ |
| 469 |
sum = 0; |
| 470 |
|
| 471 |
/* we haven't found any non-nil point values yet */ |
| 472 |
found = nil; |
| 473 |
|
| 474 |
/* |
| 475 |
* Run through all of the Achievement objects to see if we can |
| 476 |
* derive a maximum score for the game. |
| 477 |
*/ |
| 478 |
forEachInstance(Achievement, function(obj) { |
| 479 |
local m; |
| 480 |
|
| 481 |
/* |
| 482 |
* If this object has a non-nil maxPoints value, add it to |
| 483 |
* the running total. |
| 484 |
*/ |
| 485 |
if ((m = obj.maxPoints) != nil) |
| 486 |
{ |
| 487 |
/* add this one to the sum */ |
| 488 |
sum += m; |
| 489 |
|
| 490 |
/* note that we found one with a non-nil point value */ |
| 491 |
found = true; |
| 492 |
} |
| 493 |
}); |
| 494 |
|
| 495 |
/* |
| 496 |
* If we found any Achievements with point values, return the sum |
| 497 |
* of those point values; otherwise, return nil. |
| 498 |
*/ |
| 499 |
return (found ? sum : nil); |
| 500 |
} |
| 501 |
|
| 502 |
/* execute pre-initialization */ |
| 503 |
execute() |
| 504 |
{ |
| 505 |
/* register as the global score handler */ |
| 506 |
libGlobal.scoreObj = self; |
| 507 |
} |
| 508 |
; |
| 509 |
|
| 510 |
/* settings item for score notification mode */ |
| 511 |
scoreNotifySettingsItem: BinarySettingsItem |
| 512 |
/* the "factory setting" for NOTIFY is ON */ |
| 513 |
isOn = true |
| 514 |
|
| 515 |
/* our configuration file variable ID */ |
| 516 |
settingID = 'adv3.notify' |
| 517 |
|
| 518 |
/* show our description */ |
| 519 |
settingDesc = (gLibMessages.shortNotifyStatus(isOn)) |
| 520 |
; |