]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/main.c
1 /* $NetBSD: main.c,v 1.20 2008/01/28 05:38:54 dholland Exp $ */
6 __RCSID("$NetBSD: main.c,v 1.20 2008/01/28 05:38:54 dholland 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 its 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(".) %ld 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 %ld. You have %ld 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
]]);
589 switch (iven
[indx
]) {
591 show1(indx
, potionname
);
594 show1(indx
, scrollname
);
612 lprintf("\n%c) %s", indx
+ 'a', objectname
[iven
[indx
]]);
613 if (ivenarg
[indx
] > 0)
614 lprintf(" + %ld", (long) ivenarg
[indx
]);
615 else if (ivenarg
[indx
] < 0)
616 lprintf(" %ld", (long) ivenarg
[indx
]);
619 if (c
[WIELD
] == indx
)
620 lprcat(" (weapon in hand)");
621 if ((c
[WEAR
] == indx
) || (c
[SHIELD
] == indx
))
622 lprcat(" (being worn)");
623 if (++srcount
>= 22) {
631 subroutine to randomly create monsters if needed
637 return; /* don't make monsters if time is stopped */
639 rmst
= 120 - (level
<< 2);
640 fillmonst(makemonst(level
));
649 get and execute a command
657 switch (k
) { /* get the token from the input and switch on
685 return; /* northeast */
688 return; /* northeast */
691 return; /* northwest */
694 return; /* northwest */
697 return; /* southeast */
700 return; /* southeast */
703 return; /* southwest */
706 return; /* southwest */
711 return; /* stay here */
716 return; /* wield a weapon */
721 return; /* wear armor */
727 lprcat("\nYou can't read anything when you're blind!");
728 } else if (c
[TIMESTOP
] == 0)
730 return; /* to read a scroll */
734 if (c
[TIMESTOP
] == 0)
736 return; /* quaff a potion */
740 if (c
[TIMESTOP
] == 0)
742 return; /* to drop an object */
747 return; /* cast a spell */
757 if (c
[TIMESTOP
] == 0)
759 return; /* to eat a fortune cookie */
765 return; /* list spells and scrolls */
771 return; /* give the help screen */
775 lprcat("Saving . . .");
777 savegame(savefilename
);
779 died(-257); /* save the game - doesn't return */
788 lprcat("\nAs yet, you don't have enough experience to use teleportation");
789 return; /* teleport yourself */
791 case '^': /* identify traps */
792 flag
= yrepcount
= 0;
795 for (j
= playery
- 1; j
< playery
+ 2; j
++) {
800 for (i
= playerx
- 1; i
< playerx
+ 2; i
++) {
805 switch (item
[i
][j
]) {
811 lprcat(objectname
[item
[i
][j
]]);
817 lprcat("\nNo traps are visible");
821 case '_': /* this is the fudge player password for
826 if (userid
!= wisid
) {
827 lprcat("Sorry, you are not empowered to be a wizard.\n");
828 scbr(); /* system("stty -echo cbreak"); */
832 if (getpassword() == 0) {
833 scbr(); /* system("stty -echo cbreak"); */
837 scbr(); /* system("stty -echo cbreak"); */
838 for (i
= 0; i
< 6; i
++)
840 iven
[0] = iven
[1] = 0;
845 c
[WEAR
] = c
[SHIELD
] = -1;
846 raiseexperience(6000000L);
847 c
[AWARENESS
] += 25000;
850 for (i
= 0; i
< MAXY
; i
++)
851 for (j
= 0; j
< MAXX
; j
++)
853 for (i
= 0; i
< SPNUM
; i
++)
855 for (i
= 0; i
< MAXSCROLL
; i
++)
856 scrollname
[i
] = scrollhide
[i
];
857 for (i
= 0; i
< MAXPOTION
; i
++)
858 potionname
[i
] = potionhide
[i
];
860 for (i
= 0; i
< MAXSCROLL
; i
++)
861 if (strlen(scrollname
[i
]) > 2) { /* no null items */
862 item
[i
][0] = OSCROLL
;
865 for (i
= MAXX
- 1; i
> MAXX
- 1 - MAXPOTION
; i
--)
866 if (strlen(potionname
[i
- MAXX
+ MAXPOTION
]) > 2) { /* no null items */
867 item
[i
][0] = OPOTION
;
868 iarg
[i
][0] = i
- MAXX
+ MAXPOTION
;
870 for (i
= 1; i
< MAXY
; i
++) {
874 for (i
= MAXY
; i
< MAXY
+ MAXX
; i
++) {
875 item
[i
- MAXY
][MAXY
- 1] = i
;
876 iarg
[i
- MAXY
][MAXY
- 1] = 0;
878 for (i
= MAXX
+ MAXY
; i
< MAXX
+ MAXY
+ MAXY
; i
++) {
879 item
[MAXX
- 1][i
- MAXX
- MAXY
] = i
;
880 iarg
[MAXX
- 1][i
- MAXX
- MAXY
] = 0;
890 if (c
[SHIELD
] != -1) {
892 lprcat("\nYour shield is off");
894 } else if (c
[WEAR
] != -1) {
896 lprcat("\nYour armor is off");
899 lprcat("\nYou aren't wearing anything");
904 lprintf("\nThe stuff you are carrying presently weighs %ld pounds", (long) packweight());
913 lprintf("\nCaverns of Larn, Version %ld.%ld, Diff=%ld",
914 (long) VERSION
, (long) SUBVERSION
,
944 } /* create diagnostic file */
950 if (outstanding_taxes
> 0)
951 lprintf("\nYou presently owe %ld gp in taxes.",
952 (long) outstanding_taxes
);
954 lprcat("\nYou do not owe any taxes.");
965 movemonst(); /* move the monsters */
988 showcell(playerx
, playery
);
993 function to wield a weapon
1000 if ((i
= whatitem("wield")) == '\33')
1005 else if (iven
[i
- 'a'] == 0) {
1008 } else if (iven
[i
- 'a'] == OPOTION
) {
1011 } else if (iven
[i
- 'a'] == OSCROLL
) {
1014 } else if ((c
[SHIELD
] != -1) && (iven
[i
- 'a'] == O2SWORD
)) {
1015 lprcat("\nBut one arm is busy with your shield!");
1019 if (iven
[i
- 'a'] == OLANCE
)
1031 common routine to say you don't have an item
1038 lprintf("\nYou don't have item %c!", x
);
1045 lprintf("\nYou can't wield item %c!", x
);
1049 function to wear armor
1056 if ((i
= whatitem("wear")) == '\33')
1062 switch (iven
[i
- 'a']) {
1074 if (c
[WEAR
] != -1) {
1075 lprcat("\nYou're already wearing some armor");
1082 if (c
[SHIELD
] != -1) {
1083 lprcat("\nYou are already wearing a shield");
1086 if (iven
[c
[WIELD
]] == O2SWORD
) {
1087 lprcat("\nYour hands are busy with the two handed sword!");
1090 c
[SHIELD
] = i
- 'a';
1094 lprcat("\nYou can't wear that!");
1101 function to drop an object
1109 p
= &item
[playerx
][playery
];
1111 if ((i
= whatitem("drop")) == '\33')
1116 if (i
== '.') { /* drop some gold */
1118 lprcat("\nThere's something here already!");
1123 lprcat("How much gold do you drop? ");
1124 if ((amt
= readnum((long) c
[GOLD
])) == 0)
1126 if (amt
> c
[GOLD
]) {
1127 lprcat("\nYou don't have that much!");
1133 } else if (amt
<= 327670L) {
1137 } else if (amt
<= 3276700L) {
1141 } else if (amt
<= 32767000L) {
1151 lprintf("You drop %ld gold pieces", (long)amt
);
1152 iarg
[playerx
][playery
] = i
;
1154 know
[playerx
][playery
] = 0;
1158 drop_object(i
- 'a');
1165 * readscr() Subroutine to read a scroll one is carrying
1172 if ((i
= whatitem("read")) == '\33')
1178 if (iven
[i
- 'a'] == OSCROLL
) {
1179 read_scroll(ivenarg
[i
- 'a']);
1183 if (iven
[i
- 'a'] == OBOOK
) {
1184 readbook(ivenarg
[i
- 'a']);
1188 if (iven
[i
- 'a'] == 0) {
1192 lprcat("\nThere's nothing on it to read");
1200 * subroutine to eat a cookie one is carrying
1209 if ((i
= whatitem("eat")) == '\33')
1215 if (iven
[i
- 'a'] == OCOOKIE
) {
1216 lprcat("\nThe cookie was delicious.");
1218 if (!c
[BLINDCOUNT
]) {
1219 if ((p
= fortune()) != NULL
) {
1220 lprcat(" Inside you find a scrap of paper that says:\n");
1226 if (iven
[i
- 'a'] == 0) {
1230 lprcat("\nYou can't eat that!");
1238 * subroutine to quaff a potion one is carrying
1245 if ((i
= whatitem("quaff")) == '\33')
1251 if (iven
[i
- 'a'] == OPOTION
) {
1252 quaffpotion(ivenarg
[i
- 'a']);
1256 if (iven
[i
- 'a'] == 0) {
1260 lprcat("\nYou wouldn't want to quaff that, would you? ");
1268 function to ask what player wants to do
1271 whatitem(const char *str
)
1275 lprintf("\nWhat do you want to %s [* for all] ? ", str
);
1277 while (i
> 'z' || (i
< 'a' && i
!= '*' && i
!= '\33' && i
!= '.'))
1285 subroutine to get a number from the player
1286 and allow * to mean return amt, else return the number entered
1293 unsigned long amt
= 0;
1295 if ((i
= lgetchar()) == '*')
1296 amt
= mx
; /* allow him to say * for all gold */
1304 if ((i
<= '9') && (i
>= '0') && (amt
< 99999999))
1305 amt
= amt
* 10 + i
- '0';
1314 * routine to zero every byte in a string
1323 #endif /* HIDEBYLINK */