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->IsFrontEngine()) {
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->IsFrontEngine()) 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) || HasBit(c->flags, GCF_INVALID)) {
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 if (HasBit(c->flags, GCF_INVALID)) {
01365 msg = "incompatible to this version of OpenTTD";
01366 } else {
01367 msg = "unknown";
01368 }
01369
01370 ShowInfoF("ini: ignoring invalid NewGRF '%s': %s", item->name, msg);
01371 delete c;
01372 continue;
01373 }
01374
01375
01376 bool duplicate = false;
01377 for (const GRFConfig *gc = first; gc != NULL; gc = gc->next) {
01378 if (gc->ident.grfid == c->ident.grfid) {
01379 ShowInfoF("ini: ignoring NewGRF '%s': duplicate GRF ID with '%s'", item->name, gc->filename);
01380 duplicate = true;
01381 break;
01382 }
01383 }
01384 if (duplicate) {
01385 delete c;
01386 continue;
01387 }
01388
01389
01390 if (is_static) SetBit(c->flags, GCF_STATIC);
01391
01392
01393 *curr = c;
01394 curr = &c->next;
01395 }
01396
01397 return first;
01398 }
01399
01405 static void NewsDisplaySaveConfig(IniFile *ini, const char *grpname)
01406 {
01407 IniGroup *group = ini->GetGroup(grpname);
01408
01409 for (int i = 0; i < NT_END; i++) {
01410 const char *value;
01411 int v = _news_type_data[i].display;
01412
01413 value = (v == ND_OFF ? "off" : (v == ND_SUMMARY ? "summarized" : "full"));
01414
01415 group->GetItem(_news_type_data[i].name, true)->SetValue(value);
01416 }
01417 }
01418
01419 static void AISaveConfig(IniFile *ini, const char *grpname)
01420 {
01421 #ifdef ENABLE_AI
01422 IniGroup *group = ini->GetGroup(grpname);
01423
01424 if (group == NULL) return;
01425 group->Clear();
01426
01427 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01428 AIConfig *config = AIConfig::GetConfig(c, AIConfig::AISS_FORCE_NEWGAME);
01429 const char *name;
01430 char value[1024];
01431 config->SettingsToString(value, lengthof(value));
01432
01433 if (config->HasAI()) {
01434 name = config->GetName();
01435 } else {
01436 name = "none";
01437 }
01438
01439 IniItem *item = new IniItem(group, name, strlen(name));
01440 item->SetValue(value);
01441 }
01442 #endif
01443 }
01444
01449 static void SaveVersionInConfig(IniFile *ini)
01450 {
01451 IniGroup *group = ini->GetGroup("version");
01452
01453 char version[9];
01454 snprintf(version, lengthof(version), "%08X", _openttd_newgrf_version);
01455
01456 const char * const versions[][2] = {
01457 { "version_string", _openttd_revision },
01458 { "version_number", version }
01459 };
01460
01461 for (uint i = 0; i < lengthof(versions); i++) {
01462 group->GetItem(versions[i][0], true)->SetValue(versions[i][1]);
01463 }
01464 }
01465
01466
01467 static void GRFSaveConfig(IniFile *ini, const char *grpname, const GRFConfig *list)
01468 {
01469 ini->RemoveGroup(grpname);
01470 IniGroup *group = ini->GetGroup(grpname);
01471 const GRFConfig *c;
01472
01473 for (c = list; c != NULL; c = c->next) {
01474 char params[512];
01475 GRFBuildParamList(params, c, lastof(params));
01476
01477 group->GetItem(c->filename, true)->SetValue(params);
01478 }
01479 }
01480
01481
01482 static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list)
01483 {
01484 proc(ini, (const SettingDesc*)_misc_settings, "misc", NULL);
01485 proc(ini, (const SettingDesc*)_music_settings, "music", &_msf);
01486 #if defined(WIN32) && !defined(DEDICATED)
01487 proc(ini, (const SettingDesc*)_win32_settings, "win32", NULL);
01488 #endif
01489
01490 proc(ini, _settings, "patches", &_settings_newgame);
01491 proc(ini, _currency_settings,"currency", &_custom_currency);
01492 proc(ini, _company_settings, "company", &_settings_client.company);
01493
01494 #ifdef ENABLE_NETWORK
01495 proc_list(ini, "server_bind_addresses", &_network_bind_list);
01496 proc_list(ini, "servers", &_network_host_list);
01497 proc_list(ini, "bans", &_network_ban_list);
01498 #endif
01499 }
01500
01501 static IniFile *IniLoadConfig()
01502 {
01503 IniFile *ini = new IniFile(_list_group_names);
01504 ini->LoadFromDisk(_config_file);
01505 return ini;
01506 }
01507
01509 void LoadFromConfig()
01510 {
01511 IniFile *ini = IniLoadConfig();
01512 ResetCurrencies(false);
01513
01514 HandleSettingDescs(ini, IniLoadSettings, IniLoadSettingList);
01515 _grfconfig_newgame = GRFLoadConfig(ini, "newgrf", false);
01516 _grfconfig_static = GRFLoadConfig(ini, "newgrf-static", true);
01517 NewsDisplayLoadConfig(ini, "news_display");
01518 AILoadConfig(ini, "ai_players");
01519
01520 PrepareOldDiffCustom();
01521 IniLoadSettings(ini, _gameopt_settings, "gameopt", &_settings_newgame);
01522 HandleOldDiffCustom(false);
01523
01524 ValidateSettings();
01525 delete ini;
01526 }
01527
01529 void SaveToConfig()
01530 {
01531 IniFile *ini = IniLoadConfig();
01532
01533
01534 ini->RemoveGroup("patches");
01535 ini->RemoveGroup("yapf");
01536 ini->RemoveGroup("gameopt");
01537
01538 HandleSettingDescs(ini, IniSaveSettings, IniSaveSettingList);
01539 GRFSaveConfig(ini, "newgrf", _grfconfig_newgame);
01540 GRFSaveConfig(ini, "newgrf-static", _grfconfig_static);
01541 NewsDisplaySaveConfig(ini, "news_display");
01542 AISaveConfig(ini, "ai_players");
01543 SaveVersionInConfig(ini);
01544 ini->SaveToDisk(_config_file);
01545 delete ini;
01546 }
01547
01552 void GetGRFPresetList(GRFPresetList *list)
01553 {
01554 list->Clear();
01555
01556 IniFile *ini = IniLoadConfig();
01557 IniGroup *group;
01558 for (group = ini->group; group != NULL; group = group->next) {
01559 if (strncmp(group->name, "preset-", 7) == 0) {
01560 *list->Append() = strdup(group->name + 7);
01561 }
01562 }
01563
01564 delete ini;
01565 }
01566
01573 GRFConfig *LoadGRFPresetFromConfig(const char *config_name)
01574 {
01575 char *section = (char*)alloca(strlen(config_name) + 8);
01576 sprintf(section, "preset-%s", config_name);
01577
01578 IniFile *ini = IniLoadConfig();
01579 GRFConfig *config = GRFLoadConfig(ini, section, false);
01580 delete ini;
01581
01582 return config;
01583 }
01584
01591 void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
01592 {
01593 char *section = (char*)alloca(strlen(config_name) + 8);
01594 sprintf(section, "preset-%s", config_name);
01595
01596 IniFile *ini = IniLoadConfig();
01597 GRFSaveConfig(ini, section, config);
01598 ini->SaveToDisk(_config_file);
01599 delete ini;
01600 }
01601
01606 void DeleteGRFPresetFromConfig(const char *config_name)
01607 {
01608 char *section = (char*)alloca(strlen(config_name) + 8);
01609 sprintf(section, "preset-%s", config_name);
01610
01611 IniFile *ini = IniLoadConfig();
01612 ini->RemoveGroup(section);
01613 ini->SaveToDisk(_config_file);
01614 delete ini;
01615 }
01616
01617 static const SettingDesc *GetSettingDescription(uint index)
01618 {
01619 if (index >= lengthof(_settings)) return NULL;
01620 return &_settings[index];
01621 }
01622
01634 CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01635 {
01636 const SettingDesc *sd = GetSettingDescription(p1);
01637
01638 if (sd == NULL) return CMD_ERROR;
01639 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR;
01640
01641 if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return CMD_ERROR;
01642 if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return CMD_ERROR;
01643 if ((sd->desc.flags & SGF_NEWGAME_ONLY) &&
01644 (_game_mode == GM_NORMAL ||
01645 (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0))) {
01646 return CMD_ERROR;
01647 }
01648
01649 if (flags & DC_EXEC) {
01650 void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01651
01652 int32 oldval = (int32)ReadValue(var, sd->save.conv);
01653 int32 newval = (int32)p2;
01654
01655 Write_ValidateSetting(var, sd, newval);
01656 newval = (int32)ReadValue(var, sd->save.conv);
01657
01658 if (oldval == newval) return CommandCost();
01659
01660 if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01661 WriteValue(var, sd->save.conv, (int64)oldval);
01662 return CommandCost();
01663 }
01664
01665 if (sd->desc.flags & SGF_NO_NETWORK) {
01666 GamelogStartAction(GLAT_SETTING);
01667 GamelogSetting(sd->desc.name, oldval, newval);
01668 GamelogStopAction();
01669 }
01670
01671 SetWindowDirty(WC_GAME_OPTIONS, 0);
01672 }
01673
01674 return CommandCost();
01675 }
01676
01687 CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01688 {
01689 if (p1 >= lengthof(_company_settings)) return CMD_ERROR;
01690 const SettingDesc *sd = &_company_settings[p1];
01691
01692 if (flags & DC_EXEC) {
01693 void *var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01694
01695 int32 oldval = (int32)ReadValue(var, sd->save.conv);
01696 int32 newval = (int32)p2;
01697
01698 Write_ValidateSetting(var, sd, newval);
01699 newval = (int32)ReadValue(var, sd->save.conv);
01700
01701 if (oldval == newval) return CommandCost();
01702
01703 if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01704 WriteValue(var, sd->save.conv, (int64)oldval);
01705 return CommandCost();
01706 }
01707
01708 SetWindowDirty(WC_GAME_OPTIONS, 0);
01709 }
01710
01711 return CommandCost();
01712 }
01713
01721 bool SetSettingValue(uint index, int32 value, bool force_newgame)
01722 {
01723 const SettingDesc *sd = &_settings[index];
01724
01725
01726
01727
01728 if (sd->save.conv & SLF_NETWORK_NO) {
01729 void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01730 Write_ValidateSetting(var, sd, value);
01731
01732 if (_game_mode != GM_MENU) {
01733 void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01734 Write_ValidateSetting(var2, sd, value);
01735 }
01736 if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01737 SetWindowDirty(WC_GAME_OPTIONS, 0);
01738 return true;
01739 }
01740
01741 if (force_newgame) {
01742 void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01743 Write_ValidateSetting(var2, sd, value);
01744 return true;
01745 }
01746
01747
01748 if (!_networking || (_networking && _network_server)) {
01749 return DoCommandP(0, index, value, CMD_CHANGE_SETTING);
01750 }
01751 return false;
01752 }
01753
01760 void SetCompanySetting(uint index, int32 value)
01761 {
01762 const SettingDesc *sd = &_company_settings[index];
01763 if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01764 DoCommandP(0, index, value, CMD_CHANGE_COMPANY_SETTING);
01765 } else {
01766 void *var = GetVariableAddress(&_settings_client.company, &sd->save);
01767 Write_ValidateSetting(var, sd, value);
01768 if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01769 }
01770 }
01771
01775 void SetDefaultCompanySettings(CompanyID cid)
01776 {
01777 Company *c = Company::Get(cid);
01778 const SettingDesc *sd;
01779 for (sd = _company_settings; sd->save.cmd != SL_END; sd++) {
01780 void *var = GetVariableAddress(&c->settings, &sd->save);
01781 Write_ValidateSetting(var, sd, (int32)(size_t)sd->desc.def);
01782 }
01783 }
01784
01785 #if defined(ENABLE_NETWORK)
01786
01789 void SyncCompanySettings()
01790 {
01791 const SettingDesc *sd;
01792 uint i = 0;
01793 for (sd = _company_settings; sd->save.cmd != SL_END; sd++, i++) {
01794 const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01795 const void *new_var = GetVariableAddress(&_settings_client.company, &sd->save);
01796 uint32 old_value = (uint32)ReadValue(old_var, sd->save.conv);
01797 uint32 new_value = (uint32)ReadValue(new_var, sd->save.conv);
01798 if (old_value != new_value) NetworkSendCommand(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, NULL, NULL, _local_company);
01799 }
01800 }
01801 #endif
01802
01808 uint GetCompanySettingIndex(const char *name)
01809 {
01810 uint i;
01811 const SettingDesc *sd = GetSettingFromName(name, &i);
01812 assert(sd != NULL && (sd->desc.flags & SGF_PER_COMPANY) != 0);
01813 return i;
01814 }
01815
01823 bool SetSettingValue(uint index, const char *value, bool force_newgame)
01824 {
01825 const SettingDesc *sd = &_settings[index];
01826 assert(sd->save.conv & SLF_NETWORK_NO);
01827
01828 if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) {
01829 char **var = (char**)GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
01830 free(*var);
01831 *var = strcmp(value, "(null)") == 0 ? NULL : strdup(value);
01832 } else {
01833 char *var = (char*)GetVariableAddress(NULL, &sd->save);
01834 ttd_strlcpy(var, value, sd->save.length);
01835 }
01836 if (sd->desc.proc != NULL) sd->desc.proc(0);
01837
01838 return true;
01839 }
01840
01848 const SettingDesc *GetSettingFromName(const char *name, uint *i)
01849 {
01850 const SettingDesc *sd;
01851
01852
01853 for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01854 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01855 if (strcmp(sd->desc.name, name) == 0) return sd;
01856 }
01857
01858
01859 for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01860 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01861 const char *short_name = strchr(sd->desc.name, '.');
01862 if (short_name != NULL) {
01863 short_name++;
01864 if (strcmp(short_name, name) == 0) return sd;
01865 }
01866 }
01867
01868 if (strncmp(name, "company.", 8) == 0) name += 8;
01869
01870 for (*i = 0, sd = _company_settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01871 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01872 if (strcmp(sd->desc.name, name) == 0) return sd;
01873 }
01874
01875 return NULL;
01876 }
01877
01878
01879
01880 void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
01881 {
01882 uint index;
01883 const SettingDesc *sd = GetSettingFromName(name, &index);
01884
01885 if (sd == NULL) {
01886 IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01887 return;
01888 }
01889
01890 bool success;
01891 if (sd->desc.cmd == SDT_STRING) {
01892 success = SetSettingValue(index, value, force_newgame);
01893 } else {
01894 uint32 val;
01895 extern bool GetArgumentInteger(uint32 *value, const char *arg);
01896 success = GetArgumentInteger(&val, value);
01897 if (!success) {
01898 IConsolePrintF(CC_ERROR, "'%s' is not an integer.", value);
01899 return;
01900 }
01901
01902 success = SetSettingValue(index, val, force_newgame);
01903 }
01904
01905 if (!success) {
01906 if (_network_server) {
01907 IConsoleError("This command/variable is not available during network games.");
01908 } else {
01909 IConsoleError("This command/variable is only available to a network server.");
01910 }
01911 }
01912 }
01913
01914 void IConsoleSetSetting(const char *name, int value)
01915 {
01916 uint index;
01917 const SettingDesc *sd = GetSettingFromName(name, &index);
01918 assert(sd != NULL);
01919 SetSettingValue(index, value);
01920 }
01921
01927 void IConsoleGetSetting(const char *name, bool force_newgame)
01928 {
01929 char value[20];
01930 uint index;
01931 const SettingDesc *sd = GetSettingFromName(name, &index);
01932 const void *ptr;
01933
01934 if (sd == NULL) {
01935 IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01936 return;
01937 }
01938
01939 ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
01940
01941 if (sd->desc.cmd == SDT_STRING) {
01942 IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char **)ptr : (const char *)ptr);
01943 } else {
01944 if (sd->desc.cmd == SDT_BOOLX) {
01945 snprintf(value, sizeof(value), (*(bool*)ptr == 1) ? "on" : "off");
01946 } else {
01947 snprintf(value, sizeof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
01948 }
01949
01950 IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s' (min: %s%d, max: %u)",
01951 name, value, (sd->desc.flags & SGF_0ISDISABLED) ? "(0) " : "", sd->desc.min, sd->desc.max);
01952 }
01953 }
01954
01960 void IConsoleListSettings(const char *prefilter)
01961 {
01962 IConsolePrintF(CC_WARNING, "All settings with their current value:");
01963
01964 for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) {
01965 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01966 if (prefilter != NULL && strstr(sd->desc.name, prefilter) == NULL) continue;
01967 char value[80];
01968 const void *ptr = GetVariableAddress(&GetGameSettings(), &sd->save);
01969
01970 if (sd->desc.cmd == SDT_BOOLX) {
01971 snprintf(value, lengthof(value), (*(bool*)ptr == 1) ? "on" : "off");
01972 } else if (sd->desc.cmd == SDT_STRING) {
01973 snprintf(value, sizeof(value), "%s", (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char **)ptr : (const char *)ptr);
01974 } else {
01975 snprintf(value, lengthof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
01976 }
01977 IConsolePrintF(CC_DEFAULT, "%s = %s", sd->desc.name, value);
01978 }
01979
01980 IConsolePrintF(CC_WARNING, "Use 'setting' command to change a value");
01981 }
01982
01989 static void LoadSettings(const SettingDesc *osd, void *object)
01990 {
01991 for (; osd->save.cmd != SL_END; osd++) {
01992 const SaveLoad *sld = &osd->save;
01993 void *ptr = GetVariableAddress(object, sld);
01994
01995 if (!SlObjectMember(ptr, sld)) continue;
01996 if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, osd, ReadValue(ptr, sld->conv));
01997 }
01998 }
01999
02006 static void SaveSettings(const SettingDesc *sd, void *object)
02007 {
02008
02009
02010 const SettingDesc *i;
02011 size_t length = 0;
02012 for (i = sd; i->save.cmd != SL_END; i++) {
02013 length += SlCalcObjMemberLength(object, &i->save);
02014 }
02015 SlSetLength(length);
02016
02017 for (i = sd; i->save.cmd != SL_END; i++) {
02018 void *ptr = GetVariableAddress(object, &i->save);
02019 SlObjectMember(ptr, &i->save);
02020 }
02021 }
02022
02023 static void Load_OPTS()
02024 {
02025
02026
02027
02028 PrepareOldDiffCustom();
02029 LoadSettings(_gameopt_settings, &_settings_game);
02030 HandleOldDiffCustom(true);
02031 }
02032
02033 static void Load_PATS()
02034 {
02035
02036
02037
02038 LoadSettings(_settings, &_settings_game);
02039 }
02040
02041 static void Check_PATS()
02042 {
02043 LoadSettings(_settings, &_load_check_data.settings);
02044 }
02045
02046 static void Save_PATS()
02047 {
02048 SaveSettings(_settings, &_settings_game);
02049 }
02050
02051 void CheckConfig()
02052 {
02053
02054
02055
02056
02057 if (_settings_newgame.pf.opf.pf_maxdepth == 16 && _settings_newgame.pf.opf.pf_maxlength == 512) {
02058 _settings_newgame.pf.opf.pf_maxdepth = 48;
02059 _settings_newgame.pf.opf.pf_maxlength = 4096;
02060 }
02061 }
02062
02063 extern const ChunkHandler _setting_chunk_handlers[] = {
02064 { 'OPTS', NULL, Load_OPTS, NULL, NULL, CH_RIFF},
02065 { 'PATS', Save_PATS, Load_PATS, NULL, Check_PATS, CH_RIFF | CH_LAST},
02066 };
02067
02068 static bool IsSignedVarMemType(VarType vt)
02069 {
02070 switch (GetVarMemType(vt)) {
02071 case SLE_VAR_I8:
02072 case SLE_VAR_I16:
02073 case SLE_VAR_I32:
02074 case SLE_VAR_I64:
02075 return true;
02076 }
02077 return false;
02078 }