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 case SLE_VAR_I16:
00233 case SLE_VAR_U16:
00234 for (i = 0; i != nitems; i++) ((uint16*)array)[i] = items[i];
00235 break;
00236 case SLE_VAR_I32:
00237 case SLE_VAR_U32:
00238 for (i = 0; i != nitems; i++) ((uint32*)array)[i] = items[i];
00239 break;
00240 default: NOT_REACHED();
00241 }
00242
00243 return true;
00244 }
00245
00255 static void MakeIntList(char *buf, const char *last, const void *array, int nelems, VarType type)
00256 {
00257 int i, v = 0;
00258 const byte *p = (const byte *)array;
00259
00260 for (i = 0; i != nelems; i++) {
00261 switch (type) {
00262 case SLE_VAR_BL:
00263 case SLE_VAR_I8: v = *(const int8 *)p; p += 1; break;
00264 case SLE_VAR_U8: v = *(const uint8 *)p; p += 1; break;
00265 case SLE_VAR_I16: v = *(const int16 *)p; p += 2; break;
00266 case SLE_VAR_U16: v = *(const uint16 *)p; p += 2; break;
00267 case SLE_VAR_I32: v = *(const int32 *)p; p += 4; break;
00268 case SLE_VAR_U32: v = *(const uint32 *)p; p += 4; break;
00269 default: NOT_REACHED();
00270 }
00271 buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
00272 }
00273 }
00274
00282 static void MakeOneOfMany(char *buf, const char *last, const char *many, int id)
00283 {
00284 int orig_id = id;
00285
00286
00287 while (--id >= 0) {
00288 for (; *many != '|'; many++) {
00289 if (*many == '\0') {
00290 seprintf(buf, last, "%d", orig_id);
00291 return;
00292 }
00293 }
00294 many++;
00295 }
00296
00297
00298 while (*many != '\0' && *many != '|' && buf < last) *buf++ = *many++;
00299 *buf = '\0';
00300 }
00301
00310 static void MakeManyOfMany(char *buf, const char *last, const char *many, uint32 x)
00311 {
00312 const char *start;
00313 int i = 0;
00314 bool init = true;
00315
00316 for (; x != 0; x >>= 1, i++) {
00317 start = many;
00318 while (*many != 0 && *many != '|') many++;
00319
00320 if (HasBit(x, 0)) {
00321 if (!init) buf += seprintf(buf, last, "|");
00322 init = false;
00323 if (start == many) {
00324 buf += seprintf(buf, last, "%d", i);
00325 } else {
00326 memcpy(buf, start, many - start);
00327 buf += many - start;
00328 }
00329 }
00330
00331 if (*many == '|') many++;
00332 }
00333
00334 *buf = '\0';
00335 }
00336
00343 static const void *StringToVal(const SettingDescBase *desc, const char *orig_str)
00344 {
00345 const char *str = orig_str == NULL ? "" : orig_str;
00346 switch (desc->cmd) {
00347 case SDT_NUMX: {
00348 char *end;
00349 size_t val = strtoul(str, &end, 0);
00350 if (end == str) {
00351 ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
00352 msg.SetDParamStr(0, str);
00353 msg.SetDParamStr(1, desc->name);
00354 _settings_error_list.push_back(msg);
00355 return desc->def;
00356 }
00357 if (*end != '\0') {
00358 ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_TRAILING_CHARACTERS);
00359 msg.SetDParamStr(0, desc->name);
00360 _settings_error_list.push_back(msg);
00361 }
00362 return (void*)val;
00363 }
00364 case SDT_ONEOFMANY: {
00365 size_t r = LookupOneOfMany(desc->many, str);
00366
00367
00368 if (r == (size_t)-1 && desc->proc_cnvt != NULL) r = desc->proc_cnvt(str);
00369 if (r != (size_t)-1) return (void*)r;
00370
00371 ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
00372 msg.SetDParamStr(0, str);
00373 msg.SetDParamStr(1, desc->name);
00374 _settings_error_list.push_back(msg);
00375 return desc->def;
00376 }
00377 case SDT_MANYOFMANY: {
00378 size_t r = LookupManyOfMany(desc->many, str);
00379 if (r != (size_t)-1) return (void*)r;
00380 ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
00381 msg.SetDParamStr(0, str);
00382 msg.SetDParamStr(1, desc->name);
00383 _settings_error_list.push_back(msg);
00384 return desc->def;
00385 }
00386 case SDT_BOOLX: {
00387 if (strcmp(str, "true") == 0 || strcmp(str, "on") == 0 || strcmp(str, "1") == 0) return (void*)true;
00388 if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return (void*)false;
00389
00390 ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
00391 msg.SetDParamStr(0, str);
00392 msg.SetDParamStr(1, desc->name);
00393 _settings_error_list.push_back(msg);
00394 return desc->def;
00395 }
00396
00397 case SDT_STRING: return orig_str;
00398 case SDT_INTLIST: return str;
00399 default: break;
00400 }
00401
00402 return NULL;
00403 }
00404
00414 static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val)
00415 {
00416 const SettingDescBase *sdb = &sd->desc;
00417
00418 if (sdb->cmd != SDT_BOOLX &&
00419 sdb->cmd != SDT_NUMX &&
00420 sdb->cmd != SDT_ONEOFMANY &&
00421 sdb->cmd != SDT_MANYOFMANY) {
00422 return;
00423 }
00424
00425
00426 if (sdb->cmd != SDT_MANYOFMANY) {
00427
00428
00429
00430
00431
00432 switch (GetVarMemType(sd->save.conv)) {
00433 case SLE_VAR_NULL: return;
00434 case SLE_VAR_BL:
00435 case SLE_VAR_I8:
00436 case SLE_VAR_U8:
00437 case SLE_VAR_I16:
00438 case SLE_VAR_U16:
00439 case SLE_VAR_I32: {
00440
00441 if (!(sdb->flags & SGF_0ISDISABLED) || val != 0) val = Clamp(val, sdb->min, sdb->max);
00442 break;
00443 }
00444 case SLE_VAR_U32: {
00445
00446 uint min = ((sdb->flags & SGF_0ISDISABLED) && (uint)val <= (uint)sdb->min) ? 0 : sdb->min;
00447 WriteValue(ptr, SLE_VAR_U32, (int64)ClampU(val, min, sdb->max));
00448 return;
00449 }
00450 case SLE_VAR_I64:
00451 case SLE_VAR_U64:
00452 default: NOT_REACHED();
00453 }
00454 }
00455
00456 WriteValue(ptr, sd->save.conv, (int64)val);
00457 }
00458
00467 static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00468 {
00469 IniGroup *group;
00470 IniGroup *group_def = ini->GetGroup(grpname);
00471 IniItem *item;
00472 const void *p;
00473 void *ptr;
00474 const char *s;
00475
00476 for (; sd->save.cmd != SL_END; sd++) {
00477 const SettingDescBase *sdb = &sd->desc;
00478 const SaveLoad *sld = &sd->save;
00479
00480 if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00481
00482
00483 s = strchr(sdb->name, '.');
00484 if (s != NULL) {
00485 group = ini->GetGroup(sdb->name, s - sdb->name);
00486 s++;
00487 } else {
00488 s = sdb->name;
00489 group = group_def;
00490 }
00491
00492 item = group->GetItem(s, false);
00493 if (item == NULL && group != group_def) {
00494
00495
00496 item = group_def->GetItem(s, false);
00497 }
00498 if (item == NULL) {
00499
00500
00501 const char *sc = strchr(s, '.');
00502 if (sc != NULL) item = ini->GetGroup(s, sc - s)->GetItem(sc + 1, false);
00503 }
00504
00505 p = (item == NULL) ? sdb->def : StringToVal(sdb, item->value);
00506 ptr = GetVariableAddress(object, sld);
00507
00508 switch (sdb->cmd) {
00509 case SDT_BOOLX:
00510 case SDT_NUMX:
00511 case SDT_ONEOFMANY:
00512 case SDT_MANYOFMANY:
00513 Write_ValidateSetting(ptr, sd, (int32)(size_t)p);
00514 break;
00515
00516 case SDT_STRING:
00517 switch (GetVarMemType(sld->conv)) {
00518 case SLE_VAR_STRB:
00519 case SLE_VAR_STRBQ:
00520 if (p != NULL) ttd_strlcpy((char*)ptr, (const char*)p, sld->length);
00521 break;
00522 case SLE_VAR_STR:
00523 case SLE_VAR_STRQ:
00524 free(*(char**)ptr);
00525 *(char**)ptr = p == NULL ? NULL : strdup((const char*)p);
00526 break;
00527 case SLE_VAR_CHAR: if (p != NULL) *(char *)ptr = *(const char *)p; break;
00528 default: NOT_REACHED();
00529 }
00530 break;
00531
00532 case SDT_INTLIST: {
00533 if (!LoadIntList((const char*)p, ptr, sld->length, GetVarMemType(sld->conv))) {
00534 ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY);
00535 msg.SetDParamStr(0, sdb->name);
00536 _settings_error_list.push_back(msg);
00537
00538
00539 LoadIntList((const char*)sdb->def, ptr, sld->length, GetVarMemType(sld->conv));
00540 } else if (sd->desc.proc_cnvt != NULL) {
00541 sd->desc.proc_cnvt((const char*)p);
00542 }
00543 break;
00544 }
00545 default: NOT_REACHED();
00546 }
00547 }
00548 }
00549
00562 static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00563 {
00564 IniGroup *group_def = NULL, *group;
00565 IniItem *item;
00566 char buf[512];
00567 const char *s;
00568 void *ptr;
00569
00570 for (; sd->save.cmd != SL_END; sd++) {
00571 const SettingDescBase *sdb = &sd->desc;
00572 const SaveLoad *sld = &sd->save;
00573
00574
00575
00576 if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00577 if (sld->conv & SLF_NOT_IN_CONFIG) continue;
00578
00579
00580 s = strchr(sdb->name, '.');
00581 if (s != NULL) {
00582 group = ini->GetGroup(sdb->name, s - sdb->name);
00583 s++;
00584 } else {
00585 if (group_def == NULL) group_def = ini->GetGroup(grpname);
00586 s = sdb->name;
00587 group = group_def;
00588 }
00589
00590 item = group->GetItem(s, true);
00591 ptr = GetVariableAddress(object, sld);
00592
00593 if (item->value != NULL) {
00594
00595 const void *p = StringToVal(sdb, item->value);
00596
00597
00598
00599 switch (sdb->cmd) {
00600 case SDT_BOOLX:
00601 case SDT_NUMX:
00602 case SDT_ONEOFMANY:
00603 case SDT_MANYOFMANY:
00604 switch (GetVarMemType(sld->conv)) {
00605 case SLE_VAR_BL:
00606 if (*(bool*)ptr == (p != NULL)) continue;
00607 break;
00608 case SLE_VAR_I8:
00609 case SLE_VAR_U8:
00610 if (*(byte*)ptr == (byte)(size_t)p) continue;
00611 break;
00612 case SLE_VAR_I16:
00613 case SLE_VAR_U16:
00614 if (*(uint16*)ptr == (uint16)(size_t)p) continue;
00615 break;
00616 case SLE_VAR_I32:
00617 case SLE_VAR_U32:
00618 if (*(uint32*)ptr == (uint32)(size_t)p) continue;
00619 break;
00620 default: NOT_REACHED();
00621 }
00622 break;
00623 default: break;
00624 }
00625 }
00626
00627
00628 switch (sdb->cmd) {
00629 case SDT_BOOLX:
00630 case SDT_NUMX:
00631 case SDT_ONEOFMANY:
00632 case SDT_MANYOFMANY: {
00633 uint32 i = (uint32)ReadValue(ptr, sld->conv);
00634
00635 switch (sdb->cmd) {
00636 case SDT_BOOLX: strecpy(buf, (i != 0) ? "true" : "false", lastof(buf)); break;
00637 case SDT_NUMX: seprintf(buf, lastof(buf), IsSignedVarMemType(sld->conv) ? "%d" : "%u", i); break;
00638 case SDT_ONEOFMANY: MakeOneOfMany(buf, lastof(buf), sdb->many, i); break;
00639 case SDT_MANYOFMANY: MakeManyOfMany(buf, lastof(buf), sdb->many, i); break;
00640 default: NOT_REACHED();
00641 }
00642 break;
00643 }
00644
00645 case SDT_STRING:
00646 switch (GetVarMemType(sld->conv)) {
00647 case SLE_VAR_STRB: strecpy(buf, (char*)ptr, lastof(buf)); break;
00648 case SLE_VAR_STRBQ:seprintf(buf, lastof(buf), "\"%s\"", (char*)ptr); break;
00649 case SLE_VAR_STR: strecpy(buf, *(char**)ptr, lastof(buf)); break;
00650 case SLE_VAR_STRQ:
00651 if (*(char**)ptr == NULL) {
00652 buf[0] = '\0';
00653 } else {
00654 seprintf(buf, lastof(buf), "\"%s\"", *(char**)ptr);
00655 }
00656 break;
00657 case SLE_VAR_CHAR: buf[0] = *(char*)ptr; buf[1] = '\0'; break;
00658 default: NOT_REACHED();
00659 }
00660 break;
00661
00662 case SDT_INTLIST:
00663 MakeIntList(buf, lastof(buf), ptr, sld->length, GetVarMemType(sld->conv));
00664 break;
00665 default: NOT_REACHED();
00666 }
00667
00668
00669 free(item->value);
00670 item->value = strdup(buf);
00671 }
00672 }
00673
00683 static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList *list)
00684 {
00685 IniGroup *group = ini->GetGroup(grpname);
00686
00687 if (group == NULL || list == NULL) return;
00688
00689 list->Clear();
00690
00691 for (const IniItem *item = group->item; item != NULL; item = item->next) {
00692 if (item->name != NULL) *list->Append() = strdup(item->name);
00693 }
00694 }
00695
00705 static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList *list)
00706 {
00707 IniGroup *group = ini->GetGroup(grpname);
00708
00709 if (group == NULL || list == NULL) return;
00710 group->Clear();
00711
00712 for (char **iter = list->Begin(); iter != list->End(); iter++) {
00713 group->GetItem(*iter, true)->SetValue("");
00714 }
00715 }
00716
00717
00718
00720 static bool v_PositionMainToolbar(int32 p1)
00721 {
00722 if (_game_mode != GM_MENU) PositionMainToolbar(NULL);
00723 return true;
00724 }
00725
00727 static bool v_PositionStatusbar(int32 p1)
00728 {
00729 if (_game_mode != GM_MENU) {
00730 PositionStatusbar(NULL);
00731 PositionNewsMessage(NULL);
00732 PositionNetworkChatWindow(NULL);
00733 }
00734 return true;
00735 }
00736
00737 static bool PopulationInLabelActive(int32 p1)
00738 {
00739 UpdateAllTownVirtCoords();
00740 return true;
00741 }
00742
00743 static bool RedrawScreen(int32 p1)
00744 {
00745 MarkWholeScreenDirty();
00746 return true;
00747 }
00748
00754 static bool RedrawSmallmap(int32 p1)
00755 {
00756 BuildLandLegend();
00757 BuildOwnerLegend();
00758 SetWindowClassesDirty(WC_SMALLMAP);
00759 return true;
00760 }
00761
00762 static bool InvalidateDetailsWindow(int32 p1)
00763 {
00764 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00765 return true;
00766 }
00767
00768 static bool InvalidateStationBuildWindow(int32 p1)
00769 {
00770 SetWindowDirty(WC_BUILD_STATION, 0);
00771 return true;
00772 }
00773
00774 static bool InvalidateBuildIndustryWindow(int32 p1)
00775 {
00776 InvalidateWindowData(WC_BUILD_INDUSTRY, 0);
00777 return true;
00778 }
00779
00780 static bool CloseSignalGUI(int32 p1)
00781 {
00782 if (p1 == 0) {
00783 DeleteWindowByClass(WC_BUILD_SIGNAL);
00784 }
00785 return true;
00786 }
00787
00788 static bool InvalidateTownViewWindow(int32 p1)
00789 {
00790 InvalidateWindowClassesData(WC_TOWN_VIEW, p1);
00791 return true;
00792 }
00793
00794 static bool DeleteSelectStationWindow(int32 p1)
00795 {
00796 DeleteWindowById(WC_SELECT_STATION, 0);
00797 return true;
00798 }
00799
00800 static bool UpdateConsists(int32 p1)
00801 {
00802 Train *t;
00803 FOR_ALL_TRAINS(t) {
00804
00805 if (t->IsFrontEngine() || t->IsFreeWagon()) t->ConsistChanged(true);
00806 }
00807 InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00808 return true;
00809 }
00810
00811
00812 static bool CheckInterval(int32 p1)
00813 {
00814 VehicleDefaultSettings *vds;
00815 if (_game_mode == GM_MENU || !Company::IsValidID(_current_company)) {
00816 vds = &_settings_client.company.vehicle;
00817 } else {
00818 vds = &Company::Get(_current_company)->settings.vehicle;
00819 }
00820
00821 if (p1 != 0) {
00822 vds->servint_trains = 50;
00823 vds->servint_roadveh = 50;
00824 vds->servint_aircraft = 50;
00825 vds->servint_ships = 50;
00826 } else {
00827 vds->servint_trains = 150;
00828 vds->servint_roadveh = 150;
00829 vds->servint_aircraft = 100;
00830 vds->servint_ships = 360;
00831 }
00832
00833 InvalidateDetailsWindow(0);
00834
00835 return true;
00836 }
00837
00838 static bool TrainAccelerationModelChanged(int32 p1)
00839 {
00840 Train *t;
00841 FOR_ALL_TRAINS(t) {
00842 if (t->IsFrontEngine()) {
00843 t->tcache.cached_max_curve_speed = t->GetCurveSpeedLimit();
00844 t->UpdateAcceleration();
00845 }
00846 }
00847
00848
00849 SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00850 InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00851 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00852
00853 return true;
00854 }
00855
00861 static bool TrainSlopeSteepnessChanged(int32 p1)
00862 {
00863 Train *t;
00864 FOR_ALL_TRAINS(t) {
00865 if (t->IsFrontEngine()) t->CargoChanged();
00866 }
00867
00868 return true;
00869 }
00870
00876 static bool RoadVehAccelerationModelChanged(int32 p1)
00877 {
00878 if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) {
00879 RoadVehicle *rv;
00880 FOR_ALL_ROADVEHICLES(rv) {
00881 if (rv->IsFrontEngine()) {
00882 rv->CargoChanged();
00883 }
00884 }
00885 }
00886
00887
00888 SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00889 InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00890 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00891
00892 return true;
00893 }
00894
00900 static bool RoadVehSlopeSteepnessChanged(int32 p1)
00901 {
00902 RoadVehicle *rv;
00903 FOR_ALL_ROADVEHICLES(rv) {
00904 if (rv->IsFrontEngine()) rv->CargoChanged();
00905 }
00906
00907 return true;
00908 }
00909
00910 static bool DragSignalsDensityChanged(int32)
00911 {
00912 InvalidateWindowData(WC_BUILD_SIGNAL, 0);
00913
00914 return true;
00915 }
00916
00917 static bool TownFoundingChanged(int32 p1)
00918 {
00919 if (_game_mode != GM_EDITOR && _settings_game.economy.found_town == TF_FORBIDDEN) {
00920 DeleteWindowById(WC_FOUND_TOWN, 0);
00921 return true;
00922 }
00923 InvalidateWindowData(WC_FOUND_TOWN, 0);
00924 return true;
00925 }
00926
00927 static bool InvalidateVehTimetableWindow(int32 p1)
00928 {
00929 InvalidateWindowClassesData(WC_VEHICLE_TIMETABLE, -2);
00930 return true;
00931 }
00932
00933 static bool ZoomMinMaxChanged(int32 p1)
00934 {
00935 extern void ConstrainAllViewportsZoom();
00936 ConstrainAllViewportsZoom();
00937 GfxClearSpriteCache();
00938 return true;
00939 }
00940
00948 static bool InvalidateNewGRFChangeWindows(int32 p1)
00949 {
00950 InvalidateWindowClassesData(WC_SAVELOAD);
00951 DeleteWindowByClass(WC_GAME_OPTIONS);
00952 ReInitAllWindows();
00953 return true;
00954 }
00955
00956 static bool InvalidateCompanyLiveryWindow(int32 p1)
00957 {
00958 InvalidateWindowClassesData(WC_COMPANY_COLOUR);
00959 return RedrawScreen(p1);
00960 }
00961
00962 static bool InvalidateIndustryViewWindow(int32 p1)
00963 {
00964 InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
00965 return true;
00966 }
00967
00968 static bool InvalidateAISettingsWindow(int32 p1)
00969 {
00970 InvalidateWindowClassesData(WC_AI_SETTINGS);
00971 return true;
00972 }
00973
00979 static bool RedrawTownAuthority(int32 p1)
00980 {
00981 SetWindowClassesDirty(WC_TOWN_AUTHORITY);
00982 return true;
00983 }
00984
00990 static bool InvalidateCompanyInfrastructureWindow(int32 p1)
00991 {
00992 InvalidateWindowClassesData(WC_COMPANY_INFRASTRUCTURE);
00993 return true;
00994 }
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017 static const DifficultySettings _default_game_diff[3] = {
01018
01019 {2, 2, 4, 300000, 2, 0, 2, 1, 2, 0, 1, 0, 0, 0, 0, 0, 0},
01020 {4, 2, 3, 150000, 3, 1, 3, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1},
01021 {7, 3, 3, 100000, 4, 1, 3, 2, 0, 2, 3, 2, 1, 1, 1, 2, 2},
01022 };
01023
01024 void SetDifficultyLevel(int mode, DifficultySettings *gm_opt)
01025 {
01026 assert(mode <= 3);
01027
01028 if (mode != 3) {
01029 *gm_opt = _default_game_diff[mode];
01030 } else {
01031 gm_opt->diff_level = 3;
01032 }
01033 }
01034
01036 static void ValidateSettings()
01037 {
01038
01039 if (_settings_newgame.difficulty.diff_level != 3) {
01040 SetDifficultyLevel(_settings_newgame.difficulty.diff_level, &_settings_newgame.difficulty);
01041 }
01042
01043
01044 if (_settings_newgame.game_creation.land_generator == 0 &&
01045 _settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
01046 _settings_newgame.difficulty.quantity_sea_lakes = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE;
01047 }
01048 }
01049
01050 static bool DifficultyReset(int32 level)
01051 {
01052
01053
01054 if (_game_mode != GM_MENU && level != 3) return false;
01055 SetDifficultyLevel(level, &GetGameSettings().difficulty);
01056 return true;
01057 }
01058
01059 static bool DifficultyChange(int32)
01060 {
01061 if (_game_mode == GM_MENU) {
01062 if (_settings_newgame.difficulty.diff_level != 3) {
01063 ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, WL_WARNING);
01064 _settings_newgame.difficulty.diff_level = 3;
01065 }
01066 SetWindowClassesDirty(WC_SELECT_GAME);
01067 } else {
01068 _settings_game.difficulty.diff_level = 3;
01069 }
01070
01071
01072
01073
01074 if (_networking) InvalidateWindowClassesData(WC_GAME_OPTIONS, GOID_DIFFICULTY_CHANGED);
01075
01076 return true;
01077 }
01078
01079 static bool DifficultyNoiseChange(int32 i)
01080 {
01081 if (_game_mode == GM_NORMAL) {
01082 UpdateAirportsNoise();
01083 if (_settings_game.economy.station_noise_level) {
01084 InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
01085 }
01086 }
01087
01088 return DifficultyChange(i);
01089 }
01090
01091 static bool MaxNoAIsChange(int32 i)
01092 {
01093 if (GetGameSettings().difficulty.max_no_competitors != 0 &&
01094 AI::GetInfoList()->size() == 0 &&
01095 (!_networking || _network_server)) {
01096 ShowErrorMessage(STR_WARNING_NO_SUITABLE_AI, INVALID_STRING_ID, WL_CRITICAL);
01097 }
01098
01099 return DifficultyChange(i);
01100 }
01101
01107 static bool CheckRoadSide(int p1)
01108 {
01109 extern bool RoadVehiclesAreBuilt();
01110 return _game_mode == GM_MENU || !RoadVehiclesAreBuilt();
01111 }
01112
01120 static size_t ConvertLandscape(const char *value)
01121 {
01122
01123 return LookupOneOfMany("normal|hilly|desert|candy", value);
01124 }
01125
01126 static bool CheckFreeformEdges(int32 p1)
01127 {
01128 if (_game_mode == GM_MENU) return true;
01129 if (p1 != 0) {
01130 Ship *s;
01131 FOR_ALL_SHIPS(s) {
01132
01133 if (TileX(s->tile) == 0 || TileY(s->tile) == 0) {
01134 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01135 return false;
01136 }
01137 }
01138 BaseStation *st;
01139 FOR_ALL_BASE_STATIONS(st) {
01140
01141 if (st->IsInUse() && (TileX(st->xy) == 0 || TileY(st->xy) == 0)) {
01142 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01143 return false;
01144 }
01145 }
01146 for (uint i = 0; i < MapSizeX(); i++) MakeVoid(TileXY(i, 0));
01147 for (uint i = 0; i < MapSizeY(); i++) MakeVoid(TileXY(0, i));
01148 } else {
01149 for (uint i = 0; i < MapMaxX(); i++) {
01150 if (TileHeight(TileXY(i, 1)) != 0) {
01151 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01152 return false;
01153 }
01154 }
01155 for (uint i = 1; i < MapMaxX(); i++) {
01156 if (!IsTileType(TileXY(i, MapMaxY() - 1), MP_WATER) || TileHeight(TileXY(1, MapMaxY())) != 0) {
01157 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01158 return false;
01159 }
01160 }
01161 for (uint i = 0; i < MapMaxY(); i++) {
01162 if (TileHeight(TileXY(1, i)) != 0) {
01163 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01164 return false;
01165 }
01166 }
01167 for (uint i = 1; i < MapMaxY(); i++) {
01168 if (!IsTileType(TileXY(MapMaxX() - 1, i), MP_WATER) || TileHeight(TileXY(MapMaxX(), i)) != 0) {
01169 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01170 return false;
01171 }
01172 }
01173
01174 for (uint i = 0; i < MapMaxX(); i++) {
01175 SetTileHeight(TileXY(i, 0), 0);
01176 SetTileType(TileXY(i, 0), MP_WATER);
01177 }
01178 for (uint i = 0; i < MapMaxY(); i++) {
01179 SetTileHeight(TileXY(0, i), 0);
01180 SetTileType(TileXY(0, i), MP_WATER);
01181 }
01182 }
01183 MarkWholeScreenDirty();
01184 return true;
01185 }
01186
01191 static bool ChangeDynamicEngines(int32 p1)
01192 {
01193 if (_game_mode == GM_MENU) return true;
01194
01195 if (!EngineOverrideManager::ResetToCurrentNewGRFConfig()) {
01196 ShowErrorMessage(STR_CONFIG_SETTING_DYNAMIC_ENGINES_EXISTING_VEHICLES, INVALID_STRING_ID, WL_ERROR);
01197 return false;
01198 }
01199
01200 return true;
01201 }
01202
01203 static bool StationCatchmentChanged(int32 p1)
01204 {
01205 Station::RecomputeIndustriesNearForAll();
01206 return true;
01207 }
01208
01209
01210 #ifdef ENABLE_NETWORK
01211
01212 static bool UpdateClientName(int32 p1)
01213 {
01214 NetworkUpdateClientName();
01215 return true;
01216 }
01217
01218 static bool UpdateServerPassword(int32 p1)
01219 {
01220 if (strcmp(_settings_client.network.server_password, "*") == 0) {
01221 _settings_client.network.server_password[0] = '\0';
01222 }
01223
01224 return true;
01225 }
01226
01227 static bool UpdateRconPassword(int32 p1)
01228 {
01229 if (strcmp(_settings_client.network.rcon_password, "*") == 0) {
01230 _settings_client.network.rcon_password[0] = '\0';
01231 }
01232
01233 return true;
01234 }
01235
01236 static bool UpdateClientConfigValues(int32 p1)
01237 {
01238 if (_network_server) NetworkServerSendConfigUpdate();
01239
01240 return true;
01241 }
01242
01243 #endif
01244
01245
01246
01247
01251 static void PrepareOldDiffCustom()
01252 {
01253 memset(_old_diff_custom, 0, sizeof(_old_diff_custom));
01254 }
01255
01262 static void HandleOldDiffCustom(bool savegame)
01263 {
01264 uint options_to_load = GAME_DIFFICULTY_NUM - ((savegame && IsSavegameVersionBefore(4)) ? 1 : 0);
01265
01266 if (!savegame) {
01267
01268 bool old_diff_custom_used = false;
01269 for (uint i = 0; i < options_to_load && !old_diff_custom_used; i++) {
01270 old_diff_custom_used = (_old_diff_custom[i] != 0);
01271 }
01272
01273 if (!old_diff_custom_used) return;
01274 }
01275
01276 for (uint i = 0; i < options_to_load; i++) {
01277 const SettingDesc *sd = &_settings[i];
01278
01279 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01280 void *var = GetVariableAddress(savegame ? &_settings_game : &_settings_newgame, &sd->save);
01281 Write_ValidateSetting(var, sd, (int32)((i == 4 ? 1000 : 1) * _old_diff_custom[i]));
01282 }
01283 }
01284
01291 static bool ConvertOldNewsSetting(const char *name, const char *value)
01292 {
01293 if (strcasecmp(name, "openclose") == 0) {
01294
01295
01296
01297
01298 NewsDisplay display = ND_OFF;
01299 if (strcasecmp(value, "full") == 0) {
01300 display = ND_FULL;
01301 } else if (strcasecmp(value, "summarized") == 0) {
01302 display = ND_SUMMARY;
01303 }
01304
01305 _news_type_data[NT_INDUSTRY_OPEN].display = display;
01306 _news_type_data[NT_INDUSTRY_CLOSE].display = display;
01307 return true;
01308 }
01309 return false;
01310 }
01311
01317 static void NewsDisplayLoadConfig(IniFile *ini, const char *grpname)
01318 {
01319 IniGroup *group = ini->GetGroup(grpname);
01320 IniItem *item;
01321
01322
01323 if (group == NULL) return;
01324
01325 for (item = group->item; item != NULL; item = item->next) {
01326 int news_item = -1;
01327 for (int i = 0; i < NT_END; i++) {
01328 if (strcasecmp(item->name, _news_type_data[i].name) == 0) {
01329 news_item = i;
01330 break;
01331 }
01332 }
01333
01334
01335 if (news_item == -1) {
01336
01337 if (!ConvertOldNewsSetting(item->name, item->value)) {
01338 DEBUG(misc, 0, "Invalid display option: %s", item->name);
01339 }
01340
01341 continue;
01342 }
01343
01344 if (StrEmpty(item->value)) {
01345 DEBUG(misc, 0, "Empty display value for newstype %s", item->name);
01346 continue;
01347 } else if (strcasecmp(item->value, "full") == 0) {
01348 _news_type_data[news_item].display = ND_FULL;
01349 } else if (strcasecmp(item->value, "off") == 0) {
01350 _news_type_data[news_item].display = ND_OFF;
01351 } else if (strcasecmp(item->value, "summarized") == 0) {
01352 _news_type_data[news_item].display = ND_SUMMARY;
01353 } else {
01354 DEBUG(misc, 0, "Invalid display value for newstype %s: %s", item->name, item->value);
01355 continue;
01356 }
01357 }
01358 }
01359
01360 static void AILoadConfig(IniFile *ini, const char *grpname)
01361 {
01362 IniGroup *group = ini->GetGroup(grpname);
01363 IniItem *item;
01364
01365
01366 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01367 AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME)->Change(NULL);
01368 }
01369
01370
01371 if (group == NULL) return;
01372
01373 CompanyID c = COMPANY_FIRST;
01374 for (item = group->item; c < MAX_COMPANIES && item != NULL; c++, item = item->next) {
01375 AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
01376
01377 config->Change(item->name);
01378 if (!config->HasScript()) {
01379 if (strcmp(item->name, "none") != 0) {
01380 DEBUG(script, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name);
01381 continue;
01382 }
01383 }
01384 if (item->value != NULL) config->StringToSettings(item->value);
01385 }
01386 }
01387
01388 static void GameLoadConfig(IniFile *ini, const char *grpname)
01389 {
01390 IniGroup *group = ini->GetGroup(grpname);
01391 IniItem *item;
01392
01393
01394 GameConfig::GetConfig(GameConfig::SSS_FORCE_NEWGAME)->Change(NULL);
01395
01396
01397 if (group == NULL) return;
01398
01399 item = group->item;
01400 if (item == NULL) return;
01401
01402 GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
01403
01404 config->Change(item->name);
01405 if (!config->HasScript()) {
01406 if (strcmp(item->name, "none") != 0) {
01407 DEBUG(script, 0, "The GameScript by the name '%s' was no longer found, and removed from the list.", item->name);
01408 return;
01409 }
01410 }
01411 if (item->value != NULL) config->StringToSettings(item->value);
01412 }
01413
01420 static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_static)
01421 {
01422 IniGroup *group = ini->GetGroup(grpname);
01423 IniItem *item;
01424 GRFConfig *first = NULL;
01425 GRFConfig **curr = &first;
01426
01427 if (group == NULL) return NULL;
01428
01429 for (item = group->item; item != NULL; item = item->next) {
01430 GRFConfig *c = new GRFConfig(item->name);
01431
01432
01433 if (!StrEmpty(item->value)) {
01434 c->num_params = ParseIntList(item->value, (int*)c->param, lengthof(c->param));
01435 if (c->num_params == (byte)-1) {
01436 SetDParamStr(0, item->name);
01437 ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY, WL_CRITICAL);
01438 c->num_params = 0;
01439 }
01440 }
01441
01442
01443 if (!FillGRFDetails(c, is_static) || HasBit(c->flags, GCF_INVALID)) {
01444 if (c->status == GCS_NOT_FOUND) {
01445 SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_NOT_FOUND);
01446 } else if (HasBit(c->flags, GCF_UNSAFE)) {
01447 SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNSAFE);
01448 } else if (HasBit(c->flags, GCF_SYSTEM)) {
01449 SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_SYSTEM);
01450 } else if (HasBit(c->flags, GCF_INVALID)) {
01451 SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_INCOMPATIBLE);
01452 } else {
01453 SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNKNOWN);
01454 }
01455
01456 SetDParamStr(0, item->name);
01457 ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_GRF, WL_CRITICAL);
01458 delete c;
01459 continue;
01460 }
01461
01462
01463 bool duplicate = false;
01464 for (const GRFConfig *gc = first; gc != NULL; gc = gc->next) {
01465 if (gc->ident.grfid == c->ident.grfid) {
01466 SetDParamStr(0, item->name);
01467 SetDParamStr(1, gc->filename);
01468 ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_DUPLICATE_GRFID, WL_CRITICAL);
01469 duplicate = true;
01470 break;
01471 }
01472 }
01473 if (duplicate) {
01474 delete c;
01475 continue;
01476 }
01477
01478
01479 if (is_static) SetBit(c->flags, GCF_STATIC);
01480
01481
01482 *curr = c;
01483 curr = &c->next;
01484 }
01485
01486 return first;
01487 }
01488
01494 static void NewsDisplaySaveConfig(IniFile *ini, const char *grpname)
01495 {
01496 IniGroup *group = ini->GetGroup(grpname);
01497
01498 for (int i = 0; i < NT_END; i++) {
01499 const char *value;
01500 int v = _news_type_data[i].display;
01501
01502 value = (v == ND_OFF ? "off" : (v == ND_SUMMARY ? "summarized" : "full"));
01503
01504 group->GetItem(_news_type_data[i].name, true)->SetValue(value);
01505 }
01506 }
01507
01508 static void AISaveConfig(IniFile *ini, const char *grpname)
01509 {
01510 IniGroup *group = ini->GetGroup(grpname);
01511
01512 if (group == NULL) return;
01513 group->Clear();
01514
01515 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01516 AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
01517 const char *name;
01518 char value[1024];
01519 config->SettingsToString(value, lengthof(value));
01520
01521 if (config->HasScript()) {
01522 name = config->GetName();
01523 } else {
01524 name = "none";
01525 }
01526
01527 IniItem *item = new IniItem(group, name, strlen(name));
01528 item->SetValue(value);
01529 }
01530 }
01531
01532 static void GameSaveConfig(IniFile *ini, const char *grpname)
01533 {
01534 IniGroup *group = ini->GetGroup(grpname);
01535
01536 if (group == NULL) return;
01537 group->Clear();
01538
01539 GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
01540 const char *name;
01541 char value[1024];
01542 config->SettingsToString(value, lengthof(value));
01543
01544 if (config->HasScript()) {
01545 name = config->GetName();
01546 } else {
01547 name = "none";
01548 }
01549
01550 IniItem *item = new IniItem(group, name, strlen(name));
01551 item->SetValue(value);
01552 }
01553
01558 static void SaveVersionInConfig(IniFile *ini)
01559 {
01560 IniGroup *group = ini->GetGroup("version");
01561
01562 char version[9];
01563 snprintf(version, lengthof(version), "%08X", _openttd_newgrf_version);
01564
01565 const char * const versions[][2] = {
01566 { "version_string", _openttd_revision },
01567 { "version_number", version }
01568 };
01569
01570 for (uint i = 0; i < lengthof(versions); i++) {
01571 group->GetItem(versions[i][0], true)->SetValue(versions[i][1]);
01572 }
01573 }
01574
01575
01576 static void GRFSaveConfig(IniFile *ini, const char *grpname, const GRFConfig *list)
01577 {
01578 ini->RemoveGroup(grpname);
01579 IniGroup *group = ini->GetGroup(grpname);
01580 const GRFConfig *c;
01581
01582 for (c = list; c != NULL; c = c->next) {
01583 char params[512];
01584 GRFBuildParamList(params, c, lastof(params));
01585
01586 group->GetItem(c->filename, true)->SetValue(params);
01587 }
01588 }
01589
01590
01591 static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list, bool basic_settings = true, bool other_settings = true)
01592 {
01593 if (basic_settings) {
01594 proc(ini, (const SettingDesc*)_misc_settings, "misc", NULL);
01595 #if defined(WIN32) && !defined(DEDICATED)
01596 proc(ini, (const SettingDesc*)_win32_settings, "win32", NULL);
01597 #endif
01598 }
01599
01600 if (other_settings) {
01601 proc(ini, _settings, "patches", &_settings_newgame);
01602 proc(ini, _currency_settings,"currency", &_custom_currency);
01603 proc(ini, _company_settings, "company", &_settings_client.company);
01604
01605 #ifdef ENABLE_NETWORK
01606 proc_list(ini, "server_bind_addresses", &_network_bind_list);
01607 proc_list(ini, "servers", &_network_host_list);
01608 proc_list(ini, "bans", &_network_ban_list);
01609 #endif
01610 }
01611 }
01612
01613 static IniFile *IniLoadConfig()
01614 {
01615 IniFile *ini = new IniFile(_list_group_names);
01616 ini->LoadFromDisk(_config_file, BASE_DIR);
01617 return ini;
01618 }
01619
01624 void LoadFromConfig(bool minimal)
01625 {
01626 IniFile *ini = IniLoadConfig();
01627 if (!minimal) ResetCurrencies(false);
01628
01629
01630 HandleSettingDescs(ini, IniLoadSettings, IniLoadSettingList, minimal, !minimal);
01631
01632 if (!minimal) {
01633 _grfconfig_newgame = GRFLoadConfig(ini, "newgrf", false);
01634 _grfconfig_static = GRFLoadConfig(ini, "newgrf-static", true);
01635 NewsDisplayLoadConfig(ini, "news_display");
01636 AILoadConfig(ini, "ai_players");
01637 GameLoadConfig(ini, "game_scripts");
01638
01639 PrepareOldDiffCustom();
01640 IniLoadSettings(ini, _gameopt_settings, "gameopt", &_settings_newgame);
01641 HandleOldDiffCustom(false);
01642
01643 ValidateSettings();
01644
01645
01646 extern void ScheduleErrorMessage(ErrorList &datas);
01647 ScheduleErrorMessage(_settings_error_list);
01648 if (FindWindowById(WC_ERRMSG, 0) == NULL) ShowFirstError();
01649 }
01650
01651 delete ini;
01652 }
01653
01655 void SaveToConfig()
01656 {
01657 IniFile *ini = IniLoadConfig();
01658
01659
01660 ini->RemoveGroup("patches");
01661 ini->RemoveGroup("yapf");
01662 ini->RemoveGroup("gameopt");
01663
01664 HandleSettingDescs(ini, IniSaveSettings, IniSaveSettingList);
01665 GRFSaveConfig(ini, "newgrf", _grfconfig_newgame);
01666 GRFSaveConfig(ini, "newgrf-static", _grfconfig_static);
01667 NewsDisplaySaveConfig(ini, "news_display");
01668 AISaveConfig(ini, "ai_players");
01669 GameSaveConfig(ini, "game_scripts");
01670 SaveVersionInConfig(ini);
01671 ini->SaveToDisk(_config_file);
01672 delete ini;
01673 }
01674
01679 void GetGRFPresetList(GRFPresetList *list)
01680 {
01681 list->Clear();
01682
01683 IniFile *ini = IniLoadConfig();
01684 IniGroup *group;
01685 for (group = ini->group; group != NULL; group = group->next) {
01686 if (strncmp(group->name, "preset-", 7) == 0) {
01687 *list->Append() = strdup(group->name + 7);
01688 }
01689 }
01690
01691 delete ini;
01692 }
01693
01700 GRFConfig *LoadGRFPresetFromConfig(const char *config_name)
01701 {
01702 char *section = (char*)alloca(strlen(config_name) + 8);
01703 sprintf(section, "preset-%s", config_name);
01704
01705 IniFile *ini = IniLoadConfig();
01706 GRFConfig *config = GRFLoadConfig(ini, section, false);
01707 delete ini;
01708
01709 return config;
01710 }
01711
01718 void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
01719 {
01720 char *section = (char*)alloca(strlen(config_name) + 8);
01721 sprintf(section, "preset-%s", config_name);
01722
01723 IniFile *ini = IniLoadConfig();
01724 GRFSaveConfig(ini, section, config);
01725 ini->SaveToDisk(_config_file);
01726 delete ini;
01727 }
01728
01733 void DeleteGRFPresetFromConfig(const char *config_name)
01734 {
01735 char *section = (char*)alloca(strlen(config_name) + 8);
01736 sprintf(section, "preset-%s", config_name);
01737
01738 IniFile *ini = IniLoadConfig();
01739 ini->RemoveGroup(section);
01740 ini->SaveToDisk(_config_file);
01741 delete ini;
01742 }
01743
01744 static const SettingDesc *GetSettingDescription(uint index)
01745 {
01746 if (index >= lengthof(_settings)) return NULL;
01747 return &_settings[index];
01748 }
01749
01761 CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01762 {
01763 const SettingDesc *sd = GetSettingDescription(p1);
01764
01765 if (sd == NULL) return CMD_ERROR;
01766 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR;
01767
01768 if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return CMD_ERROR;
01769 if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return CMD_ERROR;
01770 if ((sd->desc.flags & SGF_NEWGAME_ONLY) &&
01771 (_game_mode == GM_NORMAL ||
01772 (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0))) {
01773 return CMD_ERROR;
01774 }
01775
01776 if (flags & DC_EXEC) {
01777 void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01778
01779 int32 oldval = (int32)ReadValue(var, sd->save.conv);
01780 int32 newval = (int32)p2;
01781
01782 Write_ValidateSetting(var, sd, newval);
01783 newval = (int32)ReadValue(var, sd->save.conv);
01784
01785 if (oldval == newval) return CommandCost();
01786
01787 if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01788 WriteValue(var, sd->save.conv, (int64)oldval);
01789 return CommandCost();
01790 }
01791
01792 if (sd->desc.flags & SGF_NO_NETWORK) {
01793 GamelogStartAction(GLAT_SETTING);
01794 GamelogSetting(sd->desc.name, oldval, newval);
01795 GamelogStopAction();
01796 }
01797
01798 SetWindowClassesDirty(WC_GAME_OPTIONS);
01799 }
01800
01801 return CommandCost();
01802 }
01803
01814 CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01815 {
01816 if (p1 >= lengthof(_company_settings)) return CMD_ERROR;
01817 const SettingDesc *sd = &_company_settings[p1];
01818
01819 if (flags & DC_EXEC) {
01820 void *var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01821
01822 int32 oldval = (int32)ReadValue(var, sd->save.conv);
01823 int32 newval = (int32)p2;
01824
01825 Write_ValidateSetting(var, sd, newval);
01826 newval = (int32)ReadValue(var, sd->save.conv);
01827
01828 if (oldval == newval) return CommandCost();
01829
01830 if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01831 WriteValue(var, sd->save.conv, (int64)oldval);
01832 return CommandCost();
01833 }
01834
01835 SetWindowClassesDirty(WC_GAME_OPTIONS);
01836 }
01837
01838 return CommandCost();
01839 }
01840
01848 bool SetSettingValue(uint index, int32 value, bool force_newgame)
01849 {
01850 const SettingDesc *sd = &_settings[index];
01851
01852
01853
01854
01855 if (sd->save.conv & SLF_NO_NETWORK_SYNC) {
01856 void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01857 Write_ValidateSetting(var, sd, value);
01858
01859 if (_game_mode != GM_MENU) {
01860 void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01861 Write_ValidateSetting(var2, sd, value);
01862 }
01863 if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01864
01865 SetWindowClassesDirty(WC_GAME_OPTIONS);
01866
01867 return true;
01868 }
01869
01870 if (force_newgame) {
01871 void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01872 Write_ValidateSetting(var2, sd, value);
01873 return true;
01874 }
01875
01876
01877 if (!_networking || (_networking && _network_server)) {
01878 return DoCommandP(0, index, value, CMD_CHANGE_SETTING);
01879 }
01880 return false;
01881 }
01882
01889 void SetCompanySetting(uint index, int32 value)
01890 {
01891 const SettingDesc *sd = &_company_settings[index];
01892 if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01893 DoCommandP(0, index, value, CMD_CHANGE_COMPANY_SETTING);
01894 } else {
01895 void *var = GetVariableAddress(&_settings_client.company, &sd->save);
01896 Write_ValidateSetting(var, sd, value);
01897 if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01898 }
01899 }
01900
01904 void SetDefaultCompanySettings(CompanyID cid)
01905 {
01906 Company *c = Company::Get(cid);
01907 const SettingDesc *sd;
01908 for (sd = _company_settings; sd->save.cmd != SL_END; sd++) {
01909 void *var = GetVariableAddress(&c->settings, &sd->save);
01910 Write_ValidateSetting(var, sd, (int32)(size_t)sd->desc.def);
01911 }
01912 }
01913
01914 #if defined(ENABLE_NETWORK)
01915
01918 void SyncCompanySettings()
01919 {
01920 const SettingDesc *sd;
01921 uint i = 0;
01922 for (sd = _company_settings; sd->save.cmd != SL_END; sd++, i++) {
01923 const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01924 const void *new_var = GetVariableAddress(&_settings_client.company, &sd->save);
01925 uint32 old_value = (uint32)ReadValue(old_var, sd->save.conv);
01926 uint32 new_value = (uint32)ReadValue(new_var, sd->save.conv);
01927 if (old_value != new_value) NetworkSendCommand(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, NULL, NULL, _local_company);
01928 }
01929 }
01930 #endif
01931
01937 uint GetCompanySettingIndex(const char *name)
01938 {
01939 uint i;
01940 const SettingDesc *sd = GetSettingFromName(name, &i);
01941 assert(sd != NULL && (sd->desc.flags & SGF_PER_COMPANY) != 0);
01942 return i;
01943 }
01944
01952 bool SetSettingValue(uint index, const char *value, bool force_newgame)
01953 {
01954 const SettingDesc *sd = &_settings[index];
01955 assert(sd->save.conv & SLF_NO_NETWORK_SYNC);
01956
01957 if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) {
01958 char **var = (char**)GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
01959 free(*var);
01960 *var = strcmp(value, "(null)") == 0 ? NULL : strdup(value);
01961 } else {
01962 char *var = (char*)GetVariableAddress(NULL, &sd->save);
01963 ttd_strlcpy(var, value, sd->save.length);
01964 }
01965 if (sd->desc.proc != NULL) sd->desc.proc(0);
01966
01967 return true;
01968 }
01969
01977 const SettingDesc *GetSettingFromName(const char *name, uint *i)
01978 {
01979 const SettingDesc *sd;
01980
01981
01982 for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01983 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01984 if (strcmp(sd->desc.name, name) == 0) return sd;
01985 }
01986
01987
01988 for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01989 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01990 const char *short_name = strchr(sd->desc.name, '.');
01991 if (short_name != NULL) {
01992 short_name++;
01993 if (strcmp(short_name, name) == 0) return sd;
01994 }
01995 }
01996
01997 if (strncmp(name, "company.", 8) == 0) name += 8;
01998
01999 for (*i = 0, sd = _company_settings; sd->save.cmd != SL_END; sd++, (*i)++) {
02000 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
02001 if (strcmp(sd->desc.name, name) == 0) return sd;
02002 }
02003
02004 return NULL;
02005 }
02006
02007
02008
02009 void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
02010 {
02011 uint index;
02012 const SettingDesc *sd = GetSettingFromName(name, &index);
02013
02014 if (sd == NULL) {
02015 IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
02016 return;
02017 }
02018
02019 bool success;
02020 if (sd->desc.cmd == SDT_STRING) {
02021 success = SetSettingValue(index, value, force_newgame);
02022 } else {
02023 uint32 val;
02024 extern bool GetArgumentInteger(uint32 *value, const char *arg);
02025 success = GetArgumentInteger(&val, value);
02026 if (!success) {
02027 IConsolePrintF(CC_ERROR, "'%s' is not an integer.", value);
02028 return;
02029 }
02030
02031 success = SetSettingValue(index, val, force_newgame);
02032 }
02033
02034 if (!success) {
02035 if (_network_server) {
02036 IConsoleError("This command/variable is not available during network games.");
02037 } else {
02038 IConsoleError("This command/variable is only available to a network server.");
02039 }
02040 }
02041 }
02042
02043 void IConsoleSetSetting(const char *name, int value)
02044 {
02045 uint index;
02046 const SettingDesc *sd = GetSettingFromName(name, &index);
02047 assert(sd != NULL);
02048 SetSettingValue(index, value);
02049 }
02050
02056 void IConsoleGetSetting(const char *name, bool force_newgame)
02057 {
02058 char value[20];
02059 uint index;
02060 const SettingDesc *sd = GetSettingFromName(name, &index);
02061 const void *ptr;
02062
02063 if (sd == NULL) {
02064 IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
02065 return;
02066 }
02067
02068 ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
02069
02070 if (sd->desc.cmd == SDT_STRING) {
02071 IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char * const *)ptr : (const char *)ptr);
02072 } else {
02073 if (sd->desc.cmd == SDT_BOOLX) {
02074 snprintf(value, sizeof(value), (*(const bool*)ptr != 0) ? "on" : "off");
02075 } else {
02076 snprintf(value, sizeof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
02077 }
02078
02079 IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s' (min: %s%d, max: %u)",
02080 name, value, (sd->desc.flags & SGF_0ISDISABLED) ? "(0) " : "", sd->desc.min, sd->desc.max);
02081 }
02082 }
02083
02089 void IConsoleListSettings(const char *prefilter)
02090 {
02091 IConsolePrintF(CC_WARNING, "All settings with their current value:");
02092
02093 for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) {
02094 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
02095 if (prefilter != NULL && strstr(sd->desc.name, prefilter) == NULL) continue;
02096 char value[80];
02097 const void *ptr = GetVariableAddress(&GetGameSettings(), &sd->save);
02098
02099 if (sd->desc.cmd == SDT_BOOLX) {
02100 snprintf(value, lengthof(value), (*(const bool *)ptr != 0) ? "on" : "off");
02101 } else if (sd->desc.cmd == SDT_STRING) {
02102 snprintf(value, sizeof(value), "%s", (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char * const *)ptr : (const char *)ptr);
02103 } else {
02104 snprintf(value, lengthof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
02105 }
02106 IConsolePrintF(CC_DEFAULT, "%s = %s", sd->desc.name, value);
02107 }
02108
02109 IConsolePrintF(CC_WARNING, "Use 'setting' command to change a value");
02110 }
02111
02118 static void LoadSettings(const SettingDesc *osd, void *object)
02119 {
02120 for (; osd->save.cmd != SL_END; osd++) {
02121 const SaveLoad *sld = &osd->save;
02122 void *ptr = GetVariableAddress(object, sld);
02123
02124 if (!SlObjectMember(ptr, sld)) continue;
02125 if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, osd, ReadValue(ptr, sld->conv));
02126 }
02127 }
02128
02135 static void SaveSettings(const SettingDesc *sd, void *object)
02136 {
02137
02138
02139 const SettingDesc *i;
02140 size_t length = 0;
02141 for (i = sd; i->save.cmd != SL_END; i++) {
02142 length += SlCalcObjMemberLength(object, &i->save);
02143 }
02144 SlSetLength(length);
02145
02146 for (i = sd; i->save.cmd != SL_END; i++) {
02147 void *ptr = GetVariableAddress(object, &i->save);
02148 SlObjectMember(ptr, &i->save);
02149 }
02150 }
02151
02152 static void Load_OPTS()
02153 {
02154
02155
02156
02157 PrepareOldDiffCustom();
02158 LoadSettings(_gameopt_settings, &_settings_game);
02159 HandleOldDiffCustom(true);
02160 }
02161
02162 static void Load_PATS()
02163 {
02164
02165
02166
02167 LoadSettings(_settings, &_settings_game);
02168 }
02169
02170 static void Check_PATS()
02171 {
02172 LoadSettings(_settings, &_load_check_data.settings);
02173 }
02174
02175 static void Save_PATS()
02176 {
02177 SaveSettings(_settings, &_settings_game);
02178 }
02179
02180 void CheckConfig()
02181 {
02182
02183
02184
02185
02186 if (_settings_newgame.pf.opf.pf_maxdepth == 16 && _settings_newgame.pf.opf.pf_maxlength == 512) {
02187 _settings_newgame.pf.opf.pf_maxdepth = 48;
02188 _settings_newgame.pf.opf.pf_maxlength = 4096;
02189 }
02190 }
02191
02192 extern const ChunkHandler _setting_chunk_handlers[] = {
02193 { 'OPTS', NULL, Load_OPTS, NULL, NULL, CH_RIFF},
02194 { 'PATS', Save_PATS, Load_PATS, NULL, Check_PATS, CH_RIFF | CH_LAST},
02195 };
02196
02197 static bool IsSignedVarMemType(VarType vt)
02198 {
02199 switch (GetVarMemType(vt)) {
02200 case SLE_VAR_I8:
02201 case SLE_VAR_I16:
02202 case SLE_VAR_I32:
02203 case SLE_VAR_I64:
02204 return true;
02205 }
02206 return false;
02207 }