Changeset - r16950:9855f8364bec
[Not reviewed]
master
0 4 0
yexo - 14 years ago 2011-01-02 12:41:24
yexo@openttd.org
(svn r21696) -Feature [FS#4362]: allow changing the AI configuration in the scenario editor / in game
4 files changed with 64 insertions and 21 deletions:
0 comments (0 inline, 0 general)
src/ai/ai_gui.cpp
Show inline comments
 
@@ -182,12 +182,17 @@ struct AIListWindow : public Window {
 
		this->vscroll->SetCapacity(nwi->current_y / this->line_height);
 
		nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 
	}
 

	
 
	virtual void OnInvalidateData(int data)
 
	{
 
		if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) {
 
			delete this;
 
			return;
 
		}
 

	
 
		this->vscroll->SetCount((int)this->ai_info_list->size() + 1);
 

	
 
		/* selected goes from -1 .. length of ai list - 1. */
 
		this->selected = min(this->selected, this->vscroll->GetCount() - 2);
 
	}
 
};
 
@@ -260,13 +265,13 @@ struct AISettingsWindow : public Window 
 
		this->ai_config = AIConfig::GetConfig(slot);
 

	
 
		this->CreateNestedTree(desc);
 
		this->vscroll = this->GetScrollbar(AIS_WIDGET_SCROLLBAR);
 
		this->FinishInitNested(desc, slot);  // Initializes 'this->line_height' as side effect.
 

	
 
		this->SetWidgetDisabledState(AIS_WIDGET_RESET, _game_mode != GM_MENU);
 
		this->SetWidgetDisabledState(AIS_WIDGET_RESET, _game_mode != GM_MENU && Company::IsValidID(this->slot));
 

	
 
		this->vscroll->SetCount((int)this->ai_config->GetConfigList()->size());
 
	}
 

	
 
	virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
 
	{
 
@@ -294,13 +299,13 @@ struct AISettingsWindow : public Window 
 
		uint text_right   = r.right - (rtl ? 28 : WD_FRAMERECT_RIGHT);
 

	
 

	
 
		int y = r.top;
 
		for (; this->vscroll->IsVisible(i) && it != config->GetConfigList()->end(); i++, it++) {
 
			int current_value = config->GetSetting((*it).name);
 
			bool editable = (_game_mode == GM_MENU) || ((it->flags & AICONFIG_INGAME) != 0);
 
			bool editable = _game_mode == GM_MENU || !Company::IsValidID(this->slot) || (it->flags & AICONFIG_INGAME) != 0;
 

	
 
			StringID str;
 
			TextColour colour;
 
			uint idx = 0;
 
			if (StrEmpty((*it).description)) {
 
				str = STR_JUST_STRING;
 
@@ -350,13 +355,13 @@ struct AISettingsWindow : public Window 
 
				int num = (pt.y - wid->pos_y) / this->line_height + this->vscroll->GetPosition();
 
				if (num >= (int)this->ai_config->GetConfigList()->size()) break;
 

	
 
				AIConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
 
				for (int i = 0; i < num; i++) it++;
 
				AIConfigItem config_item = *it;
 
				if (_game_mode != GM_MENU && (config_item.flags & AICONFIG_INGAME) == 0) return;
 
				if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot) && (config_item.flags & AICONFIG_INGAME) == 0) return;
 

	
 
				bool bool_item = (config_item.flags & AICONFIG_BOOLEAN) != 0;
 

	
 
				int x = pt.x - wid->pos_x;
 
				if (_current_text_dir == TD_RTL) x = wid->current_x - x;
 
				x -= 4;
 
@@ -395,23 +400,26 @@ struct AISettingsWindow : public Window 
 

	
 
			case AIS_WIDGET_ACCEPT:
 
				delete this;
 
				break;
 

	
 
			case AIS_WIDGET_RESET:
 
				this->ai_config->ResetSettings();
 
				this->SetDirty();
 
				if (_game_mode == GM_MENU || !Company::IsValidID(this->slot)) {
 
					this->ai_config->ResetSettings();
 
					this->SetDirty();
 
				}
 
				break;
 
		}
 
	}
 

	
 
	virtual void OnQueryTextFinished(char *str)
 
	{
 
		if (StrEmpty(str)) return;
 
		AIConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
 
		for (int i = 0; i < this->clicked_row; i++) it++;
 
		if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot) && (it->flags & AICONFIG_INGAME) == 0) return;
 
		int32 value = atoi(str);
 
		this->ai_config->SetSetting((*it).name, value);
 
		this->CheckDifficultyLevel();
 
		this->SetDirty();
 
	}
 

	
 
@@ -426,12 +434,17 @@ struct AISettingsWindow : public Window 
 
	{
 
		if (--this->timeout == 0) {
 
			this->clicked_button = -1;
 
			this->SetDirty();
 
		}
 
	}
 

	
 
	virtual void OnInvalidateData(int data)
 
	{
 
		if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) delete this;
 
	}
 
};
 

	
 
/** Widgets for the AI settings window. */
 
static const NWidgetPart _nested_ai_settings_widgets[] = {
 
	NWidget(NWID_HORIZONTAL),
 
		NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
 
@@ -553,13 +566,13 @@ struct AIConfigWindow : public Window {
 
	}
 

	
 
	virtual void SetStringParameters(int widget) const
 
	{
 
		switch (widget) {
 
			case AIC_WIDGET_NUMBER:
 
				SetDParam(0, _settings_newgame.difficulty.max_no_competitors);
 
				SetDParam(0, GetGameSettings().difficulty.max_no_competitors);
 
				break;
 
		}
 
	}
 

	
 
	virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
 
	{
 
@@ -568,30 +581,49 @@ struct AIConfigWindow : public Window {
 
				this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
 
				size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
 
				break;
 
		}
 
	}
 

	
 
	/**
 
	 * Can the AI config in the given company slot be edited?
 
	 * @param slot The slot to query.
 
	 * @return True if and only if the given AI Config slot can e edited.
 
	 */
 
	static bool IsEditable(CompanyID slot)
 
	{
 
		if (_game_mode != GM_NORMAL) {
 
			return slot > 0 && slot <= GetGameSettings().difficulty.max_no_competitors;
 
		}
 
		if (Company::IsValidID(slot) || slot < 0) return false;
 

	
 
		int max_slot = GetGameSettings().difficulty.max_no_competitors;
 
		for (CompanyID cid = COMPANY_FIRST; cid < (CompanyID)max_slot && cid < MAX_COMPANIES; cid++) {
 
			if (Company::IsValidHumanID(cid)) max_slot++;
 
		}
 
		return slot < max_slot;
 
	}
 

	
 
	virtual void DrawWidget(const Rect &r, int widget) const
 
	{
 
		switch (widget) {
 
			case AIC_WIDGET_LIST: {
 
				int y = r.top;
 
				for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < MAX_COMPANIES; i++) {
 
					StringID text;
 

	
 
					if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != NULL) {
 
					if ((_game_mode != GM_NORMAL && i == 0) || (_game_mode == GM_NORMAL && Company::IsValidHumanID(i))) {
 
						text = STR_AI_CONFIG_HUMAN_PLAYER;
 
					} else if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != NULL) {
 
						SetDParamStr(0, AIConfig::GetConfig((CompanyID)i)->GetInfo()->GetName());
 
						text = STR_JUST_RAW_STRING;
 
					} else if (i == 0) {
 
						text = STR_AI_CONFIG_HUMAN_PLAYER;
 
					} else {
 
						text = STR_AI_CONFIG_RANDOM_AI;
 
					}
 
					DrawString(r.left + 10, r.right - 10, y + WD_MATRIX_TOP, text,
 
							(this->selected_slot == i) ? TC_WHITE : ((i > _settings_newgame.difficulty.max_no_competitors || i == 0) ? TC_SILVER : TC_ORANGE));
 
							(this->selected_slot == i) ? TC_WHITE : (IsEditable((CompanyID)i) ? TC_ORANGE : TC_SILVER));
 
					y += this->line_height;
 
				}
 
				break;
 
			}
 
		}
 
	}
 
@@ -600,15 +632,15 @@ struct AIConfigWindow : public Window {
 
	{
 
		switch (widget) {
 
			case AIC_WIDGET_DECREASE:
 
			case AIC_WIDGET_INCREASE: {
 
				int new_value;
 
				if (widget == AIC_WIDGET_DECREASE) {
 
					new_value = max(0, _settings_newgame.difficulty.max_no_competitors - 1);
 
					new_value = max(0, GetGameSettings().difficulty.max_no_competitors - 1);
 
				} else {
 
					new_value = min(MAX_COMPANIES - 1, _settings_newgame.difficulty.max_no_competitors + 1);
 
					new_value = min(MAX_COMPANIES - 1, GetGameSettings().difficulty.max_no_competitors + 1);
 
				}
 
				IConsoleSetSetting("difficulty.max_no_competitors", new_value);
 
				this->InvalidateData();
 
				break;
 
			}
 

	
 
@@ -617,23 +649,23 @@ struct AIConfigWindow : public Window {
 
				this->InvalidateData();
 
				if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
 
				break;
 
			}
 

	
 
			case AIC_WIDGET_MOVE_UP:
 
				if (this->selected_slot > 1) {
 
					Swap(_settings_newgame.ai_config[this->selected_slot], _settings_newgame.ai_config[this->selected_slot - 1]);
 
				if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) {
 
					Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot - 1]);
 
					this->selected_slot--;
 
					this->vscroll->ScrollTowards(this->selected_slot);
 
					this->InvalidateData();
 
				}
 
				break;
 

	
 
			case AIC_WIDGET_MOVE_DOWN:
 
				if (this->selected_slot < _settings_newgame.difficulty.max_no_competitors) {
 
					Swap(_settings_newgame.ai_config[this->selected_slot], _settings_newgame.ai_config[this->selected_slot + 1]);
 
				if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) {
 
					Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot + 1]);
 
					this->selected_slot++;
 
					this->vscroll->ScrollTowards(this->selected_slot);
 
					this->InvalidateData();
 
				}
 
				break;
 

	
 
@@ -660,22 +692,22 @@ struct AIConfigWindow : public Window {
 
				break;
 
		}
 
	}
 

	
 
	virtual void OnInvalidateData(int data)
 
	{
 
		if (this->selected_slot == 0 || this->selected_slot > _settings_newgame.difficulty.max_no_competitors) {
 
		if (!IsEditable(this->selected_slot)) {
 
			this->selected_slot = INVALID_COMPANY;
 
		}
 

	
 
		this->SetWidgetDisabledState(AIC_WIDGET_DECREASE, _settings_newgame.difficulty.max_no_competitors == 0);
 
		this->SetWidgetDisabledState(AIC_WIDGET_INCREASE, _settings_newgame.difficulty.max_no_competitors == MAX_COMPANIES - 1);
 
		this->SetWidgetDisabledState(AIC_WIDGET_DECREASE, GetGameSettings().difficulty.max_no_competitors == 0);
 
		this->SetWidgetDisabledState(AIC_WIDGET_INCREASE, GetGameSettings().difficulty.max_no_competitors == MAX_COMPANIES - 1);
 
		this->SetWidgetDisabledState(AIC_WIDGET_CHANGE, this->selected_slot == INVALID_COMPANY);
 
		this->SetWidgetDisabledState(AIC_WIDGET_CONFIGURE, this->selected_slot == INVALID_COMPANY);
 
		this->SetWidgetDisabledState(AIC_WIDGET_MOVE_UP, this->selected_slot == INVALID_COMPANY || this->selected_slot == 1);
 
		this->SetWidgetDisabledState(AIC_WIDGET_MOVE_DOWN, this->selected_slot == INVALID_COMPANY || this->selected_slot == _settings_newgame.difficulty.max_no_competitors);
 
		this->SetWidgetDisabledState(AIC_WIDGET_MOVE_UP, this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot - 1)));
 
		this->SetWidgetDisabledState(AIC_WIDGET_MOVE_DOWN, this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot + 1)));
 
	}
 
};
 

	
 
/** Open the AI config window. */
 
void ShowAIConfigWindow()
 
{
src/company_cmd.cpp
Show inline comments
 
@@ -893,12 +893,16 @@ CommandCost CmdCompanyCtrl(TileIndex til
 
			break;
 
		}
 

	
 
		default: return CMD_ERROR;
 
	}
 

	
 
	InvalidateWindowClassesData(WC_GAME_OPTIONS);
 
	InvalidateWindowClassesData(WC_AI_SETTINGS);
 
	InvalidateWindowClassesData(WC_AI_LIST);
 

	
 
	return CommandCost();
 
}
 

	
 
/**
 
 * Change the company manager's face.
 
 * @param tile unused
src/lang/english.txt
Show inline comments
 
@@ -345,12 +345,13 @@ STR_SCENEDIT_FILE_MENU_QUIT             
 
############ range for SE file menu starts
 

	
 
############ range for settings menu starts
 
STR_SETTINGS_MENU_GAME_OPTIONS                                  :Game options
 
STR_SETTINGS_MENU_DIFFICULTY_SETTINGS                           :Difficulty settings
 
STR_SETTINGS_MENU_CONFIG_SETTINGS                               :Advanced settings
 
STR_SETTINGS_MENU_AI_SETTINGS                                   :AI settings
 
STR_SETTINGS_MENU_NEWGRF_SETTINGS                               :NewGRF settings
 
STR_SETTINGS_MENU_TRANSPARENCY_OPTIONS                          :Transparency options
 
STR_SETTINGS_MENU_TOWN_NAMES_DISPLAYED                          :Town names displayed
 
STR_SETTINGS_MENU_STATION_NAMES_DISPLAYED                       :Station names displayed
 
STR_SETTINGS_MENU_WAYPOINTS_DISPLAYED                           :Waypoint names displayed
 
STR_SETTINGS_MENU_SIGNS_DISPLAYED                               :Signs displayed
src/toolbar_gui.cpp
Show inline comments
 
@@ -286,12 +286,13 @@ static CallBackFunction ToolbarFastForwa
 
/* --- Options button menu --- */
 

	
 
enum OptionMenuEntries {
 
	OME_GAMEOPTIONS,
 
	OME_DIFFICULTIES,
 
	OME_SETTINGS,
 
	OME_AI_SETTINGS,
 
	OME_NEWGRFSETTINGS,
 
	OME_TRANSPARENCIES,
 
	OME_SHOW_TOWNNAMES,
 
	OME_SHOW_STATIONNAMES,
 
	OME_SHOW_WAYPOINTNAMES,
 
	OME_SHOW_SIGNS,
 
@@ -304,12 +305,16 @@ enum OptionMenuEntries {
 
static CallBackFunction ToolbarOptionsClick(Window *w)
 
{
 
	DropDownList *list = new DropDownList();
 
	list->push_back(new DropDownListStringItem(STR_SETTINGS_MENU_GAME_OPTIONS,             OME_GAMEOPTIONS, false));
 
	list->push_back(new DropDownListStringItem(STR_SETTINGS_MENU_DIFFICULTY_SETTINGS,      OME_DIFFICULTIES, false));
 
	list->push_back(new DropDownListStringItem(STR_SETTINGS_MENU_CONFIG_SETTINGS,          OME_SETTINGS, false));
 
	/* Changes to the per-AI settings don't get send from the server to the clients. Clients get
 
	 * the settings once they join but never update it. As such don't show the window at all
 
	 * to network clients. */
 
	if (!_networking || _network_server) list->push_back(new DropDownListStringItem(STR_SETTINGS_MENU_AI_SETTINGS, OME_AI_SETTINGS, false));
 
	list->push_back(new DropDownListStringItem(STR_SETTINGS_MENU_NEWGRF_SETTINGS,          OME_NEWGRFSETTINGS, false));
 
	list->push_back(new DropDownListStringItem(STR_SETTINGS_MENU_TRANSPARENCY_OPTIONS,     OME_TRANSPARENCIES, false));
 
	list->push_back(new DropDownListItem(-1, false));
 
	list->push_back(new DropDownListCheckedItem(STR_SETTINGS_MENU_TOWN_NAMES_DISPLAYED,    OME_SHOW_TOWNNAMES, false, HasBit(_display_opt, DO_SHOW_TOWN_NAMES)));
 
	list->push_back(new DropDownListCheckedItem(STR_SETTINGS_MENU_STATION_NAMES_DISPLAYED, OME_SHOW_STATIONNAMES, false, HasBit(_display_opt, DO_SHOW_STATION_NAMES)));
 
	list->push_back(new DropDownListCheckedItem(STR_SETTINGS_MENU_WAYPOINTS_DISPLAYED,     OME_SHOW_WAYPOINTNAMES, false, HasBit(_display_opt, DO_SHOW_WAYPOINT_NAMES)));
 
@@ -327,12 +332,13 @@ static CallBackFunction ToolbarOptionsCl
 
static CallBackFunction MenuClickSettings(int index)
 
{
 
	switch (index) {
 
		case OME_GAMEOPTIONS:          ShowGameOptions();                               return CBF_NONE;
 
		case OME_DIFFICULTIES:         ShowGameDifficulty();                            return CBF_NONE;
 
		case OME_SETTINGS:             ShowGameSettings();                              return CBF_NONE;
 
		case OME_AI_SETTINGS:          ShowAIConfigWindow();                            return CBF_NONE;
 
		case OME_NEWGRFSETTINGS:       ShowNewGRFSettings(!_networking && _settings_client.gui.UserIsAllowedToChangeNewGRFs(), true, true, &_grfconfig); return CBF_NONE;
 
		case OME_TRANSPARENCIES:       ShowTransparencyToolbar();                       break;
 

	
 
		case OME_SHOW_TOWNNAMES:       ToggleBit(_display_opt, DO_SHOW_TOWN_NAMES);     break;
 
		case OME_SHOW_STATIONNAMES:    ToggleBit(_display_opt, DO_SHOW_STATION_NAMES);  break;
 
		case OME_SHOW_WAYPOINTNAMES:   ToggleBit(_display_opt, DO_SHOW_WAYPOINT_NAMES); break;
0 comments (0 inline, 0 general)