00001
00002
00003
00004
00005
00006
00007
00008
00009
00025 #include "stdafx.h"
00026 #include "currency.h"
00027 #include "screenshot.h"
00028 #include "variables.h"
00029 #include "network/network.h"
00030 #include "network/network_func.h"
00031 #include "settings_internal.h"
00032 #include "command_func.h"
00033 #include "console_func.h"
00034 #include "pathfinder/pathfinder_type.h"
00035 #include "genworld.h"
00036 #include "train.h"
00037 #include "news_func.h"
00038 #include "window_func.h"
00039 #include "strings_func.h"
00040 #include "vehicle_func.h"
00041 #include "sound_func.h"
00042 #include "company_func.h"
00043 #include "rev.h"
00044 #ifdef WITH_FREETYPE
00045 #include "fontcache.h"
00046 #endif
00047 #include "textbuf_gui.h"
00048 #include "rail_gui.h"
00049 #include "elrail_func.h"
00050 #include "gui.h"
00051 #include "town.h"
00052 #include "video/video_driver.hpp"
00053 #include "sound/sound_driver.hpp"
00054 #include "music/music_driver.hpp"
00055 #include "blitter/factory.hpp"
00056 #include "base_media_base.h"
00057 #include "gamelog.h"
00058 #include "settings_func.h"
00059 #include "ini_type.h"
00060 #include "ai/ai_config.hpp"
00061 #include "ai/ai.hpp"
00062 #include "newgrf.h"
00063 #include "ship.h"
00064 #include "company_base.h"
00065 #include "engine_base.h"
00066 #include "smallmap_gui.h"
00067
00068 #include "void_map.h"
00069 #include "station_base.h"
00070
00071 #include "table/strings.h"
00072 #include "table/settings.h"
00073
00074 ClientSettings _settings_client;
00075 GameSettings _settings_game;
00076 GameSettings _settings_newgame;
00077 VehicleDefaultSettings _old_vds;
00078
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
00100 static int lookup_oneofmany(const char *many, const char *one, size_t onelen = 0)
00101 {
00102 const char *s;
00103 int idx;
00104
00105 if (onelen == 0) onelen = strlen(one);
00106
00107
00108 if (*one >= '0' && *one <= '9')
00109 return strtoul(one, NULL, 0);
00110
00111 idx = 0;
00112 for (;;) {
00113
00114 s = many;
00115 while (*s != '|' && *s != 0) s++;
00116 if ((size_t)(s - many) == onelen && !memcmp(one, many, onelen)) return idx;
00117 if (*s == 0) return -1;
00118 many = s + 1;
00119 idx++;
00120 }
00121 }
00122
00128 static uint32 lookup_manyofmany(const char *many, const char *str)
00129 {
00130 const char *s;
00131 int r;
00132 uint32 res = 0;
00133
00134 for (;;) {
00135
00136 while (*str == ' ' || *str == '\t' || *str == '|') str++;
00137 if (*str == 0) break;
00138
00139 s = str;
00140 while (*s != 0 && *s != ' ' && *s != '\t' && *s != '|') s++;
00141
00142 r = lookup_oneofmany(many, str, s - str);
00143 if (r == -1) return (uint32)-1;
00144
00145 SetBit(res, r);
00146 if (*s == 0) break;
00147 str = s + 1;
00148 }
00149 return res;
00150 }
00151
00158 static int parse_intlist(const char *p, int *items, int maxitems)
00159 {
00160 int n = 0, v;
00161 char *end;
00162
00163 for (;;) {
00164 v = strtol(p, &end, 0);
00165 if (p == end || n == maxitems) return -1;
00166 p = end;
00167 items[n++] = v;
00168 if (*p == '\0') break;
00169 if (*p != ',' && *p != ' ') return -1;
00170 p++;
00171 }
00172
00173 return n;
00174 }
00175
00182 static bool load_intlist(const char *str, void *array, int nelems, VarType type)
00183 {
00184 int items[64];
00185 int i, nitems;
00186
00187 if (str == NULL) {
00188 memset(items, 0, sizeof(items));
00189 nitems = nelems;
00190 } else {
00191 nitems = parse_intlist(str, items, lengthof(items));
00192 if (nitems != nelems) return false;
00193 }
00194
00195 switch (type) {
00196 case SLE_VAR_BL:
00197 case SLE_VAR_I8:
00198 case SLE_VAR_U8:
00199 for (i = 0; i != nitems; i++) ((byte*)array)[i] = items[i];
00200 break;
00201 case SLE_VAR_I16:
00202 case SLE_VAR_U16:
00203 for (i = 0; i != nitems; i++) ((uint16*)array)[i] = items[i];
00204 break;
00205 case SLE_VAR_I32:
00206 case SLE_VAR_U32:
00207 for (i = 0; i != nitems; i++) ((uint32*)array)[i] = items[i];
00208 break;
00209 default: NOT_REACHED();
00210 }
00211
00212 return true;
00213 }
00214
00222 static void make_intlist(char *buf, const char *last, const void *array, int nelems, VarType type)
00223 {
00224 int i, v = 0;
00225 byte *p = (byte*)array;
00226
00227 for (i = 0; i != nelems; i++) {
00228 switch (type) {
00229 case SLE_VAR_BL:
00230 case SLE_VAR_I8: v = *(int8*)p; p += 1; break;
00231 case SLE_VAR_U8: v = *(byte*)p; p += 1; break;
00232 case SLE_VAR_I16: v = *(int16*)p; p += 2; break;
00233 case SLE_VAR_U16: v = *(uint16*)p; p += 2; break;
00234 case SLE_VAR_I32: v = *(int32*)p; p += 4; break;
00235 case SLE_VAR_U32: v = *(uint32*)p; p += 4; break;
00236 default: NOT_REACHED();
00237 }
00238 buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
00239 }
00240 }
00241
00247 static void make_oneofmany(char *buf, const char *last, const char *many, int id)
00248 {
00249 int orig_id = id;
00250
00251
00252 while (--id >= 0) {
00253 for (; *many != '|'; many++) {
00254 if (*many == '\0') {
00255 seprintf(buf, last, "%d", orig_id);
00256 return;
00257 }
00258 }
00259 many++;
00260 }
00261
00262
00263 while (*many != '\0' && *many != '|' && buf < last) *buf++ = *many++;
00264 *buf = '\0';
00265 }
00266
00273 static void make_manyofmany(char *buf, const char *last, const char *many, uint32 x)
00274 {
00275 const char *start;
00276 int i = 0;
00277 bool init = true;
00278
00279 for (; x != 0; x >>= 1, i++) {
00280 start = many;
00281 while (*many != 0 && *many != '|') many++;
00282
00283 if (HasBit(x, 0)) {
00284 if (!init) buf += seprintf(buf, last, "|");
00285 init = false;
00286 if (start == many) {
00287 buf += seprintf(buf, last, "%d", i);
00288 } else {
00289 memcpy(buf, start, many - start);
00290 buf += many - start;
00291 }
00292 }
00293
00294 if (*many == '|') many++;
00295 }
00296
00297 *buf = '\0';
00298 }
00299
00304 static const void *string_to_val(const SettingDescBase *desc, const char *orig_str)
00305 {
00306 const char *str = orig_str == NULL ? "" : orig_str;
00307 switch (desc->cmd) {
00308 case SDT_NUMX: {
00309 char *end;
00310 unsigned long val = strtoul(str, &end, 0);
00311 if (*end != '\0') ShowInfoF("ini: trailing characters at end of setting '%s'", desc->name);
00312 return (void*)val;
00313 }
00314 case SDT_ONEOFMANY: {
00315 long r = lookup_oneofmany(desc->many, str);
00316
00317
00318 if (r == -1 && desc->proc_cnvt != NULL) r = desc->proc_cnvt(str);
00319 if (r != -1) return (void*)r;
00320 ShowInfoF("ini: invalid value '%s' for '%s'", str, desc->name);
00321 return 0;
00322 }
00323 case SDT_MANYOFMANY: {
00324 unsigned long r = lookup_manyofmany(desc->many, str);
00325 if (r != (unsigned long)-1) return (void*)r;
00326 ShowInfoF("ini: invalid value '%s' for '%s'", str, desc->name);
00327 return 0;
00328 }
00329 case SDT_BOOLX:
00330 if (strcmp(str, "true") == 0 || strcmp(str, "on") == 0 || strcmp(str, "1") == 0)
00331 return (void*)true;
00332 if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0)
00333 return (void*)false;
00334 ShowInfoF("ini: invalid setting value '%s' for '%s'", str, desc->name);
00335 break;
00336
00337 case SDT_STRING: return orig_str;
00338 case SDT_INTLIST: return str;
00339 default: break;
00340 }
00341
00342 return NULL;
00343 }
00344
00352 static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val)
00353 {
00354 const SettingDescBase *sdb = &sd->desc;
00355
00356 if (sdb->cmd != SDT_BOOLX &&
00357 sdb->cmd != SDT_NUMX &&
00358 sdb->cmd != SDT_ONEOFMANY &&
00359 sdb->cmd != SDT_MANYOFMANY) {
00360 return;
00361 }
00362
00363
00364 if (sdb->cmd != SDT_MANYOFMANY) {
00365
00366
00367
00368
00369
00370 switch (GetVarMemType(sd->save.conv)) {
00371 case SLE_VAR_NULL: return;
00372 case SLE_VAR_BL:
00373 case SLE_VAR_I8:
00374 case SLE_VAR_U8:
00375 case SLE_VAR_I16:
00376 case SLE_VAR_U16:
00377 case SLE_VAR_I32: {
00378
00379 if (!(sdb->flags & SGF_0ISDISABLED) || val != 0) val = Clamp(val, sdb->min, sdb->max);
00380 } break;
00381 case SLE_VAR_U32: {
00382
00383 uint min = ((sdb->flags & SGF_0ISDISABLED) && (uint)val <= (uint)sdb->min) ? 0 : sdb->min;
00384 WriteValue(ptr, SLE_VAR_U32, (int64)ClampU(val, min, sdb->max));
00385 return;
00386 }
00387 case SLE_VAR_I64:
00388 case SLE_VAR_U64:
00389 default: NOT_REACHED();
00390 }
00391 }
00392
00393 WriteValue(ptr, sd->save.conv, (int64)val);
00394 }
00395
00402 static void ini_load_settings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00403 {
00404 IniGroup *group;
00405 IniGroup *group_def = ini->GetGroup(grpname);
00406 IniItem *item;
00407 const void *p;
00408 void *ptr;
00409 const char *s;
00410
00411 for (; sd->save.cmd != SL_END; sd++) {
00412 const SettingDescBase *sdb = &sd->desc;
00413 const SaveLoad *sld = &sd->save;
00414
00415 if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00416
00417
00418 s = strchr(sdb->name, '.');
00419 if (s != NULL) {
00420 group = ini->GetGroup(sdb->name, s - sdb->name);
00421 s++;
00422 } else {
00423 s = sdb->name;
00424 group = group_def;
00425 }
00426
00427 item = group->GetItem(s, false);
00428 if (item == NULL && group != group_def) {
00429
00430
00431 item = group_def->GetItem(s, false);
00432 }
00433 if (item == NULL) {
00434
00435
00436 const char *sc = strchr(s, '.');
00437 if (sc != NULL) item = ini->GetGroup(s, sc - s)->GetItem(sc + 1, false);
00438 }
00439
00440 p = (item == NULL) ? sdb->def : string_to_val(sdb, item->value);
00441 ptr = GetVariableAddress(object, sld);
00442
00443 switch (sdb->cmd) {
00444 case SDT_BOOLX:
00445 case SDT_NUMX:
00446 case SDT_ONEOFMANY:
00447 case SDT_MANYOFMANY:
00448 Write_ValidateSetting(ptr, sd, (int32)(size_t)p); break;
00449
00450 case SDT_STRING:
00451 switch (GetVarMemType(sld->conv)) {
00452 case SLE_VAR_STRB:
00453 case SLE_VAR_STRBQ:
00454 if (p != NULL) ttd_strlcpy((char*)ptr, (const char*)p, sld->length);
00455 break;
00456 case SLE_VAR_STR:
00457 case SLE_VAR_STRQ:
00458 free(*(char**)ptr);
00459 *(char**)ptr = p == NULL ? NULL : strdup((const char*)p);
00460 break;
00461 case SLE_VAR_CHAR: if (p != NULL) *(char*)ptr = *(char*)p; break;
00462 default: NOT_REACHED();
00463 }
00464 break;
00465
00466 case SDT_INTLIST: {
00467 if (!load_intlist((const char*)p, ptr, sld->length, GetVarMemType(sld->conv))) {
00468 ShowInfoF("ini: error in array '%s'", sdb->name);
00469 } else if (sd->desc.proc_cnvt != NULL) {
00470 sd->desc.proc_cnvt((const char*)p);
00471 }
00472 break;
00473 }
00474 default: NOT_REACHED();
00475 }
00476 }
00477 }
00478
00490 static void ini_save_settings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00491 {
00492 IniGroup *group_def = NULL, *group;
00493 IniItem *item;
00494 char buf[512];
00495 const char *s;
00496 void *ptr;
00497
00498 for (; sd->save.cmd != SL_END; sd++) {
00499 const SettingDescBase *sdb = &sd->desc;
00500 const SaveLoad *sld = &sd->save;
00501
00502
00503
00504 if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00505 if (sld->conv & SLF_CONFIG_NO) continue;
00506
00507
00508 s = strchr(sdb->name, '.');
00509 if (s != NULL) {
00510 group = ini->GetGroup(sdb->name, s - sdb->name);
00511 s++;
00512 } else {
00513 if (group_def == NULL) group_def = ini->GetGroup(grpname);
00514 s = sdb->name;
00515 group = group_def;
00516 }
00517
00518 item = group->GetItem(s, true);
00519 ptr = GetVariableAddress(object, sld);
00520
00521 if (item->value != NULL) {
00522
00523 const void *p = string_to_val(sdb, item->value);
00524
00525
00526
00527 switch (sdb->cmd) {
00528 case SDT_BOOLX:
00529 case SDT_NUMX:
00530 case SDT_ONEOFMANY:
00531 case SDT_MANYOFMANY:
00532 switch (GetVarMemType(sld->conv)) {
00533 case SLE_VAR_BL:
00534 if (*(bool*)ptr == (p != NULL)) continue;
00535 break;
00536 case SLE_VAR_I8:
00537 case SLE_VAR_U8:
00538 if (*(byte*)ptr == (byte)(unsigned long)p) continue;
00539 break;
00540 case SLE_VAR_I16:
00541 case SLE_VAR_U16:
00542 if (*(uint16*)ptr == (uint16)(unsigned long)p) continue;
00543 break;
00544 case SLE_VAR_I32:
00545 case SLE_VAR_U32:
00546 if (*(uint32*)ptr == (uint32)(unsigned long)p) continue;
00547 break;
00548 default: NOT_REACHED();
00549 }
00550 break;
00551 default: break;
00552 }
00553 }
00554
00555
00556 switch (sdb->cmd) {
00557 case SDT_BOOLX:
00558 case SDT_NUMX:
00559 case SDT_ONEOFMANY:
00560 case SDT_MANYOFMANY: {
00561 uint32 i = (uint32)ReadValue(ptr, sld->conv);
00562
00563 switch (sdb->cmd) {
00564 case SDT_BOOLX: strecpy(buf, (i != 0) ? "true" : "false", lastof(buf)); break;
00565 case SDT_NUMX: seprintf(buf, lastof(buf), IsSignedVarMemType(sld->conv) ? "%d" : "%u", i); break;
00566 case SDT_ONEOFMANY: make_oneofmany(buf, lastof(buf), sdb->many, i); break;
00567 case SDT_MANYOFMANY: make_manyofmany(buf, lastof(buf), sdb->many, i); break;
00568 default: NOT_REACHED();
00569 }
00570 } break;
00571
00572 case SDT_STRING:
00573 switch (GetVarMemType(sld->conv)) {
00574 case SLE_VAR_STRB: strecpy(buf, (char*)ptr, lastof(buf)); break;
00575 case SLE_VAR_STRBQ:seprintf(buf, lastof(buf), "\"%s\"", (char*)ptr); break;
00576 case SLE_VAR_STR: strecpy(buf, *(char**)ptr, lastof(buf)); break;
00577 case SLE_VAR_STRQ:
00578 if (*(char**)ptr == NULL) {
00579 buf[0] = '\0';
00580 } else {
00581 seprintf(buf, lastof(buf), "\"%s\"", *(char**)ptr);
00582 }
00583 break;
00584 case SLE_VAR_CHAR: buf[0] = *(char*)ptr; buf[1] = '\0'; break;
00585 default: NOT_REACHED();
00586 }
00587 break;
00588
00589 case SDT_INTLIST:
00590 make_intlist(buf, lastof(buf), ptr, sld->length, GetVarMemType(sld->conv));
00591 break;
00592 default: NOT_REACHED();
00593 }
00594
00595
00596 free(item->value);
00597 item->value = strdup(buf);
00598 }
00599 }
00600
00609 static void ini_load_setting_list(IniFile *ini, const char *grpname, StringList *list)
00610 {
00611 IniGroup *group = ini->GetGroup(grpname);
00612
00613 if (group == NULL || list == NULL) return;
00614
00615 list->Clear();
00616
00617 for (const IniItem *item = group->item; item != NULL; item = item->next) {
00618 if (item->name != NULL) *list->Append() = strdup(item->name);
00619 }
00620 }
00621
00630 static void ini_save_setting_list(IniFile *ini, const char *grpname, StringList *list)
00631 {
00632 IniGroup *group = ini->GetGroup(grpname);
00633
00634 if (group == NULL || list == NULL) return;
00635 group->Clear();
00636
00637 for (char **iter = list->Begin(); iter != list->End(); iter++) {
00638 group->GetItem(*iter, true)->SetValue("");
00639 }
00640 }
00641
00642
00643
00644 static bool v_PositionMainToolbar(int32 p1)
00645 {
00646 if (_game_mode != GM_MENU) PositionMainToolbar(NULL);
00647 return true;
00648 }
00649
00650 static bool PopulationInLabelActive(int32 p1)
00651 {
00652 UpdateAllTownVirtCoords();
00653 return true;
00654 }
00655
00656 static bool RedrawScreen(int32 p1)
00657 {
00658 MarkWholeScreenDirty();
00659 return true;
00660 }
00661
00667 static bool RedrawSmallmap(int32 p1)
00668 {
00669 BuildLandLegend();
00670 SetWindowClassesDirty(WC_SMALLMAP);
00671 return true;
00672 }
00673
00674 static bool InvalidateDetailsWindow(int32 p1)
00675 {
00676 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00677 return true;
00678 }
00679
00680 static bool InvalidateStationBuildWindow(int32 p1)
00681 {
00682 SetWindowDirty(WC_BUILD_STATION, 0);
00683 return true;
00684 }
00685
00686 static bool InvalidateBuildIndustryWindow(int32 p1)
00687 {
00688 InvalidateWindowData(WC_BUILD_INDUSTRY, 0);
00689 return true;
00690 }
00691
00692 static bool CloseSignalGUI(int32 p1)
00693 {
00694 if (p1 == 0) {
00695 DeleteWindowByClass(WC_BUILD_SIGNAL);
00696 }
00697 return true;
00698 }
00699
00700 static bool InvalidateTownViewWindow(int32 p1)
00701 {
00702 InvalidateWindowClassesData(WC_TOWN_VIEW, p1);
00703 return true;
00704 }
00705
00706 static bool DeleteSelectStationWindow(int32 p1)
00707 {
00708 DeleteWindowById(WC_SELECT_STATION, 0);
00709 return true;
00710 }
00711
00712 static bool UpdateConsists(int32 p1)
00713 {
00714 Train *t;
00715 FOR_ALL_TRAINS(t) {
00716
00717 if (t->IsFrontEngine() || t->IsFreeWagon()) t->ConsistChanged(true);
00718 }
00719 return true;
00720 }
00721
00722
00723 static bool CheckInterval(int32 p1)
00724 {
00725 VehicleDefaultSettings *vds;
00726 if (_game_mode == GM_MENU || !Company::IsValidID(_current_company)) {
00727 vds = &_settings_client.company.vehicle;
00728 } else {
00729 vds = &Company::Get(_current_company)->settings.vehicle;
00730 }
00731
00732 if (p1) {
00733 vds->servint_trains = 50;
00734 vds->servint_roadveh = 50;
00735 vds->servint_aircraft = 50;
00736 vds->servint_ships = 50;
00737 } else {
00738 vds->servint_trains = 150;
00739 vds->servint_roadveh = 150;
00740 vds->servint_aircraft = 360;
00741 vds->servint_ships = 100;
00742 }
00743
00744 InvalidateDetailsWindow(0);
00745
00746 return true;
00747 }
00748
00749 static bool TrainAccelerationModelChanged(int32 p1)
00750 {
00751 Train *t;
00752 FOR_ALL_TRAINS(t) {
00753 if (t->IsFrontEngine()) {
00754 t->tcache.cached_max_curve_speed = t->GetCurveSpeedLimit();
00755 t->UpdateAcceleration();
00756 }
00757 }
00758
00759 return true;
00760 }
00761
00767 static bool TrainSlopeSteepnessChanged(int32 p1)
00768 {
00769 Train *t;
00770 FOR_ALL_TRAINS(t) {
00771 if (t->IsFrontEngine()) t->CargoChanged();
00772 }
00773
00774 return true;
00775 }
00776
00777 static bool DragSignalsDensityChanged(int32)
00778 {
00779 InvalidateWindowData(WC_BUILD_SIGNAL, 0);
00780
00781 return true;
00782 }
00783
00784 static bool TownFoundingChanged(int32 p1)
00785 {
00786 if (_game_mode != GM_EDITOR && _settings_game.economy.found_town == TF_FORBIDDEN) {
00787 DeleteWindowById(WC_FOUND_TOWN, 0);
00788 return true;
00789 }
00790 InvalidateWindowData(WC_FOUND_TOWN, 0);
00791 return true;
00792 }
00793
00794 static bool InvalidateVehTimetableWindow(int32 p1)
00795 {
00796 InvalidateWindowClassesData(WC_VEHICLE_TIMETABLE, -2);
00797 return true;
00798 }
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821 static const DifficultySettings _default_game_diff[3] = {
00822
00823 {2, 2, 4, 300000, 2, 0, 2, 1, 2, 0, 1, 0, 0, 0, 0, 0, 0},
00824 {4, 2, 3, 150000, 3, 1, 3, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1},
00825 {7, 3, 3, 100000, 4, 1, 3, 2, 0, 2, 3, 2, 1, 1, 1, 2, 2},
00826 };
00827
00828 void SetDifficultyLevel(int mode, DifficultySettings *gm_opt)
00829 {
00830 assert(mode <= 3);
00831
00832 if (mode != 3) {
00833 *gm_opt = _default_game_diff[mode];
00834 } else {
00835 gm_opt->diff_level = 3;
00836 }
00837 }
00838
00843 static void CheckDifficultyLevels()
00844 {
00845 if (_settings_newgame.difficulty.diff_level != 3) {
00846 SetDifficultyLevel(_settings_newgame.difficulty.diff_level, &_settings_newgame.difficulty);
00847 }
00848 }
00849
00850 static bool DifficultyReset(int32 level)
00851 {
00852 SetDifficultyLevel(level, (_game_mode == GM_MENU) ? &_settings_newgame.difficulty : &_settings_game.difficulty);
00853 return true;
00854 }
00855
00856 static bool DifficultyChange(int32)
00857 {
00858 if (_game_mode == GM_MENU) {
00859 if (_settings_newgame.difficulty.diff_level != 3) {
00860 ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, 0, 0);
00861 _settings_newgame.difficulty.diff_level = 3;
00862 }
00863 SetWindowClassesDirty(WC_SELECT_GAME);
00864 } else {
00865 _settings_game.difficulty.diff_level = 3;
00866 }
00867
00868 if (((_game_mode == GM_MENU) ? _settings_newgame.difficulty : _settings_game.difficulty).max_no_competitors != 0 &&
00869 #ifdef ENABLE_AI
00870 AI::GetInfoList()->size() == 0 &&
00871 #endif
00872 (!_networking || _network_server)) {
00873 ShowErrorMessage(STR_WARNING_NO_SUITABLE_AI, INVALID_STRING_ID, 0, 0, true);
00874 }
00875
00876
00877
00878
00879 if (_networking && FindWindowById(WC_GAME_OPTIONS, 0) != NULL) {
00880 ShowGameDifficulty();
00881 }
00882
00883 return true;
00884 }
00885
00886 static bool DifficultyNoiseChange(int32 i)
00887 {
00888 if (_game_mode == GM_NORMAL) {
00889 UpdateAirportsNoise();
00890 if (_settings_game.economy.station_noise_level) {
00891 InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
00892 }
00893 }
00894
00895 return DifficultyChange(i);
00896 }
00897
00903 static bool CheckRoadSide(int p1)
00904 {
00905 extern bool RoadVehiclesAreBuilt();
00906 return _game_mode == GM_MENU || !RoadVehiclesAreBuilt();
00907 }
00908
00915 static int32 ConvertLandscape(const char *value)
00916 {
00917
00918 return lookup_oneofmany("normal|hilly|desert|candy", value);
00919 }
00920
00921 static bool CheckFreeformEdges(int32 p1)
00922 {
00923 if (_game_mode == GM_MENU) return true;
00924 if (p1 != 0) {
00925 Ship *s;
00926 FOR_ALL_SHIPS(s) {
00927 if (TileX(s->tile) == 0 || TileY(s->tile) == 0) {
00928 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, 0, 0);
00929 return false;
00930 }
00931 }
00932 Station *st;
00933 FOR_ALL_STATIONS(st) {
00934 if (TileX(st->xy) == 0 || TileY(st->xy) == 0) {
00935 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, 0, 0);
00936 return false;
00937 }
00938 }
00939 for (uint i = 0; i < MapSizeX(); i++) MakeVoid(TileXY(i, 0));
00940 for (uint i = 0; i < MapSizeY(); i++) MakeVoid(TileXY(0, i));
00941 } else {
00942 for (uint i = 0; i < MapMaxX(); i++) {
00943 if (TileHeight(TileXY(i, 1)) != 0) {
00944 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, 0, 0);
00945 return false;
00946 }
00947 }
00948 for (uint i = 1; i < MapMaxX(); i++) {
00949 if (!IsTileType(TileXY(i, MapMaxY() - 1), MP_WATER) || TileHeight(TileXY(1, MapMaxY())) != 0) {
00950 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, 0, 0);
00951 return false;
00952 }
00953 }
00954 for (uint i = 0; i < MapMaxY(); i++) {
00955 if (TileHeight(TileXY(1, i)) != 0) {
00956 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, 0, 0);
00957 return false;
00958 }
00959 }
00960 for (uint i = 1; i < MapMaxY(); i++) {
00961 if (!IsTileType(TileXY(MapMaxX() - 1, i), MP_WATER) || TileHeight(TileXY(MapMaxX(), i)) != 0) {
00962 ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, 0, 0);
00963 return false;
00964 }
00965 }
00966
00967 for (uint i = 0; i < MapMaxX(); i++) {
00968 SetTileHeight(TileXY(i, 0), 0);
00969 SetTileType(TileXY(i, 0), MP_WATER);
00970 }
00971 for (uint i = 0; i < MapMaxY(); i++) {
00972 SetTileHeight(TileXY(0, i), 0);
00973 SetTileType(TileXY(0, i), MP_WATER);
00974 }
00975 }
00976 MarkWholeScreenDirty();
00977 return true;
00978 }
00979
00984 static bool ChangeDynamicEngines(int32 p1)
00985 {
00986 if (_game_mode == GM_MENU) return true;
00987
00988 const Vehicle *v;
00989 FOR_ALL_VEHICLES(v) {
00990 if (IsCompanyBuildableVehicleType(v)) {
00991 ShowErrorMessage(STR_CONFIG_SETTING_DYNAMIC_ENGINES_EXISTING_VEHICLES, INVALID_STRING_ID, 0, 0);
00992 return false;
00993 }
00994 }
00995
00996
00997 _engine_mngr.ResetToDefaultMapping();
00998 ReloadNewGRFData();
00999
01000 return true;
01001 }
01002
01003 static bool StationCatchmentChanged(int32 p1)
01004 {
01005 Station::RecomputeIndustriesNearForAll();
01006 return true;
01007 }
01008
01009 #ifdef ENABLE_NETWORK
01010
01011 static bool UpdateClientName(int32 p1)
01012 {
01013 NetworkUpdateClientName();
01014 return true;
01015 }
01016
01017 static bool UpdateServerPassword(int32 p1)
01018 {
01019 if (strcmp(_settings_client.network.server_password, "*") == 0) {
01020 _settings_client.network.server_password[0] = '\0';
01021 }
01022
01023 return true;
01024 }
01025
01026 static bool UpdateRconPassword(int32 p1)
01027 {
01028 if (strcmp(_settings_client.network.rcon_password, "*") == 0) {
01029 _settings_client.network.rcon_password[0] = '\0';
01030 }
01031
01032 return true;
01033 }
01034
01035 static bool UpdateClientConfigValues(int32 p1)
01036 {
01037 if (_network_server) NetworkServerSendConfigUpdate();
01038
01039 return true;
01040 }
01041
01042 #endif
01043
01044
01045
01046
01050 static void PrepareOldDiffCustom()
01051 {
01052 memset(_old_diff_custom, 0, sizeof(_old_diff_custom));
01053 }
01054
01061 static void HandleOldDiffCustom(bool savegame)
01062 {
01063 uint options_to_load = GAME_DIFFICULTY_NUM - ((savegame && CheckSavegameVersion(4)) ? 1 : 0);
01064
01065 if (!savegame) {
01066
01067 bool old_diff_custom_used = false;
01068 for (uint i = 0; i < options_to_load && !old_diff_custom_used; i++) {
01069 old_diff_custom_used = (_old_diff_custom[i] != 0);
01070 }
01071
01072 if (!old_diff_custom_used) return;
01073 }
01074
01075 for (uint i = 0; i < options_to_load; i++) {
01076 const SettingDesc *sd = &_settings[i];
01077
01078 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01079 void *var = GetVariableAddress(savegame ? &_settings_game : &_settings_newgame, &sd->save);
01080 Write_ValidateSetting(var, sd, (int32)((i == 4 ? 1000 : 1) * _old_diff_custom[i]));
01081 }
01082 }
01083
01088 static bool ConvertOldNewsSetting(const char *name, const char *value)
01089 {
01090 if (strcasecmp(name, "openclose") == 0) {
01091
01092
01093
01094
01095 NewsDisplay display = ND_OFF;
01096 if (strcasecmp(value, "full") == 0) {
01097 display = ND_FULL;
01098 } else if (strcasecmp(value, "summarized") == 0) {
01099 display = ND_SUMMARY;
01100 }
01101
01102 _news_type_data[NT_INDUSTRY_OPEN].display = display;
01103 _news_type_data[NT_INDUSTRY_CLOSE].display = display;
01104 return true;
01105 }
01106 return false;
01107 }
01108
01109 static void NewsDisplayLoadConfig(IniFile *ini, const char *grpname)
01110 {
01111 IniGroup *group = ini->GetGroup(grpname);
01112 IniItem *item;
01113
01114
01115 if (group == NULL) return;
01116
01117 for (item = group->item; item != NULL; item = item->next) {
01118 int news_item = -1;
01119 for (int i = 0; i < NT_END; i++) {
01120 if (strcasecmp(item->name, _news_type_data[i].name) == 0) {
01121 news_item = i;
01122 break;
01123 }
01124 }
01125
01126
01127 if (news_item == -1) {
01128
01129 if (!ConvertOldNewsSetting(item->name, item->value)) {
01130 DEBUG(misc, 0, "Invalid display option: %s", item->name);
01131 }
01132
01133 continue;
01134 }
01135
01136 if (StrEmpty(item->value)) {
01137 DEBUG(misc, 0, "Empty display value for newstype %s", item->name);
01138 continue;
01139 } else if (strcasecmp(item->value, "full") == 0) {
01140 _news_type_data[news_item].display = ND_FULL;
01141 } else if (strcasecmp(item->value, "off") == 0) {
01142 _news_type_data[news_item].display = ND_OFF;
01143 } else if (strcasecmp(item->value, "summarized") == 0) {
01144 _news_type_data[news_item].display = ND_SUMMARY;
01145 } else {
01146 DEBUG(misc, 0, "Invalid display value for newstype %s: %s", item->name, item->value);
01147 continue;
01148 }
01149 }
01150 }
01151
01152 static void AILoadConfig(IniFile *ini, const char *grpname)
01153 {
01154 #ifdef ENABLE_AI
01155 IniGroup *group = ini->GetGroup(grpname);
01156 IniItem *item;
01157
01158
01159 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01160 AIConfig::GetConfig(c, true)->ChangeAI(NULL);
01161 }
01162
01163
01164 if (group == NULL) return;
01165
01166 CompanyID c = COMPANY_FIRST;
01167 for (item = group->item; c < MAX_COMPANIES && item != NULL; c++, item = item->next) {
01168 AIConfig *config = AIConfig::GetConfig(c, true);
01169
01170 config->ChangeAI(item->name);
01171 if (!config->HasAI()) {
01172 if (strcmp(item->name, "none") != 0) {
01173 DEBUG(ai, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name);
01174 continue;
01175 }
01176 }
01177 if (item->value != NULL) config->StringToSettings(item->value);
01178 }
01179 #endif
01180 }
01181
01182
01183 static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_static)
01184 {
01185 IniGroup *group = ini->GetGroup(grpname);
01186 IniItem *item;
01187 GRFConfig *first = NULL;
01188 GRFConfig **curr = &first;
01189
01190 if (group == NULL) return NULL;
01191
01192 for (item = group->item; item != NULL; item = item->next) {
01193 GRFConfig *c = CallocT<GRFConfig>(1);
01194 c->filename = strdup(item->name);
01195
01196
01197 if (!StrEmpty(item->value)) {
01198 c->num_params = parse_intlist(item->value, (int*)c->param, lengthof(c->param));
01199 if (c->num_params == (byte)-1) {
01200 ShowInfoF("ini: error in array '%s'", item->name);
01201 c->num_params = 0;
01202 }
01203 }
01204
01205
01206 if (!FillGRFDetails(c, is_static)) {
01207 const char *msg;
01208
01209 if (c->status == GCS_NOT_FOUND) {
01210 msg = "not found";
01211 } else if (HasBit(c->flags, GCF_UNSAFE)) {
01212 msg = "unsafe for static use";
01213 } else if (HasBit(c->flags, GCF_SYSTEM)) {
01214 msg = "system NewGRF";
01215 } else {
01216 msg = "unknown";
01217 }
01218
01219 ShowInfoF("ini: ignoring invalid NewGRF '%s': %s", item->name, msg);
01220 ClearGRFConfig(&c);
01221 continue;
01222 }
01223
01224
01225 bool duplicate = false;
01226 for (const GRFConfig *gc = first; gc != NULL; gc = gc->next) {
01227 if (gc->grfid == c->grfid) {
01228 ShowInfoF("ini: ignoring NewGRF '%s': duplicate GRF ID with '%s'", item->name, gc->filename);
01229 duplicate = true;
01230 break;
01231 }
01232 }
01233 if (duplicate) {
01234 ClearGRFConfig(&c);
01235 continue;
01236 }
01237
01238
01239 if (is_static) SetBit(c->flags, GCF_STATIC);
01240
01241
01242 *curr = c;
01243 curr = &c->next;
01244 }
01245
01246 return first;
01247 }
01248
01249 static void NewsDisplaySaveConfig(IniFile *ini, const char *grpname)
01250 {
01251 IniGroup *group = ini->GetGroup(grpname);
01252
01253 for (int i = 0; i < NT_END; i++) {
01254 const char *value;
01255 int v = _news_type_data[i].display;
01256
01257 value = (v == ND_OFF ? "off" : (v == ND_SUMMARY ? "summarized" : "full"));
01258
01259 group->GetItem(_news_type_data[i].name, true)->SetValue(value);
01260 }
01261 }
01262
01263 static void AISaveConfig(IniFile *ini, const char *grpname)
01264 {
01265 #ifdef ENABLE_AI
01266 IniGroup *group = ini->GetGroup(grpname);
01267
01268 if (group == NULL) return;
01269 group->Clear();
01270
01271 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01272 AIConfig *config = AIConfig::GetConfig(c, true);
01273 const char *name;
01274 char value[1024];
01275 config->SettingsToString(value, lengthof(value));
01276
01277 if (config->HasAI()) {
01278 name = config->GetName();
01279 } else {
01280 name = "none";
01281 }
01282
01283 IniItem *item = new IniItem(group, name, strlen(name));
01284 item->SetValue(value);
01285 }
01286 #endif
01287 }
01288
01293 static void SaveVersionInConfig(IniFile *ini)
01294 {
01295 IniGroup *group = ini->GetGroup("version");
01296
01297 char version[9];
01298 snprintf(version, lengthof(version), "%08X", _openttd_newgrf_version);
01299
01300 const char * const versions[][2] = {
01301 { "version_string", _openttd_revision },
01302 { "version_number", version }
01303 };
01304
01305 for (uint i = 0; i < lengthof(versions); i++) {
01306 group->GetItem(versions[i][0], true)->SetValue(versions[i][1]);
01307 }
01308 }
01309
01310
01311 static void GRFSaveConfig(IniFile *ini, const char *grpname, const GRFConfig *list)
01312 {
01313 ini->RemoveGroup(grpname);
01314 IniGroup *group = ini->GetGroup(grpname);
01315 const GRFConfig *c;
01316
01317 for (c = list; c != NULL; c = c->next) {
01318 char params[512];
01319 GRFBuildParamList(params, c, lastof(params));
01320
01321 group->GetItem(c->filename, true)->SetValue(params);
01322 }
01323 }
01324
01325
01326 static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list)
01327 {
01328 proc(ini, (const SettingDesc*)_misc_settings, "misc", NULL);
01329 proc(ini, (const SettingDesc*)_music_settings, "music", &msf);
01330 #if defined(WIN32) && !defined(DEDICATED)
01331 proc(ini, (const SettingDesc*)_win32_settings, "win32", NULL);
01332 #endif
01333
01334 proc(ini, _settings, "patches", &_settings_newgame);
01335 proc(ini, _currency_settings,"currency", &_custom_currency);
01336 proc(ini, _company_settings, "company", &_settings_client.company);
01337
01338 #ifdef ENABLE_NETWORK
01339 proc_list(ini, "server_bind_addresses", &_network_bind_list);
01340 proc_list(ini, "servers", &_network_host_list);
01341 proc_list(ini, "bans", &_network_ban_list);
01342 #endif
01343 }
01344
01345 static IniFile *IniLoadConfig()
01346 {
01347 IniFile *ini = new IniFile(_list_group_names);
01348 ini->LoadFromDisk(_config_file);
01349 return ini;
01350 }
01351
01353 void LoadFromConfig()
01354 {
01355 IniFile *ini = IniLoadConfig();
01356 ResetCurrencies(false);
01357
01358 HandleSettingDescs(ini, ini_load_settings, ini_load_setting_list);
01359 _grfconfig_newgame = GRFLoadConfig(ini, "newgrf", false);
01360 _grfconfig_static = GRFLoadConfig(ini, "newgrf-static", true);
01361 NewsDisplayLoadConfig(ini, "news_display");
01362 AILoadConfig(ini, "ai_players");
01363
01364 PrepareOldDiffCustom();
01365 ini_load_settings(ini, _gameopt_settings, "gameopt", &_settings_newgame);
01366 HandleOldDiffCustom(false);
01367
01368 CheckDifficultyLevels();
01369 delete ini;
01370 }
01371
01373 void SaveToConfig()
01374 {
01375 IniFile *ini = IniLoadConfig();
01376
01377
01378 ini->RemoveGroup("patches");
01379 ini->RemoveGroup("yapf");
01380 ini->RemoveGroup("gameopt");
01381
01382 HandleSettingDescs(ini, ini_save_settings, ini_save_setting_list);
01383 GRFSaveConfig(ini, "newgrf", _grfconfig_newgame);
01384 GRFSaveConfig(ini, "newgrf-static", _grfconfig_static);
01385 NewsDisplaySaveConfig(ini, "news_display");
01386 AISaveConfig(ini, "ai_players");
01387 SaveVersionInConfig(ini);
01388 ini->SaveToDisk(_config_file);
01389 delete ini;
01390 }
01391
01392 void GetGRFPresetList(GRFPresetList *list)
01393 {
01394 list->Clear();
01395
01396 IniFile *ini = IniLoadConfig();
01397 IniGroup *group;
01398 for (group = ini->group; group != NULL; group = group->next) {
01399 if (strncmp(group->name, "preset-", 7) == 0) {
01400 *list->Append() = strdup(group->name + 7);
01401 }
01402 }
01403
01404 delete ini;
01405 }
01406
01407 GRFConfig *LoadGRFPresetFromConfig(const char *config_name)
01408 {
01409 char *section = (char*)alloca(strlen(config_name) + 8);
01410 sprintf(section, "preset-%s", config_name);
01411
01412 IniFile *ini = IniLoadConfig();
01413 GRFConfig *config = GRFLoadConfig(ini, section, false);
01414 delete ini;
01415
01416 return config;
01417 }
01418
01419 void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
01420 {
01421 char *section = (char*)alloca(strlen(config_name) + 8);
01422 sprintf(section, "preset-%s", config_name);
01423
01424 IniFile *ini = IniLoadConfig();
01425 GRFSaveConfig(ini, section, config);
01426 ini->SaveToDisk(_config_file);
01427 delete ini;
01428 }
01429
01430 void DeleteGRFPresetFromConfig(const char *config_name)
01431 {
01432 char *section = (char*)alloca(strlen(config_name) + 8);
01433 sprintf(section, "preset-%s", config_name);
01434
01435 IniFile *ini = IniLoadConfig();
01436 ini->RemoveGroup(section);
01437 ini->SaveToDisk(_config_file);
01438 delete ini;
01439 }
01440
01441 static const SettingDesc *GetSettingDescription(uint index)
01442 {
01443 if (index >= lengthof(_settings)) return NULL;
01444 return &_settings[index];
01445 }
01446
01457 CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01458 {
01459 const SettingDesc *sd = GetSettingDescription(p1);
01460
01461 if (sd == NULL) return CMD_ERROR;
01462 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR;
01463
01464 if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return CMD_ERROR;
01465 if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return CMD_ERROR;
01466 if ((sd->desc.flags & SGF_NEWGAME_ONLY) &&
01467 (_game_mode == GM_NORMAL ||
01468 (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0))) {
01469 return CMD_ERROR;
01470 }
01471
01472 if (flags & DC_EXEC) {
01473 GameSettings *s = (_game_mode == GM_MENU) ? &_settings_newgame : &_settings_game;
01474 void *var = GetVariableAddress(s, &sd->save);
01475
01476 int32 oldval = (int32)ReadValue(var, sd->save.conv);
01477 int32 newval = (int32)p2;
01478
01479 Write_ValidateSetting(var, sd, newval);
01480 newval = (int32)ReadValue(var, sd->save.conv);
01481
01482 if (oldval == newval) return CommandCost();
01483
01484 if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01485 WriteValue(var, sd->save.conv, (int64)oldval);
01486 return CommandCost();
01487 }
01488
01489 if (sd->desc.flags & SGF_NO_NETWORK) {
01490 GamelogStartAction(GLAT_SETTING);
01491 GamelogSetting(sd->desc.name, oldval, newval);
01492 GamelogStopAction();
01493 }
01494
01495 SetWindowDirty(WC_GAME_OPTIONS, 0);
01496 }
01497
01498 return CommandCost();
01499 }
01500
01510 CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01511 {
01512 if (p1 >= lengthof(_company_settings)) return CMD_ERROR;
01513 const SettingDesc *sd = &_company_settings[p1];
01514
01515 if (flags & DC_EXEC) {
01516 void *var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01517
01518 int32 oldval = (int32)ReadValue(var, sd->save.conv);
01519 int32 newval = (int32)p2;
01520
01521 Write_ValidateSetting(var, sd, newval);
01522 newval = (int32)ReadValue(var, sd->save.conv);
01523
01524 if (oldval == newval) return CommandCost();
01525
01526 if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01527 WriteValue(var, sd->save.conv, (int64)oldval);
01528 return CommandCost();
01529 }
01530
01531 SetWindowDirty(WC_GAME_OPTIONS, 0);
01532 }
01533
01534 return CommandCost();
01535 }
01536
01543 bool SetSettingValue(uint index, int32 value, bool force_newgame)
01544 {
01545 const SettingDesc *sd = &_settings[index];
01546
01547
01548
01549
01550 if (sd->save.conv & SLF_NETWORK_NO) {
01551 void *var = GetVariableAddress((_game_mode == GM_MENU) ? &_settings_newgame : &_settings_game, &sd->save);
01552 Write_ValidateSetting(var, sd, value);
01553
01554 if (_game_mode != GM_MENU) {
01555 void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01556 Write_ValidateSetting(var2, sd, value);
01557 }
01558 if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01559 SetWindowDirty(WC_GAME_OPTIONS, 0);
01560 return true;
01561 }
01562
01563 if (force_newgame) {
01564 void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01565 Write_ValidateSetting(var2, sd, value);
01566 return true;
01567 }
01568
01569
01570 if (!_networking || (_networking && _network_server)) {
01571 return DoCommandP(0, index, value, CMD_CHANGE_SETTING);
01572 }
01573 return false;
01574 }
01575
01581 void SetCompanySetting(uint index, int32 value)
01582 {
01583 const SettingDesc *sd = &_company_settings[index];
01584 if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01585 DoCommandP(0, index, value, CMD_CHANGE_COMPANY_SETTING);
01586 } else {
01587 void *var = GetVariableAddress(&_settings_client.company, &sd->save);
01588 Write_ValidateSetting(var, sd, value);
01589 if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01590 }
01591 }
01592
01596 void SetDefaultCompanySettings(CompanyID cid)
01597 {
01598 Company *c = Company::Get(cid);
01599 const SettingDesc *sd;
01600 for (sd = _company_settings; sd->save.cmd != SL_END; sd++) {
01601 void *var = GetVariableAddress(&c->settings, &sd->save);
01602 Write_ValidateSetting(var, sd, (int32)(size_t)sd->desc.def);
01603 }
01604 }
01605
01606 #if defined(ENABLE_NETWORK)
01607
01610 void SyncCompanySettings()
01611 {
01612 const SettingDesc *sd;
01613 uint i = 0;
01614 for (sd = _company_settings; sd->save.cmd != SL_END; sd++, i++) {
01615 const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01616 const void *new_var = GetVariableAddress(&_settings_client.company, &sd->save);
01617 uint32 old_value = (uint32)ReadValue(old_var, sd->save.conv);
01618 uint32 new_value = (uint32)ReadValue(new_var, sd->save.conv);
01619 if (old_value != new_value) NetworkSend_Command(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, NULL, NULL, _local_company);
01620 }
01621 }
01622 #endif
01623
01629 uint GetCompanySettingIndex(const char *name)
01630 {
01631 uint i;
01632 const SettingDesc *sd = GetSettingFromName(name, &i);
01633 assert(sd != NULL && (sd->desc.flags & SGF_PER_COMPANY) != 0);
01634 return i;
01635 }
01636
01643 bool SetSettingValue(uint index, const char *value)
01644 {
01645 const SettingDesc *sd = &_settings[index];
01646 assert(sd->save.conv & SLF_NETWORK_NO);
01647
01648 char *var = (char*)GetVariableAddress(NULL, &sd->save);
01649 ttd_strlcpy(var, value, sd->save.length);
01650 if (sd->desc.proc != NULL) sd->desc.proc(0);
01651
01652 return true;
01653 }
01654
01662 const SettingDesc *GetSettingFromName(const char *name, uint *i)
01663 {
01664 const SettingDesc *sd;
01665
01666
01667 for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01668 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01669 if (strcmp(sd->desc.name, name) == 0) return sd;
01670 }
01671
01672
01673 for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01674 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01675 const char *short_name = strchr(sd->desc.name, '.');
01676 if (short_name != NULL) {
01677 short_name++;
01678 if (strcmp(short_name, name) == 0) return sd;
01679 }
01680 }
01681
01682 if (strncmp(name, "company.", 8) == 0) name += 8;
01683
01684 for (*i = 0, sd = _company_settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01685 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01686 if (strcmp(sd->desc.name, name) == 0) return sd;
01687 }
01688
01689 return NULL;
01690 }
01691
01692
01693
01694 void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
01695 {
01696 uint index;
01697 const SettingDesc *sd = GetSettingFromName(name, &index);
01698
01699 if (sd == NULL) {
01700 IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01701 return;
01702 }
01703
01704 bool success;
01705 if (sd->desc.cmd == SDT_STRING) {
01706 success = SetSettingValue(index, value);
01707 } else {
01708 uint32 val;
01709 extern bool GetArgumentInteger(uint32 *value, const char *arg);
01710 success = GetArgumentInteger(&val, value);
01711 if (!success) {
01712 IConsolePrintF(CC_ERROR, "'%s' is not an integer.", value);
01713 return;
01714 }
01715
01716 success = SetSettingValue(index, val, force_newgame);
01717 }
01718
01719 if (!success) {
01720 if (_network_server) {
01721 IConsoleError("This command/variable is not available during network games.");
01722 } else {
01723 IConsoleError("This command/variable is only available to a network server.");
01724 }
01725 }
01726 }
01727
01728 void IConsoleSetSetting(const char *name, int value)
01729 {
01730 uint index;
01731 const SettingDesc *sd = GetSettingFromName(name, &index);
01732 assert(sd != NULL);
01733 SetSettingValue(index, value);
01734 }
01735
01741 void IConsoleGetSetting(const char *name, bool force_newgame)
01742 {
01743 char value[20];
01744 uint index;
01745 const SettingDesc *sd = GetSettingFromName(name, &index);
01746 const void *ptr;
01747
01748 if (sd == NULL) {
01749 IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01750 return;
01751 }
01752
01753 ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
01754
01755 if (sd->desc.cmd == SDT_STRING) {
01756 IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, (const char *)ptr);
01757 } else {
01758 if (sd->desc.cmd == SDT_BOOLX) {
01759 snprintf(value, sizeof(value), (*(bool*)ptr == 1) ? "on" : "off");
01760 } else {
01761 snprintf(value, sizeof(value), "%d", (int32)ReadValue(ptr, sd->save.conv));
01762 }
01763
01764 IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s' (min: %s%d, max: %d)",
01765 name, value, (sd->desc.flags & SGF_0ISDISABLED) ? "(0) " : "", sd->desc.min, sd->desc.max);
01766 }
01767 }
01768
01774 void IConsoleListSettings(const char *prefilter)
01775 {
01776 IConsolePrintF(CC_WARNING, "All settings with their current value:");
01777
01778 for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) {
01779 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01780 if (prefilter != NULL && strstr(sd->desc.name, prefilter) == NULL) continue;
01781 char value[80];
01782 const void *ptr = GetVariableAddress((_game_mode == GM_MENU) ? &_settings_newgame : &_settings_game, &sd->save);
01783
01784 if (sd->desc.cmd == SDT_BOOLX) {
01785 snprintf(value, lengthof(value), (*(bool*)ptr == 1) ? "on" : "off");
01786 } else if (sd->desc.cmd == SDT_STRING) {
01787 snprintf(value, sizeof(value), "%s", (const char *)ptr);
01788 } else {
01789 snprintf(value, lengthof(value), "%d", (uint32)ReadValue(ptr, sd->save.conv));
01790 }
01791 IConsolePrintF(CC_DEFAULT, "%s = %s", sd->desc.name, value);
01792 }
01793
01794 IConsolePrintF(CC_WARNING, "Use 'setting' command to change a value");
01795 }
01796
01801 static void LoadSettings(const SettingDesc *osd, void *object)
01802 {
01803 for (; osd->save.cmd != SL_END; osd++) {
01804 const SaveLoad *sld = &osd->save;
01805 void *ptr = GetVariableAddress(object, sld);
01806
01807 if (!SlObjectMember(ptr, sld)) continue;
01808 if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, osd, ReadValue(ptr, sld->conv));
01809 }
01810 }
01811
01816 static inline void LoadSettingsGlobList(const SettingDescGlobVarList *sdg)
01817 {
01818 LoadSettings((const SettingDesc*)sdg, NULL);
01819 }
01820
01825 static void SaveSettings(const SettingDesc *sd, void *object)
01826 {
01827
01828
01829 const SettingDesc *i;
01830 size_t length = 0;
01831 for (i = sd; i->save.cmd != SL_END; i++) {
01832 length += SlCalcObjMemberLength(object, &i->save);
01833 }
01834 SlSetLength(length);
01835
01836 for (i = sd; i->save.cmd != SL_END; i++) {
01837 void *ptr = GetVariableAddress(object, &i->save);
01838 SlObjectMember(ptr, &i->save);
01839 }
01840 }
01841
01845 static inline void SaveSettingsGlobList(const SettingDescGlobVarList *sdg)
01846 {
01847 SaveSettings((const SettingDesc*)sdg, NULL);
01848 }
01849
01850 static void Load_OPTS()
01851 {
01852
01853
01854
01855 PrepareOldDiffCustom();
01856 LoadSettings(_gameopt_settings, &_settings_game);
01857 HandleOldDiffCustom(true);
01858 }
01859
01860 static void Load_PATS()
01861 {
01862
01863
01864
01865 LoadSettings(_settings, &_settings_game);
01866 }
01867
01868 static void Save_PATS()
01869 {
01870 SaveSettings(_settings, &_settings_game);
01871 }
01872
01873 void CheckConfig()
01874 {
01875
01876
01877
01878
01879 if (_settings_newgame.pf.opf.pf_maxdepth == 16 && _settings_newgame.pf.opf.pf_maxlength == 512) {
01880 _settings_newgame.pf.opf.pf_maxdepth = 48;
01881 _settings_newgame.pf.opf.pf_maxlength = 4096;
01882 }
01883 }
01884
01885 extern const ChunkHandler _setting_chunk_handlers[] = {
01886 { 'OPTS', NULL, Load_OPTS, NULL, CH_RIFF},
01887 { 'PATS', Save_PATS, Load_PATS, NULL, CH_RIFF | CH_LAST},
01888 };
01889
01890 static bool IsSignedVarMemType(VarType vt)
01891 {
01892 switch (GetVarMemType(vt)) {
01893 case SLE_VAR_I8:
01894 case SLE_VAR_I16:
01895 case SLE_VAR_I32:
01896 case SLE_VAR_I64:
01897 return true;
01898 }
01899 return false;
01900 }