| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377 |
- #include "game_engine.h"
- #include "../controllers/action_vfx.h"
- #include "../controllers/command_controller.h"
- #include "../models/audio_system_proxy.h"
- #include "../models/cursor_manager.h"
- #include "../models/hover_tracker.h"
- #include "AudioEventHandler.h"
- #include "app/models/cursor_mode.h"
- #include "app/utils/engine_view_helpers.h"
- #include "app/utils/movement_utils.h"
- #include "app/utils/selection_utils.h"
- #include "core/system.h"
- #include "game/audio/AudioSystem.h"
- #include "game/units/spawn_type.h"
- #include "game/units/troop_type.h"
- #include <QBuffer>
- #include <QCoreApplication>
- #include <QCursor>
- #include <QDebug>
- #include <QImage>
- #include <QOpenGLContext>
- #include <QPainter>
- #include <QQuickWindow>
- #include <QSize>
- #include <QVariant>
- #include <QVariantMap>
- #include <memory>
- #include <optional>
- #include <qbuffer.h>
- #include <qcoreapplication.h>
- #include <qdir.h>
- #include <qevent.h>
- #include <qglobal.h>
- #include <qimage.h>
- #include <qjsonobject.h>
- #include <qnamespace.h>
- #include <qobject.h>
- #include <qobjectdefs.h>
- #include <qpoint.h>
- #include <qsize.h>
- #include <qstringliteral.h>
- #include <qstringview.h>
- #include <qtmetamacros.h>
- #include <qvectornd.h>
- #include <unordered_set>
- #include "../models/selected_units_model.h"
- #include "game/core/component.h"
- #include "game/core/event_manager.h"
- #include "game/core/world.h"
- #include "game/game_config.h"
- #include "game/map/environment.h"
- #include "game/map/level_loader.h"
- #include "game/map/map_catalog.h"
- #include "game/map/map_loader.h"
- #include "game/map/map_transformer.h"
- #include "game/map/minimap/minimap_generator.h"
- #include "game/map/minimap/unit_layer.h"
- #include "game/map/skirmish_loader.h"
- #include "game/map/terrain_service.h"
- #include "game/map/visibility_service.h"
- #include "game/map/world_bootstrap.h"
- #include "game/systems/ai_system.h"
- #include "game/systems/arrow_system.h"
- #include "game/systems/ballista_attack_system.h"
- #include "game/systems/building_collision_registry.h"
- #include "game/systems/camera_service.h"
- #include "game/systems/capture_system.h"
- #include "game/systems/catapult_attack_system.h"
- #include "game/systems/cleanup_system.h"
- #include "game/systems/combat_system.h"
- #include "game/systems/command_service.h"
- #include "game/systems/formation_planner.h"
- #include "game/systems/game_state_serializer.h"
- #include "game/systems/global_stats_registry.h"
- #include "game/systems/healing_system.h"
- #include "game/systems/movement_system.h"
- #include "game/systems/nation_id.h"
- #include "game/systems/nation_registry.h"
- #include "game/systems/owner_registry.h"
- #include "game/systems/patrol_system.h"
- #include "game/systems/picking_service.h"
- #include "game/systems/production_service.h"
- #include "game/systems/production_system.h"
- #include "game/systems/projectile_system.h"
- #include "game/systems/save_load_service.h"
- #include "game/systems/selection_system.h"
- #include "game/systems/terrain_alignment_system.h"
- #include "game/systems/troop_count_registry.h"
- #include "game/systems/victory_service.h"
- #include "game/units/factory.h"
- #include "game/units/troop_config.h"
- #include "render/geom/arrow.h"
- #include "render/geom/patrol_flags.h"
- #include "render/geom/stone.h"
- #include "render/gl/bootstrap.h"
- #include "render/gl/camera.h"
- #include "render/ground/biome_renderer.h"
- #include "render/ground/bridge_renderer.h"
- #include "render/ground/firecamp_renderer.h"
- #include "render/ground/fog_renderer.h"
- #include "render/ground/ground_renderer.h"
- #include "render/ground/olive_renderer.h"
- #include "render/ground/pine_renderer.h"
- #include "render/ground/plant_renderer.h"
- #include "render/ground/river_renderer.h"
- #include "render/ground/riverbank_renderer.h"
- #include "render/ground/road_renderer.h"
- #include "render/ground/stone_renderer.h"
- #include "render/ground/terrain_renderer.h"
- #include "render/scene_renderer.h"
- #include <QDir>
- #include <QFile>
- #include <QJsonArray>
- #include <QJsonDocument>
- #include <QJsonObject>
- #include <QSet>
- #include <algorithm>
- #include <cmath>
- #include <utility>
- #include <vector>
- GameEngine::GameEngine(QObject *parent)
- : QObject(parent),
- m_selectedUnitsModel(new SelectedUnitsModel(this, this)) {
- Game::Systems::NationRegistry::instance().initialize_defaults();
- Game::Systems::TroopCountRegistry::instance().initialize();
- Game::Systems::GlobalStatsRegistry::instance().initialize();
- m_world = std::make_unique<Engine::Core::World>();
- m_renderer = std::make_unique<Render::GL::Renderer>();
- m_camera = std::make_unique<Render::GL::Camera>();
- m_ground = std::make_unique<Render::GL::GroundRenderer>();
- m_terrain = std::make_unique<Render::GL::TerrainRenderer>();
- m_biome = std::make_unique<Render::GL::BiomeRenderer>();
- m_river = std::make_unique<Render::GL::RiverRenderer>();
- m_road = std::make_unique<Render::GL::RoadRenderer>();
- m_riverbank = std::make_unique<Render::GL::RiverbankRenderer>();
- m_bridge = std::make_unique<Render::GL::BridgeRenderer>();
- m_fog = std::make_unique<Render::GL::FogRenderer>();
- m_stone = std::make_unique<Render::GL::StoneRenderer>();
- m_plant = std::make_unique<Render::GL::PlantRenderer>();
- m_pine = std::make_unique<Render::GL::PineRenderer>();
- m_olive = std::make_unique<Render::GL::OliveRenderer>();
- m_firecamp = std::make_unique<Render::GL::FireCampRenderer>();
- m_passes = {m_ground.get(), m_terrain.get(), m_river.get(),
- m_road.get(), m_riverbank.get(), m_bridge.get(),
- m_biome.get(), m_stone.get(), m_plant.get(),
- m_pine.get(), m_olive.get(), m_firecamp.get(),
- m_fog.get()};
- std::unique_ptr<Engine::Core::System> arrow_sys =
- std::make_unique<Game::Systems::ArrowSystem>();
- m_world->add_system(std::move(arrow_sys));
- std::unique_ptr<Engine::Core::System> projectile_sys =
- std::make_unique<Game::Systems::ProjectileSystem>();
- m_world->add_system(std::move(projectile_sys));
- m_world->add_system(std::make_unique<Game::Systems::MovementSystem>());
- m_world->add_system(std::make_unique<Game::Systems::PatrolSystem>());
- m_world->add_system(std::make_unique<Game::Systems::CombatSystem>());
- m_world->add_system(std::make_unique<Game::Systems::CatapultAttackSystem>());
- m_world->add_system(std::make_unique<Game::Systems::BallistaAttackSystem>());
- m_world->add_system(std::make_unique<Game::Systems::HealingSystem>());
- m_world->add_system(std::make_unique<Game::Systems::CaptureSystem>());
- m_world->add_system(std::make_unique<Game::Systems::AISystem>());
- m_world->add_system(std::make_unique<Game::Systems::ProductionSystem>());
- m_world->add_system(
- std::make_unique<Game::Systems::TerrainAlignmentSystem>());
- m_world->add_system(std::make_unique<Game::Systems::CleanupSystem>());
- {
- std::unique_ptr<Engine::Core::System> sel_sys =
- std::make_unique<Game::Systems::SelectionSystem>();
- m_world->add_system(std::move(sel_sys));
- }
- m_pickingService = std::make_unique<Game::Systems::PickingService>();
- m_victoryService = std::make_unique<Game::Systems::VictoryService>();
- m_saveLoadService = std::make_unique<Game::Systems::SaveLoadService>();
- m_cameraService = std::make_unique<Game::Systems::CameraService>();
- auto *selection_system =
- m_world->get_system<Game::Systems::SelectionSystem>();
- m_selectionController = std::make_unique<Game::Systems::SelectionController>(
- m_world.get(), selection_system, m_pickingService.get());
- m_commandController = std::make_unique<App::Controllers::CommandController>(
- m_world.get(), selection_system, m_pickingService.get());
- m_cursorManager = std::make_unique<CursorManager>();
- m_hoverTracker = std::make_unique<HoverTracker>(m_pickingService.get());
- m_mapCatalog = std::make_unique<Game::Map::MapCatalog>(this);
- connect(m_mapCatalog.get(), &Game::Map::MapCatalog::map_loaded, this,
- [this](const QVariantMap &mapData) {
- m_available_maps.append(mapData);
- emit available_maps_changed();
- });
- connect(m_mapCatalog.get(), &Game::Map::MapCatalog::loading_changed, this,
- [this](bool loading) {
- m_maps_loading = loading;
- emit maps_loading_changed();
- });
- connect(m_mapCatalog.get(), &Game::Map::MapCatalog::all_maps_loaded, this,
- [this]() { emit available_maps_changed(); });
- if (AudioSystem::getInstance().initialize()) {
- qInfo() << "AudioSystem initialized successfully";
- load_audio_resources();
- } else {
- qWarning() << "Failed to initialize AudioSystem";
- }
- m_audio_systemProxy = std::make_unique<App::Models::AudioSystemProxy>(this);
- m_audioEventHandler =
- std::make_unique<Game::Audio::AudioEventHandler>(m_world.get());
- if (m_audioEventHandler->initialize()) {
- qInfo() << "AudioEventHandler initialized successfully";
- m_audioEventHandler->loadUnitVoiceMapping("archer", "archer_voice");
- m_audioEventHandler->loadUnitVoiceMapping("swordsman", "swordsman_voice");
- m_audioEventHandler->loadUnitVoiceMapping("swordsman", "swordsman_voice");
- m_audioEventHandler->loadUnitVoiceMapping("spearman", "spearman_voice");
- m_audioEventHandler->loadAmbientMusic(Engine::Core::AmbientState::PEACEFUL,
- "music_peaceful");
- m_audioEventHandler->loadAmbientMusic(Engine::Core::AmbientState::TENSE,
- "music_tense");
- m_audioEventHandler->loadAmbientMusic(Engine::Core::AmbientState::COMBAT,
- "music_combat");
- m_audioEventHandler->loadAmbientMusic(Engine::Core::AmbientState::VICTORY,
- "music_victory");
- m_audioEventHandler->loadAmbientMusic(Engine::Core::AmbientState::DEFEAT,
- "music_defeat");
- qInfo() << "Audio mappings configured";
- } else {
- qWarning() << "Failed to initialize AudioEventHandler";
- }
- connect(m_cursorManager.get(), &CursorManager::modeChanged, this,
- &GameEngine::cursor_mode_changed);
- connect(m_cursorManager.get(), &CursorManager::globalCursorChanged, this,
- &GameEngine::global_cursor_changed);
- connect(m_selectionController.get(),
- &Game::Systems::SelectionController::selection_changed, this,
- &GameEngine::selected_units_changed);
- connect(m_selectionController.get(),
- &Game::Systems::SelectionController::selection_changed, this,
- &GameEngine::sync_selection_flags);
- connect(
- m_selectionController.get(),
- &Game::Systems::SelectionController::selection_model_refresh_requested,
- this, &GameEngine::selected_units_data_changed);
- connect(m_commandController.get(),
- &App::Controllers::CommandController::attack_targetSelected,
- [this]() {
- if (auto *sel_sys =
- m_world->get_system<Game::Systems::SelectionSystem>()) {
- const auto &sel = sel_sys->get_selected_units();
- if (!sel.empty()) {
- auto *cam = m_camera.get();
- auto *picking = m_pickingService.get();
- if ((cam != nullptr) && (picking != nullptr)) {
- Engine::Core::EntityID const target_id =
- Game::Systems::PickingService::pick_unit_first(
- 0.0F, 0.0F, *m_world, *cam, m_viewport.width,
- m_viewport.height, 0);
- if (target_id != 0) {
- App::Controllers::ActionVFX::spawnAttackArrow(m_world.get(),
- target_id);
- }
- }
- }
- }
- });
- connect(m_commandController.get(),
- &App::Controllers::CommandController::troopLimitReached, [this]() {
- set_error(
- "Maximum troop limit reached. Cannot produce more units.");
- });
- connect(m_commandController.get(),
- &App::Controllers::CommandController::hold_modeChanged, this,
- &GameEngine::hold_mode_changed);
- connect(this, SIGNAL(selected_units_changed()), m_selectedUnitsModel,
- SLOT(refresh()));
- connect(this, SIGNAL(selected_units_data_changed()), m_selectedUnitsModel,
- SLOT(refresh()));
- emit selected_units_changed();
- m_unit_died_subscription =
- Engine::Core::ScopedEventSubscription<Engine::Core::UnitDiedEvent>(
- [this](const Engine::Core::UnitDiedEvent &e) {
- on_unit_died(e);
- if (e.owner_id != m_runtime.local_owner_id) {
- int const individuals_per_unit =
- Game::Units::TroopConfig::instance().getIndividualsPerUnit(
- e.spawn_type);
- m_enemyTroopsDefeated += individuals_per_unit;
- emit enemy_troops_defeated_changed();
- }
- });
- m_unit_spawned_subscription =
- Engine::Core::ScopedEventSubscription<Engine::Core::UnitSpawnedEvent>(
- [this](const Engine::Core::UnitSpawnedEvent &e) {
- on_unit_spawned(e);
- });
- }
- GameEngine::~GameEngine() {
- if (m_audioEventHandler) {
- m_audioEventHandler->shutdown();
- }
- AudioSystem::getInstance().shutdown();
- qInfo() << "AudioSystem shut down";
- }
- void GameEngine::cleanup_opengl_resources() {
- qInfo() << "Cleaning up OpenGL resources...";
- QOpenGLContext *context = QOpenGLContext::currentContext();
- const bool has_valid_context = (context != nullptr);
- if (!has_valid_context) {
- qInfo() << "No valid OpenGL context, skipping OpenGL cleanup";
- }
- if (m_renderer && has_valid_context) {
- m_renderer->shutdown();
- qInfo() << "Renderer shut down";
- }
- m_passes.clear();
- m_ground.reset();
- m_terrain.reset();
- m_biome.reset();
- m_river.reset();
- m_road.reset();
- m_riverbank.reset();
- m_bridge.reset();
- m_fog.reset();
- m_stone.reset();
- m_plant.reset();
- m_pine.reset();
- m_olive.reset();
- m_firecamp.reset();
- m_renderer.reset();
- m_resources.reset();
- qInfo() << "OpenGL resources cleaned up";
- }
- void GameEngine::on_map_clicked(qreal sx, qreal sy) {
- if (m_window == nullptr) {
- return;
- }
- ensure_initialized();
- if (m_selectionController && m_camera) {
- m_selectionController->on_click_select(sx, sy, false, m_viewport.width,
- m_viewport.height, m_camera.get(),
- m_runtime.local_owner_id);
- }
- }
- void GameEngine::on_right_click(qreal sx, qreal sy) {
- if (m_window == nullptr) {
- return;
- }
- ensure_initialized();
- auto *selection_system =
- m_world->get_system<Game::Systems::SelectionSystem>();
- if (selection_system == nullptr) {
- return;
- }
- if (m_cursorManager->mode() == CursorMode::Patrol ||
- m_cursorManager->mode() == CursorMode::Attack) {
- set_cursor_mode(CursorMode::Normal);
- return;
- }
- const auto &sel = selection_system->get_selected_units();
- if (sel.empty()) {
- return;
- }
- if (m_pickingService && m_camera) {
- Engine::Core::EntityID const target_id = m_pickingService->pick_unit_first(
- float(sx), float(sy), *m_world, *m_camera, m_viewport.width,
- m_viewport.height, 0);
- if (target_id != 0U) {
- auto *target_entity = m_world->get_entity(target_id);
- if (target_entity != nullptr) {
- auto *target_unit =
- target_entity->get_component<Engine::Core::UnitComponent>();
- if (target_unit != nullptr) {
- bool const is_enemy =
- (target_unit->owner_id != m_runtime.local_owner_id);
- if (is_enemy) {
- Game::Systems::CommandService::attack_target(*m_world, sel,
- target_id, true);
- return;
- }
- }
- }
- }
- }
- if (m_pickingService && m_camera) {
- QVector3D hit;
- if (m_pickingService->screen_to_ground(QPointF(sx, sy), *m_camera,
- m_viewport.width, m_viewport.height,
- hit)) {
- auto targets = Game::Systems::FormationPlanner::spreadFormation(
- int(sel.size()), hit,
- Game::GameConfig::instance().gameplay().formation_spacing_default);
- Game::Systems::CommandService::MoveOptions opts;
- opts.group_move = sel.size() > 1;
- Game::Systems::CommandService::moveUnits(*m_world, sel, targets, opts);
- }
- }
- }
- void GameEngine::on_attack_click(qreal sx, qreal sy) {
- if (m_window == nullptr) {
- return;
- }
- ensure_initialized();
- if (!m_commandController || !m_camera) {
- return;
- }
- auto result = m_commandController->onAttackClick(
- sx, sy, m_viewport.width, m_viewport.height, m_camera.get());
- auto *selection_system =
- m_world->get_system<Game::Systems::SelectionSystem>();
- if ((selection_system == nullptr) || !m_pickingService || !m_camera ||
- !m_world) {
- return;
- }
- const auto &selected = selection_system->get_selected_units();
- if (!selected.empty()) {
- Engine::Core::EntityID const target_id = m_pickingService->pick_unit_first(
- float(sx), float(sy), *m_world, *m_camera, m_viewport.width,
- m_viewport.height, 0);
- if (target_id != 0) {
- auto *target_entity = m_world->get_entity(target_id);
- if (target_entity != nullptr) {
- auto *target_unit =
- target_entity->get_component<Engine::Core::UnitComponent>();
- if ((target_unit != nullptr) &&
- target_unit->owner_id != m_runtime.local_owner_id) {
- App::Controllers::ActionVFX::spawnAttackArrow(m_world.get(),
- target_id);
- }
- }
- }
- }
- if (result.resetCursorToNormal) {
- set_cursor_mode(CursorMode::Normal);
- }
- }
- void GameEngine::reset_movement(Engine::Core::Entity *entity) {
- App::Utils::reset_movement(entity);
- }
- void GameEngine::on_stop_command() {
- if (!m_commandController) {
- return;
- }
- ensure_initialized();
- auto result = m_commandController->onStopCommand();
- if (result.resetCursorToNormal) {
- set_cursor_mode(CursorMode::Normal);
- }
- }
- void GameEngine::on_hold_command() {
- if (!m_commandController) {
- return;
- }
- ensure_initialized();
- auto result = m_commandController->onHoldCommand();
- if (result.resetCursorToNormal) {
- set_cursor_mode(CursorMode::Normal);
- }
- }
- auto GameEngine::any_selected_in_hold_mode() const -> bool {
- if (!m_commandController) {
- return false;
- }
- return m_commandController->anySelectedInHoldMode();
- }
- void GameEngine::on_patrol_click(qreal sx, qreal sy) {
- if (!m_commandController || !m_camera) {
- return;
- }
- ensure_initialized();
- auto result = m_commandController->onPatrolClick(
- sx, sy, m_viewport.width, m_viewport.height, m_camera.get());
- if (result.resetCursorToNormal) {
- set_cursor_mode(CursorMode::Normal);
- }
- }
- void GameEngine::update_cursor(Qt::CursorShape newCursor) {
- if (m_window == nullptr) {
- return;
- }
- if (m_runtime.current_cursor != newCursor) {
- m_runtime.current_cursor = newCursor;
- m_window->setCursor(newCursor);
- }
- }
- void GameEngine::set_error(const QString &errorMessage) {
- if (m_runtime.last_error != errorMessage) {
- m_runtime.last_error = errorMessage;
- qCritical() << "GameEngine error:" << errorMessage;
- emit last_error_changed();
- }
- }
- void GameEngine::set_cursor_mode(CursorMode mode) {
- if (!m_cursorManager) {
- return;
- }
- m_cursorManager->setMode(mode);
- m_cursorManager->updateCursorShape(m_window);
- }
- void GameEngine::set_cursor_mode(const QString &mode) {
- set_cursor_mode(CursorModeUtils::fromString(mode));
- }
- auto GameEngine::cursor_mode() const -> QString {
- if (!m_cursorManager) {
- return "normal";
- }
- return m_cursorManager->modeString();
- }
- auto GameEngine::global_cursor_x() const -> qreal {
- if (!m_cursorManager) {
- return 0;
- }
- return m_cursorManager->global_cursor_x(m_window);
- }
- auto GameEngine::global_cursor_y() const -> qreal {
- if (!m_cursorManager) {
- return 0;
- }
- return m_cursorManager->global_cursor_y(m_window);
- }
- void GameEngine::set_hover_at_screen(qreal sx, qreal sy) {
- if (m_window == nullptr) {
- return;
- }
- ensure_initialized();
- if (!m_hoverTracker || !m_camera || !m_world) {
- return;
- }
- m_cursorManager->updateCursorShape(m_window);
- m_hoverTracker->update_hover(float(sx), float(sy), *m_world, *m_camera,
- m_viewport.width, m_viewport.height);
- }
- void GameEngine::on_click_select(qreal sx, qreal sy, bool additive) {
- if (m_window == nullptr) {
- return;
- }
- ensure_initialized();
- if (m_selectionController && m_camera) {
- m_selectionController->on_click_select(sx, sy, additive, m_viewport.width,
- m_viewport.height, m_camera.get(),
- m_runtime.local_owner_id);
- }
- }
- void GameEngine::on_area_selected(qreal x1, qreal y1, qreal x2, qreal y2,
- bool additive) {
- if (m_window == nullptr) {
- return;
- }
- ensure_initialized();
- if (m_selectionController && m_camera) {
- m_selectionController->on_area_selected(
- x1, y1, x2, y2, additive, m_viewport.width, m_viewport.height,
- m_camera.get(), m_runtime.local_owner_id);
- }
- }
- void GameEngine::select_all_troops() {
- ensure_initialized();
- if (m_selectionController) {
- m_selectionController->select_all_player_troops(m_runtime.local_owner_id);
- }
- }
- void GameEngine::select_unit_by_id(int unitId) {
- ensure_initialized();
- if (!m_selectionController || (unitId <= 0)) {
- return;
- }
- m_selectionController->select_single_unit(
- static_cast<Engine::Core::EntityID>(unitId), m_runtime.local_owner_id);
- }
- void GameEngine::ensure_initialized() {
- QString error;
- Game::Map::WorldBootstrap::ensure_initialized(
- m_runtime.initialized, *m_renderer, *m_camera, m_ground.get(), &error);
- if (!error.isEmpty()) {
- set_error(error);
- }
- }
- auto GameEngine::enemy_troops_defeated() const -> int {
- return m_enemyTroopsDefeated;
- }
- auto GameEngine::get_player_stats(int owner_id) -> QVariantMap {
- QVariantMap result;
- auto &stats_registry = Game::Systems::GlobalStatsRegistry::instance();
- const auto *stats = stats_registry.get_stats(owner_id);
- if (stats != nullptr) {
- result["troopsRecruited"] = stats->troops_recruited;
- result["enemiesKilled"] = stats->enemies_killed;
- result["barracksOwned"] = stats->barracks_owned;
- result["playTimeSec"] = stats->play_time_sec;
- result["gameEnded"] = stats->game_ended;
- } else {
- result["troopsRecruited"] = 0;
- result["enemiesKilled"] = 0;
- result["barracksOwned"] = 0;
- result["playTimeSec"] = 0.0F;
- result["gameEnded"] = false;
- }
- return result;
- }
- void GameEngine::update(float dt) {
- if (m_runtime.loading) {
- return;
- }
- if (m_runtime.paused) {
- dt = 0.0F;
- } else {
- dt *= m_runtime.time_scale;
- }
- if (!m_runtime.paused && !m_runtime.loading) {
- update_ambient_state(dt);
- }
- if (m_renderer) {
- m_renderer->update_animation_time(dt);
- }
- if (m_camera) {
- m_camera->update(dt);
- }
- if (m_world) {
- m_world->update(dt);
- auto &visibility_service = Game::Map::VisibilityService::instance();
- if (visibility_service.is_initialized()) {
- m_runtime.visibilityUpdateAccumulator += dt;
- const float visibility_update_interval =
- Game::GameConfig::instance().gameplay().visibility_update_interval;
- if (m_runtime.visibilityUpdateAccumulator >= visibility_update_interval) {
- m_runtime.visibilityUpdateAccumulator = 0.0F;
- visibility_service.update(*m_world, m_runtime.local_owner_id);
- }
- const auto new_version = visibility_service.version();
- if (new_version != m_runtime.visibilityVersion) {
- if (m_fog) {
- m_fog->updateMask(visibility_service.getWidth(),
- visibility_service.getHeight(),
- visibility_service.getTileSize(),
- visibility_service.snapshotCells());
- }
- m_runtime.visibilityVersion = new_version;
- }
- }
- update_minimap_fog(dt);
- }
- if (m_victoryService && m_world) {
- m_victoryService->update(*m_world, dt);
- }
- if (m_followSelectionEnabled && m_camera && m_world && m_cameraService) {
- m_cameraService->update_follow(*m_camera, *m_world,
- m_followSelectionEnabled);
- }
- if (m_selectedUnitsModel != nullptr) {
- auto *selection_system =
- m_world->get_system<Game::Systems::SelectionSystem>();
- if ((selection_system != nullptr) &&
- !selection_system->get_selected_units().empty()) {
- m_runtime.selectionRefreshCounter++;
- if (m_runtime.selectionRefreshCounter >= 15) {
- m_runtime.selectionRefreshCounter = 0;
- emit selected_units_data_changed();
- }
- }
- }
- }
- void GameEngine::render(int pixelWidth, int pixelHeight) {
- if (!m_renderer || !m_world || !m_runtime.initialized || m_runtime.loading) {
- return;
- }
- if (pixelWidth > 0 && pixelHeight > 0) {
- m_viewport.width = pixelWidth;
- m_viewport.height = pixelHeight;
- m_renderer->set_viewport(pixelWidth, pixelHeight);
- }
- if (auto *selection_system =
- m_world->get_system<Game::Systems::SelectionSystem>()) {
- const auto &sel = selection_system->get_selected_units();
- std::vector<unsigned int> const ids(sel.begin(), sel.end());
- m_renderer->set_selected_entities(ids);
- }
- m_renderer->begin_frame();
- if (auto *res = m_renderer->resources()) {
- for (auto *pass : m_passes) {
- if (pass != nullptr) {
- pass->submit(*m_renderer, res);
- }
- }
- }
- if (m_renderer && m_hoverTracker) {
- m_renderer->set_hovered_entity_id(m_hoverTracker->getLastHoveredEntity());
- }
- if (m_renderer) {
- m_renderer->set_local_owner_id(m_runtime.local_owner_id);
- }
- m_renderer->render_world(m_world.get());
- if (auto *arrow_system = m_world->get_system<Game::Systems::ArrowSystem>()) {
- if (auto *res = m_renderer->resources()) {
- Render::GL::renderArrows(m_renderer.get(), res, *arrow_system);
- }
- }
- if (auto *projectile_system =
- m_world->get_system<Game::Systems::ProjectileSystem>()) {
- if (auto *res = m_renderer->resources()) {
- Render::GL::render_projectiles(m_renderer.get(), res, *projectile_system);
- }
- }
- if (auto *res = m_renderer->resources()) {
- std::optional<QVector3D> preview_waypoint;
- if (m_commandController && m_commandController->hasPatrolFirstWaypoint()) {
- preview_waypoint = m_commandController->getPatrolFirstWaypoint();
- }
- Render::GL::renderPatrolFlags(m_renderer.get(), res, *m_world,
- preview_waypoint);
- }
- m_renderer->end_frame();
- qreal const current_x = global_cursor_x();
- qreal const current_y = global_cursor_y();
- if (current_x != m_runtime.lastCursorX ||
- current_y != m_runtime.lastCursorY) {
- m_runtime.lastCursorX = current_x;
- m_runtime.lastCursorY = current_y;
- emit global_cursor_changed();
- }
- }
- auto GameEngine::screen_to_ground(const QPointF &screenPt,
- QVector3D &outWorld) -> bool {
- return App::Utils::screen_to_ground(m_pickingService.get(), m_camera.get(),
- m_window, m_viewport.width,
- m_viewport.height, screenPt, outWorld);
- }
- auto GameEngine::world_to_screen(const QVector3D &world,
- QPointF &outScreen) const -> bool {
- return App::Utils::world_to_screen(m_pickingService.get(), m_camera.get(),
- m_window, m_viewport.width,
- m_viewport.height, world, outScreen);
- }
- void GameEngine::sync_selection_flags() {
- auto *selection_system =
- m_world->get_system<Game::Systems::SelectionSystem>();
- if (!m_world || (selection_system == nullptr)) {
- return;
- }
- App::Utils::sanitize_selection(m_world.get(), selection_system);
- if (selection_system->get_selected_units().empty()) {
- if (m_cursorManager && m_cursorManager->mode() != CursorMode::Normal) {
- set_cursor_mode(CursorMode::Normal);
- }
- }
- }
- void GameEngine::camera_move(float dx, float dz) {
- ensure_initialized();
- if (!m_camera || !m_cameraService) {
- return;
- }
- m_cameraService->move(*m_camera, dx, dz);
- }
- void GameEngine::camera_elevate(float dy) {
- ensure_initialized();
- if (!m_camera || !m_cameraService) {
- return;
- }
- m_cameraService->elevate(*m_camera, dy);
- }
- void GameEngine::reset_camera() {
- ensure_initialized();
- if (!m_camera || !m_world || !m_cameraService) {
- return;
- }
- m_cameraService->resetCamera(*m_camera, *m_world, m_runtime.local_owner_id,
- m_level.player_unit_id);
- }
- void GameEngine::camera_zoom(float delta) {
- ensure_initialized();
- if (!m_camera || !m_cameraService) {
- return;
- }
- m_cameraService->zoom(*m_camera, delta);
- }
- auto GameEngine::camera_distance() const -> float {
- if (!m_camera || !m_cameraService) {
- return 0.0F;
- }
- return m_cameraService->get_distance(*m_camera);
- }
- void GameEngine::camera_yaw(float degrees) {
- ensure_initialized();
- if (!m_camera || !m_cameraService) {
- return;
- }
- m_cameraService->yaw(*m_camera, degrees);
- }
- void GameEngine::camera_orbit(float yaw_deg, float pitch_deg) {
- ensure_initialized();
- if (!m_camera || !m_cameraService) {
- return;
- }
- if (!std::isfinite(yaw_deg) || !std::isfinite(pitch_deg)) {
- qWarning() << "GameEngine::camera_orbit received invalid input, ignoring:"
- << yaw_deg << pitch_deg;
- return;
- }
- m_cameraService->orbit(*m_camera, yaw_deg, pitch_deg);
- }
- void GameEngine::camera_orbit_direction(int direction, bool shift) {
- if (!m_camera || !m_cameraService) {
- return;
- }
- m_cameraService->orbit_direction(*m_camera, direction, shift);
- }
- void GameEngine::camera_follow_selection(bool enable) {
- ensure_initialized();
- m_followSelectionEnabled = enable;
- if (!m_camera || !m_world || !m_cameraService) {
- return;
- }
- m_cameraService->follow_selection(*m_camera, *m_world, enable);
- }
- void GameEngine::camera_set_follow_lerp(float alpha) {
- ensure_initialized();
- if (!m_camera || !m_cameraService) {
- return;
- }
- m_cameraService->set_follow_lerp(*m_camera, alpha);
- }
- auto GameEngine::selected_units_model() -> QAbstractItemModel * {
- return m_selectedUnitsModel;
- }
- auto GameEngine::audio_system() -> QObject * {
- return m_audio_systemProxy.get();
- }
- auto GameEngine::has_units_selected() const -> bool {
- if (!m_selectionController) {
- return false;
- }
- return m_selectionController->has_units_selected();
- }
- auto GameEngine::player_troop_count() const -> int {
- return m_entityCache.playerTroopCount;
- }
- auto GameEngine::has_selected_type(const QString &type) const -> bool {
- if (!m_selectionController) {
- return false;
- }
- return m_selectionController->has_selected_type(type);
- }
- void GameEngine::recruit_near_selected(const QString &unit_type) {
- ensure_initialized();
- if (!m_commandController) {
- return;
- }
- m_commandController->recruitNearSelected(unit_type, m_runtime.local_owner_id);
- }
- auto GameEngine::get_selected_production_state() const -> QVariantMap {
- QVariantMap m;
- m["has_barracks"] = false;
- m["in_progress"] = false;
- m["time_remaining"] = 0.0;
- m["build_time"] = 0.0;
- m["produced_count"] = 0;
- m["max_units"] = 0;
- m["villager_cost"] = 1;
- if (!m_world) {
- return m;
- }
- auto *selection_system =
- m_world->get_system<Game::Systems::SelectionSystem>();
- if (selection_system == nullptr) {
- return m;
- }
- Game::Systems::ProductionState st;
- Game::Systems::ProductionService::getSelectedBarracksState(
- *m_world, selection_system->get_selected_units(),
- m_runtime.local_owner_id, st);
- m["has_barracks"] = st.has_barracks;
- m["in_progress"] = st.in_progress;
- m["product_type"] =
- QString::fromStdString(Game::Units::troop_typeToString(st.product_type));
- m["time_remaining"] = st.time_remaining;
- m["build_time"] = st.build_time;
- m["produced_count"] = st.produced_count;
- m["max_units"] = st.max_units;
- m["villager_cost"] = st.villager_cost;
- m["queue_size"] = st.queue_size;
- m["nation_id"] =
- QString::fromStdString(Game::Systems::nationIDToString(st.nation_id));
- QVariantList queue_list;
- for (const auto &unit_type : st.production_queue) {
- queue_list.append(
- QString::fromStdString(Game::Units::troop_typeToString(unit_type)));
- }
- m["production_queue"] = queue_list;
- return m;
- }
- auto GameEngine::get_unit_production_info(const QString &unit_type) const
- -> QVariantMap {
- QVariantMap info;
- const auto &config = Game::Units::TroopConfig::instance();
- std::string type_str = unit_type.toStdString();
- info["cost"] = config.getProductionCost(type_str);
- info["build_time"] = static_cast<double>(config.getBuildTime(type_str));
- info["individuals_per_unit"] = config.getIndividualsPerUnit(type_str);
- return info;
- }
- auto GameEngine::get_selected_units_command_mode() const -> QString {
- if (!m_world) {
- return "normal";
- }
- auto *selection_system =
- m_world->get_system<Game::Systems::SelectionSystem>();
- if (selection_system == nullptr) {
- return "normal";
- }
- const auto &sel = selection_system->get_selected_units();
- if (sel.empty()) {
- return "normal";
- }
- int attacking_count = 0;
- int patrolling_count = 0;
- int total_units = 0;
- for (auto id : sel) {
- auto *e = m_world->get_entity(id);
- if (e == nullptr) {
- continue;
- }
- auto *u = e->get_component<Engine::Core::UnitComponent>();
- if (u == nullptr) {
- continue;
- }
- if (u->spawn_type == Game::Units::SpawnType::Barracks) {
- continue;
- }
- total_units++;
- if (e->get_component<Engine::Core::AttackTargetComponent>() != nullptr) {
- attacking_count++;
- }
- auto *patrol = e->get_component<Engine::Core::PatrolComponent>();
- if ((patrol != nullptr) && patrol->patrolling) {
- patrolling_count++;
- }
- }
- if (total_units == 0) {
- return "normal";
- }
- if (patrolling_count == total_units) {
- return "patrol";
- }
- if (attacking_count == total_units) {
- return "attack";
- }
- return "normal";
- }
- void GameEngine::set_rally_at_screen(qreal sx, qreal sy) {
- ensure_initialized();
- if (!m_commandController || !m_camera) {
- return;
- }
- m_commandController->setRallyAtScreen(sx, sy, m_viewport.width,
- m_viewport.height, m_camera.get(),
- m_runtime.local_owner_id);
- }
- void GameEngine::start_loading_maps() {
- m_available_maps.clear();
- if (m_mapCatalog) {
- m_mapCatalog->load_maps_async();
- }
- load_campaigns();
- }
- auto GameEngine::available_maps() const -> QVariantList {
- return m_available_maps;
- }
- auto GameEngine::available_nations() const -> QVariantList {
- QVariantList nations;
- const auto ®istry = Game::Systems::NationRegistry::instance();
- const auto &all = registry.get_all_nations();
- QList<QVariantMap> ordered;
- ordered.reserve(static_cast<int>(all.size()));
- for (const auto &nation : all) {
- QVariantMap entry;
- entry.insert(
- QStringLiteral("id"),
- QString::fromStdString(Game::Systems::nationIDToString(nation.id)));
- entry.insert(QStringLiteral("name"),
- QString::fromStdString(nation.display_name));
- ordered.append(entry);
- }
- std::sort(ordered.begin(), ordered.end(),
- [](const QVariantMap &a, const QVariantMap &b) {
- return a.value(QStringLiteral("name"))
- .toString()
- .localeAwareCompare(
- b.value(QStringLiteral("name")).toString()) < 0;
- });
- for (const auto &entry : ordered) {
- nations.append(entry);
- }
- return nations;
- }
- auto GameEngine::available_campaigns() const -> QVariantList {
- return m_available_campaigns;
- }
- void GameEngine::load_campaigns() {
- if (!m_saveLoadService) {
- return;
- }
- QString error;
- auto campaigns = m_saveLoadService->list_campaigns(&error);
- if (!error.isEmpty()) {
- qWarning() << "Failed to load campaigns:" << error;
- return;
- }
- m_available_campaigns = campaigns;
- emit available_campaigns_changed();
- }
- void GameEngine::start_campaign_mission(const QString &campaign_id) {
- clear_error();
- if (!m_saveLoadService) {
- set_error("Save/Load service not initialized");
- return;
- }
- QString error;
- auto campaigns = m_saveLoadService->list_campaigns(&error);
- if (!error.isEmpty()) {
- set_error("Failed to load campaign: " + error);
- return;
- }
- QVariantMap selectedCampaign;
- for (const auto &campaign : campaigns) {
- auto campaignMap = campaign.toMap();
- if (campaignMap.value("id").toString() == campaign_id) {
- selectedCampaign = campaignMap;
- break;
- }
- }
- if (selectedCampaign.isEmpty()) {
- set_error("Campaign not found: " + campaign_id);
- return;
- }
- m_current_campaign_id = campaign_id;
- QString mapPath = selectedCampaign.value("mapPath").toString();
- QVariantList playerConfigs;
- QVariantMap player1;
- player1.insert("player_id", 1);
- player1.insert("playerName", "Carthage");
- player1.insert("colorIndex", 0);
- player1.insert("team_id", 0);
- player1.insert("nationId", "carthage");
- player1.insert("isHuman", true);
- playerConfigs.append(player1);
- QVariantMap player2;
- player2.insert("player_id", 2);
- player2.insert("playerName", "Rome");
- player2.insert("colorIndex", 1);
- player2.insert("team_id", 1);
- player2.insert("nationId", "roman_republic");
- player2.insert("isHuman", false);
- playerConfigs.append(player2);
- start_skirmish(mapPath, playerConfigs);
- }
- void GameEngine::mark_current_mission_completed() {
- if (m_current_campaign_id.isEmpty()) {
- qWarning() << "No active campaign mission to mark as completed";
- return;
- }
- if (!m_saveLoadService) {
- qWarning() << "Save/Load service not initialized";
- return;
- }
- QString error;
- bool success =
- m_saveLoadService->mark_campaign_completed(m_current_campaign_id, &error);
- if (!success) {
- qWarning() << "Failed to mark campaign as completed:" << error;
- } else {
- qInfo() << "Campaign mission" << m_current_campaign_id
- << "marked as completed";
- load_campaigns();
- }
- }
- void GameEngine::start_skirmish(const QString &map_path,
- const QVariantList &playerConfigs) {
- clear_error();
- m_level.map_path = map_path;
- m_level.map_name = map_path;
- if (!m_runtime.victory_state.isEmpty()) {
- m_runtime.victory_state = "";
- emit victory_state_changed();
- }
- if (m_victoryService) {
- m_victoryService->reset();
- }
- m_enemyTroopsDefeated = 0;
- if (!m_runtime.initialized) {
- ensure_initialized();
- return;
- }
- if (m_world && m_renderer && m_camera) {
- m_runtime.loading = true;
- if (m_hoverTracker) {
- m_hoverTracker->update_hover(-1, -1, *m_world, *m_camera, 0, 0);
- }
- m_entityCache.reset();
- Game::Map::SkirmishLoader loader(*m_world, *m_renderer, *m_camera);
- loader.set_ground_renderer(m_ground.get());
- loader.set_terrain_renderer(m_terrain.get());
- loader.set_biome_renderer(m_biome.get());
- loader.set_river_renderer(m_river.get());
- loader.set_road_renderer(m_road.get());
- loader.set_riverbank_renderer(m_riverbank.get());
- loader.set_bridge_renderer(m_bridge.get());
- loader.set_fog_renderer(m_fog.get());
- loader.set_stone_renderer(m_stone.get());
- loader.set_plant_renderer(m_plant.get());
- loader.set_pine_renderer(m_pine.get());
- loader.set_olive_renderer(m_olive.get());
- loader.set_fire_camp_renderer(m_firecamp.get());
- loader.set_on_owners_updated([this]() { emit owner_info_changed(); });
- loader.set_on_visibility_mask_ready([this]() {
- m_runtime.visibilityVersion =
- Game::Map::VisibilityService::instance().version();
- m_runtime.visibilityUpdateAccumulator = 0.0F;
- });
- int updated_player_id = m_selected_player_id;
- auto result = loader.start(map_path, playerConfigs, m_selected_player_id,
- updated_player_id);
- if (updated_player_id != m_selected_player_id) {
- m_selected_player_id = updated_player_id;
- emit selected_player_id_changed();
- }
- if (!result.ok && !result.errorMessage.isEmpty()) {
- set_error(result.errorMessage);
- }
- m_runtime.local_owner_id = updated_player_id;
- m_level.map_name = result.map_name;
- m_level.player_unit_id = result.player_unit_id;
- m_level.cam_fov = result.cam_fov;
- m_level.cam_near = result.cam_near;
- m_level.cam_far = result.cam_far;
- m_level.max_troops_per_player = result.max_troops_per_player;
- Game::GameConfig::instance().set_max_troops_per_player(
- result.max_troops_per_player);
- if (m_victoryService) {
- m_victoryService->configure(result.victoryConfig,
- m_runtime.local_owner_id);
- m_victoryService->setVictoryCallback([this](const QString &state) {
- if (m_runtime.victory_state != state) {
- m_runtime.victory_state = state;
- emit victory_state_changed();
- if (state == "victory" && !m_current_campaign_id.isEmpty()) {
- mark_current_mission_completed();
- }
- }
- });
- }
- if (result.has_focus_position && m_camera) {
- const auto &cam_config = Game::GameConfig::instance().camera();
- m_camera->setRTSView(result.focusPosition, cam_config.default_distance,
- cam_config.default_pitch, cam_config.default_yaw);
- }
- Game::Map::MapDefinition map_def;
- QString map_error;
- if (Game::Map::MapLoader::loadFromJsonFile(map_path, map_def, &map_error)) {
- generate_minimap_for_map(map_def);
- } else {
- qWarning() << "GameEngine: Failed to load map for minimap generation:"
- << map_error;
- }
- m_runtime.loading = false;
- if (auto *ai_system = m_world->get_system<Game::Systems::AISystem>()) {
- ai_system->reinitialize();
- }
- rebuild_entity_cache();
- auto &troops = Game::Systems::TroopCountRegistry::instance();
- troops.rebuild_from_world(*m_world);
- auto &stats_registry = Game::Systems::GlobalStatsRegistry::instance();
- stats_registry.rebuild_from_world(*m_world);
- auto &owner_registry = Game::Systems::OwnerRegistry::instance();
- const auto &all_owners = owner_registry.get_all_owners();
- for (const auto &owner : all_owners) {
- if (owner.type == Game::Systems::OwnerType::Player ||
- owner.type == Game::Systems::OwnerType::AI) {
- stats_registry.mark_game_start(owner.owner_id);
- }
- }
- m_currentAmbientState = Engine::Core::AmbientState::PEACEFUL;
- m_ambientCheckTimer = 0.0F;
- Engine::Core::EventManager::instance().publish(
- Engine::Core::AmbientStateChangedEvent(
- Engine::Core::AmbientState::PEACEFUL,
- Engine::Core::AmbientState::PEACEFUL));
- emit owner_info_changed();
- }
- }
- void GameEngine::open_settings() {
- if (m_saveLoadService) {
- m_saveLoadService->openSettings();
- }
- }
- void GameEngine::load_save() { load_from_slot("savegame"); }
- void GameEngine::save_game(const QString &filename) {
- save_to_slot(filename, filename);
- }
- void GameEngine::save_game_to_slot(const QString &slotName) {
- save_to_slot(slotName, slotName);
- }
- void GameEngine::load_game_from_slot(const QString &slotName) {
- load_from_slot(slotName);
- }
- auto GameEngine::load_from_slot(const QString &slot) -> bool {
- if (!m_saveLoadService || !m_world) {
- set_error("Load: not initialized");
- return false;
- }
- m_runtime.loading = true;
- if (!m_saveLoadService->load_game_from_slot(*m_world, slot)) {
- set_error(m_saveLoadService->getLastError());
- m_runtime.loading = false;
- return false;
- }
- const QJsonObject meta = m_saveLoadService->getLastMetadata();
- Game::Systems::GameStateSerializer::restoreLevelFromMetadata(meta, m_level);
- Game::Systems::GameStateSerializer::restoreCameraFromMetadata(
- meta, m_camera.get(), m_viewport.width, m_viewport.height);
- Game::Systems::RuntimeSnapshot runtime_snap = to_runtime_snapshot();
- Game::Systems::GameStateSerializer::restoreRuntimeFromMetadata(meta,
- runtime_snap);
- apply_runtime_snapshot(runtime_snap);
- restore_environment_from_metadata(meta);
- auto unit_reg = std::make_shared<Game::Units::UnitFactoryRegistry>();
- Game::Units::registerBuiltInUnits(*unit_reg);
- Game::Map::MapTransformer::setFactoryRegistry(unit_reg);
- qInfo() << "Factory registry reinitialized after loading saved game";
- rebuild_registries_after_load();
- rebuild_entity_cache();
- if (auto *ai_system = m_world->get_system<Game::Systems::AISystem>()) {
- qInfo() << "Reinitializing AI system after loading saved game";
- ai_system->reinitialize();
- }
- if (m_victoryService) {
- m_victoryService->configure(Game::Map::VictoryConfig(),
- m_runtime.local_owner_id);
- }
- m_runtime.loading = false;
- qInfo() << "Game load complete, victory/defeat checks re-enabled";
- emit selected_units_changed();
- emit owner_info_changed();
- return true;
- }
- auto GameEngine::save_to_slot(const QString &slot,
- const QString &title) -> bool {
- if (!m_saveLoadService || !m_world) {
- set_error("Save: not initialized");
- return false;
- }
- Game::Systems::RuntimeSnapshot const runtime_snap = to_runtime_snapshot();
- QJsonObject meta = Game::Systems::GameStateSerializer::buildMetadata(
- *m_world, m_camera.get(), m_level, runtime_snap);
- meta["title"] = title;
- const QByteArray screenshot = capture_screenshot();
- if (!m_saveLoadService->saveGameToSlot(*m_world, slot, title,
- m_level.map_name, meta, screenshot)) {
- set_error(m_saveLoadService->getLastError());
- return false;
- }
- emit save_slots_changed();
- return true;
- }
- auto GameEngine::get_save_slots() const -> QVariantList {
- if (!m_saveLoadService) {
- qWarning() << "Cannot get save slots: service not initialized";
- return {};
- }
- return m_saveLoadService->get_save_slots();
- }
- void GameEngine::refresh_save_slots() { emit save_slots_changed(); }
- auto GameEngine::delete_save_slot(const QString &slotName) -> bool {
- if (!m_saveLoadService) {
- qWarning() << "Cannot delete save slot: service not initialized";
- return false;
- }
- bool const success = m_saveLoadService->deleteSaveSlot(slotName);
- if (!success) {
- QString const error = m_saveLoadService->getLastError();
- qWarning() << "Failed to delete save slot:" << error;
- set_error(error);
- } else {
- emit save_slots_changed();
- }
- return success;
- }
- void GameEngine::exit_game() {
- if (m_saveLoadService) {
- m_saveLoadService->exitGame();
- }
- }
- auto GameEngine::get_owner_info() const -> QVariantList {
- QVariantList result;
- const auto &owner_registry = Game::Systems::OwnerRegistry::instance();
- const auto &owners = owner_registry.get_all_owners();
- for (const auto &owner : owners) {
- QVariantMap owner_map;
- owner_map["id"] = owner.owner_id;
- owner_map["name"] = QString::fromStdString(owner.name);
- owner_map["team_id"] = owner.team_id;
- QString type_str;
- switch (owner.type) {
- case Game::Systems::OwnerType::Player:
- type_str = "Player";
- break;
- case Game::Systems::OwnerType::AI:
- type_str = "AI";
- break;
- case Game::Systems::OwnerType::Neutral:
- type_str = "Neutral";
- break;
- }
- owner_map["type"] = type_str;
- owner_map["isLocal"] = (owner.owner_id == m_runtime.local_owner_id);
- result.append(owner_map);
- }
- return result;
- }
- void GameEngine::get_selected_unit_ids(
- std::vector<Engine::Core::EntityID> &out) const {
- out.clear();
- if (!m_selectionController) {
- return;
- }
- m_selectionController->get_selected_unit_ids(out);
- }
- auto GameEngine::get_unit_info(Engine::Core::EntityID id, QString &name,
- int &health, int &max_health, bool &isBuilding,
- bool &alive, QString &nation) const -> bool {
- if (!m_world) {
- return false;
- }
- auto *e = m_world->get_entity(id);
- if (e == nullptr) {
- return false;
- }
- isBuilding = e->has_component<Engine::Core::BuildingComponent>();
- if (auto *u = e->get_component<Engine::Core::UnitComponent>()) {
- name =
- QString::fromStdString(Game::Units::spawn_typeToString(u->spawn_type));
- health = u->health;
- max_health = u->max_health;
- alive = (u->health > 0);
- nation = Game::Systems::nationIDToQString(u->nation_id);
- return true;
- }
- name = QStringLiteral("Entity");
- health = max_health = 0;
- alive = true;
- nation = QStringLiteral("");
- return true;
- }
- void GameEngine::on_unit_spawned(const Engine::Core::UnitSpawnedEvent &event) {
- auto &owners = Game::Systems::OwnerRegistry::instance();
- if (event.owner_id == m_runtime.local_owner_id) {
- if (event.spawn_type == Game::Units::SpawnType::Barracks) {
- m_entityCache.playerBarracksAlive = true;
- } else {
- int const production_cost =
- Game::Units::TroopConfig::instance().getProductionCost(
- event.spawn_type);
- m_entityCache.playerTroopCount += production_cost;
- }
- } else if (owners.is_ai(event.owner_id)) {
- if (event.spawn_type == Game::Units::SpawnType::Barracks) {
- m_entityCache.enemyBarracksCount++;
- m_entityCache.enemyBarracksAlive = true;
- }
- }
- auto emit_if_changed = [&] {
- if (m_entityCache.playerTroopCount != m_runtime.lastTroopCount) {
- m_runtime.lastTroopCount = m_entityCache.playerTroopCount;
- emit troop_count_changed();
- }
- };
- emit_if_changed();
- }
- void GameEngine::on_unit_died(const Engine::Core::UnitDiedEvent &event) {
- auto &owners = Game::Systems::OwnerRegistry::instance();
- if (event.owner_id == m_runtime.local_owner_id) {
- if (event.spawn_type == Game::Units::SpawnType::Barracks) {
- m_entityCache.playerBarracksAlive = false;
- } else {
- int const production_cost =
- Game::Units::TroopConfig::instance().getProductionCost(
- event.spawn_type);
- m_entityCache.playerTroopCount -= production_cost;
- m_entityCache.playerTroopCount =
- std::max(0, m_entityCache.playerTroopCount);
- }
- } else if (owners.is_ai(event.owner_id)) {
- if (event.spawn_type == Game::Units::SpawnType::Barracks) {
- m_entityCache.enemyBarracksCount--;
- m_entityCache.enemyBarracksCount =
- std::max(0, m_entityCache.enemyBarracksCount);
- m_entityCache.enemyBarracksAlive = (m_entityCache.enemyBarracksCount > 0);
- }
- }
- sync_selection_flags();
- auto emit_if_changed = [&] {
- if (m_entityCache.playerTroopCount != m_runtime.lastTroopCount) {
- m_runtime.lastTroopCount = m_entityCache.playerTroopCount;
- emit troop_count_changed();
- }
- };
- emit_if_changed();
- }
- void GameEngine::rebuild_entity_cache() {
- if (!m_world) {
- m_entityCache.reset();
- return;
- }
- m_entityCache.reset();
- auto &owners = Game::Systems::OwnerRegistry::instance();
- auto entities = m_world->get_entities_with<Engine::Core::UnitComponent>();
- for (auto *e : entities) {
- auto *unit = e->get_component<Engine::Core::UnitComponent>();
- if ((unit == nullptr) || unit->health <= 0) {
- continue;
- }
- if (unit->owner_id == m_runtime.local_owner_id) {
- if (unit->spawn_type == Game::Units::SpawnType::Barracks) {
- m_entityCache.playerBarracksAlive = true;
- } else {
- int const production_cost =
- Game::Units::TroopConfig::instance().getProductionCost(
- unit->spawn_type);
- m_entityCache.playerTroopCount += production_cost;
- }
- } else if (owners.is_ai(unit->owner_id)) {
- if (unit->spawn_type == Game::Units::SpawnType::Barracks) {
- m_entityCache.enemyBarracksCount++;
- m_entityCache.enemyBarracksAlive = true;
- }
- }
- }
- auto emit_if_changed = [&] {
- if (m_entityCache.playerTroopCount != m_runtime.lastTroopCount) {
- m_runtime.lastTroopCount = m_entityCache.playerTroopCount;
- emit troop_count_changed();
- }
- };
- emit_if_changed();
- }
- void GameEngine::rebuild_registries_after_load() {
- if (!m_world) {
- return;
- }
- auto &owner_registry = Game::Systems::OwnerRegistry::instance();
- m_runtime.local_owner_id = owner_registry.get_local_player_id();
- auto &troops = Game::Systems::TroopCountRegistry::instance();
- troops.rebuild_from_world(*m_world);
- auto &stats_registry = Game::Systems::GlobalStatsRegistry::instance();
- stats_registry.rebuild_from_world(*m_world);
- const auto &all_owners = owner_registry.get_all_owners();
- for (const auto &owner : all_owners) {
- if (owner.type == Game::Systems::OwnerType::Player ||
- owner.type == Game::Systems::OwnerType::AI) {
- stats_registry.mark_game_start(owner.owner_id);
- }
- }
- rebuild_building_collisions();
- m_level.player_unit_id = 0;
- auto units = m_world->get_entities_with<Engine::Core::UnitComponent>();
- for (auto *entity : units) {
- auto *unit = entity->get_component<Engine::Core::UnitComponent>();
- if (unit == nullptr) {
- continue;
- }
- if (unit->owner_id == m_runtime.local_owner_id) {
- m_level.player_unit_id = entity->get_id();
- break;
- }
- }
- if (m_selected_player_id != m_runtime.local_owner_id) {
- m_selected_player_id = m_runtime.local_owner_id;
- emit selected_player_id_changed();
- }
- }
- void GameEngine::rebuild_building_collisions() {
- auto ®istry = Game::Systems::BuildingCollisionRegistry::instance();
- registry.clear();
- if (!m_world) {
- return;
- }
- auto buildings =
- m_world->get_entities_with<Engine::Core::BuildingComponent>();
- for (auto *entity : buildings) {
- auto *transform = entity->get_component<Engine::Core::TransformComponent>();
- auto *unit = entity->get_component<Engine::Core::UnitComponent>();
- if ((transform == nullptr) || (unit == nullptr)) {
- continue;
- }
- registry.register_building(
- entity->get_id(), Game::Units::spawn_typeToString(unit->spawn_type),
- transform->position.x, transform->position.z, unit->owner_id);
- }
- }
- auto GameEngine::to_runtime_snapshot() const -> Game::Systems::RuntimeSnapshot {
- Game::Systems::RuntimeSnapshot snap;
- snap.paused = m_runtime.paused;
- snap.time_scale = m_runtime.time_scale;
- snap.local_owner_id = m_runtime.local_owner_id;
- snap.victory_state = m_runtime.victory_state;
- snap.cursor_mode = CursorModeUtils::toInt(m_runtime.cursor_mode);
- snap.selected_player_id = m_selected_player_id;
- snap.follow_selection = m_followSelectionEnabled;
- return snap;
- }
- void GameEngine::apply_runtime_snapshot(
- const Game::Systems::RuntimeSnapshot &snapshot) {
- m_runtime.local_owner_id = snapshot.local_owner_id;
- set_paused(snapshot.paused);
- set_game_speed(snapshot.time_scale);
- if (snapshot.victory_state != m_runtime.victory_state) {
- m_runtime.victory_state = snapshot.victory_state;
- emit victory_state_changed();
- }
- set_cursor_mode(CursorModeUtils::fromInt(snapshot.cursor_mode));
- if (snapshot.selected_player_id != m_selected_player_id) {
- m_selected_player_id = snapshot.selected_player_id;
- emit selected_player_id_changed();
- }
- if (snapshot.follow_selection != m_followSelectionEnabled) {
- m_followSelectionEnabled = snapshot.follow_selection;
- if (m_camera && m_cameraService && m_world) {
- m_cameraService->follow_selection(*m_camera, *m_world,
- m_followSelectionEnabled);
- }
- }
- }
- auto GameEngine::capture_screenshot() const -> QByteArray {
- if (m_window == nullptr) {
- return {};
- }
- QImage const image = m_window->grabWindow();
- if (image.isNull()) {
- return {};
- }
- const QSize target_size(320, 180);
- QImage const scaled =
- image.scaled(target_size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- QByteArray buffer;
- QBuffer q_buffer(&buffer);
- if (!q_buffer.open(QIODevice::WriteOnly)) {
- return {};
- }
- if (!scaled.save(&q_buffer, "PNG")) {
- return {};
- }
- return buffer;
- }
- void GameEngine::restore_environment_from_metadata(
- const QJsonObject &metadata) {
- if (!m_world) {
- return;
- }
- const auto fallback_grid_width = metadata.value("grid_width").toInt(50);
- const auto fallback_grid_height = metadata.value("grid_height").toInt(50);
- const float fallback_tile_size =
- static_cast<float>(metadata.value("tile_size").toDouble(1.0));
- auto &terrain_service = Game::Map::TerrainService::instance();
- bool const terrain_already_restored = terrain_service.is_initialized();
- Game::Map::MapDefinition def;
- QString map_error;
- bool loaded_definition = false;
- const QString &map_path = m_level.map_path;
- if (!terrain_already_restored && !map_path.isEmpty()) {
- loaded_definition =
- Game::Map::MapLoader::loadFromJsonFile(map_path, def, &map_error);
- if (!loaded_definition) {
- qWarning() << "GameEngine: Failed to load map definition from" << map_path
- << "during save load:" << map_error;
- }
- }
- if (!terrain_already_restored && loaded_definition) {
- terrain_service.initialize(def);
- if (!def.name.isEmpty()) {
- m_level.map_name = def.name;
- }
- m_level.cam_fov = def.camera.fovY;
- m_level.cam_near = def.camera.near_plane;
- m_level.cam_far = def.camera.far_plane;
- }
- if (m_renderer && m_camera) {
- if (loaded_definition) {
- Game::Map::Environment::apply(def, *m_renderer, *m_camera);
- generate_minimap_for_map(def);
- } else {
- Game::Map::Environment::applyDefault(*m_renderer, *m_camera);
- }
- }
- if (terrain_service.is_initialized()) {
- const auto *height_map = terrain_service.get_height_map();
- const int grid_width =
- (height_map != nullptr) ? height_map->getWidth() : fallback_grid_width;
- const int grid_height = (height_map != nullptr) ? height_map->getHeight()
- : fallback_grid_height;
- const float tile_size = (height_map != nullptr) ? height_map->getTileSize()
- : fallback_tile_size;
- if (m_ground) {
- m_ground->configure(tile_size, grid_width, grid_height);
- m_ground->setBiome(terrain_service.biome_settings());
- }
- if (height_map != nullptr) {
- if (m_terrain) {
- m_terrain->configure(*height_map, terrain_service.biome_settings());
- }
- if (m_river) {
- m_river->configure(height_map->getRiverSegments(),
- height_map->getTileSize());
- }
- if (m_road) {
- m_road->configure(terrain_service.road_segments(),
- height_map->getTileSize());
- }
- if (m_riverbank) {
- m_riverbank->configure(height_map->getRiverSegments(), *height_map);
- }
- if (m_bridge) {
- m_bridge->configure(height_map->getBridges(),
- height_map->getTileSize());
- }
- if (m_biome) {
- m_biome->configure(*height_map, terrain_service.biome_settings());
- m_biome->refreshGrass();
- }
- if (m_stone) {
- m_stone->configure(*height_map, terrain_service.biome_settings());
- }
- if (m_plant) {
- m_plant->configure(*height_map, terrain_service.biome_settings());
- }
- if (m_pine) {
- m_pine->configure(*height_map, terrain_service.biome_settings());
- }
- if (m_olive) {
- m_olive->configure(*height_map, terrain_service.biome_settings());
- }
- if (m_firecamp) {
- m_firecamp->configure(*height_map, terrain_service.biome_settings());
- }
- }
- Game::Systems::CommandService::initialize(grid_width, grid_height);
- auto &visibility_service = Game::Map::VisibilityService::instance();
- visibility_service.initialize(grid_width, grid_height, tile_size);
- visibility_service.computeImmediate(*m_world, m_runtime.local_owner_id);
- if (m_fog && visibility_service.is_initialized()) {
- m_fog->updateMask(
- visibility_service.getWidth(), visibility_service.getHeight(),
- visibility_service.getTileSize(), visibility_service.snapshotCells());
- }
- m_runtime.visibilityVersion = visibility_service.version();
- m_runtime.visibilityUpdateAccumulator = 0.0F;
- } else {
- if (m_renderer && m_camera) {
- Game::Map::Environment::applyDefault(*m_renderer, *m_camera);
- }
- Game::Map::MapDefinition fallback_def;
- fallback_def.grid.width = fallback_grid_width;
- fallback_def.grid.height = fallback_grid_height;
- fallback_def.grid.tile_size = fallback_tile_size;
- fallback_def.max_troops_per_player = m_level.max_troops_per_player;
- terrain_service.initialize(fallback_def);
- if (m_ground) {
- m_ground->configure(fallback_tile_size, fallback_grid_width,
- fallback_grid_height);
- }
- Game::Systems::CommandService::initialize(fallback_grid_width,
- fallback_grid_height);
- auto &visibility_service = Game::Map::VisibilityService::instance();
- visibility_service.initialize(fallback_grid_width, fallback_grid_height,
- fallback_tile_size);
- visibility_service.computeImmediate(*m_world, m_runtime.local_owner_id);
- if (m_fog && visibility_service.is_initialized()) {
- m_fog->updateMask(
- visibility_service.getWidth(), visibility_service.getHeight(),
- visibility_service.getTileSize(), visibility_service.snapshotCells());
- }
- m_runtime.visibilityVersion = visibility_service.version();
- m_runtime.visibilityUpdateAccumulator = 0.0F;
- }
- }
- auto GameEngine::has_patrol_preview_waypoint() const -> bool {
- return m_commandController && m_commandController->hasPatrolFirstWaypoint();
- }
- auto GameEngine::get_patrol_preview_waypoint() const -> QVector3D {
- if (!m_commandController) {
- return {};
- }
- return m_commandController->getPatrolFirstWaypoint();
- }
- void GameEngine::update_ambient_state(float dt) {
- m_ambientCheckTimer += dt;
- const float check_interval = 2.0F;
- if (m_ambientCheckTimer < check_interval) {
- return;
- }
- m_ambientCheckTimer = 0.0F;
- Engine::Core::AmbientState new_state = Engine::Core::AmbientState::PEACEFUL;
- if (!m_runtime.victory_state.isEmpty()) {
- if (m_runtime.victory_state == "victory") {
- new_state = Engine::Core::AmbientState::VICTORY;
- } else if (m_runtime.victory_state == "defeat") {
- new_state = Engine::Core::AmbientState::DEFEAT;
- }
- } else if (is_player_in_combat()) {
- new_state = Engine::Core::AmbientState::COMBAT;
- } else if (m_entityCache.enemyBarracksAlive &&
- m_entityCache.playerBarracksAlive) {
- new_state = Engine::Core::AmbientState::TENSE;
- }
- if (new_state != m_currentAmbientState) {
- Engine::Core::AmbientState const previous_state = m_currentAmbientState;
- m_currentAmbientState = new_state;
- Engine::Core::EventManager::instance().publish(
- Engine::Core::AmbientStateChangedEvent(new_state, previous_state));
- qInfo() << "Ambient state changed from" << static_cast<int>(previous_state)
- << "to" << static_cast<int>(new_state);
- }
- }
- auto GameEngine::is_player_in_combat() const -> bool {
- if (!m_world) {
- return false;
- }
- auto units = m_world->get_entities_with<Engine::Core::UnitComponent>();
- const float combat_check_radius = 15.0F;
- for (auto *entity : units) {
- auto *unit = entity->get_component<Engine::Core::UnitComponent>();
- if ((unit == nullptr) || unit->owner_id != m_runtime.local_owner_id ||
- unit->health <= 0) {
- continue;
- }
- if (entity->has_component<Engine::Core::AttackTargetComponent>()) {
- return true;
- }
- auto *transform = entity->get_component<Engine::Core::TransformComponent>();
- if (transform == nullptr) {
- continue;
- }
- for (auto *other_entity : units) {
- auto *other_unit =
- other_entity->get_component<Engine::Core::UnitComponent>();
- if ((other_unit == nullptr) ||
- other_unit->owner_id == m_runtime.local_owner_id ||
- other_unit->health <= 0) {
- continue;
- }
- auto *other_transform =
- other_entity->get_component<Engine::Core::TransformComponent>();
- if (other_transform == nullptr) {
- continue;
- }
- float const dx = transform->position.x - other_transform->position.x;
- float const dz = transform->position.z - other_transform->position.z;
- float const dist_sq = dx * dx + dz * dz;
- if (dist_sq < combat_check_radius * combat_check_radius) {
- return true;
- }
- }
- }
- return false;
- }
- void GameEngine::load_audio_resources() {
- auto &audio_sys = AudioSystem::getInstance();
- QString const base_path =
- QCoreApplication::applicationDirPath() + "/assets/audio/";
- qInfo() << "Loading audio resources from:" << base_path;
- QDir const audio_dir(base_path);
- if (!audio_dir.exists()) {
- qWarning() << "Audio assets directory does not exist:" << base_path;
- qWarning() << "Application directory:"
- << QCoreApplication::applicationDirPath();
- return;
- }
- if (audio_sys.loadSound("archer_voice",
- (base_path + "voices/archer_voice.wav").toStdString(),
- AudioCategory::VOICE)) {
- qInfo() << "Loaded archer voice";
- } else {
- qWarning() << "Failed to load archer voice from:"
- << (base_path + "voices/archer_voice.wav");
- }
- if (audio_sys.loadSound(
- "swordsman_voice",
- (base_path + "voices/swordsman_voice.wav").toStdString(),
- AudioCategory::VOICE)) {
- qInfo() << "Loaded swordsman voice";
- } else {
- qWarning() << "Failed to load swordsman voice from:"
- << (base_path + "voices/swordsman_voice.wav");
- }
- if (audio_sys.loadSound(
- "spearman_voice",
- (base_path + "voices/spearman_voice.wav").toStdString(),
- AudioCategory::VOICE)) {
- qInfo() << "Loaded spearman voice";
- } else {
- qWarning() << "Failed to load spearman voice from:"
- << (base_path + "voices/spearman_voice.wav");
- }
- if (audio_sys.loadMusic("music_peaceful",
- (base_path + "music/peaceful.wav").toStdString())) {
- qInfo() << "Loaded peaceful music";
- } else {
- qWarning() << "Failed to load peaceful music from:"
- << (base_path + "music/peaceful.wav");
- }
- if (audio_sys.loadMusic("music_tense",
- (base_path + "music/tense.wav").toStdString())) {
- qInfo() << "Loaded tense music";
- } else {
- qWarning() << "Failed to load tense music from:"
- << (base_path + "music/tense.wav");
- }
- if (audio_sys.loadMusic("music_combat",
- (base_path + "music/combat.wav").toStdString())) {
- qInfo() << "Loaded combat music";
- } else {
- qWarning() << "Failed to load combat music from:"
- << (base_path + "music/combat.wav");
- }
- if (audio_sys.loadMusic("music_victory",
- (base_path + "music/victory.wav").toStdString())) {
- qInfo() << "Loaded victory music";
- } else {
- qWarning() << "Failed to load victory music from:"
- << (base_path + "music/victory.wav");
- }
- if (audio_sys.loadMusic("music_defeat",
- (base_path + "music/defeat.wav").toStdString())) {
- qInfo() << "Loaded defeat music";
- } else {
- qWarning() << "Failed to load defeat music from:"
- << (base_path + "music/defeat.wav");
- }
- qInfo() << "Audio resources loading complete";
- }
- auto GameEngine::minimap_image() const -> QImage { return m_minimap_image; }
- void GameEngine::generate_minimap_for_map(
- const Game::Map::MapDefinition &map_def) {
- Game::Map::Minimap::MinimapGenerator generator;
- m_minimap_base_image = generator.generate(map_def);
- if (!m_minimap_base_image.isNull()) {
- qDebug() << "GameEngine: Generated minimap of size"
- << m_minimap_base_image.width() << "x"
- << m_minimap_base_image.height();
- m_world_width = static_cast<float>(map_def.grid.width);
- m_world_height = static_cast<float>(map_def.grid.height);
- m_unit_layer = std::make_unique<Game::Map::Minimap::UnitLayer>();
- m_unit_layer->init(m_minimap_base_image.width(),
- m_minimap_base_image.height(), m_world_width,
- m_world_height);
- qDebug() << "GameEngine: Initialized unit layer for world" << m_world_width
- << "x" << m_world_height;
- m_minimap_fog_version = 0;
- m_minimap_update_timer = MINIMAP_UPDATE_INTERVAL;
- update_minimap_fog(0.0F);
- } else {
- qWarning() << "GameEngine: Failed to generate minimap";
- }
- }
- void GameEngine::update_minimap_fog(float dt) {
- if (m_minimap_base_image.isNull()) {
- return;
- }
- m_minimap_update_timer += dt;
- if (m_minimap_update_timer < MINIMAP_UPDATE_INTERVAL) {
- return;
- }
- m_minimap_update_timer = 0.0F;
- auto &visibility_service = Game::Map::VisibilityService::instance();
- if (!visibility_service.is_initialized()) {
- if (m_minimap_image != m_minimap_base_image) {
- m_minimap_image = m_minimap_base_image;
- emit minimap_image_changed();
- }
- return;
- }
- const auto current_version = visibility_service.version();
- if (current_version == m_minimap_fog_version && !m_minimap_image.isNull()) {
- update_minimap_units();
- return;
- }
- m_minimap_fog_version = current_version;
- const int vis_width = visibility_service.getWidth();
- const int vis_height = visibility_service.getHeight();
- const auto cells = visibility_service.snapshotCells();
- if (cells.empty() || vis_width <= 0 || vis_height <= 0) {
- m_minimap_image = m_minimap_base_image;
- emit minimap_image_changed();
- return;
- }
- m_minimap_image = m_minimap_base_image.copy();
- const int img_width = m_minimap_image.width();
- const int img_height = m_minimap_image.height();
- constexpr float k_inv_cos = -0.70710678118F;
- constexpr float k_inv_sin = 0.70710678118F;
- const float scale_x =
- static_cast<float>(vis_width) / static_cast<float>(img_width);
- const float scale_y =
- static_cast<float>(vis_height) / static_cast<float>(img_height);
- constexpr int FOG_R = 45;
- constexpr int FOG_G = 38;
- constexpr int FOG_B = 30;
- constexpr int ALPHA_UNSEEN = 180;
- constexpr int ALPHA_EXPLORED = 60;
- constexpr int ALPHA_VISIBLE = 0;
- constexpr float ALPHA_THRESHOLD = 0.5F;
- constexpr float ALPHA_SCALE = 1.0F / 255.0F;
- auto get_alpha = [&cells, vis_width, ALPHA_VISIBLE, ALPHA_EXPLORED,
- ALPHA_UNSEEN](int vx, int vy) -> float {
- const size_t idx = static_cast<size_t>(vy * vis_width + vx);
- if (idx >= cells.size()) {
- return static_cast<float>(ALPHA_UNSEEN);
- }
- const auto state = static_cast<Game::Map::VisibilityState>(cells[idx]);
- switch (state) {
- case Game::Map::VisibilityState::Visible:
- return static_cast<float>(ALPHA_VISIBLE);
- case Game::Map::VisibilityState::Explored:
- return static_cast<float>(ALPHA_EXPLORED);
- default:
- return static_cast<float>(ALPHA_UNSEEN);
- }
- };
- const float half_img_w = static_cast<float>(img_width) * 0.5F;
- const float half_img_h = static_cast<float>(img_height) * 0.5F;
- const float half_vis_w = static_cast<float>(vis_width) * 0.5F;
- const float half_vis_h = static_cast<float>(vis_height) * 0.5F;
- for (int y = 0; y < img_height; ++y) {
- auto *scanline = reinterpret_cast<QRgb *>(m_minimap_image.scanLine(y));
- for (int x = 0; x < img_width; ++x) {
- const float centered_x = static_cast<float>(x) - half_img_w;
- const float centered_y = static_cast<float>(y) - half_img_h;
- const float world_x = centered_x * k_inv_cos - centered_y * k_inv_sin;
- const float world_y = centered_x * k_inv_sin + centered_y * k_inv_cos;
- const float vis_x = (world_x * scale_x) + half_vis_w;
- const float vis_y = (world_y * scale_y) + half_vis_h;
- const int vx0 = std::clamp(static_cast<int>(vis_x), 0, vis_width - 1);
- const int vx1 = std::clamp(vx0 + 1, 0, vis_width - 1);
- const float fx = vis_x - static_cast<float>(vx0);
- const int vy0 = std::clamp(static_cast<int>(vis_y), 0, vis_height - 1);
- const int vy1 = std::clamp(vy0 + 1, 0, vis_height - 1);
- const float fy = vis_y - static_cast<float>(vy0);
- const float a00 = get_alpha(vx0, vy0);
- const float a10 = get_alpha(vx1, vy0);
- const float a01 = get_alpha(vx0, vy1);
- const float a11 = get_alpha(vx1, vy1);
- const float alpha_top = a00 + (a10 - a00) * fx;
- const float alpha_bot = a01 + (a11 - a01) * fx;
- const float fog_alpha = alpha_top + (alpha_bot - alpha_top) * fy;
- if (fog_alpha > ALPHA_THRESHOLD) {
- const QRgb original = scanline[x];
- const int orig_r = qRed(original);
- const int orig_g = qGreen(original);
- const int orig_b = qBlue(original);
- const float blend = fog_alpha * ALPHA_SCALE;
- const float inv_blend = 1.0F - blend;
- const int new_r = static_cast<int>(orig_r * inv_blend + FOG_R * blend);
- const int new_g = static_cast<int>(orig_g * inv_blend + FOG_G * blend);
- const int new_b = static_cast<int>(orig_b * inv_blend + FOG_B * blend);
- scanline[x] = qRgba(new_r, new_g, new_b, 255);
- }
- }
- }
- update_minimap_units();
- }
- void GameEngine::update_minimap_units() {
- if (m_minimap_image.isNull() || !m_unit_layer || !m_world) {
- emit minimap_image_changed();
- return;
- }
- std::vector<Game::Map::Minimap::UnitMarker> markers;
- markers.reserve(128);
- std::unordered_set<Engine::Core::EntityID> selected_ids;
- if (auto *selection_system =
- m_world->get_system<Game::Systems::SelectionSystem>()) {
- const auto &sel = selection_system->get_selected_units();
- selected_ids.insert(sel.begin(), sel.end());
- }
- {
- const std::lock_guard<std::recursive_mutex> lock(
- m_world->get_entity_mutex());
- const auto &entities = m_world->get_entities();
- for (const auto &[entity_id, entity] : entities) {
- const auto *unit = entity->get_component<Engine::Core::UnitComponent>();
- if (!unit) {
- continue;
- }
- const auto *transform =
- entity->get_component<Engine::Core::TransformComponent>();
- if (!transform) {
- continue;
- }
- Game::Map::Minimap::UnitMarker marker;
- marker.world_x = transform->position.x;
- marker.world_z = transform->position.z;
- marker.owner_id = unit->owner_id;
- marker.is_selected = selected_ids.count(entity_id) > 0;
- marker.is_building = Game::Units::is_building_spawn(unit->spawn_type);
- markers.push_back(marker);
- }
- }
- m_unit_layer->update(markers);
- const QImage &unit_overlay = m_unit_layer->get_image();
- if (!unit_overlay.isNull()) {
- QPainter painter(&m_minimap_image);
- painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
- painter.drawImage(0, 0, unit_overlay);
- }
- emit minimap_image_changed();
- }
|