rust/hwphysics/src/time.rs
author alfadur
Thu, 25 Jul 2019 23:02:02 +0300
changeset 15296 66c987015f2d
parent 15295 42b710b0f883
child 15303 478d5372eb4a
permissions -rw-r--r--
replace time with milliseconds
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15296
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
     1
use crate::common::{GearDataProcessor, GearId, Millis};
15291
7446258fab98 add time events
alfadur
parents:
diff changeset
     2
use std::{
7446258fab98 add time events
alfadur
parents:
diff changeset
     3
    cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
7446258fab98 add time events
alfadur
parents:
diff changeset
     4
    collections::BinaryHeap,
7446258fab98 add time events
alfadur
parents:
diff changeset
     5
};
7446258fab98 add time events
alfadur
parents:
diff changeset
     6
7446258fab98 add time events
alfadur
parents:
diff changeset
     7
pub type EventId = u16;
7446258fab98 add time events
alfadur
parents:
diff changeset
     8
7446258fab98 add time events
alfadur
parents:
diff changeset
     9
struct TimeEvent {
15296
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    10
    time: Millis,
15291
7446258fab98 add time events
alfadur
parents:
diff changeset
    11
    gear_id: GearId,
7446258fab98 add time events
alfadur
parents:
diff changeset
    12
    event_id: EventId,
7446258fab98 add time events
alfadur
parents:
diff changeset
    13
}
7446258fab98 add time events
alfadur
parents:
diff changeset
    14
7446258fab98 add time events
alfadur
parents:
diff changeset
    15
impl PartialOrd for TimeEvent {
7446258fab98 add time events
alfadur
parents:
diff changeset
    16
    #[inline]
7446258fab98 add time events
alfadur
parents:
diff changeset
    17
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
7446258fab98 add time events
alfadur
parents:
diff changeset
    18
        self.time.partial_cmp(&other.time)
7446258fab98 add time events
alfadur
parents:
diff changeset
    19
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    20
}
7446258fab98 add time events
alfadur
parents:
diff changeset
    21
7446258fab98 add time events
alfadur
parents:
diff changeset
    22
impl PartialEq for TimeEvent {
7446258fab98 add time events
alfadur
parents:
diff changeset
    23
    #[inline]
7446258fab98 add time events
alfadur
parents:
diff changeset
    24
    fn eq(&self, other: &Self) -> bool {
7446258fab98 add time events
alfadur
parents:
diff changeset
    25
        self.time.eq(&other.time)
7446258fab98 add time events
alfadur
parents:
diff changeset
    26
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    27
}
7446258fab98 add time events
alfadur
parents:
diff changeset
    28
7446258fab98 add time events
alfadur
parents:
diff changeset
    29
impl Ord for TimeEvent {
7446258fab98 add time events
alfadur
parents:
diff changeset
    30
    #[inline]
7446258fab98 add time events
alfadur
parents:
diff changeset
    31
    fn cmp(&self, other: &Self) -> Ordering {
7446258fab98 add time events
alfadur
parents:
diff changeset
    32
        self.time.cmp(&other.time)
7446258fab98 add time events
alfadur
parents:
diff changeset
    33
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    34
}
7446258fab98 add time events
alfadur
parents:
diff changeset
    35
7446258fab98 add time events
alfadur
parents:
diff changeset
    36
impl Eq for TimeEvent {}
7446258fab98 add time events
alfadur
parents:
diff changeset
    37
7446258fab98 add time events
alfadur
parents:
diff changeset
    38
pub struct OccurredEvents {
7446258fab98 add time events
alfadur
parents:
diff changeset
    39
    events: Vec<(GearId, EventId)>,
7446258fab98 add time events
alfadur
parents:
diff changeset
    40
}
7446258fab98 add time events
alfadur
parents:
diff changeset
    41
7446258fab98 add time events
alfadur
parents:
diff changeset
    42
impl OccurredEvents {
7446258fab98 add time events
alfadur
parents:
diff changeset
    43
    fn new() -> Self {
7446258fab98 add time events
alfadur
parents:
diff changeset
    44
        Self { events: vec![] }
7446258fab98 add time events
alfadur
parents:
diff changeset
    45
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    46
7446258fab98 add time events
alfadur
parents:
diff changeset
    47
    fn clear(&mut self) {
7446258fab98 add time events
alfadur
parents:
diff changeset
    48
        self.events.clear()
7446258fab98 add time events
alfadur
parents:
diff changeset
    49
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    50
}
7446258fab98 add time events
alfadur
parents:
diff changeset
    51
7446258fab98 add time events
alfadur
parents:
diff changeset
    52
pub struct TimeProcessor {
7446258fab98 add time events
alfadur
parents:
diff changeset
    53
    current_event_id: EventId,
15296
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    54
    current_time: Millis,
15291
7446258fab98 add time events
alfadur
parents:
diff changeset
    55
    events: BinaryHeap<TimeEvent>,
7446258fab98 add time events
alfadur
parents:
diff changeset
    56
    timeouts: OccurredEvents,
7446258fab98 add time events
alfadur
parents:
diff changeset
    57
}
7446258fab98 add time events
alfadur
parents:
diff changeset
    58
7446258fab98 add time events
alfadur
parents:
diff changeset
    59
impl TimeProcessor {
7446258fab98 add time events
alfadur
parents:
diff changeset
    60
    pub fn new() -> Self {
7446258fab98 add time events
alfadur
parents:
diff changeset
    61
        Self {
7446258fab98 add time events
alfadur
parents:
diff changeset
    62
            current_event_id: 0,
15296
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    63
            current_time: Millis::new(0),
15291
7446258fab98 add time events
alfadur
parents:
diff changeset
    64
            events: BinaryHeap::with_capacity(1024),
7446258fab98 add time events
alfadur
parents:
diff changeset
    65
            timeouts: OccurredEvents::new(),
7446258fab98 add time events
alfadur
parents:
diff changeset
    66
        }
7446258fab98 add time events
alfadur
parents:
diff changeset
    67
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    68
15296
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    69
    pub fn register(&mut self, gear_id: GearId, timeout: Millis) -> EventId {
15291
7446258fab98 add time events
alfadur
parents:
diff changeset
    70
        let event_id = self.current_event_id;
7446258fab98 add time events
alfadur
parents:
diff changeset
    71
        self.current_event_id.wrapping_add(1);
7446258fab98 add time events
alfadur
parents:
diff changeset
    72
        let event = TimeEvent {
7446258fab98 add time events
alfadur
parents:
diff changeset
    73
            time: self.current_time + timeout,
7446258fab98 add time events
alfadur
parents:
diff changeset
    74
            gear_id,
7446258fab98 add time events
alfadur
parents:
diff changeset
    75
            event_id,
7446258fab98 add time events
alfadur
parents:
diff changeset
    76
        };
7446258fab98 add time events
alfadur
parents:
diff changeset
    77
        self.events.push(event);
7446258fab98 add time events
alfadur
parents:
diff changeset
    78
        event_id
7446258fab98 add time events
alfadur
parents:
diff changeset
    79
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    80
15295
42b710b0f883 add gear allocator
alfadur
parents: 15291
diff changeset
    81
    pub fn cancel(&mut self, gear_id: GearId) {}
42b710b0f883 add gear allocator
alfadur
parents: 15291
diff changeset
    82
15296
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    83
    pub fn process(&mut self, time_step: Millis) -> &OccurredEvents {
15291
7446258fab98 add time events
alfadur
parents:
diff changeset
    84
        self.timeouts.clear();
15296
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    85
        self.current_time = self.current_time + time_step;
15291
7446258fab98 add time events
alfadur
parents:
diff changeset
    86
        while self
7446258fab98 add time events
alfadur
parents:
diff changeset
    87
            .events
7446258fab98 add time events
alfadur
parents:
diff changeset
    88
            .peek()
7446258fab98 add time events
alfadur
parents:
diff changeset
    89
            .filter(|e| e.time <= self.current_time)
7446258fab98 add time events
alfadur
parents:
diff changeset
    90
            .is_some()
7446258fab98 add time events
alfadur
parents:
diff changeset
    91
        {
7446258fab98 add time events
alfadur
parents:
diff changeset
    92
            let event = self.events.pop().unwrap();
7446258fab98 add time events
alfadur
parents:
diff changeset
    93
            self.timeouts.events.push((event.gear_id, event.event_id))
7446258fab98 add time events
alfadur
parents:
diff changeset
    94
        }
7446258fab98 add time events
alfadur
parents:
diff changeset
    95
        &self.timeouts
7446258fab98 add time events
alfadur
parents:
diff changeset
    96
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    97
}