Skip to content

Commit

Permalink
Change: AI/GS Config GUI overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuXarick committed Jan 22, 2019
1 parent c3dbe83 commit a02bcaa
Show file tree
Hide file tree
Showing 20 changed files with 519 additions and 148 deletions.
16 changes: 14 additions & 2 deletions src/ai/ai_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "../company_base.h"
#include "../company_func.h"
#include "../network/network.h"
#include "../saveload/saveload.h"
#include "../window_func.h"
#include "ai_scanner.hpp"
#include "ai_instance.hpp"
Expand Down Expand Up @@ -62,6 +63,11 @@
cur_company.Restore();

InvalidateWindowData(WC_AI_DEBUG, 0, -1);
if (config->IsRandom()) {
DeleteWindowById(WC_AI_SETTINGS, company);
} else {
InvalidateWindowData(WC_AI_SETTINGS, company);
}
return;
}

Expand Down Expand Up @@ -112,7 +118,13 @@
cur_company.Restore();

InvalidateWindowData(WC_AI_DEBUG, 0, -1);
DeleteWindowById(WC_AI_SETTINGS, company);
if (AIConfig::GetConfig(company, AIConfig::SSS_FORCE_GAME)->IsRandom()) {
InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_AI);
DeleteWindowById(WC_AI_SETTINGS, company);
DeleteWindowByClass(WC_TEXTFILE); // TODO: this is needed to avoid openttd crash, unless there's another way to manage this window
} else {
InvalidateWindowData(WC_AI_SETTINGS, company);
}
}

/* static */ void AI::Pause(CompanyID company)
Expand Down Expand Up @@ -276,7 +288,7 @@

/* static */ void AI::Save(CompanyID company)
{
if (!_networking || _network_server) {
if (!_networking || (_network_server && !_save_empty_script)) {
Company *c = Company::GetIfValid(company);
assert(c != NULL && c->ai_instance != NULL);

Expand Down
472 changes: 364 additions & 108 deletions src/ai/ai_gui.cpp

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/ai/ai_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@

#include "../company_base.h"
#include "../company_func.h"
#include "../window_func.h"

#include "../safeguards.h"

Expand Down Expand Up @@ -214,6 +215,7 @@ void AIInstance::Died()
ScriptInstance::Died();

ShowAIDebugWindow(_current_company);
InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_AI);

const AIInfo *info = AIConfig::GetConfig(_current_company, AIConfig::SSS_FORCE_GAME)->GetInfo();
if (info != NULL) {
Expand Down
50 changes: 40 additions & 10 deletions src/ai/ai_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool fo
AIInfo *info = NULL;
int version = -1;

if (versionParam == -1) {
if (versionParam <= -1) {
/* We want to load the latest version of this AI; so find it */
if (this->info_single_list.find(ai_name) != this->info_single_list.end()) return static_cast<AIInfo *>(this->info_single_list[ai_name]);

Expand All @@ -113,7 +113,8 @@ AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool fo
if (e == NULL) return NULL;
*e = '\0';
e++;
versionParam = atoi(e);
/* We may still want to load the latest available version of this AI */
if (versionParam == -1) versionParam = atoi(e);
/* Continue, like we were calling this function with a version. */
}

Expand All @@ -125,14 +126,43 @@ AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool fo
if (this->info_list.find(ai_name_tmp) != this->info_list.end()) return static_cast<AIInfo *>(this->info_list[ai_name_tmp]);
}

/* See if there is a compatible AI which goes by that name, with the highest
* version which allows loading the requested version */
ScriptInfoList::iterator it = this->info_list.begin();
for (; it != this->info_list.end(); it++) {
AIInfo *i = static_cast<AIInfo *>((*it).second);
if (strcasecmp(ai_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
version = (*it).second->GetVersion();
info = i;
if (versionParam != -2) {
/* See if there is a compatible AI which goes by that name, with the highest
* version which allows loading the requested version */
ScriptInfoList::iterator it = this->info_list.begin();
for (; it != this->info_list.end(); it++) {
AIInfo *i = static_cast<AIInfo *>((*it).second);
if (strcasecmp(ai_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
version = (*it).second->GetVersion();
info = i;
}
}
}

if (info == NULL && !force_exact_match && versionParam != -1) {
if (versionParam != -2) {
/* If we didn't find a match AI, maybe the user included a version */
char *e = strrchr(ai_name, '.');
if (e == NULL) return NULL;
*e = '\0';
e++;
versionParam = atoi(e);
/* Try to find a direct 'name.version' match */
char ai_name_tmp[1024];
seprintf(ai_name_tmp, lastof(ai_name_tmp), "%s.%d", ai_name, versionParam);
strtolower(ai_name_tmp);
if (this->info_list.find(ai_name_tmp) != this->info_list.end()) return static_cast<AIInfo *>(this->info_list[ai_name_tmp]);
}
/* See if there is a compatible AI which goes by that name, with the highest
* version which allows loading the requested version */
ScriptInfoList::iterator it = this->info_list.begin();
if (versionParam == -2) versionParam = -1;
for (; it != this->info_list.end(); it++) {
AIInfo *i = static_cast<AIInfo *>((*it).second);
if (strcasecmp(ai_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
version = (*it).second->GetVersion();
info = i;
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/game/game_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "../company_base.h"
#include "../company_func.h"
#include "../network/network.h"
#include "../saveload/saveload.h"
#include "../window_func.h"
#include "game.hpp"
#include "game_scanner.hpp"
Expand Down Expand Up @@ -197,7 +198,7 @@

/* static */ void Game::Save()
{
if (Game::instance != NULL && (!_networking || _network_server)) {
if (Game::instance != NULL && (!_networking || (_network_server && !_save_empty_script))) {
Backup<CompanyByte> cur_company(_current_company, OWNER_DEITY, FILE_LINE);
Game::instance->Save();
cur_company.Restore();
Expand Down
3 changes: 3 additions & 0 deletions src/game/game_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
#include "../script/api/game/game_waypointlist.hpp.sq"
#include "../script/api/game/game_window.hpp.sq"

#include "../window_func.h"

#include "../safeguards.h"


Expand Down Expand Up @@ -237,6 +239,7 @@ void GameInstance::Died()
ScriptInstance::Died();

ShowAIDebugWindow(OWNER_DEITY);
InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_AI);

const GameInfo *info = Game::GetInfo();
if (info != NULL) {
Expand Down
50 changes: 40 additions & 10 deletions src/game/game_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ GameInfo *GameScannerInfo::FindInfo(const char *nameParam, int versionParam, boo
GameInfo *info = NULL;
int version = -1;

if (versionParam == -1) {
if (versionParam <= -1) {
/* We want to load the latest version of this Game script; so find it */
if (this->info_single_list.find(game_name) != this->info_single_list.end()) return static_cast<GameInfo *>(this->info_single_list[game_name]);

Expand All @@ -54,7 +54,8 @@ GameInfo *GameScannerInfo::FindInfo(const char *nameParam, int versionParam, boo
if (e == NULL) return NULL;
*e = '\0';
e++;
versionParam = atoi(e);
/* We may still want to load the latest available version of this Game script */
if (versionParam == -1) versionParam = atoi(e);
/* Continue like we were calling this function with a version. */
}

Expand All @@ -66,14 +67,43 @@ GameInfo *GameScannerInfo::FindInfo(const char *nameParam, int versionParam, boo
if (this->info_list.find(game_name_tmp) != this->info_list.end()) return static_cast<GameInfo *>(this->info_list[game_name_tmp]);
}

/* See if there is a compatible Game script which goes by that name, with the highest
* version which allows loading the requested version */
ScriptInfoList::iterator it = this->info_list.begin();
for (; it != this->info_list.end(); it++) {
GameInfo *i = static_cast<GameInfo *>((*it).second);
if (strcasecmp(game_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
version = (*it).second->GetVersion();
info = i;
if (versionParam != -2) {
/* See if there is a compatible Game script which goes by that name, with the highest
* version which allows loading the requested version */
ScriptInfoList::iterator it = this->info_list.begin();
for (; it != this->info_list.end(); it++) {
GameInfo *i = static_cast<GameInfo *>((*it).second);
if (strcasecmp(game_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
version = (*it).second->GetVersion();
info = i;
}
}
}

if (info == NULL && !force_exact_match && versionParam != -1) {
if (versionParam != -2) {
/* If we didn't find a match Game script, maybe the user included a version */
char *e = strrchr(game_name, '.');
if (e == NULL) return NULL;
*e = '\0';
e++;
versionParam = atoi(e);
/* Try to find a direct 'name.version' match */
char game_name_tmp[1024];
seprintf(game_name_tmp, lastof(game_name_tmp), "%s.%d", game_name, versionParam);
strtolower(game_name_tmp);
if (this->info_list.find(game_name_tmp) != this->info_list.end()) return static_cast<GameInfo *>(this->info_list[game_name_tmp]);
}
/* See if there is a compatible Game script which goes by that name, with the highest
* version which allows loading the requested version */
ScriptInfoList::iterator it = this->info_list.begin();
if (versionParam == -2) versionParam = -1;
for (; it != this->info_list.end(); it++) {
GameInfo *i = static_cast<GameInfo *>((*it).second);
if (strcasecmp(game_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
version = (*it).second->GetVersion();
info = i;
}
}
}

Expand Down
20 changes: 14 additions & 6 deletions src/lang/english.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,7 @@ STR_INTRO_HIGHSCORE :{BLACK}Highscor
STR_INTRO_CONFIG_SETTINGS_TREE :{BLACK}Settings
STR_INTRO_NEWGRF_SETTINGS :{BLACK}NewGRF Settings
STR_INTRO_ONLINE_CONTENT :{BLACK}Check Online Content
STR_INTRO_SCRIPT_SETTINGS :{BLACK}AI/Game Script Settings
STR_INTRO_SCRIPT_SETTINGS :{BLACK}AI/Game Script Configuration
STR_INTRO_QUIT :{BLACK}Exit

STR_INTRO_TOOLTIP_NEW_GAME :{BLACK}Start a new game. Ctrl+Click skips map configuration
Expand All @@ -1771,7 +1771,7 @@ STR_INTRO_TOOLTIP_HIGHSCORE :{BLACK}Display
STR_INTRO_TOOLTIP_CONFIG_SETTINGS_TREE :{BLACK}Display settings
STR_INTRO_TOOLTIP_NEWGRF_SETTINGS :{BLACK}Display NewGRF settings
STR_INTRO_TOOLTIP_ONLINE_CONTENT :{BLACK}Check for new and updated content to download
STR_INTRO_TOOLTIP_SCRIPT_SETTINGS :{BLACK}Display AI/Game script settings
STR_INTRO_TOOLTIP_SCRIPT_SETTINGS :{BLACK}Display AI/Game script configuration
STR_INTRO_TOOLTIP_QUIT :{BLACK}Exit 'OpenTTD'

STR_INTRO_BASESET :{BLACK}The currently selected base graphics set is missing {NUM} sprite{P "" s}. Please check for updates for the baseset.
Expand Down Expand Up @@ -4051,10 +4051,11 @@ STR_ERROR_AI_DEBUG_SERVER_ONLY :{YELLOW}AI/Game

# AI configuration window
STR_AI_CONFIG_CAPTION :{WHITE}AI/Game Script Configuration
STR_AI_CONFIG_GAMELIST_TOOLTIP :{BLACK}The Game Script that will be loaded in the next game
STR_AI_CONFIG_AILIST_TOOLTIP :{BLACK}The AIs that will be loaded in the next game
STR_AI_CONFIG_CAPTION_INGAME :{WHITE}AI/Game Script Settings
STR_AI_CONFIG_GAMELIST_TOOLTIP :{BLACK}The Game Script that is loaded or will be loaded in the next game
STR_AI_CONFIG_AILIST_TOOLTIP :{BLACK}The AIs that are currently loaded or will be loaded next time they start on these Company IDs while in a game
STR_AI_CONFIG_HUMAN_PLAYER :Human player
STR_AI_CONFIG_RANDOM_AI :Random AI
STR_AI_CONFIG_RANDOM_AI :(random)
STR_AI_CONFIG_NONE :(none)

STR_AI_CONFIG_MOVE_UP :{BLACK}Move Up
Expand All @@ -4071,7 +4072,14 @@ STR_AI_CONFIG_CHANGE_AI :AI
STR_AI_CONFIG_CHANGE_GAMESCRIPT :Game Script
STR_AI_CONFIG_CHANGE_TOOLTIP :{BLACK}Load another script
STR_AI_CONFIG_CONFIGURE :{BLACK}Configure
STR_AI_CONFIG_CONFIGURE_TOOLTIP :{BLACK}Configure the parameters of the Script
STR_AI_CONFIG_CONFIGURE_TOOLTIP :{BLACK}Configure the parameters of the script
STR_AI_CONFIG_START :{BLACK}Start AI
STR_AI_CONFIG_START_TOOLTIP :{BLACK}Load the selected script, and start an AI Company in the selected Company ID
STR_AI_CONFIG_STOP :{BLACK}Stop AI
STR_AI_CONFIG_STOP_TOOLTIP :{BLACK}Kill the selected AI, and remove its Company

STR_ERROR_AI_CAN_T_START :{WHITE}Can't start AI...
STR_ERROR_AI_ALLOW_IN_MULTIPLAYER :Make sure to allow AI computer players to participate in multiplayer games first.

# Available AIs window
STR_AI_LIST_CAPTION :{WHITE}Available {STRING}
Expand Down
2 changes: 1 addition & 1 deletion src/saveload/ai_sl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static void Load_AIPL()
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);
config->Change(_ai_saveload_name, -2, 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);
Expand Down
6 changes: 3 additions & 3 deletions src/saveload/game_sl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ static void Load_GSDT()
if (!config->HasScript()) {
/* No version of the GameScript available that can load the data. Try to load the
* latest version of the GameScript instead. */
config->Change(_game_saveload_name, -1, false, _game_saveload_is_random);
config->Change(_game_saveload_name, -2, false, _game_saveload_is_random);
if (!config->HasScript()) {
if (strcmp(_game_saveload_name, "%_dummy") != 0) {
DEBUG(script, 0, "The savegame has an GameScript by the name '%s', version %d which is no longer available.", _game_saveload_name, _game_saveload_version);
DEBUG(script, 0, "The savegame has a GameScript by the name '%s', version %d which is no longer available.", _game_saveload_name, _game_saveload_version);
DEBUG(script, 0, "This game will continue to run without GameScript.");
} else {
DEBUG(script, 0, "The savegame had no GameScript available at the time of saving.");
DEBUG(script, 0, "This game will continue to run without GameScript.");
}
} else {
DEBUG(script, 0, "The savegame has an GameScript by the name '%s', version %d which is no longer available.", _game_saveload_name, _game_saveload_version);
DEBUG(script, 0, "The savegame has a GameScript by the name '%s', version %d which is no longer available.", _game_saveload_name, _game_saveload_version);
DEBUG(script, 0, "The latest version of that GameScript has been loaded instead, but it'll not get the savegame data as it's incompatible.");
}
/* Make sure the GameScript doesn't get the saveload data, as he was not the
Expand Down
3 changes: 3 additions & 0 deletions src/saveload/saveload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ uint16 _sl_version; ///< the major savegame version identifier
byte _sl_minor_version; ///< the minor savegame version, DO NOT USE!
char _savegame_format[8]; ///< how to compress savegames
bool _do_autosave; ///< are we doing an autosave at the moment?
bool _save_empty_script; ///< are we sending a map to a client over the nework?

/** What are we currently doing? */
enum SaveLoadAction {
Expand Down Expand Up @@ -2619,6 +2620,7 @@ static void SaveFileDone()

InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SAVELOAD_FINISH);
_sl.saveinprogress = false;
_save_empty_script = false;
}

/** Set the error message from outside of the actual loading/saving of the game (AfterLoadGame and friends) */
Expand Down Expand Up @@ -2750,6 +2752,7 @@ static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
SaveOrLoadResult SaveWithFilter(SaveFilter *writer, bool threaded)
{
try {
_save_empty_script = true;
_sl.action = SLA_SAVE;
return DoSave(writer, threaded);
} catch (...) {
Expand Down
1 change: 1 addition & 0 deletions src/saveload/saveload.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,5 +564,6 @@ bool SaveloadCrashWithMissingNewGRFs();

extern char _savegame_format[8];
extern bool _do_autosave;
extern bool _save_empty_script;

#endif /* SAVELOAD_H */
5 changes: 3 additions & 2 deletions src/script/api/game/game_window.hpp.sq
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,23 @@ void SQGSWindow_Register(Squirrel *engine)
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIS_SCROLLBAR, "WID_AIS_SCROLLBAR");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIS_ACCEPT, "WID_AIS_ACCEPT");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIS_RESET, "WID_AIS_RESET");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_CAPTION, "WID_AIC_CAPTION");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_BACKGROUND, "WID_AIC_BACKGROUND");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_DECREASE, "WID_AIC_DECREASE");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_INCREASE, "WID_AIC_INCREASE");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_NUMBER, "WID_AIC_NUMBER");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_GAMELIST, "WID_AIC_GAMELIST");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_LIST, "WID_AIC_LIST");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_SCROLLBAR, "WID_AIC_SCROLLBAR");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_MOVE_UP, "WID_AIC_MOVE_UP");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_MOVE_DOWN, "WID_AIC_MOVE_DOWN");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_CHANGE, "WID_AIC_CHANGE");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_CONFIGURE, "WID_AIC_CONFIGURE");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_CLOSE, "WID_AIC_CLOSE");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_START_STOP_TOGGLE, "WID_AIC_START_STOP_TOGGLE");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_TEXTFILE, "WID_AIC_TEXTFILE");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AIC_CONTENT_DOWNLOAD, "WID_AIC_CONTENT_DOWNLOAD");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AID_VIEW, "WID_AID_VIEW");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AID_NAME_TEXT, "WID_AID_NAME_TEXT");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AID_SETTINGS_RELOAD_WIDGETS, "WID_AID_SETTINGS_RELOAD_WIDGETS");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AID_SETTINGS, "WID_AID_SETTINGS");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AID_SCRIPT_GAME, "WID_AID_SCRIPT_GAME");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_AID_RELOAD_TOGGLE, "WID_AID_RELOAD_TOGGLE");
Expand Down
Loading

0 comments on commit a02bcaa

Please sign in to comment.