OpenTTD: src/saveload/ai_sl.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

ai_sl.cpp
Go to the documentation of this file.
1 /* $Id: ai_sl.cpp 26493 2014-04-24 04:41:54Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #include "../stdafx.h"
13 #include "../company_base.h"
14 #include "../debug.h"
15 #include "saveload.h"
16 #include "../string_func.h"
17
18 #include "../ai/ai.hpp"
19 #include "../ai/ai_config.hpp"
20 #include "../network/network.h"
21 #include "../ai/ai_instance.hpp"
22
23 #include "../safeguards.h"
24
25 static char _ai_saveload_name[64];
26 static int _ai_saveload_version;
27 static char _ai_saveload_settings[1024];
28 static bool _ai_saveload_is_random;
29
30 static const SaveLoad _ai_company[] = {
31 SLEG_STR(_ai_saveload_name, SLE_STRB),
32 SLEG_STR(_ai_saveload_settings, SLE_STRB),
33 SLEG_CONDVAR(_ai_saveload_version, SLE_UINT32, 108, SL_MAX_VERSION),
34 SLEG_CONDVAR(_ai_saveload_is_random, SLE_BOOL, 136, SL_MAX_VERSION),
35 SLE_END()
36 };
37
38 static void SaveReal_AIPL(int *index_ptr)
39 {
40 CompanyID index = (CompanyID)*index_ptr;
41 AIConfig *config = AIConfig::GetConfig(index);
42
43 if (config->HasScript()) {
44 strecpy(_ai_saveload_name, config->GetName(), lastof(_ai_saveload_name));
45 _ai_saveload_version = config->GetVersion();
46 } else {
47 /* No AI is configured for this so store an empty string as name. */
48 _ai_saveload_name[0] = '\0';
49 _ai_saveload_version = -1;
50 }
51
52 _ai_saveload_is_random = config->IsRandom();
53 _ai_saveload_settings[0] = '\0';
54 config->SettingsToString(_ai_saveload_settings, lastof(_ai_saveload_settings));
55
56 SlObject(NULL, _ai_company);
57 /* If the AI was active, store his data too */
58 if (Company::IsValidAiID(index)) AI::Save(index);
59 }
60
61 static void Load_AIPL()
62 {
63 /* Free all current data */
64 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
65 AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(NULL);
66 }
67
68 CompanyID index;
69 while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) {
70 if (index >= MAX_COMPANIES) SlErrorCorrupt("Too many AI configs");
71
72 _ai_saveload_is_random = 0;
73 _ai_saveload_version = -1;
74 SlObject(NULL, _ai_company);
75
76 if (_networking && !_network_server) {
77 if (Company::IsValidAiID(index)) AIInstance::LoadEmpty();
78 continue;
79 }
80
81 AIConfig *config = AIConfig::GetConfig(index, AIConfig::SSS_FORCE_GAME);
82 if (StrEmpty(_ai_saveload_name)) {
83 /* A random AI. */
84 config->Change(NULL, -1, false, true);
85 } else {
86 config->Change(_ai_saveload_name, _ai_saveload_version, false, _ai_saveload_is_random);
87 if (!config->HasScript()) {
88 /* No version of the AI available that can load the data. Try to load the
89 * latest version of the AI instead. */
90 config->Change(_ai_saveload_name, -1, false, _ai_saveload_is_random);
91 if (!config->HasScript()) {
92 if (strcmp(_ai_saveload_name, "%_dummy") != 0) {
93 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);
94 DEBUG(script, 0, "A random other AI will be loaded in its place.");
95 } else {
96 DEBUG(script, 0, "The savegame had no AIs available at the time of saving.");
97 DEBUG(script, 0, "A random available AI will be loaded now.");
98 }
99 } else {
100 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);
101 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.");
102 }
103 /* Make sure the AI doesn't get the saveload data, as he was not the
104 * writer of the saveload data in the first place */
105 _ai_saveload_version = -1;
106 }
107 }
108
109 config->StringToSettings(_ai_saveload_settings);
110
111 /* Start the AI directly if it was active in the savegame */
112 if (Company::IsValidAiID(index)) {
113 AI::StartNew(index, false);
114 AI::Load(index, _ai_saveload_version);
115 }
116 }
117 }
118
119 static void Save_AIPL()
120 {
121 for (int i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
122 SlSetArrayIndex(i);
123 SlAutolength((AutolengthProc *)SaveReal_AIPL, &i);
124 }
125 }
126
127 extern const ChunkHandler _ai_chunk_handlers[] = {
128 { 'AIPL', Save_AIPL, Load_AIPL, NULL, NULL, CH_ARRAY | CH_LAST},
129 };