// $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
// 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"
+#include "lisp/writer.hpp"
#include "lisp/lisp.hpp"
#include "lisp/list_iterator.hpp"
#include "object_factory.hpp"
+#include "log.hpp"
#include <assert.h>
#include <iostream>
#include <stdexcept>
+#include <sstream>
-// snap to destination if within EPSILON pixels
-#define EPSILON 1.5
-
-Path::Path(const lisp::Lisp& reader)
+Path::Path()
{
- 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 lisp::Lisp& 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") {
- std::cerr << "Warning: unknown token '" << iter.item() << "' in Path nodes list. Ignored." << std::endl;
+ 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()
-{
- registry.erase(name);
-}
-
- void
-Path::update(float elapsed_time)
+void
+Path::write(lisp::Writer& writer)
{
-
- // 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;
- }
- }
- }
-
- PathNode dn = pathNodes[destinationNode];
- timeToGo = dn.time;
- velocity = (dn.position - position) / timeToGo;
+ 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:
+ log_warning << "Don't know how to write mode " << (int) mode << " ?!?" << std::endl;
+ break;
}
- // move according to stored velocity
- last_movement = velocity * elapsed_time;
- position += last_movement;
- timeToGo -= elapsed_time;
+ for (size_t i=0; i < nodes.size(); i++) {
+ const Node& node = nodes[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);
- // stop when we arrive at our destination
- PathNode dn = pathNodes[destinationNode];
- if ((position - dn.position).norm() < EPSILON) {
- velocity = Vector(0,0);
+ writer.end_list("node");
}
+ writer.end_list("path");
}
-void
-Path::draw(DrawingContext& )
+Vector
+Path::get_base() const
{
- // TODO: Add a visible flag, draw the path if true
-}
+ if(nodes.empty())
+ return Vector(0, 0);
-const Vector&
-Path::GetPosition() {
- return position;
+ return nodes[0].position;
}
-const Vector&
-Path::GetLastMovement() {
- return last_movement;
+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;
+ }
+ }
+ return nearest_node_id;
}
-
-//////////////////////////////////////////////////////////////////////////////
-// static stuff
-
-std::map<std::string,Path*> 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");