| 1 | /* |
| 2 | * Copyright 2020-2026 Andrey Kutejko <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * For more details see the file COPYING. |
| 18 | */ |
| 19 | |
| 20 | use crate::{ |
| 21 | arena::{Arena, ObjectType, Position}, |
| 22 | game_config::{GameConfig, Reward}, |
| 23 | slot::Slot, |
| 24 | }; |
| 25 | use std::cell::{Cell, RefCell}; |
| 26 | use std::iter::{chain, repeat_n}; |
| 27 | use std::rc::Rc; |
| 28 | |
| 29 | #[derive(Clone, Copy)] |
| 30 | pub enum PlayerCommand { |
| 31 | NW, |
| 32 | N, |
| 33 | NE, |
| 34 | W, |
| 35 | Stay, |
| 36 | E, |
| 37 | SW, |
| 38 | S, |
| 39 | SE, |
| 40 | SafeTeleport, |
| 41 | RandomTeleport, |
| 42 | Wait, |
| 43 | } |
| 44 | |
| 45 | impl PlayerCommand { |
| 46 | pub fn from_direction(dx: i32, dy: i32) -> Self { |
| 47 | match (dx, dy) { |
| 48 | (-1, -1) => PlayerCommand::NW, |
| 49 | (0, -1) => PlayerCommand::N, |
| 50 | (1, -1) => PlayerCommand::NE, |
| 51 | (-1, 0) => PlayerCommand::W, |
| 52 | (0, 0) => PlayerCommand::Stay, |
| 53 | (1, 0) => PlayerCommand::E, |
| 54 | (-1, 1) => PlayerCommand::SW, |
| 55 | (0, 1) => PlayerCommand::S, |
| 56 | (1, 1) => PlayerCommand::SE, |
| 57 | _ => PlayerCommand::Stay, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | pub fn to_direction(self) -> Option<(i32, i32)> { |
| 62 | match self { |
| 63 | PlayerCommand::NW => Some((-1, -1)), |
| 64 | PlayerCommand::N => Some((0, -1)), |
| 65 | PlayerCommand::NE => Some((1, -1)), |
| 66 | PlayerCommand::W => Some((-1, 0)), |
| 67 | PlayerCommand::Stay => Some((0, 0)), |
| 68 | PlayerCommand::E => Some((1, 0)), |
| 69 | PlayerCommand::SW => Some((-1, 1)), |
| 70 | PlayerCommand::S => Some((0, 1)), |
| 71 | PlayerCommand::SE => Some((1, 1)), |
| 72 | _ => None, |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Size of the game playing area |
| 79 | */ |
| 80 | pub const GAME_WIDTH: u32 = 45; |
| 81 | pub const GAME_HEIGHT: u32 = 30; |
| 82 | |
| 83 | pub const DEAD_DELAY: u32 = 30; |
| 84 | pub const CHANGE_DELAY: u32 = 20; |
| 85 | |
| 86 | pub struct Status { |
| 87 | pub score: u32, |
| 88 | pub current_level: u32, |
| 89 | pub safe_teleports: u32, |
| 90 | } |
| 91 | |
| 92 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 93 | pub enum State { |
| 94 | Playing = 1, |
| 95 | Waiting, |
| 96 | Complete, |
| 97 | Dead, |
| 98 | Type2, |
| 99 | WaitingType2, |
| 100 | } |
| 101 | |
| 102 | #[derive(Clone, Copy)] |
| 103 | pub enum GameEvent { |
| 104 | Teleported, |
| 105 | Splat, |
| 106 | LevelComplete, |
| 107 | Death, |
| 108 | Scored(u32), |
| 109 | Victory, |
| 110 | NoTeleportLocations, |
| 111 | NoSafeTeleportLocations, |
| 112 | } |
| 113 | |
| 114 | struct ArenaChange { |
| 115 | arena: Arena, |
| 116 | player: Position, |
| 117 | push: Option<Position>, |
| 118 | } |
| 119 | |
| 120 | #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)] |
| 121 | pub enum MoveSafety { |
| 122 | Unsafe, |
| 123 | Safe, |
| 124 | SuperSafe, |
| 125 | } |
| 126 | |
| 127 | pub struct Game { |
| 128 | state: Cell<State>, |
| 129 | arena: RefCell<Arena>, |
| 130 | config: RefCell<Rc<GameConfig>>, |
| 131 | player: Cell<Position>, |
| 132 | splat: Cell<Option<Position>>, |
| 133 | rand: RefCell<Box<dyn rand::Rng>>, |
| 134 | endlev_counter: Cell<u32>, |
| 135 | current_level: Cell<u32>, |
| 136 | score: Cell<u32>, |
| 137 | kills: Cell<u32>, |
| 138 | safe_teleports: Cell<u32>, |
| 139 | |
| 140 | pub on_game_event: Slot<GameEvent>, |
| 141 | } |
| 142 | |
| 143 | impl Game { |
| 144 | pub fn new(config: &Rc<GameConfig>, rand: impl rand::Rng + 'static) -> Self { |
| 145 | Self { |
| 146 | state: Cell::new(State::Playing), |
| 147 | arena: RefCell::new(Arena::new(GAME_WIDTH, GAME_HEIGHT)), |
| 148 | config: RefCell::new(config.clone()), |
| 149 | player: Cell::new(Position { x: 0, y: 0 }), |
| 150 | splat: Cell::new(None), |
| 151 | rand: RefCell::new(Box::new(rand)), |
| 152 | endlev_counter: Cell::new(0), |
| 153 | current_level: Cell::new(0), |
| 154 | score: Cell::new(0), |
| 155 | kills: Cell::new(0), |
| 156 | safe_teleports: Cell::new(0), |
| 157 | on_game_event: Slot::new(), |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | pub fn width(&self) -> u32 { |
| 162 | self.arena.borrow().width() |
| 163 | } |
| 164 | |
| 165 | pub fn height(&self) -> u32 { |
| 166 | self.arena.borrow().height() |
| 167 | } |
| 168 | |
| 169 | pub fn get(&self, position: Position) -> ObjectType { |
| 170 | self.arena.borrow().get(position) |
| 171 | } |
| 172 | |
| 173 | pub fn state(&self) -> State { |
| 174 | self.state.get() |
| 175 | } |
| 176 | |
| 177 | pub fn status(&self) -> Status { |
| 178 | Status { |
| 179 | score: self.score.get(), |
| 180 | current_level: self.current_level.get() + 1, |
| 181 | safe_teleports: self.safe_teleports.get(), |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | pub fn player(&self) -> Position { |
| 186 | self.player.get() |
| 187 | } |
| 188 | |
| 189 | pub fn splat(&self) -> Option<Position> { |
| 190 | self.splat.get() |
| 191 | } |
| 192 | |
| 193 | pub fn config(&self) -> Rc<GameConfig> { |
| 194 | self.config.borrow().clone() |
| 195 | } |
| 196 | |
| 197 | pub fn set_config(&self, config: &Rc<GameConfig>) { |
| 198 | if *self.config.borrow() != *config { |
| 199 | *self.config.borrow_mut() = config.clone(); |
| 200 | self.start_new_game(); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /// Ends the current game. |
| 205 | fn kill_player(&self) { |
| 206 | self.state.set(State::Dead); |
| 207 | self.arena |
| 208 | .borrow() |
| 209 | .set(self.player.get(), ObjectType::Player); |
| 210 | self.endlev_counter.set(0); |
| 211 | self.on_game_event.emit(GameEvent::Death); |
| 212 | } |
| 213 | |
| 214 | /// Registers a robot kill and updates the score |
| 215 | fn add_kill(&self, object_type: ObjectType) { |
| 216 | let config = self.config.borrow().clone(); |
| 217 | |
| 218 | let score_increment = match (self.state.get(), object_type) { |
| 219 | (State::Waiting | State::WaitingType2, ObjectType::Robot1) => { |
| 220 | self.kills.update(|k| k + 1); |
| 221 | config.robot_type1.score_waiting |
| 222 | } |
| 223 | (State::Waiting | State::WaitingType2, ObjectType::Robot2) => { |
| 224 | self.kills.update(|k| k + 2); |
| 225 | config.robot_type2.score_waiting |
| 226 | } |
| 227 | (_, ObjectType::Robot1) => config.robot_type1.score, |
| 228 | (_, ObjectType::Robot2) => config.robot_type2.score, |
| 229 | _ => return, |
| 230 | }; |
| 231 | |
| 232 | self.score.update(|s| s + score_increment); |
| 233 | |
| 234 | if let Some(ref safe_teleports) = config.safe_teleports |
| 235 | && let Some(Reward { |
| 236 | price, |
| 237 | safe_teleports_reward, |
| 238 | }) = safe_teleports.kills_reward(self.safe_teleports.get(), self.kills.get()) |
| 239 | { |
| 240 | self.kills.update(|k| k - price); |
| 241 | self.safe_teleports.update(|t| t + safe_teleports_reward); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /// Creates a new level and populates it with robots |
| 246 | fn generate_level(&self) { |
| 247 | let config = self.config.borrow().clone(); |
| 248 | |
| 249 | let arena = self.arena.borrow_mut(); |
| 250 | arena.clear(); |
| 251 | |
| 252 | self.player.set(Position { |
| 253 | x: arena.width() / 2, |
| 254 | y: arena.height() / 2, |
| 255 | }); |
| 256 | arena.set(self.player.get(), ObjectType::Player); |
| 257 | |
| 258 | let mut num_robots1 = config.robot_type1.robots_on_level(self.current_level.get()); |
| 259 | let mut num_robots2 = config.robot_type2.robots_on_level(self.current_level.get()); |
| 260 | |
| 261 | let max_robots = arena.width() * arena.height() / 2; |
| 262 | if num_robots1 + num_robots2 > max_robots { |
| 263 | self.current_level.set(0); |
| 264 | num_robots1 = config.robot_type1.robots_on_level(0); |
| 265 | num_robots2 = config.robot_type2.robots_on_level(0); |
| 266 | |
| 267 | self.on_game_event.emit(GameEvent::Victory); |
| 268 | } |
| 269 | |
| 270 | if let Some(free_teleports) = config |
| 271 | .safe_teleports |
| 272 | .as_ref() |
| 273 | .map(|st| st.free(self.safe_teleports.get())) |
| 274 | { |
| 275 | self.safe_teleports.update(|t| t + free_teleports); |
| 276 | } |
| 277 | |
| 278 | for robot in chain( |
| 279 | repeat_n(ObjectType::Robot1, num_robots1 as usize), |
| 280 | repeat_n(ObjectType::Robot2, num_robots2 as usize), |
| 281 | ) { |
| 282 | let p = arena |
| 283 | .random_vacant_position(&mut *self.rand.borrow_mut()) |
| 284 | .expect("No vacant positions"); |
| 285 | arena.set(p, robot); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | fn update_arena(&self, change: ArenaChange) { |
| 290 | if let Some(push) = change.push { |
| 291 | match self.arena.borrow().get(push) { |
| 292 | ObjectType::Robot1 => { |
| 293 | self.splat.set(Some(push)); |
| 294 | self.on_game_event.emit(GameEvent::Splat); |
| 295 | self.score |
| 296 | .update(|s| s + self.config.borrow().robot_type1.score_splatted); |
| 297 | } |
| 298 | ObjectType::Robot2 => { |
| 299 | self.splat.set(Some(push)); |
| 300 | self.on_game_event.emit(GameEvent::Splat); |
| 301 | self.score |
| 302 | .update(|s| s + self.config.borrow().robot_type2.score_splatted); |
| 303 | } |
| 304 | _ => {} |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | self.player.set(change.player); |
| 309 | *self.arena.borrow_mut() = change.arena; |
| 310 | |
| 311 | if self.arena.borrow().get(change.player) != ObjectType::Player { |
| 312 | self.kill_player(); |
| 313 | } else { |
| 314 | /* This is in the else statement to catch the case where the last |
| 315 | * two robots collide on top of the human. Without the "else" this |
| 316 | * leads to the player being ressurected and winning. */ |
| 317 | if self |
| 318 | .arena |
| 319 | .borrow() |
| 320 | .count(|obj| obj == ObjectType::Robot1 || obj == ObjectType::Robot2) |
| 321 | == 0 |
| 322 | { |
| 323 | self.state.set(State::Complete); |
| 324 | self.on_game_event.emit(GameEvent::LevelComplete); |
| 325 | self.endlev_counter.set(0); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | pub fn tick(&self) { |
| 331 | match self.state.get() { |
| 332 | State::Type2 => { |
| 333 | let new_arena = self.move_type2_robots(); |
| 334 | let change = ArenaChange { |
| 335 | arena: new_arena, |
| 336 | player: self.player.get(), |
| 337 | push: None, |
| 338 | }; |
| 339 | self.update_arena(change); |
| 340 | self.state.set(State::Playing); |
| 341 | } |
| 342 | State::WaitingType2 => { |
| 343 | let new_arena = self.move_type2_robots(); |
| 344 | let change = ArenaChange { |
| 345 | arena: new_arena, |
| 346 | player: self.player.get(), |
| 347 | push: None, |
| 348 | }; |
| 349 | self.update_arena(change); |
| 350 | self.state.set(State::Waiting); |
| 351 | } |
| 352 | State::Waiting => { |
| 353 | self.splat.set(None); |
| 354 | self.move_robots(); |
| 355 | } |
| 356 | State::Complete => { |
| 357 | self.endlev_counter.set(self.endlev_counter.get() + 1); |
| 358 | if self.endlev_counter.get() >= CHANGE_DELAY { |
| 359 | self.current_level.set(self.current_level.get() + 1); |
| 360 | self.generate_level(); |
| 361 | self.state.set(State::Playing); |
| 362 | self.splat.set(None); |
| 363 | } |
| 364 | } |
| 365 | State::Dead => { |
| 366 | self.endlev_counter.set(self.endlev_counter.get() + 1); |
| 367 | if self.endlev_counter.get() >= DEAD_DELAY { |
| 368 | if self.score.get() > 0 { |
| 369 | self.on_game_event.emit(GameEvent::Scored(self.score.get())); |
| 370 | } |
| 371 | self.start_new_game(); |
| 372 | } |
| 373 | } |
| 374 | State::Playing => {} |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /// Initialises everything needed to start a new game |
| 379 | pub fn start_new_game(&self) { |
| 380 | self.current_level.set(0); |
| 381 | self.score.set(0); |
| 382 | self.kills.set(0); |
| 383 | |
| 384 | self.safe_teleports.set( |
| 385 | self.config |
| 386 | .borrow() |
| 387 | .safe_teleports |
| 388 | .as_ref() |
| 389 | .map_or(0, |s| s.initial), |
| 390 | ); |
| 391 | |
| 392 | self.splat.set(None); |
| 393 | self.generate_level(); |
| 394 | |
| 395 | self.state.set(State::Playing); |
| 396 | } |
| 397 | |
| 398 | /// Moves all of the robots and checks for collisions |
| 399 | fn move_all_robots(&self) -> Arena { |
| 400 | return Self::chase( |
| 401 | &self.arena.borrow(), |
| 402 | |obj| obj == ObjectType::Robot1 || obj == ObjectType::Robot2, |
| 403 | self.player.get(), |
| 404 | |victim| self.add_kill(victim), |
| 405 | ); |
| 406 | } |
| 407 | |
| 408 | /// Makes the extra move for all of the type2 robots |
| 409 | fn move_type2_robots(&self) -> Arena { |
| 410 | return Self::chase( |
| 411 | &self.arena.borrow(), |
| 412 | |obj| obj == ObjectType::Robot2, |
| 413 | self.player.get(), |
| 414 | |victim| self.add_kill(victim), |
| 415 | ); |
| 416 | } |
| 417 | |
| 418 | /// Starts the process of moving robots |
| 419 | fn move_robots(&self) { |
| 420 | let new_arena = self.move_all_robots(); |
| 421 | |
| 422 | let num_robots2 = new_arena.count(|obj| obj == ObjectType::Robot2); |
| 423 | if num_robots2 > 0 { |
| 424 | if self.state.get() == State::Waiting { |
| 425 | self.state.set(State::WaitingType2); |
| 426 | } else if self.state.get() == State::Playing { |
| 427 | self.state.set(State::Type2); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | let change = ArenaChange { |
| 432 | arena: new_arena, |
| 433 | player: self.player.get(), |
| 434 | push: None, |
| 435 | }; |
| 436 | |
| 437 | self.update_arena(change); |
| 438 | } |
| 439 | |
| 440 | fn chase( |
| 441 | arena: &Arena, |
| 442 | is_chaser: impl Fn(ObjectType) -> bool, |
| 443 | player: Position, |
| 444 | track_kill: impl Fn(ObjectType), |
| 445 | ) -> Arena { |
| 446 | let new_arena = arena.map(|obj| { |
| 447 | if obj == ObjectType::Player || (is_chaser)(obj) { |
| 448 | ObjectType::None |
| 449 | } else { |
| 450 | obj |
| 451 | } |
| 452 | }); |
| 453 | |
| 454 | for x in 0..arena.width() { |
| 455 | for y in 0..arena.height() { |
| 456 | let position = Position { x, y }; |
| 457 | let who = arena.get(position); |
| 458 | if (is_chaser)(who) { |
| 459 | let new_postions = position.move_by( |
| 460 | ((player.x as i32) - (position.x as i32)).signum(), |
| 461 | ((player.y as i32) - (position.y as i32)).signum(), |
| 462 | ); |
| 463 | |
| 464 | let destination = new_arena.get(new_postions); |
| 465 | if destination == ObjectType::Heap { |
| 466 | (track_kill)(who); |
| 467 | } else if destination == ObjectType::Robot1 || destination == ObjectType::Robot2 |
| 468 | { |
| 469 | (track_kill)(who); |
| 470 | (track_kill)(destination); |
| 471 | new_arena.set(new_postions, ObjectType::Heap); |
| 472 | } else { |
| 473 | new_arena.set(new_postions, who); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | if new_arena.get(player) == ObjectType::None { |
| 480 | new_arena.set(player, ObjectType::Player); |
| 481 | } |
| 482 | |
| 483 | new_arena |
| 484 | } |
| 485 | |
| 486 | /// checks whether a given location is safe |
| 487 | fn check_safe(&self, change: &ArenaChange) -> bool { |
| 488 | let temp2_arena = Self::chase( |
| 489 | &change.arena, |
| 490 | |obj| obj == ObjectType::Robot1 || obj == ObjectType::Robot2, |
| 491 | change.player, |
| 492 | |_| {}, |
| 493 | ); |
| 494 | |
| 495 | if temp2_arena.get(change.player) != ObjectType::Player { |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | let temp3_arena = Self::chase( |
| 500 | &temp2_arena, |
| 501 | |obj| obj == ObjectType::Robot2, |
| 502 | change.player, |
| 503 | |_| {}, |
| 504 | ); |
| 505 | |
| 506 | if temp3_arena.get(change.player) != ObjectType::Player { |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | true |
| 511 | } |
| 512 | |
| 513 | fn try_push_heap(&self, dx: i32, dy: i32) -> Option<ArenaChange> { |
| 514 | if !self.config.borrow().moveable_heaps { |
| 515 | return None; |
| 516 | } |
| 517 | |
| 518 | let arena = self.arena.borrow(); |
| 519 | let coords = self.player.get().move_by(dx, dy); |
| 520 | if !arena.are_coords_valid(coords) || arena.get(coords) != ObjectType::Heap { |
| 521 | return None; |
| 522 | } |
| 523 | |
| 524 | let push_to = coords.move_by(dx, dy); |
| 525 | if !arena.are_coords_valid(push_to) || arena.get(push_to) == ObjectType::Heap { |
| 526 | return None; |
| 527 | } |
| 528 | |
| 529 | let new_arena = arena.map(|obj| { |
| 530 | if obj != ObjectType::Player { |
| 531 | obj |
| 532 | } else { |
| 533 | ObjectType::None |
| 534 | } |
| 535 | }); |
| 536 | new_arena.set(coords, ObjectType::Player); |
| 537 | new_arena.set(push_to, ObjectType::Heap); |
| 538 | Some(ArenaChange { |
| 539 | arena: new_arena, |
| 540 | player: coords, |
| 541 | push: Some(push_to), |
| 542 | }) |
| 543 | } |
| 544 | |
| 545 | /// Tries to move the player in a given direction |
| 546 | fn try_player_move(&self, dx: i32, dy: i32) -> Option<ArenaChange> { |
| 547 | let coords = self.player.get().move_by(dx, dy); |
| 548 | |
| 549 | let arena = self.arena.borrow(); |
| 550 | if !arena.are_coords_valid(coords) { |
| 551 | return None; |
| 552 | } |
| 553 | |
| 554 | if arena.get(coords) == ObjectType::Heap { |
| 555 | self.try_push_heap(dx, dy) |
| 556 | } else { |
| 557 | Some(self.move_player_to(coords)) |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | fn available_moves(&self) -> impl Iterator<Item = ArenaChange> { |
| 562 | (0..9).filter_map(|d| { |
| 563 | let dx = d % 3 - 1; |
| 564 | let dy = d / 3 - 1; |
| 565 | self.try_player_move(dx, dy) |
| 566 | }) |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * safe_move_available |
| 571 | * |
| 572 | * Description: |
| 573 | * checks to see if a safe move was available to the player |
| 574 | * |
| 575 | * Returns: |
| 576 | * TRUE if there is a possible safe move, FALSE otherwise |
| 577 | **/ |
| 578 | fn safe_move_available(&self) -> bool { |
| 579 | self.available_moves() |
| 580 | .any(|change| self.check_safe(&change)) |
| 581 | } |
| 582 | |
| 583 | fn move_player_to(&self, coords: Position) -> ArenaChange { |
| 584 | let new_arena = self.arena.borrow().map(|obj| { |
| 585 | if obj != ObjectType::Player { |
| 586 | obj |
| 587 | } else { |
| 588 | ObjectType::None |
| 589 | } |
| 590 | }); |
| 591 | new_arena.put(coords, ObjectType::Player); |
| 592 | ArenaChange { |
| 593 | arena: new_arena, |
| 594 | player: coords, |
| 595 | push: None, |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * player_move |
| 601 | * @dx: x direction |
| 602 | * @dy: y direction |
| 603 | * |
| 604 | * Description: |
| 605 | * moves the player in a given direction |
| 606 | * |
| 607 | * Returns: |
| 608 | * TRUE if the player can move, FALSE otherwise |
| 609 | **/ |
| 610 | fn player_move(&self, dx: i32, dy: i32, safety: MoveSafety) -> bool { |
| 611 | let Some(change) = self.try_player_move(dx, dy) else { |
| 612 | return false; |
| 613 | }; |
| 614 | |
| 615 | match safety { |
| 616 | MoveSafety::Unsafe => {} |
| 617 | MoveSafety::Safe => { |
| 618 | // Prevent unsafe moves, but only when a safe one is available |
| 619 | if !self.check_safe(&change) && self.safe_move_available() { |
| 620 | return false; |
| 621 | } |
| 622 | } |
| 623 | MoveSafety::SuperSafe => { |
| 624 | // Prevent unsafe moves |
| 625 | if !self.check_safe(&change) { |
| 626 | return false; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | self.splat.set(None); |
| 632 | self.update_arena(change); |
| 633 | |
| 634 | true |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * random_teleport |
| 639 | * |
| 640 | * Description: |
| 641 | * randomly teleports the player |
| 642 | * |
| 643 | * Returns: |
| 644 | * TRUE if the player can be teleported, FALSE otherwise |
| 645 | **/ |
| 646 | fn random_teleport(&self) -> bool { |
| 647 | let Some(p) = self |
| 648 | .arena |
| 649 | .borrow() |
| 650 | .random_vacant_position(&mut *self.rand.borrow_mut()) |
| 651 | else { |
| 652 | /* This should never happen. */ |
| 653 | self.on_game_event.emit(GameEvent::NoTeleportLocations); |
| 654 | return false; |
| 655 | }; |
| 656 | |
| 657 | let change = self.move_player_to(p); |
| 658 | |
| 659 | self.update_arena(change); |
| 660 | self.splat.set(None); |
| 661 | self.on_game_event.emit(GameEvent::Teleported); |
| 662 | true |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * safe_teleport |
| 667 | * |
| 668 | * Description: |
| 669 | * teleports the player to safe location |
| 670 | * |
| 671 | * Returns: |
| 672 | * TRUE if player can be teleported, FALSE otherwise |
| 673 | **/ |
| 674 | fn safe_teleport(&self) -> bool { |
| 675 | if self.safe_teleports.get() == 0 { |
| 676 | return false; |
| 677 | } |
| 678 | |
| 679 | let change = loop { |
| 680 | let Some(p) = self |
| 681 | .arena |
| 682 | .borrow() |
| 683 | .random_vacant_position(&mut *self.rand.borrow_mut()) |
| 684 | else { |
| 685 | self.on_game_event.emit(GameEvent::NoSafeTeleportLocations); |
| 686 | self.kill_player(); |
| 687 | return false; |
| 688 | }; |
| 689 | |
| 690 | let change = self.move_player_to(p); |
| 691 | if self.check_safe(&change) { |
| 692 | break change; |
| 693 | } |
| 694 | }; |
| 695 | |
| 696 | self.safe_teleports.set(self.safe_teleports.get() - 1); |
| 697 | |
| 698 | self.update_arena(change); |
| 699 | self.splat.set(None); |
| 700 | self.on_game_event.emit(GameEvent::Teleported); |
| 701 | |
| 702 | true |
| 703 | } |
| 704 | |
| 705 | /// handles player's commands |
| 706 | pub fn player_command( |
| 707 | &self, |
| 708 | cmd: PlayerCommand, |
| 709 | safety: MoveSafety, /* = MoveSafety::UNSAFE */ |
| 710 | ) -> bool { |
| 711 | if self.state.get() != State::Playing { |
| 712 | return false; |
| 713 | } |
| 714 | |
| 715 | match cmd { |
| 716 | PlayerCommand::NW |
| 717 | | PlayerCommand::N |
| 718 | | PlayerCommand::NE |
| 719 | | PlayerCommand::W |
| 720 | | PlayerCommand::Stay |
| 721 | | PlayerCommand::E |
| 722 | | PlayerCommand::SW |
| 723 | | PlayerCommand::S |
| 724 | | PlayerCommand::SE => { |
| 725 | let direction = cmd.to_direction().unwrap(); |
| 726 | if self.player_move(direction.0, direction.1, safety) { |
| 727 | self.move_robots(); |
| 728 | true |
| 729 | } else { |
| 730 | false |
| 731 | } |
| 732 | } |
| 733 | PlayerCommand::SafeTeleport => { |
| 734 | if self.safe_teleport() { |
| 735 | self.move_robots(); |
| 736 | } |
| 737 | true |
| 738 | } |
| 739 | PlayerCommand::RandomTeleport => { |
| 740 | if self.random_teleport() { |
| 741 | self.move_robots(); |
| 742 | } |
| 743 | true |
| 744 | } |
| 745 | PlayerCommand::Wait => { |
| 746 | self.state.set(State::Waiting); |
| 747 | true |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | } |