-- File:        ARS_ACHIEVEMENTS.SCRIPT
-- Copyright:   2009-2010 ARS Team
-- Author:      Xmk
-- Description: Амчивменты

local ars_potroshitel_reward_time,ars_medic_reward_time,ars_medic_reward_p_time,ars_headshoter_reward_p_time,detective_reward_p_time=0,0,0,0,0
local last_medic_achievement_spawn_time=nil
local medic_achievement_items={"vodka","medkit","bandage"}
local last_headshoter_achievement_spawn_time=nil
local headshoter_achievement_items={"ammo_7.62x54_7h1"}
local headshot=false

ars_statistic={
	headshot			= 0,
	medkit				= 0,
	m_item 				= 0,
	objects				= 0
--	crow				= 0
}

local tips_icons={
	ars_headshoter		= "ui_inGame2_PD_ARS_Headshoter",
	ars_medic			= "ui_inGame2_PD_ARS_Medic",
	ars_potroshitel		= "ui_inGame2_PD_ARS_Mutanter",
	ars_scout			= "ui_inGame2_PD_ARS_Scout",
	ars_crower			= "ui_inGame2_PD_ARS_Crower"
}

local function send_tip(news_text,header,timeout,showtime,sender)
	if news_text==nil then return end
	if header==nil then header=GetString("st_tip") end
	if timeout==nil then timeout=0 end
	if showtime==nil then showtime=5 end
	xr_sound.set_sound_play(db.actor:id(),"pda_tips")
	if sender==nil then sender="default" end
	local ui_sender=tips_icons[sender]
	local news_text="%c[default]"..news_text
	if db.actor and ui_sender then
		db.actor:give_game_news(header,GetString(news_text),ui_sender,timeout*1000,showtime*1000)
	end
	return true
end

local function spawn_achivement_items(items_table,count,inv_box_story_id)
	local inv_box=alife():object(get_story_object_id(inv_box_story_id))
	for i=1,count do
		alife():create(items_table[math.random(#items_table)],inv_box.position,inv_box.m_level_vertex_id,inv_box.m_game_vertex_id,inv_box.id)
	end
end

function check_headshot_achievement()
	if not has_alife_info("ars_headshoter") then return end
	if last_headshoter_achievement_spawn_time==nil then
		last_headshoter_achievement_spawn_time=game.get_game_time()
	end
	if game.get_game_time():diffSec(last_headshoter_achievement_spawn_time)>86400 then
		if has_alife_info("ars_zaton_actor_treasure") then
			spawn_achivement_items(headshoter_achievement_items,3,"zat_a2_actor_treasure")
			ars_cop.send_tip(GetString("st_headshoter_news"),nil,nil,nil,"boroda")
		else
			ars_cop.send_tip(GetString("st_headshoter_news_nobox"),nil,nil,nil,"boroda")
			ars.write_variable("ArsHeadshoterRewardP",ars.read_variable("ArsHeadshoterRewardP",0)+1)
			if not has_alife_info("ars_headshoter_reward_p") then
				give_info("ars_headshoter_reward_p")
			end
		end
		ars_headshoter_reward_p_time=GetGameMinutes()-(GetGameMinutes()-ars_headshoter_reward_p_time)
		last_headshoter_achievement_spawn_time=game.get_game_time()
	end
end
function check_medic_achievement()
	if not has_alife_info("ars_medic") then return end
	if last_medic_achievement_spawn_time==nil then
		last_medic_achievement_spawn_time=game.get_game_time()
	end
	if game.get_game_time():diffSec(last_medic_achievement_spawn_time)>86400 then
		if has_alife_info("ars_zaton_actor_treasure") then
			spawn_achivement_items(medic_achievement_items,3,"zat_a2_actor_treasure")
			ars_cop.send_tip(GetString("st_medic_news"),nil,nil,nil,"boroda")
		else
			ars_cop.send_tip(GetString("st_medic_news_nobox"),nil,nil,nil,"boroda")
			ars.write_variable("ArsMedicRewardP",ars.read_variable("ArsMedicRewardP",0)+1)
			if not has_alife_info("ars_medic_reward_p") then
				give_info("ars_medic_reward_p")
			end
		end
		ars_medic_reward_p_time=GetGameMinutes()-(GetGameMinutes()-ars_medic_reward_p_time)
		last_medic_achievement_spawn_time=game.get_game_time()
	end
end

function headshot_counter(obj,amount,local_direction,who,bone_index)
	if not has_alife_info("ars_headshoter_precond") and who:id()==db.actor:id() then
		if amount>0 and (bone_index==14 or bone_index==15 or bone_index==16 or bone_index==17 or bone_index==19) then
			headshot=true
		elseif amount>0 and (bone_index~=14 or bone_index~=15 or bone_index~=16 or bone_index~=17 or bone_index~=19) and bone_index~="from_death_callback" and headshot==true then
			headshot=false
		elseif headshot==true and (bone_index=="from_death_callback") then
			headshot=false
			local wpn=db.actor:active_item()
--			if wpn and wpn:section() and (wpn:section()=="wpn_svd" or wpn:section()=="wpn_svd_nimble" or wpn:section()=="wpn_svu" or wpn:section()=="wpn_svu_nimble") then
			if wpn and (string.find(wpn:section(),"svd") or string.find(wpn:section(),"svu") or string.find(wpn:section(),"gauss") or string.find(wpn:section(),"vintorez") or string.find(wpn:section(),"l85") or string.find(wpn:section(),"sig550")) then
				if who:position():distance_to(obj:position())>50 then
					ars_statistic.headshot=ars_statistic.headshot+1
				end
			end
		end
		if ars_statistic.headshot>35 then
			give_info("ars_headshoter_precond")
		end
	end
end
function medkit_counter()
	if not has_alife_info("ars_medic_precond") then
		ars_statistic.medkit=ars_statistic.medkit+1
		if ars_statistic.medkit>=20 then
			give_info("ars_medic_precond")
		end
	end
end
function monster_item_counter()
	if not has_alife_info("ars_potroshitel_precond") then
		ars_statistic.m_item=ars_statistic.m_item+1
		if ars_statistic.m_item>=50 then
			give_info("ars_potroshitel_precond")
		end
	end
end
function map_objects_counter()
	if not has_alife_info("ars_scout_precond") then
		ars_statistic.objects=ars_statistic.objects+1
		if ars_statistic.objects>=54 then
			give_info("ars_scout_precond")
		end
	end
end
function crow_counter(victim,who)
	if surge_manager.is_started() then return end
	if not has_alife_info("ars_crower_precond") and who:id()==db.actor:id() then
	--	ars_statistic.crow=ars_statistic.crow+1
	--	if ars_statistic.crow>=25 then
		ars.write_variable("ArsCrowCount",ars.read_variable("ArsCrowCount",0)+1)
		if ars.read_variable("ArsCrowCount",0)>=25 then
			give_info("ars_crower_precond")
		end
	end
end

function save(package)
	package:w_u16(ars_statistic.headshot)
	package:w_u16(ars_statistic.medkit)
	package:w_u16(ars_statistic.m_item)
	package:w_u16(ars_statistic.objects)
--	package:w_u16(ars_statistic.crow)
	if last_medic_achievement_spawn_time==nil then
		package:w_bool(false)
	else
		package:w_bool(true)
		utils.w_CTime(package,last_medic_achievement_spawn_time)
	end
	if last_headshoter_achievement_spawn_time==nil then
		package:w_bool(false)
	else
		package:w_bool(true)
		utils.w_CTime(package,last_headshoter_achievement_spawn_time)
	end
	package:w_u32(ars_potroshitel_reward_time)
	package:w_u32(ars_medic_reward_time)
	package:w_u32(ars_medic_reward_p_time)
	package:w_u32(ars_headshoter_reward_p_time)
	package:w_u32(detective_reward_p_time)
end
function load(reader)
	ars_statistic={}
	ars_statistic.headshot=reader:r_u16()
	ars_statistic.medkit=reader:r_u16()
	ars_statistic.m_item=reader:r_u16()
	ars_statistic.objects=reader:r_u16()
--	ars_statistic.crow=reader:r_u16()
	local stored_achievement_time=reader:r_bool()
	if stored_achievement_time==true then
		last_medic_achievement_spawn_time=utils.r_CTime(reader)
	end
	stored_achievement_time=reader:r_bool()
	if stored_achievement_time==true then
		last_headshoter_achievement_spawn_time=utils.r_CTime(reader)
	end
	ars_potroshitel_reward_time=reader:r_u32()
	ars_medic_reward_time=reader:r_u32()
	ars_medic_reward_p_time=reader:r_u32()
	ars_headshoter_reward_p_time=reader:r_u32()
	detective_reward_p_time=reader:r_u32()
end


function ars_headshoter_functor()
	if not has_alife_info("ars_headshoter") then
		if has_alife_info("ars_headshoter_precond") then
			db.actor:give_info_portion("ars_headshoter")
			send_tip(GetString("st_ach_ars_headshoter"),nil,nil,nil,"ars_headshoter")
			ars_cop.set_npc_reputation(50,true,"killer")
		end
	end
	return has_alife_info("ars_headshoter")
end
function ars_medic_functor()
	if not has_alife_info("ars_medic") then
		if has_alife_info("ars_medic_precond") then
			db.actor:give_info_portion("ars_medic")
			send_tip(GetString("st_ach_ars_medic"),nil,nil,nil,"ars_medic")
			if has_alife_info("ars_zaton_actor_treasure") then
				ars_cop.spawn_item_in_box("stalker_medic_outfit",1,"zat_a2_actor_treasure")
				ars_cop.send_tip(GetString("st_medic_oft_news"),nil,nil,nil,"boroda")
			else
				give_info("ars_medic_reward")
				ars_cop.send_tip(GetString("st_medic_oft_news_nobox"),nil,2,nil,"boroda")
			end
			ars_medic_reward_time=GetGameMinutes()
			ars_cop.set_npc_reputation(150,true,"stalker")
		end
	end
	return has_alife_info("ars_medic")
end
function ars_potroshitel_functor()
	if not has_alife_info("ars_potroshitel") then
		if has_alife_info("ars_potroshitel_precond") then
			db.actor:give_info_portion("ars_potroshitel")
			send_tip(GetString("st_ach_ars_potroshitel"),nil,nil,nil,"ars_potroshitel")
			ars_mutanter.SetRang(math.random(10,30))
			--[[
			if has_alife_info("ars_zaton_actor_treasure") then
				ars_cop.spawn_item_in_box("ars_mutanter_knife_uniq",1,"zat_a2_actor_treasure")
				send_tip(GetString("st_potroshitel_news"),nil,2,nil,"boroda")
			else
				give_info("ars_potroshitel_reward")
				ars_cop.send_tip(GetString("st_potroshitel_news_nobox"),nil,nil,nil,"boroda")
			end
			--]]
			ars_potroshitel_reward_time=GetGameMinutes()
			ars_cop.set_npc_reputation(100,true,"stalker")
		end
	end
	return has_alife_info("ars_potroshitel")
end
function ars_scout_functor()
	if not has_alife_info("ars_scout") then
		if has_alife_info("ars_scout_precond") then
			db.actor:give_info_portion("ars_scout")
			send_tip(GetString("st_ach_ars_scout"),nil,nil,nil,"ars_scout")
			for k,v in ipairs(ars_level_changer.lc_zones_info) do
				if not(has_alife_info(v))then
					db.actor:give_info_portion(v)
				end
			end
			ars_level_changer.check_new_lc()
		end
	end
	return has_alife_info("ars_scout")
end
function ars_crower_functor()
	if not has_alife_info("ars_crower") then
		if has_alife_info("ars_crower_precond") then
			db.actor:give_info_portion("ars_crower")
			send_tip(GetString("st_ach_ars_crower"),nil,nil,nil,"ars_crower")
			ars_cop.set_npc_reputation(math.random(15,30),false,"freedom")
			ars_cop.set_npc_reputation(math.random(15,30),false,"stalker")
			ars_cop.set_npc_reputation(math.random(10,20),true,"dolg")
			ars_cop.set_npc_reputation(math.random(10,20),true,"bandit")
		end
	end
	return has_alife_info("ars_crower")
end


function get_money_from_rewards(info)
	if info=="ars_headshoter_p" then
		return ars_cop.calculator_money(ars_headshoter_reward_p_time,20,1)
	elseif info=="ars_medic" then
		return ars_cop.calculator_money(ars_medic_reward_p_time,50,1)
	elseif info=="ars_medic_p" then
		return ars_cop.calculator_money(ars_medic_reward_time,10,1)
	elseif info=="ars_potroshitel" then
		return ars_cop.calculator_money(ars_potroshitel_reward_time,30,1)
	elseif info=="detective_p" then
		return ars_cop.calculator_money(detective_reward_p_time,10,1)
	end
end

function set_detective_reward_p_time()
	detective_reward_p_time=GetGameMinutes()-(GetGameMinutes()-detective_reward_p_time)
end


------------------------------ ARS CoP MOD v0.5.x ------------------------------
--[[

If you're going to use the whole of this script or its parts in your own creative 
developments for the S.T.A.L.K.E.R. game. Leave the copyrights, note the real
author(s) and don't claim others' ideas and their realization to be your own ones.
It's just simple Modmakers' Ethics. Thank you!

Если вы собираетесь использовать данный скрипт целиком или частично в своих 
разработках по игре S.T.A.L.K.E.R. Не удаляйте копирайты, указывайте настоящего 
автора(ов) и не выдавайте чужие идеи и их реализацию за свои. Ведь это элементарная 
этика модостроителей! Спасибо за понимание.

]]--