#ifndef _map_h
#define _map_h

extern unsigned short MAP_TILE_SIZE;
extern unsigned short XTILES;
extern unsigned short YTILES;
extern unsigned short MAP_MAX_L;
extern unsigned short MAP_MAX_H;
extern unsigned short XMIN;
extern unsigned short YMIN;

#define UNREACHABLE 9999

typedef struct animtiles
{
	short set, num, framenum;
	float frameinc;
} animtiles;

class _tile
{
	short unit;
	short set, number;
	short step;  //this should never go above what UNREACHABLE is defined as
	short temp;  //neither should this
	short animated;
	short owner;
	short oldset, oldnum;
	float animframe;
	float burnframe;
public:
	_tile();
	
	bool burning;
	short turnsused;  //for cities (or "oil derricks" as they're being called now), to make cash limited
	short burnedfor;
	void reset();
	void change_type(int new_set, int new_number);
	void change_owner(int plyr);
	void change_to_city();
	void draw(int x, int y);
	void draw_unit(int x, int y, _unit *other_unit_here);
	void set_unit(int plyr, int number);
	void set_temp(short t);
	void set_step(short s);
	bool is_building() const;
	bool is_building(int type) const;
	bool is_unit_producing() const;
	bool is_HQ() const;
	bool owned_by(int plyr) const;
	bool same_type(int t) const;
	bool same_type(int tset, int tnum) const;
	short get_temp() const;
	short get_step() const;
	_unit *unit_here_ptr();
	int unit_here() const;
	int get_owner() const;
	int get_set() const;
	int get_number() const;
	int building_type() const;
	short x, y;
};

class _camera
{
	int scrollx, scrolly;
	int offsetx, offsety;
public:
	_camera();
	
	void scroll(int offset_x, int offset_y);
	void jump_to(int tile_x, int tile_y);
	void jump_to_x(int tile_x);
	void jump_to_y(int tile_y);
	
	int get_scroll_x() const;
	int get_scroll_y() const;
	int get_offset_x() const;
	int get_offset_y() const;
};

class _map
{
	string name;
	int length, height, numplayers;
	_tile *tiles;
	void create_pathmap(int plyr, int mtype, int startx, int starty, bool limited);
	void pathmap_recursive(int plyr, int mtype, int ox, int oy, int moves, bool limited);
	
public:
	_map();
	_map(int width, int height);
	_map(const string &filename);
	_map(const _map &m);
	~_map();
	
	_map &operator=(const _map &m);
	
	_camera camera;
	
	void close();
	bool load(const string &pathname, bool reset_players = true);
	void resize(int newlength, int newheight);
	
	int get_length() const;
	int get_height() const;
	void get_dimensions(unsigned int *length, unsigned int *height) const;
	
	int get_num_players() const;
	void force_numplayers(int players);
	void refresh_numplayers();
	
	bool tile_in_bounds(int tilex, int tiley) const;
	_tile *get_mouseover_tile(int mx, int my);
	_tile *get_tile(int tilex, int tiley);
	
	string get_name() const;
	void set_name(string new_name);
	
	void draw(bool showunits = true);
	
	void create_pathmap(int playernum, int movetype, int startx, int starty);
	void create_limited_pathmap(int playernum, int movetype, int startx, int starty, int limit);
	
	void backup_pathmap();
	void restore_pathmap();
};

void load_map(string mapname, const char *path, bool reset_players = true);

#endif
