Skip to content

Commit 1f5dfff

Browse files
committed
gamestate: API layer for nyan effects.
1 parent fdf3925 commit 1f5dfff

File tree

5 files changed

+237
-1
lines changed

5 files changed

+237
-1
lines changed

libopenage/gamestate/api/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_sources(libopenage
33
activity.cpp
44
animation.cpp
55
definitions.cpp
6+
effect.cpp
67
patch.cpp
78
player_setup.cpp
89
property.cpp

libopenage/gamestate/api/definitions.h

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,74 @@ static const auto ABILITY_DEFS = datastructure::create_const_map<ability_t, nyan
4545
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.ability.type.Turn"))));
4646

4747
/**
48-
* Maps internal property types to nyan API values.
48+
* Maps internal effect types to nyan API values.
49+
*/
50+
static const auto EFFECT_DEFS = datastructure::create_const_map<effect_t, nyan::ValueHolder>(
51+
std::pair(effect_t::CONTINUOUS_FLAC_DECREASE,
52+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
53+
"engine.effect.continuous.flat_attribute_change.type.FlatAttributeChangeDecrease"))),
54+
std::pair(effect_t::CONTINUOUS_FLAC_INCREASE,
55+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
56+
"engine.effect.continuous.flat_attribute_change.type.FlatAttributeChangeIncrease"))),
57+
std::pair(effect_t::CONTINUOUS_LURE,
58+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.effect.continuous.type.Lure"))),
59+
std::pair(effect_t::CONTINUOUS_TRAC_DECREASE,
60+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
61+
"engine.effect.continuous.time_relative_attribute_change.type.TimeRelativeAttributeChangeDecrease"))),
62+
std::pair(effect_t::CONTINUOUS_TRAC_INCREASE,
63+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
64+
"engine.effect.continuous.time_relative_attribute_change.type.TimeRelativeAttributeChangeIncrease"))),
65+
std::pair(effect_t::CONTINUOUS_TRPC_DECREASE,
66+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
67+
"engine.effect.continuous.time_relative_progress_change.type.TimeRelativeProgressChangeDecrease"))),
68+
std::pair(effect_t::CONTINUOUS_TRPC_INCREASE,
69+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
70+
"engine.effect.continuous.time_relative_progress_change.type.TimeRelativeProgressChangeIncrease"))),
71+
std::pair(effect_t::DISCRETE_CONVERT,
72+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.effect.discrete.Convert"))),
73+
std::pair(effect_t::DISCRETE_FLAC_DECREASE,
74+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
75+
"engine.effect.discrete.flat_attribute_change.type.FlatAttributeChangeDecrease"))),
76+
std::pair(effect_t::DISCRETE_FLAC_INCREASE,
77+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
78+
"engine.effect.discrete.flat_attribute_change.type.FlatAttributeChangeIncrease"))),
79+
std::pair(effect_t::DISCRETE_MAKE_HARVESTABLE,
80+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.effect.discrete.type.MakeHarvestable"))),
81+
std::pair(effect_t::DISCRETE_SEND_TO_CONTAINER,
82+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.effect.discrete.type.SendToContainer"))));
83+
84+
/**
85+
* Maps API effect types to internal effect types.
86+
*/
87+
static const auto EFFECT_TYPE_LOOKUP = datastructure::create_const_map<nyan::fqon_t, effect_t>(
88+
std::pair("engine.effect.continuous.flat_attribute_change.type.FlatAttributeChangeDecrease",
89+
effect_t::CONTINUOUS_FLAC_DECREASE),
90+
std::pair("engine.effect.continuous.flat_attribute_change.type.FlatAttributeChangeIncrease",
91+
effect_t::CONTINUOUS_FLAC_INCREASE),
92+
std::pair("engine.effect.continuous.type.Lure",
93+
effect_t::CONTINUOUS_LURE),
94+
std::pair("engine.effect.continuous.time_relative_attribute_change.type.TimeRelativeAttributeChangeDecrease",
95+
effect_t::CONTINUOUS_TRAC_DECREASE),
96+
std::pair("engine.effect.continuous.time_relative_attribute_change.type.TimeRelativeAttributeChangeIncrease",
97+
effect_t::CONTINUOUS_TRAC_INCREASE),
98+
std::pair("engine.effect.continuous.time_relative_progress_change.type.TimeRelativeProgressChangeDecrease",
99+
effect_t::CONTINUOUS_TRPC_DECREASE),
100+
std::pair("engine.effect.continuous.time_relative_progress_change.type.TimeRelativeProgressChangeIncrease",
101+
effect_t::CONTINUOUS_TRPC_INCREASE),
102+
std::pair("engine.effect.discrete.Convert",
103+
effect_t::DISCRETE_CONVERT),
104+
std::pair("engine.effect.discrete.flat_attribute_change.type.FlatAttributeChangeDecrease",
105+
effect_t::DISCRETE_FLAC_DECREASE),
106+
std::pair("engine.effect.discrete.flat_attribute_change.type.FlatAttributeChangeIncrease",
107+
effect_t::DISCRETE_FLAC_INCREASE),
108+
std::pair("engine.effect.discrete.type.MakeHarvestable",
109+
effect_t::DISCRETE_MAKE_HARVESTABLE),
110+
std::pair("engine.effect.discrete.type.SendToContainer",
111+
effect_t::DISCRETE_SEND_TO_CONTAINER));
112+
113+
114+
/**
115+
* Maps internal ability property types to nyan API values.
49116
*/
50117
static const auto ABILITY_PROPERTY_DEFS = datastructure::create_const_map<ability_property_t, nyan::ValueHolder>(
51118
std::pair(ability_property_t::ANIMATED,
@@ -61,6 +128,19 @@ static const auto ABILITY_PROPERTY_DEFS = datastructure::create_const_map<abilit
61128
std::pair(ability_property_t::LOCK,
62129
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.ability.property.type.Lock"))));
63130

131+
/**
132+
* Maps internal effect property types to nyan API values.
133+
*/
134+
static const auto EFFECT_PROPERTY_DEFS = datastructure::create_const_map<effect_property_t, nyan::ValueHolder>(
135+
std::pair(effect_property_t::AREA,
136+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.effect.property.type.Area"))),
137+
std::pair(effect_property_t::COST,
138+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.effect.property.type.Cost"))),
139+
std::pair(effect_property_t::DIPLOMATIC,
140+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.effect.property.type.Diplomatic"))),
141+
std::pair(effect_property_t::PRIORITY,
142+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.effect.property.type.Priority"))));
143+
64144
/**
65145
* Maps API activity node types to engine node types.
66146
*/
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2024-2024 the openage authors. See copying.md for legal info.
2+
3+
#include "effect.h"
4+
5+
#include "gamestate/api/definitions.h"
6+
7+
8+
namespace openage::gamestate::api {
9+
10+
bool APIEffect::is_effect(const nyan::Object &obj) {
11+
for (auto &parent : obj.get_parents()) {
12+
if (parent == "engine.effect.Effect") {
13+
return true;
14+
}
15+
}
16+
17+
return false;
18+
}
19+
20+
bool APIEffect::check_type(const nyan::Object &effect,
21+
const effect_t &type) {
22+
nyan::fqon_t immediate_parent = effect.get_parents()[0];
23+
nyan::ValueHolder effect_type = EFFECT_DEFS.get(type);
24+
25+
std::shared_ptr<nyan::ObjectValue> effect_val = std::dynamic_pointer_cast<nyan::ObjectValue>(
26+
effect_type.get_ptr());
27+
28+
return effect_val->get_name() == immediate_parent;
29+
}
30+
31+
bool APIEffect::check_property(const nyan::Object &effect,
32+
const effect_property_t &property) {
33+
std::shared_ptr<nyan::Dict> properties = effect.get<nyan::Dict>("Effect.properties");
34+
nyan::ValueHolder property_type = EFFECT_PROPERTY_DEFS.get(property);
35+
36+
return properties->contains(property_type);
37+
}
38+
39+
effect_t APIEffect::get_type(const nyan::Object &effect) {
40+
nyan::fqon_t immediate_parent = effect.get_parents()[0];
41+
42+
return EFFECT_TYPE_LOOKUP.get(immediate_parent);
43+
}
44+
45+
const nyan::Object APIEffect::get_property(const nyan::Object &effect,
46+
const effect_property_t &property) {
47+
std::shared_ptr<nyan::Dict> properties = effect.get<nyan::Dict>("Effect.properties");
48+
nyan::ValueHolder property_type = EFFECT_PROPERTY_DEFS.get(property);
49+
50+
std::shared_ptr<nyan::View> db_view = effect.get_view();
51+
std::shared_ptr<nyan::ObjectValue> property_val = std::dynamic_pointer_cast<nyan::ObjectValue>(
52+
properties->get().at(property_type).get_ptr());
53+
54+
return db_view->get_object(property_val->get_name());
55+
}
56+
57+
} // namespace openage::gamestate::api

libopenage/gamestate/api/effect.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2024-2024 the openage authors. See copying.md for legal info.
2+
3+
#pragma once
4+
5+
#include <nyan/nyan.h>
6+
7+
#include "gamestate/api/types.h"
8+
9+
10+
namespace openage::gamestate::api {
11+
12+
/**
13+
* Helper class for extracting values from Effect objects in the nyan API.
14+
*/
15+
class APIEffect {
16+
public:
17+
/**
18+
* Check if a nyan object is an Effect (type == \p engine.effect.Effect).
19+
*
20+
* @param obj nyan object.
21+
*
22+
* @return true if the object is an effect, else false.
23+
*/
24+
static bool is_effect(const nyan::Object &obj);
25+
26+
/**
27+
* Check if an effect is of a given type.
28+
*
29+
* @param effect \p Effect nyan object (type == \p engine.effect.Effect).
30+
* @param type Effect type.
31+
*
32+
* @return true if the effect is of the given type, else false.
33+
*/
34+
static bool check_type(const nyan::Object &effect,
35+
const effect_t &type);
36+
37+
/**
38+
* Check if an effect has a given property.
39+
*
40+
* @param effect \p Effect nyan object (type == \p engine.effect.Effect).
41+
* @param property Property type.
42+
*
43+
* @return true if the effect has the property, else false.
44+
*/
45+
static bool check_property(const nyan::Object &effect,
46+
const effect_property_t &property);
47+
48+
/**
49+
* Get the type of an effect.
50+
*
51+
* @param effect \p Effect nyan object (type == \p engine.effect.Effect).
52+
*
53+
* @return Type of the effect.
54+
*/
55+
static effect_t get_type(const nyan::Object &effect);
56+
57+
/**
58+
* Get the nyan object for a property from an effect.
59+
*
60+
* @param effect \p Effect nyan object (type == \p engine.effect.Effect).
61+
* @param property Property type.
62+
*
63+
* @return \p Property nyan object (type == \p engine.effect.property.Property).
64+
*/
65+
static const nyan::Object get_property(const nyan::Object &effect,
66+
const effect_property_t &property);
67+
};
68+
69+
} // namespace openage::gamestate::api

libopenage/gamestate/api/types.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ enum class ability_t {
2222
// TODO
2323
};
2424

25+
/**
26+
* Types of effects for API objects.
27+
*/
28+
enum class effect_t {
29+
CONTINUOUS_FLAC_DECREASE,
30+
CONTINUOUS_FLAC_INCREASE,
31+
CONTINUOUS_LURE,
32+
CONTINUOUS_TRAC_DECREASE,
33+
CONTINUOUS_TRAC_INCREASE,
34+
CONTINUOUS_TRPC_DECREASE,
35+
CONTINUOUS_TRPC_INCREASE,
36+
37+
DISCRETE_CONVERT,
38+
DISCRETE_FLAC_DECREASE,
39+
DISCRETE_FLAC_INCREASE,
40+
DISCRETE_MAKE_HARVESTABLE,
41+
DISCRETE_SEND_TO_CONTAINER,
42+
};
43+
2544
/**
2645
* Types of properties for API abilities.
2746
*/
@@ -34,6 +53,16 @@ enum class ability_property_t {
3453
LOCK,
3554
};
3655

56+
/**
57+
* Types of properties for API effects.
58+
*/
59+
enum class effect_property_t {
60+
AREA,
61+
COST,
62+
DIPLOMATIC,
63+
PRIORITY,
64+
};
65+
3766
/**
3867
* Types of properties for API patches.
3968
*/

0 commit comments

Comments
 (0)