palletLoaderDDModPassion = {};
function palletLoaderDDModPassion.prerequisitesPresent(specializations) return SpecializationUtil.hasSpecialization(Attachable, specializations); end;
function palletLoaderDDModPassion:load(xmlFile) self.objectInRange = palletLoaderDDModPassion.objectInRange;
	self.primaPallet = SpecializationUtil.callSpecializationsFunction("primaPallet");
	self.secondaPallet = SpecializationUtil.callSpecializationsFunction("secondaPallet");
	self.setcurrentPallet = SpecializationUtil.callSpecializationsFunction("setcurrentPallet"); self.currentPallet = -1;
	self.setcurrentPalletType = SpecializationUtil.callSpecializationsFunction("setcurrentPalletType"); self.currentPalletType = 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.palletId = nil; self.lockPrima.productInRange = 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.palletId = nil; self.lockSeconda.productInRange = nil;
	self.fillLevel = 0; self.setPalletLoader = 0; self.setmovingDirection = 0; self.showInvalidBaleWarning = false; self.currentPiloni = 1;
	self.capacity = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.capacity"), 0.0); self.currentSupport = true; self.currentPunte = 1; self.currentPosizione = 2; self.currentBalle = 1; end;
function palletLoaderDDModPassion:delete() self:primaPallet(nil, false); self:secondaPallet(nil, false); end;
function palletLoaderDDModPassion:readStream(streamId, connection) self:setcurrentPallet(streamReadInt8(streamId), true); self:setcurrentPalletType(streamReadInt8(streamId), true);
	self:primaPallet(streamReadInt32(streamId), true); self:secondaPallet(streamReadInt32(streamId), true); end;
function palletLoaderDDModPassion:writeStream(streamId, connection) streamWriteInt8(streamId, self.currentPallet); streamWriteInt8(streamId, self.currentPalletType);
	streamWriteInt32(streamId, self.lockPrima.palletId); streamWriteInt32(streamId, self.lockSeconda.palletId); end;
function palletLoaderDDModPassion:setcurrentPallet(int, noEventSend) self.currentPallet = int; setcurrentPalletEvent.sendEvent(self, int, noEventSend); end;
function palletLoaderDDModPassion:setcurrentPalletType(int, noEventSend) self.currentPalletType = int; setcurrentPalletTypeEvent.sendEvent(self, int, noEventSend); end;
function palletLoaderDDModPassion:loadFromAttributesAndNodes(xmlFile, key, resetVehicles) local currentPallet = getXMLInt(xmlFile, key .. "#currentPallet"); if currentPallet ~= nil then self:setcurrentPallet(currentPallet); end; self.fillLevel = 0; return BaseMission.VEHICLE_LOAD_OK; end;
function palletLoaderDDModPassion:getSaveAttributesAndNodes(nodeIdent) local attributes = 'currentPallet="' .. self.currentPallet .. '"'; self:primaPallet(nil, false); self:secondaPallet(nil, false); self.setPalletLoader = 0; return attributes; end;
function palletLoaderDDModPassion:mouseEvent(posX, posY, isDown, isUp, button) end;
function palletLoaderDDModPassion:keyEvent(unicode, sym, modifier, isDown) end;
function palletLoaderDDModPassion:update(dt)
	if self:getIsActive() and self:getIsActiveForInput() and self.currentAttacco > 0 then
		if self.currentPallet > -1 then self.showInvalidBaleWarning = false;
			if self.lockPrima.palletId == nil then self.fillLevel = 0; local palletPrima = self:objectInRange(self.lockPrima);
				if palletPrima == nil and self.setPalletLoader > 0 then self.setPalletLoader = self.setPalletLoader - 1; end;
				if palletPrima ~= nil and self.setPalletLoader == 0 and self.movingDirection == 1 then self.setPalletLoader = 1; 
				if self.currentPallet == 0 then self:primaPallet(networkGetObjectId(palletPrima), false); 
				elseif self.currentPallet == 1 then self:primaPallet(palletPrima, false); self:setWorkState(not self.attachBales); end; end; end;
			if InputBinding.hasEvent(InputBinding.baleLoaderUnlock) and self.setmovingDirection < 100 then 
				if self.currentPallet == 0 then self:primaPallet(nil, false); 
				elseif self.currentPallet == 1 then self:primaPallet(nil, false); self:setWorkState(not self.attachBales); end; end;
			if self.lockSeconda.palletId == nil and self.capacity == 2 then local palletSeconda = self:objectInRange(self.lockSeconda);
				if palletSeconda == nil and self.setPalletLoader > 1 then self.setPalletLoader = self.setPalletLoader - 1; end;
				if palletSeconda ~= nil and self.lockPrima.palletId ~= nil and self.movingDirection == 1 then self.setPalletLoader = self.fillLevel;
				if self.currentPallet == 0 then self:secondaPallet(networkGetObjectId(palletSeconda), false); 
				elseif self.currentPallet == 1 then self:secondaPallet(palletSeconda, false); end; end; end;
			if InputBinding.hasEvent(InputBinding.baleLoaderUnlock) and self.setmovingDirection < 100 then self:secondaPallet(nil, false); end; end;
		if InputBinding.hasEvent(InputBinding.change) and self.setPalletLoader == 0 and not self.balesAttached then if self.currentPallet > -1 then self:setcurrentPallet(-1); else self:setcurrentPallet(0); end; end; end;
	if self.capacity == 1 and self.fillLevel == 1 then
		self:setcurrentPuntaDx(5); setTranslation(self.fermiDx, (self.currentPuntaDx+1)/2*self.dist-self.distC, 0.756, 0.084);
		self:setcurrentPuntaSx(19); setTranslation(self.fermiSx, (self.currentPuntaSx+1)/2*self.dist-self.distC, 0.756, 0.084); end;
	if self.capacity == 2 then 
		if (self.fillLevel == 1 and self.lockPrima.palletId ~= nil) or (self.fillLevel == 2 and self.lockSeconda.palletId ~= nil and self.lockPrima.palletId ~= nil) then
			self:setcurrentPuntaDx(1); setTranslation(self.fermiDx, (self.currentPuntaDx+1)/2*self.dist-self.distC, 0.756, 0.084);
			self:setcurrentPuntaSx(13); setTranslation(self.fermiSx, (self.currentPuntaSx+1)/2*self.dist-self.distC, 0.756, 0.084);
			self:setcurrentPuntaDxB(35); setTranslation(self.fermiDxB, (self.currentPuntaDxB+1)/2*self.dist-self.distCB, 0.756, 0.084);
			self:setcurrentPuntaSxB(47); setTranslation(self.fermiSxB, (self.currentPuntaSxB+1)/2*self.dist-self.distCB, 0.756, 0.084); end;
		if self.fillLevel == 2 and self.lockSeconda.palletId == nil and self.lockPrima.palletId ~= nil then 
			self:setcurrentPuntaDx(1); setTranslation(self.fermiDx, (self.currentPuntaDx+1)/2*self.dist-self.distC, 0.756, 0.084); 
			self:setcurrentPuntaSx(17); setTranslation(self.fermiSx, (self.currentPuntaSx+1)/2*self.dist-self.distC, 0.756, 0.084);
			self:setcurrentPuntaDxB(31); setTranslation(self.fermiDxB, (self.currentPuntaDxB+1)/2*self.dist-self.distCB, 0.756, 0.084); 
			self:setcurrentPuntaSxB(47); setTranslation(self.fermiSxB, (self.currentPuntaSxB+1)/2*self.dist-self.distCB, 0.756, 0.084);
 end; end; end;
function palletLoaderDDModPassion:updateTick(dt) if self.movingDirection == 0 then self.setmovingDirection = 0; end; if self.movingDirection > 0 then self.setmovingDirection = self.setmovingDirection + dt; end; end;
function palletLoaderDDModPassion:draw()
	if self.fillLevel > 0 and self.currentPallet > -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.setPalletLoader == 0 and self.currentAttacco > 0 and self.currentPallet ~= nil and not self.balesAttached then
		if self.currentPallet > -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.currentPallet > -1 then g_currentMission:showBlinkingWarning(g_i18n:getText("InvalidBaleWarning")); end; end;
function palletLoaderDDModPassion:onAttach(attacherVehicle) end;
function palletLoaderDDModPassion:onDetach() self:setcurrentPallet(0); self:primaPallet(nil, false); self:secondaPallet(nil, false); self.setPalletLoader = 0; self.fillLevel = 0; self.currentPallet = 0; end;
function palletLoaderDDModPassion:primaPallet(palletId, noEventSend)
if self.currentPallet == 0 then
	if palletId ~= nil then if self.lockPrima.child ~= nil then local pallet = nil;
		if self.lockPrima.palletId ~= nil then pallet = networkGetObject(self.lockPrima.palletId); 
		if pallet ~= nil then pallet:unmount(); self.lockPrima.palletId = nil; end; end;
		primaPalletEvent.sendEvent(self, palletId, noEventSend); pallet = networkGetObject(palletId);
		if pallet ~= nil then print(tostring(getName(pallet.nodeId))) self.fillLevel = self.fillLevel + 1;
			if     tostring(getName(pallet.nodeId)) == "eggPallet"          then pallet:mount(self, self.lockPrima.child, 0,0,0.2, 0,0,0);
			elseif tostring(getName(pallet.nodeId)) == "lettucePallet"      then pallet:mount(self, self.lockPrima.child, 0,0,0.2, 0,0,0);
			elseif tostring(getName(pallet.nodeId)) == "woolPallet"  		then pallet:mount(self, self.lockPrima.child, 0,0,0.2, 0,0,0);
			elseif tostring(getName(pallet.nodeId)) == "saplingsPallet"     then pallet:mount(self, self.lockPrima.child, 0,0,0, 0,0,0); end;
			if self.capacity == 1 then 
				if     tostring(getName(pallet.nodeId)) == "washedPotatoPallet" then pallet:mount(self, self.lockPrima.child, 0,0.05,0.25, 0,0,0); 
				elseif tostring(getName(pallet.nodeId)) == "boardPallet"        then pallet:mount(self, self.lockPrima.child, 0,0,0.2, 0,0,0); end; end;
			if self.capacity == 2 then
				if     tostring(getName(pallet.nodeId)) == "washedPotatoPallet" then pallet:mount(self, self.lockPrima.child, 0.85,0.05,0.25, 0,0,0);
				elseif tostring(getName(pallet.nodeId)) == "boardPallet"        then pallet:mount(self, self.lockPrima.child, 0.85,0,0.2, 0,0,0); end; end;
			if (tostring(getName(pallet.nodeId)) == "washedPotatoPallet" or tostring(getName(pallet.nodeId)) == "boardPallet") and self.capacity == 2 then self.fillLevel = self.fillLevel + 1; end;
			self.lockPrima.palletId = palletId; end; end;
	else primaPalletEvent.sendEvent(self, palletId, noEventSend); if self.lockPrima.palletId ~= nil then local pallet = networkGetObject(self.lockPrima.palletId); if pallet ~= nil then pallet:unmount(); self.lockPrima.palletId = nil; end; end; end;
elseif self.currentPallet == 1 then 
		if palletId ~= nil then primaPalletEvent.sendEvent(self, palletId, noEventSend); 
			local pallet = nil; pallet = networkGetObject(palletId);
			if pallet == nil then self.fillLevel = self.fillLevel + 1; self.lockPrima.palletId = palletId;	end;
		else primaPalletEvent.sendEvent(self, palletId, noEventSend); 
			if self.lockPrima.palletId ~= nil then self.lockPrima.palletId = nil; end;
		end;
end; end;
function palletLoaderDDModPassion:secondaPallet(palletId, noEventSend)
if self.currentPallet == 0 then
	if palletId ~= nil then if self.lockSeconda.child ~= nil then local pallet = nil;
		if self.lockSeconda.palletId ~= nil then pallet = networkGetObject(self.lockSeconda.palletId); if pallet ~= nil then pallet:unmount(); self.lockSeconda.palletId = nil; end; end; secondaPalletEvent.sendEvent(self, palletId, noEventSend); pallet = networkGetObject(palletId);
		if pallet ~= nil then print(tostring(getName(pallet.nodeId))) self.fillLevel = self.fillLevel + 1;
			if     tostring(getName(pallet.nodeId)) == "eggPallet"          then pallet:mount(self, self.lockSeconda.child, 0,0,0.2, 0,0,0);
			elseif tostring(getName(pallet.nodeId)) == "lettucePallet"      then pallet:mount(self, self.lockSeconda.child, 0,0,0.2, 0,0,0);
			elseif tostring(getName(pallet.nodeId)) == "woolPallet"  		then pallet:mount(self, self.lockSeconda.child, 0,0,0.2, 0,0,0);
			elseif tostring(getName(pallet.nodeId)) == "saplingsPallet"     then pallet:mount(self, self.lockSeconda.child, 0,0,0, 0,0,0); end;
			self.lockSeconda.palletId = palletId; end; end;
	else secondaPalletEvent.sendEvent(self, palletId, noEventSend);	if self.lockSeconda.palletId ~= nil then local pallet = networkGetObject(self.lockSeconda.palletId); if pallet ~= nil then pallet:unmount(); self.lockSeconda.palletId = nil; end; end; end; 
elseif self.currentPallet == 1 then 
		if palletId ~= nil then secondaPalletEvent.sendEvent(self, palletId, noEventSend); 
			local pallet = nil; pallet = networkGetObject(palletId);
			if pallet == nil then self.fillLevel = self.fillLevel + 1; self.lockSeconda.palletId = palletId; end;
		else secondaPalletEvent.sendEvent(self, palletId, noEventSend); 
			if self.lockSeconda.palletId ~= nil then self.lockSeconda.palletId = nil; end;
		end;
end; end;
function palletLoaderDDModPassion:objectInRange(refNode) local nearestDistance = 2; local nearestPallet = 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 self.showInvalidBaleWarning = true; end; end; end; 
    for index, item in pairs(g_currentMission.itemsToSave) do if item.item:isa(FillablePallet) then local vx, vy, vz = getWorldTranslation(item.item.nodeId); local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
		if distance < nearestDistance then nearestDistance = distance; nearestPallet = item.item; self:setcurrentPallet(0); end; end; end;
	if nearestPallet == nil then 
	for index, vehicle in pairs(g_currentMission.vehicles) do local vehicleNode = vehicle.rootNode;
		if getParent(vehicleNode) == getRootNode() then	local vx, vy, vz = getWorldTranslation(vehicleNode); local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
		if distance < (nearestDistance - 1.8) then local vehicleTypeName = vehicle.typeName;
			if string.find(vehicleTypeName, "SilageAdditivesTank") or string.find(vehicleTypeName, "BuyableBlades") or string.find(vehicleTypeName, "PalletFoilCartons") or	string.find(vehicleTypeName, "netRolePallet") then nearestDistance = distance; nearestPallet = vehicleNode; self:setcurrentPallet(1);end; end; end; end; end; 
	return nearestPallet; end;
primaPalletEvent = {}; primaPalletEvent_mt = Class(primaPalletEvent, Event); InitEventClass(primaPalletEvent, "primaPalletEvent");
function primaPalletEvent:emptyNew() local self = Event:new(primaPalletEvent_mt); self.className="primaPalletEvent"; return self; end;
function primaPalletEvent:new(vehicle, palletId) local self = primaPalletEvent:emptyNew() self.vehicle = vehicle; self.palletId = palletId; return self; end;
function primaPalletEvent:readStream(streamId, connection) local id = streamReadInt32(streamId); self.palletId = streamReadInt32(streamId); self.vehicle = networkGetObject(id); self:run(connection); end;
function primaPalletEvent:writeStream(streamId, connection) streamWriteInt32(streamId, networkGetObjectId(self.vehicle)); streamWriteInt32(streamId, self.palletId); end;
function primaPalletEvent:run(connection) if self.vehicle ~= nil then self.vehicle:primaPallet(self.palletId, true); end;
	if not connection:getIsServer() then g_server:broadcastEvent(primaPalletEvent:new(self.vehicle, self.palletId), nil, connection, self.object); end; end;
function primaPalletEvent.sendEvent(vehicle, palletId, noEventSend)	if noEventSend == nil or noEventSend == false then
	if g_server ~= nil then	g_server:broadcastEvent(primaPalletEvent:new(vehicle, palletId), nil, nil, vehicle);
	else g_client:getServerConnection():sendEvent(primaPalletEvent:new(vehicle, palletId));	end; end; end;
secondaPalletEvent = {}; secondaPalletEvent_mt = Class(secondaPalletEvent, Event); InitEventClass(secondaPalletEvent, "secondaPalletEvent");
function secondaPalletEvent:emptyNew() local self = Event:new(secondaPalletEvent_mt); self.className="secondaPalletEvent"; return self; end;
function secondaPalletEvent:new(vehicle, palletId) local self = secondaPalletEvent:emptyNew() self.vehicle = vehicle; self.palletId = palletId; return self; end;
function secondaPalletEvent:readStream(streamId, connection) local id = streamReadInt32(streamId); self.palletId = streamReadInt32(streamId); self.vehicle = networkGetObject(id); self:run(connection); end;
function secondaPalletEvent:writeStream(streamId, connection) streamWriteInt32(streamId, networkGetObjectId(self.vehicle)); streamWriteInt32(streamId, self.palletId); end;
function secondaPalletEvent:run(connection) if self.vehicle ~= nil then self.vehicle:secondaPallet(self.palletId, true); end;
	if not connection:getIsServer() then g_server:broadcastEvent(secondaPalletEvent:new(self.vehicle, self.palletId), nil, connection, self.object);	end;end;
function secondaPalletEvent.sendEvent(vehicle, palletId, noEventSend) if noEventSend == nil or noEventSend == false then
	if g_server ~= nil then g_server:broadcastEvent(secondaPalletEvent:new(vehicle, palletId), nil, nil, vehicle);
	else g_client:getServerConnection():sendEvent(secondaPalletEvent:new(vehicle, palletId)); end; end; end;
setcurrentPalletEvent = {}; setcurrentPalletEvent_mt = Class(setcurrentPalletEvent, Event); InitEventClass(setcurrentPalletEvent, "setcurrentPalletEvent");
function setcurrentPalletEvent:emptyNew() local self = Event:new(setcurrentPalletEvent_mt); self.className="setcurrentPalletEvent"; return self; end;
function setcurrentPalletEvent:new(vehicle, currentPallet) local self = setcurrentPalletEvent:emptyNew() self.vehicle = vehicle; self.currentPallet = currentPallet; return self; end;
function setcurrentPalletEvent:readStream(streamId, connection) local id = streamReadInt32(streamId); self.vehicle = networkGetObject(id); self.currentPallet = streamReadInt8(streamId); if self.vehicle ~= nil then self.vehicle:setcurrentPallet(self.currentPallet, true); end;
	if not connection:getIsServer() then g_server:broadcastEvent(setcurrentPalletEvent:new(self.vehicle, self.currentPallet), nil, connection, self.vehicle); end; end;
function setcurrentPalletEvent:writeStream(streamId, connection) streamWriteInt32(streamId, networkGetObjectId(self.vehicle)); streamWriteInt8(streamId, self.currentPallet); end;
function setcurrentPalletEvent.sendEvent(vehicle, currentPallet, noEventSend)	if noEventSend == nil or noEventSend == false then
	if g_server ~= nil then	g_server:broadcastEvent(setcurrentPalletEvent:new(vehicle, currentPallet), nil, nil, vehicle);
	else g_client:getServerConnection():sendEvent(setcurrentPalletEvent:new(vehicle, currentPallet)); end; end; end;
setcurrentPalletTypeEvent = {}; setcurrentPalletTypeEvent_mt = Class(setcurrentPalletTypeEvent, Event); InitEventClass(setcurrentPalletTypeEvent, "setcurrentPalletTypeEvent");
function setcurrentPalletTypeEvent:emptyNew() local self = Event:new(setcurrentPalletTypeEvent_mt); self.className="setcurrentPalletTypeEvent"; return self; end;
function setcurrentPalletTypeEvent:new(vehicle, currentPalletType) local self = setcurrentPalletTypeEvent:emptyNew() self.vehicle = vehicle; self.currentPalletType = currentPalletType; return self; end;
function setcurrentPalletTypeEvent:readStream(streamId, connection) local id = streamReadInt32(streamId); self.vehicle = networkGetObject(id); self.currentPalletType = streamReadInt8(streamId); self.vehicle:setcurrentPalletType(self.currentPalletType, true);
	if not connection:getIsServer() then g_server:broadcastEvent(setcurrentPalletTypeEvent:new(self.vehicle, self.currentPalletType), nil, connection, self.vehicle); end; end;
function setcurrentPalletTypeEvent:writeStream(streamId, connection) streamWriteInt32(streamId, networkGetObjectId(self.vehicle)); streamWriteInt8(streamId, self.currentPalletType); end;
function setcurrentPalletTypeEvent.sendEvent(vehicle, currentPalletType, noEventSend) if noEventSend == nil or noEventSend == false then
	if g_server ~= nil then	g_server:broadcastEvent(setcurrentPalletTypeEvent:new(vehicle, currentPalletType), nil, nil, vehicle);
	else g_client:getServerConnection():sendEvent(setcurrentPalletTypeEvent:new(vehicle, currentPalletType)); end; end; end;