--[[
    ToolBoxPickupFixes

	Fixes the dynamic mount attacher's behavior to enable pickup of dynamic mounted objects by hand

	@author: 		BayernGamers
	@date: 			25.04.2025
	@version:		1.0

	History:		v1.0 @25.04.2025 - initial implementation in FS 25
                    ------------------------------------------------------------------------------------------------------
	
	License:        Terms:
                        Usage:
                            Feel free to use this work as-is as long as you adhere to the following terms:
						Attribution:
							You must give appropriate credit to the original author when using this work.
						No Derivatives:
							You may not alter, transform, or build upon this work in any way.
						Usage: 
							The work may be used for personal and commercial purposes, provided it is not modified or adapted.
						Additional Clause:
							This script may not be converted, adapted, or incorporated into any other game versions or platforms except by GIANTS Software.
]]
source(Utils.getFilename("scripts/utils/Logger.lua", g_currentModDirectory))

local log = Logger.new(true, Logger.DEBUG_LEVELS.HIGH, "ToolBoxPickupFixes.lua")

ToolBoxPickupFixes = {}

function ToolBoxPickupFixes:getCanPickUpNode(superFunc, nodeId)
	local result = superFunc(self, nodeId)
	local nodeObject = g_currentMission:getNodeObject(nodeId)

    if nodeObject ~= nil then
        if nodeObject.dynamicMountObject ~= nil then
			result = true
		end

		local spec = self.spec_hands
		if not spec.hasSuperStrength and (nodeObject.getCanBePickedUp ~= nil and not nodeObject:getCanBePickedUp(self:getCarryingPlayer())) then
            result = false
        end
	end

    return result
end

HandToolHands.getCanPickUpNode = Utils.overwrittenFunction(HandToolHands.getCanPickUpNode, ToolBoxPickupFixes.getCanPickUpNode)

function ToolBoxPickupFixes:pickUpTarget(superFunc, target, skipEvent)
    
	if self.isServer then
		local nodeObject = g_currentMission:getNodeObject(target.node)

		if nodeObject ~= nil and nodeObject.unmountDynamic ~= nil then
			nodeObject:unmountDynamic()
		end
	end

	return superFunc(self, target, skipEvent)
end

HandToolHands.pickUpTarget = Utils.overwrittenFunction(HandToolHands.pickUpTarget, ToolBoxPickupFixes.pickUpTarget)