Jump to content

Creating A Dynamic World


smithygotlost

Recommended Posts

Ill explain what im doing then ask that way you may be able to answer lol

 

ok, me and some friends are making an online dynamic world so you would explor this world using the keys on your keyboard or the buttons that you click

 

w = up

a = left

s = down

d = right

 

all done with java catch keypress ect

 

now the hard part

 

i wanna make a room that is say 12 x 12 so u can explor 12 squares in any set direction but so for example i want the have a column in the room then i dont want people to be able to walk over it like so

 

trainingmap.gif

 

so i want people to be able to walk round this room but not off of the orange coloured squares

 

any ideas ?

 

thanks

mike

Link to comment
Share on other sites

You might have to use something like a multi dimensional array in java to check this on the fly.  You could then store like different mazes in a mysql database???  I'm honestly not sure what this has to do with php but in any case here is how you create a multi-dimensional array in javascript....

 

<script type="text/javascript">

<!--//

function CreateMultiArray()

//This function creates and fills the array

{
//initialize the main array
MultiArray = new Array(5)

//add a second dimension to the first row
MultiArray [0] = new Array(5)

//create first row.  Set to true if its a valid square.  Set to false if it's not
MultiArray [0][0] = "true"
MultiArray [0][1] = "true"
MultiArray [0][2] = "true"
MultiArray [0][3] = "true"
MultiArray [0][4] = "true"

//add a second dimension to the second row
MultiArray [1] = new Array(5)

//create second row.  Set to true if its a valid square.  Set to false if it's not
MultiArray [1][0] = "false"
MultiArray [1][1] = "true"
MultiArray [1][2] = "true"
MultiArray [1][3] = "false"
MultiArray [1][4] = "false"

//add a second dimension to the third row
MultiArray [2] = new Array(5)

//create third row.  Set to true if its a valid square.  Set to false if it's not
MultiArray [2][0] = "false"
MultiArray [2][1] = "false"
MultiArray [2][2] = "true"
MultiArray [2][3] = "false"
MultiArray [2][4] = "false"

}

//This function gets the array value when the form button is pressed

function getArray(row,col){

document.arrayForm.myResult.value=MultiArray[row][col];

}//end of getArray()

// End hiding. -->

</script>

 

That only created the first 3 rows of your example.  Then basically you would just compare the coordinates of the box they were in and see if it's true (ok to move there) or false (not ok to move there).  Let me know how this works for you.

Link to comment
Share on other sites

that looks good erm how would i make the world bigger inside this using the mysql ? as i thought that is where the php would come into it lol

 

so if my maze looks like

 

trainingmap.gif

 

how would i just add 30 rows on this ?

 

as im gonna try and make a hugh world over 5000 x 5000 but with bits missing ! so the image where you click would be say only 5 x 5 so everytime you move 1 left the 5x5 square moves

 

do you get what i mean ?

Link to comment
Share on other sites

Hmmm, again, multidimensional arrays, however, try defining the naming scheme.

 

For example, $cell[x][y] will be the x and y coordinates (IE: columns = y, rows = x)

 

So $cell[1][5] will give me the cell in column 1, row 5.

 

Now, this is easily integrated with mysql, just store something like...

 

row columns movable
1 1|2|3|4|5 true|true|false|true|false

 

So if you were to use the first row, $row[1] is an array of 1 = true, 2 = true, 3 = false, 4 = true, 5 = false.

 

So $row[1][y] would return true or false, easily.

 

Now, you can easily compile all this into multidimensional arrays like this:

 

<?php
$query = mysql_query("SELECT row,columns,movable FROM rooms WHERE room_id = 'some room id' SORT BY row ASC") or die(mysql_error());
while($row = mysql_fetch_array($query)){//loop through rows
$columns = explode("|",$row['columns']);//explode into arrays
$movable = explode("|",$row['movable']);//explode into arrays

if(count($columns) != count($movable)){//means that we're missing some values
die("I'm sorry, this room is under construction at the moment.");
}
while($i=0;$i<count($columns);$i++){//loop through
$cell[$row['row']][$columns[$i]] = $movable[$i];
//we will add all the columns PER row, and then go to the next row and do it all again for the columns in that row, etc...
}
}
?>

 

Does this make sense? This is a php code that will create a multi-dimensional array of the rooms for you correctly out of a database.

Link to comment
Share on other sites

Hi kratsg

 

I read the MSG you left and the reply was amazing the only thing is how would I go about making it so that if the room is there it shows an orange square and if there is no room it wont show a square as the problem is I'm gonna have a huge map but only be able to see a 5 x 5 sqare, this is to stop people getting an idea of room numbers and jumping around :S at some point i wanna try and put a hash code after it to make it a unique click

 

i know there is but is there anyway to build the maps in sql the php code looks at it and goes hmmmm room here, here, but not here, here

 

so that it compiles from the database without the need for thosands of pics just 2 which would be orange square no orange square ?

 

 

thats an interesting idea could you explain it a bit better ?

could you use OOP to make a class with all of the basic information?

Link to comment
Share on other sites

Hi kratsg

 

I read the MSG you left and the reply was amazing the only thing is how would I go about making it so that if the room is there it shows an orange square and if there is no room it wont show a square as the problem is I'm gonna have a huge map but only be able to see a 5 x 5 sqare, this is to stop people getting an idea of room numbers and jumping around :S at some point i wanna try and put a hash code after it to make it a unique click

 

i know there is but is there anyway to build the maps in sql the php code looks at it and goes hmmmm room here, here, but not here, here

 

so that it compiles from the database without the need for thosands of pics just 2 which would be orange square no orange square ?

 

 

thats an interesting idea could you explain it a bit better ?

could you use OOP to make a class with all of the basic information?

 

Correct, try it this way.. Assuming you had the array I showed above.

 

First, compile the array we shall use:

<?php
$query = mysql_query("SELECT row,columns,movable FROM rooms WHERE room_id = 'some room id' SORT BY row ASC") or die(mysql_error());
while($row = mysql_fetch_array($query)){//loop through rows
$columns = explode("|",$row['columns']);//explode into arrays
$movable = explode("|",$row['movable']);//explode into arrays

if(count($columns) != count($movable)){//means that we're missing some values
die("I'm sorry, this room is under construction at the moment.");
}
while($i=0;$i<count($columns);$i++){//loop through
$cell[$row['row']][$columns[$i]] = $movable[$i];
//we will add all the columns PER row, and then go to the next row and do it all again for the columns in that row, etc...
}
}
?>

 

Now, let's put this array to good use, creating the map, I will use the WHOLE map as an example, as I'm sure you can easily determine what column/row a user is in, and center it, fetch data around that area based on 2 columns left and right of column A, and 2 rows above and below row B. (if this needs to be clearer... let me know).

 

<?php

//format of array: $cell[ROW][COLUMN] = TRUE/FALSE;
//we will use the $map variable to hold the map, we won't necessarily echo it just yet, (in case we want to place headers after this code...
$map = "<table border=0 cellpadding=0 cellspacing=0>";

foreach($array as $row){//let's go through it row by row..
$map .= "<tr id='row_$row'>";

foreach($row as $column){//let's take one row, then go through all columns
if($column){$src = 'orange_square.gif';} else {$src = 'blank_square.gif';}
$map .= "<td id='column_$column'><img src='$src' alt='$src'></td>";
}//we looped through all columns in this row

$map .= "</tr>";
}

$map .="</table>";

//other stuff can go here, blah blah blah omg this is fun to comment
//lyke oh mah gawsh, so much html can go here

echo $map;//echo out the table, and that shows teh map.

?>

Link to comment
Share on other sites

For some reason I cannot edit my last post, I compiled all this into a function for you.

 

Hi kratsg

 

I read the MSG you left and the reply was amazing the only thing is how would I go about making it so that if the room is there it shows an orange square and if there is no room it wont show a square as the problem is I'm gonna have a huge map but only be able to see a 5 x 5 sqare, this is to stop people getting an idea of room numbers and jumping around :S at some point i wanna try and put a hash code after it to make it a unique click

 

i know there is but is there anyway to build the maps in sql the php code looks at it and goes hmmmm room here, here, but not here, here

 

so that it compiles from the database without the need for thosands of pics just 2 which would be orange square no orange square ?

 

 

thats an interesting idea could you explain it a bit better ?

could you use OOP to make a class with all of the basic information?

 

Correct, try it this way.. Assuming you had the array I showed above.

 

First, compile the array we shall use:

<?php
$query = mysql_query("SELECT row,columns,movable FROM rooms WHERE room_id = 'some room id' SORT BY row ASC") or die(mysql_error());
while($row = mysql_fetch_array($query)){//loop through rows
$columns = explode("|",$row['columns']);//explode into arrays
$movable = explode("|",$row['movable']);//explode into arrays

if(count($columns) != count($movable)){//means that we're missing some values
die("I'm sorry, this room is under construction at the moment.");
}
while($i=0;$i<count($columns);$i++){//loop through
$cell[$row['row']][$columns[$i]] = $movable[$i];
//we will add all the columns PER row, and then go to the next row and do it all again for the columns in that row, etc...
}
}
?>

 

Now, let's put this array to good use, creating the map, I will use the WHOLE map as an example, as I'm sure you can easily determine what column/row a user is in, and center it, fetch data around that area based on 2 columns left and right of column A, and 2 rows above and below row B. (if this needs to be clearer... let me know).

 

<?php

//format of array: $cell[ROW][COLUMN] = TRUE/FALSE;
//we will use the $map variable to hold the map, we won't necessarily echo it just yet, (in case we want to place headers after this code...
$map = "<table border=0 cellpadding=0 cellspacing=0>";

foreach($array as $row){//let's go through it row by row..
$map .= "<tr id='row_$row'>";

foreach($row as $column){//let's take one row, then go through all columns
if($column){$src = 'orange_square.gif';} else {$src = 'blank_square.gif';}
$map .= "<td id='column_$column'><img src='$src' alt='$src'></td>";
}//we looped through all columns in this row

$map .= "</tr>";
}

$map .="</table>";

//other stuff can go here, blah blah blah omg this is fun to comment
//lyke oh mah gawsh, so much html can go here

echo $map;//echo out the table, and that shows teh map.

?>

 

EDIT

Also, to put this whole map-making code into a function that can be used in a class, etc... (No comments in this piece of coding, you can understand it by now, hopefully).

 

<?php

function get_map($row_min,$row_max,$column_min,$column_max){

$query = mysql_query("SELECT row,columns,movable FROM rooms WHERE room_id = 'some room id' SORT BY row ASC") or die(mysql_error());

while($row = mysql_fetch_array($query)){
$columns = explode("|",$row['columns']);
$movable = explode("|",$row['movable']);

if(count($columns) != count($movable)){
die("I'm sorry, this room is under construction at the moment.");
}

while($i=0;$i<count($columns);$i++){
$cell[$row['row']][$columns[$i]] = $movable[$i];
}

}

mysql_free_result($query);//this will help to clear off the memory so we don't bog the server down

$map = "<table border=0 cellpadding=0 cellspacing=0>";

//format of array: $cell[ROW][COLUMN] = TRUE/FALSE;

foreach($cell as $index=>$row){

if($index >= $row_min && $index <= $row_max){

$map .= "\t<tr id='row_$index'>\n";

foreach($row as $column=>$movable){

if($column >= $column_min && $column <= $column_max){

if($movable){$src = 'orange_square.gif';} else {$src = 'blank_square.gif';}
$map .= "\t\t<td id='column_$column'><img src='$src' alt='$src'></td>\n";

}

}

$map .= "\t</tr>\n";

}

}

$map .="</table>";

return $map;
}

$holder = get_map(21,25,13,17);//this can be ONE map from row 21-25 and column 13-18
$holder2 = get_map(1,5,56,60);//this can be another map from row 1-5 and column 56-60

?>

 

An added bonus for you, I inserted some tabs and newline characters in the html output, so if you view the source code of the html generated in FireFox, it will look patterned and organized :-)

Link to comment
Share on other sites

so the sql would be

-- 
-- Table structure for table `room`
-- 

CREATE TABLE `room` (
  `id` bigint(255) NOT NULL auto_increment,
  `row` varchar(255) NOT NULL,
  `columns` varchar(255) NOT NULL,
  `movable` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

-- 
-- Dumping data for table `room`
-- 

INSERT INTO `room` VALUES (1, '1', '1', 'true');
INSERT INTO `room` VALUES (2, '1', '2', 'true');

 

this would then be pulled down by the script ( i hope lol )

 

i turned it all into a php page with a database connect in it

 

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');

$dbname = 'bogblob_rpg';
mysql_select_db($dbname);

 

i executed it and i just get a blank page :S

 

sorry im being a pain i just cant get my head round it very well

 

 

Link to comment
Share on other sites

It needs to look like this:

 

id row columns movable
1 1 1|2|3|4|5 true|true|false|true|false
[code]

So, the first row will have the following data:

ID=1
Row=1
Columns = 1|2|3|4|5 <--this will be split into an array
Movable = true|true|false|true|false <-- this will be split into an array

It's a bit easier with the coding to have it listed by rows, rather than by both rows AND columns.

[/code]

Link to comment
Share on other sites

Which one are you using? The function?

 

You need to call the function in order for it to run.

 

IE:

 

$partial_map = get_map(1,5,1,6);//rows 1-5, columns 1-6
echo $partial_map;

 

OR, skip it and do this

 

echo get_map(1,5,1,6);

 

Both of these will echo out the same thing. get_map() is a function that will return an html format of the map you're looking at. You can change this HTML format inside the function, and any script that calls it will get this newly updated HTML.

Link to comment
Share on other sites

after lots of tweaking ect i have managed to make it function but i now get this error

 

Parse error: syntax error, unexpected ';' in /hsphere/local/home/bogblob/rpg.theblackmagic.co.uk/dc.php on line 15

 

the code is here

 

<?php

function get_map($row_min,$row_max,$column_min,$column_max){

$query = mysql_query("SELECT row,columns,movable FROM rooms WHERE room_id = 'some room id' SORT BY row ASC") or die(mysql_error());

while($row = mysql_fetch_array($query)){
$columns = explode("|",$row['columns']);
$movable = explode("|",$row['movable']);

if(count($columns) != count($movable)){
die("I'm sorry, this room is under construction at the moment.");
}

while($i=0;$i<count($columns);$i++){
$cell[$row['row']][$columns[$i]] = $movable[$i];
}

}

mysql_free_result($query);//this will help to clear off the memory so we don't bog the server down

$map = "<table border=0 cellpadding=0 cellspacing=0>";

//format of array: $cell[ROW][COLUMN] = TRUE/FALSE;

foreach($cell as $index=>$row){

if($index >= $row_min && $index <= $row_max){

$map .= "\t<tr id='row_$index'>\n";

foreach($row as $column=>$movable){

if($column >= $column_min && $column <= $column_max){

if($movable){$src = 'orange_square.gif';} else {$src = 'blank_square.gif';}
$map .= "\t\t<td id='column_$column'><img src='$src' alt='$src'></td>\n";

}

}

$map .= "\t</tr>\n";

}

}

$map .="</table>";

return $map;
}

$holder = get_map(21,25,13,17);//this can be ONE map from row 21-25 and column 13-18
$holder2 = get_map(1,5,56,60);//this can be another map from row 1-5 and column 56-60

$partial_map = get_map(1,5,1,6);//rows 1-5, columns 1-6
echo $partial_map;

?>

 

can anyone see the problem ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.