/*
 * location.h
 *
 * Copyright (C) 2004 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 __LOCATION_H__
#define __LOCATION_H__

#include <csutil/csstring.h>
#include <csgeom/vector3.h>
#include <csutil/parray.h>
#include <iengine/sector.h>
#include <csutil/weakref.h>

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

/**
 * A Location is a named place on the map, located
 * dynamically by NPCs as scripted.
 */
class Location
{
public:
    csVector3           pos;
    float               rot_angle;
    float               radius;
    csString            sectorName;

    csWeakRef<iSector>  sector; // Cached sector
    
    /// Return cached sector or find the sector and cache it from engine.
    iSector*            GetSector(iEngine * engine);
};

/**
 * This stores a vector of positions listing a set of 
 * points defining a common type of location, such as
 * a list of burning fires or guard stations--whatever
 * the NPCs need.
 */
class LocationType
{
public:
    csString name;
    csPDelArray<Location> locs;
    bool region;
    bool operator==(LocationType& other) const
    { return name==other.name; }
    bool operator<(LocationType& other) const
    { return (strcmp(name,other.name)<0); }

    bool Load(iDocumentNode *node);
    bool CheckWithinBounds(csVector3& pos);
};

/**
 * A waypoint is a specified circle on the map with a name,
 * location, and a list of waypoints it is connected to.  With
 * this class, a network of nodes can be created which will
 * allow for pathfinding and path wandering by NPCs.
 */
class Waypoint
{
public:
    int                id;
    csString           name;
    Location           loc;
    bool               allow_return; /// This prevents the link back to the prior waypoint from being chosen, if true.
    csArray<Waypoint*> links;        /// Not csPDelArray because this does not own the objects
    csArray<float>     dists;        /// Distances of each link. 

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

    bool Load(iDocumentNode *node, iEngine *engine);
    bool Load(iResultRow& row, iEngine *engine); 

    /// Get the sector from the location.
    iSector*            GetSector(iEngine * engine) { return loc.GetSector(engine); }

    bool CheckWithin(iEngine * engine, const csVector3& pos, const iSector* sector);

    /// Data used in the dijkstra's algorithm to find waypoint path
    float distance; /// Hold current shortest distance to the start WP.
    Waypoint * pi;  /// Predecessor WP to track shortest way back to start.
    
};

#endif

