- made TileManager::get() always return a valid tile
authorIngo Ruhnke <grumbel@gmx.de>
Thu, 25 Mar 2004 10:26:59 +0000 (10:26 +0000)
committerIngo Ruhnke <grumbel@gmx.de>
Thu, 25 Mar 2004 10:26:59 +0000 (10:26 +0000)
- made conversion code more robust against unknown tiles
- added next_tile/next_tile2 to replace the old one on some events

SVN-Revision: 356

data/images/tilesets/supertux.stgt
src/gameloop.cpp
src/level.cpp
src/tile.cpp
src/tile.h

index b5e4be4..76d5afb 100644 (file)
@@ -1,3 +1,5 @@
+;; -*- mode: scheme -*-
+
 (supertux-tiles
  (properties 
   (id 0))
@@ -86,6 +88,7 @@
        (images "bonus1.png")
        (solid #t)
        (fullbox #t)
+       (next-tile 84)
        (data 1))
  (tile (id 27)
        (images "block1.png")
 
   (tile (id 77)
         (images "brick0.png")
-       (brick #t)      
+       (brick #t)    
+        (data 1)
+        (next-tile 0)
         (solid #t))
   (tile (id 78)
         (images "brick1.png")
         (brick #t)
+        (data 1)
+        (next-tile 0)
         (solid #t))
 
   (tile (id 79)
         (images "box-full.png")
         (fullbox #t)
         (solid #t) 
+        (next-tile 84)
         (data 1))
 
   (tile (id 84)
         (images "box-full.png")
         (solid #t)
         (fullbox #t)
-        (data 2))
+        (data 2)
+        (next-tile 84))
   (tile (id 103)
         (images "box-full.png")
         (solid #t)
         (fullbox #t)
-        (data 3))
+        (data 3)
+        (next-tile 84))
  )
\ No newline at end of file
index 66e392e..36c147a 100644 (file)
@@ -1327,10 +1327,14 @@ void drawshape(float x, float y, unsigned int c)
             {
               texture_draw(&ptile->images[( ((global_frame_counter*25) / ptile->anim_speed) % (ptile->images.size()))],x,y);
             }
-          else
+          else if (ptile->images.size() == 1)
             {
               texture_draw(&ptile->images[0],x,y);
             }
+          else
+            {
+              printf("Tile not dravable %u\n", c);
+            }
         }
     }
 
@@ -1508,12 +1512,12 @@ bool isdistro(float x, float y)
 
 void trybreakbrick(float x, float y)
 {
-  if (isbrick(x, y))
+  Tile* tile = gettile(x, y);
+  if (tile->brick)
     {
-      if (shape(x, y) == 'x' || shape(x, y) == 'y')
+      if (tile->data > 0)
         {
           /* Get a distro from it: */
-
           add_bouncy_distro(((int)(x + 1) / 32) * 32,
                             (int)(y / 32) * 32);
 
@@ -1524,7 +1528,7 @@ void trybreakbrick(float x, float y)
             }
 
           if (distro_counter <= 0)
-            level_change(&current_level,x, y, TM_IA, 'a');
+            level_change(&current_level,x, y, TM_IA, tile->next_tile2);
 
           play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
           score = score + SCORE_DISTRO;
@@ -1533,8 +1537,7 @@ void trybreakbrick(float x, float y)
       else
         {
           /* Get rid of it: */
-
-          level_change(&current_level,x, y, TM_IA, '.');
+          level_change(&current_level,x, y, TM_IA, tile->next_tile);
         }
 
 
@@ -1568,7 +1571,8 @@ void bumpbrick(float x, float y)
 
 void tryemptybox(float x, float y, int col_side)
 {
-  if (!isfullbox(x, y))
+  Tile* tile = gettile(x,y);
+  if (!tile->fullbox)
     return;
 
   // according to the collision side, set the upgrade direction
@@ -1579,7 +1583,7 @@ void tryemptybox(float x, float y, int col_side)
     col_side = LEFT;
 
   // FIXME: Content of boxes must be handled otherwise
-  switch(gettile(x,y)->data)
+  switch(tile->data)
     {
     case 1: //'A':      /* Box with a distro! */
       add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32);
@@ -1602,7 +1606,7 @@ void tryemptybox(float x, float y, int col_side)
     }
 
   /* Empty the box: */
-  level_change(&current_level,x, y, TM_IA, 'a');
+  level_change(&current_level,x, y, TM_IA, tile->next_tile);
 }
 
 
@@ -1613,7 +1617,7 @@ void trygrabdistro(float x, float y, int bounciness)
   Tile* tile = gettile(x, y);
   if (tile && tile->distro)
     {
-      level_change(&current_level,x, y, TM_IA, '.');
+      level_change(&current_level,x, y, TM_IA, tile->next_tile);
       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
 
       if (bounciness == BOUNCE)
index eebecdf..a32275c 100644 (file)
@@ -10,6 +10,7 @@
 //
 //
 
+#include <map>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -300,54 +301,57 @@ int level_load(st_level* plevel, const char* filename)
       // Convert old levels to the new tile numbers
       if (version == 0)
         {
-          int transtable[256];
-          transtable[(int)'.'] = 0;
-          transtable[(int)'0'] = 0;
-          transtable[(int)'1'] = 1;
-          transtable[(int)'2'] = 2;
-          transtable[(int)'x'] = 77;
-          transtable[(int)'X'] = 77;
-          transtable[(int)'y'] = 78;
-          transtable[(int)'Y'] = 78;
-          transtable[(int)'A'] = 83;
-          transtable[(int)'B'] = 102;
-          transtable[(int)'!'] = 103;
-          transtable[(int)'a'] = 84;
-          transtable[(int)'C'] = 85;
-          transtable[(int)'D'] = 86;
-          transtable[(int)'E'] = 87;
-          transtable[(int)'F'] = 88;
-          transtable[(int)'c'] = 89;
-          transtable[(int)'d'] = 90;
-          transtable[(int)'e'] = 91;
-          transtable[(int)'f'] = 92;
-
-          transtable[(int)'G'] = 93;
-          transtable[(int)'H'] = 94;
-          transtable[(int)'I'] = 95;
-          transtable[(int)'J'] = 96;
-
-          transtable[(int)'g'] = 97;
-          transtable[(int)'h'] = 98;
-          transtable[(int)'i'] = 99;
-          transtable[(int)'j'] = 100
-;
-          transtable[(int)'#'] = 11;
-          transtable[(int)'['] = 13; 
-          transtable[(int)'='] = 14;
-          transtable[(int)']'] = 15;
-          transtable[(int)'$'] = 82;
-          transtable[(int)'^'] = 76;
-          transtable[(int)'*'] = 80;
-          transtable[(int)'|'] = 79;
-          transtable[(int)'\\'] = 81;
-          transtable[(int)'&'] = 75;
+          std::map<char, int> transtable;
+          transtable['.'] = 0;
+          transtable['0'] = 0;
+          transtable['1'] = 1;
+          transtable['2'] = 2;
+          transtable['x'] = 77;
+          transtable['X'] = 77;
+          transtable['y'] = 78;
+          transtable['Y'] = 78;
+          transtable['A'] = 83;
+          transtable['B'] = 102;
+          transtable['!'] = 103;
+          transtable['a'] = 84;
+          transtable['C'] = 85;
+          transtable['D'] = 86;
+          transtable['E'] = 87;
+          transtable['F'] = 88;
+          transtable['c'] = 89;
+          transtable['d'] = 90;
+          transtable['e'] = 91;
+          transtable['f'] = 92;
+
+          transtable['G'] = 93;
+          transtable['H'] = 94;
+          transtable['I'] = 95;
+          transtable['J'] = 96;
+
+          transtable['g'] = 97;
+          transtable['h'] = 98;
+          transtable['i'] = 99;
+          transtable['j'] = 100
+            ;
+          transtable['#'] = 11;
+          transtable['['] = 13; 
+          transtable['='] = 14;
+          transtable[']'] = 15;
+          transtable['$'] = 82;
+          transtable['^'] = 76;
+          transtable['*'] = 80;
+          transtable['|'] = 79;
+          transtable['\\'] = 81;
+          transtable['&'] = 75;
 
           for(std::vector<int>::iterator i = ia_tm.begin(); i != ia_tm.end(); ++i)
-            if (*i < 256)
-              *i = transtable[*i];
-            else
-              puts("Error: Value to high, conversion will fail");
+            {
+              std::map<char, int>::iterator j = transtable.find(*i);
+              if (j != transtable.end())
+                *i = j->second;
+              else
+                printf("Error: conversion will fail, unsupported char: '%c' (%d)\n", *i, *i);
+            }
         }
     }
 
index 80f6364..1140da7 100644 (file)
@@ -38,10 +38,8 @@ void TileManager::load_tileset(std::string filename)
 
           if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
             {
-              int id = 0;
-              std::vector<std::string> filenames;
-
-              Tile* tile = new Tile;             
+              Tile* tile = new Tile;
+              tile->id      = -1;
               tile->solid   = false;
              tile->brick   = false;
              tile->ice     = false;      
@@ -49,10 +47,12 @@ void TileManager::load_tileset(std::string filename)
               tile->distro  = false;
               tile->data    = 0;
               tile->alpha   = 0;
+              tile->next_tile  = 0;
+              tile->next_tile2 = 0;
               tile->anim_speed = 25;
   
               LispReader reader(lisp_cdr(element));
-              reader.read_int("id",  &id);
+              assert(reader.read_int("id",  &tile->id));
               reader.read_bool("solid", &tile->solid);
               reader.read_bool("brick", &tile->brick);
               reader.read_bool("ice", &tile->ice);        
@@ -61,9 +61,13 @@ void TileManager::load_tileset(std::string filename)
               reader.read_int("data",  (int*)&tile->data);
               reader.read_int("alpha",  (int*)&tile->alpha);
               reader.read_int("anim-speed",  &tile->anim_speed);
-              reader.read_string_vector("images",  &filenames);
+              reader.read_int("next-tile",  &tile->next_tile);
+              reader.read_int("next-tile2",  &tile->next_tile2);
+              reader.read_string_vector("images",  &tile->filenames);
 
-             for(std::vector<std::string>::iterator it = filenames.begin(); it != filenames.end(); ++it)
+             for(std::vector<std::string>::iterator it = tile->filenames.begin();
+                  it != tile->filenames.end();
+                  ++it)
                 {
                   texture_type cur_image;
                   tile->images.push_back(cur_image);
@@ -72,10 +76,10 @@ void TileManager::load_tileset(std::string filename)
                                USE_ALPHA);
                 }
 
-              if (id+tileset_id >= int(tiles.size()))
-                tiles.resize(id+tileset_id+1);
+              if (tile->id + tileset_id >= int(tiles.size()))
+                tiles.resize(tile->id + tileset_id+1);
 
-              tiles[id+tileset_id] = tile;
+              tiles[tile->id + tileset_id] = tile;
             }
          else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0)
             {
index ab78d59..1d301e8 100644 (file)
@@ -23,7 +23,10 @@ Tile Class
 */
 struct Tile
 {
+  int id;
+
   std::vector<texture_type> images;
+  std::vector<std::string>  filenames;
 
   /** solid tile that is indestructable by Tux */
   bool solid;
@@ -43,6 +46,11 @@ struct Tile
   /** General purpose data attached to a tile (content of a box, type of coin) */
   int data;
 
+  /** Id of the tile that is going to replace this tile once it has
+      been collected or jumped at */
+  int next_tile;
+  int next_tile2;
+
   int anim_speed;
   unsigned char alpha;
 };
@@ -57,7 +65,19 @@ class TileManager
   
  public:
   static TileManager* instance() { return instance_ ? instance_ : instance_ = new TileManager(); }
-  Tile* get(unsigned int id) { if( id < tiles.size()) { return tiles[id]; } else { return NULL; } }
+  Tile* get(unsigned int id) {
+    if(id < tiles.size())
+      {
+        return tiles[id]; 
+      }
+    else
+      {
+        // Never return 0, but return the 0th tile instead so that
+        // user code doesn't have to check for NULL pointers all over
+        // the place
+        return tiles[0]; 
+      } 
+  }
 };
 
 #endif