server_pb/
main.rs

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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
use crate::high_level::ReinforcementLearningManager;
use crate::logging::RobotLoggers;
use crate::ota::OverTheAirProgramming;
use crate::sockets::Destination::{GuiClients, Simulation};
use crate::sockets::Incoming::FromRobot;
use crate::sockets::Outgoing::{ToGameServer, ToGui, ToSimulation};
use crate::sockets::{Destination, Outgoing, Sockets};
use crate::Destination::Robot;
use crate::Outgoing::ToRobot;
use core_pb::bin_encode;
use core_pb::constants::{GUI_LISTENER_PORT, MAX_ROBOT_PATH_LENGTH};
use core_pb::grid::computed_grid::ComputedGrid;
use core_pb::grid::GRID_SIZE;
use core_pb::messages::server_status::ServerStatus;
use core_pb::messages::settings::{
    ConnectionSettings, CvLocationSource, PacbotSettings, ShouldDoTargetPath, StrategyChoice,
};
use core_pb::messages::{
    GameServerCommand, NetworkStatus, ServerToGuiMessage, ServerToRobotMessage,
    ServerToSimulationMessage,
};
use core_pb::names::{RobotName, NUM_ROBOT_NAMES};
use core_pb::pacbot_rs::game_state::GameState;
use core_pb::pacbot_rs::location::Direction;
use core_pb::threaded_websocket::TextOrT;
use core_pb::util::stopwatch::Stopwatch;
use core_pb::util::utilization::UtilizationMonitor;
use core_pb::util::WebTimeInstant;
use env_logger::Builder;
use log::{info, LevelFilter};
use nalgebra::Point2;
use rand::prelude::IteratorRandom;
use rand::thread_rng;
use std::collections::HashSet;
use std::path::Path;
use std::process::{Child, Command};
use std::time::Duration;
use tokio::select;
use tokio::time::{interval, Instant, Interval};

mod high_level;
mod logging;
pub mod network;
mod ota;
mod sockets;
// todo pub mod strategy;

#[allow(dead_code)]
pub struct App {
    status: ServerStatus,
    settings: PacbotSettings,
    utilization_monitor: UtilizationMonitor<100, WebTimeInstant>,
    inference_timer: Stopwatch<1, 10, WebTimeInstant>,

    client_http_host_process: Option<Child>,
    sim_game_engine_process: Option<Child>,

    sockets: Sockets,
    robot_ping_timers: [Option<Instant>; NUM_ROBOT_NAMES],
    robot_loggers: Option<RobotLoggers>,

    rl_manager: ReinforcementLearningManager,
    over_the_air_programming: OverTheAirProgramming,

    grid: ComputedGrid,
}

impl Default for App {
    fn default() -> Self {
        let sockets = Sockets::spawn();

        App {
            status: Default::default(),
            settings: Default::default(),
            utilization_monitor: UtilizationMonitor::default(),
            inference_timer: Stopwatch::new(
                "Inference",
                Duration::from_secs_f32(0.5),
                Duration::from_secs_f32(1.0),
                100.0,
                100.0,
            ),

            client_http_host_process: None,
            sim_game_engine_process: None,

            rl_manager: ReinforcementLearningManager::default(),
            over_the_air_programming: OverTheAirProgramming::new(sockets.outgoing.clone()),

            sockets,
            robot_ping_timers: [None; NUM_ROBOT_NAMES],
            robot_loggers: RobotLoggers::generate().ok(),

            grid: Default::default(),
        }
    }
}

#[tokio::main]
async fn main() {
    Builder::from_default_env()
        .filter_level(LevelFilter::Info)
        .filter(Some("core_pb::threaded_websocket"), LevelFilter::Off) // silence threaded_websocket
        .init();

    info!("RIT Pacbot server starting up");

    let folder_path = Path::new("server_pb");
    if !folder_path.is_dir() {
        panic!("Please run the server from the repository root.");
    }

    let mut app = App::default();
    info!("Listening on 0.0.0.0:{GUI_LISTENER_PORT}");
    app.utilization_monitor.start();

    // apply default settings
    app.update_settings(&PacbotSettings::default(), PacbotSettings::default())
        .await;

    app.run_forever().await;
}

impl App {
    async fn run_forever(&mut self) {
        let mut periodic_interval = interval(Duration::from_millis(20));
        let mut move_interval = interval(Duration::from_secs_f32(1.0 / self.settings.target_speed));
        let mut previous_settings = self.settings.clone();

        loop {
            select! {
                _ = periodic_interval.tick() => {
                    self.utilization_monitor.start();
                    self.periodic_actions(&mut previous_settings, &mut move_interval).await;
                    self.utilization_monitor.stop();
                }
                _ = move_interval.tick() => {
                    self.utilization_monitor.start();
                    self.move_pacman().await;
                    self.utilization_monitor.stop();
                }
                msg = self.sockets.incoming.recv() => {
                    let msg = msg.unwrap();
                    // we want to measure the amount of time the server spends processing messages,
                    // which shouldn't include the amount of time spent waiting for messages
                    self.utilization_monitor.start();

                    if self.settings.safe_mode {
                        if let FromRobot(msg) = &msg.1 {
                            let encoded = bin_encode(false, TextOrT::T(msg.clone())).unwrap();
                            if encoded.get(9).map(|x| *x > 7).unwrap_or(false) {
                                continue;
                            }
                        }
                    }
                    self.over_the_air_programming
                        .update(&msg, &mut self.status)
                        .await;
                    self.handle_message(msg.0, msg.1).await;

                    self.utilization_monitor.stop();
                    self.status.utilization = self.utilization_monitor.status();
                }
            }
        }
    }

    async fn move_pacman(&mut self) {
        // if the current pacman robot isn't connected, update game state with target path
        if let Some(target) = self.status.target_path.first() {
            if (self.settings.do_target_path == ShouldDoTargetPath::Yes
                || self.settings.do_target_path == ShouldDoTargetPath::DoWhilePlayed
                    && !self.status.game_state.paused)
                && self.status.advanced_game_server
                && self.status.robots[self.settings.pacman as usize].connection
                    == NetworkStatus::NotConnected
            {
                let dir = match (
                    target.x - self.status.game_state.pacman_loc.row,
                    target.y - self.status.game_state.pacman_loc.col,
                ) {
                    (-1, 0) => Direction::Up,
                    (1, 0) => Direction::Down,
                    (0, -1) => Direction::Left,
                    (0, 1) => Direction::Right,
                    _ => Direction::Stay,
                };
                if dir != Direction::Stay {
                    self.send(
                        Destination::GameServer,
                        ToGameServer(GameServerCommand::Direction(dir)),
                    )
                    .await;
                }
            }
        }
    }

    async fn periodic_actions(
        &mut self,
        previous_settings: &mut PacbotSettings,
        move_pacman_interval: &mut Interval,
    ) {
        // trigger pings to any robots who don't have an active ping
        for name in RobotName::get_all() {
            if self.status.robots[name as usize].connection == NetworkStatus::Connected
                && self.robot_ping_timers[name as usize]
                    .map(|x| x.elapsed().as_millis() > 500)
                    .unwrap_or(true)
            {
                self.robot_ping_timers[name as usize] = Some(Instant::now());
                self.send(Robot(name), ToRobot(ServerToRobotMessage::Ping))
                    .await;
            }
        }
        self.over_the_air_programming.tick(&mut self.status).await;
        if self.settings != *previous_settings {
            *previous_settings = self.settings.clone();
            *move_pacman_interval =
                interval(Duration::from_secs_f32(1.0 / self.settings.target_speed));
            self.send(
                GuiClients,
                ToGui(ServerToGuiMessage::Settings(self.settings.clone())),
            )
            .await; // check if new AI calculation is needed
        }
        // send motor commands to robots
        for name in RobotName::get_all() {
            let mut data = self.settings.robots[name as usize].config.clone();
            if name == self.settings.pacman {
                data.grid = self.settings.standard_grid;
                data.cv_location = self.status.cv_location;
                data.target_path = self
                    .status
                    .target_path
                    .clone()
                    .into_iter()
                    .take(MAX_ROBOT_PATH_LENGTH)
                    .collect();
                data.follow_target_path = self.settings.do_target_path == ShouldDoTargetPath::Yes
                    || self.settings.do_target_path == ShouldDoTargetPath::DoWhilePlayed
                        && !self.status.game_state.paused;
            }
            self.send(
                Robot(name),
                ToRobot(ServerToRobotMessage::FrequentRobotItems(data)),
            )
            .await;

            if self.settings.robots[name as usize].extra_opts_enabled {
                self.send(
                    Robot(name),
                    ToRobot(ServerToRobotMessage::ExtraOpts(
                        self.settings.robots[name as usize].extra_opts,
                    )),
                )
                .await;
            }
        }
    }

    async fn send(&mut self, destination: Destination, outgoing: Outgoing) {
        if self.settings.safe_mode {
            if let ToRobot(msg) = &outgoing {
                let encoded = bin_encode(false, TextOrT::T(msg.clone())).unwrap();
                if encoded.get(9).map(|x| *x > 7).unwrap_or(false) {
                    return;
                }
            }
        }
        self.sockets
            .outgoing
            .send((destination, outgoing))
            .await
            .unwrap();
    }

    fn trigger_strategy_update(&mut self) {
        const LOOKAHEAD_DIST: usize = 4;
        if let Some(cv_loc) = self.status.cv_location {
            match self.settings.driving.strategy {
                StrategyChoice::TestUniform => {
                    if self.status.target_path.is_empty() {
                        // find reachable location
                        if let Some(path) = self
                            .grid
                            .walkable_nodes()
                            .iter()
                            .filter(|x| **x != cv_loc)
                            .flat_map(|p| self.grid.bfs_path(cv_loc, *p))
                            .choose(&mut thread_rng())
                        {
                            self.status.target_path = path.into_iter().skip(1).collect();
                        }
                    }
                }
                StrategyChoice::ReinforcementLearning => {
                    self.inference_timer.start();
                    let first = self.status.target_path.first().copied();
                    self.status.target_path.clear();
                    if let Some(first) = first {
                        self.status.target_path.push(first);
                    }

                    // if second AI
                    if !(self.status.game_state.pellet_at((3, 1))
                        || self.status.game_state.pellet_at((23, 1))
                        || self.status.game_state.pellet_at((3, 26))
                        || self.status.game_state.pellet_at((23, 26))
                        || self
                            .status
                            .game_state
                            .ghosts
                            .into_iter()
                            .any(|g| g.is_frightened()))
                    {
                        // and less than 10 pellets
                        if self.status.game_state.num_pellets <= 10 {
                            if let Some(end_path) =
                                self.find_game_ending_path(&self.status.game_state)
                            {
                                self.status.target_path = end_path;
                                return;
                            }
                        }
                    }

                    let mut future = self.status.game_state.clone();
                    if let Some(first) = first {
                        future.set_pacman_location((first.x, first.y));
                    }
                    while self.status.target_path.len() < LOOKAHEAD_DIST {
                        if self
                            .grid
                            .wall_at(&Point2::new(future.pacman_loc.row, future.pacman_loc.col))
                            || (((future.pacman_loc.row == 3) || (future.pacman_loc.row == 23))
                                && ((future.pacman_loc.col == 1) || (future.pacman_loc.col == 26))
                                && !self.status.target_path.is_empty())
                        {
                            break;
                        }
                        let rl_direction = self.rl_manager.hybrid_strategy(future.clone());
                        let rl_vec = rl_direction.vector();
                        let new_p = Point2::new(
                            future.pacman_loc.row + rl_vec.0,
                            future.pacman_loc.col + rl_vec.1,
                        );
                        if !self.status.target_path.contains(&new_p) && new_p != cv_loc {
                            self.status.target_path.push(new_p);
                        } else {
                            break;
                        }
                        future.set_pacman_location((
                            future.pacman_loc.row + rl_vec.0,
                            future.pacman_loc.col + rl_vec.1,
                        ));
                    }

                    self.inference_timer.mark_completed("inference").unwrap();
                    self.status.inference_time = self.inference_timer.status();
                }
                StrategyChoice::TestForward => {
                    while self.status.target_path.len() < LOOKAHEAD_DIST {
                        let last_loc = self.status.target_path.last().copied().unwrap_or(cv_loc);
                        if let Some(neighbor) = self
                            .grid
                            .neighbors(&last_loc)
                            .into_iter()
                            .filter(|x| !self.status.target_path.contains(x) && *x != cv_loc)
                            .choose(&mut thread_rng())
                        {
                            self.status.target_path.push(neighbor);
                        } else {
                            break;
                        }
                    }
                }
                _ => {}
            }
        } else {
            self.status.target_path.clear();
        }
    }

    fn find_game_ending_path(&self, game_state: &GameState) -> Option<Vec<Point2<i8>>> {
        let mut cur_pos = Point2::new(game_state.pacman_loc.row, game_state.pacman_loc.col);
        let mut path = Vec::new();

        let mut remaining_pellets = (0..GRID_SIZE)
            .flat_map(|row| (0..GRID_SIZE).map(move |col| Point2::new(row as i8, col as i8)))
            .filter(|&pos| game_state.pellet_at((pos.x, pos.y)))
            .collect::<HashSet<_>>();
        while let Some(&closest_pellet) = remaining_pellets
            .iter()
            .min_by_key(|&pellet_pos| self.grid.dist(&cur_pos, pellet_pos))
        {
            for path_pos in self.grid.bfs_path(cur_pos, closest_pellet)? {
                // If any ghosts are too close to this location (extrapolating ahead in time pessimistically),
                // then abort and return None.
                if game_state.ghosts.iter().any(|ghost| {
                    // check if too close
                    let ghost_pos = Point2::new(ghost.loc.row, ghost.loc.col);
                    if let Some(dist_from_ghost) =
                        Some((path_pos.x - ghost_pos.x).abs() + (path_pos.y - ghost_pos.y).abs())
                    {
                        let num_pacman_moves = path.len();
                        let num_ghost_moves = ((10.0 / game_state.update_period as f32) // todo add as a setting
                            * num_pacman_moves as f32)
                            + 2.0;
                        // println!(
                        //     "{num_pacman_moves} {dist_from_ghost} {num_ghost_moves} {ghost_pos:?} {:?} {:?} {:?}"
                        // , self.grid.dist(&path_pos, &ghost_pos), path_pos, ghost_pos);
                        (dist_from_ghost as f32) < num_ghost_moves
                    } else {
                        false // no path from ghost to pacman
                    }
                }) {
                    return None;
                }

                let is_start_location = path.is_empty() && path_pos == cur_pos;
                let is_last_path_pos = path.last().is_some_and(|&last| last == path_pos);
                if !is_start_location && !is_last_path_pos {
                    path.push(path_pos);
                }
            }

            if let Some(&last) = path.last() {
                cur_pos = last;
            }

            remaining_pellets.remove(&closest_pellet);
        }

        Some(path)
    }

    fn trigger_cv_location_update(&mut self) {
        let old_loc = self.status.cv_location;
        self.status.cv_location = match self.settings.cv_location_source {
            CvLocationSource::GameState => Some(Point2::new(
                self.status.game_state.pacman_loc.row,
                self.status.game_state.pacman_loc.col,
            )),
            CvLocationSource::Constant(p) => p,
            CvLocationSource::Localization => self.status.robots[self.settings.pacman as usize]
                .estimated_location
                .and_then(|p| self.grid.node_nearest(p.x, p.y))
                .or(old_loc),
        };

        if old_loc != self.status.cv_location {
            if let Some(cv_loc) = self.status.cv_location {
                let mut truncate_from = None;
                for (i, loc) in self.status.target_path.iter().enumerate().rev() {
                    if *loc == cv_loc {
                        truncate_from = Some(i + 1);
                        break;
                    }
                }
                if let Some(truncate_from) = truncate_from {
                    self.status.target_path = self
                        .status
                        .target_path
                        .clone()
                        .into_iter()
                        .skip(truncate_from)
                        .collect();
                }
                if let Some(first) = self.status.target_path.first() {
                    if (first.x - cv_loc.x).abs() + (first.y - cv_loc.y).abs() > 1 {
                        self.status.target_path.clear();
                    }
                }
            } else {
                self.status.target_path.clear();
            }
            self.trigger_strategy_update();
        }
    }

    async fn update_connection(
        &mut self,
        old_settings: &ConnectionSettings,
        new_settings: &ConnectionSettings,
        destination: Destination,
    ) {
        if new_settings != old_settings {
            if new_settings.connect {
                self.send(
                    destination,
                    Outgoing::Address(Some((new_settings.ipv4, new_settings.port))),
                )
                .await;
            } else {
                self.send(destination, Outgoing::Address(None)).await;
            }
        }
    }

    async fn update_settings(&mut self, old: &PacbotSettings, new: PacbotSettings) {
        self.update_connection(
            &old.simulation.connection,
            &new.simulation.connection,
            Destination::Simulation,
        )
        .await;
        self.update_connection(
            &old.game_server.connection,
            &new.game_server.connection,
            Destination::GameServer,
        )
        .await;

        for name in RobotName::get_all() {
            let id = name as usize;
            self.update_connection(
                &old.robots[id].connection,
                &new.robots[id].connection,
                Robot(name),
            )
            .await;
        }

        if new.standard_grid != old.standard_grid {
            self.grid = new.standard_grid.compute_grid();
            self.send(
                Simulation,
                ToSimulation(ServerToSimulationMessage::SetStandardGrid(
                    new.standard_grid,
                )),
            )
            .await;
        }

        if new.simulation.simulate {
            if self.sim_game_engine_process.is_none() {
                self.sim_game_engine_process = Some(
                    Command::new("cargo")
                        .args(["run", "--bin", "sim_pb"])
                        .current_dir(env!("CARGO_MANIFEST_DIR").to_string() + "/../")
                        .spawn()
                        .unwrap(),
                );
            }
        } else if let Some(mut child) = self.sim_game_engine_process.take() {
            child.kill().unwrap();
        }

        if new.host_http {
            if self.client_http_host_process.is_none() {
                self.client_http_host_process = Some(
                    Command::new("trunk")
                        .args(["serve", "--release", "--config", "gui_pb/Trunk.toml"])
                        .current_dir(env!("CARGO_MANIFEST_DIR").to_string() + "/../")
                        .spawn()
                        .unwrap(),
                );
            }
        } else if let Some(mut child) = self.client_http_host_process.take() {
            child.kill().unwrap();
        }

        if old.pacman != new.pacman {
            self.send(
                Simulation,
                ToSimulation(ServerToSimulationMessage::SetPacman(new.pacman)),
            )
            .await;
        }

        if old.driving.strategy != new.driving.strategy || old.standard_grid != new.standard_grid {
            self.status.target_path.clear();
            self.settings.driving.strategy = new.driving.strategy.clone();
            self.trigger_strategy_update();
        }

        self.trigger_cv_location_update();

        self.settings = new;
    }
}