Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / path.cpp
index 941e9d0..c69e1a5 100644 (file)
-//  $Id$
-// 
-//  SuperTux
+//  SuperTux Path
 //  Copyright (C) 2005 Philipp <balinor@pnxs.de>
+//  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
+//  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
 //
-//  This program is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU General Public License
-//  as published by the Free Software Foundation; either version 2
-//  of the License, or (at your option) any later version.
+//  This program is free software: you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
 //
 //  This program is distributed in the hope that it will be useful,
 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 //  GNU General Public License for more details.
-// 
+//
 //  You should have received a copy of the GNU General Public License
-//  along with this program; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-//  02111-1307, USA.
-
-#include "path.hpp"
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-#include "lisp/lisp.hpp"
-#include "lisp/list_iterator.hpp"
-#include "object_factory.hpp"
+#include "object/path.hpp"
 
-#include <assert.h>
+#include <sstream>
+#include <stdexcept>
 
+#include "lisp/list_iterator.hpp"
+#include "util/log.hpp"
 
-// some constants
-#define DEFAULT_PIXELS_PER_SECOND      50
-#define EPSILON                                1.5
+Path::Path() :
+  nodes(),
+  mode()
+{
+}
 
-Path::Path(const lisp::Lisp& reader)
+Path::~Path()
 {
-  forward = true;
-  float x,y;
+}
 
+void
+Path::read(const Reader& reader)
+{
   lisp::ListIterator iter(&reader);
 
-  assert (iter.next());
-  std::string token = iter.item();
-  assert(token == "name");
-  iter.value()->get(name);
-
-  circular = true;
-  assert (iter.next());
-  token = iter.item();
-  if (token == "circular") {
-    iter.value()->get(circular);
-    iter.next();
-  }
-
-  pixels_per_second = DEFAULT_PIXELS_PER_SECOND;
-  assert (iter.next());
-  token = iter.item();
-  if (token == "speed") {
-    iter.value()->get(pixels_per_second);
-    iter.next();
-  }
-  do {
-    token = iter.item();
-    if(token == "x") {
-      iter.value()->get(x);
-    } else if(token == "y") {
-      iter.value()->get(y);
-      points.push_back(Vector(x,y));
+  mode = CIRCULAR;
+  while(iter.next()) {
+    if(iter.item() == "mode") {
+      std::string mode_string;
+      if(!iter.value()->get(mode_string))
+        throw std::runtime_error("Pathmode not a string");
+
+      if(mode_string == "oneshot")
+        mode = ONE_SHOT;
+      else if(mode_string == "pingpong")
+        mode = PING_PONG;
+      else if(mode_string == "circular")
+        mode = CIRCULAR;
+      else {
+        std::ostringstream msg;
+        msg << "Unknown pathmode '" << mode_string << "' found";
+        throw std::runtime_error(msg.str());
+      }
+      continue;
     }
-  } while(iter.next());
 
-  next_target = points.begin();
-  pos = *next_target;
-
-  calc_next_velocity();
+    if(iter.item() != "node") {
+      log_warning << "unknown token '" << iter.item() << "' in Path nodes list. Ignored." << std::endl;
+      continue;
+    }
+    const lisp::Lisp* node_lisp = iter.lisp();
 
-  // register this path for lookup:
-  registry[name] = this;
-}
+    // each new node will inherit all values from the last one
+    Node node;
+    node.time = 1;
+    if( (!node_lisp->get("x", node.position.x) ||
+         !node_lisp->get("y", node.position.y)))
+      throw std::runtime_error("Path node without x and y coordinate specified");
+    node_lisp->get("time", node.time);
 
-Path::~Path()
-{
-  registry.erase(name);
-}
+    if(node.time <= 0)
+      throw std::runtime_error("Path node with non-positive time");
 
-void
-Path::update(float elapsed_time)
-{
-  last_movement = velocity * elapsed_time;
-  pos += last_movement;
-  if ((pos - *next_target).norm() < EPSILON) {
-    pos = *next_target;
-    calc_next_velocity();
+    nodes.push_back(node);
   }
-}
-
-void
-Path::draw(DrawingContext& context)
-{
-   // TODO: Add a visible flag, draw the path if true
-}
 
-const Vector&
-Path::GetPosition() {
-  return pos;
+  if (nodes.empty())
+    throw std::runtime_error("Path with zero nodes");
 }
 
-const Vector&
-Path::GetStart() {
-  return *(points.begin());
-}
+Vector
+Path::get_base() const
+{
+  if(nodes.empty())
+    return Vector(0, 0);
 
-const Vector&
-Path::GetLastMovement() {
-  return last_movement;
+  return nodes[0].position;
 }
 
-void
-Path::calc_next_velocity()
+int
+Path::get_nearest_node_no(Vector reference_point) const
 {
-  Vector distance;
-
-  if (circular) {
-    ++next_target;
-    if (next_target == points.end()) {
-      next_target = points.begin();
+  int nearest_node_id = -1;
+  float nearest_node_dist = 0;
+  int id = 0;
+  for (std::vector<Node>::const_iterator i = nodes.begin(); i != nodes.end(); i++, id++) {
+    float dist = (i->position - reference_point).norm();
+    if ((nearest_node_id == -1) || (dist < nearest_node_dist)) {
+      nearest_node_id = id;
+      nearest_node_dist = dist;
     }
   }
-  else if (forward) {
-    ++next_target;
-    if (next_target == points.end()) {
-      forward = false;
-    }
-  }
-  else {
-    //FIXME: Implement going backwards on the list
-    //       I have no f***ing idea how this is done in C++
-  }
-
-  distance = *next_target - pos;
-  velocity = distance.unit() * pixels_per_second;
+  return nearest_node_id;
 }
 
-//////////////////////////////////////////////////////////////////////////////
-// static stuff
-
-PathRegistry Path::registry;
-
-Path*
-Path::GetByName(const std::string& name) {
-  return registry[name];
+int
+Path::get_farthest_node_no(Vector reference_point) const
+{
+  int farthest_node_id = -1;
+  float farthest_node_dist = 0;
+  int id = 0;
+  for (std::vector<Node>::const_iterator i = nodes.begin(); i != nodes.end(); i++, id++) {
+    float dist = (i->position - reference_point).norm();
+    if ((farthest_node_id == -1) || (dist > farthest_node_dist)) {
+      farthest_node_id = id;
+      farthest_node_dist = dist;
+    }
+  }
+  return farthest_node_id;
 }
 
-IMPLEMENT_FACTORY(Path, "path");
+/* EOF */