|
| 1 | +// Copyright 2024-2024 the openage authors. See copying.md for legal info. |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include <concepts> |
| 6 | +#include <functional> |
| 7 | +#include <memory> |
| 8 | +#include <string> |
| 9 | +#include <unordered_map> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +#include "gamestate/activity/node.h" |
| 13 | +#include "gamestate/activity/types.h" |
| 14 | +#include "time/time.h" |
| 15 | + |
| 16 | + |
| 17 | +namespace openage::gamestate { |
| 18 | +class GameEntity; |
| 19 | + |
| 20 | +namespace activity { |
| 21 | + |
| 22 | +/** |
| 23 | + * Chooses one of its output nodes based on enum values. |
| 24 | + * |
| 25 | + * In comparison to the XOR gate, this node type does not tie individual |
| 26 | + * conditions to each node. Instead, it operates on a single function that |
| 27 | + * returns a key for a lookup dict. The lookup dict maps these keys to output |
| 28 | + * node ID. |
| 29 | + * |
| 30 | + * This type of gate is easier to use for simpler branch switches based on |
| 31 | + * similar conditions, e.g. a branching based on the value of an enum. |
| 32 | + */ |
| 33 | +class XorSwitchGate : public Node { |
| 34 | +public: |
| 35 | + /** |
| 36 | + * Type used as lookup key for the lookup dict. |
| 37 | + */ |
| 38 | + using lookup_key_t = int; |
| 39 | + |
| 40 | + /** |
| 41 | + * Function that retrieves a lookup key for the lookup dict from |
| 42 | + * the current state of an entity. |
| 43 | + * |
| 44 | + * @param time Current simulation time. |
| 45 | + * @param entity Entity that is executing the activity. |
| 46 | + * |
| 47 | + * @return Lookup key. |
| 48 | + */ |
| 49 | + using lookup_function_t = std::function<lookup_key_t(const time::time_t &, |
| 50 | + const std::shared_ptr<gamestate::GameEntity> &)>; |
| 51 | + |
| 52 | + /** |
| 53 | + * Lookup dict that maps lookup keys to output node IDs. |
| 54 | + */ |
| 55 | + using lookup_dict_t = std::unordered_map<lookup_key_t, std::shared_ptr<Node>>; |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates a new XOR switch gate node. |
| 59 | + * |
| 60 | + * @param id Unique identifier of the node. |
| 61 | + * @param label Human-readable label of the node (optional). |
| 62 | + */ |
| 63 | + XorSwitchGate(node_id_t id, |
| 64 | + node_label_t label = "ExclusiveSwitchGateway"); |
| 65 | + |
| 66 | + /** |
| 67 | + * Creates a new XOR switch gate node. |
| 68 | + * |
| 69 | + * @param id Unique identifier of the node. |
| 70 | + * @param label Human-readable label of the node. |
| 71 | + * @param lookup_func Function that looks up the key to the lookup dict. |
| 72 | + * @param lookup_dict Initial lookup dict that maps lookup keys to output node IDs. |
| 73 | + * @param default_node Default output node. Chosen if no lookup entry is defined. |
| 74 | + */ |
| 75 | + XorSwitchGate(node_id_t id, |
| 76 | + node_label_t label, |
| 77 | + const lookup_function_t &lookup_func, |
| 78 | + const lookup_dict_t &lookup_dict, |
| 79 | + const std::shared_ptr<Node> &default_node); |
| 80 | + |
| 81 | + virtual ~XorSwitchGate() = default; |
| 82 | + |
| 83 | + inline node_t get_type() const override { |
| 84 | + return node_t::XOR_SWITCH_GATE; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Set the output node for a given lookup key. |
| 89 | + * |
| 90 | + * @param output Output node. |
| 91 | + * @param key Enumeration value. |
| 92 | + */ |
| 93 | + void set_output(const std::shared_ptr<Node> &output, |
| 94 | + const lookup_key_t &key); |
| 95 | + |
| 96 | + /** |
| 97 | + * Get the lookup function for determining the output nodes. |
| 98 | + * |
| 99 | + * @return Lookup function. |
| 100 | + */ |
| 101 | + const lookup_function_t &get_lookup_func() const; |
| 102 | + |
| 103 | + /** |
| 104 | + * Set the lookup function for determining the output nodes. |
| 105 | + * |
| 106 | + * @param lookup_func Lookup function. |
| 107 | + */ |
| 108 | + void set_lookup_func(const lookup_function_t &lookup_func); |
| 109 | + |
| 110 | + /** |
| 111 | + * Get the lookup dict for the output nodes. |
| 112 | + * |
| 113 | + * @return Lookup dict. |
| 114 | + */ |
| 115 | + const lookup_dict_t &get_lookup_dict() const; |
| 116 | + |
| 117 | + /** |
| 118 | + * Get the default output node. |
| 119 | + * |
| 120 | + * @return Default output node. |
| 121 | + */ |
| 122 | + const std::shared_ptr<Node> &get_default() const; |
| 123 | + |
| 124 | + /** |
| 125 | + * Set the the default output node. |
| 126 | + * |
| 127 | + * This node is chosen if the lookup dict does not contain an entry for the |
| 128 | + * lookup key returned by the lookup function. |
| 129 | + * |
| 130 | + * @param node Default output node. |
| 131 | + */ |
| 132 | + void set_default(const std::shared_ptr<Node> &node); |
| 133 | + |
| 134 | +private: |
| 135 | + /** |
| 136 | + * Determines the lookup key for the lookup dict from the current state. |
| 137 | + */ |
| 138 | + lookup_function_t lookup_func; |
| 139 | + |
| 140 | + /** |
| 141 | + * Maps lookup keys to output node IDs. |
| 142 | + */ |
| 143 | + lookup_dict_t lookup_dict; |
| 144 | + |
| 145 | + /** |
| 146 | + * Default output node. Chosen if no lookup entry is defined. |
| 147 | + */ |
| 148 | + std::shared_ptr<Node> default_node; |
| 149 | +}; |
| 150 | + |
| 151 | +} // namespace activity |
| 152 | +} // namespace openage::gamestate |
0 commit comments