rust/lib-hedgewars-engine/src/time.rs
author unC0Rr
Tue, 10 Sep 2024 13:56:51 +0200
branchtransitional_engine
changeset 16032 31cc1e450273
parent 15828 44b49f255e31
permissions -rw-r--r--
Add maze land generator ported from pascal engine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15828
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15775
diff changeset
     1
use hwphysics::common::{GearId, Millis};
15775
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
     2
use std::{
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
     3
    cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
     4
    collections::BinaryHeap,
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
     5
};
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
     6
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
     7
pub type EventId = u16;
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
     8
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
     9
struct TimeEvent {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    10
    time: Millis,
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    11
    gear_id: GearId,
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    12
    event_id: EventId,
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    13
}
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    14
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    15
impl PartialOrd for TimeEvent {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    16
    #[inline]
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    17
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    18
        self.time.partial_cmp(&other.time)
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    19
    }
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    20
}
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    21
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    22
impl PartialEq for TimeEvent {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    23
    #[inline]
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    24
    fn eq(&self, other: &Self) -> bool {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    25
        self.time.eq(&other.time)
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    26
    }
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    27
}
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    28
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    29
impl Ord for TimeEvent {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    30
    #[inline]
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    31
    fn cmp(&self, other: &Self) -> Ordering {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    32
        self.time.cmp(&other.time)
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    33
    }
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    34
}
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    35
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    36
impl Eq for TimeEvent {}
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    37
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    38
pub struct OccurredEvents {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    39
    events: Vec<(GearId, EventId)>,
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    40
}
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    41
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    42
impl OccurredEvents {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    43
    fn new() -> Self {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    44
        Self { events: vec![] }
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    45
    }
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    46
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    47
    fn clear(&mut self) {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    48
        self.events.clear()
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    49
    }
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    50
}
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    51
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    52
pub struct TimeProcessor {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    53
    current_event_id: EventId,
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    54
    current_time: Millis,
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    55
    events: BinaryHeap<TimeEvent>,
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    56
    timeouts: OccurredEvents,
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    57
}
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    58
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    59
impl TimeProcessor {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    60
    pub fn new() -> Self {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    61
        Self {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    62
            current_event_id: 0,
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    63
            current_time: Millis::new(0),
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    64
            events: BinaryHeap::with_capacity(1024),
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    65
            timeouts: OccurredEvents::new(),
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    66
        }
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    67
    }
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    68
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    69
    pub fn register(&mut self, gear_id: GearId, timeout: Millis) -> EventId {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    70
        let event_id = self.current_event_id;
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    71
        self.current_event_id = self.current_event_id.wrapping_add(1);
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    72
        let event = TimeEvent {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    73
            time: self.current_time + timeout,
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    74
            gear_id,
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    75
            event_id,
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    76
        };
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    77
        self.events.push(event);
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    78
        event_id
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    79
    }
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    80
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    81
    fn retain_events<P>(&mut self, predicate: P)
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    82
    where
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    83
        P: Fn(&TimeEvent) -> bool,
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    84
    {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    85
        let events = self.events.drain().filter(predicate).collect::<Vec<_>>();
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    86
        self.events.extend(events);
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    87
    }
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    88
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    89
    pub fn cancel(&mut self, event_id: EventId) {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    90
        //self.events.retain(|event| event.event_id != event_id)
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    91
        self.retain_events(|event| event.event_id != event_id)
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    92
    }
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    93
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    94
    pub fn cancel_all(&mut self, gear_id: GearId) {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    95
        //self.events.retain(|event| event.gear_id != gear_id)
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    96
        self.retain_events(|event| event.gear_id != gear_id)
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    97
    }
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    98
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
    99
    pub fn process(&mut self, time_step: Millis) -> &OccurredEvents {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
   100
        self.timeouts.clear();
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
   101
        self.current_time = self.current_time + time_step;
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
   102
        while self
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
   103
            .events
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
   104
            .peek()
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
   105
            .filter(|e| e.time <= self.current_time)
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
   106
            .is_some()
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
   107
        {
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
   108
            let event = self.events.pop().unwrap();
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
   109
            self.timeouts.events.push((event.gear_id, event.event_id))
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
   110
        }
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
   111
        &self.timeouts
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
   112
    }
95402fa4e191 move time out of physics
alfadur
parents:
diff changeset
   113
}