Module planet

Module planet 

Source
Expand description

§Planet module

This module provides common definitions for planets and their associated types that will be used by a group to construct its own planet. The Planet struct is the main component: an instance of it represents the actual planet and contains all the logic and state (see PlanetState) needed to work as one, in fact this is what the orchestrator will interact with.

You can instantiate a new planet by calling the Planet::new constructor method and passing valid construction parameters to it (look into its documentation to learn more).

One of the construction parameters is a planet is a group-defined struct that implements the PlanetAI trait, which defines several methods for handling messages coming from the orchestrator and the explorers. This is the core of each group’s planet implementation, as it defines the planet behavior, that is how a planet “reacts” to the possible events or requests.

§Examples

Intended usage (for planet definition, by groups):

use crossbeam_channel::{Sender, Receiver};
use common_game::components::planet::{Planet, PlanetAI, PlanetState, PlanetType, DummyPlanetState};
use common_game::components::resource::{Combinator, Generator};
use common_game::components::rocket::Rocket;
use common_game::components::sunray::Sunray;
use common_game::protocols::planet_explorer;
use common_game::protocols::orchestrator_planet;
use common_game::protocols::planet_explorer::ExplorerToPlanet;
// Group-defined AI struct
struct AI { /* your AI state here */ };

impl PlanetAI for AI {
    fn handle_sunray(
        &mut self,
        state: &mut PlanetState,
        generator: &Generator,
        combinator: &Combinator,
        sunray: Sunray
    ) {
        // your handler code here...
    }

    fn handle_internal_state_req(
        &mut self,
        state: &mut PlanetState,
        generator: &Generator,
        combinator: &Combinator
    ) -> DummyPlanetState {
        // your handler code here...
        state.to_dummy()
    }

    fn handle_explorer_msg(
        &mut self,
        state: &mut PlanetState,
        generator: &Generator,
        combinator: &Combinator,
        msg: ExplorerToPlanet
    ) -> Option<planet_explorer::PlanetToExplorer> {
        // your handler code here...
        None
    }

    fn handle_asteroid(
        &mut self,
        state: &mut PlanetState,
        generator: &Generator,
        combinator: &Combinator,
    ) -> Option<Rocket> {
        // your handler code here...
        None
    }
}

// This is the group's "export" function. It will be called by
// the orchestrator to spawn your planet.
pub fn create_planet(
    id: u32,
    rx_orchestrator: Receiver<orchestrator_planet::OrchestratorToPlanet>,
    tx_orchestrator: Sender<orchestrator_planet::PlanetToOrchestrator>,
    rx_explorer: Receiver<planet_explorer::ExplorerToPlanet>,
) -> Planet {
    let ai = AI {};
    let gen_rules = vec![/* your recipes */];
    let comb_rules = vec![/* your recipes */];

    // Construct the planet and return it
    Planet::new(
        id,
        PlanetType::A,
        Box::new(ai),
        gen_rules,
        comb_rules,
        (rx_orchestrator, tx_orchestrator),
        rx_explorer,
    ).unwrap() // Don't call .unwrap()! You should do error checking instead.
}

Structs§

DummyPlanetState
This is a dummy struct containing an overview of the internal state of a planet. Use PlanetState::to_dummy to construct one.
Planet
Main, top-level planet definition. This type is built on top of PlanetState, PlanetType and PlanetAI, through composition.
PlanetConstraints
Contains planet rules constraints (see PlanetType).
PlanetState
This struct is a representation of the internal state of the planet. Through its public methods, it gives access to the energy cells and rocket construction of the planet.

Enums§

PlanetType
Planet types definitions, intended to be passed to the planet constructor. Identifies the planet rules constraints, with each type having its own rules.

Traits§

PlanetAI
The trait that defines the behavior of a planet, meaning how it reacts to messages coming from the orchestrator and explorers. This is done through trait methods acting as handlers for the messages.