Skip to content

Commit

Permalink
Merge pull request pioneerspacesim#5901 from Web-eWorks/editor-sysgen
Browse files Browse the repository at this point in the history
Add ability to reroll random systems to System Editor
  • Loading branch information
Webster Sheets authored Aug 25, 2024
2 parents a4cbbe3 + 2de6265 commit 2c962df
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 55 deletions.
5 changes: 3 additions & 2 deletions src/editor/EditorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
using namespace Editor;

EditorWindow::EditorWindow(EditorApp *app) :
m_app(app)
m_app(app),
m_canBeClosed(true)
{}

EditorWindow::~EditorWindow()
Expand All @@ -25,7 +26,7 @@ void EditorWindow::Update(float deltaTime)
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.f);

bool shouldClose = false;
bool open = ImGui::Begin(GetWindowName(), &shouldClose, flags);
bool open = ImGui::Begin(GetWindowName(), m_canBeClosed ? &shouldClose : nullptr, flags);

ImGui::PopStyleVar(2);

Expand Down
4 changes: 4 additions & 0 deletions src/editor/EditorWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace Editor

virtual const char *GetWindowName() = 0;

bool GetCanBeClosed() const { return m_canBeClosed; }
void SetCanBeClosed(bool closeable) { m_canBeClosed = closeable; }

protected:

virtual void OnDraw() = 0;
Expand All @@ -29,5 +32,6 @@ namespace Editor

private:
EditorApp *m_app;
bool m_canBeClosed;
};
}
2 changes: 1 addition & 1 deletion src/editor/ViewportWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void ViewportWindow::Update(float deltaTime)
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.f);

bool shouldClose = false;
bool open = ImGui::Begin(GetWindowName(), (flags & ImGuiWindowFlags_NoTitleBar) ? nullptr : &shouldClose, flags);
bool open = ImGui::Begin(GetWindowName(), (flags & ImGuiWindowFlags_NoTitleBar || !GetCanBeClosed()) ? nullptr : &shouldClose, flags);

ImGui::PopStyleVar(2);

Expand Down
6 changes: 3 additions & 3 deletions src/editor/system/GalaxyEditAPI.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
#include "GalaxyEditAPI.h"

#include "GalaxyEditAPI.h"

#include "EditorIcons.h"
#include "SystemEditorHelpers.h"
Expand All @@ -11,10 +11,10 @@
#include "editor/UndoStepType.h"
#include "editor/EditorDraw.h"

#include "EnumStrings.h"
#include "galaxy/Sector.h"
#include "galaxy/Factions.h"
#include "galaxy/Galaxy.h"
#include "galaxy/NameGenerator.h"
#include "galaxy/Sector.h"
#include "galaxy/StarSystemGenerator.h"

#include "imgui/imgui.h"
Expand Down
128 changes: 101 additions & 27 deletions src/editor/system/SystemEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@

#include "GalaxyEditAPI.h"
#include "SystemBodyUndo.h"
#include "SystemEditorHelpers.h"
#include "SystemEditorViewport.h"
#include "SystemEditorModals.h"

#include "EnumStrings.h"
#include "FileSystem.h"
#include "JsonUtils.h"
#include "ModManager.h"
#include "Pi.h" // just here for Pi::luaNameGen
#include "SystemView.h"
#include "core/StringUtils.h"
Expand All @@ -22,7 +19,6 @@
#include "editor/EditorDraw.h"
#include "editor/EditorIcons.h"
#include "editor/UndoSystem.h"
#include "editor/UndoStepType.h"

#include "galaxy/Galaxy.h"
#include "galaxy/GalaxyGenerator.h"
Expand Down Expand Up @@ -91,6 +87,24 @@ class SystemEditor::UndoSetSelection : public UndoStep {
SystemBody *m_selection;
};

class SystemEditor::UndoSetEditedSystem : public UndoStep {
public:
UndoSetEditedSystem(SystemEditor *editor, RefCountedPtr<StarSystem> system) :
m_editor(editor),
m_system(std::move(system))
{
}

void Swap() override {
std::swap(m_editor->m_system, m_system);
m_editor->m_viewport->SetSystem(m_editor->m_system);
}

private:
SystemEditor *m_editor;
RefCountedPtr<StarSystem> m_system;
};

SystemEditor::SystemEditor(EditorApp *app) :
m_app(app),
m_system(nullptr),
Expand All @@ -116,6 +130,7 @@ SystemEditor::SystemEditor(EditorApp *app) :
m_systemLoader.reset(new CustomSystemsDatabase(m_galaxy.Get(), "systems"));

m_viewport.reset(new SystemEditorViewport(m_app, this));
m_viewport->SetCanBeClosed(false);

m_random.seed({
// generate random values not dependent on app runtime
Expand Down Expand Up @@ -341,6 +356,54 @@ void SystemEditor::LoadSystemFromGalaxy(RefCountedPtr<StarSystem> system)
m_systemInfo.faction = system->GetFaction() ? system->GetFaction()->name : "";
}

bool SystemEditor::RegenerateSystem(uint32_t newSeed)
{
SystemPath path = m_system->GetPath();
uint32_t _init[5] = { newSeed, uint32_t(path.sectorX), uint32_t(path.sectorY), uint32_t(path.sectorZ), UNIVERSE_SEED };
Random rng(_init, 5);

RefCountedPtr<StarSystem::GeneratorAPI> system(new StarSystem::GeneratorAPI(path, m_galaxy, nullptr, rng));

GalaxyGenerator::StarSystemConfig config;
auto stage1 = std::make_unique<StarSystemFromSectorGenerator>();
auto stage2 = std::make_unique<StarSystemCustomGenerator>();
auto stage3 = std::make_unique<StarSystemRandomGenerator>();
auto stage4 = std::make_unique<PopulateStarSystemGenerator>();

if (!stage1->Apply(rng, m_galaxy, system, &config)) {
Log::Error("Cannot apply stage1 generator");
return false;
}

// Stage1 uses the seed created by sector generation
system->SetSeed(newSeed);

if (!stage2->Apply(rng, m_galaxy, system, &config)) {
Log::Error("Cannot apply stage2 generator");
return false;
}

if (!stage3->Apply(rng, m_galaxy, system, &config)) {
Log::Error("Cannot apply stage3 generator");
return false;
}

if (!stage4->Apply(rng, m_galaxy, system, &config)) {
Log::Error("Cannot apply stage4 generator");
return false;
}

if (!system->GetRootBody()) {
Log::Error("System doesn't have a root body (should never happen)!");
return false;
}

m_system = system;
m_viewport->SetSystem(system);

return true;
}

void SystemEditor::ClearSystem()
{
GetUndo()->Clear();
Expand Down Expand Up @@ -378,6 +441,10 @@ void SystemEditor::End()

void SystemEditor::RegisterMenuActions()
{
auto hasNonCustomSystem = [&]() {
return m_system.Valid() && !m_system->HasCustomBodies();
};

m_menuBinder->BeginMenu("File");

m_menuBinder->AddAction("New", {
Expand Down Expand Up @@ -434,6 +501,36 @@ void SystemEditor::RegisterMenuActions()

m_menuBinder->BeginMenu("Edit");

m_menuBinder->BeginMenu("System");

m_menuBinder->AddAction("Regenerate", {
"Generate from current Seed", {}, hasNonCustomSystem,
[&]() {
GetUndo()->BeginEntry("Regenerate System");
GetUndo()->AddUndoStep<UndoSetSelection>(this, nullptr);
GetUndo()->AddUndoStep<UndoSetEditedSystem>(this, m_system);
GetUndo()->EndEntry();

RegenerateSystem(m_system->GetSeed());
}
});

m_menuBinder->AddAction("RegenerateNewSeed", {
"Generate from new Seed", ImGuiKey_ModCtrl | ImGuiKey_ModShift | ImGuiKey_R, hasNonCustomSystem,
[&]() {
uint32_t seed = Random(m_system->GetSeed()).Int32();

GetUndo()->BeginEntry("Regenerate System");
GetUndo()->AddUndoStep<UndoSetSelection>(this, nullptr);
GetUndo()->AddUndoStep<UndoSetEditedSystem>(this, m_system);
GetUndo()->EndEntry();

RegenerateSystem(seed);
}
});

m_menuBinder->EndMenu();

auto hasSelectedBody = [&]() { return m_contextBody != nullptr; };
auto hasParentBody = [&]() { return m_contextBody && m_contextBody->GetParent(); };

Expand Down Expand Up @@ -831,29 +928,6 @@ void SystemEditor::DrawInterface()
}
ImGui::End();

#if 0
if (ImGui::Begin("ModList")) {
for (const auto &mod : ModManager::EnumerateMods()) {
if (!mod.enabled)
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(255, 64, 64, 255));

ImGui::PushFont(m_app->GetPiGui()->GetFont("orbiteer", 14));
ImGui::TextUnformatted(mod.name.c_str());
ImGui::PopFont();

ImGui::PushFont(m_app->GetPiGui()->GetFont("pionillium", 12));
ImGui::TextUnformatted(mod.path.c_str());
ImGui::PopFont();

if (!mod.enabled)
ImGui::PopStyleColor();

ImGui::Spacing();
}
}
ImGui::End();
#endif

if (m_binderWindowOpen)
m_menuBinder->DrawOverview("Shortcut List", &m_binderWindowOpen);

Expand Down
3 changes: 2 additions & 1 deletion src/editor/system/SystemEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "SystemEditorModals.h"

#include "Input.h"
#include "Random.h"
#include "RefCounted.h"
#include "core/Application.h"
Expand Down Expand Up @@ -74,6 +73,7 @@ class SystemEditor : public Application::Lifecycle {
bool LoadSystemFromFile(const FileSystem::FileInfo &file);
bool LoadCustomSystem(const CustomSystem *system);
void LoadSystemFromGalaxy(RefCountedPtr<StarSystem> system);
bool RegenerateSystem(uint32_t newSeed);
void ClearSystem();

void OnFilepathChanged();
Expand Down Expand Up @@ -109,6 +109,7 @@ class SystemEditor : public Application::Lifecycle {

private:
class UndoSetSelection;
class UndoSetEditedSystem;

// Pending file actions which triggered an unsaved changes modal
enum FileRequestType {
Expand Down
11 changes: 7 additions & 4 deletions src/editor/system/SystemEditorHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

#include "SystemEditorHelpers.h"

#include "MathUtil.h"

#include "core/macros.h"
#include "gameconsts.h"

Expand Down Expand Up @@ -265,18 +268,18 @@ bool Draw::InputFixedRadius(const char *str, fixed *val, bool is_solar, ImGuiInp
if (ImGui::IsDisabled()) val_step *= 0.0;

if (is_solar && unit_type != RADIUS_SOLS)
val_d *= unit_type == RADIUS_EARTH ? SOL_TO_EARTH_MASS : SOL_RADIUS_KM;
val_d *= unit_type == RADIUS_EARTH ? SOL_TO_EARTH_RADIUS : SOL_RADIUS_KM;
if (!is_solar && unit_type != RADIUS_EARTH)
val_d *= unit_type == RADIUS_SOLS ? (1.0 / SOL_TO_EARTH_MASS) : EARTH_RADIUS_KM;
val_d *= unit_type == RADIUS_SOLS ? (1.0 / SOL_TO_EARTH_RADIUS) : EARTH_RADIUS_KM;

ImGui::SameLine(0.f, 1.f);
Draw::SubtractItemWidth();
bool changed = ImGui::InputDouble(str, &val_d, val_step, val_step * 10.0, radius_formats[unit_type], flags);

if (is_solar && unit_type != RADIUS_SOLS)
val_d /= unit_type == RADIUS_EARTH ? SOL_TO_EARTH_MASS : SOL_RADIUS_KM;
val_d /= unit_type == RADIUS_EARTH ? SOL_TO_EARTH_RADIUS : SOL_RADIUS_KM;
if (!is_solar && unit_type != RADIUS_EARTH)
val_d /= unit_type == RADIUS_SOLS ? (1.0 / SOL_TO_EARTH_MASS) : EARTH_RADIUS_KM;
val_d /= unit_type == RADIUS_SOLS ? (1.0 / SOL_TO_EARTH_RADIUS) : EARTH_RADIUS_KM;

if (changed)
*val = fixed::FromDouble(val_d);
Expand Down
2 changes: 0 additions & 2 deletions src/editor/system/SystemEditorHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
#pragma once

#include "EditorIcons.h"
#include "MathUtil.h"
#include "imgui/imgui.h"
#include "imgui/imgui_stdlib.h"

#include "fixed.h"

Expand Down
6 changes: 5 additions & 1 deletion src/editor/system/SystemEditorModals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ void NewSystemModal::DrawInternal()
ImGui::EndChild();

if (ImGui::Button("New System")) {
m_editor->NewSystem(m_path->SectorOnly());
// Ensure we generate a valid system index
SystemPath newPath = m_path->SectorOnly();
newPath.systemIndex = sec->m_systems.size();

m_editor->NewSystem(newPath);
ImGui::CloseCurrentPopup();
}

Expand Down
1 change: 0 additions & 1 deletion src/editor/system/SystemEditorViewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "Background.h"
#include "SystemView.h"
#include "galaxy/Galaxy.h"
#include "galaxy/StarSystem.h"

#include "editor/EditorApp.h"
Expand Down
4 changes: 2 additions & 2 deletions src/galaxy/StarSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class StarSystem : public RefCounted {
double GetExploredTime() const { return m_exploredTime; }
void ExploreSystem(double time);

bool HasCustomBodies() const { return m_hasCustomBodies; }

fixed GetMetallicity() const { return m_metallicity; }
fixed GetIndustrial() const { return m_industrial; }
fixed GetAgricultural() const { return m_agricultural; }
Expand Down Expand Up @@ -170,8 +172,6 @@ class StarSystem::GeneratorAPI : public StarSystem {
public:
GeneratorAPI(const SystemPath &path, RefCountedPtr<Galaxy> galaxy, StarSystemCache *cache, Random &rand);

bool HasCustomBodies() const { return m_hasCustomBodies; }

void SetCustom(bool isCustom, bool hasCustomBodies)
{
m_isCustom = isCustom;
Expand Down
Loading

0 comments on commit 2c962df

Please sign in to comment.