Could some godlike person fix this...

Could some godlike person fix this...

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

Post Reply
User avatar
mother
Posts: 379
Joined: 04 May 2005, 05:43

Could some godlike person fix this...

Post by mother »

Ok, well, If anyone has a few minutes and could take this version of MapConv/FeatureCreator.cpp, fix whatever stupidity I incurred in it, and compile mapconv.exe with this in place of the original...

Many many people would be made happy.

BTW The point is to make trees place via Green values of 200-215 & Grass at G 128 in the metalmap, as opposed to using the texture bmp.

Code: Select all

#include "StdAfx.h"
#include ".\featurecreator.h"

#define NUM_TREE_TYPES 16
#define treetop 215
extern float* heightmap;

CFeatureCreator::CFeatureCreator(void)
{
}

CFeatureCreator::~CFeatureCreator(void)
{
}

void CFeatureCreator::WriteToFile(ofstream* file)
{
	//write vegetation map
	file->write((char*)vegMap,xsize/4*ysize/4);
	delete[] vegMap;

	//write features
	MapFeatureHeader fh;
	fh.numFeatures=(int)features.size();
	fh.numFeatureType=NUM_TREE_TYPES+1;

	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(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,std::string metalfile)
{
	printf("Creating features\n");
	xsize=bm->xsize/8;
	ysize=bm->ysize/8;
	int mapx=xsize+1;

	//geovents
	CBitmap vent("geovent.bmp");
	CBitmap metal(metalfile);		//use the green channel for geos
// moved tree n Grassdefs
	vegMap=new unsigned char[xsize/4*ysize/4];
	memset(vegMap,0,xsize/4*ysize/4);
	unsigned char* map=new unsigned char[ysize*xsize];

	for(int y=0;y<metal.ysize;++y){
		for(int x=0;x<metal.xsize;++x){
			unsigned char c=metal.mem[(y*metal.xsize+x)*4+1];
			if(c>127){
				int bx=x*xsize/metal.xsize;
				int by=y*ysize/metal.ysize;
				for(int tries=0;tries<1000;++tries){
					int x=bx+rand()*(40)/RAND_MAX-20;
					int y=by+rand()*(40)/RAND_MAX-20;
					if(x<5)
						x=5;
					if(x>xsize-5)
						x=xsize-5;
					if(y<5)
						y=5;
					if(y>ysize-5)
						y=ysize-5;

					float h=heightmap[y*mapx+x];
					if(h<5)
						continue;

					bool good=true;
					for(int y2=y-3;y2<=y+3;++y2){
						for(int x2=x-3;x2<=x+3;++x2){
							if(fabs(h-heightmap[(y2)*mapx+x2])>3)
								good=false;
						}
					}
					if(good){
						if(c=255){
							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);


						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;
					}
						//trees metalmap green 200-216
						if(c>199 && c<=treetop){
							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;

							features.push_back(ffs);
						}
						//Grass metalmap green 128 & not under water
						if(c=128 && heightmap[(y*4+2)*mapx+x*4+2]>2){
							vegMap[y*xsize/4+x]=1;
						}
					}

					}
				}
			}
		}
	}
FYI In CFeatureCreator::WriteToFile the printf for geovents s/s\"/s\",a/
User avatar
jcnossen
Former Engine Dev
Posts: 2440
Joined: 05 Jun 2005, 19:13

Post by jcnossen »

But do you have any clue of what goes wrong with it?
What does it currently do? Crash, nothing, hang?
User avatar
mother
Posts: 379
Joined: 04 May 2005, 05:43

Post by mother »

Sorry! heh.

It runs absolutely fine.

Unfortunately spring doesn't like the maps it makes too much. After loading when you press enter it procees to consume your system.

I'm talking like after 5 minutes I hit reset because taskmgr hadn't fully opened yet kinda consumed.
--------
Ok the issue was that everything was a geo- thats what was bringing the machine to its knees.

Jeeze assignment vs comparison operators, who woulda thunk it.

The = in the if statements should all be ==.

But now it barfs (at the if statement for grass), and I think I know why, just not what to do about it. The xsize/4 bit kinda assumes a lot more about the metalmap then we know, and I think Im doing Bad Things(tm) with memory there.

Unfortunately I'm already pushing my skillz to the maximum here...
User avatar
hrmph
Posts: 1054
Joined: 12 May 2005, 20:08

Post by hrmph »

Hey, its great that your making an effort to fix the mapconv problems. Have you made any more progress? Does it produce the map, or does mapconv crash out before it reaches that point?
User avatar
mother
Posts: 379
Joined: 04 May 2005, 05:43

Post by mother »

hrmph wrote:Hey, its great that your making an effort to fix the mapconv problems. Have you made any more progress? Does it produce the map, or does mapconv crash out before it reaches that point?
I'm godlike I guess.

I actually think it works!

Current FeatureCreator.cpp:

Code: Select all

#include "StdAfx.h"
#include ".\featurecreator.h"

#define NUM_TREE_TYPES 16
#define treetop 215
extern float* heightmap;

CFeatureCreator::CFeatureCreator(void)
{
}

CFeatureCreator::~CFeatureCreator(void)
{
}

void CFeatureCreator::WriteToFile(ofstream* file)
{
	//write vegetation map
	file->write((char*)vegMap,xsize/4*ysize/4);
	delete[] vegMap;

	//write features
	MapFeatureHeader fh;
	fh.numFeatures=(int)features.size();
	fh.numFeatureType=NUM_TREE_TYPES+1;

	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(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,std::string metalfile)
{
	printf("Creating features\n");
	xsize=bm->xsize/8;
	ysize=bm->ysize/8;
	int mapx=xsize+1;

	//geovents
	CBitmap vent("geovent.bmp");
	CBitmap metal(metalfile);		//use the green channel for geos
// moved tree n Grassdefs

	unsigned char* map=new unsigned char[ysize*xsize];

	int LastTree[2]={0,0};
	for(int y=0;y<metal.ysize;++y){
		for(int x=0;x<metal.xsize;++x){
			unsigned char c=metal.mem[(y*metal.xsize+x)*4+1];
			if(c>127){
				int bx=x*xsize/metal.xsize;
				int by=y*ysize/metal.ysize;
				for(int tries=0;tries<1000;++tries){
					int x=bx+rand()*(40)/RAND_MAX-20;
					int y=by+rand()*(40)/RAND_MAX-20;
					if(x<5)
						x=5;
					if(x>xsize-5)
						x=xsize-5;
					if(y<5)
						y=5;
					if(y>ysize-5)
						y=ysize-5;

					float h=heightmap[y*mapx+x];
					if(h<5)
						continue;

					bool good=true;
					for(int y2=y-3;y2<=y+3;++y2){
						for(int x2=x-3;x2<=x+3;++x2){
							if(fabs(h-heightmap[(y2)*mapx+x2])>3)
								good=false;
						}
					}
					if(good){
						if(c==255){
							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);


						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;
					}
						//trees metalmap green 200-216
						if(c>199 && c<=treetop){
							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);
							}
						//break;
						}
						//Grass metalmap green 128 & not under water
						//if(c==128){
						//else{	vegMap[y*xsize+x]=1;
						break;
						//}
					}

					}
				}
			}
	}
	//grass take 2
	CBitmap vegmetal=metal.CreateRescaled(xsize/4,ysize/4);
	vegMap=new unsigned char[vegmetal.xsize*vegmetal.ysize];
	memset(vegMap,0,vegmetal.xsize*vegmetal.ysize);
	for(int y=0;y<ysize/4;++y){
		for(int x=0;x<vegmetal.xsize;++x){
			unsigned char c=vegmetal.mem[(y*vegmetal.xsize+x)*4+1];
			if (c==128){
				vegMap[y*vegmetal.xsize+x]=1;
			}
		}
	}

	}

http://www.fileuniverse.com/?p=showitem&ID=1404 I'd still really appreciate if someone could take a look at it, I'm sure Im doing BadThings(tm) or at least doing stupid things...
IMSabbel
Posts: 747
Joined: 30 Jul 2005, 13:29

Post by IMSabbel »

yeah.

i mean, it was just rediculous... people manage to create an awsome 3d version of ta, shadows, reflection, ect ect, but you cant use green in your map :D
Post Reply

Return to “Engine”