
quest event_easter_2013 begin
	state start begin
		-- 2013 --
		function spawn_metin(map_index)
			local settings = event_easter_2013.get_settings()
			local level_table = settings.spawn.maps[map_index]
			if level_table == nil then
				return
			end
			table.foreach(level_table, 
				function(index, level)
					for i=0, 5 do
						regen_in_map(map_index, "locale/_master/map/easter/metin_regen_level" .. level .. ".txt")
					end
				end
			)
			mob.spawn(20352, 1, 1, 1, 1, 1)
		end
		function metin_killed(vnum)
			local settings = event_easter_2013.get_settings()
			local level = settings.metins[vnum]
			
			regen_in_map(pc.get_map_index(), "locale/_master/map/easter/metin_regen_level" .. level .. ".txt")
			if level < pc.get_level() - settings.max_level_difference then
				return
			else
				local eggVnum =  math.random(50160, 50179)
				game.drop_item_with_ownership(eggVnum, 1) 
				if math.random(1, 100) <= settings.magicegg.pct then
					game.drop_item_with_ownership(settings.magicegg.vnum, 1) 
				end
			end
		end
	
	
		-- 2012 --
		function is_running()
			return (game.get_event_flag("easter_drop") > 0)
		end
		function has_rabbit()
			return (game.get_event_flag("easter_rabbit") > 0)
		end
		function get_settings()
			local spawnPromille = game.get_event_flag("easter_spawn_chance")
			if spawnPromille <= 0 then
				spawnPromille = 5
			end
			local magicEggPercent = game.get_event_flag("easter_magicegg_chance")
			if magicEggPercent <= 0 then
				magicEggPercent = 5
			end
			return {
				["eggvnums"] = {
					["first"] = 50160,
					["last"] = 50179,
				},
				["baskets"] = {
					["full"] = 50181,
					["empty"] = 50180,
				},
				["magicegg"] = {
					["vnum"] = 71150,
					["pct"] = magicEggPercent,
				},
				["metins"] = {
					[8041] = 10,
					[8042] = 20,
					[8043] = 30,
					[8044] = 40,
					[8045] = 50,
					[8046] = 60,
					[8047] = 70,
					[8048] = 80,
					[8049] = 90,
					[8050] = 95
				},
				["max_level_difference"] = 15,
				["rabbit"] = 30129,
				["spawn"] = {
					["kill"] = 100,
					["cooldown"] = 60, 
					["promille"] = spawnPromille,
					["maps"] = {
						[1] = {10, 20},
						[3] = {20, 30},
						[21] = {10, 20},
						[23] = {20, 30},
						[41] = {10, 20},
						[43] = {20, 30},
						[61] = {50, 60},
						[62] = {50, 60},
						[63] = {40, 50},
						[64] = {30, 40},
						[65] = {40, 50},
						[67] = {60, 70},
						[68] = {70, 80},
						[69] = {60, 70},
						[70] = {70, 80},
						[301] = {90, 95},
						[302] = {90, 95},
						[303] = {90, 95},
						[304] = {90, 95},
					},
				},
			}
		end
		function get_reward_table_for_item()
			-- for each level range there is a different reward
			local playerlevel = pc.get_level()
			if playerlevel <= 18 then
				return {
					{27002, 13},
					{27005, 13},
					{27100, 8},
					{27103, 8},
					{50721, 4},
					{30053, 9, 20},
					{30073, 9, 20},
					{30033, 9, 20},
					{30010, 9, 20},
					{30031, 9, 20},
					{30032, 9, 20}
				}
			elseif playerlevel > 18 and playerlevel <= 30 then
				return {
					{27002, 18},
					{27005, 18},
					{27100, 7},
					{27103, 7},
					{27101, 3},
					{27104, 3},
					{50721, 8},
					{50722, 8},
					{50723, 8},
					{50724, 8},
					{50300, 2, 20},
					{30196, 5, 20},
					{30090, 5, 20}
				}
			elseif playerlevel > 30 and playerlevel <= 52 then
				return {
					{27101, 8},
					{27104, 8},
					{27003, 17},
					{27006, 17},
					{27102, 3},
					{27105, 3},
					{50721, 5},
					{50722, 5},
					{50723, 5},
					{50724, 5},
					{50725, 5},
					{50726, 5},
					{50727, 5},
					{50728, 5},
					{50300, 1, 20},
					{30195, 4, 20},
					{30197, 4, 20},
					{30193, 3, 20}
				} 
			elseif playerlevel > 52 and playerlevel <= 74 then
				return {
					{27003, 14},
					{27006, 14},
					{27102, 9},
					{27105, 9},
					{50721, 5},
					{50722, 5},
					{50723, 5},
					{50724, 5},
					{50725, 5},
					{50726, 5},
					{50727, 5},
					{50728, 5},
					{50300, 1, 20},
					{30198, 4, 20},
					{30199, 4, 20},
					{08005, 3},
				}
			else
				return {
					{27102, 11},
					{27105, 11},
					{50091, 15},
					{50092, 15},
					{50093, 15},
					{50094, 15},
					{50300, 4, 20},
					{50060, 6, 20},
					{08005, 4},
					{30196, 2, 20},
					{30090, 2, 20},
					{30195, 2, 20},
					{30197, 2, 20},
					{30198, 2, 20},
					{30199, 2, 20}
				}
			end
		end
		function get_reward_amount_for_vnum(reward_table, vnum_to_search)
			local amount_needed = 1
			table.foreachi(reward_table, 
				function(index, item)
					local itemVnum = item[1]
					if itemVnum == vnum_to_search then
						if table.getn(item) > 2 then
							amount_needed = item[3]
						else
							amount_needed = 1
						end
						return
					end
				end
			)
			return amount_needed
		end
		function get_number_of_eggs()
			local settings = event_easter_2013.get_settings()
			return count_item_range(settings.eggvnums.first, settings.eggvnums.last) 
		end
		function get_random_reward_vnum(reward_table)
			local temp_table = {}
			local easterEggCount = event_easter_2013.get_number_of_eggs()
			table.foreachi(reward_table, 
				function(index, item)
					local itemProbability = item[2]
					local itemVnum = item[1]
					local meetsAmountLimit = true
					-- check if we need more than one easter egg
					if table.getn(item) > 2 then 
						if easterEggCount < item[3] then
							-- player does not have enough eggs for this item
							meetsAmountLimit = false
						end
					end
					if meetsAmountLimit then
						for amount = 1, itemProbability do
							table.insert(temp_table, itemVnum)
						end
					end
				end
			)
			return temp_table[math.random(table.getn(temp_table))]
		end

		
		when login or enter with event_easter_2013.has_rabbit() begin
			-- spawn rabbit if he is not there
			local settings = event_easter_2013.get_settings()
			local mapIndex = pc.get_map_index()
			if mapIndex == 1 or mapIndex == 21 or mapIndex == 41 then
				if find_npc_by_vnum(settings.rabbit) == 0 then
					if mapIndex == 1 then
						mob.spawn(settings.rabbit, 591, 472, 1, 1, 1)
					elseif mapIndex == 21 then
						mob.spawn(settings.rabbit, 517, 742, 1, 1, 1)
					elseif mapIndex == 41 then
						mob.spawn(settings.rabbit, 306, 828, 1, 1, 1)
					end
				end
			end
			if find_npc_by_vnum(20352) == 0 then
				event_easter_2013.spawn_metin(pc.get_map_index())
			end
		end
		
		-- 2013 -- 
		-- when login or enter with event_easter_2013.is_running() begin
			-- if is_test_server() then
				-- notice("test1")
			-- end
			-- if find_npc_by_vnum(20352) == 0 then
				-- event_easter_2013.spawn_metin(pc.get_map_index())
				-- if is_test_server() then
					-- notice("test2")
				-- end
			-- end
		-- end

		-- "Was machst du hier?"
		when 30129.chat.gameforge.main_quest_lv2._10_npcChat with event_easter_2013.get_number_of_eggs() == 0 begin
			say_title(gameforge.event_easter._020_sayTitle) -- "Osterhase: "
			say(gameforge.event_easter._030_say) -- "Ich verstecke Ostereier. Wenn du welche findest und mir zur?k bringst, belohne ich dich toll!"
			say(gameforge.event_easter._035_say)
			if not event_easter_2013.has_rabbit() then
				-- purge the rabbit if the event has finished
				npc.purge()
			end
		end		--when
		
		when 30129.chat.gameforge.event_easter._040_chat with event_easter_2013.get_number_of_eggs() > 0 begin
			local settings = event_easter_2013.get_settings()
			local reward_table = event_easter_2013.get_reward_table_for_item()
			local amount_to_remove = 0
			local amount_to_give = 0
			local vnum_to_give = 0
			local number_of_eggs = event_easter_2013.get_number_of_eggs()
			local wants_items = 2
			while wants_items == 2 do
				say_title(gameforge.event_easter._020_sayTitle) -- "Osterhase: "
				vnum_to_give = event_easter_2013.get_random_reward_vnum(reward_table)
				local amount_needed = event_easter_2013.get_reward_amount_for_vnum(reward_table, vnum_to_give)
				if amount_needed == 20 then
					amount_to_remove = 20
					amount_to_give = 1
					say(gameforge.event_easter._060_say)
					say(string.format(gameforge.event_easter._065_say, amount_to_remove)) -- "Daf? musst du mir %d Eier geben! M?htest du das tun?"

					
					say(string.format(gameforge.event_easter._050_say, amount_to_give)) -- "Ich gebe dir %d hiervon:"
					say_item_vnum(vnum_to_give)
					

					say(gameforge.event_easter._100_say)
				else
					if number_of_eggs > 20 then
						amount_to_remove = 20
					else
						amount_to_remove = number_of_eggs
					end
					if vnum_to_give < 27200 then
						amount_to_give = amount_to_remove * 3
					else
						amount_to_give = amount_to_remove * 5
					end
					say(gameforge.event_easter._060_say)
					say(string.format(gameforge.event_easter._055_say, event_easter_2013.get_number_of_eggs()) )

					say(string.format(gameforge.event_easter._050_say, amount_to_give)) 
					say_item_vnum(vnum_to_give)
					

					say(gameforge.event_easter._100_say)
				end
				end
				
			if (remove_item_range(amount_to_remove, settings.eggvnums.first, settings.eggvnums.last)) then
				pc.give_item2(vnum_to_give, amount_to_give)
			end
			say_title(gameforge.event_easter._020_sayTitle) -- "Osterhase: "
			say(gameforge.event_easter._080_say) -- "Danke f? die Eier, ich hoffe du hast Spa?mit dem was du bekommen hast"
			say("")	
			if not event_easter_2013.has_rabbit() then
				-- purge the rabbit if the event has finished
				npc.purge()
			end
		end  --when
		when 8041.kill begin
			event_easter_2013.metin_killed(8041)
		end
		when 8042.kill begin
			event_easter_2013.metin_killed(8042)
		end
		when 8043.kill begin
			event_easter_2013.metin_killed(8043)
		end
		when 8044.kill begin
			event_easter_2013.metin_killed(8044)
		end
		when 8045.kill begin
			event_easter_2013.metin_killed(8045)
		end
		when 8046.kill begin
			event_easter_2013.metin_killed(8046)
		end
		when 8047.kill begin
			event_easter_2013.metin_killed(8047)
		end
		when 8048.kill begin
			event_easter_2013.metin_killed(8048)
		end
		when 8049.kill begin
			event_easter_2013.metin_killed(8049)
		end
		when 8050.kill begin
			event_easter_2013.metin_killed(8050)
		end

	end  --state
end   --quest
