1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! Contains public API for creating built-in SC2 AI opponents.

use futures::prelude::*;
use futures::unsync::mpsc;
use tokio_core::reactor;

use constants::sc2_bug_tag;
use data::{Difficulty, PlayerSetup, Race};
use services::computer_service::ComputerService;
use services::melee_service::{MeleeCompetitor, MeleeRequest};

use Result;

/// Build a built-in AI opponent.
pub struct OpponentBuilder {
    race: Race,
    difficulty: Difficulty,
}

impl OpponentBuilder {
    /// Create the builder.
    pub fn new() -> Self {
        Self {
            race: Race::Random,
            difficulty: Difficulty::Medium,
        }
    }

    /// Set the race of the AI (default is Random).
    pub fn race(self, race: Race) -> Self {
        Self {
            race: race,
            ..self
        }
    }

    /// Set the difficulty of the AI (default is Medium).
    pub fn difficulty(self, difficulty: Difficulty) -> Self {
        Self {
            difficulty: difficulty,
            ..self
        }
    }
}

impl MeleeCompetitor for OpponentBuilder {
    fn spawn(
        &mut self,
        handle: &reactor::Handle,
        control_rx: mpsc::Receiver<MeleeRequest>,
    ) -> Result<()> {
        handle.spawn(
            ComputerService::new(PlayerSetup::Computer(
                self.race,
                self.difficulty,
            )).run(control_rx)
                .map_err(|e| {
                    panic!(
                        "{}: OpponentService ended unexpectedly - {:#?}",
                        sc2_bug_tag(),
                        e
                    )
                }),
        );

        Ok(())
    }
}