-- Import the engine.
modimport("engine.lua")


-- Actions Initialization.
--use "data/actions/init"

-- Imports to keep the keyhandler from working while typing in chat.
Load "chatinputscreen"
Load "consolescreen"
Load "textedit"




local TECH = GLOBAL.TECH
local EQUIPSLOTS = GLOBAL.EQUIPSLOTS
--local Player = GLOBAL.ThePlayer
--GetPlayer = GLOBAL.GetPlayer
local require = GLOBAL.require
local STRINGS = GLOBAL.STRINGS
local Recipe = GLOBAL.Recipe
local RECIPETABS = GLOBAL.RECIPETABS
local Ingredient = GLOBAL.Ingredient
local containers = require("containers")
local SpawnPrefab = GLOBAL.SpawnPrefab
--local Vector3 = GLOBAL.Vector3



wuqipd={}
wuqipd.jingangon=false
wuqipd.xueyuon=false
wuqipd.mindaoon=false
wuqipd.longlinon=false
GLOBAL.wuqipd=wuqipd










PrefabFiles = {
	"quanyecha","huoshuqiu","jianqiao","feiren_projectile","metalclaw","pot","xueyu","jingang","mindao","longlin","potie","jutsuspecial",--[["miho","mihobell","fengzhishang",]]"buff_accelerate","duanjianr","duanjianb",
	
}



Assets = {
    Asset( "IMAGE", "images/saveslot_portraits/quanyecha.tex" ),
    Asset( "ATLAS", "images/saveslot_portraits/quanyecha.xml" ),

    Asset( "IMAGE", "images/selectscreen_portraits/quanyecha.tex" ),
    Asset( "ATLAS", "images/selectscreen_portraits/quanyecha.xml" ),
	
    Asset( "IMAGE", "images/selectscreen_portraits/quanyecha_silho.tex" ),
    Asset( "ATLAS", "images/selectscreen_portraits/quanyecha_silho.xml" ),

    Asset( "IMAGE", "bigportraits/quanyecha.tex" ),
    Asset( "ATLAS", "bigportraits/quanyecha.xml" ),
	
	Asset( "IMAGE", "images/map_icons/quanyecha.tex" ),
	Asset( "ATLAS", "images/map_icons/quanyecha.xml" ),
	

	
	Asset( "IMAGE", "images/avatars/avatar_quanyecha.tex" ),
    Asset( "ATLAS", "images/avatars/avatar_quanyecha.xml" ),
	
	Asset( "IMAGE", "images/avatars/avatar_ghost_quanyecha.tex" ),
    Asset( "ATLAS", "images/avatars/avatar_ghost_quanyecha.xml" ),
	
	Asset( "IMAGE", "images/avatars/self_inspect_quanyecha.tex" ),
    Asset( "ATLAS", "images/avatars/self_inspect_quanyecha.xml" ),
	
	Asset( "IMAGE", "images/names_quanyecha.tex" ),
    Asset( "ATLAS", "images/names_quanyecha.xml" ),
	
	--Asset( "IMAGE", "images/map_icons/miho.tex" ),
	--Asset( "ATLAS", "images/map_icons/miho.xml" ),
	--Asset( "IMAGE", "images/map_icons/mihobell.tex" ),
	--Asset( "ATLAS", "images/map_icons/mihobell.xml" ),
   --Asset("ANIM", "anim/ui_miho_4x4.zip"),
	
    --Asset( "SOUNDPACKAGE" , "sound/foxpointup.fev" ),
    --Asset( "SOUND" , "sound/foxpointup.fsb" ),
	
	--Asset("SOUNDPACKAGE", "sound/mayoi.fev"),
    --Asset("SOUND", "sound/mayoi.fsb"),
	
	--Asset("ANIM", "anim/fengzhishang.zip"),
	 Asset("ANIM", "anim/accelerate.zip"),

	--Asset( "IMAGE", "images/inventoryimages/tab/quanyechatab.tex"),
	--Asset( "ATLAS", "images/inventoryimages/tab/quanyechatab.xml"),
	
}

local case = 0

local function GetWeapon(inst)

	if not GLOBAL.TheWorld.ismastersim then
		return
	end




if inst.components.inventory and inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS) and inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS).prefab  == "pot" then
case = 1

	elseif inst.components.inventory and inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS) and inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS).prefab  == "jingang" then
case = 2

	elseif inst.components.inventory and inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS) and inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS).prefab  == "xueyu" then

case = 3
		
	elseif inst.components.inventory and inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS) and inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS).prefab  == "mindao" then

case = 4

	elseif inst.components.inventory and inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS) and inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS).prefab  == "longlin" then
case = 5

	elseif inst.components.inventory and inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS) and inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS).prefab  == "potie" and not TheWorld.state.isfullmoon then
case = 6

	

	else 
case = 0
	
    end




end

GLOBAL.TUNING.WEAPON = {}
GLOBAL.TUNING.WEAPON.KEYSEVEN = GetModConfigData("key7") or 114

--Transformation

local function ChangeWeaponFn(inst)

if inst:HasTag("playerghost") then return end

GetWeapon(inst)

--if inst.transformed then
--inst.Transform:SetScale(0.8, 0.8, 0.8)
if case == 1 then

if wuqipd.jingangon then
inst.components.talker:Say("金刚铁碎牙!", 2.5,true)

inst.components.inventory:Equip(SpawnPrefab("jingang"))
elseif wuqipd.xueyuon then
inst.components.talker:Say("血玉铁碎牙!", 2.5,true)
inst.components.inventory:Equip(SpawnPrefab("xueyu"))
elseif wuqipd.mindaoon then
inst.components.talker:Say("冥道残月破铁碎牙!", 2.5,true)
inst.components.inventory:Equip(SpawnPrefab("mindao"))
elseif wuqipd.longlinon then
inst.components.talker:Say("龙鳞铁碎牙!", 2.5,true)
inst.components.inventory:Equip(SpawnPrefab("longlin"))
	for i, v in ipairs(AllPlayers) do
		v:ShakeCamera(CAMERASHAKE.FULL, .7, .02, .3, inst, 40)
	end
else
inst.components.talker:Say("铁碎牙还没吸收到任何妖力!", 2.5,true)
local IsWeapon = function (item)
return item:HasTag("feng")
end
pot = inst.components.inventory:FindItem(IsWeapon)
inst.components.inventory:Equip(pot)
end

elseif case == 2 then

if wuqipd.xueyuon then
inst.components.talker:Say("血玉铁碎牙!", 2.5,true)
inst.components.inventory:Equip(SpawnPrefab("xueyu"))
elseif wuqipd.mindaoon then
inst.components.talker:Say("冥道残月破铁碎牙!", 2.5,true)
inst.components.inventory:Equip(SpawnPrefab("mindao"))
elseif wuqipd.longlinon then
inst.components.talker:Say("龙鳞铁碎牙!", 2.5,true)
inst.components.inventory:Equip(SpawnPrefab("longlin"))
	for i, v in ipairs(AllPlayers) do
		v:ShakeCamera(CAMERASHAKE.FULL, .7, .02, .3, inst, 40)
	end
else
local IsWeapon = function (item)
return item:HasTag("feng")
end
pot = inst.components.inventory:FindItem(IsWeapon)
inst.components.inventory:Equip(pot)
end

elseif case == 3 then

if wuqipd.mindaoon then
inst.components.talker:Say("冥道残月破铁碎牙!", 2.5,true)
inst.components.inventory:Equip(SpawnPrefab("mindao"))
elseif wuqipd.longlinon then
inst.components.talker:Say("龙鳞铁碎牙!", 2.5,true)
inst.components.inventory:Equip(SpawnPrefab("longlin"))
	for i, v in ipairs(AllPlayers) do
		v:ShakeCamera(CAMERASHAKE.FULL, .7, .02, .3, inst, 40)
	end
else 
local IsWeapon = function (item)
return item:HasTag("feng")
end
pot = inst.components.inventory:FindItem(IsWeapon)
inst.components.inventory:Equip(pot)
end

elseif case == 4 then

if wuqipd.longlinon then
inst.components.talker:Say("龙鳞铁碎牙!", 2.5,true)
inst.components.inventory:Equip(SpawnPrefab("longlin"))
	for i, v in ipairs(AllPlayers) do
		v:ShakeCamera(CAMERASHAKE.FULL, .7, .02, .3, inst, 40)
	end
	--inst.components.talker:Say("这份强大的力量由我驾驭!", 2.5,true) 
else 
local IsWeapon = function (item)
return item:HasTag("feng")
end
pot = inst.components.inventory:FindItem(IsWeapon)
inst.components.inventory:Equip(pot)
end


elseif case == 5 then

--local items = {"pot"}
--inst.components.inventory:FindItems(fn)
--inst.components.inventory:IsItemEquipped(item)
local IsWeapon = function (item)
return item:HasTag("feng")
end
pot = inst.components.inventory:FindItem(IsWeapon)
inst.components.inventory:Equip(pot)

--inst.components.inventory:Equip(SpawnPrefab("pot"))
elseif case == 6 then

local IsWeapon = function (item)
return item:HasTag("feng")
end
pot = inst.components.inventory:FindItem(IsWeapon)
inst.components.inventory:Equip(pot)

inst.components.talker:Say("妖力回来了，杂碎们寿司吧!", 2.5,true)

else


local IsWeapon = function (item)
return item:HasTag("feng")
end
pot = inst.components.inventory:FindItem(IsWeapon)
inst.components.inventory:Equip(pot)
--inst.components.talker:Say("需要铁碎牙？", 2.2,true) 


end



return true
 
end
 
AddModRPCHandler("quanyecha", "ChanGe", ChangeWeaponFn)


AddPlayerPostInit(function(inst)
   if inst.components.combat == nil then
        return
    end
	if GLOBAL.TheWorld.ismastersim then
		local old_start = inst.components.combat.StartAttack
		inst.components.combat.StartAttack = function(self)
			old_start(self)
			if self.target then
				local weapon = self:GetWeapon()
				if weapon and weapon.components.weapon and weapon.components.weapon.blinking then
					local distance = math.sqrt(self.inst:GetDistanceSqToInst(self.target))
					local hitrange = weapon.components.weapon.hitrange
					if distance > hitrange then
						local smoke1 = SpawnPrefab("maxwell_smoke")
						local smoke2 = SpawnPrefab("maxwell_smoke")
						local sx, sy, sz = self.inst.Transform:GetWorldPosition()
						smoke1.Transform:SetPosition(sx, sy, sz)
						local fx, fy, fz = self.target.Transform:GetWorldPosition()
						local q = (distance - hitrange + 0.2) / distance
						local dx = sx - q * (sx - fx)
						local dy = sy - q * (sy - fy)
						local dz = sz - q * (sz - fz)
						inst.Transform:SetPosition(dx, dy, dz)
						smoke2.Transform:SetPosition(dx, dy, dz)
						--inst.components.combat:SetAreaDamage(3, 0.7)
					   --end
					   --else
					   --inst.components.combat:SetAreaDamage(1, 0.5)
					end
				end
			end
		end
	end
end)


function Addtradableprefab(inst)
	if not inst.components.tradable then
		inst:AddComponent("tradable")
end end
AddPrefabPostInit("walrus_tusk", Addtradableprefab)
AddPrefabPostInit("dragon_scales", Addtradableprefab)
AddPrefabPostInit("houndstooth", Addtradableprefab)
AddPrefabPostInit("nightmarefuel", Addtradableprefab)




local containers = require("containers")
 
local olderwidgetsetup = containers.widgetsetup
containers.widgetsetup = function(container, prefab)
    if not prefab and container.inst.prefab == "huoshuqiu" then
        prefab = "shadowchester"
    end
    olderwidgetsetup(container, prefab)
end 





function load()
 local pot = GLOBAL.Recipe("pot", {Ingredient("duanjianr", 1), Ingredient("duanjianb", 1),Ingredient("houndstooth", 1),Ingredient("goldnugget", 5),}, RECIPETABS.WAR, {SCIENCE=2})
pot.atlas = "images/inventoryimages/tisuiya.xml"
pot.image = "tisuiya.tex"

 local huoshuqiu = GLOBAL.Recipe("huoshuqiu", {Ingredient("feather_robin", 5), Ingredient("silk", 2),}, RECIPETABS.DRESS, {SCIENCE=2})
huoshuqiu.atlas = "images/inventoryimages/huoshuqiu.xml"
huoshuqiu.image = "huoshuqiu.tex"


 local jianqiao = GLOBAL.Recipe("jianqiao", {Ingredient("livinglog", 2), Ingredient("twigs", 5),}, RECIPETABS.WAR, {SCIENCE=2})
jianqiao.atlas = "images/inventoryimages/jianqiao.xml"
jianqiao.image = "jianqiao.tex"



end
load()



-- The character select screen lines
STRINGS.CHARACTER_TITLES.quanyecha = "犬夜叉"
STRINGS.CHARACTER_NAMES.quanyecha = "犬夜叉"
STRINGS.CHARACTER_DESCRIPTIONS.quanyecha = "半妖犬夜叉，有着一把能吸收妖力的妖刀"
STRINGS.CHARACTER_QUOTES.quanyecha = "\"啰嗦！\""

STRINGS.NAMES.HUOSHUQIU = "火鼠裘"
STRINGS.RECIPE_DESC.HUOSHUQIU = "火鼠毛做成的衣服"
--STRINGS.CHARACTERS.GENERIC.DESCRIBE.HUOSHUQIU = "抗寒防雨"

STRINGS.NAMES.JIANQIAO = "剑鞘"
STRINGS.RECIPE_DESC.JIANQIAO = "铁碎牙的剑鞘"
--STRINGS.CHARACTERS.GENERIC.DESCRIBE.JIANQIAO = "防御!"





STRINGS.NAMES.METALCLAW = "普攻"
STRINGS.RECIPE_DESC.METALCLAW = "Sharp and dangerous!"

STRINGS.NAMES.POT = "铁碎牙"
STRINGS.RECIPE_DESC.POT = "究极奥义!风之伤!"

STRINGS.NAMES.XUEYU = "血玉铁碎牙"
STRINGS.RECIPE_DESC.XUEYU = "究极奥义!破甲!"

STRINGS.NAMES.JINGANG = "金钢枪破铁碎牙"
STRINGS.RECIPE_DESC.JINGANG = "究极奥义!金钢枪破!"

STRINGS.NAMES.MINDAO = "冥界铁碎牙"
STRINGS.RECIPE_DESC.MINDAO = "究极奥义!冥道残月破!"

STRINGS.NAMES.LONGLIN = "龙鳞铁碎牙"
STRINGS.RECIPE_DESC.LONGLIN = "究极奥义!龙炎!"

STRINGS.NAMES.POTIE = "破旧的铁碎牙"
STRINGS.RECIPE_DESC.POTIE = "未持有妖力的破旧武士刀!"


STRINGS.NAMES.DUANJIANR = "剑刃"
STRINGS.RECIPE_DESC.DUANJIANR = "被折断的铁碎牙剑刃!"

STRINGS.NAMES.DUANJIANB = "剑柄"
STRINGS.RECIPE_DESC.DUANJIANB= "被折断的铁碎牙剑柄!"





-- Custom speech strings
STRINGS.CHARACTERS.QUANYECHA = require "speech_quanyecha"



-- The character's name as appears in-game 
STRINGS.NAMES.QUANYECHA = "犬夜叉"

-- The default responses of examining the character
STRINGS.CHARACTERS.GENERIC.DESCRIBE.quanyecha = 
{
	GENERIC = "他是犬夜叉!",
	ATTACKER = "可恶的半妖",
	MURDERER = "就是这个可恶的半妖!",
	REVIVER = "quanyecha, friend of ghosts.",
	GHOST = "quanyecha could use a heart.",
}

-- Let the game know character is male, female, or robot
--table.insert(GLOBAL.CHARACTER_GENDERS.MALE, "quanyecha")





AddMinimapAtlas("images/map_icons/quanyecha.xml")
AddModCharacter("quanyecha", "MALE")

