1 /* $NetBSD: main.c,v 1.23 2008/02/03 21:24:58 dholland Exp $ */
6 __RCSID("$NetBSD: main.c,v 1.23 2008/02/03 21:24:58 dholland Exp $");
18 static void showstr(void);
19 static void t_setup(int);
20 static void t_endup(int);
21 static void showwear(void);
22 static void showwield(void);
23 static void showread(void);
24 static void showeat(void);
25 static void showquaff(void);
26 static void show1(int, const char *[]);
27 static void randmonst(void);
28 static void parse(void);
30 static void wield(void);
31 static void ydhi(int);
32 static void ycwi(int);
33 static void wear(void);
34 static void dropobj(void);
35 static void readscr(void);
36 static void eatcookie(void);
37 static void quaff(void);
38 static int whatitem(const char *);
40 static char copyright
[] = "\nLarn is copyrighted 1986 by Noah Morgan.\n";
41 int srcount
= 0; /* line counter for showstr() */
42 int dropflag
= 0; /* if 1 then don't lookforobject() next round */
43 int rmst
= 80; /* random monster creation counter */
44 int userid
; /* the players login user id number */
45 gid_t gid
, egid
; /* used for security */
46 u_char nowelcome
= 0, nomove
= 0; /* if (nomove) then don't
47 * count next iteration as a
49 static char viewflag
= 0;
51 * if viewflag then we have done a 99 stay here and don't showcell in the
54 u_char restorflag
= 0; /* 1 means restore has been done */
55 static char cmdhelp
[] = "\
56 Cmd line format: larn [-slicnh] [-o<optsfile>] [-##] [++]\n\
57 -s show the scoreboard\n\
58 -l show the logfile (wizard id only)\n\
59 -i show scoreboard with inventories of dead characters\n\
60 -c create new scoreboard (wizard id only)\n\
61 -n suppress welcome message on starting game\n\
62 -## specify level of difficulty (example: -5)\n\
63 -h print this help text\n\
64 ++ restore game from checkpoint file\n\
65 -o<optsfile> specify .larnopts filename to be used instead of \"~/.larnopts\"\n\
68 static char *termtypes
[] = {"vt100", "vt101", "vt102", "vt103", "vt125",
69 "vt131", "vt140", "vt180", "vt220", "vt240", "vt241", "vt320", "vt340",
90 setegid(gid
); /* give up "games" if we have it */
92 * first task is to identify the player
95 init_term(); /* setup the terminal (find out what type)
98 /* try to get login name */
99 if (((ptr
= getlogin()) == 0) || (*ptr
== 0)) {
100 /* can we get it from /etc/passwd? */
101 if ((pwe
= getpwuid(getuid())) != NULL
)
103 else if ((ptr
= getenv("USER")) == 0)
104 if ((ptr
= getenv("LOGNAME")) == 0) {
105 noone
: write(2, "Can't find your logname. Who Are You?\n", 39);
111 if (strlen(ptr
) == 0)
114 * second task is to prepare the pathnames the player will need
116 strcpy(loginname
, ptr
); /* save loginname of the user for logging
118 strcpy(logname
, ptr
); /* this will be overwritten with the players
120 if ((ptr
= getenv("HOME")) == NULL
)
122 strcpy(savefilename
, ptr
);
123 strcat(savefilename
, "/Larn.sav"); /* save file name in home
125 snprintf(optsfile
, sizeof(optsfile
), "%s/.larnopts", ptr
);
126 /* the .larnopts filename */
129 * now malloc the memory for the dungeon
131 cell
= (struct cel
*) malloc(sizeof(struct cel
) * (MAXLEVEL
+ MAXVLEVEL
) * MAXX
* MAXY
);
133 died(-285); /* malloc failure */
134 lpbuf
= malloc((5 * BUFBIG
) >> 2); /* output buffer */
135 inbuffer
= malloc((5 * MAXIBUF
) >> 2); /* output buffer */
136 if ((lpbuf
== 0) || (inbuffer
== 0))
137 died(-285); /* malloc() failure */
140 newgame(); /* set the initial clock */
145 * check terminal type to avoid users who have not vt100 type terminals
147 ttype
= getenv("TERM");
148 for (j
= 1, i
= 0; i
< sizeof(termtypes
) / sizeof(char *); i
++)
149 if (strcmp(ttype
, termtypes
[i
]) == 0) {
154 lprcat("Sorry, Larn needs a VT100 family terminal for all its features.\n");
161 * now make scoreboard if it is not there (don't clear)
163 if (access(scorefile
, 0) == -1) /* not there */
167 * now process the command line arguments
169 for (i
= 1; i
< argc
; i
++) {
170 if (argv
[i
][0] == '-')
171 switch (argv
[i
][1]) {
174 exit(0); /* show scoreboard */
176 case 'l': /* show log file */
182 exit(0); /* show all scoreboard */
184 case 'c': /* anyone with password can create
186 lprcat("Preparing to initialize the scoreboard.\n");
187 if (getpassword() != 0) { /* make new scoreboard */
194 case 'n': /* no welcome msg */
208 case '9': /* for hardness */
209 sscanf(&argv
[i
][1], "%d", &hard
);
212 case 'h': /* print out command line arguments */
213 write(1, cmdhelp
, sizeof(cmdhelp
));
216 case 'o': /* specify a .larnopts filename */
217 strncpy(optsfile
, argv
[i
] + 2, 127);
221 printf("Unknown option <%s>\n", argv
[i
]);
225 if (argv
[i
][0] == '+') {
228 if (argv
[i
][1] == '+') {
230 restoregame(ckpfile
); /* restore checkpointed
237 readopts(); /* read the options file if there is one */
241 userid
= geteuid(); /* obtain the user's effective id number */
243 userid
= getplid(logname
); /* obtain the players id number */
244 #endif /* UIDSCORE */
246 write(2, "Can't obtain playerid\n", 22);
251 * this section of code causes the program to look like something else to ps
253 if (strcmp(psname
, argv
[0])) { /* if a different process name only */
254 if ((i
= access(psname
, 1)) < 0) { /* link not there */
255 if (link(argv
[0], psname
) >= 0) {
262 for (i
= 1; i
< argc
; i
++) {
263 szero(argv
[i
]); /* zero the argument to avoid ps snooping */
265 #endif /* HIDEBYLINK */
267 if (access(savefilename
, 0) == 0) { /* restore game if need to */
271 restoregame(savefilename
); /* restore last game */
273 sigsetup(); /* trap all needed signals */
274 sethard(hard
); /* set up the desired difficulty */
275 setupvt100(); /* setup the terminal special mode */
276 if (c
[HP
] == 0) { /* create new game */
277 makeplayer(); /* make the character that will play */
278 newcavelevel(0);/* make the dungeon */
279 predostuff
= 1; /* tell signals that we are in the welcome
282 welcome(); /* welcome the player to the game */
284 drawscreen(); /* show the initial dungeon */
285 predostuff
= 2; /* tell the trap functions that they must do
286 * a showplayer() from here on */
288 nice(1); /* games should be run niced */
290 yrepcount
= hit2flag
= 0;
293 lookforobject(); /* see if there is an object
296 dropflag
= 0; /* don't show it just dropped an item */
301 } /* move the monsters */
303 showcell(playerx
, playery
);
305 viewflag
= 0; /* show stuff around player */
308 hitflag
= hit3flag
= 0;
310 bot_linex(); /* update bottom line */
316 } /* get commands and make moves */
317 regen(); /* regenerate hp and spells */
318 if (c
[TIMESTOP
] == 0)
320 rmst
= 120 - (level
<< 2);
321 fillmonst(makemonst(level
));
330 show character's inventory
336 for (number
= 3, i
= 0; i
< 26; i
++)
338 number
++; /* count items in inventory */
350 nosignal
= 1; /* don't allow ^c etc */
352 lprintf(".) %ld gold pieces", (long) c
[GOLD
]);
355 for (k
= 26; k
>= 0; k
--)
357 for (i
= 22; i
< 84; i
++)
358 for (j
= 0; j
<= k
; j
++)
363 lprintf("\nElapsed time is %ld. You have %ld mobuls left", (long) ((gltime
+ 99) / 100 + 1), (long) ((TIMELIMIT
- gltime
) / 100));
369 * subroutine to clear screen depending on # lines to display
375 if (count
< 20) { /* how do we clear the screen? */
385 * subroutine to restore normal display screen depending on t_setup()
391 if (count
< 18) /* how did we clear the screen? */
392 draws(0, MAXX
, 0, (count
> MAXY
) ? MAXY
: count
);
400 function to show the things player is wearing only
405 int i
, j
, sigsav
, count
;
407 nosignal
= 1; /* don't allow ^c etc */
410 for (count
= 2, j
= 0; j
<= 26; j
++) /* count number of items we
412 if ((i
= iven
[j
]) != 0)
428 for (i
= 22; i
< 84; i
++)
429 for (j
= 0; j
<= 26; j
++)
449 function to show the things player can wield only
454 int i
, j
, sigsav
, count
;
456 nosignal
= 1; /* don't allow ^c etc */
459 for (count
= 2, j
= 0; j
<= 26; j
++) /* count how many items */
460 if ((i
= iven
[j
]) != 0)
481 for (i
= 22; i
< 84; i
++)
482 for (j
= 0; j
<= 26; j
++)
507 * function to show the things player can read only
512 int i
, j
, sigsav
, count
;
514 nosignal
= 1; /* don't allow ^c etc */
517 for (count
= 2, j
= 0; j
<= 26; j
++)
525 for (i
= 22; i
< 84; i
++)
526 for (j
= 0; j
<= 26; j
++)
539 * function to show the things player can eat only
544 int i
, j
, sigsav
, count
;
546 nosignal
= 1; /* don't allow ^c etc */
549 for (count
= 2, j
= 0; j
<= 26; j
++)
556 for (i
= 22; i
< 84; i
++)
557 for (j
= 0; j
<= 26; j
++)
569 function to show the things player can quaff only
574 int i
, j
, sigsav
, count
;
576 nosignal
= 1; /* don't allow ^c etc */
579 for (count
= 2, j
= 0; j
<= 26; j
++)
586 for (i
= 22; i
< 84; i
++)
587 for (j
= 0; j
<= 26; j
++)
603 lprintf("\n%c) %s", idx
+ 'a', objectname
[iven
[idx
]]);
604 if (str2
!= 0 && str2
[ivenarg
[idx
]][0] != 0)
605 lprintf(" of%s", str2
[ivenarg
[idx
]]);
611 switch (iven
[indx
]) {
613 show1(indx
, potionname
);
616 show1(indx
, scrollname
);
634 lprintf("\n%c) %s", indx
+ 'a', objectname
[iven
[indx
]]);
635 if (ivenarg
[indx
] > 0)
636 lprintf(" + %ld", (long) ivenarg
[indx
]);
637 else if (ivenarg
[indx
] < 0)
638 lprintf(" %ld", (long) ivenarg
[indx
]);
641 if (c
[WIELD
] == indx
)
642 lprcat(" (weapon in hand)");
643 if ((c
[WEAR
] == indx
) || (c
[SHIELD
] == indx
))
644 lprcat(" (being worn)");
645 if (++srcount
>= 22) {
653 subroutine to randomly create monsters if needed
659 return; /* don't make monsters if time is stopped */
661 rmst
= 120 - (level
<< 2);
662 fillmonst(makemonst(level
));
671 get and execute a command
679 switch (k
) { /* get the token from the input and switch on
707 return; /* northeast */
710 return; /* northeast */
713 return; /* northwest */
716 return; /* northwest */
719 return; /* southeast */
722 return; /* southeast */
725 return; /* southwest */
728 return; /* southwest */
733 return; /* stay here */
738 return; /* wield a weapon */
743 return; /* wear armor */
749 lprcat("\nYou can't read anything when you're blind!");
750 } else if (c
[TIMESTOP
] == 0)
752 return; /* to read a scroll */
756 if (c
[TIMESTOP
] == 0)
758 return; /* quaff a potion */
762 if (c
[TIMESTOP
] == 0)
764 return; /* to drop an object */
769 return; /* cast a spell */
779 if (c
[TIMESTOP
] == 0)
781 return; /* to eat a fortune cookie */
787 return; /* list spells and scrolls */
793 return; /* give the help screen */
797 lprcat("Saving . . .");
799 savegame(savefilename
);
801 died(-257); /* save the game - doesn't return */
810 lprcat("\nAs yet, you don't have enough experience to use teleportation");
811 return; /* teleport yourself */
813 case '^': /* identify traps */
814 flag
= yrepcount
= 0;
817 for (j
= playery
- 1; j
< playery
+ 2; j
++) {
822 for (i
= playerx
- 1; i
< playerx
+ 2; i
++) {
827 switch (item
[i
][j
]) {
833 lprcat(objectname
[item
[i
][j
]]);
839 lprcat("\nNo traps are visible");
843 case '_': /* this is the fudge player password for
848 if (userid
!= wisid
) {
849 lprcat("Sorry, you are not empowered to be a wizard.\n");
850 scbr(); /* system("stty -echo cbreak"); */
854 if (getpassword() == 0) {
855 scbr(); /* system("stty -echo cbreak"); */
859 scbr(); /* system("stty -echo cbreak"); */
860 for (i
= 0; i
< 6; i
++)
862 iven
[0] = iven
[1] = 0;
867 c
[WEAR
] = c
[SHIELD
] = -1;
868 raiseexperience(6000000L);
869 c
[AWARENESS
] += 25000;
872 for (i
= 0; i
< MAXY
; i
++)
873 for (j
= 0; j
< MAXX
; j
++)
875 for (i
= 0; i
< SPNUM
; i
++)
877 for (i
= 0; i
< MAXSCROLL
; i
++)
878 scrollname
[i
] = scrollhide
[i
];
879 for (i
= 0; i
< MAXPOTION
; i
++)
880 potionname
[i
] = potionhide
[i
];
882 for (i
= 0; i
< MAXSCROLL
; i
++)
883 if (strlen(scrollname
[i
]) > 2) { /* no null items */
884 item
[i
][0] = OSCROLL
;
887 for (i
= MAXX
- 1; i
> MAXX
- 1 - MAXPOTION
; i
--)
888 if (strlen(potionname
[i
- MAXX
+ MAXPOTION
]) > 2) { /* no null items */
889 item
[i
][0] = OPOTION
;
890 iarg
[i
][0] = i
- MAXX
+ MAXPOTION
;
892 for (i
= 1; i
< MAXY
; i
++) {
896 for (i
= MAXY
; i
< MAXY
+ MAXX
; i
++) {
897 item
[i
- MAXY
][MAXY
- 1] = i
;
898 iarg
[i
- MAXY
][MAXY
- 1] = 0;
900 for (i
= MAXX
+ MAXY
; i
< MAXX
+ MAXY
+ MAXY
; i
++) {
901 item
[MAXX
- 1][i
- MAXX
- MAXY
] = i
;
902 iarg
[MAXX
- 1][i
- MAXX
- MAXY
] = 0;
912 if (c
[SHIELD
] != -1) {
914 lprcat("\nYour shield is off");
916 } else if (c
[WEAR
] != -1) {
918 lprcat("\nYour armor is off");
921 lprcat("\nYou aren't wearing anything");
926 lprintf("\nThe stuff you are carrying presently weighs %ld pounds", (long) packweight());
935 lprintf("\nCaverns of Larn, Version %ld.%ld, Diff=%ld",
936 (long) VERSION
, (long) SUBVERSION
,
966 } /* create diagnostic file */
972 if (outstanding_taxes
> 0)
973 lprintf("\nYou presently owe %ld gp in taxes.",
974 (long) outstanding_taxes
);
976 lprcat("\nYou do not owe any taxes.");
987 movemonst(); /* move the monsters */
1010 showcell(playerx
, playery
);
1015 function to wield a weapon
1022 if ((i
= whatitem("wield")) == '\33')
1027 else if (iven
[i
- 'a'] == 0) {
1030 } else if (iven
[i
- 'a'] == OPOTION
) {
1033 } else if (iven
[i
- 'a'] == OSCROLL
) {
1036 } else if ((c
[SHIELD
] != -1) && (iven
[i
- 'a'] == O2SWORD
)) {
1037 lprcat("\nBut one arm is busy with your shield!");
1041 if (iven
[i
- 'a'] == OLANCE
)
1053 common routine to say you don't have an item
1060 lprintf("\nYou don't have item %c!", x
);
1067 lprintf("\nYou can't wield item %c!", x
);
1071 function to wear armor
1078 if ((i
= whatitem("wear")) == '\33')
1084 switch (iven
[i
- 'a']) {
1096 if (c
[WEAR
] != -1) {
1097 lprcat("\nYou're already wearing some armor");
1104 if (c
[SHIELD
] != -1) {
1105 lprcat("\nYou are already wearing a shield");
1108 if (iven
[c
[WIELD
]] == O2SWORD
) {
1109 lprcat("\nYour hands are busy with the two handed sword!");
1112 c
[SHIELD
] = i
- 'a';
1116 lprcat("\nYou can't wear that!");
1123 function to drop an object
1131 p
= &item
[playerx
][playery
];
1133 if ((i
= whatitem("drop")) == '\33')
1138 if (i
== '.') { /* drop some gold */
1140 lprcat("\nThere's something here already!");
1145 lprcat("How much gold do you drop? ");
1146 if ((amt
= readnum((long) c
[GOLD
])) == 0)
1148 if (amt
> c
[GOLD
]) {
1149 lprcat("\nYou don't have that much!");
1155 } else if (amt
<= 327670L) {
1159 } else if (amt
<= 3276700L) {
1163 } else if (amt
<= 32767000L) {
1173 lprintf("You drop %ld gold pieces", (long)amt
);
1174 iarg
[playerx
][playery
] = i
;
1176 know
[playerx
][playery
] = 0;
1180 drop_object(i
- 'a');
1187 * readscr() Subroutine to read a scroll one is carrying
1194 if ((i
= whatitem("read")) == '\33')
1200 if (iven
[i
- 'a'] == OSCROLL
) {
1201 read_scroll(ivenarg
[i
- 'a']);
1205 if (iven
[i
- 'a'] == OBOOK
) {
1206 readbook(ivenarg
[i
- 'a']);
1210 if (iven
[i
- 'a'] == 0) {
1214 lprcat("\nThere's nothing on it to read");
1222 * subroutine to eat a cookie one is carrying
1231 if ((i
= whatitem("eat")) == '\33')
1237 if (iven
[i
- 'a'] == OCOOKIE
) {
1238 lprcat("\nThe cookie was delicious.");
1240 if (!c
[BLINDCOUNT
]) {
1241 if ((p
= fortune()) != NULL
) {
1242 lprcat(" Inside you find a scrap of paper that says:\n");
1248 if (iven
[i
- 'a'] == 0) {
1252 lprcat("\nYou can't eat that!");
1260 * subroutine to quaff a potion one is carrying
1267 if ((i
= whatitem("quaff")) == '\33')
1273 if (iven
[i
- 'a'] == OPOTION
) {
1274 quaffpotion(ivenarg
[i
- 'a']);
1278 if (iven
[i
- 'a'] == 0) {
1282 lprcat("\nYou wouldn't want to quaff that, would you? ");
1290 function to ask what player wants to do
1293 whatitem(const char *str
)
1297 lprintf("\nWhat do you want to %s [* for all] ? ", str
);
1299 while (i
> 'z' || (i
< 'a' && i
!= '*' && i
!= '\33' && i
!= '.'))
1307 subroutine to get a number from the player
1308 and allow * to mean return amt, else return the number entered
1315 unsigned long amt
= 0;
1317 if ((i
= ttgetch()) == '*')
1318 amt
= mx
; /* allow him to say * for all gold */
1326 if ((i
<= '9') && (i
>= '0') && (amt
< 99999999))
1327 amt
= amt
* 10 + i
- '0';
1336 * routine to zero every byte in a string
1345 #endif /* HIDEBYLINK */