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 serialize resource #893

Merged
merged 10 commits into from
Oct 14, 2024
Merged
56 changes: 54 additions & 2 deletions engine/src/resource/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

#include <algorithm>
#include <iostream>
#include <vector>

#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>

#include "random_utils.h"

Expand All @@ -38,9 +42,49 @@ Resource<T>::Resource(const T &value, const T &min_value, const T &max_value):
adjusted_max_value_(max_value),
no_max_(max_value==-1) {}

template<>
Resource<double>::Resource(const std::string input, const double modifier, const double minimum_functionality):
value_(0),
min_value_(0),
max_value_(1),
adjusted_max_value_(1),
no_max_(false) {
if(input.empty()) {
value_ = max_value_ = adjusted_max_value_ = 0.0;
return;
}

std::vector<std::string> result;
boost::split(result, input, boost::is_any_of("/"));

switch(result.size()) {
case 1:
value_ = max_value_ = adjusted_max_value_ = std::stod(result[0]) * modifier;
break;
case 2:
value_ = adjusted_max_value_ = std::stod(result[0]) * modifier;
max_value_ = std::stod(result[1]) * modifier;
break;
case 3:
value_ = std::stod(result[0]) * modifier;
adjusted_max_value_ = std::stod(result[1]) * modifier;
max_value_ = std::stod(result[2]) * modifier;
break;
default:
value_ = max_value_ = adjusted_max_value_ = 0.0;
}

min_value_ = max_value_ * minimum_functionality;
}

/*
* Methods
*/
template<>
const std::string Resource<double>::Serialize(const double modifier) const {
return (boost::format("%1$.2f/%2$.2f/%3$.2f") % (value_/modifier) %
(adjusted_max_value_/modifier) % (max_value_/modifier)).str();
}


template<typename T>
Expand Down Expand Up @@ -69,7 +113,7 @@ template<typename T>
void Resource<T>::Set(const T &value) {
value_ = value;
if(!no_max_) {
value_ = std::min(max_value_, value_);
value_ = std::min(adjusted_max_value_, value_);
}
value_ = std::max(min_value_, value_);
}
Expand Down Expand Up @@ -218,7 +262,7 @@ template<typename T>
Resource<T> Resource<T>::operator=(const T &value) {
value_ = value;
if(!no_max_) {
value_ = std::min(max_value_, value_);
value_ = std::min(adjusted_max_value_, value_);
}
value_ = std::max(min_value_, value_);

Expand Down Expand Up @@ -260,6 +304,11 @@ Resource<T> Resource<T>::operator-=(T &value) {
return *this;
}

template<typename T>
bool operator==(const Resource<T> &lhs, const Resource<T> &rhs) {
return lhs.Value() == rhs.Value();
}

template<typename T>
bool operator==(const Resource<T> &lhs, const T &rhs) {
return lhs.Value() == rhs;
Expand Down Expand Up @@ -325,6 +374,7 @@ T operator/(const T &lhs, const Resource<T> &rhs) {
template
class Resource<float>;

template bool operator==(const Resource<float> &lhs, const Resource<float> &rhs);
template bool operator==(const Resource<float> &lhs, const float &rhs);
template bool operator>(const Resource<float> &lhs, const float &rhs);
template bool operator<(const Resource<float> &lhs, const float &rhs);
Expand All @@ -344,6 +394,7 @@ template float operator/(const float &lhs, const Resource<float> &rhs);
template
class Resource<double>;

template bool operator==(const Resource<double> &lhs, const Resource<double> &rhs);
template bool operator==(const Resource<double> &lhs, const double &rhs);
template bool operator>(const Resource<double> &lhs, const double &rhs);
template bool operator<(const Resource<double> &lhs, const double &rhs);
Expand All @@ -360,6 +411,7 @@ template double operator/(const double &lhs, const Resource<double> &rhs);
template
class Resource<int>;

template bool operator==(const Resource<int> &lhs, const Resource<int> &rhs);
template bool operator==(const Resource<int> &lhs, const int &rhs);
template bool operator>(const Resource<int> &lhs, const int &rhs);
template bool operator<(const Resource<int> &lhs, const int &rhs);
Expand Down
9 changes: 9 additions & 0 deletions engine/src/resource/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#ifndef VEGA_STRIKE_ENGINE_RESOURCE_RESOURCE_H
#define VEGA_STRIKE_ENGINE_RESOURCE_RESOURCE_H

#include <string>

/**
* @brief A resource is any part of the game that can be used up and filled up.
* The purpose of this class is to simplify code throughout the game by placing it here.
Expand All @@ -36,9 +38,14 @@ class Resource {
T min_value_;
T max_value_;
T adjusted_max_value_;

// TODO: make this const
bool no_max_;
public:
Resource(const T &value = 0, const T &min_value = 0, const T &max_value = -1);
Resource(const std::string input, const double modifier = 1.0, const double minimum_functionality = 0.0);

const std::string Serialize(const double modifier = 1.0) const;

//const T operator=(Resource<T> value) const;
Resource<T> operator=(const T &value);
Expand Down Expand Up @@ -116,6 +123,8 @@ class Resource {
T* MaxValuePtr() { return &max_value_; }
};

template<typename T>
bool operator==(const Resource<T> &lhs, const Resource<T> &rhs);
template<typename T>
bool operator==(const Resource<T> &lhs, const T &rhs);
template<typename T>
Expand Down
43 changes: 43 additions & 0 deletions engine/src/resource/tests/resource_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ TEST(Resource, Operators) {
EXPECT_EQ(resource, 0.0);
--resource;
EXPECT_EQ(resource, 0.0);

Resource<int> a(1,0,1);
Resource<int> b(1,0,1);

EXPECT_TRUE(a == b);
}


Expand Down Expand Up @@ -174,3 +179,41 @@ TEST(Resource, Damage_Repair) {
EXPECT_EQ(resource.AdjustedValue(), 10.0);
EXPECT_EQ(resource.Value(), 10.0);
}

TEST(Resource, Serialization) {
const double value = 15.0;
const double adjusted = 21.0;
const double max = 30.0;
const double modifier = 3.0;
const std::string one = "10.0";
const std::string two = "7.0/10.0";
const std::string three = "5.0/7.0/10.0";

const std::string three_ten = "10.00/10.00/10.00";
const std::string seven_seven_ten = "7.00/7.00/10.00";
const std::string five_seven_ten = "5.00/7.00/10.00";

Resource<double> resource = Resource<double>(one, modifier);
std::string serialization_string = resource.Serialize(modifier);

EXPECT_EQ(resource.Value(), max);
EXPECT_EQ(resource.AdjustedValue(), max);
EXPECT_EQ(resource.MaxValue(), max);
EXPECT_EQ(serialization_string, three_ten);

resource = Resource<double>(two, modifier);
serialization_string = resource.Serialize(modifier);

EXPECT_EQ(resource.Value(), adjusted);
EXPECT_EQ(resource.AdjustedValue(), adjusted);
EXPECT_EQ(resource.MaxValue(), max);
EXPECT_EQ(serialization_string, seven_seven_ten);

resource = Resource<double>(three, modifier);
serialization_string = resource.Serialize(modifier);

EXPECT_EQ(resource.Value(), value);
EXPECT_EQ(resource.AdjustedValue(), adjusted);
EXPECT_EQ(resource.MaxValue(), max);
EXPECT_EQ(serialization_string, five_seven_ten);
}
Loading