-
-
Notifications
You must be signed in to change notification settings - Fork 44
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
Task core components #860
Changes from 6 commits
5e0c681
d5f7361
d08bfcf
5126833
65ac862
b1dcb7c
2ed44a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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; | ||
} | ||
|
||
void Component::SetIntegral(bool integral) { | ||
this->integral = integral; | ||
} |
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() = 0; | ||
|
||
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 |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we get some comments on why we're having another There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} |
There was a problem hiding this comment.
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, thenprotected
, thenprivate
. This helps with ABI compatibility, not that we're really concerned about that right now.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.