Jump to content


IceMetalPunk

Member Since 12 Mar 2004
Offline Last Active Apr 15 2013 12:49 PM

Topics I've Started

Ensuring A Contiguous Word Exists In A Letter Grid

13 April 2013 - 05:24 PM

I know I haven't been around in a long time, and honestly, I probably will continue to be mostly absent. Work, school, and personal life have been taking top priority.

However! I am working on a game in whatever little bit of free time I can find, and I've run into a bit of a theoretical dilemma.

 

Some background: the game consists of an 18x13 grid of blocks, each with a letter on it. You click-and-drag to form words from contiguous blocks, and if the word you spelled is a real word, those blocks disappear and the ones above fall down on it, making new blocks to fill the top gaps. It's basically a mixture of block-breaking games and word games.

 

I've made a small DLL to handle the dictionary, so don't worry about that. What I do need help with is this: how do I make sure that there's always at least one word that can be formed? As of now, the letters are all randomly chosen, which clearly makes no guarantee that they'll form any words.

Keep in mind that words may be formed in any direction, as long as each letter is touching the letter before it in the grid.


Creating rooms given only relative positions

25 April 2012 - 06:31 PM

I'm practicing for the Jam (in case I actually have time to create something for it!) by tackling arbitrary programming challenges. The one I'm working on now is a type of procedural generation that creates a number of rectangular rooms in a building (all assumed to be the same size, 32x32, for now) and generates the building's layout. It does this by connecting rooms together at their edges, and also adding new floors at random (and connecting the new floors to old ones with stairwells).

The problem I'm having is that the generated buildings aren't correct when I actually place the room instances. They lack stairwells (which should be created whenever a new floor is added) and more rooms are generated than I've actually specified (I run the room addition code 20 times, but get arbitrarily more rooms in the final result).

You'll need a bit of information about the structure of the layout, I suppose.

  • Firstly, each floor is a different value in a map, where the key is the floor number. I figured this would make it more efficient since I'd only need one floor at a time, and finding a floor in a map is O(log n).
  • The floors themselves are stored as ds_lists, with each entry in the list representing either a room or a stairwell.
  • Rooms and stairwells are ds_maps. They have a key called "type" that identifies whether it's a room or a stairwell.
  • Rooms have a key called "floor" that indicates which floor the room is on, and a key called "connections" that is a 4-element ds_list specifying the other rooms and stairwells it's connected to. More details about this below. They also have a list of "invalid" rooms, that is, rooms which it can physically not connect to at any time in the future (though this is irrelevant to the problem at hand).
  • Stairwells also have "floor" and "connections" keys. The "floor" is the lower floor it's connected to. The "connections" is a ds_list with only two elements, which are the room it's connected to on the lower floor and the room it's connected to on the higher floor, respectively.

A little more detail about the "connections" list of a room: a room can only have at most 1 room or stairwell connected to each side, meaning 4 connections maximum. The connections list holds the ds_map ID of the connected rooms to the North, East, South, and West (up, right, down, left) respectively. Where there is no connected room, a -1 is used instead of an ID.

Whew. Now that all that information has been explained, here's the problem: given all that structure, how would I go about drawing the layout of the building? For simplicity, let's just assume I'm only drawing one floor at a time. How can I figure out the relative positions of the rooms given their connections?

I first tried a basic depth-first search, placing a room object whenever a room was popped off the stack. While that showed some nice room layouts, it also showed some problems. Firstly, even though I used repeat(20) to add exactly 20 rooms, I'm somehow showing more rooms than that being generated (in arbitrary numbers; one time I had over 50, another time I had 38). Secondly, stairs don't exist. I know the stairwell creation code is being executed, yet when the room objects are created, they all have type ROOM and none have type STAIRS (even though there are several floors to the generated buildings).

I don't know if the problem is in the way I'm doing my DFS or if it's actually in the underlying data structures, so any help would be nice.

Here's all the code I'm using:

InitRooms() - Initializes variables used for the building generation
Spoiler


AddStairs(lowFloor, highFloor, room) - Adds a stairwell to connect the two floors, always connected to the given room. Returns "false" if there is no place for a stairwell between these floors.
Spoiler


Connect(room, dir) - Attempts to connect the new room "room" to the existing building in direction "dir". Dir indicates which wall to connect; 0=north, 1=east, 2=south, 3=west. Returns false if no rooms on the same floor have a place to connect to it from that side.
Spoiler


Concat(list1, list2) - Concatenates list2's unique values into list1, basically converting list1 into the union of the two lists. This is used as a helper function to build up the "invalid room connections" lists of each new room from the connections of the rooms it's connected to; this prevents physically impossible room layouts
Spoiler


And, the piece de resistance:

AddRoom() - Attempts to add a new room to the existing building. Returns false if that's not possible (i.e. all walls of all existing rooms are occupied; should never happen)
Spoiler


And lastly, I'm displaying the final layouts with this DFS script:

MakeFloor(floor) - Reads the data for the specified floor and places roomObj instances where the rooms are. Also sets the roomObj instances' "mytype" variable to the node's type, either TY_ROOM or TY_STAIRS.
Spoiler


Wow. That's quite a bit of code. Mind you, I'm not getting any explicit errors, it's just an algorithmic logic problem somewhere. I know it's a lot to look through, but...can anyone help? Please?

-IMP

Sorting points in clockwise order

23 March 2012 - 08:20 PM

I started playing around a bit with GMPhysics. One of the things I noticed was that creating polygonal bodies with it only works as long as the vertexes are set in clockwise order. I was hoping to try a system where the user can draw the body himself, but there's no guarantee he'll draw it in that order.

So my question is this: given a ds_list filled with points like {(x0, y0), (x1, y1), ... , (xn, yn)}, how can I sort the points in clockwise order before creating the polygon?

-IMP

Quadratic Regression

06 March 2012 - 06:22 PM

Perhaps it's because my brain is fried from waking up early for a biology exam this morning, but I can't quite seem to figure out the mathematical function I need for my task.

Basically, I have a sprite of a card, and I want it to move from (x1, y2) to (x2, y2), and as it does, I want it to grow and flip. The flipping is done by simply modifying the xscale so it shrinks, hits 0, then expands again.

The initial scale of the sprite is <0.2, 0.2> (both X and Y scales), and it should grow to <1, 1>. The 0.2 is arbitrary, and thus may change, but is stored in the variable smallScale.

I figured I'd connect the scale values by tying them both to the distance from the destination point. If you're at the starting point, the scale would be <smallScale, smallScale>. When you're at the midpoint, the scale should be <0, smallScale + (1 - smallScale) / 2>; and when you reach the end, the scale should be <1, 1>.

I realize this is the perfect opportunity for a quadratic regression...yet I'm not sure how to go about performing that regression. Usually, I use online applets for quadratic regressions, but those don't work when a variable (here, smallScale) is involved.

Can anyone help me come up with the formulas for xscale and yscale, please?

-IMP

*EDIT* Sorry, I forgot to mention this: the y-scale part is easy, and I have that done. It's the x-scale formula I really need. I have this:

image_xscale = (2 * smallScale + 2)/(d * d) * n * n + (-smallScale - 3)/ d * n + 1

Where d=total distance from start to end and n=current distance to end. But it flips the card (i.e. passes xscale=0) two times during the transition instead of just once.

Restricting Movement Within An Ellipse

23 February 2012 - 01:44 AM

I'm trying to restrict the motion of an object to be within an ellipse bounded by the room size. It can move anywhere inside the ellipse, but should stop if it reaches the ellipse's border.

I've tried this (in the STEP event):

dir=point_direction(room_width/2, room_height/2, x, y);
if (dir<90 || dir>270) {
  x=min(x, room_width/2+lengthdir_x(room_width/2, dir));
}
else {
  x=max(x, room_width/2+lengthdir_x(room_width/2, dir));
}

if (dir<180) {
  y=max(y, room_height/2+lengthdir_y(room_height/2, dir));
}
else {
  y=min(y, room_height/2+lengthdir_y(room_height/2, dir));
}

The x-coordinate seems to work, but somehow the y-coordinate is off. It seems to stop before reaching the edge, with the correct x-coordinate but not the y.

Help, please?

-IMP