Example #1
0
void recall_result::do_check_before()
{
	LOG_AI_ACTIONS << " check_before " << *this << std::endl;
	const game_info& s_info = get_subjective_info();
	const game_info& info = get_info();

	const unit_map& s_units = s_info.units;
	const unit_map& units = info.units;

	const team& s_my_team = get_my_team(s_info);
	const team& my_team = get_my_team(info);

	//Unit available for recalling?
	if ( !test_available_for_recalling(s_my_team) ||
	    (is_execution() && using_subjective_info() &&
	     !test_available_for_recalling(my_team, true)))
	{
		return;
	}

	//Enough gold?
	if (!test_enough_gold(s_my_team) ||
	    (is_execution() && using_subjective_info() &&
	     !test_enough_gold(my_team, true)))
	{
		return;
	}

	//Leader present?
	const unit *s_my_leader = get_leader(s_units);

	if (!s_my_leader ||
	    (is_execution() && using_subjective_info() &&
	     !get_leader(units, true)))
	{
		return;
	}

	//Leader on keep?
	const gamemap& s_map = s_info.map;
	const gamemap& map = info.map;

	if (!test_leader_on_keep(s_map, *s_my_leader) ||
	    (is_execution() && using_subjective_info() &&
	     !test_leader_on_keep(map, *s_my_leader, true)))
	{
		return;
	}

	//Try to get suitable recall location. Is suitable location available ?
	if (!test_suitable_recall_location(s_map, s_units, *s_my_leader) ||
	    (is_execution() && using_subjective_info() &&
	     !test_suitable_recall_location(map, units, *s_my_leader, true)))
	{
		return;
	}

}
Example #2
0
void init_game()
{
  p("Bonjour, monde !\n");
  p("je suis %d.\n", get_my_team());
  p("ma base est en %d:%d, j'ai %d fourmis.\n",
    get_anthill_pos_x(get_my_team()),
    get_anthill_pos_y(get_my_team()),
    get_nb_ant(get_my_team()));
  p("le terrain fait %d:%d\n", get_landscape_width(), get_landscape_height());
  p("on est parti !.\n");
}
Example #3
0
void play_turn()
{
  int i;

  for (i = 0; i < 4; i++)
    {
      int x = get_ant_pos_x(i);
      int y = get_ant_pos_y(i);

      // nourriture !
      if (get_land_type(x, y) == FOOD)
	{
	  carry_food(i);
	  continue;
	}

      // on est arrive a destination ?
      if (get_anthill_pos_x(get_my_team()) == x
	  && get_anthill_pos_y(get_my_team()) == y)
	{
	  if (get_ant_luggage(i) > 0)
	    {
	      fprintf(stderr, "Weeeeee, manger !!! (%d)\n", i);
	      let_food(i);
	      continue;
	    }
	  // demi tour !
	  direction[i].x *= -1;
	  direction[i].y *= -1;
	}

      x += direction[i].x;
      y += direction[i].y;
      
      // obstacle, demi tour (et on y perd un tour) :/
      if (get_land_type(x, y) == OBSTACLE
	  || get_land_type(x, y) == BAD_ARGUMENT
	  || (get_land_type(x, y) == ANTHILL
	      && !(get_anthill_pos_x(get_my_team()) == x
	      && get_anthill_pos_y(get_my_team()) == y)))
	{
	  direction[i].x *= -1;
	  direction[i].y *= -1;
	}

      move_ant(i, x, y);
    }
  print_map();
}
Example #4
0
void move_result::do_check_before()
{
	LOG_AI_ACTIONS << " check_before " << *this << std::endl;
	const game_info& s_info = get_subjective_info();
	const game_info& info = get_info();

	const unit_map& s_units = s_info.units;
	const unit_map& units = info.units;

	const gamemap& s_map = s_info.map;

	const team& s_my_team = get_my_team(s_info);

	const std::vector<team> &s_teams = s_info.teams;
	const std::vector<team> &teams = info.teams;

	const unit *s_unit = get_unit(s_units, s_teams);
	if (!s_unit || (is_execution() && using_subjective_info() && !get_unit(units, teams, true)))
	{
		return;
	}

	if (!test_route(*s_unit, s_my_team, s_units, s_teams, s_map)) {
		//impossible to test using real info without revealing anything
		//prematurely, since moves are done 'in steps'
		return;
	}
}
Example #5
0
bool move_result::test_route(const unit &un)
{
	if (from_== to_) {
		if (!remove_movement_ || (un.movement_left() == 0) ) {
			set_error(E_EMPTY_MOVE);
			return false;
		}
		return true;
	}

	if (un.movement_left() == 0 ) {
		set_error(E_EMPTY_MOVE);
		return false;
	}

	if (!to_.valid()) {
		set_error(E_NO_ROUTE);
		return false;
	}

	team &my_team = get_my_team();
	const pathfind::shortest_path_calculator calc(un, my_team, resources::gameboard->teams(), resources::gameboard->map());

	//allowed teleports
	pathfind::teleport_map allowed_teleports = pathfind::get_teleport_locations(un, my_team, true);///@todo 1.9: see_all -> false

	//do an A*-search
	route_ = std::shared_ptr<pathfind::plain_route>( new pathfind::plain_route(pathfind::a_star_search(un.get_location(), to_, 10000.0, calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports)));
	if (route_->steps.empty()) {
		set_error(E_NO_ROUTE);
		return false;
	}
	return true;
}
Example #6
0
void recruit_result::do_check_before()
{
	LOG_AI_ACTIONS << " check_before " << *this << std::endl;
	const team& my_team = get_my_team();
	const bool location_specified = recruit_location_.valid();

	//Unit type known ?
	const unit_type *s_type = get_unit_type_known(unit_name_);
	if (!s_type) {
		return;
	}

	//Enough gold?
	if (!test_enough_gold(my_team, *s_type)) {
		return;
	}

	// Leader available for recruiting?
	switch ( ::actions::check_recruit_location(get_side(), recruit_location_,
	                                           recruit_from_, unit_name_) )
	{
	case ::actions::RECRUIT_NO_LEADER:
	case ::actions::RECRUIT_NO_ABLE_LEADER:
		set_error(E_NO_LEADER);
		return;

	case ::actions::RECRUIT_NO_KEEP_LEADER:
		set_error(E_LEADER_NOT_ON_KEEP);
		return;

	case ::actions::RECRUIT_NO_VACANCY:
		set_error(E_BAD_RECRUIT_LOCATION);
		return;

	case ::actions::RECRUIT_ALTERNATE_LOCATION:
		if ( location_specified ) {
			set_error(E_BAD_RECRUIT_LOCATION);
			return;
		}
		// No break. If the location was not specified, this counts as "OK".
	case ::actions::RECRUIT_OK:
		location_checked_ = true;
	}
}
Example #7
0
void recall_result::do_execute()
{
	LOG_AI_ACTIONS << "start of execution of: " << *this << std::endl;
	assert(is_success());

	game_info& info = get_info();
	team& my_team = get_my_team(info);

	const events::command_disabler disable_commands;

	std::vector<unit>::iterator rec = std::find_if(my_team.recall_list().begin(), my_team.recall_list().end(), boost::bind(&unit::matches_id, _1, unit_id_));

	assert(rec != my_team.recall_list().end());

	const std::string &err = find_recruit_location(get_side(), recall_location_);
	if(!err.empty()) {
		set_error(AI_ACTION_FAILURE);
		return;
	} else {

		unit &un = *rec;
		recorder.add_recall(un.id(), recall_location_);
		un.set_game_context(&info.units);
		place_recruit(un, recall_location_, true, true);
		statistics::recall_unit(un);
		my_team.spend_gold(game_config::recall_cost);

		my_team.recall_list().erase(rec);
		if (resources::screen!=NULL) {
			resources::screen->invalidate_game_status();
			resources::screen->invalidate_all();
		}
		recorder.add_checksum_check(recall_location_);
		set_gamestate_changed();
		try {
			manager::raise_gamestate_changed();
		} catch (...) {
			is_ok(); //Silences "unchecked result" warning
			throw;
		}
	}

}
Example #8
0
void recruit_result::do_execute()
{
	LOG_AI_ACTIONS << "start of execution of: " << *this << std::endl;
	assert(is_success());
	game_info& info = get_info();
	// We have to add the recruit command now, because when the unit
	// is created it has to have the recruit command in the recorder
	// to be able to put random numbers into to generate unit traits.
	// However, we're not sure if the transaction will be successful,
	// so use a replay_undo object to cancel it if we don't get
	// a confirmation for the transaction.
	recorder.add_recruit(num_,recruit_location_);
	replay_undo replay_guard(recorder);
	const unit_type *u = unit_types.find(unit_name_);
	const events::command_disabler disable_commands;
	const std::string recruit_err = find_recruit_location(get_side(), recruit_location_);
	if(recruit_err.empty()) {
		const unit new_unit(&info.units, u, get_side(), true);
		place_recruit(new_unit, recruit_location_, false, preferences::show_ai_moves());
		statistics::recruit_unit(new_unit);
		get_my_team(info).spend_gold(u->cost());
		// Confirm the transaction - i.e. don't undo recruitment
		replay_guard.confirm_transaction();
		set_gamestate_changed();
		try {
			manager::raise_gamestate_changed();
		} catch (...) {
			is_ok(); //Silences "unchecked result" warning
			throw;
		}
	} else {
		set_error(AI_ACTION_FAILURE);
	}


}
Example #9
0
void attack_result::do_check_before()
{
	LOG_AI_ACTIONS << " check_before " << *this << std::endl;
	const unit *attacker = get_unit(get_info(),attacker_loc_);
	const unit *defender = get_unit(get_info(),defender_loc_);

	if(attacker==NULL)
	{
		LOG_AI_ACTIONS << "attempt to attack without attacker\n";
		set_error(E_EMPTY_ATTACKER);
		return;
	}

	if (defender==NULL)
	{
		LOG_AI_ACTIONS << "attempt to attack without defender\n";
		set_error(E_EMPTY_DEFENDER);
		return;
	}

	if(attacker->incapacitated()) {
		LOG_AI_ACTIONS << "attempt to attack with unit that is petrified\n";
		set_error(E_INCAPACITATED_ATTACKER);
		return;
	}

	if(defender->incapacitated()) {
		LOG_AI_ACTIONS << "attempt to attack unit that is petrified\n";
		set_error(E_INCAPACITATED_DEFENDER);
		return;
	}

	if(!attacker->attacks_left()) {
		LOG_AI_ACTIONS << "attempt to attack with no attacks left\n";
		set_error(E_NO_ATTACKS_LEFT);
		return;
	}

	if(attacker->side()!=get_side()) {
		LOG_AI_ACTIONS << "attempt to attack with not own unit\n";
		set_error(E_NOT_OWN_ATTACKER);
		return;
	}

	if(!get_my_team(get_info()).is_enemy(defender->side())) {
		LOG_AI_ACTIONS << "attempt to attack unit that is not enemy\n";
		set_error(E_NOT_ENEMY_DEFENDER);
		return;
	}

	if (attacker_weapon_!=-1) {
		if ((attacker_weapon_<0)||(attacker_weapon_ >= static_cast<int>(attacker->attacks().size()))) {
			LOG_AI_ACTIONS << "invalid weapon selection for the attacker\n";
			set_error(E_WRONG_ATTACKER_WEAPON);
			return;
		}
	}

	if (!tiles_adjacent(attacker_loc_,defender_loc_)) {
		LOG_AI_ACTIONS << "attacker and defender not adjacent\n";
		set_error(E_ATTACKER_AND_DEFENDER_NOT_ADJACENT);
		return;
	}
}