Start the Windows game at a colony and save menu

This commit is contained in:
Codex committed 2026-11-29 18:34:15 +03:00
1 parent ac4c010c71
commit 5cb3b9cec2
5 files changed
+395 -60

No files matched your search

+144 -54
View File
@@ -8,6 +8,7 @@
#include "isometric_camera.hpp"
#include "lan_session.hpp"
#include "title_screen_win32.hpp"
#include "settlement/core/world.hpp"
#include <algorithm>
@@ -33,8 +34,7 @@ namespace {
constexpr wchar_t window_class[] = L"SettlementIsometricViewer";
constexpr wchar_t resident_window_class[] = L"SettlementResidentRoster";
constexpr int worker_combo_id = 1001;
constexpr int apply_button_id = 1002;
constexpr int title_menu_id = 1002;
constexpr int status_label_id = 1003;
constexpr int cabin_button_id = 1004;
constexpr int save_menu_id = 1005;
@@ -82,8 +82,6 @@ constexpr int work_button_gap = 8;
constexpr settlement::GridPosition sawmill{15, 14};
constexpr settlement::GridPosition stonecutter{17, 14};
HWND worker_combo = nullptr;
HWND apply_button = nullptr;
HWND status_label = nullptr;
HWND production_label = nullptr;
HWND wood_bed_button = nullptr;
@@ -93,10 +91,16 @@ HWND pause_button = nullptr;
HWND residents_button = nullptr;
std::array<HWND, 3> dock_section_buttons{};
std::unique_ptr<settlement::World> world;
namespace title = settlement::viewer;
title::TitleScreen title_screen;
bool showing_title = true;
bool has_session = false;
HMENU play_menu = nullptr;
using settlement::viewer::LanState;
settlement::viewer::LanSession lan;
LanState displayed_lan_state = LanState::Offline;
std::size_t selected_workers = settlement::min_worker_count;
const auto local_worker_count = std::clamp<std::size_t>(
std::thread::hardware_concurrency(), settlement::min_worker_count, 8);
enum class PlacementMode { None, Building, Foraging, BerryField, FlaxField, TreePlot, Mine, Loom, Storage, Pantry, Move, Demolish };
PlacementMode placement_mode = PlacementMode::None;
enum class WorkPanel { Farmer, Woodcutter, Miner, Builder, Gathering, Zones, Construction, None };
@@ -316,7 +320,8 @@ bool shows_bed_choices() {
}
bool is_map_point(const RECT& client, POINT point) {
return point.x >= 0 && point.x < client.right && point.y >= map_top && point.y < client.bottom - map_footer;
return !showing_title && point.x >= 0 && point.x < client.right &&
point.y >= map_top && point.y < client.bottom - map_footer;
}
POINT client_point(LPARAM coordinates) {
@@ -832,8 +837,7 @@ void update_status() {
L" Заняты: " + std::to_wstring(busy) +
L" Выполнено работ: " + std::to_wstring(world->jobs_completed()) +
L" Дерево: " + std::to_wstring(wood) + L" Камень: " + std::to_wstring(stone) +
L" Ход: " + std::to_wstring(world->tick()) +
(lan.is_guest() ? L" Симуляция у хозяина" : L" Потоков: " + std::to_wstring(world->worker_count()));
L" Ход: " + std::to_wstring(world->tick());
SetWindowTextW(status_label, text.c_str());
const bool scenario = world->scenario_active();
const bool can_play = can_give_orders();
@@ -910,13 +914,41 @@ void toggle_work_panel(HWND window, WorkPanel panel) {
SetFocus(window);
}
void show_title_screen(HWND window, bool visible) {
showing_title = visible;
if (visible) {
if (roster.window) DestroyWindow(roster.window);
set_work_panel(window, WorkPanel::None);
set_placement_mode(window, PlacementMode::None);
title_screen.reload_saves();
title_screen.update(has_session, lan.state() == LanState::Connecting);
} else {
has_session = true;
}
const int flags = visible ? SW_HIDE : SW_SHOWNA;
for (const auto control : {status_label, production_label, priority_button, pause_button, residents_button}) {
ShowWindow(control, flags);
}
for (const auto control : dock_section_buttons) ShowWindow(control, flags);
title_screen.show(visible);
SetMenu(window, visible ? nullptr : play_menu);
DrawMenuBar(window);
RECT client{};
GetClientRect(window, &client);
title_screen.layout(client.right, client.bottom);
layout_command_dock(window);
SetFocus(window);
InvalidateRect(window, nullptr, FALSE);
}
void reset_world(HWND window) {
world = settlement::make_first_colony(selected_workers);
world = settlement::make_first_colony(local_worker_count);
camera = {};
selected_resident.reset();
sync_resident_roster(true);
set_work_panel(window, WorkPanel::None);
set_placement_mode(window, PlacementMode::None);
show_title_screen(window, false);
update_status();
invalidate_scene(window);
}
@@ -932,6 +964,12 @@ std::optional<std::filesystem::path> choose_save_file(HWND window, bool saving)
dialog.lpstrFile = filename;
dialog.nMaxFile = static_cast<DWORD>(std::size(filename));
dialog.lpstrDefExt = L"settlement";
const auto folder = title::saves_directory();
if (!folder.empty()) {
std::error_code error;
if (saving) std::filesystem::create_directories(folder, error);
if (std::filesystem::is_directory(folder, error)) dialog.lpstrInitialDir = folder.c_str();
}
dialog.Flags = OFN_EXPLORER | OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST |
(saving ? OFN_OVERWRITEPROMPT : OFN_FILEMUSTEXIST);
const BOOL accepted = saving ? GetSaveFileNameW(&dialog) : GetOpenFileNameW(&dialog);
@@ -961,8 +999,8 @@ void save_world_file(const std::filesystem::path& path) {
}
}
void handle_save_menu(HWND window, bool saving) {
if (file_dialog_active || !world) return;
void handle_save_menu(HWND window, bool saving, std::optional<std::filesystem::path> selected = std::nullopt) {
if (file_dialog_active || (saving && !world)) return;
if (lan.is_guest() || (!saving && lan.state() != LanState::Offline)) return;
file_dialog_active = true;
struct RestoreTimer {
@@ -970,7 +1008,7 @@ void handle_save_menu(HWND window, bool saving) {
} restore_timer;
try {
const auto path = choose_save_file(window, saving);
const auto path = selected ? std::move(selected) : choose_save_file(window, saving);
if (!path) return;
if (saving) {
save_world_file(*path);
@@ -980,15 +1018,17 @@ void handle_save_menu(HWND window, bool saving) {
}
std::ifstream stream(*path, std::ios::binary);
if (!stream) throw std::runtime_error("Could not open the requested save.");
auto loaded = settlement::World::read_save(stream, selected_workers);
auto loaded = settlement::World::read_save(stream, local_worker_count);
if (loaded->width() != map_size || loaded->height() != map_size) {
throw std::runtime_error("This viewer opens saves with a 32 by 32 map.");
}
world = std::move(loaded);
camera = {};
selected_resident.reset();
sync_resident_roster(true);
set_work_panel(window, WorkPanel::None);
set_placement_mode(window, PlacementMode::None, L"Поселение загружено.");
show_title_screen(window, false);
update_status();
} catch (const std::exception& error) {
const std::string_view details(error.what());
@@ -1104,17 +1144,15 @@ void refresh_lan_controls(HWND window) {
const bool offline = state == LanState::Offline;
const bool hosting = state == LanState::Hosting || state == LanState::HostConnected;
const bool disconnected = state == LanState::GuestDisconnected;
EnableWindow(worker_combo, offline);
EnableWindow(apply_button, offline);
const auto menu = GetMenu(window);
const auto enable = [&](UINT command, bool enabled) {
EnableMenuItem(menu, command, MF_BYCOMMAND | (enabled ? MF_ENABLED : MF_GRAYED));
if (play_menu) EnableMenuItem(play_menu, command, MF_BYCOMMAND | (enabled ? MF_ENABLED : MF_GRAYED));
};
enable(lan_host_id, offline);
enable(lan_join_id, offline || disconnected);
enable(lan_info_id, hosting);
enable(lan_reconnect_id, disconnected);
enable(lan_leave_id, !offline);
enable(title_menu_id, offline);
enable(save_menu_id, !lan.is_guest());
enable(load_menu_id, offline);
const wchar_t* suffix = L"";
@@ -1126,24 +1164,29 @@ void refresh_lan_controls(HWND window) {
case LanState::GuestConnected: suffix = L" · гость"; break;
case LanState::GuestDisconnected: suffix = L" · связь потеряна"; break;
}
const std::wstring title = std::wstring(L"Settlement — Первое поселение") + suffix;
SetWindowTextW(window, title.c_str());
const auto caption = std::wstring(showing_title ? L"Settlement" : L"Settlement — Первое поселение") + suffix;
SetWindowTextW(window, caption.c_str());
if (disconnected) {
set_placement_mode(window, PlacementMode::None);
placement_message = L"Связь прервана. «Кооператив → Подключиться снова». " + widen_ascii(lan.detail());
placement_message = showing_title ? L"Не удалось подключиться. Проверьте адрес и код." :
L"Связь прервана. «Кооператив → Подключиться снова». " + widen_ascii(lan.detail());
} else if (state == LanState::GuestConnected) {
placement_message = L"Подключено к колонии хозяина.";
}
DrawMenuBar(window);
if (roster.window) update_roster_details();
update_status();
if (showing_title) {
title_screen.update(has_session, state == LanState::Connecting);
InvalidateRect(window, nullptr, FALSE);
}
invalidate_scene(window);
}
void handle_lan_menu(HWND window, int command) {
try {
if (command == lan_host_id && lan.state() == LanState::Offline) {
if (!world->scenario_active()) {
if (!world || !world->scenario_active()) {
throw std::runtime_error("Start the playable colony before hosting.");
}
lan.host();
@@ -1153,6 +1196,7 @@ void handle_lan_menu(HWND window, int command) {
(lan.state() == LanState::Offline || lan.state() == LanState::GuestDisconnected)) {
const auto request = ask_join_address(window);
if (!request) return;
if (!world) world = settlement::make_first_colony(local_worker_count);
lan.join(narrow_lan_input(request->address.data()), narrow_lan_input(request->code.data()));
selected_resident.reset();
sync_resident_roster(true);
@@ -1175,6 +1219,41 @@ void handle_lan_menu(HWND window, int command) {
}
}
void handle_title_command(HWND window, int command, int notification) {
if (command == title::title_exit_id && notification == BN_CLICKED) {
DestroyWindow(window);
return;
}
if (lan.state() == LanState::Connecting) return;
if (command == title::title_saves_id && notification == LBN_SELCHANGE) {
title_screen.update(has_session, false);
return;
}
if (command == title::title_join_id && notification == BN_CLICKED) {
handle_lan_menu(window, lan_join_id);
return;
}
const bool open_selected = (command == title::title_open_id && notification == BN_CLICKED) ||
(command == title::title_saves_id && notification == LBN_DBLCLK);
if (!open_selected && (notification != BN_CLICKED ||
(command != title::title_new_id && command != title::title_continue_id && command != title::title_browse_id))) {
return;
}
if (lan.state() == LanState::GuestDisconnected) lan.leave();
if (open_selected) {
if (const auto path = title_screen.selected_save()) handle_save_menu(window, false, path);
} else if (command == title::title_new_id) {
reset_world(window);
} else if (command == title::title_browse_id) {
handle_save_menu(window, false);
} else if (has_session) {
show_title_screen(window, false);
} else if (const auto path = title_screen.latest_save()) {
handle_save_menu(window, false, path);
}
refresh_lan_controls(window);
}
const settlement::ResourceNode* berry_source(settlement::GridPosition cell) {
for (const auto& node : world->resource_nodes()) {
if (node.position == cell && node.type == settlement::ItemType::Berries) return &node;
@@ -1714,27 +1793,9 @@ LRESULT CALLBACK window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lp
return 0;
}
case WM_CREATE: {
constexpr auto minimum = static_cast<int>(settlement::min_worker_count);
const UINT hardware = std::max(static_cast<UINT>(minimum), std::thread::hardware_concurrency());
const int max_workers = static_cast<int>(std::min(hardware, 32U));
selected_workers = static_cast<std::size_t>(std::min(max_workers, 8));
CreateWindowExW(0, L"STATIC", L"Число потоков:", WS_CHILD | WS_VISIBLE,
18, 14, 112, 25, window, nullptr, nullptr, nullptr);
worker_combo = CreateWindowExW(0, WC_COMBOBOXW, L"", WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
132, 10, 76, 280, window,
reinterpret_cast<HMENU>(static_cast<INT_PTR>(worker_combo_id)),
GetModuleHandleW(nullptr), nullptr);
for (int count = minimum; count <= max_workers; ++count) {
const std::wstring option = std::to_wstring(count);
SendMessageW(worker_combo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(option.c_str()));
}
SendMessageW(worker_combo, CB_SETCURSEL, static_cast<WPARAM>(selected_workers - settlement::min_worker_count), 0);
apply_button = CreateWindowExW(0, L"BUTTON", L"Применить и начать заново", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
220, 10, 190, 28, window,
reinterpret_cast<HMENU>(static_cast<INT_PTR>(apply_button_id)),
GetModuleHandleW(nullptr), nullptr);
play_menu = GetMenu(window);
status_label = CreateWindowExW(0, L"STATIC", L"", WS_CHILD | WS_VISIBLE,
430, 14, 620, 24, window,
18, 14, 735, 24, window,
reinterpret_cast<HMENU>(static_cast<INT_PTR>(status_label_id)),
GetModuleHandleW(nullptr), nullptr);
production_label = CreateWindowExW(0, L"STATIC", L"", WS_CHILD | WS_VISIBLE,
@@ -1760,17 +1821,29 @@ LRESULT CALLBACK window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lp
0, 100, L"", BS_OWNERDRAW, 0);
ShowWindow(work_choice_buttons[index], SW_HIDE);
}
layout_command_dock(window);
reset_world(window);
title_screen.create(window);
show_title_screen(window, true);
refresh_lan_controls(window);
SetTimer(window, timer_id, 60, nullptr);
return 0;
}
case WM_DRAWITEM:
if (const auto* item = reinterpret_cast<const DRAWITEMSTRUCT*>(lparam);
item && item->CtlType == ODT_BUTTON && draw_toolbar_button(*item)) return TRUE;
item && item->CtlType == ODT_BUTTON) {
if (showing_title ? title::TitleScreen::draw_button(*item) : draw_toolbar_button(*item)) return TRUE;
}
break;
case WM_CTLCOLORLISTBOX:
if (showing_title) {
if (const auto brush = title_screen.paint_list_background(reinterpret_cast<HWND>(lparam),
reinterpret_cast<HDC>(wparam))) return reinterpret_cast<LRESULT>(brush);
}
break;
case WM_COMMAND:
if (showing_title) {
handle_title_command(window, LOWORD(wparam), HIWORD(wparam));
return 0;
}
if (LOWORD(wparam) == residents_button_id && HIWORD(wparam) == BN_CLICKED) {
if (world) show_resident_roster(window);
return 0;
@@ -1825,11 +1898,11 @@ LRESULT CALLBACK window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lp
handle_save_menu(window, LOWORD(wparam) == save_menu_id);
return 0;
}
if (LOWORD(wparam) == apply_button_id) {
if (lan.state() != LanState::Offline) return 0;
const LRESULT selection = SendMessageW(worker_combo, CB_GETCURSEL, 0, 0);
if (selection != CB_ERR) selected_workers = static_cast<std::size_t>(selection) + settlement::min_worker_count;
reset_world(window);
if (LOWORD(wparam) == title_menu_id) {
if (lan.state() == LanState::Offline) {
show_title_screen(window, true);
refresh_lan_controls(window);
}
return 0;
}
if (LOWORD(wparam) == cabin_button_id && HIWORD(wparam) == BN_CLICKED) {
@@ -2016,6 +2089,7 @@ LRESULT CALLBACK window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lp
}
break;
case WM_KEYDOWN:
if (showing_title) break;
if ((GetKeyState(VK_CONTROL) & 0x8000) == 0 && (wparam == 'H' || wparam == 'Z' || wparam == 'B')) {
if ((lparam & (1L << 30)) == 0) {
toggle_work_panel(window, wparam == 'H' ? WorkPanel::Gathering :
@@ -2039,8 +2113,12 @@ LRESULT CALLBACK window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lp
break;
case WM_TIMER:
if (wparam == timer_id && world) {
if (!file_dialog_active && !lan.is_guest()) world->step();
if (auto snapshot = lan.poll(*world)) world = std::move(snapshot);
if (showing_title && lan.state() != LanState::Connecting) return 0;
if (!showing_title && !file_dialog_active && !lan.is_guest()) world->step();
if (auto snapshot = lan.poll(*world)) {
world = std::move(snapshot);
if (showing_title) show_title_screen(window, false);
}
sync_resident_roster();
if (lan.state() != displayed_lan_state) refresh_lan_controls(window);
if (const auto rejection = lan.take_rejection()) {
@@ -2056,7 +2134,13 @@ LRESULT CALLBACK window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lp
case WM_ERASEBKGND:
return 1;
case WM_SIZE:
layout_command_dock(window);
if (showing_title) {
RECT client{};
GetClientRect(window, &client);
title_screen.layout(client.right, client.bottom);
} else {
layout_command_dock(window);
}
InvalidateRect(window, nullptr, FALSE);
break;
case WM_PAINT: {
@@ -2069,7 +2153,8 @@ LRESULT CALLBACK window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lp
HDC buffer = CreateCompatibleDC(target);
HBITMAP bitmap = CreateCompatibleBitmap(target, width, height);
HGDIOBJ old_bitmap = SelectObject(buffer, bitmap);
draw_world(buffer, width, height);
if (showing_title) title_screen.draw(buffer, width, height, placement_message);
else draw_world(buffer, width, height);
const RECT& dirty = paint.rcPaint;
BitBlt(target, dirty.left, dirty.top, dirty.right - dirty.left, dirty.bottom - dirty.top,
buffer, dirty.left, dirty.top, SRCCOPY);
@@ -2082,6 +2167,8 @@ LRESULT CALLBACK window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lp
case WM_DESTROY:
KillTimer(window, timer_id);
lan.leave();
if (play_menu && GetMenu(window) != play_menu) DestroyMenu(play_menu);
play_menu = nullptr;
PostQuitMessage(0);
return 0;
}
@@ -2109,6 +2196,8 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR, int show_command) {
HMENU work_menu = create_work_menu();
AppendMenuW(file_menu, MF_STRING, save_menu_id, L"Сохранить поселение…");
AppendMenuW(file_menu, MF_STRING, load_menu_id, L"Загрузить поселение…");
AppendMenuW(file_menu, MF_SEPARATOR, 0, nullptr);
AppendMenuW(file_menu, MF_STRING, title_menu_id, L"Главное меню");
AppendMenuW(menu_bar, MF_POPUP, reinterpret_cast<UINT_PTR>(file_menu), L"Поселение");
AppendMenuW(view_menu, MF_STRING, reset_camera_id, L"Общий план\tHome");
AppendMenuW(menu_bar, MF_POPUP, reinterpret_cast<UINT_PTR>(view_menu), L"Вид");
@@ -2122,7 +2211,7 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR, int show_command) {
AppendMenuW(coop_menu, MF_SEPARATOR, 0, nullptr);
AppendMenuW(coop_menu, MF_STRING, lan_leave_id, L"Выйти из сетевой игры");
AppendMenuW(menu_bar, MF_POPUP, reinterpret_cast<UINT_PTR>(coop_menu), L"Кооператив");
HWND window = CreateWindowExW(0, window_class, L"Settlement — Первое поселение",
HWND window = CreateWindowExW(0, window_class, L"Settlement",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 1120, 752,
nullptr, menu_bar, instance, nullptr);
if (!window) {
@@ -2135,6 +2224,7 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR, int show_command) {
MSG message{};
while (GetMessageW(&message, nullptr, 0, 0) > 0) {
if (roster.window && IsDialogMessageW(roster.window, &message)) continue;
if (showing_title && IsDialogMessageW(window, &message)) continue;
TranslateMessage(&message);
DispatchMessageW(&message);
}