Skip to content

Commit

Permalink
Task core components (#860)
Browse files Browse the repository at this point in the history
* Add Component, Reactor, EConsumer and EContainer classes

Add unit tests. 
Remove UnitCsvFactory reliance on VSFileSystem for better separation of concerns. Game doesn't build because unit tests require VSFileSystem and that pulls in half the game with it.
As this is imported from the original lib component branch by file, some stuff here is for future commits.

* Fix error C7555 in CICD on windows

use of designated initializers requires at least '/std:c++20'

* Fix bug - Implement Downgrade in Component

Reverse order of visibility in EnergyConsumer.
Make visibility modifiers explicit in EnergyContainer and Reactor.
  • Loading branch information
royfalk authored May 16, 2024
1 parent 893bc91 commit b34ad92
Show file tree
Hide file tree
Showing 15 changed files with 1,052 additions and 6 deletions.
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:
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;

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

0 comments on commit b34ad92

Please sign in to comment.