1 /***************************************************************************
2  *   Copyright (C) 2010 by Andrey Afletdinov <fheroes2@gmail.com>          *
3  *                                                                         *
4  *   Part of the Free Heroes2 Engine:                                      *
5  *   http://sourceforge.net/projects/fheroes2                              *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  *   This program is distributed in the hope that it will be useful,       *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15  *   GNU General Public License for more details.                          *
16  *                                                                         *
17  *   You should have received a copy of the GNU General Public License     *
18  *   along with this program; if not, write to the                         *
19  *   Free Software Foundation, Inc.,                                       *
20  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21  ***************************************************************************/
22 
23 #ifndef H2BATTLE_CELL_H
24 #define H2BATTLE_CELL_H
25 
26 #include <utility>
27 
28 #include "math_base.h"
29 #include "types.h"
30 
31 #define CELLW 44
32 #define CELLH 52
33 
34 namespace Battle
35 {
36     class Unit;
37 
38     enum direction_t
39     {
40         UNKNOWN = 0x00,
41         TOP_LEFT = 0x01,
42         TOP_RIGHT = 0x02,
43         RIGHT = 0x04,
44         BOTTOM_RIGHT = 0x08,
45         BOTTOM_LEFT = 0x10,
46         LEFT = 0x20,
47         CENTER = 0x40,
48         RIGHT_SIDE = TOP_RIGHT | RIGHT | BOTTOM_RIGHT,
49         LEFT_SIDE = TOP_LEFT | LEFT | BOTTOM_LEFT,
50         AROUND = RIGHT_SIDE | LEFT_SIDE
51     };
52 
53     class Cell
54     {
55     public:
56         explicit Cell( int32_t );
57 
58         void ResetQuality( void );
59         void resetReachability();
60 
61         void SetObject( int );
62         void SetQuality( u32 );
63 
64         void setReachableForHead();
65         void setReachableForTail();
66 
67         void SetArea( const fheroes2::Rect & );
68 
69         bool isReachableForHead() const;
70         bool isReachableForTail() const;
71 
72         bool isPassable4( const Unit &, const Cell & ) const;
73         bool isPassable3( const Unit &, bool check_reflect ) const;
74         bool isPassable1( bool check_troop ) const;
75         bool isPositionIncludePoint( const fheroes2::Point & ) const;
76 
77         s32 GetIndex( void ) const;
78         const fheroes2::Rect & GetPos( void ) const;
79         int GetObject( void ) const;
80         s32 GetQuality( void ) const;
81         direction_t GetTriangleDirection( const fheroes2::Point & ) const;
82 
83         const Unit * GetUnit( void ) const;
84         Unit * GetUnit( void );
85         void SetUnit( Unit * );
86 
87     private:
88         s32 index;
89         fheroes2::Rect pos;
90         int object;
91         bool _reachableForHead;
92         bool _reachableForTail;
93         s32 quality;
94         Unit * troop;
95         fheroes2::Point coord[7];
96     };
97 
98     class Position : protected std::pair<Cell *, Cell *>
99     {
100     public:
Position()101         Position()
102             : std::pair<Cell *, Cell *>( nullptr, nullptr )
103         {}
104 
105         void Set( s32 head, bool wide, bool reflect );
106         void Swap( void );
107         bool isReflect( void ) const;
108         bool contains( int cellIndex ) const;
109 
110         // Returns the position that the given unit would occupy after moving to the given index
111         static Position GetPositionWhenMoved( const Unit & unit, const int32_t dst );
112 
113         // Returns the reachable position for the current unit (to which the current
114         // passability information relates) which corresponds to the given index or
115         // an empty Position object if the given index is unreachable
116         static Position GetReachable( const Unit & currentUnit, const int32_t dst );
117 
118         fheroes2::Rect GetRect( void ) const;
119         Cell * GetHead( void );
120         const Cell * GetHead( void ) const;
121         Cell * GetTail( void );
122         const Cell * GetTail( void ) const;
123     };
124 }
125 
126 #endif
127