Skip to content

Commit 16a1df3

Browse files
committed
gamestate: API layer for nyan resistances.
1 parent 1f5dfff commit 16a1df3

File tree

5 files changed

+210
-0
lines changed

5 files changed

+210
-0
lines changed

libopenage/gamestate/api/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_sources(libopenage
77
patch.cpp
88
player_setup.cpp
99
property.cpp
10+
resistance.cpp
1011
sound.cpp
1112
terrain.cpp
1213
types.cpp

libopenage/gamestate/api/definitions.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,43 @@ static const auto EFFECT_DEFS = datastructure::create_const_map<effect_t, nyan::
8181
std::pair(effect_t::DISCRETE_SEND_TO_CONTAINER,
8282
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.effect.discrete.type.SendToContainer"))));
8383

84+
/**
85+
* Maps internal effect types to nyan API values.
86+
*/
87+
static const auto RESISTANCE_DEFS = datastructure::create_const_map<effect_t, nyan::ValueHolder>(
88+
std::pair(effect_t::CONTINUOUS_FLAC_DECREASE,
89+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
90+
"engine.resistance.type.ContinuousFlatAttributeChangeDecrease"))),
91+
std::pair(effect_t::CONTINUOUS_FLAC_INCREASE,
92+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
93+
"engine.resistance.type.ContinuousFlatAttributeChangeIncrease"))),
94+
std::pair(effect_t::CONTINUOUS_LURE,
95+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.resistance.type.Lure"))),
96+
std::pair(effect_t::CONTINUOUS_TRAC_DECREASE,
97+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
98+
"engine.resistance.type.ContinuousTimeRelativeAttributeChangeDecrease"))),
99+
std::pair(effect_t::CONTINUOUS_TRAC_INCREASE,
100+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
101+
"engine.resistance.type.ContinuousTimeRelativeAttributeChangeIncrease"))),
102+
std::pair(effect_t::CONTINUOUS_TRPC_DECREASE,
103+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
104+
"engine.resistance.type.ContinuousTimeRelativeProgressChangeDecrease"))),
105+
std::pair(effect_t::CONTINUOUS_TRPC_INCREASE,
106+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
107+
"engine.resistance.type.ContinuousTimeRelativeProgressChangeIncrease"))),
108+
std::pair(effect_t::DISCRETE_CONVERT,
109+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.resistance.type.Convert"))),
110+
std::pair(effect_t::DISCRETE_FLAC_DECREASE,
111+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
112+
"engine.resistance.type.DiscreteFlatAttributeChangeDecrease"))),
113+
std::pair(effect_t::DISCRETE_FLAC_INCREASE,
114+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
115+
"engine.resistance.type.DiscreteFlatAttributeChangeIncrease"))),
116+
std::pair(effect_t::DISCRETE_MAKE_HARVESTABLE,
117+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.resistance.type.MakeHarvestable"))),
118+
std::pair(effect_t::DISCRETE_SEND_TO_CONTAINER,
119+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.resistance.type.SendToContainer"))));
120+
84121
/**
85122
* Maps API effect types to internal effect types.
86123
*/
@@ -110,6 +147,35 @@ static const auto EFFECT_TYPE_LOOKUP = datastructure::create_const_map<nyan::fqo
110147
std::pair("engine.effect.discrete.type.SendToContainer",
111148
effect_t::DISCRETE_SEND_TO_CONTAINER));
112149

150+
/**
151+
* Maps API resistance types to internal effect types.
152+
*/
153+
static const auto RESISTANCE_TYPE_LOOKUP = datastructure::create_const_map<nyan::fqon_t, effect_t>(
154+
std::pair("engine.resistance.type.ContinuousFlatAttributeChangeDecrease",
155+
effect_t::CONTINUOUS_FLAC_DECREASE),
156+
std::pair("engine.resistance.type.ContinuousFlatAttributeChangeIncrease",
157+
effect_t::CONTINUOUS_FLAC_INCREASE),
158+
std::pair("engine.resistance.type.Lure",
159+
effect_t::CONTINUOUS_LURE),
160+
std::pair("engine.resistance.type.ContinuousTimeRelativeAttributeChangeDecrease",
161+
effect_t::CONTINUOUS_TRAC_DECREASE),
162+
std::pair("engine.resistance.type.ContinuousTimeRelativeAttributeChangeIncrease",
163+
effect_t::CONTINUOUS_TRAC_INCREASE),
164+
std::pair("engine.resistance.type.ContinuousTimeRelativeProgressChangeDecrease",
165+
effect_t::CONTINUOUS_TRPC_DECREASE),
166+
std::pair("engine.resistance.type.ContinuousTimeRelativeProgressChangeIncrease",
167+
effect_t::CONTINUOUS_TRPC_INCREASE),
168+
std::pair("engine.resistance.type.Convert",
169+
effect_t::DISCRETE_CONVERT),
170+
std::pair("engine.resistance.type.DiscreteFlatAttributeChangeDecrease",
171+
effect_t::DISCRETE_FLAC_DECREASE),
172+
std::pair("engine.resistance.type.DiscreteFlatAttributeChangeIncrease",
173+
effect_t::DISCRETE_FLAC_INCREASE),
174+
std::pair("engine.resistance.type.MakeHarvestable",
175+
effect_t::DISCRETE_MAKE_HARVESTABLE),
176+
std::pair("engine.resistance.type.SendToContainer",
177+
effect_t::DISCRETE_SEND_TO_CONTAINER));
178+
113179

114180
/**
115181
* Maps internal ability property types to nyan API values.
@@ -141,6 +207,15 @@ static const auto EFFECT_PROPERTY_DEFS = datastructure::create_const_map<effect_
141207
std::pair(effect_property_t::PRIORITY,
142208
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.effect.property.type.Priority"))));
143209

210+
/**
211+
* Maps internal resistance property types to nyan API values.
212+
*/
213+
static const auto RESISTANCE_PROPERTY_DEFS = datastructure::create_const_map<resistance_property_t, nyan::ValueHolder>(
214+
std::pair(resistance_property_t::COST,
215+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.resistance.property.type.Cost"))),
216+
std::pair(resistance_property_t::STACKED,
217+
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.resistance.property.type.Stacked"))));
218+
144219
/**
145220
* Maps API activity node types to engine node types.
146221
*/
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 "resistance.h"
4+
5+
#include "gamestate/api/definitions.h"
6+
7+
8+
namespace openage::gamestate::api {
9+
10+
bool APIResistance::is_resistance(const nyan::Object &obj) {
11+
for (auto &parent : obj.get_parents()) {
12+
if (parent == "engine.resistance.Resistance") {
13+
return true;
14+
}
15+
}
16+
17+
return false;
18+
}
19+
20+
bool APIResistance::check_effect_type(const nyan::Object &resistance,
21+
const effect_t &type) {
22+
nyan::fqon_t immediate_parent = resistance.get_parents()[0];
23+
nyan::ValueHolder effect_type = RESISTANCE_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 APIResistance::check_property(const nyan::Object &resistance,
32+
const resistance_property_t &property) {
33+
std::shared_ptr<nyan::Dict> properties = resistance.get<nyan::Dict>("Resistance.properties");
34+
nyan::ValueHolder property_type = RESISTANCE_PROPERTY_DEFS.get(property);
35+
36+
return properties->contains(property_type);
37+
}
38+
39+
effect_t APIResistance::get_effect_type(const nyan::Object &resistance) {
40+
nyan::fqon_t immediate_parent = resistance.get_parents()[0];
41+
42+
return RESISTANCE_TYPE_LOOKUP.get(immediate_parent);
43+
}
44+
45+
const nyan::Object APIResistance::get_property(const nyan::Object &resistance,
46+
const resistance_property_t &property) {
47+
std::shared_ptr<nyan::Dict> properties = resistance.get<nyan::Dict>("Resistance.properties");
48+
nyan::ValueHolder property_type = RESISTANCE_PROPERTY_DEFS.get(property);
49+
50+
std::shared_ptr<nyan::View> db_view = resistance.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
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 Resistance objects in the nyan API.
14+
*/
15+
class APIResistance {
16+
public:
17+
/**
18+
* Check if a nyan object is an Resistance (type == \p engine.resistance.Resistance).
19+
*
20+
* @param obj nyan object.
21+
*
22+
* @return true if the object is an resistance, else false.
23+
*/
24+
static bool is_resistance(const nyan::Object &obj);
25+
26+
/**
27+
* Check if an resistance matches a given effect type.
28+
*
29+
* @param resistance \p Resistance nyan object (type == \p engine.resistance.Resistance).
30+
* @param type Effect type.
31+
*
32+
* @return true if the resistance is of the given type, else false.
33+
*/
34+
static bool check_effect_type(const nyan::Object &resistance,
35+
const effect_t &type);
36+
37+
/**
38+
* Check if an resistance has a given property.
39+
*
40+
* @param resistance \p Resistance nyan object (type == \p engine.resistance.Resistance).
41+
* @param property Property type.
42+
*
43+
* @return true if the resistance has the property, else false.
44+
*/
45+
static bool check_property(const nyan::Object &resistance,
46+
const resistance_property_t &property);
47+
48+
/**
49+
* Get the matching effect type of a resistance.
50+
*
51+
* @param resistance \p Resistance nyan object (type == \p engine.resistance.Resistance).
52+
*
53+
* @return Type of the resistance.
54+
*/
55+
static effect_t get_effect_type(const nyan::Object &resistance);
56+
57+
/**
58+
* Get the nyan object for a property from an resistance.
59+
*
60+
* @param resistance \p Resistance nyan object (type == \p engine.resistance.Resistance).
61+
* @param property Property type.
62+
*
63+
* @return \p Property nyan object (type == \p engine.resistance.property.Property).
64+
*/
65+
static const nyan::Object get_property(const nyan::Object &resistance,
66+
const resistance_property_t &property);
67+
};
68+
69+
} // namespace openage::gamestate::api

libopenage/gamestate/api/types.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ enum class effect_property_t {
6363
PRIORITY,
6464
};
6565

66+
/**
67+
* Types of properties for API resistances.
68+
*/
69+
enum class resistance_property_t {
70+
COST,
71+
STACKED,
72+
};
73+
6674
/**
6775
* Types of properties for API patches.
6876
*/

0 commit comments

Comments
 (0)