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