]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/main.c
1 /* $NetBSD: main.c,v 1.26 2019/02/03 03:19:25 mrg Exp $ */
6 __RCSID("$NetBSD: main.c,v 1.26 2019/02/03 03:19:25 mrg 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",
78 main(int argc
, char **argv
)
88 setegid(gid
); /* give up "games" if we have it */
90 * first task is to identify the player
93 init_term(); /* setup the terminal (find out what type)
96 /* try to get login name */
97 if (((ptr
= getlogin()) == 0) || (*ptr
== 0)) {
98 /* can we get it from /etc/passwd? */
99 if ((pwe
= getpwuid(getuid())) != NULL
)
101 else if ((ptr
= getenv("USER")) == 0)
102 if ((ptr
= getenv("LOGNAME")) == 0) {
103 noone
: write(2, "Can't find your logname. Who Are You?\n", 39);
109 if (strlen(ptr
) == 0)
112 * second task is to prepare the pathnames the player will need
114 strcpy(loginname
, ptr
); /* save loginname of the user for logging
116 strcpy(logname
, ptr
); /* this will be overwritten with the players
118 if ((ptr
= getenv("HOME")) == NULL
)
120 strcpy(savefilename
, ptr
);
121 strcat(savefilename
, "/Larn.sav"); /* save file name in home
123 snprintf(optsfile
, sizeof(optsfile
), "%s/.larnopts", ptr
);
124 /* the .larnopts filename */
127 * now malloc the memory for the dungeon
129 cell
= (struct cel
*) malloc(sizeof(struct cel
) * (MAXLEVEL
+ MAXVLEVEL
) * MAXX
* MAXY
);
131 died(-285); /* malloc failure */
132 lpbuf
= malloc((5 * BUFBIG
) >> 2); /* output buffer */
133 inbuffer
= malloc((5 * MAXIBUF
) >> 2); /* output buffer */
134 if ((lpbuf
== 0) || (inbuffer
== 0))
135 died(-285); /* malloc() failure */
138 newgame(); /* set the initial clock */
143 * check terminal type to avoid users who have not vt100 type terminals
145 ttype
= getenv("TERM");
146 for (j
= 1, i
= 0; i
< sizeof(termtypes
) / sizeof(char *); i
++)
147 if (strcmp(ttype
, termtypes
[i
]) == 0) {
152 lprcat("Sorry, Larn needs a VT100 family terminal for all its features.\n");
159 * now make scoreboard if it is not there (don't clear)
161 if (access(scorefile
, 0) == -1) /* not there */
165 * now process the command line arguments
167 for (i
= 1; i
< argc
; i
++) {
168 if (argv
[i
][0] == '-')
169 switch (argv
[i
][1]) {
172 exit(0); /* show scoreboard */
174 case 'l': /* show log file */
180 exit(0); /* show all scoreboard */
182 case 'c': /* anyone with password can create
184 lprcat("Preparing to initialize the scoreboard.\n");
185 if (getpassword() != 0) { /* make new scoreboard */
192 case 'n': /* no welcome msg */
206 case '9': /* for hardness */
207 sscanf(&argv
[i
][1], "%d", &hard
);
210 case 'h': /* print out command line arguments */
211 write(1, cmdhelp
, sizeof(cmdhelp
));
214 case 'o': /* specify a .larnopts filename */
215 strncpy(optsfile
, argv
[i
] + 2, 127);
219 printf("Unknown option <%s>\n", argv
[i
]);
223 if (argv
[i
][0] == '+') {
226 if (argv
[i
][1] == '+') {
228 restoregame(ckpfile
); /* restore checkpointed
235 readopts(); /* read the options file if there is one */
239 userid
= geteuid(); /* obtain the user's effective id number */
241 userid
= getplid(logname
); /* obtain the players id number */
242 #endif /* UIDSCORE */
244 write(2, "Can't obtain playerid\n", 22);
249 * this section of code causes the program to look like something else to ps
251 if (strcmp(psname
, argv
[0])) { /* if a different process name only */
252 if ((i
= access(psname
, 1)) < 0) { /* link not there */
253 if (link(argv
[0], psname
) >= 0) {
260 for (i
= 1; i
< argc
; i
++) {
261 szero(argv
[i
]); /* zero the argument to avoid ps snooping */
263 #endif /* HIDEBYLINK */
265 if (access(savefilename
, 0) == 0) { /* restore game if need to */
269 restoregame(savefilename
); /* restore last game */
271 sigsetup(); /* trap all needed signals */
272 sethard(hard
); /* set up the desired difficulty */
273 setupvt100(); /* setup the terminal special mode */
274 if (c
[HP
] == 0) { /* create new game */
275 makeplayer(); /* make the character that will play */
276 newcavelevel(0);/* make the dungeon */
277 predostuff
= 1; /* tell signals that we are in the welcome
280 welcome(); /* welcome the player to the game */
282 drawscreen(); /* show the initial dungeon */
283 predostuff
= 2; /* tell the trap functions that they must do
284 * a showplayer() from here on */
286 nice(1); /* games should be run niced */
288 yrepcount
= hit2flag
= 0;
291 lookforobject(); /* see if there is an object
294 dropflag
= 0; /* don't show it just dropped an item */
299 } /* move the monsters */
301 showcell(playerx
, playery
);
303 viewflag
= 0; /* show stuff around player */
306 hitflag
= hit3flag
= 0;
308 bot_linex(); /* update bottom line */
314 } /* get commands and make moves */
315 regen(); /* regenerate hp and spells */
316 if (c
[TIMESTOP
] == 0)
318 rmst
= 120 - (level
<< 2);
319 fillmonst(makemonst(level
));
328 show character's inventory
334 for (number
= 3, i
= 0; i
< 26; i
++)
336 number
++; /* count items in inventory */
348 nosignal
= 1; /* don't allow ^c etc */
350 lprintf(".) %ld gold pieces", (long) c
[GOLD
]);
353 for (k
= 26; k
>= 0; k
--)
355 for (i
= 22; i
< 84; i
++)
356 for (j
= 0; j
<= k
; j
++)
361 lprintf("\nElapsed time is %ld. You have %ld mobuls left", (long) ((gltime
+ 99) / 100 + 1), (long) ((TIMELIMIT
- gltime
) / 100));
367 * subroutine to clear screen depending on # lines to display
372 if (count
< 20) { /* how do we clear the screen? */
382 * subroutine to restore normal display screen depending on t_setup()
387 if (count
< 18) /* how did we clear the screen? */
388 draws(0, MAXX
, 0, (count
> MAXY
) ? MAXY
: count
);
396 function to show the things player is wearing only
401 int i
, j
, sigsav
, count
;
403 nosignal
= 1; /* don't allow ^c etc */
406 for (count
= 2, j
= 0; j
<= 26; j
++) /* count number of items we
408 if ((i
= iven
[j
]) != 0)
424 for (i
= 22; i
< 84; i
++)
425 for (j
= 0; j
<= 26; j
++)
445 function to show the things player can wield only
450 int i
, j
, sigsav
, count
;
452 nosignal
= 1; /* don't allow ^c etc */
455 for (count
= 2, j
= 0; j
<= 26; j
++) /* count how many items */
456 if ((i
= iven
[j
]) != 0)
477 for (i
= 22; i
< 84; i
++)
478 for (j
= 0; j
<= 26; j
++)
503 * function to show the things player can read only
508 int i
, j
, sigsav
, count
;
510 nosignal
= 1; /* don't allow ^c etc */
513 for (count
= 2, j
= 0; j
<= 26; j
++)
521 for (i
= 22; i
< 84; i
++)
522 for (j
= 0; j
<= 26; j
++)
535 * function to show the things player can eat only
540 int i
, j
, sigsav
, count
;
542 nosignal
= 1; /* don't allow ^c etc */
545 for (count
= 2, j
= 0; j
<= 26; j
++)
552 for (i
= 22; i
< 84; i
++)
553 for (j
= 0; j
<= 26; j
++)
565 function to show the things player can quaff only
570 int i
, j
, sigsav
, count
;
572 nosignal
= 1; /* don't allow ^c etc */
575 for (count
= 2, j
= 0; j
<= 26; j
++)
582 for (i
= 22; i
< 84; i
++)
583 for (j
= 0; j
<= 26; j
++)
595 show1(int idx
, const char *str2
[])
597 lprintf("\n%c) %s", idx
+ 'a', objectname
[iven
[idx
]]);
598 if (str2
!= 0 && str2
[ivenarg
[idx
]][0] != 0)
599 lprintf(" of%s", str2
[ivenarg
[idx
]]);
605 switch (iven
[indx
]) {
607 show1(indx
, potionname
);
610 show1(indx
, scrollname
);
628 lprintf("\n%c) %s", indx
+ 'a', objectname
[iven
[indx
]]);
629 if (ivenarg
[indx
] > 0)
630 lprintf(" + %ld", (long) ivenarg
[indx
]);
631 else if (ivenarg
[indx
] < 0)
632 lprintf(" %ld", (long) ivenarg
[indx
]);
635 if (c
[WIELD
] == indx
)
636 lprcat(" (weapon in hand)");
637 if ((c
[WEAR
] == indx
) || (c
[SHIELD
] == indx
))
638 lprcat(" (being worn)");
639 if (++srcount
>= 22) {
647 subroutine to randomly create monsters if needed
653 return; /* don't make monsters if time is stopped */
655 rmst
= 120 - (level
<< 2);
656 fillmonst(makemonst(level
));
665 get and execute a command
673 switch (k
) { /* get the token from the input and switch on
701 return; /* northeast */
704 return; /* northeast */
707 return; /* northwest */
710 return; /* northwest */
713 return; /* southeast */
716 return; /* southeast */
719 return; /* southwest */
722 return; /* southwest */
727 return; /* stay here */
732 return; /* wield a weapon */
737 return; /* wear armor */
743 lprcat("\nYou can't read anything when you're blind!");
744 } else if (c
[TIMESTOP
] == 0)
746 return; /* to read a scroll */
750 if (c
[TIMESTOP
] == 0)
752 return; /* quaff a potion */
756 if (c
[TIMESTOP
] == 0)
758 return; /* to drop an object */
763 return; /* cast a spell */
773 if (c
[TIMESTOP
] == 0)
775 return; /* to eat a fortune cookie */
781 return; /* list spells and scrolls */
787 return; /* give the help screen */
791 lprcat("Saving . . .");
793 savegame(savefilename
);
795 died(-257); /* save the game - doesn't return */
805 lprcat("\nAs yet, you don't have enough experience to use teleportation");
806 return; /* teleport yourself */
808 case '^': /* identify traps */
809 flag
= yrepcount
= 0;
812 for (j
= playery
- 1; j
< playery
+ 2; j
++) {
817 for (i
= playerx
- 1; i
< playerx
+ 2; i
++) {
822 switch (item
[i
][j
]) {
828 lprcat(objectname
[item
[i
][j
]]);
834 lprcat("\nNo traps are visible");
838 case '_': /* this is the fudge player password for
843 if (userid
!= wisid
) {
844 lprcat("Sorry, you are not empowered to be a wizard.\n");
845 scbr(); /* system("stty -echo cbreak"); */
849 if (getpassword() == 0) {
850 scbr(); /* system("stty -echo cbreak"); */
854 scbr(); /* system("stty -echo cbreak"); */
855 for (i
= 0; i
< 6; i
++)
857 iven
[0] = iven
[1] = 0;
862 c
[WEAR
] = c
[SHIELD
] = -1;
863 raiseexperience(6000000L);
864 c
[AWARENESS
] += 25000;
867 for (i
= 0; i
< MAXY
; i
++)
868 for (j
= 0; j
< MAXX
; j
++)
870 for (i
= 0; i
< SPNUM
; i
++)
872 for (i
= 0; i
< MAXSCROLL
; i
++)
873 scrollname
[i
] = scrollhide
[i
];
874 for (i
= 0; i
< MAXPOTION
; i
++)
875 potionname
[i
] = potionhide
[i
];
877 for (i
= 0; i
< MAXSCROLL
; i
++)
878 if (strlen(scrollname
[i
]) > 2) { /* no null items */
879 item
[i
][0] = OSCROLL
;
882 for (i
= MAXX
- 1; i
> MAXX
- 1 - MAXPOTION
; i
--)
883 if (strlen(potionname
[i
- MAXX
+ MAXPOTION
]) > 2) { /* no null items */
884 item
[i
][0] = OPOTION
;
885 iarg
[i
][0] = i
- MAXX
+ MAXPOTION
;
887 for (i
= 1; i
< MAXY
; i
++) {
891 for (i
= MAXY
; i
< MAXY
+ MAXX
; i
++) {
892 item
[i
- MAXY
][MAXY
- 1] = i
;
893 iarg
[i
- MAXY
][MAXY
- 1] = 0;
895 for (i
= MAXX
+ MAXY
; i
< MAXX
+ MAXY
+ MAXY
; i
++) {
896 item
[MAXX
- 1][i
- MAXX
- MAXY
] = i
;
897 iarg
[MAXX
- 1][i
- MAXX
- MAXY
] = 0;
907 if (c
[SHIELD
] != -1) {
909 lprcat("\nYour shield is off");
911 } else if (c
[WEAR
] != -1) {
913 lprcat("\nYour armor is off");
916 lprcat("\nYou aren't wearing anything");
921 lprintf("\nThe stuff you are carrying presently weighs %ld pounds", (long) packweight());
931 lprintf("\nCaverns of Larn, Version %ld.%ld, Diff=%ld",
932 (long) VERSION
, (long) SUBVERSION
,
962 } /* create diagnostic file */
968 if (outstanding_taxes
> 0)
969 lprintf("\nYou presently owe %ld gp in taxes.",
970 (long) outstanding_taxes
);
972 lprcat("\nYou do not owe any taxes.");
983 movemonst(); /* move the monsters */
1005 showcell(playerx
, playery
);
1010 function to wield a weapon
1017 if ((i
= whatitem("wield")) == '\33')
1022 else if (iven
[i
- 'a'] == 0) {
1025 } else if (iven
[i
- 'a'] == OPOTION
) {
1028 } else if (iven
[i
- 'a'] == OSCROLL
) {
1031 } else if ((c
[SHIELD
] != -1) && (iven
[i
- 'a'] == O2SWORD
)) {
1032 lprcat("\nBut one arm is busy with your shield!");
1036 if (iven
[i
- 'a'] == OLANCE
)
1048 common routine to say you don't have an item
1054 lprintf("\nYou don't have item %c!", x
);
1060 lprintf("\nYou can't wield item %c!", x
);
1064 function to wear armor
1071 if ((i
= whatitem("wear")) == '\33')
1077 switch (iven
[i
- 'a']) {
1089 if (c
[WEAR
] != -1) {
1090 lprcat("\nYou're already wearing some armor");
1097 if (c
[SHIELD
] != -1) {
1098 lprcat("\nYou are already wearing a shield");
1101 if (iven
[c
[WIELD
]] == O2SWORD
) {
1102 lprcat("\nYour hands are busy with the two handed sword!");
1105 c
[SHIELD
] = i
- 'a';
1109 lprcat("\nYou can't wear that!");
1116 function to drop an object
1124 p
= &item
[playerx
][playery
];
1126 if ((i
= whatitem("drop")) == '\33')
1131 if (i
== '.') { /* drop some gold */
1133 lprcat("\nThere's something here already!");
1138 lprcat("How much gold do you drop? ");
1139 if ((amt
= readnum((long) c
[GOLD
])) == 0)
1141 if (amt
> c
[GOLD
]) {
1142 lprcat("\nYou don't have that much!");
1148 } else if (amt
<= 327670L) {
1152 } else if (amt
<= 3276700L) {
1156 } else if (amt
<= 32767000L) {
1166 lprintf("You drop %ld gold pieces", (long)amt
);
1167 iarg
[playerx
][playery
] = i
;
1169 know
[playerx
][playery
] = 0;
1173 drop_object(i
- 'a');
1180 * readscr() Subroutine to read a scroll one is carrying
1187 if ((i
= whatitem("read")) == '\33')
1193 if (iven
[i
- 'a'] == OSCROLL
) {
1194 read_scroll(ivenarg
[i
- 'a']);
1198 if (iven
[i
- 'a'] == OBOOK
) {
1199 readbook(ivenarg
[i
- 'a']);
1203 if (iven
[i
- 'a'] == 0) {
1207 lprcat("\nThere's nothing on it to read");
1215 * subroutine to eat a cookie one is carrying
1224 if ((i
= whatitem("eat")) == '\33')
1230 if (iven
[i
- 'a'] == OCOOKIE
) {
1231 lprcat("\nThe cookie was delicious.");
1233 if (!c
[BLINDCOUNT
]) {
1234 if ((p
= fortune()) != NULL
) {
1235 lprcat(" Inside you find a scrap of paper that says:\n");
1241 if (iven
[i
- 'a'] == 0) {
1245 lprcat("\nYou can't eat that!");
1253 * subroutine to quaff a potion one is carrying
1260 if ((i
= whatitem("quaff")) == '\33')
1266 if (iven
[i
- 'a'] == OPOTION
) {
1267 quaffpotion(ivenarg
[i
- 'a']);
1271 if (iven
[i
- 'a'] == 0) {
1275 lprcat("\nYou wouldn't want to quaff that, would you? ");
1283 function to ask what player wants to do
1286 whatitem(const char *str
)
1290 lprintf("\nWhat do you want to %s [* for all] ? ", str
);
1292 while (i
> 'z' || (i
< 'a' && i
!= '*' && i
!= '\33' && i
!= '.'))
1300 subroutine to get a number from the player
1301 and allow * to mean return amt, else return the number entered
1307 unsigned long amt
= 0;
1309 if ((i
= ttgetch()) == '*')
1310 amt
= mx
; /* allow him to say * for all gold */
1318 if ((i
<= '9') && (i
>= '0') && (amt
< 99999999))
1319 amt
= amt
* 10 + i
- '0';
1328 * routine to zero every byte in a string
1337 #endif /* HIDEBYLINK */