Beispiel #1
0
const turn_state strategy_formulation_with_rca::simulate_state(int decision_no_, const turn_state &state)
{
	DBG_AI_TESTING_SF_WITH_RCA << "------simulate_state() begin------" << std::endl;
	// Simulate CA based on state and get resulted turn_state.

	const unit_map &units_ = state.get_units();
	const std::vector<team> &teams_ = state.get_teams();

	// Simulate.
	// First set current data structure according to previous simulation.
	*resources::units = units_;
	*resources::teams = teams_;
	init_side();

	// Second run RCA based on current decision on current data structures.
	DBG_AI_TESTING_SF_WITH_RCA << "Set strategy flag: " << decision_no_ << std::endl;
	decision_no_ == 0 ? set_offense() : set_defense();
	rca_->do_play_stage();

	// Third return the data structures after simulation.
	boost::shared_ptr<turn_state> state_next(new turn_state(this->get_side(), state.get_turn_no()+1, *resources::units, *resources::teams));
	const decision &decision_ = state.get_decision();
	if(decision_.is_valid()){
		state_next->set_decision(decision_.get_decision_no());
	} else {
		state_next->set_decision(decision_no_);
	}

	DBG_AI_TESTING_SF_WITH_RCA << "Clear strategy flag" << std::endl;
	clear_strategy();

	DBG_AI_TESTING_SF_WITH_RCA << "------simulate_state() end------" << std::endl;
	return *state_next;
}
// @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();
}
possible_end_play_signal playsingle_controller::play_turn()
{
    whiteboard_manager_->on_gamestate_change();
    gui_->new_turn();
    gui_->invalidate_game_status();
    events::raise_draw_event();

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

    if(non_interactive()) {
        LOG_AIT << "Turn " << turn() << ":" << std::endl;
    }

    for (player_number_ = first_player_; player_number_ <= int(gamestate_.board_.teams().size()); ++player_number_)
    {
        // If a side is empty skip over it.
        if (current_team().is_empty()) continue;

        possible_end_play_signal signal;
        {
            save_blocker blocker;
            signal = init_side();
        }

        if (signal) {
            switch (boost::apply_visitor(get_signal_type(), *signal)) {
            case END_TURN:
                if (current_team().is_network() == false) {
                    turn_data_.send_data();
                    recorder.end_turn();
                    turn_data_.sync_network();
                }
                continue;
            case END_LEVEL:
                return signal;
            }
        }

        if (replaying_) {
            LOG_NG << "doing replay " << player_number_ << "\n";
            HANDLE_END_PLAY_SIGNAL ( replaying_ = ::do_replay() == REPLAY_FOUND_END_TURN );
            LOG_NG << "result of replay: " << (replaying_?"true":"false") << "\n";
        } else {
            ai_testing::log_turn_start(player_number_);
            PROPOGATE_END_PLAY_SIGNAL ( play_side() );
        }

        finish_side_turn();

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

        HANDLE_END_PLAY_SIGNAL ( check_victory() );

        //if loading a savegame, network turns might not have reset this yet
        loading_game_ = false;
    }
    //If the loop exits due to the last team having been processed,
    //player_number_ will be 1 too high
    if(player_number_ > static_cast<int>(gamestate_.board_.teams().size()))
        player_number_ = gamestate_.board_.teams().size();

    finish_turn();

    // Time has run out
    PROPOGATE_END_PLAY_SIGNAL ( check_time_over() );
    return boost::none;
}
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();
}
Beispiel #5
0
bool G_Map::init() {
    auto tab_info = the_app->script()->read_table("the_map_info");
    if (tab_info->is_nil()) {
        return false;
    }
    for (unsigned i = 1; ; ++i) {
        auto tab_item = tab_info->read_table(i);
        if (tab_item->is_nil()) {
            break;
        }

        int id = tab_item->read_integer("id", -1);
        if (id < 0) {
            continue;
        }

        G_MapCity *city = probe_city(id);

        city->_coin = tab_item->read_integer("tongqian");
        unsigned side_id = tab_item->read_integer("mbelong");
        side_id--;
        if (side_id > G_SIDE_UNKNOWN) {
            log_error("unknown side id '%d'.", side_id + 1);
            return false;
        }

        city->_origin = city->_side = get_side(side_id);
        city->_side->_coin += city->_coin;

        auto joins = tab_item->read_table("xiangling");
        if (joins->is_nil()) {
            log_error("city '%d' is solo.", id);
            return false;
        }

        for (unsigned j = 1; ; ++j) {
            int join_item = joins->read_integer(j, -1);
            if (join_item < 0) {
                break;
            }
            city->_joins[join_item] = probe_city(join_item);
        }
    }
    if (!init_side()) {
        return false;
    }
    for (G_MapCity *city : _city_list) {
        if (!city->_origin) {
            log_error("city %d bad.", city->_id);
            continue;
        }
        city->init();
    }

    object<G_GraphicMatrix> path(_city_list.size());
    G_GraphicMatrix mat(_city_list.size());

    for (unsigned i = 0; i < _city_list.size(); ++i) {
        for (unsigned j = 0; j < _city_list.size(); ++j) {
            if (i == j) {
                mat.elem(i, j) = 0;
            }
            else {
                G_MapCity *city = _city_list[i];
                G_MapCity *city2 = _city_list[j];
                if (city->_joins.find(city2->_id) == city->_joins.end()) {
                    mat.elem(i, j) = UINT32_MAX;
                }
                else {
                    mat.elem(i, j) = 1;
                }
            }
        }
    }
    for (unsigned i = 0; i < _city_list.size(); ++i) {
        for (unsigned j = 0; j < _city_list.size(); ++j) {
            path->elem(i, j) = UINT32_MAX;
        }
    }
    for(unsigned k = 0; k < _city_list.size(); k++) {
        for(unsigned i = 0; i < _city_list.size(); i++) {
            for(unsigned j = 0; j < _city_list.size(); j++) {
                if((mat.elem(i, k) && mat.elem(k, j) && mat.elem(i, k) < UINT32_MAX && mat.elem(k, j) < UINT32_MAX) && 
                   (mat.elem(i, k) + mat.elem(k, j) < mat.elem(i, j))) {
                    mat.elem(i, j) = mat.elem(i, k) + mat.elem(k, j);
                    path->elem(i, j) = path->elem(k, j);
                }  
            }  
        }
    }
    _path = path;
    return true;
}