local TARGET_TILE = GROUND.TILEFROSTY


require "prefabutil"

local function test_ground(inst, pt)
if not SaveGameIndex:IsModeShipwrecked() then
	local tiletype = GetGroundTypeAtPosition(pt)
	return tiletype == GROUND.DIRT or inst.data.tile == "webbing"
elseif GetWorld():HasTag("shipwrecked") then
	local tiletype = GetGroundTypeAtPosition(pt)
	return tiletype == GROUND.BEACH or inst.data.tile == "webbing"
elseif GetWorld():HasTag("volcano") then
	local tiletype = GetGroundTypeAtPosition(pt)
	return tiletype == GROUND.VOLCANO_ROCK or inst.data.tile == "webbing"
end
end


local function ondeploy(inst, pt, deployer)
	if deployer and deployer.SoundEmitter then
		deployer.SoundEmitter:PlaySound("dontstarve/wilson/dig")
	end

	local ground = GetWorld()
	if ground then
		local original_tile_type = ground.Map:GetTileAtPoint(pt.x, pt.y, pt.z)
		local x, y = ground.Map:GetTileCoordsAtPoint(pt.x, pt.y, pt.z)
		if x and y then
			ground.Map:SetTile(x,y, inst.data.tile)
			ground.Map:RebuildLayer( original_tile_type, x, y )
			ground.Map:RebuildLayer( inst.data.tile, x, y )
		end

		local minimap = TheSim:FindFirstEntityWithTag("minimap")
		if minimap then
			minimap.MiniMap:RebuildLayer( original_tile_type, x, y )
			minimap.MiniMap:RebuildLayer( inst.data.tile, x, y )
		end
	end
	
	inst.components.stackable:Get():Remove()
end

local assets =
{
	Asset("ANIM", "anim/turf.zip"),
    Asset("ANIM", "anim/tilefrosty.zip"),
    Asset("IMAGE", "images/inventoryimages/tilefrosty.tex"),
    Asset("ATLAS", "images/inventoryimages/tilefrosty.xml")
}
	
local prefabs =
{
	"gridplacer",
}


local data = {name = "turf_tilefrosty", anim = "fungus", tile = TARGET_TILE}
	
-- function GetHeatFn(inst)
--    return -20
--end	
	
local function fn(Sim)

	local inst = CreateEntity()
	
    inst.entity:AddTransform()
	inst.entity:AddAnimState()

	
	MakeInventoryPhysics(inst)
	
	inst:AddTag("groundtile")

	inst.AnimState:SetBank("tilefrosty")
    inst.AnimState:SetBuild("tilefrosty")
    --inst.AnimState:PlayAnimation(data.anim)
    inst.AnimState:PlayAnimation("idle")
	
    inst:AddTag("molebait")

	
	inst:AddComponent("stackable")
	inst.components.stackable.maxsize = TUNING.STACK_SIZE_LARGEITEM
		
	inst:AddComponent("inspectable")
	inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.imagename = "tilefrosty"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/tilefrosty.xml"
    --inst.components.inventoryitem:ChangeImageName("turf_carpetfloor")
    inst.data = data
	
    inst:AddComponent("bait")
        
    inst:AddComponent("fuel")
    inst.components.fuel.fuelvalue = TUNING.MED_FUEL
    MakeMediumBurnable(inst, TUNING.MED_BURNTIME)
    MakeSmallPropagator(inst)

	inst:AddComponent("deployable")
	--inst.components.deployable.test = function() return true end
	inst.components.deployable.ondeploy = ondeploy
	inst.components.deployable.test = test_ground
	inst.components.deployable.min_spacing = 0
	inst.components.deployable.placer = "gridplacer"

	---------------------  
	return inst	  
end

return Prefab( "common/objects/turf_tilefrosty", fn, assets, prefabs)
