Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
layarmmo-monsters [2012/11/09 13:03]
matthewleach quick structuring.
layarmmo-monsters [2012/11/18 13:51]
matthewleach Added descriptions around code
Line 11: Line 11:
  
 In the echo: In the echo:
 +
 +Where the description of the hotspots normally goes, we're going to call a function **findMonsters**. ​ This function is defined in the monsters.php file that we included above.
  
 <code php> <code php>
Line 33: Line 35:
  
 ===Process for Loading Monsters=== ===Process for Loading Monsters===
-The process:+Now we'll start looking at what to put in the monsters.php file.  ​The basic process ​that we're going to follow is outlined below:
  
 <​code>​ <​code>​
Line 44: Line 46:
  
 ===Code for Loading Monsters=== ===Code for Loading Monsters===
-The actual code:+Shown below is the actual code.  Copy it, paste it, and save the file as monsters.php. ​ This will search the database for any monsters in range, and output them as '​hotspots'​ (or '​points of interest',​ or some other terminology that just means something with a location).
  
 <code php> <code php>
Line 67: Line 69:
  $monsterLon = $results[$loop]['​lon'​];​  $monsterLon = $results[$loop]['​lon'​];​
   
- // Find monster details+ // Find specific ​monster details, including some details from besitary
  $mdet = $db->​prepare( "​SELECT `name`, `desc`, `image` FROM `bestiary` WHERE `id`=:​montype"​ );  $mdet = $db->​prepare( "​SELECT `name`, `desc`, `image` FROM `bestiary` WHERE `id`=:​montype"​ );
  $mdet->​bindParam( ':​montype',​ $results[$loop]['​type'​],​ PDO::​PARAM_INT);​  $mdet->​bindParam( ':​montype',​ $results[$loop]['​type'​],​ PDO::​PARAM_INT);​
Line 112: Line 114:
 ===Explanation of Code=== ===Explanation of Code===
  
-Explanation ​particularly on the distance ​query.+Almost the first line is something that probably makes no sense to you - the call to the database. 
 + 
 +<code php> 
 +        // 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;" ); 
 +</​code>​ 
 + 
 +The main part is a distance calculation,​ and if that is removed, you should find it less daunting. 
 + 
 +<code php> 
 +        // Find monsters in range 
 + $sql = $db->​prepare( "​SELECT *, calculate_distance FROM monster ORDER BY distance LIMIT 0 , 20;" ); 
 +</​code>​ 
 + 
 +As all but the medieval-minded amongst us are aware, the Earth is roughly a sphere. ​ This makes distance calculations quite complicated,​ since latitude is actually a measure of your angle around the Earth, and is a longer distance at the equator than near the pole. I just used the code from one of Layar'​s examples, but you can look into it further on [[http://​en.wikipedia.org/​wiki/​Great-circle_distance|Wikipedia:​ Great-circle distances]]. 
 + 
 +The main bulk of the function should be fairly familiar. ​ It is just outputting any monsters that are found in the JSON format for Layar. 
 +