This is an old revision of the document!


Loading Monsters From the Database

It's time to add our monsters to the real-world. We are going to make some significant changes to the code that we already have, so that so that the games searches for and displays any monsters within range of the player.

Changes to Main File

In our main file we are going to add another include statement.

include 'dbinfo.php';
include 'monsters.php';

In the echo:

echo '{
    "layer": "mmodemo",
	"actions": [
		{
			"uri": "http://www.thoughtfulmonkey.com/layar-mmo/display-stats.php",
			"label": "ST:'.$userST.' T:'.$userT.' HP:'.$userHP.'",
			"contentType": "text/html"
		}
	],
    "hotspots": [';
 
findMonsters($latitude, $longitude, $db, $userID);
 
echo'],
    "errorCode": 0,
    "errorString": "ok"
}';

Process for Loading Monsters

The process:

Search database for monsters within range
Loop through all of the ones that were found
    Load their specific details
    Output the details in JSON format
Next monster

Code for Loading Monsters

The actual code:

<?php
 
function findMonsters ($latitude, $longitude, $db){
 
	// Find monsters in range
	$sql = $db->prepare( "SELECT *, ( 6371000 * ACOS( COS( RADIANS(:lat) ) * COS( RADIANS( lat ) ) * cos( RADIANS( lon ) - RADIANS(:lon) ) + SIN(RADIANS(:lat) ) * SIN( RADIANS( lat ) ) ) ) AS distance FROM monster ORDER BY distance LIMIT 0 , 20;" );
	$sql->bindParam( ':lat', $latitude, PDO::PARAM_STR);
	$sql->bindParam( ':lon', $longitude, PDO::PARAM_STR);
	$sql->execute();
 
	// If any were found, then convert to array
	$count = $sql->rowCount();
	if ($count > 0) $results = $sql->fetchAll(PDO::FETCH_ASSOC);
 
	// Loop through array, and output required data in JSON format
	for ($loop=0; $loop < $count; $loop++)
	{
		$monsterLat = $results[$loop]['lat'];
		$monsterLon = $results[$loop]['lon'];
 
		// Find monster details
		$mdet = $db->prepare( "SELECT `name`, `desc`, `image` FROM `bestiary` WHERE `id`=:montype" );
		$mdet->bindParam( ':montype', $results[$loop]['type'], PDO::PARAM_INT);
		$mdet->execute();
		$mvals = $mdet->fetchAll(PDO::FETCH_ASSOC);
 
		// Risky, but assume that data is always returned
 
		echo '{
				"id": '.$loop.',
				"anchor":{
					"geolocation":{
						"lat": '.$monsterLat.',
						"lon": '.$monsterLon.'
					}
				},
				"text":{
					"title": "'.$mvals[0]['name'].'",
					"description": "'.$mvals[0]['desc'].'"
				},
				"object":{
					"contentType": "image/png",
					"url": "'.$mvals[0]['image'].'",
					"size": 1.8
				},
				"transform":{
					"rotate": { 
						"axis": { 
							"x": 0, 
							"y": 0, 
							"z": 1
						}, 
						"angle": 0, 
						"rel": true 
					}
				}
			}';
	}
}
 
?>

Explanation of Code

Explanation - particularly on the distance query.