-- emb цепочка невидимых тайников. Я знаю, что номера в начале лишние, сделано для удобочитаемости и отладки - не удалять.

local embs = {
	-- Темная Долина
	[1] = {
		rukzak = {section = "emb_inventory_box_td1", position={x=94.705299377441,y=6.8894472122192,z=-68.985610046387},gv=992,lv=290142, sound = "snp\\kapanie_vodi25", location = "l04_darkvalley"},
		info = "embt_td1_done"
	},
	[2] = {
		rukzak = {section = "emb_inventory_box_td2", position={x=132.84004211426,y=12.454592704773,z=-180.78562011719},gv=926,lv=328905, sound = "snp\\golubi25", location = "l04_darkvalley"},
		note = "emb_note1"
	},
	[3] = {
		rukzak = {section = "emb_inventory_box_td3", position={x=15.324894332886,y=-2.9804856777191,z=-290.08323730469},gv=842,lv=194882, sound = "snp\\vorona30", location = "l04_darkvalley"},
		note = "emb_note2"
	},
	[4] = {
		rukzak = {section = "emb_inventory_box_td4", position={x=59.029266357422,y=7.9867372512817,z=-54.53861618042},gv=991,lv=246925, sound = "snp\\radiopomehi25", location = "l04_darkvalley"},
		note = "emb_note3"
	},

	-- Восточная Припять
	[5] = {
		rukzak = {section = "emb_inventory_box_vp1", position={x=134.16,y=4.36,z=-42.87},gv=3767,lv=362197, sound = "snp\\monstr40", location = "pripyat"},
		note = "emb_note4"
	},
	[6] = {
		rukzak = {section = "emb_inventory_box_vp2", position={x=26.556156158447,y=3.8782942295074,z=-257.77416992188},gv=3756,lv=231471, sound = "snp\\dogd1_25", location = "pripyat"},
		note = "emb_note5"
	},
	[7] = {
		rukzak = {section = "emb_inventory_box_vp3", position={x=187.97006225586,y=-0.50561583042145,z=-208.50059509277},gv=3765,lv=418356, sound = "snp\\dvigatel25", location = "pripyat"},
		note = "emb_note6"
	},
	[8] = {
		rukzak = {section = "emb_inventory_box_vp4", position={x=-123.32452392578,y=4.5589375495911,z=-145.15596008301},gv=3752,lv=73264, sound = "snp\\dogd2_25", location = "pripyat"},
		note = "emb_note7"
	},
	
	-- Агропром
	[9] = {
		restrictor = {radius = 1, func = "emb.f1", position={x=-47.218143463135,y=0.43208029866219,z=-33.43550491333},gv=654,lv=194127, sound = "snp\\muxi25", location = "l03_agroprom"},
		note = "emb_note8"
	},
	[10] = {
		teleport = {section = "emb_tp1", position={x=-17.896926879883,y=8.0806503295898,z=44.693161010742},gv=471,lv=224908}
	},
	[11] = {
		rukzak = {section = "emb_inventory_box_agro1", position={x=-44.724803924561,y=-6.4586811065674,z=-35.975868225098},gv=654,lv=194127}
	},
	[12] = {
		teleport = {section = "emb_tp2", position={x=-44.58158493042,y=-47.682334899902,z=-43.689117431641},gv=654,lv=194127}
	},
	[13] = {
		rukzak = {section = "emb_inventory_box_agro2", position={x=-80.469635009766,y=11.983932495117,z=-113.33744049072},gv=698,lv=159485},
		note = "emb_note9"
	},
	[14] = {
		teleport = {section = "emb_tp3", position={x=-75.222267150879,y=-49.906688690186,z=-114.86611938477},gv=543,lv=173013}
	},
	[15] = {
		rukzak = {section = "emb_inventory_box_agro3", position={x=-183.84883117676,y=9.3691387176514,z=-127.19804382324},gv=446,lv=51156, sound = "snp\\rik25", location = "l03_agroprom"},
		note = "emb_note10"
	},

	-- Генераторы
	[16] = {
		rukzak = {section = "emb_inventory_box_gen1", position={x=-40.98,y=39.25,z=-440.20},gv=3101,lv=285983, sound = "snp\\chaika25", location = "generators"},
		note = "emb_note11"
	},
	[17] = {
		rukzak = {section = "emb_inventory_box_gen2", position={x=265.76,y=14.87,z=-88.56},gv=3120,lv=645001},
		note = "emb_note12"
        },
	[18] = {
		teleport = {section = "emb_tp4", position= "actor"}
	},
	[19] = {
		teleport = {section = "emb_tp5", position={x=330.68,y=9.13,z=-65.64},gv=3120,lv=724594}
	},
	[20] = {
		rukzak = {section = "emb_inventory_box_gen3", position={x=74.52,y=25.70,z=-47.08},gv=3120,lv=424998},
	        note = "emb_note13"
	},
	[21] = {
		teleport = {section = "emb_tp6", position= "actor"}
	},
	[22] = {
		rukzak = {section = "emb_inventory_box_gen4", position={x=13.53,y=46.75,z=-174.77},gv=3112,lv=353948, sound = "snp\\datel25", location = "generators"},
		note = "emb_note14"
	},
	[23] = {
		rukzak = {section = "emb_inventory_box_gen5", position={x=-123.58,y=33.29,z=-502.17},gv=3096,lv=191317, sound = "snp\\waterfall25", location = "generators"},
		note = "emb_note15"
	},
	[24] = {
		teleport = {section = "emb_tp7", position = "actor"}
	},

	-- Красный Лес
	[25] = {
		rukzak = {section = "emb_inventory_box_kl1", position={x=0.74603170156479,y=-1.065274477005,z=-61.155601501465},gv=3287,lv=68190, sound = "snp\\sverchok25", location = "red_forest"},
		note = "emb_note16"
	},
	[26] = {
		rukzak = {section = "emb_inventory_box_kl2", position={x=26.922819137573,y=26.285343170166,z=19.268026351929},gv=3263,lv=83040},
		note = "emb_note17"
	},

	-- Армейские Склады
	[27] = {
		rukzak = {section = "emb_inventory_box_as1", position={x=-22.357492446899,y=-0.56715404987335,z=-17.154308319092},gv=1593,lv=285457, sound = "snp\\schebet1_25", location = "l07_military"},
		note = "emb_note18"
	},
	
	-- Центральная Припять
	[28] = {
		rukzak = {section = "emb_inventory_box_cp1", position={x=143.46368408203,y=8.3857183456421,z=-28.497867584229},gv=2242,lv=241842, sound = "snp\\schebet2_25", location = "l11_pripyat"},
		note = "emb_note19"
	},
	[29] = {
		rukzak = {section = "emb_inventory_box_cp2", position={x=132.29019165039,y=-0.92885887622833,z=-35.088806152344},gv=2230,lv=233988},
		note = "emb_note20"
	},
	[30] = {
		rukzak = {section = "emb_inventory_box_cp3", position={x=132.17375183105,y=3.9322052001953,z=-38.824993133545},gv=2241,lv=233983},
		note = "emb_note21"
	},

	-- Саркофаг
	[31] = {
		rukzak = {section = "emb_inventory_box_sarcofag1", position={x=-24.229337692261,y=3.4016950130463,z=43.089736938477},gv=2466,lv=120, sound = "snp\\vertolet25", location = "l12u_sarcofag"},
		note = "emb_note22"
	},
	[32] = {
		teleport = {section = "emb_tp8", position={x=-31.395652770996,y=19.466318130493,z=39.390933990479},gv=2419,lv=16}
	},
	[33] = {
		rukzak = {section = "emb_inventory_box_sarcofag2", position={x=-29.330762863159,y=13.072887420654,z=50.847515106201},gv=2403,lv=96},
		note = "emb_note23"
	},
	[34] = {
		teleport = {section = "emb_tp9", position="actor"}
	},

	-- Радар
	[35] = {
		rukzak = {section = "emb_inventory_box_radar1", position={x=611.76867675781,y=-42.561958312988,z=-321.46279907227},gv=1881,lv=220300, sound = "snp\\skrip_lampi25", location = "l10_radar"},
		note = "emb_note24"
	},
	[36] = {
		rukzak = {section = "emb_inventory_box_radar2", position={x=34.375961303711,y=-3.3735945224762,z=-37.291954040527},gv=1997,lv=21736, sound = "snp\\telefon25", location = "l10_radar"},
		note = "emb_note25",
		info = "embt_sak1_start"
	},
	[37] = { }, -- заглушка для диалога
	[38] = {
		rukzak = {section = "emb_inventory_box_radar3", position={x=529.87585449219,y=-56.362426757813,z=-463.63214111328},gv=1868,lv=201672, sound = "snp\\vorobey75", location = "l10_radar"},
		note = "emb_note26",
		info = "embt_sak1_done"
	},
	[39] = {
		teleport = {section = "emb_tp10", position={x=526.91674804688,y=-102.29163360596,z=-463.58129882813},gv=1868,lv=201672}
	},
	[40] = {
		teleport = {section = "emb_tp11", position={x=584.76989746094,y=-37.039344787598,z=-7.4732613563538},gv=1900,lv=207465, sound = "snp\\tiktak25", location = "l10_radar"},
		note = "emb_note27",
		func = "snp.play_snd_rnd_ratchant()"
		},
	[41] = {
		rukzak = {section = "emb_inventory_box_radar4", position={x=640.78607177734,y=-24.565687179565,z=174.59934997559},gv=1902,lv=194550},
		info = "emb_done",
		func = "emb.f2()"
	}
}

-- Обработчик. При каждом вызове увеличивает индекс таблицы. Параметр передавать только для отладки конкретного объекта
function worker(index)
	local idx = get_value("emb_idx", 0)
	if index then
		idx = index
	end

	local obj
	
	-- удаляем текущий объект
	if embs[idx] then
	
		-- выключаем звук рюкзака
		if embs[idx].rukzak and embs[idx].rukzak.sound then
			stop_sound(embs[idx].rukzak.sound)
		end
		
		-- выключаем звук рестриктора
		if embs[idx].restrictor and embs[idx].restrictor.sound then
			stop_sound(embs[idx].restrictor.sound)
		end

		-- выключаем звук телепорта
		if embs[idx].teleport and embs[idx].teleport.sound then
			stop_sound(embs[idx].teleport.sound)
		end

		-- удаляем телепорт
		if embs[idx].teleport then
			release_teleport(embs[idx].teleport.section)
		end

		-- удаляем предыдущую записку
		if embs[idx].note then
			lost_item(embs[idx].note)
		end
		
		-- выдаем поршень
		if embs[idx].info then
			give_info(embs[idx].info)
		end

		-- выполняем функцию
		if embs[idx].func then
			loadstring(embs[idx].func)()
		end
	end
	
	-- следующий объект
	idx = idx+1
	set_value("emb_idx", idx)

	-- создаем следующий объект
	if embs[idx] then
	
		-- рюкзак
		obj = embs[idx].rukzak
		if obj then
			local obj = create(obj.section, xyz(obj.position.x, obj.position.y, obj.position.z), obj.lv, obj.gv)
			map_spot(obj.id, "treasure", "Invisible Cache")
			sms("Received a tip on the cache", nil, nil, 10000)
		end

		-- рестриктор
		obj = embs[idx].restrictor
		if obj then
			snp.create_restrictor(xyz(obj.position.x, obj.position.y, obj.position.z), obj.lv, obj.gv, obj.radius, obj.func)
			level.map_add_object_spot_ser(obj.id, "crlc_big", "Estimated location of the stash")
			sms("Received a tip on the cache", nil, nil, 10000)
		end

		-- телепорт
		obj = embs[idx].teleport
		if obj then
			if obj.position == "actor" then
				create_teleport(obj.section, db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id())
			else
				create_teleport(obj.section, xyz(obj.position.x, obj.position.y, obj.position.z), obj.lv, obj.gv)
				level.map_add_object_spot_ser(obj.id, "crlc_big", "Estimated location of the stash")
				sms("Received a tip on the cache", nil, nil, 10000)
			end
		end

		-- запускаем звуки
		start_sound()
	else
		-- все, кончилась цепочка
		del_value("emb_idx")
	end
end

-- запуск сюжетных звуков
function start_sound()
	local idx = get_value("emb_idx")
	if not idx then return end
	
	local obj
	
	-- рюкзаки
	obj = embs[idx].rukzak
	if obj and obj.sound and obj.location == level.name() then
		play_sound(obj.sound, vector():set(obj.position.x, obj.position.y, obj.position.z))
	end

	-- рестрикторы
	obj = embs[idx].restrictor
	if obj and obj.sound and obj.location == level.name() then
		play_sound(obj.sound, vector():set(obj.position.x, obj.position.y, obj.position.z))
	end

	-- телепорты
	obj = embs[idx].teleport
	if obj and obj.sound and obj.location == level.name() then
		play_sound(obj.sound, vector():set(obj.position.x, obj.position.y, obj.position.z))
	end
end

-- собственно запуск звука
function play_sound(snd, pos)
	voice(snd):play_at_pos(db.actor, pos, 0, sound_object.s3d+sound_object.looped)
end
-- собственно остановка звука
function stop_sound(snd)
	voice(snd):stop()
end

function f1()
	sms("%c[sender]"..translate("name_istochnik_neizvesten")..":\\n%c[sms_red]"..translate("f1_0_0"), nil, nil, 20000)
	worker()
end
function f2()
	sms("%c[sender]"..translate("name_istochnik_neizvesten")..":\\n%c[sms_red]"..translate("f2_0_0")..user_name()..translate("f2_0_1"), nil, nil, 20000)
	lost_item("emb_fridge")
end

-- Функции для диалогов
function run_worker()
	worker()
end
function emb_container_have()
	return has("emb_container",1)
end
function emb_container_done()
	lost("emb_container",1)
end
