]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/main.c
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 int whatitem(const char *);
20 static char copyright
[] = "\nLarn is copyrighted 1986 by Noah Morgan.\n";
21 int srcount
= 0; /* line counter for showstr() */
22 int dropflag
= 0; /* if 1 then don't lookforobject() next round */
23 int rmst
= 80; /* random monster creation counter */
24 int userid
; /* the players login user id number */
25 gid_t gid
, egid
; /* used for security */
26 u_char nowelcome
= 0, nomove
= 0; /* if (nomove) then don't
27 * count next iteration as a
29 static char viewflag
= 0;
31 * if viewflag then we have done a 99 stay here and don't showcell in the
34 u_char restorflag
= 0; /* 1 means restore has been done */
35 static char cmdhelp
[] = "\
36 Cmd line format: larn [-slicnh] [-o<optsfile>] [-##] [++]\n\
37 -s show the scoreboard\n\
38 -l show the logfile (wizard id only)\n\
39 -i show scoreboard with inventories of dead characters\n\
40 -c create new scoreboard (wizard id only)\n\
41 -n suppress welcome message on starting game\n\
42 -## specify level of difficulty (example: -5)\n\
43 -h print this help text\n\
44 ++ restore game from checkpoint file\n\
45 -o<optsfile> specify .larnopts filename to be used instead of \"~/.larnopts\"\n\
48 static char *termtypes
[] = {"vt100", "vt101", "vt102", "vt103", "vt125",
49 "vt131", "vt140", "vt180", "vt220", "vt240", "vt241", "vt320", "vt340",
70 setegid(gid
); /* give up "games" if we have it */
72 * first task is to identify the player
75 init_term(); /* setup the terminal (find out what type)
78 /* try to get login name */
79 if (((ptr
= getlogin()) == 0) || (*ptr
== 0)) {
80 /* can we get it from /etc/passwd? */
81 if ((pwe
= getpwuid(getuid())) != NULL
)
83 else if ((ptr
= getenv("USER")) == 0)
84 if ((ptr
= getenv("LOGNAME")) == 0) {
85 noone
: write(2, "Can't find your logname. Who Are You?\n", 39);
94 * second task is to prepare the pathnames the player will need
96 strcpy(loginname
, ptr
); /* save loginname of the user for logging
98 strcpy(logname
, ptr
); /* this will be overwritten with the players
100 if ((ptr
= getenv("HOME")) == NULL
)
102 strcpy(savefilename
, ptr
);
103 strcat(savefilename
, "/Larn.sav"); /* save file name in home
105 snprintf(optsfile
, sizeof(optsfile
), "%s/.larnopts", ptr
);
106 /* the .larnopts filename */
109 * now malloc the memory for the dungeon
111 cell
= (struct cel
*) malloc(sizeof(struct cel
) * (MAXLEVEL
+ MAXVLEVEL
) * MAXX
* MAXY
);
113 died(-285); /* malloc failure */
114 lpbuf
= malloc((5 * BUFBIG
) >> 2); /* output buffer */
115 inbuffer
= malloc((5 * MAXIBUF
) >> 2); /* output buffer */
116 if ((lpbuf
== 0) || (inbuffer
== 0))
117 died(-285); /* malloc() failure */
120 newgame(); /* set the initial clock */
125 * check terminal type to avoid users who have not vt100 type terminals
127 ttype
= getenv("TERM");
128 for (j
= 1, i
= 0; i
< sizeof(termtypes
) / sizeof(char *); i
++)
129 if (strcmp(ttype
, termtypes
[i
]) == 0) {
134 lprcat("Sorry, Larn needs a VT100 family terminal for all its features.\n");
141 * now make scoreboard if it is not there (don't clear)
143 if (access(scorefile
, 0) == -1) /* not there */
147 * now process the command line arguments
149 for (i
= 1; i
< argc
; i
++) {
150 if (argv
[i
][0] == '-')
151 switch (argv
[i
][1]) {
154 exit(0); /* show scoreboard */
156 case 'l': /* show log file */
162 exit(0); /* show all scoreboard */
164 case 'c': /* anyone with password can create
166 lprcat("Preparing to initialize the scoreboard.\n");
167 if (getpassword() != 0) { /* make new scoreboard */
174 case 'n': /* no welcome msg */
188 case '9': /* for hardness */
189 sscanf(&argv
[i
][1], "%d", &hard
);
192 case 'h': /* print out command line arguments */
193 write(1, cmdhelp
, sizeof(cmdhelp
));
196 case 'o': /* specify a .larnopts filename */
197 strncpy(optsfile
, argv
[i
] + 2, 127);
201 printf("Unknown option <%s>\n", argv
[i
]);
205 if (argv
[i
][0] == '+') {
208 if (argv
[i
][1] == '+') {
210 restoregame(ckpfile
); /* restore checkpointed
217 readopts(); /* read the options file if there is one */
221 userid
= geteuid(); /* obtain the user's effective id number */
223 userid
= getplid(logname
); /* obtain the players id number */
224 #endif /* UIDSCORE */
226 write(2, "Can't obtain playerid\n", 22);
231 * this section of code causes the program to look like something else to ps
233 if (strcmp(psname
, argv
[0])) { /* if a different process name only */
234 if ((i
= access(psname
, 1)) < 0) { /* link not there */
235 if (link(argv
[0], psname
) >= 0) {
242 for (i
= 1; i
< argc
; i
++) {
243 szero(argv
[i
]); /* zero the argument to avoid ps snooping */
245 #endif /* HIDEBYLINK */
247 if (access(savefilename
, 0) == 0) { /* restore game if need to */
251 restoregame(savefilename
); /* restore last game */
253 sigsetup(); /* trap all needed signals */
254 sethard(hard
); /* set up the desired difficulty */
255 setupvt100(); /* setup the terminal special mode */
256 if (c
[HP
] == 0) { /* create new game */
257 makeplayer(); /* make the character that will play */
258 newcavelevel(0);/* make the dungeon */
259 predostuff
= 1; /* tell signals that we are in the welcome
262 welcome(); /* welcome the player to the game */
264 drawscreen(); /* show the initial dungeon */
265 predostuff
= 2; /* tell the trap functions that they must do
266 * a showplayer() from here on */
268 nice(1); /* games should be run niced */
270 yrepcount
= hit2flag
= 0;
273 lookforobject(); /* see if there is an object
276 dropflag
= 0; /* don't show it just dropped an item */
281 } /* move the monsters */
283 showcell(playerx
, playery
);
285 viewflag
= 0; /* show stuff around player */
288 hitflag
= hit3flag
= 0;
290 bot_linex(); /* update bottom line */
296 } /* get commands and make moves */
297 regen(); /* regenerate hp and spells */
298 if (c
[TIMESTOP
] == 0)
300 rmst
= 120 - (level
<< 2);
301 fillmonst(makemonst(level
));
310 show character's inventory
316 for (number
= 3, i
= 0; i
< 26; i
++)
318 number
++; /* count items in inventory */
330 nosignal
= 1; /* don't allow ^c etc */
332 lprintf(".) %ld gold pieces", (long) c
[GOLD
]);
335 for (k
= 26; k
>= 0; k
--)
337 for (i
= 22; i
< 84; i
++)
338 for (j
= 0; j
<= k
; j
++)
343 lprintf("\nElapsed time is %ld. You have %ld mobuls left", (long) ((gltime
+ 99) / 100 + 1), (long) ((TIMELIMIT
- gltime
) / 100));
349 * subroutine to clear screen depending on # lines to display
355 if (count
< 20) { /* how do we clear the screen? */
365 * subroutine to restore normal display screen depending on t_setup()
371 if (count
< 18) /* how did we clear the screen? */
372 draws(0, MAXX
, 0, (count
> MAXY
) ? MAXY
: count
);
380 function to show the things player is wearing only
385 int i
, j
, sigsav
, count
;
387 nosignal
= 1; /* don't allow ^c etc */
390 for (count
= 2, j
= 0; j
<= 26; j
++) /* count number of items we
392 if ((i
= iven
[j
]) != 0)
408 for (i
= 22; i
< 84; i
++)
409 for (j
= 0; j
<= 26; j
++)
429 function to show the things player can wield only
434 int i
, j
, sigsav
, count
;
436 nosignal
= 1; /* don't allow ^c etc */
439 for (count
= 2, j
= 0; j
<= 26; j
++) /* count how many items */
440 if ((i
= iven
[j
]) != 0)
461 for (i
= 22; i
< 84; i
++)
462 for (j
= 0; j
<= 26; j
++)
487 * function to show the things player can read only
492 int i
, j
, sigsav
, count
;
494 nosignal
= 1; /* don't allow ^c etc */
497 for (count
= 2, j
= 0; j
<= 26; j
++)
505 for (i
= 22; i
< 84; i
++)
506 for (j
= 0; j
<= 26; j
++)
519 * function to show the things player can eat only
524 int i
, j
, sigsav
, count
;
526 nosignal
= 1; /* don't allow ^c etc */
529 for (count
= 2, j
= 0; j
<= 26; j
++)
536 for (i
= 22; i
< 84; i
++)
537 for (j
= 0; j
<= 26; j
++)
549 function to show the things player can quaff only
554 int i
, j
, sigsav
, count
;
556 nosignal
= 1; /* don't allow ^c etc */
559 for (count
= 2, j
= 0; j
<= 26; j
++)
566 for (i
= 22; i
< 84; i
++)
567 for (j
= 0; j
<= 26; j
++)
583 lprintf("\n%c) %s", idx
+ 'a', objectname
[iven
[idx
]]);
584 if (str2
!= 0 && str2
[ivenarg
[idx
]][0] != 0)
585 lprintf(" of%s", str2
[ivenarg
[idx
]]);
591 switch (iven
[indx
]) {
593 show1(indx
, potionname
);
596 show1(indx
, scrollname
);
614 lprintf("\n%c) %s", indx
+ 'a', objectname
[iven
[indx
]]);
615 if (ivenarg
[indx
] > 0)
616 lprintf(" + %ld", (long) ivenarg
[indx
]);
617 else if (ivenarg
[indx
] < 0)
618 lprintf(" %ld", (long) ivenarg
[indx
]);
621 if (c
[WIELD
] == indx
)
622 lprcat(" (weapon in hand)");
623 if ((c
[WEAR
] == indx
) || (c
[SHIELD
] == indx
))
624 lprcat(" (being worn)");
625 if (++srcount
>= 22) {
633 subroutine to randomly create monsters if needed
639 return; /* don't make monsters if time is stopped */
641 rmst
= 120 - (level
<< 2);
642 fillmonst(makemonst(level
));
651 get and execute a command
659 switch (k
) { /* get the token from the input and switch on
687 return; /* northeast */
690 return; /* northeast */
693 return; /* northwest */
696 return; /* northwest */
699 return; /* southeast */
702 return; /* southeast */
705 return; /* southwest */
708 return; /* southwest */
713 return; /* stay here */
718 return; /* wield a weapon */
723 return; /* wear armor */
729 lprcat("\nYou can't read anything when you're blind!");
730 } else if (c
[TIMESTOP
] == 0)
732 return; /* to read a scroll */
736 if (c
[TIMESTOP
] == 0)
738 return; /* quaff a potion */
742 if (c
[TIMESTOP
] == 0)
744 return; /* to drop an object */
749 return; /* cast a spell */
759 if (c
[TIMESTOP
] == 0)
761 return; /* to eat a fortune cookie */
767 return; /* list spells and scrolls */
773 return; /* give the help screen */
777 lprcat("Saving . . .");
779 savegame(savefilename
);
781 died(-257); /* save the game - doesn't return */
790 lprcat("\nAs yet, you don't have enough experience to use teleportation");
791 return; /* teleport yourself */
793 case '^': /* identify traps */
794 flag
= yrepcount
= 0;
797 for (j
= playery
- 1; j
< playery
+ 2; j
++) {
802 for (i
= playerx
- 1; i
< playerx
+ 2; i
++) {
807 switch (item
[i
][j
]) {
813 lprcat(objectname
[item
[i
][j
]]);
819 lprcat("\nNo traps are visible");
823 case '_': /* this is the fudge player password for
828 if (userid
!= wisid
) {
829 lprcat("Sorry, you are not empowered to be a wizard.\n");
830 scbr(); /* system("stty -echo cbreak"); */
834 if (getpassword() == 0) {
835 scbr(); /* system("stty -echo cbreak"); */
839 scbr(); /* system("stty -echo cbreak"); */
840 for (i
= 0; i
< 6; i
++)
842 iven
[0] = iven
[1] = 0;
847 c
[WEAR
] = c
[SHIELD
] = -1;
848 raiseexperience(6000000L);
849 c
[AWARENESS
] += 25000;
852 for (i
= 0; i
< MAXY
; i
++)
853 for (j
= 0; j
< MAXX
; j
++)
855 for (i
= 0; i
< SPNUM
; i
++)
857 for (i
= 0; i
< MAXSCROLL
; i
++)
858 scrollname
[i
] = scrollhide
[i
];
859 for (i
= 0; i
< MAXPOTION
; i
++)
860 potionname
[i
] = potionhide
[i
];
862 for (i
= 0; i
< MAXSCROLL
; i
++)
863 if (strlen(scrollname
[i
]) > 2) { /* no null items */
864 item
[i
][0] = OSCROLL
;
867 for (i
= MAXX
- 1; i
> MAXX
- 1 - MAXPOTION
; i
--)
868 if (strlen(potionname
[i
- MAXX
+ MAXPOTION
]) > 2) { /* no null items */
869 item
[i
][0] = OPOTION
;
870 iarg
[i
][0] = i
- MAXX
+ MAXPOTION
;
872 for (i
= 1; i
< MAXY
; i
++) {
876 for (i
= MAXY
; i
< MAXY
+ MAXX
; i
++) {
877 item
[i
- MAXY
][MAXY
- 1] = i
;
878 iarg
[i
- MAXY
][MAXY
- 1] = 0;
880 for (i
= MAXX
+ MAXY
; i
< MAXX
+ MAXY
+ MAXY
; i
++) {
881 item
[MAXX
- 1][i
- MAXX
- MAXY
] = i
;
882 iarg
[MAXX
- 1][i
- MAXX
- MAXY
] = 0;
892 if (c
[SHIELD
] != -1) {
894 lprcat("\nYour shield is off");
896 } else if (c
[WEAR
] != -1) {
898 lprcat("\nYour armor is off");
901 lprcat("\nYou aren't wearing anything");
906 lprintf("\nThe stuff you are carrying presently weighs %ld pounds", (long) packweight());
915 lprintf("\nCaverns of Larn, Version %ld.%ld, Diff=%ld",
916 (long) VERSION
, (long) SUBVERSION
,
946 } /* create diagnostic file */
952 if (outstanding_taxes
> 0)
953 lprintf("\nYou presently owe %ld gp in taxes.",
954 (long) outstanding_taxes
);
956 lprcat("\nYou do not owe any taxes.");
967 movemonst(); /* move the monsters */
990 showcell(playerx
, playery
);
995 function to wield a weapon
1002 if ((i
= whatitem("wield")) == '\33')
1007 else if (iven
[i
- 'a'] == 0) {
1010 } else if (iven
[i
- 'a'] == OPOTION
) {
1013 } else if (iven
[i
- 'a'] == OSCROLL
) {
1016 } else if ((c
[SHIELD
] != -1) && (iven
[i
- 'a'] == O2SWORD
)) {
1017 lprcat("\nBut one arm is busy with your shield!");
1021 if (iven
[i
- 'a'] == OLANCE
)
1033 common routine to say you don't have an item
1040 lprintf("\nYou don't have item %c!", x
);
1047 lprintf("\nYou can't wield item %c!", x
);
1051 function to wear armor
1058 if ((i
= whatitem("wear")) == '\33')
1064 switch (iven
[i
- 'a']) {
1076 if (c
[WEAR
] != -1) {
1077 lprcat("\nYou're already wearing some armor");
1084 if (c
[SHIELD
] != -1) {
1085 lprcat("\nYou are already wearing a shield");
1088 if (iven
[c
[WIELD
]] == O2SWORD
) {
1089 lprcat("\nYour hands are busy with the two handed sword!");
1092 c
[SHIELD
] = i
- 'a';
1096 lprcat("\nYou can't wear that!");
1103 function to drop an object
1111 p
= &item
[playerx
][playery
];
1113 if ((i
= whatitem("drop")) == '\33')
1118 if (i
== '.') { /* drop some gold */
1120 lprcat("\nThere's something here already!");
1125 lprcat("How much gold do you drop? ");
1126 if ((amt
= readnum((long) c
[GOLD
])) == 0)
1128 if (amt
> c
[GOLD
]) {
1129 lprcat("\nYou don't have that much!");
1135 } else if (amt
<= 327670L) {
1139 } else if (amt
<= 3276700L) {
1143 } else if (amt
<= 32767000L) {
1153 lprintf("You drop %ld gold pieces", (long)amt
);
1154 iarg
[playerx
][playery
] = i
;
1156 know
[playerx
][playery
] = 0;
1160 drop_object(i
- 'a');
1167 * readscr() Subroutine to read a scroll one is carrying
1174 if ((i
= whatitem("read")) == '\33')
1180 if (iven
[i
- 'a'] == OSCROLL
) {
1181 read_scroll(ivenarg
[i
- 'a']);
1185 if (iven
[i
- 'a'] == OBOOK
) {
1186 readbook(ivenarg
[i
- 'a']);
1190 if (iven
[i
- 'a'] == 0) {
1194 lprcat("\nThere's nothing on it to read");
1202 * subroutine to eat a cookie one is carrying
1211 if ((i
= whatitem("eat")) == '\33')
1217 if (iven
[i
- 'a'] == OCOOKIE
) {
1218 lprcat("\nThe cookie was delicious.");
1220 if (!c
[BLINDCOUNT
]) {
1221 if ((p
= fortune()) != NULL
) {
1222 lprcat(" Inside you find a scrap of paper that says:\n");
1228 if (iven
[i
- 'a'] == 0) {
1232 lprcat("\nYou can't eat that!");
1240 * subroutine to quaff a potion one is carrying
1247 if ((i
= whatitem("quaff")) == '\33')
1253 if (iven
[i
- 'a'] == OPOTION
) {
1254 quaffpotion(ivenarg
[i
- 'a']);
1258 if (iven
[i
- 'a'] == 0) {
1262 lprcat("\nYou wouldn't want to quaff that, would you? ");
1270 function to ask what player wants to do
1273 whatitem(const char *str
)
1277 lprintf("\nWhat do you want to %s [* for all] ? ", str
);
1279 while (i
> 'z' || (i
< 'a' && i
!= '*' && i
!= '\33' && i
!= '.'))
1287 subroutine to get a number from the player
1288 and allow * to mean return amt, else return the number entered
1295 unsigned long amt
= 0;
1297 if ((i
= ttgetch()) == '*')
1298 amt
= mx
; /* allow him to say * for all gold */
1306 if ((i
<= '9') && (i
>= '0') && (amt
< 99999999))
1307 amt
= amt
* 10 + i
- '0';
1316 * routine to zero every byte in a string
1325 #endif /* HIDEBYLINK */