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 "gui.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 "ship.h"
00061 #include "smallmap_gui.h"
00062 #include "roadveh.h"
00063 #include "fios.h"
00064
00065 #include "void_map.h"
00066 #include "station_base.h"
00067
00068 #include "table/strings.h"
00069 #include "table/settings.h"
00070
00071 ClientSettings _settings_client;
00072 GameSettings _settings_game;
00073 GameSettings _settings_newgame;
00074 VehicleDefaultSettings _old_vds;
00075 char *_config_file;
00076
00077 typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object);
00078 typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList *list);
00079
00080 static bool IsSignedVarMemType(VarType vt);
00081
00085 static const char * const _list_group_names[] = {
00086 "bans",
00087 "newgrf",
00088 "servers",
00089 "server_bind_addresses",
00090 NULL
00091 };
00092
00100 static int LookupOneOfMany(const char *many, const char *one, size_t onelen = 0)
00101 {
00102 const char *s;
00103 int idx;
00104
00105 if (onelen == 0) onelen = strlen(one);
00106
00107
00108 if (*one >= '0' && *one <= '9') return strtoul(one, NULL, 0);
00109
00110 idx = 0;
00111 for (;;) {
00112
00113 s = many;
00114 while (*s != '|' && *s != 0) s++;
00115 if ((size_t)(s - many) == onelen && !memcmp(one, many, onelen)) return idx;
00116 if (*s == 0) return -1;
00117 many = s + 1;
00118 idx++;
00119 }
00120 }
00121
00129 static uint32 LookupManyOfMany(const char *many, const char *str)
00130 {
00131 const char *s;
00132 int r;
00133 uint32 res = 0;
00134
00135 for (;;) {
00136
00137 while (*str == ' ' || *str == '\t' || *str == '|') str++;
00138 if (*str == 0) break;
00139
00140 s = str;
00141 while (*s != 0 && *s != ' ' && *s != '\t' && *s != '|') s++;
00142
00143 r = LookupOneOfMany(many, str, s - str);
00144 if (r == -1) return (uint32)-1;
00145
00146 SetBit(res, r);
00147 if (*s == 0) break;
00148 str = s + 1;
00149 }
00150 return res;
00151 }
00152
00161 static int ParseIntList(const char *p, int *items, int maxitems)
00162 {
00163 int n = 0;
00164 bool comma = false;
00165
00166 while (*p != '\0') {
00167 switch (*p) {
00168 case ',':
00169
00170 if (!comma) return -1;
00171 comma = false;
00172
00173 case ' ':
00174 p++;
00175 break;
00176
00177 default: {
00178 if (n == maxitems) return -1;
00179 char *end;
00180 long v = strtol(p, &end, 0);
00181 if (p == end) return -1;
00182 if (sizeof(int) < sizeof(long)) v = ClampToI32(v);
00183 items[n++] = v;
00184 p = end;
00185 comma = true;
00186 break;
00187 }
00188 }
00189 }
00190
00191
00192
00193 if (n != 0 && !comma) return -1;
00194
00195 return n;
00196 }
00197
00206 static bool LoadIntList(const char *str, void *array, int nelems, VarType type)
00207 {
00208 int items[64];
00209 int i, nitems;
00210
00211 if (str == NULL) {
00212 memset(items, 0, sizeof(items));
00213 nitems = nelems;
00214 } else {
00215 nitems = ParseIntList(str, items, lengthof(items));
00216 if (nitems != nelems) return false;
00217 }
00218
00219 switch (type) {
00220 case SLE_VAR_BL:
00221 case SLE_VAR_I8:
00222 case SLE_VAR_U8:
00223 for (i = 0; i != nitems; i++) ((byte*)array)[i] = items[i];
00224 break;
00225 case SLE_VAR_I16:
00226 case SLE_VAR_U16:
00227 for (i = 0; i != nitems; i++) ((uint16*)array)[i] = items[i];
00228 break;
00229 case SLE_VAR_I32:
00230 case SLE_VAR_U32:
00231 for (i = 0; i != nitems; i++) ((uint32*)array)[i] = items[i];
00232 break;
00233 default: NOT_REACHED();
00234 }
00235
00236 return true;
00237 }
00238
00248 static void MakeIntList(char *buf, const char *last, const void *array, int nelems, VarType type)
00249 {
00250 int i, v = 0;
00251 byte *p = (byte*)array;
00252
00253 for (i = 0; i != nelems; i++) {
00254 switch (type) {
00255 case SLE_VAR_BL:
00256 case SLE_VAR_I8: v = *(int8*)p; p += 1; break;
00257 case SLE_VAR_U8: v = *(byte*)p; p += 1; break;
00258 case SLE_VAR_I16: v = *(int16*)p; p += 2; break;
00259 case SLE_VAR_U16: v = *(uint16*)p; p += 2; break;
00260 case SLE_VAR_I32: v = *(int32*)p; p += 4; break;
00261 case SLE_VAR_U32: v = *(uint32*)p; p += 4; break;
00262 default: NOT_REACHED();
00263 }
00264 buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
00265 }
00266 }
00267
00275 static void MakeOneOfMany(char *buf, const char *last, const char *many, int id)
00276 {
00277 int orig_id = id;
00278
00279
00280 while (--id >= 0) {
00281 for (; *many != '|'; many++) {
00282 if (*many == '\0') {
00283 seprintf(buf, last, "%d", orig_id);
00284 return;
00285 }
00286 }
00287 many++;
00288 }
00289
00290
00291 while (*many != '\0' && *many != '|' && buf < last) *buf++ = *many++;
00292 *buf = '\0';
00293 }
00294
00303 static void MakeManyOfMany(char *buf, const char *last, const char *many, uint32 x)
00304 {
00305 const char *start;
00306 int i = 0;
00307 bool init = true;
00308
00309 for (; x != 0; x >>= 1, i++) {
00310 start = many;
00311 while (*many != 0 && *many != '|') many++;
00312
00313 if (HasBit(x, 0)) {
00314 if (!init) buf += seprintf(buf, last, "|");
00315 init = false;
00316 if (start == many) {
00317 buf += seprintf(buf, last, "%d", i);
00318 } else {
00319 memcpy(buf, start, many - start);
00320 buf += many - start;
00321 }
00322 }
00323
00324 if (*many == '|') many++;
00325 }
00326
00327 *buf = '\0';
00328 }
00329
00336 static const void *StringToVal(const SettingDescBase *desc, const char *orig_str)
00337 {
00338 const char *str = orig_str == NULL ? "" : orig_str;
00339 switch (desc->cmd) {
00340 case SDT_NUMX: {
00341 char *end;
00342 size_t val = strtoul(str, &end, 0);
00343 if (*end != '\0') ShowInfoF("ini: trailing characters at end of setting '%s'", desc->name);
00344 return (void*)val;
00345 }
00346 case SDT_ONEOFMANY: {
00347 size_t r = LookupOneOfMany(desc->many, str);
00348
00349
00350 if (r == (size_t)-1 && desc->proc_cnvt != NULL) r = desc->proc_cnvt(str);
00351 if (r != (size_t)-1) return (void*)r;
00352 ShowInfoF("ini: invalid value '%s' for '%s'", str, desc->name);
00353 return 0;
00354 }
00355 case SDT_MANYOFMANY: {
00356 size_t r = LookupManyOfMany(desc->many, str);
00357 if (r != (size_t)-1) return (void*)r;
00358 ShowInfoF("ini: invalid value '%s' for '%s'", str, desc->name);
00359 return NULL;
00360 }
00361 case SDT_BOOLX:
00362 if (strcmp(str, "true") == 0 || strcmp(str, "on") == 0 || strcmp(str, "1") == 0) return (void*)true;
00363 if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return (void*)false;
00364 ShowInfoF("ini: invalid setting value '%s' for '%s'", str, desc->name);
00365 break;
00366
00367 case SDT_STRING: return orig_str;
00368 case SDT_INTLIST: return str;
00369 default: break;
00370 }
00371
00372 return NULL;
00373 }
00374
00384 static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val)
00385 {
00386 const SettingDescBase *sdb = &sd->desc;
00387
00388 if (sdb->cmd != SDT_BOOLX &&
00389 sdb->cmd != SDT_NUMX &&
00390 sdb->cmd != SDT_ONEOFMANY &&
00391 sdb->cmd != SDT_MANYOFMANY) {
00392 return;
00393 }
00394
00395
00396 if (sdb->cmd != SDT_MANYOFMANY) {
00397
00398
00399
00400
00401
00402 switch (GetVarMemType(sd->save.conv)) {
00403 case SLE_VAR_NULL: return;
00404 case SLE_VAR_BL:
00405 case SLE_VAR_I8:
00406 case SLE_VAR_U8:
00407 case SLE_VAR_I16:
00408 case SLE_VAR_U16:
00409 case SLE_VAR_I32: {
00410
00411 if (!(sdb->flags & SGF_0ISDISABLED) || val != 0) val = Clamp(val, sdb->min, sdb->max);
00412 break;
00413 }
00414 case SLE_VAR_U32: {
00415
00416 uint min = ((sdb->flags & SGF_0ISDISABLED) && (uint)val <= (uint)sdb->min) ? 0 : sdb->min;
00417 WriteValue(ptr, SLE_VAR_U32, (int64)ClampU(val, min, sdb->max));
00418 return;
00419 }
00420 case SLE_VAR_I64:
00421 case SLE_VAR_U64:
00422 default: NOT_REACHED();
00423 }
00424 }
00425
00426 WriteValue(ptr, sd->save.conv, (int64)val);
00427 }
00428
00437 static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00438 {
00439 IniGroup *group;
00440 IniGroup *group_def = ini->GetGroup(grpname);
00441 IniItem *item;
00442 const void *p;
00443 void *ptr;
00444 const char *s;
00445
00446 for (; sd->save.cmd != SL_END; sd++) {
00447 const SettingDescBase *sdb = &sd->desc;
00448 const SaveLoad *sld = &sd->save;
00449
00450 if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00451
00452
00453 s = strchr(sdb->name, '.');
00454 if (s != NULL) {
00455 group = ini->GetGroup(sdb->name, s - sdb->name);
00456 s++;
00457 } else {
00458 s = sdb->name;
00459 group = group_def;
00460 }
00461
00462 item = group->GetItem(s, false);
00463 if (item == NULL && group != group_def) {
00464
00465
00466 item = group_def->GetItem(s, false);
00467 }
00468 if (item == NULL) {
00469
00470
00471 const char *sc = strchr(s, '.');
00472 if (sc != NULL) item = ini->GetGroup(s, sc - s)->GetItem(sc + 1, false);
00473 }
00474
00475 p = (item == NULL) ? sdb->def : StringToVal(sdb, item->value);
00476 ptr = GetVariableAddress(object, sld);
00477
00478 switch (sdb->cmd) {
00479 case SDT_BOOLX:
00480 case SDT_NUMX:
00481 case SDT_ONEOFMANY:
00482 case SDT_MANYOFMANY:
00483 Write_ValidateSetting(ptr, sd, (int32)(size_t)p); break;
00484
00485 case SDT_STRING:
00486 switch (GetVarMemType(sld->conv)) {
00487 case SLE_VAR_STRB:
00488 case SLE_VAR_STRBQ:
00489 if (p != NULL) ttd_strlcpy((char*)ptr, (const char*)p, sld->length);
00490 break;
00491 case SLE_VAR_STR:
00492 case SLE_VAR_STRQ:
00493 free(*(char**)ptr);
00494 *(char**)ptr = p == NULL ? NULL : strdup((const char*)p);
00495 break;
00496 case SLE_VAR_CHAR: if (p != NULL) *(char*)ptr = *(char*)p; break;
00497 default: NOT_REACHED();
00498 }
00499 break;
00500
00501 case SDT_INTLIST: {
00502 if (!LoadIntList((const char*)p, ptr, sld->length, GetVarMemType(sld->conv))) {
00503 ShowInfoF("ini: error in array '%s'", sdb->name);
00504 } else if (sd->desc.proc_cnvt != NULL) {
00505 sd->desc.proc_cnvt((const char*)p);
00506 }
00507 break;
00508 }
00509 default: NOT_REACHED();
00510 }
00511 }
00512 }
00513
00526 static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00527 {
00528 IniGroup *group_def = NULL, *group;
00529 IniItem *item;
00530 char buf[512];
00531 const char *s;
00532 void *ptr;
00533
00534 for (; sd->save.cmd != SL_END; sd++) {
00535 const SettingDescBase *sdb = &sd->desc;
00536 const SaveLoad *sld = &sd->save;
00537
00538
00539
00540 if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00541 if (sld->conv & SLF_NOT_IN_CONFIG) continue;
00542
00543
00544 s = strchr(sdb->name, '.');
00545 if (s != NULL) {
00546 group = ini->GetGroup(sdb->name, s - sdb->name);
00547 s++;
00548 } else {
00549 if (group_def == NULL) group_def = ini->GetGroup(grpname);
00550 s = sdb->name;
00551 group = group_def;
00552 }
00553
00554 item = group->GetItem(s, true);
00555 ptr = GetVariableAddress(object, sld);
00556
00557 if (item->value != NULL) {
00558
00559 const void *p = StringToVal(sdb, item->value);
00560
00561
00562
00563 switch (sdb->cmd) {
00564 case SDT_BOOLX:
00565 case SDT_NUMX:
00566 case SDT_ONEOFMANY:
00567 case SDT_MANYOFMANY:
00568 switch (GetVarMemType(sld->conv)) {
00569 case SLE_VAR_BL:
00570 if (*(bool*)ptr == (p != NULL)) continue;
00571 break;
00572 case SLE_VAR_I8:
00573 case SLE_VAR_U8:
00574 if (*(byte*)ptr == (byte)(size_t)p) continue;
00575 break;
00576 case SLE_VAR_I16:
00577 case SLE_VAR_U16:
00578 if (*(uint16*)ptr == (uint16)(size_t)p) continue;
00579 break;
00580 case SLE_VAR_I32:
00581 case SLE_VAR_U32:
00582 if (*(uint32*)ptr == (uint32)(size_t)p) continue;
00583 break;
00584 default: NOT_REACHED();
00585 }
00586 break;
00587 default: break;
00588 }
00589 }
00590
00591
00592 switch (sdb->cmd) {
00593 case SDT_BOOLX:
00594 case SDT_NUMX:
00595 case SDT_ONEOFMANY:
00596 case SDT_MANYOFMANY: {
00597 uint32 i = (uint32)ReadValue(ptr, sld->conv);
00598
00599 switch (sdb->cmd) {
00600 case SDT_BOOLX: strecpy(buf, (i != 0) ? "true" : "false", lastof(buf)); break;
00601 case SDT_NUMX: seprintf(buf, lastof(buf), IsSignedVarMemType(sld->conv) ? "%d" : "%u", i); break;
00602 case SDT_ONEOFMANY: MakeOneOfMany(buf, lastof(buf), sdb->many, i); break;
00603 case SDT_MANYOFMANY: MakeManyOfMany(buf, lastof(buf), sdb->many, i); break;
00604 default: NOT_REACHED();
00605 }
00606 break;
00607 }
00608
00609 case SDT_STRING:
00610 switch (GetVarMemType(sld->conv)) {
00611 case SLE_VAR_STRB: strecpy(buf, (char*)ptr, lastof(buf)); break;
00612 case SLE_VAR_STRBQ:seprintf(buf, lastof(buf), "\"%s\"", (char*)ptr); break;
00613 case SLE_VAR_STR: strecpy(buf, *(char**)ptr, lastof(buf)); break;
00614 case SLE_VAR_STRQ:
00615 if (*(char**)ptr == NULL) {
00616 buf[0] = '\0';
00617 } else {
00618 seprintf(buf, lastof(buf), "\"%s\"", *(char**)ptr);
00619 }
00620 break;
00621 case SLE_VAR_CHAR: buf[0] = *(char*)ptr; buf[1] = '\0'; break;
00622 default: NOT_REACHED();
00623 }
00624 break;
00625
00626 case SDT_INTLIST:
00627 MakeIntList(buf, lastof(buf), ptr, sld->length, GetVarMemType(sld->conv));
00628 break;
00629 default: NOT_REACHED();
00630 }
00631
00632
00633 free(item->value);
00634 item->value = strdup(buf);
00635 }
00636 }
00637
00647 static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList *list)
00648 {
00649 IniGroup *group = ini->GetGroup(grpname);
00650
00651 if (group == NULL || list == NULL) return;
00652
00653 list->Clear();
00654
00655 for (const IniItem *item = group->item; item != NULL; item = item->next) {
00656 if (item->name != NULL) *list->Append() = strdup(item->name);
00657 }
00658 }
00659
00669 static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList *list)
00670 {
00671 IniGroup *group = ini->GetGroup(grpname);
00672
00673 if (group == NULL || list == NULL) return;
00674 group->Clear();
00675
00676 for (char **iter = list->Begin(); iter != list->End(); iter++) {
00677 group->GetItem(*iter, true)->SetValue("");
00678 }
00679 }
00680
00681
00682
00684 static bool v_PositionMainToolbar(int32 p1)
00685 {
00686 if (_game_mode != GM_MENU) PositionMainToolbar(NULL);
00687 return true;
00688 }
00689
00691 static bool v_PositionStatusbar(int32 p1)
00692 {
00693 if (_game_mode != GM_MENU) {
00694 PositionStatusbar(NULL);
00695 PositionNewsMessage(NULL);
00696 PositionNetworkChatWindow(NULL);
00697 }
00698 return true;
00699 }
00700
00701 static bool PopulationInLabelActive(int32 p1)
00702 {
00703 UpdateAllTownVirtCoords();
00704 return true;
00705 }
00706
00707 static bool RedrawScreen(int32 p1)
00708 {
00709 MarkWholeScreenDirty();
00710 return true;
00711 }
00712
00718 static bool RedrawSmallmap(int32 p1)
00719 {
00720 BuildLandLegend();
00721 BuildOwnerLegend();
00722 SetWindowClassesDirty(WC_SMALLMAP);
00723 return true;
00724 }
00725
00726 static bool InvalidateDetailsWindow(int32 p1)
00727 {
00728 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00729 return true;
00730 }
00731
00732 static bool InvalidateStationBuildWindow(int32 p1)
00733 {
00734 SetWindowDirty(WC_BUILD_STATION, 0);
00735 return true;
00736 }
00737
00738 static bool InvalidateBuildIndustryWindow(int32 p1)
00739 {
00740 InvalidateWindowData(WC_BUILD_INDUSTRY, 0);
00741 return true;
00742 }
00743
00744 static bool CloseSignalGUI(int32 p1)
00745 {
00746 if (p1 == 0) {
00747 DeleteWindowByClass(WC_BUILD_SIGNAL);
00748 }
00749 return true;
00750 }
00751
00752 static bool InvalidateTownViewWindow(int32 p1)
00753 {
00754 InvalidateWindowClassesData(WC_TOWN_VIEW, p1);
00755 return true;
00756 }
00757
00758 static bool DeleteSelectStationWindow(int32 p1)
00759 {
00760 DeleteWindowById(WC_SELECT_STATION, 0);
00761 return true;
00762 }
00763
00764 static bool UpdateConsists(int32 p1)
00765 {
00766 Train *t;
00767 FOR_ALL_TRAINS(t) {
00768
00769 if (t->IsFrontEngine() || t->IsFreeWagon()) t->ConsistChanged(true);
00770 }
00771 return true;
00772 }
00773
00774
00775 static bool CheckInterval(int32 p1)
00776 {
00777 VehicleDefaultSettings *vds;
00778 if (_game_mode == GM_MENU || !Company::IsValidID(_current_company)) {
00779 vds = &_settings_client.company.vehicle;
00780 } else {
00781 vds = &Company::Get(_current_company)->settings.vehicle;
00782 }
00783
00784 if (p1 != 0) {
00785 vds->servint_trains = 50;
00786 vds->servint_roadveh = 50;
00787 vds->servint_aircraft = 50;
00788 vds->servint_ships = 50;
00789 } else {
00790 vds->servint_trains = 150;
00791 vds->servint_roadveh = 150;
00792 vds->servint_aircraft = 100;
00793 vds->servint_ships = 360;
00794 }
00795
00796 InvalidateDetailsWindow(0);
00797
00798 return true;
00799 }
00800
00801 static bool TrainAccelerationModelChanged(int32 p1)
00802 {
00803 Train *t;
00804 FOR_ALL_TRAINS(t) {
00805 if (t->IsFrontEngine()) {
00806 t->tcache.cached_max_curve_speed = t->GetCurveSpeedLimit();
00807 t->UpdateAcceleration();
00808 }
00809 }
00810
00811
00812 SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00813 InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00814 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00815
00816 return true;
00817 }
00818
00824 static bool TrainSlopeSteepnessChanged(int32 p1)
00825 {
00826 Train *t;
00827 FOR_ALL_TRAINS(t) {
00828 if (t->IsFrontEngine()) t->CargoChanged();
00829 }
00830
00831 return true;
00832 }
00833
00839 static bool RoadVehAccelerationModelChanged(int32 p1)
00840 {
00841 if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) {
00842 RoadVehicle *rv;
00843 FOR_ALL_ROADVEHICLES(rv) {
00844 if (rv->IsFrontEngine()) {
00845 rv->CargoChanged();
00846 }
00847 }
00848 }
00849
00850
00851 SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00852 InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00853 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00854
00855 return true;
00856 }
00857
00863 static bool RoadVehSlopeSteepnessChanged(int32 p1)
00864 {
00865 RoadVehicle *rv;
00866 FOR_ALL_ROADVEHICLES(rv) {
00867 if (rv->IsFrontEngine()) rv->CargoChanged();
00868 }
00869
00870 return true;
00871 }
00872
00873 static bool DragSignalsDensityChanged(int32)
00874 {
00875 InvalidateWindowData(WC_BUILD_SIGNAL, 0);
00876
00877 return true;
00878 }
00879
00880 static bool TownFoundingChanged(int32 p1)
00881 {
00882 if (_game_mode != GM_EDITOR && _settings_game.economy.found_town == TF_FORBIDDEN) {
00883 DeleteWindowById(WC_FOUND_TOWN, 0);
00884 return true;
00885 }
00886 InvalidateWindowData(WC_FOUND_TOWN, 0);
00887 return true;
00888 }
00889
00890 static bool InvalidateVehTimetableWindow(int32 p1)
00891 {
00892 InvalidateWindowClassesData(WC_VEHICLE_TIMETABLE, -2);
00893 return true;
00894 }
00895
00903 static bool InvalidateNewGRFChangeWindows(int32 p1)
00904 {
00905 InvalidateWindowClassesData(WC_SAVELOAD);
00906 DeleteWindowByClass(WC_GAME_OPTIONS);
00907 ReInitAllWindows();
00908 return true;
00909 }
00910
00911 static bool InvalidateCompanyLiveryWindow(int32 p1)
00912 {
00913 InvalidateWindowClassesData(WC_COMPANY_COLOUR);
00914 return RedrawScreen(p1);
00915 }
00916
00917 static bool InvalidateIndustryViewWindow(int32 p1)
00918 {
00919 InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
00920 return true;
00921 }
00922
00928 static bool RedrawTownAuthority(int32 p1)
00929 {
00930 SetWindowClassesDirty(WC_TOWN_AUTHORITY);
00931 return true;
00932 }
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955 static const DifficultySettings _default_game_diff[3] = {
00956
00957 {2, 2, 4, 300000, 2, 0, 2, 1, 2, 0, 1, 0, 0, 0, 0, 0, 0},
00958 {4, 2, 3, 150000, 3, 1, 3, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1},
00959 {7, 3, 3, 100000, 4, 1, 3, 2, 0, 2, 3, 2, 1, 1, 1, 2, 2},
00960 };
00961
00962 void SetDifficultyLevel(int mode, DifficultySettings *gm_opt)
00963 {
00964 assert(mode <= 3);
00965
00966 if (mode != 3) {
00967 *gm_opt = _default_game_diff[mode];
00968 } else {
00969 gm_opt->diff_level = 3;
00970 }
00971 }
00972
00974 static void ValidateSettings()
00975 {
00976
00977 if (_settings_newgame.difficulty.diff_level != 3) {
00978 SetDifficultyLevel(_settings_newgame.difficulty.diff_level, &_settings_newgame.difficulty);
00979 }
00980
00981
00982 if (_settings_newgame.game_creation.land_generator == 0 &&
00983 _settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
00984 _settings_newgame.difficulty.quantity_sea_lakes = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE;
00985 }
00986 }
00987
00988 static bool DifficultyReset(int32 level)
00989 {
00990
00991
00992 if (_game_mode != GM_MENU && level != 3) return false;
00993 SetDifficultyLevel(level, &GetGameSettings().difficulty);
00994 return true;
00995 }
00996
00997 static bool DifficultyChange(int32)
00998 {
00999 if (_game_mode == GM_MENU) {
01000 if (_settings_newgame.difficulty.diff_level != 3) {
01001 ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, WL_WARNING);
01002 _settings_newgame.difficulty.diff_level = 3;
01003 }
01004 SetWindowClassesDirty(WC_SELECT_GAME);
01005 } else {
01006 _settings_game.difficulty.diff_level = 3;
01007 }
01008
01009
01010
01011
01012 if (_networking && FindWindowById(WC_GAME_OPTIONS, 0) != NULL) {
01013 ShowGameDifficulty();
01014 }
01015
01016 return true;
01017 }
01018
01019 static bool DifficultyNoiseChange(int32 i)
01020 {
01021 if (_game_mode == GM_NORMAL) {
01022 UpdateAirportsNoise();
01023 if (_settings_game.economy.station_noise_level) {
01024 InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
01025 }
01026 }
01027
01028 return DifficultyChange(i);
01029 }
01030
01031 static bool MaxNoAIsChange(int32 i)
01032 {
01033 if (GetGameSettings().difficulty.max_no_competitors != 0 &&
01034 #ifdef ENABLE_AI
01035 AI::GetInfoList()->size() == 0 &&
01036 #endif
01037 (!_networking || _network_server)) {
01038 ShowErrorMessage(STR_WARNING_NO_SUITABLE_AI, INVALID_STRING_ID, WL_CRITICAL);
01039 }
01040
01041 return DifficultyChange(i);
01042 }
01043
01049 static bool CheckRoadSide(int p1)
01050 {
01051 extern bool RoadVehiclesAreBuilt();
01052 return _game_mode == GM_MENU || !RoadVehiclesAreBuilt();
01053 }
01054
01062 static int32 ConvertLandscape(const char *value)
01063 {
01064
01065 return LookupOneOfMany("normal|hilly|desert|candy", value);
01066 }
01067
01068 static bool CheckFreeformEdges(int32 p1)
01069 {
01070 if (_game_mode == GM_MENU) return true;
01071 if (p1 != 0) {
01072 Ship *s;
01073 FOR_ALL_SHIPS(s) {
01074
01075 if (TileX(s->tile) == 0 || TileY(s->tile) == 0) {
01076 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01077 return false;
01078 }
01079 }
01080 BaseStation *st;
01081 FOR_ALL_BASE_STATIONS(st) {
01082
01083 if (st->IsInUse() && (TileX(st->xy) == 0 || TileY(st->xy) == 0)) {
01084 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01085 return false;
01086 }
01087 }
01088 for (uint i = 0; i < MapSizeX(); i++) MakeVoid(TileXY(i, 0));
01089 for (uint i = 0; i < MapSizeY(); i++) MakeVoid(TileXY(0, i));
01090 } else {
01091 for (uint i = 0; i < MapMaxX(); i++) {
01092 if (TileHeight(TileXY(i, 1)) != 0) {
01093 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01094 return false;
01095 }
01096 }
01097 for (uint i = 1; i < MapMaxX(); i++) {
01098 if (!IsTileType(TileXY(i, MapMaxY() - 1), MP_WATER) || TileHeight(TileXY(1, MapMaxY())) != 0) {
01099 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01100 return false;
01101 }
01102 }
01103 for (uint i = 0; i < MapMaxY(); i++) {
01104 if (TileHeight(TileXY(1, i)) != 0) {
01105 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01106 return false;
01107 }
01108 }
01109 for (uint i = 1; i < MapMaxY(); i++) {
01110 if (!IsTileType(TileXY(MapMaxX() - 1, i), MP_WATER) || TileHeight(TileXY(MapMaxX(), i)) != 0) {
01111 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01112 return false;
01113 }
01114 }
01115
01116 for (uint i = 0; i < MapMaxX(); i++) {
01117 SetTileHeight(TileXY(i, 0), 0);
01118 SetTileType(TileXY(i, 0), MP_WATER);
01119 }
01120 for (uint i = 0; i < MapMaxY(); i++) {
01121 SetTileHeight(TileXY(0, i), 0);
01122 SetTileType(TileXY(0, i), MP_WATER);
01123 }
01124 }
01125 MarkWholeScreenDirty();
01126 return true;
01127 }
01128
01133 static bool ChangeDynamicEngines(int32 p1)
01134 {
01135 if (_game_mode == GM_MENU) return true;
01136
01137 if (!EngineOverrideManager::ResetToCurrentNewGRFConfig()) {
01138 ShowErrorMessage(STR_CONFIG_SETTING_DYNAMIC_ENGINES_EXISTING_VEHICLES, INVALID_STRING_ID, WL_ERROR);
01139 return false;
01140 }
01141
01142 return true;
01143 }
01144
01145 static bool StationCatchmentChanged(int32 p1)
01146 {
01147 Station::RecomputeIndustriesNearForAll();
01148 return true;
01149 }
01150
01151
01152 #ifdef ENABLE_NETWORK
01153
01154 static bool UpdateClientName(int32 p1)
01155 {
01156 NetworkUpdateClientName();
01157 return true;
01158 }
01159
01160 static bool UpdateServerPassword(int32 p1)
01161 {
01162 if (strcmp(_settings_client.network.server_password, "*") == 0) {
01163 _settings_client.network.server_password[0] = '\0';
01164 }
01165
01166 return true;
01167 }
01168
01169 static bool UpdateRconPassword(int32 p1)
01170 {
01171 if (strcmp(_settings_client.network.rcon_password, "*") == 0) {
01172 _settings_client.network.rcon_password[0] = '\0';
01173 }
01174
01175 return true;
01176 }
01177
01178 static bool UpdateClientConfigValues(int32 p1)
01179 {
01180 if (_network_server) NetworkServerSendConfigUpdate();
01181
01182 return true;
01183 }
01184
01185 #endif
01186
01187
01188
01189
01193 static void PrepareOldDiffCustom()
01194 {
01195 memset(_old_diff_custom, 0, sizeof(_old_diff_custom));
01196 }
01197
01204 static void HandleOldDiffCustom(bool savegame)
01205 {
01206 uint options_to_load = GAME_DIFFICULTY_NUM - ((savegame && IsSavegameVersionBefore(4)) ? 1 : 0);
01207
01208 if (!savegame) {
01209
01210 bool old_diff_custom_used = false;
01211 for (uint i = 0; i < options_to_load && !old_diff_custom_used; i++) {
01212 old_diff_custom_used = (_old_diff_custom[i] != 0);
01213 }
01214
01215 if (!old_diff_custom_used) return;
01216 }
01217
01218 for (uint i = 0; i < options_to_load; i++) {
01219 const SettingDesc *sd = &_settings[i];
01220
01221 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01222 void *var = GetVariableAddress(savegame ? &_settings_game : &_settings_newgame, &sd->save);
01223 Write_ValidateSetting(var, sd, (int32)((i == 4 ? 1000 : 1) * _old_diff_custom[i]));
01224 }
01225 }
01226
01233 static bool ConvertOldNewsSetting(const char *name, const char *value)
01234 {
01235 if (strcasecmp(name, "openclose") == 0) {
01236
01237
01238
01239
01240 NewsDisplay display = ND_OFF;
01241 if (strcasecmp(value, "full") == 0) {
01242 display = ND_FULL;
01243 } else if (strcasecmp(value, "summarized") == 0) {
01244 display = ND_SUMMARY;
01245 }
01246
01247 _news_type_data[NT_INDUSTRY_OPEN].display = display;
01248 _news_type_data[NT_INDUSTRY_CLOSE].display = display;
01249 return true;
01250 }
01251 return false;
01252 }
01253
01259 static void NewsDisplayLoadConfig(IniFile *ini, const char *grpname)
01260 {
01261 IniGroup *group = ini->GetGroup(grpname);
01262 IniItem *item;
01263
01264
01265 if (group == NULL) return;
01266
01267 for (item = group->item; item != NULL; item = item->next) {
01268 int news_item = -1;
01269 for (int i = 0; i < NT_END; i++) {
01270 if (strcasecmp(item->name, _news_type_data[i].name) == 0) {
01271 news_item = i;
01272 break;
01273 }
01274 }
01275
01276
01277 if (news_item == -1) {
01278
01279 if (!ConvertOldNewsSetting(item->name, item->value)) {
01280 DEBUG(misc, 0, "Invalid display option: %s", item->name);
01281 }
01282
01283 continue;
01284 }
01285
01286 if (StrEmpty(item->value)) {
01287 DEBUG(misc, 0, "Empty display value for newstype %s", item->name);
01288 continue;
01289 } else if (strcasecmp(item->value, "full") == 0) {
01290 _news_type_data[news_item].display = ND_FULL;
01291 } else if (strcasecmp(item->value, "off") == 0) {
01292 _news_type_data[news_item].display = ND_OFF;
01293 } else if (strcasecmp(item->value, "summarized") == 0) {
01294 _news_type_data[news_item].display = ND_SUMMARY;
01295 } else {
01296 DEBUG(misc, 0, "Invalid display value for newstype %s: %s", item->name, item->value);
01297 continue;
01298 }
01299 }
01300 }
01301
01302 static void AILoadConfig(IniFile *ini, const char *grpname)
01303 {
01304 #ifdef ENABLE_AI
01305 IniGroup *group = ini->GetGroup(grpname);
01306 IniItem *item;
01307
01308
01309 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01310 AIConfig::GetConfig(c, AIConfig::AISS_FORCE_NEWGAME)->ChangeAI(NULL);
01311 }
01312
01313
01314 if (group == NULL) return;
01315
01316 CompanyID c = COMPANY_FIRST;
01317 for (item = group->item; c < MAX_COMPANIES && item != NULL; c++, item = item->next) {
01318 AIConfig *config = AIConfig::GetConfig(c, AIConfig::AISS_FORCE_NEWGAME);
01319
01320 config->ChangeAI(item->name);
01321 if (!config->HasAI()) {
01322 if (strcmp(item->name, "none") != 0) {
01323 DEBUG(ai, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name);
01324 continue;
01325 }
01326 }
01327 if (item->value != NULL) config->StringToSettings(item->value);
01328 }
01329 #endif
01330 }
01331
01338 static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_static)
01339 {
01340 IniGroup *group = ini->GetGroup(grpname);
01341 IniItem *item;
01342 GRFConfig *first = NULL;
01343 GRFConfig **curr = &first;
01344
01345 if (group == NULL) return NULL;
01346
01347 for (item = group->item; item != NULL; item = item->next) {
01348 GRFConfig *c = new GRFConfig(item->name);
01349
01350
01351 if (!StrEmpty(item->value)) {
01352 c->num_params = ParseIntList(item->value, (int*)c->param, lengthof(c->param));
01353 if (c->num_params == (byte)-1) {
01354 ShowInfoF("ini: error in array '%s'", item->name);
01355 c->num_params = 0;
01356 }
01357 }
01358
01359
01360 if (!FillGRFDetails(c, is_static) || HasBit(c->flags, GCF_INVALID)) {
01361 const char *msg;
01362
01363 if (c->status == GCS_NOT_FOUND) {
01364 msg = "not found";
01365 } else if (HasBit(c->flags, GCF_UNSAFE)) {
01366 msg = "unsafe for static use";
01367 } else if (HasBit(c->flags, GCF_SYSTEM)) {
01368 msg = "system NewGRF";
01369 } else if (HasBit(c->flags, GCF_INVALID)) {
01370 msg = "incompatible to this version of OpenTTD";
01371 } else {
01372 msg = "unknown";
01373 }
01374
01375 ShowInfoF("ini: ignoring invalid NewGRF '%s': %s", item->name, msg);
01376 delete c;
01377 continue;
01378 }
01379
01380
01381 bool duplicate = false;
01382 for (const GRFConfig *gc = first; gc != NULL; gc = gc->next) {
01383 if (gc->ident.grfid == c->ident.grfid) {
01384 ShowInfoF("ini: ignoring NewGRF '%s': duplicate GRF ID with '%s'", item->name, gc->filename);
01385 duplicate = true;
01386 break;
01387 }
01388 }
01389 if (duplicate) {
01390 delete c;
01391 continue;
01392 }
01393
01394
01395 if (is_static) SetBit(c->flags, GCF_STATIC);
01396
01397
01398 *curr = c;
01399 curr = &c->next;
01400 }
01401
01402 return first;
01403 }
01404
01410 static void NewsDisplaySaveConfig(IniFile *ini, const char *grpname)
01411 {
01412 IniGroup *group = ini->GetGroup(grpname);
01413
01414 for (int i = 0; i < NT_END; i++) {
01415 const char *value;
01416 int v = _news_type_data[i].display;
01417
01418 value = (v == ND_OFF ? "off" : (v == ND_SUMMARY ? "summarized" : "full"));
01419
01420 group->GetItem(_news_type_data[i].name, true)->SetValue(value);
01421 }
01422 }
01423
01424 static void AISaveConfig(IniFile *ini, const char *grpname)
01425 {
01426 #ifdef ENABLE_AI
01427 IniGroup *group = ini->GetGroup(grpname);
01428
01429 if (group == NULL) return;
01430 group->Clear();
01431
01432 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01433 AIConfig *config = AIConfig::GetConfig(c, AIConfig::AISS_FORCE_NEWGAME);
01434 const char *name;
01435 char value[1024];
01436 config->SettingsToString(value, lengthof(value));
01437
01438 if (config->HasAI()) {
01439 name = config->GetName();
01440 } else {
01441 name = "none";
01442 }
01443
01444 IniItem *item = new IniItem(group, name, strlen(name));
01445 item->SetValue(value);
01446 }
01447 #endif
01448 }
01449
01454 static void SaveVersionInConfig(IniFile *ini)
01455 {
01456 IniGroup *group = ini->GetGroup("version");
01457
01458 char version[9];
01459 snprintf(version, lengthof(version), "%08X", _openttd_newgrf_version);
01460
01461 const char * const versions[][2] = {
01462 { "version_string", _openttd_revision },
01463 { "version_number", version }
01464 };
01465
01466 for (uint i = 0; i < lengthof(versions); i++) {
01467 group->GetItem(versions[i][0], true)->SetValue(versions[i][1]);
01468 }
01469 }
01470
01471
01472 static void GRFSaveConfig(IniFile *ini, const char *grpname, const GRFConfig *list)
01473 {
01474 ini->RemoveGroup(grpname);
01475 IniGroup *group = ini->GetGroup(grpname);
01476 const GRFConfig *c;
01477
01478 for (c = list; c != NULL; c = c->next) {
01479 char params[512];
01480 GRFBuildParamList(params, c, lastof(params));
01481
01482 group->GetItem(c->filename, true)->SetValue(params);
01483 }
01484 }
01485
01486
01487 static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list)
01488 {
01489 proc(ini, (const SettingDesc*)_misc_settings, "misc", NULL);
01490 #if defined(WIN32) && !defined(DEDICATED)
01491 proc(ini, (const SettingDesc*)_win32_settings, "win32", NULL);
01492 #endif
01493
01494 proc(ini, _settings, "patches", &_settings_newgame);
01495 proc(ini, _currency_settings,"currency", &_custom_currency);
01496 proc(ini, _company_settings, "company", &_settings_client.company);
01497
01498 #ifdef ENABLE_NETWORK
01499 proc_list(ini, "server_bind_addresses", &_network_bind_list);
01500 proc_list(ini, "servers", &_network_host_list);
01501 proc_list(ini, "bans", &_network_ban_list);
01502 #endif
01503 }
01504
01505 static IniFile *IniLoadConfig()
01506 {
01507 IniFile *ini = new IniFile(_list_group_names);
01508 ini->LoadFromDisk(_config_file);
01509 return ini;
01510 }
01511
01513 void LoadFromConfig()
01514 {
01515 IniFile *ini = IniLoadConfig();
01516 ResetCurrencies(false);
01517
01518 HandleSettingDescs(ini, IniLoadSettings, IniLoadSettingList);
01519 _grfconfig_newgame = GRFLoadConfig(ini, "newgrf", false);
01520 _grfconfig_static = GRFLoadConfig(ini, "newgrf-static", true);
01521 NewsDisplayLoadConfig(ini, "news_display");
01522 AILoadConfig(ini, "ai_players");
01523
01524 PrepareOldDiffCustom();
01525 IniLoadSettings(ini, _gameopt_settings, "gameopt", &_settings_newgame);
01526 HandleOldDiffCustom(false);
01527
01528 ValidateSettings();
01529 delete ini;
01530 }
01531
01533 void SaveToConfig()
01534 {
01535 IniFile *ini = IniLoadConfig();
01536
01537
01538 ini->RemoveGroup("patches");
01539 ini->RemoveGroup("yapf");
01540 ini->RemoveGroup("gameopt");
01541
01542 HandleSettingDescs(ini, IniSaveSettings, IniSaveSettingList);
01543 GRFSaveConfig(ini, "newgrf", _grfconfig_newgame);
01544 GRFSaveConfig(ini, "newgrf-static", _grfconfig_static);
01545 NewsDisplaySaveConfig(ini, "news_display");
01546 AISaveConfig(ini, "ai_players");
01547 SaveVersionInConfig(ini);
01548 ini->SaveToDisk(_config_file);
01549 delete ini;
01550 }
01551
01556 void GetGRFPresetList(GRFPresetList *list)
01557 {
01558 list->Clear();
01559
01560 IniFile *ini = IniLoadConfig();
01561 IniGroup *group;
01562 for (group = ini->group; group != NULL; group = group->next) {
01563 if (strncmp(group->name, "preset-", 7) == 0) {
01564 *list->Append() = strdup(group->name + 7);
01565 }
01566 }
01567
01568 delete ini;
01569 }
01570
01577 GRFConfig *LoadGRFPresetFromConfig(const char *config_name)
01578 {
01579 char *section = (char*)alloca(strlen(config_name) + 8);
01580 sprintf(section, "preset-%s", config_name);
01581
01582 IniFile *ini = IniLoadConfig();
01583 GRFConfig *config = GRFLoadConfig(ini, section, false);
01584 delete ini;
01585
01586 return config;
01587 }
01588
01595 void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
01596 {
01597 char *section = (char*)alloca(strlen(config_name) + 8);
01598 sprintf(section, "preset-%s", config_name);
01599
01600 IniFile *ini = IniLoadConfig();
01601 GRFSaveConfig(ini, section, config);
01602 ini->SaveToDisk(_config_file);
01603 delete ini;
01604 }
01605
01610 void DeleteGRFPresetFromConfig(const char *config_name)
01611 {
01612 char *section = (char*)alloca(strlen(config_name) + 8);
01613 sprintf(section, "preset-%s", config_name);
01614
01615 IniFile *ini = IniLoadConfig();
01616 ini->RemoveGroup(section);
01617 ini->SaveToDisk(_config_file);
01618 delete ini;
01619 }
01620
01621 const SettingDesc *GetSettingDescription(uint index)
01622 {
01623 if (index >= lengthof(_settings)) return NULL;
01624 return &_settings[index];
01625 }
01626
01638 CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01639 {
01640 const SettingDesc *sd = GetSettingDescription(p1);
01641
01642 if (sd == NULL) return CMD_ERROR;
01643 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR;
01644
01645 if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return CMD_ERROR;
01646 if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return CMD_ERROR;
01647 if ((sd->desc.flags & SGF_NEWGAME_ONLY) &&
01648 (_game_mode == GM_NORMAL ||
01649 (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0))) {
01650 return CMD_ERROR;
01651 }
01652
01653 if (flags & DC_EXEC) {
01654 void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01655
01656 int32 oldval = (int32)ReadValue(var, sd->save.conv);
01657 int32 newval = (int32)p2;
01658
01659 Write_ValidateSetting(var, sd, newval);
01660 newval = (int32)ReadValue(var, sd->save.conv);
01661
01662 if (oldval == newval) return CommandCost();
01663
01664 if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01665 WriteValue(var, sd->save.conv, (int64)oldval);
01666 return CommandCost();
01667 }
01668
01669 if (sd->desc.flags & SGF_NO_NETWORK) {
01670 GamelogStartAction(GLAT_SETTING);
01671 GamelogSetting(sd->desc.name, oldval, newval);
01672 GamelogStopAction();
01673 }
01674
01675 SetWindowDirty(WC_GAME_OPTIONS, 0);
01676 }
01677
01678 return CommandCost();
01679 }
01680
01691 CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01692 {
01693 if (p1 >= lengthof(_company_settings)) return CMD_ERROR;
01694 const SettingDesc *sd = &_company_settings[p1];
01695
01696 if (flags & DC_EXEC) {
01697 void *var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01698
01699 int32 oldval = (int32)ReadValue(var, sd->save.conv);
01700 int32 newval = (int32)p2;
01701
01702 Write_ValidateSetting(var, sd, newval);
01703 newval = (int32)ReadValue(var, sd->save.conv);
01704
01705 if (oldval == newval) return CommandCost();
01706
01707 if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01708 WriteValue(var, sd->save.conv, (int64)oldval);
01709 return CommandCost();
01710 }
01711
01712 SetWindowDirty(WC_GAME_OPTIONS, 0);
01713 }
01714
01715 return CommandCost();
01716 }
01717
01725 bool SetSettingValue(uint index, int32 value, bool force_newgame)
01726 {
01727 const SettingDesc *sd = &_settings[index];
01728
01729
01730
01731
01732 if (sd->save.conv & SLF_NO_NETWORK_SYNC) {
01733 void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01734 Write_ValidateSetting(var, sd, value);
01735
01736 if (_game_mode != GM_MENU) {
01737 void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01738 Write_ValidateSetting(var2, sd, value);
01739 }
01740 if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01741 SetWindowDirty(WC_GAME_OPTIONS, 0);
01742 return true;
01743 }
01744
01745 if (force_newgame) {
01746 void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01747 Write_ValidateSetting(var2, sd, value);
01748 return true;
01749 }
01750
01751
01752 if (!_networking || (_networking && _network_server)) {
01753 return DoCommandP(0, index, value, CMD_CHANGE_SETTING);
01754 }
01755 return false;
01756 }
01757
01764 void SetCompanySetting(uint index, int32 value)
01765 {
01766 const SettingDesc *sd = &_company_settings[index];
01767 if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01768 DoCommandP(0, index, value, CMD_CHANGE_COMPANY_SETTING);
01769 } else {
01770 void *var = GetVariableAddress(&_settings_client.company, &sd->save);
01771 Write_ValidateSetting(var, sd, value);
01772 if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01773 }
01774 }
01775
01779 void SetDefaultCompanySettings(CompanyID cid)
01780 {
01781 Company *c = Company::Get(cid);
01782 const SettingDesc *sd;
01783 for (sd = _company_settings; sd->save.cmd != SL_END; sd++) {
01784 void *var = GetVariableAddress(&c->settings, &sd->save);
01785 Write_ValidateSetting(var, sd, (int32)(size_t)sd->desc.def);
01786 }
01787 }
01788
01789 #if defined(ENABLE_NETWORK)
01790
01793 void SyncCompanySettings()
01794 {
01795 const SettingDesc *sd;
01796 uint i = 0;
01797 for (sd = _company_settings; sd->save.cmd != SL_END; sd++, i++) {
01798 const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01799 const void *new_var = GetVariableAddress(&_settings_client.company, &sd->save);
01800 uint32 old_value = (uint32)ReadValue(old_var, sd->save.conv);
01801 uint32 new_value = (uint32)ReadValue(new_var, sd->save.conv);
01802 if (old_value != new_value) NetworkSendCommand(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, NULL, NULL, _local_company);
01803 }
01804 }
01805 #endif
01806
01812 uint GetCompanySettingIndex(const char *name)
01813 {
01814 uint i;
01815 const SettingDesc *sd = GetSettingFromName(name, &i);
01816 assert(sd != NULL && (sd->desc.flags & SGF_PER_COMPANY) != 0);
01817 return i;
01818 }
01819
01827 bool SetSettingValue(uint index, const char *value, bool force_newgame)
01828 {
01829 const SettingDesc *sd = &_settings[index];
01830 assert(sd->save.conv & SLF_NO_NETWORK_SYNC);
01831
01832 if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) {
01833 char **var = (char**)GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
01834 free(*var);
01835 *var = strcmp(value, "(null)") == 0 ? NULL : strdup(value);
01836 } else {
01837 char *var = (char*)GetVariableAddress(NULL, &sd->save);
01838 ttd_strlcpy(var, value, sd->save.length);
01839 }
01840 if (sd->desc.proc != NULL) sd->desc.proc(0);
01841
01842 return true;
01843 }
01844
01852 const SettingDesc *GetSettingFromName(const char *name, uint *i)
01853 {
01854 const SettingDesc *sd;
01855
01856
01857 for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01858 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01859 if (strcmp(sd->desc.name, name) == 0) return sd;
01860 }
01861
01862
01863 for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01864 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01865 const char *short_name = strchr(sd->desc.name, '.');
01866 if (short_name != NULL) {
01867 short_name++;
01868 if (strcmp(short_name, name) == 0) return sd;
01869 }
01870 }
01871
01872 if (strncmp(name, "company.", 8) == 0) name += 8;
01873
01874 for (*i = 0, sd = _company_settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01875 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01876 if (strcmp(sd->desc.name, name) == 0) return sd;
01877 }
01878
01879 return NULL;
01880 }
01881
01882
01883
01884 void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
01885 {
01886 uint index;
01887 const SettingDesc *sd = GetSettingFromName(name, &index);
01888
01889 if (sd == NULL) {
01890 IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01891 return;
01892 }
01893
01894 bool success;
01895 if (sd->desc.cmd == SDT_STRING) {
01896 success = SetSettingValue(index, value, force_newgame);
01897 } else {
01898 uint32 val;
01899 extern bool GetArgumentInteger(uint32 *value, const char *arg);
01900 success = GetArgumentInteger(&val, value);
01901 if (!success) {
01902 IConsolePrintF(CC_ERROR, "'%s' is not an integer.", value);
01903 return;
01904 }
01905
01906 success = SetSettingValue(index, val, force_newgame);
01907 }
01908
01909 if (!success) {
01910 if (_network_server) {
01911 IConsoleError("This command/variable is not available during network games.");
01912 } else {
01913 IConsoleError("This command/variable is only available to a network server.");
01914 }
01915 }
01916 }
01917
01918 void IConsoleSetSetting(const char *name, int value)
01919 {
01920 uint index;
01921 const SettingDesc *sd = GetSettingFromName(name, &index);
01922 assert(sd != NULL);
01923 SetSettingValue(index, value);
01924 }
01925
01931 void IConsoleGetSetting(const char *name, bool force_newgame)
01932 {
01933 char value[20];
01934 uint index;
01935 const SettingDesc *sd = GetSettingFromName(name, &index);
01936 const void *ptr;
01937
01938 if (sd == NULL) {
01939 IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01940 return;
01941 }
01942
01943 ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
01944
01945 if (sd->desc.cmd == SDT_STRING) {
01946 IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char **)ptr : (const char *)ptr);
01947 } else {
01948 if (sd->desc.cmd == SDT_BOOLX) {
01949 snprintf(value, sizeof(value), (*(bool*)ptr == 1) ? "on" : "off");
01950 } else {
01951 snprintf(value, sizeof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
01952 }
01953
01954 IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s' (min: %s%d, max: %u)",
01955 name, value, (sd->desc.flags & SGF_0ISDISABLED) ? "(0) " : "", sd->desc.min, sd->desc.max);
01956 }
01957 }
01958
01964 void IConsoleListSettings(const char *prefilter)
01965 {
01966 IConsolePrintF(CC_WARNING, "All settings with their current value:");
01967
01968 for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) {
01969 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01970 if (prefilter != NULL && strstr(sd->desc.name, prefilter) == NULL) continue;
01971 char value[80];
01972 const void *ptr = GetVariableAddress(&GetGameSettings(), &sd->save);
01973
01974 if (sd->desc.cmd == SDT_BOOLX) {
01975 snprintf(value, lengthof(value), (*(bool*)ptr == 1) ? "on" : "off");
01976 } else if (sd->desc.cmd == SDT_STRING) {
01977 snprintf(value, sizeof(value), "%s", (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char **)ptr : (const char *)ptr);
01978 } else {
01979 snprintf(value, lengthof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
01980 }
01981 IConsolePrintF(CC_DEFAULT, "%s = %s", sd->desc.name, value);
01982 }
01983
01984 IConsolePrintF(CC_WARNING, "Use 'setting' command to change a value");
01985 }
01986
01993 static void LoadSettings(const SettingDesc *osd, void *object)
01994 {
01995 for (; osd->save.cmd != SL_END; osd++) {
01996 const SaveLoad *sld = &osd->save;
01997 void *ptr = GetVariableAddress(object, sld);
01998
01999 if (!SlObjectMember(ptr, sld)) continue;
02000 if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, osd, ReadValue(ptr, sld->conv));
02001 }
02002 }
02003
02010 static void SaveSettings(const SettingDesc *sd, void *object)
02011 {
02012
02013
02014 const SettingDesc *i;
02015 size_t length = 0;
02016 for (i = sd; i->save.cmd != SL_END; i++) {
02017 length += SlCalcObjMemberLength(object, &i->save);
02018 }
02019 SlSetLength(length);
02020
02021 for (i = sd; i->save.cmd != SL_END; i++) {
02022 void *ptr = GetVariableAddress(object, &i->save);
02023 SlObjectMember(ptr, &i->save);
02024 }
02025 }
02026
02027 static void Load_OPTS()
02028 {
02029
02030
02031
02032 PrepareOldDiffCustom();
02033 LoadSettings(_gameopt_settings, &_settings_game);
02034 HandleOldDiffCustom(true);
02035 }
02036
02037 static void Load_PATS()
02038 {
02039
02040
02041
02042 LoadSettings(_settings, &_settings_game);
02043 }
02044
02045 static void Check_PATS()
02046 {
02047 LoadSettings(_settings, &_load_check_data.settings);
02048 }
02049
02050 static void Save_PATS()
02051 {
02052 SaveSettings(_settings, &_settings_game);
02053 }
02054
02055 void CheckConfig()
02056 {
02057
02058
02059
02060
02061 if (_settings_newgame.pf.opf.pf_maxdepth == 16 && _settings_newgame.pf.opf.pf_maxlength == 512) {
02062 _settings_newgame.pf.opf.pf_maxdepth = 48;
02063 _settings_newgame.pf.opf.pf_maxlength = 4096;
02064 }
02065 }
02066
02067 extern const ChunkHandler _setting_chunk_handlers[] = {
02068 { 'OPTS', NULL, Load_OPTS, NULL, NULL, CH_RIFF},
02069 { 'PATS', Save_PATS, Load_PATS, NULL, Check_PATS, CH_RIFF | CH_LAST},
02070 };
02071
02072 static bool IsSignedVarMemType(VarType vt)
02073 {
02074 switch (GetVarMemType(vt)) {
02075 case SLE_VAR_I8:
02076 case SLE_VAR_I16:
02077 case SLE_VAR_I32:
02078 case SLE_VAR_I64:
02079 return true;
02080 }
02081 return false;
02082 }