| 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 randrust-analyzer cargo rand 0.10.0 crate/::RngExtrust-analyzer cargo rand 0.10.0 rng/RngExt#; |
| 21 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#; |
| 22 | |
| 23 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)] |
| 24 | pub enum ObjectTyperust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType# { |
| 25 | Playerrust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#Player# = 0, |
| 26 | Heaprust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#Heap# = 1, |
| 27 | Robot1rust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#Robot1# = 2, |
| 28 | Robot2rust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#Robot2# = 3, |
| 29 | Nonerust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#None# = 99, |
| 30 | } |
| 31 | |
| 32 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)] |
| 33 | pub struct Positionrust-analyzer cargo gnome-robots 50.0.0 arena/Position# { |
| 34 | pub xrust-analyzer cargo gnome-robots 50.0.0 arena/Position#x.: u32, |
| 35 | pub yrust-analyzer cargo gnome-robots 50.0.0 arena/Position#y.: u32, |
| 36 | } |
| 37 | |
| 38 | impl Positionrust-analyzer cargo gnome-robots 50.0.0 arena/Position# { |
| 39 | pub fn move_byrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Position]move_by().(self, dx: i32, dy: i32) -> Selfrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Position] { |
| 40 | Selfrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Position] { |
| 41 | xrust-analyzer cargo gnome-robots 50.0.0 arena/Position#x.: self.xrust-analyzer cargo gnome-robots 50.0.0 arena/Position#x..saturating_add_signedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]saturating_add_signed().(dx), |
| 42 | yrust-analyzer cargo gnome-robots 50.0.0 arena/Position#y.: self.yrust-analyzer cargo gnome-robots 50.0.0 arena/Position#y..saturating_add_signedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]saturating_add_signed().(dy), |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | pub struct Arenarust-analyzer cargo gnome-robots 50.0.0 arena/Arena# { |
| 48 | widthrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#width.: u32, |
| 49 | heightrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#height.: u32, |
| 50 | objectsrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#objects.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#<ObjectTyperust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#>>, |
| 51 | } |
| 52 | |
| 53 | impl Arenarust-analyzer cargo gnome-robots 50.0.0 arena/Arena# { |
| 54 | pub fn newrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]new().(width: u32, height: u32) -> Selfrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena] { |
| 55 | Selfrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena] { |
| 56 | widthrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#width., |
| 57 | heightrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#height., |
| 58 | objectsrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#objects.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]new().(ObjectTyperust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#::Nonerust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#None#); (width *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Mul<Self>`]mul(). height) as usize], |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn widthrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]width().(&self) -> u32 { |
| 63 | self.widthrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#width. |
| 64 | } |
| 65 | |
| 66 | pub fn heightrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]height().(&self) -> u32 { |
| 67 | self.heightrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#height. |
| 68 | } |
| 69 | |
| 70 | pub fn clearrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]clear().(&self) { |
| 71 | for obj in &self.objectsrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#objects. { |
| 72 | obj.setrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]set().(ObjectTyperust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#::Nonerust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#None#); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | pub fn are_coords_validrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]are_coords_valid().(&self, position: Positionrust-analyzer cargo gnome-robots 50.0.0 arena/Position#) -> bool { |
| 77 | position.xrust-analyzer cargo gnome-robots 50.0.0 arena/Position#x. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]lt().self.widthrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#width. && position.yrust-analyzer cargo gnome-robots 50.0.0 arena/Position#y. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]lt().self.heightrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#height. |
| 78 | } |
| 79 | |
| 80 | fn index_ofrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]index_of().(&self, position: Positionrust-analyzer cargo gnome-robots 50.0.0 arena/Position#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize> { |
| 81 | if self.are_coords_validrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]are_coords_valid().(position) { |
| 82 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((position.xrust-analyzer cargo gnome-robots 50.0.0 arena/Position#x. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().position.yrust-analyzer cargo gnome-robots 50.0.0 arena/Position#y. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Mul<Self>`]mul().self.widthrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#width.) as usize) |
| 83 | } else { |
| 84 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | pub fn getrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]get().(&self, position: Positionrust-analyzer cargo gnome-robots 50.0.0 arena/Position#) -> ObjectTyperust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType# { |
| 89 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(index) = self.index_ofrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]index_of().(position) { |
| 90 | self.objectsrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#objects.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().index]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]get().() |
| 91 | } else { |
| 92 | ObjectTyperust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#::Nonerust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#None# |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | pub fn setrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]set().(&self, position: Positionrust-analyzer cargo gnome-robots 50.0.0 arena/Position#, obj: ObjectTyperust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#) { |
| 97 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(index) = self.index_ofrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]index_of().(position) { |
| 98 | self.objectsrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#objects.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().index]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..setrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]set().(obj); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pub fn putrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]put().(&self, position: Positionrust-analyzer cargo gnome-robots 50.0.0 arena/Position#, obj: ObjectTyperust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#) -> bool { |
| 103 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(index) = self.index_ofrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]index_of().(position) |
| 104 | && self.objectsrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#objects.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().index]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]get().() rust-analyzer cargo gnome-robots 50.0.0 arena/impl#[ObjectType][`PartialEq<Self>`]eq().==rust-analyzer cargo gnome-robots 50.0.0 arena/impl#[ObjectType][`PartialEq<Self>`]eq(). rust-analyzer cargo gnome-robots 50.0.0 arena/impl#[ObjectType][`PartialEq<Self>`]eq().ObjectTyperust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#::Nonerust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#None# |
| 105 | { |
| 106 | self.objectsrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#objects.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().index]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..setrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]set().(obj); |
| 107 | true |
| 108 | } else { |
| 109 | false |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | pub fn maprust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]map().(&self, mapper: impl Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(ObjectTyperust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#) -> ObjectTyperust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#) -> Selfrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena] { |
| 114 | let new_arena = Selfrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena] { |
| 115 | widthrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#width.: self.widthrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#width., |
| 116 | heightrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#height.: self.heightrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#height., |
| 117 | objectsrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#objects.: self.objectsrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#objects..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(), |
| 118 | }; |
| 119 | for obj in &new_arena.objectsrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#objects. { |
| 120 | obj.setrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]set().((mapper)(obj.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]get().())); |
| 121 | } |
| 122 | new_arena |
| 123 | } |
| 124 | |
| 125 | pub fn countrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]count().(&self, predicate: impl Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(ObjectTyperust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#) -> bool) -> usize { |
| 126 | let mut result = 0; |
| 127 | for obj in &self.objectsrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#objects. { |
| 128 | if predicate(obj.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]get().()) { |
| 129 | result rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 130 | } |
| 131 | } |
| 132 | result |
| 133 | } |
| 134 | |
| 135 | pub fn random_vacant_positionrust-analyzer cargo gnome-robots 50.0.0 arena/impl#[Arena]random_vacant_position().(&self, rand: &mut dyn randrust-analyzer cargo rand 0.10.0 crate/::Rngrust-analyzer cargo rand_core 0.10.0 Rng#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Positionrust-analyzer cargo gnome-robots 50.0.0 arena/Position#> { |
| 136 | let size = self.objectsrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#objects..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 137 | let start = rand.random_rangerust-analyzer cargo rand 0.10.0 rng/RngExt#random_range().(0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#size); |
| 138 | for i in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#size { |
| 139 | let index = (start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().i) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().size; |
| 140 | if self.objectsrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#objects.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().index]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]get().() rust-analyzer cargo gnome-robots 50.0.0 arena/impl#[ObjectType][`PartialEq<Self>`]eq().==rust-analyzer cargo gnome-robots 50.0.0 arena/impl#[ObjectType][`PartialEq<Self>`]eq(). rust-analyzer cargo gnome-robots 50.0.0 arena/impl#[ObjectType][`PartialEq<Self>`]eq().ObjectTyperust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#::Nonerust-analyzer cargo gnome-robots 50.0.0 arena/ObjectType#None# { |
| 141 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Positionrust-analyzer cargo gnome-robots 50.0.0 arena/Position# { |
| 142 | xrust-analyzer cargo gnome-robots 50.0.0 arena/Position#x.: index as u32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Rem<Self>`]rem().self.widthrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#width., |
| 143 | yrust-analyzer cargo gnome-robots 50.0.0 arena/Position#y.: index as u32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Div<Self>`]div().self.widthrust-analyzer cargo gnome-robots 50.0.0 arena/Arena#width., |
| 144 | }); |
| 145 | } |
| 146 | } |
| 147 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 148 | } |
| 149 | } |