Jump to content

If statement making me mad!


reportingsjr

Recommended Posts

Ok, so right now im making a game. So im turning it into a 2d game. I have moving around set. But there are areas I dont want people to go to. I tried to figure it out but it wouldnt work! Here is my code:

	//GO RIGHT	if($position->x >= 594 && ($position->x >= 48 && $position->x <= 168 && $position->y >= 502)){}else{		$database->query("UPDATE `map` SET x = x+2 WHERE name = '{$_POST['username']}'");	}

you wont be able to see it because you need to be logged in and I dont think anyone here wants to register etc.. So $position->x is the left right distance and $position->y is the up down value. What do I have to put so it wont allow you to move into a square of any size?

Link to comment
Share on other sites

Of any size? You're only talking about position, what do you mean size?I think you're probably using AND when you should be using OR (for example, x will never be greater then or equal to 594 and less then or equal to 168). It's hard to say what you want though without seeing the board visually. But I would think it would be something like this:

	if(!($position->x >= 594 || ($position->x >= 48 && $position->x <= 168 && $position->y >= 502))) 	{		$database->query("UPDATE `map` SET x = x+2 WHERE name = '{$_POST['username']}'");	}

That means that the database will be updated if x is not either greater then 594, or between 48 and 168 when y is greater then 502.

Link to comment
Share on other sites

I hope that works! Well, I do have a test account that you can see it in. go to http://excibius.com and login as "blehe" with a password of "blehe". Then on the right menu when you log in click the big red link that says "NEW 2D MAP". The on the right there are arrows you can use to move around. It supposed to make it so you cant move around the lake, or off the right edge of the page. Thanks! I will test when I get home!

Link to comment
Share on other sites

I see someone got on it! Well, it works. Sort of :), I have this: but I tried to move down and it wouldnt move down! Here is my whole code so far:

if($_POST['way'] =="up"){	//GO UP	if(!($position->y <= 0 || ($position->x >= 48 && $position->x <= 168 && $position->y >= 502))){}else{		$database->query("UPDATE `map` SET y = y-2 WHERE name = '{$_POST['username']}'");	}}elseif($_POST['way'] =="down"){	//GO DOWN	if(!($position->y >= 594 || ($position->x >= 48 && $position->x <= 168 && $position->y <= 320))){}else{		$database->query("UPDATE `map` SET y = y+2 WHERE name = '{$_POST['username']}'");	}}elseif($_POST['way'] =="left"){	//GO LEFT	if(!($position->x <= 0 || ($position->x <= 168 && $position->y >= 502 && $position->y <= 320))){}else{		$database->query("UPDATE `map` SET x = x-2 WHERE name = '{$_POST['username']}'");	}}else{	//GO RIGHT		if(!($position->x >= 594 || ($position->x >= 48 && $position->y >= 502 && $position->y <= 320))){			$database->query("UPDATE `map` SET x = x+2 WHERE name = '{$_POST['username']}'");		}}

Link to comment
Share on other sites

Hmm. It could be related to your no-op:if (...){}else{Instead of having nothing for the if, just negate the entire thing and remove the else. You can see I added that above. Instead of having this as the condition:$position->x >= 594 || ($position->x >= 48 && $position->x <= 168 && $position->y >= 502)And then doing nothing if it's true, and using an else, I wrapped the whole thing in parens and put a negation operator at the front:!($position->x >= 594 || ($position->x >= 48 && $position->x <= 168 && $position->y >= 502))So then if the inside condition is not true, then the negation makes it true and the if statement executes. So you don't need the empty if block followed by an else. It makes it a little hard to understand if someone doesn't notice that there is an {}else{ at the end of the condition.Is that confusing?

Link to comment
Share on other sites

Doh! I messed up :). So thats why it was moving. I forgot to remove the {}else from all of them. Checking it now!EDIT: No :), it still doesnt work! What do you think is going wrong here? I would love for this to work!!FULL CURRENT CODE:

if($_POST['way'] =="up"){	//GO UP	if(!($position->y <= 0 || ($position->x >= 48 && $position->x <= 168 && $position->y >= 502))){		$database->query("UPDATE `map` SET y = y-2 WHERE name = '{$_POST['username']}'");	}}elseif($_POST['way'] =="down"){	//GO DOWN	if(!($position->y >= 594 || ($position->x >= 48 && $position->x <= 168 && $position->y <= 320))){		$database->query("UPDATE `map` SET y = y+2 WHERE name = '{$_POST['username']}'");	}}elseif($_POST['way'] =="left"){	//GO LEFT	if(!($position->x <= 0 || ($position->x <= 168 && $position->y >= 502 && $position->y <= 320))){		$database->query("UPDATE `map` SET x = x-2 WHERE name = '{$_POST['username']}'");	}}else{	//GO RIGHT		if(!($position->x >= 594 || ($position->x >= 48 && $position->y >= 502 && $position->y <= 320))){			$database->query("UPDATE `map` SET x = x+2 WHERE name = '{$_POST['username']}'");		}}

Link to comment
Share on other sites

Well, when all else fails, follow the logic.

move upif(!($position->y <= 0 || ($position->x >= 48 && $position->x <= 168 && $position->y >= 502)))

This will be true if y is greater then 0, and if x is not between 48 and 168 when y is greater then 502.

move downif(!($position->y >= 594 || ($position->x >= 48 && $position->x <= 168 && $position->y <= 320)))

This will be true if y is less then 594 and x is not between 48 and 168 when y is less then 320. So, if x is between 48 and 168 when y is less then 320, they won't be able to move down. The condition for y probably needs to be changed.

move leftif(!($position->x <= 0 || ($position->x <= 168 && $position->y >= 502 && $position->y <= 320)))

They can move left if x is greater then 0 and if x is not less then 168 when y is greater then 502 and less then 320. That doesn't make sense, the values for y need to be switched, and you might need a lower bound for x at 48.

move rightif(!($position->x >= 594 || ($position->x >= 48 && $position->y >= 502 && $position->y <= 320)))

They can move right if x is less then 594 and if x is not greater then 48 when y is greater then 502 and less then 320. Again, the values for y need to be switched, and there probably needs to an upper bound on x, probably at 168.

Link to comment
Share on other sites

It seems fine to me, at what x,y position would they get stuck at?You know, another way you could do this would be to define the map in an array and then just check if they can move to the target square. Your maps would be saved in files that you could include, maybe even XML, and stored in an array. So if your map is 594x594, then you would make a 594x594 array then just check if $map[$x][$y] is 1 or 0. Then you can get all modular, and keep your maps in included files and work on your game engine instead of the map. Then you can have other people make maps for you, and you can define whatever squares you want to be able to access, or have a certain item on them like a door or a chest or a dock or a brothel. You know, whatever.

Link to comment
Share on other sites

Single picels or what? Tahat may take a while to do single pixels! I wish I could do abstract shapes :). Good idea, I need to figure out xml parsing though. So like this:

if(in_array($position->x,$nogo){ if(in_array($position->y,$nogo[$position->x)){	return false;	//do nothing, both x and y in $nogo }else{   return true;   //tell other if function its god to go, that area is fine! }}

Or maybe another easier way? But $nogo would have all of the positions I need..

Link to comment
Share on other sites

Think of an array laid out exactly like your map. Here would be a sample array for a 10x10 map:

1111111111110111011111001001111101110111110001110001010111101101011100101101111110100111111111111111

10 rows, and 10 columns. This is a simplified example, where each square can only be a 1 or a 0, 1 if you can move there and 0 if not. But you can tell by looking at that array where you can go and where you can't. So, if a person wants to move to position $x,$y then you can check if $map[$x][$y] is equal to 1 or 0 to determine if they can move there.

if ($map[$x][$y])  //update the databaseelse  //illegal move

You can go a step further, and instead of having just 1 or 0, the square could be a "Y" if you can move there, a "N" if you can't, maybe a "C" if there is a chest there, and moving onto it opens it up, a "D" for a door, "W" for water, etc. Then if someone has some sort of item or skill they might be able to move on the water.XML would be easiest, but it's not strictly necessary. You can just define the $map array literally, like this for the first 3 rows above:

$map = array();$row = array(1,1,1,1,1,1,1,1,1,1);$map[] = $row;$row = array(1,1,0,1,1,1,0,1,1,1);$map[] = $row;$row = array(1,1,0,0,1,0,0,1,1,1);$map[] = $row;

Which would take a while for a 594x594 array. Or, you can write the map file exactly just like this:

1111111111110111011111001001111101110111110001110001010111101101011100101101111110100111111111111111

And have your game engine parse that up into rows and columns and build the array programmatically. In fact, doing that would far and away be the smallest file size, but it would eliminate a couple options XML would give you, like being able to specify a name or description for any given square.But defining your maps externally would give you a lot more flexibility, you can have rooms that are just smaller maps (like the 10x10 above), dungeons, etc where you don't have to add a lot of logic to the game engine. And you can define any arbitrary shape you want in a grid like that.

Link to comment
Share on other sites

So like $map[row][column]. Maybe I could make it true or false? I might make it xml. So I guess I will try and work on it today. But thats going to be a buttload of work! The first map is a 600X600 :), 3600 things to put in the array. Maybe I wil make it smaller, and do only even (you move by 2 start at 0,0 so you can go to odd numbered anyways). I think for skills I will just have it check if you are close enough, if you are then the ajax will update and you get a link over the area. Or something like that! I am going to have a little box at the bottom that is chat for a whole section of map PLUS you can see results of skills and such. Like for opening a chest you will see in italics "You open the chest and find blah blah". Or something. Thanks steve! Ill start it when I get home :).

Link to comment
Share on other sites

Well, turns out even the smallest file size is way too big :). Here is what I got:

[Mon Dec 04 15:54:47 2006] [error] [client *.*.*.*] Allowed memory size of 8388608 bytes exhausted (tried to allocate 11796480 bytes)[Mon Dec 04 15:54:47 2006] [error] [client *.*.*.*] Premature end of script headers: php-script
=[so.. either smaller maps, just even (probably that), Or database.
Link to comment
Share on other sites

With XML you are going to run into some pretty big filesizes. It would be much smaller to just use a bitmap type of thing, a 600x600 file would be 360KB. Or you can try increasing the memory limit of PHP, 11 megs of memory space isn't that much, but I guess it only allows 8 megs.

Link to comment
Share on other sites

A bitmap type of thing? What do you mean?I dont have access to the php.ini file (well, its readonly). And I cant delete it. What would happen if I edited it, copied and made a new php.ini? I think I may just go with evens. Or make smaller map sizes.

Link to comment
Share on other sites

There's got to be a readonly attribute you can change. But unless you're hosting your site, I guess the file that matters is the one on the server.A bitmap is what I described above. Like, for example, here would be a 20x20 map with a rock or some type of non-passable thing in the upper right, and a small oddly-shaped pond in the lower-right:

YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYNNNYYYYYYYYYYYYYYYYNNNNNYYYYYYYYYYYYYYYNNNNNYYYYYYYYYYYYYYYNNNNNYYYYYYYYYYYYYYYYNNNYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYWWYYYYYYYYYYYWYYYYYWWWWYYYYYYYYYWWWYYYWWWWWWYYYYYYYYWWWWWWWWWWWWYYYYYYYYYWWWWWWYWWWWYYYYYYYYYYWWWWYYYWWYYYYYYYYYYYYYYYYYYYYYYY

So, for this, "Y" means you can walk there, "N" means you can't, and "W" means water (maybe you can move there, maybe not, depending on your game). You can have "T" for a tree, or "D" for a door, or whatever. This way, the map is only 1 byte per pixel or per square or whatever. To read the map, you would read the file into a string (file_get_contents), use explode to split it up into an array of lines, and then use explode again on each line to split each line up into an array of characters. Then, you are left with an array of lines, where each line is an array or characters, or an array of rows/columns. So $map[0][0] would refer to the upper-left square. This approach would take a little more time to make the map file, but it gives you enough flexibility that you can define a different type for each individual square. So you can have regions shaped however you want them. You can make mazes or whatever. And, a file like that would be very easy to use. It might also take a little more time to make edits to the map, but ideally you wouldn't have to do a lot of editing anyway.That's just my thoughts. I would probably go about it doing it something like that. You can have a companion file to the bitmap that would be an XML file where maybe you could define text or a description for particular regions. But this type of approach totally separates the content (map) from the game engine. So you can update either independently without worrying about the other one. And, you can focus on the game engine and have other people build the maps.You can make the maps easier to read too, like here is the same thing as above with different characters:

...................................RRR................RRRRR...............RRRRR...............RRRRR................RRR............................................................................................................................................................WW...........W.....WWWW.........WWW...WWWWWW........WWWWWWWWWWWW.........WWWWWW.WWWW..........WWWW...WW.......................

Link to comment
Share on other sites

Ohh, so how would I split it up into an array? But yeah that would be alot better! So I just need how to split it into an array. Would I just do $map = explode('', $bitmap); ? I know how to pull the file.. But ok!

Link to comment
Share on other sites

If you have your map in a file called map1.map or something, you would read it and parse it like this:

<?php$maptext = file_get_contents("map1.map");$map = explode("\n", $maptext);foreach ($map as $i => $mapline){  $map[$i] = str_split($mapline);}?>

Now you have an array called $map[][] where the first index is the row (y) and the second is the column (x). I guess that's a little backwards, but that's how it's set up. So $map[0][9] would refer to the 10th square on the first row.I guess you could get creative, and have characters that visually look like water, or a rock or whatever.

...................................***................*****...............*****...............*****................***............................................................................................................................................................~~...........~.....~~~~.........~~~...~~~~~~........~~~~~~~~~~~~.........~~~~~~.~~~~..........~~~~...~~.......................

Link to comment
Share on other sites

@justsomeguy - Nice looking array!Maybe you could add a mountain range. :)

...................................***................*****...............*****...............*****................***..........................^..................^^^.................^^.................^^.................^^.................^^^.................^^............~~....^......~.....~~~~.........~~~...~~~~~~........~~~~~~~~~~~~.........~~~~~~.~~~~..........~~~~...~~.......................

Link to comment
Share on other sites

A field with some flowers..

.....................@.............***.......@........*****.@..@..........*****......@........*****..@.............***..........................^..................^^^.................^^.................^^.................^^.................^^^.................^^............~~....^......~.....~~~~.........~~~...~~~~~~........~~~~~~~~~~~~.........~~~~~~.~~~~..........~~~~...~~.......................

Link to comment
Share on other sites

Explode returns false if the delimiter is an empty string. You can use this version:

if (!function_exists("str_split")){  function str_split($str, $nr=1) {  	 return array_slice(split("-l-", chunk_split($str, $nr, '-l-')), 0, -1);  }}

Link to comment
Share on other sites

Arghh! It only goes up to 172 before it stops.. Then thats when the memory is used up :). http://www.excibius.com/excibius/skills/wi...jr&way=left If you look at the source it should say test, but it only has a meta which is at the top of the page. How can I increase the memory size of php?

Link to comment
Share on other sites

You can use ini_set, the configuration option is called memory_limit.http://www.php.net/manual/en/ini.core.php#ini.memory-limitBut it seems weird that it would take up that much memory for something like this. Can you post the code that is reaching the memory limit? If this script is going to be running for multiple players, you definately don't want it using this much memory. It might be worth it to have a script that can "compile" a map for you so that it loads quicker if it is going to be needed for every move for every player.You can also use this function in your loop or something to show you how much memory is being allocated:http://www.php.net/manual/en/function.memory-get-usage.phpIf you want, I can help you create a map class that would either load a compiled version of the map quickly, or compile the map if it doesn't exist.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...