]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/main.c
1 /* $NetBSD: main.c,v 1.14 1998/08/30 09:19:38 veego Exp $ */
6 __RCSID("$NetBSD: main.c,v 1.14 1998/08/30 09:19:38 veego 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",
67 seteuid(uid
); /* give up "games" if we have it */
69 * first task is to identify the player
72 init_term(); /* setup the terminal (find out what type)
75 /* try to get login name */
76 if (((ptr
= getlogin()) == 0) || (*ptr
== 0)) {
77 /* can we get it from /etc/passwd? */
78 if ((pwe
= getpwuid(getuid())) != NULL
)
80 else if ((ptr
= getenv("USER")) == 0)
81 if ((ptr
= getenv("LOGNAME")) == 0) {
82 noone
: write(2, "Can't find your logname. Who Are You?\n", 39);
91 * second task is to prepare the pathnames the player will need
93 strcpy(loginname
, ptr
); /* save loginname of the user for logging
95 strcpy(logname
, ptr
); /* this will be overwritten with the players
97 if ((ptr
= getenv("HOME")) == NULL
)
99 strcpy(savefilename
, ptr
);
100 strcat(savefilename
, "/Larn.sav"); /* save file name in home
102 sprintf(optsfile
, "%s/.larnopts", ptr
); /* the .larnopts filename */
105 * now malloc the memory for the dungeon
107 cell
= (struct cel
*) malloc(sizeof(struct cel
) * (MAXLEVEL
+ MAXVLEVEL
) * MAXX
* MAXY
);
109 died(-285); /* malloc failure */
110 lpbuf
= malloc((5 * BUFBIG
) >> 2); /* output buffer */
111 inbuffer
= malloc((5 * MAXIBUF
) >> 2); /* output buffer */
112 if ((lpbuf
== 0) || (inbuffer
== 0))
113 died(-285); /* malloc() failure */
116 newgame(); /* set the initial clock */
121 * check terminal type to avoid users who have not vt100 type terminals
123 ttype
= getenv("TERM");
124 for (j
= 1, i
= 0; i
< sizeof(termtypes
) / sizeof(char *); i
++)
125 if (strcmp(ttype
, termtypes
[i
]) == 0) {
130 lprcat("Sorry, Larn needs a VT100 family terminal for all it's features.\n");
137 * now make scoreboard if it is not there (don't clear)
139 if (access(scorefile
, 0) == -1) /* not there */
143 * now process the command line arguments
145 for (i
= 1; i
< argc
; i
++) {
146 if (argv
[i
][0] == '-')
147 switch (argv
[i
][1]) {
150 exit(0); /* show scoreboard */
152 case 'l': /* show log file */
158 exit(0); /* show all scoreboard */
160 case 'c': /* anyone with password can create
162 lprcat("Preparing to initialize the scoreboard.\n");
163 if (getpassword() != 0) { /* make new scoreboard */
170 case 'n': /* no welcome msg */
184 case '9': /* for hardness */
185 sscanf(&argv
[i
][1], "%d", &hard
);
188 case 'h': /* print out command line arguments */
189 write(1, cmdhelp
, sizeof(cmdhelp
));
192 case 'o': /* specify a .larnopts filename */
193 strncpy(optsfile
, argv
[i
] + 2, 127);
197 printf("Unknown option <%s>\n", argv
[i
]);
201 if (argv
[i
][0] == '+') {
204 if (argv
[i
][1] == '+') {
206 restoregame(ckpfile
); /* restore checkpointed
213 readopts(); /* read the options file if there is one */
217 userid
= geteuid(); /* obtain the user's effective id number */
219 userid
= getplid(logname
); /* obtain the players id number */
220 #endif /* UIDSCORE */
222 write(2, "Can't obtain playerid\n", 22);
227 * this section of code causes the program to look like something else to ps
229 if (strcmp(psname
, argv
[0])) { /* if a different process name only */
230 if ((i
= access(psname
, 1)) < 0) { /* link not there */
231 if (link(argv
[0], psname
) >= 0) {
238 for (i
= 1; i
< argc
; i
++) {
239 szero(argv
[i
]); /* zero the argument to avoid ps snooping */
241 #endif /* HIDEBYLINK */
243 if (access(savefilename
, 0) == 0) { /* restore game if need to */
247 restoregame(savefilename
); /* restore last game */
249 sigsetup(); /* trap all needed signals */
250 sethard(hard
); /* set up the desired difficulty */
251 setupvt100(); /* setup the terminal special mode */
252 if (c
[HP
] == 0) { /* create new game */
253 makeplayer(); /* make the character that will play */
254 newcavelevel(0);/* make the dungeon */
255 predostuff
= 1; /* tell signals that we are in the welcome
258 welcome(); /* welcome the player to the game */
260 drawscreen(); /* show the initial dungeon */
261 predostuff
= 2; /* tell the trap functions that they must do
262 * a showplayer() from here on */
264 nice(1); /* games should be run niced */
266 yrepcount
= hit2flag
= 0;
269 lookforobject(); /* see if there is an object
272 dropflag
= 0; /* don't show it just dropped an item */
277 } /* move the monsters */
279 showcell(playerx
, playery
);
281 viewflag
= 0; /* show stuff around player */
284 hitflag
= hit3flag
= 0;
286 bot_linex(); /* update bottom line */
292 } /* get commands and make moves */
293 regen(); /* regenerate hp and spells */
294 if (c
[TIMESTOP
] == 0)
296 rmst
= 120 - (level
<< 2);
297 fillmonst(makemonst(level
));
306 show character's inventory
312 for (number
= 3, i
= 0; i
< 26; i
++)
314 number
++; /* count items in inventory */
326 nosignal
= 1; /* don't allow ^c etc */
328 lprintf(".) %d gold pieces", (long) c
[GOLD
]);
331 for (k
= 26; k
>= 0; k
--)
333 for (i
= 22; i
< 84; i
++)
334 for (j
= 0; j
<= k
; j
++)
339 lprintf("\nElapsed time is %d. You have %d mobuls left", (long) ((gltime
+ 99) / 100 + 1), (long) ((TIMELIMIT
- gltime
) / 100));
345 * subroutine to clear screen depending on # lines to display
351 if (count
< 20) { /* how do we clear the screen? */
361 * subroutine to restore normal display screen depending on t_setup()
367 if (count
< 18) /* how did we clear the screen? */
368 draws(0, MAXX
, 0, (count
> MAXY
) ? MAXY
: count
);
376 function to show the things player is wearing only
381 int i
, j
, sigsav
, count
;
383 nosignal
= 1; /* don't allow ^c etc */
386 for (count
= 2, j
= 0; j
<= 26; j
++) /* count number of items we
388 if ((i
= iven
[j
]) != 0)
404 for (i
= 22; i
< 84; i
++)
405 for (j
= 0; j
<= 26; j
++)
425 function to show the things player can wield only
430 int i
, j
, sigsav
, count
;
432 nosignal
= 1; /* don't allow ^c etc */
435 for (count
= 2, j
= 0; j
<= 26; j
++) /* count how many items */
436 if ((i
= iven
[j
]) != 0)
457 for (i
= 22; i
< 84; i
++)
458 for (j
= 0; j
<= 26; j
++)
483 * function to show the things player can read only
488 int i
, j
, sigsav
, count
;
490 nosignal
= 1; /* don't allow ^c etc */
493 for (count
= 2, j
= 0; j
<= 26; j
++)
501 for (i
= 22; i
< 84; i
++)
502 for (j
= 0; j
<= 26; j
++)
515 * function to show the things player can eat only
520 int i
, j
, sigsav
, count
;
522 nosignal
= 1; /* don't allow ^c etc */
525 for (count
= 2, j
= 0; j
<= 26; j
++)
532 for (i
= 22; i
< 84; i
++)
533 for (j
= 0; j
<= 26; j
++)
545 function to show the things player can quaff only
550 int i
, j
, sigsav
, count
;
552 nosignal
= 1; /* don't allow ^c etc */
555 for (count
= 2, j
= 0; j
<= 26; j
++)
562 for (i
= 22; i
< 84; i
++)
563 for (j
= 0; j
<= 26; j
++)
579 lprintf("\n%c) %s", idx
+ 'a', objectname
[iven
[idx
]]);
580 if (str2
!= 0 && str2
[ivenarg
[idx
]][0] != 0)
581 lprintf(" of%s", str2
[ivenarg
[idx
]]);
588 switch (iven
[index
]) {
590 show1(index
, potionname
);
593 show1(index
, scrollname
);
607 show1(index
, (char **) 0);
611 lprintf("\n%c) %s", index
+ 'a', objectname
[iven
[index
]]);
612 if (ivenarg
[index
] > 0)
613 lprintf(" + %d", (long) ivenarg
[index
]);
614 else if (ivenarg
[index
] < 0)
615 lprintf(" %d", (long) ivenarg
[index
]);
618 if (c
[WIELD
] == index
)
619 lprcat(" (weapon in hand)");
620 if ((c
[WEAR
] == index
) || (c
[SHIELD
] == index
))
621 lprcat(" (being worn)");
622 if (++srcount
>= 22) {
630 subroutine to randomly create monsters if needed
636 return; /* don't make monsters if time is stopped */
638 rmst
= 120 - (level
<< 2);
639 fillmonst(makemonst(level
));
648 get and execute a command
656 switch (k
) { /* get the token from the input and switch on
684 return; /* northeast */
687 return; /* northeast */
690 return; /* northwest */
693 return; /* northwest */
696 return; /* southeast */
699 return; /* southeast */
702 return; /* southwest */
705 return; /* southwest */
710 return; /* stay here */
715 return; /* wield a weapon */
720 return; /* wear armor */
726 lprcat("\nYou can't read anything when you're blind!");
727 } else if (c
[TIMESTOP
] == 0)
729 return; /* to read a scroll */
733 if (c
[TIMESTOP
] == 0)
735 return; /* quaff a potion */
739 if (c
[TIMESTOP
] == 0)
741 return; /* to drop an object */
746 return; /* cast a spell */
756 if (c
[TIMESTOP
] == 0)
758 return; /* to eat a fortune cookie */
764 return; /* list spells and scrolls */
770 return; /* give the help screen */
774 lprcat("Saving . . .");
776 savegame(savefilename
);
778 died(-257); /* save the game - doesn't return */
787 lprcat("\nAs yet, you don't have enough experience to use teleportation");
788 return; /* teleport yourself */
790 case '^': /* identify traps */
791 flag
= yrepcount
= 0;
794 for (j
= playery
- 1; j
< playery
+ 2; j
++) {
799 for (i
= playerx
- 1; i
< playerx
+ 2; i
++) {
804 switch (item
[i
][j
]) {
810 lprcat(objectname
[item
[i
][j
]]);
816 lprcat("\nNo traps are visible");
820 case '_': /* this is the fudge player password for
825 if (userid
!= wisid
) {
826 lprcat("Sorry, you are not empowered to be a wizard.\n");
827 scbr(); /* system("stty -echo cbreak"); */
831 if (getpassword() == 0) {
832 scbr(); /* system("stty -echo cbreak"); */
836 scbr(); /* system("stty -echo cbreak"); */
837 for (i
= 0; i
< 6; i
++)
839 iven
[0] = iven
[1] = 0;
844 c
[WEAR
] = c
[SHIELD
] = -1;
845 raiseexperience(6000000L);
846 c
[AWARENESS
] += 25000;
849 for (i
= 0; i
< MAXY
; i
++)
850 for (j
= 0; j
< MAXX
; j
++)
852 for (i
= 0; i
< SPNUM
; i
++)
854 for (i
= 0; i
< MAXSCROLL
; i
++)
855 scrollname
[i
] = scrollhide
[i
];
856 for (i
= 0; i
< MAXPOTION
; i
++)
857 potionname
[i
] = potionhide
[i
];
859 for (i
= 0; i
< MAXSCROLL
; i
++)
860 if (strlen(scrollname
[i
]) > 2) { /* no null items */
861 item
[i
][0] = OSCROLL
;
864 for (i
= MAXX
- 1; i
> MAXX
- 1 - MAXPOTION
; i
--)
865 if (strlen(potionname
[i
- MAXX
+ MAXPOTION
]) > 2) { /* no null items */
866 item
[i
][0] = OPOTION
;
867 iarg
[i
][0] = i
- MAXX
+ MAXPOTION
;
869 for (i
= 1; i
< MAXY
; i
++) {
873 for (i
= MAXY
; i
< MAXY
+ MAXX
; i
++) {
874 item
[i
- MAXY
][MAXY
- 1] = i
;
875 iarg
[i
- MAXY
][MAXY
- 1] = 0;
877 for (i
= MAXX
+ MAXY
; i
< MAXX
+ MAXY
+ MAXY
; i
++) {
878 item
[MAXX
- 1][i
- MAXX
- MAXY
] = i
;
879 iarg
[MAXX
- 1][i
- MAXX
- MAXY
] = 0;
889 if (c
[SHIELD
] != -1) {
891 lprcat("\nYour shield is off");
893 } else if (c
[WEAR
] != -1) {
895 lprcat("\nYour armor is off");
898 lprcat("\nYou aren't wearing anything");
903 lprintf("\nThe stuff you are carrying presently weighs %d pounds", (long) packweight());
912 lprintf("\nCaverns of Larn, Version %d.%d, Diff=%d", (long) VERSION
, (long) SUBVERSION
, (long) c
[HARDGAME
]);
941 } /* create diagnostic file */
947 if (outstanding_taxes
> 0)
948 lprintf("\nYou presently owe %d gp in taxes.", (long) outstanding_taxes
);
950 lprcat("\nYou do not owe any taxes.");
961 movemonst(); /* move the monsters */
984 showcell(playerx
, playery
);
989 function to wield a weapon
996 if ((i
= whatitem("wield")) == '\33')
1001 else if (iven
[i
- 'a'] == 0) {
1004 } else if (iven
[i
- 'a'] == OPOTION
) {
1007 } else if (iven
[i
- 'a'] == OSCROLL
) {
1010 } else if ((c
[SHIELD
] != -1) && (iven
[i
- 'a'] == O2SWORD
)) {
1011 lprcat("\nBut one arm is busy with your shield!");
1015 if (iven
[i
- 'a'] == OLANCE
)
1027 common routine to say you don't have an item
1034 lprintf("\nYou don't have item %c!", x
);
1041 lprintf("\nYou can't wield item %c!", x
);
1045 function to wear armor
1052 if ((i
= whatitem("wear")) == '\33')
1058 switch (iven
[i
- 'a']) {
1070 if (c
[WEAR
] != -1) {
1071 lprcat("\nYou're already wearing some armor");
1078 if (c
[SHIELD
] != -1) {
1079 lprcat("\nYou are already wearing a shield");
1082 if (iven
[c
[WIELD
]] == O2SWORD
) {
1083 lprcat("\nYour hands are busy with the two handed sword!");
1086 c
[SHIELD
] = i
- 'a';
1090 lprcat("\nYou can't wear that!");
1097 function to drop an object
1105 p
= &item
[playerx
][playery
];
1107 if ((i
= whatitem("drop")) == '\33')
1112 if (i
== '.') { /* drop some gold */
1114 lprcat("\nThere's something here already!");
1119 lprcat("How much gold do you drop? ");
1120 if ((amt
= readnum((long) c
[GOLD
])) == 0)
1122 if (amt
> c
[GOLD
]) {
1123 lprcat("\nYou don't have that much!");
1129 } else if (amt
<= 327670L) {
1133 } else if (amt
<= 3276700L) {
1137 } else if (amt
<= 32767000L) {
1147 lprintf("You drop %d gold pieces", (long) amt
);
1148 iarg
[playerx
][playery
] = i
;
1150 know
[playerx
][playery
] = 0;
1154 drop_object(i
- 'a');
1161 * readscr() Subroutine to read a scroll one is carrying
1168 if ((i
= whatitem("read")) == '\33')
1174 if (iven
[i
- 'a'] == OSCROLL
) {
1175 read_scroll(ivenarg
[i
- 'a']);
1179 if (iven
[i
- 'a'] == OBOOK
) {
1180 readbook(ivenarg
[i
- 'a']);
1184 if (iven
[i
- 'a'] == 0) {
1188 lprcat("\nThere's nothing on it to read");
1196 * subroutine to eat a cookie one is carrying
1204 if ((i
= whatitem("eat")) == '\33')
1210 if (iven
[i
- 'a'] == OCOOKIE
) {
1211 lprcat("\nThe cookie was delicious.");
1213 if (!c
[BLINDCOUNT
]) {
1214 if ((p
= fortune()) != NULL
) {
1215 lprcat(" Inside you find a scrap of paper that says:\n");
1221 if (iven
[i
- 'a'] == 0) {
1225 lprcat("\nYou can't eat that!");
1233 * subroutine to quaff a potion one is carrying
1240 if ((i
= whatitem("quaff")) == '\33')
1246 if (iven
[i
- 'a'] == OPOTION
) {
1247 quaffpotion(ivenarg
[i
- 'a']);
1251 if (iven
[i
- 'a'] == 0) {
1255 lprcat("\nYou wouldn't want to quaff that, would you? ");
1263 function to ask what player wants to do
1271 lprintf("\nWhat do you want to %s [* for all] ? ", str
);
1273 while (i
> 'z' || (i
< 'a' && i
!= '*' && i
!= '\33' && i
!= '.'))
1281 subroutine to get a number from the player
1282 and allow * to mean return amt, else return the number entered
1289 unsigned long amt
= 0;
1291 if ((i
= getchar()) == '*')
1292 amt
= mx
; /* allow him to say * for all gold */
1300 if ((i
<= '9') && (i
>= '0') && (amt
< 99999999))
1301 amt
= amt
* 10 + i
- '0';
1310 * routine to zero every byte in a string
1319 #endif /* HIDEBYLINK */