/* static */ void AI::StartNew(CompanyID company, bool rerandomise_ai) { assert(Company::IsValidID(company)); /* Clients shouldn't start AIs */ if (_networking && !_network_server) return; AIConfig *config = AIConfig::GetConfig(company, AIConfig::SSS_FORCE_GAME); AIInfo *info = config->GetInfo(); if (info == NULL || (rerandomise_ai && config->IsRandom())) { info = AI::scanner_info->SelectRandomAI(); assert(info != NULL); /* Load default data and store the name in the settings */ config->Change(info->GetName(), -1, false, true); } Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE); Company *c = Company::Get(company); c->ai_info = info; assert(c->ai_instance == NULL); c->ai_instance = new AIInstance(); c->ai_instance->Initialize(info); cur_company.Restore(); InvalidateWindowData(WC_AI_DEBUG, 0, -1); return; }
virtual void DrawWidget(const Rect &r, int widget) const { if (widget != AIS_WIDGET_BACKGROUND) return; AIConfig *config = this->ai_config; AIConfigItemList::const_iterator it = config->GetConfigList()->begin(); int i = 0; for (; !this->vscroll->IsVisible(i); i++) it++; bool rtl = _current_text_dir == TD_RTL; uint buttons_left = rtl ? r.right - 23 : r.left + 4; uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : 28); 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 || !Company::IsValidID(this->slot) || (it->flags & AICONFIG_INGAME) != 0; StringID str; TextColour colour; uint idx = 0; if (StrEmpty((*it).description)) { str = STR_JUST_STRING; colour = TC_ORANGE; } else { str = STR_AI_SETTINGS_SETTING; colour = TC_LIGHT_BLUE; SetDParamStr(idx++, (*it).description); } if (((*it).flags & AICONFIG_BOOLEAN) != 0) { DrawFrameRect(buttons_left, y + 2, buttons_left + 19, y + 10, (current_value != 0) ? COLOUR_GREEN : COLOUR_RED, (current_value != 0) ? FR_LOWERED : FR_NONE); SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON); } else { DrawArrowButtons(buttons_left, y + 2, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > (*it).min_value, editable && current_value < (*it).max_value); if (it->labels != NULL && it->labels->Contains(current_value)) { SetDParam(idx++, STR_JUST_RAW_STRING); SetDParamStr(idx++, it->labels->Find(current_value)->second); } else { SetDParam(idx++, STR_JUST_INT); SetDParam(idx++, current_value); } } DrawString(text_left, text_right, y + WD_MATRIX_TOP, str, colour); y += this->line_height; } }
static void Load_AIPL() { /* Free all current data */ for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) { AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(NULL); } CompanyID index; while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) { if (index >= MAX_COMPANIES) SlErrorCorrupt("Too many AI configs"); _ai_saveload_is_random = 0; _ai_saveload_version = -1; SlObject(NULL, _ai_company); if (_networking && !_network_server) { if (Company::IsValidAiID(index)) AIInstance::LoadEmpty(); continue; } AIConfig *config = AIConfig::GetConfig(index, AIConfig::SSS_FORCE_GAME); if (StrEmpty(_ai_saveload_name)) { /* A random AI. */ config->Change(NULL, -1, false, true); } else { config->Change(_ai_saveload_name, _ai_saveload_version, false, _ai_saveload_is_random); if (!config->HasScript()) { /* No version of the AI available that can load the data. Try to load the * latest version of the AI instead. */ config->Change(_ai_saveload_name, -1, false, _ai_saveload_is_random); if (!config->HasScript()) { if (strcmp(_ai_saveload_name, "%_dummy") != 0) { DEBUG(script, 0, "The savegame has an AI by the name '%s', version %d which is no longer available.", _ai_saveload_name, _ai_saveload_version); DEBUG(script, 0, "A random other AI will be loaded in its place."); } else { DEBUG(script, 0, "The savegame had no AIs available at the time of saving."); DEBUG(script, 0, "A random available AI will be loaded now."); } } else { DEBUG(script, 0, "The savegame has an AI by the name '%s', version %d which is no longer available.", _ai_saveload_name, _ai_saveload_version); DEBUG(script, 0, "The latest version of that AI has been loaded instead, but it'll not get the savegame data as it's incompatible."); } /* Make sure the AI doesn't get the saveload data, as he was not the * writer of the saveload data in the first place */ _ai_saveload_version = -1; } } config->StringToSettings(_ai_saveload_settings); /* Start the AI directly if it was active in the savegame */ if (Company::IsValidAiID(index)) { AI::StartNew(index, false); AI::Load(index, _ai_saveload_version); } } }
virtual void DrawWidget(const Rect &r, int widget) const { if (widget != AIS_WIDGET_BACKGROUND) return; AIConfig *config = this->ai_config; AIConfigItemList::const_iterator it = config->GetConfigList()->begin(); int i = 0; for (; !this->vscroll.IsVisible(i); i++) it++; bool rtl = _dynlang.text_dir == TD_RTL; uint buttons_left = rtl ? r.right - 23 : r.left + 4; uint value_left = r.left + (rtl ? WD_FRAMERECT_LEFT : 28); uint value_right = r.right - (rtl ? 28 : WD_FRAMERECT_RIGHT); uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : 54); uint text_right = r.right - (rtl ? 54 : 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); uint x = rtl ? r.right : r.left; if (((*it).flags & AICONFIG_BOOLEAN) != 0) { DrawFrameRect(buttons_left, y + 2, buttons_left + 19, y + 10, (current_value != 0) ? COLOUR_GREEN : COLOUR_RED, (current_value != 0) ? FR_LOWERED : FR_NONE); } else { DrawArrowButtons(buttons_left, y + 2, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > (*it).min_value, editable && current_value < (*it).max_value); if (it->labels != NULL && it->labels->Find(current_value) != it->labels->End()) { x = DrawString(value_left, value_right, y + WD_MATRIX_TOP, it->labels->Find(current_value)->second, TC_ORANGE); } else { SetDParam(0, current_value); x = DrawString(value_left, value_right, y + WD_MATRIX_TOP, STR_JUST_INT, TC_ORANGE); } } DrawString(max(rtl ? 0U : x + 3, text_left), min(rtl ? x - 3 : r.right, text_right), y + WD_MATRIX_TOP, (*it).description, TC_LIGHT_BLUE); y += this->line_height; } }
static void SaveReal_AIPL(int *index_ptr) { CompanyID index = (CompanyID)*index_ptr; AIConfig *config = AIConfig::GetConfig(index); if (config->HasScript()) { strecpy(_ai_saveload_name, config->GetName(), lastof(_ai_saveload_name)); _ai_saveload_version = config->GetVersion(); } else { /* No AI is configured for this so store an empty string as name. */ _ai_saveload_name[0] = '\0'; _ai_saveload_version = -1; } _ai_saveload_is_random = config->IsRandom(); _ai_saveload_settings[0] = '\0'; config->SettingsToString(_ai_saveload_settings, lastof(_ai_saveload_settings)); SlObject(NULL, _ai_company); /* If the AI was active, store his data too */ if (Company::IsValidAiID(index)) AI::Save(index); }