---
-- StableManureArea
--
-- 
-- Copyright (c) Marhu, 2017 - Danke für die Freigabe
-- Copyright (c) [HoT]DerElky, 2019


StableManureArea = {};
addModEventListener(StableManureArea);

function StableManureArea:loadMap(name)
		
		HusbandryModuleManure.updateManure = StableManureArea.updateManure
		
end;

function StableManureArea:deleteMap()
end;

function StableManureArea:keyEvent(unicode, sym, modifier, isDown)
end;

function StableManureArea:mouseEvent(posX, posY, isDown, isUp, button)
end;

function StableManureArea:update(dt)
end;

function StableManureArea:draw()	
end;

function StableManureArea:updateManure(manureIncrease)
	
	if self.manureArea == nil then
        return;
    end

    local manureDropped = 0;
    local fillType = self.fillTypeIndex;
    local manureArea = self.manureArea;

    if manureIncrease > g_densityMapHeightManager:getMinValidLiterValue(fillType) then
        local xs,_,zs = getWorldTranslation(manureArea.start);
        local xw,_,zw = getWorldTranslation(manureArea.width);
        local xh,_,zh = getWorldTranslation(manureArea.height);

        local ux, uz = xw-xs, zw-zs;
        local vx, vz = xh-xs, zh-zs;

        local vLength = MathUtil.vector2Length(vx,vz);

        local sx = xs + (math.random()*ux) + (math.random()*vx);
        local sz = zs + (math.random()*uz) + (math.random()*vz);

        local ex = xs + (math.random()*ux) + (math.random()*vx);
        local ez = zs + (math.random()*uz) + (math.random()*vz);

        local sy = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, sx,0,sz);
        local ey = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, ex,0,ez);
		
		local dropped, lineOffset = DensityMapHeightUtil.tipToGroundAroundLine(nil, manureIncrease, fillType, sx,sy,sz, ex,ey,ez, 0, vLength, self.lineOffsetManure, false, nil)
        manureDropped = dropped;
        self.lineOffsetManure = lineOffset;
    end
	
	return manureDropped;
end
