Forum

> > CS2D > Maps/Editor > C++ program generating an empty map
Forums overviewCS2D overview Maps/Editor overviewLog in to reply

English C++ program generating an empty map

3 replies
To the start Previous 1 Next To the start

old C++ program generating an empty map

Avo
User Off Offline

Quote
Hello us,

Is there any good programmer who can provide me an example C++ code generating an empty and proper CS2D map?

Theory (click me) seems simple but I'm not good at C++ so I'm asking for the code.

Thanks in advance.

old Re: C++ program generating an empty map

MikuAuahDark
User Off Offline

Quote
Here you go
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
#include <iostream>		// all-in-one(std::string, stdio.h, stdlib.h,...)
#include <fstream>		// complete file stream type
#include <ctime>		// well, iostream does not include time.h

typedef struct MapWriter_Info {
	bool scroll_Tiles;
	bool use_Modifier;
	int USGN_ID;
	std::string author_Name;
	int Map_XSize;
	int Map_YSize;
	// without path but with extension, "aztec.bmp" for example
	std::string tileset_Name;
	std::string background_Name;
	int scroll_X;
	int scroll_Y;
	char default_Frame;
	// [0]=R, [1]=G, [2]=B
	char background_RGB[3];
} *PointerMapWriter_Info;

void generate_empty_map(std::string& filename,PointerMapWriter_Info mapinfo) {
	int temp_int;
	time_t temp_time_t;
	char temp_str[1024];
	tm* temp_tm;
	std::fstream file(filename.c_str(),std::ios::out|std::ios::binary|std::ios::trunc);	// fopen(filename.c_str(),"wb"); << fopen equivalent
	// STRING header
	file << "Unreal Software's Counter-Strike 2D Map File (max)\r\n";
	// BYTE scroll map like tiles? (0 no / 1 yes)
	file.write((const char*)&mapinfo->scroll_Tiles,1);
	// BYTE use modifiers in this map (blending, tile rotation, tile brightness) (0 no / 1 yes)
	file.write((const char*)&mapinfo->use_Modifier,1);
	// 8 bytes are unused + uptime of system = 0
	file.write("\0\0\0\0\0\0\0\0\0\0\0\0",12);
	// INT USGN ID of the map author with an offset of +51 (0 if author was not logged in)
	temp_int=mapinfo->USGN_ID==0 ? 0 : mapinfo->USGN_ID+51;
	file.write((const char*)&temp_int,4);
	// 8 Int are unused. Writing 32 null byte
	file.write("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",32);
	// 10 strings for map settings
	file << mapinfo->author_Name.c_str() << "\r\n" << "\r\n" << "\r\n" << "\r\n" << "\r\n" << "\r\n" << "\r\n" << "\r\n" << "\r\n" << "\r\n";
	// Time info
	temp_time_t=time(nullptr);
	temp_tm=localtime(&temp_time_t);
	// STRING string which is built this way (map_xsize (in tiles) * map_ysize (in tiles)) + "x" + tile_count + "$" + CurrentSystemTime (HHMMSS) + "%" + SystemUpTime (in Milliseconds)
	sprintf(temp_str,"%dx%d$%02d%02d%02d%%%d",mapinfo->Map_XSize*mapinfo->Map_YSize,255,temp_tm->tm_hour,temp_tm->tm_min,temp_tm->tm_sec,0);
	// and STRING filename of the tilset image (without path but with extension, "aztec.bmp" for example)
	file << temp_str << "\r\n" << mapinfo->tileset_Name.c_str() << "\r\n";
	// BYTE number of tiles required from this tileset (tile_count) -1
	file.write("\376",1);	// 254
	// INT map width (map_xsize) in tiles -1 (because it starts at 0)
	temp_int=mapinfo->Map_XSize-1;
	file.write((const char*)&temp_int,4);
	// INT map height (map_ysize) in tiles -1 (because it starts at 0)
	temp_int=mapinfo->Map_YSize-1;
	file.write((const char*)&temp_int,4);
	// STRING filename of map background image (without path but with extension)
	file << mapinfo->background_Name.c_str() << "\r\n";
	// INT map background scroll x speed
	file.write((const char*)&mapinfo->scroll_X,4);
	// INT map background scroll y speed
	file.write((const char*)&mapinfo->scroll_Y,4);
	// 3 BYTE RGB background color
	file.write(mapinfo->background_RGB,3);
	// STRING string to test if header is okay with value "ed.erawtfoslaernu" (unrealsoftware.de backwards)
	file << "ed.erawtfoslaernu\r\n";
	// Here i'm writting floor soundless tile info and i'm writing 16 bytes in 16x loop because 16*16=256
	for(int i=0;i<16;i++)
		file.write("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",16);
	for(int x=0;x<mapinfo->Map_XSize;x++) {
		for(int y=0;y<mapinfo->Map_YSize;y++)
			file.write(&mapinfo->default_Frame,1);
	}
	// tile modifier
	if(mapinfo->use_Modifier) {
		for(int x=0;x<mapinfo->Map_XSize;x++) {
			for(int y=0;y<mapinfo->Map_YSize;y++)
				file.write("\0",1);
		}
	}
	file.write("\0\0\0\0",4);	// INT number of entities in the map (entity_count) = 0
	file.close();	// close file
}

program exe test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main(int argc,char* argv[]) {
	MapWriter_Info mapinfo;
	if(argc<2) {
		printf("Usage: %s <file name>\n",argv[0]);
		return 1;
	}
	memset(&mapinfo,0,sizeof(MapWriter_Info));
	mapinfo.author_Name=std::string("Test name");
	mapinfo.background_Name=std::string("");		// if there's a string info that you don't need, always create new empty string
	// X and Y size
	mapinfo.Map_XSize=5;
	mapinfo.Map_YSize=5;
	
	mapinfo.scroll_Tiles=false;	// Don't scroll background like tiles
	mapinfo.tileset_Name=std::string("cs2dnorm.bmp");	// change it if necessary
	mapinfo.use_Modifier=true;	// use tile rotation, opacity, etc.
	mapinfo.USGN_ID=88318;		// your USGN ID here. It does +51 while writing only. So just put real USGN id in here
	generate_empty_map(std::string(argv[1]),&mapinfo);	// generate map based from first program argument.
	return 0;
}

old Re: C++ program generating an empty map

Avo
User Off Offline

Quote
Wow, thank you, Miku!

I just edited few things like memset (I used new and delete instead). 'Works like a charm.

Would it be a problem to add CT_Spawn and T_Spawn entities?

I'd like to know how to read a map file, too. If you find time - you can post it here as well

old Re: C++ program generating an empty map

MikuAuahDark
User Off Offline

Quote
In lua, you can check these files: file cs2d MikuAuahDark: Map Protection v1.0 and file cs2d Minimap Image Generator v1.2
Maybe i'm going to write Map reader and writer in C++ later.

memset does set the struct memory to zero, so if you don't need some integer value you can ignore it.

About T & CT spawn entities. No, there's no problem on adding T & CT spawn entities, but if you want to do it, you can add some piece of code before file.close() and also editing the entity count to 2(for T & CT).
To the start Previous 1 Next To the start
Log in to reply Maps/Editor overviewCS2D overviewForums overview