quest dragon_lair_weekly begin
	state start begin
		
		function set_settings()
			local settings = { }
			-- how long the player has to wait until he can do the quest again
			settings.cooldown_time = 561600 -- 60 seconds * 60 minutes * 24 hours * 6.5 days
			-- how long the player has time to get the items
			settings.time_to_hunt = 7200 -- 2 hours
			
			-- those items should be hunted
			settings.items_to_hunt = {
				30198,
				30199,
				50721,
				50722,
				50723,
				50725,
				50726,
				50727
			}
			-- the amount of items to be hunted. Array field relate to settings.items_to_hunt
			settings.amount_to_hunt = {
				10,
				10,
				50,
				50,
				50,
				50,
				50,
				50
			}
			
			settings.amount_to_pay = 150000
			
			-- Rewards:
			-- how often he gets a normal item instead of a poly marble
			settings.normal_items_rate = 50 -- in percent
			
			-- reward tables follow the rules: {vnum, quantity, mininum level, maximum level}
			
			-- reward items when giving a normal item
			settings.normal_items = {
				{70008, 1}
			}
			
			-- vnum of the poly marble (to show an icon to the player)
			settings.poly_item_vnum = 71093
			
			-- mobs to give in a poly marble
			settings.poly_mobs = {
				{175, 6},
				{184, 6},
				{501, 4},
				{502, 4},
				{552, 2, 85},
				{771, 6},
				{1601, 2, 95},
				{2001, 4},
				{2051, 2, 90},
				{2061, 2, 90},
				{2131, 6},
				{5003, 6}
			}
			
			return settings
		end
		
		when login or enter or levelup with pc.get_level() >= 85 begin
			set_state(getquest)
		end
	end
	
	state getquest begin

		when enter begin
			pc.setqf("item_to_hunt", 0)
			pc.setqf("amount_to_hunt", 0)
		end
	
		-- "Give me polymorph items!"
		when 30122.chat.gameforge.dragon_lair_weekly._010_chat with pc.get_map_index() == 73 begin
			local settings = dragon_lair_weekly.set_settings()
			local time_to_wait = pc.getqf("lasttime") - get_global_time() + settings.cooldown_time
			-- "Ghost of a warrior: "
			say_title(gameforge.dragon_lair_weekly._020_sayTitle)
			if time_to_wait < 0 then 
				-- the player has no cooldown any longer
				local item_to_hunt = pc.getqf("item_to_hunt")
				local amount_to_hunt = pc.getqf("amount_to_hunt")
				if item_to_hunt == 0 then
					local table_number = math.random(table.getn(settings.items_to_hunt))
					item_to_hunt = settings.items_to_hunt[table_number]
					amount_to_hunt = settings.amount_to_hunt[table_number]
					pc.setqf("item_to_hunt", item_to_hunt)
					pc.setqf("amount_to_hunt", amount_to_hunt)
				end
				
				-- "You have to collect %s:"
				say(string.format(gameforge.dragon_lair_weekly._030_say, amount_to_hunt))
				say_item_vnum(item_to_hunt)
				-- "Then I will give you polymorph!"
				say(gameforge.dragon_lair_weekly._040_say)
				-- yes / no
				local accept = select(gameforge.dragon_lair_weekly._050_select, gameforge.dragon_lair_weekly._060_select)
				if accept == 1 then
					set_state(hunt_item)
				else
					say_title(gameforge.dragon_lair_weekly._020_sayTitle)
					-- "You are mean to me!"
					say(gameforge.dragon_lair_weekly._070_say)
				end
			else
				-- player still has cooldown
				-- "You have to wait %s longer!"
				say(string.format(gameforge.dragon_lair_weekly._080_say, get_time_remaining(time_to_wait)))
			end
		end
	end
	
	state hunt_item begin
		
		-- returns the amount of items the player still has to hunt
		function get_amount_remaining()
			return (pc.getqf("amount_to_hunt") - pc.count_item(pc.getqf("item_to_hunt")))
		end
		
		-- returns the time in seconds the player has left collecting the items.
		function get_time_remaining()
			local settings = dragon_lair_weekly.set_settings()
			return (pc.getqf("lasttime") - get_global_time() + settings.time_to_hunt)
		end
		
		when login begin
			-- add a quest icon and timer
			local time_remaining = dragon_lair_weekly.get_time_remaining()
			q.set_clock(gameforge.dragon_lair_weekly._090_questRest, time_remaining) -- timer that will be shown to the player
			timer("dragon_lair_weekly_timer", time_remaining) -- timer that will trigger the failing of this quest 
			if dragon_lair_weekly.get_amount_remaining() > 0 then
				-- "Collect things!"
				send_letter(gameforge.dragon_lair_weekly._100_sendLetter)
				q.set_title(gameforge.dragon_lair_weekly._110_sayTitle)
			else
				-- "Things collected!"
				send_letter(gameforge.dragon_lair_weekly._120_sendLetter)
				q.set_title(gameforge.dragon_lair_weekly._130_sayTitle)
			end
			q.start()
		end
		
		when enter begin
			local settings = dragon_lair_weekly.set_settings()
			local time_remaining = settings.time_to_hunt
			pc.setqf("lasttime", get_global_time()) -- reset the cooldown
			-- "Collect things!"
			send_letter(gameforge.dragon_lair_weekly._100_sendLetter)
			q.set_title(gameforge.dragon_lair_weekly._110_sayTitle)
			q.set_clock(gameforge.dragon_lair_weekly._090_questRest, time_remaining) -- timer that will trigger the failing of this quest 
			timer("dragon_lair_weekly_timer", time_remaining) -- timer that will trigger the failing of this quest 
			q.start()
		end
		
		when info or button begin
			local settings = dragon_lair_weekly.set_settings()
			local amount_remaining = dragon_lair_weekly.get_amount_remaining()
			if (amount_remaining > 0) then
				local time_remaining = dragon_lair_weekly.get_time_remaining()
				if (time_remaining > 0) then
					-- player still has to collect items
					say_title(gameforge.dragon_lair_weekly._110_sayTitle) -- quest title
					-- "You still have to collect %s:"
					say(string.format(gameforge.dragon_lair_weekly._140_say, amount_remaining))
					say_item_vnum(pc.getqf("item_to_hunt"))
					say("")
					say(gameforge.dragon_lair_weekly._090_questRest .. get_time_remaining(time_remaining))
				else
					-- player did not collect the items in time
					
					-- "You failed collecting the items in time!"
					notice_multiline(gameforge.dragon_lair_weekly._160_notice,notice)
					q.done()
					set_state(getquest) -- reset the quest, but the cooldown remains
				end
			else
				say_title(gameforge.dragon_lair_weekly._130_sayTitle) -- quest title
				-- "You have all needed items. Go back to the warrior Ghost! He is in heavens cave 2"
				say(gameforge.dragon_lair_weekly._150_say)
			end
		end
		
		when dragon_lair_weekly_timer.timer begin
			-- the player did not collect the items in time
			
			-- "You failed collecting the items in time!"
			notice_multiline(gameforge.dragon_lair_weekly._160_notice,notice)
			q.done()
			set_state(getquest) -- reset the quest, but the cooldown remains
		end	
		
		
		when 30122.chat.gameforge.dragon_lair_weekly._010_chat with pc.get_map_index() == 73 begin
			local settings = dragon_lair_weekly.set_settings()
			say_title(gameforge.dragon_lair_weekly._020_sayTitle)

			local amount_remaining = dragon_lair_weekly.get_amount_remaining()
			if amount_remaining > 0 then
				-- the player did not collect enough items
				-- "You still have to collect %s:"
				say(string.format(gameforge.dragon_lair_weekly._140_say, amount_remaining))
				say_item_vnum(pc.getqf("item_to_hunt"))
				
				-- "Then I will give you polymorph!"
				say(gameforge.dragon_lair_weekly._040_say)
			elseif pc.money < settings.amount_to_pay then
				-- you don't have enough yang to pay
				say(gameforge.dragon_lair_weekly._175_say)
			else
				-- "You want to give me %s ?: "
				say(string.format(gameforge.dragon_lair_weekly._170_say, pc.getqf("amount_to_hunt")))
				say_item_vnum(pc.getqf("item_to_hunt"))
				-- "Yes" / "No"
				local give_item = select(gameforge.dragon_lair_weekly._180_select, gameforge.dragon_lair_weekly._190_select)
				say_title(gameforge.dragon_lair_weekly._020_sayTitle)
				if (give_item == 1) then
					-- "Thanks, now you get this from me: "
					-- see if a normal item, or a polymorph item should be given
					if math.random(100) < settings.normal_items_rate then -- normal item
						say(gameforge.dragon_lair_weekly._205_sayReward)
						local award_vnum = get_random_vnum_from_table(settings.normal_items)
						say_item_vnum(award_vnum)
						pc.give_item2(award_vnum)
					else -- polymorph item
						say(gameforge.dragon_lair_weekly._200_sayReward)
						local award_vnum = get_random_vnum_from_table(settings.poly_mobs)
						say_item(item_name(settings.poly_item_vnum), settings.poly_item_vnum, mob_name(award_vnum))
						pc.give_poly_marble(award_vnum) -- give a poly morph item with this mob vnum in it
					end

					pc.remove_item(pc.getqf("item_to_hunt"), pc.getqf("amount_to_hunt")) -- remove the questitems from inventory
					-- The sign was missing: this line CREDITED the fee it was meant to
					-- charge, so every hand-over paid the player 150,000 Yang instead
					-- of taking it. The branch above only checks that the player COULD
					-- have afforded it, which is what made this repeatable.
					pc.changemoney(-settings.amount_to_pay)
					q.done()
					set_state(getquest)
				else
					say(gameforge.dragon_lair_weekly._210_say)
				end
			end
		end
	end
end