File: map.cc

package info (click to toggle)
enemylines3 1.2-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,380 kB
  • ctags: 1,444
  • sloc: cpp: 16,323; makefile: 74; sh: 52
file content (226 lines) | stat: -rw-r--r-- 4,077 bytes parent folder | download | duplicates (6)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "map.h"

namespace el3 {

Map::Map() {
	init();
}

void Map::init() {
	int x,z;


	for (x=0;x<MX;x++) {
		for (z=0;z<MZ;z++) {
			data[x][z]=TT_LEVEL1;
		}
	}

}


unsigned int Map::dx() { return MX; }
unsigned int Map::dy() { return 0; }
unsigned int Map::dz() { return MZ; }


bool Map::inside(C3 c) {
	if (c.x<0) return false;
	if (c.z<0) return false;
	if (c.x>=dx()) return false;
	if (c.z>=dz()) return false;
	return true;
}
void Map::clear(e_tiletype t) {
	C3 p;
	for (p.x=0;p.x<dx();p.x++) {
		for (p.z=0;p.z<dz();p.z++) {
			set(p,t);

		}
	}

}

void Map::set(C3 c,e_tiletype t) {
	if (!inside(c)) return;

	data[c.x][c.z]=t;
}
	
e_tiletype Map::get(C3 c) {
	if (!inside(c)) return TT_LEVEL0;

	return data[c.x][c.z];
}
std::ostream& operator<<(std::ostream&s, Map sk) {

	unsigned int x,z;
	for (x=0;x<sk.dx();x++) {
		for (z=0;z<sk.dz();z++) {
			s << sk.get(C3(x,0,z));
		}
		s << std::endl;
	}
	return s;
}
void Map::destroy(C3 p) {
	e_tiletype t;
	t=get(p);
	if (t>TT_LEVEL1) {
		set(p,static_cast<e_tiletype>(static_cast<int>(t)-1));
	}
}

void Map::lower(C3 p) {

   el3::e_tiletype t;
   t=get(p);
   int i=static_cast<int>(t);
   if (t>el3::TT_LEVEL1) i--;

   t=static_cast<el3::e_tiletype>(i);

   set(p,t);
}
void Map::raise(C3 p) {
   el3::e_tiletype t;
   t=get(p);
   int i=static_cast<int>(t);
   if (t<el3::TT_LEVEL5) i++;

   t=static_cast<el3::e_tiletype>(i);

   set(p,t);
}

int Map::typetoheight(e_tiletype t) {
   switch (t) {
      case TT_LEVEL0: return 0;
      case TT_LEVEL1: return 1;
      case TT_LEVEL2: return 2;
      case TT_LEVEL3: return 3;
      case TT_LEVEL4: return 4;
      case TT_LEVEL5: return 5;
      case TT_DEFEND: return 2;
      default: return 0;
   }
}
static const int maxheight=5;


C3 toint(C3f p) {
	p.y-=0.2f;

	C3 pi;
	pi=p.toint();
	return pi;
}
bool Map::isvalid(e_tiletype t,float y) {
	if (y<0) return false;
	if (y>maxheight) return true;
	if (t==TT_LEVEL0) return true;
	if (t==TT_LEVEL1) return true;
	if (t==TT_LEVEL2||t==TT_DEFEND) { if (y>1.00f) return true; return false; }
	if (t==TT_LEVEL3) { bool r; r=false; if (y>2.00f) r=true; return r; }
	if (t==TT_LEVEL4) { bool r; r=false; if (y>3.00f) r=true; return r; }
	if (t==TT_LEVEL5) { bool r; r=false; if (y>4.00f) r=true; return r; }
	return false;
}

bool Map::invalidahead(C3f p,C3 *ret) {
	/*if (p.y>maxheight) return true;

	C3 pi=p.toint();
	if (!isvalid(pi,p.y)) {
		ret->x=pi.x; ret->y=pi.y; ret->z=pi.z;
		return false;
	}
	C3f r=p;

	r.x-=pi.x; r.y-=pi.y; r.z-=pi.z; 
	static const float dist=0.45f;
	static const float adist=1.0f-dist;

	C3 n=pi;
	C3 n2=pi;
	if (r.x<dist) { 
		n.x--;  n2.x--; 
		if (!isvalid(n2,p.y)) {
			ret->x=n2.x; ret->y=n2.y; ret->z=n2.z;
			return false; 
		}
		n2=pi;
	} 
	if (r.x>adist) { 
		n.x++;  n2.x++; 
		if (!isvalid(n2,p.y)) {
			ret->x=n2.x; ret->y=n2.y; ret->z=n2.z;
			return false; 
		}
		n2=pi;
	} 
	if (r.z<dist) { 
		n.z--;  n2.z--; 
		if (!isvalid(n2,p.y)) {
			ret->x=n2.x; ret->y=n2.y; ret->z=n2.z;
			return false; 
		}
		n2=pi;
	} 
	if (r.z>adist) { 
		n.z++;  n2.z++; 
		if (!isvalid(n2,p.y)) {
			ret->x=n2.x; ret->y=n2.y; ret->z=n2.z;
			return false; 
		}
		n2=pi;
	} 

	if (!isvalid(n,p.y)) {
		ret->x=n.x; ret->y=n.y; ret->z=n.z;
		return false;
	}*/

	return true;
}

bool Map::isvalid(C3 p,float y,C3 *ret) {
	e_tiletype t;
	t=get(p);
	*ret=p;
	return isvalid(t,y);
}

bool Map::isvalid(C3f p,C3 *ret) {
	if (p.y<0) return false;
	if (p.y>maxheight) return true;

	C3 pi=toint(p);

	
	if (!isvalid(pi,p.y,ret)) return false;

	C3f r=p;

	r.x-=pi.x;
	r.y-=pi.y;
	r.z-=pi.z;

	static const float dist=0.45f;
	static const float adist=1.0f-dist;

	C3 n=pi;
	C3 n2=pi;
	if (r.x<dist) { n.x--;  n2.x--; if (!isvalid(n2,p.y,ret)) return false; n2=pi;} 
	if (r.x>adist) { n.x++;  n2.x++; if (!isvalid(n2,p.y,ret)) return false; n2=pi;} 
	if (r.z<dist) { n.z--;  n2.z--; if (!isvalid(n2,p.y,ret)) return false; n2=pi;} 
	if (r.z>adist) { n.z++;  n2.z++; if (!isvalid(n2,p.y,ret)) return false; n2=pi;} 

	if (!isvalid(n,p.y,ret)) return false;


	return true;
}

} //namespace