changeset 19853:1d664830080e draft

(svn r24786) -Codechange: Add SettingsProfile enum for difficulty profiles and highscore tables.
author frosch <frosch@openttd.org>
date Wed, 05 Dec 2012 19:34:45 +0000
parents aad34b2f73e6
children 376fe8400147
files src/ai/ai_config.cpp src/ai/ai_gui.cpp src/highscore.cpp src/highscore.h src/highscore_gui.cpp src/script/script_config.cpp src/script/script_info.cpp src/settings.cpp src/settings_type.h src/table/gameopt_settings.ini
diffstat 10 files changed, 60 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/ai_config.cpp
+++ b/src/ai/ai_config.cpp
@@ -80,13 +80,13 @@
 {
 	if (this->info == NULL) {
 		SettingValueList::const_iterator it = this->settings.find(name);
-		if (it == this->settings.end() || GetGameSettings().difficulty.diff_level != 3) {
+		if (it == this->settings.end() || GetGameSettings().difficulty.diff_level != SP_CUSTOM) {
 			assert(strcmp("start_date", name) == 0);
 			switch (GetGameSettings().difficulty.diff_level) {
-				case 0: return AI::START_NEXT_EASY;
-				case 1: return AI::START_NEXT_MEDIUM;
-				case 2: return AI::START_NEXT_HARD;
-				case 3: return AI::START_NEXT_MEDIUM;
+				case SP_EASY:   return AI::START_NEXT_EASY;
+				case SP_MEDIUM: return AI::START_NEXT_MEDIUM;
+				case SP_HARD:   return AI::START_NEXT_HARD;
+				case SP_CUSTOM: return AI::START_NEXT_MEDIUM;
 				default: NOT_REACHED();
 			}
 		}
--- a/src/ai/ai_gui.cpp
+++ b/src/ai/ai_gui.cpp
@@ -422,12 +422,12 @@
 	void CheckDifficultyLevel()
 	{
 		if (_game_mode == GM_MENU) {
-			if (_settings_newgame.difficulty.diff_level != 3) {
-				_settings_newgame.difficulty.diff_level = 3;
+			if (_settings_newgame.difficulty.diff_level != SP_CUSTOM) {
+				_settings_newgame.difficulty.diff_level = SP_CUSTOM;
 				ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, WL_WARNING);
 			}
-		} else if (_settings_game.difficulty.diff_level != 3) {
-			IConsoleSetSetting("difficulty.diff_level", 3);
+		} else if (_settings_game.difficulty.diff_level != SP_CUSTOM) {
+			IConsoleSetSetting("difficulty.diff_level", SP_CUSTOM);
 		}
 	}
 
--- a/src/highscore.cpp
+++ b/src/highscore.cpp
@@ -20,7 +20,7 @@
 #include "core/sort_func.hpp"
 #include "debug.h"
 
-HighScore _highscore_table[5][5]; // 4 difficulty-settings (+ network); top 5
+HighScore _highscore_table[SP_HIGHSCORE_END][5]; ///< various difficulty-settings; top 5
 char *_highscore_file; ///< The file to store the highscore data in.
 
 static const StringID _endgame_perf_titles[] = {
@@ -82,8 +82,10 @@
 	return (*b)->old_economy[0].performance_history - (*a)->old_economy[0].performance_history;
 }
 
-/* Save the highscores in a network game when it has ended */
-#define LAST_HS_ITEM lengthof(_highscore_table) - 1
+/**
+ * Save the highscores in a network game when it has ended
+ * @return Position of the local company in the highscore list.
+ */
 int8 SaveHighScoreValueNetwork()
 {
 	const Company *c;
@@ -99,11 +101,11 @@
 	{
 		uint i;
 
-		memset(_highscore_table[LAST_HS_ITEM], 0, sizeof(_highscore_table[0]));
+		memset(_highscore_table[SP_MULTIPLAYER], 0, sizeof(_highscore_table[SP_MULTIPLAYER]));
 
 		/* Copy over Top5 companies */
-		for (i = 0; i < lengthof(_highscore_table[LAST_HS_ITEM]) && i < count; i++) {
-			HighScore *hs = &_highscore_table[LAST_HS_ITEM][i];
+		for (i = 0; i < lengthof(_highscore_table[SP_MULTIPLAYER]) && i < count; i++) {
+			HighScore *hs = &_highscore_table[SP_MULTIPLAYER][i];
 
 			SetDParam(0, cl[i]->index);
 			SetDParam(1, cl[i]->index);
@@ -129,7 +131,7 @@
 		uint i;
 		HighScore *hs;
 
-		for (i = 0; i < LAST_HS_ITEM; i++) { // don't save network highscores
+		for (i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
 			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), StrEmpty(hs->company) ? 0 : (int)strlen(&hs->company[1]) + 1);
@@ -139,7 +141,7 @@
 						fwrite(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
 						fwrite("  ", 2, 1, fp)                       != 1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
 					DEBUG(misc, 1, "Could not save highscore.");
-					i = LAST_HS_ITEM;
+					i = SP_SAVED_HIGHSCORE_END;
 					break;
 				}
 			}
@@ -159,7 +161,7 @@
 		uint i;
 		HighScore *hs;
 
-		for (i = 0; i < LAST_HS_ITEM; i++) { // don't load network highscores
+		for (i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
 			for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
 				byte length;
 				if (fread(&length, sizeof(length), 1, fp)       !=  1 ||
@@ -167,7 +169,7 @@
 						fread(&hs->score, sizeof(hs->score), 1, fp) !=  1 ||
 						fseek(fp, 2, SEEK_CUR)                      == -1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
 					DEBUG(misc, 1, "Highscore corrupted");
-					i = LAST_HS_ITEM;
+					i = SP_SAVED_HIGHSCORE_END;
 					break;
 				}
 				*lastof(hs->company) = '\0';
--- a/src/highscore.h
+++ b/src/highscore.h
@@ -14,6 +14,7 @@
 
 #include "strings_type.h"
 #include "company_type.h"
+#include "settings_type.h"
 
 struct HighScore {
 	char company[100];
@@ -21,7 +22,7 @@
 	uint16 score;   ///< do NOT change type, will break hs.dat
 };
 
-extern HighScore _highscore_table[5][5]; // 4 difficulty-settings (+ network); top 5
+extern HighScore _highscore_table[SP_HIGHSCORE_END][5];
 
 void SaveToHighScore();
 void LoadFromHighScore();
--- a/src/highscore_gui.cpp
+++ b/src/highscore_gui.cpp
@@ -103,10 +103,10 @@
 			}
 		}
 
-		/* In a network game show the endscores of the custom difficulty 'network' which is the last one
-		 * as well as generate a TOP5 of that game, and not an all-time top5. */
+		/* In a network game show the endscores of the custom difficulty 'network' which is
+		 * a TOP5 of that game, and not an all-time TOP5. */
 		if (_networking) {
-			this->window_number = lengthof(_highscore_table) - 1;
+			this->window_number = SP_MULTIPLAYER;
 			this->rank = SaveHighScoreValueNetwork();
 		} else {
 			/* in single player _local company is always valid */
--- a/src/script/script_config.cpp
+++ b/src/script/script_config.cpp
@@ -87,7 +87,7 @@
 int ScriptConfig::GetSetting(const char *name) const
 {
 	/* Return default values if the difficulty is not set to Custom */
-	if (GetGameSettings().difficulty.diff_level != 3) {
+	if (GetGameSettings().difficulty.diff_level != SP_CUSTOM) {
 		return this->info->GetSettingDefaultValue(name);
 	}
 
--- a/src/script/script_info.cpp
+++ b/src/script/script_info.cpp
@@ -294,10 +294,10 @@
 		if (strcmp((*it).name, name) != 0) continue;
 		/* The default value depends on the difficulty level */
 		switch (GetGameSettings().difficulty.diff_level) {
-			case 0: return (*it).easy_value;
-			case 1: return (*it).medium_value;
-			case 2: return (*it).hard_value;
-			case 3: return (*it).custom_value;
+			case SP_EASY:   return (*it).easy_value;
+			case SP_MEDIUM: return (*it).medium_value;
+			case SP_HARD:   return (*it).hard_value;
+			case SP_CUSTOM: return (*it).custom_value;
 			default: NOT_REACHED();
 		}
 	}
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -1032,7 +1032,7 @@
  * R: area restructuring (0 = permissive, 2 = hostile)
  * S: the difficulty level
  */
-static const DifficultySettings _default_game_diff[3] = { /*
+static const DifficultySettings _default_game_diff[SP_END] = { /*
 	 A, C, D,      E, F, G, H, J, K, L, M, N, O, P, Q, R, S*/
 	{2, 2, 4, 300000, 2, 0, 2, 1, 2, 0, 1, 0, 0, 0, 0, 0, 0}, ///< easy
 	{4, 2, 3, 150000, 3, 1, 3, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1}, ///< medium
@@ -1041,12 +1041,11 @@
 
 void SetDifficultyLevel(int mode, DifficultySettings *gm_opt)
 {
-	assert(mode <= 3);
-
-	if (mode != 3) {
+	if (mode != SP_CUSTOM) {
+		assert(mode >= SP_BEGIN && mode < SP_END);
 		*gm_opt = _default_game_diff[mode];
 	} else {
-		gm_opt->diff_level = 3;
+		gm_opt->diff_level = SP_CUSTOM;
 	}
 }
 
@@ -1054,7 +1053,7 @@
 static void ValidateSettings()
 {
 	/* Force the difficulty levels to correct values if they are invalid. */
-	if (_settings_newgame.difficulty.diff_level != 3) {
+	if (_settings_newgame.difficulty.diff_level != SP_CUSTOM) {
 		SetDifficultyLevel(_settings_newgame.difficulty.diff_level, &_settings_newgame.difficulty);
 	}
 
@@ -1077,13 +1076,13 @@
 static bool DifficultyChange(int32)
 {
 	if (_game_mode == GM_MENU) {
-		if (_settings_newgame.difficulty.diff_level != 3) {
+		if (_settings_newgame.difficulty.diff_level != SP_CUSTOM) {
 			ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, WL_WARNING);
-			_settings_newgame.difficulty.diff_level = 3;
+			_settings_newgame.difficulty.diff_level = SP_CUSTOM;
 		}
 		SetWindowClassesDirty(WC_SELECT_GAME);
 	} else {
-		_settings_game.difficulty.diff_level = 3;
+		_settings_game.difficulty.diff_level = SP_CUSTOM;
 	}
 
 	/* If we are a network-client, update the difficult setting (if it is open).
--- a/src/settings_type.h
+++ b/src/settings_type.h
@@ -20,6 +20,23 @@
 #include "zoom_type.h"
 #include "openttd.h"
 
+
+/** Settings profiles and highscore tables. */
+enum SettingsProfile {
+	SP_BEGIN = 0,
+	SP_EASY = SP_BEGIN,                       ///< Easy difficulty.
+	SP_MEDIUM,                                ///< Medium difficulty.
+	SP_HARD,                                  ///< Hard difficulty.
+
+	SP_END,                                   ///< End of setting profiles.
+
+	SP_CUSTOM = SP_END,                       ///< No profile, special "custom" highscore.
+	SP_SAVED_HIGHSCORE_END,                   ///< End of saved highscore tables.
+
+	SP_MULTIPLAYER = SP_SAVED_HIGHSCORE_END,  ///< Special "multiplayer" highscore. Not saved, always specific to the current game.
+	SP_HIGHSCORE_END,                         ///< End of highscore tables.
+};
+
 /** Available industry map generation densities. */
 enum IndustryDensity {
 	ID_FUND_ONLY, ///< The game does not build industries.
@@ -50,7 +67,7 @@
 	byte   line_reverse_mode;                ///< reversing at stations or not
 	byte   disasters;                        ///< are disasters enabled
 	byte   town_council_tolerance;           ///< minimum required town ratings to be allowed to demolish stuff
-	byte   diff_level;                       ///< the difficulty level
+	byte   diff_level;                       ///< the difficulty level. @see SettingsProfile
 };
 
 /** Settings related to the GUI and other stuff that is not saved in the savegame. */
--- a/src/table/gameopt_settings.ini
+++ b/src/table/gameopt_settings.ini
@@ -92,9 +92,9 @@
 base     = GameSettings
 var      = difficulty.diff_level
 type     = SLE_UINT8
-def      = 3
-min      = 0
-max      = 3
+def      = SP_CUSTOM
+min      = SP_EASY
+max      = SP_CUSTOM
 cat      = SC_BASIC
 
 [SDT_OMANY]