Changeset - r8987:5af9a103757a
[Not reviewed]
master
0 7 0
rubidium - 16 years ago 2008-04-19 08:21:55
rubidium@openttd.org
(svn r12782) -Codechange: remove some functions from functions.h and do not statically 'waste' memory when the old name array is not needed anymore.
7 files changed with 139 insertions and 105 deletions:
0 comments (0 inline, 0 general)
src/functions.h
Show inline comments
 
@@ -30,16 +30,12 @@ void InitializeAnimatedTiles();
 

	
 
/* misc_cmd.cpp */
 
void PlaceTreesRandomly();
 

	
 
void InitializeLandscapeVariables(bool only_constants);
 

	
 
/* misc.cpp */
 
bool IsCustomName(StringID id);
 
char *CopyFromOldName(StringID id);
 

	
 
/* misc functions */
 
/**
 
 * Mark a tile given by its coordinate dirty for repaint.
 
 *
 
 * @ingroup dirty
 
 */
 
@@ -65,17 +61,14 @@ void ShowHighscoreTable(int difficulty, 
 

	
 
void AskExitGame();
 
void AskExitToGameMenu();
 

	
 
void RedrawAutosave();
 

	
 
StringID RemapOldStringID(StringID s);
 

	
 
void UpdateViewportSignPos(ViewportSign *sign, int left, int top, StringID str);
 

	
 

	
 
/* callback from drivers that is called if the game size changes dynamically */
 
void GameSizeChanged();
 
const char *GetCurrentLocale(const char *param);
 

	
 
int ttd_main(int argc, char* argv[]);
 
void HandleExitGameRequest();
src/misc.cpp
Show inline comments
 
@@ -20,20 +20,18 @@
 
#include "zoom_func.h"
 
#include "functions.h"
 
#include "map_func.h"
 
#include "date_func.h"
 
#include "vehicle_func.h"
 
#include "texteff.hpp"
 
#include "string_func.h"
 
#include "gfx_func.h"
 
#include "core/alloc_type.hpp"
 

	
 
#include "table/strings.h"
 
#include "table/sprites.h"
 

	
 
char _name_array[512][32];
 
extern TileIndex _cur_tileloop_tile;
 

	
 
void InitializeVehicles();
 
void InitializeWaypoints();
 
void InitializeDepots();
 
void InitializeEngines();
 
@@ -46,16 +44,16 @@ void InitializeDockGui();
 
void InitializeIndustries();
 
void InitializeTowns();
 
void InitializeTrees();
 
void InitializeSigns();
 
void InitializeStations();
 
void InitializeCargoPackets();
 
static void InitializeNameMgr();
 
void InitializePlayers();
 
void InitializeCheats();
 
void InitializeNPF();
 
void InitializeOldNames();
 

	
 
void InitializeGame(int mode, uint size_x, uint size_y)
 
{
 
	AllocateMap(size_x, size_y);
 

	
 
	SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, VHM_NONE, WC_MAIN_WINDOW, 0);
 
@@ -90,13 +88,13 @@ void InitializeGame(int mode, uint size_
 
	InitializeSigns();
 
	InitializeStations();
 
	InitializeCargoPackets();
 
	InitializeIndustries();
 
	InitializeBuildingCounts();
 

	
 
	InitializeNameMgr();
 
	InitializeOldNames();
 
	InitializeVehiclesGuiList();
 
	InitializeTrains();
 
	InitializeNPF();
 

	
 
	AI_Initialize();
 
	InitializePlayers();
 
@@ -108,87 +106,24 @@ void InitializeGame(int mode, uint size_
 

	
 
	InitializeLandscapeVariables(false);
 

	
 
	ResetObjectToPlace();
 
}
 

	
 
bool IsCustomName(StringID id)
 
{
 
	return GB(id, 11, 5) == 15;
 
}
 

	
 

	
 
static void InitializeNameMgr()
 
{
 
	memset(_name_array, 0, sizeof(_name_array));
 
}
 

	
 
/* Copy and convert old custom names to UTF-8 */
 
char *CopyFromOldName(StringID id)
 
{
 
	if (!IsCustomName(id)) return NULL;
 

	
 
	if (CheckSavegameVersion(37)) {
 
		/* Old names were 32 characters long, so 128 characters should be
 
		 * plenty to allow for expansion when converted to UTF-8. */
 
		char tmp[128];
 
		const char *strfrom = _name_array[GB(id, 0, 9)];
 
		char *strto = tmp;
 

	
 
		for (; *strfrom != '\0'; strfrom++) {
 
			WChar c = (byte)*strfrom;
 

	
 
			/* Map from non-ISO8859-15 characters to UTF-8. */
 
			switch (c) {
 
				case 0xA4: c = 0x20AC; break; // Euro
 
				case 0xA6: c = 0x0160; break; // S with caron
 
				case 0xA8: c = 0x0161; break; // s with caron
 
				case 0xB4: c = 0x017D; break; // Z with caron
 
				case 0xB8: c = 0x017E; break; // z with caron
 
				case 0xBC: c = 0x0152; break; // OE ligature
 
				case 0xBD: c = 0x0153; break; // oe ligature
 
				case 0xBE: c = 0x0178; break; // Y with diaresis
 
				default: break;
 
			}
 

	
 
			/* Check character will fit into our buffer. */
 
			if (strto + Utf8CharLen(c) > lastof(tmp)) break;
 

	
 
			strto += Utf8Encode(strto, c);
 
		}
 

	
 
		/* Terminate the new string and copy it back to the name array */
 
		*strto = '\0';
 

	
 
		return strdup(tmp);
 
	} else {
 
		/* Name will already be in UTF-8. */
 
		return strdup(_name_array[GB(id, 0, 9)]);
 
	}
 
}
 

	
 
/* Calculate constants that depend on the landscape type. */
 
void InitializeLandscapeVariables(bool only_constants)
 
{
 
	if (only_constants) return;
 

	
 
	for (CargoID i = 0; i < NUM_CARGO; i++) {
 
		_cargo_payment_rates[i] = GetCargo(i)->initial_payment;
 
		_cargo_payment_rates_frac[i] = 0;
 
	}
 
}
 

	
 
static void Load_NAME()
 
{
 
	int index;
 

	
 
	while ((index = SlIterateArray()) != -1) {
 
		SlArray(_name_array[index], SlGetFieldLength(), SLE_UINT8);
 
	}
 
}
 

	
 
static const SaveLoadGlobVarList _date_desc[] = {
 
	SLEG_CONDVAR(_date,                   SLE_FILE_U16 | SLE_VAR_I32,  0,  30),
 
	SLEG_CONDVAR(_date,                   SLE_INT32,                  31, SL_MAX_VERSION),
 
	    SLEG_VAR(_date_fract,             SLE_UINT16),
 
	    SLEG_VAR(_tick_counter,           SLE_UINT16),
 
	    SLEG_VAR(_vehicle_id_ctr_day,     SLE_UINT16),
 
@@ -466,10 +401,9 @@ extern const ChunkHandler _misc_chunk_ha
 
	{ 'M3LO', Save_MAP3,     Load_MAP3,     CH_RIFF },
 
	{ 'M3HI', Save_MAP4,     Load_MAP4,     CH_RIFF },
 
	{ 'MAP5', Save_MAP5,     Load_MAP5,     CH_RIFF },
 
	{ 'MAPE', Save_MAP6,     Load_MAP6,     CH_RIFF },
 
	{ 'MAP7', Save_MAP7,     Load_MAP7,     CH_RIFF },
 

	
 
	{ 'NAME', NULL,          Load_NAME,     CH_ARRAY},
 
	{ 'DATE', SaveLoad_DATE, SaveLoad_DATE, CH_RIFF},
 
	{ 'VIEW', SaveLoad_VIEW, SaveLoad_VIEW, CH_RIFF | CH_LAST},
 
};
src/oldloader.cpp
Show inline comments
 
@@ -21,12 +21,13 @@
 
#include "ai/default/default.h"
 
#include "zoom_func.h"
 
#include "functions.h"
 
#include "date_func.h"
 
#include "vehicle_func.h"
 
#include "variables.h"
 
#include "strings_func.h"
 

	
 
#include "table/strings.h"
 

	
 
enum {
 
	HEADER_SIZE = 49,
 
	BUFFER_SIZE = 4096,
 
@@ -81,12 +82,18 @@ enum OldChunkType {
 
	OC_UINT16    = OC_VAR_U16  | OC_FILE_U16,
 
	OC_INT32     = OC_VAR_I32  | OC_FILE_I32,
 
	OC_UINT32    = OC_VAR_U32  | OC_FILE_U32,
 

	
 
	OC_TILE      = OC_VAR_U32  | OC_FILE_U16,
 

	
 
	/**
 
	 * Dereference the pointer once before writing to it,
 
	 * so we do not have to use big static arrays.
 
	 */
 
	OC_DEREFERENCE_POINTER = 1 << 31,
 

	
 
	OC_END       = 0 ///< End of the whole chunk, all 32bits set to zero
 
};
 

	
 
DECLARE_ENUM_AS_BIT_SET(OldChunkType);
 

	
 
typedef bool OldChunkProc(LoadgameState *ls, int num);
 
@@ -198,16 +205,16 @@ static inline uint32 ReadUint32(Loadgame
 
static bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks)
 
{
 
	const OldChunks *chunk = chunks;
 
	byte *base_ptr = (byte*)base;
 

	
 
	while (chunk->type != OC_END) {
 
		byte* ptr = (byte*)chunk->ptr;
 
		uint i;
 
		byte *ptr = (byte*)chunk->ptr;
 
		if ((chunk->type & OC_DEREFERENCE_POINTER) != 0) ptr = *(byte**)ptr;
 

	
 
		for (i = 0; i < chunk->amount; i++) {
 
		for (uint i = 0; i < chunk->amount; i++) {
 
			if (ls->failed) return false;
 

	
 
			/* Handle simple types */
 
			if (GetOldChunkType(chunk->type) != 0) {
 
				switch (GetOldChunkType(chunk->type)) {
 
					/* Just read the byte and forget about it */
 
@@ -388,13 +395,13 @@ static void FixOldVehicles()
 
   Old Orders are 2 bytes big */
 
#define REMAP_TOWN_IDX(x) ((x) - (0x0459154 - 0x0458EF0)) / 94
 
#define REMAP_ORDER_IDX(x) ((x) - (0x045AB08 - 0x0458EF0)) / 2
 

	
 
extern TileIndex *_animated_tile_list;
 
extern uint _animated_tile_count;
 
extern char _name_array[512][32];
 
extern char *_old_name_array;
 

	
 
static byte   _old_vehicle_multiplier;
 
static uint8  _old_map3[OLD_MAP_SIZE * 2];
 
static bool   _new_ttdpatch_format;
 
static uint32 _old_town_index;
 
static uint16 _old_string_id;
 
@@ -1518,13 +1525,13 @@ static const OldChunks main_chunk[] = {
 
	OCL_ASSERT( 0x547F2 ),
 

	
 
	OCL_CHUNK( 850, LoadOldVehicle ),
 

	
 
	OCL_ASSERT( 0x6F0F2 ),
 

	
 
	OCL_VAR (  OC_UINT8, 32 * 500, &_name_array[0] ),
 
	OCL_VAR (  OC_UINT8 | OC_DEREFERENCE_POINTER, 32 * 500, &_old_name_array ),
 

	
 
	OCL_NULL( 0x2000 ),            ///< Old hash-table, no longer in use
 

	
 
	OCL_CHUNK( 40, LoadOldSign ),
 
	OCL_CHUNK(256, LoadOldEngine ),
 

	
src/openttd.cpp
Show inline comments
 
@@ -88,12 +88,13 @@ StringID _switch_mode_errorstr;
 

	
 
void CallLandscapeTick();
 
void IncreaseDate();
 
void DoPaletteAnimations();
 
void MusicLoop();
 
void ResetMusic();
 
void ResetOldNames();
 

	
 
extern void SetDifficultyLevel(int mode, GameOptions *gm_opt);
 
extern Player* DoStartupNewPlayer(bool is_ai);
 
extern void ShowOSErrorBox(const char *buf);
 
extern void InitializeRailGUI();
 

	
 
@@ -1387,12 +1388,15 @@ bool AfterLoadGame()
 
		FOR_ALL_WAYPOINTS(wp) {
 
			wp->name = CopyFromOldName(wp->string);
 
			wp->string = STR_EMPTY;
 
		}
 
	}
 

	
 
	/* From this point the old names array is cleared. */
 
	ResetOldNames();
 

	
 
	/* convert road side to my format. */
 
	if (_opt.road_side) _opt.road_side = 1;
 

	
 
	/* Check if all NewGRFs are present, we are very strict in MP mode */
 
	GRFListCompatibility gcf_res = IsGoodGRFConfigList();
 
	if (_networking && gcf_res != GLC_ALL_GOOD) {
src/saveload.cpp
Show inline comments
 
@@ -1245,12 +1245,13 @@ static void UninitWriteZlib()
 
/*******************************************
 
 ************* END OF CODE *****************
 
 *******************************************/
 

	
 
/* these define the chunks */
 
extern const ChunkHandler _misc_chunk_handlers[];
 
extern const ChunkHandler _name_chunk_handlers[];
 
extern const ChunkHandler _cheat_chunk_handlers[] ;
 
extern const ChunkHandler _setting_chunk_handlers[];
 
extern const ChunkHandler _player_chunk_handlers[];
 
extern const ChunkHandler _engine_chunk_handlers[];
 
extern const ChunkHandler _veh_chunk_handlers[];
 
extern const ChunkHandler _waypoint_chunk_handlers[];
 
@@ -1265,12 +1266,13 @@ extern const ChunkHandler _animated_tile
 
extern const ChunkHandler _newgrf_chunk_handlers[];
 
extern const ChunkHandler _group_chunk_handlers[];
 
extern const ChunkHandler _cargopacket_chunk_handlers[];
 

	
 
static const ChunkHandler * const _chunk_handlers[] = {
 
	_misc_chunk_handlers,
 
	_name_chunk_handlers,
 
	_cheat_chunk_handlers,
 
	_setting_chunk_handlers,
 
	_veh_chunk_handlers,
 
	_waypoint_chunk_handlers,
 
	_depot_chunk_handlers,
 
	_order_chunk_handlers,
src/strings.cpp
Show inline comments
 
@@ -34,12 +34,13 @@
 
#include "player_func.h"
 
#include "player_base.h"
 
#include "fios.h"
 
#include "settings_type.h"
 
#include "video/video_driver.hpp"
 
#include "engine_func.h"
 
#include "saveload.h"
 

	
 
#include "table/strings.h"
 
#include "table/control_codes.h"
 

	
 
DynamicLanguages _dynlang;
 
char _userstring[128];
 
@@ -1223,37 +1224,12 @@ static char *GetSpecialPlayerNameString(
 
	}
 

	
 
	assert(0);
 
	return NULL;
 
}
 

	
 
/**
 
 * remap a string ID from the old format to the new format
 
 * @param s StringID that requires remapping
 
 * @return translated ID*/
 
StringID RemapOldStringID(StringID s)
 
{
 
	switch (s) {
 
		case 0x0006: return STR_SV_EMPTY;
 
		case 0x7000: return STR_SV_UNNAMED;
 
		case 0x70E4: return SPECSTR_PLAYERNAME_ENGLISH;
 
		case 0x70E9: return SPECSTR_PLAYERNAME_ENGLISH;
 
		case 0x8864: return STR_SV_TRAIN_NAME;
 
		case 0x902B: return STR_SV_ROADVEH_NAME;
 
		case 0x9830: return STR_SV_SHIP_NAME;
 
		case 0xA02F: return STR_SV_AIRCRAFT_NAME;
 

	
 
		default:
 
			if (IsInsideMM(s, 0x300F, 0x3030)) {
 
				return s - 0x300F + STR_SV_STNAME;
 
			} else {
 
				return s;
 
			}
 
	}
 
}
 

	
 
#ifdef ENABLE_NETWORK
 
extern void SortNetworkLanguages();
 
#else /* ENABLE_NETWORK */
 
static inline void SortNetworkLanguages() {}
 
#endif /* ENABLE_NETWORK */
 

	
 
@@ -1545,6 +1521,121 @@ void CheckForMissingGlyphsInLoadedLangua
 
					return;
 
				}
 
			}
 
		}
 
	}
 
}
 

	
 

	
 
/* --- Handling of saving/loading string IDs from old savegames --- */
 

	
 
/**
 
 * Remap a string ID from the old format to the new format
 
 * @param s StringID that requires remapping
 
 * @return translated ID
 
 */
 
StringID RemapOldStringID(StringID s)
 
{
 
	switch (s) {
 
		case 0x0006: return STR_SV_EMPTY;
 
		case 0x7000: return STR_SV_UNNAMED;
 
		case 0x70E4: return SPECSTR_PLAYERNAME_ENGLISH;
 
		case 0x70E9: return SPECSTR_PLAYERNAME_ENGLISH;
 
		case 0x8864: return STR_SV_TRAIN_NAME;
 
		case 0x902B: return STR_SV_ROADVEH_NAME;
 
		case 0x9830: return STR_SV_SHIP_NAME;
 
		case 0xA02F: return STR_SV_AIRCRAFT_NAME;
 

	
 
		default:
 
			if (IsInsideMM(s, 0x300F, 0x3030)) {
 
				return s - 0x300F + STR_SV_STNAME;
 
			} else {
 
				return s;
 
			}
 
	}
 
}
 

	
 
/** Location to load the old names to. */
 
char *_old_name_array = NULL;
 

	
 
/**
 
 * Copy and convert old custom names to UTF-8.
 
 * They were all stored in a 512 by 32 long string array and are
 
 * now stored with stations, waypoints and other places with names.
 
 * @param id the StringID of the custom name to clone.
 
 * @return the clones custom name.
 
 */
 
char *CopyFromOldName(StringID id)
 
{
 
	/* Is this name an (old) custom name? */
 
	if (GB(id, 11, 5) != 15) return NULL;
 

	
 
	if (CheckSavegameVersion(37)) {
 
		/* Old names were 32 characters long, so 128 characters should be
 
		 * plenty to allow for expansion when converted to UTF-8. */
 
		char tmp[128];
 
		const char *strfrom = &_old_name_array[32 * GB(id, 0, 9)];
 
		char *strto = tmp;
 

	
 
		for (; *strfrom != '\0'; strfrom++) {
 
			WChar c = (byte)*strfrom;
 

	
 
			/* Map from non-ISO8859-15 characters to UTF-8. */
 
			switch (c) {
 
				case 0xA4: c = 0x20AC; break; // Euro
 
				case 0xA6: c = 0x0160; break; // S with caron
 
				case 0xA8: c = 0x0161; break; // s with caron
 
				case 0xB4: c = 0x017D; break; // Z with caron
 
				case 0xB8: c = 0x017E; break; // z with caron
 
				case 0xBC: c = 0x0152; break; // OE ligature
 
				case 0xBD: c = 0x0153; break; // oe ligature
 
				case 0xBE: c = 0x0178; break; // Y with diaresis
 
				default: break;
 
			}
 

	
 
			/* Check character will fit into our buffer. */
 
			if (strto + Utf8CharLen(c) > lastof(tmp)) break;
 

	
 
			strto += Utf8Encode(strto, c);
 
		}
 

	
 
		/* Terminate the new string and copy it back to the name array */
 
		*strto = '\0';
 

	
 
		return strdup(tmp);
 
	} else {
 
		/* Name will already be in UTF-8. */
 
		return strdup(&_old_name_array[32 * GB(id, 0, 9)]);
 
	}
 
}
 

	
 
/**
 
 * Free the memory of the old names array.
 
 * Should be called once the old names have all been converted.
 
 */
 
void ResetOldNames()
 
{
 
	free(_old_name_array);
 
	_old_name_array = NULL;
 
}
 

	
 
/**
 
 * Initialize the old names table memory.
 
 */
 
void InitializeOldNames()
 
{
 
	free(_old_name_array);
 
	_old_name_array = CallocT<char>(512 * 32);
 
}
 

	
 
static void Load_NAME()
 
{
 
	int index;
 

	
 
	while ((index = SlIterateArray()) != -1) {
 
		SlArray(&_old_name_array[32 * index], SlGetFieldLength(), SLE_UINT8);
 
	}
 
}
 

	
 
extern const ChunkHandler _name_chunk_handlers[] = {
 
	{ 'NAME', NULL, Load_NAME, CH_ARRAY | CH_LAST},
 
};
src/strings_func.h
Show inline comments
 
@@ -75,7 +75,10 @@ struct StringIDCompare
 
{
 
	bool operator()(StringID s1, StringID s2) const { return StringIDSorter(&s1, &s2) < 0; }
 
};
 

	
 
void CheckForMissingGlyphsInLoadedLanguagePack();
 

	
 
StringID RemapOldStringID(StringID s);
 
char *CopyFromOldName(StringID id);
 

	
 
#endif /* STRINGS_TYPE_H */
0 comments (0 inline, 0 general)