1
0
Fork 0

Compare commits

...

2 Commits

Author SHA1 Message Date
Patric Stout 80ebcc72fb
Change: rebrand Cheats as Sandbox Options (#11874) 2024-01-26 19:56:01 +01:00
Tyler Trahan 28716548d2
Feature: Setting to automatically restart server based on hours played (#11142) 2024-01-26 10:25:25 -05:00
8 changed files with 95 additions and 51 deletions

View File

@ -217,10 +217,7 @@ static constexpr NWidgetPart _nested_cheat_widgets[] = {
NWidget(WWT_SHADEBOX, COLOUR_GREY),
NWidget(WWT_STICKYBOX, COLOUR_GREY),
EndContainer(),
NWidget(WWT_PANEL, COLOUR_GREY, WID_C_PANEL), SetDataTip(0x0, STR_CHEATS_TOOLTIP), EndContainer(),
NWidget(WWT_PANEL, COLOUR_GREY),
NWidget(WWT_LABEL, COLOUR_GREY, WID_C_NOTE), SetFill(1, 1), SetDataTip(STR_CHEATS_NOTE, STR_NULL), SetPadding(WidgetDimensions::unscaled.frametext),
EndContainer(),
NWidget(WWT_PANEL, COLOUR_GREY, WID_C_PANEL), EndContainer(),
};
/** GUI for the cheats. */
@ -228,7 +225,6 @@ struct CheatWindow : Window {
int clicked;
int clicked_widget;
uint line_height;
Dimension box; ///< Dimension of box sprite
Dimension icon; ///< Dimension of company icon sprite
CheatWindow(WindowDesc *desc) : Window(desc)
@ -238,7 +234,6 @@ struct CheatWindow : Window {
void OnInit() override
{
this->box = maxdim(GetSpriteSize(SPR_BOX_EMPTY), GetSpriteSize(SPR_BOX_CHECKED));
this->icon = GetSpriteSize(SPR_COMPANY_ICON);
}
@ -250,21 +245,17 @@ struct CheatWindow : Window {
int y = ir.top;
bool rtl = _current_text_dir == TD_RTL;
uint box_left = rtl ? ir.right - this->box.width - WidgetDimensions::scaled.hsep_wide : ir.left + WidgetDimensions::scaled.hsep_wide;
uint button_left = rtl ? ir.right - this->box.width - WidgetDimensions::scaled.hsep_wide * 2 - SETTING_BUTTON_WIDTH : ir.left + this->box.width + WidgetDimensions::scaled.hsep_wide * 2;
uint text_left = ir.left + (rtl ? 0 : WidgetDimensions::scaled.hsep_wide * 4 + this->box.width + SETTING_BUTTON_WIDTH);
uint text_right = ir.right - (rtl ? WidgetDimensions::scaled.hsep_wide * 4 + this->box.width + SETTING_BUTTON_WIDTH : 0);
uint button_left = rtl ? ir.right - SETTING_BUTTON_WIDTH : ir.left;
uint text_left = ir.left + (rtl ? 0 : WidgetDimensions::scaled.hsep_wide + SETTING_BUTTON_WIDTH);
uint text_right = ir.right - (rtl ? WidgetDimensions::scaled.hsep_wide + SETTING_BUTTON_WIDTH : 0);
int text_y_offset = (this->line_height - GetCharacterHeight(FS_NORMAL)) / 2;
int box_y_offset = (this->line_height - this->box.height) / 2;
int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
int icon_y_offset = (this->line_height - this->icon.height) / 2;
for (int i = 0; i != lengthof(_cheats_ui); i++) {
const CheatEntry *ce = &_cheats_ui[i];
DrawSprite((*ce->been_used) ? SPR_BOX_CHECKED : SPR_BOX_EMPTY, PAL_NONE, box_left, y + box_y_offset);
switch (ce->type) {
case SLE_BOOL: {
bool on = (*(bool*)ce->variable);
@ -330,7 +321,7 @@ struct CheatWindow : Window {
/* Draw coloured flag for change company cheat */
case STR_CHEAT_CHANGE_COMPANY:
SetDParamMaxValue(0, MAX_COMPANIES);
width = std::max(width, GetStringBoundingBox(ce->str).width + WidgetDimensions::scaled.hsep_wide * 4);
width = std::max(width, GetStringBoundingBox(ce->str).width + WidgetDimensions::scaled.hsep_wide);
break;
default:
@ -342,11 +333,10 @@ struct CheatWindow : Window {
}
}
this->line_height = std::max(this->box.height, this->icon.height);
this->line_height = std::max<uint>(this->line_height, SETTING_BUTTON_HEIGHT);
this->line_height = std::max<uint>(this->line_height, GetCharacterHeight(FS_NORMAL)) + WidgetDimensions::scaled.framerect.Vertical();
size->width = width + WidgetDimensions::scaled.hsep_wide * 4 + this->box.width + SETTING_BUTTON_WIDTH /* stuff on the left */ + WidgetDimensions::scaled.hsep_wide * 2 /* extra spacing on right */;
size->width = width + WidgetDimensions::scaled.hsep_wide * 2 + SETTING_BUTTON_WIDTH;
size->height = WidgetDimensions::scaled.framerect.Vertical() + this->line_height * lengthof(_cheats_ui);
}
@ -356,7 +346,7 @@ struct CheatWindow : Window {
Rect r = this->GetWidget<NWidgetBase>(WID_C_PANEL)->GetCurrentRect().Shrink(WidgetDimensions::scaled.framerect);
uint btn = (pt.y - r.top) / this->line_height;
uint x = pt.x - r.left;
int x = pt.x - r.left;
bool rtl = _current_text_dir == TD_RTL;
if (rtl) x = r.Width() - 1 - x;
@ -366,13 +356,13 @@ struct CheatWindow : Window {
int value = (int32_t)ReadValue(ce->variable, ce->type);
int oldvalue = value;
if (btn == CHT_CHANGE_DATE && x >= WidgetDimensions::scaled.hsep_wide * 2 + this->box.width + SETTING_BUTTON_WIDTH) {
if (btn == CHT_CHANGE_DATE && x >= SETTING_BUTTON_WIDTH) {
/* Click at the date text directly. */
clicked_widget = CHT_CHANGE_DATE;
SetDParam(0, value);
ShowQueryString(STR_JUST_INT, STR_CHEAT_CHANGE_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
return;
} else if (btn == CHT_EDIT_MAX_HL && x >= WidgetDimensions::scaled.hsep_wide * 2 + this->box.width + SETTING_BUTTON_WIDTH) {
} else if (btn == CHT_EDIT_MAX_HL && x >= SETTING_BUTTON_WIDTH) {
clicked_widget = CHT_EDIT_MAX_HL;
SetDParam(0, value);
ShowQueryString(STR_JUST_INT, STR_CHEAT_EDIT_MAX_HL_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
@ -380,7 +370,7 @@ struct CheatWindow : Window {
}
/* Not clicking a button? */
if (!IsInsideMM(x, WidgetDimensions::scaled.hsep_wide * 2 + this->box.width, WidgetDimensions::scaled.hsep_wide * 2 + this->box.width + SETTING_BUTTON_WIDTH)) return;
if (!IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) return;
*ce->been_used = true;
@ -392,10 +382,10 @@ struct CheatWindow : Window {
default:
/* Take whatever the function returns */
value = ce->proc(value + ((x >= WidgetDimensions::scaled.hsep_wide * 2 + this->box.width + SETTING_BUTTON_WIDTH / 2) ? 1 : -1), (x >= WidgetDimensions::scaled.hsep_wide * 2 + this->box.width + SETTING_BUTTON_WIDTH / 2) ? 1 : -1);
value = ce->proc(value + ((x >= SETTING_BUTTON_WIDTH / 2) ? 1 : -1), (x >= SETTING_BUTTON_WIDTH / 2) ? 1 : -1);
/* The first cheat (money), doesn't return a different value. */
if (value != oldvalue || btn == CHT_MONEY) this->clicked = btn * 2 + 1 + ((x >= WidgetDimensions::scaled.hsep_wide * 2 + this->box.width + SETTING_BUTTON_WIDTH / 2) != rtl ? 1 : 0);
if (value != oldvalue || btn == CHT_MONEY) this->clicked = btn * 2 + 1 + ((x >= SETTING_BUTTON_WIDTH / 2) != rtl ? 1 : 0);
break;
}

View File

@ -434,12 +434,13 @@ STR_SCENEDIT_FILE_MENU_SEPARATOR :
STR_SCENEDIT_FILE_MENU_QUIT :Exit
# Settings menu
###length 15
###length 16
STR_SETTINGS_MENU_GAME_OPTIONS :Game options
STR_SETTINGS_MENU_CONFIG_SETTINGS_TREE :Settings
STR_SETTINGS_MENU_AI_SETTINGS :AI settings
STR_SETTINGS_MENU_GAMESCRIPT_SETTINGS :Game script settings
STR_SETTINGS_MENU_NEWGRF_SETTINGS :NewGRF settings
STR_SETTINGS_MENU_SANDBOX_OPTIONS :Sandbox options
STR_SETTINGS_MENU_TRANSPARENCY_OPTIONS :Transparency options
STR_SETTINGS_MENU_TOWN_NAMES_DISPLAYED :Town names displayed
STR_SETTINGS_MENU_STATION_NAMES_DISPLAYED :Station names displayed
@ -2224,9 +2225,7 @@ STR_HELP_WINDOW_BUGTRACKER :{BLACK}Report a
STR_HELP_WINDOW_COMMUNITY :{BLACK}Community
# Cheat window
STR_CHEATS :{WHITE}Cheats
STR_CHEATS_TOOLTIP :{BLACK}Checkboxes indicate if you have used this cheat before
STR_CHEATS_NOTE :{BLACK}Note: any usage of these settings will be recorded by the savegame
STR_CHEATS :{WHITE}Sandbox Options
STR_CHEAT_MONEY :{LTBLUE}Increase money by {CURRENCY_LONG}
STR_CHEAT_CHANGE_COMPANY :{LTBLUE}Playing as company: {ORANGE}{COMMA}
STR_CHEAT_EXTRA_DYNAMITE :{LTBLUE}Magic bulldozer (remove industries, unmovable objects): {ORANGE}{STRING1}

View File

@ -33,6 +33,7 @@
#include "../timer/timer.h"
#include "../timer/timer_game_calendar.h"
#include "../timer/timer_game_economy.h"
#include "../timer/timer_game_realtime.h"
#include <mutex>
#include <condition_variable>
@ -1491,29 +1492,6 @@ void NetworkUpdateClientInfo(ClientID client_id)
NetworkAdminClientUpdate(ci);
}
/** Check if we want to restart the map */
static void NetworkCheckRestartMap()
{
if (_settings_client.network.restart_game_year != 0 && TimerGameCalendar::year >= _settings_client.network.restart_game_year) {
Debug(net, 3, "Auto-restarting map: year {} reached", TimerGameCalendar::year);
_settings_newgame.game_creation.generation_seed = GENERATE_NEW_SEED;
switch(_file_to_saveload.abstract_ftype) {
case FT_SAVEGAME:
case FT_SCENARIO:
_switch_mode = SM_LOAD_GAME;
break;
case FT_HEIGHTMAP:
_switch_mode = SM_START_HEIGHTMAP;
break;
default:
_switch_mode = SM_NEWGAME;
}
}
}
/** Check if the server has autoclean_companies activated
* Two things happen:
* 1) If a company is not protected, it is closed after 1 year (for example)
@ -1811,11 +1789,65 @@ void NetworkServer_Tick(bool send_frame)
}
}
/** Helper function to restart the map. */
static void NetworkRestartMap()
{
_settings_newgame.game_creation.generation_seed = GENERATE_NEW_SEED;
switch (_file_to_saveload.abstract_ftype) {
case FT_SAVEGAME:
case FT_SCENARIO:
_switch_mode = SM_LOAD_GAME;
break;
case FT_HEIGHTMAP:
_switch_mode = SM_START_HEIGHTMAP;
break;
default:
_switch_mode = SM_NEWGAME;
}
}
/** Timer to restart a network server automatically based on real-time hours played. Initialized at zero to disable until settings are loaded. */
static IntervalTimer<TimerGameRealtime> _network_restart_map_timer({std::chrono::hours::zero(), TimerGameRealtime::UNPAUSED}, [](auto)
{
if (!_network_server) return;
/* If setting is 0, this feature is disabled. */
if (_settings_client.network.restart_hours == 0) return;
Debug(net, 3, "Auto-restarting map: {} hours played", _settings_client.network.restart_hours);
NetworkRestartMap();
});
/**
* Reset the automatic network restart time interval.
* @param reset Whether to reset the timer to zero.
*/
void ChangeNetworkRestartTime(bool reset)
{
if (!_network_server) return;
_network_restart_map_timer.SetInterval({ std::chrono::hours(_settings_client.network.restart_hours), TimerGameRealtime::UNPAUSED }, reset);
}
/** Check if we want to restart the map based on the year. */
static void NetworkCheckRestartMapYear()
{
/* If setting is 0, this feature is disabled. */
if (_settings_client.network.restart_game_year == 0) return;
if (TimerGameCalendar::year >= _settings_client.network.restart_game_year) {
Debug(net, 3, "Auto-restarting map: year {} reached", TimerGameCalendar::year);
NetworkRestartMap();
}
}
/** Calendar yearly "callback". Called whenever the calendar year changes. */
static IntervalTimer<TimerGameCalendar> _calendar_network_yearly({ TimerGameCalendar::YEAR, TimerGameCalendar::Priority::NONE }, [](auto) {
if (!_network_server) return;
NetworkCheckRestartMap();
NetworkCheckRestartMapYear();
});
/** Economy yearly "callback". Called whenever the economy year changes. */

View File

@ -120,6 +120,7 @@ public:
};
void NetworkServer_Tick(bool send_frame);
void ChangeNetworkRestartTime(bool reset);
void NetworkServerSetCompanyPassword(CompanyID company_id, const std::string &password, bool already_hashed = true);
void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded);

View File

@ -34,6 +34,7 @@
#include "console_func.h"
#include "screenshot.h"
#include "network/network.h"
#include "network/network_server.h"
#include "network/network_func.h"
#include "ai/ai.hpp"
#include "ai/ai_config.hpp"
@ -917,7 +918,11 @@ static void MakeNewGameDone()
CheckIndustries();
MarkWholeScreenDirty();
if (_network_server && !_network_dedicated) ShowClientList();
if (_network_server) {
ChangeNetworkRestartTime(true);
if (!_network_dedicated) ShowClientList();
}
}
static void MakeNewGame(bool from_heightmap, bool reset_settings)

View File

@ -325,6 +325,7 @@ struct NetworkSettings {
uint8_t max_companies; ///< maximum amount of companies
uint8_t max_clients; ///< maximum amount of clients
TimerGameCalendar::Year restart_game_year; ///< year the server restarts
uint16_t restart_hours; ///< number of hours to run the server before automatic restart
uint8_t min_active_clients; ///< minimum amount of active clients to unpause the game
bool reload_cfg; ///< reload the config file before restarting
std::string last_joined; ///< Last joined server

View File

@ -8,6 +8,7 @@
[pre-amble]
static void UpdateClientConfigValues();
void ChangeNetworkRestartTime(bool reset);
static constexpr std::initializer_list<const char*> _server_game_type{"local", "public", "invite-only"};
@ -241,6 +242,16 @@ min = CalendarTime::MIN_YEAR
max = CalendarTime::MAX_YEAR
interval = 1
[SDTC_VAR]
var = network.restart_hours
type = SLE_UINT16
flags = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC | SF_GUI_0_IS_SPECIAL | SF_NETWORK_ONLY
def = 0
min = 0
max = UINT16_MAX
interval = 1
post_cb = [](auto) { ChangeNetworkRestartTime(false); }
[SDTC_VAR]
var = network.min_active_clients
type = SLE_UINT8

View File

@ -228,6 +228,7 @@ enum OptionMenuEntries {
OME_AI_SETTINGS,
OME_GAMESCRIPT_SETTINGS,
OME_NEWGRFSETTINGS,
ONE_SANDBOX,
OME_TRANSPARENCIES,
OME_SHOW_TOWNNAMES,
OME_SHOW_STATIONNAMES,
@ -259,6 +260,9 @@ static CallBackFunction ToolbarOptionsClick(Window *w)
list.push_back(std::make_unique<DropDownListStringItem>(STR_SETTINGS_MENU_GAMESCRIPT_SETTINGS, OME_GAMESCRIPT_SETTINGS, false));
}
list.push_back(std::make_unique<DropDownListStringItem>(STR_SETTINGS_MENU_NEWGRF_SETTINGS, OME_NEWGRFSETTINGS, false));
if (_game_mode != GM_EDITOR) {
list.push_back(std::make_unique<DropDownListStringItem>(STR_SETTINGS_MENU_SANDBOX_OPTIONS, ONE_SANDBOX, false));
}
list.push_back(std::make_unique<DropDownListStringItem>(STR_SETTINGS_MENU_TRANSPARENCY_OPTIONS, OME_TRANSPARENCIES, false));
list.push_back(std::make_unique<DropDownListDividerItem>(-1, false));
list.push_back(std::make_unique<DropDownListCheckedItem>(HasBit(_display_opt, DO_SHOW_TOWN_NAMES), STR_SETTINGS_MENU_TOWN_NAMES_DISPLAYED, OME_SHOW_TOWNNAMES, false));
@ -290,6 +294,7 @@ static CallBackFunction MenuClickSettings(int index)
case OME_AI_SETTINGS: ShowAIConfigWindow(); return CBF_NONE;
case OME_GAMESCRIPT_SETTINGS: ShowGSConfigWindow(); return CBF_NONE;
case OME_NEWGRFSETTINGS: ShowNewGRFSettings(!_networking && _settings_client.gui.UserIsAllowedToChangeNewGRFs(), true, true, &_grfconfig); return CBF_NONE;
case ONE_SANDBOX: ShowCheatWindow(); break;
case OME_TRANSPARENCIES: ShowTransparencyToolbar(); break;
case OME_SHOW_TOWNNAMES: ToggleBit(_display_opt, DO_SHOW_TOWN_NAMES); break;