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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
use std::collections::{BinaryHeap, HashMap, VecDeque};
use std::ops::Range;
use std::sync::RwLock;
use crate::fleet::{Fleet, FleetId, FleetsManager};
use crate::timeman::Second;
use crate::solar_system::{Kilometers, SolarSystem};
pub trait ScheduleCommand
{
//Remaining time interval for this command.
fn time_range(&self, time: &Range<Second>) -> Range<Second>;
fn tick(
&self,
star_systems: &[SolarSystem],
fleet: &mut Fleet,
time: &Range<Second>)
-> Result<(), Second>;
}
pub struct Schedule
{
commands: Vec<Box<dyn ScheduleCommand>>,
groups: Vec<Range<usize>>,
start_time: Second
}
pub struct ScheduleManager
{
schedules: HashMap<FleetId, Schedule>
}
impl Schedule
{
pub fn new()
-> Self {
Self {
commands: vec![],
groups: vec![],
start_time: 0
}
}
pub fn commands(&self) -> &[Box<dyn ScheduleCommand>] { &self.commands }
pub fn top(&self) -> Option<&Box<dyn ScheduleCommand>> { self.commands.first() }
pub fn top_mut(&mut self) -> Option<&mut Box<dyn ScheduleCommand>> { self.commands.first_mut() }
pub fn pop(&mut self) -> Option<Box<dyn ScheduleCommand>> { self.commands.pop() }
pub fn add_command<T: ScheduleCommand + 'static>(
&mut self,
command: T)
{
self.commands.push(Box::new(command));
}
pub(crate) fn tick(
&mut self,
star_systems: &[SolarSystem],
fleet: &mut Fleet,
time: &Range<Second>)
-> Result<(), Second>
{
while let Some(command) = self.top() {
if command.time_range(time).end > time.start {
self.pop();
continue;
}
return command.tick(star_systems, fleet, time);
};
Ok(())
}
} // impl Schedule
impl ScheduleManager
{
pub fn new() -> Self
{
Self {
schedules: HashMap::new()
}
}
pub fn schedules_mut(
&mut self)
-> std::collections::hash_map::IterMut<'_, FleetId, Schedule>
{
self.schedules.iter_mut()
}
pub fn schedules(
&self)
-> std::collections::hash_map::Iter<'_, FleetId, Schedule>
{
self.schedules.iter()
}
pub fn add_command_to_schedule
<T: ScheduleCommand + 'static>(
&mut self,
id: FleetId,
command: T)
{
if let Some(schedule) = self.schedules.get_mut(&id) {
schedule.add_command(command);
}
}
pub fn schedule_mut(
&mut self,
id: FleetId)
-> Option<&mut Schedule>
{
self.schedules.get_mut(&id)
}
pub fn schedule(
&self,
id: FleetId)
-> Option<&Schedule>
{
self.schedules.get(&id)
}
pub fn remove_schedule(
&mut self,
id: FleetId)
{
self.schedules.remove_entry(&id);
}
fn next_schedule_tick(
&self,
time: &Range<Second>)
-> Option<(FleetId, Second)> {
let mut least_cmd: Option<(FleetId, Second)> = None;
for (id, schedule) in self.schedules.iter() {
let top_cmd = match schedule.top() {
Some(cmd) => cmd.time_range(time).end,
None => { continue; }
};
if let Some(least_val) = least_cmd {
if least_val.1 > top_cmd {
least_cmd = Some((*id, top_cmd));
}
}else{
least_cmd = Some((*id, top_cmd));
}
}
least_cmd
}
pub fn subtick(
&mut self,
fleets_man: &mut FleetsManager,
star_systems: &[SolarSystem],
time: &Range<Second>)
-> Result<(), Second>
{
let mut last_time = time.start;
while let Some((fleet_id, fleet_tick)) = self.next_schedule_tick(time) {
if fleet_tick > time.end {
break;
}
let fleet = match fleets_man.fleet_mut(fleet_id) {
Some(v) => v,
None => {
self.remove_schedule(fleet_id);
continue;
}
};
let schedule = self.schedules.get_mut(&fleet_id).unwrap();
let tick_range = last_time..fleet_tick;
let schedule_tick_interrupt = schedule.tick(
star_systems, fleet, &tick_range);
if schedule_tick_interrupt.is_err() {
return schedule_tick_interrupt;
}
last_time = fleet_tick;
};
Ok(())
}
}
|