Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #define GETTEXT_DOMAIN "wesnoth-test"
00017
00018 #include "tests/utils/play_scenario.hpp"
00019 #include "tests/utils/game_config_manager.hpp"
00020 #include "tests/utils/fake_display.hpp"
00021
00022 #include "game_display.hpp"
00023 #include "gamestatus.hpp"
00024 #include "playcampaign.hpp"
00025 #include "unit.hpp"
00026 #include "unit_map.hpp"
00027
00028 namespace test_utils {
00029 play_scenario::play_scenario(const std::string& id) :
00030 id_(id),
00031 source_(),
00032 game_config_(get_test_config_ref()),
00033 current_time_(80),
00034 end_pos_()
00035 {
00036 add_initial_signals();
00037 }
00038
00039 void play_scenario::add_initial_signals()
00040 {
00041 source_.type_key(current_time_++, SDLK_RETURN);
00042 current_time_+= 100;
00043
00044 }
00045
00046 struct add_key_type_to_source {
00047 add_key_type_to_source(fake_event_source& source, timing& time) :
00048 source_(source),
00049 time_(time)
00050 {}
00051 void operator()(const std::string::value_type& c)
00052 {
00053
00054 source_.type_key(time_++, static_cast<SDLKey>(c));
00055 }
00056 private:
00057 fake_event_source& source_;
00058 timing& time_;
00059 };
00060
00061 void play_scenario::add_formula_command(const std::string& command)
00062 {
00063
00064 source_.type_key(current_time_++, SDLK_f);
00065 std::for_each(command.begin(), command.end(),
00066 add_key_type_to_source(source_, current_time_));
00067 source_.type_key(current_time_++, SDLK_RETURN);
00068 }
00069
00070 class end_position_collector : public event_node {
00071 game_state state_;
00072 unit_map units_;
00073
00074 public:
00075 end_position_collector(const size_t time) :
00076 event_node(time, SDL_Event()),
00077 state_(),
00078 units_()
00079 {
00080 }
00081
00082 virtual void fire_event()
00083 {
00084
00085 units_ = game_display::get_singleton()->get_units();
00086 }
00087
00088 game_state& get_state()
00089 {
00090 return state_;
00091 }
00092
00093 unit_map& get_units()
00094 {
00095 return units_;
00096 }
00097 };
00098
00099 void play_scenario::play()
00100 {
00101
00102 end_pos_.reset(new end_position_collector(current_time_++));
00103 end_position_collector* end = static_cast<end_position_collector*>(end_pos_.get());
00104 source_.add_event(end_pos_);
00105
00106 source_.type_key(current_time_++, SDLK_COLON, SDLMod(KMOD_LSHIFT | KMOD_SHIFT) );
00107 source_.type_key(current_time_++, SDLK_q);
00108 source_.type_key(current_time_++, SDLK_EXCLAIM);
00109 source_.type_key(current_time_++, SDLK_RETURN);
00110
00111 game_state& state = end->get_state();
00112 state.classification().campaign_type = "test";
00113 state.classification().scenario = id_;
00114 play_game(get_fake_display(1024, 768), state, game_config_);
00115 }
00116
00117 std::string play_scenario::get_unit_id(const map_location &loc)
00118 {
00119 if (!end_pos_)
00120 return std::string();
00121
00122 end_position_collector* end = static_cast<end_position_collector*>(end_pos_.get());
00123
00124 unit_map::iterator it = end->get_units().find(loc);
00125 if (it == end->get_units().end())
00126 return std::string();
00127 return it->id();
00128 }
00129
00130 }