baleLoaderDDModPassion = {};
function baleLoaderDDModPassion.prerequisitesPresent(specializations) return SpecializationUtil.hasSpecialization(Attachable, specializations); end;
function baleLoaderDDModPassion:load(xmlFile) self.objectInRange = baleLoaderDDModPassion.objectInRange;
	self.primaBale = SpecializationUtil.callSpecializationsFunction("primaBale");
	self.secondaBale = SpecializationUtil.callSpecializationsFunction("secondaBale");
	self.terzaBale = SpecializationUtil.callSpecializationsFunction("terzaBale");
	self.quartaBale = SpecializationUtil.callSpecializationsFunction("quartaBale");
	self.setcurrentBalle = SpecializationUtil.callSpecializationsFunction("setcurrentBalle");
	self.setcurrentSupport = SpecializationUtil.callSpecializationsFunction("setcurrentSupport");
	self.setcurrentPiloni = SpecializationUtil.callSpecializationsFunction("setcurrentPiloni"); self.currentPiloni = 0;
	self.setcurrentBaleSize = SpecializationUtil.callSpecializationsFunction("setcurrentBaleSize"); self.currentBaleSize = 0;
	self.lockPrima = {}; self.lockPrima.node = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.lockPrima#index"));
	self.lockPrima.child = getChildAt(self.lockPrima.node, 0); self.lockPrima.baleId = nil;
	self.lockSeconda = {}; self.lockSeconda.node = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.lockSeconda#index"));
	self.lockSeconda.child = getChildAt(self.lockSeconda.node, 0); self.lockSeconda.baleId = nil;
	self.lockTerza = {}; self.lockTerza.node = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.lockTerza#index"));
	self.lockTerza.child = getChildAt(self.lockTerza.node, 0); self.lockTerza.baleId = nil;
	self.lockQuarta = {}; self.lockQuarta.node = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.lockTerza#index"));
	self.lockQuarta.child = getChildAt(self.lockQuarta.node, 0); self.lockQuarta.baleId = nil;
	self.fillLevel = 0; self.setBaleLoader = 0; self.setmovingDirection = 0; self.showInvalidBaleWarning = false; self.currentPallet = 1;
	self.capacity = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.capacity"), 0.0);
	if self.capacity > 100 then self.capacityBale = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.capacityBale"), 0.0); end;
 	self.baleNSilageFrame = Utils.getFilename("image/baleNSilage.dds", self.baseDirectory);
	self.baleNSilageF = Overlay:new("baleNSilageF", self.baleNSilageFrame, 0.96, 0.06, 0.04, 0.085);
	self.baleYSilageFrame = Utils.getFilename("image/baleYSilage.dds", self.baseDirectory);
	self.baleYSilageF = Overlay:new("baleYSilageF", self.baleYSilageFrame, 0.96, 0.06, 0.04, 0.085);
	self.baleSquareFrame = Utils.getFilename("image/baleSquare.dds", self.baseDirectory);
	self.baleSquareF = Overlay:new("baleSquareF", self.baleSquareFrame, 0.96, 0.06, 0.04, 0.085);
	self.baleRoundNSilageFrame = Utils.getFilename("image/baleRoundNSilage.dds", self.baseDirectory);
	self.baleRoundNSilageF = Overlay:new("baleRoundNSilageF", self.baleRoundNSilageFrame, 0.96, 0.06, 0.04, 0.085); end;
function baleLoaderDDModPassion:delete() self:primaBale(nil, false); self:secondaBale(nil, false); self:terzaBale(nil, false); self:quartaBale(nil, false); end;
function baleLoaderDDModPassion:readStream(streamId, connection) self:setcurrentBalle(streamReadInt8(streamId), true); self:setcurrentSupport(streamReadBool(streamId), true); self:setcurrentPiloni(streamReadInt8(streamId), true); self:setcurrentBaleSize(streamReadInt8(streamId), true);
	self:primaBale(streamReadInt32(streamId), true); self:secondaBale(streamReadInt32(streamId), true); self:terzaBale(streamReadInt32(streamId), true); self:quartaBale(streamReadInt32(streamId), true); end;
function baleLoaderDDModPassion:writeStream(streamId, connection) streamWriteInt8(streamId, self.currentBalle); streamWriteBool(streamId, self.currentSupport); streamWriteInt8(streamId, self.currentPiloni); streamWriteInt8(streamId, self.currentBaleSize);
	streamWriteInt32(streamId, self.lockPrima.baleId); streamWriteInt32(streamId, self.lockSeconda.baleId); streamWriteInt32(streamId, self.lockTerza.baleId); streamWriteInt32(streamId, self.lockQuarta.baleId); end;
function baleLoaderDDModPassion:setcurrentBalle(int, noEventSend) self.currentBalle = int; setcurrentBalleEvent.sendEvent(self, int, noEventSend); end;
function baleLoaderDDModPassion:setcurrentSupport(int, noEventSend) self.currentSupport = int; setcurrentSupportEvent.sendEvent(self, int, noEventSend); end;
function baleLoaderDDModPassion:setcurrentPiloni(int, noEventSend) self.currentPiloni = int; setcurrentPiloniEvent.sendEvent(self, int, noEventSend); end;
function baleLoaderDDModPassion:setcurrentBaleSize(int, noEventSend) self.currentBaleSize = int; setcurrentBaleSizeEvent.sendEvent(self, int, noEventSend); end;
function baleLoaderDDModPassion:loadFromAttributesAndNodes(xmlFile, key, resetVehicles) local currentBalle = getXMLInt(xmlFile, key .. "#currentBalle"); if currentBalle ~= nil then self:setcurrentBalle(currentBalle); end; self.fillLevel = 0; return BaseMission.VEHICLE_LOAD_OK; end;
function baleLoaderDDModPassion:getSaveAttributesAndNodes(nodeIdent) local attributes = 'currentBalle="' .. self.currentBalle .. '"'; self:primaBale(nil, false); self:secondaBale(nil, false); self:terzaBale(nil, false); self:quartaBale(nil, false); self.setBaleLoader = 0; return attributes; end;
function baleLoaderDDModPassion:mouseEvent(posX, posY, isDown, isUp, button) end;
function baleLoaderDDModPassion:keyEvent(unicode, sym, modifier, isDown) end;
function baleLoaderDDModPassion:update(dt)
	if self:getIsActive() and self:getIsActiveForInput() and self.currentAttacco > 0 and self.currentSupport then
		if self.currentPunte == 1 then if self.currentBalle > -1 then self.showInvalidBaleWarning = false;
			if self.setBaleLoader == 0 then	if self.sceltaPunteA == 3 or self.sceltaPunteB == 3 or self.sceltaPunteB == 7 or self.sceltaPunteC == 3 then self:setcurrentBalle(0); else self:setcurrentBalle(1); end; if self.sceltaPunteB == 5 then self:setcurrentBalle(2); end; if self.sceltaPunteD == 3 then self:setcurrentBalle(3); end; if self.sceltaPunteE == 3 then self:setcurrentBalle(4); end; end; 
			if self.lockPrima.baleId == nil then self.fillLevel = 0; local balePrima = self:objectInRange(self.lockPrima);
				if balePrima == nil and self.setBaleLoader > 0 then self.setBaleLoader = self.setBaleLoader - 1; end;
				if balePrima ~= nil and self.setBaleLoader == 0 and self.movingDirection == 1 then self:primaBale(networkGetObjectId(balePrima), false); self.setBaleLoader = 1; end; end;
			if InputBinding.hasEvent(InputBinding.baleLoaderUnlock) and self.setmovingDirection < 100 then self:primaBale(nil, false); end;
			if self.lockSeconda.baleId == nil then local baleSeconda = self:objectInRange(self.lockSeconda);
				if baleSeconda == nil and self.setBaleLoader > 1 then self.setBaleLoader = self.setBaleLoader - 1; end;
				if baleSeconda ~= nil and self.lockPrima.baleId ~= nil and self.movingDirection == 1 then 
					if (self.currentBalle == 1 or self.currentBalle == 3) and self.currentPiloni == 3 then self:secondaBale(networkGetObjectId(baleSeconda), false); self.setBaleLoader = self.fillLevel;
					elseif self.currentBalle == 0 or self.currentBalle == 2 or self.currentBalle == 4 or (self.currentBalle == 5 and self.currentPiloni == 3) then self:secondaBale(networkGetObjectId(baleSeconda), false); self.setBaleLoader = self.fillLevel; end; end; end;
			if InputBinding.hasEvent(InputBinding.baleLoaderUnlock) and self.setmovingDirection < 100 then self:secondaBale(nil, false); end;
			if self.lockTerza.baleId == nil and self.currentPiloni == 3 and (self.currentBalle == 2 or self.currentBalle == 4 or self.currentBalle == 5) then local baleTerza = self:objectInRange(self.lockTerza);
				if baleTerza == nil and self.setBaleLoader > 2 then self.setBaleLoader = self.setBaleLoader - 1; end;
				if baleTerza ~= nil and self.lockPrima.baleId ~= nil and self.lockSeconda.baleId ~= nil and self.movingDirection == 1 then 
					if (self.currentBalle == 2 or self.currentBalle == 4 or self.currentBalle == 5) and self.currentPiloni == 3 then self:terzaBale(networkGetObjectId(baleTerza), false); self.setBaleLoader = self.fillLevel; end; end; end;
			if InputBinding.hasEvent(InputBinding.baleLoaderUnlock) and self.setmovingDirection < 100 then self:terzaBale(nil, false); end;
			if self.lockQuarta.baleId == nil and self.currentBalle == 4 and self.currentPiloni == 3 then local baleQuarta = self:objectInRange(self.lockQuarta);
				if baleQuarta == nil and self.setBaleLoader > 3 then self.setBaleLoader = self.setBaleLoader - 1; end;
				if baleQuarta ~= nil and self.lockPrima.baleId ~= nil and self.lockSeconda.baleId ~= nil and self.lockTerza.baleId ~= nil and self.movingDirection == 1 then
					if self.currentBalle == 4 and self.currentPiloni == 3 then self:quartaBale(networkGetObjectId(baleQuarta), false); self.setBaleLoader = self.fillLevel; end; end; end;
			if InputBinding.hasEvent(InputBinding.baleLoaderUnlock) and self.setmovingDirection < 100 then self:quartaBale(nil, false); end; end;
		elseif self.currentPunte > 1 then g_currentMission:addWarning(string.format(g_i18n:getText("baleLoaderWarning")), 0, 0); end;
		if InputBinding.hasEvent(InputBinding.change) and self.setBaleLoader == 0 and not self.balesAttached then if self.currentBalle > -1 then self:setcurrentBalle(-1); else self:setcurrentBalle(0); end; end; end; end;
function baleLoaderDDModPassion:updateTick(dt)
	if self.currentBalle == 1 then self:playAnimation("collisione", 1, nil, true); else self:playAnimation("collisione", -1, nil, true); end; 
	if self.movingDirection == 0 then self.setmovingDirection = 0; end; if self.movingDirection > 0 then self.setmovingDirection = self.setmovingDirection + dt; end; end;
function baleLoaderDDModPassion:draw()
	if self.fillLevel > 0 and self.currentBalle > -1 then
		if self.setmovingDirection < 100 then g_currentMission:addHelpButtonText(g_i18n:getText("baleLoaderUnlock"), InputBinding.baleLoaderUnlock);
		else g_currentMission:addExtraPrintText(g_i18n:getText("baleLoaderUnlockWarning")); end; end;
	if self.setBaleLoader == 0 and self.currentAttacco > 0 and (self.currentPunte == 1 or self.currentBalle ~= nil) and self.currentSupport and not self.balesAttached then
		if self.currentBalle > -1 then g_currentMission:addHelpButtonText(g_i18n:getText("AutoOn"), InputBinding.change);
		else g_currentMission:addHelpButtonText(g_i18n:getText("AutoOff"), InputBinding.change); end; end; 
	if self.showInvalidBaleWarning and self.currentBalle > -1 then g_currentMission:showBlinkingWarning(g_i18n:getText("InvalidBaleWarning")); end;
	if self.currentBalle == 0 then self.baleNSilageF:render(); self.capacity = 2;
	elseif self.currentBalle == 1 and self.currentPiloni == 3 then self.baleYSilageF:render(); self.capacity = 2;
	elseif self.currentBalle == 1 and self.currentPiloni == 1 then self.baleYSilageF:render(); self.capacity = 1;
	elseif self.currentBalle == 2 and self.currentPiloni == 3 then self.baleSquareF:render(); self.capacity = 3;
	elseif self.currentBalle == 2 and self.currentPiloni == 1 then self.baleSquareF:render(); self.capacity = 2;
	elseif self.currentBalle == 3 and self.currentPiloni == 3 then self.baleRoundNSilageF:render(); self.capacity = 2; 
	elseif self.currentBalle == 3 and self.currentPiloni == 1 then self.baleRoundNSilageF:render(); self.capacity = 1; 
	elseif self.currentBalle == 4 and self.currentPiloni == 3 then self.baleNSilageF:render(); self.capacity = 4; 
	elseif self.currentBalle == 4 and self.currentPiloni == 1 then self.baleNSilageF:render(); self.capacity = 2;
	elseif self.currentBalle == 5 and self.currentPiloni == 3 then self.baleNSilageF:render(); self.capacity = 3; 
	elseif self.currentBalle == 5 and self.currentPiloni == 1 then self.baleNSilageF:render(); self.capacity = 1; end;
	if self.currentBalle == 0 or self.currentBalle == 1 or self.currentBalle == 3 then
		if self.lockPrima.baleId ~= nil and self.fillLevel > 0 then baleP = networkGetObject(self.lockPrima.baleId); setTextColor(1,1,1,1); renderText(0.98,0.088,0.015,string.format("%d",baleP.baleDiameter*100.001)); end;
		if self.lockSeconda.baleId ~= nil and self.fillLevel > 1 then baleS = networkGetObject(self.lockSeconda.baleId); setTextColor(1,1,1,1); renderText(0.98,0.102,0.015,string.format("%d",baleS.baleDiameter*100.001)); end; end;
	if self.currentBalle == 4 then
		if self.lockPrima.baleId ~= nil and self.fillLevel > 0 then baleP = networkGetObject(self.lockPrima.baleId); setTextColor(1,1,1,1); renderText(0.96,0.088,0.015,string.format("%d",baleP.baleDiameter*100.001)); end;
		if self.lockSeconda.baleId ~= nil and self.fillLevel > 1 then baleS = networkGetObject(self.lockSeconda.baleId); setTextColor(1,1,1,1); renderText(0.98,0.088,0.015,string.format("%d",baleS.baleDiameter*100.001)); end; 
		if self.lockTerza.baleId ~= nil and self.fillLevel > 2 then baleT = networkGetObject(self.lockTerza.baleId); setTextColor(1,1,1,1); renderText(0.96,0.132,0.015,string.format("%d",baleP.baleDiameter*100.001)); end;
		if self.lockQuarta.baleId ~= nil and self.fillLevel > 3 then baleQ = networkGetObject(self.lockQuarta.baleId); setTextColor(1,1,1,1); renderText(0.98,0.132,0.015,string.format("%d",baleS.baleDiameter*100.001)); end; end; end;
function baleLoaderDDModPassion:onAttach(attacherVehicle) end;
function baleLoaderDDModPassion:onDetach() self:setcurrentBalle(0); self:playAnimation("collisione", -1, nil, true); self:primaBale(nil, false); self:secondaBale(nil, false); self:terzaBale(nil, false); self:quartaBale(nil, false); self.setBaleLoader = 0; self.fillLevel = 0; end;
function baleLoaderDDModPassion:primaBale(baleId, noEventSend)
	if baleId ~= nil then if self.lockPrima.child ~= nil then local bale = nil;
		if self.lockPrima.baleId ~= nil then bale = networkGetObject(self.lockPrima.baleId); if bale ~= nil then bale:unmount(); self.lockPrima.baleId = nil; end; end;	primaBaleEvent.sendEvent(self, baleId, noEventSend); bale = networkGetObject(baleId);
		if bale ~= nil then if getUserAttribute(bale.nodeId, "isRoundbale") then local baleSizeP = 0;
			if bale.baleDiameter > 0.99 then baleSizeP = 0.000; end; if bale.baleDiameter > 1.31 then baleSizeP = 0.078; end; if bale.baleDiameter > 1.41 then baleSizeP = 0.141; end; if bale.baleDiameter > 1.51 then baleSizeP = 0.168; end; if bale.baleDiameter > 1.61 then baleSizeP = 0.348; end; self:setcurrentBaleSize(baleSizeP);
			if self.currentBalle == 1 or self.currentBalle == 3 then bale:mount(self, self.lockPrima.child, 0-self.currentBaleSize,0.605,0.3, math.pi/2,math.pi/2,0); self.lockPrima.baleId = baleId; self.fillLevel = self.fillLevel + 1;
			elseif self.currentBalle == 0 then bale:mount(self, self.lockPrima.child, 0,0,0.95+self.currentBaleSize, 0,math.pi/2,0); self.lockPrima.baleId = baleId; self.fillLevel = self.fillLevel + 1;
			elseif self.currentBalle == 4 then bale:mount(self, self.lockPrima.child, 0-self.currentBaleSize,0.605,0.95+self.currentBaleSize, math.pi/2,math.pi/2,0); self.lockPrima.baleId = baleId; self.fillLevel = self.fillLevel + 1; end;
		elseif self.currentBalle == 4 then self:setcurrentBalle(5); bale:mount(self, self.lockPrima.child, 0,0.42,0.3, 0,0,0); self.lockPrima.baleId = baleId; self.fillLevel = self.fillLevel + 1;
		elseif self.currentBalle ~= 3 then self:setcurrentBalle(2); bale:mount(self, self.lockPrima.child, 0,0,0.3, 0,0,0); self.lockPrima.baleId = baleId; self.fillLevel = self.fillLevel + 1; end; end; end;
	else primaBaleEvent.sendEvent(self, baleId, noEventSend);
	if self.lockPrima.baleId ~= nil then local bale = networkGetObject(self.lockPrima.baleId); if bale ~= nil then bale:unmount(); self.lockPrima.baleId = nil; end; end; end; end;
function baleLoaderDDModPassion:secondaBale(baleId, noEventSend)
	if baleId ~= nil then if self.lockSeconda.child ~= nil then local bale = nil;
		if self.lockSeconda.baleId ~= nil then bale = networkGetObject(self.lockSeconda.baleId); if bale ~= nil then bale:unmount(); self.lockSeconda.baleId = nil; end; end; secondaBaleEvent.sendEvent(self, baleId, noEventSend); bale = networkGetObject(baleId);
		if bale ~= nil then	if getUserAttribute(bale.nodeId, "isRoundbale") then local baleSizeS = 0;
			if bale.baleDiameter > 0.99 then baleSizeS = 0.000; end; if bale.baleDiameter > 1.31 then baleSizeS = 0.078; end; if bale.baleDiameter > 1.41 then baleSizeS = 0.141; end; if bale.baleDiameter > 1.51 then baleSizeS = 0.168; end; if bale.baleDiameter > 1.61 then baleSizeS = 0.348; end; self:setcurrentBaleSize(baleSizeS);
			if self.currentBalle == 1 or self.currentBalle == 3 then bale:mount(self, self.lockPrima.child, 0-self.currentBaleSize,1.73,0.3, math.pi/2,math.pi/2,0); self.lockSeconda.baleId = baleId; self.fillLevel = self.fillLevel + 1; 
			elseif self.currentBalle == 0 then bale:mount(self, self.lockPrima.child, 0,0,-0.35-self.currentBaleSize, 0,math.pi/2,0); self.lockSeconda.baleId = baleId; self.fillLevel = self.fillLevel + 1; 
			elseif self.currentBalle == 4 then bale:mount(self, self.lockPrima.child, 0-self.currentBaleSize,0.605,-0.35-self.currentBaleSize, math.pi/2,math.pi/2,0); self.lockSeconda.baleId = baleId; self.fillLevel = self.fillLevel + 1; end;
		elseif not getUserAttribute(bale.nodeId, "isRoundbale") and self.currentBalle == 2 then bale:mount(self, self.lockPrima.child, 0,0.8,0.3, 0,0,0); self.lockSeconda.baleId = baleId; self.fillLevel = self.fillLevel + 1;
		elseif not getUserAttribute(bale.nodeId, "isRoundbale") and self.currentBalle == 5 then bale:mount(self, self.lockPrima.child, 0,1.22,0.3, 0,0,0); self.lockSeconda.baleId = baleId; self.fillLevel = self.fillLevel + 1; end; end; end;
	else secondaBaleEvent.sendEvent(self, baleId, noEventSend);
	if self.lockSeconda.baleId ~= nil then local bale = networkGetObject(self.lockSeconda.baleId); if bale ~= nil then bale:unmount(); self.lockSeconda.baleId = nil; end; end; end; end;
function baleLoaderDDModPassion:terzaBale(baleId, noEventSend)
	if baleId ~= nil then if self.lockTerza.child ~= nil then local bale = nil;
		if self.lockTerza.baleId ~= nil then bale = networkGetObject(self.lockTerza.baleId); if bale ~= nil then bale:unmount(); self.lockTerza.baleId = nil; end; end; terzaBaleEvent.sendEvent(self, baleId, noEventSend); bale = networkGetObject(baleId);
		if bale ~= nil then if getUserAttribute(bale.nodeId, "isRoundbale") then local baleSizeT = 0;
			if bale.baleDiameter > 0.99 then baleSizeT = 0.000; end; if bale.baleDiameter > 1.31 then baleSizeT = 0.078; end; if bale.baleDiameter > 1.41 then baleSizeT = 0.141; end; if bale.baleDiameter > 1.51 then baleSizeT = 0.168; end; if bale.baleDiameter > 1.61 then baleSizeT = 0.348; end; self:setcurrentBaleSize(baleSizeT);
			if self.currentBalle == 4 then bale:mount(self, self.lockPrima.child, 0-self.currentBaleSize,1.73,0.95+self.currentBaleSize, math.pi/2,math.pi/2,0); self.lockTerza.baleId = baleId; self.fillLevel = self.fillLevel + 1; end;
		elseif not getUserAttribute(bale.nodeId, "isRoundbale") and self.currentBalle == 2 then bale:mount(self, self.lockPrima.child, 0,1.6,0.3, 0,0,0); self.lockTerza.baleId = baleId; self.fillLevel = self.fillLevel + 1;
		elseif not getUserAttribute(bale.nodeId, "isRoundbale") and self.currentBalle == 5 then bale:mount(self, self.lockPrima.child, 0,2.02,0.3, 0,0,0); self.lockTerza.baleId = baleId; self.fillLevel = self.fillLevel + 1; end; end; end;
	else terzaBaleEvent.sendEvent(self, baleId, noEventSend);
	if self.lockTerza.baleId ~= nil then local bale = networkGetObject(self.lockTerza.baleId); if bale ~= nil then bale:unmount(); self.lockTerza.baleId = nil; end; end; end; end;
function baleLoaderDDModPassion:quartaBale(baleId, noEventSend)
	if baleId ~= nil then if self.lockQuarta.child ~= nil then local bale = nil;
		if self.lockQuarta.baleId ~= nil then bale = networkGetObject(self.lockQuarta.baleId); if bale ~= nil then bale:unmount(); self.lockQuarta.baleId = nil; end; end; quartaBaleEvent.sendEvent(self, baleId, noEventSend); bale = networkGetObject(baleId);
		if bale ~= nil then if getUserAttribute(bale.nodeId, "isRoundbale") then local baleSizeQ = 0; 
			if bale.baleDiameter > 0.99 then baleSizeQ = 0.000; end; if bale.baleDiameter > 1.31 then baleSizeQ = 0.078; end; if bale.baleDiameter > 1.41 then baleSizeQ = 0.141; end; if bale.baleDiameter > 1.51 then baleSizeQ = 0.168; end; if bale.baleDiameter > 1.61 then baleSizeQ = 0.348; end; self:setcurrentBaleSize(baleSizeQ);
			if self.currentBalle == 4 then bale:mount(self, self.lockPrima.child, 0-self.currentBaleSize,1.73,-0.35-self.currentBaleSize, math.pi/2,math.pi/2,0); self.lockQuarta.baleId = baleId; self.fillLevel = self.fillLevel + 1; end; end; end; end;
	else quartaBaleEvent.sendEvent(self, baleId, noEventSend);
	if self.lockQuarta.baleId ~= nil then local bale = networkGetObject(self.lockQuarta.baleId); if bale ~= nil then bale:unmount(); self.lockQuarta.baleId = nil; end; end; end; end;
function baleLoaderDDModPassion:objectInRange(refNode) local nearestDistance = 2; local nearestBale = nil; local px, py, pz = getWorldTranslation(refNode.node);
    for index, item in pairs(g_currentMission.itemsToSave) do if item.item:isa(Bale) then local vx, vy, vz = getWorldTranslation(item.item.nodeId); local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
		if distance < nearestDistance then
			if getUserAttribute(item.item.nodeId, "isRoundbale") and item.item.baleDiameter ~= nil then
				if self.currentBalle == 0 then if item.item.wrappingState == 0 then nearestDistance = distance; nearestBale = item.item; 
					elseif item.item.wrappingState > 0 then self.showInvalidBaleWarning = true; end; end;
				if self.currentBalle == 1 then if self.lockPrima.baleId == nil then baleSizeCS = item.item.baleDiameter; end;
					if item.item.baleDiameter > 0.99 and self.lockPrima.baleId == nil then nearestDistance = distance; nearestBale = item.item;
					elseif item.item.baleDiameter < 1 and self.lockPrima.baleId == nil then self.showInvalidBaleWarning = true;
					elseif item.item.baleDiameter <= baleSizeCS and self.lockSeconda.baleId == nil and self.lockPrima.baleId ~= nil then nearestDistance = distance; nearestBale = item.item;
					elseif item.item.baleDiameter > baleSizeCS and self.lockSeconda.baleId == nil and self.lockPrima.baleId ~= nil then self.showInvalidBaleWarning = true; end; end;
				if self.currentBalle == 3 then if self.lockPrima.baleId == nil then baleSizeCS = item.item.baleDiameter; end;
					if item.item.baleDiameter > 1.19 and self.lockPrima.baleId == nil and item.item.wrappingState == 0 then nearestDistance = distance; nearestBale = item.item;
					elseif item.item.baleDiameter < 1.20 and self.lockPrima.baleId == nil and item.item.wrappingState > 0 then self.showInvalidBaleWarning = true;
					elseif item.item.baleDiameter == baleSizeCS and self.lockSeconda.baleId == nil and self.lockPrima.baleId ~= nil and item.item.wrappingState == 0 then nearestDistance = distance; nearestBale = item.item;
					elseif item.item.baleDiameter ~= baleSizeCS and self.lockSeconda.baleId == nil and self.lockPrima.baleId ~= nil and self.currentPiloni == 3 and item.item.wrappingState > 0 then self.showInvalidBaleWarning = true; end; end;
				if self.currentBalle == 4 then if self.lockPrima.baleId == nil then baleSizeCS = item.item.baleDiameter; end; if self.lockSeconda.baleId == nil then baleSizeCSB = item.item.baleDiameter; end;
					if item.item.baleDiameter > 1.19 and item.item.baleDiameter < 1.51 and self.lockPrima.baleId == nil and item.item.wrappingState == 0 then nearestDistance = distance; nearestBale = item.item;
					elseif item.item.baleDiameter < 1.20 and item.item.baleDiameter > 1.51 and self.lockPrima.baleId == nil and item.item.wrappingState > 0 then self.showInvalidBaleWarning = true;
					elseif item.item.baleDiameter > 1.19 and item.item.baleDiameter < 1.51 and self.lockSeconda.baleId == nil and self.lockPrima.baleId ~= nil and item.item.wrappingState == 0 then nearestDistance = distance; nearestBale = item.item;
					elseif item.item.baleDiameter < 1.20 and item.item.baleDiameter > 1.51 and self.lockSeconda.baleId == nil and self.lockPrima.baleId ~= nil and item.item.wrappingState > 0 then self.showInvalidBaleWarning = true; 
					elseif item.item.baleDiameter <= baleSizeCS and self.lockTerza.baleId == nil and self.lockSeconda.baleId ~= nil and self.lockPrima.baleId ~= nil and self.currentPiloni == 3 and item.item.wrappingState == 0 then nearestDistance = distance; nearestBale = item.item;
					elseif item.item.baleDiameter > baleSizeCS and self.lockTerza.baleId == nil and self.lockSeconda.baleId ~= nil and self.lockPrima.baleId ~= nil and self.currentPiloni == 3 and item.item.wrappingState > 0 then self.showInvalidBaleWarning = true;
					elseif item.item.baleDiameter <= baleSizeCSB and self.lockQuarta.baleId == nil and self.lockTerza.baleId ~= nil and self.lockSeconda.baleId ~= nil and self.lockPrima.baleId ~= nil and self.currentPiloni == 3 and item.item.wrappingState == 0 then nearestDistance = distance; nearestBale = item.item;
					elseif item.item.baleDiameter > baleSizeCSB and self.lockQuarta.baleId == nil and self.lockTerza.baleId ~= nil and self.lockSeconda.baleId ~= nil and self.lockPrima.baleId ~= nil and self.currentPiloni == 3 and item.item.wrappingState > 0 then self.showInvalidBaleWarning = true; end; end;
			elseif not getUserAttribute(item.item.nodeId, "isRoundbale") then nearestDistance = distance; nearestBale = item.item; end; end; end; end; return nearestBale; end;
primaBaleEvent = {}; primaBaleEvent_mt = Class(primaBaleEvent, Event); InitEventClass(primaBaleEvent, "primaBaleEvent");
function primaBaleEvent:emptyNew() local self = Event:new(primaBaleEvent_mt); self.className="primaBaleEvent"; return self; end;
function primaBaleEvent:new(vehicle, baleId) local self = primaBaleEvent:emptyNew() self.vehicle = vehicle; self.baleId = baleId; return self; end;
function primaBaleEvent:readStream(streamId, connection) local id = streamReadInt32(streamId); self.baleId = streamReadInt32(streamId); self.vehicle = networkGetObject(id); self:run(connection); end;
function primaBaleEvent:writeStream(streamId, connection) streamWriteInt32(streamId, networkGetObjectId(self.vehicle)); streamWriteInt32(streamId, self.baleId); end;
function primaBaleEvent:run(connection) if self.vehicle ~= nil then self.vehicle:primaBale(self.baleId, true); end;
	if not connection:getIsServer() then g_server:broadcastEvent(primaBaleEvent:new(self.vehicle, self.baleId), nil, connection, self.object); end; end;
function primaBaleEvent.sendEvent(vehicle, baleId, noEventSend)	if noEventSend == nil or noEventSend == false then
	if g_server ~= nil then	g_server:broadcastEvent(primaBaleEvent:new(vehicle, baleId), nil, nil, vehicle);
	else g_client:getServerConnection():sendEvent(primaBaleEvent:new(vehicle, baleId));	end; end; end;
secondaBaleEvent = {}; secondaBaleEvent_mt = Class(secondaBaleEvent, Event); InitEventClass(secondaBaleEvent, "secondaBaleEvent");
function secondaBaleEvent:emptyNew() local self = Event:new(secondaBaleEvent_mt); self.className="secondaBaleEvent"; return self; end;
function secondaBaleEvent:new(vehicle, baleId) local self = secondaBaleEvent:emptyNew() self.vehicle = vehicle; self.baleId = baleId; return self; end;
function secondaBaleEvent:readStream(streamId, connection) local id = streamReadInt32(streamId); self.baleId = streamReadInt32(streamId); self.vehicle = networkGetObject(id); self:run(connection); end;
function secondaBaleEvent:writeStream(streamId, connection) streamWriteInt32(streamId, networkGetObjectId(self.vehicle)); streamWriteInt32(streamId, self.baleId); end;
function secondaBaleEvent:run(connection) if self.vehicle ~= nil then self.vehicle:secondaBale(self.baleId, true); end;
	if not connection:getIsServer() then g_server:broadcastEvent(secondaBaleEvent:new(self.vehicle, self.baleId), nil, connection, self.object);	end;end;
function secondaBaleEvent.sendEvent(vehicle, baleId, noEventSend) if noEventSend == nil or noEventSend == false then
	if g_server ~= nil then g_server:broadcastEvent(secondaBaleEvent:new(vehicle, baleId), nil, nil, vehicle);
	else g_client:getServerConnection():sendEvent(secondaBaleEvent:new(vehicle, baleId)); end; end; end;
terzaBaleEvent = {}; terzaBaleEvent_mt = Class(terzaBaleEvent, Event); InitEventClass(terzaBaleEvent, "terzaBaleEvent");
function terzaBaleEvent:emptyNew() local self = Event:new(terzaBaleEvent_mt); self.className="terzaBaleEvent"; return self; end;
function terzaBaleEvent:new(vehicle, baleId) local self = terzaBaleEvent:emptyNew() self.vehicle = vehicle; self.baleId = baleId; return self; end;
function terzaBaleEvent:readStream(streamId, connection) local id = streamReadInt32(streamId); self.baleId = streamReadInt32(streamId); self.vehicle = networkGetObject(id); self:run(connection); end;
function terzaBaleEvent:writeStream(streamId, connection) streamWriteInt32(streamId, networkGetObjectId(self.vehicle)); streamWriteInt32(streamId, self.baleId); end;
function terzaBaleEvent:run(connection) if self.vehicle ~= nil then self.vehicle:terzaBale(self.baleId, true); end;
	if not connection:getIsServer() then g_server:broadcastEvent(terzaBaleEvent:new(self.vehicle, self.baleId), nil, connection, self.object);	end;end;
function terzaBaleEvent.sendEvent(vehicle, baleId, noEventSend) if noEventSend == nil or noEventSend == false then
	if g_server ~= nil then g_server:broadcastEvent(terzaBaleEvent:new(vehicle, baleId), nil, nil, vehicle);
	else g_client:getServerConnection():sendEvent(terzaBaleEvent:new(vehicle, baleId)); end; end; end;
quartaBaleEvent = {}; quartaBaleEvent_mt = Class(quartaBaleEvent, Event); InitEventClass(quartaBaleEvent, "quartaBaleEvent");
function quartaBaleEvent:emptyNew() local self = Event:new(quartaBaleEvent_mt); self.className="quartaBaleEvent"; return self; end;
function quartaBaleEvent:new(vehicle, baleId) local self = quartaBaleEvent:emptyNew() self.vehicle = vehicle; self.baleId = baleId; return self; end;
function quartaBaleEvent:readStream(streamId, connection) local id = streamReadInt32(streamId); self.baleId = streamReadInt32(streamId); self.vehicle = networkGetObject(id); self:run(connection); end;
function quartaBaleEvent:writeStream(streamId, connection) streamWriteInt32(streamId, networkGetObjectId(self.vehicle)); streamWriteInt32(streamId, self.baleId); end;
function quartaBaleEvent:run(connection) if self.vehicle ~= nil then self.vehicle:quartaBale(self.baleId, true); end;
	if not connection:getIsServer() then g_server:broadcastEvent(quartaBaleEvent:new(self.vehicle, self.baleId), nil, connection, self.object);	end;end;
function quartaBaleEvent.sendEvent(vehicle, baleId, noEventSend) if noEventSend == nil or noEventSend == false then
	if g_server ~= nil then g_server:broadcastEvent(quartaBaleEvent:new(vehicle, baleId), nil, nil, vehicle);
	else g_client:getServerConnection():sendEvent(quartaBaleEvent:new(vehicle, baleId)); end; end; end;
setcurrentBalleEvent = {}; setcurrentBalleEvent_mt = Class(setcurrentBalleEvent, Event); InitEventClass(setcurrentBalleEvent, "setcurrentBalleEvent");
function setcurrentBalleEvent:emptyNew() local self = Event:new(setcurrentBalleEvent_mt); self.className="setcurrentBalleEvent"; return self; end;
function setcurrentBalleEvent:new(vehicle, currentBalle) local self = setcurrentBalleEvent:emptyNew() self.vehicle = vehicle; self.currentBalle = currentBalle; return self; end;
function setcurrentBalleEvent:readStream(streamId, connection) local id = streamReadInt32(streamId); self.vehicle = networkGetObject(id); self.currentBalle = streamReadInt8(streamId); if self.vehicle ~= nil then self.vehicle:setcurrentBalle(self.currentBalle, true); end;
	if not connection:getIsServer() then g_server:broadcastEvent(setcurrentBalleEvent:new(self.vehicle, self.currentBalle), nil, connection, self.vehicle); end; end;
function setcurrentBalleEvent:writeStream(streamId, connection) streamWriteInt32(streamId, networkGetObjectId(self.vehicle)); streamWriteInt8(streamId, self.currentBalle); end;
function setcurrentBalleEvent.sendEvent(vehicle, currentBalle, noEventSend)	if noEventSend == nil or noEventSend == false then
	if g_server ~= nil then	g_server:broadcastEvent(setcurrentBalleEvent:new(vehicle, currentBalle), nil, nil, vehicle);
	else g_client:getServerConnection():sendEvent(setcurrentBalleEvent:new(vehicle, currentBalle)); end; end; end;
setcurrentSupportEvent = {}; setcurrentSupportEvent_mt = Class(setcurrentSupportEvent, Event); InitEventClass(setcurrentSupportEvent, "setcurrentSupportEvent");
function setcurrentSupportEvent:emptyNew() local self = Event:new(setcurrentSupportEvent_mt); self.className="setcurrentSupportEvent"; return self; end;
function setcurrentSupportEvent:new(vehicle, currentSupport) local self = setcurrentSupportEvent:emptyNew() self.vehicle = vehicle; self.currentSupport = currentSupport; return self; end;
function setcurrentSupportEvent:readStream(streamId, connection) local id = streamReadInt32(streamId);
    self.vehicle = networkGetObject(id); self.currentSupport = streamReadBool(streamId); self.vehicle:setcurrentSupport(self.currentSupport, true);
	if not connection:getIsServer() then g_server:broadcastEvent(setcurrentSupportEvent:new(self.vehicle, self.currentSupport), nil, connection, self.vehicle); end; end;
function setcurrentSupportEvent:writeStream(streamId, connection) streamWriteInt32(streamId, networkGetObjectId(self.vehicle)); streamWriteBool(streamId, self.currentSupport); end;
function setcurrentSupportEvent.sendEvent(vehicle, currentSupport, noEventSend) if noEventSend == nil or noEventSend == false then
	if g_server ~= nil then	g_server:broadcastEvent(setcurrentSupportEvent:new(vehicle, currentSupport), nil, nil, vehicle);
	else g_client:getServerConnection():sendEvent(setcurrentSupportEvent:new(vehicle, currentSupport)); end; end; end;
setcurrentPiloniEvent = {}; setcurrentPiloniEvent_mt = Class(setcurrentPiloniEvent, Event); InitEventClass(setcurrentPiloniEvent, "setcurrentPiloniEvent");
function setcurrentPiloniEvent:emptyNew() local self = Event:new(setcurrentPiloniEvent_mt); self.className="setcurrentPiloniEvent"; return self; end;
function setcurrentPiloniEvent:new(vehicle, currentPiloni) local self = setcurrentPiloniEvent:emptyNew() self.vehicle = vehicle; self.currentPiloni = currentPiloni; return self; end;
function setcurrentPiloniEvent:readStream(streamId, connection) local id = streamReadInt32(streamId); self.vehicle = networkGetObject(id); self.currentPiloni = streamReadInt8(streamId); self.vehicle:setcurrentPiloni(self.currentPiloni, true);
	if not connection:getIsServer() then g_server:broadcastEvent(setcurrentPiloniEvent:new(self.vehicle, self.currentPiloni), nil, connection, self.vehicle); end; end;
function setcurrentPiloniEvent:writeStream(streamId, connection) streamWriteInt32(streamId, networkGetObjectId(self.vehicle)); streamWriteInt8(streamId, self.currentPiloni); end;
function setcurrentPiloniEvent.sendEvent(vehicle, currentPiloni, noEventSend) if noEventSend == nil or noEventSend == false then
	if g_server ~= nil then	g_server:broadcastEvent(setcurrentPiloniEvent:new(vehicle, currentPiloni), nil, nil, vehicle);
	else g_client:getServerConnection():sendEvent(setcurrentPiloniEvent:new(vehicle, currentPiloni)); end; end; end;
setcurrentBaleSizeEvent = {}; setcurrentBaleSizeEvent_mt = Class(setcurrentBaleSizeEvent, Event); InitEventClass(setcurrentBaleSizeEvent, "setcurrentBaleSizeEvent");
function setcurrentBaleSizeEvent:emptyNew() local self = Event:new(setcurrentBaleSizeEvent_mt); self.className="setcurrentBaleSizeEvent"; return self; end;
function setcurrentBaleSizeEvent:new(vehicle, currentBaleSize) local self = setcurrentBaleSizeEvent:emptyNew() self.vehicle = vehicle; self.currentBaleSize = currentBaleSize; return self; end;
function setcurrentBaleSizeEvent:readStream(streamId, connection) local id = streamReadInt32(streamId); self.vehicle = networkGetObject(id); self.currentBaleSize = streamReadInt8(streamId); self.vehicle:setcurrentBaleSize(self.currentBaleSize, true);
	if not connection:getIsServer() then g_server:broadcastEvent(setcurrentBaleSizeEvent:new(self.vehicle, self.currentBaleSize), nil, connection, self.vehicle); end; end;
function setcurrentBaleSizeEvent:writeStream(streamId, connection) streamWriteInt32(streamId, networkGetObjectId(self.vehicle)); streamWriteInt8(streamId, self.currentBaleSize); end;
function setcurrentBaleSizeEvent.sendEvent(vehicle, currentBaleSize, noEventSend) if noEventSend == nil or noEventSend == false then
	if g_server ~= nil then	g_server:broadcastEvent(setcurrentBaleSizeEvent:new(vehicle, currentBaleSize), nil, nil, vehicle);
	else g_client:getServerConnection():sendEvent(setcurrentBaleSizeEvent:new(vehicle, currentBaleSize)); end; end; end;