00001
00002
00003
00004
00005
00006
00007
00008
00009
00026 #include "stdafx.h"
00027 #include "currency.h"
00028 #include "screenshot.h"
00029 #include "network/network.h"
00030 #include "network/network_func.h"
00031 #include "settings_internal.h"
00032 #include "command_func.h"
00033 #include "console_func.h"
00034 #include "pathfinder/pathfinder_type.h"
00035 #include "genworld.h"
00036 #include "train.h"
00037 #include "news_func.h"
00038 #include "window_func.h"
00039 #include "sound_func.h"
00040 #include "company_func.h"
00041 #include "rev.h"
00042 #ifdef WITH_FREETYPE
00043 #include "fontcache.h"
00044 #endif
00045 #include "textbuf_gui.h"
00046 #include "rail_gui.h"
00047 #include "elrail_func.h"
00048 #include "error.h"
00049 #include "town.h"
00050 #include "video/video_driver.hpp"
00051 #include "sound/sound_driver.hpp"
00052 #include "music/music_driver.hpp"
00053 #include "blitter/factory.hpp"
00054 #include "base_media_base.h"
00055 #include "gamelog.h"
00056 #include "settings_func.h"
00057 #include "ini_type.h"
00058 #include "ai/ai_config.hpp"
00059 #include "ai/ai.hpp"
00060 #include "game/game_config.hpp"
00061 #include "game/game.hpp"
00062 #include "ship.h"
00063 #include "smallmap_gui.h"
00064 #include "roadveh.h"
00065 #include "fios.h"
00066 #include "strings_func.h"
00067
00068 #include "void_map.h"
00069 #include "station_base.h"
00070
00071 #include "table/strings.h"
00072 #include "table/settings.h"
00073
00074 ClientSettings _settings_client;
00075 GameSettings _settings_game;
00076 GameSettings _settings_newgame;
00077 VehicleDefaultSettings _old_vds;
00078 char *_config_file;
00079
00080 typedef std::list<ErrorMessageData> ErrorList;
00081 static ErrorList _settings_error_list;
00082
00083
00084 typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object);
00085 typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList *list);
00086
00087 static bool IsSignedVarMemType(VarType vt);
00088
00092 static const char * const _list_group_names[] = {
00093 "bans",
00094 "newgrf",
00095 "servers",
00096 "server_bind_addresses",
00097 NULL
00098 };
00099
00107 static size_t LookupOneOfMany(const char *many, const char *one, size_t onelen = 0)
00108 {
00109 const char *s;
00110 size_t idx;
00111
00112 if (onelen == 0) onelen = strlen(one);
00113
00114
00115 if (*one >= '0' && *one <= '9') return strtoul(one, NULL, 0);
00116
00117 idx = 0;
00118 for (;;) {
00119
00120 s = many;
00121 while (*s != '|' && *s != 0) s++;
00122 if ((size_t)(s - many) == onelen && !memcmp(one, many, onelen)) return idx;
00123 if (*s == 0) return (size_t)-1;
00124 many = s + 1;
00125 idx++;
00126 }
00127 }
00128
00136 static size_t LookupManyOfMany(const char *many, const char *str)
00137 {
00138 const char *s;
00139 size_t r;
00140 size_t res = 0;
00141
00142 for (;;) {
00143
00144 while (*str == ' ' || *str == '\t' || *str == '|') str++;
00145 if (*str == 0) break;
00146
00147 s = str;
00148 while (*s != 0 && *s != ' ' && *s != '\t' && *s != '|') s++;
00149
00150 r = LookupOneOfMany(many, str, s - str);
00151 if (r == (size_t)-1) return r;
00152
00153 SetBit(res, (uint8)r);
00154 if (*s == 0) break;
00155 str = s + 1;
00156 }
00157 return res;
00158 }
00159
00168 static int ParseIntList(const char *p, int *items, int maxitems)
00169 {
00170 int n = 0;
00171 bool comma = false;
00172
00173 while (*p != '\0') {
00174 switch (*p) {
00175 case ',':
00176
00177 if (!comma) return -1;
00178 comma = false;
00179
00180 case ' ':
00181 p++;
00182 break;
00183
00184 default: {
00185 if (n == maxitems) return -1;
00186 char *end;
00187 long v = strtol(p, &end, 0);
00188 if (p == end) return -1;
00189 if (sizeof(int) < sizeof(long)) v = ClampToI32(v);
00190 items[n++] = v;
00191 p = end;
00192 comma = true;
00193 break;
00194 }
00195 }
00196 }
00197
00198
00199
00200 if (n != 0 && !comma) return -1;
00201
00202 return n;
00203 }
00204
00213 static bool LoadIntList(const char *str, void *array, int nelems, VarType type)
00214 {
00215 int items[64];
00216 int i, nitems;
00217
00218 if (str == NULL) {
00219 memset(items, 0, sizeof(items));
00220 nitems = nelems;
00221 } else {
00222 nitems = ParseIntList(str, items, lengthof(items));
00223 if (nitems != nelems) return false;
00224 }
00225
00226 switch (type) {
00227 case SLE_VAR_BL:
00228 case SLE_VAR_I8:
00229 case SLE_VAR_U8:
00230 for (i = 0; i != nitems; i++) ((byte*)array)[i] = items[i];
00231 break;
00232
00233 case SLE_VAR_I16:
00234 case SLE_VAR_U16:
00235 for (i = 0; i != nitems; i++) ((uint16*)array)[i] = items[i];
00236 break;
00237
00238 case SLE_VAR_I32:
00239 case SLE_VAR_U32:
00240 for (i = 0; i != nitems; i++) ((uint32*)array)[i] = items[i];
00241 break;
00242
00243 default: NOT_REACHED();
00244 }
00245
00246 return true;
00247 }
00248
00258 static void MakeIntList(char *buf, const char *last, const void *array, int nelems, VarType type)
00259 {
00260 int i, v = 0;
00261 const byte *p = (const byte *)array;
00262
00263 for (i = 0; i != nelems; i++) {
00264 switch (type) {
00265 case SLE_VAR_BL:
00266 case SLE_VAR_I8: v = *(const int8 *)p; p += 1; break;
00267 case SLE_VAR_U8: v = *(const uint8 *)p; p += 1; break;
00268 case SLE_VAR_I16: v = *(const int16 *)p; p += 2; break;
00269 case SLE_VAR_U16: v = *(const uint16 *)p; p += 2; break;
00270 case SLE_VAR_I32: v = *(const int32 *)p; p += 4; break;
00271 case SLE_VAR_U32: v = *(const uint32 *)p; p += 4; break;
00272 default: NOT_REACHED();
00273 }
00274 buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
00275 }
00276 }
00277
00285 static void MakeOneOfMany(char *buf, const char *last, const char *many, int id)
00286 {
00287 int orig_id = id;
00288
00289
00290 while (--id >= 0) {
00291 for (; *many != '|'; many++) {
00292 if (*many == '\0') {
00293 seprintf(buf, last, "%d", orig_id);
00294 return;
00295 }
00296 }
00297 many++;
00298 }
00299
00300
00301 while (*many != '\0' && *many != '|' && buf < last) *buf++ = *many++;
00302 *buf = '\0';
00303 }
00304
00313 static void MakeManyOfMany(char *buf, const char *last, const char *many, uint32 x)
00314 {
00315 const char *start;
00316 int i = 0;
00317 bool init = true;
00318
00319 for (; x != 0; x >>= 1, i++) {
00320 start = many;
00321 while (*many != 0 && *many != '|') many++;
00322
00323 if (HasBit(x, 0)) {
00324 if (!init) buf += seprintf(buf, last, "|");
00325 init = false;
00326 if (start == many) {
00327 buf += seprintf(buf, last, "%d", i);
00328 } else {
00329 memcpy(buf, start, many - start);
00330 buf += many - start;
00331 }
00332 }
00333
00334 if (*many == '|') many++;
00335 }
00336
00337 *buf = '\0';
00338 }
00339
00346 static const void *StringToVal(const SettingDescBase *desc, const char *orig_str)
00347 {
00348 const char *str = orig_str == NULL ? "" : orig_str;
00349
00350 switch (desc->cmd) {
00351 case SDT_NUMX: {
00352 char *end;
00353 size_t val = strtoul(str, &end, 0);
00354 if (end == str) {
00355 ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
00356 msg.SetDParamStr(0, str);
00357 msg.SetDParamStr(1, desc->name);
00358 _settings_error_list.push_back(msg);
00359 return desc->def;
00360 }
00361 if (*end != '\0') {
00362 ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_TRAILING_CHARACTERS);
00363 msg.SetDParamStr(0, desc->name);
00364 _settings_error_list.push_back(msg);
00365 }
00366 return (void*)val;
00367 }
00368
00369 case SDT_ONEOFMANY: {
00370 size_t r = LookupOneOfMany(desc->many, str);
00371
00372
00373 if (r == (size_t)-1 && desc->proc_cnvt != NULL) r = desc->proc_cnvt(str);
00374 if (r != (size_t)-1) return (void*)r;
00375
00376 ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
00377 msg.SetDParamStr(0, str);
00378 msg.SetDParamStr(1, desc->name);
00379 _settings_error_list.push_back(msg);
00380 return desc->def;
00381 }
00382
00383 case SDT_MANYOFMANY: {
00384 size_t r = LookupManyOfMany(desc->many, str);
00385 if (r != (size_t)-1) return (void*)r;
00386 ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
00387 msg.SetDParamStr(0, str);
00388 msg.SetDParamStr(1, desc->name);
00389 _settings_error_list.push_back(msg);
00390 return desc->def;
00391 }
00392
00393 case SDT_BOOLX: {
00394 if (strcmp(str, "true") == 0 || strcmp(str, "on") == 0 || strcmp(str, "1") == 0) return (void*)true;
00395 if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return (void*)false;
00396
00397 ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
00398 msg.SetDParamStr(0, str);
00399 msg.SetDParamStr(1, desc->name);
00400 _settings_error_list.push_back(msg);
00401 return desc->def;
00402 }
00403
00404 case SDT_STRING: return orig_str;
00405 case SDT_INTLIST: return str;
00406 default: break;
00407 }
00408
00409 return NULL;
00410 }
00411
00421 static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val)
00422 {
00423 const SettingDescBase *sdb = &sd->desc;
00424
00425 if (sdb->cmd != SDT_BOOLX &&
00426 sdb->cmd != SDT_NUMX &&
00427 sdb->cmd != SDT_ONEOFMANY &&
00428 sdb->cmd != SDT_MANYOFMANY) {
00429 return;
00430 }
00431
00432
00433 if (sdb->cmd != SDT_MANYOFMANY) {
00434
00435
00436
00437
00438
00439 switch (GetVarMemType(sd->save.conv)) {
00440 case SLE_VAR_NULL: return;
00441 case SLE_VAR_BL:
00442 case SLE_VAR_I8:
00443 case SLE_VAR_U8:
00444 case SLE_VAR_I16:
00445 case SLE_VAR_U16:
00446 case SLE_VAR_I32: {
00447
00448 if (!(sdb->flags & SGF_0ISDISABLED) || val != 0) val = Clamp(val, sdb->min, sdb->max);
00449 break;
00450 }
00451 case SLE_VAR_U32: {
00452
00453 uint min = ((sdb->flags & SGF_0ISDISABLED) && (uint)val <= (uint)sdb->min) ? 0 : sdb->min;
00454 WriteValue(ptr, SLE_VAR_U32, (int64)ClampU(val, min, sdb->max));
00455 return;
00456 }
00457 case SLE_VAR_I64:
00458 case SLE_VAR_U64:
00459 default: NOT_REACHED();
00460 }
00461 }
00462
00463 WriteValue(ptr, sd->save.conv, (int64)val);
00464 }
00465
00474 static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00475 {
00476 IniGroup *group;
00477 IniGroup *group_def = ini->GetGroup(grpname);
00478 IniItem *item;
00479 const void *p;
00480 void *ptr;
00481 const char *s;
00482
00483 for (; sd->save.cmd != SL_END; sd++) {
00484 const SettingDescBase *sdb = &sd->desc;
00485 const SaveLoad *sld = &sd->save;
00486
00487 if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00488
00489
00490 s = strchr(sdb->name, '.');
00491 if (s != NULL) {
00492 group = ini->GetGroup(sdb->name, s - sdb->name);
00493 s++;
00494 } else {
00495 s = sdb->name;
00496 group = group_def;
00497 }
00498
00499 item = group->GetItem(s, false);
00500 if (item == NULL && group != group_def) {
00501
00502
00503 item = group_def->GetItem(s, false);
00504 }
00505 if (item == NULL) {
00506
00507
00508 const char *sc = strchr(s, '.');
00509 if (sc != NULL) item = ini->GetGroup(s, sc - s)->GetItem(sc + 1, false);
00510 }
00511
00512 p = (item == NULL) ? sdb->def : StringToVal(sdb, item->value);
00513 ptr = GetVariableAddress(object, sld);
00514
00515 switch (sdb->cmd) {
00516 case SDT_BOOLX:
00517 case SDT_NUMX:
00518 case SDT_ONEOFMANY:
00519 case SDT_MANYOFMANY:
00520 Write_ValidateSetting(ptr, sd, (int32)(size_t)p);
00521 break;
00522
00523 case SDT_STRING:
00524 switch (GetVarMemType(sld->conv)) {
00525 case SLE_VAR_STRB:
00526 case SLE_VAR_STRBQ:
00527 if (p != NULL) ttd_strlcpy((char*)ptr, (const char*)p, sld->length);
00528 break;
00529
00530 case SLE_VAR_STR:
00531 case SLE_VAR_STRQ:
00532 free(*(char**)ptr);
00533 *(char**)ptr = p == NULL ? NULL : strdup((const char*)p);
00534 break;
00535
00536 case SLE_VAR_CHAR: if (p != NULL) *(char *)ptr = *(const char *)p; break;
00537
00538 default: NOT_REACHED();
00539 }
00540 break;
00541
00542 case SDT_INTLIST: {
00543 if (!LoadIntList((const char*)p, ptr, sld->length, GetVarMemType(sld->conv))) {
00544 ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY);
00545 msg.SetDParamStr(0, sdb->name);
00546 _settings_error_list.push_back(msg);
00547
00548
00549 LoadIntList((const char*)sdb->def, ptr, sld->length, GetVarMemType(sld->conv));
00550 } else if (sd->desc.proc_cnvt != NULL) {
00551 sd->desc.proc_cnvt((const char*)p);
00552 }
00553 break;
00554 }
00555 default: NOT_REACHED();
00556 }
00557 }
00558 }
00559
00572 static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00573 {
00574 IniGroup *group_def = NULL, *group;
00575 IniItem *item;
00576 char buf[512];
00577 const char *s;
00578 void *ptr;
00579
00580 for (; sd->save.cmd != SL_END; sd++) {
00581 const SettingDescBase *sdb = &sd->desc;
00582 const SaveLoad *sld = &sd->save;
00583
00584
00585
00586 if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00587 if (sld->conv & SLF_NOT_IN_CONFIG) continue;
00588
00589
00590 s = strchr(sdb->name, '.');
00591 if (s != NULL) {
00592 group = ini->GetGroup(sdb->name, s - sdb->name);
00593 s++;
00594 } else {
00595 if (group_def == NULL) group_def = ini->GetGroup(grpname);
00596 s = sdb->name;
00597 group = group_def;
00598 }
00599
00600 item = group->GetItem(s, true);
00601 ptr = GetVariableAddress(object, sld);
00602
00603 if (item->value != NULL) {
00604
00605 const void *p = StringToVal(sdb, item->value);
00606
00607
00608
00609 switch (sdb->cmd) {
00610 case SDT_BOOLX:
00611 case SDT_NUMX:
00612 case SDT_ONEOFMANY:
00613 case SDT_MANYOFMANY:
00614 switch (GetVarMemType(sld->conv)) {
00615 case SLE_VAR_BL:
00616 if (*(bool*)ptr == (p != NULL)) continue;
00617 break;
00618
00619 case SLE_VAR_I8:
00620 case SLE_VAR_U8:
00621 if (*(byte*)ptr == (byte)(size_t)p) continue;
00622 break;
00623
00624 case SLE_VAR_I16:
00625 case SLE_VAR_U16:
00626 if (*(uint16*)ptr == (uint16)(size_t)p) continue;
00627 break;
00628
00629 case SLE_VAR_I32:
00630 case SLE_VAR_U32:
00631 if (*(uint32*)ptr == (uint32)(size_t)p) continue;
00632 break;
00633
00634 default: NOT_REACHED();
00635 }
00636 break;
00637
00638 default: break;
00639 }
00640 }
00641
00642
00643 switch (sdb->cmd) {
00644 case SDT_BOOLX:
00645 case SDT_NUMX:
00646 case SDT_ONEOFMANY:
00647 case SDT_MANYOFMANY: {
00648 uint32 i = (uint32)ReadValue(ptr, sld->conv);
00649
00650 switch (sdb->cmd) {
00651 case SDT_BOOLX: strecpy(buf, (i != 0) ? "true" : "false", lastof(buf)); break;
00652 case SDT_NUMX: seprintf(buf, lastof(buf), IsSignedVarMemType(sld->conv) ? "%d" : "%u", i); break;
00653 case SDT_ONEOFMANY: MakeOneOfMany(buf, lastof(buf), sdb->many, i); break;
00654 case SDT_MANYOFMANY: MakeManyOfMany(buf, lastof(buf), sdb->many, i); break;
00655 default: NOT_REACHED();
00656 }
00657 break;
00658 }
00659
00660 case SDT_STRING:
00661 switch (GetVarMemType(sld->conv)) {
00662 case SLE_VAR_STRB: strecpy(buf, (char*)ptr, lastof(buf)); break;
00663 case SLE_VAR_STRBQ:seprintf(buf, lastof(buf), "\"%s\"", (char*)ptr); break;
00664 case SLE_VAR_STR: strecpy(buf, *(char**)ptr, lastof(buf)); break;
00665
00666 case SLE_VAR_STRQ:
00667 if (*(char**)ptr == NULL) {
00668 buf[0] = '\0';
00669 } else {
00670 seprintf(buf, lastof(buf), "\"%s\"", *(char**)ptr);
00671 }
00672 break;
00673
00674 case SLE_VAR_CHAR: buf[0] = *(char*)ptr; buf[1] = '\0'; break;
00675 default: NOT_REACHED();
00676 }
00677 break;
00678
00679 case SDT_INTLIST:
00680 MakeIntList(buf, lastof(buf), ptr, sld->length, GetVarMemType(sld->conv));
00681 break;
00682
00683 default: NOT_REACHED();
00684 }
00685
00686
00687 free(item->value);
00688 item->value = strdup(buf);
00689 }
00690 }
00691
00701 static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList *list)
00702 {
00703 IniGroup *group = ini->GetGroup(grpname);
00704
00705 if (group == NULL || list == NULL) return;
00706
00707 list->Clear();
00708
00709 for (const IniItem *item = group->item; item != NULL; item = item->next) {
00710 if (item->name != NULL) *list->Append() = strdup(item->name);
00711 }
00712 }
00713
00723 static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList *list)
00724 {
00725 IniGroup *group = ini->GetGroup(grpname);
00726
00727 if (group == NULL || list == NULL) return;
00728 group->Clear();
00729
00730 for (char **iter = list->Begin(); iter != list->End(); iter++) {
00731 group->GetItem(*iter, true)->SetValue("");
00732 }
00733 }
00734
00740 bool SettingDesc::IsEditable(bool do_command) const
00741 {
00742 if (!do_command && !(this->save.conv & SLF_NO_NETWORK_SYNC) && _networking && !_network_server && !(this->desc.flags & SGF_PER_COMPANY)) return false;
00743 if ((this->desc.flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return false;
00744 if ((this->desc.flags & SGF_NO_NETWORK) && _networking) return false;
00745 if ((this->desc.flags & SGF_NEWGAME_ONLY) &&
00746 (_game_mode == GM_NORMAL ||
00747 (_game_mode == GM_EDITOR && !(this->desc.flags & SGF_SCENEDIT_TOO)))) return false;
00748 return true;
00749 }
00750
00755 SettingType SettingDesc::GetType() const
00756 {
00757 if (this->desc.flags & SGF_PER_COMPANY) return ST_COMPANY;
00758 return (this->save.conv & SLF_NOT_IN_SAVE) ? ST_CLIENT : ST_GAME;
00759 }
00760
00761
00762
00764 static bool v_PositionMainToolbar(int32 p1)
00765 {
00766 if (_game_mode != GM_MENU) PositionMainToolbar(NULL);
00767 return true;
00768 }
00769
00771 static bool v_PositionStatusbar(int32 p1)
00772 {
00773 if (_game_mode != GM_MENU) {
00774 PositionStatusbar(NULL);
00775 PositionNewsMessage(NULL);
00776 PositionNetworkChatWindow(NULL);
00777 }
00778 return true;
00779 }
00780
00781 static bool PopulationInLabelActive(int32 p1)
00782 {
00783 UpdateAllTownVirtCoords();
00784 return true;
00785 }
00786
00787 static bool RedrawScreen(int32 p1)
00788 {
00789 MarkWholeScreenDirty();
00790 return true;
00791 }
00792
00798 static bool RedrawSmallmap(int32 p1)
00799 {
00800 BuildLandLegend();
00801 BuildOwnerLegend();
00802 SetWindowClassesDirty(WC_SMALLMAP);
00803 return true;
00804 }
00805
00806 static bool InvalidateDetailsWindow(int32 p1)
00807 {
00808 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00809 return true;
00810 }
00811
00812 static bool StationSpreadChanged(int32 p1)
00813 {
00814 InvalidateWindowData(WC_SELECT_STATION, 0);
00815 InvalidateWindowData(WC_BUILD_STATION, 0);
00816 return true;
00817 }
00818
00819 static bool InvalidateBuildIndustryWindow(int32 p1)
00820 {
00821 InvalidateWindowData(WC_BUILD_INDUSTRY, 0);
00822 return true;
00823 }
00824
00825 static bool CloseSignalGUI(int32 p1)
00826 {
00827 if (p1 == 0) {
00828 DeleteWindowByClass(WC_BUILD_SIGNAL);
00829 }
00830 return true;
00831 }
00832
00833 static bool InvalidateTownViewWindow(int32 p1)
00834 {
00835 InvalidateWindowClassesData(WC_TOWN_VIEW, p1);
00836 return true;
00837 }
00838
00839 static bool DeleteSelectStationWindow(int32 p1)
00840 {
00841 DeleteWindowById(WC_SELECT_STATION, 0);
00842 return true;
00843 }
00844
00845 static bool UpdateConsists(int32 p1)
00846 {
00847 Train *t;
00848 FOR_ALL_TRAINS(t) {
00849
00850 if (t->IsFrontEngine() || t->IsFreeWagon()) t->ConsistChanged(true);
00851 }
00852 InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00853 return true;
00854 }
00855
00856
00857 static bool CheckInterval(int32 p1)
00858 {
00859 bool update_vehicles;
00860 VehicleDefaultSettings *vds;
00861 if (_game_mode == GM_MENU || !Company::IsValidID(_current_company)) {
00862 vds = &_settings_client.company.vehicle;
00863 update_vehicles = false;
00864 } else {
00865 vds = &Company::Get(_current_company)->settings.vehicle;
00866 update_vehicles = true;
00867 }
00868
00869 if (p1 != 0) {
00870 vds->servint_trains = 50;
00871 vds->servint_roadveh = 50;
00872 vds->servint_aircraft = 50;
00873 vds->servint_ships = 50;
00874 } else {
00875 vds->servint_trains = 150;
00876 vds->servint_roadveh = 150;
00877 vds->servint_aircraft = 100;
00878 vds->servint_ships = 360;
00879 }
00880
00881 if (update_vehicles) {
00882 const Company *c = Company::Get(_current_company);
00883 Vehicle *v;
00884 FOR_ALL_VEHICLES(v) {
00885 if (v->owner == _current_company && v->IsPrimaryVehicle() && !v->ServiceIntervalIsCustom()) {
00886 v->SetServiceInterval(CompanyServiceInterval(c, v->type));
00887 v->SetServiceIntervalIsPercent(p1 != 0);
00888 }
00889 }
00890 }
00891
00892 InvalidateDetailsWindow(0);
00893
00894 return true;
00895 }
00896
00897 static bool UpdateInterval(VehicleType type, int32 p1)
00898 {
00899 bool update_vehicles;
00900 VehicleDefaultSettings *vds;
00901 if (_game_mode == GM_MENU || !Company::IsValidID(_current_company)) {
00902 vds = &_settings_client.company.vehicle;
00903 update_vehicles = false;
00904 } else {
00905 vds = &Company::Get(_current_company)->settings.vehicle;
00906 update_vehicles = true;
00907 }
00908
00909
00910 uint16 interval = GetServiceIntervalClamped(p1, vds->servint_ispercent);
00911 if (interval != p1) return false;
00912
00913 if (update_vehicles) {
00914 Vehicle *v;
00915 FOR_ALL_VEHICLES(v) {
00916 if (v->owner == _current_company && v->type == type && v->IsPrimaryVehicle() && !v->ServiceIntervalIsCustom()) {
00917 v->SetServiceInterval(p1);
00918 }
00919 }
00920 }
00921
00922 InvalidateDetailsWindow(0);
00923
00924 return true;
00925 }
00926
00927 static bool UpdateIntervalTrains(int32 p1)
00928 {
00929 return UpdateInterval(VEH_TRAIN, p1);
00930 }
00931
00932 static bool UpdateIntervalRoadVeh(int32 p1)
00933 {
00934 return UpdateInterval(VEH_ROAD, p1);
00935 }
00936
00937 static bool UpdateIntervalShips(int32 p1)
00938 {
00939 return UpdateInterval(VEH_SHIP, p1);
00940 }
00941
00942 static bool UpdateIntervalAircraft(int32 p1)
00943 {
00944 return UpdateInterval(VEH_AIRCRAFT, p1);
00945 }
00946
00947 static bool TrainAccelerationModelChanged(int32 p1)
00948 {
00949 Train *t;
00950 FOR_ALL_TRAINS(t) {
00951 if (t->IsFrontEngine()) {
00952 t->tcache.cached_max_curve_speed = t->GetCurveSpeedLimit();
00953 t->UpdateAcceleration();
00954 }
00955 }
00956
00957
00958 SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00959 InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00960 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00961
00962 return true;
00963 }
00964
00970 static bool TrainSlopeSteepnessChanged(int32 p1)
00971 {
00972 Train *t;
00973 FOR_ALL_TRAINS(t) {
00974 if (t->IsFrontEngine()) t->CargoChanged();
00975 }
00976
00977 return true;
00978 }
00979
00985 static bool RoadVehAccelerationModelChanged(int32 p1)
00986 {
00987 if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) {
00988 RoadVehicle *rv;
00989 FOR_ALL_ROADVEHICLES(rv) {
00990 if (rv->IsFrontEngine()) {
00991 rv->CargoChanged();
00992 }
00993 }
00994 }
00995
00996
00997 SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00998 InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00999 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
01000
01001 return true;
01002 }
01003
01009 static bool RoadVehSlopeSteepnessChanged(int32 p1)
01010 {
01011 RoadVehicle *rv;
01012 FOR_ALL_ROADVEHICLES(rv) {
01013 if (rv->IsFrontEngine()) rv->CargoChanged();
01014 }
01015
01016 return true;
01017 }
01018
01019 static bool DragSignalsDensityChanged(int32)
01020 {
01021 InvalidateWindowData(WC_BUILD_SIGNAL, 0);
01022
01023 return true;
01024 }
01025
01026 static bool TownFoundingChanged(int32 p1)
01027 {
01028 if (_game_mode != GM_EDITOR && _settings_game.economy.found_town == TF_FORBIDDEN) {
01029 DeleteWindowById(WC_FOUND_TOWN, 0);
01030 return true;
01031 }
01032 InvalidateWindowData(WC_FOUND_TOWN, 0);
01033 return true;
01034 }
01035
01036 static bool InvalidateVehTimetableWindow(int32 p1)
01037 {
01038 InvalidateWindowClassesData(WC_VEHICLE_TIMETABLE, VIWD_MODIFY_ORDERS);
01039 return true;
01040 }
01041
01042 static bool ZoomMinMaxChanged(int32 p1)
01043 {
01044 extern void ConstrainAllViewportsZoom();
01045 ConstrainAllViewportsZoom();
01046 GfxClearSpriteCache();
01047 return true;
01048 }
01049
01057 static bool InvalidateNewGRFChangeWindows(int32 p1)
01058 {
01059 InvalidateWindowClassesData(WC_SAVELOAD);
01060 DeleteWindowByClass(WC_GAME_OPTIONS);
01061 ReInitAllWindows();
01062 return true;
01063 }
01064
01065 static bool InvalidateCompanyLiveryWindow(int32 p1)
01066 {
01067 InvalidateWindowClassesData(WC_COMPANY_COLOUR);
01068 return RedrawScreen(p1);
01069 }
01070
01071 static bool InvalidateIndustryViewWindow(int32 p1)
01072 {
01073 InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
01074 return true;
01075 }
01076
01077 static bool InvalidateAISettingsWindow(int32 p1)
01078 {
01079 InvalidateWindowClassesData(WC_AI_SETTINGS);
01080 return true;
01081 }
01082
01088 static bool RedrawTownAuthority(int32 p1)
01089 {
01090 SetWindowClassesDirty(WC_TOWN_AUTHORITY);
01091 return true;
01092 }
01093
01099 static bool InvalidateCompanyInfrastructureWindow(int32 p1)
01100 {
01101 InvalidateWindowClassesData(WC_COMPANY_INFRASTRUCTURE);
01102 return true;
01103 }
01104
01110 static bool InvalidateCompanyWindow(int32 p1)
01111 {
01112 InvalidateWindowClassesData(WC_COMPANY);
01113 return true;
01114 }
01115
01117 static void ValidateSettings()
01118 {
01119
01120 if (_settings_newgame.game_creation.land_generator == 0 &&
01121 _settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
01122 _settings_newgame.difficulty.quantity_sea_lakes = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE;
01123 }
01124 }
01125
01126 static bool DifficultyNoiseChange(int32 i)
01127 {
01128 if (_game_mode == GM_NORMAL) {
01129 UpdateAirportsNoise();
01130 if (_settings_game.economy.station_noise_level) {
01131 InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
01132 }
01133 }
01134
01135 return true;
01136 }
01137
01138 static bool MaxNoAIsChange(int32 i)
01139 {
01140 if (GetGameSettings().difficulty.max_no_competitors != 0 &&
01141 AI::GetInfoList()->size() == 0 &&
01142 (!_networking || _network_server)) {
01143 ShowErrorMessage(STR_WARNING_NO_SUITABLE_AI, INVALID_STRING_ID, WL_CRITICAL);
01144 }
01145
01146 return true;
01147 }
01148
01154 static bool CheckRoadSide(int p1)
01155 {
01156 extern bool RoadVehiclesAreBuilt();
01157 return _game_mode == GM_MENU || !RoadVehiclesAreBuilt();
01158 }
01159
01167 static size_t ConvertLandscape(const char *value)
01168 {
01169
01170 return LookupOneOfMany("normal|hilly|desert|candy", value);
01171 }
01172
01173 static bool CheckFreeformEdges(int32 p1)
01174 {
01175 if (_game_mode == GM_MENU) return true;
01176 if (p1 != 0) {
01177 Ship *s;
01178 FOR_ALL_SHIPS(s) {
01179
01180 if (TileX(s->tile) == 0 || TileY(s->tile) == 0) {
01181 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01182 return false;
01183 }
01184 }
01185 BaseStation *st;
01186 FOR_ALL_BASE_STATIONS(st) {
01187
01188 if (st->IsInUse() && (TileX(st->xy) == 0 || TileY(st->xy) == 0)) {
01189 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01190 return false;
01191 }
01192 }
01193 for (uint i = 0; i < MapSizeX(); i++) MakeVoid(TileXY(i, 0));
01194 for (uint i = 0; i < MapSizeY(); i++) MakeVoid(TileXY(0, i));
01195 } else {
01196 for (uint i = 0; i < MapMaxX(); i++) {
01197 if (TileHeight(TileXY(i, 1)) != 0) {
01198 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01199 return false;
01200 }
01201 }
01202 for (uint i = 1; i < MapMaxX(); i++) {
01203 if (!IsTileType(TileXY(i, MapMaxY() - 1), MP_WATER) || TileHeight(TileXY(1, MapMaxY())) != 0) {
01204 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01205 return false;
01206 }
01207 }
01208 for (uint i = 0; i < MapMaxY(); i++) {
01209 if (TileHeight(TileXY(1, i)) != 0) {
01210 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01211 return false;
01212 }
01213 }
01214 for (uint i = 1; i < MapMaxY(); i++) {
01215 if (!IsTileType(TileXY(MapMaxX() - 1, i), MP_WATER) || TileHeight(TileXY(MapMaxX(), i)) != 0) {
01216 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01217 return false;
01218 }
01219 }
01220
01221 for (uint i = 0; i < MapMaxX(); i++) {
01222 SetTileHeight(TileXY(i, 0), 0);
01223 SetTileType(TileXY(i, 0), MP_WATER);
01224 }
01225 for (uint i = 0; i < MapMaxY(); i++) {
01226 SetTileHeight(TileXY(0, i), 0);
01227 SetTileType(TileXY(0, i), MP_WATER);
01228 }
01229 }
01230 MarkWholeScreenDirty();
01231 return true;
01232 }
01233
01238 static bool ChangeDynamicEngines(int32 p1)
01239 {
01240 if (_game_mode == GM_MENU) return true;
01241
01242 if (!EngineOverrideManager::ResetToCurrentNewGRFConfig()) {
01243 ShowErrorMessage(STR_CONFIG_SETTING_DYNAMIC_ENGINES_EXISTING_VEHICLES, INVALID_STRING_ID, WL_ERROR);
01244 return false;
01245 }
01246
01247 return true;
01248 }
01249
01250 static bool StationCatchmentChanged(int32 p1)
01251 {
01252 Station::RecomputeIndustriesNearForAll();
01253 return true;
01254 }
01255
01256
01257 #ifdef ENABLE_NETWORK
01258
01259 static bool UpdateClientName(int32 p1)
01260 {
01261 NetworkUpdateClientName();
01262 return true;
01263 }
01264
01265 static bool UpdateServerPassword(int32 p1)
01266 {
01267 if (strcmp(_settings_client.network.server_password, "*") == 0) {
01268 _settings_client.network.server_password[0] = '\0';
01269 }
01270
01271 return true;
01272 }
01273
01274 static bool UpdateRconPassword(int32 p1)
01275 {
01276 if (strcmp(_settings_client.network.rcon_password, "*") == 0) {
01277 _settings_client.network.rcon_password[0] = '\0';
01278 }
01279
01280 return true;
01281 }
01282
01283 static bool UpdateClientConfigValues(int32 p1)
01284 {
01285 if (_network_server) NetworkServerSendConfigUpdate();
01286
01287 return true;
01288 }
01289
01290 #endif
01291
01292
01293
01294
01298 static void PrepareOldDiffCustom()
01299 {
01300 memset(_old_diff_custom, 0, sizeof(_old_diff_custom));
01301 }
01302
01309 static void HandleOldDiffCustom(bool savegame)
01310 {
01311 uint options_to_load = GAME_DIFFICULTY_NUM - ((savegame && IsSavegameVersionBefore(4)) ? 1 : 0);
01312
01313 if (!savegame) {
01314
01315 bool old_diff_custom_used = false;
01316 for (uint i = 0; i < options_to_load && !old_diff_custom_used; i++) {
01317 old_diff_custom_used = (_old_diff_custom[i] != 0);
01318 }
01319
01320 if (!old_diff_custom_used) return;
01321 }
01322
01323 for (uint i = 0; i < options_to_load; i++) {
01324 const SettingDesc *sd = &_settings[i];
01325
01326 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01327 void *var = GetVariableAddress(savegame ? &_settings_game : &_settings_newgame, &sd->save);
01328 Write_ValidateSetting(var, sd, (int32)((i == 4 ? 1000 : 1) * _old_diff_custom[i]));
01329 }
01330 }
01331
01332 static void AILoadConfig(IniFile *ini, const char *grpname)
01333 {
01334 IniGroup *group = ini->GetGroup(grpname);
01335 IniItem *item;
01336
01337
01338 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01339 AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME)->Change(NULL);
01340 }
01341
01342
01343 if (group == NULL) return;
01344
01345 CompanyID c = COMPANY_FIRST;
01346 for (item = group->item; c < MAX_COMPANIES && item != NULL; c++, item = item->next) {
01347 AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
01348
01349 config->Change(item->name);
01350 if (!config->HasScript()) {
01351 if (strcmp(item->name, "none") != 0) {
01352 DEBUG(script, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name);
01353 continue;
01354 }
01355 }
01356 if (item->value != NULL) config->StringToSettings(item->value);
01357 }
01358 }
01359
01360 static void GameLoadConfig(IniFile *ini, const char *grpname)
01361 {
01362 IniGroup *group = ini->GetGroup(grpname);
01363 IniItem *item;
01364
01365
01366 GameConfig::GetConfig(GameConfig::SSS_FORCE_NEWGAME)->Change(NULL);
01367
01368
01369 if (group == NULL) return;
01370
01371 item = group->item;
01372 if (item == NULL) return;
01373
01374 GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
01375
01376 config->Change(item->name);
01377 if (!config->HasScript()) {
01378 if (strcmp(item->name, "none") != 0) {
01379 DEBUG(script, 0, "The GameScript by the name '%s' was no longer found, and removed from the list.", item->name);
01380 return;
01381 }
01382 }
01383 if (item->value != NULL) config->StringToSettings(item->value);
01384 }
01385
01392 static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_static)
01393 {
01394 IniGroup *group = ini->GetGroup(grpname);
01395 IniItem *item;
01396 GRFConfig *first = NULL;
01397 GRFConfig **curr = &first;
01398
01399 if (group == NULL) return NULL;
01400
01401 for (item = group->item; item != NULL; item = item->next) {
01402 GRFConfig *c = new GRFConfig(item->name);
01403
01404
01405 if (!StrEmpty(item->value)) {
01406 c->num_params = ParseIntList(item->value, (int*)c->param, lengthof(c->param));
01407 if (c->num_params == (byte)-1) {
01408 SetDParamStr(0, item->name);
01409 ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY, WL_CRITICAL);
01410 c->num_params = 0;
01411 }
01412 }
01413
01414
01415 if (!FillGRFDetails(c, is_static) || HasBit(c->flags, GCF_INVALID)) {
01416 if (c->status == GCS_NOT_FOUND) {
01417 SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_NOT_FOUND);
01418 } else if (HasBit(c->flags, GCF_UNSAFE)) {
01419 SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNSAFE);
01420 } else if (HasBit(c->flags, GCF_SYSTEM)) {
01421 SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_SYSTEM);
01422 } else if (HasBit(c->flags, GCF_INVALID)) {
01423 SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_INCOMPATIBLE);
01424 } else {
01425 SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNKNOWN);
01426 }
01427
01428 SetDParamStr(0, item->name);
01429 ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_GRF, WL_CRITICAL);
01430 delete c;
01431 continue;
01432 }
01433
01434
01435 bool duplicate = false;
01436 for (const GRFConfig *gc = first; gc != NULL; gc = gc->next) {
01437 if (gc->ident.grfid == c->ident.grfid) {
01438 SetDParamStr(0, item->name);
01439 SetDParamStr(1, gc->filename);
01440 ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_DUPLICATE_GRFID, WL_CRITICAL);
01441 duplicate = true;
01442 break;
01443 }
01444 }
01445 if (duplicate) {
01446 delete c;
01447 continue;
01448 }
01449
01450
01451 if (is_static) SetBit(c->flags, GCF_STATIC);
01452
01453
01454 *curr = c;
01455 curr = &c->next;
01456 }
01457
01458 return first;
01459 }
01460
01461 static void AISaveConfig(IniFile *ini, const char *grpname)
01462 {
01463 IniGroup *group = ini->GetGroup(grpname);
01464
01465 if (group == NULL) return;
01466 group->Clear();
01467
01468 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01469 AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
01470 const char *name;
01471 char value[1024];
01472 config->SettingsToString(value, lengthof(value));
01473
01474 if (config->HasScript()) {
01475 name = config->GetName();
01476 } else {
01477 name = "none";
01478 }
01479
01480 IniItem *item = new IniItem(group, name, strlen(name));
01481 item->SetValue(value);
01482 }
01483 }
01484
01485 static void GameSaveConfig(IniFile *ini, const char *grpname)
01486 {
01487 IniGroup *group = ini->GetGroup(grpname);
01488
01489 if (group == NULL) return;
01490 group->Clear();
01491
01492 GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
01493 const char *name;
01494 char value[1024];
01495 config->SettingsToString(value, lengthof(value));
01496
01497 if (config->HasScript()) {
01498 name = config->GetName();
01499 } else {
01500 name = "none";
01501 }
01502
01503 IniItem *item = new IniItem(group, name, strlen(name));
01504 item->SetValue(value);
01505 }
01506
01511 static void SaveVersionInConfig(IniFile *ini)
01512 {
01513 IniGroup *group = ini->GetGroup("version");
01514
01515 char version[9];
01516 snprintf(version, lengthof(version), "%08X", _openttd_newgrf_version);
01517
01518 const char * const versions[][2] = {
01519 { "version_string", _openttd_revision },
01520 { "version_number", version }
01521 };
01522
01523 for (uint i = 0; i < lengthof(versions); i++) {
01524 group->GetItem(versions[i][0], true)->SetValue(versions[i][1]);
01525 }
01526 }
01527
01528
01529 static void GRFSaveConfig(IniFile *ini, const char *grpname, const GRFConfig *list)
01530 {
01531 ini->RemoveGroup(grpname);
01532 IniGroup *group = ini->GetGroup(grpname);
01533 const GRFConfig *c;
01534
01535 for (c = list; c != NULL; c = c->next) {
01536 char params[512];
01537 GRFBuildParamList(params, c, lastof(params));
01538
01539 group->GetItem(c->filename, true)->SetValue(params);
01540 }
01541 }
01542
01543
01544 static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list, bool basic_settings = true, bool other_settings = true)
01545 {
01546 if (basic_settings) {
01547 proc(ini, (const SettingDesc*)_misc_settings, "misc", NULL);
01548 #if defined(WIN32) && !defined(DEDICATED)
01549 proc(ini, (const SettingDesc*)_win32_settings, "win32", NULL);
01550 #endif
01551 }
01552
01553 if (other_settings) {
01554 proc(ini, _settings, "patches", &_settings_newgame);
01555 proc(ini, _currency_settings,"currency", &_custom_currency);
01556 proc(ini, _company_settings, "company", &_settings_client.company);
01557
01558 #ifdef ENABLE_NETWORK
01559 proc_list(ini, "server_bind_addresses", &_network_bind_list);
01560 proc_list(ini, "servers", &_network_host_list);
01561 proc_list(ini, "bans", &_network_ban_list);
01562 #endif
01563 }
01564 }
01565
01566 static IniFile *IniLoadConfig()
01567 {
01568 IniFile *ini = new IniFile(_list_group_names);
01569 ini->LoadFromDisk(_config_file, BASE_DIR);
01570 return ini;
01571 }
01572
01577 void LoadFromConfig(bool minimal)
01578 {
01579 IniFile *ini = IniLoadConfig();
01580 if (!minimal) ResetCurrencies(false);
01581
01582
01583 HandleSettingDescs(ini, IniLoadSettings, IniLoadSettingList, minimal, !minimal);
01584
01585 if (!minimal) {
01586 _grfconfig_newgame = GRFLoadConfig(ini, "newgrf", false);
01587 _grfconfig_static = GRFLoadConfig(ini, "newgrf-static", true);
01588 AILoadConfig(ini, "ai_players");
01589 GameLoadConfig(ini, "game_scripts");
01590
01591 PrepareOldDiffCustom();
01592 IniLoadSettings(ini, _gameopt_settings, "gameopt", &_settings_newgame);
01593 HandleOldDiffCustom(false);
01594
01595 ValidateSettings();
01596
01597
01598 extern void ScheduleErrorMessage(ErrorList &datas);
01599 ScheduleErrorMessage(_settings_error_list);
01600 if (FindWindowById(WC_ERRMSG, 0) == NULL) ShowFirstError();
01601 }
01602
01603 delete ini;
01604 }
01605
01607 void SaveToConfig()
01608 {
01609 IniFile *ini = IniLoadConfig();
01610
01611
01612 ini->RemoveGroup("patches");
01613 ini->RemoveGroup("yapf");
01614 ini->RemoveGroup("gameopt");
01615
01616 HandleSettingDescs(ini, IniSaveSettings, IniSaveSettingList);
01617 GRFSaveConfig(ini, "newgrf", _grfconfig_newgame);
01618 GRFSaveConfig(ini, "newgrf-static", _grfconfig_static);
01619 AISaveConfig(ini, "ai_players");
01620 GameSaveConfig(ini, "game_scripts");
01621 SaveVersionInConfig(ini);
01622 ini->SaveToDisk(_config_file);
01623 delete ini;
01624 }
01625
01630 void GetGRFPresetList(GRFPresetList *list)
01631 {
01632 list->Clear();
01633
01634 IniFile *ini = IniLoadConfig();
01635 IniGroup *group;
01636 for (group = ini->group; group != NULL; group = group->next) {
01637 if (strncmp(group->name, "preset-", 7) == 0) {
01638 *list->Append() = strdup(group->name + 7);
01639 }
01640 }
01641
01642 delete ini;
01643 }
01644
01651 GRFConfig *LoadGRFPresetFromConfig(const char *config_name)
01652 {
01653 char *section = (char*)alloca(strlen(config_name) + 8);
01654 sprintf(section, "preset-%s", config_name);
01655
01656 IniFile *ini = IniLoadConfig();
01657 GRFConfig *config = GRFLoadConfig(ini, section, false);
01658 delete ini;
01659
01660 return config;
01661 }
01662
01669 void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
01670 {
01671 char *section = (char*)alloca(strlen(config_name) + 8);
01672 sprintf(section, "preset-%s", config_name);
01673
01674 IniFile *ini = IniLoadConfig();
01675 GRFSaveConfig(ini, section, config);
01676 ini->SaveToDisk(_config_file);
01677 delete ini;
01678 }
01679
01684 void DeleteGRFPresetFromConfig(const char *config_name)
01685 {
01686 char *section = (char*)alloca(strlen(config_name) + 8);
01687 sprintf(section, "preset-%s", config_name);
01688
01689 IniFile *ini = IniLoadConfig();
01690 ini->RemoveGroup(section);
01691 ini->SaveToDisk(_config_file);
01692 delete ini;
01693 }
01694
01695 static const SettingDesc *GetSettingDescription(uint index)
01696 {
01697 if (index >= lengthof(_settings)) return NULL;
01698 return &_settings[index];
01699 }
01700
01712 CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01713 {
01714 const SettingDesc *sd = GetSettingDescription(p1);
01715
01716 if (sd == NULL) return CMD_ERROR;
01717 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR;
01718
01719 if (!sd->IsEditable(true)) return CMD_ERROR;
01720
01721 if (flags & DC_EXEC) {
01722 void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01723
01724 int32 oldval = (int32)ReadValue(var, sd->save.conv);
01725 int32 newval = (int32)p2;
01726
01727 Write_ValidateSetting(var, sd, newval);
01728 newval = (int32)ReadValue(var, sd->save.conv);
01729
01730 if (oldval == newval) return CommandCost();
01731
01732 if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01733 WriteValue(var, sd->save.conv, (int64)oldval);
01734 return CommandCost();
01735 }
01736
01737 if (sd->desc.flags & SGF_NO_NETWORK) {
01738 GamelogStartAction(GLAT_SETTING);
01739 GamelogSetting(sd->desc.name, oldval, newval);
01740 GamelogStopAction();
01741 }
01742
01743 SetWindowClassesDirty(WC_GAME_OPTIONS);
01744 }
01745
01746 return CommandCost();
01747 }
01748
01759 CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01760 {
01761 if (p1 >= lengthof(_company_settings)) return CMD_ERROR;
01762 const SettingDesc *sd = &_company_settings[p1];
01763
01764 if (flags & DC_EXEC) {
01765 void *var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01766
01767 int32 oldval = (int32)ReadValue(var, sd->save.conv);
01768 int32 newval = (int32)p2;
01769
01770 Write_ValidateSetting(var, sd, newval);
01771 newval = (int32)ReadValue(var, sd->save.conv);
01772
01773 if (oldval == newval) return CommandCost();
01774
01775 if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01776 WriteValue(var, sd->save.conv, (int64)oldval);
01777 return CommandCost();
01778 }
01779
01780 SetWindowClassesDirty(WC_GAME_OPTIONS);
01781 }
01782
01783 return CommandCost();
01784 }
01785
01793 bool SetSettingValue(uint index, int32 value, bool force_newgame)
01794 {
01795 const SettingDesc *sd = &_settings[index];
01796
01797
01798
01799
01800 if (sd->save.conv & SLF_NO_NETWORK_SYNC) {
01801 void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01802 Write_ValidateSetting(var, sd, value);
01803
01804 if (_game_mode != GM_MENU) {
01805 void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01806 Write_ValidateSetting(var2, sd, value);
01807 }
01808 if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01809
01810 SetWindowClassesDirty(WC_GAME_OPTIONS);
01811
01812 return true;
01813 }
01814
01815 if (force_newgame) {
01816 void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01817 Write_ValidateSetting(var2, sd, value);
01818 return true;
01819 }
01820
01821
01822 if (!_networking || (_networking && _network_server)) {
01823 return DoCommandP(0, index, value, CMD_CHANGE_SETTING);
01824 }
01825 return false;
01826 }
01827
01834 void SetCompanySetting(uint index, int32 value)
01835 {
01836 const SettingDesc *sd = &_company_settings[index];
01837 if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01838 DoCommandP(0, index, value, CMD_CHANGE_COMPANY_SETTING);
01839 } else {
01840 void *var = GetVariableAddress(&_settings_client.company, &sd->save);
01841 Write_ValidateSetting(var, sd, value);
01842 if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01843 }
01844 }
01845
01849 void SetDefaultCompanySettings(CompanyID cid)
01850 {
01851 Company *c = Company::Get(cid);
01852 const SettingDesc *sd;
01853 for (sd = _company_settings; sd->save.cmd != SL_END; sd++) {
01854 void *var = GetVariableAddress(&c->settings, &sd->save);
01855 Write_ValidateSetting(var, sd, (int32)(size_t)sd->desc.def);
01856 }
01857 }
01858
01859 #if defined(ENABLE_NETWORK)
01860
01863 void SyncCompanySettings()
01864 {
01865 const SettingDesc *sd;
01866 uint i = 0;
01867 for (sd = _company_settings; sd->save.cmd != SL_END; sd++, i++) {
01868 const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01869 const void *new_var = GetVariableAddress(&_settings_client.company, &sd->save);
01870 uint32 old_value = (uint32)ReadValue(old_var, sd->save.conv);
01871 uint32 new_value = (uint32)ReadValue(new_var, sd->save.conv);
01872 if (old_value != new_value) NetworkSendCommand(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, NULL, NULL, _local_company);
01873 }
01874 }
01875 #endif
01876
01882 uint GetCompanySettingIndex(const char *name)
01883 {
01884 uint i;
01885 const SettingDesc *sd = GetSettingFromName(name, &i);
01886 assert(sd != NULL && (sd->desc.flags & SGF_PER_COMPANY) != 0);
01887 return i;
01888 }
01889
01897 bool SetSettingValue(uint index, const char *value, bool force_newgame)
01898 {
01899 const SettingDesc *sd = &_settings[index];
01900 assert(sd->save.conv & SLF_NO_NETWORK_SYNC);
01901
01902 if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) {
01903 char **var = (char**)GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
01904 free(*var);
01905 *var = strcmp(value, "(null)") == 0 ? NULL : strdup(value);
01906 } else {
01907 char *var = (char*)GetVariableAddress(NULL, &sd->save);
01908 ttd_strlcpy(var, value, sd->save.length);
01909 }
01910 if (sd->desc.proc != NULL) sd->desc.proc(0);
01911
01912 return true;
01913 }
01914
01922 const SettingDesc *GetSettingFromName(const char *name, uint *i)
01923 {
01924 const SettingDesc *sd;
01925
01926
01927 for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01928 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01929 if (strcmp(sd->desc.name, name) == 0) return sd;
01930 }
01931
01932
01933 for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01934 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01935 const char *short_name = strchr(sd->desc.name, '.');
01936 if (short_name != NULL) {
01937 short_name++;
01938 if (strcmp(short_name, name) == 0) return sd;
01939 }
01940 }
01941
01942 if (strncmp(name, "company.", 8) == 0) name += 8;
01943
01944 for (*i = 0, sd = _company_settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01945 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01946 if (strcmp(sd->desc.name, name) == 0) return sd;
01947 }
01948
01949 return NULL;
01950 }
01951
01952
01953
01954 void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
01955 {
01956 uint index;
01957 const SettingDesc *sd = GetSettingFromName(name, &index);
01958
01959 if (sd == NULL) {
01960 IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01961 return;
01962 }
01963
01964 bool success;
01965 if (sd->desc.cmd == SDT_STRING) {
01966 success = SetSettingValue(index, value, force_newgame);
01967 } else {
01968 uint32 val;
01969 extern bool GetArgumentInteger(uint32 *value, const char *arg);
01970 success = GetArgumentInteger(&val, value);
01971 if (!success) {
01972 IConsolePrintF(CC_ERROR, "'%s' is not an integer.", value);
01973 return;
01974 }
01975
01976 success = SetSettingValue(index, val, force_newgame);
01977 }
01978
01979 if (!success) {
01980 if (_network_server) {
01981 IConsoleError("This command/variable is not available during network games.");
01982 } else {
01983 IConsoleError("This command/variable is only available to a network server.");
01984 }
01985 }
01986 }
01987
01988 void IConsoleSetSetting(const char *name, int value)
01989 {
01990 uint index;
01991 const SettingDesc *sd = GetSettingFromName(name, &index);
01992 assert(sd != NULL);
01993 SetSettingValue(index, value);
01994 }
01995
02001 void IConsoleGetSetting(const char *name, bool force_newgame)
02002 {
02003 char value[20];
02004 uint index;
02005 const SettingDesc *sd = GetSettingFromName(name, &index);
02006 const void *ptr;
02007
02008 if (sd == NULL) {
02009 IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
02010 return;
02011 }
02012
02013 ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
02014
02015 if (sd->desc.cmd == SDT_STRING) {
02016 IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char * const *)ptr : (const char *)ptr);
02017 } else {
02018 if (sd->desc.cmd == SDT_BOOLX) {
02019 snprintf(value, sizeof(value), (*(const bool*)ptr != 0) ? "on" : "off");
02020 } else {
02021 snprintf(value, sizeof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
02022 }
02023
02024 IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s' (min: %s%d, max: %u)",
02025 name, value, (sd->desc.flags & SGF_0ISDISABLED) ? "(0) " : "", sd->desc.min, sd->desc.max);
02026 }
02027 }
02028
02034 void IConsoleListSettings(const char *prefilter)
02035 {
02036 IConsolePrintF(CC_WARNING, "All settings with their current value:");
02037
02038 for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) {
02039 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
02040 if (prefilter != NULL && strstr(sd->desc.name, prefilter) == NULL) continue;
02041 char value[80];
02042 const void *ptr = GetVariableAddress(&GetGameSettings(), &sd->save);
02043
02044 if (sd->desc.cmd == SDT_BOOLX) {
02045 snprintf(value, lengthof(value), (*(const bool *)ptr != 0) ? "on" : "off");
02046 } else if (sd->desc.cmd == SDT_STRING) {
02047 snprintf(value, sizeof(value), "%s", (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char * const *)ptr : (const char *)ptr);
02048 } else {
02049 snprintf(value, lengthof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
02050 }
02051 IConsolePrintF(CC_DEFAULT, "%s = %s", sd->desc.name, value);
02052 }
02053
02054 IConsolePrintF(CC_WARNING, "Use 'setting' command to change a value");
02055 }
02056
02063 static void LoadSettings(const SettingDesc *osd, void *object)
02064 {
02065 for (; osd->save.cmd != SL_END; osd++) {
02066 const SaveLoad *sld = &osd->save;
02067 void *ptr = GetVariableAddress(object, sld);
02068
02069 if (!SlObjectMember(ptr, sld)) continue;
02070 if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, osd, ReadValue(ptr, sld->conv));
02071 }
02072 }
02073
02080 static void SaveSettings(const SettingDesc *sd, void *object)
02081 {
02082
02083
02084 const SettingDesc *i;
02085 size_t length = 0;
02086 for (i = sd; i->save.cmd != SL_END; i++) {
02087 length += SlCalcObjMemberLength(object, &i->save);
02088 }
02089 SlSetLength(length);
02090
02091 for (i = sd; i->save.cmd != SL_END; i++) {
02092 void *ptr = GetVariableAddress(object, &i->save);
02093 SlObjectMember(ptr, &i->save);
02094 }
02095 }
02096
02097 static void Load_OPTS()
02098 {
02099
02100
02101
02102 PrepareOldDiffCustom();
02103 LoadSettings(_gameopt_settings, &_settings_game);
02104 HandleOldDiffCustom(true);
02105 }
02106
02107 static void Load_PATS()
02108 {
02109
02110
02111
02112 LoadSettings(_settings, &_settings_game);
02113 }
02114
02115 static void Check_PATS()
02116 {
02117 LoadSettings(_settings, &_load_check_data.settings);
02118 }
02119
02120 static void Save_PATS()
02121 {
02122 SaveSettings(_settings, &_settings_game);
02123 }
02124
02125 void CheckConfig()
02126 {
02127
02128
02129
02130
02131 if (_settings_newgame.pf.opf.pf_maxdepth == 16 && _settings_newgame.pf.opf.pf_maxlength == 512) {
02132 _settings_newgame.pf.opf.pf_maxdepth = 48;
02133 _settings_newgame.pf.opf.pf_maxlength = 4096;
02134 }
02135 }
02136
02137 extern const ChunkHandler _setting_chunk_handlers[] = {
02138 { 'OPTS', NULL, Load_OPTS, NULL, NULL, CH_RIFF},
02139 { 'PATS', Save_PATS, Load_PATS, NULL, Check_PATS, CH_RIFF | CH_LAST},
02140 };
02141
02142 static bool IsSignedVarMemType(VarType vt)
02143 {
02144 switch (GetVarMemType(vt)) {
02145 case SLE_VAR_I8:
02146 case SLE_VAR_I16:
02147 case SLE_VAR_I32:
02148 case SLE_VAR_I64:
02149 return true;
02150 }
02151 return false;
02152 }