Changeset - r2987:d77581f9edfd
[Not reviewed]
master
0 1 0
tron - 19 years ago 2006-02-06 08:28:31
tron@openttd.org
(svn r3562) Remove pointless buffer copies
1 file changed with 2 insertions and 9 deletions:
0 comments (0 inline, 0 general)
players.c
Show inline comments
 
@@ -895,254 +895,247 @@ int32 CmdPlayerCtrl(int x, int y, uint32
 

	
 
		if (!(flags & DC_EXEC)) return 0;
 

	
 
		p = GetPlayer(p2);
 

	
 
		/* Only allow removal of HUMAN companies */
 
		if (IS_HUMAN_PLAYER(p->index)) {
 
			/* Delete any open window of the company */
 
			DeletePlayerWindows(p->index);
 

	
 
			/* Show the bankrupt news */
 
			SetDParam(0, p->name_1);
 
			SetDParam(1, p->name_2);
 
			AddNewsItem( (StringID)(p->index + 16*3), NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
 

	
 
			/* Remove the company */
 
			ChangeOwnershipOfPlayerItems(p->index, OWNER_SPECTATOR);
 
			p->money64 = p->player_money = 100000000; // XXX - wtf?
 
			p->is_active = false;
 
		}
 
		RemoveAllEngineReplacementForPlayer(p);
 

	
 
	} break;
 

	
 
	case 3: { /* Merge a company (#1) into another company (#2), elimination company #1 */
 
		PlayerID pid_old = GB(p2,  0, 16);
 
		PlayerID pid_new = GB(p2, 16, 16);
 

	
 
		if (pid_old >= MAX_PLAYERS || pid_new >= MAX_PLAYERS) return CMD_ERROR;
 

	
 
		if (!(flags & DC_EXEC)) return CMD_ERROR;
 

	
 
		ChangeOwnershipOfPlayerItems(pid_old, pid_new);
 
		DeletePlayerStuff(pid_old);
 
	} break;
 
	default: return CMD_ERROR;
 
	}
 

	
 
	return 0;
 
}
 

	
 
static const StringID _endgame_perf_titles[16] = {
 
	STR_0213_BUSINESSMAN,
 
	STR_0213_BUSINESSMAN,
 
	STR_0213_BUSINESSMAN,
 
	STR_0213_BUSINESSMAN,
 
	STR_0213_BUSINESSMAN,
 
	STR_0214_ENTREPRENEUR,
 
	STR_0214_ENTREPRENEUR,
 
	STR_0215_INDUSTRIALIST,
 
	STR_0215_INDUSTRIALIST,
 
	STR_0216_CAPITALIST,
 
	STR_0216_CAPITALIST,
 
	STR_0217_MAGNATE,
 
	STR_0217_MAGNATE,
 
	STR_0218_MOGUL,
 
	STR_0218_MOGUL,
 
	STR_0219_TYCOON_OF_THE_CENTURY,
 
};
 

	
 
StringID EndGameGetPerformanceTitleFromValue(uint value)
 
{
 
	value = minu(value, 1000) >> 6;
 
	if (value >= lengthof(_endgame_perf_titles)) value = lengthof(_endgame_perf_titles) - 1;
 

	
 
	return _endgame_perf_titles[value];
 
}
 

	
 
/* Return true if any cheat has been used, false otherwise */
 
static bool CheatHasBeenUsed(void)
 
{
 
	const Cheat* cht = (Cheat*) &_cheats;
 
	const Cheat* cht_last = &cht[sizeof(_cheats) / sizeof(Cheat)];
 

	
 
	for (; cht != cht_last; cht++) {
 
		if (cht->been_used)
 
			return true;
 
	}
 

	
 
	return false;
 
}
 

	
 
/* Save the highscore for the player */
 
int8 SaveHighScoreValue(const Player *p)
 
{
 
	HighScore *hs = _highscore_table[_opt.diff_level];
 
	uint i;
 
	uint16 score = p->old_economy[0].performance_history;
 

	
 
	/* Exclude cheaters from the honour of being in the highscore table */
 
	if (CheatHasBeenUsed())
 
		return -1;
 

	
 
	for (i = 0; i < lengthof(_highscore_table[0]); i++) {
 
		/* You are in the TOP5. Move all values one down and save us there */
 
		if (hs[i].score <= score) {
 
			char buf[sizeof(hs[i].company)];
 

	
 
			// move all elements one down starting from the replaced one
 
			memmove(&hs[i + 1], &hs[i], sizeof(HighScore) * (lengthof(_highscore_table[0]) - i - 1));
 
			SetDParam(0, p->president_name_1);
 
			SetDParam(1, p->president_name_2);
 
			SetDParam(2, p->name_1);
 
			SetDParam(3, p->name_2);
 
			GetString(buf, STR_HIGHSCORE_NAME); // get manager/company name string
 
			ttd_strlcpy(hs[i].company, buf, sizeof(buf));
 
			GetString(hs[i].company, STR_HIGHSCORE_NAME); // get manager/company name string
 
			hs[i].score = score;
 
			hs[i].title = EndGameGetPerformanceTitleFromValue(score);
 
			return i;
 
		}
 
	}
 

	
 
	return -1; // too bad; we did not make it into the top5
 
}
 

	
 
/* Sort all players given their performance */
 
static int CDECL HighScoreSorter(const void *a, const void *b)
 
{
 
	const Player *pa = *(const Player* const*)a;
 
	const Player *pb = *(const Player* const*)b;
 

	
 
	return pb->old_economy[0].performance_history - pa->old_economy[0].performance_history;
 
}
 

	
 
/* Save the highscores in a network game when it has ended */
 
#define LAST_HS_ITEM lengthof(_highscore_table) - 1
 
int8 SaveHighScoreValueNetwork(void)
 
{
 
	Player *p, *player_sort[MAX_PLAYERS];
 
	size_t count = 0;
 
	int8 player = -1;
 

	
 
	/* Sort all active players with the highest score first */
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->is_active)
 
			player_sort[count++] = p;
 
	}
 
	qsort(player_sort, count, sizeof(player_sort[0]), HighScoreSorter);
 

	
 
	{
 
		HighScore *hs;
 
		Player* const *p_cur = &player_sort[0];
 
		uint8 i;
 

	
 
		memset(_highscore_table[LAST_HS_ITEM], 0, sizeof(_highscore_table[0]));
 

	
 
		/* Copy over Top5 companies */
 
		for (i = 0; i < lengthof(_highscore_table[LAST_HS_ITEM]) && i < count; i++) {
 
			char buf[sizeof(_highscore_table[0]->company)];
 

	
 
			hs = &_highscore_table[LAST_HS_ITEM][i];
 
			SetDParam(0, (*p_cur)->president_name_1);
 
			SetDParam(1, (*p_cur)->president_name_2);
 
			SetDParam(2, (*p_cur)->name_1);
 
			SetDParam(3, (*p_cur)->name_2);
 
			GetString(buf, STR_HIGHSCORE_NAME); // get manager/company name string
 

	
 
			ttd_strlcpy(hs->company, buf, sizeof(buf));
 
			GetString(hs->company, STR_HIGHSCORE_NAME); // get manager/company name string
 
			hs->score = (*p_cur)->old_economy[0].performance_history;
 
			hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
 

	
 
			// get the ranking of the local player
 
			if ((*p_cur)->index == _local_player)
 
				player = i;
 

	
 
			p_cur++;
 
		}
 
	}
 

	
 
	/* Add top5 players to highscore table */
 
	return player;
 
}
 

	
 
/* Save HighScore table to file */
 
void SaveToHighScore(void)
 
{
 
	FILE *fp = fopen(_highscore_file, "wb");
 

	
 
	if (fp != NULL) {
 
		uint i;
 
		HighScore *hs;
 

	
 
		for (i = 0; i < LAST_HS_ITEM; i++) { // don't save network highscores
 
			for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
 
				/* First character is a command character, so strlen will fail on that */
 
				byte length = min(sizeof(hs->company), (hs->company[0] == '\0') ? 0 : strlen(&hs->company[1]) + 1);
 

	
 
				fwrite(&length, sizeof(length), 1, fp); // write away string length
 
				fwrite(hs->company, length, 1, fp);
 
				fwrite(&hs->score, sizeof(hs->score), 1, fp);
 
				fwrite("", 2, 1, fp); /* XXX - placeholder for hs->title, not saved anymore; compatibility */
 
			}
 
		}
 
		fclose(fp);
 
	}
 
}
 

	
 
/* Initialize the highscore table to 0 and if any file exists, load in values */
 
void LoadFromHighScore(void)
 
{
 
	FILE *fp = fopen(_highscore_file, "rb");
 

	
 
	memset(_highscore_table, 0, sizeof(_highscore_table));
 

	
 
	if (fp != NULL) {
 
		uint i;
 
		HighScore *hs;
 

	
 
		for (i = 0; i < LAST_HS_ITEM; i++) { // don't load network highscores
 
			for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
 
				byte length;
 
				fread(&length, sizeof(length), 1, fp);
 

	
 
				fread(hs->company, 1, length, fp);
 
				fread(&hs->score, sizeof(hs->score), 1, fp);
 
				fseek(fp, 2, SEEK_CUR); /* XXX - placeholder for hs->title, not saved anymore; compatibility */
 
				hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
 
			}
 
		}
 
		fclose(fp);
 
	}
 

	
 
	/* Initialize end of game variable (when to show highscore chart) */
 
	 _patches.ending_date = 2051;
 
}
 

	
 
// Save/load of players
 
static const SaveLoad _player_desc[] = {
 
	SLE_VAR(Player,name_2,					SLE_UINT32),
 
	SLE_VAR(Player,name_1,					SLE_STRINGID),
 

	
 
	SLE_VAR(Player,president_name_1,SLE_UINT16),
 
	SLE_VAR(Player,president_name_2,SLE_UINT32),
 

	
 
	SLE_VAR(Player,face,						SLE_UINT32),
 

	
 
	// money was changed to a 64 bit field in savegame version 1.
 
	SLE_CONDVAR(Player,money64,			SLE_VAR_I64 | SLE_FILE_I32, 0, 0),
 
	SLE_CONDVAR(Player,money64,			SLE_INT64, 1, 255),
 

	
 
	SLE_VAR(Player,current_loan,		SLE_INT32),
 

	
 
	SLE_VAR(Player,player_color,		SLE_UINT8),
 
	SLE_VAR(Player,player_money_fraction,SLE_UINT8),
 
	SLE_VAR(Player,avail_railtypes,		SLE_UINT8),
 
	SLE_VAR(Player,block_preview,		SLE_UINT8),
 

	
 
	SLE_VAR(Player,cargo_types,			SLE_UINT16),
 
	SLE_CONDVAR(Player, location_of_house,     SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
 
	SLE_CONDVAR(Player, location_of_house,     SLE_UINT32, 6, 255),
 
	SLE_CONDVAR(Player, last_build_coordinate, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
 
	SLE_CONDVAR(Player, last_build_coordinate, SLE_UINT32, 6, 255),
 
	SLE_VAR(Player,inaugurated_year,SLE_UINT8),
 

	
0 comments (0 inline, 0 general)