-- File:        UI_spawner.SCRIPT
-- Created:     25.05.2007
-- Copyright:   2007 Haoose ; based on Script from Jeppa
-- Author:      Haoose
-- Version:     0.1 ?

-- инициализация
class "spawner" (CUIScriptWnd)

-- инициализируем элементы интерфейса и каллбаки
function spawner:__init(owner) super()
	self.owner = owner
	self:InitControls()
	self:InitCallBacks()
end

function spawner:__finalize()

end

function spawner:InitControls()
	self:SetWndRect					(Frect():set(50,50,550,450))

	-- файл-описатель наших элементов
	local xml = CScriptXmlInit()
	xml:ParseFile("ui_spawner_wnd.xml")

	-- видео на заднем плане
	xml:InitStatic("back_video", self)

	-- рамка
	xml:InitStatic("background", self)

	-- кнопки
	self:Register(xml:Init3tButton("btn_pkm", self),"btn_pkm")
	self:Register(xml:Init3tButton("btn_pkm_ammo", self),"btn_pkm_ammo")
	self:Register(xml:Init3tButton("btn_svd", self),"btn_svd")
	self:Register(xml:Init3tButton("btn_svd_ammo", self),"btn_svd_ammo")
-- ****************************************************************************
	self:Register(xml:Init3tButton("btn_dog_weak", self),"btn_dog_weak")
	self:Register(xml:Init3tButton("btn_snork_weak", self),"btn_snork_weak")
	self:Register(xml:Init3tButton("btn_chimera_normal", self),"btn_chimera_normal")	
	self:Register(xml:Init3tButton("btn_default_freedom", self),"btn_default_freedom")
	self:Register(xml:Init3tButton("btn_psy_dog", self),"btn_psy_dog")	
	self:Register(xml:Init3tButton("btn_gigant_normal", self),"btn_gigant_normal")
	self:Register(xml:Init3tButton("btn_m_controller_old", self),"btn_m_controller_old")
	self:Register(xml:Init3tButton("btn_bloodsucker_jumper_deadly", self),"btn_bloodsucker_jumper_deadly")	
-- ****************************************************************************	
	self:Register(xml:Init3tButton("btn_niva", self),"btn_niva")
	self:Register(xml:Init3tButton("btn_groza", self),"btn_groza")
	self:Register(xml:Init3tButton("btn_groza_ammo", self),"btn_groza_ammo")
	self:Register(xml:Init3tButton("btn_btr", self),"btn_btr")
	self:Register(xml:Init3tButton("btn_kamaz", self),"btn_kamaz")
	self:Register(xml:Init3tButton("btn_tr13", self),"btn_tr13")
	self:Register(xml:Init3tButton("btn_uaz", self),"btn_uaz")
	self:Register(xml:Init3tButton("btn_zaz", self),"btn_zaz")	
	self:Register(xml:Init3tButton("btn_vintorez", self),"btn_vintorez")
	self:Register(xml:Init3tButton("btn_vintorez_ammo", self),"btn_vintorez_ammo")
	self:Register(xml:Init3tButton("btn_abakan", self),"btn_abakan")
	self:Register(xml:Init3tButton("btn_abakan_ammo", self),"btn_abakan_ammo")
	self:Register(xml:Init3tButton("btn_sig550", self),"btn_sig550")
	self:Register(xml:Init3tButton("btn_sig550_ammo", self),"btn_sig550_ammo")	
	self:Register(xml:Init3tButton("btn_ak74u_ammo", self),"btn_ak74u_ammo")
	self:Register(xml:Init3tButton("btn_ak74u", self),"btn_ak74u")
	self:Register(xml:Init3tButton("btn_weapons", self),"btn_weapons")
	self:Register(xml:Init3tButton("btn_ammo", self),"btn_ammo")
	-- кнопка выхода
	self:Register(xml:Init3tButton("btn_quit", self),"btn_quit")

    	xml:InitStatic("WeaponsTXT", self)
	xml:InitStatic("AmmoTXT", self)
	xml:InitStatic("MonstersTXT", self)
	xml:InitStatic("TransportTXT", self)
	xml:InitStatic("InfoTXT", self)

end

function spawner:InitCallBacks()
	-- тут интерактивные элементы, при определенном действии выполняется заданная ф-ия
	self:AddCallback("btn_pkm", ui_events.BUTTON_CLICKED, self.spawn_pkm, self)
	self:AddCallback("btn_pkm_ammo", ui_events.BUTTON_CLICKED, self.spawn_pkm_ammo, self)
	self:AddCallback("btn_svd", ui_events.BUTTON_CLICKED, self.spawn_svd, self)
	self:AddCallback("btn_svd_ammo", ui_events.BUTTON_CLICKED, self.spawn_svd_ammo, self)
-- *********************************************************************************************
	self:AddCallback("btn_dog_weak", ui_events.BUTTON_CLICKED, self.spawn_dog_weak, self)
	self:AddCallback("btn_snork_weak", ui_events.BUTTON_CLICKED, self.spawn_snork_weak, self)
	self:AddCallback("btn_chimera_normal", ui_events.BUTTON_CLICKED, self.spawn_chimera_normal, self)
    self:AddCallback("btn_default_freedom", ui_events.BUTTON_CLICKED, self.spawn_default_freedom, self)
	self:AddCallback("btn_psy_dog", ui_events.BUTTON_CLICKED, self.spawn_psy_dog, self)	
	self:AddCallback("btn_gigant_normal", ui_events.BUTTON_CLICKED, self.spawn_gigant_normal, self)	
	self:AddCallback("btn_m_controller_old", ui_events.BUTTON_CLICKED, self.spawn_m_controller_old, self)
	self:AddCallback("btn_bloodsucker_jumper_deadly", ui_events.BUTTON_CLICKED, self.spawn_bloodsucker_jumper_deadly, self)	
-- *********************************************************************************************
	self:AddCallback("btn_niva", ui_events.BUTTON_CLICKED, self.spawn_niva, self)
	self:AddCallback("btn_groza", ui_events.BUTTON_CLICKED, self.spawn_groza, self)
	self:AddCallback("btn_groza_ammo", ui_events.BUTTON_CLICKED, self.spawn_groza_ammo, self)
	self:AddCallback("btn_btr", ui_events.BUTTON_CLICKED, self.spawn_btr, self)
	self:AddCallback("btn_kamaz", ui_events.BUTTON_CLICKED, self.spawn_kamaz, self)
	self:AddCallback("btn_tr13", ui_events.BUTTON_CLICKED, self.spawn_tr13, self)
	self:AddCallback("btn_uaz", ui_events.BUTTON_CLICKED, self.spawn_uaz, self)
	self:AddCallback("btn_zaz", ui_events.BUTTON_CLICKED, self.spawn_zaz, self)	
	self:AddCallback("btn_vintorez", ui_events.BUTTON_CLICKED, self.spawn_vintorez, self)
	self:AddCallback("btn_vintorez_ammo", ui_events.BUTTON_CLICKED, self.spawn_vintorez_ammo, self)
	self:AddCallback("btn_abakan", ui_events.BUTTON_CLICKED, self.spawn_abakan, self)
	self:AddCallback("btn_abakan_ammo", ui_events.BUTTON_CLICKED, self.spawn_abakan_ammo, self)
	self:AddCallback("btn_sig550", ui_events.BUTTON_CLICKED, self.spawn_sig550, self)
	self:AddCallback("btn_sig550_ammo", ui_events.BUTTON_CLICKED, self.spawn_sig550_ammo, self)	
	self:AddCallback("btn_ak74u_ammo", ui_events.BUTTON_CLICKED, self.spawn_ak74u_ammo, self)
	self:AddCallback("btn_ak74u", ui_events.BUTTON_CLICKED, self.spawn_ak74u, self)
	self:AddCallback("btn_weapons", ui_events.BUTTON_CLICKED, self.spawn_weapons, self)
	self:AddCallback("btn_ammo", ui_events.BUTTON_CLICKED, self.spawn_ammo, self)

	self:AddCallback("btn_quit", ui_events.BUTTON_CLICKED, self.on_quit, self)
end

-- сразу созадим ф-ию для выхода
function spawner:on_quit()
	-- убираем наше окно, показываем предыдущее (или возвращаемся в игру)
	local console = get_console()
	self:GetHolder():start_stop_menu (self.owner, true)
	self:GetHolder():start_stop_menu (self,true)
	self.owner:Show	(true)
	-- скрытие главного меню только если запущена игра
	if self:check_game() then
		console:execute("main_menu off")
	end
end

function spawner:OnKeyboard(dik, keyboard_action)
     CUIScriptWnd.OnKeyboard(self,dik,keyboard_action)
     if keyboard_action == ui_events.WINDOW_KEY_PRESSED then
-- на выход повесим Esc
           if     dik == DIK_keys.DIK_ESCAPE then self:on_quit()
           elseif dik == DIK_keys.DIK_NUMPAD1 then      self:spawn_svd()
           elseif dik == DIK_keys.DIK_NUMPAD2 then      self:spawn_svd_ammo()
           elseif dik == DIK_keys.DIK_NUMPAD3 then      self:spawn_groza()
           elseif dik == DIK_keys.DIK_NUMPAD4 then      self:spawn_groza_ammo()
           elseif dik == DIK_keys.DIK_NUMPAD5 then      self:spawn_uaz()
           elseif dik == DIK_keys.DIK_NUMPAD6 then      self:spawn_niva()		   
           elseif dik == DIK_keys.DIK_NUMPAD7 then      self:spawn_btr()
           elseif dik == DIK_keys.DIK_NUMPAD8 then      self:spawn_kamaz()
           elseif dik == DIK_keys.DIK_NUMPAD9 then      self:spawn_zaz()
           end
     end
     return true
end

-- spawn_item - необходимый предмет
-- dist - радиус появления (случайно)
function spawner:spawn_item(spawn_item, dist)
	-- спавним только в игре
	if self:check_game() then
--		self:on_quit()
		local	pos = db.actor:position()
		local	dir = db.actor:direction()
		pos = pos:add(dir:mul(dist))
		alife():create(spawn_item, pos, 1, db.actor:game_vertex_id())
	end
end

function spawner:spawn_item_inv(spawn_item)
	-- спавним только в игре
	if self:check_game() then
--		self:on_quit()
	 	alife():create	(spawn_item, db.actor:position(),  db.actor:level_vertex_id(),
 			db.actor:game_vertex_id(), db.actor:id())
	end
end

function spawner:check_game()
	-- возвращает true если запущена игра
	local check = false
	if level.present() and (db.actor ~= nil) and db.actor:alive() then
		check = true
	end
	return check
end

function spawner:spawn_pkm()
	self:on_quit()
	-- колдуем PKM
	self:spawn_item("wpn_pkm", 3)
end

function spawner:spawn_pkm_ammo()
	self:on_quit()
	-- патроны к ней
	self:spawn_item("ammo_pkm_100", 3)
end

function spawner:spawn_svd()
	self:on_quit()
	-- колдуем СВД
	self:spawn_item("wpn_svd", 3)
end

function spawner:spawn_svd_ammo()
	self:on_quit()
	-- патроны к ней
	self:spawn_item("ammo_7.62x54_7h14", 3)
end
-- ****************************************************************
function spawner:spawn_dog_weak()
	self:on_quit()
	-- и того, кого будем из СВД мучить подальше от нас
	self:spawn_item("dog_weak", 30)
end

function spawner:spawn_snork_weak()
	self:on_quit()
	-- и того, кого будем из СВД мучить подальше от нас
	self:spawn_item("snork_weak", 30)
end

function spawner:spawn_chimera_normal()
	self:on_quit()
	-- и того, кого будем из СВД мучить подальше от нас
	self:spawn_item("chimera_normal", 30)
end

function spawner:spawn_default_freedom()
	self:on_quit()
	-- и того, кого будем из СВД мучить подальше от нас
	self:spawn_item("default_freedom", 6)
	self:spawn_item("default_freedom", 5)
	self:spawn_item("default_freedom", 4)
	self:spawn_item("default_freedom", 3)
	self:spawn_item("default_freedom", 2)
end

function spawner:spawn_psy_dog()
	self:on_quit()
	-- и того, кого будем из СВД мучить подальше от нас
	self:spawn_item("psy_dog", 30)
end

function spawner:spawn_gigant_normal()
	self:on_quit()
	-- и того, кого будем из СВД мучить подальше от нас
	self:spawn_item("gigant_normal", 30)
end

function spawner:spawn_m_controller_old()
	self:on_quit()
	-- и того, кого будем из СВД мучить подальше от нас
	self:spawn_item("m_controller_old", 30)
end

function spawner:spawn_bloodsucker_jumper_deadly()
	self:on_quit()
	-- и того, кого будем из СВД мучить подальше от нас
	self:spawn_item("bloodsucker_jumper_deadly", 30)
end
-- *****************************************************************
function spawner:spawn_niva()
	self:on_quit()
	self:spawn_item("ven_niva_spawn", 7)
end

function spawner:spawn_groza()
	self:on_quit()
-- колдуем грозу на расстоянии 3 метров.
	self:spawn_item("wpn_groza", 3)
end

function spawner:spawn_groza_ammo()
	self:on_quit()
	self:spawn_item("ammo_9x39_pab9", 3)
end

function spawner:spawn_btr()
	self:on_quit()
	self:spawn_item("ven_btr_spawn", 7)
end

function spawner:spawn_kamaz()
	self:on_quit()
	self:spawn_item("ven_kamaz_spawn", 7)
end

function spawner:spawn_tr13()
	self:on_quit()
	self:spawn_item("ven_tr13_spawn", 7)
end

function spawner:spawn_uaz()
	self:on_quit()
	self:spawn_item("ven_uaz_spawn", 7)
end

function spawner:spawn_zaz()
	self:on_quit()
	self:spawn_item("ven_zaz_spawn", 7)
end

function spawner:spawn_vintorez()
	self:on_quit()
	self:spawn_item("wpn_vintorez", 3)
end

function spawner:spawn_vintorez_ammo()
	self:on_quit()
	self:spawn_item("ammo_9x39_ap", 3)
end

function spawner:spawn_abakan()
	self:on_quit()
	self:spawn_item("wpn_abakan", 3)
end

function spawner:spawn_abakan_ammo()
	self:on_quit()
	self:spawn_item("ammo_5.45x39_fmj", 3)
end

function spawner:spawn_sig550()
	self:on_quit()
	self:spawn_item("wpn_sig550", 3)
end

function spawner:spawn_sig550_ammo()
	self:on_quit()
	self:spawn_item("ammo_5.56x45_ap", 3)
end

function spawner:spawn_ak74u()
	self:on_quit()
	self:spawn_item("wpn_ak74u", 3)
end

function spawner:spawn_ak74u_ammo()
	self:on_quit()
	self:spawn_item("ammo_5.45x39_ap", 3)
end

function spawner:spawn_weapons()
	self:on_quit()
	self:spawn_item("wpn_pkm", 3)
	self:spawn_item("wpn_svd", 3)
	self:spawn_item("wpn_groza", 3)
	self:spawn_item("wpn_vintorez", 3)
	self:spawn_item("wpn_abakan", 3)
	self:spawn_item("wpn_sig550", 3)
	self:spawn_item("wpn_ak74u", 3)
	self:spawn_item("wpn_beretta", 3)
	self:spawn_item("wpn_bm16", 3)
	self:spawn_item("wpn_colt1911", 3)
	self:spawn_item("wpn_desert_eagle", 3)
	self:spawn_item("wpn_fn2000", 3)
	self:spawn_item("wpn_fort", 3)
	self:spawn_item("wpn_g36", 3)
end

function spawner:spawn_ammo()
	self:on_quit()
	self:spawn_item("ammo_pkm_100", 3)
	self:spawn_item("ammo_7.62x54_7h14", 3)
	self:spawn_item("ammo_9x39_pab9", 3)
	self:spawn_item("ammo_9x39_ap", 3)
	self:spawn_item("ammo_5.45x39_fmj", 3)
	self:spawn_item("ammo_5.45x39_ap", 3)
	self:spawn_item("ammo_9x19_fmj", 3)
	self:spawn_item("ammo_9x19_pbp", 3)
	self:spawn_item("ammo_12x70_buck", 3)
	self:spawn_item("ammo_12x76_zhekan", 3)
	self:spawn_item("ammo_12x76_dart", 3)
	self:spawn_item("ammo_11.43x23_fmj", 3)
	self:spawn_item("ammo_11.43x23_hydro", 3)
	self:spawn_item("ammo_5.56x45_ss190", 3)
	self:spawn_item("ammo_5.56x45_ap", 3)
	self:spawn_item("ammo_9x18_fmj", 3)
	self:spawn_item("ammo_9x18_pmm", 3)
end

