diff --git a/source/tools/atlas/AtlasUI/ScenarioEditor/Sections/Map/Map.cpp b/source/tools/atlas/AtlasUI/ScenarioEditor/Sections/Map/Map.cpp index 74b6e88f87..3a35d3f278 100644 --- a/source/tools/atlas/AtlasUI/ScenarioEditor/Sections/Map/Map.cpp +++ b/source/tools/atlas/AtlasUI/ScenarioEditor/Sections/Map/Map.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2023 Wildfire Games. +/* Copyright (C) 2024 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -29,8 +29,10 @@ #include #include #include +#include #include #include +#include #define CREATE_CHECKBOX(window, parentSizer, name, description, ID) \ parentSizer->Add(new wxStaticText(window, wxID_ANY, _(name)), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT)); \ @@ -51,6 +53,7 @@ enum ID_MapKW_Trigger, ID_RandomScript, ID_RandomSize, + ID_RandomBiome, ID_RandomNomad, ID_RandomSeed, ID_RandomReseed, @@ -468,6 +471,10 @@ MapSidebar::MapSidebar(ScenarioEditor& scenarioEditor, wxWindow* sidebarContaine wxFlexGridSizer* gridSizer = new wxFlexGridSizer(2, 5, 5); gridSizer->AddGrowableCol(1); + gridSizer->Add(new wxStaticText(scrolledWindow, wxID_ANY, _("Biome")), + wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT)); + gridSizer->Add(new wxChoice(scrolledWindow, ID_RandomBiome), wxSizerFlags().Expand()); + wxChoice* sizeChoice = new wxChoice(scrolledWindow, ID_RandomSize); gridSizer->Add(new wxStaticText(scrolledWindow, wxID_ANY, _("Map size")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT)); gridSizer->Add(sizeChoice, wxSizerFlags().Expand()); @@ -669,6 +676,41 @@ void MapSidebar::OnSimReset(wxCommandEvent& WXUNUSED(event)) UpdateSimButtons(); } +void MapSidebar::OnRandomScript(wxCommandEvent& WXUNUSED(evt)) +{ + wxChoice* biomeChoice = wxDynamicCast(FindWindow(ID_RandomBiome), wxChoice); + wxChoice* scriptChoice = wxDynamicCast(FindWindow(ID_RandomScript), wxChoice); + + if (scriptChoice->GetSelection() < 0) + return; + + biomeChoice->Clear(); + + AtObj mapSettings = dynamic_cast(scriptChoice->GetClientObject( + scriptChoice->GetSelection()))->GetValue(); + std::vector biomes; + if (mapSettings["SupportedBiomes"]["@array"].defined()) + { + for (AtIter it = mapSettings["SupportedBiomes"]["item"]; it.defined(); ++it) + biomes.push_back(static_cast(*it)); + } + else + { + std::string singleBiome{static_cast(*mapSettings["SupportedBiomes"])}; + if (!singleBiome.empty()) + biomes.push_back(singleBiome); + } + AtlasMessage::qExpandBiomes qry; + qry.biomes = std::move(biomes); + qry.Post(); + biomes = *qry.biomes; + + for (const std::string& biome : biomes) + biomeChoice->Append(wxString::FromUTF8(biome.c_str())); + + biomeChoice->SetSelection(0); +} + void MapSidebar::OnRandomReseed(wxCommandEvent& WXUNUSED(evt)) { std::mt19937 engine(std::time(nullptr)); @@ -708,6 +750,10 @@ void MapSidebar::OnRandomGenerate(wxCommandEvent& WXUNUSED(evt)) settings.setInt("Seed", wxAtoi(wxDynamicCast(FindWindow(ID_RandomSeed), wxTextCtrl)->GetValue())); + wxChoice* biomeChoice = wxDynamicCast(FindWindow(ID_RandomBiome), wxChoice); + wxString biome = biomeChoice->GetString(biomeChoice->GetSelection()); + settings.set("Biome", biome.utf8_str()); + std::string json = AtlasObject::SaveToJSON(settings); wxBusyInfo busy(_("Generating map")); @@ -764,4 +810,5 @@ BEGIN_EVENT_TABLE(MapSidebar, Sidebar) EVT_BUTTON(ID_RandomGenerate, MapSidebar::OnRandomGenerate) EVT_BUTTON(ID_ResizeMap, MapSidebar::OnResizeMap) EVT_BUTTON(ID_OpenPlayerPanel, MapSidebar::OnOpenPlayerPanel) + EVT_CHOICE(ID_RandomScript, MapSidebar::OnRandomScript) END_EVENT_TABLE(); diff --git a/source/tools/atlas/AtlasUI/ScenarioEditor/Sections/Map/Map.h b/source/tools/atlas/AtlasUI/ScenarioEditor/Sections/Map/Map.h index eedfef2ae3..885ccbaea2 100644 --- a/source/tools/atlas/AtlasUI/ScenarioEditor/Sections/Map/Map.h +++ b/source/tools/atlas/AtlasUI/ScenarioEditor/Sections/Map/Map.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2024 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -36,6 +36,7 @@ private: void OnCollapse(wxCollapsiblePaneEvent& evt); void OnOpenPlayerPanel(wxCommandEvent& evt); + void OnRandomScript(wxCommandEvent& evt); void OnRandomReseed(wxCommandEvent& evt); void OnRandomGenerate(wxCommandEvent& evt); void OnResizeMap(wxCommandEvent& evt); diff --git a/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp b/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp index 171e1be65a..f83936e066 100644 --- a/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp +++ b/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2023 Wildfire Games. +/* Copyright (C) 2024 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -57,6 +57,9 @@ #include "simulation2/components/ICmpVisual.h" #include "simulation2/system/ParamNode.h" +#include +#include +#include #ifdef _MSC_VER # pragma warning(disable: 4458) // Declaration hides class member. @@ -357,6 +360,41 @@ QUERYHANDLER(GetRMSData) msg->data = g_Game->GetSimulation2()->GetRMSData(); } +QUERYHANDLER(ExpandBiomes) +{ + std::vector unexpandedBiomes = *msg->biomes; + std::vector expandedBiomes; + for (const std::string& toExpand : unexpandedBiomes) + { + if (toExpand.empty()) + { + LOGERROR("Got an empty biome"); + continue; + } + if (toExpand.back() != '/') + { + expandedBiomes.push_back(toExpand); + continue; + } + + VfsPaths biomesList; + if (vfs::GetPathnames(g_VFS, "maps/random/rmbiome/" + toExpand, L"*.json", biomesList) == + INFO::OK) + { + std::transform(biomesList.begin(), biomesList.end(), std::back_inserter(expandedBiomes), + [&](const VfsPath& biome) + { + return toExpand + biome.Basename().string8(); + }); + } + else + LOGERROR("Error reading biome files in %s", toExpand); + + } + + msg->biomes = std::move(expandedBiomes); +} + QUERYHANDLER(GetCurrentMapSize) { msg->size = g_Game->GetWorld()->GetTerrain().GetTilesPerSide(); diff --git a/source/tools/atlas/GameInterface/Messages.h b/source/tools/atlas/GameInterface/Messages.h index a486d93a68..40ba13d494 100644 --- a/source/tools/atlas/GameInterface/Messages.h +++ b/source/tools/atlas/GameInterface/Messages.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2023 Wildfire Games. +/* Copyright (C) 2024 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -222,6 +222,11 @@ QUERY(GetRMSData, ((std::vector, data)) ); +QUERY(ExpandBiomes, + , + ((std::vector, biomes)) + ); + COMMAND(ResizeMap, NOMERGE, ((int, tiles)) ((int, offsetX))