Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / path.cpp
index c16f1de..c69e1a5 100644 (file)
-//  $Id$
-// 
 //  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 "msg.hpp"
+#include "object/path.hpp"
 
-#include <assert.h>
-#include <iostream>
+#include <sstream>
 #include <stdexcept>
 
-// snap to destination if within EPSILON pixels
-#define EPSILON 1.5
+#include "lisp/list_iterator.hpp"
+#include "util/log.hpp"
 
-Path::Path(const lisp::Lisp& reader)
+Path::Path() :
+  nodes(),
+  mode()
 {
-  circular = true;
-  forward = true;
-
-  if (!reader.get("name", name)) throw std::runtime_error("Path without name");
-  reader.get("circular", circular);
-  reader.get("forward", forward);
-
-  const lisp::Lisp* nodes_lisp = reader.get_lisp("nodes");
-  if(!nodes_lisp) throw std::runtime_error("Path without nodes");
+}
 
-  lisp::ListIterator iter(nodes_lisp);
+Path::~Path()
+{
+}
 
-  PathNode node;
-  node.time = 1;
+void
+Path::read(const Reader& reader)
+{
+  lisp::ListIterator iter(&reader);
 
+  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;
+    }
+
     if(iter.item() != "node") {
-      msg_warning("unknown token '" << iter.item() << "' in Path nodes list. Ignored.");
+      log_warning << "unknown token '" << iter.item() << "' in Path nodes list. Ignored." << std::endl;
       continue;
     }
     const lisp::Lisp* node_lisp = iter.lisp();
 
     // each new node will inherit all values from the last one
-    node_lisp->get("x", node.position.x);
-    node_lisp->get("y", node.position.y);
+    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);
 
-    if(node.time <= 0) throw std::runtime_error("Path node with non-positive time");
+    if(node.time <= 0)
+      throw std::runtime_error("Path node with non-positive time");
 
-    pathNodes.push_back(node);
+    nodes.push_back(node);
   }
 
-  if (pathNodes.size() < 1) throw std::runtime_error("Path with zero nodes");
-
-  // initial position and velocity will be set with the first update, as timeToGo is initialized to 0.
-  destinationNode = 0;
-
-  // register this path for lookup:
-  registry[name] = this;
+  if (nodes.empty())
+    throw std::runtime_error("Path with zero nodes");
 }
 
-Path::~Path()
+Vector
+Path::get_base() const
 {
-  registry.erase(name);
+  if(nodes.empty())
+    return Vector(0, 0);
+
+  return nodes[0].position;
 }
 
-       void
-Path::update(float elapsed_time)
+int
+Path::get_nearest_node_no(Vector reference_point) const
 {
-
-  // TODO: carry excess time over to next node? This is how it was done in camera.cpp:
-  /*
-  if(auto_t - elapsed_time >= 0) {
-    translation += current_dir * elapsed_time;
-    auto_t -= elapsed_time;
-  } else {
-    // do the rest of the old movement
-    translation += current_dir * auto_t;
-    elapsed_time -= auto_t;
-    auto_t = 0;
-
-    // construct path for next point
-    if(auto_idx+1 >= scrollpoints.size()) {
-      keep_in_bounds(translation);
-      return;
-    }
-    Vector distance = scrollpoints[auto_idx+1].position 
-                      - scrollpoints[auto_idx].position;
-    current_dir = distance.unit() * scrollpoints[auto_idx].speed;
-    auto_t = distance.norm() / scrollpoints[auto_idx].speed;
-
-    // do movement for the remaining time
-    translation += current_dir * elapsed_time;
-    auto_t -= elapsed_time;
-    auto_idx++;
-  }
-  */
-
-  // advance to next node at scheduled time
-  if (timeToGo <= 0) {
-    position = pathNodes[destinationNode].position;
-
-    // set destinationNode to next node
-    if (forward) {
-      destinationNode++;
-      if (destinationNode >= (int)pathNodes.size()) {
-       if (circular) {
-         destinationNode = 0;
-       } else {
-         destinationNode = (int)pathNodes.size()-1;
-       }
-      }
-    } else {
-      destinationNode--;
-      if (destinationNode < 0) {
-       if (circular) {
-         destinationNode = (int)pathNodes.size()-1;
-       } else {
-         destinationNode = 0;
-       }
-      }
+  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;
     }
-
-    PathNode dn = pathNodes[destinationNode];
-    timeToGo = dn.time;
-    velocity = (dn.position - position) / timeToGo;
   }
-
-  // move according to stored velocity
-  last_movement = velocity * elapsed_time;
-  position += last_movement;
-  timeToGo -= elapsed_time;
-
-  // stop when we arrive at our destination
-  PathNode dn = pathNodes[destinationNode];
-  if ((position - dn.position).norm() < EPSILON) {
-    velocity = Vector(0,0);
-  }
-
+  return nearest_node_id;
 }
 
-void
-Path::draw(DrawingContext& )
+int
+Path::get_farthest_node_no(Vector reference_point) const
 {
-  // TODO: Add a visible flag, draw the path if true
-}
-
-void
-Path::write(lisp::Writer& writer)
-{
-  writer.start_list("path");
-
-  writer.write_string("name", name);
-  writer.write_bool("circular", circular);
-  writer.write_bool("forward", forward);
-
-  for (int i=0; i < (int)pathNodes.size(); i++) {
-    PathNode node = pathNodes[i];
-
-    writer.start_list("node");
-    writer.write_float("x", node.position.x);
-    writer.write_float("y", node.position.y);
-    writer.write_float("time", node.time);
-
-    writer.end_list("node");
+  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;
+    }
   }
-
-  writer.end_list("path");
-}
-
-const Vector&
-Path::GetPosition() {
-  return position;
-}
-
-const Vector&
-Path::GetLastMovement() {
-  return last_movement;
-}
-
-const std::string
-Path::GetName() {
-  return name;
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// static stuff
-
-std::map<std::string,Path*> Path::registry;
-
-Path*
-Path::GetByName(const std::string& name) {
-  return registry[name];
+  return farthest_node_id;
 }
 
-IMPLEMENT_FACTORY(Path, "path");
+/* EOF */