示例#1
0
int main(int argc, char *argv[])
{
	int i, j, x, z; char fnwoe[384], *e, *minimap, *hmap, **tname; int *tid;
	if(argc < 2) {printf("Usage: bcm2snr \"input.bcm\"\n"); return 0;}
	assert(file = fopen(argv[1], "rb"));

	fseek(file, 12, SEEK_CUR);
	mapw = read32(); maph = read32();

	strcpy(fnwoe, argv[1]);
	e = strrchr(fnwoe, '.');
	if(!e) {e = fnwoe + strlen(fnwoe); *e = '.';}

	strcpy(e, ".snr");
	assert(fsnr = fopen(fnwoe, "w"));
	fprintf(fsnr, "SCENARIO_VERSION 4.00\n");
	fprintf(fsnr, "SCENARIO_DIMENSIONS %u %u\n", mapw, maph);
	fprintf(fsnr, "SCENARIO_EDGE_WIDTH %u\n", read32());
	fprintf(fsnr, "SCENARIO_TEXTURE_DATABASE \""); ReadPrintWString(fsnr);
	fprintf(fsnr, "\"\n");
	strcpy(e, "");
	fprintf(fsnr, "SCENARIO_TERRAIN \"%s.trn\"\n", fnwoe);
	fprintf(fsnr, "SCENARIO_HEIGHTMAP \"%s_heightmap.pcx\"\n", fnwoe);
	fprintf(fsnr, "SCENARIO_HEIGHT_SCALE_FACTOR %f\n", readfloat());
	fprintf(fsnr, "SCENARIO_SUN_COLOUR");
	for(i = 0; i < 3; i++) fprintf(fsnr, " %u", read32());
	fprintf(fsnr, "\n");
	fprintf(fsnr, "SCENARIO_SUN_VECTOR");
	for(i = 0; i < 3; i++) fprintf(fsnr, " %f", readfloat());
	fprintf(fsnr, "\n");
	fprintf(fsnr, "SCENARIO_FOG_COLOUR");
	for(i = 0; i < 3; i++) fprintf(fsnr, " %u", read32());
	fprintf(fsnr, "\n");
	fprintf(fsnr, "SCENARIO_SKY_TEXTURES_DIRECTORY \""); ReadPrintWString(fsnr);
	fprintf(fsnr, "\"\n");
	fprintf(fsnr, "SCENARIO_MINIMAP \"%s_minimap.pcx\"\n", fnwoe);

	strcpy(e, "_minimap.pcx");
	assert(fpcx = fopen(fnwoe, "wb"));
	assert(minimap = (char*)malloc(0xC000));
	fread(minimap, 0xC000, 1, file);
	WritePCXHeader(fpcx, 128, 128, 3);
	WritePCXData(fpcx, minimap, 128, 128, 3);
	fclose(fpcx);
	free(minimap);

	nlakes = readnb(6);
	for(i = 0; i < nlakes; i++)
	{
		fprintf(fsnr, "SCENARIO_LAKE");
		for(j = 0; j < 3; j++) fprintf(fsnr, " %f", readfloat());
		fprintf(fsnr, " 0.0\n");
		readnb(2);
	}

	printf("Number of names: %u\n", nnames = read16());
	assert(tname = (char**)malloc(nnames * sizeof(char*)));
	for(i = 0; i < nnames; i++)
		{tname[i] = (char*)malloc(128); ReadName(tname[i]);}

	printf("Number of IDs: %u\n", nids = read16());
	assert(tid = (int*)malloc(nids * sizeof(int)));
	for(i = 0; i < nids; i++) tid[i] = read32();

	printf("\n------------\n\n");
	ngrpbits = GetMaxBits(nnames); //2;
	nidbits = GetMaxBits(nids); //3;

	strcpy(e, ".trn");
	assert(ftrn = fopen(fnwoe, "w"));
	for(z = maph-1; z >= 0; z--)
	for(x = 0; x < mapw; x++)
	{
		fprintf(ftrn, "X %u Z %u ", x+1, z+1);
		fprintf(ftrn, "GROUP \"%s\" ", tname[readnb(ngrpbits)]);
		fprintf(ftrn, "ID %u ", tid[readnb(nidbits)]);
		fprintf(ftrn, "ROTATION %u ", readnb(2));
		fprintf(ftrn, "XFLIP %u ", readbit());
		fprintf(ftrn, "ZFLIP %u\n", readbit());
	}
	fclose(ftrn);

	printf("End offset: 0x%08X\n", ftell(file));

	printf("\n------------\n\n");

	printf("Num of ?: %u\n", nunk = read32());
	printf("0x62: 0x%X\n", read32());
	for(i = 0; i < nunk; i++)
		{readnb(ngrpbits); readnb(nidbits);}

	assert(hmap = (char*)malloc((mapw+1)*(maph+1)));
	fread(hmap, (mapw+1)*(maph+1), 1, file);
	strcpy(e, "_heightmap.pcx");
	assert(fpcx = fopen(fnwoe, "wb"));
	WritePCXHeader(fpcx, mapw+1, maph+1, 1);
	WritePCXData(fpcx, hmap, mapw+1, maph+1, 1);
	write8(fpcx, 12);
	for(i = 0; i < 256; i++)
		for(j = 0; j < 3; j++)
			write8(fpcx, i);
	fclose(fpcx);
	free(hmap);

	printf("End offset: 0x%08X\n", ftell(file));

	fclose(file);
	fclose(fsnr);
	free(tid);
	for(i = 0; i < nnames; i++) free(tname[i]);
	free(tname);
}
示例#2
0
void ConvertRainbowTable( std::string pathName, std::string resultFileName, std::string sType, bool debug, uint32 dropLastNchains, uint32 dropHighSPcount, int sptl )
{
#ifdef _WIN32
	std::string::size_type nIndex = pathName.find_last_of('\\');
#else
	std::string::size_type nIndex = pathName.find_last_of('/');
#endif
	std::string fileName;

	if ( nIndex != std::string::npos )
		fileName = pathName.substr( nIndex + 1 );
	else
		fileName = pathName;

	// Info
	printf("%s:\n", fileName.c_str());
	FILE *fResult = fopen(resultFileName.c_str(), "wb");
	if(fResult == NULL)
	{
		printf("Could not open %s for write access", resultFileName.c_str());
		return;
	}
	static CMemoryPool mp;
	uint64 nAllocatedSize;
	BaseRTReader *reader = NULL;
	if(sType == "RTI2")
		reader = new RTI2Reader( pathName );
	else if(sType == "RTI")
		reader = new RTIReader( pathName );
	else 
	{
		printf("Invalid table type '%s'", sType.c_str());
		return ;
	}

	if ( debug )
		reader->Dump();

	uint64 size = reader->getChainsLeft() * sizeof(RainbowChainO);
	uint64 rainbowChainCount = reader->getChainsLeft();
	uint64 chainsLeft;

	rainbowChainCount -= dropLastNchains;
	rainbowChainCount -= dropHighSPcount;

	size -= sizeof(RainbowChainO) * dropLastNchains;

#ifdef _MEMORYDEBUG
	printf("Starting allocation of %i bytes\n", size);
#endif
	RainbowChainO* pChain = (RainbowChainO*)mp.Allocate(size, nAllocatedSize);
#ifdef _MEMORYDEBUG
	printf("Finished. Got %i bytes\n", nAllocatedSize);
#endif
	if (pChain != NULL)
	{
		nAllocatedSize = nAllocatedSize / sizeof(RainbowChainO) * sizeof(RainbowChainO);		// Round to boundary
		unsigned int nChains = nAllocatedSize / sizeof(RainbowChainO);
		while( ( chainsLeft = reader->getChainsLeft() ) > 0 && chainsLeft > dropLastNchains )
		{
#ifdef _MEMORYDEBUG
			printf("Grabbing %i chains from file\n", nChains);
#endif
			reader->readChains(nChains, pChain);
#ifdef _MEMORYDEBUG
			printf("Recieved %i chains from file\n", nChains);
#endif
			for(uint32 i = 0; i < nChains; i++)
			{
				if ( dropHighSPcount > 0 && GetMaxBits(pChain[i].nIndexS) > sptl )
				{
					//dropHighSPcount++;
				}
				else if ( dropLastNchains > 0 && dropLastNchains >= ( chainsLeft - i ) )
				{
				}
				else
					fwrite(&pChain[i], 1, 16, fResult);
			}
		}
	}
	fclose(fResult);

	if(reader != NULL)
		delete reader;

	if ( dropHighSPcount > 0 )
	{
		std::string::size_type lastX = resultFileName.find_last_of('x');

		if ( lastX == std::string::npos )
		{
			std::cout << "Could not parse the filename to drop the high SP chains"
				<< std::endl;
			exit( -1 );
		}
		
		std::string::size_type firstSplit
			= resultFileName.find_first_of('_',lastX);

		if ( firstSplit == std::string::npos )
		{
			std::cout << "Could not parse the filename to drop the high SP chains"
				<< std::endl;
			exit( -1 );
		}

		std::string newResultFileName = resultFileName;

		newResultFileName.replace( lastX + 1, firstSplit - lastX - 1, uint64tostr( rainbowChainCount ) );
		
		if ( rename( resultFileName.c_str(), newResultFileName.c_str() ) != 0 )
		{
			std::cout << "Could not parse the filename to drop the high SP chains"
				<< std::endl;
			exit( -1 );
		}
	}
}