-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMap.cpp
More file actions
186 lines (173 loc) · 4.49 KB
/
Map.cpp
File metadata and controls
186 lines (173 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Map.cpp, Version 1.2 (11 Mar 2015)
#include<ncurses.h>
#include<panel.h>
#include<cstdlib>
#include<cstring>
#include<utility> //for std::pair
#include<iostream>
#include<stdexcept>
#include"goldchase.h"
#include"Screen.h"
#include"Map.h"
//Initialize the object and draw the map
Map::Map(const unsigned char* mmem, int ylength, int xwidth)
: mapHeight(ylength), mapWidth(xwidth), mapmem(mmem), theMap(ylength, xwidth)
{
drawMap();
}
int Map::getKey()
{
return theMap.getKey();
}
void Map::postNotice(const char* msg)
{
theMap.notice(msg);
}
//Calculate offset into memory array
char Map::operator()(int y, int x)
{
if(y<0 || y>=mapHeight)
throw std::out_of_range("Y coordinate out of range");
if(x<0 || x>=mapWidth)
throw std::out_of_range("X Coordinate out of range");
return *(mapmem+y*mapWidth+x);
}
unsigned int Map::getPlayer(unsigned int playerMask)
{
std::vector<int> players;
//Map shouldn't be aware of this mapping.
//Oh, well. So much to do and so little time.
if(G_PLR0 & playerMask)
players.push_back(1);
if(G_PLR1 & playerMask)
players.push_back(2);
if(G_PLR2 & playerMask)
players.push_back(3);
if(G_PLR3 & playerMask)
players.push_back(4);
if(G_PLR4 & playerMask)
players.push_back(5);
if(players.size()==0)
{
postNotice("ERROR: no players to select from!");
return 0;
}
int choice=theMap.getOrdinal("Player?",players);
switch(choice)
{
case 1:
return G_PLR0;
case 2:
return G_PLR1;
case 3:
return G_PLR2;
case 4:
return G_PLR3;
case 5:
return G_PLR4;
default:
return -1;
}
}
std::string Map::getMessage()
{
return theMap.getText();
}
//Draw and refresh map from memory array
void Map::drawMap()
{
Map& mymap=*this; //ease referencing ourself
bool upper, lower, left, right;
for(int y=0; y<mapHeight; ++y)
{
for(int x=0; x<mapWidth; ++x)
{
char ch=mymap(y,x);
//Draw an empty square
if(ch==0)
{
theMap.plot(y,x,' ');
continue;
}
//Draw a wall
if(ch & G_WALL)
{
//determine what walls, if any, surround us
//the redundant &&true will force true values to 1
upper = y==0 ? true : mymap(y-1,x) & G_WALL && true;
lower = y==mapHeight-1 ? true : mymap(y+1,x) & G_WALL && true;
left = x==0 ? true : mymap(y,x-1) & G_WALL && true;
right = x==mapWidth-1 ? true : mymap(y,x+1) & G_WALL && true;
int num_walls=upper+lower+left+right;
// This switch statement plots the correct wall shape.
// The wall shape changes depending on the presence
// or absence of walls in the surrounding squares
switch(num_walls)
{
case 0:
case 4:
theMap.plot(y,x,ACS_PLUS);
break;
case 3:
if(!upper)
theMap.plot(y,x,ACS_TTEE);
if(!lower)
theMap.plot(y,x,ACS_BTEE);
if(!left)
theMap.plot(y,x,ACS_LTEE);
if(!right)
theMap.plot(y,x,ACS_RTEE);
break;
case 2:
if(!upper && !left)
theMap.plot(y,x,ACS_ULCORNER);
if(!lower && !left)
theMap.plot(y,x,ACS_LLCORNER);
if(!upper && !right)
theMap.plot(y,x,ACS_URCORNER);
if(!lower && !right)
theMap.plot(y,x,ACS_LRCORNER);
if(!lower && !upper)
theMap.plot(y,x,ACS_HLINE);
if(!left && !right)
theMap.plot(y,x,ACS_VLINE);
break;
case 1:
if(lower || upper)
theMap.plot(y,x,ACS_VLINE);
if(left || right)
theMap.plot(y,x,ACS_HLINE);
break;
} //end switch
}//end if ch & G_WALL
//Draw gold
if(ch & G_GOLD || ch & G_FOOL)
{
theMap.plot(y,x,'G',COLOR_PAIR(Screen::c_gold));
}
//Draw player
if(ch & G_ANYP)
{
switch(ch & G_ANYP)
{
case G_PLR0:
theMap.plot(y,x,'1',A_STANDOUT);
break;
case G_PLR1:
theMap.plot(y,x,'2',A_STANDOUT);
break;
case G_PLR2:
theMap.plot(y,x,'3',A_STANDOUT);
break;
case G_PLR3:
theMap.plot(y,x,'4',A_STANDOUT);
break;
case G_PLR4:
theMap.plot(y,x,'5',A_STANDOUT);
break;
}
}
} //for(x...)
} //for(y..)
theMap.panelRefresh();
}