]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - warp/score.c
8e9991d62d172f48c365fa021cd77d3054b62774
1 /* Header: /usr/src/games/warp/RCS/score.c,v 1.1 87/07/03 02:13:26 games Exp */
4 * Revision 7.0.1.2a 87/07/03 02:13:26 games
5 * Fixed numerous long vs. int bugs in printfs, etc.
7 * Revision 7.0.1.2 86/10/20 12:06:56 lwall
8 * Made all exits reset tty.
10 * Revision 7.0.1.1 86/10/16 10:52:47 lwall
11 * Added Damage. Fixed random bugs.
13 * Revision 7.0 86/10/08 15:13:14 lwall
14 * Split into separate files. Added amoebas and pirates.
38 if (stat(SAVEDIR
,&filestat
)) {
39 printf("Cannot access %s\r\n",SAVEDIR
);
42 if (filestat
.st_gid
!= getegid() && geteuid() != 0) {
43 printf("Warp will not run right without being setgid.\r\n");
46 if ((filestat
.st_mode
& 0605) != 0605) {
47 printf("%s is not protected correctly (must be u+rw o+rx).\r\n",SAVEDIR
);
52 interp(longlognam
, sizeof longlognam
, "%N");
53 for (i
=strlen(longlognam
); i
<24; i
++)
54 longlognam
[i
] = ' '; /* make sure it is 24 long for strncmp */
55 longlognam
[24] = '\0';
57 interp(longlognam
, sizeof longlognam
, "%L");
58 for (i
=strlen(longlognam
); i
<8; i
++)
59 longlognam
[i
] = ' '; /* make sure it is 8 long for strncmp */
66 snprintf(savefilename
, sizeof(savefilename
), "%s/save.%s",
69 savfil
= experimenting
? NULL
: fopen(savefilename
,"r");
70 if (savfil
!= NULL
&& fgets(spbuf
,100,savfil
) != NULL
) {
73 spbuf
[strlen(spbuf
)-1] = '\0';
74 if (fgets(tmpbuf
,80,savfil
) != NULL
) {
77 tmpbuf
[strlen(tmpbuf
)-1] = '\0';
78 printf("You seem to have left a game %s.\r\n",tmpbuf
+9);
79 s
= strchr(tmpbuf
+9, ',');
81 processnum
= atoi(s
+11);
82 if (kill(processnum
, SIGINT
)) {
83 /* does process not exist? */
84 /* (warp ignores SIGINT) */
86 That process does not seem to exist anymore, so you'll have to start the\r\n");
88 "last wave over.\r\n\n");
90 " [type anything to continue]");
94 if (*tmpbuf
== INTRCH
)
99 if (strcmp(term
+8,tmpbuf
+23)) {
101 "That is not your current terminal--you are on %s.\r\n", term
+5);
102 printf("\r\nYour options:\r\n");
103 printf(" 1) Exit and find the terminal it's running on\r\n");
106 printf("\r\nYour options:\r\n");
107 printf(" 1) Exit and try to foreground it\r\n");
109 printf(" 2) Let me terminate the other game\r\n\n");
110 printf("What do you want to do? ");
115 if (*tmpbuf
== INTRCH
)
117 if (*tmpbuf
== '1') {
119 "If you don't succeed, come back and do option 2 instead. Good luck.\r\n");
123 "Ok, hang on a few moments \r\n");
125 if (kill(processnum
, SIGQUIT
)) {
126 printf("Unable to kill process #%d!\r\n",processnum
);
131 kill(processnum
, SIGCONT
);
135 if (kill(processnum
,SIGINT
))
136 /* does process not exist? */
137 /* (warp ignores SIGINT) */
142 savfil
= fopen(savefilename
,"r");
143 if (savfil
!= NULL
) {
144 fgets(spbuf
,100,savfil
);
151 if (savfil
== NULL
) {
152 totalscore
= smarts
= cumsmarts
= wave
= 0;
157 totalscore
= atol(spbuf
+9);
158 smarts
= atoi(spbuf
+20);
159 cumsmarts
= atoi(spbuf
+24);
160 numents
= atoi(spbuf
+30);
161 numbases
= atoi(spbuf
+33);
162 wave
= atoi(spbuf
+36);
163 apolspec
= (spbuf
[40] == 'a');
164 beginner
= (spbuf
[41] == 'b');
165 crushspec
= (spbuf
[42] == 'c');
166 gornspec
= (spbuf
[43] == 'g');
167 massacre
= (spbuf
[44] == 'm');
168 romspec
= (spbuf
[45] == 'r');
169 tholspec
= (spbuf
[46] == 't');
170 lowspeed
= (spbuf
[47] == 'l') || lowspeed
;
171 amoebaspec
= (spbuf
[48] == '&');
178 page(NEWSFILE
,false);
180 printf("\r\nSaved game: SCORE DIFF CUMDIFF ENTERPRISES BASES WAVE");
181 printf("\r\n %7ld %2d %4d %1d %1d %3d",
182 totalscore
,smarts
,cumsmarts
,numents
,numbases
,wave
);
184 printf("\r\nWould you like instructions? ");
191 if (*buf
== 'Y' || *buf
== 'y') {
192 page(HELPFILE
,false);
193 printf("\r\nWould you like to play easy games for a while? ");
198 if (*buf
== 'Y' || *buf
== 'y') {
212 printf(" TOP WARPISTS\r\n\n");
213 printf("RANK WHO AKA SCORE DIFF CUMDIFF WHEN\r\n");
214 page(SCOREBOARD
,true);
215 printf(" [Type anything to continue]");
218 if (*spbuf
== INTRCH
)
221 printf(" TOP LOW-SPEED WARPISTS\r\n\n");
222 printf("RANK WHO AKA SCORE DIFF CUMDIFF WHEN\r\n");
223 page(LSCOREBOARD
,true);
224 printf(" [Type anything to continue]");
227 if (*spbuf
== INTRCH
)
230 printf(" TOP FUNNY WARPISTS\r\n\n");
231 printf("RANK WHO AKA SCORE DIFF CUMDIFF WHEN\r\n");
232 page(FSCOREBOARD
,true);
233 printf(" [Type anything to continue]");
236 if (*spbuf
== INTRCH
)
239 printf(" GAMES SAVED OR IN PROGRESS\r\n\n");
240 printf("WHO SCORE DF CDF E B WV FLAGS\r\n");
242 snprintf(spbuf
, sizeof(spbuf
), "/bin/cat %ssave.*",SAVEDIR
);
243 execl("/bin/sh", "sh", "-c", spbuf
, NULL
);
252 static const char *status_names
[] = {"Impl", "Warp", "Base", "****" };
254 if (oldstatus
!= status
) {
255 snprintf(spbuf
, sizeof(spbuf
), "%-4s",status_names
[status
]);
256 mvaddstr(0,0, spbuf
);
260 if (ent
->energy
!= oldeenergy
) {
261 oldeenergy
= ent
->energy
;
262 snprintf(spbuf
, sizeof(spbuf
), "%4ld",oldeenergy
);
263 mvaddstr(0,8, spbuf
);
265 if (etorp
!= oldetorp
) {
266 snprintf(spbuf
, sizeof(spbuf
), "%2d",etorp
);
267 mvaddstr(0,13, spbuf
);
274 mvaddstr(0,8,"*******");
279 if (base
->energy
!= oldbenergy
) {
280 oldbenergy
= base
->energy
;
281 snprintf(spbuf
, sizeof(spbuf
), "%5ld",oldbenergy
);
282 mvaddstr(0,19, spbuf
);
284 if (btorp
!= oldbtorp
) {
285 snprintf(spbuf
, sizeof(spbuf
), "%3d",btorp
);
286 mvaddstr(0,25, spbuf
);
293 mvaddstr(0,19,"*********");
298 mvaddstr(0,42,"*** ");
299 if (damage
> 1 || !damflag
[dam
]) {
301 if (++dam
== MAXDAMAGE
)
303 } while (!damflag
[dam
]);
305 if (!--damflag
[dam
]) {
308 snprintf(spbuf
, sizeof(spbuf
), "%s OK *** ",dammess
[dam
]);
310 mvaddstr(0,46,spbuf
);
312 else if (dam
== NOSHIELDS
) {
314 tmp
= (34 - damflag
[dam
]) * 3 - rand_mod(3);
317 snprintf(spbuf
, sizeof(spbuf
), "%d%% %s *** ",tmp
,dammess
[dam
]);
319 mvaddstr(0,46,spbuf
);
321 else if (dam
!= lastdam
|| !olddamage
) {
323 snprintf(spbuf
, sizeof(spbuf
), "NO %s *** ",dammess
[dam
]);
325 mvaddstr(0,46,spbuf
);
328 if (dam
== NOIMPULSE
&& !entmode
)
329 status
= entmode
= 1;
330 if (dam
== NOWARP
&& entmode
)
331 status
= entmode
= 0;
333 tmp
= damflag
[dam
] * damage
;
334 snprintf(spbuf
, sizeof(spbuf
), "%3d.%1d ETR",tmp
/10,tmp%10
);
335 mvaddstr(0,69,spbuf
);
340 snprintf(spbuf
, sizeof(spbuf
), "Stars: %-3d Stardate",numstars
);
341 mvaddstr(0,42,spbuf
);
346 else if (numstars
!= oldstrs
) {
347 snprintf(spbuf
, sizeof(spbuf
), "%-3d",numstars
);
348 mvaddstr(0,49, spbuf
);
352 if (numenemies
!= oldenemies
) {
353 snprintf(spbuf
, sizeof(spbuf
), "%-3d",numenemies
);
354 mvaddstr(0,38, spbuf
);
355 oldenemies
= numenemies
;
357 if ((tmp
= timer%10
) != 0) {
358 snprintf(spbuf
, sizeof(spbuf
), "%1d",tmp
);
359 mvaddstr(0,67, spbuf
);
362 snprintf(spbuf
, sizeof(spbuf
), "%5d.%1d",timer
/10+smarts
*100,tmp
);
363 mvaddstr(0,61, spbuf
);
365 if ((!damage
|| !damflag
[dam
]) && curscore
!= oldcurscore
) {
366 snprintf(spbuf
, sizeof(spbuf
), "%9ld",curscore
);
367 mvaddstr(0,69, spbuf
);
368 oldcurscore
= curscore
;
375 double power
, effectscore
, starscore
, pi_over_2
;
382 if (curscore
> possiblescore
)
383 curscore
= possiblescore
;
384 pi_over_2
= 3.14159265 / 2.0;
385 power
= pow((double)inumenemies
+ /* total number of enemies */
386 inumroms
*2+ /* count roms 3 times */
387 inumgorns
+ /* count gorns 2 times */
388 inumthols
+ /* count thols 2 times */
389 inumapollos
*4+ /* count apollo 5 times */
390 inumcrushes
*3+ /* count crushers 4 times */
391 inumamoebas
*5 /* count amoebas 6 times */
392 , 0.50) * /* skew it a little */
393 (double)smarts
; /* average energy and intelligence */
394 if (inumstars
< 350 && inumenemies
> 5)
395 power
+= (350.0 - (double)inumstars
) * ((double)inumenemies
- 5.0);
396 if (inumstars
> 850 && inumenemies
> 2)
397 power
+= ((double)inumstars
- 850.0) * ((double)inumenemies
- 2.0);
398 effectscore
= ((double)curscore
/ possiblescore
) *
399 atan2(power
, (double) timer
+ 1.0) / pi_over_2
;
401 starscore
= (double) numstars
/ (double) inumstars
;
405 snprintf(spbuf
, sizeof(spbuf
), "Wave = %d, Difficulty = %d, cumulative difficulty = %d",
406 wave
, smarts
, cumsmarts
);
407 mvaddstr(1, 13+(smarts
<10), spbuf
);
408 mvaddstr( 4, 68, " BONUS");
409 snprintf(spbuf
, sizeof(spbuf
), "Efficiency rating: %1.8f (diff=%0.2f,time=%d)",
410 effectscore
, power
, timer
+ 1);
411 mvaddstr( 5,5, spbuf
);
412 if (effectscore
< 0.8)
415 bonuses
= tmp
= (long) ((effectscore
-0.8) * smarts
* 1000);
416 snprintf(spbuf
, sizeof(spbuf
), "%6ld", tmp
);
417 mvaddstr( 5, 68, spbuf
);
418 snprintf(spbuf
, sizeof(spbuf
), "Star save ratio: %1.8f (%d/%d)",
419 starscore
, numstars
, inumstars
);
420 mvaddstr( 6,5, spbuf
);
421 bonuses
+= tmp
= (long) (((double)curscore
/ possiblescore
) *
422 (starscore
*starscore
) * smarts
* 20);
423 snprintf(spbuf
, sizeof(spbuf
), "%6ld", tmp
);
424 mvaddstr( 6, 68, spbuf
);
426 if (inuminhab
!= numinhab
) {
427 snprintf(spbuf
, sizeof(spbuf
), "Inhabited stars depopulated: %5d", inuminhab
-numinhab
);
428 mvaddstr(row
,5, spbuf
);
429 bonuses
+= tmp
= (long) (inuminhab
-numinhab
) * -500;
430 snprintf(spbuf
, sizeof(spbuf
), "%6ld", tmp
);
431 mvaddstr(row
, 68, spbuf
);
434 if (inumfriends
!= numfriends
) {
435 snprintf(spbuf
, sizeof(spbuf
), "Friendly craft destroyed: %5d",
436 inumfriends
-numfriends
);
437 mvaddstr(row
,5, spbuf
);
438 bonuses
+= tmp
= (long) (inumfriends
-numfriends
) * -250;
439 snprintf(spbuf
, sizeof(spbuf
), "%6ld", tmp
);
440 mvaddstr(row
, 68, spbuf
);
444 mvaddstr(row
,5,"For destroying Harry Mudd:");
445 bonuses
+= tmp
= (long) rand_mod(deadmudds
* 20 + 1) - deadmudds
*10;
446 snprintf(spbuf
, sizeof(spbuf
), "%6ld", tmp
);
447 mvaddstr(row
, 68, spbuf
);
451 mvaddstr(row
,5, "For running away from reality:");
452 bonuses
+= tmp
= (long) -possiblescore
/2;
453 snprintf(spbuf
, sizeof(spbuf
), "%6ld", tmp
);
454 mvaddstr(row
, 68, spbuf
);
459 snprintf(spbuf
, sizeof(spbuf
), "Enterprise: %-9s%5d remaining",
460 !ient
?"":ent
?"saved":"destroyed", numents
);
461 mvaddstr(row
,5, spbuf
);
462 bonuses
+= tmp
= ent
&& !bombed_out
? (smarts
+1)*15 : 0;
463 snprintf(spbuf
, sizeof(spbuf
), "%6ld", tmp
);
464 mvaddstr(row
, 68, spbuf
);
466 snprintf(spbuf
, sizeof(spbuf
), "Base: %-9s %5d remaining",
467 !ibase
?"":base
?"saved":"destroyed", numbases
);
468 mvaddstr(row
,5, spbuf
);
469 bonuses
+= tmp
= base
&& !bombed_out
? (smarts
+1)*10 : 0;
470 snprintf(spbuf
, sizeof(spbuf
), "%6ld", tmp
);
471 mvaddstr(row
, 68, spbuf
);
473 mvaddstr(13+(row
>11),19, "(Special games count only a tenth as much)");
477 snprintf(spbuf
, sizeof(spbuf
), "Previous point total:%10ld",lastscore
);
478 mvaddstr(15,24, spbuf
);
479 snprintf(spbuf
, sizeof(spbuf
), "Points this round: %10ld",curscore
);
480 mvaddstr(16,24, spbuf
);
481 snprintf(spbuf
, sizeof(spbuf
), "Bonuses: %10ld",bonuses
);
482 mvaddstr(17,24, spbuf
);
483 totalscore
= lastscore
+ curscore
+ bonuses
;
484 snprintf(spbuf
, sizeof(spbuf
), "New point total: %10ld",totalscore
);
485 mvaddstr(18,24, spbuf
);
486 if (lastscore
/ ENTBOUNDARY
< totalscore
/ ENTBOUNDARY
) {
487 mvaddstr(row
-1,42,"+ 1 new");
490 else if (numents
>0 &&
491 lastscore
/ ENTBOUNDARY
> totalscore
/ ENTBOUNDARY
) {
492 mvaddstr(row
-1,42,"- 1 obsolete");
495 if (lastscore
/ BASEBOUNDARY
< totalscore
/ BASEBOUNDARY
) {
496 mvaddstr(row
,42,"+ 1 new");
499 else if (numbases
>0 &&
500 lastscore
/ BASEBOUNDARY
> totalscore
/ BASEBOUNDARY
) {
501 mvaddstr(row
,42,"- 1 obsolete");
504 if (starscore
< 0.8 && inumstars
> 200 && numstars
> 50) {
505 snprintf(spbuf
, sizeof(spbuf
), "smap.%d",rand_mod(MAPS
-PERMMAPS
)+PERMMAPS
);
506 if ((mapfp
= fopen(spbuf
, "w")) != NULL
) {
509 fprintf(mapfp
,"%d\n",numstars
);
510 for (obj
= root
.next
; obj
!= &root
; obj
= obj
->next
) {
511 if (obj
->type
== Star
) {
512 fprintf(mapfp
,"%d %d\n",obj
->posy
,obj
->posx
);
523 char tmp
, *retval
, cdate
[30];
528 const char *scoreboard
;
530 for (i
=0; link(LOGFILE
, LOCKFILE
) == -1 && i
<10; i
++)
532 nowtime
= time(NULL
);
533 strcpy(cdate
,ctime(&nowtime
));
534 if ((logfd
= fopen(LOGFILE
,"a")) != NULL
) {
536 "%-24s%-9s%7ld%c%2d %4d %s",
537 realname
, logname
, totalscore
, c
,smarts
, cumsmarts
, cdate
);
540 strcpy(cdate
+11,cdate
+20);
542 scoreboard
= FSCOREBOARD
;
544 scoreboard
= LSCOREBOARD
;
546 scoreboard
= SCOREBOARD
;
547 if (eaccess(scoreboard
,0)) {
548 if ((logfd
= fopen(scoreboard
,"w")) != NULL
)
551 if ((logfd
= fopen(scoreboard
,"r")) != NULL
&&
552 (outfd
= fopen(TMPSCOREBOARD
,"w")) != NULL
) {
553 for (i
=0; i
<20; i
++) {
554 if ((retval
= fgets(buf
, 100, logfd
)) == NULL
)
556 if (atol(buf
+32) < totalscore
)
558 if (strnEQ(buf
+COMPOFF
,COMPNAME
,COMPLEN
)) {
562 fprintf(outfd
, "%s", buf
);
565 mvaddstr(20,21, "You did not better your previous score");
567 unlink(TMPSCOREBOARD
);
570 fprintf(outfd
, "%-24s%-8s%8ld%c %2d %4d %s",
571 realname
, logname
, totalscore
, c
,smarts
, cumsmarts
, cdate
);
573 snprintf(spbuf
, sizeof(spbuf
), " Congratulations--you've placed %d%s",
574 i
, i
==1?"st":(i
==2?"nd":(i
==3?"rd":"th")));
575 if (retval
!= NULL
) {
576 if (strnNE(buf
+COMPOFF
,COMPNAME
,COMPLEN
)) {
577 fprintf(outfd
, "%s", buf
);
581 strcpy(spbuf
, "Congratulations--you've bettered your score");
583 if (fgets(buf
, 100, logfd
) == NULL
)
585 if (strnNE(buf
+COMPOFF
,COMPNAME
,COMPLEN
)) {
586 fprintf(outfd
, "%s", buf
);
591 mvaddstr(20,19, spbuf
);
594 while (unlink(scoreboard
) == 0)
596 link(TMPSCOREBOARD
,scoreboard
);
597 unlink(TMPSCOREBOARD
);
598 logfd
= fopen(scoreboard
,"r");
601 mvaddstr(20,22,"You did not place within the top 20");
606 snprintf(spbuf
, sizeof(spbuf
), "(Cannot access %s file, error %d)",
607 (logfd
==NULL
?"log":"tmp"),errno
);
608 mvaddstr(20,22,spbuf
);
613 "[Hit space for scoreboard, 'r' for new game, 'q' to quit]");
619 } while (tmp
!= INTRCH
&& tmp
!= BREAKCH
&& !strchr(" rqQ",tmp
));
620 if (strchr("qQr",tmp
)) {
621 justonemoretime
= (tmp
== 'r');
630 mvaddstr(0,31,"TOP FUNNY WARPISTS");
632 mvaddstr(0,29,"TOP LOW-SPEED WARPISTS");
634 mvaddstr(0,33,"TOP WARPISTS");
635 mvaddstr(2,0,"RANK WHO AKA SCORE DIFF CUMDIFF WHEN");
636 for (i
=1; i
<=20; i
++) {
637 if (fgets(buf
, 100, logfd
) == NULL
)
639 buf
[strlen(buf
)-1] = '\0';
640 snprintf(spbuf
, sizeof(spbuf
), " %2d %s", i
, buf
);
641 mvaddstr(i
+2,0, spbuf
);
646 mvaddstr(23,25,"Would you like to play again?");
650 } while (tmp
!= INTRCH
&& tmp
!= BREAKCH
&& !strchr("nNyY \n\r",tmp
));
651 if (tmp
== 'n' || tmp
== 'N' || tmp
== INTRCH
|| tmp
== BREAKCH
)
652 justonemoretime
= false;
656 totalscore
= cumsmarts
= wave
= 0;
663 massacre
= (ismarts
>= 40);
675 if ((savfil
= fopen(savefilename
,"w")) == NULL
) {
678 printf("Cannot save game in %s (%s)\r\n", savefilename
, strerror(e
));
681 fprintf(savfil
, "%-8s %10ld, %2d,%5d,%2d,%2d,%3d %c%c%c%c%c%c%c%c%c\n",
682 logname
, totalscore
, smarts
, cumsmarts
, numents
, numbases
, wave
,
683 apolspec
? 'a' : ' ',
684 beginner
? 'b' : ' ',
685 crushspec
? 'c' : ' ',
686 gornspec
? 'g' : ' ',
687 massacre
? 'm' : ' ',
689 tholspec
? 't' : ' ',
690 lowspeed
? 'l' : ' ',
691 amoebaspec
? '&' : ' '