/*
 * pathmanager.h
 *
 * Copyright (C) 2003 Atomic Blue (info@planeshift.it, http://www.atomicblue.org) 
 *
 *
 * 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 (version 2 of the License)
 * 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.
 *
 */
#ifndef __PATHMANAGER_H__
#define __PATHMANAGER_H__

#include <csgeom/path.h>

#include "util/prb.h"
#include "util/growarray.h"

struct iDocumentNode;
class csPath;

/**
 * A SplinePoint is vertex on a csPath spline
 * which specifies a 3d coordinate AND an "up"
 * vector to define how the model should twist
 * as it goes.
 */
class SplinePoint
{
public:
    csVector3 pos;
    csVector3 up;
    float time_value;
    csString action;
};

PS_TYPEDEF_GROWING_ARRAY(SplinePointVector, SplinePoint*);

class Spline
{
protected:
    csString name;
    float constant_vel;

public:
    SplinePointVector points;
    csString sector;

    Spline() {}
    Spline(const char *n) { name=n; constant_vel=false; }
    ~Spline() {}

    bool Load(iDocumentNode *node);

    bool operator==(Spline& other) const
    { return name==other.name; }
    bool operator<(Spline& other) const
    { return (strcmp(name,other.name)<0); }
};

/**
 * This class override the CS interpolated forward
 * function to get a derived forward possition instead
 * of a catmullrom interpolated forward vector that
 * never could have been correct since the derived
 * for a catmullrom isn't a catmullrom. So until
 * someone fix CS this helper class will give what
 * what we would like for paths.
 */
class psPath : public csPath
{
public:
    csVector3 heading;
    psPath (int p) : csPath(p){ }

    /// Calculate internal values for this spline given some time value.
    virtual void CalculateAtTime (float time);
    
    /// Get the interpolated forward vector.
    virtual void GetInterpolatedForward (csVector3& pos);

};


/**
 * This class stores the splines, makes csPaths
 * from it, and makes interpolated merged csPaths
 * when transitioning from one to another.
 */
class PathManager
{
protected:
    BinaryRBTree<Spline> paths;

public:
    PathManager() { };

    bool Load(iDocumentNode *node);
    csPath *CreatePath(const char *name,csVector3& offset,csVector3& startpt);
    Spline *Find(const char *name);
};


#endif

