Rockbox.org home
release
dev builds
extras
themes manual
wiki
device status forums
mailing lists
IRC bugs
patches
dev guide
translations



Rockbox mail archive

Subject: [patch] wormlet.c

[patch] wormlet.c

From: perterm <perterm_at_vce.de>
Date: Thu, 29 Aug 2002 11:11:07 +0200

bugs fixed:
    - always showed "Argh! I'm poisoned" instead of current worm state
    - while growing the worm could bite it's invisible tail
    - invisible tail could erase a trace through new
      generated args /foods

new feature:
    - highscore
    - reduced many int to short -> hopefully saves a few bytes


Phil


? wormlet.h_
? wormlet.c_
? diff.txt
? patchfile
Index: wormlet.c
===================================================================
RCS file: /cvsroot/rockbox/apps/recorder/wormlet.c,v
retrieving revision 1.2
diff -u -b -r1.2 wormlet.c
--- wormlet.c 26 Aug 2002 09:21:59 -0000 1.2
+++ wormlet.c 29 Aug 2002 07:50:33 -0000
_at__at_ -33,12 +33,13 _at__at_
 static short wormx[MAX_WORM_LENGTH];
 static short wormy[MAX_WORM_LENGTH];
 
-static int head; /* index of the head within the buffer */
+static short head; /* index of the head within the buffer */
 static short headx;
 static short heady;
 
-static int tail; /* index of the tail within the buffer */
-static int growing; /* number of cyles the worm still keeps growing */
+static short tail; /* index of the tail within the buffer */
+static short growing; /* number of cyles the worm still keeps growing */
+static short highscore;
 
 
 #define MAX_FOOD 5 /* maximal number of food items */
_at__at_ -51,13 +52,13 _at__at_
 #define ARGHS_PER_FOOD 2 /* number of arghs produced per eaten food */
 static short arghx[MAX_ARGH];
 static short arghy[MAX_ARGH];
-static int arghCount;
+static short arghCount;
 
 /* direction vector in which the worm moves */
 static short dirx; /* only values -1 0 1 allowed */
 static short diry; /* only values -1 0 1 allowed */
 
-static int speed = 10;
+static short speed = 10;
 
 /* return values of checkCollision */
 #define COLLISION_NONE 0
_at__at_ -74,11 +75,11 _at__at_
 
 /**
  * Returns the current length of the worm.
- * _at_return int a positive value
+ * _at_return short a positive value
  */
-static int getWormLength(void) {
+static short getWormLength(void) {
     /* initial simple calculation will be overwritten if wrong. */
- int retVal = head - tail;
+ short retVal = head - tail;
 
     /* if the worm 'crosses' the boundaries of the ringbuffer */
     if (retVal < 0) {
_at__at_ -91,13 +92,13 _at__at_
 /**
  * Checks wether a specific food in the food arrays is at the
  * specified coordinates.
- * _at_param int foodIndex The index of the food in the food arrays
- * _at_param int x the x coordinate.
- * _at_param int y the y coordinate.
+ * _at_param short foodIndex The index of the food in the food arrays
+ * _at_param short x the x coordinate.
+ * _at_param short y the y coordinate.
  * _at_return Returns true if the coordinate hits the food specified by
  * foodIndex.
  */
-static bool specificFoodCollision(int foodIndex, int x, int y) {
+static bool specificFoodCollision(short foodIndex, short x, short y) {
     bool retVal = false;
     if (x >= foodx[foodIndex] &&
         x < foodx[foodIndex] + FOOD_SIZE &&
_at__at_ -113,11 +114,11 _at__at_
  * Returns the index of the food that is at the
  * given coordinates. If no food is at the coordinates
  * -1 is returned.
- * _at_return int -1 <= value < MAX_FOOD
+ * _at_return short -1 <= value < MAX_FOOD
  */
-static int foodCollision(int x, int y) {
- int i = 0;
- int retVal = -1;
+static short foodCollision(short x, short y) {
+ short i = 0;
+ short retVal = -1;
     for (i = 0; i < MAX_FOOD; i++) {
         if (specificFoodCollision(i, x, y)) {
             retVal = i;
_at__at_ -130,13 +131,13 _at__at_
 /**
  * Checks wether a specific argh in the argh arrays is at the
  * specified coordinates.
- * _at_param int arghIndex The index of the argh in the argh arrays
- * _at_param int x the x coordinate.
- * _at_param int y the y coordinate.
+ * _at_param short arghIndex The index of the argh in the argh arrays
+ * _at_param short x the x coordinate.
+ * _at_param short y the y coordinate.
  * _at_return Returns true if the coordinate hits the argh specified by
  * arghIndex.
  */
-static bool specificArghCollision(int arghIndex, int x, int y) {
+static bool specificArghCollision(short arghIndex, short x, short y) {
 
     if ( x >= arghx[arghIndex] &&
          y >= arghy[arghIndex] &&
_at__at_ -153,13 +154,13 _at__at_
  * Returns the index of the argh that is at the
  * given coordinates. If no argh is at the coordinates
  * -1 is returned.
- * _at_param int x The x coordinate.
- * _at_param int y The y coordinate.
- * _at_return int -1 <= value < arghCount <= MAX_ARGH
- */
-static int arghCollision(int x, int y) {
- int i = 0;
- int retVal = -1;
+ * _at_param short x The x coordinate.
+ * _at_param short y The y coordinate.
+ * _at_return short -1 <= value < arghCount <= MAX_ARGH
+ */
+static short arghCollision(short x, short y) {
+ short i = 0;
+ short retVal = -1;
 
     /* search for the argh that has the specified coords */
     for (i = 0; i < arghCount; i++) {
_at__at_ -173,24 +174,24 _at__at_
 
 /**
  * Checks wether the worm collides with the food at the specfied food-arrays.
- * _at_param int foodIndex The index of the food in the arrays. Ensure the value is
+ * _at_param short foodIndex The index of the food in the arrays. Ensure the value is
  * 0 <= foodIndex <= MAX_FOOD
  * _at_return Returns true if the worm collides with the specified food.
  */
-static bool wormFoodCollision(int foodIndex)
+static bool wormFoodCollision(short foodIndex)
 {
     bool retVal = false;
 
     /* buffer wormLength because getWormLength is expensive */
- int wormLength = getWormLength();
- int i;
+ short wormLength = getWormLength();
+ short i;
 
     /* although all the worm gets iterated i is NOT
        the index of the worm arrays */
     for (i = 0; i < wormLength; i++) {
 
         /* convert i to the true worm indices */
- int wormIndex = (tail + i) % MAX_WORM_LENGTH;
+ short wormIndex = (tail + i) % MAX_WORM_LENGTH;
         if (specificFoodCollision(foodIndex,
                                   wormx[wormIndex],
                                   wormy[wormIndex])) {
_at__at_ -204,24 +205,24 _at__at_
 
 /**
  * Checks wether the worm collides with the argh at the specfied argh-arrays.
- * _at_param int arghIndex The index of the argh in the arrays.
+ * _at_param short arghIndex The index of the argh in the arrays.
  * Ensure the value is 0 <= arghIndex < arghCount <= MAX_ARGH
  * _at_return Returns true if the worm collides with the specified argh.
  */
-static bool wormArghCollision(int arghIndex)
+static bool wormArghCollision(short arghIndex)
 {
     bool retVal = false;
 
     /* buffer wormLength because getWormLength is expensive */
- int wormLength = getWormLength();
- int i;
+ short wormLength = getWormLength();
+ short i;
 
     /* although all the worm gets iterated i is NOT
        the index of the worm arrays */
     for (i = 0; i < wormLength; i++) {
 
         /* convert i to the true worm indices */
- int wormIndex = (tail + i) % MAX_WORM_LENGTH;
+ short wormIndex = (tail + i) % MAX_WORM_LENGTH;
         if (specificArghCollision(arghIndex,
                                   wormx[wormIndex],
                                   wormy[wormIndex])) {
_at__at_ -236,13 +237,13 _at__at_
 /**
  * Find new coordinates for the food stored in foodx[index], foody[index]
  * that don't collide with any other food or argh
- * _at_param int index
+ * _at_param short index
  * Ensure that 0 <= index < MAX_FOOD.
  */
-static void makeFood(int index) {
+static void makeFood(short index) {
 
- int x = 0;
- int y = 0;
+ short x = 0;
+ short y = 0;
     bool collisionDetected = false;
 
     do {
_at__at_ -278,11 +279,11 _at__at_
 
 /**
  * Clears a food from the lcd buffer.
- * _at_param int index The index of the food arrays under which
+ * _at_param short index The index of the food arrays under which
  * the coordinates of the desired food can be found. Ensure
  * that the value is 0 <= index <= MAX_FOOD.
  */
-static void clearFood(int index)
+static void clearFood(short index)
 {
     /* remove the old food from the screen */
     lcd_clearrect(foodx[index] + FIELD_RECT_X,
_at__at_ -292,11 +293,11 _at__at_
 
 /**
  * Draws a food in the lcd buffer.
- * _at_param int index The index of the food arrays under which
+ * _at_param short index The index of the food arrays under which
  * the coordinates of the desired food can be found. Ensure
  * that the value is 0 <= index <= MAX_FOOD.
  */
-static void drawFood(int index)
+static void drawFood(short index)
 {
     /* draw the food object */
     lcd_fillrect(foodx[index] + FIELD_RECT_X,
_at__at_ -310,13 +311,13 _at__at_
 /**
  * Find new coordinates for the argh stored in arghx[index], arghy[index]
  * that don't collide with any other food or argh.
- * _at_param int index
+ * _at_param short index
  * Ensure that 0 <= index < arghCount < MAX_ARGH.
  */
-static void makeArgh(int index)
+static void makeArgh(short index)
 {
- int x;
- int y;
+ short x;
+ short y;
     bool collisionDetected = false;
 
     do {
_at__at_ -351,11 +352,11 _at__at_
 
 /**
  * Draws an argh in the lcd buffer.
- * _at_param int index The index of the argh arrays under which
+ * _at_param short index The index of the argh arrays under which
  * the coordinates of the desired argh can be found. Ensure
  * that the value is 0 <= index < arghCount <= MAX_ARGH.
  */
-static void drawArgh(int index)
+static void drawArgh(short index)
 {
     /* draw the new argh */
     lcd_fillrect(arghx[index] + FIELD_RECT_X,
_at__at_ -369,7 +370,7 _at__at_
  */
 static void initWormlet(void)
 {
- int i;
+ short i;
 
     /* Initialize all the worm coordinates to 0,0. */
     memset(wormx,0,sizeof wormx);
_at__at_ -467,25 +468,25 _at__at_
 
 /**
  * Checks wether the coordinate is part of the worm.
- * _at_param x int The x coordinate
- * _at_param y int The y coordinate
- * _at_return int The index of the worm arrays that contain x, y.
+ * _at_param x short The x coordinate
+ * _at_param y short The y coordinate
+ * _at_return short The index of the worm arrays that contain x, y.
  * Returns -1 if the coordinates are not part of the worm.
  */
-static int wormCollision(int x, int y)
+static short wormCollision(short x, short y)
 {
- int retVal = -1;
+ short retVal = -1;
 
     /* getWormLength is expensive -> buffer the value */
- int wormLength = getWormLength();
- int i;
+ short wormLength = getWormLength();
+ short i;
 
     /* test each entry that is part of the worm */
     for (i = 0; i < wormLength && retVal == -1; i++) {
 
         /* The iteration iterates the length of the worm.
            Here's the conversion to the true indices within the worm arrays. */
- int trueIndex = (tail + i) % MAX_WORM_LENGTH;
+ short trueIndex = (tail + i) % MAX_WORM_LENGTH;
         if (wormx[trueIndex] == x && wormy[trueIndex] == y)
             retVal = trueIndex;
     }
_at__at_ -513,16 +514,16 _at__at_
 /**
  * Checks and returns wether the head of the worm
  * is colliding with something currently.
- * _at_return int One of the values:
+ * _at_return short One of the values:
  * COLLISION_NONE
  * COLLISION_WORM
  * COLLISION_FOOD
  * COLLISION_ARGH
  * COLLISION_FIELD
  */
-static int checkCollision(void)
+static short checkCollision(void)
 {
- int retVal = COLLISION_NONE;
+ short retVal = COLLISION_NONE;
 
     if (wormCollision(headx, heady) >= 0)
         retVal = COLLISION_WORM;
_at__at_ -530,7 +531,7 _at__at_
     if (foodCollision(headx, heady) >= 0)
         retVal = COLLISION_FOOD;
 
- if (arghCollision(headx, heady))
+ if (arghCollision(headx, heady) >= 0)
         retVal = COLLISION_ARGH;
 
     if (fieldCollision())
_at__at_ -547,48 +548,62 _at__at_
 {
     char buf[15];
     char buf2[15];
+ short y = 0;
+ short score = getWormLength() - growing;
 
- /* Title */
- snprintf(buf, sizeof (buf), "Wormlet");
- lcd_putsxy(FIELD_RECT_WIDTH + 3, 0, buf, 0);
+ lcd_clearrect(FIELD_RECT_WIDTH + FIELD_RECT_X + 1, 0,
+ LCD_WIDTH - (FIELD_RECT_WIDTH + FIELD_RECT_X + 1),
+ LCD_HEIGHT);
 
     /* length */
- snprintf(buf, sizeof (buf), "length:");
- lcd_putsxy(FIELD_RECT_WIDTH + 3, 12, buf, 0);
- snprintf(buf, sizeof (buf), "%d ", getWormLength() - growing);
- lcd_putsxy(FIELD_RECT_WIDTH + 3, 20, buf, 0);
+ snprintf(buf, sizeof (buf), "Length");
+ lcd_putsxy(FIELD_RECT_WIDTH + 3, y, buf, 0);
+ snprintf(buf, sizeof (buf), "%d ", score);
+ lcd_putsxy(FIELD_RECT_WIDTH + 3, y += 8, buf, 0);
 
     switch (checkCollision()) {
         case COLLISION_NONE:
- snprintf(buf, sizeof(buf), "I'm hungry! ");
- if (growing > 0)
+ snprintf(buf, sizeof(buf), "I'm hungry");
+ if (growing > 0){
                 snprintf(buf2, sizeof(buf2), "growing");
- else
+ }
+ else{
                 snprintf(buf2, sizeof(buf2), " ");
+ }
             break;
 
         case COLLISION_WORM:
             snprintf(buf, sizeof(buf), "Ouch! I bit");
- snprintf(buf2, sizeof(buf2), "myself! ");
+ snprintf(buf2, sizeof(buf2), "myself!");
             break;
 
         case COLLISION_FOOD:
- snprintf(buf, sizeof(buf), "Yummy! ");
+ snprintf(buf, sizeof(buf), "Yummy!");
             snprintf(buf2, sizeof(buf2), "growing");
             break;
 
         case COLLISION_ARGH:
- snprintf(buf, sizeof(buf), "Argh! I'm ");
- snprintf(buf2, sizeof(buf2), "poisoned! ");
+ snprintf(buf, sizeof(buf), "Argh! I'm");
+ snprintf(buf2, sizeof(buf2), "poisoned!");
             break;
 
         case COLLISION_FIELD:
- snprintf(buf, sizeof(buf), "Boing! ");
- snprintf(buf2, sizeof(buf2), "Headcrash!");
+ snprintf(buf, sizeof(buf), "Boing!");
+ snprintf(buf2, sizeof(buf2), "Headcrash");
             break;
     }
- lcd_putsxy(FIELD_RECT_WIDTH + 3, 32, buf, 0);
- lcd_putsxy(FIELD_RECT_WIDTH + 3, 40, buf2, 0);
+ lcd_putsxy(FIELD_RECT_WIDTH + 3, y += 12, buf, 0);
+ lcd_putsxy(FIELD_RECT_WIDTH + 3, y += 8 , buf2, 0);
+
+ /* high score */
+ if (highscore < score) {
+ highscore = score;
+ }
+ snprintf(buf , sizeof(buf), "Highscore");
+ snprintf(buf2, sizeof(buf2), "%d ", highscore);
+
+ lcd_putsxy(FIELD_RECT_WIDTH + 3, y += 12, buf, 0);
+ lcd_putsxy(FIELD_RECT_WIDTH + 3, y += 8 , buf2, 0);
 }
 
 /**
_at__at_ -600,7 +615,9 _at__at_
 static bool processCollisions(void)
 {
     bool wormDead = false;
- int index = -1;
+ short index = -1;
+ short tailx;
+ short taily;
 
     wormDead = fieldCollision();
 
_at__at_ -609,12 +626,14 _at__at_
         /* check if food was eaten */
         index = foodCollision(headx, heady);
         if (index != -1){
- int i;
+ short i;
             
+ /* make new food */
             clearFood(index);
             makeFood(index);
             drawFood(index);
             
+ /* make new argh */
             for (i = 0; i < ARGHS_PER_FOOD; i++) {
                 arghCount++;
                 if (arghCount > MAX_ARGH)
_at__at_ -623,11 +642,29 _at__at_
                 drawArgh(arghCount - 1);
             }
 
+ /* buffer the current tail position because all new
+ tail segments have to be set to this position */
+ tailx = wormx[tail];
+ taily = wormy[tail];
+
+ /* enlarge the tail of the worm. */
             tail -= WORM_PER_FOOD;
             growing += WORM_PER_FOOD;
+
+ /* correct the tail index as it might have crossed
+ the array boundaries */
             if (tail < 0)
                 tail += MAX_WORM_LENGTH;
 
+ /* Set all the new tail to the current tail position.
+ This avoids that the worm drags an 'invisible tail'
+ while it is growing. */
+ for (i = 0; i < WORM_PER_FOOD; i++) {
+ short wormIndex = (tail + i) % MAX_WORM_LENGTH;
+ wormx[wormIndex] = tailx;
+ wormy[wormIndex] = taily;
+ }
+
             drawWorm();
         }
 
_at__at_ -653,7 +690,7 _at__at_
 static bool run(void)
 {
     int button = 0;
- int wormDead = false;
+ bool wormDead = false;
 
     /* initialize the board and so on */
     initWormlet();
Received on 2002-08-29

Page template was last modified "Tue Sep 7 00:00:02 2021" The Rockbox Crew -- Privacy Policy