cutsceneload = nil
map16 = nil
multiplayermenuload = nil
credits = nil
gameload = nil
gameover = nil
collectgarbage()

map16,message = loadfile("map.lua")
if map16 == nil then
screen:clear()
screen:print(0,0,"LOAD FAILED",Color.new(255,0,0))
screen:print(0,10,message,Color.new(255,255,255))
screen.flip()
end

gameover,message = loadfile("gameover.lua")
if gameover == nil then
screen:clear()
screen:print(0,0,"LOAD FAILED",Color.new(255,0,0))
screen:print(0,10,message,Color.new(255,255,255))
screen.flip()
end

-----------------**Collision Check**---------

function detectCollision(player, opponent)

if (player.x + character:width() > opponent.x) and (player.x  < opponent.x + opponent.img:width()) and (player.y + character:height() > opponent.y) and (player.y < opponent.y + opponent.img:height())  then
player.x = oldx
player.y = oldy

if pad:cross() then
	select:play()
 	screen:blit(0, 199, box1) 
	screen:blit(0,199, zeldap)
	screen:blit(390,199, linkenp)
	screen:print(100, 220, "Linken, You have awoken...", black)
	screen:print(100, 240, "Go outside and begin your quest", black)
  	screen.flip()
	screen.waitVblankStart(250)	
	end
end
end

	skullguydead1 = false
	skullguydeada = false
	skullguydeadb = false
	skullguydeadc = false
	skullguydeadd = false
	bossdead = false
	playercollectsword = false
	secretroom = true
	bosshp = 10
	bombcounter = 0
	useoncea = 0 
	useonceb = 0 
	useoncec = 0 
	useonced = 0 
	useoncee = 0 
	a2timer = 0
	
	status = "game"

------------**init some colors**-----

	blue = Color.new(0 , 153, 255)
	black = Color.new(255,255,255)
	orange = Color.new(255, 102, 0)

---------**backgrounds and title screens**-----

	backgroundCache = Image.load("backgrounds/background.png")
    shopmenu = Image.load("backgrounds/shopmenu.png")
	soon = Image.load("backgrounds/soon.png")
 
--------Right sprites---------

	player1 = Image.load("sprites/right1.png")
	player2 = Image.load("sprites/right2.png")
	player3 = Image.load("sprites/right3.png")

---------left sprites--------

	player4 = Image.load("sprites/left1.png")
	player5 = Image.load("sprites/left2.png")
	player6 = Image.load("sprites/left3.png")

-------Up sprites-------------

	player7 = Image.load("sprites/up1.png")
	player8 = Image.load("sprites/up2.png")
	player9 = Image.load("sprites/up3.png")
       	
-------Down sprites------------

	player10 = Image.load("sprites/down1.png")
	player11 = Image.load("sprites/down2.png")
	player12 = Image.load("sprites/down3.png")
	
------**Attacking Sprites**----------

	upatt1 = Image.load("sprites/uatt1.png")
	downatt1 = Image.load("sprites/datt1.png")
	leftatt1 = Image.load("sprites/latt1.png")
	rightatt1 = Image.load("sprites/ratt1.png")

-------**Townspeople**----

	zelda = Image.load("townspeople/zelda.png")
	zeldap = Image.load("townspeople/zeldaport.png")
	linkenp = Image.load("townspeople/linkport.png")
	shopp = Image.load("townspeople/shopport.png")
	childp = Image.load("townspeople/childport.png")
	child2p = Image.load("townspeople/child2port.png")
	child1 = Image.load("townspeople/child1.png")
	child2 = Image.load("townspeople/child2.png")
	hylian1 = Image.load("townspeople/hylian1.png")
	hylian2 = Image.load("townspeople/hylian2.png")
	hylian3 = Image.load("townspeople/hylian1.png")
	postman = Image.load("townspeople/postman.png")
	shopkeeper1 = Image.load("townspeople/shopkeeper1.png")
	shopkeeper2 = Image.load("townspeople/shopkeeper2.png")
	shopkeeper3 = Image.load("townspeople/shopkeeper3.png")
	soccerkid1 = Image.load("townspeople/skr1.png")
	soccerkid2 = Image.load("townspeople/skr2.png")
	soccerkid3 = Image.load("townspeople/skl1.png")
	soccerkid4 = Image.load("townspeople/skl2.png")
	ball = Image.load("objects/ball.png")
	wiseguy1 = Image.load("townspeople/wgu1.png")
	wiseguy2 = Image.load("townspeople/wgu2.png")
	wiseguy3 = Image.load("townspeople/wgu3.png")
	wiseguy4 = Image.load("townspeople/wgd1.png")
	wiseguy5 = Image.load("townspeople/wgd2.png")
	wiseguy6 = Image.load("townspeople/wgd3.png")
			   
-------**objects**-----

	pressx = Image.load("objects/pressx.png")
	box1 = Image.load("objects/box1.png")
	rupeess = Image.load("objects/rupee.png")
 	triangle = Image.load("objects/triangle.png")
	circle = Image.load("objects/circle.png")
	square = Image.load("objects/square.png")
	cross = Image.load("objects/cross.png")
	bomb =  Image.load("objects/bomb.png")
	sword = Image.load("objects/sword.png")
	container = Image.load("objects/container.png")
	maja = Image.load("objects/maja.png")
	rupeess = Image.load("objects/Rupee.png")
	rupeeA = Image.load("objects/rupee2-1.png")
	rupeeB = Image.load("objects/rupee2-1.png")
	rupeeC = Image.load("objects/rupee2-1.png")
	rupeeD = Image.load("objects/rupee2-1.png")	
	rupeeA1 = Image.load("objects/rupee2-1.png")
	rupeeB1 = Image.load("objects/rupee2-1.png")
	rupeeC1 = Image.load("objects/rupee2-1.png")
	rupeeD1 = Image.load("objects/rupee2-1.png")	
	rupee2A = Image.load("objects/rupee2-1.png")
	rupee3A = Image.load("objects/rupee2-2.png")
	rupee4A = Image.load("objects/rupee2-3.png")
	rupee2B = Image.load("objects/rupee2-1.png")
	rupee3B = Image.load("objects/rupee2-2.png")
	rupee4B = Image.load("objects/rupee2-3.png")
	rupee2C = Image.load("objects/rupee2-1.png")
	rupee3C = Image.load("objects/rupee2-2.png")
	rupee4C = Image.load("objects/rupee2-3.png")
	rupee2D = Image.load("objects/rupee2-1.png")
	rupee3D = Image.load("objects/rupee2-2.png")
	rupee4D = Image.load("objects/rupee2-3.png")
	rupee2A1 = Image.load("objects/rupee2-1.png")
	rupee3A1 = Image.load("objects/rupee2-2.png")
	rupee4A1 = Image.load("objects/rupee2-3.png")
	rupee2B1 = Image.load("objects/rupee2-1.png")
	rupee3B1 = Image.load("objects/rupee2-2.png")
	rupee4B1 = Image.load("objects/rupee2-3.png")
	rupee2C1 = Image.load("objects/rupee2-1.png")
	rupee3C1 = Image.load("objects/rupee2-2.png")
	rupee4C1 = Image.load("objects/rupee2-3.png")
	rupee2D1 = Image.load("objects/rupee2-1.png")
	rupee3D1 = Image.load("objects/rupee2-2.png")
	rupee4D1 = Image.load("objects/rupee2-3.png")
	heart1 = Image.load("objects/h2.png")
	heart2 = Image.load("objects/h2.png")
	heart3 = Image.load("objects/h2.png")
	chicken1 = Image.load("animals/chknd1.png")
	chicken2 = Image.load("animals/chknd2.png")
	chicken3 = Image.load("animals/chknd3.png")
	chicken4 = Image.load("animals/chknu1.png")
	chicken5 = Image.load("animals/chknu2.png")
	chicken6 = Image.load("animals/chknu3.png")
	
----------**Sound Loading**----

	att = Sound.load("music/att.wav", false)
	chest = Sound.load("music/chest.wav", false)
	select = Sound.load("music/select.wav", false)
	kill = Sound.load("music/enemykill.wav", false)
	chickens = Sound.load("music/chicken.wav", false)
	rupeesound = Sound.load("music/rupee.wav", false)
	tresure = Sound.load("music/tresure.wav", false)
	explode = Sound.load("music/explode.wav", false)
	hurt = Sound.load("music/hurt.wav", false)
	
-----------**Walls**---------
         	 
	wall1 = Image.createEmpty(14,135) -----Top Left Hand Wall
	wall2 = Image.createEmpty(14,100) -----Bottom Left Hand Wall
	wall3 = Image.createEmpty(452,27) -----Top Wall
	wall4 = Image.createEmpty(40,70)  -----Middle Wall - Vertical
	wall5 = Image.createEmpty(30,38)  -----Middle Wall - Stone Block
	wall6 = Image.createEmpty(128,50) -----Middle Wall - Horizontal
	wall7 = Image.createEmpty(3,1)    -----Middle Wall - Right Pillar
	wall8 = Image.createEmpty(47,1)   -----Middle Door/Wall - Near bed
	wall9 = Image.createEmpty(14,120) -----Right Wall - Bottom
	wall10 = Image.createEmpty(74,19) -----Bottom Wall - Right Side
	wall11 = Image.createEmpty(283,19) ----Bottom Wall - Left Side
		
----------**Object Collision**---

	wall12 = Image.createEmpty(38,40) ------Table
	wall13 = Image.createEmpty(10,20) ------Chairs  
	wall14 = Image.createEmpty(30,1)  ------Stone Brick - Top Left
	wall15 = Image.createEmpty(30,2)  ------Fireplace? Top Middle
	wall16 = Image.createEmpty(8,2)   ------Box - Top Middle
	wall17 = Image.createEmpty(2,2)   ------Statue - Top Right
	wall18 = Image.createEmpty(43,1)  ------Table - Bottom Left
	wall19 = Image.createEmpty(43,1)  ------Table - Bottom Right
	wall20 = Image.createEmpty(10,1)  ------Chair - Bottom Left
	wall21 = Image.createEmpty(10,1)  ------Chair - Bottom Right
	wall22 = Image.createEmpty(17,16) ------Well? - Middle Next To Pillar
	  
------------House Walls------
	   
   	wall1 = {x = 2, y = 6, height = wall1:height(), width = wall1:width() }
   	wall2 = {x = 2, y = 172, height = wall2:height(), width = wall2:width() } 
   	wall3 = {x = 3, y = 6, height = wall3:height(), width = wall3:width() }
   	wall4 = {x = 230, y = 29, height = wall4:height(), width = wall4:width() }
   	wall5 = {x = 225, y = 116, height = wall5:height(), width = wall5:width() }
   	wall6 = {x = 270, y = 98, height = wall6:height(), width = wall6:width() }
   	wall7 = {x = 394, y = 155, height = wall7:height(), width = wall7:width() } 
   	wall8 = {x = 399, y = 130, height = wall8:height(), width = wall8:width() }
   	wall9 = {x = 464, y = 130, height = wall9:height(), width = wall9:width() }
   	wall10 = {x = 366, y = 249, height = wall10:height(), width = wall10:width() }
   	wall11 = {x = 2, y = 249, height = wall11:height(), width = wall11:width() }
   	
----------Object walls-------   	
      
	wall12 = {x = 107, y = 79, height = wall12:height(), width = wall12:width() }
   	wall13 = {x = 150, y = 90, height = wall13:height(), width = wall13:width() }
   	wall14 = {x = 50, y = 41, height = wall14:height(), width = wall14:width() }
   	wall15 = {x = 104, y = 40, height = wall15:height(), width = wall15:width() }
   	wall16 = {x = 161, y = 41, height = wall16:height(), width = wall16:width() } 
   	wall17 = {x = 196, y = 40, height = wall17:height(), width = wall17:width() }
   	wall18 = {x = 86, y = 238, height = wall18:height(), width = wall18:width() }
   	wall19 = {x = 170, y = 238, height = wall19:height(), width = wall19:width() }
   	wall20 = {x = 106, y = 230, height = wall20:height(), width = wall20:width() }
   	wall21 = {x = 181, y = 230, height = wall21:height(), width = wall21:width() }
   	wall22 = {x = 281, y = 167, height = wall22:height(), width = wall22:width() }                   

--------**player values**---------

	player = {}
	player.x = 420
	player.y = 140
	playerHeight = 20
	playerWidth = 25

	player.img = player10

	character = zelda
	
	zelda1 = {}
	zelda1.x = 3
	zelda1.y = 132
	zelda1.img = zelda
		
-----------------------Rupee counter-------------

RupeeCounter = 0

RupeeTable = {1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,
68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100} ---Rupee Table

---------------**character values**----

-----------**Timer init**--------

	counter = Timer.new()
	counter:start()
    oldTime = counter:time()
    
---------**timer values**--------
    
    Timer = 0
	RupeeTimer1 = 0
	RupeeTimer2 = 0
	RupeeTimer3 = 0
	RupeeTimer4 = 0
	heartsc = 80
	
	--------attacking-------
	
		function attackingr()
	
		if pad:right() and oldpad:square() ~= pad:square() and playercollectsword == true then
		player.img = rightatt1
		end
	end
	
	function attackingl()
		
		if pad:left() and oldpad:square() ~= pad:square() and playercollectsword == true then
		player.img = leftatt1
		end
	end
	
		function attackingu()
	
		if pad:up() and oldpad:square() ~= pad:square() and playercollectsword == true then
		player.img = upatt1
		end
	end
	
	function attackingd()
		
		if pad:down() and oldpad:square() ~= pad:square() and playercollectsword == true then
		player.img = downatt1
		end
	end
		
-------------**player sprite loading & Timer values**--------
   
function moveRight()   ------Right Animation
direction = right
Timer = Timer + 1
  
   
 	if Timer>= 20 or Timer<=0  then
    Timer = 0
  	end         
			                 
        if Timer==5 then
        player.img = player1
        end
        
        if Timer==10 then
        player.img = player2
        end
        
		if Timer==15 then
		player.img = player3
		counter:reset()
		end
 end

function  moveLeft()   ------Left Animation
direction = left
  Timer = Timer + 1

	if Timer>= 20 or Timer<=0 then
	    Timer = 0
	    end
	           	            			    
		if Timer==5 then
		player.img = player4
		end
		
		if Timer==10 then
		player.img = player5
		end
		
		if Timer==15 then
		player.img = player6
		counter:reset()
		end
 end

function moveUp()    ------Up Animation
direction = up
   Timer  = Timer + 1
   
        if Timer>= 20 or Timer <= 0 then
        Timer = 0
		end
		
		if Timer==5 then
		player.img = player7
		end
		
		if Timer==10 then
		player.img  = player8
		end
		
		if Timer==15 then
		player.img = player9
		counter:reset()
		end
end

function moveDown()  ------Down Animation
direction = down
   Timer = Timer + 1
   
        if Timer>= 20 or Timer<=0 then
        Timer = 0
        end
        
        if Timer==5 then
        player.img = player10
        end
        
        if Timer==10 then
        player.img = player11
        end
        
        if Timer==15 then
		player.img = player12
		counter:reset()
		end
 end
 
 ----------------**Rupee Animation**-----

function Ranimation1()
RupeeTimer1 = RupeeTimer1 + 1

	if RupeeTimer1>=35 or RupeeTimer1<=0  then
    RupeeTimer1 = 0
  	end
	
	if RupeeTimer1 ==11 then 
	rupeeA = rupee2A
	end
	
	if RupeeTimer1 ==22 then 
	rupeeA = rupee3A
	end
	
	if RupeeTimer1 ==33 then
	rupeeA = rupee4A
	end
end

function Ranimation2()
RupeeTimer2 = RupeeTimer2 + 1

	if RupeeTimer2>=35 or RupeeTimer2<=0  then
    RupeeTimer2 = 0
  	end
	
	if RupeeTimer2 ==11 then 
	rupeeB = rupee2B
	end
	
	if RupeeTimer2 ==22 then 
	rupeeB = rupee3B
	end
	
	if RupeeTimer2 ==33 then
	rupeeB = rupee4B
	end
end

function Ranimation3()
RupeeTimer3 = RupeeTimer3 + 1

	if RupeeTimer3>=35 or RupeeTimer3<=0  then
    RupeeTimer3 = 0
  	end
	
	if RupeeTimer3 ==11 then 
	rupeeC = rupee2C
	end
	
	if RupeeTimer3 ==22 then 
	rupeeC = rupee3C
	end
	
	if RupeeTimer3 ==33 then
	rupeeC = rupee4C
	end
end

function Ranimation4()
RupeeTimer4 = RupeeTimer4 + 1

	if RupeeTimer4>=35 or RupeeTimer4<=0  then
    RupeeTimer4 = 0
  	end
	
	if RupeeTimer4 ==11 then 
	rupeeD = rupee2D
	end
	
	if RupeeTimer4 ==22 then 
	rupeeD = rupee3D
	end
	
	if RupeeTimer4 ==33 then
	rupeeD = rupee4D
	end
end
	
--------------**map loading**------

function maps()   ------Map 1 (Outside House)
 
	if player.x >=270 and player.x <=359 and
	player.y >=263 and player.y <=272 then
    player.x = 306
	player.y = 86
	player.img = player10
	Music.playFile("music/ingmusic2.it", true)
    map16()
	end 
end 

---------------**collision**---------

function collisionCheck(test)
	
	if (player.x + playerWidth > test.x) and (player.x < test.x + test.width) and
	(player.y + playerHeight > test.y) and (player.y < test.y + test.height) then
	
	player.x = oldx
	player.y = oldy
	end
end

----------**music level 1**------

	Music.playFile("music/ingmusic1.it", true)  ----First House Music

--------rupees------------

	function rupeesg()

if player.x >=134 and player.x <=158 and player.y >=125 and player.y <=170 and
	RupeeTable[1] then
		rupeesound:play()
		RupeeTable[1] = false
		rupeeA:clear()
		rupee2A:clear()
		rupee3A:clear()
		rupee4A:clear()	
		RupeeCounter = RupeeCounter + 1 
			
		elseif player.x >=121 and player.x <=135 and player.y >=125 and player.y <=170 and
	RupeeTable[2] then
		rupeesound:play()
		RupeeTable[2] = false
		rupeeB:clear()
		rupee2B:clear()
		rupee3B:clear()
		rupee4B:clear()
		RupeeCounter = RupeeCounter + 1 
		
		elseif player.x >=96 and player.x <=110 and player.y >=125 and player.y <=170 and
	RupeeTable[3] then
		rupeesound:play()
		RupeeTable[3] = false
		rupeeC:clear()
		rupee2C:clear()
		rupee3C:clear()
		rupee4C:clear()
		RupeeCounter = RupeeCounter + 1 
		
		elseif player.x >=71 and player.x <=85 and player.y >=125 and player.y <=170 and
	RupeeTable[4] then
		rupeesound:play()
		RupeeTable[4] = false
		rupeeD:clear()
		rupee2D:clear()
		rupee3D:clear()
		rupee4D:clear()
		RupeeCounter = RupeeCounter + 1 
	end
end

function loose()
	if heartsc ==19 then 
	gameover()
	end
end 

oldpad = Controls.read()
	
--------**Start Main Game Loop**-----

     while true do
	 	 
-------**other values**------

		     
-------**load Map**---------

    maps()
	rupeesg()
	Ranimation1()
	Ranimation2()
	Ranimation3()
	Ranimation4()
	loose()
	      
-------**store players position at beginning of each loop**----

	oldx = player.x
	oldy = player.y
	screen:clear()
	
---------**init gamepad**-------

	pad = Controls.read()
	
	attackingr()
	attackingl()
	attackingu()
	attackingd()

---------**player movement**---

	if pad:right() then 	-----Right Movement
	direction = right
	moveRight()
	player.x = player.x + 2
 	end
	 
	if pad:left() then        -----Left Movement
	direction = left
	moveLeft()
	player.x = player.x - 2
  	end
	
	if pad:up() then         -----Up Movement
	direction = up
	moveUp()
 	player.y = player.y - 2
	end
	
	if pad:down() then 	-----Down Movement
	direction = down
	moveDown()
 	player.y = player.y + 2
	end
	
------------**Other**--------

 	if pad:select() then screen:save("screenshot.png") end  ----Take Screenshot 
	
------**check collision for wall**-----
    
	if status == "game" then
	
	collisionCheck(wall1)
	collisionCheck(wall2)
	collisionCheck(wall3)
	collisionCheck(wall4)
	collisionCheck(wall5)
	collisionCheck(wall6)
	collisionCheck(wall7)
	collisionCheck(wall8)
	collisionCheck(wall9)
	collisionCheck(wall10)
	collisionCheck(wall11)
	collisionCheck(wall12)
	collisionCheck(wall13)
	collisionCheck(wall14)
	collisionCheck(wall15)
	collisionCheck(wall16)
	collisionCheck(wall17)
	collisionCheck(wall18)
	collisionCheck(wall19)
	collisionCheck(wall20)
	collisionCheck(wall21)
	collisionCheck(wall22)
	end
	                             
-------**blit the background & player**------

	screen:blit(0, 0, backgroundCache)
 	screen:blit(player.x, player.y, player.img) 
	screen:blit(zelda1.x, zelda1.y, zelda1.img)
   	screen:blit(400, 10, container)
 	screen:blit(417, 13, triangle)
 	screen:blit(432, 30, circle)
   	screen:blit(403, 30, square)
   	screen:blit(417, 45, cross)
 	screen:blit(404, 32, sword)
 	screen:blit(420, 15, bomb)
 	screen:blit(420, 49, maja)
 	screen:blit(240, 0, rupeess) 
	screen:blit(150, 150, rupeeA)
	screen:blit(125, 150, rupeeB)
	screen:blit(100, 150, rupeeC)
	screen:blit(75, 150, rupeeD)
	screen:blit(0, 0, heart1)
	screen:blit(30, 0, heart2)
	screen:blit(60, 0, heart3)
	screen:print(280,10, RupeeCounter, orange)
   	screen.flip()

   detectCollision(player, zelda1)

-------**end loop**-----
               
	screen.waitVblankStart()  
	oldpad = pad
end
