/************************************************
 * 
 * Desc: This is your Quest's main script, it 
 *       will execute every time the program 
 *       loops. It it also useful for
 *       initialising game data, like the player.
 *
 *       This script is complex, but most of it
 *       should be automatically generated for
 *       you.
 *  
 ***********************************************/
#include <entity>
#include <general>
#include <counter>

main()
{
   if (FirstRun())
   {
      /*****************************************************
       * This section is only run once, it sets up a lot of
       * the game entities, like the weapons etc..
       ****************************************************/

      // Create the Players Weapon entities
      CreateEntityWithID("_swordweapon1", 0, 0, "_swordweapon1");	// Sword
      CreateEntityWithID("_pbombweapon1", 0, 0, "_pbombweapon1");	// Bombs
      
      // Create counters used globally by all scripts
      CreateCounterWithID(0, 200, "rupees");    // Create a Rupee Counter
      CreateCounterWithID(0, 99,  "smallKeys"); // Create a Dungeon "small-Key" Counter
      CreateCounterWithID(0, 20,  "bombs");		// Create a bomb counter
      CreateCounterWithID(0, 20,  "arrows");	// Create an arrow counter
      CreateCounterWithID(0, 1,   "masterKey");	// Master key for dungeon
      CreateCounterWithID(0, 44,  "magicMeter");// How much magic the player has - the max 
      										    // is 44 becuase thats how many pixels high the meter is            
      // Set up the menu
      CallFunction("_menulib", false, "Init", "NULL");
 
      // Setup the item library - add different dropable items to it
      CallFunction("_itemlib", false, "Init", "NULL");
      CallFunction("_itemlib", false, "AddItem", "sn", "_itemheart",  50);
      CallFunction("_itemlib", false, "AddItem", "sn", "_itemrupeeg", 50);
      CallFunction("_itemlib", false, "AddItem", "sn", "_itemrupeeb", 20);
      CallFunction("_itemlib", false, "AddItem", "sn", "_itemrupeer", 5);
   }
      
   // Call a function to handle the start Menu
   if (CallFunction("_menulib", false, "HandleMenu", "NULL") != 1)  
   {
   	   // Dont draw the hud if the menu is being drawn
	   // Draw the HUD at the top of the Screen
	   DrawHUD();
   }
}


//----------------------------------------
// Name: DrawHUD()
//----------------------------------------
DrawHUD()
{
   new hudY = 10;
   new string[16];
   new i = 0; 
   new max = GetMaxHealth("player1") / 50;
   new hp = GetHealth("player1") / 50;
   new x = 0;
   new y = 0;

   SetTextSize(10);

   // Draw the Ruppee Counter
   DrawImage("__rup01", 100, hudY);
   ToString(GetCounterValue("rupees"), string);
   DrawText(string, 120, hudY, 255,255,255,255);
   
   // Draw the number of bombs
   DrawImage("_bombhud", 170, hudY);
   ToString(GetCounterValue("bombs"), string);
   DrawText(string, 190, hudY, 255,255,255,255);
   
   // Draw the number of Arrows
   DrawImage("_arrowhud", 240, hudY);
   ToString(GetCounterValue("arrows"), string);
   DrawText(string, 270, hudY, 255,255,255,255);
   
   // Draw the magic meter
   DrawImage("_magichud1", 30, hudY, 255, 255, 255, 255, 0, 200); 
   DrawImage("_magichud2", 30, hudY + 16, 255, 255, 255, 255, 0, 200); 
   DrawImage("_magichud2", 30, hudY + 32, 255, 255, 255, 255, 0, 200);
   DrawImage("_magichud3", 30, hudY + 48, 255, 255, 255, 255, 0, 200); 
   DrawRectangle( 30, ((44 - ( GetCounterValue("magicMeter") ))) + hudY + 6, 46, 64, 0, 180, 0);
   
   // Draw life meter
   DrawImage("__life1", 500, hudY); 
   
   // Draw hearts
   while( i < max ) 
   { 
      if( i < hp - 1 ) 
         string = "_heart1";  
      else if( i == hp - 1 ) 
         string = "_heart3"; 
      else
         string = "_heart2"; 

      DrawImage( string, 464 + x * 16, 30 + ( y * 16 ) ); 
      x++; 
      if( x >= 10 ) 
      { 
         y++; 
         x = 0; 
      } 
      i+=2; 
   } 
}