]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/main.c
1 /* $NetBSD: main.c,v 1.16 2001/02/05 00:57:33 christos Exp $ */
6 __RCSID("$NetBSD: main.c,v 1.16 2001/02/05 00:57:33 christos Exp $");
18 static char copyright
[] = "\nLarn is copyrighted 1986 by Noah Morgan.\n";
19 int srcount
= 0; /* line counter for showstr() */
20 int dropflag
= 0; /* if 1 then don't lookforobject() next round */
21 int rmst
= 80; /* random monster creation counter */
22 int userid
; /* the players login user id number */
23 uid_t uid
, euid
; /* used for security */
24 u_char nowelcome
= 0, nomove
= 0; /* if (nomove) then don't
25 * count next iteration as a
27 static char viewflag
= 0;
29 * if viewflag then we have done a 99 stay here and don't showcell in the
32 u_char restorflag
= 0; /* 1 means restore has been done */
33 static char cmdhelp
[] = "\
34 Cmd line format: larn [-slicnh] [-o<optsfile>] [-##] [++]\n\
35 -s show the scoreboard\n\
36 -l show the logfile (wizard id only)\n\
37 -i show scoreboard with inventories of dead characters\n\
38 -c create new scoreboard (wizard id only)\n\
39 -n suppress welcome message on starting game\n\
40 -## specify level of difficulty (example: -5)\n\
41 -h print this help text\n\
42 ++ restore game from checkpoint file\n\
43 -o<optsfile> specify .larnopts filename to be used instead of \"~/.larnopts\"\n\
46 static char *termtypes
[] = {"vt100", "vt101", "vt102", "vt103", "vt125",
47 "vt131", "vt140", "vt180", "vt220", "vt240", "vt241", "vt320", "vt340",
68 seteuid(uid
); /* give up "games" if we have it */
70 * first task is to identify the player
73 init_term(); /* setup the terminal (find out what type)
76 /* try to get login name */
77 if (((ptr
= getlogin()) == 0) || (*ptr
== 0)) {
78 /* can we get it from /etc/passwd? */
79 if ((pwe
= getpwuid(getuid())) != NULL
)
81 else if ((ptr
= getenv("USER")) == 0)
82 if ((ptr
= getenv("LOGNAME")) == 0) {
83 noone
: write(2, "Can't find your logname. Who Are You?\n", 39);
92 * second task is to prepare the pathnames the player will need
94 strcpy(loginname
, ptr
); /* save loginname of the user for logging
96 strcpy(logname
, ptr
); /* this will be overwritten with the players
98 if ((ptr
= getenv("HOME")) == NULL
)
100 strcpy(savefilename
, ptr
);
101 strcat(savefilename
, "/Larn.sav"); /* save file name in home
103 snprintf(optsfile
, sizeof(optsfile
), "%s/.larnopts", ptr
);
104 /* the .larnopts filename */
107 * now malloc the memory for the dungeon
109 cell
= (struct cel
*) malloc(sizeof(struct cel
) * (MAXLEVEL
+ MAXVLEVEL
) * MAXX
* MAXY
);
111 died(-285); /* malloc failure */
112 lpbuf
= malloc((5 * BUFBIG
) >> 2); /* output buffer */
113 inbuffer
= malloc((5 * MAXIBUF
) >> 2); /* output buffer */
114 if ((lpbuf
== 0) || (inbuffer
== 0))
115 died(-285); /* malloc() failure */
118 newgame(); /* set the initial clock */
123 * check terminal type to avoid users who have not vt100 type terminals
125 ttype
= getenv("TERM");
126 for (j
= 1, i
= 0; i
< sizeof(termtypes
) / sizeof(char *); i
++)
127 if (strcmp(ttype
, termtypes
[i
]) == 0) {
132 lprcat("Sorry, Larn needs a VT100 family terminal for all it's features.\n");
139 * now make scoreboard if it is not there (don't clear)
141 if (access(scorefile
, 0) == -1) /* not there */
145 * now process the command line arguments
147 for (i
= 1; i
< argc
; i
++) {
148 if (argv
[i
][0] == '-')
149 switch (argv
[i
][1]) {
152 exit(0); /* show scoreboard */
154 case 'l': /* show log file */
160 exit(0); /* show all scoreboard */
162 case 'c': /* anyone with password can create
164 lprcat("Preparing to initialize the scoreboard.\n");
165 if (getpassword() != 0) { /* make new scoreboard */
172 case 'n': /* no welcome msg */
186 case '9': /* for hardness */
187 sscanf(&argv
[i
][1], "%d", &hard
);
190 case 'h': /* print out command line arguments */
191 write(1, cmdhelp
, sizeof(cmdhelp
));
194 case 'o': /* specify a .larnopts filename */
195 strncpy(optsfile
, argv
[i
] + 2, 127);
199 printf("Unknown option <%s>\n", argv
[i
]);
203 if (argv
[i
][0] == '+') {
206 if (argv
[i
][1] == '+') {
208 restoregame(ckpfile
); /* restore checkpointed
215 readopts(); /* read the options file if there is one */
219 userid
= geteuid(); /* obtain the user's effective id number */
221 userid
= getplid(logname
); /* obtain the players id number */
222 #endif /* UIDSCORE */
224 write(2, "Can't obtain playerid\n", 22);
229 * this section of code causes the program to look like something else to ps
231 if (strcmp(psname
, argv
[0])) { /* if a different process name only */
232 if ((i
= access(psname
, 1)) < 0) { /* link not there */
233 if (link(argv
[0], psname
) >= 0) {
240 for (i
= 1; i
< argc
; i
++) {
241 szero(argv
[i
]); /* zero the argument to avoid ps snooping */
243 #endif /* HIDEBYLINK */
245 if (access(savefilename
, 0) == 0) { /* restore game if need to */
249 restoregame(savefilename
); /* restore last game */
251 sigsetup(); /* trap all needed signals */
252 sethard(hard
); /* set up the desired difficulty */
253 setupvt100(); /* setup the terminal special mode */
254 if (c
[HP
] == 0) { /* create new game */
255 makeplayer(); /* make the character that will play */
256 newcavelevel(0);/* make the dungeon */
257 predostuff
= 1; /* tell signals that we are in the welcome
260 welcome(); /* welcome the player to the game */
262 drawscreen(); /* show the initial dungeon */
263 predostuff
= 2; /* tell the trap functions that they must do
264 * a showplayer() from here on */
266 nice(1); /* games should be run niced */
268 yrepcount
= hit2flag
= 0;
271 lookforobject(); /* see if there is an object
274 dropflag
= 0; /* don't show it just dropped an item */
279 } /* move the monsters */
281 showcell(playerx
, playery
);
283 viewflag
= 0; /* show stuff around player */
286 hitflag
= hit3flag
= 0;
288 bot_linex(); /* update bottom line */
294 } /* get commands and make moves */
295 regen(); /* regenerate hp and spells */
296 if (c
[TIMESTOP
] == 0)
298 rmst
= 120 - (level
<< 2);
299 fillmonst(makemonst(level
));
308 show character's inventory
314 for (number
= 3, i
= 0; i
< 26; i
++)
316 number
++; /* count items in inventory */
328 nosignal
= 1; /* don't allow ^c etc */
330 lprintf(".) %d gold pieces", (long) c
[GOLD
]);
333 for (k
= 26; k
>= 0; k
--)
335 for (i
= 22; i
< 84; i
++)
336 for (j
= 0; j
<= k
; j
++)
341 lprintf("\nElapsed time is %d. You have %d mobuls left", (long) ((gltime
+ 99) / 100 + 1), (long) ((TIMELIMIT
- gltime
) / 100));
347 * subroutine to clear screen depending on # lines to display
353 if (count
< 20) { /* how do we clear the screen? */
363 * subroutine to restore normal display screen depending on t_setup()
369 if (count
< 18) /* how did we clear the screen? */
370 draws(0, MAXX
, 0, (count
> MAXY
) ? MAXY
: count
);
378 function to show the things player is wearing only
383 int i
, j
, sigsav
, count
;
385 nosignal
= 1; /* don't allow ^c etc */
388 for (count
= 2, j
= 0; j
<= 26; j
++) /* count number of items we
390 if ((i
= iven
[j
]) != 0)
406 for (i
= 22; i
< 84; i
++)
407 for (j
= 0; j
<= 26; j
++)
427 function to show the things player can wield only
432 int i
, j
, sigsav
, count
;
434 nosignal
= 1; /* don't allow ^c etc */
437 for (count
= 2, j
= 0; j
<= 26; j
++) /* count how many items */
438 if ((i
= iven
[j
]) != 0)
459 for (i
= 22; i
< 84; i
++)
460 for (j
= 0; j
<= 26; j
++)
485 * function to show the things player can read only
490 int i
, j
, sigsav
, count
;
492 nosignal
= 1; /* don't allow ^c etc */
495 for (count
= 2, j
= 0; j
<= 26; j
++)
503 for (i
= 22; i
< 84; i
++)
504 for (j
= 0; j
<= 26; j
++)
517 * function to show the things player can eat only
522 int i
, j
, sigsav
, count
;
524 nosignal
= 1; /* don't allow ^c etc */
527 for (count
= 2, j
= 0; j
<= 26; j
++)
534 for (i
= 22; i
< 84; i
++)
535 for (j
= 0; j
<= 26; j
++)
547 function to show the things player can quaff only
552 int i
, j
, sigsav
, count
;
554 nosignal
= 1; /* don't allow ^c etc */
557 for (count
= 2, j
= 0; j
<= 26; j
++)
564 for (i
= 22; i
< 84; i
++)
565 for (j
= 0; j
<= 26; j
++)
581 lprintf("\n%c) %s", idx
+ 'a', objectname
[iven
[idx
]]);
582 if (str2
!= 0 && str2
[ivenarg
[idx
]][0] != 0)
583 lprintf(" of%s", str2
[ivenarg
[idx
]]);
590 switch (iven
[index
]) {
592 show1(index
, potionname
);
595 show1(index
, scrollname
);
609 show1(index
, (char **) 0);
613 lprintf("\n%c) %s", index
+ 'a', objectname
[iven
[index
]]);
614 if (ivenarg
[index
] > 0)
615 lprintf(" + %d", (long) ivenarg
[index
]);
616 else if (ivenarg
[index
] < 0)
617 lprintf(" %d", (long) ivenarg
[index
]);
620 if (c
[WIELD
] == index
)
621 lprcat(" (weapon in hand)");
622 if ((c
[WEAR
] == index
) || (c
[SHIELD
] == index
))
623 lprcat(" (being worn)");
624 if (++srcount
>= 22) {
632 subroutine to randomly create monsters if needed
638 return; /* don't make monsters if time is stopped */
640 rmst
= 120 - (level
<< 2);
641 fillmonst(makemonst(level
));
650 get and execute a command
658 switch (k
) { /* get the token from the input and switch on
686 return; /* northeast */
689 return; /* northeast */
692 return; /* northwest */
695 return; /* northwest */
698 return; /* southeast */
701 return; /* southeast */
704 return; /* southwest */
707 return; /* southwest */
712 return; /* stay here */
717 return; /* wield a weapon */
722 return; /* wear armor */
728 lprcat("\nYou can't read anything when you're blind!");
729 } else if (c
[TIMESTOP
] == 0)
731 return; /* to read a scroll */
735 if (c
[TIMESTOP
] == 0)
737 return; /* quaff a potion */
741 if (c
[TIMESTOP
] == 0)
743 return; /* to drop an object */
748 return; /* cast a spell */
758 if (c
[TIMESTOP
] == 0)
760 return; /* to eat a fortune cookie */
766 return; /* list spells and scrolls */
772 return; /* give the help screen */
776 lprcat("Saving . . .");
778 savegame(savefilename
);
780 died(-257); /* save the game - doesn't return */
789 lprcat("\nAs yet, you don't have enough experience to use teleportation");
790 return; /* teleport yourself */
792 case '^': /* identify traps */
793 flag
= yrepcount
= 0;
796 for (j
= playery
- 1; j
< playery
+ 2; j
++) {
801 for (i
= playerx
- 1; i
< playerx
+ 2; i
++) {
806 switch (item
[i
][j
]) {
812 lprcat(objectname
[item
[i
][j
]]);
818 lprcat("\nNo traps are visible");
822 case '_': /* this is the fudge player password for
827 if (userid
!= wisid
) {
828 lprcat("Sorry, you are not empowered to be a wizard.\n");
829 scbr(); /* system("stty -echo cbreak"); */
833 if (getpassword() == 0) {
834 scbr(); /* system("stty -echo cbreak"); */
838 scbr(); /* system("stty -echo cbreak"); */
839 for (i
= 0; i
< 6; i
++)
841 iven
[0] = iven
[1] = 0;
846 c
[WEAR
] = c
[SHIELD
] = -1;
847 raiseexperience(6000000L);
848 c
[AWARENESS
] += 25000;
851 for (i
= 0; i
< MAXY
; i
++)
852 for (j
= 0; j
< MAXX
; j
++)
854 for (i
= 0; i
< SPNUM
; i
++)
856 for (i
= 0; i
< MAXSCROLL
; i
++)
857 scrollname
[i
] = scrollhide
[i
];
858 for (i
= 0; i
< MAXPOTION
; i
++)
859 potionname
[i
] = potionhide
[i
];
861 for (i
= 0; i
< MAXSCROLL
; i
++)
862 if (strlen(scrollname
[i
]) > 2) { /* no null items */
863 item
[i
][0] = OSCROLL
;
866 for (i
= MAXX
- 1; i
> MAXX
- 1 - MAXPOTION
; i
--)
867 if (strlen(potionname
[i
- MAXX
+ MAXPOTION
]) > 2) { /* no null items */
868 item
[i
][0] = OPOTION
;
869 iarg
[i
][0] = i
- MAXX
+ MAXPOTION
;
871 for (i
= 1; i
< MAXY
; i
++) {
875 for (i
= MAXY
; i
< MAXY
+ MAXX
; i
++) {
876 item
[i
- MAXY
][MAXY
- 1] = i
;
877 iarg
[i
- MAXY
][MAXY
- 1] = 0;
879 for (i
= MAXX
+ MAXY
; i
< MAXX
+ MAXY
+ MAXY
; i
++) {
880 item
[MAXX
- 1][i
- MAXX
- MAXY
] = i
;
881 iarg
[MAXX
- 1][i
- MAXX
- MAXY
] = 0;
891 if (c
[SHIELD
] != -1) {
893 lprcat("\nYour shield is off");
895 } else if (c
[WEAR
] != -1) {
897 lprcat("\nYour armor is off");
900 lprcat("\nYou aren't wearing anything");
905 lprintf("\nThe stuff you are carrying presently weighs %d pounds", (long) packweight());
914 lprintf("\nCaverns of Larn, Version %d.%d, Diff=%d", (long) VERSION
, (long) SUBVERSION
, (long) c
[HARDGAME
]);
943 } /* create diagnostic file */
949 if (outstanding_taxes
> 0)
950 lprintf("\nYou presently owe %d gp in taxes.", (long) outstanding_taxes
);
952 lprcat("\nYou do not owe any taxes.");
963 movemonst(); /* move the monsters */
986 showcell(playerx
, playery
);
991 function to wield a weapon
998 if ((i
= whatitem("wield")) == '\33')
1003 else if (iven
[i
- 'a'] == 0) {
1006 } else if (iven
[i
- 'a'] == OPOTION
) {
1009 } else if (iven
[i
- 'a'] == OSCROLL
) {
1012 } else if ((c
[SHIELD
] != -1) && (iven
[i
- 'a'] == O2SWORD
)) {
1013 lprcat("\nBut one arm is busy with your shield!");
1017 if (iven
[i
- 'a'] == OLANCE
)
1029 common routine to say you don't have an item
1036 lprintf("\nYou don't have item %c!", x
);
1043 lprintf("\nYou can't wield item %c!", x
);
1047 function to wear armor
1054 if ((i
= whatitem("wear")) == '\33')
1060 switch (iven
[i
- 'a']) {
1072 if (c
[WEAR
] != -1) {
1073 lprcat("\nYou're already wearing some armor");
1080 if (c
[SHIELD
] != -1) {
1081 lprcat("\nYou are already wearing a shield");
1084 if (iven
[c
[WIELD
]] == O2SWORD
) {
1085 lprcat("\nYour hands are busy with the two handed sword!");
1088 c
[SHIELD
] = i
- 'a';
1092 lprcat("\nYou can't wear that!");
1099 function to drop an object
1107 p
= &item
[playerx
][playery
];
1109 if ((i
= whatitem("drop")) == '\33')
1114 if (i
== '.') { /* drop some gold */
1116 lprcat("\nThere's something here already!");
1121 lprcat("How much gold do you drop? ");
1122 if ((amt
= readnum((long) c
[GOLD
])) == 0)
1124 if (amt
> c
[GOLD
]) {
1125 lprcat("\nYou don't have that much!");
1131 } else if (amt
<= 327670L) {
1135 } else if (amt
<= 3276700L) {
1139 } else if (amt
<= 32767000L) {
1149 lprintf("You drop %d gold pieces", (long) amt
);
1150 iarg
[playerx
][playery
] = i
;
1152 know
[playerx
][playery
] = 0;
1156 drop_object(i
- 'a');
1163 * readscr() Subroutine to read a scroll one is carrying
1170 if ((i
= whatitem("read")) == '\33')
1176 if (iven
[i
- 'a'] == OSCROLL
) {
1177 read_scroll(ivenarg
[i
- 'a']);
1181 if (iven
[i
- 'a'] == OBOOK
) {
1182 readbook(ivenarg
[i
- 'a']);
1186 if (iven
[i
- 'a'] == 0) {
1190 lprcat("\nThere's nothing on it to read");
1198 * subroutine to eat a cookie one is carrying
1206 if ((i
= whatitem("eat")) == '\33')
1212 if (iven
[i
- 'a'] == OCOOKIE
) {
1213 lprcat("\nThe cookie was delicious.");
1215 if (!c
[BLINDCOUNT
]) {
1216 if ((p
= fortune()) != NULL
) {
1217 lprcat(" Inside you find a scrap of paper that says:\n");
1223 if (iven
[i
- 'a'] == 0) {
1227 lprcat("\nYou can't eat that!");
1235 * subroutine to quaff a potion one is carrying
1242 if ((i
= whatitem("quaff")) == '\33')
1248 if (iven
[i
- 'a'] == OPOTION
) {
1249 quaffpotion(ivenarg
[i
- 'a']);
1253 if (iven
[i
- 'a'] == 0) {
1257 lprcat("\nYou wouldn't want to quaff that, would you? ");
1265 function to ask what player wants to do
1273 lprintf("\nWhat do you want to %s [* for all] ? ", str
);
1275 while (i
> 'z' || (i
< 'a' && i
!= '*' && i
!= '\33' && i
!= '.'))
1283 subroutine to get a number from the player
1284 and allow * to mean return amt, else return the number entered
1291 unsigned long amt
= 0;
1293 if ((i
= lgetchar()) == '*')
1294 amt
= mx
; /* allow him to say * for all gold */
1302 if ((i
<= '9') && (i
>= '0') && (amt
< 99999999))
1303 amt
= amt
* 10 + i
- '0';
1312 * routine to zero every byte in a string
1321 #endif /* HIDEBYLINK */