Hero = {
	Description = "Hero",

	HP         = 100,
	MaxHP      = 10,

	Level      = 1,
	Experience = 0,
	NextExp    = 30,

	Gold       = 0,

	AttackRating  = 40,
	DefenseRating = 30,

	AttackPlus	= 0,
	DefensePlus	= 0,
	RangePlus	= 0,

	Visibility = 10,

	Inventory = {
		Weapon	= nil,
		Armor	= nil,
		Usable 	= { },
		Other  	= { }
	},

	DogCreated = false
}

function Hero:Update()
	if not self.DogCreated then
		self:AddToInventory( CopyObject( Dagger  ), "Weapon" )
		self:AddToInventory( CopyObject( BoarFur ), "Armor"  )

		self.StartTime = os.date( "%d.%m.%Y %H:%M" )

		self.DogCreated = true
	end

	if not LightingInitialized then InitVisibility() end

	UpdateVisibility( self.TileX, self.TileY, self.Visibility + self.RangePlus, 191, 191, 191 )

	LightingInitialized = false

	SetColor( 250, 250, 250 )

	if KeyHit( 84 ) then
		if WaitForTrash then
			PrintLog( "No item thrown away." )
		else
			PrintLog( "Press keys 1-9 to throw an item in your inventory away." )
			PrintLog( "Note that items are destroyed once they leave your inventory." )
		end

		WaitForTrash = not WaitForTrash
	elseif KeyHit( 68 ) then
		if not WaitForDescription then
			PrintLog( "Press keys 1-9 to display a description of an item in your inventory." )
		end

		WaitForDescription = not WaitForDescription
	end

	DrawText( "Inventory:", 600, 54 )
	DrawText( "Usable items:", 620, 72 )
	DrawText( "Miscellaneous:", 620, 198 )

	for I = 1, 5 do
		local Item = self.Inventory.Usable[ I ]

		if KeyHit( NumKeys[ I ] ) and Item then
			if WaitForTrash then
				self:RemoveFromInventory( Item, "Usable" )

				WaitForTrash = false
			elseif WaitForDescription then
				DescriptionItem = Item

				WaitForDescription = false
			else
				Item:Use()
			end
		end

		Item = self.Inventory.Usable[ I ]

		DrawText( I .. ": " .. ( Item and Item.ShortDescription or "---" ), 640, 72 + I*18 )
	end

	self. AttackPlus = 0
	self.DefensePlus = 0
	self.  RangePlus = 0

	for I = 1, 5 do
		local Index = ( I < 5 and I + 5 or 0 )

		local Item = self.Inventory.Other[ I ]

		if KeyHit( NumKeys[ Index ] ) and Item then
			if WaitForTrash then
				self:RemoveFromInventory( Item, "Other" )

				WaitForTrash = false
			elseif WaitForDescription then
				DescriptionItem = Item

				WaitForDescription = false
			end
		end

		if Item then
			if Item. AttackPlus then self. AttackPlus = self. AttackPlus + Item. AttackPlus end
			if Item.DefensePlus then self.DefensePlus = self.DefensePlus + Item.DefensePlus end
			if Item.  RangePlus then self.  RangePlus = self.  RangePlus + Item.  RangePlus end
		end

		DrawText( Index .. ": " .. ( Item and Item.ShortDescription or "---" ), 640, 198 + I*18 )
	end

	if self.Inventory.Weapon then self. AttackPlus = self. AttackPlus + self.Inventory.Weapon. AttackRating end
	if self.Inventory. Armor then self.DefensePlus = self.DefensePlus + self.Inventory. Armor.DefenseRating end

	if DescriptionItem and DescriptionItem.LongDescription then
		DrawText( DescriptionItem.ShortDescription .. ":", 600, 312 )
		DrawLineText( BuildDescription( DescriptionItem ), 600, 330 )
	end

	local A, B, C, D, E, F, G, Depth = GetMapData( 0, 0 )

	DrawText( "Weapon: " .. ( self.Inventory.Weapon and self.Inventory.Weapon.ShortDescription or "None" ), 600, 0  )
	DrawText( "Armor:  " .. ( self.Inventory.Armor  and self.Inventory.Armor .ShortDescription or "None" ), 600, 18 )

	DrawText( "HP: " .. self.HP .. "/" .. self.MaxHP ..
		   "   Attack: "     .. self.AttackRating  .. "%" .. ( self.Inventory.Weapon and ( " " .. NumberToString( self. AttackPlus ) .. "%" ) or "" ) ..
		   "   Defense: "    .. self.DefenseRating .. "%" .. ( self.Inventory.Armor  and ( " " .. NumberToString( self.DefensePlus ) .. "%" ) or "" ) ..
		   "   Level: "      .. self.Level ..
		   "   EXP: "        .. self.Experience .. "/" .. self.NextExp ..
		   "   Gold: "       .. self.Gold ..
		   "   Floor: "      .. Depth, 0, 584 )

	for I = 1, #MessageLog do
		DrawText( MessageLog[ I ], 0, 494 + I*18 )
	end
end

function Hero:AddToInventory( Item, ItemKind )
	if  Item.Initialized == nil then
		RandomizeItem( Item )

		Item.Initialized = true
	end

	if ItemKind == "Weapon" or ItemKind == "Armor" then
		local Index = self:FindInInventory( Item, "Usable" )

		if Index then
			self.Inventory[ "Usable" ][ Index ] = self.Inventory[ ItemKind ]

			self:RearrangeInventory( "Usable" )
		end

		self.Inventory[ ItemKind ] = Item

		PrintLog( "You equip a " .. Item.Description .. "." )
	else
		if #self.Inventory[ ItemKind ] == 5 then
			PrintLog( "You are overburdened and cannot possibly pick up that " .. Item.Description .. "." )

			return false
		else
			self.Inventory[ ItemKind ][ #self.Inventory[ ItemKind ] + 1 ] = Item

			PrintLog( "You pick up a " .. Item.Description .. "." )

			RemoveFromMap( Item )

			return true
		end
	end
end

function Hero:RearrangeInventory( ItemKind )
	for T = 1, 5 do
		if self.Inventory[ ItemKind ][ T ] == nil then
			for I = T, 5 do
				self.Inventory[ ItemKind ][ I ] = self.Inventory[ ItemKind ][ I + 1 ]
			end
		end
	end
end

function Hero:RemoveFromInventory( Item, ItemKind )
	self.Inventory[ ItemKind ][ self:FindInInventory( Item, ItemKind ) ] = nil

	self:RearrangeInventory( ItemKind )
end

function Hero:FindInInventory( Item, ItemKind )
	for I = 1, #self.Inventory[ ItemKind ] do
		if self.Inventory[ ItemKind ][ I ] == Item then return I end
	end

	return nil
end

function Hero:Death()
	if not GameWon then
		SetColor( 255, 255, 0 )

		local YouWin = [[
 **    **                               **            **
//**  **                               //            /**
 //****  ******  **   **    ***     **  **  *******  /**
  //**  **////**/**  /**   //**  * /** /** //**///** /**
   /** /**   /**/**  /**    /** ***/** /**  /**  /** /**
   /** /**   /**/**  /**    /****/**** /**  /**  /** //
   /** //****** //******    ***/ ///** /**  ***  /**  **
   //   //////   //////    ///    ///  //  ///   //  // ]]

		local Y = 150

		for Line in string.gmatch( YouWin, "%C+" ) do
			DrawText( Line, 160, Y )

			Y = Y + 15
		end

		SetColor( 255, 255, 255 )

		DrawText( "You slashed " .. MonsterCount .. " monsters and collected " .. Hero.Gold .. " Gold.", 200, 350 )
		DrawText( "Try again on a different level of difficulty!", 200, 368 )
		DrawText( "Press any key to continue", 270, 420 )
	else
		SetColor( 255, 255, 255 )

		local GraveStone = [[
           ________
          /        \
         /          \
        /   R.I.P    \
       /              \
      /     HERO       \
     /                  \
     |                  |
     |                  |
    *|     *  *      *  |*
____)/\_//(\/(/\/\\/\)\/|(____]]

		local Y = 150
		local Counter = 1

		for Line in string.gmatch( GraveStone, "%C+" ) do
			DrawText( Line, 250, Y )

			if Counter == 8 then
				DrawText( "     | " .. Hero.StartTime .. " |", 250, Y + 15 )
				DrawText( "     |         -        |", 250, Y + 30 )
				DrawText( "     | " .. os.date( "%d.%m.%Y %H:%M" ) .. " |", 250, Y + 45 )

				Y = Y + 45
			elseif Counter == 11 then
				DrawText( "_______________                      ________________", 160, Y )
			end

			Counter = Counter + 1
			Y = Y + 15
		end

		DrawText( "Press any key to continue", 270, Y + 50 )
	end
end
