Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / path.cpp
index 8d2b9f9..c69e1a5 100644 (file)
@@ -1,39 +1,32 @@
-//  $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 <config.h>
-
-#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 <stdexcept>
 #include <sstream>
+#include <stdexcept>
+
+#include "lisp/list_iterator.hpp"
+#include "util/log.hpp"
 
-Path::Path()
+Path::Path() :
+  nodes(),
+  mode()
 {
 }
 
@@ -42,7 +35,7 @@ Path::~Path()
 }
 
 void
-Path::read(const lisp::Lisp& reader)
+Path::read(const Reader& reader)
 {
   lisp::ListIterator iter(&reader);
 
@@ -66,9 +59,9 @@ Path::read(const lisp::Lisp& reader)
       }
       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();
@@ -77,7 +70,7 @@ Path::read(const lisp::Lisp& reader)
     Node node;
     node.time = 1;
     if( (!node_lisp->get("x", node.position.x) ||
-          !node_lisp->get("y", node.position.y)))
+         !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);
 
@@ -91,46 +84,45 @@ Path::read(const lisp::Lisp& reader)
     throw std::runtime_error("Path with zero nodes");
 }
 
-void
-Path::write(lisp::Writer& writer)
+Vector
+Path::get_base() const
 {
-  writer.start_list("path");
-
-  switch(mode) {
-    case ONE_SHOT:
-      writer.write_string("mode", "oneshot");
-      break;
-    case PING_PONG:
-      writer.write_string("mode", "pingpong");
-      break;
-    case CIRCULAR:
-      writer.write_string("mode", "circular");
-      break;
-    default:
-      msg_warning("Don't know how to write mode " << (int) mode << " ?!?");
-      break;
-  }
-
-  for (size_t i=0; i < nodes.size(); i++) {
-    const Node& node = nodes[i];
+  if(nodes.empty())
+    return Vector(0, 0);
 
-    writer.start_list("node");
-    writer.write_float("x", node.position.x);
-    writer.write_float("y", node.position.y);
-    writer.write_float("time", node.time);
+  return nodes[0].position;
+}
 
-    writer.end_list("node");
+int
+Path::get_nearest_node_no(Vector reference_point) const
+{
+  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;
+    }
   }
-
-  writer.end_list("path");
+  return nearest_node_id;
 }
 
-Vector
-Path::get_base() const
+int
+Path::get_farthest_node_no(Vector reference_point) const
 {
-  if(nodes.empty())
-    return Vector(0, 0);
-  
-  return nodes[0].position;
+  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;
 }
 
+/* EOF */