@@ -21,27 +21,24 @@ fn random_room_gen(width_as_str: &str,
2121 )
2222 -> Result < String > {
2323 let default_hash: u64 = rand:: thread_rng ( ) . gen ( ) ;
24- let width = width_as_str. parse :: < i32 > ( ) ?;
25- let height = height_as_str. parse :: < i32 > ( ) ?;
26- let desired_room_count = desired_room_count_as_str. parse :: < i32 > ( ) ?;
27-
28- //let seed: &str = Alphanumeric.sample_string(&mut rand::thread_rng(), 32).as_str();
24+ let width = width_as_str. parse :: < usize > ( ) ?;
25+ let height = height_as_str. parse :: < usize > ( ) ?;
26+ let desired_room_count = desired_room_count_as_str. parse :: < usize > ( ) ?;
2927
3028 let mut rng: StdRng = SeedableRng :: seed_from_u64 ( hash_as_str. parse :: < usize > ( ) ?. try_into ( ) . unwrap_or ( default_hash) ) ;
3129
3230
3331 let level = RandomRoomLevel :: new ( width, height, desired_room_count, & mut rng) ;
3432
35- Ok ( serde_json:: to_string ( & level. all_rooms ) ?)
33+ Ok ( serde_json:: to_string ( & level. rooms ) ?)
3634}
3735
3836impl RandomRoomLevel {
3937 fn new (
40- width : i32 ,
41- height : i32 ,
42- desired_room_count : i32 ,
38+ width : usize ,
39+ height : usize ,
40+ desired_room_count : usize ,
4341 rng : & mut StdRng ,
44-
4542 ) -> Level {
4643 let level = Level :: new ( width, height) ;
4744
@@ -51,11 +48,11 @@ impl RandomRoomLevel {
5148 map. level
5249 }
5350
54- fn place_rooms_random ( & mut self , desired_room_count : i32 , rng : & mut StdRng ) {
51+ fn place_rooms_random ( & mut self , desired_room_count : usize , rng : & mut StdRng ) {
5552 let max_rooms = desired_room_count as usize ;
5653 let max_attempts = 15 ;
5754 let mut attempts = 0 ;
58- while self . level . all_rooms . iter ( ) . filter ( | & rm| rm . room_type == 3 ) . count ( ) <= max_rooms && attempts <= max_attempts {
55+ while self . level . rooms . len ( ) <= max_rooms && attempts <= max_attempts {
5956 attempts += 1 ;
6057 let mut x = rng. gen_range ( 0 ..self . level . width ) ;
6158 let mut y = rng. gen_range ( 0 ..self . level . height ) ;
@@ -84,9 +81,9 @@ impl RandomRoomLevel {
8481 }
8582
8683 let mut collides = false ;
87- let room = Room :: new ( format ! ( "ruin room: {}" , self . level. all_rooms . iter ( ) . filter ( | & rm| rm . room_type == 3 ) . count ( ) ) , x, y, width, height, 3 ) ;
84+ let room = Room :: new ( format ! ( "ruin room: {}" , self . level. rooms . len ( ) ) , x, y, width, height) ;
8885
89- for other_room in & self . level . all_rooms {
86+ for other_room in & self . level . rooms {
9087 if room. intersects ( & other_room) {
9188 collides = true ;
9289 break ;
@@ -102,79 +99,70 @@ impl RandomRoomLevel {
10299}
103100
104101pub struct Level {
105- width : i32 ,
106- height : i32 ,
107- board : Vec < Vec < i32 > > ,
108- all_rooms : Vec < Room > ,
109- increment : i32 ,
110- //hash: String,
102+ width : usize ,
103+ height : usize ,
104+ board : Vec < Vec < usize > > ,
105+ rooms : Vec < Room > ,
106+ increment : usize ,
107+ }
108+
109+ #[ derive( Debug , Clone , Copy ) ]
110+ pub enum TileType {
111+ Space = 0 ,
112+ Floor = 1 ,
113+ Wall = 2 ,
111114}
112115
113116impl Level {
114117 fn new (
115- width : i32 ,
116- height : i32 ,
118+ width : usize ,
119+ height : usize ,
117120 ) -> Self {
118121 let mut new_level = Level {
119122 width,
120123 height,
121124 board : Vec :: new ( ) ,
122- all_rooms : Vec :: new ( ) ,
125+ rooms : Vec :: new ( ) ,
123126 increment : 0 ,
124127 } ;
125128 new_level. update_board ( ) ;
126129 new_level
127130 }
128131
129- fn update_board ( & mut self ) -> Vec < Vec < i32 > > {
132+ fn update_board ( & mut self ) -> Vec < Vec < usize > > {
130133 let mut new_board = Vec :: new ( ) ;
131134 self . increment +=1 ;
132135 for _ in 0 ..self . height {
133- let space_tile = 0 ;
134- //let wall_tile = 1;
135- let floor_tile = 5 ;
136136 let gen_floor_first = true ;
137137
138- let mut row = vec ! [ floor_tile ; self . width as usize ] ;
138+ let mut row = vec ! [ TileType :: Floor as usize ; self . width as usize ] ;
139139 if !gen_floor_first {
140- row = vec ! [ space_tile ; self . width as usize ] ;
140+ row = vec ! [ TileType :: Space as usize ; self . width as usize ] ;
141141 }
142- // if gen_floor_first {
143- // if index == 0 || index == self.height - 1 {
144- // row = vec![wall_tile; self.width as usize];
145- // }
146142
147- // row[0] = wall_tile;
148- // row[self.width as usize - 1] = wall_tile;
149- // }
150143
151144 new_board. push ( row) ;
152145 }
153- for room in & self . all_rooms {
146+ for room in & self . rooms {
154147 for row in 0 ..room. height {
155148 for col in 0 ..room. width {
156149 let y = ( room. y + row) as usize ;
157150 let x = ( room. x + col) as usize ;
158151 if row == 0 || col == 0 || row == room. height - 1 || col == room. width - 1 {
159152 // might just let byond handle the walls
160- new_board[ y] [ x] = 1 ;
153+ new_board[ y] [ x] = TileType :: Wall as usize ;
161154 } else {
162- new_board[ y] [ x] = room . room_type ;
155+ new_board[ y] [ x] = TileType :: Floor as usize ;
163156 }
164157 }
165158 }
166159 }
167160 self . board = new_board. clone ( ) ;
168- //draw(self, "increments", &self.increment.to_string()).unwrap();
169161 new_board
170162 }
171163
172164 fn add_room ( & mut self , room : & Room ) {
173- // match room.room_type {
174- // 2 => self.mandatory_rooms.push(room.clone()),
175- // _ => self.rooms.push(room.clone()),
176- // }
177- self . all_rooms . push ( room. clone ( ) ) ;
165+ self . rooms . push ( room. clone ( ) ) ;
178166 self . update_board ( ) ;
179167
180168 }
@@ -187,7 +175,6 @@ impl fmt::Display for Level {
187175 for col in 0 ..self . width as usize {
188176 write ! ( f, "{}" , self . board[ row] [ col] ) ?
189177 }
190- // write!(f, "\n")?
191178 }
192179
193180 Ok ( ( ) )
@@ -205,54 +192,49 @@ pub enum RoomDimensions {
205192 Maint10x10 ,
206193}
207194impl RoomDimensions {
208- fn get_height ( & self ) -> i32 {
209- let height: i32 ;
210- match * self {
211- RoomDimensions :: Maint3x3 => height = 3 ,
212- RoomDimensions :: Maint3x5 => height = 5 ,
213- RoomDimensions :: Maint5x3 => height = 3 ,
214- RoomDimensions :: Maint5x4 => height = 4 ,
215- RoomDimensions :: Maint10x5 => height = 5 ,
216- RoomDimensions :: Maint10x10 => height = 10 ,
217- }
218- return height + 2
195+ fn get_height ( & self ) -> usize {
196+ return match * self {
197+ RoomDimensions :: Maint3x3 => 3 ,
198+ RoomDimensions :: Maint3x5 => 5 ,
199+ RoomDimensions :: Maint5x3 => 3 ,
200+ RoomDimensions :: Maint5x4 => 4 ,
201+ RoomDimensions :: Maint10x5 => 5 ,
202+ RoomDimensions :: Maint10x10 => 10 ,
203+ } + 2 //add 2 because the dimensions are equal to the inside of the room, and we need the dimensions with the walls in mind
219204 }
220205
221- fn get_width ( & self ) -> i32 {
222- let width: i32 ;
223- match * self {
224- RoomDimensions :: Maint3x3 => width = 3 ,
225- RoomDimensions :: Maint3x5 => width = 3 ,
226- RoomDimensions :: Maint5x3 => width = 5 ,
227- RoomDimensions :: Maint5x4 => width = 5 ,
228- RoomDimensions :: Maint10x5 => width = 10 ,
229- RoomDimensions :: Maint10x10 => width = 10 ,
230- }
231- return width + 2 ;
206+ fn get_width ( & self ) -> usize {
207+ return match * self {
208+ RoomDimensions :: Maint3x3 => 3 ,
209+ RoomDimensions :: Maint3x5 => 3 ,
210+ RoomDimensions :: Maint5x3 => 5 ,
211+ RoomDimensions :: Maint5x4 => 5 ,
212+ RoomDimensions :: Maint10x5 => 10 ,
213+ RoomDimensions :: Maint10x10 => 10 ,
214+ } + 2 ; //add 2 because the dimensions are equal to the inside of the room, and we need the dimensions with the walls in mind
232215 }
233216}
234217
235218#[ derive( Debug , Clone , Copy , Eq , Ord , PartialEq , PartialOrd , Serialize ) ]
236219pub struct Point {
237- x : i32 ,
238- y : i32 ,
220+ x : usize ,
221+ y : usize ,
239222}
240223
241224#[ derive( Clone , Debug , Eq , Ord , PartialEq , PartialOrd , Serialize ) ]
242225pub struct Room {
243226 id : String ,
244- x : i32 ,
245- y : i32 ,
246- x2 : i32 ,
247- y2 : i32 ,
248- width : i32 ,
249- height : i32 ,
227+ x : usize ,
228+ y : usize ,
229+ x2 : usize ,
230+ y2 : usize ,
231+ width : usize ,
232+ height : usize ,
250233 center : Point ,
251- room_type : i32 ,
252234}
253235
254236impl Room {
255- pub fn new ( id : String , x : i32 , y : i32 , width : i32 , height : i32 , room_type : i32 ) -> Self {
237+ pub fn new ( id : String , x : usize , y : usize , width : usize , height : usize ) -> Self {
256238 Room {
257239 id,
258240 x,
@@ -265,14 +247,13 @@ impl Room {
265247 x : x + ( width / 2 ) ,
266248 y : y + ( height / 2 ) ,
267249 } ,
268- room_type,
269250 }
270251 }
271252
272253 pub fn intersects ( & self , other : & Self ) -> bool {
273254 self . x <= other. x2 && self . x2 >= other. x && self . y <= other. y2 && self . y2 >= other. y
274255 }
275- pub fn get_distance_to ( & self , other : & Point ) -> i32 {
276- ( ( ( other. x - self . center . x ) . pow ( 2 ) + ( other. y - self . center . y ) . pow ( 2 ) ) as f64 ) . sqrt ( ) as i32
256+ pub fn get_distance_to ( & self , other : & Point ) -> usize {
257+ ( ( ( other. x - self . center . x ) . pow ( 2 ) + ( other. y - self . center . y ) . pow ( 2 ) ) as f64 ) . sqrt ( ) as usize
277258 }
278259}
0 commit comments