Merge branch 'order_occupancy-sx' into jgrpp

Conflicts:
	src/saveload/extended_ver_sl.cpp
	src/settings_gui.cpp
	src/table/company_settings.ini
This commit is contained in:
Jonathan G Rennison 2015-10-25 21:47:07 +00:00
commit 60a677692e
9 changed files with 257 additions and 46 deletions

View File

@ -3182,6 +3182,14 @@ bool AfterLoadGame()
FOR_ALL_STATIONS(st) UpdateStationAcceptance(st, false); FOR_ALL_STATIONS(st) UpdateStationAcceptance(st, false);
} }
// setting moved from game settings to company settings
if (SlXvIsFeaturePresent(XSLFI_ORDER_OCCUPANCY, 1, 1)) {
Company *c;
FOR_ALL_COMPANIES(c) {
c->settings.order_occupancy_smoothness = _settings_game.order.old_occupancy_smoothness;
}
}
/* Road stops is 'only' updating some caches */ /* Road stops is 'only' updating some caches */
AfterLoadRoadStops(); AfterLoadRoadStops();
AfterLoadLabelMaps(); AfterLoadLabelMaps();

View File

@ -16,6 +16,7 @@
#include "../tunnelbridge_map.h" #include "../tunnelbridge_map.h"
#include "../tunnelbridge.h" #include "../tunnelbridge.h"
#include "../station_base.h" #include "../station_base.h"
#include "../settings_func.h"
#include "saveload.h" #include "saveload.h"
@ -489,6 +490,7 @@ static void Load_PLYR()
int index; int index;
while ((index = SlIterateArray()) != -1) { while ((index = SlIterateArray()) != -1) {
Company *c = new (index) Company(); Company *c = new (index) Company();
SetDefaultCompanySettings(c->index);
SaveLoad_PLYR(c); SaveLoad_PLYR(c);
_company_colours[index] = (Colours)c->colour; _company_colours[index] = (Colours)c->colour;
} }
@ -532,7 +534,25 @@ static void Ptrs_PLYR()
} }
} }
extern void LoadSettingsPlyx(bool skip);
extern void SaveSettingsPlyx();
static void Load_PLYX()
{
LoadSettingsPlyx(false);
}
static void Check_PLYX()
{
LoadSettingsPlyx(true);
}
static void Save_PLYX()
{
SaveSettingsPlyx();
}
extern const ChunkHandler _company_chunk_handlers[] = { extern const ChunkHandler _company_chunk_handlers[] = {
{ 'PLYR', Save_PLYR, Load_PLYR, Ptrs_PLYR, Check_PLYR, CH_ARRAY | CH_LAST}, { 'PLYR', Save_PLYR, Load_PLYR, Ptrs_PLYR, Check_PLYR, CH_ARRAY },
{ 'PLYX', Save_PLYX, Load_PLYX, NULL, Check_PLYX, CH_RIFF | CH_LAST},
}; };

View File

@ -61,7 +61,7 @@ const SlxiSubChunkInfo _sl_xv_sub_chunk_infos[] = {
{ XSLFI_ENH_VIEWPORT_PLANS, XSCF_IGNORABLE_ALL, 1, 1, "enh_viewport_plans", NULL, NULL, "PLAN,PLLN" }, { XSLFI_ENH_VIEWPORT_PLANS, XSCF_IGNORABLE_ALL, 1, 1, "enh_viewport_plans", NULL, NULL, "PLAN,PLLN" },
{ XSLFI_INFRA_SHARING, XSCF_NULL, 1, 1, "infra_sharing", NULL, NULL, NULL }, { XSLFI_INFRA_SHARING, XSCF_NULL, 1, 1, "infra_sharing", NULL, NULL, NULL },
{ XSLFI_VARIABLE_DAY_LENGTH, XSCF_NULL, 1, 1, "variable_day_length", NULL, NULL, NULL }, { XSLFI_VARIABLE_DAY_LENGTH, XSCF_NULL, 1, 1, "variable_day_length", NULL, NULL, NULL },
{ XSLFI_ORDER_OCCUPANCY, XSCF_NULL, 1, 1, "order_occupancy", NULL, NULL, NULL }, { XSLFI_ORDER_OCCUPANCY, XSCF_NULL, 2, 2, "order_occupancy", NULL, NULL, NULL },
{ XSLFI_MORE_COND_ORDERS, XSCF_NULL, 1, 1, "more_cond_orders", NULL, NULL, NULL }, { XSLFI_MORE_COND_ORDERS, XSCF_NULL, 1, 1, "more_cond_orders", NULL, NULL, NULL },
{ XSLFI_EXTRA_LARGE_MAP, XSCF_NULL, 0, 1, "extra_large_map", NULL, NULL, NULL }, { XSLFI_EXTRA_LARGE_MAP, XSCF_NULL, 0, 1, "extra_large_map", NULL, NULL, NULL },
{ XSLFI_NULL, XSCF_NULL, 0, 0, NULL, NULL, NULL, NULL },// This is the end marker { XSLFI_NULL, XSCF_NULL, 0, 0, NULL, NULL, NULL, NULL },// This is the end marker

View File

@ -2299,10 +2299,10 @@ static void SaveSettings(const SettingDesc *sd, void *object)
* *
* The PATX chunk contents has the following format: * The PATX chunk contents has the following format:
* *
* uint32 chunk flags * uint32 chunk flags (unused)
* uint32 number of settings * uint32 number of settings
* For each of N settings: * For each of N settings:
* uint32 setting flags * uint32 setting flags (unused)
* SLE_STR setting name * SLE_STR setting name
* uint32 length of setting field * uint32 length of setting field
* N bytes setting field * N bytes setting field
@ -2333,6 +2333,7 @@ static void MakeSettingsPatxList(const SettingDesc *sd)
static const SettingDesc *previous = NULL; static const SettingDesc *previous = NULL;
if (sd == previous) return; if (sd == previous) return;
previous = sd;
_sorted_patx_settings.clear(); _sorted_patx_settings.clear();
for (const SettingDesc *desc = sd; desc->save.cmd != SL_END; desc++) { for (const SettingDesc *desc = sd; desc->save.cmd != SL_END; desc++) {
@ -2343,16 +2344,6 @@ static void MakeSettingsPatxList(const SettingDesc *sd)
std::sort(_sorted_patx_settings.begin(), _sorted_patx_settings.end(), StringSorter()); std::sort(_sorted_patx_settings.begin(), _sorted_patx_settings.end(), StringSorter());
} }
/**
* Internal structure used in LoadSettingsPatx()
* placed outside for legacy compiler compatibility
*/
struct SettingsPatxLoad {
uint32 flags;
char name[256];
uint32 setting_length;
};
/** /**
* Internal structure used in LoadSettingsPatx() * Internal structure used in LoadSettingsPatx()
* placed outside for legacy compiler compatibility * placed outside for legacy compiler compatibility
@ -2372,6 +2363,38 @@ struct StringSearcher {
} }
}; };
/**
* Internal structure used in LoadSettingsPatx() and LoadSettingsPlyx()
*/
struct SettingsExtLoad {
uint32 flags;
char name[256];
uint32 setting_length;
};
static const SaveLoad _settings_ext_load_desc[] = {
SLE_VAR(SettingsExtLoad, flags, SLE_UINT32),
SLE_STR(SettingsExtLoad, name, SLE_STRB, 256),
SLE_VAR(SettingsExtLoad, setting_length, SLE_UINT32),
SLE_END()
};
/**
* Internal structure used in SaveSettingsPatx() and SaveSettingsPlyx()
*/
struct SettingsExtSave {
uint32 flags;
const char *name;
uint32 setting_length;
};
static const SaveLoad _settings_ext_save_desc[] = {
SLE_VAR(SettingsExtSave, flags, SLE_UINT32),
SLE_STR(SettingsExtSave, name, SLE_STR, 0),
SLE_VAR(SettingsExtSave, setting_length, SLE_UINT32),
SLE_END()
};
/** /**
* Load handler for settings which go in the PATX chunk * Load handler for settings which go in the PATX chunk
* @param osd SettingDesc struct containing all information * @param osd SettingDesc struct containing all information
@ -2382,14 +2405,7 @@ static void LoadSettingsPatx(const SettingDesc *sd, void *object)
{ {
MakeSettingsPatxList(sd); MakeSettingsPatxList(sd);
SettingsPatxLoad current_setting; SettingsExtLoad current_setting;
static const SaveLoad _settings_patx_desc[] = {
SLE_VAR(SettingsPatxLoad, flags, SLE_UINT32),
SLE_STR(SettingsPatxLoad, name, SLE_STRB, 256),
SLE_VAR(SettingsPatxLoad, setting_length, SLE_UINT32),
SLE_END()
};
uint32 flags = SlReadUint32(); uint32 flags = SlReadUint32();
// flags are not in use yet, reserve for future expansion // flags are not in use yet, reserve for future expansion
@ -2397,7 +2413,7 @@ static void LoadSettingsPatx(const SettingDesc *sd, void *object)
uint32 settings_count = SlReadUint32(); uint32 settings_count = SlReadUint32();
for (uint32 i = 0; i < settings_count; i++) { for (uint32 i = 0; i < settings_count; i++) {
SlObject(&current_setting, _settings_patx_desc); SlObject(&current_setting, _settings_ext_load_desc);
// flags are not in use yet, reserve for future expansion // flags are not in use yet, reserve for future expansion
if (current_setting.flags != 0) SlErrorCorruptFmt("PATX chunk: unknown setting header flags: 0x%X", current_setting.flags); if (current_setting.flags != 0) SlErrorCorruptFmt("PATX chunk: unknown setting header flags: 0x%X", current_setting.flags);
@ -2441,19 +2457,7 @@ struct SettingToAdd {
*/ */
static void SaveSettingsPatx(const SettingDesc *sd, void *object) static void SaveSettingsPatx(const SettingDesc *sd, void *object)
{ {
struct SettingsPatxSave { SettingsExtSave current_setting;
uint32 flags;
const char *name;
uint32 setting_length;
};
SettingsPatxSave current_setting;
static const SaveLoad _settings_patx_desc[] = {
SLE_VAR(SettingsPatxSave, flags, SLE_UINT32),
SLE_STR(SettingsPatxSave, name, SLE_STR, 0),
SLE_VAR(SettingsPatxSave, setting_length, SLE_UINT32),
SLE_END()
};
std::vector<SettingToAdd> settings_to_add; std::vector<SettingToAdd> settings_to_add;
@ -2466,7 +2470,7 @@ static void SaveSettingsPatx(const SettingDesc *sd, void *object)
current_setting.name = desc->patx_name; current_setting.name = desc->patx_name;
// add length of setting header // add length of setting header
length += SlCalcObjLength(&current_setting, _settings_patx_desc); length += SlCalcObjLength(&current_setting, _settings_ext_save_desc);
// add length of actual setting // add length of actual setting
length += setting_length; length += setting_length;
@ -2485,12 +2489,171 @@ static void SaveSettingsPatx(const SettingDesc *sd, void *object)
current_setting.flags = 0; current_setting.flags = 0;
current_setting.name = desc->patx_name; current_setting.name = desc->patx_name;
current_setting.setting_length = settings_to_add[i].setting_length; current_setting.setting_length = settings_to_add[i].setting_length;
SlObject(&current_setting, _settings_patx_desc); SlObject(&current_setting, _settings_ext_save_desc);
void *ptr = GetVariableAddress(object, &desc->save); void *ptr = GetVariableAddress(object, &desc->save);
SlObjectMember(ptr, &desc->save); SlObjectMember(ptr, &desc->save);
} }
} }
/** @file
*
* The PLYX chunk stores additional company settings in an unordered
* format which is tolerant of extra, missing or reordered settings.
* The format is similar to the PATX chunk.
* Additional settings generally means those that aren't in trunk.
*
* The PLYX chunk contents has the following format:
*
* uint32 chunk flags (unused)
* uint32 number of companies
* For each of N companies:
* uint32 company ID
* uint32 company flags (unused)
* uint32 number of settings
* For each of N settings:
* uint32 setting flags (unused)
* SLE_STR setting name
* uint32 length of setting field
* N bytes setting field
*/
/**
* Load handler for company settings which go in the PLYX chunk
* @param check_mode Whether to skip over settings without reading
*/
void LoadSettingsPlyx(bool skip)
{
SettingsExtLoad current_setting;
uint32 chunk_flags = SlReadUint32();
// flags are not in use yet, reserve for future expansion
if (chunk_flags != 0) SlErrorCorruptFmt("PLYX chunk: unknown chunk header flags: 0x%X", chunk_flags);
uint32 company_count = SlReadUint32();
for (uint32 i = 0; i < company_count; i++) {
uint32 company_id = SlReadUint32();
if (company_id >= MAX_COMPANIES) SlErrorCorruptFmt("PLYX chunk: invalid company ID: %u", company_id);
const Company *c = NULL;
if (!skip) {
c = Company::GetIfValid(company_id);
if (c == NULL) SlErrorCorruptFmt("PLYX chunk: non-existant company ID: %u", company_id);
}
uint32 company_flags = SlReadUint32();
// flags are not in use yet, reserve for future expansion
if (company_flags != 0) SlErrorCorruptFmt("PLYX chunk: unknown company flags: 0x%X", company_flags);
uint32 settings_count = SlReadUint32();
for (uint32 j = 0; j < settings_count; j++) {
SlObject(&current_setting, _settings_ext_load_desc);
// flags are not in use yet, reserve for future expansion
if (current_setting.flags != 0) SlErrorCorruptFmt("PLYX chunk: unknown setting header flags: 0x%X", current_setting.flags);
if (skip) {
SlSkipBytes(current_setting.setting_length);
continue;
}
const SettingDesc *setting = NULL;
// not many company settings, so perform a linear scan
for (const SettingDesc *desc = _company_settings; desc->save.cmd != SL_END; desc++) {
if (desc->patx_name != NULL && strcmp(desc->patx_name, current_setting.name) == 0) {
setting = desc;
break;
}
}
if (setting != NULL) {
// found setting
const SaveLoad *sld = &(setting->save);
size_t read = SlGetBytesRead();
void *ptr = GetVariableAddress(&(c->settings), sld);
SlObjectMember(ptr, sld);
if (SlGetBytesRead() != read + current_setting.setting_length) {
SlErrorCorruptFmt("PLYX chunk: setting read length mismatch for setting: '%s'", current_setting.name);
}
if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, setting, ReadValue(ptr, sld->conv));
} else {
DEBUG(sl, 1, "PLYX chunk: Could not find company setting: '%s', ignoring", current_setting.name);
SlSkipBytes(current_setting.setting_length);
}
}
}
}
/**
* Save handler for settings which go in the PLYX chunk
*/
void SaveSettingsPlyx()
{
SettingsExtSave current_setting;
static const SaveLoad _settings_plyx_desc[] = {
SLE_VAR(SettingsExtSave, flags, SLE_UINT32),
SLE_STR(SettingsExtSave, name, SLE_STR, 0),
SLE_VAR(SettingsExtSave, setting_length, SLE_UINT32),
SLE_END()
};
std::vector<uint32> company_setting_counts;
size_t length = 8;
uint32 companies_count = 0;
Company *c;
FOR_ALL_COMPANIES(c) {
length += 12;
companies_count++;
uint32 setting_count = 0;
for (const SettingDesc *desc = _company_settings; desc->save.cmd != SL_END; desc++) {
if (desc->patx_name == NULL) continue;
uint32 setting_length = SlCalcObjMemberLength(&(c->settings), &desc->save);
if (!setting_length) continue;
current_setting.name = desc->patx_name;
// add length of setting header
length += SlCalcObjLength(&current_setting, _settings_ext_save_desc);
// add length of actual setting
length += setting_length;
setting_count++;
}
company_setting_counts.push_back(setting_count);
}
SlSetLength(length);
SlWriteUint32(0); // flags
SlWriteUint32(companies_count); // companies count
size_t index = 0;
FOR_ALL_COMPANIES(c) {
length += 12;
companies_count++;
SlWriteUint32(c->index); // company ID
SlWriteUint32(0); // flags
SlWriteUint32(company_setting_counts[index]); // setting count
index++;
for (const SettingDesc *desc = _company_settings; desc->save.cmd != SL_END; desc++) {
if (desc->patx_name == NULL) continue;
uint32 setting_length = SlCalcObjMemberLength(&(c->settings), &desc->save);
if (!setting_length) continue;
current_setting.flags = 0;
current_setting.name = desc->patx_name;
current_setting.setting_length = setting_length;
SlObject(&current_setting, _settings_plyx_desc);
void *ptr = GetVariableAddress(&(c->settings), &desc->save);
SlObjectMember(ptr, &desc->save);
}
}
}
static void Load_OPTS() static void Load_OPTS()
{ {
/* Copy over default setting since some might not get loaded in /* Copy over default setting since some might not get loaded in

View File

@ -1635,6 +1635,7 @@ static SettingsContainer &GetSettingsTree()
company->Add(new SettingEntry("vehicle.servint_ships")); company->Add(new SettingEntry("vehicle.servint_ships"));
company->Add(new SettingEntry("vehicle.servint_aircraft")); company->Add(new SettingEntry("vehicle.servint_aircraft"));
company->Add(new SettingEntry("vehicle.auto_timetable_by_default")); company->Add(new SettingEntry("vehicle.auto_timetable_by_default"));
company->Add(new SettingEntry("order_occupancy_smoothness"));
} }
SettingsPage *accounting = main->Add(new SettingsPage(STR_CONFIG_SETTING_ACCOUNTING)); SettingsPage *accounting = main->Add(new SettingsPage(STR_CONFIG_SETTING_ACCOUNTING));
@ -1680,7 +1681,6 @@ static SettingsContainer &GetSettingsTree()
vehicles->Add(new SettingEntry("order.timetable_separation")); vehicles->Add(new SettingEntry("order.timetable_separation"));
vehicles->Add(new SettingEntry("order.timetable_separation_rate")); vehicles->Add(new SettingEntry("order.timetable_separation_rate"));
vehicles->Add(new SettingEntry("vehicle.adjacent_crossings")); vehicles->Add(new SettingEntry("vehicle.adjacent_crossings"));
vehicles->Add(new SettingEntry("order.occupancy_smoothness"));
} }
SettingsPage *limitations = main->Add(new SettingsPage(STR_CONFIG_SETTING_LIMITATIONS)); SettingsPage *limitations = main->Add(new SettingsPage(STR_CONFIG_SETTING_LIMITATIONS));

View File

@ -481,7 +481,8 @@ struct OrderSettings {
bool timetable_separation; ///< whether to perform automatic separation based on timetable bool timetable_separation; ///< whether to perform automatic separation based on timetable
uint8 timetable_separation_rate; ///< percentage of timetable separation change to apply uint8 timetable_separation_rate; ///< percentage of timetable separation change to apply
bool serviceathelipad; ///< service helicopters at helipads automatically (no need to send to depot) bool serviceathelipad; ///< service helicopters at helipads automatically (no need to send to depot)
uint8 occupancy_smoothness; ///< percentage smoothness of occupancy measurement changes
uint8 old_occupancy_smoothness; ///< moved to company settings: percentage smoothness of occupancy measurement changes
}; };
/** Settings related to vehicles. */ /** Settings related to vehicles. */
@ -588,6 +589,7 @@ struct CompanySettings {
uint32 engine_renew_money; ///< minimum amount of money before autorenew is used uint32 engine_renew_money; ///< minimum amount of money before autorenew is used
bool renew_keep_length; ///< sell some wagons if after autoreplace the train is longer than before bool renew_keep_length; ///< sell some wagons if after autoreplace the train is longer than before
VehicleDefaultSettings vehicle; ///< default settings for vehicles VehicleDefaultSettings vehicle; ///< default settings for vehicles
uint8 order_occupancy_smoothness; ///< percentage smoothness of occupancy measurement changes
}; };
/** All settings together for the game. */ /** All settings together for the game. */

View File

@ -18,8 +18,8 @@ static const SettingDesc _company_settings[] = {
[post-amble] [post-amble]
}; };
[templates] [templates]
SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, $patxname),
SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, $patxname),
SDT_NULL = SDT_NULL($length, $from, $to, $extver), SDT_NULL = SDT_NULL($length, $from, $to, $extver),
SDT_END = SDT_END() SDT_END = SDT_END()
@ -36,6 +36,7 @@ from = 0
to = SL_MAX_VERSION to = SL_MAX_VERSION
cat = SC_ADVANCED cat = SC_ADVANCED
extver = SlXvFeatureTest() extver = SlXvFeatureTest()
patxname = NULL
@ -148,6 +149,21 @@ def = false
str = STR_CONFIG_SETTING_AUTO_TIMETABLE_BY_DEFAULT str = STR_CONFIG_SETTING_AUTO_TIMETABLE_BY_DEFAULT
strhelp = STR_CONFIG_SETTING_AUTO_TIMETABLE_BY_DEFAULT_HELPTEXT strhelp = STR_CONFIG_SETTING_AUTO_TIMETABLE_BY_DEFAULT_HELPTEXT
[SDT_VAR]
base = CompanySettings
var = order_occupancy_smoothness
type = SLE_UINT8
guiflags = SGF_PER_COMPANY
def = 75
min = 0
max = 100
interval = 10
str = STR_CONFIG_OCCUPANCY_SMOOTHNESS
strhelp = STR_CONFIG_OCCUPANCY_SMOOTHNESS_HELPTEXT
strval = STR_CONFIG_SETTING_PERCENTAGE
cat = SC_EXPERT
patxname = ""order_occupancy_smoothness""
[SDT_END] [SDT_END]

View File

@ -2537,7 +2537,7 @@ cat = SC_EXPERT
[SDT_VAR] [SDT_VAR]
base = GameSettings base = GameSettings
var = order.occupancy_smoothness var = order.old_occupancy_smoothness
type = SLE_UINT8 type = SLE_UINT8
def = 75 def = 75
min = 0 min = 0
@ -2547,7 +2547,7 @@ str = STR_CONFIG_OCCUPANCY_SMOOTHNESS
strhelp = STR_CONFIG_OCCUPANCY_SMOOTHNESS_HELPTEXT strhelp = STR_CONFIG_OCCUPANCY_SMOOTHNESS_HELPTEXT
strval = STR_CONFIG_SETTING_PERCENTAGE strval = STR_CONFIG_SETTING_PERCENTAGE
cat = SC_EXPERT cat = SC_EXPERT
extver = SlXvFeatureTest(XSLFTO_AND, XSLFI_ORDER_OCCUPANCY) extver = SlXvFeatureTest(XSLFTO_AND, XSLFI_ORDER_OCCUPANCY, 1, 1)
patxname = ""order_occupancy.order.occupancy_smoothness"" patxname = ""order_occupancy.order.occupancy_smoothness""
## ##

View File

@ -2456,9 +2456,11 @@ void Vehicle::LeaveStation()
if (old_occupancy == 0) { if (old_occupancy == 0) {
new_occupancy = current_occupancy; new_occupancy = current_occupancy;
} else { } else {
Company *owner = Company::GetIfValid(this->owner);
uint8 occupancy_smoothness = owner ? owner->settings.order_occupancy_smoothness : 0;
// Exponential weighted moving average using occupancy_smoothness // Exponential weighted moving average using occupancy_smoothness
new_occupancy = (old_occupancy - 1) * _settings_game.order.occupancy_smoothness; new_occupancy = (old_occupancy - 1) * occupancy_smoothness;
new_occupancy += current_occupancy * (100 - _settings_game.order.occupancy_smoothness); new_occupancy += current_occupancy * (100 - occupancy_smoothness);
new_occupancy += 50; // round to nearest integer percent, rather than just floor new_occupancy += 50; // round to nearest integer percent, rather than just floor
new_occupancy /= 100; new_occupancy /= 100;
} }