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 "strings_func.h"
00040 #include "vehicle_func.h"
00041 #include "sound_func.h"
00042 #include "company_func.h"
00043 #include "rev.h"
00044 #ifdef WITH_FREETYPE
00045 #include "fontcache.h"
00046 #endif
00047 #include "textbuf_gui.h"
00048 #include "rail_gui.h"
00049 #include "elrail_func.h"
00050 #include "gui.h"
00051 #include "town.h"
00052 #include "video/video_driver.hpp"
00053 #include "sound/sound_driver.hpp"
00054 #include "music/music_driver.hpp"
00055 #include "blitter/factory.hpp"
00056 #include "base_media_base.h"
00057 #include "gamelog.h"
00058 #include "settings_func.h"
00059 #include "ini_type.h"
00060 #include "ai/ai_config.hpp"
00061 #include "ai/ai.hpp"
00062 #include "newgrf.h"
00063 #include "ship.h"
00064 #include "smallmap_gui.h"
00065 #include "roadveh.h"
00066 #include "fios.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 int LookupOneOfMany(const char *many, const char *one, size_t onelen = 0)
00104 {
00105 const char *s;
00106 int 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 -1;
00120 many = s + 1;
00121 idx++;
00122 }
00123 }
00124
00132 static uint32 LookupManyOfMany(const char *many, const char *str)
00133 {
00134 const char *s;
00135 int r;
00136 uint32 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 == -1) return (uint32)-1;
00148
00149 SetBit(res, 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 case SLE_VAR_I16:
00229 case SLE_VAR_U16:
00230 for (i = 0; i != nitems; i++) ((uint16*)array)[i] = items[i];
00231 break;
00232 case SLE_VAR_I32:
00233 case SLE_VAR_U32:
00234 for (i = 0; i != nitems; i++) ((uint32*)array)[i] = items[i];
00235 break;
00236 default: NOT_REACHED();
00237 }
00238
00239 return true;
00240 }
00241
00251 static void MakeIntList(char *buf, const char *last, const void *array, int nelems, VarType type)
00252 {
00253 int i, v = 0;
00254 byte *p = (byte*)array;
00255
00256 for (i = 0; i != nelems; i++) {
00257 switch (type) {
00258 case SLE_VAR_BL:
00259 case SLE_VAR_I8: v = *(int8*)p; p += 1; break;
00260 case SLE_VAR_U8: v = *(byte*)p; p += 1; break;
00261 case SLE_VAR_I16: v = *(int16*)p; p += 2; break;
00262 case SLE_VAR_U16: v = *(uint16*)p; p += 2; break;
00263 case SLE_VAR_I32: v = *(int32*)p; p += 4; break;
00264 case SLE_VAR_U32: v = *(uint32*)p; p += 4; break;
00265 default: NOT_REACHED();
00266 }
00267 buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
00268 }
00269 }
00270
00278 static void MakeOneOfMany(char *buf, const char *last, const char *many, int id)
00279 {
00280 int orig_id = id;
00281
00282
00283 while (--id >= 0) {
00284 for (; *many != '|'; many++) {
00285 if (*many == '\0') {
00286 seprintf(buf, last, "%d", orig_id);
00287 return;
00288 }
00289 }
00290 many++;
00291 }
00292
00293
00294 while (*many != '\0' && *many != '|' && buf < last) *buf++ = *many++;
00295 *buf = '\0';
00296 }
00297
00306 static void MakeManyOfMany(char *buf, const char *last, const char *many, uint32 x)
00307 {
00308 const char *start;
00309 int i = 0;
00310 bool init = true;
00311
00312 for (; x != 0; x >>= 1, i++) {
00313 start = many;
00314 while (*many != 0 && *many != '|') many++;
00315
00316 if (HasBit(x, 0)) {
00317 if (!init) buf += seprintf(buf, last, "|");
00318 init = false;
00319 if (start == many) {
00320 buf += seprintf(buf, last, "%d", i);
00321 } else {
00322 memcpy(buf, start, many - start);
00323 buf += many - start;
00324 }
00325 }
00326
00327 if (*many == '|') many++;
00328 }
00329
00330 *buf = '\0';
00331 }
00332
00339 static const void *StringToVal(const SettingDescBase *desc, const char *orig_str)
00340 {
00341 const char *str = orig_str == NULL ? "" : orig_str;
00342 switch (desc->cmd) {
00343 case SDT_NUMX: {
00344 char *end;
00345 unsigned long val = strtoul(str, &end, 0);
00346 if (*end != '\0') ShowInfoF("ini: trailing characters at end of setting '%s'", desc->name);
00347 return (void*)val;
00348 }
00349 case SDT_ONEOFMANY: {
00350 long r = LookupOneOfMany(desc->many, str);
00351
00352
00353 if (r == -1 && desc->proc_cnvt != NULL) r = desc->proc_cnvt(str);
00354 if (r != -1) return (void*)r;
00355 ShowInfoF("ini: invalid value '%s' for '%s'", str, desc->name);
00356 return 0;
00357 }
00358 case SDT_MANYOFMANY: {
00359 unsigned long r = LookupManyOfMany(desc->many, str);
00360 if (r != (unsigned long)-1) return (void*)r;
00361 ShowInfoF("ini: invalid value '%s' for '%s'", str, desc->name);
00362 return 0;
00363 }
00364 case SDT_BOOLX:
00365 if (strcmp(str, "true") == 0 || strcmp(str, "on") == 0 || strcmp(str, "1") == 0) return (void*)true;
00366 if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return (void*)false;
00367 ShowInfoF("ini: invalid setting value '%s' for '%s'", str, desc->name);
00368 break;
00369
00370 case SDT_STRING: return orig_str;
00371 case SDT_INTLIST: return str;
00372 default: break;
00373 }
00374
00375 return NULL;
00376 }
00377
00387 static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val)
00388 {
00389 const SettingDescBase *sdb = &sd->desc;
00390
00391 if (sdb->cmd != SDT_BOOLX &&
00392 sdb->cmd != SDT_NUMX &&
00393 sdb->cmd != SDT_ONEOFMANY &&
00394 sdb->cmd != SDT_MANYOFMANY) {
00395 return;
00396 }
00397
00398
00399 if (sdb->cmd != SDT_MANYOFMANY) {
00400
00401
00402
00403
00404
00405 switch (GetVarMemType(sd->save.conv)) {
00406 case SLE_VAR_NULL: return;
00407 case SLE_VAR_BL:
00408 case SLE_VAR_I8:
00409 case SLE_VAR_U8:
00410 case SLE_VAR_I16:
00411 case SLE_VAR_U16:
00412 case SLE_VAR_I32: {
00413
00414 if (!(sdb->flags & SGF_0ISDISABLED) || val != 0) val = Clamp(val, sdb->min, sdb->max);
00415 break;
00416 }
00417 case SLE_VAR_U32: {
00418
00419 uint min = ((sdb->flags & SGF_0ISDISABLED) && (uint)val <= (uint)sdb->min) ? 0 : sdb->min;
00420 WriteValue(ptr, SLE_VAR_U32, (int64)ClampU(val, min, sdb->max));
00421 return;
00422 }
00423 case SLE_VAR_I64:
00424 case SLE_VAR_U64:
00425 default: NOT_REACHED();
00426 }
00427 }
00428
00429 WriteValue(ptr, sd->save.conv, (int64)val);
00430 }
00431
00440 static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00441 {
00442 IniGroup *group;
00443 IniGroup *group_def = ini->GetGroup(grpname);
00444 IniItem *item;
00445 const void *p;
00446 void *ptr;
00447 const char *s;
00448
00449 for (; sd->save.cmd != SL_END; sd++) {
00450 const SettingDescBase *sdb = &sd->desc;
00451 const SaveLoad *sld = &sd->save;
00452
00453 if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00454
00455
00456 s = strchr(sdb->name, '.');
00457 if (s != NULL) {
00458 group = ini->GetGroup(sdb->name, s - sdb->name);
00459 s++;
00460 } else {
00461 s = sdb->name;
00462 group = group_def;
00463 }
00464
00465 item = group->GetItem(s, false);
00466 if (item == NULL && group != group_def) {
00467
00468
00469 item = group_def->GetItem(s, false);
00470 }
00471 if (item == NULL) {
00472
00473
00474 const char *sc = strchr(s, '.');
00475 if (sc != NULL) item = ini->GetGroup(s, sc - s)->GetItem(sc + 1, false);
00476 }
00477
00478 p = (item == NULL) ? sdb->def : StringToVal(sdb, item->value);
00479 ptr = GetVariableAddress(object, sld);
00480
00481 switch (sdb->cmd) {
00482 case SDT_BOOLX:
00483 case SDT_NUMX:
00484 case SDT_ONEOFMANY:
00485 case SDT_MANYOFMANY:
00486 Write_ValidateSetting(ptr, sd, (int32)(size_t)p); break;
00487
00488 case SDT_STRING:
00489 switch (GetVarMemType(sld->conv)) {
00490 case SLE_VAR_STRB:
00491 case SLE_VAR_STRBQ:
00492 if (p != NULL) ttd_strlcpy((char*)ptr, (const char*)p, sld->length);
00493 break;
00494 case SLE_VAR_STR:
00495 case SLE_VAR_STRQ:
00496 free(*(char**)ptr);
00497 *(char**)ptr = p == NULL ? NULL : strdup((const char*)p);
00498 break;
00499 case SLE_VAR_CHAR: if (p != NULL) *(char*)ptr = *(char*)p; break;
00500 default: NOT_REACHED();
00501 }
00502 break;
00503
00504 case SDT_INTLIST: {
00505 if (!LoadIntList((const char*)p, ptr, sld->length, GetVarMemType(sld->conv))) {
00506 ShowInfoF("ini: error in array '%s'", sdb->name);
00507 } else if (sd->desc.proc_cnvt != NULL) {
00508 sd->desc.proc_cnvt((const char*)p);
00509 }
00510 break;
00511 }
00512 default: NOT_REACHED();
00513 }
00514 }
00515 }
00516
00529 static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00530 {
00531 IniGroup *group_def = NULL, *group;
00532 IniItem *item;
00533 char buf[512];
00534 const char *s;
00535 void *ptr;
00536
00537 for (; sd->save.cmd != SL_END; sd++) {
00538 const SettingDescBase *sdb = &sd->desc;
00539 const SaveLoad *sld = &sd->save;
00540
00541
00542
00543 if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00544 if (sld->conv & SLF_CONFIG_NO) continue;
00545
00546
00547 s = strchr(sdb->name, '.');
00548 if (s != NULL) {
00549 group = ini->GetGroup(sdb->name, s - sdb->name);
00550 s++;
00551 } else {
00552 if (group_def == NULL) group_def = ini->GetGroup(grpname);
00553 s = sdb->name;
00554 group = group_def;
00555 }
00556
00557 item = group->GetItem(s, true);
00558 ptr = GetVariableAddress(object, sld);
00559
00560 if (item->value != NULL) {
00561
00562 const void *p = StringToVal(sdb, item->value);
00563
00564
00565
00566 switch (sdb->cmd) {
00567 case SDT_BOOLX:
00568 case SDT_NUMX:
00569 case SDT_ONEOFMANY:
00570 case SDT_MANYOFMANY:
00571 switch (GetVarMemType(sld->conv)) {
00572 case SLE_VAR_BL:
00573 if (*(bool*)ptr == (p != NULL)) continue;
00574 break;
00575 case SLE_VAR_I8:
00576 case SLE_VAR_U8:
00577 if (*(byte*)ptr == (byte)(unsigned long)p) continue;
00578 break;
00579 case SLE_VAR_I16:
00580 case SLE_VAR_U16:
00581 if (*(uint16*)ptr == (uint16)(unsigned long)p) continue;
00582 break;
00583 case SLE_VAR_I32:
00584 case SLE_VAR_U32:
00585 if (*(uint32*)ptr == (uint32)(unsigned long)p) continue;
00586 break;
00587 default: NOT_REACHED();
00588 }
00589 break;
00590 default: break;
00591 }
00592 }
00593
00594
00595 switch (sdb->cmd) {
00596 case SDT_BOOLX:
00597 case SDT_NUMX:
00598 case SDT_ONEOFMANY:
00599 case SDT_MANYOFMANY: {
00600 uint32 i = (uint32)ReadValue(ptr, sld->conv);
00601
00602 switch (sdb->cmd) {
00603 case SDT_BOOLX: strecpy(buf, (i != 0) ? "true" : "false", lastof(buf)); break;
00604 case SDT_NUMX: seprintf(buf, lastof(buf), IsSignedVarMemType(sld->conv) ? "%d" : "%u", i); break;
00605 case SDT_ONEOFMANY: MakeOneOfMany(buf, lastof(buf), sdb->many, i); break;
00606 case SDT_MANYOFMANY: MakeManyOfMany(buf, lastof(buf), sdb->many, i); break;
00607 default: NOT_REACHED();
00608 }
00609 break;
00610 }
00611
00612 case SDT_STRING:
00613 switch (GetVarMemType(sld->conv)) {
00614 case SLE_VAR_STRB: strecpy(buf, (char*)ptr, lastof(buf)); break;
00615 case SLE_VAR_STRBQ:seprintf(buf, lastof(buf), "\"%s\"", (char*)ptr); break;
00616 case SLE_VAR_STR: strecpy(buf, *(char**)ptr, lastof(buf)); break;
00617 case SLE_VAR_STRQ:
00618 if (*(char**)ptr == NULL) {
00619 buf[0] = '\0';
00620 } else {
00621 seprintf(buf, lastof(buf), "\"%s\"", *(char**)ptr);
00622 }
00623 break;
00624 case SLE_VAR_CHAR: buf[0] = *(char*)ptr; buf[1] = '\0'; break;
00625 default: NOT_REACHED();
00626 }
00627 break;
00628
00629 case SDT_INTLIST:
00630 MakeIntList(buf, lastof(buf), ptr, sld->length, GetVarMemType(sld->conv));
00631 break;
00632 default: NOT_REACHED();
00633 }
00634
00635
00636 free(item->value);
00637 item->value = strdup(buf);
00638 }
00639 }
00640
00650 static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList *list)
00651 {
00652 IniGroup *group = ini->GetGroup(grpname);
00653
00654 if (group == NULL || list == NULL) return;
00655
00656 list->Clear();
00657
00658 for (const IniItem *item = group->item; item != NULL; item = item->next) {
00659 if (item->name != NULL) *list->Append() = strdup(item->name);
00660 }
00661 }
00662
00672 static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList *list)
00673 {
00674 IniGroup *group = ini->GetGroup(grpname);
00675
00676 if (group == NULL || list == NULL) return;
00677 group->Clear();
00678
00679 for (char **iter = list->Begin(); iter != list->End(); iter++) {
00680 group->GetItem(*iter, true)->SetValue("");
00681 }
00682 }
00683
00684
00685
00687 static bool v_PositionMainToolbar(int32 p1)
00688 {
00689 if (_game_mode != GM_MENU) PositionMainToolbar(NULL);
00690 return true;
00691 }
00692
00694 static bool v_PositionStatusbar(int32 p1)
00695 {
00696 if (_game_mode != GM_MENU) {
00697 PositionStatusbar(NULL);
00698 PositionNewsMessage(NULL);
00699 }
00700 return true;
00701 }
00702
00703 static bool PopulationInLabelActive(int32 p1)
00704 {
00705 UpdateAllTownVirtCoords();
00706 return true;
00707 }
00708
00709 static bool RedrawScreen(int32 p1)
00710 {
00711 MarkWholeScreenDirty();
00712 return true;
00713 }
00714
00720 static bool RedrawSmallmap(int32 p1)
00721 {
00722 BuildLandLegend();
00723 BuildOwnerLegend();
00724 SetWindowClassesDirty(WC_SMALLMAP);
00725 return true;
00726 }
00727
00728 static bool InvalidateDetailsWindow(int32 p1)
00729 {
00730 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00731 return true;
00732 }
00733
00734 static bool InvalidateStationBuildWindow(int32 p1)
00735 {
00736 SetWindowDirty(WC_BUILD_STATION, 0);
00737 return true;
00738 }
00739
00740 static bool InvalidateBuildIndustryWindow(int32 p1)
00741 {
00742 InvalidateWindowData(WC_BUILD_INDUSTRY, 0);
00743 return true;
00744 }
00745
00746 static bool CloseSignalGUI(int32 p1)
00747 {
00748 if (p1 == 0) {
00749 DeleteWindowByClass(WC_BUILD_SIGNAL);
00750 }
00751 return true;
00752 }
00753
00754 static bool InvalidateTownViewWindow(int32 p1)
00755 {
00756 InvalidateWindowClassesData(WC_TOWN_VIEW, p1);
00757 return true;
00758 }
00759
00760 static bool DeleteSelectStationWindow(int32 p1)
00761 {
00762 DeleteWindowById(WC_SELECT_STATION, 0);
00763 return true;
00764 }
00765
00766 static bool UpdateConsists(int32 p1)
00767 {
00768 Train *t;
00769 FOR_ALL_TRAINS(t) {
00770
00771 if (t->IsFrontEngine() || t->IsFreeWagon()) t->ConsistChanged(true);
00772 }
00773 return true;
00774 }
00775
00776
00777 static bool CheckInterval(int32 p1)
00778 {
00779 VehicleDefaultSettings *vds;
00780 if (_game_mode == GM_MENU || !Company::IsValidID(_current_company)) {
00781 vds = &_settings_client.company.vehicle;
00782 } else {
00783 vds = &Company::Get(_current_company)->settings.vehicle;
00784 }
00785
00786 if (p1) {
00787 vds->servint_trains = 50;
00788 vds->servint_roadveh = 50;
00789 vds->servint_aircraft = 50;
00790 vds->servint_ships = 50;
00791 } else {
00792 vds->servint_trains = 150;
00793 vds->servint_roadveh = 150;
00794 vds->servint_aircraft = 100;
00795 vds->servint_ships = 360;
00796 }
00797
00798 InvalidateDetailsWindow(0);
00799
00800 return true;
00801 }
00802
00803 static bool TrainAccelerationModelChanged(int32 p1)
00804 {
00805 Train *t;
00806 FOR_ALL_TRAINS(t) {
00807 if (t->IsFrontEngine()) {
00808 t->tcache.cached_max_curve_speed = t->GetCurveSpeedLimit();
00809 t->UpdateAcceleration();
00810 }
00811 }
00812
00813
00814 SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00815 InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00816 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00817
00818 return true;
00819 }
00820
00826 static bool TrainSlopeSteepnessChanged(int32 p1)
00827 {
00828 Train *t;
00829 FOR_ALL_TRAINS(t) {
00830 if (t->IsFrontEngine()) t->CargoChanged();
00831 }
00832
00833 return true;
00834 }
00835
00841 static bool RoadVehAccelerationModelChanged(int32 p1)
00842 {
00843 if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) {
00844 RoadVehicle *rv;
00845 FOR_ALL_ROADVEHICLES(rv) {
00846 if (rv->IsRoadVehFront()) {
00847 rv->CargoChanged();
00848 }
00849 }
00850 }
00851
00852
00853 SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00854 InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00855 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00856
00857 return true;
00858 }
00859
00865 static bool RoadVehSlopeSteepnessChanged(int32 p1)
00866 {
00867 RoadVehicle *rv;
00868 FOR_ALL_ROADVEHICLES(rv) {
00869 if (rv->IsRoadVehFront()) rv->CargoChanged();
00870 }
00871
00872 return true;
00873 }
00874
00875 static bool DragSignalsDensityChanged(int32)
00876 {
00877 InvalidateWindowData(WC_BUILD_SIGNAL, 0);
00878
00879 return true;
00880 }
00881
00882 static bool TownFoundingChanged(int32 p1)
00883 {
00884 if (_game_mode != GM_EDITOR && _settings_game.economy.found_town == TF_FORBIDDEN) {
00885 DeleteWindowById(WC_FOUND_TOWN, 0);
00886 return true;
00887 }
00888 InvalidateWindowData(WC_FOUND_TOWN, 0);
00889 return true;
00890 }
00891
00892 static bool InvalidateVehTimetableWindow(int32 p1)
00893 {
00894 InvalidateWindowClassesData(WC_VEHICLE_TIMETABLE, -2);
00895 return true;
00896 }
00897
00905 static bool InvalidateNewGRFChangeWindows(int32 p1)
00906 {
00907 InvalidateWindowClassesData(WC_SAVELOAD);
00908 DeleteWindowByClass(WC_GAME_OPTIONS);
00909 ReInitAllWindows();
00910 return true;
00911 }
00912
00913 static bool InvalidateCompanyLiveryWindow(int32 p1)
00914 {
00915 InvalidateWindowClassesData(WC_COMPANY_COLOUR);
00916 return RedrawScreen(p1);
00917 }
00918
00919 static bool InvalidateIndustryViewWindow(int32 p1)
00920 {
00921 InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
00922 return true;
00923 }
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946 static const DifficultySettings _default_game_diff[3] = {
00947
00948 {2, 2, 4, 300000, 2, 0, 2, 1, 2, 0, 1, 0, 0, 0, 0, 0, 0},
00949 {4, 2, 3, 150000, 3, 1, 3, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1},
00950 {7, 3, 3, 100000, 4, 1, 3, 2, 0, 2, 3, 2, 1, 1, 1, 2, 2},
00951 };
00952
00953 void SetDifficultyLevel(int mode, DifficultySettings *gm_opt)
00954 {
00955 assert(mode <= 3);
00956
00957 if (mode != 3) {
00958 *gm_opt = _default_game_diff[mode];
00959 } else {
00960 gm_opt->diff_level = 3;
00961 }
00962 }
00963
00965 static void ValidateSettings()
00966 {
00967
00968 if (_settings_newgame.difficulty.diff_level != 3) {
00969 SetDifficultyLevel(_settings_newgame.difficulty.diff_level, &_settings_newgame.difficulty);
00970 }
00971
00972
00973 if (_settings_newgame.game_creation.land_generator == 0 &&
00974 _settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
00975 _settings_newgame.difficulty.quantity_sea_lakes = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE;
00976 }
00977 }
00978
00979 static bool DifficultyReset(int32 level)
00980 {
00981
00982
00983 if (_game_mode != GM_MENU && level != 3) return false;
00984 SetDifficultyLevel(level, &GetGameSettings().difficulty);
00985 return true;
00986 }
00987
00988 static bool DifficultyChange(int32)
00989 {
00990 if (_game_mode == GM_MENU) {
00991 if (_settings_newgame.difficulty.diff_level != 3) {
00992 ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, WL_WARNING);
00993 _settings_newgame.difficulty.diff_level = 3;
00994 }
00995 SetWindowClassesDirty(WC_SELECT_GAME);
00996 } else {
00997 _settings_game.difficulty.diff_level = 3;
00998 }
00999
01000
01001
01002
01003 if (_networking && FindWindowById(WC_GAME_OPTIONS, 0) != NULL) {
01004 ShowGameDifficulty();
01005 }
01006
01007 return true;
01008 }
01009
01010 static bool DifficultyNoiseChange(int32 i)
01011 {
01012 if (_game_mode == GM_NORMAL) {
01013 UpdateAirportsNoise();
01014 if (_settings_game.economy.station_noise_level) {
01015 InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
01016 }
01017 }
01018
01019 return DifficultyChange(i);
01020 }
01021
01022 static bool MaxNoAIsChange(int32 i)
01023 {
01024 if (GetGameSettings().difficulty.max_no_competitors != 0 &&
01025 #ifdef ENABLE_AI
01026 AI::GetInfoList()->size() == 0 &&
01027 #endif
01028 (!_networking || _network_server)) {
01029 ShowErrorMessage(STR_WARNING_NO_SUITABLE_AI, INVALID_STRING_ID, WL_CRITICAL);
01030 }
01031
01032 return DifficultyChange(i);
01033 }
01034
01040 static bool CheckRoadSide(int p1)
01041 {
01042 extern bool RoadVehiclesAreBuilt();
01043 return _game_mode == GM_MENU || !RoadVehiclesAreBuilt();
01044 }
01045
01053 static int32 ConvertLandscape(const char *value)
01054 {
01055
01056 return LookupOneOfMany("normal|hilly|desert|candy", value);
01057 }
01058
01059 static bool CheckFreeformEdges(int32 p1)
01060 {
01061 if (_game_mode == GM_MENU) return true;
01062 if (p1 != 0) {
01063 Ship *s;
01064 FOR_ALL_SHIPS(s) {
01065 if (TileX(s->tile) == 0 || TileY(s->tile) == 0) {
01066 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01067 return false;
01068 }
01069 }
01070 Station *st;
01071 FOR_ALL_STATIONS(st) {
01072 if (TileX(st->xy) == 0 || TileY(st->xy) == 0) {
01073 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01074 return false;
01075 }
01076 }
01077 for (uint i = 0; i < MapSizeX(); i++) MakeVoid(TileXY(i, 0));
01078 for (uint i = 0; i < MapSizeY(); i++) MakeVoid(TileXY(0, i));
01079 } else {
01080 for (uint i = 0; i < MapMaxX(); i++) {
01081 if (TileHeight(TileXY(i, 1)) != 0) {
01082 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01083 return false;
01084 }
01085 }
01086 for (uint i = 1; i < MapMaxX(); i++) {
01087 if (!IsTileType(TileXY(i, MapMaxY() - 1), MP_WATER) || TileHeight(TileXY(1, MapMaxY())) != 0) {
01088 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01089 return false;
01090 }
01091 }
01092 for (uint i = 0; i < MapMaxY(); i++) {
01093 if (TileHeight(TileXY(1, i)) != 0) {
01094 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01095 return false;
01096 }
01097 }
01098 for (uint i = 1; i < MapMaxY(); i++) {
01099 if (!IsTileType(TileXY(MapMaxX() - 1, i), MP_WATER) || TileHeight(TileXY(MapMaxX(), i)) != 0) {
01100 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01101 return false;
01102 }
01103 }
01104
01105 for (uint i = 0; i < MapMaxX(); i++) {
01106 SetTileHeight(TileXY(i, 0), 0);
01107 SetTileType(TileXY(i, 0), MP_WATER);
01108 }
01109 for (uint i = 0; i < MapMaxY(); i++) {
01110 SetTileHeight(TileXY(0, i), 0);
01111 SetTileType(TileXY(0, i), MP_WATER);
01112 }
01113 }
01114 MarkWholeScreenDirty();
01115 return true;
01116 }
01117
01122 static bool ChangeDynamicEngines(int32 p1)
01123 {
01124 if (_game_mode == GM_MENU) return true;
01125
01126 const Vehicle *v;
01127 FOR_ALL_VEHICLES(v) {
01128 if (IsCompanyBuildableVehicleType(v)) {
01129 ShowErrorMessage(STR_CONFIG_SETTING_DYNAMIC_ENGINES_EXISTING_VEHICLES, INVALID_STRING_ID, WL_ERROR);
01130 return false;
01131 }
01132 }
01133
01134
01135 _engine_mngr.ResetToDefaultMapping();
01136 ReloadNewGRFData();
01137
01138 return true;
01139 }
01140
01141 static bool StationCatchmentChanged(int32 p1)
01142 {
01143 Station::RecomputeIndustriesNearForAll();
01144 return true;
01145 }
01146
01147 #ifdef ENABLE_NETWORK
01148
01149 static bool UpdateClientName(int32 p1)
01150 {
01151 NetworkUpdateClientName();
01152 return true;
01153 }
01154
01155 static bool UpdateServerPassword(int32 p1)
01156 {
01157 if (strcmp(_settings_client.network.server_password, "*") == 0) {
01158 _settings_client.network.server_password[0] = '\0';
01159 }
01160
01161 return true;
01162 }
01163
01164 static bool UpdateRconPassword(int32 p1)
01165 {
01166 if (strcmp(_settings_client.network.rcon_password, "*") == 0) {
01167 _settings_client.network.rcon_password[0] = '\0';
01168 }
01169
01170 return true;
01171 }
01172
01173 static bool UpdateClientConfigValues(int32 p1)
01174 {
01175 if (_network_server) NetworkServerSendConfigUpdate();
01176
01177 return true;
01178 }
01179
01180 #endif
01181
01182
01183
01184
01188 static void PrepareOldDiffCustom()
01189 {
01190 memset(_old_diff_custom, 0, sizeof(_old_diff_custom));
01191 }
01192
01199 static void HandleOldDiffCustom(bool savegame)
01200 {
01201 uint options_to_load = GAME_DIFFICULTY_NUM - ((savegame && IsSavegameVersionBefore(4)) ? 1 : 0);
01202
01203 if (!savegame) {
01204
01205 bool old_diff_custom_used = false;
01206 for (uint i = 0; i < options_to_load && !old_diff_custom_used; i++) {
01207 old_diff_custom_used = (_old_diff_custom[i] != 0);
01208 }
01209
01210 if (!old_diff_custom_used) return;
01211 }
01212
01213 for (uint i = 0; i < options_to_load; i++) {
01214 const SettingDesc *sd = &_settings[i];
01215
01216 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01217 void *var = GetVariableAddress(savegame ? &_settings_game : &_settings_newgame, &sd->save);
01218 Write_ValidateSetting(var, sd, (int32)((i == 4 ? 1000 : 1) * _old_diff_custom[i]));
01219 }
01220 }
01221
01228 static bool ConvertOldNewsSetting(const char *name, const char *value)
01229 {
01230 if (strcasecmp(name, "openclose") == 0) {
01231
01232
01233
01234
01235 NewsDisplay display = ND_OFF;
01236 if (strcasecmp(value, "full") == 0) {
01237 display = ND_FULL;
01238 } else if (strcasecmp(value, "summarized") == 0) {
01239 display = ND_SUMMARY;
01240 }
01241
01242 _news_type_data[NT_INDUSTRY_OPEN].display = display;
01243 _news_type_data[NT_INDUSTRY_CLOSE].display = display;
01244 return true;
01245 }
01246 return false;
01247 }
01248
01254 static void NewsDisplayLoadConfig(IniFile *ini, const char *grpname)
01255 {
01256 IniGroup *group = ini->GetGroup(grpname);
01257 IniItem *item;
01258
01259
01260 if (group == NULL) return;
01261
01262 for (item = group->item; item != NULL; item = item->next) {
01263 int news_item = -1;
01264 for (int i = 0; i < NT_END; i++) {
01265 if (strcasecmp(item->name, _news_type_data[i].name) == 0) {
01266 news_item = i;
01267 break;
01268 }
01269 }
01270
01271
01272 if (news_item == -1) {
01273
01274 if (!ConvertOldNewsSetting(item->name, item->value)) {
01275 DEBUG(misc, 0, "Invalid display option: %s", item->name);
01276 }
01277
01278 continue;
01279 }
01280
01281 if (StrEmpty(item->value)) {
01282 DEBUG(misc, 0, "Empty display value for newstype %s", item->name);
01283 continue;
01284 } else if (strcasecmp(item->value, "full") == 0) {
01285 _news_type_data[news_item].display = ND_FULL;
01286 } else if (strcasecmp(item->value, "off") == 0) {
01287 _news_type_data[news_item].display = ND_OFF;
01288 } else if (strcasecmp(item->value, "summarized") == 0) {
01289 _news_type_data[news_item].display = ND_SUMMARY;
01290 } else {
01291 DEBUG(misc, 0, "Invalid display value for newstype %s: %s", item->name, item->value);
01292 continue;
01293 }
01294 }
01295 }
01296
01297 static void AILoadConfig(IniFile *ini, const char *grpname)
01298 {
01299 #ifdef ENABLE_AI
01300 IniGroup *group = ini->GetGroup(grpname);
01301 IniItem *item;
01302
01303
01304 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01305 AIConfig::GetConfig(c, AIConfig::AISS_FORCE_NEWGAME)->ChangeAI(NULL);
01306 }
01307
01308
01309 if (group == NULL) return;
01310
01311 CompanyID c = COMPANY_FIRST;
01312 for (item = group->item; c < MAX_COMPANIES && item != NULL; c++, item = item->next) {
01313 AIConfig *config = AIConfig::GetConfig(c, AIConfig::AISS_FORCE_NEWGAME);
01314
01315 config->ChangeAI(item->name);
01316 if (!config->HasAI()) {
01317 if (strcmp(item->name, "none") != 0) {
01318 DEBUG(ai, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name);
01319 continue;
01320 }
01321 }
01322 if (item->value != NULL) config->StringToSettings(item->value);
01323 }
01324 #endif
01325 }
01326
01333 static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_static)
01334 {
01335 IniGroup *group = ini->GetGroup(grpname);
01336 IniItem *item;
01337 GRFConfig *first = NULL;
01338 GRFConfig **curr = &first;
01339
01340 if (group == NULL) return NULL;
01341
01342 for (item = group->item; item != NULL; item = item->next) {
01343 GRFConfig *c = new GRFConfig(item->name);
01344
01345
01346 if (!StrEmpty(item->value)) {
01347 c->num_params = ParseIntList(item->value, (int*)c->param, lengthof(c->param));
01348 if (c->num_params == (byte)-1) {
01349 ShowInfoF("ini: error in array '%s'", item->name);
01350 c->num_params = 0;
01351 }
01352 }
01353
01354
01355 if (!FillGRFDetails(c, is_static)) {
01356 const char *msg;
01357
01358 if (c->status == GCS_NOT_FOUND) {
01359 msg = "not found";
01360 } else if (HasBit(c->flags, GCF_UNSAFE)) {
01361 msg = "unsafe for static use";
01362 } else if (HasBit(c->flags, GCF_SYSTEM)) {
01363 msg = "system NewGRF";
01364 } else {
01365 msg = "unknown";
01366 }
01367
01368 ShowInfoF("ini: ignoring invalid NewGRF '%s': %s", item->name, msg);
01369 delete c;
01370 continue;
01371 }
01372
01373
01374 bool duplicate = false;
01375 for (const GRFConfig *gc = first; gc != NULL; gc = gc->next) {
01376 if (gc->ident.grfid == c->ident.grfid) {
01377 ShowInfoF("ini: ignoring NewGRF '%s': duplicate GRF ID with '%s'", item->name, gc->filename);
01378 duplicate = true;
01379 break;
01380 }
01381 }
01382 if (duplicate) {
01383 delete c;
01384 continue;
01385 }
01386
01387
01388 if (is_static) SetBit(c->flags, GCF_STATIC);
01389
01390
01391 *curr = c;
01392 curr = &c->next;
01393 }
01394
01395 return first;
01396 }
01397
01403 static void NewsDisplaySaveConfig(IniFile *ini, const char *grpname)
01404 {
01405 IniGroup *group = ini->GetGroup(grpname);
01406
01407 for (int i = 0; i < NT_END; i++) {
01408 const char *value;
01409 int v = _news_type_data[i].display;
01410
01411 value = (v == ND_OFF ? "off" : (v == ND_SUMMARY ? "summarized" : "full"));
01412
01413 group->GetItem(_news_type_data[i].name, true)->SetValue(value);
01414 }
01415 }
01416
01417 static void AISaveConfig(IniFile *ini, const char *grpname)
01418 {
01419 #ifdef ENABLE_AI
01420 IniGroup *group = ini->GetGroup(grpname);
01421
01422 if (group == NULL) return;
01423 group->Clear();
01424
01425 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01426 AIConfig *config = AIConfig::GetConfig(c, AIConfig::AISS_FORCE_NEWGAME);
01427 const char *name;
01428 char value[1024];
01429 config->SettingsToString(value, lengthof(value));
01430
01431 if (config->HasAI()) {
01432 name = config->GetName();
01433 } else {
01434 name = "none";
01435 }
01436
01437 IniItem *item = new IniItem(group, name, strlen(name));
01438 item->SetValue(value);
01439 }
01440 #endif
01441 }
01442
01447 static void SaveVersionInConfig(IniFile *ini)
01448 {
01449 IniGroup *group = ini->GetGroup("version");
01450
01451 char version[9];
01452 snprintf(version, lengthof(version), "%08X", _openttd_newgrf_version);
01453
01454 const char * const versions[][2] = {
01455 { "version_string", _openttd_revision },
01456 { "version_number", version }
01457 };
01458
01459 for (uint i = 0; i < lengthof(versions); i++) {
01460 group->GetItem(versions[i][0], true)->SetValue(versions[i][1]);
01461 }
01462 }
01463
01464
01465 static void GRFSaveConfig(IniFile *ini, const char *grpname, const GRFConfig *list)
01466 {
01467 ini->RemoveGroup(grpname);
01468 IniGroup *group = ini->GetGroup(grpname);
01469 const GRFConfig *c;
01470
01471 for (c = list; c != NULL; c = c->next) {
01472 char params[512];
01473 GRFBuildParamList(params, c, lastof(params));
01474
01475 group->GetItem(c->filename, true)->SetValue(params);
01476 }
01477 }
01478
01479
01480 static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list)
01481 {
01482 proc(ini, (const SettingDesc*)_misc_settings, "misc", NULL);
01483 proc(ini, (const SettingDesc*)_music_settings, "music", &_msf);
01484 #if defined(WIN32) && !defined(DEDICATED)
01485 proc(ini, (const SettingDesc*)_win32_settings, "win32", NULL);
01486 #endif
01487
01488 proc(ini, _settings, "patches", &_settings_newgame);
01489 proc(ini, _currency_settings,"currency", &_custom_currency);
01490 proc(ini, _company_settings, "company", &_settings_client.company);
01491
01492 #ifdef ENABLE_NETWORK
01493 proc_list(ini, "server_bind_addresses", &_network_bind_list);
01494 proc_list(ini, "servers", &_network_host_list);
01495 proc_list(ini, "bans", &_network_ban_list);
01496 #endif
01497 }
01498
01499 static IniFile *IniLoadConfig()
01500 {
01501 IniFile *ini = new IniFile(_list_group_names);
01502 ini->LoadFromDisk(_config_file);
01503 return ini;
01504 }
01505
01507 void LoadFromConfig()
01508 {
01509 IniFile *ini = IniLoadConfig();
01510 ResetCurrencies(false);
01511
01512 HandleSettingDescs(ini, IniLoadSettings, IniLoadSettingList);
01513 _grfconfig_newgame = GRFLoadConfig(ini, "newgrf", false);
01514 _grfconfig_static = GRFLoadConfig(ini, "newgrf-static", true);
01515 NewsDisplayLoadConfig(ini, "news_display");
01516 AILoadConfig(ini, "ai_players");
01517
01518 PrepareOldDiffCustom();
01519 IniLoadSettings(ini, _gameopt_settings, "gameopt", &_settings_newgame);
01520 HandleOldDiffCustom(false);
01521
01522 ValidateSettings();
01523 delete ini;
01524 }
01525
01527 void SaveToConfig()
01528 {
01529 IniFile *ini = IniLoadConfig();
01530
01531
01532 ini->RemoveGroup("patches");
01533 ini->RemoveGroup("yapf");
01534 ini->RemoveGroup("gameopt");
01535
01536 HandleSettingDescs(ini, IniSaveSettings, IniSaveSettingList);
01537 GRFSaveConfig(ini, "newgrf", _grfconfig_newgame);
01538 GRFSaveConfig(ini, "newgrf-static", _grfconfig_static);
01539 NewsDisplaySaveConfig(ini, "news_display");
01540 AISaveConfig(ini, "ai_players");
01541 SaveVersionInConfig(ini);
01542 ini->SaveToDisk(_config_file);
01543 delete ini;
01544 }
01545
01550 void GetGRFPresetList(GRFPresetList *list)
01551 {
01552 list->Clear();
01553
01554 IniFile *ini = IniLoadConfig();
01555 IniGroup *group;
01556 for (group = ini->group; group != NULL; group = group->next) {
01557 if (strncmp(group->name, "preset-", 7) == 0) {
01558 *list->Append() = strdup(group->name + 7);
01559 }
01560 }
01561
01562 delete ini;
01563 }
01564
01571 GRFConfig *LoadGRFPresetFromConfig(const char *config_name)
01572 {
01573 char *section = (char*)alloca(strlen(config_name) + 8);
01574 sprintf(section, "preset-%s", config_name);
01575
01576 IniFile *ini = IniLoadConfig();
01577 GRFConfig *config = GRFLoadConfig(ini, section, false);
01578 delete ini;
01579
01580 return config;
01581 }
01582
01589 void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
01590 {
01591 char *section = (char*)alloca(strlen(config_name) + 8);
01592 sprintf(section, "preset-%s", config_name);
01593
01594 IniFile *ini = IniLoadConfig();
01595 GRFSaveConfig(ini, section, config);
01596 ini->SaveToDisk(_config_file);
01597 delete ini;
01598 }
01599
01604 void DeleteGRFPresetFromConfig(const char *config_name)
01605 {
01606 char *section = (char*)alloca(strlen(config_name) + 8);
01607 sprintf(section, "preset-%s", config_name);
01608
01609 IniFile *ini = IniLoadConfig();
01610 ini->RemoveGroup(section);
01611 ini->SaveToDisk(_config_file);
01612 delete ini;
01613 }
01614
01615 static const SettingDesc *GetSettingDescription(uint index)
01616 {
01617 if (index >= lengthof(_settings)) return NULL;
01618 return &_settings[index];
01619 }
01620
01632 CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01633 {
01634 const SettingDesc *sd = GetSettingDescription(p1);
01635
01636 if (sd == NULL) return CMD_ERROR;
01637 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR;
01638
01639 if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return CMD_ERROR;
01640 if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return CMD_ERROR;
01641 if ((sd->desc.flags & SGF_NEWGAME_ONLY) &&
01642 (_game_mode == GM_NORMAL ||
01643 (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0))) {
01644 return CMD_ERROR;
01645 }
01646
01647 if (flags & DC_EXEC) {
01648 void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01649
01650 int32 oldval = (int32)ReadValue(var, sd->save.conv);
01651 int32 newval = (int32)p2;
01652
01653 Write_ValidateSetting(var, sd, newval);
01654 newval = (int32)ReadValue(var, sd->save.conv);
01655
01656 if (oldval == newval) return CommandCost();
01657
01658 if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01659 WriteValue(var, sd->save.conv, (int64)oldval);
01660 return CommandCost();
01661 }
01662
01663 if (sd->desc.flags & SGF_NO_NETWORK) {
01664 GamelogStartAction(GLAT_SETTING);
01665 GamelogSetting(sd->desc.name, oldval, newval);
01666 GamelogStopAction();
01667 }
01668
01669 SetWindowDirty(WC_GAME_OPTIONS, 0);
01670 }
01671
01672 return CommandCost();
01673 }
01674
01685 CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01686 {
01687 if (p1 >= lengthof(_company_settings)) return CMD_ERROR;
01688 const SettingDesc *sd = &_company_settings[p1];
01689
01690 if (flags & DC_EXEC) {
01691 void *var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01692
01693 int32 oldval = (int32)ReadValue(var, sd->save.conv);
01694 int32 newval = (int32)p2;
01695
01696 Write_ValidateSetting(var, sd, newval);
01697 newval = (int32)ReadValue(var, sd->save.conv);
01698
01699 if (oldval == newval) return CommandCost();
01700
01701 if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01702 WriteValue(var, sd->save.conv, (int64)oldval);
01703 return CommandCost();
01704 }
01705
01706 SetWindowDirty(WC_GAME_OPTIONS, 0);
01707 }
01708
01709 return CommandCost();
01710 }
01711
01719 bool SetSettingValue(uint index, int32 value, bool force_newgame)
01720 {
01721 const SettingDesc *sd = &_settings[index];
01722
01723
01724
01725
01726 if (sd->save.conv & SLF_NETWORK_NO) {
01727 void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01728 Write_ValidateSetting(var, sd, value);
01729
01730 if (_game_mode != GM_MENU) {
01731 void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01732 Write_ValidateSetting(var2, sd, value);
01733 }
01734 if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01735 SetWindowDirty(WC_GAME_OPTIONS, 0);
01736 return true;
01737 }
01738
01739 if (force_newgame) {
01740 void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01741 Write_ValidateSetting(var2, sd, value);
01742 return true;
01743 }
01744
01745
01746 if (!_networking || (_networking && _network_server)) {
01747 return DoCommandP(0, index, value, CMD_CHANGE_SETTING);
01748 }
01749 return false;
01750 }
01751
01758 void SetCompanySetting(uint index, int32 value)
01759 {
01760 const SettingDesc *sd = &_company_settings[index];
01761 if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01762 DoCommandP(0, index, value, CMD_CHANGE_COMPANY_SETTING);
01763 } else {
01764 void *var = GetVariableAddress(&_settings_client.company, &sd->save);
01765 Write_ValidateSetting(var, sd, value);
01766 if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01767 }
01768 }
01769
01773 void SetDefaultCompanySettings(CompanyID cid)
01774 {
01775 Company *c = Company::Get(cid);
01776 const SettingDesc *sd;
01777 for (sd = _company_settings; sd->save.cmd != SL_END; sd++) {
01778 void *var = GetVariableAddress(&c->settings, &sd->save);
01779 Write_ValidateSetting(var, sd, (int32)(size_t)sd->desc.def);
01780 }
01781 }
01782
01783 #if defined(ENABLE_NETWORK)
01784
01787 void SyncCompanySettings()
01788 {
01789 const SettingDesc *sd;
01790 uint i = 0;
01791 for (sd = _company_settings; sd->save.cmd != SL_END; sd++, i++) {
01792 const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01793 const void *new_var = GetVariableAddress(&_settings_client.company, &sd->save);
01794 uint32 old_value = (uint32)ReadValue(old_var, sd->save.conv);
01795 uint32 new_value = (uint32)ReadValue(new_var, sd->save.conv);
01796 if (old_value != new_value) NetworkSendCommand(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, NULL, NULL, _local_company);
01797 }
01798 }
01799 #endif
01800
01806 uint GetCompanySettingIndex(const char *name)
01807 {
01808 uint i;
01809 const SettingDesc *sd = GetSettingFromName(name, &i);
01810 assert(sd != NULL && (sd->desc.flags & SGF_PER_COMPANY) != 0);
01811 return i;
01812 }
01813
01821 bool SetSettingValue(uint index, const char *value, bool force_newgame)
01822 {
01823 const SettingDesc *sd = &_settings[index];
01824 assert(sd->save.conv & SLF_NETWORK_NO);
01825
01826 if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) {
01827 char **var = (char**)GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
01828 free(*var);
01829 *var = strcmp(value, "(null)") == 0 ? NULL : strdup(value);
01830 } else {
01831 char *var = (char*)GetVariableAddress(NULL, &sd->save);
01832 ttd_strlcpy(var, value, sd->save.length);
01833 }
01834 if (sd->desc.proc != NULL) sd->desc.proc(0);
01835
01836 return true;
01837 }
01838
01846 const SettingDesc *GetSettingFromName(const char *name, uint *i)
01847 {
01848 const SettingDesc *sd;
01849
01850
01851 for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01852 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01853 if (strcmp(sd->desc.name, name) == 0) return sd;
01854 }
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 const char *short_name = strchr(sd->desc.name, '.');
01860 if (short_name != NULL) {
01861 short_name++;
01862 if (strcmp(short_name, name) == 0) return sd;
01863 }
01864 }
01865
01866 if (strncmp(name, "company.", 8) == 0) name += 8;
01867
01868 for (*i = 0, sd = _company_settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01869 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01870 if (strcmp(sd->desc.name, name) == 0) return sd;
01871 }
01872
01873 return NULL;
01874 }
01875
01876
01877
01878 void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
01879 {
01880 uint index;
01881 const SettingDesc *sd = GetSettingFromName(name, &index);
01882
01883 if (sd == NULL) {
01884 IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01885 return;
01886 }
01887
01888 bool success;
01889 if (sd->desc.cmd == SDT_STRING) {
01890 success = SetSettingValue(index, value, force_newgame);
01891 } else {
01892 uint32 val;
01893 extern bool GetArgumentInteger(uint32 *value, const char *arg);
01894 success = GetArgumentInteger(&val, value);
01895 if (!success) {
01896 IConsolePrintF(CC_ERROR, "'%s' is not an integer.", value);
01897 return;
01898 }
01899
01900 success = SetSettingValue(index, val, force_newgame);
01901 }
01902
01903 if (!success) {
01904 if (_network_server) {
01905 IConsoleError("This command/variable is not available during network games.");
01906 } else {
01907 IConsoleError("This command/variable is only available to a network server.");
01908 }
01909 }
01910 }
01911
01912 void IConsoleSetSetting(const char *name, int value)
01913 {
01914 uint index;
01915 const SettingDesc *sd = GetSettingFromName(name, &index);
01916 assert(sd != NULL);
01917 SetSettingValue(index, value);
01918 }
01919
01925 void IConsoleGetSetting(const char *name, bool force_newgame)
01926 {
01927 char value[20];
01928 uint index;
01929 const SettingDesc *sd = GetSettingFromName(name, &index);
01930 const void *ptr;
01931
01932 if (sd == NULL) {
01933 IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01934 return;
01935 }
01936
01937 ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
01938
01939 if (sd->desc.cmd == SDT_STRING) {
01940 IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char **)ptr : (const char *)ptr);
01941 } else {
01942 if (sd->desc.cmd == SDT_BOOLX) {
01943 snprintf(value, sizeof(value), (*(bool*)ptr == 1) ? "on" : "off");
01944 } else {
01945 snprintf(value, sizeof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
01946 }
01947
01948 IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s' (min: %s%d, max: %u)",
01949 name, value, (sd->desc.flags & SGF_0ISDISABLED) ? "(0) " : "", sd->desc.min, sd->desc.max);
01950 }
01951 }
01952
01958 void IConsoleListSettings(const char *prefilter)
01959 {
01960 IConsolePrintF(CC_WARNING, "All settings with their current value:");
01961
01962 for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) {
01963 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01964 if (prefilter != NULL && strstr(sd->desc.name, prefilter) == NULL) continue;
01965 char value[80];
01966 const void *ptr = GetVariableAddress(&GetGameSettings(), &sd->save);
01967
01968 if (sd->desc.cmd == SDT_BOOLX) {
01969 snprintf(value, lengthof(value), (*(bool*)ptr == 1) ? "on" : "off");
01970 } else if (sd->desc.cmd == SDT_STRING) {
01971 snprintf(value, sizeof(value), "%s", (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char **)ptr : (const char *)ptr);
01972 } else {
01973 snprintf(value, lengthof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
01974 }
01975 IConsolePrintF(CC_DEFAULT, "%s = %s", sd->desc.name, value);
01976 }
01977
01978 IConsolePrintF(CC_WARNING, "Use 'setting' command to change a value");
01979 }
01980
01987 static void LoadSettings(const SettingDesc *osd, void *object)
01988 {
01989 for (; osd->save.cmd != SL_END; osd++) {
01990 const SaveLoad *sld = &osd->save;
01991 void *ptr = GetVariableAddress(object, sld);
01992
01993 if (!SlObjectMember(ptr, sld)) continue;
01994 if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, osd, ReadValue(ptr, sld->conv));
01995 }
01996 }
01997
02004 static void SaveSettings(const SettingDesc *sd, void *object)
02005 {
02006
02007
02008 const SettingDesc *i;
02009 size_t length = 0;
02010 for (i = sd; i->save.cmd != SL_END; i++) {
02011 length += SlCalcObjMemberLength(object, &i->save);
02012 }
02013 SlSetLength(length);
02014
02015 for (i = sd; i->save.cmd != SL_END; i++) {
02016 void *ptr = GetVariableAddress(object, &i->save);
02017 SlObjectMember(ptr, &i->save);
02018 }
02019 }
02020
02021 static void Load_OPTS()
02022 {
02023
02024
02025
02026 PrepareOldDiffCustom();
02027 LoadSettings(_gameopt_settings, &_settings_game);
02028 HandleOldDiffCustom(true);
02029 }
02030
02031 static void Load_PATS()
02032 {
02033
02034
02035
02036 LoadSettings(_settings, &_settings_game);
02037 }
02038
02039 static void Check_PATS()
02040 {
02041 LoadSettings(_settings, &_load_check_data.settings);
02042 }
02043
02044 static void Save_PATS()
02045 {
02046 SaveSettings(_settings, &_settings_game);
02047 }
02048
02049 void CheckConfig()
02050 {
02051
02052
02053
02054
02055 if (_settings_newgame.pf.opf.pf_maxdepth == 16 && _settings_newgame.pf.opf.pf_maxlength == 512) {
02056 _settings_newgame.pf.opf.pf_maxdepth = 48;
02057 _settings_newgame.pf.opf.pf_maxlength = 4096;
02058 }
02059 }
02060
02061 extern const ChunkHandler _setting_chunk_handlers[] = {
02062 { 'OPTS', NULL, Load_OPTS, NULL, NULL, CH_RIFF},
02063 { 'PATS', Save_PATS, Load_PATS, NULL, Check_PATS, CH_RIFF | CH_LAST},
02064 };
02065
02066 static bool IsSignedVarMemType(VarType vt)
02067 {
02068 switch (GetVarMemType(vt)) {
02069 case SLE_VAR_I8:
02070 case SLE_VAR_I16:
02071 case SLE_VAR_I32:
02072 case SLE_VAR_I64:
02073 return true;
02074 }
02075 return false;
02076 }