patch from dragonking:

add a generic way to export a map as a variant object for formula AI
This commit is contained in:
Nils Kneuper 2009-02-07 14:48:01 +00:00
parent a2027440b1
commit 4cf78e2eb5
4 changed files with 24 additions and 1 deletions

View File

@ -11,9 +11,19 @@
See the COPYING file for more details.
*/
#include "callable_objects.hpp"
template <typename T, typename K>
variant convert_map( const std::map<T, K>& input_map ) {
std::map<variant,variant> tmp;
for(typename std::map< T, K>::const_iterator i = input_map.begin(); i != input_map.end(); ++i) {
tmp[ variant(i->first) ] = variant( i->second );
}
return variant( &tmp );
}
variant location_callable::get_value(const std::string& key) const
{
if(key == "x") {
@ -213,6 +223,10 @@ variant unit_callable::get_value(const std::string& key) const
res.push_back( variant(*it) );
}
return variant( &res );
} else if(key == "states") {
const std::map<std::string, std::string>& states_map = u_.get_states();
return convert_map( states_map );
} else if(key == "side") {
return variant(u_.side()-1);
} else if(key == "cost") {
@ -249,6 +263,7 @@ void unit_callable::get_inputs(std::vector<game_logic::formula_input>* inputs) c
inputs->push_back(game_logic::formula_input("total_movement", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("movement_left", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("side", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("states", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("cost", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("vars", FORMULA_READ_ONLY));
}

View File

@ -59,6 +59,8 @@ public: \
} \
};
template <typename T, typename K> variant convert_map( const std::map<T,K>& map );
class terrain_callable : public game_logic::formula_callable {
public:
typedef map_location location;

View File

@ -960,6 +960,11 @@ void unit::heal(int amount)
}
}
const std::map<std::string,std::string>& unit::get_states() const
{
return states_;
}
std::string unit::get_state(const std::string& state) const
{
std::map<std::string,std::string>::const_iterator i = states_.find(state);

View File

@ -164,6 +164,7 @@ public:
bool resting() const { return resting_; }
void set_resting(bool rest) { resting_ = rest; }
const std::map<std::string,std::string>& get_states() const;
std::string get_state(const std::string& state) const;
void set_state(const std::string& state, const std::string& value);