Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task core components #860

Merged
merged 7 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ IF (UNIX)
${Vega_Strike_SOURCE_DIR}/src/cmd
${Vega_Strike_SOURCE_DIR}/src/damage
${Vega_Strike_SOURCE_DIR}/src/resource
${Vega_Strike_SOURCE_DIR}/src/components
${Vega_Strike_BINARY_DIR}
${Vega_Strike_BINARY_DIR}/src
/usr/include/harfbuzz/
Expand All @@ -175,6 +176,7 @@ ELSE ()
${Vega_Strike_SOURCE_DIR}/src/cmd
${Vega_Strike_SOURCE_DIR}/src/damage
${Vega_Strike_SOURCE_DIR}/src/resource
${Vega_Strike_SOURCE_DIR}/src/components
${Vega_Strike_BINARY_DIR}
${Vega_Strike_BINARY_DIR}/src
)
Expand Down Expand Up @@ -714,6 +716,14 @@ SET(LIBRESOURCE
src/cmd/json.cpp
)

SET(LIBCOMPONENT
src/components/component.cpp

src/components/energy_consumer.cpp
src/components/energy_container.cpp
src/components/reactor.cpp
)

SET(LIBGUI_SOURCES
src/gui/button.cpp
src/gui/control.cpp
Expand Down Expand Up @@ -1123,6 +1133,7 @@ ADD_LIBRARY(vegastrike-engine_com
${LIBCONFIG}
${LIBDAMAGE}
${LIBRESOURCE}
${LIBCOMPONENT}
${LIBAI_SOURCES}
${LIBCMD_SOURCES}
${LIBNET_SOURCES}
Expand Down Expand Up @@ -1709,12 +1720,15 @@ IF (USE_GTEST)
src/resource/tests/manifest_tests.cpp
src/resource/tests/random_tests.cpp
src/exit_unit_tests.cpp
src/components/tests/energy_container_tests.cpp
src/components/tests/balancing_tests.cpp
)

ADD_LIBRARY(vegastrike-testing
${LIBCONFIG}
${LIBDAMAGE}
${LIBRESOURCE}
${LIBCOMPONENT}
${LIBCMD_SOURCES}
${LIBVS_LOGGING}
)
Expand Down
10 changes: 7 additions & 3 deletions engine/src/cmd/unit_csv_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ std::vector<std::string> ProcessLine(std::string &line) {
return cells;
}

void UnitCSVFactory::ParseCSV(VSFileSystem::VSFile &file, bool saved_game) {
void UnitCSVFactory::ParseCSV(std::string data, std::string root, bool saved_game) {
std::vector<std::string> columns;
std::string data = file.ReadFull();
std::string delimiter = "\n";
size_t pos = 0;
std::string token;
Expand Down Expand Up @@ -124,7 +123,7 @@ void UnitCSVFactory::ParseCSV(VSFileSystem::VSFile &file, bool saved_game) {
}

// Add root
unit_attributes["root"] = file.GetRoot();
unit_attributes["root"] = root;

std::string key = (saved_game ? "player_ship" : line[0]);

Expand All @@ -151,3 +150,8 @@ std::string GetUnitKeyFromNameAndFaction(const std::string unit_name, const std:

return std::string();
}

void UnitCSVFactory::LoadUnit(std::string key,
std::map<std::string,std::string> unit_map) {
UnitCSVFactory::units[key] = unit_map;
}
7 changes: 6 additions & 1 deletion engine/src/cmd/unit_csv_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ const std::string keys[] = {"Key", "Directory", "Name", "STATUS", "Object_Type",
"Explosion", "Num_Animation_Stages", "Upgrade_Storage_Volume", "Heat_Sink_Rating",
"Shield_Efficiency", "Num_Chunks", "Chunk_0", "Collide_Subunits", "Spec_Interdiction",
"Tractorability",
// For component upgrade
"Upgrade_Type", "Facets",
// These values are not in units.csv! There are probably more but I stopped mapping.
// TODO: map all missing values using the commented out code below!
"FaceCamera", "Unit_Role", "Attack_Preference", "Hidden_Hold_Volume", "Equipment_Space"};
Expand Down Expand Up @@ -102,7 +104,7 @@ class UnitCSVFactory {
friend class UnitJSONFactory;
friend class UnitOptimizeFactory;
public:
static void ParseCSV(VSFileSystem::VSFile &file, bool saved_game);
static void ParseCSV(std::string data, std::string root, bool saved_game);

template<class T>
static inline T GetVariable(std::string unit_key, std::string const &attribute_key, T default_value) = delete;
Expand All @@ -124,6 +126,9 @@ class UnitCSVFactory {
static std::map<std::string, std::string> GetUnit(std::string key) {
return UnitCSVFactory::units[key];
}

static void LoadUnit(std::string key,
std::map<std::string,std::string> unit_map);
};

// Template Specialization
Expand Down
3 changes: 2 additions & 1 deletion engine/src/cmd/unit_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ void Unit::Init(const char *filename,
VSFile unitTab;
VSError taberr = unitTab.OpenReadOnly(filepath + ".csv", UnitSaveFile);
if (taberr <= Ok) {
UnitCSVFactory::ParseCSV(unitTab, true);
std::string data = unitTab.ReadFull();
UnitCSVFactory::ParseCSV(data, unitTab.GetRoot(), true);
unitTab.Close();
saved_game = true;
}
Expand Down
78 changes: 78 additions & 0 deletions engine/src/components/component.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* component.cpp
*
* Copyright (c) 2001-2002 Daniel Horn
* Copyright (c) 2002-2019 pyramid3d and other Vega Strike Contributors
* Copyright (c) 2019-2023 Stephen G. Tuggy, Benjamen R. Meyer, Roy Falk and other Vega Strike Contributors
*
* https://github.com/vegastrike/Vega-Strike-Engine-Source
*
* This file is part of Vega Strike.
*
* Vega Strike is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Vega Strike is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Vega Strike. If not, see <https://www.gnu.org/licenses/>.
*/

// -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-

#include "component.h"
#include "unit_csv_factory.h"

Component::Component(double mass, double volume, bool integral):
unit_key(""),
upgrade_name(""),
mass(mass), volume(volume),
integral(integral) {}


void Component::Load(std::string upgrade_key, std::string unit_key) {
this->unit_key = unit_key;
upgrade_name = UnitCSVFactory::GetVariable(upgrade_key, "Name", std::string());
this->upgrade_key = upgrade_key;

mass = UnitCSVFactory::GetVariable(upgrade_key, "Mass", 0.0);
// TODO: volume = UnitCSVFactory::GetVariable(upgrade_key, "Volume", 0.0);
// TODO: bool integral = false;
}

// TODO: convert to std::pair<bool, double>
bool Component::CanWillUpDowngrade(const std::string upgrade_key,
bool upgrade, bool apply) {
if(upgrade) {
if(apply) {
return Upgrade(upgrade_key);
} else {
return CanUpgrade(upgrade_key);
}
} else {
if(apply) {
return Downgrade();
} else {
return CanDowngrade();
}
}
}

bool Component::Downgrade() {
upgrade_name = std::string();
upgrade_key = std::string();

mass = 0.0;
volume = 0.0;

return true;
}

void Component::SetIntegral(bool integral) {
this->integral = integral;
}
92 changes: 92 additions & 0 deletions engine/src/components/component.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* component.h
*
* Copyright (c) 2001-2002 Daniel Horn
* Copyright (c) 2002-2019 pyramid3d and other Vega Strike Contributors
* Copyright (c) 2019-2023 Stephen G. Tuggy, Benjamen R. Meyer, Roy Falk and other Vega Strike Contributors
*
* https://github.com/vegastrike/Vega-Strike-Engine-Source
*
* This file is part of Vega Strike.
*
* Vega Strike is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Vega Strike is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Vega Strike. If not, see <https://www.gnu.org/licenses/>.
*/

// -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-

#ifndef COMPONENT_H
#define COMPONENT_H

#include <string>
#include <map>

/**
* LibComponent is currently tightly coupled to LibDamage and
* other various libraries in VegaStrike engine.
* Consider decoupling and subclassing every component in it.
*/

class Unit;

// TODO: add complete list
enum class ComponentType {
Hull,
Armor,
Shield,
Drive
};

class Component
{
protected:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to note, we should put public information first, then protected, then private. This helps with ABI compatibility, not that we're really concerned about that right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason I assumed the reverse. I'll fix this here, though I can't promise for all of my work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing things changes the offsets. Keeping public data first keeps the offsets to it the same, and new public goes at the end, pushing the protected and private data further away from the zero point.

If we really wanted a public binary ABI then we'd look at doing another method (what Qt does), but it's not really important just good habit for when it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I dug deeper and while most agree on public/protected/private order, fields/constructors/methods is less so.
Since most of the code is fields/constructors/methods, I'll stick with that but reverse the visibility as I go along as these are already not consistent.

std::string unit_key; // Areus.blank
std::string upgrade_name; // Isometal Armor
std::string upgrade_key; // armor03__upgrades

double mass = 0;
double volume = 0;

bool integral = false; // Part of the ship. Can't be upgraded/downgraded
public:
Component(double mass,
double volume, bool integral);

// Load from units dictionary
virtual void Load(std::string upgrade_key, std::string unit_key);

virtual void SaveToCSV(std::map<std::string, std::string>& unit) const = 0;

virtual std::string Describe() const = 0; // Describe component in base_computer

// Handle the four cases of CanUpgrade/Upgrade/CanDowngrade/Downgrade
bool CanWillUpDowngrade(const std::string upgrade_key,
bool upgrade, bool apply);

virtual bool CanDowngrade() const = 0;

virtual bool Downgrade();

virtual bool CanUpgrade(const std::string upgrade_key) const = 0;

virtual bool Upgrade(const std::string upgrade_key) = 0;

virtual void Damage() = 0;
virtual void Repair() = 0;

virtual bool Damaged() const = 0;
virtual bool Installed() const = 0;

void SetIntegral(bool integral);
};
#endif // COMPONENT_H
68 changes: 68 additions & 0 deletions engine/src/components/energy_consumer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* energy_consumer.cpp
*
* Copyright (c) 2001-2002 Daniel Horn
* Copyright (c) 2002-2019 pyramid3d and other Vega Strike Contributors
* Copyright (c) 2019-2023 Stephen G. Tuggy, Benjamen R. Meyer, Roy Falk and other Vega Strike Contributors
*
* https://github.com/vegastrike/Vega-Strike-Engine-Source
*
* This file is part of Vega Strike.
*
* Vega Strike is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Vega Strike is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Vega Strike. If not, see <https://www.gnu.org/licenses/>.
*/

// -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-

#include "energy_consumer.h"

double EnergyConsumer::simulation_atom_var = 0.1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we get some comments on why we're having another simulation_atom_var?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if you want this in the code or just here - the issue is one of encapsulation. With the work done on the various libraries - resource, components, damage, it's become clear that we need much better separation of concerns.
simulation_atom_var is pervasive throughout the game. However, I need a version of it for consumers. This was my solution.
Ideally, I'd like to create a new library - core. It should have some important constants and the vector class. Maybe with Resource class as well. However, I prefer to finish lib component before tackling something else.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this has the potential to cause a lot of confusion, having two (or more) different instantiations of simulation_atom_var in different scopes. But I suppose we can leave it for now. Approving.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@royfalk thanks for the explanation; it'd probably be good to have some documentation about it in the code; but not going to require it for this PR to go through. Would you please file a follow-up PR with a small comment about it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like your idea of the follow-on to have a core library that way too, BTW, and certainly agree we need to take a calculated, stepped approach to getting there.


EnergyConsumer::EnergyConsumer(EnergyContainer *source,
bool partial,
double consumption):
source(source),
partial(partial),
consumption(consumption),
atom_consumption(consumption * simulation_atom_var) {}


double EnergyConsumer::Consume() {
if(!source) {
return 0.0;
}

return source->Deplete(partial, atom_consumption);
}

double EnergyConsumer::GetConsumption() const {
return consumption;
}

double EnergyConsumer::GetAtomConsumption() const {
return atom_consumption;
}

void EnergyConsumer::SetConsumption(double consumption) {
this->consumption = consumption;
atom_consumption = consumption * simulation_atom_var;
}

void EnergyConsumer::SetSource(EnergyContainer* source) {
this->source = source;
}

void EnergyConsumer::ZeroSource() {
source->Zero();
}
Loading
Loading