Bug 571: Take tilemap movement into account when handling unisolid tiles for collisio...
[supertux.git] / src / supertux / sector.cpp
index 0ca6601..611f36d 100644 (file)
 #include "lisp/list_iterator.hpp"
 #include "math/aatriangle.hpp"
 #include "object/background.hpp"
+#include "object/bonus_block.hpp"
+#include "object/brick.hpp"
 #include "object/bullet.hpp"
 #include "object/camera.hpp"
+#include "object/cloud_particle_system.hpp"
 #include "object/coin.hpp"
+#include "object/comet_particle_system.hpp"
 #include "object/display_effect.hpp"
+#include "object/ghost_particle_system.hpp"
 #include "object/gradient.hpp"
 #include "object/invisible_block.hpp"
 #include "object/particlesystem.hpp"
-#include "object/cloud_particle_system.hpp"
-#include "object/ghost_particle_system.hpp"
-#include "object/snow_particle_system.hpp"
 #include "object/particlesystem_interactive.hpp"
 #include "object/player.hpp"
 #include "object/portable.hpp"
 #include "object/pulsing_light.hpp"
+#include "object/rain_particle_system.hpp"
 #include "object/smoke_cloud.hpp"
+#include "object/snow_particle_system.hpp"
 #include "object/text_object.hpp"
 #include "object/tilemap.hpp"
-#include "physfs/physfs_stream.hpp"
+#include "physfs/ifile_stream.hpp"
 #include "scripting/squirrel_util.hpp"
 #include "supertux/collision.hpp"
 #include "supertux/constants.hpp"
+#include "supertux/game_session.hpp"
+#include "supertux/globals.hpp"
 #include "supertux/level.hpp"
-#include "supertux/main.hpp"
 #include "supertux/object_factory.hpp"
+#include "supertux/player_status.hpp"
 #include "supertux/spawn_point.hpp"
 #include "supertux/tile.hpp"
 #include "trigger/sequence_trigger.hpp"
 #include "util/file_system.hpp"
 
+#define DEFORM_BOTTOM  AATriangle::DEFORM1
+#define DEFORM_TOP     AATriangle::DEFORM2
+#define DEFORM_LEFT    AATriangle::DEFORM3
+#define DEFORM_RIGHT   AATriangle::DEFORM4
+
 Sector* Sector::_current = 0;
 
 bool Sector::show_collrects = false;
@@ -79,32 +90,32 @@ Sector::Sector(Level* parent) :
   camera(0), 
   effect(0)
 {
-  add_object(new Player(player_status, "Tux"));
+  add_object(new Player(GameSession::current()->get_player_status(), "Tux"));
   add_object(new DisplayEffect("Effect"));
   add_object(new TextObject("Text"));
 
   sound_manager->preload("sounds/shoot.wav");
 
   // create a new squirrel table for the sector
-  using namespace Scripting;
+  using namespace scripting;
 
   sq_collectgarbage(global_vm);
 
   sq_newtable(global_vm);
   sq_pushroottable(global_vm);
   if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
-    throw Scripting::SquirrelError(global_vm, "Couldn't set sector_table delegate");
+    throw scripting::SquirrelError(global_vm, "Couldn't set sector_table delegate");
 
   sq_resetobject(&sector_table);
   if(SQ_FAILED(sq_getstackobj(global_vm, -1, &sector_table)))
-    throw Scripting::SquirrelError(global_vm, "Couldn't get sector table");
+    throw scripting::SquirrelError(global_vm, "Couldn't get sector table");
   sq_addref(global_vm, &sector_table);
   sq_pop(global_vm, 1);
 }
 
 Sector::~Sector()
 {
-  using namespace Scripting;
+  using namespace scripting;
 
   deactivate();
 
@@ -166,15 +177,14 @@ Sector::parse_object(const std::string& name, const Reader& reader)
     return partsys;
   } else if(name == "money") { // for compatibility with old maps
     return new Jumpy(reader);
+  } else {
+    try {
+      return ObjectFactory::instance().create(name, reader);
+    } catch(std::exception& e) {
+      log_warning << e.what() << "" << std::endl;
+      return 0;
+    }
   }
-
-  try {
-    return create_object(name, reader);
-  } catch(std::exception& e) {
-    log_warning << e.what() << "" << std::endl;
-  }
-
-  return 0;
 }
 
 void
@@ -191,7 +201,7 @@ Sector::parse(const Reader& sector)
     } else if(token == "music") {
       iter.value()->get(music);
     } else if(token == "spawnpoint") {
-      SpawnPoint* sp = new SpawnPoint(iter.lisp());
+      SpawnPoint* sp = new SpawnPoint(*iter.lisp());
       spawnpoints.push_back(sp);
     } else if(token == "init-script") {
       iter.value()->get(init_script);
@@ -404,7 +414,7 @@ Sector::fix_old_tiles()
       for(size_t y=0; y < solids->get_height(); ++y) {
         uint32_t    id   = solids->get_tile_id(x, y);
         const Tile *tile = solids->get_tile(x, y);
-        Vector pos(solids->get_x_offset() + x*32, solids->get_y_offset() + y*32);
+        Vector pos = solids->get_tile_position(x, y);
 
         if(id == 112) {
           add_object(new InvisibleBlock(pos));
@@ -434,8 +444,8 @@ Sector::fix_old_tiles()
     for(size_t x=0; x < tm->get_width(); ++x) {
       for(size_t y=0; y < tm->get_height(); ++y) {
         uint32_t id = tm->get_tile_id(x, y);
-        Vector pos(tm->get_x_offset() + x*32, tm->get_y_offset() + y*32);
-        Vector center(pos.x + 16, pos.y + 16);
+        Vector pos = tm->get_tile_position(x, y);
+        Vector center = pos + Vector(16, 16);
 
         // torch
         if (id == 1517) {
@@ -462,7 +472,7 @@ Sector::fix_old_tiles()
 HSQUIRRELVM
 Sector::run_script(std::istream& in, const std::string& sourcename)
 {
-  using namespace Scripting;
+  using namespace scripting;
 
   // garbage collect thread list
   for(ScriptList::iterator i = scripts.begin();
@@ -501,7 +511,7 @@ void
 Sector::add_object(GameObject* object)
 {
   // make sure the object isn't already in the list
-#ifdef DEBUG
+#ifndef NDEBUG
   for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
       ++i) {
     if(*i == object) {
@@ -552,12 +562,12 @@ Sector::activate(const Vector& player_pos)
     _current = this;
 
     // register sectortable as sector in scripting
-    HSQUIRRELVM vm = Scripting::global_vm;
+    HSQUIRRELVM vm = scripting::global_vm;
     sq_pushroottable(vm);
     sq_pushstring(vm, "sector", -1);
     sq_pushobject(vm, sector_table);
     if(SQ_FAILED(sq_createslot(vm, -3)))
-      throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
+      throw scripting::SquirrelError(vm, "Couldn't set sector in roottable");
     sq_pop(vm, 1);
 
     for(GameObjects::iterator i = gameobjects.begin();
@@ -613,11 +623,11 @@ Sector::deactivate()
     return;
 
   // remove sector entry from global vm
-  HSQUIRRELVM vm = Scripting::global_vm;
+  HSQUIRRELVM vm = scripting::global_vm;
   sq_pushroottable(vm);
   sq_pushstring(vm, "sector", -1);
   if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
-    throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
+    throw scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
   sq_pop(vm, 1);
 
   for(GameObjects::iterator i = gameobjects.begin();
@@ -631,10 +641,10 @@ Sector::deactivate()
   _current = NULL;
 }
 
-Rect
+Rectf
 Sector::get_active_region()
 {
-  return Rect(
+  return Rectf(
     camera->get_translation() - Vector(1600, 1200),
     camera->get_translation() + Vector(1600, 1200) + Vector(SCREEN_WIDTH,SCREEN_HEIGHT));
 }
@@ -763,11 +773,11 @@ Sector::before_object_add(GameObject* object)
 void
 Sector::try_expose(GameObject* object)
 {
-  ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
-  if(interface != NULL) {
-    HSQUIRRELVM vm = Scripting::global_vm;
+  ScriptInterface* object_ = dynamic_cast<ScriptInterface*> (object);
+  if(object_ != NULL) {
+    HSQUIRRELVM vm = scripting::global_vm;
     sq_pushobject(vm, sector_table);
-    interface->expose(vm, -1);
+    object_->expose(vm, -1);
     sq_pop(vm, 1);
   }
 }
@@ -775,10 +785,10 @@ Sector::try_expose(GameObject* object)
 void
 Sector::try_expose_me()
 {
-  HSQUIRRELVM vm = Scripting::global_vm;
+  HSQUIRRELVM vm = scripting::global_vm;
   sq_pushobject(vm, sector_table);
-  Scripting::SSector* interface = static_cast<Scripting::SSector*> (this);
-  expose_object(vm, -1, interface, "settings", false);
+  scripting::SSector* this_ = static_cast<scripting::SSector*> (this);
+  expose_object(vm, -1, this_, "settings", false);
   sq_pop(vm, 1);
 }
 
@@ -806,13 +816,13 @@ Sector::before_object_remove(GameObject* object)
 void
 Sector::try_unexpose(GameObject* object)
 {
-  ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
-  if(interface != NULL) {
-    HSQUIRRELVM vm = Scripting::global_vm;
+  ScriptInterface* object_ = dynamic_cast<ScriptInterface*> (object);
+  if(object_ != NULL) {
+    HSQUIRRELVM vm = scripting::global_vm;
     SQInteger oldtop = sq_gettop(vm);
     sq_pushobject(vm, sector_table);
     try {
-      interface->unexpose(vm, -1);
+      object_->unexpose(vm, -1);
     } catch(std::exception& e) {
       log_warning << "Couldn't unregister object: " << e.what() << std::endl;
     }
@@ -823,11 +833,11 @@ Sector::try_unexpose(GameObject* object)
 void
 Sector::try_unexpose_me()
 {
-  HSQUIRRELVM vm = Scripting::global_vm;
+  HSQUIRRELVM vm = scripting::global_vm;
   SQInteger oldtop = sq_gettop(vm);
   sq_pushobject(vm, sector_table);
   try {
-    Scripting::unexpose_object(vm, -1, "settings");
+    scripting::unexpose_object(vm, -1, "settings");
   } catch(std::exception& e) {
     log_warning << "Couldn't unregister object: " << e.what() << std::endl;
   }
@@ -861,7 +871,7 @@ Sector::draw(DrawingContext& context)
     for(MovingObjects::iterator i = moving_objects.begin();
         i != moving_objects.end(); ++i) {
       MovingObject* object = *i;
-      const Rect& rect = object->get_bbox();
+      const Rectf& rect = object->get_bbox();
 
       context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
     }
@@ -876,7 +886,7 @@ Sector::draw(DrawingContext& context)
 
 /** r1 is supposed to be moving, r2 a solid object */
 void check_collisions(collision::Constraints* constraints,
-                      const Vector& movement, const Rect& r1, const Rect& r2,
+                      const Vector& movement, const Rectf& r1, const Rectf& r2,
                       GameObject* object = NULL, MovingObject* other = NULL, const Vector& addl_ground_movement = Vector(0,0))
 {
   if(!collision::intersects(r1, r2))
@@ -897,19 +907,19 @@ void check_collisions(collision::Constraints* constraints,
 
   if(fabsf(movement.y) > fabsf(movement.x)) {
     if(ileft < SHIFT_DELTA) {
-      constraints->right = std::min(constraints->right, r2.get_left());
+      constraints->min_right(r2.get_left());
       return;
     } else if(iright < SHIFT_DELTA) {
-      constraints->left = std::max(constraints->left, r2.get_right());
+      constraints->max_left(r2.get_right());
       return;
     }
   } else {
     // shiftout bottom/top
     if(itop < SHIFT_DELTA) {
-      constraints->bottom = std::min(constraints->bottom, r2.get_top());
+      constraints->min_bottom(r2.get_top());
       return;
     } else if(ibottom < SHIFT_DELTA) {
-      constraints->top = std::max(constraints->top, r2.get_bottom());
+      constraints->max_top(r2.get_bottom());
       return;
     }
   }
@@ -917,7 +927,7 @@ void check_collisions(collision::Constraints* constraints,
   constraints->ground_movement += addl_ground_movement;
   if(other != NULL) {
     HitResponse response = other->collision(*object, dummy);
-    if(response == PASSTHROUGH)
+    if(response == ABORT_MOVE)
       return;
 
     if(other->get_movement() != Vector(0, 0)) {
@@ -930,26 +940,313 @@ void check_collisions(collision::Constraints* constraints,
   float horiz_penetration = std::min(ileft, iright);
   if(vert_penetration < horiz_penetration) {
     if(itop < ibottom) {
-      constraints->bottom = std::min(constraints->bottom, r2.get_top());
+      constraints->min_bottom(r2.get_top());
       constraints->hit.bottom = true;
     } else {
-      constraints->top = std::max(constraints->top, r2.get_bottom());
+      constraints->max_top(r2.get_bottom());
       constraints->hit.top = true;
     }
   } else {
     if(ileft < iright) {
-      constraints->right = std::min(constraints->right, r2.get_left());
+      constraints->min_right(r2.get_left());
       constraints->hit.right = true;
     } else {
-      constraints->left = std::max(constraints->left, r2.get_right());
+      constraints->max_left(r2.get_right());
       constraints->hit.left = true;
     }
   }
 }
 
+/* Returns zero if a unisolid tile is non-solid due to the movement direction,
+ * non-zero if the tile is solid due to direction. */
+int check_movement_unisolid (const Vector& movement, const Tile* tile)
+{
+  int slope_info;
+  double mv_x;
+  double mv_y;
+  double mv_tan;
+  double slope_tan;
+
+#define MV_NON_SOLID 0
+#define MV_SOLID 1
+
+  /* If the tile is not a slope, this is very easy. */
+  if ((tile->getAttributes() & Tile::SLOPE) == 0)
+  {
+    if (movement.y >= 0) /* moving down */
+      return MV_SOLID;
+    else /* moving up */
+      return MV_NON_SOLID;
+  }
+
+  /* Initialize mv_x and mv_y. Depending on the slope the axis are inverted so
+   * that we can always use the "SOUTHEAST" case of the slope. The southeast
+   * case is the following:
+   *     .
+   *    /!
+   *   / !
+   *  +--+
+   */
+  mv_x = (double) movement.x;
+  mv_y = (double) movement.y;
+
+  slope_info = tile->getData();
+  switch (slope_info & AATriangle::DIRECTION_MASK)
+  {
+    case AATriangle::SOUTHEAST: /*    . */
+      /* do nothing */          /*   /! */
+      break;                    /*  / ! */
+                                /* +--+ */
+    case AATriangle::SOUTHWEST: /* .    */
+      mv_x *= (-1.0);           /* !\   */
+      break;                    /* ! \  */
+                                /* +--+ */
+    case AATriangle::NORTHEAST: /* +--+ */
+      mv_y *= (-1.0);           /*  \ ! */
+      break;                    /*   \! */
+                                /*    ' */
+    case AATriangle::NORTHWEST: /* +--+ */
+      mv_x *= (-1.0);           /* ! /  */
+      mv_y *= (-1.0);           /* !/   */
+      break;                    /* '    */
+  } /* switch (slope_info & DIRECTION_MASK) */
+
+  /* Handle the easy cases first */
+  /* If we're moving to the right and down, then the slope is solid. */
+  if ((mv_x >= 0.0) && (mv_y >= 0.0)) /* 4th quadrant */
+    return MV_SOLID;
+  /* If we're moving to the left and up, then the slope is not solid. */
+  else if ((mv_x <= 0.0) && (mv_y <= 0.0)) /* 2nd quadrant */
+    return MV_NON_SOLID;
+
+  /* The pure up-down and left-right movements have already been handled. */
+  assert (mv_x != 0.0);
+  assert (mv_y != 0.0);
+
+  /* calculate tangent of movement */
+  mv_tan = (-1.0) * mv_y / mv_x;
+
+  /* determine tangent of the slope */
+  slope_tan = 1.0;
+  if (((slope_info & AATriangle::DEFORM_MASK) == DEFORM_BOTTOM)
+      || ((slope_info & AATriangle::DEFORM_MASK) == DEFORM_TOP))
+    slope_tan = 0.5; /* ~= 26.6 deg */
+  else if (((slope_info & AATriangle::DEFORM_MASK) == DEFORM_LEFT)
+      || ((slope_info & AATriangle::DEFORM_MASK) == DEFORM_RIGHT))
+    slope_tan = 2.0; /* ~= 63.4 deg */
+
+  /* up and right */
+  if (mv_x > 0.0) /* 1st quadrant */
+  {
+    assert (mv_y < 0.0);
+    if (mv_tan <= slope_tan)
+      return MV_SOLID;
+    else
+      return MV_NON_SOLID;
+  }
+  /* down and left */
+  else if (mv_x < 0.0) /* 3rd quadrant */
+  {
+    assert (mv_y > 0.0);
+    if (mv_tan >= slope_tan)
+      return MV_SOLID;
+    else
+      return MV_NON_SOLID;
+  }
+
+  assert (1 != 1);
+  return (-1);
+
+#undef MV_NON_SOLID
+#undef MV_SOLID
+} /* int check_movement_unisolid */
+
+int is_above_line (float l_x, float l_y, float m,
+    float p_x, float p_y)
+{
+  float interp_y = (l_y + (m * (p_x - l_x)));
+  if (interp_y == p_y)
+    return (1);
+  else if (interp_y > p_y)
+    return (1);
+  else
+    return (0);
+}
+
+int is_below_line (float l_x, float l_y, float m,
+    float p_x, float p_y)
+{
+  if (is_above_line (l_x, l_y, m, p_x, p_y))
+    return (0);
+  else
+    return (1);
+}
+
+int check_position_unisolid (const Rectf& obj_bbox,
+    const Rectf& tile_bbox,
+    const Tile* tile)
+{
+  int slope_info;
+  float tile_x;
+  float tile_y;
+  float gradient;
+  float delta_x;
+  float delta_y;
+  float obj_x;
+  float obj_y;
+
+#define POS_NON_SOLID 0
+#define POS_SOLID 1
+
+  /* If this is not a slope, this is - again - easy */
+  if ((tile->getAttributes() & Tile::SLOPE) == 0)
+  {
+    if ((obj_bbox.get_bottom () - SHIFT_DELTA) <= tile_bbox.get_top ())
+      return POS_SOLID;
+    else
+      return POS_NON_SOLID;
+  }
+
+  /* There are 20 different cases. For each case, calculate a line that
+   * describes the slope's surface. The line is defined by x, y, and m, the
+   * gradient. */
+  slope_info = tile->getData();
+  switch (slope_info
+      & (AATriangle::DIRECTION_MASK | AATriangle::DEFORM_MASK))
+  {
+    case AATriangle::SOUTHWEST:
+    case AATriangle::SOUTHWEST | DEFORM_TOP:
+    case AATriangle::SOUTHWEST | DEFORM_LEFT:
+    case AATriangle::NORTHEAST:
+    case AATriangle::NORTHEAST | DEFORM_TOP:
+    case AATriangle::NORTHEAST | DEFORM_LEFT:
+      tile_x = tile_bbox.get_left ();
+      tile_y = tile_bbox.get_top ();
+      gradient = 1.0;
+      break;
+
+    case AATriangle::SOUTHEAST:
+    case AATriangle::SOUTHEAST | DEFORM_TOP:
+    case AATriangle::SOUTHEAST | DEFORM_RIGHT:
+    case AATriangle::NORTHWEST:
+    case AATriangle::NORTHWEST | DEFORM_TOP:
+    case AATriangle::NORTHWEST | DEFORM_RIGHT:
+      tile_x = tile_bbox.get_right ();
+      tile_y = tile_bbox.get_top ();
+      gradient = -1.0;
+      break;
+
+    case AATriangle::SOUTHEAST | DEFORM_BOTTOM:
+    case AATriangle::SOUTHEAST | DEFORM_LEFT:
+    case AATriangle::NORTHWEST | DEFORM_BOTTOM:
+    case AATriangle::NORTHWEST | DEFORM_LEFT:
+      tile_x = tile_bbox.get_left ();
+      tile_y = tile_bbox.get_bottom ();
+      gradient = -1.0;
+      break;
+
+    case AATriangle::SOUTHWEST | DEFORM_BOTTOM:
+    case AATriangle::SOUTHWEST | DEFORM_RIGHT:
+    case AATriangle::NORTHEAST | DEFORM_BOTTOM:
+    case AATriangle::NORTHEAST | DEFORM_RIGHT:
+      tile_x = tile_bbox.get_right ();
+      tile_y = tile_bbox.get_bottom ();
+      gradient = 1.0;
+      break;
+
+    default:
+      assert (23 == 42);
+  }
+
+  /* delta_x, delta_y: Gradient aware version of SHIFT_DELTA. Here, we set the
+   * sign of the values only. Also, we determine here which corner of the
+   * object's bounding box is the interesting one for us. */
+  delta_x = 1.0 * SHIFT_DELTA;
+  delta_y = 1.0 * SHIFT_DELTA;
+  switch (slope_info & AATriangle::DIRECTION_MASK)
+  {
+    case AATriangle::SOUTHWEST:
+      delta_x *= 1.0;
+      delta_y *= -1.0;
+      obj_x = obj_bbox.get_left ();
+      obj_y = obj_bbox.get_bottom ();
+      break;
+
+    case AATriangle::SOUTHEAST:
+      delta_x *= -1.0;
+      delta_y *= -1.0;
+      obj_x = obj_bbox.get_right ();
+      obj_y = obj_bbox.get_bottom ();
+      break;
+
+    case AATriangle::NORTHWEST:
+      delta_x *= 1.0;
+      delta_y *= 1.0;
+      obj_x = obj_bbox.get_left ();
+      obj_y = obj_bbox.get_top ();
+      break;
+
+    case AATriangle::NORTHEAST:
+      delta_x *= -1.0;
+      delta_y *= 1.0;
+      obj_x = obj_bbox.get_right ();
+      obj_y = obj_bbox.get_top ();
+      break;
+  }
+
+  /* Adapt the delta_x, delta_y and the gradient for the 26.6 deg and 63.4 deg
+   * cases. */
+  switch (slope_info & AATriangle::DEFORM_MASK)
+  {
+    case 0:
+      delta_x *= .70710678118654752440; /* 1/sqrt(2) */
+      delta_y *= .70710678118654752440; /* 1/sqrt(2) */
+      break;
+
+    case DEFORM_BOTTOM:
+    case DEFORM_TOP:
+      delta_x *= .44721359549995793928; /* 1/sqrt(5) */
+      delta_y *= .89442719099991587856; /* 2/sqrt(5) */
+      gradient *= 0.5;
+      break;
+
+    case DEFORM_LEFT:
+    case DEFORM_RIGHT:
+      delta_x *= .89442719099991587856; /* 2/sqrt(5) */
+      delta_y *= .44721359549995793928; /* 1/sqrt(5) */
+      gradient *= 2.0;
+      break;
+  }
+
+  /* With a south slope, check if all points are above the line. If one point
+   * isn't, the slope is not solid. => You can pass through a south-slope from
+   * below but not from above. */
+  if (((slope_info & AATriangle::DIRECTION_MASK) == AATriangle::SOUTHWEST)
+      || ((slope_info & AATriangle::DIRECTION_MASK) == AATriangle::SOUTHEAST))
+  {
+    if (is_below_line (tile_x, tile_y, gradient, obj_x + delta_x, obj_y + delta_y))
+      return (POS_NON_SOLID);
+    else
+      return (POS_SOLID);
+  }
+  /* northwest or northeast. Same as above, but inverted. You can pass from top
+   * to bottom but not vice versa. */
+  else
+  {
+    if (is_above_line (tile_x, tile_y, gradient, obj_x + delta_x, obj_y + delta_y))
+      return (POS_NON_SOLID);
+    else
+      return (POS_SOLID);
+  }
+
+#undef POS_NON_SOLID
+#undef POS_SOLID
+} /* int check_position_unisolid */
+
 void
 Sector::collision_tilemap(collision::Constraints* constraints,
-                          const Vector& movement, const Rect& dest) const
+                          const Vector& movement, const Rectf& dest,
+                          MovingObject& object) const
 {
   // calculate rectangle where the object will move
   float x1 = dest.get_left();
@@ -961,36 +1258,52 @@ Sector::collision_tilemap(collision::Constraints* constraints,
     TileMap* solids = *i;
 
     // test with all tiles in this rectangle
-    int starttilex = int(x1 - solids->get_x_offset()) / 32;
-    int starttiley = int(y1 - solids->get_y_offset()) / 32;
-    int max_x = int(x2 - solids->get_x_offset());
-    int max_y = int(y2+1 - solids->get_y_offset());
+    Rect test_tiles = solids->get_tiles_overlapping(Rectf(x1, y1, x2, y2));
 
-    for(int x = starttilex; x*32 < max_x; ++x) {
-      for(int y = starttiley; y*32 < max_y; ++y) {
+    for(int x = test_tiles.left; x < test_tiles.right; ++x) {
+      for(int y = test_tiles.top; y < test_tiles.bottom; ++y) {
         const Tile* tile = solids->get_tile(x, y);
         if(!tile)
           continue;
         // skip non-solid tiles
         if((tile->getAttributes() & Tile::SOLID) == 0)
           continue;
+        Rectf tile_bbox = solids->get_tile_bbox(x, y);
+
         // only handle unisolid when the player is falling down and when he was
         // above the tile before
         if(tile->getAttributes() & Tile::UNISOLID) {
-          if(movement.y <= 0 || dest.get_bottom() - movement.y - SHIFT_DELTA > y*32)
+          int status;
+
+          /* Check if the tile is solid given the current movement. This works
+           * for south-slopes (which are solid when moving "down") and
+           * north-slopes (which are solid when moving "up". "up" and "down" is
+           * in quotation marks because because the slope's gradient is taken
+           * Also, this uses the movement relative to the tilemaps own movement
+           * (if any).  --octo */
+          status = check_movement_unisolid (movement - solids->get_movement (), tile);
+          /* If zero is returned, the unisolid tile is non-solid. */
+          if (status == 0)
+            continue;
+
+          /* Check whether the object is already *in* the tile. If so, the tile
+           * is non-solid. Otherwise, if the object is "above" (south slopes)
+           * or "below" (north slopes), the tile will be solid. */
+          status = check_position_unisolid (object.get_bbox(), tile_bbox, tile);
+          if (status == 0)
             continue;
         }
 
         if(tile->getAttributes() & Tile::SLOPE) { // slope tile
           AATriangle triangle;
-          Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
-          Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
-          triangle = AATriangle(p1, p2, tile->getData());
+          int slope_data = tile->getData();
+          if (solids->get_drawing_effect() == VERTICAL_FLIP)
+            slope_data = AATriangle::vertical_flip(slope_data);
+          triangle = AATriangle(tile_bbox, slope_data);
 
           collision::rectangle_aatriangle(constraints, dest, triangle, solids->get_movement());
         } else { // normal rectangular tile
-          Rect rect(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset(), (x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
-          check_collisions(constraints, movement, dest, rect, NULL, NULL, solids->get_movement());
+          check_collisions(constraints, movement, dest, tile_bbox, NULL, NULL, solids->get_movement());
         }
       }
     }
@@ -998,30 +1311,36 @@ Sector::collision_tilemap(collision::Constraints* constraints,
 }
 
 uint32_t
-Sector::collision_tile_attributes(const Rect& dest) const
+Sector::collision_tile_attributes(const Rectf& dest) const
 {
   float x1 = dest.p1.x;
   float y1 = dest.p1.y;
   float x2 = dest.p2.x;
-  float y2 = dest.p2.y + SHIFT_DELTA;
+  float y2 = dest.p2.y;
 
   uint32_t result = 0;
   for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
     TileMap* solids = *i;
 
     // test with all tiles in this rectangle
-    int starttilex = int(x1 - solids->get_x_offset()) / 32;
-    int starttiley = int(y1 - solids->get_y_offset()) / 32;
-    int max_x = int(x2 - solids->get_x_offset());
-    int max_y = int(y2+1 - solids->get_y_offset());
+    Rect test_tiles = solids->get_tiles_overlapping(Rectf(x1, y1, x2, y2));
+    // For ice (only), add a little fudge to recognize tiles Tux is standing on.
+    Rect test_tiles_ice = solids->get_tiles_overlapping(Rectf(x1, y1, x2, y2 + SHIFT_DELTA));
 
-    for(int x = starttilex; x*32 < max_x; ++x) {
-      for(int y = starttiley; y*32 < max_y; ++y) {
+    for(int x = test_tiles.left; x < test_tiles.right; ++x) {
+      int y;
+      for(y = test_tiles.top; y < test_tiles.bottom; ++y) {
         const Tile* tile = solids->get_tile(x, y);
         if(!tile)
           continue;
         result |= tile->getAttributes();
       }
+      for(; y < test_tiles_ice.bottom; ++y) {
+        const Tile* tile = solids->get_tile(x, y);
+        if(!tile)
+          continue;
+        result |= (tile->getAttributes() & Tile::ICE);
+      }
     }
   }
 
@@ -1029,7 +1348,7 @@ Sector::collision_tile_attributes(const Rect& dest) const
 }
 
 /** fills in CollisionHit and Normal vector of 2 intersecting rectangle */
-static void get_hit_normal(const Rect& r1, const Rect& r2, CollisionHit& hit,
+static void get_hit_normal(const Rectf& r1, const Rectf& r2, CollisionHit& hit,
                            Vector& normal)
 {
   float itop = r1.get_bottom() - r2.get_top();
@@ -1063,8 +1382,8 @@ Sector::collision_object(MovingObject* object1, MovingObject* object2) const
 {
   using namespace collision;
 
-  const Rect& r1 = object1->dest;
-  const Rect& r2 = object2->dest;
+  const Rectf& r1 = object1->dest;
+  const Rectf& r2 = object2->dest;
 
   CollisionHit hit;
   if(intersects(object1->dest, object2->dest)) {
@@ -1100,10 +1419,10 @@ Sector::collision_object(MovingObject* object1, MovingObject* object2) const
 
 void
 Sector::collision_static(collision::Constraints* constraints,
-                         const Vector& movement, const Rect& dest,
-                         GameObject& object)
+                         const Vector& movement, const Rectf& dest,
+                         MovingObject& object)
 {
-  collision_tilemap(constraints, movement, dest);
+  collision_tilemap(constraints, movement, dest, object);
 
   // collision with other (static) objects
   for(MovingObjects::iterator i = moving_objects.begin();
@@ -1129,7 +1448,7 @@ Sector::collision_static_constrains(MovingObject& object)
 
   Constraints constraints;
   Vector movement = object.get_movement();
-  Rect& dest = object.dest;
+  Rectf& dest = object.dest;
   float owidth = object.get_bbox().get_width();
   float oheight = object.get_bbox().get_height();
 
@@ -1171,8 +1490,8 @@ Sector::collision_static_constrains(MovingObject& object)
       break;
 
     // apply calculated vertical constraints
-    if(constraints.right < infinity) {
-      float width = constraints.right - constraints.left;
+    float width = constraints.right - constraints.left;
+    if(width < infinity) {
       if(width + SHIFT_DELTA < owidth) {
 #if 0
         printf("Object %p crushed horizontally... L:%f R:%f\n", &object,
@@ -1184,9 +1503,13 @@ Sector::collision_static_constrains(MovingObject& object)
         h.crush = true;
         object.collision_solid(h);
       } else {
-        dest.p2.x = constraints.right - DELTA;
-        dest.p1.x = dest.p2.x - owidth;
+        float xmid = (constraints.left + constraints.right) / 2;
+        dest.p1.x = xmid - owidth/2;
+        dest.p2.x = xmid + owidth/2;
       }
+    } else if(constraints.right < infinity) {
+      dest.p2.x = constraints.right - DELTA;
+      dest.p1.x = dest.p2.x - owidth;
     } else if(constraints.left > -infinity) {
       dest.p1.x = constraints.left + DELTA;
       dest.p2.x = dest.p1.x + owidth;
@@ -1267,7 +1590,7 @@ Sector::handle_collisions()
       continue;
 
     uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
-    if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
+    if(tile_attributes >= Tile::FIRST_INTERESTING_FLAG) {
       moving_object->collision_tile(tile_attributes);
     }
   }
@@ -1337,7 +1660,7 @@ Sector::handle_collisions()
 }
 
 bool
-Sector::is_free_of_tiles(const Rect& rect, const bool ignoreUnisolid) const
+Sector::is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid) const
 {
   using namespace collision;
 
@@ -1345,24 +1668,26 @@ Sector::is_free_of_tiles(const Rect& rect, const bool ignoreUnisolid) const
     TileMap* solids = *i;
 
     // test with all tiles in this rectangle
-    int starttilex = int(rect.p1.x - solids->get_x_offset()) / 32;
-    int starttiley = int(rect.p1.y - solids->get_y_offset()) / 32;
-    int max_x = int(rect.p2.x - solids->get_x_offset());
-    int max_y = int(rect.p2.y - solids->get_y_offset());
+    Rect test_tiles = solids->get_tiles_overlapping(rect);
 
-    for(int x = starttilex; x*32 <= max_x; ++x) {
-      for(int y = starttiley; y*32 <= max_y; ++y) {
+    for(int x = test_tiles.left; x < test_tiles.right; ++x) {
+      for(int y = test_tiles.top; y < test_tiles.bottom; ++y) {
         const Tile* tile = solids->get_tile(x, y);
         if(!tile) continue;
+        if(!(tile->getAttributes() & Tile::SOLID))
+          continue;
+        if((tile->getAttributes() & Tile::UNISOLID) && ignoreUnisolid)
+          continue;
         if(tile->getAttributes() & Tile::SLOPE) {
           AATriangle triangle;
-          Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
-          Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
-          triangle = AATriangle(p1, p2, tile->getData());
+          Rectf tbbox = solids->get_tile_bbox(x, y);
+          triangle = AATriangle(tbbox, tile->getData());
           Constraints constraints;
-          if(collision::rectangle_aatriangle(&constraints, rect, triangle) && (!ignoreUnisolid || !(tile->getAttributes() & Tile::UNISOLID))) return false;
+          if(!collision::rectangle_aatriangle(&constraints, rect, triangle))
+            continue;
         }
-        if((tile->getAttributes() & Tile::SOLID) && (!ignoreUnisolid || !(tile->getAttributes() & Tile::UNISOLID))) return false;
+        // We have a solid tile that overlaps the given rectangle.
+        return false;
       }
     }
   }
@@ -1371,7 +1696,7 @@ Sector::is_free_of_tiles(const Rect& rect, const bool ignoreUnisolid) const
 }
 
 bool
-Sector::is_free_of_statics(const Rect& rect, const MovingObject* ignore_object, const bool ignoreUnisolid) const
+Sector::is_free_of_statics(const Rectf& rect, const MovingObject* ignore_object, const bool ignoreUnisolid) const
 {
   using namespace collision;
 
@@ -1391,7 +1716,7 @@ Sector::is_free_of_statics(const Rect& rect, const MovingObject* ignore_object,
 }
 
 bool
-Sector::is_free_of_movingstatics(const Rect& rect, const MovingObject* ignore_object) const
+Sector::is_free_of_movingstatics(const Rectf& rect, const MovingObject* ignore_object) const
 {
   using namespace collision;
 
@@ -1413,7 +1738,7 @@ Sector::is_free_of_movingstatics(const Rect& rect, const MovingObject* ignore_ob
 }
 
 bool
-Sector::add_bullet(const Vector& pos, float xm, Direction dir)
+Sector::add_bullet(const Vector& pos, const PlayerStatus* player_status, float xm, Direction dir)
 {
   // TODO remove this function and move these checks elsewhere...
 
@@ -1479,14 +1804,15 @@ Sector::get_total_badguys()
 }
 
 bool
-Sector::inside(const Rect& rect) const
+Sector::inside(const Rectf& rect) const
 {
   for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
     TileMap* solids = *i;
-    bool horizontally = ((rect.p2.x >= 0 + solids->get_x_offset()) && (rect.p1.x <= solids->get_width() * 32 + solids->get_x_offset()));
-    bool vertically = (rect.p1.y <= solids->get_height() * 32 + solids->get_y_offset());
 
-    if (horizontally && vertically)
+    Rectf bbox = solids->get_bbox();
+    bbox.p1.y = -INFINITY; // pretend the tilemap extends infinitely far upwards
+
+    if (bbox.contains(rect))
       return true;
   }
   return false;
@@ -1499,9 +1825,7 @@ Sector::get_width() const
   for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin();
       i != solid_tilemaps.end(); i++) {
     TileMap* solids = *i;
-    if ((solids->get_width() * 32 + solids->get_x_offset()) > width) {
-      width = solids->get_width() * 32 + solids->get_x_offset();
-    }
+    width = std::max(width, solids->get_bbox().get_right());
   }
 
   return width;
@@ -1514,9 +1838,7 @@ Sector::get_height() const
   for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin();
       i != solid_tilemaps.end(); i++) {
     TileMap* solids = *i;
-    if ((solids->get_height() * 32 + solids->get_y_offset()) > height) {
-      height = solids->get_height() * 32 + solids->get_y_offset();
-    }
+    height = std::max(height, solids->get_bbox().get_bottom());
   }
 
   return height;