initial commit, lordsawar source, slightly modified
[lordsawar] / src / FogMap.h
1 //  Copyright (C) 2007, 2008, 2009 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
16 //  02110-1301, USA.
17
18 #ifndef FOGMAP_H
19 #define FOGMAP_H
20
21 #include <list>
22 #include "vector.h"
23
24 class XML_Helper;
25 class SightMap;
26 class Player;
27
28 //! What a player can see on a hidden map.
29 /**
30  * 
31  * Map that represents fog.  Overlays regular SmallMap. 
32  */
33 class FogMap
34 {
35     public:
36         //! The xml tag of this object in a saved-game file.
37         static std::string d_tag; 
38
39         //! The two fog types.
40         enum FogType {
41           //! Completely open to view.
42           OPEN = 0, 
43           //! Closed to view can be partially obscured.
44           CLOSED = 1
45         };
46         enum ShadeType {
47           NONE = 0,
48           LIGHTLY_TO_SOUTH_AND_EAST = 1,
49           LIGHTLY_TO_SOUTH_AND_WEST = 2,
50           LIGHTLY_TO_NORTH_AND_WEST = 3,
51           LIGHTLY_TO_NORTH_AND_EAST = 4,
52           DARKLY_TO_NORTH_AND_WEST_LIGHTLY_TO_SOUTH_AND_EAST = 5,
53           DARKLY_TO_NORTH_AND_EAST_LIGHTLY_TO_SOUTH_AND_WEST = 6,
54           DARKLY_TO_SOUTH_AND_EAST_LIGHTLY_TO_NORTH_AND_WEST = 7,
55           DARKLY_TO_SOUTH_AND_WEST_LIGHTLY_TO_NORTH_AND_EAST = 8,
56           DARKLY_TO_SOUTH_LIGHTLY_TO_EAST_AND_WEST = 9,
57           DARKLY_TO_NORTH_LIGHTLY_TO_EAST_AND_WEST = 10,
58           DARKLY_TO_WEST_LIGHTLY_TO_NORTH_AND_SOUTH = 11,
59           DARKLY_TO_EAST_LIGHTLY_TO_NORTH_AND_SOUTH = 12,
60           ALL = 13,
61           DARKLY_TO_SOUTH_AND_WEST_DARKLY_TO_NORTH_AND_EAST = 14,
62           DARKLY_TO_NORTH_AND_WEST_DARKLY_TO_SOUTH_AND_EAST = 15,
63         };
64         
65         //! Standard constructor: create a given map
66         /**
67          * @param width   The GameMap is this wide.
68          * @param height  The GameMap is this hight.
69          */
70         FogMap(int width, int height);
71
72         //! Loading constructor.
73         /**
74          * Load the fogmap from a file.
75          * FogMaps are stored in the saved-game file at:
76          * lordsawar.playerlist.player.fogmap.
77          *
78          * @param helper  The opened saved-game file to load the fogmap from.
79          */
80         FogMap(XML_Helper* helper);
81
82         //! Copy constructor.
83         FogMap(const FogMap&);
84
85         //! Destructor.
86         ~FogMap();
87
88         //! Returns the width of the fog map.
89         int getWidth() const {return d_width;}
90  
91         //! Returns the height of the fog map.
92         int getHeight() const {return d_height;}
93
94         //! Fill the fogmap with a status.
95         /** 
96          * @param type             The status to use.
97          *
98          * @return True on success, false on error.
99          */
100         bool fill(FogType type);
101
102         //! Get the foggedness of a given position.
103         FogType getFogTile(Vector<int> pos) const;
104         ShadeType getShadeTile(Vector<int> pos) const;
105
106         //! Alter the fog around a given position in the fog map.
107         /** 
108          * @param pt       The point around which status is altered.
109          * @param radius   The radius around the point where the fog is altered.
110          * @param new_type The type which the area gets.
111          */
112         void alterFogRadius(Vector<int> pt, int radius, FogType new_type);
113
114         //! Alter the fog in a rectangle at a given position on the fog map.
115         /** 
116          * @param pt       The upper left point of the rectangle.
117          * @param width    The width of the rectangle.
118          * @param height   The height of the rectangle.
119          * @param new_type The fog type which the area gets.
120          */
121         void alterFogRectangle(Vector<int> pt, int height, int width, FogType new_type);
122
123         //! Defog the map according to the given sightmap.
124         /** 
125          * @param sightmap The portion of the map to defog.
126          */
127         void alterFog(SightMap *sightmap);
128
129         //! Smooth the fogmap.
130         /** 
131          * Sweep the fog map for squares that are fogged that are
132          * surrounded by defogged squres, and remove them.
133          */
134         void smooth();
135
136         //! Returns whether or not the fog on a tile is surrounded by openness.
137         /** 
138          * This method is for BigMap purposes, it helps to know when a given 
139          * fog tile shouldn't be rendered.
140          */
141         bool isLoneFogTile(Vector<int> pos);
142
143         //! Save a fogmap.
144         /**
145          * @param helper  The opened saved-game file to save the fogmap to.
146          *
147          * @return True if saving went well, false otherwise.
148          */
149         bool save(XML_Helper* helper) const;
150
151         //! Is a tile fogged?
152         /**
153          * Assists the BigMap and SmallMap in knowing if a given tile is
154          * obscured or not.
155          *
156          * @param pos  The position in the fogmap to query.
157          * @param player The player's fogmap to query.
158          *
159          * @return True if the position is obscured due to fog, false if not.
160          */
161         //static bool isFogged(Vector <int> pos, Player *player);
162         bool isFogged(Vector <int> pos);
163         static bool isClear(Vector <int> pos, Player *player) ;
164
165         bool isCompletelyObscuredFogTile(Vector<int> pos) const;
166
167         ShadeType calculateShade(Vector<int> tile);
168
169     private:
170
171         void calculateShadeMap();
172         // Data
173         //! The width of the fog map.
174         int d_width;
175
176         //! The height of the fog map.
177         int d_height;
178
179         //! An array of tiles that describe how a tile is fogged.
180         FogType * d_fogmap;
181         ShadeType *shademap;
182
183         //! A list of tiles that are completely obscured.
184         std::list<Vector<int> > completely_obscured;
185 };
186
187 #endif
188
189 // End of file