Ejemplo n.º 1
0
bool DTerrain::Save( const char* Filename )
{
	DArchive* Saver = CreateFileWriter( Filename );
	if( Saver == NULL ) return false;

	Saver->SetBinary();
	DTerrain::Serialize( *Saver );
	delete Saver;

	return true;
}
Ejemplo n.º 2
0
// Write the map presentation file
static int WriteMapPresentation(const std::string &mapname, CMap *map, char *mapsetup)
{
	FileWriter *f = NULL;
	int i;
	int topplayer;
	int numplayers;
	char *mapsetupname;
	const char *type[] = {"", "", "neutral", "nobody", 
		"computer", "person", "rescue-passive", "rescue-active"};
	
	numplayers = 0;
	topplayer = PlayerMax - 2;
	
	try {
		f = CreateFileWriter(mapname);
		f->printf("-- Stratagus Map Presentation\n");
		f->printf("-- File generated by the stratagus builtin editor.\n");
		// MAPTODO Copyright notice in generated file
		f->printf("-- File licensed under the GNU GPL version 2.\n\n");

		f->printf("DefinePlayerTypes(");
		f->printf("\"%s\"", type[map->Info.PlayerType[0]]);
		while (topplayer > 0 && map->Info.PlayerType[topplayer] == PlayerNobody) {
			--topplayer;
		}
		for (i = 1; i <= topplayer; ++i) {
			f->printf(", \"%s\"", type[map->Info.PlayerType[i]]);
			++numplayers;
		}
		f->printf(")\n");
	
		f->printf("PresentMap(\"%s\", %d, %d, %d, %d)\n",
			map->Info.Description.c_str(), numplayers, map->Info.MapWidth, map->Info.MapHeight,
			map->Info.MapUID + 1);

		mapsetupname = strrchr(mapsetup, '/');
		if (!mapsetupname) {
			mapsetupname = mapsetup;
		}
		f->printf("DefineMapSetup(GetCurrentLuaPath()..\"%s\")\n", mapsetupname);
	} catch (const FileException &) {
		fprintf(stderr, "ERROR: cannot write the map presentation\n");
		delete f;
		return -1;
	}
	delete f;
	
	return 1;
}
uint32 FFileManagerGeneric::CopyWithProgress( const TCHAR* InDestFile, const TCHAR* InSrcFile, bool ReplaceExisting, bool EvenIfReadOnly, bool Attributes, FCopyProgress* Progress )
{
	uint32	Result = COPY_OK;

	// Direct file copier.
	if( Progress->Poll( 0.0 ) )
	{
		FString SrcFile		= InSrcFile;
		FString DestFile	= InDestFile;
	
		FArchive* Src = CreateFileReader( *SrcFile );
		if( !Src )
		{
			Result = COPY_Fail;
		}
		else
		{
			FArchive* Dest = CreateFileWriter( *DestFile,( ReplaceExisting ? 0 : FILEWRITE_NoReplaceExisting ) | ( EvenIfReadOnly ? FILEWRITE_EvenIfReadOnly : 0 ) );
			if( !Dest )
			{
				Result = COPY_Fail;
			}
			else
			{
				int64 Size = Src->TotalSize();
				int64 Percent = 0, NewPercent = 0;
				uint8* Buffer = new uint8[COPYBLOCKSIZE];
				for( int64 Total = 0; Total < Size; Total += sizeof(Buffer) )
				{
					int64 Count = FMath::Min( Size - Total, (int64)sizeof(Buffer) );
					Src->Serialize( Buffer, Count );
					if( Src->IsError() )
					{
						Result = COPY_Fail;
						break;
					}
					Dest->Serialize( Buffer, Count );
					if( Dest->IsError() )
					{
						Result = COPY_Fail;
						break;
					}
					NewPercent = Total * 100 / Size;
					if( Progress && Percent != NewPercent && !Progress->Poll( ( float )NewPercent / 100.f ) )
					{
						Result = COPY_Canceled;
						break;
					}
					Percent = NewPercent;
				}
				delete [] Buffer;
				if( Result == COPY_OK && !Dest->Close() )
				{
					Result = COPY_Fail;
				}
				delete Dest;
				if( Result != COPY_OK )
				{
					Delete( *DestFile );
				}
			}
			if( Result == COPY_OK && !Src->Close() )
			{
				Result = COPY_Fail;
			}
			delete Src;
		}
		if( Progress && Result==COPY_OK && !Progress->Poll( 1.0 ) )
		{
			Result = COPY_Canceled;
		}
	}
	else
	{
		Result = COPY_Canceled;
	}

	return Result;
}
Ejemplo n.º 4
0
/**
**  Write the map setup file.
**
**  @param mapsetup      map filename
**  @param map           map to save
**  @param writeTerrain  write the tiles map in the .sms
*/
int WriteMapSetup(const char *mapsetup, CMap *map, int writeTerrain)
{
	FileWriter *f = NULL;
	int i, j;

	try {
		f = CreateFileWriter(mapsetup);
	
		f->printf("-- Stratagus Map Setup\n");
		f->printf("-- File generated by the stratagus builtin editor.\n");
		// MAPTODO Copyright notice in generated file
		f->printf("-- File licensed under the GNU GPL version 2.\n\n");
	
		f->printf("-- player configuration\n");
		for (i = 0; i < PlayerMax; ++i) {
			f->printf("SetStartView(%d, %d, %d)\n", i, Players[i].StartX, Players[i].StartY);
			f->printf("SetPlayerData(%d, \"Resources\", \"%s\", %d)\n",
				i, DefaultResourceNames[WoodCost], 
				Players[i].Resources[WoodCost]);
			f->printf("SetPlayerData(%d, \"Resources\", \"%s\", %d)\n",
				i, DefaultResourceNames[GoldCost], 
				Players[i].Resources[GoldCost]);
			f->printf("SetPlayerData(%d, \"Resources\", \"%s\", %d)\n",
				i, DefaultResourceNames[OilCost], 
				Players[i].Resources[OilCost]);
			f->printf("SetPlayerData(%d, \"RaceName\", \"%s\")\n",
				i, PlayerRaces.Name[Players[i].Race]);
			f->printf("SetAiType(%d, \"%s\")\n",
				i, Players[i].AiName.c_str());
		}
		f->printf("\n");

		f->printf("-- load tilesets\n");
		f->printf("LoadTileModels(\"%s\")\n\n", map->TileModelsFileName);
	
		if (writeTerrain) {
			f->printf("-- Tiles Map\n");
			for (i = 0; i < map->Info.MapHeight; ++i) {
				for (j = 0; j < map->Info.MapWidth; ++j) {
					int tile;
					int n;
			
					tile = map->Fields[j+i*map->Info.MapWidth].Tile;
					for (n=0; n < map->Tileset.NumTiles && tile != map->Tileset.Table[n]; ++n) {
					}
					f->printf("SetTile(%3d, %d, %d)\n", n, j, i);
				}
			}
		}

		f->printf("-- place units\n");
		for (i = 0; i < NumUnits; ++i) {
			f->printf("unit= CreateUnit(\"%s\", %d, {%d, %d})\n",
				Units[i]->Type->Ident.c_str(),
				Units[i]->Player->Index,
				Units[i]->X, Units[i]->Y);
			if (Units[i]->Type->GivesResource) {
				f->printf("SetResourcesHeld(unit, %d)\n", Units[i]->ResourcesHeld);
			}
		}
		f->printf("\n\n");
	} catch (const FileException &) {
		fprintf(stderr,"Can't save map setup : `%s' \n", mapsetup);
		delete f;
		return -1;
	}
	delete f;

	return 1;
}