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