-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFeatureCreator.cpp
More file actions
256 lines (220 loc) · 5.93 KB
/
FeatureCreator.cpp
File metadata and controls
256 lines (220 loc) · 5.93 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "FeatureCreator.h"
#include "Bitmap.h"
#include "math.h"
#define NUM_TREE_TYPES 16
#define treetop 215 /* TODO Shouldn't this be related to the previous define somehow? */
extern float* heightmap;
#include <iostream> /* cout */
#include <string.h>
#include <stdlib.h>
using namespace std; /* cout */
CFeatureCreator::CFeatureCreator(void)
{
}
CFeatureCreator::~CFeatureCreator(void)
{
}
void CFeatureCreator::WriteToFile(ofstream* file, vector<string> F_map)
{
//write vegetation map
file->write((char*)vegMap,xsize/4*ysize/4);
delete[] vegMap;
vector<string>::iterator F_map_iter;
int ArbFeatureTypes=F_map.size();
//write features
MapFeatureHeader fh;
fh.numFeatures=(int)features.size();
fh.numFeatureType=NUM_TREE_TYPES+1+ArbFeatureTypes;
printf("Writing %i features\n",fh.numFeatures);
file->write((char*)&fh,sizeof(fh));
for(int a=0;a<NUM_TREE_TYPES;++a){
char c[100];
sprintf(c,"TreeType%i",a);
file->write(c,(int)strlen(c)+1);
}
char c[100];
sprintf(c,"GeoVent");
file->write(c,(int)strlen(c)+1);
for (F_map_iter=F_map.begin();F_map_iter!=F_map.end();F_map_iter++){
string c=*F_map_iter;
file->write(c.c_str(),(int)strlen(c.c_str())+1);
}
for(vector<MapFeatureStruct>::iterator fi=features.begin();fi!=features.end();++fi){
file->write((char*)&*fi,sizeof(MapFeatureStruct));
}
}
void CFeatureCreator::CreateFeatures(CBitmap* bm, int startx, int starty, int arbFeatureTypes, std::string featurefile, std::string geoVentFile)
{
printf("Creating features\n");
xsize=bm->xsize/8;
ysize=bm->ysize/8;
mapx=xsize+1;
//geovent decal
CBitmap vent(geoVentFile);
CBitmap feature(featurefile);
unsigned char* map=new unsigned char[ysize*xsize];
int LastTree[2]={0,0};
for(int y=0;y<feature.ysize;++y){
for(int x=0;x<feature.xsize;++x){
/* Read vents and trees from the green channel. */
unsigned char c=feature.mem[(y*feature.xsize+x)*4+1];
if(c==255){
PlaceVent(x, y, &feature, &vent, bm);
}
else if(c>199 && c<=treetop){
//trees featuremap green 200-215
float h=heightmap[y*mapx+x];
if(h<5)
continue;
bool good=true;
if (!FlatSpot(x, y))
good = false;
if (!((LastTree[0]==(x-1) && LastTree[1]==y) || (LastTree[0]==x && LastTree[1]==(y-1)))){
int t_type=(c-200);
map[y*xsize+x]=1;
MapFeatureStruct ffs;
ffs.featureType=t_type;
ffs.relativeSize=0.8f+float(rand())/RAND_MAX*0.4f;
ffs.rotation=0;
ffs.xpos=(float)startx+x*8+4;
ffs.ypos=0;
ffs.zpos=(float)starty+y*8+4;
LastTree[0]=x;
LastTree[1]=y;
features.push_back(ffs);
}
}
else if (c != 0){
printf("Does nothing: green %02x at X %d Y %d\n", c, x, y);
}
/* Read fs.txt's features from the red channel. */
unsigned char f=feature.mem[(y*feature.xsize+x)*4];
if ((f) && ((256-f)<= arbFeatureTypes)) {
cout<<"Feature Type"<<(256-f)<<" at:"<<x<<":"<<y<<endl;
map[y*xsize+x]=1;
MapFeatureStruct ffs;
ffs.featureType=NUM_TREE_TYPES+(int)(256-f);
ffs.relativeSize=1;
ffs.rotation=0;
ffs.xpos=(float)startx+x*8+4;
ffs.ypos=0;
ffs.zpos=(float)starty+y*8+4;
features.push_back(ffs);
}
else if (f != 0){
printf("Does nothing: red %02x at X %d Y %d (put more lines in fs.txt?)\n",
f, x, y);
}
}
}
//grass take 2
CBitmap vegfeature=feature.CreateRescaled(xsize/4,ysize/4);
vegMap=new unsigned char[vegfeature.xsize*vegfeature.ysize];
memset(vegMap,0,vegfeature.xsize*vegfeature.ysize);
cout<<"Grass Placement:\n";
for(int y=0;y<ysize/4;++y){
for(int x=0;x<vegfeature.xsize;++x){
/* Read grass from the blue channel. */
unsigned char c=vegfeature.mem[(y*vegfeature.xsize+x)*4+2];
float a=c/255;
float b=1-a;
int grass=(rand()%(255))+c;
if (grass > 254)
{
#ifdef WIN32
cout<<"\002";
#else
cout<<"@";
#endif
vegMap[y*vegfeature.xsize+x]=1;
}
else
cout<<" ";
}
cout<<"\n";
}
}
void CFeatureCreator::PlaceVent(int x, int y, CBitmap * feature, CBitmap * vent, CBitmap * bm)
{
cout<<"Geo at:";
int bx=x*xsize/feature->xsize;
int by=y*ysize/feature->ysize;
for(int tries=0;tries<1000;++tries){
float h=heightmap[y*mapx+x];
if(h<5)
return;
bool good=true;
if (!FlatSpot(x, y))
good = false;
if(good){
#ifdef WIN32
cout<<""<<x<<":"<<y<<" \001\n";
#else
cout<<""<<x<<":"<<y<<" *\n";
#endif
MapFeatureStruct ffs;
ffs.featureType=NUM_TREE_TYPES;
ffs.relativeSize=1;
ffs.rotation=0;
ffs.xpos=(float)x*8+4;
ffs.ypos=0;
ffs.zpos=(float)y*8+4;
features.push_back(ffs);
// Draw the 'vent crack' on the map.
for(int y2=0;y2<vent->ysize;++y2){
for(int x2=0;x2<vent->xsize;++x2){
if(vent->mem[(y2*vent->xsize+x2)*4+0]!=255 || vent->mem[(y2*vent->xsize+x2)*4+2]!=255){
bm->mem[((y*8+y2-vent->ysize/2)*bm->xsize+x*8+x2-vent->xsize/2)*4+0]=vent->mem[(y2*vent->xsize+x2)*4+0];
bm->mem[((y*8+y2-vent->ysize/2)*bm->xsize+x*8+x2-vent->xsize/2)*4+1]=vent->mem[(y2*vent->xsize+x2)*4+1];
bm->mem[((y*8+y2-vent->ysize/2)*bm->xsize+x*8+x2-vent->xsize/2)*4+2]=vent->mem[(y2*vent->xsize+x2)*4+2];
}
}
}
break;
}
else {
/* Roll up somewhere else to try and put it. */
#ifdef WIN32
cout<< x <<":"<< y <<" X \009";
#else
cout<< x <<":"<< y <<" X @";
#endif
#ifdef WIN32
x=bx+rand()*(40)/RAND_MAX-20;
y=by+rand()*(40)/RAND_MAX-20;
#else
x=bx+( (float)(rand())*(40.0) )/RAND_MAX-20;
y=by+( (float)(rand())*(40.0) )/RAND_MAX-20;
#endif
if(x<5)
x=5;
if(x>xsize-5)
x=xsize-5;
if(y<5)
y=5;
if(y>ysize-5)
y=ysize-5;
}
}
}
bool CFeatureCreator::FlatSpot(int x, int y)
{
float h=heightmap[y*mapx+x];
// Check that the ground around this spot is about the same elevation.
for(int y2=y-3;y2<=y+3;++y2){
// Don't check off the heightmap.
if (y2 < 0)
continue;
if (y2 > ysize)
continue;
for(int x2=x-3;x2<=x+3;++x2){
if (x2 < 0)
continue;
if (x2 > xsize)
continue;
if(fabs(h-heightmap[(y2)*mapx+x2])>3)
return false;
}
}
return true;
}