This repository has been archived by the owner on Apr 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRecordings.c
1322 lines (1140 loc) · 38.8 KB
/
Recordings.c
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Recordings.c
* Brogue
*
* Created by Brian Walker on 8/8/10.
* Copyright 2012. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <math.h>
#include <time.h>
#include <limits.h>
#include "Rogue.h"
#include "IncludeGlobals.h"
#define RECORDING_HEADER_LENGTH 32 // bytes at the start of the recording file to store global data
#pragma mark Recording functions
void recordChar(unsigned char c) {
inputRecordBuffer[locationInRecordingBuffer++] = c;
recordingLocation++;
}
void considerFlushingBufferToFile() {
if (locationInRecordingBuffer >= INPUT_RECORD_BUFFER) {
flushBufferToFile();
}
}
// compresses a uchar into a char, discarding stuff we don't need
unsigned char compressKeystroke(uchar c) {
const uchar ucharTable[] = {UP_ARROW, LEFT_ARROW, DOWN_ARROW, RIGHT_ARROW,
ESCAPE_KEY, RETURN_KEY, ENTER_KEY, DELETE_KEY, TAB_KEY, NUMPAD_0, NUMPAD_1,
NUMPAD_2, NUMPAD_3, NUMPAD_4, NUMPAD_5, NUMPAD_6, NUMPAD_7, NUMPAD_8, NUMPAD_9};
short i;
for (i=0; i<19; i++) {
if (ucharTable[i] == c) {
return (unsigned char) (128 + i);
}
}
if (c < 256) {
return (unsigned char) c;
}
return UNKNOWN_KEY;
}
void numberToString(unsigned long number, short numberOfBytes, unsigned char *recordTo) {
short i;
unsigned long n;
n = number;
for (i=numberOfBytes - 1; i >= 0; i--) {
recordTo[i] = n % 256;
n /= 256;
}
if (n > 0) {
printf("\nError: the number %li does not fit in %i bytes.", number, numberOfBytes);
brogueAssert(false);
}
}
// numberOfBytes can't be greater than 10
void recordNumber(unsigned long number, short numberOfBytes) {
short i;
unsigned char c[10];
numberToString(number, numberOfBytes, c);
for (i=0; i<numberOfBytes; i++) {
recordChar(c[i]);
}
}
// Events are recorded as follows:
// Keystrokes: Event type, keystroke value, modifier flags. (3 bytes.)
// All other events: Event type, x-coordinate of the event, y-coordinate of the event, modifier flags. (4 bytes.)
// Note: these must be sanitized, because user input may contain more than one byte per parameter.
void recordEvent(rogueEvent *event) {
unsigned char c;
if (rogue.playbackMode) {
return;
}
recordChar((unsigned char) event->eventType);
if (event->eventType == KEYSTROKE) {
// record which key
c = compressKeystroke(event->param1);
if (c == UNKNOWN_KEY) {
return;
}
recordChar(c);
} else {
recordChar((unsigned char) event->param1);
recordChar((unsigned char) event->param2);
}
// record the modifier keys
c = 0;
if (event->controlKey) {
c += Fl(1);
}
if (event->shiftKey) {
c += Fl(2);
}
recordChar(c);
}
// For convenience.
void recordKeystroke(uchar keystroke, boolean controlKey, boolean shiftKey) {
rogueEvent theEvent;
if (rogue.playbackMode) {
return;
}
theEvent.eventType = KEYSTROKE;
theEvent.param1 = keystroke;
theEvent.controlKey = controlKey;
theEvent.shiftKey = shiftKey;
recordEvent(&theEvent);
}
// record a series of keystrokes; string must end with a null terminator
void recordKeystrokeSequence(unsigned char *keystrokeSequence) {
short i;
for (i=0; keystrokeSequence[i] != '\0'; i++) {
recordKeystroke(keystrokeSequence[i], false, false);
}
}
// For convenience.
void recordMouseClick(short x, short y, boolean controlKey, boolean shiftKey) {
rogueEvent theEvent;
if (rogue.playbackMode) {
return;
}
theEvent.eventType = MOUSE_UP;
theEvent.param1 = x;
theEvent.param2 = y;
theEvent.controlKey = controlKey;
theEvent.shiftKey = shiftKey;
recordEvent(&theEvent);
}
void writeHeaderInfo(char *path) {
unsigned char c[RECORDING_HEADER_LENGTH];
short i;
FILE *recordFile;
// Zero out the entire header to start.
for (i=0; i<RECORDING_HEADER_LENGTH; i++) {
c[i] = 0;
}
// Note the version string to gracefully deny compatibility when necessary.
for (i = 0; BROGUE_VERSION_STRING[i] != '\0'; i++) {
c[i] = BROGUE_VERSION_STRING[i];
}
i = 16;
numberToString(rogue.seed, 4, &c[i]);
i += 4;
numberToString(rogue.playerTurnNumber, 4, &c[i]);
i += 4;
numberToString(rogue.deepestLevel, 4, &c[i]);
i += 4;
numberToString(lengthOfPlaybackFile, 4, &c[i]);
i += 4;
if (!fileExists(path)) {
recordFile = fopen(path, "wb");
if (recordFile) {
fclose(recordFile);
}
}
recordFile = fopen(path, "r+b");
rewind(recordFile);
for (i=0; i<RECORDING_HEADER_LENGTH; i++) {
putc(c[i], recordFile);
}
if (recordFile) {
fclose(recordFile);
}
if (lengthOfPlaybackFile < RECORDING_HEADER_LENGTH) {
lengthOfPlaybackFile = RECORDING_HEADER_LENGTH;
}
}
void flushBufferToFile() {
short i;
FILE *recordFile;
if (rogue.playbackMode) {
return;
}
lengthOfPlaybackFile += locationInRecordingBuffer;
writeHeaderInfo(currentFilePath);
if (locationInRecordingBuffer != 0) {
recordFile = fopen(currentFilePath, "ab");
for (i=0; i<locationInRecordingBuffer; i++) {
putc(inputRecordBuffer[i], recordFile);
}
if (recordFile) {
fclose(recordFile);
}
locationInRecordingBuffer = 0;
}
}
#pragma mark Playback functions
void fillBufferFromFile() {
// short i;
FILE *recordFile;
recordFile = fopen(currentFilePath, "rb");
fseek(recordFile, positionInPlaybackFile, SEEK_SET);
fread((void *) inputRecordBuffer, 1, INPUT_RECORD_BUFFER, recordFile);
positionInPlaybackFile = ftell(recordFile);
fclose(recordFile);
locationInRecordingBuffer = 0;
}
unsigned char recallChar() {
unsigned char c;
if (recordingLocation > lengthOfPlaybackFile) {
return END_OF_RECORDING;
}
c = inputRecordBuffer[locationInRecordingBuffer++];
recordingLocation++;
if (locationInRecordingBuffer >= INPUT_RECORD_BUFFER) {
fillBufferFromFile();
}
return c;
}
uchar uncompressKeystroke(uchar c) {
const uchar ucharTable[] = {UP_ARROW, LEFT_ARROW, DOWN_ARROW, RIGHT_ARROW,
ESCAPE_KEY, RETURN_KEY, ENTER_KEY, DELETE_KEY, TAB_KEY, NUMPAD_0, NUMPAD_1,
NUMPAD_2, NUMPAD_3, NUMPAD_4, NUMPAD_5, NUMPAD_6, NUMPAD_7, NUMPAD_8, NUMPAD_9};
if (c >= 128 && c <= UNKNOWN_KEY) {
return ucharTable[c - 128];
}
return (uchar) c;
}
unsigned long recallNumber(short numberOfBytes) {
short i;
unsigned long n;
n = 0;
for (i=0; i<numberOfBytes; i++) {
n *= 256;
n += (unsigned long) recallChar();
}
return n;
}
#define OOS_APOLOGY "Playback of the recording has diverged from the originally recorded game.\n\n\
This could be caused by recording or playing the file on a modified version of Brogue, or it could \
simply be the result of a bug. (The recording feature is still in beta for this reason.)\n\n\
If this is a different computer from the one on which the recording was saved, the recording \
might succeed on the original computer."
void playbackPanic() {
cellDisplayBuffer rbuf[COLS][ROWS];
if (!rogue.playbackOOS) {
rogue.playbackFastForward = false;
rogue.playbackPaused = true;
rogue.playbackOOS = true;
blackOutScreen();
displayLevel();
refreshSideBar(-1, -1, false);
confirmMessages();
message("Playback is out of sync.", false);
printTextBox(OOS_APOLOGY, 0, 0, 0, &white, &black, rbuf, NULL, 0);
rogue.playbackMode = false;
displayMoreSign();
rogue.playbackMode = true;
overlayDisplayBuffer(rbuf, 0);
printf("\n\nPlayback panic at location %li!", recordingLocation - 1);
overlayDisplayBuffer(rbuf, 0);
mainInputLoop();
}
}
void recallEvent(rogueEvent *event) {
unsigned char c;
boolean tryAgain;
do {
tryAgain = false;
c = recallChar();
event->eventType = c;
switch (c) {
case KEYSTROKE:
// record which key
event->param1 = uncompressKeystroke(recallChar());
event->param2 = 0;
break;
case SAVED_GAME_LOADED:
tryAgain = true;
flashTemporaryAlert(" Saved game loaded ", 1000);
break;
case MOUSE_UP:
case MOUSE_DOWN:
case MOUSE_ENTERED_CELL:
case RIGHT_MOUSE_UP:
case RIGHT_MOUSE_DOWN:
event->param1 = recallChar();
event->param2 = recallChar();
break;
case RNG_CHECK:
case END_OF_RECORDING:
case EVENT_ERROR:
default:
message("Unrecognized event type in playback.", true);
printf("Unrecognized event type in playback: event ID %i", c);
tryAgain = true;
playbackPanic();
break;
}
} while (tryAgain && !rogue.gameHasEnded);
// record the modifier keys
c = recallChar();
event->controlKey = (c & Fl(1)) ? true : false;
event->shiftKey = (c & Fl(2)) ? true : false;
}
void loadNextAnnotation() {
unsigned long currentReadTurn;
short i;
FILE *annotationFile;
if (rogue.nextAnnotationTurn == -1) {
return;
}
annotationFile = fopen(annotationPathname, "r");
fseek(annotationFile, rogue.locationInAnnotationFile, SEEK_SET);
for (;;) {
// load turn number
if (fscanf(annotationFile, "%lu\t", &(currentReadTurn)) != 1) {
if (feof(annotationFile)) {
rogue.nextAnnotation[0] = '\0';
rogue.nextAnnotationTurn = -1;
break;
} else {
// advance to the end of the line
fgets(rogue.nextAnnotation, 5000, annotationFile);
continue;
}
}
// load description
fgets(rogue.nextAnnotation, 5000, annotationFile);
if (currentReadTurn > rogue.playerTurnNumber ||
(currentReadTurn <= 1 && rogue.playerTurnNumber <= 1 && currentReadTurn >= rogue.playerTurnNumber)) {
rogue.nextAnnotationTurn = currentReadTurn;
// strip the newline off the end
rogue.nextAnnotation[strlen(rogue.nextAnnotation) - 1] = '\0';
// strip out any gremlins in the annotation
for (i=0; i<5000 && rogue.nextAnnotation[i]; i++) {
if (rogue.nextAnnotation[i] < ' '
|| rogue.nextAnnotation[i] > '~') {
rogue.nextAnnotation[i] = ' ';
}
}
break;
}
}
rogue.locationInAnnotationFile = ftell(annotationFile);
fclose(annotationFile);
}
void displayAnnotation() {
cellDisplayBuffer rbuf[COLS][ROWS];
if (rogue.playbackMode
&& rogue.playerTurnNumber == rogue.nextAnnotationTurn) {
if (!rogue.playbackFastForward) {
refreshSideBar(-1, -1, false);
printTextBox(rogue.nextAnnotation, player.xLoc, 0, 0, &black, &white, rbuf, NULL, 0);
rogue.playbackMode = false;
displayMoreSign();
rogue.playbackMode = true;
overlayDisplayBuffer(rbuf, 0);
}
loadNextAnnotation();
}
}
#pragma mark Hybrid and miscellaneous
// creates a game recording file, or if in playback mode,
// initializes based on and starts reading from the recording file
void initRecording() {
short i;
char versionString[16], buf[100];
FILE *recordFile;
//initializeBrogueSaveLocation();
#ifdef AUDIT_RNG
if (fileExists(RNG_LOG)) {
remove(RNG_LOG);
}
RNGLogFile = fopen(RNG_LOG, "a");
#endif
locationInRecordingBuffer = 0;
positionInPlaybackFile = 0;
recordingLocation = 0;
maxLevelChanges = 0;
rogue.playbackOOS = false;
rogue.playbackOmniscience = false;
rogue.nextAnnotationTurn = 0;
rogue.nextAnnotation[0] = '\0';
rogue.locationInAnnotationFile = 0;
if (rogue.playbackMode) {
lengthOfPlaybackFile = 100000; // so recall functions don't freak out
rogue.playbackDelayPerTurn = DEFAULT_PLAYBACK_DELAY;
rogue.playbackDelayThisTurn = rogue.playbackDelayPerTurn;
rogue.playbackPaused = false;
fillBufferFromFile();
for (i=0; i<16; i++) {
versionString[i] = recallChar();
}
if (strcmp(versionString, BROGUE_VERSION_STRING)) {
rogue.playbackMode = false;
rogue.playbackFastForward = false;
sprintf(buf, "This file is from version %s and cannot be opened in version %s.", versionString, BROGUE_VERSION_STRING);
dialogAlert(buf);
rogue.playbackMode = true;
rogue.playbackPaused = true;
rogue.playbackFastForward = false;
rogue.playbackOOS = false;
rogue.gameHasEnded = true;
}
rogue.seed = recallNumber(4); // master random seed
rogue.howManyTurns = recallNumber(4); // how many turns are in this recording
maxLevelChanges = recallNumber(4); // how many times the player changes depths
lengthOfPlaybackFile = recallNumber(4);
seedRandomGenerator(rogue.seed);
previousGameSeed = rogue.seed;
if (fileExists(annotationPathname)) {
loadNextAnnotation();
} else {
rogue.nextAnnotationTurn = -1;
}
} else {
lengthOfPlaybackFile = 1;
remove(currentFilePath);
recordFile = fopen(currentFilePath, "wb"); // create the file
fclose(recordFile);
flushBufferToFile(); // header info never makes it into inputRecordBuffer when recording
}
rogue.currentTurnNumber = 0;
}
void OOSCheck(unsigned long x, short numberOfBytes) {
unsigned char eventType;
unsigned long recordedNumber;
if (rogue.playbackMode) {
eventType = recallChar();
recordedNumber = recallNumber(numberOfBytes);
if (eventType != RNG_CHECK || recordedNumber != x) {
if (eventType != RNG_CHECK) {
message("Event type mismatch in RNG check.", false);
playbackPanic();
} else if (recordedNumber != x) {
printf("\nExpected RNG output of %li; got %i.", recordedNumber, (int) x);
playbackPanic();
}
}
} else {
recordChar(RNG_CHECK);
recordNumber(x, numberOfBytes);
considerFlushingBufferToFile();
}
}
// compare a random number once per player turn so we instantly know if we are out of sync during playback
void RNGCheck() {
short oldRNG;
unsigned long randomNumber;
oldRNG = rogue.RNG;
rogue.RNG = RNG_SUBSTANTIVE;
//#ifdef AUDIT_RNG
//reportRNGState();
//#endif
randomNumber = (unsigned long) rand_range(0, 255);
OOSCheck(randomNumber, 1);
rogue.RNG = oldRNG;
}
boolean unpause() {
if (rogue.playbackOOS) {
flashTemporaryAlert(" Out of sync ", 2000);
} else if (rogue.playbackPaused) {
rogue.playbackPaused = false;
return true;
}
return false;
}
#define PLAYBACK_HELP_LINE_COUNT 19
void printPlaybackHelpScreen() {
short i, j;
cellDisplayBuffer dbuf[COLS][ROWS], rbuf[COLS][ROWS];
char helpText[PLAYBACK_HELP_LINE_COUNT][80] = {
"Commands:",
"",
" <space>: ****pause or unpause playback",
" k or up arrow: ****play back faster",
" j or down arrow: ****play back slower",
" >: ****skip to next level",
" 0-9: ****skip to specified turn number",
"l or right arrow: ****advance one turn (shift for 5 turns; control for 20)",
"",
" <tab>: ****enable or disable omniscience",
" return: ****examine surroundings",
" i: ****display inventory",
" D: ****display discovered items",
" V: ****view saved recording",
" O: ****open and resume saved game",
" N: ****begin a new game",
" Q: ****quit to title screen",
"",
" -- press any key to continue --"
};
// Replace the "****"s with color escapes.
for (i=0; i<PLAYBACK_HELP_LINE_COUNT; i++) {
for (j=0; helpText[i][j]; j++) {
if (helpText[i][j] == '*') {
j = encodeMessageColor(helpText[i], j, &white);
}
}
}
clearDisplayBuffer(dbuf);
for (i=0; i<PLAYBACK_HELP_LINE_COUNT; i++) {
printString(helpText[i], mapToWindowX(5), mapToWindowY(i), &itemMessageColor, &black, dbuf);
}
for (i=0; i<COLS; i++) {
for (j=0; j<ROWS; j++) {
dbuf[i][j].opacity = (i < STAT_BAR_WIDTH ? 0 : INTERFACE_OPACITY);
}
}
overlayDisplayBuffer(dbuf, rbuf);
rogue.playbackMode = false;
waitForAcknowledgment();
rogue.playbackMode = true;
overlayDisplayBuffer(rbuf, NULL);
}
void advanceToLocation(unsigned long destinationFrame) {
unsigned long progressBarInterval, initialFrameNumber;
rogueEvent theEvent;
boolean useProgressBar, omniscient, stealth, trueColors;
omniscient = rogue.playbackOmniscience;
stealth = rogue.displayAggroRangeMode;
trueColors = rogue.trueColorMode;
rogue.playbackOmniscience = false;
rogue.displayAggroRangeMode = false;
rogue.trueColorMode = false;
cellDisplayBuffer dbuf[COLS][ROWS];
if (destinationFrame < rogue.playerTurnNumber) {
useProgressBar = (destinationFrame > 100 ? true : false);
// Start the recording over, and fast-forward to chosen frame.
freeEverything();
randomNumbersGenerated = 0;
rogue.playbackMode = true;
initializeRogue(0); // Seed argument is ignored because we're in playback.
startLevel(rogue.depthLevel, 1);
if (useProgressBar) {
blackOutScreen();
}
} else {
useProgressBar = (destinationFrame - rogue.playerTurnNumber > 100 ? true : false);
}
clearDisplayBuffer(dbuf);
rectangularShading((COLS - 20) / 2, ROWS / 2, 20, 1, &black, INTERFACE_OPACITY, dbuf);
overlayDisplayBuffer(dbuf, 0);
commitDraws();
displayMoreSign();
rogue.playbackFastForward = true;
progressBarInterval = max(1, (destinationFrame - rogue.playerTurnNumber) / 500);
initialFrameNumber = rogue.playerTurnNumber;
while (rogue.playerTurnNumber < destinationFrame && !rogue.gameHasEnded && !rogue.playbackOOS) {
if (useProgressBar && !(rogue.playerTurnNumber % progressBarInterval)) {
rogue.playbackFastForward = false;
printProgressBar((COLS - 20) / 2, ROWS / 2, "[ Loading... ]",
rogue.playerTurnNumber - initialFrameNumber,
destinationFrame - initialFrameNumber, &darkPurple, false);
rogue.playbackFastForward = true;
pauseBrogue(1);
}
rogue.RNG = RNG_COSMETIC; // dancing terrain colors can't influence recordings
rogue.playbackDelayThisTurn = 0;
nextBrogueEvent(&theEvent, false, true, false);
rogue.RNG = RNG_SUBSTANTIVE;
executeEvent(&theEvent);
}
rogue.playbackOmniscience = omniscient;
rogue.displayAggroRangeMode = stealth;
rogue.trueColorMode = trueColors;
rogue.playbackPaused = true;
rogue.playbackFastForward = false;
confirmMessages();
updateMessageDisplay();
refreshSideBar(-1, -1, false);
displayLevel();
}
void promptToAdvanceToLocation(short keystroke) {
char entryText[30], buf[max(30, DCOLS)];
unsigned long destinationFrame;
boolean enteredText;
if (!rogue.playbackPaused || unpause()) {
buf[0] = (keystroke == '0' ? '\0' : keystroke);
buf[1] = '\0';
rogue.playbackMode = false;
enteredText = getInputTextString(entryText, "Go to turn number: ", log10(ULONG_MAX) - 1, buf, "", TEXT_INPUT_NUMBERS, false);
confirmMessages();
rogue.playbackMode = true;
if (enteredText && entryText[0] != '\0') {
sscanf(entryText, "%lu", &destinationFrame);
if (destinationFrame >= rogue.howManyTurns) {
flashTemporaryAlert(" Past end of recording ", 3000);
} else if (destinationFrame == rogue.playerTurnNumber) {
sprintf(buf, " Already at turn %li ", destinationFrame);
flashTemporaryAlert(buf, 1000);
} else {
advanceToLocation(destinationFrame);
}
rogue.playbackPaused = true;
}
}
}
void pausePlayback() {
short oldRNG;
if (!rogue.playbackPaused) {
rogue.playbackPaused = true;
messageWithColor(KEYBOARD_LABELS ? "recording paused. Press space to play." : "recording paused.",
&teal, false);
refreshSideBar(-1, -1, false);
oldRNG = rogue.RNG;
//rogue.RNG = RNG_SUBSTANTIVE;
mainInputLoop();
//rogue.RNG = oldRNG;
messageWithColor("recording unpaused.", &teal, false);
rogue.playbackPaused = false;
refreshSideBar(-1, -1, false);
rogue.playbackDelayThisTurn = DEFAULT_PLAYBACK_DELAY;
}
}
// Used to interact with playback -- e.g. changing speed, pausing.
void executePlaybackInput(rogueEvent *recordingInput) {
signed long key;
short newDelay, frameCount, x, y, previousDeepestLevel;
unsigned long destinationFrame;
boolean pauseState, proceed;
rogueEvent theEvent;
char path[BROGUE_FILENAME_MAX];
if (!rogue.playbackMode) {
return;
}
if (recordingInput->eventType == KEYSTROKE) {
key = recordingInput->param1;
stripShiftFromMovementKeystroke(&key);
switch (key) {
case UP_ARROW:
case UP_KEY:
newDelay = max(1, min(rogue.playbackDelayPerTurn / 1.5, rogue.playbackDelayPerTurn - 1));
if (newDelay != rogue.playbackDelayPerTurn) {
flashTemporaryAlert(" Faster ", 300);
}
rogue.playbackDelayPerTurn = newDelay;
break;
case DOWN_ARROW:
case DOWN_KEY:
newDelay = min(3000, max(rogue.playbackDelayPerTurn * 1.5, rogue.playbackDelayPerTurn + 1));
if (newDelay != rogue.playbackDelayPerTurn) {
flashTemporaryAlert(" Slower ", 300);
}
rogue.playbackDelayPerTurn = newDelay;
break;
case ACKNOWLEDGE_KEY:
if (rogue.playbackOOS && rogue.playbackPaused) {
flashTemporaryAlert(" Out of sync ", 2000);
} else {
rogue.playbackPaused = !rogue.playbackPaused;
}
break;
case TAB_KEY:
rogue.playbackOmniscience = !rogue.playbackOmniscience;
displayLevel();
refreshSideBar(-1, -1, false);
if (rogue.playbackOmniscience) {
messageWithColor("Omniscience enabled.", &teal, false);
} else {
messageWithColor("Omniscience disabled.", &teal, false);
}
break;
case DESCEND_KEY:
pauseState = rogue.playbackPaused;
previousDeepestLevel = rogue.deepestLevel;
if (!rogue.playbackPaused || unpause()) {
if ((unsigned long) rogue.deepestLevel < maxLevelChanges) {
displayCenteredAlert(" Loading... ");
pauseBrogue(5);
rogue.playbackFastForward = true;
while ((rogue.deepestLevel <= previousDeepestLevel || !rogue.playbackBetweenTurns)
&& !rogue.gameHasEnded) {
rogue.RNG = RNG_COSMETIC; // dancing terrain colors can't influence recordings
nextBrogueEvent(&theEvent, false, true, false);
rogue.RNG = RNG_SUBSTANTIVE;
executeEvent(&theEvent);
}
rogue.playbackFastForward = false;
rogue.playbackPaused = pauseState;
displayLevel();
refreshSideBar(-1, -1, false);
updateMessageDisplay();
} else {
flashTemporaryAlert(" Already reached deepest depth explored ", 1000);
}
}
rogue.playbackPaused = pauseState;
break;
case INVENTORY_KEY:
rogue.playbackMode = false;
displayInventory(ALL_ITEMS, 0, 0, true, false);
rogue.playbackMode = true;
break;
case RIGHT_KEY:
case RIGHT_ARROW:
case LEFT_KEY:
case LEFT_ARROW:
if (key == RIGHT_KEY || key == RIGHT_ARROW) {
frameCount = 1;
} else {
frameCount = -1;
}
if (recordingInput->shiftKey) {
frameCount *= 5;
rogue.playbackFastForward = true;
}
if (recordingInput->controlKey) {
frameCount *= 20;
rogue.playbackFastForward = true;
}
if (frameCount < 0) {
if ((unsigned long) (frameCount * -1) > rogue.playerTurnNumber) {
destinationFrame = 0;
} else {
destinationFrame = rogue.playerTurnNumber + frameCount;
}
} else {
destinationFrame = min(rogue.playerTurnNumber + frameCount, rogue.howManyTurns - 1);
}
if (destinationFrame == rogue.playerTurnNumber) {
flashTemporaryAlert(" Already at end of recording ", 1000);
} else if (frameCount < 0) {
rogue.playbackMode = false;
proceed = (rogue.playerTurnNumber < 100 || confirm("Rewind?", true));
rogue.playbackMode = true;
if (proceed) {
advanceToLocation(destinationFrame);
}
} else {
// advance by the right number of turns
if (!rogue.playbackPaused || unpause()) {
while (rogue.playerTurnNumber < destinationFrame && !rogue.gameHasEnded && !rogue.playbackOOS) {
rogue.RNG = RNG_COSMETIC; // dancing terrain colors can't influence recordings
rogue.playbackDelayThisTurn = 0;
nextBrogueEvent(&theEvent, false, true, false);
rogue.RNG = RNG_SUBSTANTIVE;
executeEvent(&theEvent);
}
rogue.playbackPaused = true;
if (rogue.playbackFastForward) {
rogue.playbackFastForward = false;
displayLevel();
updateMessageDisplay();
}
refreshSideBar(-1, -1, false);
}
}
break;
case HELP_KEY:
printPlaybackHelpScreen();
break;
case DISCOVERIES_KEY:
rogue.playbackMode = false;
printDiscoveriesScreen();
rogue.playbackMode = true;
break;
case MESSAGE_ARCHIVE_KEY:
rogue.playbackMode = false;
displayMessageArchive();
rogue.playbackMode = true;
break;
case VIEW_RECORDING_KEY:
confirmMessages();
rogue.playbackMode = false;
if (dialogChooseFile(path, RECORDING_SUFFIX, "View recording: ")) {
if (fileExists(path)) {
strcpy(rogue.nextGamePath, path);
rogue.nextGame = NG_VIEW_RECORDING;
rogue.gameHasEnded = true;
} else {
message("File not found.", false);
}
}
rogue.playbackMode = true;
break;
case LOAD_SAVED_GAME_KEY:
confirmMessages();
rogue.playbackMode = false;
if (dialogChooseFile(path, GAME_SUFFIX, "Open saved game: ")) {
if (fileExists(path)) {
strcpy(rogue.nextGamePath, path);
rogue.nextGame = NG_OPEN_GAME;
rogue.gameHasEnded = true;
} else {
message("File not found.", false);
}
}
rogue.playbackMode = true;
break;
case NEW_GAME_KEY:
rogue.playbackMode = false;
if (confirm("Close recording and begin a new game?", true)) {
rogue.nextGame = NG_NEW_GAME;
rogue.gameHasEnded = true;
}
rogue.playbackMode = true;
break;
case QUIT_KEY:
//freeEverything();
rogue.gameHasEnded = true;
rogue.playbackOOS = false;
rogue.creaturesWillFlashThisTurn = false;
break;
case TRUE_COLORS_KEY:
rogue.trueColorMode = !rogue.trueColorMode;
displayLevel();
refreshSideBar(-1, -1, false);
if (rogue.trueColorMode) {
messageWithColor(KEYBOARD_LABELS ? "Color effects disabled. Press '\\' again to enable." : "Color effects disabled.",
&teal, false);
} else {
messageWithColor(KEYBOARD_LABELS ? "Color effects enabled. Press '\\' again to disable." : "Color effects enabled.",
&teal, false);
}
break;
case AGGRO_DISPLAY_KEY:
rogue.displayAggroRangeMode = !rogue.displayAggroRangeMode;
displayLevel();
refreshSideBar(-1, -1, false);
if (rogue.displayAggroRangeMode) {
messageWithColor(KEYBOARD_LABELS ? "Stealth range displayed. Press ']' again to hide." : "Stealth range displayed.",
&teal, false);
} else {
messageWithColor(KEYBOARD_LABELS ? "Stealth range hidden. Press ']' again to display." : "Stealth range hidden.",
&teal, false);
}
break;
case SEED_KEY:
//rogue.playbackMode = false;
//DEBUG {displayGrid(safetyMap); displayMoreSign(); displayLevel();}
//rogue.playbackMode = true;
printSeed();
break;
default:
if (key >= '0' && key <= '9'
|| key >= NUMPAD_0 && key <= NUMPAD_9) {
promptToAdvanceToLocation(key);
}
break;
}
} else if (recordingInput->eventType == MOUSE_UP) {
x = recordingInput->param1;
y = recordingInput->param2;
if (windowToMapX(x) >= 0 && windowToMapX(x) < DCOLS && y >= 0 && y < MESSAGE_LINES) {
// If the click location is in the message block, display the message archive.
rogue.playbackMode = false;
displayMessageArchive();
rogue.playbackMode = true;
}
} else if (recordingInput->eventType == RIGHT_MOUSE_UP) {
rogue.playbackMode = false;
displayInventory(ALL_ITEMS, 0, 0, true, false);
rogue.playbackMode = true;
}
}
// Pass in defaultPath (the file name WITHOUT suffix), and the suffix.
// Get back either defaultPath, or "defaultPath N",
// where N is the lowest counting number that doesn't collide with an existing file.
void getAvailableFilePath(char *returnPath, const char *defaultPath, const char *suffix) {
char fullPath[BROGUE_FILENAME_MAX];
short fileNameIterator = 2;
strcpy(returnPath, defaultPath);
sprintf(fullPath, "%s%s", returnPath, suffix);
while (fileExists(fullPath)) {
sprintf(returnPath, "%s %i", defaultPath, fileNameIterator);
sprintf(fullPath, "%s%s", returnPath, suffix);
fileNameIterator++;
}