local require = GLOBAL.require
local STRINGS = GLOBAL.STRINGS
local Recipe = GLOBAL.Recipe
local RECIPETABS = GLOBAL.RECIPETABS
local TECH = GLOBAL.TECH
local State = GLOBAL.State
local SpawnPrefab = GLOBAL.SpawnPrefab
local TimeEvent = GLOBAL.TimeEvent
local FRAMES = GLOBAL.FRAMES
local TheInput = GLOBAL.TheInput
local Vector3 = GLOBAL.Vector3
local FindEntity =GLOBAL.FindEntity
local TUNING = GLOBAL.TUNING
local GetPlayer = GLOBAL.GetPlayer
local GetWorld = GLOBAL.GetWorld
local GetClock = GLOBAL.GetClock
local ACTIONS = GLOBAL.ACTIONS
local Action = GLOBAL.Action
local GROUND = GLOBAL.GROUND
local EventHandler = GLOBAL.EventHandler
local ActionHandler = GLOBAL.ActionHandler
local KnownModIndex = GLOBAL.KnownModIndex
local Armor = require "components/armor"
local Health = require "components/health"
local Combat = require "components/combat"
local SGWilson=require "stategraphs/SGwilson"
local ModsScreen = require "screens/modsscreen"
require "actions"
require "components/playercontroller"
TUNING.YIYUISOPEN = 0
TUNING.DIFFICULTY_YUYI = 1
TUNING.YIYU_MONSTERBUFF_YUYI = "off"
TUNING.ATTACKDAY_YUYI = "off"
TUNING.YUYI_LANGUAGES = GetModConfigData("language_yuyi")
TUNING.YUYI_SOUNDVOLUME = GetModConfigData("Sound_Volume")
TUNING.YUYI_HUNGERRATE = GetModConfigData("yuyi_Hunger")
TUNING.YUYI_STARTHP = GetModConfigData("Initial attribute")
TUNING.YUYI_NEEDEXP = GetModConfigData("yuyi_expneed")
TUNING.YUYI_SKILLSCOST = GetModConfigData("yuyi_skillscost")
TUNING.YUYI_QUICKPICK = GetModConfigData("quick_pick")
TUNING.YUYI_KEY1 = GetModConfigData("yuyi_key1")
TUNING.YUYI_KEY2 = GetModConfigData("yuyi_key2")
TUNING.YUYI_KEY3 = GetModConfigData("yuyi_key3")
TUNING.YUYI_KEY4 = GetModConfigData("yuyi_key4")
TUNING.YUYI_KEY5 = GetModConfigData("yuyi_key5")
TUNING.YUYI_KEY6 = GetModConfigData("yuyi_key6")
TUNING.YUYI_KEY7 = GetModConfigData("yuyi_key7")

if TUNING.YUYI_LANGUAGES == 1 then
	STRINGS.CHARACTERS.YUYI = require "speech_yuyien"
	require "tuning_yuyiE"
elseif TUNING.YUYI_LANGUAGES == 0 then
	STRINGS.CHARACTERS.YUYI = require "speech_yuyich"
	require "tuning_yuyiC"
elseif TUNING.YUYI_LANGUAGES == 2 then
	STRINGS.CHARACTERS.YUYI = require "speech_yuyioo"
	require "tuning_yuyiO"
end

TUNING.CREEPINTHEDEEPS = false
for _, moddir in ipairs(KnownModIndex:GetModsToLoad()) do
if KnownModIndex:GetModInfo(moddir).name == "Creeps in the Deeps" then TUNING.CREEPINTHEDEEPS = true end
end

PrefabFiles = {
	"yuyi",
    "yuyi_fx",
	"yuyi_hat",
	"yuyi_sack",
	"yuyi_books",
	"yuyi_weapon",
	"lianqifang",
	"waxpaper",
	"yuyi_firesuppressor",
--	"dragon_yuyi",
}

local yiyu_bookboss = 
{
	"deerclops",--巨鹿
	"moose",--鹿鸭
	"dragonfly",--蜻蜓
	"bearger",--熊
	"minotaur",--远古守护者
	"quacken",
	"tigershark",--虎鲨
	"twister",--飓风BOSS
}

local yiyu_bookboss2 =
{
	"leif",--树精
	"treeguard",--椰树守卫
	"leif_sparse",--稀有树精
	"spiderqueen", --蜘蛛女王
	"warg",--座狼
}

local yiyu_items = 
{
	"bonealtar",
	"yiyusack",
	"yiyuhattop",
	"magicflute",
	"destructionflute",
	"zijing",
	"qianghuafu",
	"dug_cave_banana_tree",
	"dug_lichen",
	"dug_cactus",
	"dug_flower_cave",
	"dug_reeds",
	"dug_shrooms",
	"dug_cacao_tree",
	"gardenershovel",
	"guangmang",
	"leiting",
	"yiyuhuan",
	"fenghouzhishu",
	"writingbrush",
	"yiyu_deerclops",
	"icemerm",
	"imp",
	"palacelantern",
	"snailarmor",
	"package_yiyu",
	"ryondae",
	"ryondae_item",
	"yiyu_headdress",
	"yiyu_glass",
	"yiyuhouse",
	"backcity",
	"yiyubeebox",
	"yiyutrap",
	"yiyubbq",
	"strawberry",
	"yiyu_waxwell",
	"yiyuwaxwell_boat",
	"yiyu_cat",
	"yiyu_cathouse",
	"yiyu_chest",
	"yiyu_ice_box",
    "turf_yiyua",
    "turf_yiyuc",
    "turf_yiyub",
    "turf_yiyu1d",
    "turf_yiyu2d",
    "turf_yiyu3d",
    "turf_yiyu4d",
    "turf_yiyu5d",
    "turf_yiyu6d",
    "turf_yiyu7d",
    "turf_yiyu8d",
    "turf_yiyu9d",
	"beequeen",
	"beeguard",
	"hivehat",
	"yiyucrystal",
	"crystalmine",
	"emerald_flower",
	"dug_emerald_flower",
}

Assets = {
    Asset( "IMAGE", "images/saveslot_portraits/yuyi.tex" ),
    Asset( "ATLAS", "images/saveslot_portraits/yuyi.xml" ),
    Asset( "IMAGE", "images/selectscreen_portraits/yuyi.tex" ),
    Asset( "ATLAS", "images/selectscreen_portraits/yuyi.xml" ),
	Asset( "IMAGE", "images/yuyitabs/yuyitab.tex"),
	Asset( "ATLAS", "images/yuyitabs/yuyitab.xml"),
    Asset( "IMAGE", "bigportraits/yuyi.tex" ),
    Asset( "ATLAS", "bigportraits/yuyi.xml" ),
	Asset( "IMAGE", "images/map_icons/yuyi.tex" ),
	Asset( "ATLAS", "images/map_icons/yuyi.xml" ),

    Asset("ANIM", "anim/lianqifang.zip"),
    Asset("IMAGE", "images/inventoryimages/lianqifang.tex"),
    Asset("ATLAS", "images/inventoryimages/lianqifang.xml"),
    Asset("IMAGE", "images/yuyitabs/yuyi_firesuppressor.tex"),
    Asset("ATLAS", "images/yuyitabs/yuyi_firesuppressor.xml"),

    Asset("IMAGE", "images/inventoryimages/longya.tex"),
    Asset("ATLAS", "images/inventoryimages/longya.xml"),
    Asset("IMAGE", "images/inventoryimages/xueyin.tex"),
    Asset("ATLAS", "images/inventoryimages/xueyin.xml"),
    Asset("IMAGE", "images/inventoryimages/haihun.tex"),
    Asset("ATLAS", "images/inventoryimages/haihun.xml"),
	Asset("IMAGE", "images/yuyi_shezhi.tex" ),
	Asset("ATLAS", "images/yuyi_shezhi.xml" ),

    Asset("IMAGE", "images/inventoryimages/yuyi_hat1.tex"),
    Asset("ATLAS", "images/inventoryimages/yuyi_hat1.xml"),
    Asset("IMAGE", "images/inventoryimages/yuyi_hat2.tex"),
    Asset("ATLAS", "images/inventoryimages/yuyi_hat2.xml"),
    Asset("IMAGE", "images/inventoryimages/yuyi_hat3.tex"),
    Asset("ATLAS", "images/inventoryimages/yuyi_hat3.xml"),

    Asset("IMAGE", "images/inventoryimages/yuyi_sack1.tex"),
    Asset("ATLAS", "images/inventoryimages/yuyi_sack1.xml"),
    Asset("IMAGE", "images/inventoryimages/yuyi_sack2.tex"),
    Asset("ATLAS", "images/inventoryimages/yuyi_sack2.xml"),
    Asset("IMAGE", "images/inventoryimages/yuyi_sack3.tex"),
    Asset("ATLAS", "images/inventoryimages/yuyi_sack3.xml"),

    Asset( "IMAGE", "images/inventoryimages/wax_paper.tex" ),
    Asset( "ATLAS", "images/inventoryimages/wax_paper.xml" ),
    Asset( "IMAGE", "images/inventoryimages/suiji_book.tex" ),
    Asset( "ATLAS", "images/inventoryimages/suiji_book.xml" ),

	Asset("SOUNDPACKAGE", 	"sound/yuyi_attack.fev"),
	Asset("SOUND", 			"sound/yuyi_attack.fsb"),
}

function RepicPostInit(inst)
local haihun1 = Ingredient( "haihun", 1)
haihun1.atlas = "images/inventoryimages/haihun.xml"
local longya1 = Ingredient( "longya", 1)
longya1.atlas = "images/inventoryimages/longya.xml"
local yuyi_hat11 = Ingredient( "yuyi_hat1", 1)
yuyi_hat11.atlas = "images/inventoryimages/yuyi_hat1.xml"
local yuyi_sack11 = Ingredient( "yuyi_sack1", 1)
yuyi_sack11.atlas = "images/inventoryimages/yuyi_sack1.xml"
local yuyi_sack21 = Ingredient( "yuyi_sack2", 1)
yuyi_sack21.atlas = "images/inventoryimages/yuyi_sack2.xml"

--Recipe("bundlewrap", {Ingredient("waxpaper", 1), Ingredient("rope", 1)}, RECIPETABS.SURVIVAL, TECH.LOST)
local suiji_book = GLOBAL.Recipe("suiji_book", {Ingredient("papyrus", 5),Ingredient("silk", 20),Ingredient("purplegem", 1)}, RECIPETABS.YUYITAB, {SCIENCE=2})
suiji_book.atlas = "images/inventoryimages/suiji_book.xml"

local haihun = GLOBAL.Recipe("haihun", {Ingredient("goldnugget", 5), Ingredient("spear", 1)}, RECIPETABS.YUYITAB, {SCIENCE = 1})
haihun.atlas = "images/inventoryimages/haihun.xml"
local longya = GLOBAL.Recipe("longya", {Ingredient("purplegem", 4), haihun1, Ingredient("gears", 2)}, RECIPETABS.YUYITAB, {SCIENCE = 1})
longya.atlas = "images/inventoryimages/longya.xml"
local xueyin = GLOBAL.Recipe("xueyin", {Ingredient("greengem", 4), longya1, Ingredient("gears", 10)}, RECIPETABS.YUYITAB, {SCIENCE = 1})
xueyin.atlas = "images/inventoryimages/xueyin.xml"

local yuyi_hat1 = GLOBAL.Recipe("yuyi_hat1", {Ingredient("featherhat", 1), Ingredient("goldnugget", 10)}, RECIPETABS.YUYITAB, {SCIENCE = 1})
yuyi_hat1.atlas = "images/inventoryimages/yuyi_hat1.xml"
local yuyi_hat2 = GLOBAL.Recipe("yuyi_hat2", {Ingredient("orangegem", 5), yuyi_hat11, Ingredient("goldnugget", 20)}, RECIPETABS.YUYITAB, {SCIENCE = 1})
yuyi_hat2.atlas = "images/inventoryimages/yuyi_hat2.xml"
local yuyi_hat3 = GLOBAL.Recipe("yuyi_hat3", {Ingredient("yellowgem", 5), yuyi_hat11, Ingredient("goldnugget", 20)}, RECIPETABS.YUYITAB, {SCIENCE = 1})
yuyi_hat3.atlas = "images/inventoryimages/yuyi_hat3.xml"

local yuyi_sack1 = GLOBAL.Recipe("yuyi_sack1", {Ingredient("armorwood", 1), Ingredient("goldnugget", 10)}, RECIPETABS.YUYITAB, {SCIENCE = 0})
yuyi_sack1.atlas = "images/inventoryimages/yuyi_sack1.xml"

if GLOBAL.SaveGameIndex:IsModeShipwrecked() then
local yuyi_sack2 = GLOBAL.Recipe("yuyi_sack2", {Ingredient("magic_seal", 1), yuyi_sack11, Ingredient("purplegem", 1)}, RECIPETABS.YUYITAB, {SCIENCE = 2})
yuyi_sack2.atlas = "images/inventoryimages/yuyi_sack2.xml"
local yuyi_sack3 = GLOBAL.Recipe("yuyi_sack3", {Ingredient("shark_gills", 4), yuyi_sack21, Ingredient("purplegem", 1)}, RECIPETABS.YUYITAB, {SCIENCE = 2})
yuyi_sack3.atlas = "images/inventoryimages/yuyi_sack3.xml"
else
local yuyi_sack2 = GLOBAL.Recipe("yuyi_sack2", {Ingredient("dragon_scales", 1), yuyi_sack11, Ingredient("purplegem", 1)}, RECIPETABS.YUYITAB, {SCIENCE = 2})
yuyi_sack2.atlas = "images/inventoryimages/yuyi_sack2.xml"
local yuyi_sack3 = GLOBAL.Recipe("yuyi_sack3", {Ingredient("bearger_fur", 1), yuyi_sack21, Ingredient("purplegem", 1)}, RECIPETABS.YUYITAB, {SCIENCE = 2})
yuyi_sack3.atlas = "images/inventoryimages/yuyi_sack3.xml"
end
--------------------------------------------------------------

GetPlayer():DoTaskInTime(1, function()
if TUNING.YIYUISOPEN == 1 then
if GLOBAL.IsDLCEnabled(GLOBAL.REIGN_OF_GIANTS) then
	if TUNING.YIYUISOPEN == 1 then
		local lianqifang = GLOBAL.Recipe("lianqifang", {Ingredient("livinglog", 2), Ingredient("cutstone", 2), Ingredient("purplegem", 4)}, RECIPETABS.YUYITAB, {SCIENCE = 2}, "lianqifang_placer" )
		lianqifang.atlas = "images/inventoryimages/lianqifang.xml"
		local yuyi_firesuppressor = GLOBAL.Recipe("yuyi_firesuppressor", {Ingredient("ice", 15), Ingredient("gears", 2), Ingredient("purplegem", 1)}, RECIPETABS.YUYITAB, {SCIENCE = 2},"firesuppressor_placer" )
		yuyi_firesuppressor.atlas = "images/yuyitabs/yuyi_firesuppressor.xml"
		local maxwelllight = GLOBAL.Recipe("maxwelllight", {Ingredient("goldnugget", 10), Ingredient("marble", 2), crystal1}, RECIPETABS.YUYITAB, TECH.LOST, "maxwelllight_placer" , 1)
		maxwelllight.atlas = "images/map_icons/maxwelllight.xml"
		local pigtorch = GLOBAL.Recipe("pigtorch", {Ingredient("goldnugget", 10), Ingredient("pigskin", 6), crystal1}, RECIPETABS.YUYITAB, TECH.LOST, "pigtorch_placer" , 1)
		pigtorch.atlas = "images/map_icons/pigtorch.xml"
		local yiyu_chest = GLOBAL.Recipe("yiyu_chest", {Ingredient("goldnugget", 2),Ingredient("boards", 3)}, RECIPETABS.YUYITAB, TECH.LOST, "yiyu_chest_placer" , 1)
		yiyu_chest.atlas = "images/inventoryimages/yiyu_chest.xml"
		local yiyu_ice_box = GLOBAL.Recipe("yiyu_ice_box", {Ingredient("cutstone", 2),Ingredient("gears", 2),Ingredient("transistor", 2)}, RECIPETABS.YUYITAB, TECH.LOST, "yiyu_ice_box_placer" , 1)
		yiyu_ice_box.atlas = "images/inventoryimages/yiyu_ice_box.xml"
		local yiyubbq = GLOBAL.Recipe("yiyubbq", {Ingredient("cutstone", 2),Ingredient("charcoal", 2),Ingredient("rope", 1)}, RECIPETABS.YUYITAB, {SCIENCE = 1}, "yiyubbq_placer" )
		yiyubbq.atlas = "images/inventoryimages/yiyubbq.xml"
		local yiyubeebox = GLOBAL.Recipe("yiyubeebox", {Ingredient("honeycomb", 1),Ingredient("bee", 5),Ingredient("boards", 4)}, RECIPETABS.YUYITAB, {SCIENCE = 1}, "yiyubeebox_placer" )
		yiyubeebox.atlas = "images/inventoryimages/yiyubeebox.xml"
--		local yiyuhuan = GLOBAL.Recipe("yiyuhuan", {Ingredient("greengem", 2), darktrident1, Ingredient("purplegem", 5)}, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE = 2})
--		yiyuhuan.atlas = "images/inventoryimages/yiyuhuan.xml"
--		local yiyuhattop = GLOBAL.Recipe("yiyuhattop", {Ingredient("deerclops_eyeball", 1), Ingredient("walrushat", 1), Ingredient("purplegem", 4)}, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE = 2})
--		yiyuhattop.atlas = "images/inventoryimages/yiyuhattop.xml"
		local yiyu_headdress = GLOBAL.Recipe("yiyu_headdress", {Ingredient("coontail", 10), Ingredient("manrabbit_tail", 10), Ingredient("yellowgem", 8)}, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE = 2})
		yiyu_headdress.atlas = "images/inventoryimages/yiyu_headdress.xml"
		local yiyu_glass = GLOBAL.Recipe("yiyu_glass", {Ingredient("dragon_scales", 1), Ingredient("purplegem", 10)}, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE = 2})
		yiyu_glass.atlas = "images/inventoryimages/yiyu_glass.xml"
		local writingbrush = GLOBAL.Recipe("writingbrush", {Ingredient("greengem", 1),Ingredient("manrabbit_tail", 10), Ingredient("walrus_tusk", 1) }, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE=2})
		writingbrush.atlas = "images/inventoryimages/writingbrush.xml"
		local gardenershovel = GLOBAL.Recipe("gardenershovel", {Ingredient("purplegem", 2),Ingredient("livinglog", 2), Ingredient("goldnugget", 5) }, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE=1})
		gardenershovel.atlas = "images/inventoryimages/gardenershovel.xml"
		local turf_yiyua = Recipe( "turf_yiyua", {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		turf_yiyua.atlas = "images/inventoryimages/turf_yiyua.xml"
		local turf_yiyuc = Recipe( "turf_yiyuc", {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		turf_yiyuc.atlas = "images/inventoryimages/turf_yiyuc.xml"
		local turf_yiyub = Recipe( "turf_yiyub", {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		turf_yiyub.atlas = "images/inventoryimages/turf_yiyub.xml"
		local Turf_yiyu1d = Recipe( "turf_yiyu1d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu1d.atlas = "images/inventoryimages/turf_yiyu1d.xml"
		local Turf_yiyu2d = Recipe( "turf_yiyu2d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu2d.atlas = "images/inventoryimages/turf_yiyu2d.xml"
		local Turf_yiyu3d = Recipe( "turf_yiyu3d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu3d.atlas = "images/inventoryimages/turf_yiyu3d.xml"
		local Turf_yiyu4d = Recipe( "turf_yiyu4d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu4d.atlas = "images/inventoryimages/turf_yiyu4d.xml"
		local Turf_yiyu5d = Recipe( "turf_yiyu5d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu5d.atlas = "images/inventoryimages/turf_yiyu5d.xml"
		local Turf_yiyu6d = Recipe( "turf_yiyu6d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu6d.atlas = "images/inventoryimages/turf_yiyu6d.xml"
		local Turf_yiyu7d = Recipe( "turf_yiyu7d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu7d.atlas = "images/inventoryimages/turf_yiyu7d.xml"
		local Turf_yiyu8d = Recipe( "turf_yiyu8d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu8d.atlas = "images/inventoryimages/turf_yiyu8d.xml"
		local Turf_yiyu9d = Recipe( "turf_yiyu9d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu9d.atlas = "images/inventoryimages/turf_yiyu9d.xml"
	end
elseif not GLOBAL.SaveGameIndex:IsModeShipwrecked() then
--------------------------------------------------------------
	if TUNING.YIYUISOPEN == 1 then
		local lianqifang = GLOBAL.Recipe("lianqifang", {Ingredient("livinglog", 2), Ingredient("cutstone", 2), Ingredient("purplegem", 4)}, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil,"lianqifang_placer" )
		lianqifang.atlas = "images/inventoryimages/lianqifang.xml"
		local yuyi_firesuppressor = GLOBAL.Recipe("yuyi_firesuppressor", {Ingredient("ice", 15), Ingredient("gears", 2), Ingredient("purplegem", 1)}, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil,"firesuppressor_placer" )
		yuyi_firesuppressor.atlas = "images/yuyitabs/yuyi_firesuppressor.xml"
		local maxwelllight = GLOBAL.Recipe("maxwelllight", {Ingredient("goldnugget", 10), Ingredient("marble", 2), crystal1}, RECIPETABS.YUYITAB, TECH.LOST, nil, "maxwelllight_placer" , 1)
		maxwelllight.atlas = "images/map_icons/maxwelllight.xml"
		local pigtorch = GLOBAL.Recipe("pigtorch", {Ingredient("goldnugget", 10), Ingredient("pigskin", 6), crystal1}, RECIPETABS.YUYITAB, TECH.LOST, nil, "pigtorch_placer" , 1)
		pigtorch.atlas = "images/map_icons/pigtorch.xml"
		local yiyu_chest = GLOBAL.Recipe("yiyu_chest", {Ingredient("goldnugget", 2),Ingredient("boards", 3)}, RECIPETABS.YUYITAB, TECH.LOST, nil, "yiyu_chest_placer" , 1)
		yiyu_chest.atlas = "images/inventoryimages/yiyu_chest.xml"
		local yiyu_ice_box = GLOBAL.Recipe("yiyu_ice_box", {Ingredient("cutstone", 2),Ingredient("gears", 2),Ingredient("transistor", 2)}, RECIPETABS.YUYITAB, TECH.LOST, nil, "yiyu_ice_box_placer" , 1)
		yiyu_ice_box.atlas = "images/inventoryimages/yiyu_ice_box.xml"
		local yiyubbq = GLOBAL.Recipe("yiyubbq", {Ingredient("cutstone", 2),Ingredient("charcoal", 2),Ingredient("rope", 1)}, RECIPETABS.YUYITAB, {SCIENCE = 1}, nil, "yiyubbq_placer" )
		yiyubbq.atlas = "images/inventoryimages/yiyubbq.xml"
		local yiyubeebox = GLOBAL.Recipe("yiyubeebox", {Ingredient("honeycomb", 1),Ingredient("bee", 5),Ingredient("boards", 4)}, RECIPETABS.YUYITAB, {SCIENCE = 1}, nil, "yiyubeebox_placer" )
		yiyubeebox.atlas = "images/inventoryimages/yiyubeebox.xml"
--		local boatrepairkit = Recipe("boatrepairkit", {Ingredient("boards", 4), Ingredient("stinger", 4), Ingredient("rope", 2)}, RECIPETABS.YUYITAB, {SCIENCE = 1}, nil, nil, nil, 1 )
--		local ryondae_item = Recipe("ryondae_item", {Ingredient("greengem", 2),Ingredient("livinglog", 4), Ingredient("petals_evil", 9),}, RECIPETABS.YUYITAB, {SCIENCE = 1}, nil, nil, nil, 1 )
--		ryondae_item.atlas = "images/inventoryimages/ryondae.xml"
--		ryondae_item.image = "ryondae.tex"
--		local yiyuhuan = GLOBAL.Recipe("yiyuhuan", {Ingredient("greengem", 2), darktrident1, Ingredient("purplegem", 5)}, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE = 2})
--		yiyuhuan.atlas = "images/inventoryimages/yiyuhuan.xml"
--		local yiyuhattop = GLOBAL.Recipe("yiyuhattop", {Ingredient("deerclops_eyeball", 1), Ingredient("walrushat", 1), Ingredient("purplegem", 4)}, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE = 2})
--		yiyuhattop.atlas = "images/inventoryimages/yiyuhattop.xml"
		local yiyu_headdress = GLOBAL.Recipe("yiyu_headdress", {Ingredient("coontail", 10), Ingredient("crab", 10), Ingredient("manrabbit_tail", 10), Ingredient("yellowgem", 8)}, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE = 2})
		yiyu_headdress.atlas = "images/inventoryimages/yiyu_headdress.xml"
		local yiyu_glass = GLOBAL.Recipe("yiyu_glass", {Ingredient("dragon_scales", 1), Ingredient("purplegem", 10)}, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE = 2})
		yiyu_glass.atlas = "images/inventoryimages/yiyu_glass.xml"
		local writingbrush = GLOBAL.Recipe("writingbrush", {Ingredient("greengem", 1),Ingredient("manrabbit_tail", 10), Ingredient("walrus_tusk", 1) }, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE=2})
		writingbrush.atlas = "images/inventoryimages/writingbrush.xml"
		local gardenershovel = GLOBAL.Recipe("gardenershovel", {Ingredient("purplegem", 2),Ingredient("livinglog", 2), Ingredient("goldnugget", 5) }, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE=1})
		gardenershovel.atlas = "images/inventoryimages/gardenershovel.xml"
		local turf_yiyua = Recipe( "turf_yiyua", {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		turf_yiyua.atlas = "images/inventoryimages/turf_yiyua.xml"
		local turf_yiyuc = Recipe( "turf_yiyuc", {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		turf_yiyuc.atlas = "images/inventoryimages/turf_yiyuc.xml"
		local turf_yiyub = Recipe( "turf_yiyub", {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		turf_yiyub.atlas = "images/inventoryimages/turf_yiyub.xml"
		local Turf_yiyu1d = Recipe( "turf_yiyu1d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu1d.atlas = "images/inventoryimages/turf_yiyu1d.xml"
		local Turf_yiyu2d = Recipe( "turf_yiyu2d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu2d.atlas = "images/inventoryimages/turf_yiyu2d.xml"
		local Turf_yiyu3d = Recipe( "turf_yiyu3d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu3d.atlas = "images/inventoryimages/turf_yiyu3d.xml"
		local Turf_yiyu4d = Recipe( "turf_yiyu4d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu4d.atlas = "images/inventoryimages/turf_yiyu4d.xml"
		local Turf_yiyu5d = Recipe( "turf_yiyu5d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu5d.atlas = "images/inventoryimages/turf_yiyu5d.xml"
		local Turf_yiyu6d = Recipe( "turf_yiyu6d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu6d.atlas = "images/inventoryimages/turf_yiyu6d.xml"
		local Turf_yiyu7d = Recipe( "turf_yiyu7d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu7d.atlas = "images/inventoryimages/turf_yiyu7d.xml"
		local Turf_yiyu8d = Recipe( "turf_yiyu8d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu8d.atlas = "images/inventoryimages/turf_yiyu8d.xml"
		local Turf_yiyu9d = Recipe( "turf_yiyu9d",  {crystal2, Ingredient("beefalowool", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu9d.atlas = "images/inventoryimages/turf_yiyu9d.xml"
	end
--------------------------------------------------------------
elseif GLOBAL.SaveGameIndex:IsModeShipwrecked() then
--------------------------------------------------------------
	if TUNING.YIYUISOPEN == 1 then
		local lianqifang = GLOBAL.Recipe("lianqifang", {Ingredient("livinglog", 2), Ingredient("cutstone", 2), Ingredient("purplegem", 4)}, RECIPETABS.YUYITAB, TECH.SCIENCE_ONE,GLOBAL.RECIPE_GAME_TYPE.SHIPWRECKED,"lianqifang_placer" )
		lianqifang.atlas = "images/inventoryimages/lianqifang.xml"
		local yuyi_firesuppressor = GLOBAL.Recipe("yuyi_firesuppressor", {Ingredient("ice", 15), Ingredient("gears", 2), Ingredient("purplegem", 1)}, RECIPETABS.YUYITAB, TECH.SCIENCE_ONE,GLOBAL.RECIPE_GAME_TYPE.SHIPWRECKED,"firesuppressor_placer" )
		yuyi_firesuppressor.atlas = "images/yuyitabs/yuyi_firesuppressor.xml"
		local maxwelllight = GLOBAL.Recipe("maxwelllight", {Ingredient("goldnugget", 10), Ingredient("obsidian", 2), crystal1}, RECIPETABS.YUYITAB, TECH.LOST, GLOBAL.RECIPE_GAME_TYPE.SHIPWRECKED, "maxwelllight_placer" , 1)
		maxwelllight.atlas = "images/map_icons/maxwelllight.xml"
		local pigtorch = GLOBAL.Recipe("pigtorch", {Ingredient("goldnugget", 10), Ingredient("pigskin", 6), crystal1}, RECIPETABS.YUYITAB, TECH.LOST, GLOBAL.RECIPE_GAME_TYPE.SHIPWRECKED, "pigtorch_placer" , 1)
		pigtorch.atlas = "images/map_icons/pigtorch.xml"
		local yiyu_chest = GLOBAL.Recipe("yiyu_chest", {Ingredient("goldnugget", 2),Ingredient("boards", 3)}, RECIPETABS.YUYITAB, TECH.LOST, GLOBAL.RECIPE_GAME_TYPE.SHIPWRECKED, "yiyu_chest_placer" , 1)
		yiyu_chest.atlas = "images/inventoryimages/yiyu_chest.xml"
		local yiyu_ice_box = GLOBAL.Recipe("yiyu_ice_box", {Ingredient("cutstone", 2),Ingredient("gears", 2),Ingredient("transistor", 2)}, RECIPETABS.YUYITAB, TECH.LOST, GLOBAL.RECIPE_GAME_TYPE.SHIPWRECKED, "yiyu_ice_box_placer" , 1)
		yiyu_ice_box.atlas = "images/inventoryimages/yiyu_ice_box.xml"
		local yiyubbq = GLOBAL.Recipe("yiyubbq", {Ingredient("cutstone", 2),Ingredient("charcoal", 2),Ingredient("rope", 1)}, RECIPETABS.YUYITAB, TECH.SCIENCE_ONE,GLOBAL.RECIPE_GAME_TYPE.SHIPWRECKED, "yiyubbq_placer" )
		yiyubbq.atlas = "images/inventoryimages/yiyubbq.xml"
		local yiyubeebox = GLOBAL.Recipe("yiyubeebox", {Ingredient("honeycomb", 1),Ingredient("bee", 5),Ingredient("boards", 4)}, RECIPETABS.YUYITAB, TECH.SCIENCE_ONE,GLOBAL.RECIPE_GAME_TYPE.SHIPWRECKED, "yiyubeebox_placer" )
		yiyubeebox.atlas = "images/inventoryimages/yiyubeebox.xml"
--		local ryondae_item = Recipe("ryondae_item", {Ingredient("purplegem", 2),Ingredient("livinglog", 4), Ingredient("petals_evil", 9),}, RECIPETABS.YUYITAB, {SCIENCE = 1}, nil, nil, nil, 1 )
--		ryondae_item.atlas = "images/inventoryimages/ryondae.xml"
--		ryondae_item.image = "ryondae.tex"
--		local yiyuhuan = GLOBAL.Recipe("yiyuhuan", {Ingredient("purplegem", 5), darktrident1, Ingredient("gears", 4)}, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE = 2})
--		yiyuhuan.atlas = "images/inventoryimages/yiyuhuan.xml"
--		local yiyuhattop = GLOBAL.Recipe("yiyuhattop", {Ingredient("magic_seal", 1), Ingredient("tigereye", 2), Ingredient("doydoyfeather", 8)}, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE = 2})
--		yiyuhattop.atlas = "images/inventoryimages/yiyuhattop.xml"
		local yiyu_headdress = GLOBAL.Recipe("yiyu_headdress", {Ingredient("coontail", 10), Ingredient("crab", 10), Ingredient("manrabbit_tail", 10), Ingredient("yellowgem", 8)}, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE = 2})
		yiyu_headdress.atlas = "images/inventoryimages/yiyu_headdress.xml"
		local yiyu_glass = GLOBAL.Recipe("yiyu_glass", {Ingredient("purplegem", 8), Ingredient("obsidian", 20)}, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE = 2})
		yiyu_glass.atlas = "images/inventoryimages/yiyu_glass.xml"
		local writingbrush = GLOBAL.Recipe("writingbrush", {Ingredient("purplegem", 4),Ingredient("beardhair", 15), Ingredient("bamboo", 20) }, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE=2})
		writingbrush.atlas = "images/inventoryimages/writingbrush.xml"
		local gardenershovel = GLOBAL.Recipe("gardenershovel", {Ingredient("purplegem", 2),Ingredient("livinglog", 2), Ingredient("goldnugget", 5) }, GLOBAL.RECIPETABS.YUYITAB, {SCIENCE=1})
		gardenershovel.atlas = "images/inventoryimages/gardenershovel.xml"
		local turf_yiyua = Recipe( "turf_yiyua", {crystal2, Ingredient("limestone", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		turf_yiyua.atlas = "images/inventoryimages/turf_yiyua.xml"
		local turf_yiyuc = Recipe( "turf_yiyuc", {crystal2, Ingredient("limestone", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		turf_yiyuc.atlas = "images/inventoryimages/turf_yiyuc.xml"
		local turf_yiyub = Recipe( "turf_yiyub", {crystal2, Ingredient("limestone", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		turf_yiyub.atlas = "images/inventoryimages/turf_yiyub.xml"
		local Turf_yiyu1d = Recipe( "turf_yiyu1d",  {crystal2, Ingredient("limestone", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu1d.atlas = "images/inventoryimages/turf_yiyu1d.xml"
		local Turf_yiyu2d = Recipe( "turf_yiyu2d",  {crystal2, Ingredient("limestone", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu2d.atlas = "images/inventoryimages/turf_yiyu2d.xml"
		local Turf_yiyu3d = Recipe( "turf_yiyu3d",  {crystal2, Ingredient("limestone", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu3d.atlas = "images/inventoryimages/turf_yiyu3d.xml"
		local Turf_yiyu4d = Recipe( "turf_yiyu4d",  {crystal2, Ingredient("limestone", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu4d.atlas = "images/inventoryimages/turf_yiyu4d.xml"
		local Turf_yiyu5d = Recipe( "turf_yiyu5d",  {crystal2, Ingredient("limestone", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu5d.atlas = "images/inventoryimages/turf_yiyu5d.xml"
		local Turf_yiyu6d = Recipe( "turf_yiyu6d",  {crystal2, Ingredient("limestone", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu6d.atlas = "images/inventoryimages/turf_yiyu6d.xml"
		local Turf_yiyu7d = Recipe( "turf_yiyu7d",  {crystal2, Ingredient("limestone", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu7d.atlas = "images/inventoryimages/turf_yiyu7d.xml"
		local Turf_yiyu8d = Recipe( "turf_yiyu8d",  {crystal2, Ingredient("limestone", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu8d.atlas = "images/inventoryimages/turf_yiyu8d.xml"
		local Turf_yiyu9d = Recipe( "turf_yiyu9d",  {crystal2, Ingredient("limestone", 1), Ingredient("goldnugget", 1) }, RECIPETABS.YUYITAB, {SCIENCE = 2}, nil, nil, nil, 1 )
		Turf_yiyu9d.atlas = "images/inventoryimages/turf_yiyu9d.xml"
	end
end
end
end)
end

function PostInit(inst)
	RepicPostInit(inst)
	if inst.components.keeponland then inst:RemoveComponent("keeponland") end
	if inst.components.yiyu_keeponland then inst:RemoveComponent("yiyu_keeponland") end
	if inst and not inst.components.yuyi_keeponland then inst:AddComponent("yuyi_keeponland") end
end

local function createbook(inst)
	local p = GetPlayer()
	if p.components.yuyicast and p.components.age:GetAge() == 0 then
	local x, y, z = p.Transform:GetWorldPosition()
	local book = SpawnPrefab("leidianshu_book")
	book.Transform:SetPosition(x-2-math.random(2), y, z-2-math.random(2))
	GLOBAL.c_give("baconeggs",1)
	GLOBAL.c_give("grass_umbrella",1)
	GLOBAL.c_give("flowerhat",1)
    end
end
AddPrefabPostInit("wallyintro", createbook)
AddPrefabPostInit("maxwellintro", createbook)

function Combat:GetDamageModifier()
	local mod = 1
	for k,v in pairs(self.attack_damage_modifiers) do
		mod = mod + v
	end
	return mod
end

function Combat:CalcDamage(target, weapon, multiplier)
	local p = GetPlayer()
	if target:HasTag("alwaysblock") then return 0 end
	local multiplier = multiplier or 1
    if self.damagemultiplier then
		multiplier = multiplier * self.damagemultiplier
	end
	if GLOBAL.SaveGameIndex:IsModeShipwrecked() then
		multiplier = multiplier * self:GetDamageModifier()
	end
	local weapondamage = 0
	local bonus = self.damagebonus or 0
	if weapon then

		if weapon.components.weapon.variedmodefn then
			local d = weapon.components.weapon.variedmodefn(weapon)
			weapondamage = d.damage        
		else
			weapondamage = weapon.components.weapon.damage
		end
		if not weapondamage then weapondamage = 0 end
		if self.inst.skill_OLv1 and p.yuyi_states == 1 then weapondamage = 35+p.skill_OLv1*5 end
		if self.inst:HasTag("liehuojianfa") then
			weapondamage = weapondamage*(1+self.inst.skill_JLv1*0.3)
			self.inst:RemoveTag("liehuojianfa")
			if self.inst.fire then self.inst.fire:Remove() self.inst.fire = nil end
			SpawnPrefab("explode_small").Transform:SetPosition(target.Transform:GetWorldPosition())
			self.inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/liehuojizhong")
		end
		if self.inst:HasTag("leidianshu") then
			weapondamage = weapondamage+self.inst.skill_JLv0*5
			multiplier = 1
			self.inst.components.combat:SetRange(2, 2)
			self.inst:RemoveTag("leidianshu")
			SpawnPrefab("lightning").Transform:SetPosition(target:GetPosition():Get())
		end
		return weapondamage*multiplier + bonus
	end

	if self.inst.skill_OLv1 and p.yuyi_states == 1 then self.defaultdamage = 25+p.skill_OLv1*10 end
	if self.inst:HasTag("liehuojianfa") then
		self.defaultdamage = self.defaultdamage*(1+self.inst.skill_JLv1*0.3)
		if self.inst.fire then self.inst.fire:Remove() self.inst.fire = nil end
		self.inst:RemoveTag("liehuojianfa")
		SpawnPrefab("explode_small").Transform:SetPosition(target.Transform:GetWorldPosition())
		self.inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/liehuojizhong")
	end
	if self.inst:HasTag("leidianshu") then
		weapondamage = weapondamage+self.inst.skill_JLv0*5
		multiplier = 1
		self.inst.components.combat:SetRange(2, 2)
		self.inst:RemoveTag("leidianshu")
		SpawnPrefab("lightning").Transform:SetPosition(target:GetPosition():Get())
	end
	if target and target:HasTag("player") then
		if target:HasTag("mofadun") and target.skill_KLv0 and target.skill_KLv0 > 0 then
			if self.inst.components.freezable then self.inst.components.freezable:AddColdness(target.skill_KLv0, 3) end
			return self.defaultdamage * self.playerdamagepercent * multiplier + bonus - target.skill_KLv0*5
		end
		return self.defaultdamage * self.playerdamagepercent * multiplier + bonus
	end
	
	return self.defaultdamage * multiplier + bonus
end

function Armor:SetCondition(amount)
    self.condition = amount
    self.inst:PushEvent("percentusedchange", {percent = self:GetPercent()})
    if self.condition <= 0 then
        self.condition = 0
        GLOBAL.ProfileStatsSet("armor_broke_" .. self.inst.prefab, true)
        GLOBAL.ProfileStatsSet("armor", self.inst.prefab)       
        if METRICS_ENABLED then
			FightStat_BrokenArmor(self.inst.prefab)
		end
        if self.onfinished then
            self.onfinished()
        end
	if not self.inst.weapon_upgrade then
		self.inst:Remove()
	end
    end
end

ACTIONS.STORE.fn = function(act)
if act.invobject.prefab == "nightmarefuel" and act.target.armor_absorb and act.target.components.fueled and act.target.components.fueled.currentfuel and act.target.components.fueled.currentfuel < 3240 then
    if act.doer.components.inventory then
	    local fuel = act.doer.components.inventory:RemoveItem(act.invobject)
		if fuel then
	        if act.target.components.fueled:TakeFuelItem(fuel) then
	            return true
	        else
                print("False")
	            act.doer.components.inventory:GiveItem(fuel)
	        end
	    end
    end
end
if act.target.components.container and act.invobject.components.inventoryitem and act.doer.components.inventory then
		if not act.target.components.container:CanTakeItemInSlot(act.invobject) then
			return false, "NOTALLOWED"
		end

		local item = act.invobject.components.inventoryitem:RemoveFromOwner(act.target.components.container.acceptsstacks)
		if item then
			if not act.target.components.inventoryitem then
				act.target.components.container:Open(act.doer)
			end
			
			if not act.target.components.container:GiveItem(item,nil,nil,false) then
				if TheInput:ControllerAttached() then
					act.doer.components.inventory:GiveItem(item)
				else
					act.doer.components.inventory:GiveActiveItem(item)
				end
				return false
			end
			return true            
		end
	elseif act.target.components.occupiable and act.invobject and act.invobject.components.occupier and act.target.components.occupiable:CanOccupy(act.invobject) then
		local item = act.invobject.components.inventoryitem:RemoveFromOwner()
		act.target.components.occupiable:Occupy(item)
		return true
	end
end

ACTIONS.STORE.strfn = function(act)
	if act.target and act.target.components.stewer then
		return "COOK"
	elseif act.target and act.target.components.occupiable then
		return "IMPRISON"
	elseif act.invobject.prefab == "nightmarefuel" and act.target.armor_absorb and act.target.components.fueled and act.target.components.fueled.currentfuel and act.target.components.fueled.currentfuel < 3240 then
		return "FUELED"
	end
end

ACTIONS.EQUIP.fn = function(act)
	local weapon =  act.doer.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS)
	if act.doer:HasTag("banyuewandao") then
		act.doer:RemoveTag("banyuewandao")
		act.doer.skill_KCD1 = 1
		act.doer.components.combat.areahitrange = 0
		act.doer.components.combat.areahitdamagepercent = 0
		if weapon then weapon.components.weapon:SetProjectile(nil) end
	end
	if weapon and weapon.weapon_hunger and weapon.weapon_hunger > 0 then
	act.doer.components.hunger.hungerrate = TUNING.WILSON_HUNGER_RATE*(TUNING.YUYI_HUNGERRATE-weapon.weapon_hunger/10)
	end
	if act.doer.components.inventory and act.invobject.components.equippable.equipslot then
		return act.doer.components.inventory:Equip(act.invobject)
	end
	--Boat equip slots 
	if act.doer.components.driver and act.doer.components.driver.vehicle and act.invobject.components.equippable.boatequipslot then 
		local vehicle = act.doer.components.driver.vehicle
		if vehicle.components.container and vehicle.components.container.hasboatequipslots then 
			vehicle.components.container:Equip(act.invobject)
		end 
	end 
end

ACTIONS.UNEQUIP.fn = function(act)
	local weapon =  act.doer.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS)
	if act.doer:HasTag("banyuewandao") then
		act.doer:RemoveTag("banyuewandao")
		act.doer.skill_KCD1 = 1
		act.doer.components.combat.areahitrange = 0
		act.doer.components.combat.areahitdamagepercent = 0
		if weapon then weapon.components.weapon:SetProjectile(nil) end
	end
	if weapon and weapon.weapon_hunger and weapon.weapon_hunger > 0 then
	act.doer.components.hunger.hungerrate = TUNING.WILSON_HUNGER_RATE*TUNING.YUYI_HUNGERRATE
	end
	if act.doer.components.driver and act.doer.components.driver.vehicle and act.invobject.components.equippable.boatequipslot then 
		local vehicle = act.doer.components.driver.vehicle
		if vehicle.components.container then 
			vehicle.components.container:Unequip(act.invobject.components.equippable.boatequipslot)
			act.doer.components.inventory:GiveItem(act.invobject)
		end 
		return true
	elseif act.doer.components.inventory and act.invobject and act.invobject.components.inventoryitem.cangoincontainer then
		act.doer.components.inventory:GiveItem(act.invobject)
		--return act.doer.components.inventory:Unequip(act.invobject)
		return true
	elseif act.doer.components.inventory and act.invobject and not act.invobject.components.inventoryitem.cangoincontainer then
		act.doer.components.inventory:DropItem(act.invobject, true, true)
		return true
	end
end

local YUYICAST = GLOBAL.Action(10, false, true, 36)
YUYICAST.str = "cast"
YUYICAST.id = "YUYICAST"
YUYICAST.fn = function(act)
	if GetPlayer():HasTag("huoqiang") or GetPlayer():HasTag("baofengxue") then
        return GetPlayer().components.yuyicast:areaskill(GetPlayer())
	end
end

AddStategraphState("wilson", GLOBAL.State {
        name = "yuyicast",
        tags = {"doing", "busy", "canrotate"},
        onenter = function(inst)
        end,
        timeline = 
        {
            GLOBAL.TimeEvent(0*FRAMES, function(inst) 
                if inst and inst.bufferedaction then
                    inst:PerformBufferedAction()
                end
            end),
        },
        events = {
            GLOBAL.EventHandler("animover", function(inst)
                inst.sg:GoToState("idle") 
            end ),
        },
})

AddStategraphState("wilsonboating", GLOBAL.State {
        name = "yuyicast",
        tags = {"doing", "busy", "canrotate"},
        onenter = function(inst)
        end,
        timeline = 
        {
            GLOBAL.TimeEvent(0*FRAMES, function(inst) 
                if inst and inst.bufferedaction then
                    inst:PerformBufferedAction()
                end
            end),
        },
        events = {
            GLOBAL.EventHandler("animover", function(inst)
                inst.sg:GoToState("idle") 
            end ),
        },
})

SGWilson.states["attack"] = GLOBAL.State {
        name = "attack",
        tags = {"attack", "notalking", "abouttoattack", "busy"},
        onenter = function(inst)
            local weapon = inst.components.combat:GetWeapon()
            local otherequipped = inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS)
            if (weapon and weapon:HasTag("gun")) or (otherequipped and otherequipped:HasTag("gun")) then
                inst.sg:GoToState("shoot")
                return
            end
            if weapon then
				inst.AnimState:PlayAnimation("atk")
				if weapon:HasTag("icestaff") then
                    inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_icestaff")
				elseif weapon:HasTag("shadow") then
                    inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_nightsword")
                elseif weapon:HasTag("firestaff") then
                    inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_firestaff")
                elseif weapon:HasTag("cutlass") then
                    inst.SoundEmitter:PlaySound("dontstarve_DLC002/common/swordfish_sword")
                elseif weapon:HasTag("pegleg") then
                    inst.SoundEmitter:PlaySound("dontstarve_DLC002/common/pegleg_weapon")
                else
                    inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_weapon")
                end
            elseif otherequipped and (otherequipped:HasTag("light") or otherequipped:HasTag("nopunch")) then
				inst.AnimState:PlayAnimation("atk")
                inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_weapon")
            else
				inst.sg.statemem.slow = true
				inst.AnimState:PlayAnimation("atk")
                inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_whoosh")
            end
            if inst.components.combat.target then
                inst.components.combat:BattleCry()
                if inst.components.combat.target and inst.components.combat.target:IsValid() then
                    inst:FacePoint(Point(inst.components.combat.target.Transform:GetWorldPosition()))
                end
            end
            inst.sg.statemem.target = inst.components.combat.target
            inst.components.combat:StartAttack()
            inst.components.locomotor:Stop()
			if inst:HasTag("banyuewandao") then inst.AnimState:PlayAnimation("chop_loop") inst.components.hunger:DoDelta(-1) end
        end,
        
        timeline=
        {
            TimeEvent(8*FRAMES, function(inst) inst.components.combat:DoAttack(inst.sg.statemem.target) inst.sg:RemoveStateTag("abouttoattack") end),
            TimeEvent(12*FRAMES, function(inst) 
				inst.sg:RemoveStateTag("busy")
			end),				
            TimeEvent(13*FRAMES, function(inst)
				if not inst.sg.statemem.slow then
					inst.sg:RemoveStateTag("attack")
				end
            end),
            TimeEvent(24*FRAMES, function(inst)
				if inst.sg.statemem.slow then
					inst.sg:RemoveStateTag("attack")
				end
            end),
        },
        
        events=
        {
            EventHandler("animover", function(inst)
                inst.sg:GoToState("idle")
            end ),
        },
 }

local function yuyi_items(inst)
	if GetPlayer().prefab == "yuyi" and inst and inst.components.characterspecific then
		inst.components.characterspecific:SetOwner("yuyi")
	end
end

local function yuyi_boss(inst)
	if not inst.components.lootdropper then inst:AddComponent("lootdropper") end
	if inst.components.lootdropper then
    inst.components.lootdropper:AddRandomLoot("zhuanzhi_book", 0.05)
    inst.components.lootdropper:AddRandomLoot("liehuojianfa_book", 0.05)
    inst.components.lootdropper:AddRandomLoot("banyuewandao_book", 0.05)
    inst.components.lootdropper:AddRandomLoot("huanhuashu_book", 0.05)
    inst.components.lootdropper:AddRandomLoot("duanjin_book", 0.05)
    inst.components.lootdropper:AddRandomLoot("huxiaoshanlin_book", 0.05)
    inst.components.lootdropper:AddRandomLoot("zhurijianfa_book", 0.05)
    inst.components.lootdropper:AddRandomLoot("leidianshu_book", 0.05)
    inst.components.lootdropper:AddRandomLoot("mofadun_book", 0.05)
    inst.components.lootdropper:AddRandomLoot("huoqiang_book", 0.05)
    inst.components.lootdropper:AddRandomLoot("diyuleiguang_book", 0.05)
    inst.components.lootdropper:AddRandomLoot("baofengxue_book", 0.05)
    inst.components.lootdropper:AddRandomLoot("zhihuihuanxing_book", 0.05)
    inst.components.lootdropper:AddRandomLoot("redgem", 0.05)
    inst.components.lootdropper:AddRandomLoot("bluegem", 0.05)
    inst.components.lootdropper:AddRandomLoot("purplegem", 0.05)
	inst.components.lootdropper.numrandomloot = 0.8
	end
end

local function yuyi_boss2(inst)
	if not inst.components.lootdropper then inst:AddComponent("lootdropper") end
	if inst.components.lootdropper then
    inst.components.lootdropper:AddRandomLoot("zhuanzhi_book", 0.04)
    inst.components.lootdropper:AddRandomLoot("liehuojianfa_book", 0.04)
    inst.components.lootdropper:AddRandomLoot("banyuewandao_book", 0.04)
    inst.components.lootdropper:AddRandomLoot("huanhuashu_book", 0.04)
    inst.components.lootdropper:AddRandomLoot("duanjin_book", 0.04)
    inst.components.lootdropper:AddRandomLoot("huxiaoshanlin_book", 0.04)
    inst.components.lootdropper:AddRandomLoot("zhurijianfa_book", 0.04)
    inst.components.lootdropper:AddRandomLoot("leidianshu_book", 0.04)
    inst.components.lootdropper:AddRandomLoot("mofadun_book", 0.04)
    inst.components.lootdropper:AddRandomLoot("huoqiang_book", 0.04)
    inst.components.lootdropper:AddRandomLoot("diyuleiguang_book", 0.04)
    inst.components.lootdropper:AddRandomLoot("baofengxue_book", 0.04)
    inst.components.lootdropper:AddRandomLoot("zhihuihuanxing_book", 0.04)
    inst.components.lootdropper:AddRandomLoot("lightninggoat", 0.08)
    inst.components.lootdropper:AddRandomLoot("flower_cave_triple", 0.10)
    inst.components.lootdropper:AddRandomLoot("babybeefalo", 0.10)
    inst.components.lootdropper:AddRandomLoot("catcoon", 0.10)
    inst.components.lootdropper:AddRandomLoot("little_walrus", 0.10)
	inst.components.lootdropper.numrandomloot = 1
	end
end

local function yuyi_leif(inst)
local function WeaponDropped(inst) inst:Remove() end
local function EquipWeapon(inst)
    if inst.components.inventory and not inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS) then
        local weapon = GLOBAL.CreateEntity()
        weapon.entity:AddTransform()
        weapon:AddComponent("weapon")
        weapon.components.weapon:SetDamage(inst.components.combat.defaultdamage)
        weapon.components.weapon:SetRange(inst.components.combat.attackrange, inst.components.combat.attackrange+4)
        weapon.components.weapon:SetProjectile("yuyi_leiflog")
        weapon:AddComponent("inventoryitem")
        weapon.persists = false
        weapon.components.inventoryitem:SetOnDroppedFn(WeaponDropped)
        weapon:AddComponent("equippable")
        inst.components.inventory:Equip(weapon)
		inst.Physics:Stop()
		inst.AnimState:PlayAnimation("transform_ent")
		inst.AnimState:SetMultColour(0/255,150/255,150/255,1)
    end
end
local function OnAttackOther(inst, data)
	if inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS) then
		inst.components.inventory:Unequip(GLOBAL.EQUIPSLOTS.HANDS)
		inst.AnimState:SetMultColour(255/255,255/255,255/255,1)
	end
end
	inst:AddComponent("inventory")
	inst:ListenForEvent("onhitother", OnAttackOther)
	inst.enemyskill = inst:DoPeriodicTask(15, EquipWeapon)
--	inst:ListenForEvent("onattackother", OnAttackOther)
end

local function yuyi_hound(inst)
local function WeaponDropped(inst) inst:Remove() end
local function EquipWeapon(inst)
    if inst.components.inventory and not inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS) then
        local weapon = GLOBAL.CreateEntity()
        weapon.entity:AddTransform()
        weapon:AddComponent("weapon")
        weapon.components.weapon:SetDamage(inst.components.combat.defaultdamage)
        weapon.components.weapon:SetRange(inst.components.combat.attackrange, inst.components.combat.attackrange+4)
		if inst.prefab == "icehound" then
        weapon.components.weapon:SetProjectile("yuyi_ice_projectile")
		elseif inst.prefab == "firehound" then
        weapon.components.weapon:SetProjectile("yuyi_fire_projectile")
		end
        weapon:AddComponent("inventoryitem")
        weapon.persists = false
        weapon.components.inventoryitem:SetOnDroppedFn(WeaponDropped)
        weapon:AddComponent("equippable")
        inst.components.inventory:Equip(weapon)
		inst.Physics:Stop()
		inst.AnimState:PlayAnimation("taunt")
		inst.AnimState:SetMultColour(250/255,150/255,0/255,1)
    end
end
local function OnAttackOther(inst, data)
	if inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS) then
		inst.components.inventory:Unequip(GLOBAL.EQUIPSLOTS.HANDS)
		inst.AnimState:SetMultColour(255/255,255/255,255/255,1)
	end
end
	inst:AddComponent("inventory")
	inst:ListenForEvent("onhitother", OnAttackOther)
	inst.enemyskill = inst:DoPeriodicTask(10, EquipWeapon)
end

AddPrefabPostInit( "icehound", yuyi_hound )
AddPrefabPostInit( "firehound", yuyi_hound )
AddPrefabPostInit( "treeguard", yuyi_leif )
AddPrefabPostInit( "leif_sparse", yuyi_leif )
AddPrefabPostInit( "leif", yuyi_leif )
AddAction(YUYICAST)
AddStategraphActionHandler("wilson", GLOBAL.ActionHandler(YUYICAST, "yuyicast"))
AddStategraphActionHandler("wilsonboating", GLOBAL.ActionHandler(YUYICAST, "yuyicast"))
AddPrefabPostInit("lianqifang", AddMap)
AddMinimapAtlas("images/inventoryimages/lianqifang.xml")
AddPrefabPostInit("yuyi_firesuppressor", AddMap)
AddMinimapAtlas("images/yuyitabs/yuyi_firesuppressor.xml")
AddPrefabPostInit("yuyi_sack1", AddMap)
AddMinimapAtlas("images/inventoryimages/yuyi_sack1.xml")
AddPrefabPostInit("yuyi_sack2", AddMap)
AddMinimapAtlas("images/inventoryimages/yuyi_sack2.xml")
AddPrefabPostInit("yuyi_sack3", AddMap)
AddMinimapAtlas("images/inventoryimages/yuyi_sack3.xml")
AddPrefabPostInit("yuyi_hat1", AddMap)
AddMinimapAtlas("images/inventoryimages/yuyi_hat1.xml")
AddPrefabPostInit("yuyi_hat2", AddMap)
AddMinimapAtlas("images/inventoryimages/yuyi_hat2.xml")
AddPrefabPostInit("yuyi_hat3", AddMap)
AddMinimapAtlas("images/inventoryimages/yuyi_hat3.xml")
AddPrefabPostInit("xueyin", AddMap)
AddMinimapAtlas("images/inventoryimages/xueyin.xml")
AddPrefabPostInit("longya", AddMap)
AddMinimapAtlas("images/inventoryimages/longya.xml")
AddPrefabPostInit("haihun", AddMap)
AddMinimapAtlas("images/inventoryimages/haihun.xml")
AddMinimapAtlas("images/map_icons/yuyi.xml")

table.insert(GLOBAL.CHARACTER_GENDERS.FEMALE, "yuyi")
AddModCharacter("yuyi")
AddSimPostInit(function(inst) PostInit(inst) end)
for k,v in pairs(yiyu_items) do AddPrefabPostInit(v ,function(inst) yuyi_items(inst) end) end
for k,v in pairs(yiyu_bookboss) do AddPrefabPostInit(v ,function(inst) yuyi_boss(inst) end) end
for k,v in pairs(yiyu_bookboss2) do AddPrefabPostInit(v ,function(inst) yuyi_boss2(inst) end) end