BSD 4_4_Lite1 release
[unix-history] / usr / src / games / snake / snake / snake.c
index d0e64d2..b99da10 100644 (file)
@@ -1,18 +1,44 @@
 /*
 /*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1980, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1980 Regents of the University of California.\n\
- All rights reserved.\n";
+static char copyright[] =
+"@(#) Copyright (c) 1980, 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)snake.c    5.8 (Berkeley) %G%";
+static char sccsid[] = "@(#)snake.c    8.2 (Berkeley) 1/7/94";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -27,7 +53,12 @@ static char sccsid[] = "@(#)snake.c  5.8 (Berkeley) %G%";
  */
 
 #include <sys/param.h>
  */
 
 #include <sys/param.h>
+
+#include <errno.h>
+#include <fcntl.h>
 #include <pwd.h>
 #include <pwd.h>
+#include <time.h>
+
 #include "snake.h"
 #include "pathnames.h"
 
 #include "snake.h"
 #include "pathnames.h"
 
@@ -69,9 +100,8 @@ char **argv;
        extern char *optarg;
        extern int optind;
        int ch, i, j, k;
        extern char *optarg;
        extern int optind;
        int ch, i, j, k;
-       time_t time();
        long atol();
        long atol();
-       int stop();
+       void stop();
 
        (void)time(&tv);
        srandom((int)tv);
 
        (void)time(&tv);
        srandom((int)tv);
@@ -101,7 +131,7 @@ char **argv;
        i = MIN(lcnt, ccnt);
        if (i < 4) {
                cook();
        i = MIN(lcnt, ccnt);
        if (i < 4) {
                cook();
-               printf("snake: screen too small for a fair game.\n");
+               pr("snake: screen too small for a fair game.\n");
                exit(1);
        }
 
                exit(1);
        }
 
@@ -344,10 +374,10 @@ mainloop()
                                win(&finish);
                                ll();
                                cook();
                                win(&finish);
                                ll();
                                cook();
-                               printf("You have won with $%d.\n",cashvalue);
+                               pr("You have won with $%d.\n",cashvalue);
                                fflush(stdout);
                                logit("won");
                                fflush(stdout);
                                logit("won");
-                               post(cashvalue,0);
+                               post(cashvalue,1);
                                length(moves);
                                done();
                        }
                                length(moves);
                                done();
                        }
@@ -438,43 +468,50 @@ int       iscore, flag;
        short   uid;
        short   oldbest=0;
        short   allbwho=0, allbscore=0;
        short   uid;
        short   oldbest=0;
        short   allbwho=0, allbscore=0;
-       struct  passwd *p, *getpwuid();
+       struct  passwd *p;
 
        /*
         * Neg uid, 0, and 1 cannot have scores recorded.
         */
 
        /*
         * Neg uid, 0, and 1 cannot have scores recorded.
         */
-       if ((uid=getuid()) > 1 && (rawscores=open(_PATH_RAWSCORES,2))>=0) {
-               /* Figure out what happened in the past */
-               read(rawscores, &allbscore, sizeof(short));
-               read(rawscores, &allbwho, sizeof(short));
+       if ((uid = getuid()) <= 1) {
+               pr("No saved scores for uid %d.\n", uid);
+               return(1);
+       }
+       if ((rawscores = open(_PATH_RAWSCORES, O_RDWR|O_CREAT, 0644)) < 0) {
+               pr("No score file %s: %s.\n", _PATH_RAWSCORES,
+                   strerror(errno));
+               return(1);
+       }
+       /* Figure out what happened in the past */
+       read(rawscores, &allbscore, sizeof(short));
+       read(rawscores, &allbwho, sizeof(short));
+       lseek(rawscores, ((long)uid)*sizeof(short), 0);
+       read(rawscores, &oldbest, sizeof(short));
+       if (!flag)
+               return (score > oldbest ? 1 : 0);
+
+       /* Update this jokers best */
+       if (score > oldbest) {
                lseek(rawscores, ((long)uid)*sizeof(short), 0);
                lseek(rawscores, ((long)uid)*sizeof(short), 0);
-               read(rawscores, &oldbest, sizeof(short));
-               if (flag) return (score > oldbest ? 1 : 0);
-
-               /* Update this jokers best */
-               if (score > oldbest) {
-                       lseek(rawscores, ((long)uid)*sizeof(short), 0);
-                       write(rawscores, &score, sizeof(short));
-                       printf("You bettered your previous best of $%d\n", oldbest);
-               } else
-                       printf("Your best to date is $%d\n", oldbest);
-
-               /* See if we have a new champ */
-               p = getpwuid(allbwho);
-               if (p == NULL || score > allbscore) {
-                       lseek(rawscores, (long)0, 0);
-                       write(rawscores, &score, sizeof(short));
-                       write(rawscores, &uid, sizeof(short));
-                       if (p != NULL)
-                               printf("You beat %s's old record of $%d!\n", p->pw_name, allbscore);
-                       else
-                               printf("You set a new record!\n");
-               } else
-                       printf("The highest is %s with $%d\n", p->pw_name, allbscore);
-               close(rawscores);
+               write(rawscores, &score, sizeof(short));
+               pr("You bettered your previous best of $%d\n", oldbest);
+       } else
+               pr("Your best to date is $%d\n", oldbest);
+
+       /* See if we have a new champ */
+       p = getpwuid(allbwho);
+       if (p == NULL || score > allbscore) {
+               lseek(rawscores, (long)0, 0);
+               write(rawscores, &score, sizeof(short));
+               write(rawscores, &uid, sizeof(short));
+               if (allbwho)
+                       pr("You beat %s's old record of $%d!\n",
+                           p->pw_name, allbscore);
+               else
+                       pr("You set a new record!\n");
        } else
        } else
-               if (!flag)
-                       printf("Unable to post score.\n");
+               pr("The highest is %s with $%d\n", p->pw_name, allbscore);
+       close(rawscores);
        return (1);
 }
 
        return (1);
 }
 
@@ -502,7 +539,7 @@ struct point *sp, *np;
           snake would get too good */
        struct point d;
        int w, i, wt[8];
           snake would get too good */
        struct point d;
        int w, i, wt[8];
-       double sqrt(), v1, v2, vp, max;
+       double v1, v2, vp, max;
        point(&d,you.col-sp->col,you.line-sp->line);
        v1 = sqrt( (double) (d.col*d.col + d.line*d.line) );
        w=0; 
        point(&d,you.col-sp->col,you.line-sp->line);
        v1 = sqrt( (double) (d.col*d.col + d.line*d.line) );
        w=0; 
@@ -547,7 +584,7 @@ struct point *sp, *np;
                else
                        vp -= wt[i];
        if (i==8) {
                else
                        vp -= wt[i];
        if (i==8) {
-               printf("failure\n"); 
+               pr("failure\n"); 
                i=0;
                while (wt[i]==0) i++;
        }
                i=0;
                while (wt[i]==0) i++;
        }
@@ -578,7 +615,7 @@ int w;{
        for(j=0;j<3;j++){
                clear();
                delay(5);
        for(j=0;j<3;j++){
                clear();
                delay(5);
-               aprintf(&p,str);
+               apr(&p,str);
                delay(10);
        }
        setup();
                delay(10);
        }
        setup();
@@ -604,19 +641,19 @@ snap()
        if (! stretch(&money)) if (! stretch(&finish)) delay(10);
        if(you.line < 3){
                point(&p,you.col,0);
        if (! stretch(&money)) if (! stretch(&finish)) delay(10);
        if(you.line < 3){
                point(&p,you.col,0);
-               remove(&p);
+               chk(&p);
        }
        if(you.line > lcnt-4){
                point(&p,you.col,lcnt-1);
        }
        if(you.line > lcnt-4){
                point(&p,you.col,lcnt-1);
-               remove(&p);
+               chk(&p);
        }
        if(you.col < 10){
                point(&p,0,you.line);
        }
        if(you.col < 10){
                point(&p,0,you.line);
-               remove(&p);
+               chk(&p);
        }
        if(you.col > ccnt-10){
                point(&p,ccnt-1,you.line);
        }
        if(you.col > ccnt-10){
                point(&p,ccnt-1,you.line);
-               remove(&p);
+               chk(&p);
        }
        fflush(stdout);
 }
        }
        fflush(stdout);
 }
@@ -631,13 +668,13 @@ struct point *ps;{
                                pchar(&p,'v');
                        delay(10);
                        for (;p.line > you.line;p.line--)
                                pchar(&p,'v');
                        delay(10);
                        for (;p.line > you.line;p.line--)
-                               remove(&p);
+                               chk(&p);
                } else {
                        for (p.line = you.line-1;p.line >= ps->line;p.line--)
                                pchar(&p,'^');
                        delay(10);
                        for (;p.line < you.line;p.line++)
                } else {
                        for (p.line = you.line-1;p.line >= ps->line;p.line--)
                                pchar(&p,'^');
                        delay(10);
                        for (;p.line < you.line;p.line++)
-                               remove(&p);
+                               chk(&p);
                }
                return(1);
        } else if(abs(ps->line-you.line) < 3){
                }
                return(1);
        } else if(abs(ps->line-you.line) < 3){
@@ -647,13 +684,13 @@ struct point *ps;{
                                pchar(&p,'>');
                        delay(10);
                        for (;p.col > you.col;p.col--)
                                pchar(&p,'>');
                        delay(10);
                        for (;p.col > you.col;p.col--)
-                               remove(&p);
+                               chk(&p);
                } else {
                        for (p.col = you.col-1;p.col >= ps->col;p.col--)
                                pchar(&p,'<');
                        delay(10);
                        for (;p.col < you.col;p.col++)
                } else {
                        for (p.col = you.col-1;p.col >= ps->col;p.col--)
                                pchar(&p,'<');
                        delay(10);
                        for (;p.col < you.col;p.col++)
-                               remove(&p);
+                               chk(&p);
                }
                return(1);
        }
                }
                return(1);
        }
@@ -669,20 +706,20 @@ struct point *ps;{
        if(ps->line == 0)ps->line++;
        if(ps->line == LINES -1)ps->line--;
        if(ps->col == COLUMNS -1)ps->col--;
        if(ps->line == 0)ps->line++;
        if(ps->line == LINES -1)ps->line--;
        if(ps->col == COLUMNS -1)ps->col--;
-       aprintf(point(&x,ps->col-1,ps->line-1),"/*\\\r* *\r\\*/");
+       apr(point(&x,ps->col-1,ps->line-1),"/*\\\r* *\r\\*/");
        for (j=0;j<20;j++){
                pchar(ps,'@');
                delay(1);
                pchar(ps,' ');
                delay(1);
        }
        for (j=0;j<20;j++){
                pchar(ps,'@');
                delay(1);
                pchar(ps,' ');
                delay(1);
        }
-       if (post(cashvalue,1)) {
-               aprintf(point(&x,ps->col-1,ps->line-1),"   \ro.o\r\\_/");
+       if (post(cashvalue,0)) {
+               apr(point(&x,ps->col-1,ps->line-1),"   \ro.o\r\\_/");
                delay(6);
                delay(6);
-               aprintf(point(&x,ps->col-1,ps->line-1),"   \ro.-\r\\_/");
+               apr(point(&x,ps->col-1,ps->line-1),"   \ro.-\r\\_/");
                delay(6);
        }
                delay(6);
        }
-       aprintf(point(&x,ps->col-1,ps->line-1),"   \ro.o\r\\_/");
+       apr(point(&x,ps->col-1,ps->line-1),"   \ro.o\r\\_/");
 }
 win(ps)
 struct point *ps;
 }
 win(ps)
 struct point *ps;
@@ -744,7 +781,7 @@ pushsnake()
                        i = (cashvalue) % 10;
                        bonus = ((rand()>>8) & 0377)% 10;
                        ll();
                        i = (cashvalue) % 10;
                        bonus = ((rand()>>8) & 0377)% 10;
                        ll();
-                       printf("%d\n", bonus);
+                       pr("%d\n", bonus);
                        delay(30);
                        if (bonus == i) {
                                spacewarp(1);
                        delay(30);
                        if (bonus == i) {
                                spacewarp(1);
@@ -753,9 +790,11 @@ pushsnake()
                                return(1);
                        }
                        if ( loot >= penalty ){
                                return(1);
                        }
                        if ( loot >= penalty ){
-                               printf("You and your $%d have been eaten\n",cashvalue);
+                               pr("You and your $%d have been eaten\n",
+                                   cashvalue);
                        } else {
                        } else {
-                               printf("The snake ate you.  You owe $%d.\n",-cashvalue);
+                               pr("The snake ate you.  You owe $%d.\n",
+                                   -cashvalue);
                        }
                        logit("eaten");
                        length(moves);
                        }
                        logit("eaten");
                        length(moves);
@@ -765,7 +804,7 @@ pushsnake()
        return(0);
 }
        
        return(0);
 }
        
-remove(sp)
+chk(sp)
 struct point *sp;
 {
        int j;
 struct point *sp;
 {
        int j;
@@ -808,12 +847,13 @@ int won;
        p.line = p.col = 1;
        if(won>0){
                move(&p);
        p.line = p.col = 1;
        if(won>0){
                move(&p);
-               printf("$%d",won);
+               pr("$%d",won);
        }
 }
 
        }
 }
 
+void
 stop(){
 stop(){
-       signal(SIGINT,1);
+       signal(SIGINT,SIG_IGN);
        ll();
        length(moves);
        done();
        ll();
        length(moves);
        done();
@@ -834,7 +874,7 @@ suspend()
 length(num)
 int num;
 {
 length(num)
 int num;
 {
-       printf("You made %d moves.\n",num);
+       pr("You made %d moves.\n",num);
 }
 
 logit(msg)
 }
 
 logit(msg)
@@ -845,7 +885,8 @@ char *msg;
 
        if ((logfile=fopen(_PATH_LOGFILE, "a")) != NULL) {
                time(&t);
 
        if ((logfile=fopen(_PATH_LOGFILE, "a")) != NULL) {
                time(&t);
-               fprintf(logfile, "%s $%d %dx%d %s %s", getlogin(), cashvalue, lcnt, ccnt, msg, ctime(&t));
+               fprintf(logfile, "%s $%d %dx%d %s %s",
+                   getlogin(), cashvalue, lcnt, ccnt, msg, ctime(&t));
                fclose(logfile);
        }
 }
                fclose(logfile);
        }
 }