rust/lib-hedgewars-engine/src/world.rs
author alfadur
Mon, 26 Nov 2018 02:50:54 +0300
changeset 14295 fd8e0e52d5bd
parent 14272 3152d9fdb499
child 14373 4409344db447
permissions -rw-r--r--
add hwphysics to engine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
     1
use fpnum::{FPNum, fp};
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
     2
use integral_geometry::{Point, Rect, Size};
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
     3
use land2d::Land2D;
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
     4
use landgen::{
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
     5
    outline_template::OutlineTemplate,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
     6
    template_based::TemplatedLandGenerator,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
     7
    LandGenerationParameters,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
     8
    LandGenerator,
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
     9
};
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    10
use lfprng::LaggedFibonacciPRNG;
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    11
use hwphysics as hwp;
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    12
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    13
struct GameState {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    14
    land: Land2D<u32>,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    15
    physics: hwp::World,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    16
}
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    17
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    18
impl GameState {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    19
    fn new(land: Land2D<u32>, physics: hwp::World) -> Self {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    20
        Self {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    21
            land,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    22
            physics,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    23
        }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    24
    }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    25
}
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    26
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    27
pub struct World {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    28
    random_numbers_gen: LaggedFibonacciPRNG,
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    29
    preview: Land2D<u8>,
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    30
    game_state: Option<GameState>,
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    31
}
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    32
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    33
impl World {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    34
    pub fn new() -> Self {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    35
        Self {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    36
            random_numbers_gen: LaggedFibonacciPRNG::new(&[]),
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    37
            preview: Land2D::new(Size::new(0, 0), 0),
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    38
            game_state: None,
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    39
        }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    40
    }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    41
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    42
    pub fn set_seed(&mut self, seed: &[u8]) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    43
        self.random_numbers_gen = LaggedFibonacciPRNG::new(seed);
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    44
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    45
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    46
    pub fn preview(&self) -> &Land2D<u8> {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    47
        &self.preview
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    48
    }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    49
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    50
    pub fn generate_preview(&mut self) {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    51
        fn template() -> OutlineTemplate {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    52
            let mut template = OutlineTemplate::new(Size::new(4096, 2048));
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    53
            template.islands = vec![vec![
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    54
                Rect::from_size_coords(100, 2050, 1, 1),
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    55
                Rect::from_size_coords(100, 500, 400, 1200),
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    56
                Rect::from_size_coords(3600, 500, 400, 1200),
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    57
                Rect::from_size_coords(3900, 2050, 1, 1),
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    58
            ]];
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    59
            template.fill_points = vec![Point::new(1, 0)];
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    60
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    61
            template
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    62
        }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    63
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    64
        let params = LandGenerationParameters::new(0u8, u8::max_value(), 5, false, false);
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    65
        let landgen = TemplatedLandGenerator::new(template());
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    66
        self.preview = landgen.generate_land(&params, &mut self.random_numbers_gen);
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    67
    }
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    68
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    69
    pub fn init(&mut self, template: OutlineTemplate) {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    70
        let physics = hwp::World::new(template.size);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    71
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    72
        let params = LandGenerationParameters::new(0u32, u32::max_value(), 5, false, false);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    73
        let landgen = TemplatedLandGenerator::new(template);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    74
        let land = landgen.generate_land(&params, &mut self.random_numbers_gen);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    75
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    76
        self.game_state = Some(GameState::new(land, physics));
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    77
    }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    78
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    79
    pub fn step(&mut self) {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    80
        if let Some(ref mut state) = self.game_state {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    81
            state.physics.step(fp!(1), &state.land);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    82
        }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    83
    }
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    84
}