forked from rogueforge/rogomatic14
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
1075 lines (914 loc) · 37.8 KB
/
main.c
File metadata and controls
1075 lines (914 loc) · 37.8 KB
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
/*
* Rog-O-Matic
* Automatically exploring the dungeons of doom.
*
* Copyright (C) 2008 by Anthony Molinaro
* Copyright (C) 1985 by Appel, Jacobson, Hamey, and Mauldin.
*
* This file is part of Rog-O-Matic.
*
* Rog-O-Matic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rog-O-Matic 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rog-O-Matic. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************
*
* History: I. Andrew Appel & Guy Jacobson, 10/81 [created]
* II. Andrew Appel & Guy Jacobson, 1/82 [added search]
* III. Michael Mauldin, 3/82 [added termcap]
* IV. Michael Mauldin, 3/82 [searching]
* V. Michael Mauldin, 4/82 [cheat mode]
* VI. Michael Mauldin, 4/82 [object database]
* VII. All three, 5/82 [running away]
* VIII. Michael Mauldin, 9/82 [improved cheating]
* IX. Michael Mauldin, 10/82 [replaced termcap]
* X. Mauldin, Hamey, 11/82 [Fixes, Rogue 5.2]
* XI. Mauldin, 11/82 [Fixes, Score lock]
* XII. Hamey, Mauldin, 06/83 [Fixes, New Replay]
* XIII. Mauldin, Hamey, 11/83 [Fixes, Rogue 5.3]
* XIV. Mauldin 01/85 [Fixes, UT mods]
* 0.0.0 Anthony Molinaro 03/2008 [Restored]
*
* General:
*
* This is the main routine for the player process, which decodes the
* Rogue output and sends commands back. This process is execl'd by the
* rogomatic process (cf. setup.c) which also execl's the Rogue process,
* conveniently connecting the two via two pipes.
*
* Source Files:
*
* arms.c Armor, Weapon, and Ring handling functions
* command.c Effector interface, sends cmds to Rogue
* database.c Memory for objects "discovered"
* debug.c Contains the debugging functions
* explore.c Path searching functions, exploration
* findscore.c Reads Rogue scoreboard
* io.c I/O functions, Sensory interface
* main.c Main Program for 'player' (this file)
* mess.c Handles messages from Rogue
* monsters.c Monster handling utilities
* mover.c Creates command strings to accomplish moves
* rooms.c Room specific functions, new levels
* scorefile.c Score file handling utilities
* search.c Does shortest path
* setup.c Main program for 'rogomatic'
* strategy.c Makes high level decisions
* survival.c Find cycles and places to run to
* tactics.c Medium level intelligence
* things.c Builds commands, part of Effector interface
* titlepage.c Prints the animated copyright notice
* utility.c Miscellaneous Unix (tm) functions
* worth.c Evaluates the items in the pack
*
* Include files:
*
* globals.h External defs for all global variables
* install.h Machine dependent DEFINES
* termtokens.h Defines various tokens to/from Rogue
* types.h Global DEFINES, macros, and typedefs.
*
* Other files which may be included with your distribution include
*
* rplot A shell script, prints a scatter plot of Rog's scores.
* rgmplot.c A program used by rplot.
* histplot.c A program which plots a histogram of Rgm's scores.
*
* Acknowledgments
*
* The UTexas modifications included in this distribution
* came from Dan Reynolds, and are included by permission.
* Rog-O-Matics first total winner against version 5.3 was
* on a UTexas computer.
*****************************************************************/
# include <stdlib.h>
# include <unistd.h>
# include <stdio.h>
# include <ctype.h>
# include <signal.h>
# include <setjmp.h>
# include <string.h>
# include <sys/types.h>
# include <errno.h>
# include <sys/stat.h>
# include "have_strlcat.h"
# include "have_strlcpy.h"
# include "strl.h"
# include "modern_curses.h"
# include "types.h"
# include "config.h"
# include "install.h"
# include "termtokens.h"
/* FIXME: get rid of this prototype in the correct way */
FILE *rogo_openlog (char *genelog);
/* global data - see globals.h for current definitions */
/* Files */
FILE *logfile = NULL; /* Rogomatic score file */
FILE *realstdout = NULL; /* Real stdout for Emacs, terse mode */
FILE *snapshot = NULL; /* File for snapshot command */
FILE *trogue = NULL; /* Pipe to Rogue process */
/* Characters */
char afterid = '\0'; /* Letter of obj after identify */
char genepool[TY_BUF + 1]; /* Gene pool, +1 for paranoia */
char *genocide; /* List of monsters to be genocided */
char genocided[MU_BUF + 1]; /* List of monsters genocided, +1 for paranoia */
char lastcmd[MU_BUF + 1]; /* Copy of last command sent to Rogue, +1 for paranoia */
char lastname[NAMSIZ + 1]; /* Name of last potion/scroll/wand, +1 for paranoia */
char nextid = '\0'; /* Next object to identify */
char screen[R][C + 1]; /* Map of current rogue screen - characters drawn by rogue, +1 for paranoia */
char sumline[BIGBUF + 1]; /* Termination message for Rogomatic, +1 for paranoia */
char sumline2[BIGBUF + 1]; /* alternate sumline buffer, +1 for paranoia */
char ourkiller[MU_BUF + 1]; /* What was listed on the tombstone - How we died, +1 for paranoia */
char pending_call_letter = ' '; /* If non-blank we have a call it to do - Pack object we know a name for */
char pending_call_name[NAMSIZ + 1]; /* Pack object name for letter, +1 for paranoia */
char versionstr[MU_BUF + 1]; /* Version of Rogue being used, +1 for paranoia */
char roguename[MU_BUF + 1]; /* "Rog-O-Matic XIV for" + Name we are playing under, +1 for paranoia */
char playername[MU_BUF + 1]; /* Name we are playing under, +1 for paranoia */
char *termination = "perditus"; /* Latin verb for how we died */
/* Integers */
bool aggravated = false; /* True if we have aggravated this level */
int agoalc = NONE; /* Goal square to arch from (row) */
int agoalr = NONE; /* Goal square to arch from (col) */
int ammo = 0; /* Number of missiles in pack */
bool arrowshot = false; /* True if an arrow shot us last turn */
int atrow = 0; /* Current position of the rogue (@) (row) */
int atcol = 0; /* Current position of the rogue (@) (col) */
int atrow0 = 0; /* Position at start of turn (row) */
int atcol0 = 0; /* Position at start of turn (col) */
int attempt = 0; /* Number times we searched whole level */
bool badarrow = false; /* True if cursed/lousy arrow in hand */
int beingheld = 0; /* Turns a fungus has held of us */
int beingstalked = 0; /* Invisible stalker strategies */
bool blinded = false; /* True if blinded */
int blindir = 0; /* Last direction we moved when blind */
int cancelled = 0; /* Turns till use cancellation again */
bool cheat = false; /* True ==> cheat, use bugs, etc. */
bool checkrange = false; /* True ==> check range */
bool chicken = false; /* True ==> check run away code */
bool compression = true; /* True ==> move more than one square/turn */
bool confused = false; /* True if we are confused */
bool cosmic = false; /* True if we are hallucinating */
int currentarmor = NONE; /* Index of our armor */
int currentweapon = NONE; /* Index of our weapon */
bool cursedarmor = false; /* True if our armor is cursed */
bool cursedweapon = false; /* True if we are wielding cursed weapon */
int darkdir = NONE; /* Direction of arrow in dark room */
int darkturns = 0; /* Number of arrows left to fire in dark room */
int debugging = D_NORMAL; /* Debugging options in effect */
int didreadmap = 0; /* Last level we read a map on */
int doorlist[40]; /* Holds row or column of new doors found on this level */
bool doublehasted = false; /* True if double hasted (Rogue 3.6) */
int droppedscare = 0; /* Number of scare mon. on this level */
bool diddrop = false; /* True if we dropped anything on this spot */
bool emacs = false; /* True ==> format output for Emacs */
bool exploredlevel = false; /* True if we completely explored this level */
bool floating = false; /* True if we are levitating */
int foughtmonster = 0; /* rounds we fought a monster */
bool foundarrowtrap = false; /* Found arrow trap this level */
bool foundtrapdoor = false; /* Found trap door this level */
int goalr = NONE; /* Current goal square (row) */
int goalc = NONE; /* Current goal square (col) */
int goodarrow = 0; /* Number of times we killed in one blow */
bool goodweapon = false; /* True if weapon in hand worth >= 100 */
int gplusdam = 1; /* Our plus damage bonus from strength */
int gplushit = 0; /* Our plus to hit bonus from strength */
bool hasted = false; /* True if hasted */
int head = 0; /* starting index of circular queue */
int tail = 0; /* ending index of circular queue */
int hitstokill = 0; /* Number of hits to kill last monster */
bool interrupted = false; /* True if at commandtop from onintr() */
bool knowident = false; /* True if found an identify scroll */
int larder = 1; /* Number of foods left */
int lastate = 0; /* Time we last ate */
int lastdamage = 0; /* Amount of last hit by a monster */
int lastdrop = NONE; /* Last object we tried to drop */
int lastfoodlevel = 1; /* Last level we found food */
int lastmonster = NONE; /* Last monster we tried to hit */
int lastobj = NONE; /* What did we last try to use */
int lastwand = NONE; /* Index of last wand */
int leftring = NONE; /* Index of our left ring */
bool logdigested = false; /* True if log file has been read by replay */
bool logging = false; /* True if keeping record of game */
bool lyinginwait = false; /* True if we waited for a monster */
int maxobj = 22; /* How much can we carry */
bool missedstairs = false; /* True if we searched everywhere */
int morecount = 0; /* Number of messages since last command */
bool msgonscreen = false; /* True if there is a rogomatic msg on the screen */
bool newarmor = true; /* True if our armor status has changed */
int *newdoors = NULL; /* pointer to a row or column of a new door found on this level */
bool newring = true; /* True if our ring status has changed */
bool newweapon = true; /* True if our armor status has changed */
bool nohalf = false; /* True if no halftime show */
bool noterm = false; /* True if no human watching */
int objcount = 0; /* Number of objects */
int ourscore = 0; /* Our score when we died/quit */
bool playing = true; /* True if still playing game */
bool poorarrow = false; /* True if arrow has missed */
bool protected = false; /* True if we protected our armor */
int putonseeinv = 0; /* Turn when last put on see inv ring */
bool redhands = false; /* True if we have red hands */
bool replaying = false; /* True if replaying old game */
bool revvideo = false; /* True if in rev. video mode */
int rightring = NONE; /* Index of our right ring */
int rogpid = -1; /* Pid of rogue process */
int room[RGRID + 1]; /* Flags for each room, +! for paranoia */
int row = 0; /* Current cursor position (row) */
int col = 0; /* Current cursor position (col) */
int scrmap[R][C + 1]; /* Flags bits for level map, +1 for paranoia */
int slowed = 0; /* turns since we slowed a monster */
int stairrow = 0; /* Position of stairs on this level (row) */
int staircol = 0; /* Position of stairs on this level (col) */
int teleported = 0; /* Number of times teleported this level */
bool terse = false; /* True if in terse mode */
bool transparent = false; /* True if in user command mode */
int trapr = NONE; /* Location of arrow trap, this level (row) */
int trapc = NONE; /* Location of arrow trap, this level (col) */
int urocnt = 0; /* Un-identified Rogue Object count */
bool usesynch = false; /* True when the inventory is correct - finished using something */
bool usingarrow = false; /* True if wielding an arrow from a trap */
int version = 0; /* rogue version is an integer as set by getrougeversion() */
int wplusdam = 2; /* Our plus damage from weapon bonus */
int wplushit = 1; /* Our plus hit from weapon bonus */
int zone = NONE; /* Current screen zone, 0..8 */
int zonemap[RGRID][RGRID + 1]; /* Connectivity map - Map of zones connections, +1 for paranoia */
/* Functions */
void (*istat)(int);
/* Stuff list, list of objects on this level */
stuffrec slist[MAXSTUFF + 1]; /* +1 for paranoia */
int slistlen = 0; /* count of objects in slist[] */
/* Monster list, list of monsters on this level */
monrec mlist[MAXMONST + 1]; /* +1 for paranoia */
int mlistlen = 0; /* count of monsters in mlist[] */
char targetmonster = '@'; /* Monster we are arching at */
/* Monster attribute and Long term memory arrays */
attrec monatt[Z + 1]; /* Monster attributes, +1 for paranoia */
lrnrec ltm; /* Long term memory -- general */
ltmrec monhist[MAXMON + 1]; /* Long term memory -- creatures, +1 for paranoia */
int nextmon = 0; /* Length of LTM */
int monindex[MAXMONST + 1]; /* Index into monhist array, +1 for paranoia */
/* Genetic learning parameters (and defaults) */
int geneid = 0; /* Id of genotype */
int genebest = 0; /* Best score of genotype */
int geneavg = 0; /* Average score of genotype */
int k_srch = 50; /* Propensity for searching for traps */
int k_door = 50; /* Propensity for searching for doors */
int k_rest = 50; /* Propensity for resting */
int k_arch = 50; /* Propensity for firing arrows */
int k_exper = 50; /* Level*10 on which to experiment with items */
int k_run = 50; /* Propensity for retreating */
int k_wake = 50; /* Propensity for waking things up */
int k_food = 50; /* Propensity for hoarding food (affects rings) */
static int knob[MAXKNOB] = {50, 50, 50, 50, 50, 50, 50, 50}; /* Knobs */
char *knob_name[MAXKNOB] = {
"trap searching: ",
"door searching: ",
"resting: ",
"using arrows: ",
"experimenting: ",
"retreating: ",
"waking monsters: ",
"hoarding food: "
};
/* Door search map */
char timessearched[R][C + 1]; /* +1 for paranoia */
char timestosearch = '\0';
int searchstartr = NONE;
int searchstartc = NONE;
int reusepsd = 0;
bool new_mark = true;
bool new_findroom = true;
bool new_search = true;
bool new_stairs = true;
bool new_arch = true;
/* Results of last call to makemove() */
int ontarget = 0;
int targetrow = NONE;
int targetcol = NONE;
/* Rog-O-Matics model of his stats */
int Level = 0;
int MaxLevel = 0;
int Gold = 0;
int Hp = 12;
int Hpmax = 12;
int Str = 16;
int Strmax = 16;
int Ac = 6;
int Exp = 0;
int Explev = 1;
int turns = 0;
char Ms[30]; /* The message about his state of hunger */
/* Miscellaneous movement tables, +1 for paranoia */
int deltrc[DNUM + 1] = { 1, -(C-1), -C, -(C+1), -1, C-1, C, C+1, 0 };
int deltc[DNUM + 1] = { 1, 1, 0, -1, -1, -1, 0, 1, 0 };
int deltr[DNUM + 1] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
char keydir[DNUM + 1] = { 'l', 'u', 'k', 'y', 'h', 'b', 'j', 'n', '\0' };
int movedir = 0;
/* Map characters on screen into object types */
stuff translate[128] = {
/* \00x */ none, none, none, none, none, none, none, none,
/* \01x */ none, none, none, none, none, none, none, none,
/* \02x */ none, none, none, none, none, none, none, none,
/* \03x */ none, none, none, none, none, none, none, none,
/* \04x */ none, potion, none, none, none, none, none, none,
/* \05x */ hitter, hitter, gold, none, amulet, none, none, wand,
/* \06x */ none, none, none, none, none, none, none, none,
/* \07x */ none, none, food, none, none, ring, none, Scroll,
/* \10x */ none, none, none, none, none, none, none, none,
/* \11x */ none, none, none, none, none, none, none, none,
/* \12x */ none, none, none, none, none, none, none, none,
/* \13x */ none, none, none, armor, none, armor, none, none,
/* \14x */ none, none, none, none, none, none, none, none,
/* \15x */ none, none, none, none, none, none, none, none,
/* \16x */ none, none, none, none, none, none, none, none,
/* \17x */ none, none, none, none, none, none, none, none
};
/* Inventory, contents of our pack */
char space[MAXINV][NAMSIZ + 1]; /* inventory string space, +1 for paranoia */
invrec inven[MAXINV + 1]; /* +1 for paranoia */
int invcount = 0;
/* Time history */
timerec timespent[50];
/* End of the game messages */
char *gamename = "Rog-O-Matic";
/* Used by onintr() to restart Rgm at top of command loop */
jmp_buf commandtop;
/* static storage */
static char genelock[TY_BUF + 1]; /* Gene pool lock file, +1 for paranoia */
static char genelog[TY_BUF + 1]; /* Genetic learning log file, +1 for paranoia */
/* static function declarations */
static void onintr (int sig);
static void startlesson (void);
static void endlesson (void);
/*
* Main program
*/
int
main (int argc, char *argv[])
{
char msg[SM_BUF + 1]; /* message buffer, +1 for paranoia */
bool singlestep = false; /* True ==> go one turn */
bool startecho = false; /* True ==> turn on echoing on startup */
bool startingup = true; /* True ==> startup started but not complete */
char logfilename[100]; /* Name of log file */
pid_t pid = -1; /* process id */
char pidfilename[TY_BUF + 1]; /* +1 for paranoia */
FILE *pidfp = NULL; /* open pidfilename stream */
char ch;
char *s;
int i;
/*
* Initialize some storage
*
* zeroize arrays and other initializations
*/
/* zeroize arrays */
memset (genepool, 0, sizeof(genepool)); /* paranoia */
memset (genocided, 0, sizeof(genocided)); /* paranoia */
memset (lastcmd, 0, sizeof(lastcmd)); /* paranoia */
lastcmd[0] = 'i';
memset (lastname, 0, sizeof(lastname)); /* paranoia */
for (i=0; i < R; ++i) {
memset (&(screen[i][0]), ' ', sizeof(screen[i])); /* screen lines initialize with ASCII space */
screen[i][C] = '\0'; /* paranoia */
}
memset (sumline, 0, sizeof(sumline)); /* paranoia */
memset (sumline2, 0, sizeof(sumline2)); /* paranoia */
memset (ourkiller, 0, sizeof(ourkiller)); /* paranoia */
strlcpy (ourkiller, "unknown", sizeof(ourkiller));
memset (pending_call_name, 0, sizeof(pending_call_name)); /* paranoia */
memset (versionstr, 0, sizeof(versionstr)); /* paranoia */
strlcpy (versionstr, DEFVER, sizeof(versionstr));
memset (rgmdir, 0, sizeof(rgmdir)); /* paranoia */
memset (lock_path, 0, sizeof(lock_path)); /* paranoia */
memset (roguename, 0, sizeof(roguename)); /* paranoia */
memset (playername, 0, sizeof(playername)); /* paranoia */
/**/
memset (room, 0, sizeof(room)); /* paranoia */
memset (scrmap, 0, sizeof(scrmap)); /* paranoia */
memset (zonemap, 0, sizeof(zonemap)); /* paranoia */
memset (slist, 0, sizeof(slist)); /* paranoia */
memset (mlist, 0, sizeof(mlist)); /* paranoia */
memset (monatt, 0, sizeof(monatt)); /* paranoia */
memset (<m, 0, sizeof(ltm)); /* paranoia */
memset (monhist, 0, sizeof(monhist)); /* paranoia */
memset (monindex, 0, sizeof(monindex)); /* paranoia */
memset (timessearched, 0, sizeof(timessearched)); /* paranoia */
memset (Ms, 0, sizeof(Ms)); /* paranoia */
memset (space, 0, sizeof(space)); /* paranoia */
memset (inven, 0, sizeof(inven)); /* paranoia */
doresetinv (); /* reset the inventory */
memset (timespent, 0, sizeof(timespent)); /* paranoia */
memset (commandtop, 0, sizeof(commandtop)); /* paranoia */
/* zeroize static storage */
memset (genelock, 0, sizeof(genelock)); /* paranoia */
memset (genelog, 0, sizeof(genelog)); /* paranoia */
/* zeroize local arrays */
memset (msg, 0, sizeof(msg)); /* paranoia */
memset (logfilename, 0, sizeof(logfilename)); /* paranoia */
memset (pidfilename, 0, sizeof(pidfilename)); /* paranoia */
/*
* on exit: cleanup I/O, and shutdown ncurses (if needed)
*/
(void) atexit (endwin_and_ncurses_cleanup);
int_exit (inter_endwin_and_ncurses_cleanup);
/* The second argument to player is the process id of Rogue */
errno = 0;
if (argc > 2) {
rogpid = atoi (argv[2]);
if (errno != 0) {
fprintf (stderr, "ERROR: %s: file: %s line: %d dungeon: %u argv[2]: %s cannot be converted into an int: %s\n",
__func__, __FILE__, __LINE__, dnum, argv[2], strerror (errno));
exit(1);
}
if (rogpid < 0) {
fprintf (stderr, "ERROR: %s: file: %s line: %d dungeon: %u argv[2]: %s pid arg < 0: %d\n",
__func__, __FILE__, __LINE__, dnum, argv[2], rogpid);
exit(1);
}
}
/* The third argument is an option list */
if (argc > 3) {
int cheat_int; /* integer form of cheat boolean */
int noterm_int; /* integer form of noterm boolean */
int startecho_int; /* integer form of startecho boolean */
int nohalf_int; /* integer form of nohalf boolean */
int emacs_int; /* integer form of emacs boolean */
int terse_int; /* integer form of terse boolean */
int transparent_int; /* integer form of transparent boolean */
/* parse options in third argument */
i = sscanf (argv[3], "%d,%d,%d,%d,%d,%d,%d,%u",
&cheat_int, ¬erm_int, &startecho_int, &nohalf_int,
&emacs_int, &terse_int, &transparent_int, &dnum);
if (i != 8) {
fprintf (stderr, "ERROR: %s: file: %s line: %d dungeon: %u argv[3]: %s failed to scanf 8 flags or values, returned: %d\n",
__func__, __FILE__, __LINE__, dnum, argv[3], i);
exit(1);
}
/* convert ints to booleans */
cheat = (cheat_int == 0) ? false : true;
noterm = (noterm_int == 0) ? false : true;
startecho = (startecho_int == 0) ? false : true;
nohalf = (nohalf_int == 0) ? false : true;
emacs = (emacs_int == 0) ? false : true;
terse = (terse_int == 0) ? false : true;
transparent = (transparent_int == 0) ? false : true;
}
/* The fourth argument is the Rogue name */
if (argc > 4) {
strlcpy (roguename, argv[4], sizeof(roguename));
} else {
snprintf (roguename, MU_BUF, "Rog-O-Matic %s", RGMVER);
}
roguename[MU_BUF] = '\0'; /* paranoia */
/*
* use the final word of the roguename as the player name
*/
s = strrchr (roguename, ' ');
if (s == NULL) {
s = roguename;
} else {
++s;
}
if (*s == '\0') {
s = "((rogo-rogue))";
}
strlcpy (playername, s, sizeof(playername));
/* The 5th argument is the non-default rogomatic directory path */
if (argc > 5) {
memset (rgmdir, 0, sizeof(rgmdir));
strlcpy (rgmdir, argv[5], sizeof(rgmdir));
}
/*
* determine the rogomatic directory path and rogomatic lock file path
*/
set_rgmdir (false);
/*
* send stderr to an errlog file
*/
redirect_stderr (rgmdir, "errlog");
/*
* append rogue pid and rogue dungeon number to pidlog
*/
append_pidlog (rgmdir, "pidlog");
/*
* The first argument to player is a two character string encoding
* the file descriptors of the pipe ends. See setup.c for call.
*
* If we get 'ZZ', then we are replaying an old game, and there
* are no pipes to read/write.
*/
if (argv[1][0] == 'Z') {
replaying = true;
gamename = "Iteratum Rog-O-Maticus";
termination = "finis";
strlcpy (logfilename, argv[4], sizeof(logfilename));
startreplay (&logfile, logfilename);
}
else {
int frogue_fd = argv[1][0] - 'a';
int trogue_fd = argv[1][1] - 'a';
open_frogue_fd (frogue_fd);
open_frogue_debuglog (rgmdir, "debuglog.frogue");
trogue = fdopen (trogue_fd, "w");
setbuf (trogue, NULL);
}
/*
* open debug logging
*
* Get the process id of this player program if the
* environment variable is set which requests this be
* done. Then create the file name with the PID so
* that the debugging scripts can find it and use the
* PID.
*
* This code can be removed if you don't need to use
* the debugging scripts.
*
*/
if (getenv("GETROGOMATICPID") != NULL) {
pid = getpid ();
memset (pidfilename, 0, sizeof(pidfilename));
snprintf (pidfilename, sizeof(pidfilename)-1, "%s/rogomaticpid.%d", rgmdir, pid);
if ((pidfp = fopen (pidfilename, "w")) == NULL) {
fprintf (stderr, "ERROR: %s: file: %s line: %d dungeon: %u Can't open '%s'.\n",
__func__, __FILE__, __LINE__, dnum, pidfilename);
exit(1);
}
}
debuglog_open (rgmdir, "debuglog.player");
/* If we are in one-line mode, then squirrel away stdout */
if (emacs || terse) {
realstdout = fdopen (dup (STDOUT_FILENO), "w");
freopen ("/dev/null", "w", stdout);
}
initscr (); crmode (); noecho (); /* Initialize the Curses package */
if (startecho) toggleecho (); /* Start logging? */
clear (); /* Clear the screen */
getrogver (); /* Figure out Rogue version */
if (!replaying) {
restoreltm (); /* Get long term memory of version */
startlesson (); /* Start genetic learning */
}
/*
* Give a hello message
*/
if (replaying)
snprintf (msg, SM_BUF, " Replaying log file %s, version %s.",
logfilename, versionstr);
else
snprintf (msg, SM_BUF, " %s: version %s, genotype %d.",
roguename, versionstr, geneid);
if (emacs)
{ fprintf (realstdout, "%s (%%b)", msg); fflush (realstdout); }
else if (terse)
{ fprintf (realstdout, "%s\n", msg); fflush (realstdout); }
else
{ saynow ("%s", msg); }
/*
* Now that we have the version figured out, we can properly
* interpret the screen. Force a redraw by sending a redraw
* screen command (^L for old, ^R for new).
*
* Also identify wands (/), so that we can differentiate
* older Rogue 3.6 from Rogue 3.6 with extra magic...
*/
if (version < RV53A)
sendnow ("%c//;", ctrl('l'));
else
sendnow ("%c;", ctrl('r'));
/*
* If we are not replaying an old game, we must position the
* input after the next form feed, which signals the start of
* the level drawing.
*/
{
if (!replaying)
while ((int) (ch = getroguetoken ()) != CL_TOK && (int) ch != EOF) {
/* FIXME: If you start next to a monster this will get stuck, as
pressing 'v' takes time in version 3.6, so rogue will be waiting
for input and we will be waiting for rogue to print a CL_TOK,
so deadlock - NYM */
}
}
/*
* Note: If we are replaying, the logfile is now in synch
*/
getrogue (ILL, 2); /* Read the input up to end of first command */
/* Identify all 26 monsters */
if (!replaying)
for (ch = 'A'; ch <= 'Z'; ch++) rogo_send ("/%c", ch);
/*
* Signal handling. On an interrupt, Rogomatic goes into transparent
* mode and clears what state information it can. This code is styled
* after that in "UNIX Programming -- Second Edition" by Brian
* Kernigan & Dennis Ritchie. I sure wouldn't have thought of it.
*/
istat = signal (SIGINT, SIG_IGN); /* save original status */
setjmp (commandtop); /* save stack position */
if (istat != SIG_IGN)
signal (SIGINT, onintr);
if (interrupted) {
saynow ("Interrupt [enter command]:");
interrupted = false;
transparent = true;
}
if (transparent) {
noterm = false;
}
while (playing) {
refresh ();
/* If we have any commands to send, send them */
while (resend ()) {
if (startingup) showcommand (lastcmd);
sendnow (";");
getrogue (ILL, 2);
}
if (startingup) { /* All monsters identified */
versiondep (); /* Do version specific things */
startingup = false; /* Clear starting flag */
}
if (!playing) break; /* In case we died */
/*
* No more stored commands, so either get a command from the
* user (if we are in transparent mode or the user has typed
* something), or let the strategize module try its luck. If
* strategize fails we wait for the user to type something. If
* there is no user (noterm mode) then use ROGQUIT to signal a
* quit command.
*/
if ((transparent && !singlestep) ||
(!emacs && charsavail ()) ||
!strategize()) {
ch = (noterm) ? ROGQUIT : getch ();
switch (ch) {
case '?': givehelp (); break;
case '\n': if (terse)
{ printsnap (realstdout); fflush (realstdout); }
else
{ singlestep = true; transparent = true; }
break;
/* Rogue Command Characters */
case 'H': case 'J': case 'K': case 'L':
case 'Y': case 'U': case 'B': case 'N':
case 'h': case 'j': case 'k': case 'l':
case 'y': case 'u': case 'b': case 'n':
case 's': command (T_OTHER, "%c", ch); transparent = true; break;
case 'f': ch = getch ();
for (s = "hjklyubnHJKLYUBN"; *s; s++) {
if (ch == *s) {
if (version < RV53A) command (T_OTHER, "f%c", ch);
else command (T_OTHER, "%c", ctrl (ch));
}
}
transparent = true; break;
case '\f': redrawscreen (); break;
case 'm': dumpmonstertable (); break;
case 'M': dumpmazedoor (); break;
case '>': if (atrow == stairrow && atcol == staircol)
command (T_OTHER, ">");
transparent = true; break;
case '<': if (atrow == stairrow && atcol == staircol &&
have (amulet) != NONE) command (T_OTHER, "<");
transparent = true; break;
case 't': transparent = !transparent; break;
case ')': new_mark = true; markcycles (DOPRINT); at (row, col); break;
case '+': setpsd (DOPRINT); at (row, col); break;
case 'A': attempt = (attempt+1) % 5;
saynow ("Attempt %d", attempt); break;
case 'G': mvprintw (0, 0,
"%d: Sr %d Dr %d Re %d Ar %d Ex %d Rn %d Wk %d Fd %d, %d/%d",
geneid, k_srch, k_door, k_rest, k_arch,
k_exper, k_run, k_wake, k_food, genebest, geneavg);
clrtoeol (); at (row, col); refresh (); break;
case ':': chicken = !chicken;
say (chicken ? "chicken" : "aggressive");
break;
case '~': if (replaying)
saynow ("Replaying log file %s, version %s.",
logfilename, versionstr);
else
saynow (" %s: version %s, genotype %d.",
roguename, versionstr, geneid);
break;
case '[': at (0,0);
printw ("%s = %d, %s = %d, %s = %d, %s = %d.",
"hitstokill", hitstokill,
"goodweapon", goodweapon,
"usingarrow", usingarrow,
"goodarrow", goodarrow);
clrtoeol ();
at (row, col);
refresh ();
break;
case '-': saynow ("%s", statusline ());
break;
case '`': clear ();
summary ((FILE *) NULL, '\n');
pauserogue ();
break;
case '|': clear ();
timehistory ((FILE *) NULL, '\n');
pauserogue ();
break;
case 'r': resetinv (); say ("Inventory reset."); break;
case 'i': clear (); dumpinv ((FILE *) NULL); pauserogue (); break;
case '/': dosnapshot ();
break;
case '(': clear (); dumpdatabase (); pauserogue (); break;
case 'c': cheat = !cheat;
say (cheat ? "cheating" : "righteous");
break;
case 'd': toggledebug (); break;
case 'e': toggleecho (); break;
case '!': dumpstuff (); break;
case '@': dumpmonster (); break;
case '#': dumpwalls (); break;
case '%': clear (); havearmor (1, DOPRINT, ANY); pauserogue (); break;
case ']': clear (); havearmor (1, DOPRINT, RUSTPROOF);
pauserogue (); break;
case '=': clear (); havering (1, DOPRINT); pauserogue (); break;
case '$': clear (); haveweapon (1, DOPRINT); pauserogue (); break;
case '^': clear (); havebow (1, DOPRINT); pauserogue (); break;
case '{': promptforflags (); break;
case '&': saynow ("Object count is %d.", objcount); break;
case '*': blinded = !blinded;
saynow (blinded ? "blinded" : "sighted");
break;
case 'C': cosmic = !cosmic;
saynow (cosmic ? "cosmic" : "boring");
break;
case 'E': dwait (D_ERROR, __func__, "Testing the ERROR trap"); break;
case 'F': dwait (D_FATAL, __func__, "Testing the FATAL trap"); break;
case 'R': if (replaying) {
positionreplay (); getrogue (ILL, 2);
if (transparent) singlestep = true;
}
else
saynow ("Replay position only works in replay mode.");
break;
case 'S': quitrogue ("saved", Gold, SAVED);
playing = false; break;
case 'Q': quitrogue ("user typing quit", Gold, FINISHED);
playing = false; break;
case ROGQUIT: dwait (D_ERROR, __func__, "Strategize failed: gave up");
quitrogue ("gave up", Gold, SAVED); break;
}
}
else {
singlestep = false;
}
}
if (! replaying) {
saveltm (Gold); /* Save new long term memory */
endlesson (); /* End genetic learning */
}
/* Print termination messages */
at (R-1, 0);
clrtoeol ();
refresh ();
/*
* Give the user a brief period of time to see the termination messages from rogue
*/
sleep (2);
/*
* restore the normal (non-ncurses) terminal state
*/
nocrmode ();
noraw ();
echo ();
endwin ();
restore_termattr (NULL);
if (emacs) {
if (*sumline) fprintf (realstdout, " %s", sumline);
}
else if (terse) {
if (*sumline) fprintf (realstdout, "\n%s\n",sumline);
fprintf (realstdout, "\n%s %s est.\n", gamename, termination);
}
else {
putchar ('\n');
printf ("%s%s", "Date Time User Gold Killed by",
" Lvl Hp Str Ac Exp Game Dungeon\n");
if (*sumline) printf ("%s\n",sumline);
printf ("\n%s %s est.\n", gamename, termination);
}
/*
* Rename log file, if it is open
*/
if (logging) {
char lognam[MU_BUF + 1]; /* log filename, +1 for paranoia */
/* zeroize arrays */
memset (lognam, 0, sizeof(lognam)); /* paranoia */
/* Make up a new log file name */
snprintf (lognam, MU_BUF, "%.4s.%d.%d", ourkiller, MaxLevel, ourscore);
/* Close the open file */
toggleecho ();
/* Rename the log file */
if (link (ROGUELOG, lognam) == 0) {
unlink (ROGUELOG);
printf ("Log file left on %s\n", lognam);
}
else
printf ("Log file left on %s\n", ROGUELOG);
}
close_frogue_debuglog ();
debuglog_close ();
close_errlog();
exit (0);
}
/*
* onintr: The SIGINT handler. Pass interrupts to main loop, setting
* transparent mode. Also send some synchronization characters to Rogue,
* and reset some goal variables.
*/
static void
onintr (int sig)
{
sendnow ("n\033"); /* Tell Rogue we don't want to quit */
refresh (); /* Clear terminal output */
clearsendqueue (); /* Clear command queue */
setnewgoal (); /* Don't believe ex */
transparent = true; /* Drop into transprent mode */
interrupted = true; /* Mark as an interrupt */
noterm = false; /* Allow commands */
longjmp (commandtop,0); /* Back to command Process */
}
/*
* startlesson: Genetic learning algorithm, pick a genotype to
* test this game, and set the parameters (or "knobs") accordingly.
*/
static void
startlesson (void)
{
unsigned int tmpseed = 0;
int lock_fd;
snprintf (genelog, sizeof(genelog)-1, "%s/GeneLog%d", rgmdir, version);
snprintf (genepool, sizeof(genepool)-1, "%s/GenePool%d", rgmdir, version);
snprintf (genelock, sizeof(genelock)-1, "%s/GeneLock%d", rgmdir, version);
/* set up random number generation */