Skip to content

Commit

Permalink
Various bug fixes and additions to Resource class
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
royfalk committed Apr 30, 2024
1 parent d08bfcf commit 304e833
Show file tree
Hide file tree
Showing 12 changed files with 456 additions and 79 deletions.
2 changes: 2 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ SET(LIBRESOURCE
src/resource/product.cpp
src/resource/cargo.cpp
src/resource/manifest.cpp
src/resource/random_utils.cpp
src/cmd/json.cpp
)

Expand Down Expand Up @@ -1705,6 +1706,7 @@ IF (USE_GTEST)
src/resource/tests/buy_sell.cpp
src/resource/tests/resource_test.cpp
src/resource/tests/manifest_tests.cpp
src/resource/tests/random_tests.cpp
src/exit_unit_tests.cpp
)

Expand Down
2 changes: 1 addition & 1 deletion engine/src/cmd/unit_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ void Unit::DamageRandSys(float dam, const Vector &vec, float randnum, float degr
if (dam < mindam) {
dam = mindam;
}
energy.DowngradeByPercent(dam);
energy.DamageByPercent(dam);
} else if (repair_droid > 0) {
repair_droid--;
}
Expand Down
14 changes: 4 additions & 10 deletions engine/src/damage/damageable_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@

#include "damageable_layer.h"

#include <random>
#include <cassert>

// TODO: this is a use of the code in a different library.
// I'm unhappy with this, so it needs to change.
#include "mount_size.h"

#include "random_utils.h"

DamageableLayer::DamageableLayer(int layer_index,
FacetConfiguration configuration,
Health health_template,
Expand Down Expand Up @@ -229,15 +230,8 @@ void DamageableLayer::ReduceLayerCapability(const float &percent,
return;
}

static std::random_device randome_device;
static std::mt19937 gen(randome_device());

// TODO: this feels a bit sloppy, as we're dealing in integers here.
static std::uniform_int_distribution<> impact_distribution(1, 100);
static std::uniform_int_distribution<> facet_distribution(0, facets.size() - 1);

bool affect_regeneration = impact_distribution(gen) <= chance_to_reduce_regeneration;
int facet_index = facet_distribution(gen);
bool affect_regeneration = randomDouble() <= chance_to_reduce_regeneration;
int facet_index = randomInt(number_of_facets);

if (affect_regeneration) {
// Reduce regeneration
Expand Down
8 changes: 2 additions & 6 deletions engine/src/resource/manifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

#include <fstream>
#include <sstream>
#include <random>
#include <algorithm>
#include <iostream>

//#include "xml_support.h" // TODO: replace this later
#include "json.h"
#include "random_utils.h"


// TODO: get rid of this helper function and others like it.
Expand Down Expand Up @@ -121,11 +121,7 @@ Cargo Manifest::GetRandomCargo(int quantity) {
return Cargo();
}

std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> int_dist(0,_items.size()-1);

int index = int_dist(rng); // TODO: test this gets all items
int index = randomInt(_items.size()-1);
Cargo c = _items[index];
c.SetQuantity(quantity);
return c;
Expand Down
43 changes: 43 additions & 0 deletions engine/src/resource/random_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* random_utils.cpp
*
* Copyright (C) 2001-2024 Daniel Horn, Benjaman Meyer, Roy Falk, Stephen G. Tuggy,
* 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 3 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/>.
*/

#include <random>

int randomInt(int max, int min = 0 ) {
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> int_dist(min,max);

return int_dist(rng); // TODO: test this gets all items
}


double randomDouble() {
const int precision = 10000;
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> int_dist(0,precision);
int random_int = int_dist(rng);
return (double)random_int/precision;
}
31 changes: 31 additions & 0 deletions engine/src/resource/random_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* random_utils.h
*
* Copyright (C) 2001-2024 Daniel Horn, Benjaman Meyer, Roy Falk, Stephen G. Tuggy,
* 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 3 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/>.
*/

#ifndef VEGA_STRIKE_ENGINE_RESOURCE_RANDOM_UTILS_H
#define VEGA_STRIKE_ENGINE_RESOURCE_RANDOM_UTILS_H

int randomInt(int max, int min = 0 );
double randomDouble();

#endif //VEGA_STRIKE_ENGINE_RESOURCE_RANDOM_UTILS_H
145 changes: 117 additions & 28 deletions engine/src/resource/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <algorithm>
#include <iostream>

#include "random_utils.h"

/*
* Constructors
*/
Expand All @@ -33,33 +35,21 @@ Resource<T>::Resource(const T &value, const T &min_value, const T &max_value):
value_(value),
min_value_(min_value),
max_value_(max_value),
adjusted_max_value_(value),
adjusted_max_value_(max_value),
no_max_(max_value==-1) {}

/*
* Methods
*/
template<typename T>
void Resource<T>::Downgrade(const T &value) {
if(no_max_) { // Can't downgrade if there's no max
return;
}

adjusted_max_value_ = std::max(min_value_, adjusted_max_value_ - value);
}

template<typename T>
void Resource<T>::DowngradeByPercent(const T &value) {
if(no_max_) { // Can't downgrade if there's no max
return;
double Resource<T>::Percent() const {
if(no_max_) { // Can't calculate percent if there's no max
return -1;
}

adjusted_max_value_ = std::max(min_value_, adjusted_max_value_ - (max_value_ * value));
}

template<typename T>
T Resource<T>::Percent() const {
if(no_max_) { // Can't calculate percent if there's no max
if(max_value_ == 0) { // Can't calculate percent if divider is 0
return -1;
}

Expand All @@ -85,30 +75,37 @@ void Resource<T>::Set(const T &value) {
}

template<typename T>
void Resource<T>::SetMaxValue(const T &value) {
if(no_max_) { // Can't set max if there's no max
void Resource<T>::SetToMax() {
if(no_max_) { // Can't set to max if there's no max
return;
}

adjusted_max_value_ = max_value_ = value;
value_ = adjusted_max_value_ = max_value_;
}

template<typename T>
void Resource<T>::Upgrade(const T &value) {
if(no_max_) { // Can't upgrade max if there's no max
void Resource<T>::SetMaxValue(const T &value) {
if(no_max_) { // Can't set max if there's no max
return;
}

adjusted_max_value_ = std::min(max_value_, adjusted_max_value_ + value);
value_ = adjusted_max_value_ = max_value_ = value;
}

template<typename T>
void Resource<T>::UpgradeByPercent(const T &value) {
if(no_max_) { // Can't upgrade max if there's no max
void Resource<T>::SetAdjustedMaxValue(const T &value) {
T v = value;

if(no_max_) { // Can't set max if there's no max
return;
}

adjusted_max_value_ = std::min(max_value_, adjusted_max_value_ + (max_value_ * value));
v = std::max(min_value_, v);
v = std::min(max_value_, v);

adjusted_max_value_ = v;

value_ = std::min(value_, adjusted_max_value_);
}

template<typename T>
Expand Down Expand Up @@ -136,14 +133,106 @@ void Resource<T>::Zero() {
value_ = adjusted_max_value_ = min_value_;
}

// Damage & Repair
template<typename T>
void Resource<T>::Destroy() {
value_ = adjusted_max_value_ = min_value_;
}

template<typename T>
bool Resource<T>::Destroyed() {
return adjusted_max_value_ == min_value_;
}

template<typename T>
void Resource<T>::RandomDamage() {
const double severity = randomDouble();

if(severity > .95) {
// Destroy system
adjusted_max_value_ = min_value_;
} else {
// Damage system
DamageByPercent(severity);
}
}

template<typename T>
void Resource<T>::DamageByValue(const T &value) {
if(no_max_) { // Can't downgrade if there's no max
return;
}

adjusted_max_value_ = std::max(min_value_, adjusted_max_value_ - value);
value_ = std::min(value_, adjusted_max_value_);
}

template<typename T>
void Resource<T>::DamageByPercent(const T &value) {
if(no_max_) { // Can't downgrade if there's no max
return;
}

adjusted_max_value_ = std::max(min_value_, adjusted_max_value_ - (max_value_ * value));
value_ = std::min(value_, adjusted_max_value_);
}

template<typename T>
bool Resource<T>::Damaged() const {
return adjusted_max_value_ < max_value_;
}

// TODO: partial repair
template<typename T>
void Resource<T>::RepairFully() {
value_ = adjusted_max_value_ = max_value_;
}

template<typename T>
void Resource<T>::RepairByValue(const T &value) {
if(no_max_) { // Can't upgrade max if there's no max
return;
}

adjusted_max_value_ = std::min(max_value_, adjusted_max_value_ + value);
value_ = adjusted_max_value_;
}

template<typename T>
void Resource<T>::RepairByPercent(const T &value) {
if(no_max_) { // Can't upgrade max if there's no max
return;
}

adjusted_max_value_ = std::min(max_value_, adjusted_max_value_ + (max_value_ * value));
value_ = adjusted_max_value_;
}

/*
* Overloaded operators
*/

template<typename T>
const T Resource<T>::operator=(Resource<T> value) const {
return value.value_;
}


template<typename T>
Resource<T> Resource<T>::operator=(const T &value) {
value_ = value;
if(!no_max_) {
value_ = std::min(max_value_, value_);
}
value_ = std::max(min_value_, value_);

return *this;
}

template<typename T>
Resource<T> Resource<T>::operator+=(const T &value) {
if(!no_max_) { // Only applicable if there's max
value_ = std::min(value_ + value, max_value_);
value_ = std::min(value_ + value, adjusted_max_value_);
} else {
value_ += value;
}
Expand All @@ -161,7 +250,7 @@ Resource<T> Resource<T>::operator-=(const T &value) {
template<typename T>
Resource<T> Resource<T>::operator+=(T &value) {
if(!no_max_) { // Only applicable if there's max
value_ = std::min(value_ + value, max_value_);
value_ = std::min(value_ + value, adjusted_max_value_);
} else {
value_ += value;
}
Expand Down
Loading

0 comments on commit 304e833

Please sign in to comment.