Esempio n. 1
0
void playsingle_controller::play_ai_turn(){
	LOG_NG << "is ai...\n";
	end_turn_enable(false);
	browse_ = true;
	gui_->recalculate_minimap();

	const cursor::setter cursor_setter(cursor::WAIT);

	// Correct an oddball case where a human could have left delayed shroud
	// updates on before giving control to the AI. (The AI does not bother
	// with the undo stack, so it cannot delay shroud updates.)
	team & cur_team = current_team();
	if ( !cur_team.auto_shroud_updates() ) {
		// We just took control, so the undo stack is empty. We still need
		// to record this change for the replay though.
		recorder.add_auto_shroud(true);
		cur_team.set_auto_shroud_updates(true);
	}

	turn_info turn_data(player_number_, replay_sender_);

	try {
		ai::manager::play_turn(player_number_);
	} catch (end_turn_exception&) {
	}
	recorder.end_turn();
	turn_data.sync_network();

	gui_->recalculate_minimap();
	gui_->invalidate_unit();
	gui_->invalidate_game_status();
	gui_->invalidate_all();
	gui_->draw();
	gui_->delay(100);
}
void playsingle_controller::play_ai_turn(){
	LOG_NG << "is ai...\n";
	gui_->enable_menu("endturn", false);
	browse_ = true;
	gui_->recalculate_minimap();

	const cursor::setter cursor_setter(cursor::WAIT);

	turn_info turn_data(player_number_, replay_sender_);

	try {
		ai::manager::play_turn(player_number_);
	} catch (end_turn_exception&) {
	}
	recorder.end_turn();
	turn_data.sync_network();

	gui_->recalculate_minimap();
	::clear_shroud(player_number_);
	gui_->invalidate_unit();
	gui_->invalidate_game_status();
	gui_->invalidate_all();
	gui_->draw();
	gui_->delay(100);
}
Esempio n. 3
0
void playmp_controller::play_network_turn(){
	LOG_NG << "is networked...\n";

	gui_->set_theme_object_active("undo", false);
	gui_->set_theme_object_active("endturn", false);
	turn_info turn_data(player_number_, replay_sender_, undo_stack_);
	turn_data.host_transfer().attach_handler(this);

	for(;;) {

		if (!network_processing_stopped_){
			bool turn_ended = false;
			config cfg;

			network::connection from = network::null_connection;

			if (data_backlog_.empty() == false) {
				cfg = data_backlog_.front();
				data_backlog_.pop_front();

			} else if (!received_data_cfg_.empty()) {
				cfg = received_data_cfg_.front();
				received_data_cfg_.erase(received_data_cfg_.begin());
				from = lobby->transit.conn();
			}

			if (!cfg.empty()) {
				if (skip_replay_ && replay_last_turn_ <= turn()) {
					skip_replay_ = false;
				}
				const turn_info::PROCESS_DATA_RESULT result = turn_data.process_network_data(cfg, from, data_backlog_, skip_replay_);
				if (result == turn_info::PROCESS_RESTART_TURN) {
					player_type_changed_ = true;
					return;
				} else if (result == turn_info::PROCESS_END_TURN) {
					turn_ended = true;
					break;
				}
			}
			if (turn_ended) {
				break;
			}
		}

		play_slice();
		check_end_level();

		if (!network_processing_stopped_){
			turn_data.send_data();
		}

		gui_->draw();
	}

	turn_data.host_transfer().detach_handler(this);
	LOG_NG << "finished networked...\n";
	return;
}
// @save: It isn't mean this is loaded game. it mean whether execute auto-save. 
//        false: this is first turn of loaded game, true: this isn't first turn or this isn't loaded game.
void playsingle_controller::play_turn(bool save)
{
	gui_->new_turn();
	gui_->invalidate_game_status();
	events::raise_draw_event();

	for (player_number_ = first_player_; player_number_ <= int(teams_.size()); ++player_number_) {
		unit_display::player_number_ = player_number_;
		// If a side is empty skip over it.
		if (current_team().is_empty()) continue;
		try {
			save_blocker blocker;
			init_side(player_number_ - 1, false, save || (player_number_ != first_player_));
		} catch (end_turn_exception) {
			if (current_team().is_network() == false) {
				turn_info turn_data(player_number_, replay_sender_, undo_stack_);
				recorder.end_turn();
				turn_data.sync_network();
			}
			continue;
		}

		// If a side is dead end the turn.
		if (current_team().is_human() && side_units(player_number_) == 0) {
			turn_info turn_data(player_number_, replay_sender_, undo_stack_);
			recorder.end_turn();
			turn_data.sync_network();
			continue;
		}

		play_side(player_number_, save);

		finish_side_turn();

		check_victory();

		//if loading a savegame, network turns might not have reset this yet
		loading_game_ = false;
	}

	// Time has run out
	check_time_over();

	finish_turn();
}
Esempio n. 5
0
void playmp_controller::play_network_turn(){
	LOG_NG << "is networked...\n";

	gui_->enable_menu("endturn", false);
	turn_info turn_data(player_number_, replay_sender_, undo_stack_);
	turn_data.host_transfer().attach_handler(this);

	for(;;) {

		if (!network_processing_stopped_){
			bool have_data = false;
			config cfg;

			network::connection from = network::null_connection;

			if(data_backlog_.empty() == false) {
				have_data = true;
				cfg = data_backlog_.front();
				data_backlog_.pop_front();
			} else {
				from = network::receive_data(cfg);
				have_data = from != network::null_connection;
			}

			if(have_data) {
				if (skip_replay_ && replay_last_turn_ <= turn()){
						skip_replay_ = false;
				}
				const turn_info::PROCESS_DATA_RESULT result = turn_data.process_network_data(cfg, from, data_backlog_, skip_replay_);
				if (result == turn_info::PROCESS_RESTART_TURN) {
					player_type_changed_ = true;
					return;
				} else if (result == turn_info::PROCESS_END_TURN) {
					break;
				}
			}
		}

		play_slice();
		check_end_level();

		if (!network_processing_stopped_){
			turn_data.send_data();
		}

		gui_->draw();
	}

	turn_data.host_transfer().detach_handler(this);
	LOG_NG << "finished networked...\n";
	return;
}
Esempio n. 6
0
void playmp_controller::handle_generic_event(const std::string& name){
	turn_info turn_data(player_number_, replay_sender_, undo_stack_);

	if (name == "ai_user_interact"){
		playsingle_controller::handle_generic_event(name);
		turn_data.send_data();
	}
	else if ((name == "ai_gamestate_changed") || (name == "ai_sync_network")){
		turn_data.sync_network();
	}
	else if (name == "host_transfer"){
		is_host_ = true;
		if (linger_){
			gui::button* btn_end = gui_->find_button("button-endturn");
			btn_end->enable(true);
			gui_->invalidate_theme();
		}
	}
	if (end_turn_) {
		throw end_turn_exception();
	}
}
Esempio n. 7
0
void playmp_controller::handle_generic_event(const std::string& name){
	turn_info turn_data(player_number_, replay_sender_, undo_stack_);

	if (name == "ai_user_interact"){
		playsingle_controller::handle_generic_event(name);
		turn_data.send_data();
	}
	else if ((name == "ai_gamestate_changed") || (name == "ai_sync_network")){
		turn_data.sync_network();
	}
	else if (name == "host_transfer"){
		is_host_ = true;
		if (linger_){
			gui_->set_theme_object_active("endturn", true);
		}
	}
/*
	// why remark, reference http://www.freeors.com/bbs/forum.php?mod=viewthread&tid=21952&page=1&extra=#pid30175
	if (end_turn_) {
		throw end_turn_exception();
	}
*/
}
void playsingle_controller::play_ai_turn()
{
	uint32_t start = SDL_GetTicks();
	total_draw = 0;
	total_draws = 0;
	total_analyzing = 0;
	total_recruit = 0;
	total_combat = 0;
	total_build = 0;
	total_move = 0;
	total_diplomatism = 0;

	team& current_team = teams_[player_number_ - 1];

	{
		const events::command_disabler disable_commands;

		if (card_mode_) {
			execute_card_uh(turn(), player_number_);
		}
		execute_guard_attack(player_number_);
	}

	gui_->enable_menu("play_card", false);
	gui_->enable_menu("undo", false);
	gui_->enable_menu("endturn", false);
	browse_ = true;
	gui_->recalculate_minimap();

	const cursor::setter cursor_setter(cursor::WAIT);

	turn_info turn_data(player_number_, replay_sender_, undo_stack_);

	uint32_t before = SDL_GetTicks();
	try {
		ai::manager::play_turn(player_number_);
	} catch (end_turn_exception&) {
	}
	uint32_t after = SDL_GetTicks();

	if (!teams_[player_number_ - 1].is_human()) {
		recorder.end_turn();
	}
	turn_data.sync_network();

	if (!current_team.is_human()) {
		gui_->recalculate_minimap();
		::clear_shroud(player_number_);
		gui_->invalidate_unit();
		gui_->invalidate_game_status();
		gui_->invalidate_all();
		
		gui_->draw();

		if (card_mode_) {
			execute_card_bh(turn(), player_number_);
		}
	}

	uint32_t stop = SDL_GetTicks();
	posix_print("#%i, play_ai_turn %u ms, (draw: %u(%i), analyzing: %u), [%u](%u+[%u]+%u)(recruit: %u, combat: %u, build: %u, move: %u, diplomatism: %u)\n", 
		player_number_, stop - start, total_draw, total_draws, total_analyzing, 
		before - start + total_recruit + total_combat + total_build + total_move + total_diplomatism + stop - after,
		before - start, total_recruit + total_combat + total_build + total_move + total_diplomatism, stop - after,
		total_recruit, total_combat, total_build, total_move, total_diplomatism);
}
void playsingle_controller::play_turn(bool save)
{
	resources::whiteboard->on_gamestate_change();
	gui_->new_turn();
	gui_->invalidate_game_status();
	events::raise_draw_event();

	LOG_NG << "turn: " << turn() << "\n";

	if(non_interactive())
		std::cout << "Turn " << turn() << ":" << std::endl;


	for (player_number_ = first_player_; player_number_ <= int(teams_.size()); ++player_number_)
	{
		// If a side is empty skip over it.
		if (current_team().is_empty()) continue;
		try {
			save_blocker blocker;
			init_side(player_number_ - 1);
		} catch (end_turn_exception) {
			if (current_team().is_network() == false) {
				turn_info turn_data(player_number_, replay_sender_);
				recorder.end_turn();
				turn_data.sync_network();
			}
			continue;
		}

		if (replaying_) {
			LOG_NG << "doing replay " << player_number_ << "\n";
			replaying_ = ::do_replay(player_number_);
			LOG_NG << "result of replay: " << (replaying_?"true":"false") << "\n";
		} else {
			// If a side is dead end the turn, but play at least side=1's
			// turn in case all sides are dead
			if (current_team().is_human() && side_units(player_number_) == 0
				&& (resources::units->size() != 0 || player_number_ != 1))
			{
				turn_info turn_data(player_number_, replay_sender_);
				recorder.end_turn();
				turn_data.sync_network();
				continue;
			}
			ai_testing::log_turn_start(player_number_);
			play_side(player_number_, save);
		}

		finish_side_turn();

		if(non_interactive()) {
			std::cout << " Player " << player_number_ << ": " <<
				current_team().villages().size() << " Villages" <<
				std::endl;
			ai_testing::log_turn_end(player_number_);
		}

		check_victory();

		//if loading a savegame, network turns might not have reset this yet
		loading_game_ = false;
	}

	finish_turn();

	// Time has run out
	check_time_over();
}