--
-- FS25 - Firewood Sell Point v1.0.0.0
-- @author:    	kenny456 (kenny456@seznam.cz)
-- @history:	v1.0.0.0 - 2020-02-20 - first release
-- 				v1.1.1.0 - 2022-12-08 - fixed LUA error on Platinum Edition DLC
-- Copyright (C) kenny456, All Rights Reserved.
-- converted by Sablerock
-- date: 2026/06/04
-- v1.1.1.0 - 2026-06-04 - fixed MathUtil deprecation for FS25


FirewoodSellPoint = {}

function FirewoodSellPoint:calculateWoodBaseValue(superFunc, objectId)
    local volume = getVolume(objectId)
    local splitType = g_splitShapeManager:getSplitTypeByIndex(getSplitType(objectId))
    local sizeX, sizeY, sizeZ, numConvexes, numAttachments = getSplitShapeStats(objectId)

    return self:calculateWoodBaseValueForData(volume, splitType, sizeX, sizeY, sizeZ, numConvexes, numAttachments)
end

function FirewoodSellPoint:calculateWoodBaseValueForData(superFunc, volume, splitType, sizeX, sizeY, sizeZ, numConvexes, numAttachments)
	
	local isFirewood = false
	local price = 0
	local maxSize = 0
	if sizeX < 0.8 and volume < 0.25 then
		isFirewood = true
	end
	if self.isFirewoodSellPoint == nil then
		if self.triggerNode ~= nil then
			self.isFirewoodSellPoint = Utils.getNoNil(getUserAttribute(self.triggerNode, "isFirewoodSellPoint"), false)
		else
			self.isFirewoodSellPoint = false
		end
	end
	if isFirewood and self.isFirewoodSellPoint then
		price = splitType.pricePerLiter * 2.5
	else
		local qualityScale = 1
		local lengthScale = 1
		local defoliageScale = 1
		if sizeX ~= nil and volume > 0 then
			local bvVolume = sizeX*sizeY*sizeZ
			local volumeRatio = bvVolume / volume
			-- FS25 Fix: MathUtil.clamp changed to math.clamp
			local volumeQuality = 1-math.sqrt(math.clamp((volumeRatio-3)/7, 0,1)) * 0.95  --  ratio <= 3: 100%, ratio >= 10: 5%
			-- FS25 Fix: MathUtil.clamp changed to math.clamp
			local convexityQuality = 1-math.clamp((numConvexes-2)/(6-2), 0,1) * 0.95  -- 0-2: 100%:, >= 6: 5%

			maxSize = math.max(sizeX, sizeY, sizeZ)
			-- 1m: 60%, 6-11m: 120%, 19m: 60%
			if maxSize < 11 then
				lengthScale = 0.6 + math.min(math.max((maxSize-1)/5, 0), 1)*0.6
			else
				lengthScale = 1.2 - math.min(math.max((maxSize-11)/8, 0), 1)*0.6
			end

			local minQuality = math.min(convexityQuality, volumeQuality)
			local maxQuality = math.max(convexityQuality, volumeQuality)
			qualityScale = minQuality + (maxQuality - minQuality) * 0.3  -- use 70% of min quality

			defoliageScale = 1-math.min(numAttachments/15, 1) * 0.8  -- #attachments 0: 100%, >=15: 20%
		end

		 -- Only take 33% into account of the quality criteria on low
		qualityScale = MathUtil.lerp(1, qualityScale, g_currentMission.missionInfo.economicDifficulty / 3)
		defoliageScale = MathUtil.lerp(1, defoliageScale, g_currentMission.missionInfo.economicDifficulty / 3)
		price = splitType.pricePerLiter * qualityScale * defoliageScale * lengthScale
	end
    return volume * splitType.volumeToLiter, price, maxSize
end

if WoodUnloadTrigger ~= nil and not WoodUnloadTrigger.firewoodRegistered then
    WoodUnloadTrigger.calculateWoodBaseValue = Utils.overwrittenFunction(WoodUnloadTrigger.calculateWoodBaseValue, FirewoodSellPoint.calculateWoodBaseValue)
    WoodUnloadTrigger.calculateWoodBaseValueForData = Utils.overwrittenFunction(WoodUnloadTrigger.calculateWoodBaseValueForData, FirewoodSellPoint.calculateWoodBaseValueForData)
	WoodUnloadTrigger.firewoodRegistered = true
end