00001
00002
00003
00004
00005
00006
00007
00008
00009
00026 #include "stdafx.h"
00027 #include "currency.h"
00028 #include "screenshot.h"
00029 #include "network/network.h"
00030 #include "network/network_func.h"
00031 #include "settings_internal.h"
00032 #include "command_func.h"
00033 #include "console_func.h"
00034 #include "pathfinder/pathfinder_type.h"
00035 #include "genworld.h"
00036 #include "train.h"
00037 #include "news_func.h"
00038 #include "window_func.h"
00039 #include "sound_func.h"
00040 #include "company_func.h"
00041 #include "rev.h"
00042 #ifdef WITH_FREETYPE
00043 #include "fontcache.h"
00044 #endif
00045 #include "textbuf_gui.h"
00046 #include "rail_gui.h"
00047 #include "elrail_func.h"
00048 #include "gui.h"
00049 #include "town.h"
00050 #include "video/video_driver.hpp"
00051 #include "sound/sound_driver.hpp"
00052 #include "music/music_driver.hpp"
00053 #include "blitter/factory.hpp"
00054 #include "base_media_base.h"
00055 #include "gamelog.h"
00056 #include "settings_func.h"
00057 #include "ini_type.h"
00058 #include "ai/ai_config.hpp"
00059 #include "ai/ai.hpp"
00060 #include "ship.h"
00061 #include "smallmap_gui.h"
00062 #include "roadveh.h"
00063 #include "fios.h"
00064 #include "company_manager_face.h"
00065 #include "infrastructure_func.h"
00066 #include "strings_func.h"
00067 #include "trafficlight_func.h"
00068
00069 #include "void_map.h"
00070 #include "station_base.h"
00071
00072 #include "table/strings.h"
00073 #include "table/settings.h"
00074
00075 ClientSettings _settings_client;
00076 GameSettings _settings_game;
00077 GameSettings _settings_newgame;
00078 VehicleDefaultSettings _old_vds;
00079 char *_config_file;
00080
00081 typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object);
00082 typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList *list);
00083
00084 static bool IsSignedVarMemType(VarType vt);
00085
00089 static const char * const _list_group_names[] = {
00090 "bans",
00091 "newgrf",
00092 "servers",
00093 "server_bind_addresses",
00094 NULL
00095 };
00096
00104 static size_t LookupOneOfMany(const char *many, const char *one, size_t onelen = 0)
00105 {
00106 const char *s;
00107 size_t idx;
00108
00109 if (onelen == 0) onelen = strlen(one);
00110
00111
00112 if (*one >= '0' && *one <= '9') return strtoul(one, NULL, 0);
00113
00114 idx = 0;
00115 for (;;) {
00116
00117 s = many;
00118 while (*s != '|' && *s != 0) s++;
00119 if ((size_t)(s - many) == onelen && !memcmp(one, many, onelen)) return idx;
00120 if (*s == 0) return (size_t)-1;
00121 many = s + 1;
00122 idx++;
00123 }
00124 }
00125
00133 static size_t LookupManyOfMany(const char *many, const char *str)
00134 {
00135 const char *s;
00136 size_t r;
00137 size_t res = 0;
00138
00139 for (;;) {
00140
00141 while (*str == ' ' || *str == '\t' || *str == '|') str++;
00142 if (*str == 0) break;
00143
00144 s = str;
00145 while (*s != 0 && *s != ' ' && *s != '\t' && *s != '|') s++;
00146
00147 r = LookupOneOfMany(many, str, s - str);
00148 if (r == (size_t)-1) return r;
00149
00150 SetBit(res, r);
00151 if (*s == 0) break;
00152 str = s + 1;
00153 }
00154 return res;
00155 }
00156
00165 static int ParseIntList(const char *p, int *items, int maxitems)
00166 {
00167 int n = 0;
00168 bool comma = false;
00169
00170 while (*p != '\0') {
00171 switch (*p) {
00172 case ',':
00173
00174 if (!comma) return -1;
00175 comma = false;
00176
00177 case ' ':
00178 p++;
00179 break;
00180
00181 default: {
00182 if (n == maxitems) return -1;
00183 char *end;
00184 long v = strtol(p, &end, 0);
00185 if (p == end) return -1;
00186 if (sizeof(int) < sizeof(long)) v = ClampToI32(v);
00187 items[n++] = v;
00188 p = end;
00189 comma = true;
00190 break;
00191 }
00192 }
00193 }
00194
00195
00196
00197 if (n != 0 && !comma) return -1;
00198
00199 return n;
00200 }
00201
00210 static bool LoadIntList(const char *str, void *array, int nelems, VarType type)
00211 {
00212 int items[64];
00213 int i, nitems;
00214
00215 if (str == NULL) {
00216 memset(items, 0, sizeof(items));
00217 nitems = nelems;
00218 } else {
00219 nitems = ParseIntList(str, items, lengthof(items));
00220 if (nitems != nelems) return false;
00221 }
00222
00223 switch (type) {
00224 case SLE_VAR_BL:
00225 case SLE_VAR_I8:
00226 case SLE_VAR_U8:
00227 for (i = 0; i != nitems; i++) ((byte*)array)[i] = items[i];
00228 break;
00229 case SLE_VAR_I16:
00230 case SLE_VAR_U16:
00231 for (i = 0; i != nitems; i++) ((uint16*)array)[i] = items[i];
00232 break;
00233 case SLE_VAR_I32:
00234 case SLE_VAR_U32:
00235 for (i = 0; i != nitems; i++) ((uint32*)array)[i] = items[i];
00236 break;
00237 default: NOT_REACHED();
00238 }
00239
00240 return true;
00241 }
00242
00252 static void MakeIntList(char *buf, const char *last, const void *array, int nelems, VarType type)
00253 {
00254 int i, v = 0;
00255 byte *p = (byte*)array;
00256
00257 for (i = 0; i != nelems; i++) {
00258 switch (type) {
00259 case SLE_VAR_BL:
00260 case SLE_VAR_I8: v = *(int8*)p; p += 1; break;
00261 case SLE_VAR_U8: v = *(byte*)p; p += 1; break;
00262 case SLE_VAR_I16: v = *(int16*)p; p += 2; break;
00263 case SLE_VAR_U16: v = *(uint16*)p; p += 2; break;
00264 case SLE_VAR_I32: v = *(int32*)p; p += 4; break;
00265 case SLE_VAR_U32: v = *(uint32*)p; p += 4; break;
00266 default: NOT_REACHED();
00267 }
00268 buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
00269 }
00270 }
00271
00279 static void MakeOneOfMany(char *buf, const char *last, const char *many, int id)
00280 {
00281 int orig_id = id;
00282
00283
00284 while (--id >= 0) {
00285 for (; *many != '|'; many++) {
00286 if (*many == '\0') {
00287 seprintf(buf, last, "%d", orig_id);
00288 return;
00289 }
00290 }
00291 many++;
00292 }
00293
00294
00295 while (*many != '\0' && *many != '|' && buf < last) *buf++ = *many++;
00296 *buf = '\0';
00297 }
00298
00307 static void MakeManyOfMany(char *buf, const char *last, const char *many, uint32 x)
00308 {
00309 const char *start;
00310 int i = 0;
00311 bool init = true;
00312
00313 for (; x != 0; x >>= 1, i++) {
00314 start = many;
00315 while (*many != 0 && *many != '|') many++;
00316
00317 if (HasBit(x, 0)) {
00318 if (!init) buf += seprintf(buf, last, "|");
00319 init = false;
00320 if (start == many) {
00321 buf += seprintf(buf, last, "%d", i);
00322 } else {
00323 memcpy(buf, start, many - start);
00324 buf += many - start;
00325 }
00326 }
00327
00328 if (*many == '|') many++;
00329 }
00330
00331 *buf = '\0';
00332 }
00333
00340 static const void *StringToVal(const SettingDescBase *desc, const char *orig_str)
00341 {
00342 const char *str = orig_str == NULL ? "" : orig_str;
00343 switch (desc->cmd) {
00344 case SDT_NUMX: {
00345 char *end;
00346 size_t val = strtoul(str, &end, 0);
00347 if (*end != '\0') ShowInfoF("ini: trailing characters at end of setting '%s'", desc->name);
00348 return (void*)val;
00349 }
00350 case SDT_ONEOFMANY: {
00351 size_t r = LookupOneOfMany(desc->many, str);
00352
00353
00354 if (r == (size_t)-1 && desc->proc_cnvt != NULL) r = desc->proc_cnvt(str);
00355 if (r != (size_t)-1) return (void*)r;
00356 ShowInfoF("ini: invalid value '%s' for '%s'", str, desc->name);
00357 return 0;
00358 }
00359 case SDT_MANYOFMANY: {
00360 size_t r = LookupManyOfMany(desc->many, str);
00361 if (r != (size_t)-1) return (void*)r;
00362 ShowInfoF("ini: invalid value '%s' for '%s'", str, desc->name);
00363 return NULL;
00364 }
00365 case SDT_BOOLX:
00366 if (strcmp(str, "true") == 0 || strcmp(str, "on") == 0 || strcmp(str, "1") == 0) return (void*)true;
00367 if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return (void*)false;
00368 ShowInfoF("ini: invalid setting value '%s' for '%s'", str, desc->name);
00369 break;
00370
00371 case SDT_STRING: return orig_str;
00372 case SDT_INTLIST: return str;
00373 default: break;
00374 }
00375
00376 return NULL;
00377 }
00378
00388 static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val)
00389 {
00390 const SettingDescBase *sdb = &sd->desc;
00391
00392 if (sdb->cmd != SDT_BOOLX &&
00393 sdb->cmd != SDT_NUMX &&
00394 sdb->cmd != SDT_ONEOFMANY &&
00395 sdb->cmd != SDT_MANYOFMANY) {
00396 return;
00397 }
00398
00399
00400 if (sdb->cmd != SDT_MANYOFMANY) {
00401
00402
00403
00404
00405
00406 switch (GetVarMemType(sd->save.conv)) {
00407 case SLE_VAR_NULL: return;
00408 case SLE_VAR_BL:
00409 case SLE_VAR_I8:
00410 case SLE_VAR_U8:
00411 case SLE_VAR_I16:
00412 case SLE_VAR_U16:
00413 case SLE_VAR_I32: {
00414
00415 if (!(sdb->flags & SGF_0ISDISABLED) || val != 0) val = Clamp(val, sdb->min, sdb->max);
00416 break;
00417 }
00418 case SLE_VAR_U32: {
00419
00420 uint min = ((sdb->flags & SGF_0ISDISABLED) && (uint)val <= (uint)sdb->min) ? 0 : sdb->min;
00421 WriteValue(ptr, SLE_VAR_U32, (int64)ClampU(val, min, sdb->max));
00422 return;
00423 }
00424 case SLE_VAR_I64:
00425 case SLE_VAR_U64:
00426 default: NOT_REACHED();
00427 }
00428 }
00429
00430 WriteValue(ptr, sd->save.conv, (int64)val);
00431 }
00432
00441 static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00442 {
00443 IniGroup *group;
00444 IniGroup *group_def = ini->GetGroup(grpname);
00445 IniItem *item;
00446 const void *p;
00447 void *ptr;
00448 const char *s;
00449
00450 for (; sd->save.cmd != SL_END; sd++) {
00451 const SettingDescBase *sdb = &sd->desc;
00452 const SaveLoad *sld = &sd->save;
00453
00454 if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00455
00456
00457 s = strchr(sdb->name, '.');
00458 if (s != NULL) {
00459 group = ini->GetGroup(sdb->name, s - sdb->name);
00460 s++;
00461 } else {
00462 s = sdb->name;
00463 group = group_def;
00464 }
00465
00466 item = group->GetItem(s, false);
00467 if (item == NULL && group != group_def) {
00468
00469
00470 item = group_def->GetItem(s, false);
00471 }
00472 if (item == NULL) {
00473
00474
00475 const char *sc = strchr(s, '.');
00476 if (sc != NULL) item = ini->GetGroup(s, sc - s)->GetItem(sc + 1, false);
00477 }
00478
00479 p = (item == NULL) ? sdb->def : StringToVal(sdb, item->value);
00480 ptr = GetVariableAddress(object, sld);
00481
00482 switch (sdb->cmd) {
00483 case SDT_BOOLX:
00484 case SDT_NUMX:
00485 case SDT_ONEOFMANY:
00486 case SDT_MANYOFMANY:
00487 Write_ValidateSetting(ptr, sd, (int32)(size_t)p); break;
00488
00489 case SDT_STRING:
00490 switch (GetVarMemType(sld->conv)) {
00491 case SLE_VAR_STRB:
00492 case SLE_VAR_STRBQ:
00493 if (p != NULL) ttd_strlcpy((char*)ptr, (const char*)p, sld->length);
00494 break;
00495 case SLE_VAR_STR:
00496 case SLE_VAR_STRQ:
00497 free(*(char**)ptr);
00498 *(char**)ptr = p == NULL ? NULL : strdup((const char*)p);
00499 break;
00500 case SLE_VAR_CHAR: if (p != NULL) *(char*)ptr = *(char*)p; break;
00501 default: NOT_REACHED();
00502 }
00503 break;
00504
00505 case SDT_INTLIST: {
00506 if (!LoadIntList((const char*)p, ptr, sld->length, GetVarMemType(sld->conv))) {
00507 ShowInfoF("ini: error in array '%s'", sdb->name);
00508 } else if (sd->desc.proc_cnvt != NULL) {
00509 sd->desc.proc_cnvt((const char*)p);
00510 }
00511 break;
00512 }
00513 default: NOT_REACHED();
00514 }
00515 }
00516 }
00517
00530 static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00531 {
00532 IniGroup *group_def = NULL, *group;
00533 IniItem *item;
00534 char buf[512];
00535 const char *s;
00536 void *ptr;
00537
00538 for (; sd->save.cmd != SL_END; sd++) {
00539 const SettingDescBase *sdb = &sd->desc;
00540 const SaveLoad *sld = &sd->save;
00541
00542
00543
00544 if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00545 if (sld->conv & SLF_NOT_IN_CONFIG) continue;
00546
00547
00548 s = strchr(sdb->name, '.');
00549 if (s != NULL) {
00550 group = ini->GetGroup(sdb->name, s - sdb->name);
00551 s++;
00552 } else {
00553 if (group_def == NULL) group_def = ini->GetGroup(grpname);
00554 s = sdb->name;
00555 group = group_def;
00556 }
00557
00558 item = group->GetItem(s, true);
00559 ptr = GetVariableAddress(object, sld);
00560
00561 if (item->value != NULL) {
00562
00563 const void *p = StringToVal(sdb, item->value);
00564
00565
00566
00567 switch (sdb->cmd) {
00568 case SDT_BOOLX:
00569 case SDT_NUMX:
00570 case SDT_ONEOFMANY:
00571 case SDT_MANYOFMANY:
00572 switch (GetVarMemType(sld->conv)) {
00573 case SLE_VAR_BL:
00574 if (*(bool*)ptr == (p != NULL)) continue;
00575 break;
00576 case SLE_VAR_I8:
00577 case SLE_VAR_U8:
00578 if (*(byte*)ptr == (byte)(size_t)p) continue;
00579 break;
00580 case SLE_VAR_I16:
00581 case SLE_VAR_U16:
00582 if (*(uint16*)ptr == (uint16)(size_t)p) continue;
00583 break;
00584 case SLE_VAR_I32:
00585 case SLE_VAR_U32:
00586 if (*(uint32*)ptr == (uint32)(size_t)p) continue;
00587 break;
00588 default: NOT_REACHED();
00589 }
00590 break;
00591 default: break;
00592 }
00593 }
00594
00595
00596 switch (sdb->cmd) {
00597 case SDT_BOOLX:
00598 case SDT_NUMX:
00599 case SDT_ONEOFMANY:
00600 case SDT_MANYOFMANY: {
00601 uint32 i = (uint32)ReadValue(ptr, sld->conv);
00602
00603 switch (sdb->cmd) {
00604 case SDT_BOOLX: strecpy(buf, (i != 0) ? "true" : "false", lastof(buf)); break;
00605 case SDT_NUMX: seprintf(buf, lastof(buf), IsSignedVarMemType(sld->conv) ? "%d" : "%u", i); break;
00606 case SDT_ONEOFMANY: MakeOneOfMany(buf, lastof(buf), sdb->many, i); break;
00607 case SDT_MANYOFMANY: MakeManyOfMany(buf, lastof(buf), sdb->many, i); break;
00608 default: NOT_REACHED();
00609 }
00610 break;
00611 }
00612
00613 case SDT_STRING:
00614 switch (GetVarMemType(sld->conv)) {
00615 case SLE_VAR_STRB: strecpy(buf, (char*)ptr, lastof(buf)); break;
00616 case SLE_VAR_STRBQ:seprintf(buf, lastof(buf), "\"%s\"", (char*)ptr); break;
00617 case SLE_VAR_STR: strecpy(buf, *(char**)ptr, lastof(buf)); break;
00618 case SLE_VAR_STRQ:
00619 if (*(char**)ptr == NULL) {
00620 buf[0] = '\0';
00621 } else {
00622 seprintf(buf, lastof(buf), "\"%s\"", *(char**)ptr);
00623 }
00624 break;
00625 case SLE_VAR_CHAR: buf[0] = *(char*)ptr; buf[1] = '\0'; break;
00626 default: NOT_REACHED();
00627 }
00628 break;
00629
00630 case SDT_INTLIST:
00631 MakeIntList(buf, lastof(buf), ptr, sld->length, GetVarMemType(sld->conv));
00632 break;
00633 default: NOT_REACHED();
00634 }
00635
00636
00637 free(item->value);
00638 item->value = strdup(buf);
00639 }
00640 }
00641
00651 static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList *list)
00652 {
00653 IniGroup *group = ini->GetGroup(grpname);
00654
00655 if (group == NULL || list == NULL) return;
00656
00657 list->Clear();
00658
00659 for (const IniItem *item = group->item; item != NULL; item = item->next) {
00660 if (item->name != NULL) *list->Append() = strdup(item->name);
00661 }
00662 }
00663
00673 static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList *list)
00674 {
00675 IniGroup *group = ini->GetGroup(grpname);
00676
00677 if (group == NULL || list == NULL) return;
00678 group->Clear();
00679
00680 for (char **iter = list->Begin(); iter != list->End(); iter++) {
00681 group->GetItem(*iter, true)->SetValue("");
00682 }
00683 }
00684
00685
00686
00688 static bool v_PositionMainToolbar(int32 p1)
00689 {
00690 if (_game_mode != GM_MENU) PositionMainToolbar(NULL);
00691 return true;
00692 }
00693
00695 static bool v_PositionStatusbar(int32 p1)
00696 {
00697 if (_game_mode != GM_MENU) {
00698 PositionStatusbar(NULL);
00699 PositionNewsMessage(NULL);
00700 PositionNetworkChatWindow(NULL);
00701 }
00702 return true;
00703 }
00704
00705 static bool PopulationInLabelActive(int32 p1)
00706 {
00707 UpdateAllTownVirtCoords();
00708 return true;
00709 }
00710
00711 static bool RedrawScreen(int32 p1)
00712 {
00713 MarkWholeScreenDirty();
00714 return true;
00715 }
00716
00722 static bool RedrawSmallmap(int32 p1)
00723 {
00724 BuildLandLegend();
00725 BuildOwnerLegend();
00726 SetWindowClassesDirty(WC_SMALLMAP);
00727 return true;
00728 }
00729
00730 static bool InvalidateDetailsWindow(int32 p1)
00731 {
00732 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00733 return true;
00734 }
00735
00736 static bool InvalidateStationBuildWindow(int32 p1)
00737 {
00738 SetWindowDirty(WC_BUILD_STATION, 0);
00739 return true;
00740 }
00741
00742 static bool InvalidateBuildIndustryWindow(int32 p1)
00743 {
00744 InvalidateWindowData(WC_BUILD_INDUSTRY, 0);
00745 return true;
00746 }
00747
00748 static bool CloseSignalGUI(int32 p1)
00749 {
00750 if (p1 == 0) {
00751 DeleteWindowByClass(WC_BUILD_SIGNAL);
00752 }
00753 return true;
00754 }
00755
00756 static bool InvalidateTownViewWindow(int32 p1)
00757 {
00758 InvalidateWindowClassesData(WC_TOWN_VIEW, p1);
00759 return true;
00760 }
00761
00762 static bool DeleteSelectStationWindow(int32 p1)
00763 {
00764 DeleteWindowById(WC_SELECT_STATION, 0);
00765 return true;
00766 }
00767
00768 static bool UpdateConsists(int32 p1)
00769 {
00770 Train *t;
00771 FOR_ALL_TRAINS(t) {
00772
00773 if (t->IsFrontEngine() || t->IsFreeWagon()) t->ConsistChanged(true);
00774 }
00775 return true;
00776 }
00777
00778
00779 static bool CheckInterval(int32 p1)
00780 {
00781 VehicleDefaultSettings *vds;
00782 if (_game_mode == GM_MENU || !Company::IsValidID(_current_company)) {
00783 vds = &_settings_client.company.vehicle;
00784 } else {
00785 vds = &Company::Get(_current_company)->settings.vehicle;
00786 }
00787
00788 if (p1 != 0) {
00789 vds->servint_trains = 50;
00790 vds->servint_roadveh = 50;
00791 vds->servint_aircraft = 50;
00792 vds->servint_ships = 50;
00793 } else {
00794 vds->servint_trains = 150;
00795 vds->servint_roadveh = 150;
00796 vds->servint_aircraft = 100;
00797 vds->servint_ships = 360;
00798 }
00799
00800 InvalidateDetailsWindow(0);
00801
00802 return true;
00803 }
00804
00805 static bool TrainAccelerationModelChanged(int32 p1)
00806 {
00807 Train *t;
00808 FOR_ALL_TRAINS(t) {
00809 if (t->IsFrontEngine()) {
00810 t->tcache.cached_max_curve_speed = t->GetCurveSpeedLimit();
00811 t->UpdateAcceleration();
00812 }
00813 }
00814
00815
00816 SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00817 InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00818 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00819
00820 return true;
00821 }
00822
00828 static bool TrainSlopeSteepnessChanged(int32 p1)
00829 {
00830 Train *t;
00831 FOR_ALL_TRAINS(t) {
00832 if (t->IsFrontEngine()) t->CargoChanged();
00833 }
00834
00835 return true;
00836 }
00837
00843 static bool RoadVehAccelerationModelChanged(int32 p1)
00844 {
00845 if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) {
00846 RoadVehicle *rv;
00847 FOR_ALL_ROADVEHICLES(rv) {
00848 if (rv->IsFrontEngine()) {
00849 rv->CargoChanged();
00850 }
00851 }
00852 }
00853
00854
00855 SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00856 InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00857 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00858
00859 return true;
00860 }
00861
00867 static bool RoadVehSlopeSteepnessChanged(int32 p1)
00868 {
00869 RoadVehicle *rv;
00870 FOR_ALL_ROADVEHICLES(rv) {
00871 if (rv->IsFrontEngine()) rv->CargoChanged();
00872 }
00873
00874 return true;
00875 }
00876
00877 static bool DragSignalsDensityChanged(int32)
00878 {
00879 InvalidateWindowData(WC_BUILD_SIGNAL, 0);
00880
00881 return true;
00882 }
00883
00884 static bool TownFoundingChanged(int32 p1)
00885 {
00886 if (_game_mode != GM_EDITOR && _settings_game.economy.found_town == TF_FORBIDDEN) {
00887 DeleteWindowById(WC_FOUND_TOWN, 0);
00888 return true;
00889 }
00890 InvalidateWindowData(WC_FOUND_TOWN, 0);
00891 return true;
00892 }
00893
00894 static bool InvalidateVehTimetableWindow(int32 p1)
00895 {
00896 InvalidateWindowClassesData(WC_VEHICLE_TIMETABLE, -2);
00897 return true;
00898 }
00899
00907 static bool InvalidateNewGRFChangeWindows(int32 p1)
00908 {
00909 InvalidateWindowClassesData(WC_SAVELOAD);
00910 DeleteWindowByClass(WC_GAME_OPTIONS);
00911 ReInitAllWindows();
00912 return true;
00913 }
00914
00915 static bool InvalidateCompanyLiveryWindow(int32 p1)
00916 {
00917 InvalidateWindowClassesData(WC_COMPANY_COLOUR);
00918 return RedrawScreen(p1);
00919 }
00920
00921 static bool InvalidateIndustryViewWindow(int32 p1)
00922 {
00923 InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
00924 return true;
00925 }
00926
00932 static bool RedrawTownAuthority(int32 p1)
00933 {
00934 SetWindowClassesDirty(WC_TOWN_AUTHORITY);
00935 return true;
00936 }
00937
00943 static bool InvalidateCompanyInfrastructureWindow(int32 p1)
00944 {
00945 InvalidateWindowClassesData(WC_COMPANY_INFRASTRUCTURE);
00946 return true;
00947 }
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970 static const DifficultySettings _default_game_diff[3] = {
00971
00972 {0, 2, 1, 999950000, 0, 0, 0, 0, 3, 0, 1, 1, 0, 0, 0, 0, 0},
00973 {4, 2, 3, 150000, 3, 1, 3, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1},
00974 {7, 3, 3, 100000, 4, 1, 3, 2, 0, 2, 3, 2, 1, 1, 1, 2, 2},
00975 };
00976
00977 void SetDifficultyLevel(int mode, DifficultySettings *gm_opt)
00978 {
00979 assert(mode <= 3);
00980
00981 if (mode != 3) {
00982 *gm_opt = _default_game_diff[mode];
00983 } else {
00984 gm_opt->diff_level = 3;
00985 }
00986 }
00987
00989 static void ValidateSettings()
00990 {
00991
00992 if (_settings_newgame.difficulty.diff_level != 3) {
00993 SetDifficultyLevel(_settings_newgame.difficulty.diff_level, &_settings_newgame.difficulty);
00994 }
00995
00996
00997 if (_settings_newgame.game_creation.land_generator == 0 &&
00998 _settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
00999 _settings_newgame.difficulty.quantity_sea_lakes = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE;
01000 }
01001 }
01002
01003 static bool DifficultyReset(int32 level)
01004 {
01005
01006
01007 if (_game_mode != GM_MENU && level != 3) return false;
01008 SetDifficultyLevel(level, &GetGameSettings().difficulty);
01009 return true;
01010 }
01011
01012 static bool DifficultyChange(int32)
01013 {
01014 if (_game_mode == GM_MENU) {
01015 if (_settings_newgame.difficulty.diff_level != 3) {
01016 ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, WL_WARNING);
01017 _settings_newgame.difficulty.diff_level = 3;
01018 }
01019 SetWindowClassesDirty(WC_SELECT_GAME);
01020 } else {
01021 _settings_game.difficulty.diff_level = 3;
01022 }
01023
01024
01025
01026
01027 if (_networking && FindWindowById(WC_GAME_OPTIONS, 0) != NULL) {
01028 ShowGameDifficulty();
01029 }
01030
01031 return true;
01032 }
01033
01034 static bool DifficultyNoiseChange(int32 i)
01035 {
01036 if (_game_mode == GM_NORMAL) {
01037 UpdateAirportsNoise();
01038 if (_settings_game.economy.station_noise_level) {
01039 InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
01040 }
01041 }
01042
01043 return DifficultyChange(i);
01044 }
01045
01046 static bool MaxNoAIsChange(int32 i)
01047 {
01048 if (GetGameSettings().difficulty.max_no_competitors != 0 &&
01049 #ifdef ENABLE_AI
01050 AI::GetInfoList()->size() == 0 &&
01051 #endif
01052 (!_networking || _network_server)) {
01053 ShowErrorMessage(STR_WARNING_NO_SUITABLE_AI, INVALID_STRING_ID, WL_CRITICAL);
01054 }
01055
01056 return DifficultyChange(i);
01057 }
01058
01064 static bool CheckRoadSide(int p1)
01065 {
01066 extern bool RoadVehiclesAreBuilt();
01067 return _game_mode == GM_MENU || !RoadVehiclesAreBuilt();
01068 }
01069
01077 static size_t ConvertLandscape(const char *value)
01078 {
01079
01080 return LookupOneOfMany("normal|hilly|desert|candy", value);
01081 }
01082
01088 static bool TLSettingChanged(int32 p1)
01089 {
01090
01091 MarkWholeScreenDirty();
01092
01093
01094 if (!_settings_game.construction.traffic_lights) ClearAllTrafficLights();
01095 return true;
01096 }
01097
01098 static bool CheckFreeformEdges(int32 p1)
01099 {
01100 if (_game_mode == GM_MENU) return true;
01101 if (p1 != 0) {
01102 Ship *s;
01103 FOR_ALL_SHIPS(s) {
01104
01105 if (TileX(s->tile) == 0 || TileY(s->tile) == 0) {
01106 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01107 return false;
01108 }
01109 }
01110 BaseStation *st;
01111 FOR_ALL_BASE_STATIONS(st) {
01112
01113 if (st->IsInUse() && (TileX(st->xy) == 0 || TileY(st->xy) == 0)) {
01114 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01115 return false;
01116 }
01117 }
01118 for (uint i = 0; i < MapSizeX(); i++) MakeVoid(TileXY(i, 0));
01119 for (uint i = 0; i < MapSizeY(); i++) MakeVoid(TileXY(0, i));
01120 } else {
01121 for (uint i = 0; i < MapMaxX(); i++) {
01122 if (TileHeight(TileXY(i, 1)) != 0) {
01123 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01124 return false;
01125 }
01126 }
01127 for (uint i = 1; i < MapMaxX(); i++) {
01128 if (!IsTileType(TileXY(i, MapMaxY() - 1), MP_WATER) || TileHeight(TileXY(1, MapMaxY())) != 0) {
01129 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01130 return false;
01131 }
01132 }
01133 for (uint i = 0; i < MapMaxY(); i++) {
01134 if (TileHeight(TileXY(1, i)) != 0) {
01135 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01136 return false;
01137 }
01138 }
01139 for (uint i = 1; i < MapMaxY(); i++) {
01140 if (!IsTileType(TileXY(MapMaxX() - 1, i), MP_WATER) || TileHeight(TileXY(MapMaxX(), i)) != 0) {
01141 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01142 return false;
01143 }
01144 }
01145
01146 for (uint i = 0; i < MapMaxX(); i++) {
01147 SetTileHeight(TileXY(i, 0), 0);
01148 SetTileType(TileXY(i, 0), MP_WATER);
01149 }
01150 for (uint i = 0; i < MapMaxY(); i++) {
01151 SetTileHeight(TileXY(0, i), 0);
01152 SetTileType(TileXY(0, i), MP_WATER);
01153 }
01154 }
01155 MarkWholeScreenDirty();
01156 return true;
01157 }
01158
01163 static bool ChangeDynamicEngines(int32 p1)
01164 {
01165 if (_game_mode == GM_MENU) return true;
01166
01167 if (!EngineOverrideManager::ResetToCurrentNewGRFConfig()) {
01168 ShowErrorMessage(STR_CONFIG_SETTING_DYNAMIC_ENGINES_EXISTING_VEHICLES, INVALID_STRING_ID, WL_ERROR);
01169 return false;
01170 }
01171
01172 return true;
01173 }
01174
01175 static bool StationCatchmentChanged(int32 p1)
01176 {
01177 Station::RecomputeIndustriesNearForAll();
01178 return true;
01179 }
01180
01181
01182 #ifdef ENABLE_NETWORK
01183
01184 static bool UpdateClientName(int32 p1)
01185 {
01186 NetworkUpdateClientName();
01187 return true;
01188 }
01189
01190 static bool UpdateServerPassword(int32 p1)
01191 {
01192 if (strcmp(_settings_client.network.server_password, "*") == 0) {
01193 _settings_client.network.server_password[0] = '\0';
01194 }
01195
01196 return true;
01197 }
01198
01199 static bool UpdateRconPassword(int32 p1)
01200 {
01201 if (strcmp(_settings_client.network.rcon_password, "*") == 0) {
01202 _settings_client.network.rcon_password[0] = '\0';
01203 }
01204
01205 return true;
01206 }
01207
01208 static bool UpdateClientConfigValues(int32 p1)
01209 {
01210 if (_network_server) NetworkServerSendConfigUpdate();
01211
01212 return true;
01213 }
01214
01215 static bool CheckSharingRail(int32 p1)
01216 {
01217 if (!CheckSharingChangePossible(VEH_TRAIN)) return false;
01218 UpdateAllBlockSignals();
01219 return true;
01220 }
01221
01222 static bool CheckSharingRoad(int32 p1)
01223 {
01224 return CheckSharingChangePossible(VEH_ROAD);
01225 }
01226
01227 static bool CheckSharingWater(int32 p1)
01228 {
01229 return CheckSharingChangePossible(VEH_SHIP);
01230 }
01231
01232 static bool CheckSharingAir(int32 p1)
01233 {
01234 return CheckSharingChangePossible(VEH_AIRCRAFT);
01235 }
01236
01237 #endif
01238
01239
01240
01241
01245 static void PrepareOldDiffCustom()
01246 {
01247 memset(_old_diff_custom, 0, sizeof(_old_diff_custom));
01248 }
01249
01256 static void HandleOldDiffCustom(bool savegame)
01257 {
01258 uint options_to_load = GAME_DIFFICULTY_NUM - ((savegame && IsSavegameVersionBefore(4)) ? 1 : 0);
01259
01260 if (!savegame) {
01261
01262 bool old_diff_custom_used = false;
01263 for (uint i = 0; i < options_to_load && !old_diff_custom_used; i++) {
01264 old_diff_custom_used = (_old_diff_custom[i] != 0);
01265 }
01266
01267 if (!old_diff_custom_used) return;
01268 }
01269
01270 for (uint i = 0; i < options_to_load; i++) {
01271 const SettingDesc *sd = &_settings[i];
01272
01273 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01274 void *var = GetVariableAddress(savegame ? &_settings_game : &_settings_newgame, &sd->save);
01275 Write_ValidateSetting(var, sd, (int32)((i == 4 ? 1000 : 1) * _old_diff_custom[i]));
01276 }
01277 }
01278
01285 static bool ConvertOldNewsSetting(const char *name, const char *value)
01286 {
01287 if (strcasecmp(name, "openclose") == 0) {
01288
01289
01290
01291
01292 NewsDisplay display = ND_OFF;
01293 if (strcasecmp(value, "full") == 0) {
01294 display = ND_FULL;
01295 } else if (strcasecmp(value, "summarized") == 0) {
01296 display = ND_SUMMARY;
01297 }
01298
01299 _news_type_data[NT_INDUSTRY_OPEN].display = display;
01300 _news_type_data[NT_INDUSTRY_CLOSE].display = display;
01301 return true;
01302 }
01303 return false;
01304 }
01305
01311 static void NewsDisplayLoadConfig(IniFile *ini, const char *grpname)
01312 {
01313 IniGroup *group = ini->GetGroup(grpname);
01314 IniItem *item;
01315
01316
01317 if (group == NULL) return;
01318
01319 for (item = group->item; item != NULL; item = item->next) {
01320 int news_item = -1;
01321 for (int i = 0; i < NT_END; i++) {
01322 if (strcasecmp(item->name, _news_type_data[i].name) == 0) {
01323 news_item = i;
01324 break;
01325 }
01326 }
01327
01328
01329 if (news_item == -1) {
01330
01331 if (!ConvertOldNewsSetting(item->name, item->value)) {
01332 DEBUG(misc, 0, "Invalid display option: %s", item->name);
01333 }
01334
01335 continue;
01336 }
01337
01338 if (StrEmpty(item->value)) {
01339 DEBUG(misc, 0, "Empty display value for newstype %s", item->name);
01340 continue;
01341 } else if (strcasecmp(item->value, "full") == 0) {
01342 _news_type_data[news_item].display = ND_FULL;
01343 } else if (strcasecmp(item->value, "off") == 0) {
01344 _news_type_data[news_item].display = ND_OFF;
01345 } else if (strcasecmp(item->value, "summarized") == 0) {
01346 _news_type_data[news_item].display = ND_SUMMARY;
01347 } else {
01348 DEBUG(misc, 0, "Invalid display value for newstype %s: %s", item->name, item->value);
01349 continue;
01350 }
01351 }
01352 }
01353
01354 static void AILoadConfig(IniFile *ini, const char *grpname)
01355 {
01356 #ifdef ENABLE_AI
01357 IniGroup *group = ini->GetGroup(grpname);
01358 IniItem *item;
01359
01360
01361 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01362 AIConfig::GetConfig(c, AIConfig::AISS_FORCE_NEWGAME)->ChangeAI(NULL);
01363 }
01364
01365
01366 if (group == NULL) return;
01367
01368 CompanyID c = COMPANY_FIRST;
01369 for (item = group->item; c < MAX_COMPANIES && item != NULL; c++, item = item->next) {
01370 AIConfig *config = AIConfig::GetConfig(c, AIConfig::AISS_FORCE_NEWGAME);
01371
01372 config->ChangeAI(item->name);
01373 if (!config->HasAI()) {
01374 if (strcmp(item->name, "none") != 0) {
01375 DEBUG(ai, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name);
01376 continue;
01377 }
01378 }
01379 if (item->value != NULL) config->StringToSettings(item->value);
01380 }
01381 #endif
01382 }
01383
01390 static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_static)
01391 {
01392 IniGroup *group = ini->GetGroup(grpname);
01393 IniItem *item;
01394 GRFConfig *first = NULL;
01395 GRFConfig **curr = &first;
01396
01397 if (group == NULL) return NULL;
01398
01399 for (item = group->item; item != NULL; item = item->next) {
01400 GRFConfig *c = new GRFConfig(item->name);
01401
01402
01403 if (!StrEmpty(item->value)) {
01404 c->num_params = ParseIntList(item->value, (int*)c->param, lengthof(c->param));
01405 if (c->num_params == (byte)-1) {
01406 ShowInfoF("ini: error in array '%s'", item->name);
01407 c->num_params = 0;
01408 }
01409 }
01410
01411
01412 if (!FillGRFDetails(c, is_static) || HasBit(c->flags, GCF_INVALID)) {
01413 const char *msg;
01414
01415 if (c->status == GCS_NOT_FOUND) {
01416 msg = "not found";
01417 } else if (HasBit(c->flags, GCF_UNSAFE)) {
01418 msg = "unsafe for static use";
01419 } else if (HasBit(c->flags, GCF_SYSTEM)) {
01420 msg = "system NewGRF";
01421 } else if (HasBit(c->flags, GCF_INVALID)) {
01422 msg = "incompatible to this version of OpenTTD";
01423 } else {
01424 msg = "unknown";
01425 }
01426
01427 ShowInfoF("ini: ignoring invalid NewGRF '%s': %s", item->name, msg);
01428 delete c;
01429 continue;
01430 }
01431
01432
01433 bool duplicate = false;
01434 for (const GRFConfig *gc = first; gc != NULL; gc = gc->next) {
01435 if (gc->ident.grfid == c->ident.grfid) {
01436 ShowInfoF("ini: ignoring NewGRF '%s': duplicate GRF ID with '%s'", item->name, gc->filename);
01437 duplicate = true;
01438 break;
01439 }
01440 }
01441 if (duplicate) {
01442 delete c;
01443 continue;
01444 }
01445
01446
01447 if (is_static) SetBit(c->flags, GCF_STATIC);
01448
01449
01450 *curr = c;
01451 curr = &c->next;
01452 }
01453
01454 return first;
01455 }
01456
01462 static void NewsDisplaySaveConfig(IniFile *ini, const char *grpname)
01463 {
01464 IniGroup *group = ini->GetGroup(grpname);
01465
01466 for (int i = 0; i < NT_END; i++) {
01467 const char *value;
01468 int v = _news_type_data[i].display;
01469
01470 value = (v == ND_OFF ? "off" : (v == ND_SUMMARY ? "summarized" : "full"));
01471
01472 group->GetItem(_news_type_data[i].name, true)->SetValue(value);
01473 }
01474 }
01475
01476 static void AISaveConfig(IniFile *ini, const char *grpname)
01477 {
01478 #ifdef ENABLE_AI
01479 IniGroup *group = ini->GetGroup(grpname);
01480
01481 if (group == NULL) return;
01482 group->Clear();
01483
01484 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01485 AIConfig *config = AIConfig::GetConfig(c, AIConfig::AISS_FORCE_NEWGAME);
01486 const char *name;
01487 char value[1024];
01488 config->SettingsToString(value, lengthof(value));
01489
01490 if (config->HasAI()) {
01491 name = config->GetName();
01492 } else {
01493 name = "none";
01494 }
01495
01496 IniItem *item = new IniItem(group, name, strlen(name));
01497 item->SetValue(value);
01498 }
01499 #endif
01500 }
01501
01506 static void SaveVersionInConfig(IniFile *ini)
01507 {
01508 IniGroup *group = ini->GetGroup("version");
01509
01510 char version[9];
01511 snprintf(version, lengthof(version), "%08X", _openttd_newgrf_version);
01512
01513 const char * const versions[][2] = {
01514 { "version_string", _openttd_revision },
01515 { "version_number", version }
01516 };
01517
01518 for (uint i = 0; i < lengthof(versions); i++) {
01519 group->GetItem(versions[i][0], true)->SetValue(versions[i][1]);
01520 }
01521 }
01522
01523
01524 static void GRFSaveConfig(IniFile *ini, const char *grpname, const GRFConfig *list)
01525 {
01526 ini->RemoveGroup(grpname);
01527 IniGroup *group = ini->GetGroup(grpname);
01528 const GRFConfig *c;
01529
01530 for (c = list; c != NULL; c = c->next) {
01531 char params[512];
01532 GRFBuildParamList(params, c, lastof(params));
01533
01534 group->GetItem(c->filename, true)->SetValue(params);
01535 }
01536 }
01537
01538
01539 static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list)
01540 {
01541 proc(ini, (const SettingDesc*)_misc_settings, "misc", NULL);
01542 proc(ini, (const SettingDesc*)_music_settings, "music", &_msf);
01543 #if defined(WIN32) && !defined(DEDICATED)
01544 proc(ini, (const SettingDesc*)_win32_settings, "win32", NULL);
01545 #endif
01546
01547 proc(ini, _settings, "patches", &_settings_newgame);
01548 proc(ini, _currency_settings,"currency", &_custom_currency);
01549 proc(ini, _company_settings, "company", &_settings_client.company);
01550
01551 #ifdef ENABLE_NETWORK
01552 proc_list(ini, "server_bind_addresses", &_network_bind_list);
01553 proc_list(ini, "servers", &_network_host_list);
01554 proc_list(ini, "bans", &_network_ban_list);
01555 #endif
01556 }
01557
01558 static IniFile *IniLoadConfig()
01559 {
01560 IniFile *ini = new IniFile(_list_group_names);
01561 ini->LoadFromDisk(_config_file);
01562 return ini;
01563 }
01564
01566 void LoadFromConfig()
01567 {
01568 IniFile *ini = IniLoadConfig();
01569 ResetCurrencies(false);
01570
01571 HandleSettingDescs(ini, IniLoadSettings, IniLoadSettingList);
01572 _grfconfig_newgame = GRFLoadConfig(ini, "newgrf", false);
01573 _grfconfig_static = GRFLoadConfig(ini, "newgrf-static", true);
01574 NewsDisplayLoadConfig(ini, "news_display");
01575 AILoadConfig(ini, "ai_players");
01576
01577 PrepareOldDiffCustom();
01578 IniLoadSettings(ini, _gameopt_settings, "gameopt", &_settings_newgame);
01579 HandleOldDiffCustom(false);
01580
01581 ValidateSettings();
01582 delete ini;
01583 }
01584
01586 void SaveToConfig()
01587 {
01588 IniFile *ini = IniLoadConfig();
01589
01590
01591 ini->RemoveGroup("patches");
01592 ini->RemoveGroup("yapf");
01593 ini->RemoveGroup("gameopt");
01594
01595 HandleSettingDescs(ini, IniSaveSettings, IniSaveSettingList);
01596 GRFSaveConfig(ini, "newgrf", _grfconfig_newgame);
01597 GRFSaveConfig(ini, "newgrf-static", _grfconfig_static);
01598 NewsDisplaySaveConfig(ini, "news_display");
01599 AISaveConfig(ini, "ai_players");
01600 SaveVersionInConfig(ini);
01601 ini->SaveToDisk(_config_file);
01602 delete ini;
01603 }
01604
01609 void GetGRFPresetList(GRFPresetList *list)
01610 {
01611 list->Clear();
01612
01613 IniFile *ini = IniLoadConfig();
01614 IniGroup *group;
01615 for (group = ini->group; group != NULL; group = group->next) {
01616 if (strncmp(group->name, "preset-", 7) == 0) {
01617 *list->Append() = strdup(group->name + 7);
01618 }
01619 }
01620
01621 delete ini;
01622 }
01623
01630 GRFConfig *LoadGRFPresetFromConfig(const char *config_name)
01631 {
01632 char *section = (char*)alloca(strlen(config_name) + 8);
01633 sprintf(section, "preset-%s", config_name);
01634
01635 IniFile *ini = IniLoadConfig();
01636 GRFConfig *config = GRFLoadConfig(ini, section, false);
01637 delete ini;
01638
01639 return config;
01640 }
01641
01648 void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
01649 {
01650 char *section = (char*)alloca(strlen(config_name) + 8);
01651 sprintf(section, "preset-%s", config_name);
01652
01653 IniFile *ini = IniLoadConfig();
01654 GRFSaveConfig(ini, section, config);
01655 ini->SaveToDisk(_config_file);
01656 delete ini;
01657 }
01658
01663 void DeleteGRFPresetFromConfig(const char *config_name)
01664 {
01665 char *section = (char*)alloca(strlen(config_name) + 8);
01666 sprintf(section, "preset-%s", config_name);
01667
01668 IniFile *ini = IniLoadConfig();
01669 ini->RemoveGroup(section);
01670 ini->SaveToDisk(_config_file);
01671 delete ini;
01672 }
01673
01674 const SettingDesc *GetSettingDescription(uint index)
01675 {
01676 if (index >= lengthof(_settings)) return NULL;
01677 return &_settings[index];
01678 }
01679
01691 CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01692 {
01693 const SettingDesc *sd = GetSettingDescription(p1);
01694
01695 if (sd == NULL) return CMD_ERROR;
01696 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR;
01697
01698 if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return CMD_ERROR;
01699 if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return CMD_ERROR;
01700 if ((sd->desc.flags & SGF_NEWGAME_ONLY) &&
01701 (_game_mode == GM_NORMAL ||
01702 (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0))) {
01703 return CMD_ERROR;
01704 }
01705
01706 if (flags & DC_EXEC) {
01707 void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01708
01709 int32 oldval = (int32)ReadValue(var, sd->save.conv);
01710 int32 newval = (int32)p2;
01711
01712 Write_ValidateSetting(var, sd, newval);
01713 newval = (int32)ReadValue(var, sd->save.conv);
01714
01715 if (oldval == newval) return CommandCost();
01716
01717 if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01718 WriteValue(var, sd->save.conv, (int64)oldval);
01719 return CommandCost();
01720 }
01721
01722 if (sd->desc.flags & SGF_NO_NETWORK) {
01723 GamelogStartAction(GLAT_SETTING);
01724 GamelogSetting(sd->desc.name, oldval, newval);
01725 GamelogStopAction();
01726 }
01727
01728 SetWindowDirty(WC_GAME_OPTIONS, 0);
01729 }
01730
01731 return CommandCost();
01732 }
01733
01744 CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01745 {
01746 if (p1 >= lengthof(_company_settings)) return CMD_ERROR;
01747 const SettingDesc *sd = &_company_settings[p1];
01748
01749 if (flags & DC_EXEC) {
01750 void *var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01751
01752 int32 oldval = (int32)ReadValue(var, sd->save.conv);
01753 int32 newval = (int32)p2;
01754
01755 Write_ValidateSetting(var, sd, newval);
01756 newval = (int32)ReadValue(var, sd->save.conv);
01757
01758 if (oldval == newval) return CommandCost();
01759
01760 if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01761 WriteValue(var, sd->save.conv, (int64)oldval);
01762 return CommandCost();
01763 }
01764
01765 SetWindowDirty(WC_GAME_OPTIONS, 0);
01766 }
01767
01768 return CommandCost();
01769 }
01770
01778 bool SetSettingValue(uint index, int32 value, bool force_newgame)
01779 {
01780 const SettingDesc *sd = &_settings[index];
01781
01782
01783
01784
01785 if (sd->save.conv & SLF_NO_NETWORK_SYNC) {
01786 void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01787 Write_ValidateSetting(var, sd, value);
01788
01789 if (_game_mode != GM_MENU) {
01790 void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01791 Write_ValidateSetting(var2, sd, value);
01792 }
01793 if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01794 SetWindowDirty(WC_GAME_OPTIONS, 0);
01795 return true;
01796 }
01797
01798 if (force_newgame) {
01799 void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01800 Write_ValidateSetting(var2, sd, value);
01801 return true;
01802 }
01803
01804
01805 if (!_networking || (_networking && _network_server)) {
01806 return DoCommandP(0, index, value, CMD_CHANGE_SETTING);
01807 }
01808 return false;
01809 }
01810
01817 void SetCompanySetting(uint index, int32 value)
01818 {
01819 const SettingDesc *sd = &_company_settings[index];
01820 if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01821 DoCommandP(0, index, value, CMD_CHANGE_COMPANY_SETTING);
01822 } else {
01823 void *var = GetVariableAddress(&_settings_client.company, &sd->save);
01824 Write_ValidateSetting(var, sd, value);
01825 if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01826 }
01827 }
01828
01832 void SetDefaultCompanySettings(CompanyID cid)
01833 {
01834 Company *c = Company::Get(cid);
01835 const SettingDesc *sd;
01836 for (sd = _company_settings; sd->save.cmd != SL_END; sd++) {
01837 void *var = GetVariableAddress(&c->settings, &sd->save);
01838 Write_ValidateSetting(var, sd, (int32)(size_t)sd->desc.def);
01839 }
01840 }
01841
01842 #if defined(ENABLE_NETWORK)
01843
01846 void SyncCompanySettings()
01847 {
01848 const SettingDesc *sd;
01849 uint i = 0;
01850 for (sd = _company_settings; sd->save.cmd != SL_END; sd++, i++) {
01851 const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01852 const void *new_var = GetVariableAddress(&_settings_client.company, &sd->save);
01853 uint32 old_value = (uint32)ReadValue(old_var, sd->save.conv);
01854 uint32 new_value = (uint32)ReadValue(new_var, sd->save.conv);
01855 if (old_value != new_value) NetworkSendCommand(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, NULL, NULL, _local_company);
01856 }
01857 }
01858 #endif
01859
01865 uint GetCompanySettingIndex(const char *name)
01866 {
01867 uint i;
01868 const SettingDesc *sd = GetSettingFromName(name, &i);
01869 assert(sd != NULL && (sd->desc.flags & SGF_PER_COMPANY) != 0);
01870 return i;
01871 }
01872
01878 CompanyProfile GetCompanyProfile(const Company &company)
01879 {
01880 CompanyProfile company_profile;
01881
01882 SetDParam(0, company.index);
01883 GetString(company_profile.president_name, STR_PRESIDENT_NAME, lastof(company_profile.president_name));
01884
01885 SetDParam(0, company.index);
01886 GetString(company_profile.company_name, STR_COMPANY_NAME, lastof(company_profile.company_name));
01887
01888 company_profile.face = company.face;
01889
01890 for (int i = 0; i < LS_END; i++) {
01891 company_profile.livery[i] = company.livery[i];
01892 }
01893
01894 return company_profile;
01895
01896 }
01897
01903 StringID SetCompanyProfile(const CompanyProfile &company_profile)
01904 {
01905 char buf[MAX_LENGTH_PRESIDENT_NAME_CHARS > MAX_LENGTH_COMPANY_NAME_CHARS ? MAX_LENGTH_PRESIDENT_NAME_CHARS : MAX_LENGTH_COMPANY_NAME_CHARS];
01906
01907 sprintf(buf, "company_profile");
01908
01909
01910 SetDParam(0, _company_pool.Get(_local_company)->index);
01911 GetString(buf, STR_PRESIDENT_NAME, lastof(buf));
01912 if (strcmp(buf, company_profile.president_name) != 0) {
01913 if (!DoCommandP(0, 0, 0, CMD_RENAME_PRESIDENT, NULL, company_profile.president_name)) return STR_ERROR_CAN_T_CHANGE_PRESIDENT;
01914 }
01915
01916
01917 SetDParam(0, _company_pool.Get(_local_company)->index);
01918 GetString(buf, STR_COMPANY_NAME, lastof(buf));
01919 if (strcmp(buf, company_profile.company_name) != 0) {
01920 if (!DoCommandP(0, 0, 0, CMD_RENAME_COMPANY, NULL, company_profile.company_name)) return STR_ERROR_CAN_T_CHANGE_COMPANY_NAME;
01921 }
01922
01923
01924 if (!IsValidCompanyManagerFace(company_profile.face) || !DoCommandP(0, 0, company_profile.face, CMD_SET_COMPANY_MANAGER_FACE)) return STR_PROFILE_FACE_ERROR;
01925
01926
01927 for (int i = 0; i < LS_END; i++) {
01928 if (!DoCommandP(0, i, company_profile.livery[i].colour1, CMD_SET_COMPANY_COLOUR)) return STR_PROFILE_COLOUR_ERROR;
01929 if (!DoCommandP(0, i | (1 << 8), company_profile.livery[i].colour2, CMD_SET_COMPANY_COLOUR)) return STR_PROFILE_COLOUR_ERROR;
01930 if (!DoCommandP(0, i | (2 << 8), company_profile.livery[i].in_use, CMD_SET_COMPANY_COLOUR)) return STR_PROFILE_COLOUR_ERROR;
01931 }
01932
01933 return STR_NULL;
01934
01935 }
01936
01942 StringID CompanyLoadProfile(CompanyProfile &company_profile)
01943 {
01944 IniFile *ini;
01945 IniGroup *group;
01946 IniItem *item;
01947 char buf[MAX_LENGTH_PRESIDENT_NAME_CHARS > MAX_LENGTH_COMPANY_NAME_CHARS ? MAX_LENGTH_PRESIDENT_NAME_CHARS : MAX_LENGTH_COMPANY_NAME_CHARS];
01948 char buf_ptr[16];
01949 uint colour1,colour2;
01950
01951 sprintf(buf, "company_profile");
01952
01953
01954 ini = IniLoadConfig();
01955 group = ini->GetGroup(buf);
01956
01957
01958 item = group->GetItem("president_name", true);
01959 if (item->value == NULL || strlen(item->value) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) return STR_PROFILE_LOAD_ERROR;
01960 strcpy(company_profile.president_name, item->value);
01961
01962
01963 item = group->GetItem("company_name", true);
01964 if (item->value == NULL || strlen(item->value) >= MAX_LENGTH_COMPANY_NAME_CHARS) return STR_PROFILE_LOAD_ERROR;
01965 strcpy(company_profile.company_name, item->value);
01966
01967
01968 item = group->GetItem("face", true);
01969 if (item->value == NULL || sscanf(item->value, "%u", &(company_profile.face)) != 1) return STR_PROFILE_LOAD_ERROR;
01970
01971
01972 for (int i = 0; i < LS_END; i++) {
01973 sprintf(buf_ptr, "livery_%i", i);
01974 item = group->GetItem(buf_ptr, true);
01975 if (item->value == NULL) return STR_PROFILE_LOAD_ERROR;
01976 sscanf(strtok(item->value, ","), "%u", &colour1);
01977 sscanf(strtok(NULL, ","), "%u", &colour2);
01978 company_profile.livery[i].colour1 = (byte) colour1;
01979 company_profile.livery[i].colour2 = (byte) colour2;
01980 company_profile.livery[i].in_use = strcmp(strtok(NULL, ","),"1") == 0 ? true : false;
01981 }
01982
01983 return STR_NULL;
01984 }
01985
01991 StringID CompanySaveProfile(const CompanyProfile &company_profile)
01992 {
01993 IniFile *ini;
01994 IniGroup *group;
01995 char buf[32];
01996 char buf_item[16];
01997 char buf_ptr[16];
01998
01999 sprintf(buf, "company_profile");
02000
02001 ini = IniLoadConfig();
02002 ini->RemoveGroup(buf);
02003 group = ini->GetGroup(buf);
02004
02005 group->GetItem("president_name", true)->SetValue(company_profile.president_name);
02006 group->GetItem("company_name", true)->SetValue(company_profile.company_name);
02007
02008 sprintf(buf, "%u", company_profile.face);
02009 group->GetItem("face", true)->SetValue(buf);
02010
02011
02012 for (int i = 0; i < LS_END; i++) {
02013 sprintf(buf_item, "livery_%i", i);
02014 sprintf(buf_ptr, "%u,%u,%u", uint(company_profile.livery[i].colour1), uint(company_profile.livery[i].colour2), uint(company_profile.livery[i].in_use));
02015 group->GetItem(buf_item, true)->SetValue(buf_ptr);
02016 }
02017
02018 StringID result = ini->SaveToDisk(_config_file) ? STR_NULL : STR_PROFILE_SAVE_ERROR;
02019 delete ini;
02020
02021 return result;
02022 }
02023
02031 bool SetSettingValue(uint index, const char *value, bool force_newgame)
02032 {
02033 const SettingDesc *sd = &_settings[index];
02034 assert(sd->save.conv & SLF_NO_NETWORK_SYNC);
02035
02036 if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) {
02037 char **var = (char**)GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
02038 free(*var);
02039 *var = strcmp(value, "(null)") == 0 ? NULL : strdup(value);
02040 } else {
02041 char *var = (char*)GetVariableAddress(NULL, &sd->save);
02042 ttd_strlcpy(var, value, sd->save.length);
02043 }
02044 if (sd->desc.proc != NULL) sd->desc.proc(0);
02045
02046 return true;
02047 }
02048
02056 const SettingDesc *GetSettingFromName(const char *name, uint *i)
02057 {
02058 const SettingDesc *sd;
02059
02060
02061 for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
02062 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
02063 if (strcmp(sd->desc.name, name) == 0) return sd;
02064 }
02065
02066
02067 for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
02068 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
02069 const char *short_name = strchr(sd->desc.name, '.');
02070 if (short_name != NULL) {
02071 short_name++;
02072 if (strcmp(short_name, name) == 0) return sd;
02073 }
02074 }
02075
02076 if (strncmp(name, "company.", 8) == 0) name += 8;
02077
02078 for (*i = 0, sd = _company_settings; sd->save.cmd != SL_END; sd++, (*i)++) {
02079 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
02080 if (strcmp(sd->desc.name, name) == 0) return sd;
02081 }
02082
02083 return NULL;
02084 }
02085
02086
02087
02088 void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
02089 {
02090 uint index;
02091 const SettingDesc *sd = GetSettingFromName(name, &index);
02092
02093 if (sd == NULL) {
02094 IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
02095 return;
02096 }
02097
02098 bool success;
02099 if (sd->desc.cmd == SDT_STRING) {
02100 success = SetSettingValue(index, value, force_newgame);
02101 } else {
02102 uint32 val;
02103 extern bool GetArgumentInteger(uint32 *value, const char *arg);
02104 success = GetArgumentInteger(&val, value);
02105 if (!success) {
02106 IConsolePrintF(CC_ERROR, "'%s' is not an integer.", value);
02107 return;
02108 }
02109
02110 success = SetSettingValue(index, val, force_newgame);
02111 }
02112
02113 if (!success) {
02114 if (_network_server) {
02115 IConsoleError("This command/variable is not available during network games.");
02116 } else {
02117 IConsoleError("This command/variable is only available to a network server.");
02118 }
02119 }
02120 }
02121
02122 void IConsoleSetSetting(const char *name, int value)
02123 {
02124 uint index;
02125 const SettingDesc *sd = GetSettingFromName(name, &index);
02126 assert(sd != NULL);
02127 SetSettingValue(index, value);
02128 }
02129
02135 void IConsoleGetSetting(const char *name, bool force_newgame)
02136 {
02137 char value[20];
02138 uint index;
02139 const SettingDesc *sd = GetSettingFromName(name, &index);
02140 const void *ptr;
02141
02142 if (sd == NULL) {
02143 IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
02144 return;
02145 }
02146
02147 ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
02148
02149 if (sd->desc.cmd == SDT_STRING) {
02150 IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char **)ptr : (const char *)ptr);
02151 } else {
02152 if (sd->desc.cmd == SDT_BOOLX) {
02153 snprintf(value, sizeof(value), (*(bool*)ptr == 1) ? "on" : "off");
02154 } else {
02155 snprintf(value, sizeof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
02156 }
02157
02158 IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s' (min: %s%d, max: %u)",
02159 name, value, (sd->desc.flags & SGF_0ISDISABLED) ? "(0) " : "", sd->desc.min, sd->desc.max);
02160 }
02161 }
02162
02168 void IConsoleListSettings(const char *prefilter)
02169 {
02170 IConsolePrintF(CC_WARNING, "All settings with their current value:");
02171
02172 for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) {
02173 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
02174 if (prefilter != NULL && strstr(sd->desc.name, prefilter) == NULL) continue;
02175 char value[80];
02176 const void *ptr = GetVariableAddress(&GetGameSettings(), &sd->save);
02177
02178 if (sd->desc.cmd == SDT_BOOLX) {
02179 snprintf(value, lengthof(value), (*(bool*)ptr == 1) ? "on" : "off");
02180 } else if (sd->desc.cmd == SDT_STRING) {
02181 snprintf(value, sizeof(value), "%s", (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char **)ptr : (const char *)ptr);
02182 } else {
02183 snprintf(value, lengthof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
02184 }
02185 IConsolePrintF(CC_DEFAULT, "%s = %s", sd->desc.name, value);
02186 }
02187
02188 IConsolePrintF(CC_WARNING, "Use 'setting' command to change a value");
02189 }
02190
02197 static void LoadSettings(const SettingDesc *osd, void *object)
02198 {
02199 for (; osd->save.cmd != SL_END; osd++) {
02200 const SaveLoad *sld = &osd->save;
02201 void *ptr = GetVariableAddress(object, sld);
02202
02203 if (!SlObjectMember(ptr, sld)) continue;
02204 if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, osd, ReadValue(ptr, sld->conv));
02205 }
02206 }
02207
02214 static void SaveSettings(const SettingDesc *sd, void *object)
02215 {
02216
02217
02218 const SettingDesc *i;
02219 size_t length = 0;
02220 for (i = sd; i->save.cmd != SL_END; i++) {
02221 length += SlCalcObjMemberLength(object, &i->save);
02222 }
02223 SlSetLength(length);
02224
02225 for (i = sd; i->save.cmd != SL_END; i++) {
02226 void *ptr = GetVariableAddress(object, &i->save);
02227 SlObjectMember(ptr, &i->save);
02228 }
02229 }
02230
02231 static void Load_OPTS()
02232 {
02233
02234
02235
02236 PrepareOldDiffCustom();
02237 LoadSettings(_gameopt_settings, &_settings_game);
02238 HandleOldDiffCustom(true);
02239 }
02240
02241 static void Load_PATS()
02242 {
02243
02244
02245
02246 LoadSettings(_settings, &_settings_game);
02247 }
02248
02249 static void Check_PATS()
02250 {
02251 LoadSettings(_settings, &_load_check_data.settings);
02252 }
02253
02254 static void Save_PATS()
02255 {
02256 SaveSettings(_settings, &_settings_game);
02257 }
02258
02259 void CheckConfig()
02260 {
02261
02262
02263
02264
02265 if (_settings_newgame.pf.opf.pf_maxdepth == 16 && _settings_newgame.pf.opf.pf_maxlength == 512) {
02266 _settings_newgame.pf.opf.pf_maxdepth = 48;
02267 _settings_newgame.pf.opf.pf_maxlength = 4096;
02268 }
02269 }
02270
02271 extern const ChunkHandler _setting_chunk_handlers[] = {
02272 { 'OPTS', NULL, Load_OPTS, NULL, NULL, CH_RIFF},
02273 { 'PATS', Save_PATS, Load_PATS, NULL, Check_PATS, CH_RIFF | CH_LAST},
02274 };
02275
02276 static bool IsSignedVarMemType(VarType vt)
02277 {
02278 switch (GetVarMemType(vt)) {
02279 case SLE_VAR_I8:
02280 case SLE_VAR_I16:
02281 case SLE_VAR_I32:
02282 case SLE_VAR_I64:
02283 return true;
02284 }
02285 return false;
02286 }