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



Rockbox mail archive

Subject: [patch] wormlet.c
From: perterm (perterm_at_vce.de)
Date: 2002-08-29


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
@@ -33,12 +33,13 @@
 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 */
@@ -51,13 +52,13 @@
 #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
@@ -74,11 +75,11 @@
 
 /**
  * Returns the current length of the worm.
- * @return int a positive value
+ * @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) {
@@ -91,13 +92,13 @@
 /**
  * Checks wether a specific food in the food arrays is at the
  * specified coordinates.
- * @param int foodIndex The index of the food in the food arrays
- * @param int x the x coordinate.
- * @param int y the y coordinate.
+ * @param short foodIndex The index of the food in the food arrays
+ * @param short x the x coordinate.
+ * @param short y the y coordinate.
  * @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 &&
@@ -113,11 +114,11 @@
  * Returns the index of the food that is at the
  * given coordinates. If no food is at the coordinates
  * -1 is returned.
- * @return int -1 <= value < MAX_FOOD
+ * @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;
@@ -130,13 +131,13 @@
 /**
  * Checks wether a specific argh in the argh arrays is at the
  * specified coordinates.
- * @param int arghIndex The index of the argh in the argh arrays
- * @param int x the x coordinate.
- * @param int y the y coordinate.
+ * @param short arghIndex The index of the argh in the argh arrays
+ * @param short x the x coordinate.
+ * @param short y the y coordinate.
  * @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] &&
@@ -153,13 +154,13 @@
  * Returns the index of the argh that is at the
  * given coordinates. If no argh is at the coordinates
  * -1 is returned.
- * @param int x The x coordinate.
- * @param int y The y coordinate.
- * @return int -1 <= value < arghCount <= MAX_ARGH
- */
-static int arghCollision(int x, int y) {
- int i = 0;
- int retVal = -1;
+ * @param short x The x coordinate.
+ * @param short y The y coordinate.
+ * @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++) {
@@ -173,24 +174,24 @@
 
 /**
  * Checks wether the worm collides with the food at the specfied food-arrays.
- * @param int foodIndex The index of the food in the arrays. Ensure the value is
+ * @param short foodIndex The index of the food in the arrays. Ensure the value is
  * 0 <= foodIndex <= MAX_FOOD
  * @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])) {
@@ -204,24 +205,24 @@
 
 /**
  * Checks wether the worm collides with the argh at the specfied argh-arrays.
- * @param int arghIndex The index of the argh in the arrays.
+ * @param short arghIndex The index of the argh in the arrays.
  * Ensure the value is 0 <= arghIndex < arghCount <= MAX_ARGH
  * @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])) {
@@ -236,13 +237,13 @@
 /**
  * Find new coordinates for the food stored in foodx[index], foody[index]
  * that don't collide with any other food or argh
- * @param int index
+ * @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 {
@@ -278,11 +279,11 @@
 
 /**
  * Clears a food from the lcd buffer.
- * @param int index The index of the food arrays under which
+ * @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,
@@ -292,11 +293,11 @@
 
 /**
  * Draws a food in the lcd buffer.
- * @param int index The index of the food arrays under which
+ * @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,
@@ -310,13 +311,13 @@
 /**
  * Find new coordinates for the argh stored in arghx[index], arghy[index]
  * that don't collide with any other food or argh.
- * @param int index
+ * @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 {
@@ -351,11 +352,11 @@
 
 /**
  * Draws an argh in the lcd buffer.
- * @param int index The index of the argh arrays under which
+ * @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,
@@ -369,7 +370,7 @@
  */
 static void initWormlet(void)
 {
- int i;
+ short i;
 
     /* Initialize all the worm coordinates to 0,0. */
     memset(wormx,0,sizeof wormx);
@@ -467,25 +468,25 @@
 
 /**
  * Checks wether the coordinate is part of the worm.
- * @param x int The x coordinate
- * @param y int The y coordinate
- * @return int The index of the worm arrays that contain x, y.
+ * @param x short The x coordinate
+ * @param y short The y coordinate
+ * @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;
     }
@@ -513,16 +514,16 @@
 /**
  * Checks and returns wether the head of the worm
  * is colliding with something currently.
- * @return int One of the values:
+ * @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;
@@ -530,7 +531,7 @@
     if (foodCollision(headx, heady) >= 0)
         retVal = COLLISION_FOOD;
 
- if (arghCollision(headx, heady))
+ if (arghCollision(headx, heady) >= 0)
         retVal = COLLISION_ARGH;
 
     if (fieldCollision())
@@ -547,48 +548,62 @@
 {
     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);
 }
 
 /**
@@ -600,7 +615,9 @@
 static bool processCollisions(void)
 {
     bool wormDead = false;
- int index = -1;
+ short index = -1;
+ short tailx;
+ short taily;
 
     wormDead = fieldCollision();
 
@@ -609,12 +626,14 @@
         /* 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)
@@ -623,11 +642,29 @@
                 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();
         }
 
@@ -653,7 +690,7 @@
 static bool run(void)
 {
     int button = 0;
- int wormDead = false;
+ bool wormDead = false;
 
     /* initialize the board and so on */
     initWormlet();



Page was last modified "Jan 10 2012" The Rockbox Crew
aaa