HOG2
Map.h
Go to the documentation of this file.
1 /*
2  * $Id: map.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 03/07/07.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 // HOG File
13 
14 #ifndef MAP_H
15 #define MAP_H
16 
17 
18 #include <cassert>
19 #include <cmath>
20 #include <cstdarg>
21 #include <cstdio>
22 #include <unistd.h>
23 #include <iostream>
24 #include <stdint.h>
25 
26 #include "GLUtil.h"
27 //#include "Graph.h"
28 
29 enum tTileset {
36 };
37 
38 enum tDisplay {
42 };
43 
44 // types of ground
45 // bit definition:
46 // low 4 bits are subcategories
47 // high bits are large categories
48 
49 const int terrainBits = 4;
50 
51 enum tTerrain {
52  kOutOfBounds =0x0, // not part of map
53  kOutOfBounds2=0x1, // not part of map - different color
54  kWater=0x10, // water
55  kGround=0x20, // ground
56  kSwamp=0x21,
57  kGrass=0x22,
58  kBlight=0x23,
59  kTrees=0x30,
60  kUndefined=0x40 // mixed type due to split tile
61 };
62 
63 inline bool CanPass(long a, long b)
64 { return ((a&0xF0) == (b&0xF0)); }
65 
66 // ground split types
67 enum tSplit {
69  kForwardSplit=1, // ie like forward slash
70  kBackwardSplit=2 // ie like back slash
71 };
72 
73 // specifying which side of the split you want
74 enum tSplitSide {
76  kLeftSide = 2,
78 };
79 
80 enum tEdge {
86 };
87 
88 // other constants
89 enum {
91 };
92 
93 const int kNoGraphNode = -1;
94 
95 // corner types
96 enum tCorner {
97  kNone = 0,
98  kTopLeft = 1,
99  kTopRight = 2,
102 };
103 
104 class halfTile {
105 public:
106  halfTile();
107 
108  long corners[3];
110  long node;
111 };
112 
113 class Tile {
114 public:
115  Tile();
118 };
119 
120 enum tMapType {
125 };
126 
127 /*
128  *
129  * All the set/get functions should be fairly obvious, with the following
130  * behavior:
131  *
132  * Setting the height/type without a split for a tile will reset it to be a
133  * non-split tile.
134  *
135  * If you specify a split for a tile that is not split,
136  */
137 
142 class Map {
143 public:
144  Map(long width, long height);
145  Map(const char *filename);
146  Map(Map *);
147  Map(FILE *);
148  Map(std::istringstream &data);
149  ~Map();
150  void Load(const char *filename);
151  void Load(FILE *f);
152  void setSizeMultipler(int _sizeMultiplier)
153  { sizeMultiplier = _sizeMultiplier; }
154  void Scale(long newWidth, long newHeight);
155  void Trim();
156  void Save(std::stringstream &data);
157  void Save(const char *filename);
158  void Save(FILE *f);
159  Map *Clone() { return new Map(this); }
160  const char *GetMapName();
161  void Print(int scale = 1);
163  inline long GetMapWidth() const { return width; }
165  inline long GetMapHeight() const { return height; }
166 
167  void SetTileSet(tTileset ts);
169 
170  Tile &GetTile(long x, long y);
171 
172  tSplit GetSplit(long x, long y) const;
173  void SetSplit(long x, long y, tSplit split);
174 
175 
176  // returns kUndefined if tile is split and you request kWholeTile
177  long GetTerrainType(long x, long y, tSplitSide split = kWholeTile) const;
178  long GetTerrainType(long x, long y, tEdge side) const;
179  void SetTerrainType(int32_t x1, int32_t y1,
180  int32_t x2, int32_t y2, tTerrain t);
181 
182  // if tile is not split and you specify a split side, nothing happens
183  // if tile is split and you specify kWholeTile, the split remains,
184  // and the terrain is applied to both sides
185  void SetTerrainType(long x, long y, tTerrain type, tSplitSide split = kWholeTile);
186 
187  // returns kUndefinedHeight if the tile is split and you specify
188  // the whole tile
189  long GetHeight(long x, long y, tSplitSide split = kWholeTile);
190 
191  // if you specify a split, and the tile isn't split that direction,
192  // the tile will be split that direction, and then the height applied
193  void SetHeight(long x, long y, long height, tSplitSide split = kWholeTile);
194 
195  // returns kUndefinedHeight if the split is inconsistant with the tile type
196  long GetCornerHeight(long x, long y, tCorner which, tEdge edge) const;
197  long GetCornerHeight(long x, long y, tCorner which, tSplitSide split = kWholeTile) const;
198  void SetCornerHeight(long x, long y, tCorner which, long height, tSplitSide split = kWholeTile);
199 
200 
201  // ===============================================
202  void SmoothSetRectHeight(long x1, long y1, long x2, long y2, long h, tTerrain type = kGround);
203  void SetRectHeight(long x1, long y1, long x2, long y2, long h, tTerrain type = kGround);
204  bool AdjacentEdges(long x, long y, tEdge edge) const;
205  bool AdjacentCorners(long x, long y, tCorner corner) const;
206  // returns whether we can step between two locations or not
207  bool CanStep(long x1, long y1, long x2, long y2) const;
208 
209  void OpenGLDraw(tDisplay how = kPolygons) const;
210  bool GetOpenGLCoord(int _x, int _y, GLdouble &x, GLdouble &y, GLdouble &z, GLdouble &radius) const;
211  bool GetOpenGLCoord(float _x, float _y, GLdouble &x, GLdouble &y, GLdouble &z, GLdouble &radius) const;
212  void GetPointFromCoordinate(point3d loc, int &px, int &py) const;
213  double GetCoordinateScale();
214 
215  void SetDrawLand(bool land);
216  bool GetDrawLand() { return drawLand; }
217  void DrawTile(Tile *t, int x, int y, tDisplay how) const;
218  void DoVertexColor(tTerrain type, int height, bool darken = false) const;
219  void DoNormal(tSplit split, halfTile *t, int x, int y) const;
220 
221  float GetEdgeWidthX(int x, int y);
222  float GetEdgeWidthY(int x, int y);
223 
224  int GetNodeNum(int x, int y, tCorner c = kNone);
225  void SetNodeNum(int num, int x, int y, tCorner c = kNone);
226  int GetRevision() { return revision; }
227 private:
228  void loadRaw(FILE *f, int height, int width);
229  void loadOctile(FILE *f, int height, int width);
230  void loadOctileCorner(FILE *f, int height, int width);
231  void saveOctile(FILE *f);
232  void saveRaw(FILE *f);
233  bool tryLoadRollingStone(FILE *f);
234  bool tryDragonAge(FILE *f);
235  bool isLegalStone(char c);
236  void paintRoomInside(int x, int y);
237  void drawLandQuickly() const;
238  int width, height;
240  bool drawLand;
241  mutable GLuint dList;
242  mutable bool updated;
244  int revision;
245  char map_name[128];
248 };
249 
250 #endif
Map::GetEdgeWidthX
float GetEdgeWidthX(int x, int y)
Returns the edge width between (x, y) and (x+1, y)
Definition: Map.cpp:2498
Map::GetEdgeWidthY
float GetEdgeWidthY(int x, int y)
Returns the edge width between (x, y) and (x, y+1)
Definition: Map.cpp:2541
halfTile::corners
long corners[3]
Definition: Map.h:108
kRaw
@ kRaw
Definition: Map.h:124
Map::GetNodeNum
int GetNodeNum(int x, int y, tCorner c=kNone)
Gets the abstract Graph node number for this tile.
Definition: Map.cpp:2339
kRightEdge
@ kRightEdge
Definition: Map.h:83
Map::loadOctileCorner
void loadOctileCorner(FILE *f, int height, int width)
Definition: Map.cpp:428
Map::OpenGLDraw
void OpenGLDraw(tDisplay how=kPolygons) const
Does actual OpenGL drawing of the map.
Definition: Map.cpp:1777
kBitmap
@ kBitmap
Definition: Map.h:35
Map::GetCoordinateScale
double GetCoordinateScale()
Returns the scale multiplier between openGL coordinates and map coordinates.
Definition: Map.cpp:1892
terrainBits
const int terrainBits
Definition: Map.h:49
kUndefinedHeight
@ kUndefinedHeight
Definition: Map.h:90
Map::mapType
tMapType mapType
Definition: Map.h:246
Map::SetNodeNum
void SetNodeNum(int num, int x, int y, tCorner c=kNone)
Sets the abstract Graph node number for this tile.
Definition: Map.cpp:2319
kTrees
@ kTrees
Definition: Map.h:59
Map::drawLand
bool drawLand
Definition: Map.h:240
kOutOfBounds
@ kOutOfBounds
Definition: Map.h:52
halfTile::halfTile
halfTile()
Construct a half tile, initializing to flat values.
Definition: Map.cpp:61
kFast
@ kFast
Definition: Map.h:34
Map::SetRectHeight
void SetRectHeight(long x1, long y1, long x2, long y2, long h, tTerrain type=kGround)
Set the height and terrain of a set of tiles.
Definition: Map.cpp:1578
Map::SetDrawLand
void SetDrawLand(bool land)
Toggles whether the land is draw when you call OpenGLDraw.
Definition: Map.cpp:1738
kPoints
@ kPoints
Definition: Map.h:41
Map::GetDrawLand
bool GetDrawLand()
Definition: Map.h:216
Map::Clone
Map * Clone()
Definition: Map.h:159
halfTile::type
tTerrain type
Definition: Map.h:109
Map::GetPointFromCoordinate
void GetPointFromCoordinate(point3d loc, int &px, int &py) const
Definition: Map.cpp:1900
kBlight
@ kBlight
Definition: Map.h:58
kTopRight
@ kTopRight
Definition: Map.h:99
tEdge
tEdge
Definition: Map.h:80
kWinter
@ kWinter
Definition: Map.h:32
Map::updated
bool updated
Definition: Map.h:242
Map::GetTile
Tile & GetTile(long x, long y)
Return the tile at location x, y.
Definition: Map.cpp:994
tTileset
tTileset
Definition: Map.h:29
Map::Load
void Load(const char *filename)
Resets the current map by loading the file passed in.
Definition: Map.cpp:257
Tile::tile1
halfTile tile1
Definition: Map.h:116
kTopLeft
@ kTopLeft
Definition: Map.h:98
Map::land
Tile ** land
Definition: Map.h:239
Map::saveRaw
void saveRaw(FILE *f)
Definition: Map.cpp:896
Map::AdjacentCorners
bool AdjacentCorners(long x, long y, tCorner corner) const
Definition: Map.cpp:1661
Map::Print
void Print(int scale=1)
Do an ASCII/ANSI print out of the map.
Definition: Map.cpp:917
kOctile
@ kOctile
Definition: Map.h:121
Map::loadOctile
void loadOctile(FILE *f, int height, int width)
Definition: Map.cpp:362
Map::DoNormal
void DoNormal(tSplit split, halfTile *t, int x, int y) const
does a rough approximation of the normal for a particular halfTile.
Definition: Map.cpp:2224
kSokoban
@ kSokoban
Definition: Map.h:123
Map::tileSet
tTileset tileSet
Definition: Map.h:247
Tile::split
tSplit split
Definition: Map.h:117
tCorner
tCorner
Definition: Map.h:96
Map::GetRevision
int GetRevision()
Definition: Map.h:226
Map::SetCornerHeight
void SetCornerHeight(long x, long y, tCorner which, long height, tSplitSide split=kWholeTile)
Set the height of any one corner of a tile.
Definition: Map.cpp:1335
kNoSplit
@ kNoSplit
Definition: Map.h:68
kGround
@ kGround
Definition: Map.h:55
CanPass
bool CanPass(long a, long b)
Definition: Map.h:63
tSplit
tSplit
Definition: Map.h:67
kBottomLeft
@ kBottomLeft
Definition: Map.h:100
halfTile
Definition: Map.h:104
Tile::tile2
halfTile tile2
Definition: Map.h:116
kNoGraphNode
const int kNoGraphNode
Definition: Map.h:93
Map::DoVertexColor
void DoVertexColor(tTerrain type, int height, bool darken=false) const
Using OpenGL set the correct color for a particular vertex.
Definition: Map.cpp:2147
kSwamp
@ kSwamp
Definition: Map.h:56
kFallTile
@ kFallTile
Definition: Map.h:31
loc
Definition: MapGenerators.cpp:296
Map::SetSplit
void SetSplit(long x, long y, tSplit split)
Set the split of the tile at x, y.
Definition: Map.cpp:1014
Map::Scale
void Scale(long newWidth, long newHeight)
Definition: Map.cpp:177
kFall
@ kFall
Definition: Map.h:30
Map::width
int width
Definition: Map.h:238
point3d
#define point3d
Definition: GLUtil.h:123
kForwardSplit
@ kForwardSplit
Definition: Map.h:69
kBackwardSplit
@ kBackwardSplit
Definition: Map.h:70
Map::saveOctile
void saveOctile(FILE *f)
Definition: Map.cpp:873
Map::dList
GLuint dList
Definition: Map.h:241
Map::Trim
void Trim()
Definition: Map.cpp:212
Map::GetMapWidth
long GetMapWidth() const
return the width of the map
Definition: Map.h:163
Map::SetTileSet
void SetTileSet(tTileset ts)
Choose the tileset used for land colors.
Definition: Map.cpp:1747
Map::Map
Map(long width, long height)
Create a new map of a particular size.
Definition: Map.cpp:84
Map::loadRaw
void loadRaw(FILE *f, int height, int width)
Definition: Map.cpp:340
kInternalEdge
@ kInternalEdge
Definition: Map.h:81
Map::GetTerrainType
long GetTerrainType(long x, long y, tSplitSide split=kWholeTile) const
Get the terrain type of the (split) tile at x, y.
Definition: Map.cpp:1028
kUndefined
@ kUndefined
Definition: Map.h:60
Tile::Tile
Tile()
Construct a tile with no split.
Definition: Map.cpp:75
Map::sizeMultiplier
int sizeMultiplier
Definition: Map.h:243
Map::SetHeight
void SetHeight(long x, long y, long height, tSplitSide split=kWholeTile)
Set the (flat) height of the tile at x, y.
Definition: Map.cpp:1173
Map::GetCornerHeight
long GetCornerHeight(long x, long y, tCorner which, tEdge edge) const
Set the height of any one corner of a tile.
Definition: Map.cpp:1204
Map::height
int height
Definition: Map.h:238
kOutOfBounds2
@ kOutOfBounds2
Definition: Map.h:53
Map::setSizeMultipler
void setSizeMultipler(int _sizeMultiplier)
Definition: Map.h:152
GLUtil.h
Map::GetMapName
const char * GetMapName()
Definition: Map.cpp:981
kWholeTile
@ kWholeTile
Definition: Map.h:75
Map::GetHeight
long GetHeight(long x, long y, tSplitSide split=kWholeTile)
Get the (flat) height of the tile at x, y.
Definition: Map.cpp:1144
kOctileCorner
@ kOctileCorner
Definition: Map.h:122
halfTile::node
long node
Definition: Map.h:110
kLeftSide
@ kLeftSide
Definition: Map.h:76
kLines
@ kLines
Definition: Map.h:40
tTerrain
tTerrain
Definition: Map.h:51
kWinterTile
@ kWinterTile
Definition: Map.h:33
tDisplay
tDisplay
Definition: Map.h:38
Map::CanStep
bool CanStep(long x1, long y1, long x2, long y2) const
Definition: Map.cpp:1694
kWater
@ kWater
Definition: Map.h:54
Map::revision
int revision
Definition: Map.h:244
kPolygons
@ kPolygons
Definition: Map.h:39
Map::GetMapHeight
long GetMapHeight() const
return the height of the map
Definition: Map.h:165
Map::~Map
~Map()
Definition: Map.cpp:169
Tile
Definition: Map.h:113
Map::GetTileSet
tTileset GetTileSet()
Get the tileset used for land colors.
Definition: Map.cpp:1758
kBottomRight
@ kBottomRight
Definition: Map.h:101
Map::GetSplit
tSplit GetSplit(long x, long y) const
Return the split of the tile at x, y.
Definition: Map.cpp:1004
Map::drawLandQuickly
void drawLandQuickly() const
Definition: Map.cpp:2254
split
std::vector< std::string > split(const std::string &s, char delim)
Splits a string into elements.
Definition: StringUtils.cpp:16
Map::SmoothSetRectHeight
void SmoothSetRectHeight(long x1, long y1, long x2, long y2, long h, tTerrain type=kGround)
Places a rectangle into the map, but also modifies the edges to make the transition smooth.
Definition: Map.cpp:1395
kTopEdge
@ kTopEdge
Definition: Map.h:84
Map::map_name
char map_name[128]
Definition: Map.h:245
Map::tryLoadRollingStone
bool tryLoadRollingStone(FILE *f)
Definition: Map.cpp:696
Map::AdjacentEdges
bool AdjacentEdges(long x, long y, tEdge edge) const
Is the tile at x, y adjacent across the edge?
Definition: Map.cpp:1602
Map::GetOpenGLCoord
bool GetOpenGLCoord(int _x, int _y, GLdouble &x, GLdouble &y, GLdouble &z, GLdouble &radius) const
Get the openGL coordinates of a given tile.
Definition: Map.cpp:1826
kLeftEdge
@ kLeftEdge
Definition: Map.h:82
kGrass
@ kGrass
Definition: Map.h:57
Map::DrawTile
void DrawTile(Tile *t, int x, int y, tDisplay how) const
Draw a single tile.
Definition: Map.cpp:1941
Map::SetTerrainType
void SetTerrainType(int32_t x1, int32_t y1, int32_t x2, int32_t y2, tTerrain t)
Map::SetTerrainType()
Definition: Map.cpp:1049
kBottomEdge
@ kBottomEdge
Definition: Map.h:85
Map::isLegalStone
bool isLegalStone(char c)
Definition: Map.cpp:817
tSplitSide
tSplitSide
Definition: Map.h:74
Map::Save
void Save(std::stringstream &data)
unimplemented.
Definition: Map.cpp:831
Map::tryDragonAge
bool tryDragonAge(FILE *f)
Definition: Map.cpp:521
tMapType
tMapType
Definition: Map.h:120
Map::paintRoomInside
void paintRoomInside(int x, int y)
Definition: Map.cpp:800
Map
A tile-based representation of the world.
Definition: Map.h:142
kNone
@ kNone
Definition: Map.h:97
edge
Edge class for connections between node in a Graph.
Definition: Graph.h:129
kRightSide
@ kRightSide
Definition: Map.h:77