Skip to content

Commit

Permalink
gamestate: API layer for nyan resistances.
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed Sep 7, 2024
1 parent e883868 commit 1bddf5f
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 0 deletions.
1 change: 1 addition & 0 deletions libopenage/gamestate/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_sources(libopenage
patch.cpp
player_setup.cpp
property.cpp
resistance.cpp
sound.cpp
terrain.cpp
types.cpp
Expand Down
75 changes: 75 additions & 0 deletions libopenage/gamestate/api/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,43 @@ static const auto EFFECT_DEFS = datastructure::create_const_map<effect_t, nyan::
std::pair(effect_t::DISCRETE_SEND_TO_CONTAINER,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.effect.discrete.type.SendToContainer"))));

/**
* Maps internal effect types to nyan API values.
*/
static const auto RESISTANCE_DEFS = datastructure::create_const_map<effect_t, nyan::ValueHolder>(
std::pair(effect_t::CONTINUOUS_FLAC_DECREASE,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
"engine.resistance.type.ContinuousFlatAttributeChangeDecrease"))),
std::pair(effect_t::CONTINUOUS_FLAC_INCREASE,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
"engine.resistance.type.ContinuousFlatAttributeChangeIncrease"))),
std::pair(effect_t::CONTINUOUS_LURE,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.resistance.type.Lure"))),
std::pair(effect_t::CONTINUOUS_TRAC_DECREASE,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
"engine.resistance.type.ContinuousTimeRelativeAttributeChangeDecrease"))),
std::pair(effect_t::CONTINUOUS_TRAC_INCREASE,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
"engine.resistance.type.ContinuousTimeRelativeAttributeChangeIncrease"))),
std::pair(effect_t::CONTINUOUS_TRPC_DECREASE,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
"engine.resistance.type.ContinuousTimeRelativeProgressChangeDecrease"))),
std::pair(effect_t::CONTINUOUS_TRPC_INCREASE,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
"engine.resistance.type.ContinuousTimeRelativeProgressChangeIncrease"))),
std::pair(effect_t::DISCRETE_CONVERT,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.resistance.type.Convert"))),
std::pair(effect_t::DISCRETE_FLAC_DECREASE,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
"engine.resistance.type.DiscreteFlatAttributeChangeDecrease"))),
std::pair(effect_t::DISCRETE_FLAC_INCREASE,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>(
"engine.resistance.type.DiscreteFlatAttributeChangeIncrease"))),
std::pair(effect_t::DISCRETE_MAKE_HARVESTABLE,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.resistance.type.MakeHarvestable"))),
std::pair(effect_t::DISCRETE_SEND_TO_CONTAINER,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.resistance.type.SendToContainer"))));

/**
* Maps API effect types to internal effect types.
*/
Expand Down Expand Up @@ -110,6 +147,35 @@ static const auto EFFECT_TYPE_LOOKUP = datastructure::create_const_map<nyan::fqo
std::pair("engine.effect.discrete.type.SendToContainer",
effect_t::DISCRETE_SEND_TO_CONTAINER));

/**
* Maps API resistance types to internal effect types.
*/
static const auto RESISTANCE_TYPE_LOOKUP = datastructure::create_const_map<nyan::fqon_t, effect_t>(
std::pair("engine.resistance.type.ContinuousFlatAttributeChangeDecrease",
effect_t::CONTINUOUS_FLAC_DECREASE),
std::pair("engine.resistance.type.ContinuousFlatAttributeChangeIncrease",
effect_t::CONTINUOUS_FLAC_INCREASE),
std::pair("engine.resistance.type.Lure",
effect_t::CONTINUOUS_LURE),
std::pair("engine.resistance.type.ContinuousTimeRelativeAttributeChangeDecrease",
effect_t::CONTINUOUS_TRAC_DECREASE),
std::pair("engine.resistance.type.ContinuousTimeRelativeAttributeChangeIncrease",
effect_t::CONTINUOUS_TRAC_INCREASE),
std::pair("engine.resistance.type.ContinuousTimeRelativeProgressChangeDecrease",
effect_t::CONTINUOUS_TRPC_DECREASE),
std::pair("engine.resistance.type.ContinuousTimeRelativeProgressChangeIncrease",
effect_t::CONTINUOUS_TRPC_INCREASE),
std::pair("engine.resistance.type.Convert",
effect_t::DISCRETE_CONVERT),
std::pair("engine.resistance.type.DiscreteFlatAttributeChangeDecrease",
effect_t::DISCRETE_FLAC_DECREASE),
std::pair("engine.resistance.type.DiscreteFlatAttributeChangeIncrease",
effect_t::DISCRETE_FLAC_INCREASE),
std::pair("engine.resistance.type.MakeHarvestable",
effect_t::DISCRETE_MAKE_HARVESTABLE),
std::pair("engine.resistance.type.SendToContainer",
effect_t::DISCRETE_SEND_TO_CONTAINER));


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

/**
* Maps internal resistance property types to nyan API values.
*/
static const auto RESISTANCE_PROPERTY_DEFS = datastructure::create_const_map<resistance_property_t, nyan::ValueHolder>(
std::pair(resistance_property_t::COST,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.resistance.property.type.Cost"))),
std::pair(resistance_property_t::STACKED,
nyan::ValueHolder(std::make_shared<nyan::ObjectValue>("engine.resistance.property.type.Stacked"))));

/**
* Maps API activity node types to engine node types.
*/
Expand Down
57 changes: 57 additions & 0 deletions libopenage/gamestate/api/resistance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2024-2024 the openage authors. See copying.md for legal info.

#include "resistance.h"

#include "gamestate/api/definitions.h"


namespace openage::gamestate::api {

bool APIResistance::is_resistance(const nyan::Object &obj) {
for (auto &parent : obj.get_parents()) {
if (parent == "engine.resistance.Resistance") {
return true;
}
}

return false;
}

bool APIResistance::check_effect_type(const nyan::Object &resistance,
const effect_t &type) {
nyan::fqon_t immediate_parent = resistance.get_parents()[0];
nyan::ValueHolder effect_type = RESISTANCE_DEFS.get(type);

std::shared_ptr<nyan::ObjectValue> effect_val = std::dynamic_pointer_cast<nyan::ObjectValue>(
effect_type.get_ptr());

return effect_val->get_name() == immediate_parent;
}

bool APIResistance::check_property(const nyan::Object &resistance,
const resistance_property_t &property) {
std::shared_ptr<nyan::Dict> properties = resistance.get<nyan::Dict>("Resistance.properties");
nyan::ValueHolder property_type = RESISTANCE_PROPERTY_DEFS.get(property);

return properties->contains(property_type);
}

effect_t APIResistance::get_effect_type(const nyan::Object &resistance) {
nyan::fqon_t immediate_parent = resistance.get_parents()[0];

return RESISTANCE_TYPE_LOOKUP.get(immediate_parent);
}

const nyan::Object APIResistance::get_property(const nyan::Object &resistance,
const resistance_property_t &property) {
std::shared_ptr<nyan::Dict> properties = resistance.get<nyan::Dict>("Resistance.properties");
nyan::ValueHolder property_type = RESISTANCE_PROPERTY_DEFS.get(property);

std::shared_ptr<nyan::View> db_view = resistance.get_view();
std::shared_ptr<nyan::ObjectValue> property_val = std::dynamic_pointer_cast<nyan::ObjectValue>(
properties->get().at(property_type).get_ptr());

return db_view->get_object(property_val->get_name());
}

} // namespace openage::gamestate::api
69 changes: 69 additions & 0 deletions libopenage/gamestate/api/resistance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2024-2024 the openage authors. See copying.md for legal info.

#pragma once

#include <nyan/nyan.h>

#include "gamestate/api/types.h"


namespace openage::gamestate::api {

/**
* Helper class for extracting values from Resistance objects in the nyan API.
*/
class APIResistance {
public:
/**
* Check if a nyan object is an Resistance (type == \p engine.resistance.Resistance).
*
* @param obj nyan object.
*
* @return true if the object is an resistance, else false.
*/
static bool is_resistance(const nyan::Object &obj);

/**
* Check if an resistance matches a given effect type.
*
* @param resistance \p Resistance nyan object (type == \p engine.resistance.Resistance).
* @param type Effect type.
*
* @return true if the resistance is of the given type, else false.
*/
static bool check_effect_type(const nyan::Object &resistance,
const effect_t &type);

/**
* Check if an resistance has a given property.
*
* @param resistance \p Resistance nyan object (type == \p engine.resistance.Resistance).
* @param property Property type.
*
* @return true if the resistance has the property, else false.
*/
static bool check_property(const nyan::Object &resistance,
const resistance_property_t &property);

/**
* Get the matching effect type of a resistance.
*
* @param resistance \p Resistance nyan object (type == \p engine.resistance.Resistance).
*
* @return Type of the resistance.
*/
static effect_t get_effect_type(const nyan::Object &resistance);

/**
* Get the nyan object for a property from an resistance.
*
* @param resistance \p Resistance nyan object (type == \p engine.resistance.Resistance).
* @param property Property type.
*
* @return \p Property nyan object (type == \p engine.resistance.property.Property).
*/
static const nyan::Object get_property(const nyan::Object &resistance,
const resistance_property_t &property);
};

} // namespace openage::gamestate::api
8 changes: 8 additions & 0 deletions libopenage/gamestate/api/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ enum class effect_property_t {
PRIORITY,
};

/**
* Types of properties for API resistances.
*/
enum class resistance_property_t {
COST,
STACKED,
};

/**
* Types of properties for API patches.
*/
Expand Down

0 comments on commit 1bddf5f

Please sign in to comment.