--[[
	CareWheelFix.lua

	Specialization to let the care wheels be care wheels. Thanks GIANTS. :)
	
	Autor: 		Ifko[nator]
	Datum: 		19.04.2019
	Version: 	1.0
	
	Changelog:	V 1.0 @ 19.04.2019 - initial implementation in FS 19
]]

CareWheelFix = {};

function CareWheelFix.prerequisitesPresent(specializations)
	return true;
end;

function CareWheelFix.registerEventListeners(vehicleType)
	SpecializationUtil.registerEventListener(vehicleType, "onLoad", CareWheelFix);
end;

function CareWheelFix:onLoad(savegame)
	self.getIsWheelFoliageDestructionAllowed = Utils.overwrittenFunction(self.getIsWheelFoliageDestructionAllowed, CareWheelFix.getIsWheelFoliageDestructionAllowed);
end;

function CareWheelFix:getIsWheelFoliageDestructionAllowed(superFunc, wheel)
	local key, _ = ConfigurationUtil.getXMLConfigurationKey(self.xmlFile, self.configurations["wheel"], "vehicle.wheels.wheelConfigurations.wheelConfiguration", "vehicle", "wheels");
	local configName = getXMLString(self.xmlFile, key .. "#name");

	if configName == "$l10n_configuration_valueWheelNarrow" 
		or configName == "$l10n_configuration_valueTwinWheelsNarrow" 
		or not g_currentMission.missionInfo.fruitDestruction
		or wheel.contact ~= Wheels.WHEEL_GROUND_CONTACT
		or wheel.isCareWheel
	then
		return false;
	end;
	
    if self.getBlockFoliageDestruction ~= nil then
        if self:getBlockFoliageDestruction() then
            return false;
        end;
	end;

    return superFunc;
end;