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
//! Port of BSP toolkit.

use bindings::ffi;
use bindings::AsNative;
use bindings::{c_void, c_bool};
use random::Rng;
use std::ops::{Deref, DerefMut};
use std::fmt;
use std::mem;

#[derive(Copy, Clone, Debug)]
pub enum TraverseOrder {
    PreOrder,
    InOrder,
    PostOrder,
    LevelOrder,
    InvertedLevelOrder,
}

/// This struct encapsulates `TCOD_bsp_t`. It mirrors original's fields (`x`, `y`, etc.)
/// with the exception of `horizontal`. See example.
///
/// # Examples
///
/// ```no_run
/// # use tcod::bsp::*;
/// let mut bsp = Bsp::new_with_size(0, 0, 50, 60);
///
/// assert_eq!(bsp.x, 0);
/// assert_eq!(bsp.y, 0);
/// assert_eq!(bsp.w, 50);
/// assert_eq!(bsp.h, 60);
/// assert_eq!(bsp.horizontal(), false);
///
/// bsp.x = 10;
/// bsp.y = 20;
/// bsp.set_horizontal(true);
/// ```
pub struct Bsp<'a> {
    bsp: &'a mut ffi::TCOD_bsp_t,
    root: bool
}

impl<'a> Deref for Bsp<'a> {
    type Target = ffi::TCOD_bsp_t;

    fn deref(&self) -> &Self::Target {
        self.bsp
    }
}

impl<'a> DerefMut for Bsp<'a> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.bsp
    }
}

impl<'a> fmt::Debug for Bsp<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(f, "Bsp{{x: {}, y:{}, w: {}, h: {}, position: {}, level: {}, horizontal: {} }}",
               self.x, self.y, self.w, self.h, self.position, self.level, self.horizontal)
    }
}


extern "C" fn callback_wrapper<T>(node: *mut ffi::TCOD_bsp_t, user_data: *mut c_void) -> c_bool
    where T: FnMut(&mut Bsp) -> bool
{
    let callback_ptr = user_data as *mut T;
    let cb: &mut T = unsafe {
        &mut *callback_ptr
    };
    if node.is_null() { panic!("Null node when traversing a BSP.") }
    let mut bsp = Bsp { bsp: unsafe { &mut *node }, root: false };
    cb(&mut bsp) as c_bool
}

impl<'a> Bsp<'a> {
    pub fn new_with_size(x: i32, y: i32, w: i32, h: i32) -> Self {
        let bsp = unsafe {
            let pointer = ffi::TCOD_bsp_new_with_size(x, y, w, h);
            if pointer.is_null() {
                panic!("TCOD_bsp_new_with_size returned a NULL BSP.");
            }
            &mut *pointer
        };
        Bsp { bsp: bsp, root: true }
    }

    pub fn remove_sons(&mut self) {
        unsafe { ffi::TCOD_bsp_remove_sons(self.bsp as *mut ffi::TCOD_bsp_t) }
    }

    pub fn split_once(&mut self, horizontal: bool, position: i32) {
        unsafe { ffi::TCOD_bsp_split_once(self.bsp as *mut ffi::TCOD_bsp_t,
                                          horizontal as u8,
                                          position) }
    }

    pub fn split_recursive(&mut self,
                           randomizer: Option<Rng>,
                           nb: i32,
                           min_h_size: i32,
                           min_v_size: i32,
                           max_h_ratio: f32,
                           max_v_ratio: f32) {
        let rnd = randomizer.unwrap_or(Rng::get_instance());
        unsafe {
            ffi::TCOD_bsp_split_recursive(self.bsp as *mut ffi::TCOD_bsp_t,
                                          *rnd.as_native(),
                                          nb,
                                          min_h_size,
                                          min_v_size,
                                          max_h_ratio,
                                          max_v_ratio)
        }
    }

    pub fn resize(&mut self, x: i32, y: i32, w: i32, h: i32) {
        unsafe { ffi::TCOD_bsp_resize(self.bsp as *mut ffi::TCOD_bsp_t, x, y, w, h) }
    }

    /// Returns `Some(Bsp)` with left subtree, or `None` if the BSP has not been split.
    pub fn left(&self) -> Option<Self> {
        unsafe {
            let left = ffi::TCOD_bsp_left(self.bsp as *const ffi::TCOD_bsp_t);
            if left.is_null() {
                None
            } else {
                Some(Bsp {
                    bsp: &mut *left,
                    root: false
                })
            }
        }
    }

    /// Returns `Some(Bsp)` with right subtree, or `None` if the BSP has not been split.
    pub fn right(&self) -> Option<Self> {
        unsafe {
            let right = ffi::TCOD_bsp_right(self.bsp as *const ffi::TCOD_bsp_t) ;
            if right.is_null() {
                None
            } else {
                Some(Bsp {
                    bsp: &mut *right,
                    root:false
                })
            }
        }
    }

    /// Returns `Some(Bsp)` with father, or `None` if the node is root.
    pub fn father(&self) -> Option<Self> {
        unsafe {
            let father = ffi::TCOD_bsp_father(self.bsp as *const ffi::TCOD_bsp_t);
            if father.is_null() {
                None
            } else {
                Some(Bsp {
                    bsp: &mut *father,
                    root: false,
                })
            }
        }
    }

    pub fn is_leaf(&self) -> bool {
        unsafe { ffi::TCOD_bsp_is_leaf(self.bsp as *const ffi::TCOD_bsp_t) != 0 }
    }

    pub fn contains(&self, cx: i32, cy: i32) -> bool {
        unsafe { ffi::TCOD_bsp_contains(self.bsp as *const ffi::TCOD_bsp_t, cx, cy) != 0 }
    }

    pub fn find_node(&self, cx: i32, cy: i32) -> Option<Self> {
        unsafe {
            let pointer = ffi::TCOD_bsp_find_node(self.bsp as *const ffi::TCOD_bsp_t,
                                                  cx, cy);
            if pointer.is_null() {
                None
            } else {
                Some(Bsp {
                    bsp: &mut *pointer,
                    root: false
                })
            }
        }
    }

    pub fn horizontal(&self) -> bool {
        self.horizontal != 0
    }

    pub fn set_horizontal(&mut self, h: bool) {
        self.horizontal = h as u8;
    }

    /// Instead of 5 `traverse*` functions as in original API, Rust binding
    /// provides a single `traverse` function with an `order` parameter.
    ///
    /// # Examples
    ///
    /// ```no_run
    ///    # use tcod::bsp::*;
    ///    let bsp = Bsp::new_with_size(0, 0, 50, 50);
    ///    let mut counter = 0;
    ///
    ///    bsp.traverse(TraverseOrder::PreOrder, |node| {
    ///        counter += 1;
    ///        true
    ///    });
    ///    assert_eq!(counter, 1);
    /// ```
    pub fn traverse<F>(&self, order: TraverseOrder, mut callback: F) -> bool
        where F: FnMut(&mut Bsp) -> bool
    {
        let mut cb: &mut FnMut(&mut Bsp) -> bool = &mut callback;
        let retval = unsafe {
            let bsp = mem::transmute(self.bsp as *const ffi::TCOD_bsp_t);
            match order {
                TraverseOrder::PreOrder =>
                    ffi::TCOD_bsp_traverse_pre_order(bsp,
                                                     Some(callback_wrapper::<F>),
                                                     cb as *mut _ as *mut c_void),
                TraverseOrder::InOrder =>
                    ffi::TCOD_bsp_traverse_in_order(bsp,
                                                    Some(callback_wrapper::<F>),
                                                    cb as *mut _ as *mut c_void),
                TraverseOrder::PostOrder =>
                    ffi::TCOD_bsp_traverse_post_order(bsp,
                                                      Some(callback_wrapper::<F>),
                                                      cb as *mut _ as *mut c_void),
                TraverseOrder::LevelOrder =>
                    ffi::TCOD_bsp_traverse_level_order(bsp,
                                                       Some(callback_wrapper::<F>),
                                                       cb as *mut _ as *mut c_void),
                TraverseOrder::InvertedLevelOrder =>
                    ffi::TCOD_bsp_traverse_inverted_level_order(bsp,
                                                                Some(callback_wrapper::<F>),
                                                                cb as *mut _ as *mut c_void),
            }
        };
        retval != 0
    }
}

impl<'a> Drop for Bsp<'a> {
    fn drop(&mut self) {
        if self.root {
            unsafe { ffi::TCOD_bsp_delete(self.bsp as *mut ffi::TCOD_bsp_t) }
        }
    }
}

#[cfg(test)]
mod test {
    use super::Bsp;
    use super::TraverseOrder;
    use bindings::ffi;

    #[test]
    #[allow(unused_variables)]
    fn created_destroyed_no_panic() {
        let bsp = Bsp::new_with_size(0, 0, 50, 50);
        let left = bsp.left(); // left has null .bsp
    }

    #[test]
    fn accessors() {
        let mut bsp = Bsp::new_with_size(0, 0, 50, 60);

        assert_eq!(bsp.x, 0);
        assert_eq!(bsp.y, 0);
        assert_eq!(bsp.w, 50);
        assert_eq!(bsp.h, 60);
        assert_eq!(bsp.horizontal(), false);
        bsp.x = 10;
        bsp.y = 20;
        bsp.set_horizontal(true);
        assert_eq!(bsp.x, 10);
        assert_eq!(bsp.y, 20);
        assert_eq!(bsp.horizontal(), true);
    }

    #[test]
    fn split() {
        let mut bsp = Bsp::new_with_size(0, 0, 50, 50);

        assert_eq!(bsp.position, 0);
        assert_eq!(bsp.horizontal(), false);

        bsp.split_once(true, 20);
        assert_eq!(bsp.position, 20);
        assert_eq!(bsp.horizontal(), true);
    }

    #[test]
    fn split_recursive() {
        let mut bsp = Bsp::new_with_size(0, 0, 100,100);
        let mut counter = 0;

        bsp.split_recursive(None, 2, 5, 5, 1.5, 1.5);
        bsp.traverse(TraverseOrder::PreOrder, |node| {
            assert!(node.h >= 5);
            assert!(node.w >= 5);
            counter += 1;
            true
        });
        assert_eq!(counter, 7);
    }

    #[test]
    fn children() {
        let mut bsp = Bsp::new_with_size(0, 0, 50, 50);

        assert!(bsp.left().is_none());
        assert_eq!(bsp.level, 0);

        bsp.split_once(false, 20);
        assert!(bsp.left().is_some());
        assert!(bsp.right().is_some());
        assert_eq!(bsp.left().unwrap().level, 1);
        assert_eq!(bsp.right().unwrap().level, 1);
    }

    #[test]
    fn father() {
        let mut bsp = Bsp::new_with_size(0, 0, 50, 50);
        assert!(bsp.father().is_none());

        bsp.split_once(false, 30);
        assert!(bsp.left().unwrap().father().is_some());
        assert!(bsp.right().unwrap().father().is_some());
    }

    #[test]
    fn traverse() {
        let mut bsp = Bsp::new_with_size(0, 0, 50, 50);
        let mut counter = 0;

        bsp.traverse(TraverseOrder::PreOrder, |node| {
            assert!(cmp(node, &bsp));
            counter += 1;
            true
        });
        assert_eq!(counter, 1);

        counter = 0;
        bsp.split_once(true, 25);
        bsp.traverse(TraverseOrder::PreOrder, |_node| {
            counter += 1;
            true
        });
        assert_eq!(counter, 3);
    }

    fn cmp(a: &ffi::TCOD_bsp_t, b: &ffi::TCOD_bsp_t) -> bool {
        a.x == b.x
            && a.y == b.y
            && a.w == b.w
            && a.h == b.h
            && a.position == b.position
            && a.level == b.level
            && a.horizontal == b.horizontal
    }

    #[test]
    fn traverse_orders() {
        let mut root = Bsp::new_with_size(0, 0, 100,100);
        let mut counter = 0;

        root.split_recursive(None, 2, 5, 5, 1.5, 1.5);

        let middle_left = root.left().unwrap();
        let middle_right = root.right().unwrap();
        let leaf1 = middle_left.left().unwrap();
        let leaf2 = middle_left.right().unwrap();
        let leaf3 = middle_right.left().unwrap();
        let leaf4 = middle_right.right().unwrap();

        root.traverse(TraverseOrder::PreOrder, |node| {
            match counter {
                0 => assert!(cmp(node.bsp, root.bsp)),
                1 => assert!(cmp(node.bsp, middle_left.bsp)),
                2 => assert!(cmp(node.bsp, leaf1.bsp)),
                3 => assert!(cmp(node.bsp, leaf2.bsp)),
                4 => assert!(cmp(node.bsp, middle_right.bsp)),
                5 => assert!(cmp(node.bsp, leaf3.bsp)),
                6 => assert!(cmp(node.bsp, leaf4.bsp)),
                _ => panic!("Wrong number of nodes in the tree"),
            };
            counter += 1;
            true
        });

        counter = 0;
        root.traverse(TraverseOrder::InOrder, |node| {
            match counter {
                0 => assert!(cmp(node.bsp, leaf1.bsp)),
                1 => assert!(cmp(node.bsp, middle_left.bsp)),
                2 => assert!(cmp(node.bsp, leaf2.bsp)),
                3 => assert!(cmp(node.bsp, root.bsp)),
                4 => assert!(cmp(node.bsp, leaf3.bsp)),
                5 => assert!(cmp(node.bsp, middle_right.bsp)),
                6 => assert!(cmp(node.bsp, leaf4.bsp)),
                _ => panic!("Wrong number of nodes in the tree"),
            };
            counter += 1;
            true
        });

        counter = 0;
        root.traverse(TraverseOrder::PostOrder, |node| {
            match counter {
                0 => assert!(cmp(node.bsp, leaf1.bsp)),
                1 => assert!(cmp(node.bsp, leaf2.bsp)),
                2 => assert!(cmp(node.bsp, middle_left.bsp)),
                3 => assert!(cmp(node.bsp, leaf3.bsp)),
                4 => assert!(cmp(node.bsp, leaf4.bsp)),
                5 => assert!(cmp(node.bsp, middle_right.bsp)),
                6 => assert!(cmp(node.bsp, root.bsp)),
                _ => panic!("Wrong number of nodes in the tree"),
            };
            counter += 1;
            true
        });

        counter = 0;
        root.traverse(TraverseOrder::LevelOrder, |node| {
            match counter {
                0 => assert!(cmp(node.bsp, root.bsp)),
                1 => assert!(cmp(node.bsp, middle_left.bsp)),
                2 => assert!(cmp(node.bsp, middle_right.bsp)),
                3 => assert!(cmp(node.bsp, leaf1.bsp)),
                4 => assert!(cmp(node.bsp, leaf2.bsp)),
                5 => assert!(cmp(node.bsp, leaf3.bsp)),
                6 => assert!(cmp(node.bsp, leaf4.bsp)),
                _ => panic!("Wrong number of nodes in the tree"),
            };
            counter += 1;
            true
        });

        counter = 0;
        root.traverse(TraverseOrder::InvertedLevelOrder, |node| {
            match counter {
                0 => assert!(cmp(node.bsp, leaf4.bsp)),
                1 => assert!(cmp(node.bsp, leaf3.bsp)),
                2 => assert!(cmp(node.bsp, leaf2.bsp)),
                3 => assert!(cmp(node.bsp, leaf1.bsp)),
                4 => assert!(cmp(node.bsp, middle_right.bsp)),
                5 => assert!(cmp(node.bsp, middle_left.bsp)),
                6 => assert!(cmp(node.bsp, root.bsp)),
                _ => panic!("Wrong number of nodes in the tree"),
            };
            counter += 1;
            true
        });
    }

    #[test]
    fn break_traverse() {
        let mut bsp = Bsp::new_with_size(0, 0, 100,100);
        let mut counter = 0;

        bsp.split_recursive(None, 2, 5, 5, 1.5, 1.5);
        bsp.traverse(TraverseOrder::PreOrder, |_node| {
            counter += 1;
            false
        });
        assert_eq!(counter, 1);
    }

    #[test]
    fn safe_tree_pointer() {
        let mut bsp1 = Bsp::new_with_size(0, 0, 100,100);
        bsp1.split_recursive(None, 2, 5, 5, 1.5, 1.5);
        let mut bsp2 = Bsp::new_with_size(0, 0, 100,100);
        bsp2.split_recursive(None, 2, 5, 5, 1.5, 1.5);

        unsafe {
            assert!(bsp1.left().unwrap().tree().father != bsp2.left().unwrap().tree().father);
            assert!(bsp1.left().unwrap().tree().father == bsp1.right().unwrap().tree().father);
        }
    }
}