1
0
forked from 0ad/0ad

Allow selecting a biome in Atlas

This commit is contained in:
phosit 2024-09-21 13:45:48 +02:00
parent 668ae8a20e
commit 1b86fc70fd
4 changed files with 95 additions and 4 deletions

View File

@ -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 <algorithm>
#include <ctime>
#include <random>
#include <string>
#include <wx/busyinfo.h>
#include <wx/filename.h>
#include <vector>
#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<AtObjClientData*>(scriptChoice->GetClientObject(
scriptChoice->GetSelection()))->GetValue();
std::vector<std::string> biomes;
if (mapSettings["SupportedBiomes"]["@array"].defined())
{
for (AtIter it = mapSettings["SupportedBiomes"]["item"]; it.defined(); ++it)
biomes.push_back(static_cast<const char*>(*it));
}
else
{
std::string singleBiome{static_cast<const char*>(*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();

View File

@ -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);

View File

@ -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 <algorithm>
#include <string>
#include <vector>
#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<std::string> unexpandedBiomes = *msg->biomes;
std::vector<std::string> 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();

View File

@ -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<std::string>, data))
);
QUERY(ExpandBiomes,
,
((std::vector<std::string>, biomes))
);
COMMAND(ResizeMap, NOMERGE,
((int, tiles))
((int, offsetX))