////////////////////////////////////////////////////////////////////////////////
//    Scorched3D (c) 2000-2011, 2025, 2026
//
//    This file is part of Scorched3D.
//
//    Scorched3D 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.
//
//    Scorched3D 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.,
//    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
////////////////////////////////////////////////////////////////////////////////

#ifndef __INCLUDE_HeightMap_hpp_INCLUDE__
#define __INCLUDE_HeightMap_hpp_INCLUDE__

#include <stdlib.h>
#include <common/ProgressCounter.hpp>
#include <common/FixedVector.hpp>
#include <common/Vector.hpp>

class Line;
class GraphicalHeightMap;

class HeightMap
{
public:
	HeightMap();
	virtual ~HeightMap();

	void create( const int width, const int height, bool invertedNormals );
	void reset();

	// clang-format off
	// uncrustify off
	// Height map size fns
	int getMapWidth()  { return width_; }
	int getMapHeight() { return height_; }
	// uncrustify on
	// clang-format on

	// Get height fns (z values)
	inline fixed getHeight( int w, int h )
	{
		if ( 0 <= w && w <= width_ && 0 <= h && h <= height_ ) return heightData_[( width_ + 1 ) * h + w].position[2];

		return fixed( 0 );
	}

	fixed getInterpHeight( fixed w, fixed h );

	// Get normal functions
	FixedVector& getNormal( int w, int h );
	void         getInterpNormal( fixed w, fixed h, FixedVector& normal );

	bool getIntersect( Line& direction, Vector& intersect );

	// Alters the actual internal HeightMap points
	void setHeight( int w, int h, fixed height );

	// clang-format off
	// uncrustify off
	GraphicalHeightMap* getGraphicalMap()                          { return graphicalMap_; }
	void                setGraphicalMap( GraphicalHeightMap* map ) { graphicalMap_ = map; }
	// uncrustify on
	// clang-format on

protected:
	struct HeightData
	{
		FixedVector position;
		FixedVector normal;
	};

	int                 width_;
	int                 height_;
	HeightData*         heightData_;
	GraphicalHeightMap* graphicalMap_;
	bool                invertedNormals_;

	bool getVector( FixedVector& vec, int x, int y );
	void getVectorPos( int pos, int& x, int& y, int dist = 1 );
};

#endif  // __INCLUDE_HeightMap_hpp_INCLUDE__
