Game Map

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Terriator
Forum Commoner
Posts: 60
Joined: Mon Jul 04, 2005 12:46 pm

Game Map

Post by Terriator »

Hey,

I'm currently creating a game, which needs a map. Basically, every time a user registers, I want him to get a village located at a random location X,Y, where no other villages are currently placed. I'm foreseeing a few problems though:

1. Structurewise I was planning to let each new user be inserted into a table along with the village name and location. My problem is how to make sure that each new user doesn't get his village placed on top of another village; that is, make sure that no new user is assigned the same X,Y as another user?

2. How would I most effectively try and give new users a location "near" other users (and not 10000 fields away), but still not right next to?

3. I was planning to echo the map as a 5x5 table. How would that most effectively be extracted from the tablestructure mentioned above?

Thanks for the help,
Mathias
User avatar
ReverendDexter
Forum Contributor
Posts: 193
Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA

Post by ReverendDexter »

Well, in answer to #1, make the location a unique index. I'm not exactly sure how to go about that if you're keeping the X and Y seperate, but it would be cake if they're the same field.

For #2, I'd just write some logic, with a minimum distance and some trig, other than that, it just sounds like a random number generator. I'd also suggest a max distance, just so players aren't too far apart, basically a "min_distance < accepted_rand_distance < max_distance".

For #3, just store the central grid point, then know that the area is that point and all points +/- 2(x,y). You don't have to store it if it's calculated.

One thing you could look into to help for this would be coding up a version of Life. (http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life). It's a great excercise in relating 2-dimensional space.

Hope that helps!
Terriator
Forum Commoner
Posts: 60
Joined: Mon Jul 04, 2005 12:46 pm

Post by Terriator »

Ok, so a unique index at the "location" column. I'm still troubled with the exact query to use for this;

If I use rand() in php and the generated location is on top of another player, the query would fail and have to be executed once more.. If the rand() once again missed a free spot, it'd have to try again and again, which I imagine isn't a too healthy loop..
User avatar
ReverendDexter
Forum Contributor
Posts: 193
Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA

Post by ReverendDexter »

Well, you don't have to send a new query after every random location generation. You could initially populate an array with all of the taken locations, then check against that, then send the update query once you found an open slot locally.

The other thing to consider is the average number of queries before it hits an open location. I can't imagine that it would take more than 5 queries to ever find a spot of open land, and for every one that does take that much, there'll be 100 that find open territory on the first query. I honestly wouldn't worry too much about optimizing the conflict situation at this point. Maybe once it's implemented, you can keep it as a note as something to come back to if your server is starting to bog down, but not at design time.

Again, hope it helps!
mrkite
Forum Contributor
Posts: 104
Joined: Tue Sep 11, 2007 4:19 am

Post by mrkite »

You can create a unique key on (x,y) then just keep inserting until it succeeds :)

edit:

Code: Select all

alter table mytable add unique index location_index (x,y);
That'll add a unique index to x and y columns.. (meaning that no record can have the exact same x and y as another record.. but they can have a same x and different y, or same y and different x).
Post Reply