Skip to content

Commit 8bf49af

Browse files
committed
gamestate: Allow fractional values for attributes.
1 parent 50f175f commit 8bf49af

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
// Copyright 2021-2023 the openage authors. See copying.md for legal info.
1+
// Copyright 2021-2024 the openage authors. See copying.md for legal info.
22

33
#include "live.h"
44

5-
#include <optional>
6-
7-
#include "curve/discrete.h"
85
#include "curve/iterator.h"
96
#include "curve/map_filter_iterator.h"
7+
#include "curve/segmented.h"
108
#include "gamestate/component/types.h"
119

1210

@@ -18,20 +16,22 @@ component_t Live::get_type() const {
1816

1917
void Live::add_attribute(const time::time_t &time,
2018
const nyan::fqon_t &attribute,
21-
std::shared_ptr<curve::Discrete<int64_t>> starting_values) {
22-
this->attribute_values.insert(time, attribute, starting_values);
19+
std::shared_ptr<curve::Segmented<attribute_value_t>> starting_values) {
20+
this->attributes.insert(time, attribute, starting_values);
2321
}
2422

2523
void Live::set_attribute(const time::time_t &time,
2624
const nyan::fqon_t &attribute,
27-
int64_t value) {
28-
auto attribute_value = this->attribute_values.at(time, attribute);
29-
30-
if (attribute_value) {
31-
(**attribute_value)->set_last(time, value);
25+
attribute_value_t value) {
26+
auto attribute_iterator = this->attributes.at(time, attribute);
27+
if (attribute_iterator) {
28+
auto attribute_curve = **attribute_iterator;
29+
auto current_value = attribute_curve->get(time);
30+
attribute_curve->set_last_jump(time, current_value, value);
3231
}
3332
else {
34-
// TODO: fail here
33+
throw Error(MSG(err) << "Attribute not found: " << attribute);
3534
}
3635
}
36+
3737
} // namespace openage::gamestate::component

libopenage/gamestate/component/api/live.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@
1313
#include "time/time.h"
1414

1515

16-
namespace openage::gamestate::component {
16+
namespace openage {
17+
18+
namespace curve {
19+
template <typename T>
20+
class Segmented;
21+
} // namespace curve
22+
23+
namespace gamestate::component {
1724
class Live final : public APIComponent {
1825
public:
1926
using APIComponent::APIComponent;
@@ -29,7 +36,7 @@ class Live final : public APIComponent {
2936
*/
3037
void add_attribute(const time::time_t &time,
3138
const nyan::fqon_t &attribute,
32-
std::shared_ptr<curve::Discrete<int64_t>> starting_values);
39+
std::shared_ptr<curve::Segmented<attribute_value_t>> starting_values);
3340

3441
/**
3542
* Set the value of an attribute at a given time.
@@ -40,16 +47,17 @@ class Live final : public APIComponent {
4047
*/
4148
void set_attribute(const time::time_t &time,
4249
const nyan::fqon_t &attribute,
43-
int64_t value);
50+
attribute_value_t value);
4451

4552
private:
4653
using attribute_storage_t = curve::UnorderedMap<nyan::fqon_t,
47-
std::shared_ptr<curve::Discrete<int64_t>>>;
54+
std::shared_ptr<curve::Segmented<attribute_value_t>>>;
4855

4956
/**
5057
* Map of attribute values by attribute type.
5158
*/
52-
attribute_storage_t attribute_values;
59+
attribute_storage_t attributes;
5360
};
5461

55-
} // namespace openage::gamestate::component
62+
} // namespace gamestate::component
63+
} // namespace openage

libopenage/gamestate/component/types.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22

33
#pragma once
44

5+
#include "util/fixed_point.h"
6+
57

68
namespace openage::gamestate::component {
79

10+
/**
11+
* Type for attribute values.
12+
*/
13+
using attribute_value_t = util::FixedPoint<int64_t, 16>;
14+
815
/**
916
* Types of components.
1017
*/

libopenage/gamestate/entity_factory.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,12 @@ void EntityFactory::init_components(const std::shared_ptr<openage::event::EventL
197197

198198
live->add_attribute(time::TIME_MIN,
199199
attribute.get_name(),
200-
std::make_shared<curve::Discrete<int64_t>>(loop,
201-
0,
202-
"",
203-
nullptr,
204-
start_value));
200+
std::make_shared<curve::Segmented<component::attribute_value_t>>(
201+
loop,
202+
0,
203+
"",
204+
nullptr,
205+
start_value));
205206
}
206207
}
207208
else if (ability_parent == "engine.ability.type.Activity") {

0 commit comments

Comments
 (0)