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