Creating a game world... there's got to be a better way.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Creating a game world... there's got to be a better way.

Post by Mightywayne »

I hate making mobs in my game. Do you know why? Because my code looks like this.

Code: Select all

if ($x >= 780 && $x <= 820 && $y >= 1260 && $y <= 1290) // between neworsa and abrupt valley, little box
		{
			if ($randommob3 == 1)
			{
			$monster = 'Slime';
			mysql_query("UPDATE explore SET mobname = '$monster' WHERE username = '$user'");
			}
			if ($randommob3 == 2)
			{
			$monster = 'Vicious Rabbit';
			mysql_query("UPDATE explore SET mobname = '$monster' WHERE username = '$user'");
			}
			if ($randommob3 == 3)
			{
			$monster = 'Dire Wolf';
			mysql_query("UPDATE explore SET mobname = '$monster' WHERE username = '$user'");
			}
		}
My world is like, 4000 x 3000. That's a LOT of stupid if statements, especially when not every spot produces a monster. (there's oceans, mountains, etc.)

So what I'm saying is... do you guys have any better ideas for this? I tried the Quantum Game Library, atleast for ideas, but that runs on java and real-time viewing of other players; mine's instanced, server-based, and no pretty effects.

I'm thinking maybe some sort of huge array where I could set a whole group of coords as mountains, and oceans. I just hate to work with giant rectangles being oceans, and players being technically able to move into the ocean, because I missed a coord somewhere.

I don't care how hard your solution is, but I'd prefer to stay in PHP's range.
josa
Forum Commoner
Posts: 75
Joined: Mon Jun 24, 2002 4:58 am
Location: Sweden

Post by josa »

One solution that comes to mind is to store more data in the database. I may look like this for example:

Areas
-------
id
xmin
xmax
ymin
ymax
name

Monsters
-----------
id
name

Areas_Monsters_Assn
----------------------------
area_id
monster_id

Explore
---------
username
mobname
(more columns I don't know about)

In the Areas and Monsters table you store all data about areas and monsters (add more columns as needed). The Areas_Monsters_Assn lets you have a many-to-many relationship between monsters and areas, meaning that one monster can be tied to more than one area and one area can hold many monsters. Now you do like this to get a random monster for the current area:

Code: Select all

SELECT monsters.name
FROM monsters
INNER JOIN areas_monsters_assn
ON areas_monsters_assn.monster_id = monsters.id
INNER JOIN areas
ON areas_monsters_assn.area_id = areas.id
WHERE 
  areas.xmin <= $x
AND
  areas.ymin <= $y
AND 
  areas.xmax >= $x
AND
  areas.ymax >= $y
ORDER BY RAND()
LIMIT 1
When you have your monster you can update the Explore table:

Code: Select all

UPDATE explore SET mobname = $monster WHERE username = $user
I wrote this post a little to fast to be able to guarantee that the design optimal. But it should be a start.

/josa


/josa
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Hmm, but that's a bit hard on the database. Still interesting, don't get me wrong. I'll save it in my big notepad file of knowledge.

I'm currently having a problem with plotting polygons... I didn't make it to a high math in school because I never cared for math, I guess this is one example of me actually needing it for my work.

Code: Select all

$x >= 770 && $x <= 820 && $y >= 1210 && $y <= 1290
That's a square... would you know how to make a polygon?

Edit:
Haha, I got it! :D

Code: Select all

if (($x >= 769 || $x >= 750) && ($x <= 820 && $y >= 1210 && $y <= 1290))
That SHOULD be it, right? That is a polygon? It should take all from 770 - 750, and then match it up with the rest.
Post Reply