supertux/sector.cpp: Add support for "south", "west" and "east" unisolid tiles.
[supertux.git] / src / supertux / sector.cpp
index a724e8e..f85ef49 100644 (file)
 #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;
@@ -514,15 +509,11 @@ Sector::add_object(GameObject* object)
 #ifndef NDEBUG
   for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
       ++i) {
-    if(*i == object) {
-      assert("object already added to sector" == 0);
-    }
+    assert(*i != object);
   }
   for(GameObjects::iterator i = gameobjects_new.begin();
       i != gameobjects_new.end(); ++i) {
-    if(*i == object) {
-      assert("object already added to sector" == 0);
-    }
+    assert(*i != object);
   }
 #endif
 
@@ -579,19 +570,28 @@ Sector::activate(const Vector& player_pos)
   }
   try_expose_me();
 
-  // spawn smalltux below spawnpoint
-  if (!player->is_big()) {
-    player->move(player_pos + Vector(0,32));
-  } else {
-    player->move(player_pos);
-  }
 
-  // spawning tux in the ground would kill him
-  if(!is_free_of_tiles(player->get_bbox())) {
-    log_warning << "Tried spawning Tux in solid matter. Compensating." << std::endl;
-    Vector npos = player->get_bbox().p1;
-    npos.y-=32;
-    player->move(npos);
+  // two-player hack: move other players to main player's position
+  // Maybe specify 2 spawnpoints in the level?
+  for(GameObjects::iterator i = gameobjects.begin();
+      i != gameobjects.end(); ++i) {
+    Player* p = dynamic_cast<Player*>(*i);
+    if (!p) continue;
+
+    // spawn smalltux below spawnpoint
+    if (!p->is_big()) {
+      p->move(player_pos + Vector(0,32));
+    } else {
+      p->move(player_pos);
+    }
+
+    // spawning tux in the ground would kill him
+    if(!is_free_of_tiles(p->get_bbox())) {
+      log_warning << "Tried spawning Tux in solid matter. Compensating." << std::endl;
+      Vector npos = p->get_bbox().p1;
+      npos.y-=32;
+      p->move(npos);
+    }
   }
 
   camera->reset(player->get_pos());
@@ -652,7 +652,7 @@ Sector::get_active_region()
 void
 Sector::update(float elapsed_time)
 {
-  player->check_bounds(camera);
+  player->check_bounds();
 
   /* update objects */
   for(GameObjects::iterator i = gameobjects.begin();
@@ -867,13 +867,13 @@ Sector::draw(DrawingContext& context)
   }
 
   if(show_collrects) {
-    Color col(0.2f, 0.2f, 0.2f, 0.7f);
+    Color color(1.0f, 0.0f, 0.0f, 0.75f);
     for(MovingObjects::iterator i = moving_objects.begin();
         i != moving_objects.end(); ++i) {
       MovingObject* object = *i;
       const Rectf& rect = object->get_bbox();
 
-      context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
+      context.draw_filled_rect(rect, color, LAYER_FOREGROUND1 + 10);
     }
   }
 
@@ -886,10 +886,10 @@ Sector::draw(DrawingContext& context)
 
 /** r1 is supposed to be moving, r2 a solid object */
 void check_collisions(collision::Constraints* constraints,
-                      const Vector& movement, const Rectf& r1, const Rectf& r2,
-                      GameObject* object = NULL, MovingObject* other = NULL, const Vector& addl_ground_movement = Vector(0,0))
+                      const Vector& obj_movement, const Rectf& obj_rect, const Rectf& other_rect,
+                      GameObject* object = NULL, MovingObject* other = NULL, const Vector& other_movement = Vector(0,0))
 {
-  if(!collision::intersects(r1, r2))
+  if(!collision::intersects(obj_rect, other_rect))
     return;
 
   MovingObject *moving_object = dynamic_cast<MovingObject*> (object);
@@ -900,31 +900,31 @@ void check_collisions(collision::Constraints* constraints,
     return;
 
   // calculate intersection
-  float itop    = r1.get_bottom() - r2.get_top();
-  float ibottom = r2.get_bottom() - r1.get_top();
-  float ileft   = r1.get_right() - r2.get_left();
-  float iright  = r2.get_right() - r1.get_left();
+  float itop    = obj_rect.get_bottom() - other_rect.get_top();
+  float ibottom = other_rect.get_bottom() - obj_rect.get_top();
+  float ileft   = obj_rect.get_right() - other_rect.get_left();
+  float iright  = other_rect.get_right() - obj_rect.get_left();
 
-  if(fabsf(movement.y) > fabsf(movement.x)) {
+  if(fabsf(obj_movement.y) > fabsf(obj_movement.x)) {
     if(ileft < SHIFT_DELTA) {
-      constraints->constrain_right(r2.get_left());
+      constraints->constrain_right(other_rect.get_left(), other_movement.x);
       return;
     } else if(iright < SHIFT_DELTA) {
-      constraints->constrain_left(r2.get_right());
+      constraints->constrain_left(other_rect.get_right(), other_movement.x);
       return;
     }
   } else {
     // shiftout bottom/top
     if(itop < SHIFT_DELTA) {
-      constraints->constrain_bottom(r2.get_top());
+      constraints->constrain_bottom(other_rect.get_top(), other_movement.y);
       return;
     } else if(ibottom < SHIFT_DELTA) {
-      constraints->constrain_top(r2.get_bottom());
+      constraints->constrain_top(other_rect.get_bottom(), other_movement.y);
       return;
     }
   }
 
-  constraints->ground_movement += addl_ground_movement;
+  constraints->ground_movement += other_movement;
   if(other != NULL) {
     HitResponse response = other->collision(*object, dummy);
     if(response == ABORT_MOVE)
@@ -940,18 +940,18 @@ void check_collisions(collision::Constraints* constraints,
   float horiz_penetration = std::min(ileft, iright);
   if(vert_penetration < horiz_penetration) {
     if(itop < ibottom) {
-      constraints->constrain_bottom(r2.get_top());
+      constraints->constrain_bottom(other_rect.get_top(), other_movement.y);
       constraints->hit.bottom = true;
     } else {
-      constraints->constrain_top(r2.get_bottom());
+      constraints->constrain_top(other_rect.get_bottom(), other_movement.y);
       constraints->hit.top = true;
     }
   } else {
     if(ileft < iright) {
-      constraints->constrain_right(r2.get_left());
+      constraints->constrain_right(other_rect.get_left(), other_movement.x);
       constraints->hit.right = true;
     } else {
-      constraints->constrain_left(r2.get_right());
+      constraints->constrain_left(other_rect.get_right(), other_movement.x);
       constraints->hit.left = true;
     }
   }
@@ -971,11 +971,21 @@ int check_movement_unisolid (Vector movement, const Tile* tile)
 #define MV_SOLID 1
 
   /* If the tile is not a slope, this is very easy. */
-  if ((tile->getAttributes() & Tile::SLOPE) == 0)
+  if (!tile->is_slope ())
   {
-    if (movement.y >= 0) /* moving down */
+    int dir = tile->getData () & ((int) Tile::UNI_DIR_MASK);
+
+    log_debug << "Tile data is " << tile->getData () << ", dir = " << dir << std::endl;
+
+    if (dir != Tile::UNI_DIR_NORTH)
+      log_debug << "Found non-north facing unisolid tile." << std::endl;
+
+    if (((dir == Tile::UNI_DIR_NORTH) && (movement.y >= 0)) /* moving down */
+        || ((dir == Tile::UNI_DIR_SOUTH) && (movement.y < 0)) /* moving up */
+        || ((dir == Tile::UNI_DIR_WEST) && (movement.x >= 0)) /* moving right */
+        || ((dir == Tile::UNI_DIR_EAST) && (movement.x < 0))) /* moving left */
       return MV_SOLID;
-    else /* moving up */
+    else
       return MV_NON_SOLID;
   }
 
@@ -1028,11 +1038,11 @@ int check_movement_unisolid (Vector movement, const Tile* tile)
 
   /* determine tangent of the slope */
   slope_tan = 1.0;
-  if (((slope_info & AATriangle::DEFORM_MASK) == DEFORM_BOTTOM)
-      || ((slope_info & AATriangle::DEFORM_MASK) == DEFORM_TOP))
+  if (((slope_info & AATriangle::DEFORM_MASK) == AATriangle::DEFORM_BOTTOM)
+      || ((slope_info & AATriangle::DEFORM_MASK) == AATriangle::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))
+  else if (((slope_info & AATriangle::DEFORM_MASK) == AATriangle::DEFORM_LEFT)
+      || ((slope_info & AATriangle::DEFORM_MASK) == AATriangle::DEFORM_RIGHT))
     slope_tan = 2.0; /* ~= 63.4 deg */
 
   /* up and right */
@@ -1099,12 +1109,24 @@ int check_position_unisolid (const Rectf& obj_bbox,
 #define POS_SOLID 1
 
   /* If this is not a slope, this is - again - easy */
-  if ((tile->getAttributes() & Tile::SLOPE) == 0)
+  if (!tile->is_slope ())
   {
-    if ((obj_bbox.get_bottom () - SHIFT_DELTA) <= tile_bbox.get_top ())
+    int dir = tile->getData () & Tile::UNI_DIR_MASK;
+
+    if ((dir == Tile::UNI_DIR_NORTH)
+        && ((obj_bbox.get_bottom () - SHIFT_DELTA) <= tile_bbox.get_top ()))
       return POS_SOLID;
-    else
-      return POS_NON_SOLID;
+    else if ((dir == Tile::UNI_DIR_SOUTH)
+        && ((obj_bbox.get_top () + SHIFT_DELTA) >= tile_bbox.get_bottom ()))
+      return POS_SOLID;
+    else if ((dir == Tile::UNI_DIR_WEST)
+        && ((obj_bbox.get_right () - SHIFT_DELTA) <= tile_bbox.get_left ()))
+      return POS_SOLID;
+    else if ((dir == Tile::UNI_DIR_EAST)
+        && ((obj_bbox.get_left () + SHIFT_DELTA) >= tile_bbox.get_right ()))
+      return POS_SOLID;
+
+    return POS_NON_SOLID;
   }
 
   /* There are 20 different cases. For each case, calculate a line that
@@ -1115,40 +1137,40 @@ int check_position_unisolid (const Rectf& obj_bbox,
       & (AATriangle::DIRECTION_MASK | AATriangle::DEFORM_MASK))
   {
     case AATriangle::SOUTHWEST:
-    case AATriangle::SOUTHWEST | DEFORM_TOP:
-    case AATriangle::SOUTHWEST | DEFORM_LEFT:
+    case AATriangle::SOUTHWEST | AATriangle::DEFORM_TOP:
+    case AATriangle::SOUTHWEST | AATriangle::DEFORM_LEFT:
     case AATriangle::NORTHEAST:
-    case AATriangle::NORTHEAST | DEFORM_TOP:
-    case AATriangle::NORTHEAST | DEFORM_LEFT:
+    case AATriangle::NORTHEAST | AATriangle::DEFORM_TOP:
+    case AATriangle::NORTHEAST | AATriangle::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::SOUTHEAST | AATriangle::DEFORM_TOP:
+    case AATriangle::SOUTHEAST | AATriangle::DEFORM_RIGHT:
     case AATriangle::NORTHWEST:
-    case AATriangle::NORTHWEST | DEFORM_TOP:
-    case AATriangle::NORTHWEST | DEFORM_RIGHT:
+    case AATriangle::NORTHWEST | AATriangle::DEFORM_TOP:
+    case AATriangle::NORTHWEST | AATriangle::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:
+    case AATriangle::SOUTHEAST | AATriangle::DEFORM_BOTTOM:
+    case AATriangle::SOUTHEAST | AATriangle::DEFORM_LEFT:
+    case AATriangle::NORTHWEST | AATriangle::DEFORM_BOTTOM:
+    case AATriangle::NORTHWEST | AATriangle::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:
+    case AATriangle::SOUTHWEST | AATriangle::DEFORM_BOTTOM:
+    case AATriangle::SOUTHWEST | AATriangle::DEFORM_RIGHT:
+    case AATriangle::NORTHEAST | AATriangle::DEFORM_BOTTOM:
+    case AATriangle::NORTHEAST | AATriangle::DEFORM_RIGHT:
       tile_x = tile_bbox.get_right ();
       tile_y = tile_bbox.get_bottom ();
       gradient = 1.0;
@@ -1156,6 +1178,7 @@ int check_position_unisolid (const Rectf& obj_bbox,
 
     default:
       assert (23 == 42);
+      return POS_NON_SOLID;
   }
 
   /* delta_x, delta_y: Gradient aware version of SHIFT_DELTA. Here, we set the
@@ -1203,15 +1226,15 @@ int check_position_unisolid (const Rectf& obj_bbox,
       delta_y *= .70710678118654752440; /* 1/sqrt(2) */
       break;
 
-    case DEFORM_BOTTOM:
-    case DEFORM_TOP:
+    case AATriangle::DEFORM_BOTTOM:
+    case AATriangle::DEFORM_TOP:
       delta_x *= .44721359549995793928; /* 1/sqrt(5) */
       delta_y *= .89442719099991587856; /* 2/sqrt(5) */
       gradient *= 0.5;
       break;
 
-    case DEFORM_LEFT:
-    case DEFORM_RIGHT:
+    case AATriangle::DEFORM_LEFT:
+    case AATriangle::DEFORM_RIGHT:
       delta_x *= .89442719099991587856; /* 2/sqrt(5) */
       delta_y *= .44721359549995793928; /* 1/sqrt(5) */
       gradient *= 2.0;
@@ -1272,16 +1295,18 @@ Sector::collision_tilemap(collision::Constraints* constraints,
 
         // only handle unisolid when the player is falling down and when he was
         // above the tile before
-        if(tile->getAttributes() & Tile::UNISOLID) {
+        if(tile->is_unisolid ()) {
           int status;
+          Vector relative_movement = movement
+            - solids->get_movement(/* actual = */ true);
 
           /* 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
+           * 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);
+          status = check_movement_unisolid (relative_movement, tile);
           /* If zero is returned, the unisolid tile is non-solid. */
           if (status == 0)
             continue;
@@ -1294,16 +1319,18 @@ Sector::collision_tilemap(collision::Constraints* constraints,
             continue;
         }
 
-        if(tile->getAttributes() & Tile::SLOPE) { // slope tile
+        if(tile->is_slope ()) { // slope tile
           AATriangle triangle;
           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());
+          collision::rectangle_aatriangle(constraints, dest, triangle,
+              solids->get_movement(/* actual = */ false));
         } else { // normal rectangular tile
-          check_collisions(constraints, movement, dest, tile_bbox, NULL, NULL, solids->get_movement());
+          check_collisions(constraints, movement, dest, tile_bbox, NULL, NULL,
+              solids->get_movement(/* actual = */ false));
         }
       }
     }
@@ -1458,17 +1485,17 @@ Sector::collision_static_constrains(MovingObject& object)
       break;
 
     // apply calculated horizontal constraints
-    if(constraints.bottom < infinity) {
-      float height = constraints.bottom - constraints.top;
+    if(constraints.get_position_bottom() < infinity) {
+      float height = constraints.get_height ();
       if(height < oheight) {
         // we're crushed, but ignore this for now, we'll get this again
         // later if we're really crushed or things will solve itself when
         // looking at the vertical constraints
       }
-      dest.p2.y = constraints.bottom - DELTA;
+      dest.p2.y = constraints.get_position_bottom() - DELTA;
       dest.p1.y = dest.p2.y - oheight;
-    } else if(constraints.top > -infinity) {
-      dest.p1.y = constraints.top + DELTA;
+    } else if(constraints.get_position_top() > -infinity) {
+      dest.p1.y = constraints.get_position_top() + DELTA;
       dest.p2.y = dest.p1.y + oheight;
     }
   }
@@ -1490,12 +1517,12 @@ Sector::collision_static_constrains(MovingObject& object)
       break;
 
     // apply calculated vertical constraints
-    float width = constraints.right - constraints.left;
+    float width = constraints.get_width ();
     if(width < infinity) {
       if(width + SHIFT_DELTA < owidth) {
 #if 0
         printf("Object %p crushed horizontally... L:%f R:%f\n", &object,
-               constraints.left, constraints.right);
+               constraints.get_position_left(), constraints.get_position_right());
 #endif
         CollisionHit h;
         h.left = true;
@@ -1503,15 +1530,15 @@ Sector::collision_static_constrains(MovingObject& object)
         h.crush = true;
         object.collision_solid(h);
       } else {
-        float xmid = (constraints.left + constraints.right) / 2;
+        float xmid = constraints.get_x_midpoint ();
         dest.p1.x = xmid - owidth/2;
         dest.p2.x = xmid + owidth/2;
       }
-    } else if(constraints.right < infinity) {
-      dest.p2.x = constraints.right - DELTA;
+    } else if(constraints.get_position_right() < infinity) {
+      dest.p2.x = constraints.get_position_right() - DELTA;
       dest.p1.x = dest.p2.x - owidth;
-    } else if(constraints.left > -infinity) {
-      dest.p1.x = constraints.left + DELTA;
+    } else if(constraints.get_position_left() > -infinity) {
+      dest.p1.x = constraints.get_position_left() + DELTA;
       dest.p2.x = dest.p1.x + owidth;
     }
   }
@@ -1526,8 +1553,8 @@ Sector::collision_static_constrains(MovingObject& object)
   // an extra pass to make sure we're not crushed horizontally
   constraints = Constraints();
   collision_static(&constraints, movement, dest, object);
-  if(constraints.bottom < infinity) {
-    float height = constraints.bottom - constraints.top;
+  if(constraints.get_position_bottom() < infinity) {
+    float height = constraints.get_height ();
     if(height + SHIFT_DELTA < oheight) {
 #if 0
       printf("Object %p crushed vertically...\n", &object);
@@ -1676,9 +1703,9 @@ Sector::is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid) const
         if(!tile) continue;
         if(!(tile->getAttributes() & Tile::SOLID))
           continue;
-        if((tile->getAttributes() & Tile::UNISOLID) && ignoreUnisolid)
+        if(tile->is_unisolid () && ignoreUnisolid)
           continue;
-        if(tile->getAttributes() & Tile::SLOPE) {
+        if(tile->is_slope ()) {
           AATriangle triangle;
           Rectf tbbox = solids->get_tile_bbox(x, y);
           triangle = AATriangle(tbbox, tile->getData());
@@ -1892,4 +1919,52 @@ Sector::get_gravity() const
   return gravity;
 }
 
+Player*
+Sector::get_nearest_player (const Vector& pos)
+{
+  Player *nearest_player = NULL;
+  float nearest_dist = std::numeric_limits<float>::max();
+
+  std::vector<Player*> players = Sector::current()->get_players();
+  for (std::vector<Player*>::iterator playerIter = players.begin();
+      playerIter != players.end();
+      ++playerIter)
+  {
+    Player *this_player = *playerIter;
+    if (this_player->is_dying() || this_player->is_dead())
+      continue;
+
+    float this_dist = this_player->get_bbox ().distance(pos);
+
+    if (this_dist < nearest_dist) {
+      nearest_player = this_player;
+      nearest_dist = this_dist;
+    }
+  }
+
+  return nearest_player;
+} /* Player *get_nearest_player */
+
+std::vector<MovingObject*>
+Sector::get_nearby_objects (const Vector& center, float max_distance)
+{
+  std::vector<MovingObject*> ret;
+  std::vector<Player*> players = Sector::current()->get_players();
+
+  for (size_t i = 0; i < players.size (); i++) {
+    float distance = players[i]->get_bbox ().distance (center);
+    if (distance <= max_distance)
+      ret.push_back (players[i]);
+  }
+
+  for (size_t i = 0; i < moving_objects.size (); i++) {
+    float distance = moving_objects[i]->get_bbox ().distance (center);
+    if (distance <= max_distance)
+      ret.push_back (moving_objects[i]);
+  }
+
+  return (ret);
+}
+
+/* vim: set sw=2 sts=2 et : */
 /* EOF */