스타크래프트2 봇

스타크래프트 2 봇 개발 일지-3(Ocraft)

오잎 클로버 2022. 1. 3. 12:30
728x90

지난 번에 건설 작업 후 놀고 있는 일꾼에게 다시 명령을 내리는 것을 했었습니다.

 

이번에는 배럭 건설 및 마린 생산, 그리고 최종적으로 공격이다.

 

필수 메소드

private int countUnitType(Units unitType) {
    return observation().getUnits(Alliance.SELF, UnitInPool.isUnit(unitType)).size();
}

위 메소드는 해당 유닛 종류가 몇 기나 있는 지 반환해주는 메소드이다.

 

병영 건설

private void tryBuildBarracks() {
    if (countUnitType(Units.TERRAN_SUPPLY_DEPOT) < 1) {
        return;
    }

    if (countUnitType(Units.TERRAN_BARRACKS) > 0) {
        return;
    }

    tryBuildStructure(Abilities.BUILD_BARRACKS);
}

그리고 난 후 tryBuildBarracks를 onStep 메소드에 호출한다.

@Override
public void onStep() {
    tryBuildSupplyDepot();
    tryBuildBarracks();
}

 

해병 생산

onUnitIdle 메소드에 TERRAN_BARRACKS 를 추가하여

처리할 수 있다.

case TERRAN_BARRACKS: {
    actions().unitCommand(unit, Abilities.TRAIN_MARINE, false);
    break;
}

 

공격

공격하기 위해서는 적군 위치, 공격할 유닛. 이 두가지가 요구된다.

먼저

에 있는 것처럼 하기 위해서 병영 건설 파트를

if (countUnitType(Units.TERRAN_BARRACKS) > 2) {
    return;
}

로 교체합니다.

private Optional<Point2d> findEnemyPosition() {
    ResponseGameInfo gameInfo = observation().getGameInfo();

    Optional<StartRaw> startRaw = gameInfo.getStartRaw();
    if (startRaw.isPresent()) {
        Set<Point2d> startLocations = new HashSet<>(startRaw.get().getStartLocations());
        startLocations.remove(observation().getStartLocation().toPoint2d());
        if (startLocations.isEmpty()) {
            return Optional.empty();
        }
        return Optional.of(new ArrayList<>(startLocations)
                .get(ThreadLocalRandom.current().nextInt(startLocations.size())));
    }
    else {
        return Optional.empty();
    }
}

게임 정보들을 가져와서 위치들로 하여금 상대 위치를 가늠합니다.

case TERRAN_MARINE: {
    if (countUnitType(Units.TERRAN_MARINE) < 10) {
        break;
    }
    findEnemyPosition().ifPresent(point2d -> actions().unitCommand(unit, Abilities.ATTACK_ATTACK, point2d, false));
}

놀고 있는 마린들이 10기 초과가 되면 공격 명령을 합니다.

스타크래프트 II 2022-01-03 03-19-30.mp4
5.83MB

이상입니다.

(그리고 아직까지도 가스 건설을 처리하지 못하였습니다. 하지만 빠른 시일 내에 해결해서 글 올리도록 하겠습니다.)