/************************************************
 * 
 * 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>
#include <core>
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
      
      // Give the player the fire + ice rod
      CreateEntityWithID("_firerodweapon", 0, 0, "_firerodweapon");
      SetOwnedFlag("_firerodweapon", true);
      CreateEntityWithID("_icerodweapon", 0, 0, "_icerodweapon");
      SetOwnedFlag("_icerodweapon", true);
       
      // 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",  60);  // a heart
      CallFunction("_itemlib", false, "AddItem", "sn", "_itemrupeeg", 65);  // green rupee
      CallFunction("_itemlib", false, "AddItem", "sn", "_itemrupeeb", 20);  // blue rupee
      CallFunction("_itemlib", false, "AddItem", "sn", "_itemrupeer", 5);   // red rupee
      CallFunction("_itemlib", false, "AddItem", "sn", "_itemmagics", 30);  // small magic pot
      CallFunction("_itemlib", false, "AddItem", "sn", "_itemmagicl", 10);  // large magic pot
      CallFunction("_itemlib", false, "AddItem", "sn", "_itembomb1",  30);  // 1 bomb
      CallFunction("_itemlib", false, "AddItem", "sn", "_itembomb2",  13);  // 5 bombs
      CallFunction("_itemlib", false, "AddItem", "sn", "_itembomb3",  5 );  // 10 bombs
   }
      
   // 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 hudx = 85;
   new string[16];
   new i = 0; 
   new max = GetMaxHealth("player1") / 50;
   new hp = GetHealth("player1") / 50;
   new x = 0;
   new y = 0;
   new heartstart = 614 - ( max *16);
   new Entity[20];
   new Buffer[20];
   
   SetTextSize(10);

   // Draw the Ruppee Counter
   DrawImage("__rup01", 80, hudY);
   ToString(GetCounterValue("rupees"), string);
   if ( strlen(string) == 4 )
		hudx = hudx - 8;
	else if ( strlen(string) == 3 )
		hudx = hudx - 4;
	else if ( strlen(string) == 2 )
		hudx = hudx;
	else if ( strlen(string) == 1 )
		hudx = hudx + 4;	 
   DrawBitmapText(string, hudx, hudY + 24, 255,255,255,255);
   
   // Draw the number of bombs
   hudx = 125;
   DrawImage("_bombhud", 120, hudY);
   ToString(GetCounterValue("bombs"), string);
   if ( strlen(string) == 4 )
		hudx = hudx - 8;
	else if ( strlen(string) == 3 )
		hudx = hudx - 4;
	else if ( strlen(string) == 2 )
		hudx = hudx;
	else if ( strlen(string) == 1 )
		hudx = hudx + 4;	 
   DrawBitmapText(string, hudx, hudY + 24, 255,255,255,255);
   
   // Draw the number of Arrows
   DrawImage("_arrowhud", 160, hudY);
   hudx = 165;
   ToString(GetCounterValue("arrows"), string);
   if ( strlen(string) == 4 )
		hudx = hudx - 8;
	else if ( strlen(string) == 3 )
		hudx = hudx - 4;
	else if ( strlen(string) == 2 )
		hudx = hudx;
	else if ( strlen(string) == 1 )
		hudx = hudx + 4;	 
   DrawBitmapText(string, hudx, hudY + 24, 255,255,255,255);
  
  // Draw Time
	if (!FirstRun())
	{
		new hour = GetHourCount();
		new minute = GetMinuteCount();
		ToString(hour, string);
		DrawBitmapText(string, 290, hudY, 255,255,255,255);
		ToString(minute, string);
		if ( strlen(string) == 1 ) 
		{
			DrawBitmapText("0", 330, hudY, 255,255,255,255);
				DrawBitmapText(string, 342, hudY, 255,255,255,255);
		}
		else
			DrawBitmapText(string, 330, hudY, 255,255,255,255);
	}
  	
   GetString("player1", 1, Entity);
	GetImage(Entity, Buffer);
	DrawImage("_itemhud", 596, hudY, 255, 255, 255, 200, 0, 200); 
	DrawImage(Buffer, 608, hudY+12, 255, 255, 255, 200, 0, 200);
  
	if( GetGlobal(1) > 0 ) {
	   DrawImage("_keyhud", 200, hudY);
	   ToString(GetCounterValue("smallKeys"), string);
	   DrawBitmapText(string, 205, hudY + 24, 255,255,255,255);
	}
	
   // Draw the magic meter
   DrawImage("_magichud1", 30, hudY, 255, 255, 255, 200, 0, 200); 
   DrawImage("_magichud2", 30, hudY + 8, 255, 255, 255, 200, 0, 200);
   DrawImage("_magichud2", 30, hudY + 16, 255, 255, 255, 200, 0, 200); 
   DrawImage("_magichud2", 30, hudY + 24, 255, 255, 255, 200, 0, 200); 
   DrawImage("_magichud2", 30, hudY + 32, 255, 255, 255, 200, 0, 200);
   DrawImage("_magichud3", 30, hudY + 48, 255, 255, 255, 200, 0, 200); 
   DrawRectangle( 30, ((44 - ( GetCounterValue("magicMeter") ))) + hudY + 6, 46, 64, 0, 180, 0);
   
  
   // Draw hearts
   while( i < max ) 
   { 
      if( i < hp - 1 ) 
         string = "_heart1";  
      else if( i == hp - 1 ) 
         string = "_heart3"; 
      else
         string = "_heart2"; 

      DrawImage( string, heartstart + x * 16, hudY ); 
      x++; 
      if( x >= 10 ) 
      { 
         y++; 
         x = 0; 
      } 
      i+=2; 
   } 
}