Add high score keeping, add title screen scores display. Also fix
authorMatthew Mondor <mmondor@pulsar-zone.net>
Thu, 13 Apr 2023 23:36:18 +0000 (23:36 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Thu, 13 Apr 2023 23:36:18 +0000 (23:36 +0000)
a memory leak where pads were temporarily and unnecessarily created
everytime the title screen was shown.  The game may also leak other
memory, to eventually investigate.

nInvaders.c
nInvaders.h
view.c

index d59b387..bc45701 100644 (file)
 #define FPS 50
 
 int lives;
-long score;
+long score = 0, hiscore = 0;
 int status; // status handled in timer
 int weite;
-int level;
+int level = 0, hilevel = 0;
 int skill_level;
 
 #define GAME_LOOP 1
@@ -99,6 +99,25 @@ static void evaluateCommandLine(int argc, char **argv)
        argv += optind;
 }
 
+const char *rating(int score)
+{
+
+       if(score<5000)
+               return "Alien Fodder";
+       else if(score<7500)
+               return "Easy Target";
+       else if(score<10000)
+               return "Barely Mediocre";
+       else if(score<12500)
+               return "Shows Promise";
+       else if(score<15000)
+               return "Alien Blaster";
+       else if(score<20000)
+               return "Earth Defender";
+       else if(score>19999)
+               return "Supreme Protector";
+       else return "Unknown";
+}
 
 static void finish(int sig)
 {
@@ -109,25 +128,15 @@ static void finish(int sig)
        fprintf(stderr,"\n");
        fprintf(stderr,"=========================================================================\n");
        fprintf(stderr,"\n");
-       
-       fprintf(stderr,"Final score: %7.7ld, Final level: %2.2d\nFinal rating... ",score,level);
+
+       if (hiscore > 0)
+               fprintf(stderr, " High score: %7.7ld, Level: %2.2d, Rating: %s\n",
+                   hiscore, hilevel, rating(hiscore));
+       fprintf(stderr, "Final score: %7.7ld, Final level: %2.2d\nFinal rating... ",score,level);
        if (lives>0)
-               fprintf(stderr,"Quitter\n\n");
-       else if(score<5000)
-               fprintf(stderr,"Alien Fodder\n\n");
-       else if(score<7500)
-               fprintf(stderr,"Easy Target\n\n");
-       else if(score<10000)
-               fprintf(stderr,"Barely Mediocre\n\n");
-       else if(score<12500)
-               fprintf(stderr,"Shows Promise\n\n");
-       else if(score<15000)
-               fprintf(stderr,"Alien Blaster\n\n");
-       else if(score<20000)
-               fprintf(stderr,"Earth Defender\n\n");
-       else if(score>19999)
-               fprintf(stderr,"Supreme Protector\n\n");
-       
+               fprintf(stderr,"Quitter, ");
+       fprintf(stderr, "%s\n\n", rating(score));
+
        showVersion();
         exit(0);
 }
index 0c7f6e2..7400d98 100644 (file)
@@ -49,9 +49,12 @@ void readInput(void);
 void handleTimer(int);
 void setUpTimer(void);
 
+extern const char *rating(int);
+
 // todo: let's try to not having to declare these "public"
 extern int weite;
-extern int level;
+extern long score, hiscore;
+extern int level, hilevel;
 extern int skill_level;
 
 // included from globals.h
diff --git a/view.c b/view.c
index 7e5b2f9..c617c4d 100644 (file)
--- a/view.c
+++ b/view.c
@@ -24,6 +24,7 @@
 
 #include "view.h"
 #include "globals.h"
+#include "nInvaders.h"
 #include <stdbool.h>
 #include <stdlib.h>    
 #include <unistd.h>
@@ -425,7 +426,7 @@ void titleScreenDisplay(void)
                {",^,", "_O-", "-o-",  "o=o", "<O>", "_x_", "*^*", "\\_/", "o o"},
                {".-.", "-O_", "/o\\", "o-o", "<o>", "-x-", "o^o", "/~\\", "oo "}
        };
-       int score[3] = {200, 150, 100};
+       int scores[3] = {200, 150, 100};
        int colors[9] = {RED, GREEN, BLUE, RED, GREEN, BLUE, RED, GREEN, BLUE};
        char buffer[12];
        static int alien_type = 0;
@@ -453,7 +454,7 @@ void titleScreenDisplay(void)
        }
        for (i = alien_type; i < alien_type + 3; i++) {
                waddstr(wAliens, "           ");
-               snprintf(buffer, sizeof(buffer), "%s   = %d", aliens[frame % 2][i], score[i % 3]);
+               snprintf(buffer, sizeof(buffer), "%s   = %d", aliens[frame % 2][i], scores[i % 3]);
                wattrset(wAliens, COLOR_PAIR(colors[i]));
                waddstr(wAliens, buffer);
        }
@@ -474,10 +475,43 @@ void titleScreenDisplay(void)
        x = (SCREENWIDTH / 2) - (20 / 2);
        y = SCREENHEIGHT - 2;
        copywin(wStartText, wTitleScreen, 0, 0, y, x, y, x + 19, 0);
-       
+
+       /*
+        * A bit ridiculous to use pads or everything, especially when leaking
+        * them after each game to display the title screen again.  Let's not
+        * follow the routine, then let's actually add delwin(3) below for the
+        * above pads that used to leak.
+        */
+       if (score != 0) {
+               wattrset(wTitleScreen, COLOR_PAIR(BLUE));
+               mvwprintw(wTitleScreen, SCREENHEIGHT - 7, 12,
+                   "Last score: %7.7ld", score);
+               mvwprintw(wTitleScreen, SCREENHEIGHT - 6, 12,
+                   "Last level: %2.2d", level);
+               mvwprintw(wTitleScreen, SCREENHEIGHT - 5, 16,
+                   "Rating: %s", rating(score));
+
+               if (score > hiscore) {
+                       hiscore = score;
+                       hilevel = level;
+               }
+       }
+       if (hiscore != 0) {
+               wattrset(wTitleScreen, COLOR_PAIR(MAGENTA));
+               mvwprintw(wTitleScreen, SCREENHEIGHT - 7, SCREENWIDTH - 31,
+                   "High score: %7.7ld", hiscore);
+               mvwprintw(wTitleScreen, SCREENHEIGHT - 6, SCREENWIDTH - 29,
+                   "At level: %2.2d", hilevel);
+               mvwprintw(wTitleScreen, SCREENHEIGHT - 5, SCREENWIDTH - 27,
+                   "Rating: %s", rating(hiscore));
+       }
+
        copywin(wTitleScreen, wBattleField, 0, 0, 0, 0, SCREENHEIGHT-1, SCREENWIDTH-1, 0);
-       
        wrefresh(wBattleField);
+
+       delwin(wStartText);
+       delwin(wAliens);
+       delwin(wTitleText);
 }