Saving randomly generated dungeons

0 favourites
  • 10 posts
From the Asset Store
Match the similar cards with each other and free all animals!
  • I have been using arrays to save relevant room data for my generated dungeons. I had a thought though, and wanted to run it by folks who might know C2 under the hood a little better than I.

    What if I just moved all dungeon objects outside of the layout when not using that level, then moved them back when returning to that level? Can you shift the view window or layout area around areas outside of the original layout position, or does that not make sense because locations in C2 are defined by the layout? What about making a HUGE layout and we shift the view window? Will mobs, etc still be active when they are outside the layout? I am trying to figure out the ramifications of doing things this way instead of saving them in arrays. Ultimately, the level data has to get stored in some form so that it's all preserved when C2 makes a savegame.

    Anybody got any thoughts on this?


  • Saving in an array is a valid solution here. Don't go making a huge level and putting everything inside, you'll most likely diminish the performances, depending on how you deal with the items outside of layout. About this subject, it is you that decides how the items outside a layout should behave. In a case like this one (having a large layout and enemies everywhere on the map), the best you can do (of my knowledge) is "unload" the enemies outside a "game box", a large sprite, larger than the screen by something like 30%. Loading/unloading something is roughly disabling its behaviors/collisions or reactivating those, so a useless object doesn't weight too much in the game logic. You won't have problem with the display performance (something outside of view isn't drawn) but the game logic won't be happy if you have thousands of objects at the same time, all active.

    Back to your case now. If you can store a level state in an array, then do it. You'll benefit from the built-in save functions, and can unload all objects from a not-currently-useful level.

  • That makes sense. Thanks, this game is intended for mobile so performance is a big concern.

  • Yttermayn, would you mind telling me a bit about how you're randomly generating dungeons in C2? It's something I've been trying to figure out for a while now without much luck, and I'd love to know more! If you have any example .capx or anything that would be super <img src="smileys/smiley4.gif" border="0" align="middle" />

  • lolpaca, I struggled with it too. I decided to abandon the tile-based design (which C2 didn't natively support, per se, at the time that I started writing it.) Instead, I used C2's strengths: object oriented design.

    With that in mind, I wrote my random number generator that uses a seed, instead of using C2's. This lets me generate the same random world, like minecraft.

    I decided to start off making cave-like dungeons: irregularly shaped rooms and tunnels, all connected to each other somehow. I made a sprite object called cave rooms that has several frames of animation, each frame irregularly shaped. I adjusted the collision boxes to roughly fit the shapes of each frame, yet use no more than 8 sides as is recommended.

    Now the generation: You run a loop that creates room sprites, randomly assigns them X and Y, frame number, rotation, and even flipped or not for maximum variability. You make them start moving around randomly, within a boundary. You make one of them stop moving. This one room is "infected" like in the evil sprite demo. Any room that bumps into it stops and becomes infected also. This continues until all rooms are infected, and stopped. Doing it this way ensures that all rooms are connected somehow, no stragglers.

    Doing man made looking dungeons would work similarly, but use rectangular rooms and hallways moving in cardinal directions instead of all angles.

    For how to keep the player confined to the room sprites, see my post:

  • Yttermayn, thanks for taking the time to explain that. It sounds a little bit like a method I was looking at(flipping & rotating sprites, at least) before I got sidetracked with arrays and miners, which I also didn't get too far with because I'm a bit of a noob with those things.

    Your method sounds really interesting, that's a clever way of making sure everything ends up connected. I wonder I might be able to place the rooms randomly without the initial connections, then generate doors at random points of the walls of each room after they've been placed. Then use 4-directional pathfinding nodes (starting at one random door, ending at another) as 'miners' to dig out natural-looking corridors connecting them. The corridors should then naturally criss-cross and end up with some interesting level designs.

    I might not be describing it too well but I'll have a play around with that later - anyway, thanks a lot, you've given me a lot to think about! <img src="smileys/smiley4.gif" border="0" align="middle" />

  • Yttermayn thanks too. I learned a lot from what you explained. By the way, did you make a seed based random as a plugin, or using C2s functions ?

    Anyway, I looked into randomly generated dungeons some time before, but you explained it a lot better :)

  • Guizmus lolpaca You are welcome. Here is the function I wrote for the seeded random part. It's short and sweet and fast, though math eggheads cringe to call it's output 'random'. It is an old design adapted for use in C2. I claim no credit for it's design.

    Seeded Random Function capx

  • lolpaca You described you design idea well, I understand it. It sound like it would work, but I am not sure what the miners would dig out- a series of objects overlapping as the miner moves from one door to another? That's the only part I hung up on. When I make my man made dungeon generator function, I was thinking of pinning door objects to the ends of random sized hallway objects that get mixed in with regular rooms and bounce around with them, but the only way a hallway gets infected from a room or vice-versa is when a hallway's door collides, not hallway object itself. I don't know if that will work out yet or not, I'm betting there will be some complications that will have to be sorted out.

    I like your idea, frankly, and may try to implement it instead. Maybe instead of miners creating a long string of objects (thus eating memory and performance), we have hallways with pinned doors that one end starts at a point on a room and then stretched (play with scale in one axis) until the door pinned to the other end touches another room?

    Let me know what you think, and/or clarify your miners functionality?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I'm still using arrays, so I might be coming at a slightly different angle to you, although I reckon it can be done more or less the same way with sprites.

    So, once you've spawned your doors, you somehow pick two doors to link together. Set the walls of your rooms to solids, spawn a sprite with pathfinding (diagonals off) at the first door and plot a path to the second, using a pseudo-movement for the sprite (stepping one tile at a time, but every tick so it's as fast as possible) to go from node to node in order, leaving behind a trail of corridors as it goes.

    Alternatively, you could use the stretch function to draw lines from one node to the next, then generate corridors on every tile where there's a line (come to think of it, that's probably a much quicker way to do it...) Repeating this for a bunch of doors should give you a nice network of corridors with lots of branching paths.

    If anyone would like to help a brother out, there's a .capx below showing what I've been working on tonight. As you can see, I'm auto-generating rooms of random length and width just using a single map array, but I can't figure out a way to stop them overlapping. I tried making a function to test if the cells at Map.PositionX and Y + Roomsize X and Y were anything other than zero, but that didn't seem to work - I'm quite new to arrays though so I probably got something wrong.

    Ideally, I'd like each room to be spaced at least one tile apart (walls included) so they can always be connected by corridors - if anyone can tell me how to do this, I'd be really grateful!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)