| 1 | /* |
| 2 | * Copyright 2020-2024 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 rand::RngExt; |
| 21 | use std::cell::Cell; |
| 22 | |
| 23 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 24 | pub enum ObjectType { |
| 25 | Player = 0, |
| 26 | Heap = 1, |
| 27 | Robot1 = 2, |
| 28 | Robot2 = 3, |
| 29 | None = 99, |
| 30 | } |
| 31 | |
| 32 | #[derive(Clone, Copy)] |
| 33 | pub struct Position { |
| 34 | pub x: u32, |
| 35 | pub y: u32, |
| 36 | } |
| 37 | |
| 38 | impl Position { |
| 39 | pub fn move_by(self, dx: i32, dy: i32) -> Self { |
| 40 | Self { |
| 41 | x: self.x.saturating_add_signed(dx), |
| 42 | y: self.y.saturating_add_signed(dy), |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | pub struct Arena { |
| 48 | width: u32, |
| 49 | height: u32, |
| 50 | objects: Vec<Cell<ObjectType>>, |
| 51 | } |
| 52 | |
| 53 | impl Arena { |
| 54 | pub fn new(width: u32, height: u32) -> Self { |
| 55 | Self { |
| 56 | width, |
| 57 | height, |
| 58 | objects: vec![Cell::new(ObjectType::None); (width * height) as usize], |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn width(&self) -> u32 { |
| 63 | self.width |
| 64 | } |
| 65 | |
| 66 | pub fn height(&self) -> u32 { |
| 67 | self.height |
| 68 | } |
| 69 | |
| 70 | pub fn clear(&self) { |
| 71 | for obj in &self.objects { |
| 72 | obj.set(ObjectType::None); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | pub fn are_coords_valid(&self, position: Position) -> bool { |
| 77 | position.x < self.width && position.y < self.height |
| 78 | } |
| 79 | |
| 80 | fn index_of(&self, position: Position) -> Option<usize> { |
| 81 | if self.are_coords_valid(position) { |
| 82 | Some((position.x + position.y * self.width) as usize) |
| 83 | } else { |
| 84 | None |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | pub fn get(&self, position: Position) -> ObjectType { |
| 89 | if let Some(index) = self.index_of(position) { |
| 90 | self.objects[index].get() |
| 91 | } else { |
| 92 | ObjectType::None |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | pub fn set(&self, position: Position, obj: ObjectType) { |
| 97 | if let Some(index) = self.index_of(position) { |
| 98 | self.objects[index].set(obj); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pub fn put(&self, position: Position, obj: ObjectType) -> bool { |
| 103 | if let Some(index) = self.index_of(position) |
| 104 | && self.objects[index].get() == ObjectType::None |
| 105 | { |
| 106 | self.objects[index].set(obj); |
| 107 | true |
| 108 | } else { |
| 109 | false |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | pub fn map(&self, mapper: impl Fn(ObjectType) -> ObjectType) -> Self { |
| 114 | let new_arena = Self { |
| 115 | width: self.width, |
| 116 | height: self.height, |
| 117 | objects: self.objects.clone(), |
| 118 | }; |
| 119 | for obj in &new_arena.objects { |
| 120 | obj.set((mapper)(obj.get())); |
| 121 | } |
| 122 | new_arena |
| 123 | } |
| 124 | |
| 125 | pub fn count(&self, predicate: impl Fn(ObjectType) -> bool) -> usize { |
| 126 | let mut result = 0; |
| 127 | for obj in &self.objects { |
| 128 | if predicate(obj.get()) { |
| 129 | result += 1; |
| 130 | } |
| 131 | } |
| 132 | result |
| 133 | } |
| 134 | |
| 135 | pub fn random_vacant_position(&self, rand: &mut dyn rand::Rng) -> Option<Position> { |
| 136 | let size = self.objects.len(); |
| 137 | let start = rand.random_range(0..size); |
| 138 | for i in 0..size { |
| 139 | let index = (start + i) % size; |
| 140 | if self.objects[index].get() == ObjectType::None { |
| 141 | return Some(Position { |
| 142 | x: index as u32 % self.width, |
| 143 | y: index as u32 / self.width, |
| 144 | }); |
| 145 | } |
| 146 | } |
| 147 | None |
| 148 | } |
| 149 | } |