Selecting regions and cut/copy/paste are basic features in the Lode Runner Online: The Mad Monks' Revenge puzzle editor and thanks to a complete rewrite, they just got better!
Are you a dedicated puzzle maker who spends hours tinkering away? Do you like to make symmetrical puzzles? Say hello to the latest editor feature: flip horizontally and vertically. Just select a region and press CTRL+< to flip horizontally or CTRL+> for vertical flipping, that's it. In the video below, I mirror the missing half of the puzzle in 10 seconds compared to 2 minutes and 30 seconds odd by manually painting it. It would have saved more time too if I didn't have three practice attempts to get a nicer looking video.
*Comparison of creating a symmetrical puzzle*As of September 2016 buttons to toggle flipping are now available on the toolbox.
Flip toggle now on the toolbox
Why the the rewrite?
For the rest of this article SCCP means 'select, cut, copy and paste' to save on typing.
If you've read one of the first posts in this blog, you may remember that after working out the resources, I started with the editor. I left what I thought was the two most complex features until last: SCCP and undo. The original SCCP code remained untouched for nearly a year. It worked, wasn't particularly poor or slow, a little complex, but overall fine... so why touch it?
I noticed three things that needed fixing; 1) that when you select a region, then press delete without moving it nothing happens - it should be deleted like you'd expect it to be, 2) selecting a region cut then pasted the tiles under it - the same as the original - which looks very odd and 3) if copying an area of puzzle from one world and pasting it to a puzzle with another (or changing the theme after you copied it) you got this:
Grass coated in anti-freeze
I think you be surprised that behind-the-scenes things are not as simple as you'd first think. There are a lot of things going on. After not really looking at the code for a year it proved to be a little too complex to make quite a few large-ish changes. After fixing issue one only and partway into looking into issue 3 I made a note to reduce its complexity if I ever needed to work on SCCP in the future. Ten minutes later and still looking over issue three I had the idea to add flip horizontal support like MS Paint. I stopped work and started a full rewrite.
Thanks to the newly rewritten SCCP code;
- much better performance
- no longer cuts/pastes the area under a selection (which the original suffers from too)
- far less code + easier to maintain
- selecting a region and then clicking 'copy' no longer clears the selection (same as the original and MS Paint)
- you can now select an area and press delete on the keyboard without having to start dragging first (same as MS Paint)
- copying areas of a puzzle in a particular world and pasting to another world no longer results in odd visuals
- selecting a region, moving it and clicking undo will now clear it and put it back in one undo instead of the previous two
- horizontal flip support added
How does selecting and dragging a region work?
Once the user has drawn a region/box, you build a list of all the tiles within that area. I store the coordinates starting from zero and the tile data (what it is, any properties it has, etc.) Tile data can also store more than one item per coordinate (think of buried items). This information is then used to create a texture. The texture is what is moved around the screen and any tiles under it are not drawn (to enable you to drag empty tiles).
I know the above is not very in depth but you get the idea.
Flipping horizontally
Flipping a selected region horizontally is pretty straight-forward. All you need to do is swap the X-coordinates e.g. the left becomes the right, the second from the left becomes the second from the right, etc. This StackOverflow answer gives the basic idea of what to do in C#.
That is essentially it - you now have the selected items flipped horizontally. The problem is now visualizing it. Below are four regions; the top one is what was selected and the rest are my three attempts to display it.
Attempt 1: after flipping the coordinates and rebuilding the texture, this is what I ended up with. Naturally the block edge seaming is well off as the code that creates the texture is using the exact block variation it was copied as.
Attempt 2: a thought struck me "why not just flip the texture?" As you can see, it was silly thought by me trying to convince myself I didn't really need to edge all the blocks again.
Attempt 3: as you can see, this attempt worked fine. I had to recalculate all the edges for the blocks. Unfortunately I couldn't just recycle the existing code that the editor uses and the game (when blasting or exploding things) as these functions work on the puzzle itself. Remember, the selected items are just a dictionary of data that is not a part of the puzzle, in a different format and with a different coordinate system.
Flipping vertically
Nothing to see here. Once horizontal worked, vertical was the same, the only thing done differently is flipping the Y-coordinate instead of the X. Both share the same method as only one line of code is different. I'm not too sure how useful flipping vertically is but other puzzle makers may find it very handy.
Conclusions
Overall, I am extremely pleased with the upgrades to the SCCP+flip system. Flipping is just under 150 lines of code which is very good when you consider all the tiles selected need to be checked if it can edge and then choose another edge if it can.
I think this is a nice little feature that will be very useful for puzzle makers yet doesn't detract from the original game.