3

I'm writing a map generator in Rust which will split the world into sections and then within those sections create rooms in order to connect these rooms. What I would normally do is have a list and then as I iterate through the list get the parent (if there is one) and then run some code to connect these rooms.

I'm struggling with the traversing through a list and getting the parent.

Here are the data structures I currently have:

struct MapSectionPosition {
    x: f32,
    y: f32,
}

struct MapSectionRect {
    top_left: MapSectionPosition,
    top_right: MapSectionPosition,
    bottom_right: MapSectionPosition,
    bottom_left: MapSectionPosition,
}

struct MapSection {
    width: f32,
    height: f32,
    points: MapSectionRect,
}

struct RoomExit {
    position: MapSectionPosition,
}

struct Room {
    width: f32,
    height: f32,
    exit: RoomExit,
}

At the moment I'm using a linked list:

let mut rooms: LinkedList<Room> = LinkedList::new();

let room = Room {
    width: section.width / 4.0,
    height: section.height / 4.0,
    exit: RoomExit {
        position: MapSectionPosition { x: 20.0, y: 20.0 },
    },
};

rooms.push_back(room);

I'm struggling to both iterate and get the parent, so I'm thinking this might not be the right data structure.

With some playing around I've found a hacky solution. Hopefully this will better illustrate what I'm trying to do and if there is something more elegant.

let mut previous_room: Option<&Room> = None;
for room in rooms.iter() {
    match previous_room {
        Some(p_room) => {
            println!("previous {}", p_room.width);
            println!("current {}", room.width);
            println!("connected");
        },
        None => {
            println!("no previous room");
        }
    }

    previous_room = Some(room);
}

Something a little more elegant

If rooms are a slice, then we can use the windows method to create an iterator to access the data in question.

let mut iter = rooms[..].windows(2);
while let Some([prev, next]) = iter.next() {
    println!("prev - {}", prev.width);
    println!("next - {}", next.width);
    println!("done...")
}

This has been answered before in Are there equivalents to slice::chunks/windows for iterators to loop over pairs, triplets etc?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Jake Lacey
  • 623
  • 7
  • 24
  • Does your `RoomExit` have room `field` or not ? And what is the question ? – Ömer Erden May 28 '19 at 16:50
  • > Does your RoomExit have room field or not ? No sorry it does not, when I've implemented it with a linked list. Thanks for spotting that I will fix that. My question is, how can I structure my data in such a way so that I can iterate through and compare adjacent rooms. so that I can figure out how these two adjacent rooms will be joined. – Jake Lacey May 28 '19 at 16:57
  • How do you determine adjacency? Is it literally just two nodes back-to-back? Is your "map" a straight line? A linked list can only be a straight line... – Shepmaster May 28 '19 at 17:08
  • @Shepmaster the adjacency is determined by the order in which the rooms are created. The map is technically a straight line yes, with the first room connecting to the next until we get to the last room. – Jake Lacey May 28 '19 at 18:57
  • I've been having a play around and come up with a hacky solution, hopefully this will better illustrate what I'm trying to do @Shepmaster. Appreciate the edits also. – Jake Lacey May 28 '19 at 19:29
  • 1
    It looks like your question might be answered by the answers of [Are there equivalents to slice::chunks/windows for iterators to loop over pairs, triplets etc?](https://stackoverflow.com/q/42134874/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster May 28 '19 at 19:31
  • 1
    You don't *have* to switch off a `LinkedList` (although I would). You can use `Itertools::tuple_chunks` with `LinkedList::iter`. – Shepmaster May 28 '19 at 20:41
  • I'll have a look into doing so cheers :) – Jake Lacey May 29 '19 at 12:12

0 Answers0