*possible* fix for bugs #453 and #373
[supertux.git] / src / worldmap / tux.cpp
index 2b070a5..f505fc2 100644 (file)
@@ -1,4 +1,4 @@
-//  $Id: worldmap.cpp 3327 2006-04-13 15:02:40Z ravu_al_hemio $
+//  $Id$
 //
 //  SuperTux -  A Jump'n Run
 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
@@ -20,6 +20,7 @@
 #include <config.h>
 
 #include "tux.hpp"
+#include "sprite/sprite.hpp"
 #include "sprite/sprite_manager.hpp"
 #include "video/drawing_context.hpp"
 #include "player_status.hpp"
 #include "special_tile.hpp"
 #include "sprite_change.hpp"
 #include "control/joystickkeyboardcontroller.hpp"
-#include "scripting/wrapper_util.hpp"
+#include "scripting/squirrel_util.hpp"
+#include "tile.hpp"
 #include "main.hpp"
 
 namespace WorldMapNS
 {
 
-static const float TUXSPEED = 200; 
-static const float map_message_TIME = 2.8;
+static const float TUXSPEED = 200;
+static const float map_message_TIME = 2.8f;
 
 Tux::Tux(WorldMap* worldmap_)
   : worldmap(worldmap_)
 {
   sprite.reset(sprite_manager->create("images/worldmap/common/tux.sprite"));
-  
+
   offset = 0;
   moving = false;
   direction = D_NONE;
@@ -98,7 +100,7 @@ Tux::get_pos()
     case D_NONE:
       break;
     }
-  
+
   return Vector(x, y);
 }
 
@@ -117,11 +119,11 @@ Tux::set_direction(Direction dir)
   input_direction = dir;
 }
 
-void 
-Tux::tryStartWalking() 
+void
+Tux::tryStartWalking()
 {
   if (moving)
-    return;  
+    return;
   if (input_direction == D_NONE)
     return;
 
@@ -143,16 +145,16 @@ Tux::tryStartWalking()
   }
 }
 
-bool 
-Tux::canWalk(const Tile* tile, Direction dir)
+bool
+Tux::canWalk(int tile_data, Direction dir)
 {
-  return ((tile->getData() & Tile::WORLDMAP_NORTH && dir == D_NORTH) ||
-         (tile->getData() & Tile::WORLDMAP_SOUTH && dir == D_SOUTH) ||
-         (tile->getData() & Tile::WORLDMAP_EAST && dir == D_EAST) ||
-         (tile->getData() & Tile::WORLDMAP_WEST && dir == D_WEST));
+  return ((tile_data & Tile::WORLDMAP_NORTH && dir == D_NORTH) ||
+      (tile_data & Tile::WORLDMAP_SOUTH && dir == D_SOUTH) ||
+      (tile_data & Tile::WORLDMAP_EAST && dir == D_EAST) ||
+      (tile_data & Tile::WORLDMAP_WEST && dir == D_WEST));
 }
 
-void 
+void
 Tux::tryContinueWalking(float elapsed_time)
 {
   if (!moving)
@@ -170,19 +172,19 @@ Tux::tryContinueWalking(float elapsed_time)
   SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
   if(sprite_change != NULL) {
     sprite.reset(new Sprite( *(sprite_change->sprite.get()) ));
-    sprite_change->in_stay_action = false;
+    sprite_change->clear_stay_action();
   }
 
   // if this is a special_tile with passive_message, display it
   SpecialTile* special_tile = worldmap->at_special_tile();
   if(special_tile)
-  {  
+  {
     // direction and the apply_action_ are opposites, since they "see"
     // directions in a different way
     if((direction == D_NORTH && special_tile->apply_action_south) ||
-                   (direction == D_SOUTH && special_tile->apply_action_north) ||
-                   (direction == D_WEST && special_tile->apply_action_east) ||
-                   (direction == D_EAST && special_tile->apply_action_west))
+            (direction == D_SOUTH && special_tile->apply_action_north) ||
+            (direction == D_WEST && special_tile->apply_action_east) ||
+            (direction == D_EAST && special_tile->apply_action_west))
     {
       if(special_tile->passive_message) {
         worldmap->passive_message = special_tile->map_message;
@@ -190,8 +192,7 @@ Tux::tryContinueWalking(float elapsed_time)
       } else if(special_tile->script != "") {
         try {
           std::istringstream in(special_tile->script);
-          HSQUIRRELVM vm = ScriptManager::instance->create_thread();
-          Scripting::compile_and_run(vm, in, "specialtile");
+          worldmap->run_script(in, "specialtile");
         } catch(std::exception& e) {
           log_warning << "Couldn't execute special tile script: " << e.what()
                       << std::endl;
@@ -200,11 +201,15 @@ Tux::tryContinueWalking(float elapsed_time)
     }
   }
 
-  // stop if we reached a level, a WORLDMAP_STOP tile or a special tile without a passive_message
+  // check if we are at a Teleporter
+  Teleporter* teleporter = worldmap->at_teleporter(tile_pos);
+
+  // stop if we reached a level, a WORLDMAP_STOP tile, a teleporter or a special tile without a passive_message
   if ((worldmap->at_level())
-      || (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP)
+      || (worldmap->tile_data_at(tile_pos) & Tile::WORLDMAP_STOP)
       || (special_tile && !special_tile->passive_message
-                       && special_tile->script == "")) {
+                       && special_tile->script == "")
+      || (teleporter)) {
     if(special_tile && !special_tile->map_message.empty()
         && !special_tile->passive_message)
       worldmap->passive_message_timer.start(0);
@@ -213,21 +218,19 @@ Tux::tryContinueWalking(float elapsed_time)
   }
 
   // if user wants to change direction, try changing, else guess the direction in which to walk next
-  const Tile* tile = worldmap->at(tile_pos);
-  if (direction != input_direction) { 
-    if(canWalk(tile, input_direction)) {  
-      direction = input_direction;
-      back_direction = reverse_dir(direction);
-    }
+  const int tile_data = worldmap->tile_data_at(tile_pos);
+  if ((direction != input_direction) && canWalk(tile_data, input_direction)) {
+    direction = input_direction;
+    back_direction = reverse_dir(direction);
   } else {
     Direction dir = D_NONE;
-    if (tile->getData() & Tile::WORLDMAP_NORTH && back_direction != D_NORTH)
+    if (tile_data & Tile::WORLDMAP_NORTH && back_direction != D_NORTH)
       dir = D_NORTH;
-    else if (tile->getData() & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH)
+    else if (tile_data & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH)
       dir = D_SOUTH;
-    else if (tile->getData() & Tile::WORLDMAP_EAST && back_direction != D_EAST)
+    else if (tile_data & Tile::WORLDMAP_EAST && back_direction != D_EAST)
       dir = D_EAST;
-    else if (tile->getData() & Tile::WORLDMAP_WEST && back_direction != D_WEST)
+    else if (tile_data & Tile::WORLDMAP_WEST && back_direction != D_WEST)
       dir = D_WEST;
 
     if (dir == D_NONE) {
@@ -245,10 +248,10 @@ Tux::tryContinueWalking(float elapsed_time)
   // Walk automatically to the next tile
   if(direction == D_NONE)
     return;
-  
+
   Vector next_tile;
   if (!worldmap->path_ok(direction, tile_pos, &next_tile)) {
-    log_warning << "Tilemap data is buggy" << std::endl;
+    log_debug << "Tilemap data is buggy" << std::endl;
     stop();
     return;
   }
@@ -256,12 +259,12 @@ Tux::tryContinueWalking(float elapsed_time)
   SpriteChange* next_sprite = worldmap->at_sprite_change(next_tile);
   if(next_sprite != NULL && next_sprite->change_on_touch) {
     sprite.reset(new Sprite( *(next_sprite->sprite.get()) ));
-    next_sprite->in_stay_action = false;
+    next_sprite->clear_stay_action();
   }
   SpriteChange* last_sprite = worldmap->at_sprite_change(tile_pos);
   if(last_sprite != NULL && next_sprite != NULL) {
     log_debug << "Old: " << tile_pos << " New: " << next_tile << std::endl;
-    last_sprite->in_stay_action = true;
+    last_sprite->set_stay_action();
   }
 
   tile_pos = next_tile;
@@ -283,11 +286,22 @@ Tux::updateInputDirection()
 void
 Tux::update(float elapsed_time)
 {
-  updateInputDirection(); 
+  updateInputDirection();
   if (moving)
     tryContinueWalking(elapsed_time);
   else
     tryStartWalking();
 }
 
+void
+Tux::setup()
+{
+  // check if we already touch a SpriteChange object
+  SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
+  if(sprite_change != NULL) {
+    sprite.reset(new Sprite( *(sprite_change->sprite.get()) ));
+    sprite_change->clear_stay_action();
+  }
+}
+
 }