safe_zone_hud = GenerateHud()

addhook("movetile","safezone_check")
function safezone_check(id,x,y)
	if inSafeZone(x, y) then
		if not USERTEMP[id]["insafezone"] then
			Hudtxt2(id, safe_zone_hud, "SAFE ZONE", 15, 85)
			USERTEMP[id]["insafezone"] = true
		end
	else
		if USERTEMP[id]["insafezone"] then
			Freehud(id, safe_zone_hud)
			USERTEMP[id]["insafezone"] = nil
		end
	end
end

function safezone_hit(id,source)
	if source and player(source,"exists") then
		if inSafeZone(player(source,"tilex"), player(source,"tiley")) then
			return 1
		end
	elseif inSafeZone(player(id,"tilex"), player(id,"tiley")) then
		return 1
	end
end
CreateHitAttachment(safezone_hit)

function inSafeZone(x,y)
	for _, e in pairs(safe_zone_entity) do
		if x >= e[1] and y >= e[2] and x < e[3] and y < e[4] then
			return {x = e[1], y = e[2]}
		end
	end
end

safe_zone_entity = {}
function Init()
	for _, e in pairs(entitylist()) do
		if entity(e.x, e.y, "type") == 27 and entity(e.x, e.y, "int0") == -100 then
			table.insert(safe_zone_entity, {e.x, e.y, e.x+entity(e.x, e.y, "int2"), e.y+entity(e.x, e.y, "int3")})
		end
	end
end