jointsDDModPassion = {};
Vehicle.registerJointType("liftingHook");
function jointsDDModPassion.prerequisitesPresent(specializations) return true; end;
function jointsDDModPassion:load(xmlFile) self.cameraState = true;
	self.setcurrentAttacco = SpecializationUtil.callSpecializationsFunction("setcurrentAttacco"); self.currentAttacco = 0; 
	self.setsceltaAttacco = SpecializationUtil.callSpecializationsFunction("setsceltaAttacco"); self.sceltaAttacco = -7;
	self.setAttaccoFFrame = Utils.getFilename("image/setAttaccoF.dds", self.baseDirectory);
	self.setAttaccoF = Overlay:new("setAttaccoF", self.setAttaccoFFrame, 0.35, 0.16, 0.3, 0.24);
	self.setAttaccoTFrame = Utils.getFilename("image/setAttaccoT.dds", self.baseDirectory);
	self.setAttaccoT = Overlay:new("setAttaccoT", self.setAttaccoTFrame, 0.35, 0.16, 0.3, 0.24);
	self.setAttaccoWFrame = Utils.getFilename("image/setAttaccoW.dds", self.baseDirectory);
	self.setAttaccoW = Overlay:new("setAttaccoW", self.setAttaccoWFrame, 0.35, 0.16, 0.3, 0.24);
	self.speedViolationMaxTimeJoints = 1500; self.speedViolationTimerJoints = self.speedViolationMaxTimeJoints;
	self.isDetachAllowed = Utils.overwrittenFunction(self.isDetachAllowed, jointsDDModPassion.isDetachAllowed); self.jointState = true;
	self.speedWaitingMaxTimeJoints = 5000; self.speedWaitingTimerJoints = self.speedWaitingMaxTimeJoints; end;
function jointsDDModPassion:readStream(streamId, connection) self:setcurrentAttacco(streamReadInt8(streamId), true); self:setsceltaAttacco(streamReadInt8(streamId), true); end;
function jointsDDModPassion:writeStream(streamId, connection) streamWriteInt8(streamId, self.currentAttacco); streamWriteInt8(streamId, self.sceltaAttacco); end;
function jointsDDModPassion:setcurrentAttacco(int, noEventSend) self.currentAttacco = int; setcurrentAttaccoEvent.sendEvent(self, int, noEventSend); end;
function jointsDDModPassion:setsceltaAttacco(int, noEventSend) self.sceltaAttacco = int; setsceltaAttaccoEvent.sendEvent(self, int, noEventSend); end;
function jointsDDModPassion:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)	local currentAttacco = getXMLInt(xmlFile, key .. "#currentAttacco"); self:setcurrentAttacco(currentAttacco); if currentAttacco > 0 then self.speedWaitingTimerJoints = 0; end; return BaseMission.VEHICLE_LOAD_OK; end;
function jointsDDModPassion:getSaveAttributesAndNodes(nodeIdent) local attributes = 'currentAttacco="' .. self.currentAttacco .. '"'; return attributes; end;
function jointsDDModPassion:delete() end;
function jointsDDModPassion:mouseEvent(posX, posY, isDown, isUp, button) end;
function jointsDDModPassion:keyEvent(unicode, sym, modifier, isDown) end;
function jointsDDModPassion:update(dt)
	if self.currentAttacco == 0 then self:playAnimation("nessunAttacco", 1, nil, true); self:setsceltaAttacco(-7); self.speedViolationTimerJoints = self.speedViolationMaxTimeJoints; end;
	if InputBinding.hasEvent(InputBinding.changeJoint) and self.speedWaitingTimerJoints > 0 and self.currentAttacco > 0 and self:getIsActiveForInput() and self:getIsActive() then self:setcurrentAttacco(-1); self.speedWaitingTimerJoints = 0; end;
	if self.currentAttacco < 0 and self:getIsActiveForInput() and self:getIsActive() then
		if InputBinding.hasEvent(InputBinding.selectRight) then self.sceltaAttacco = self.sceltaAttacco + 1; if self.sceltaAttacco == -4 then self.sceltaAttacco = -7; end; self:setsceltaAttacco(self.sceltaAttacco); end;
		if InputBinding.hasEvent(InputBinding.selectLeft) then self.sceltaAttacco = self.sceltaAttacco - 1; if self.sceltaAttacco == -8 then self.sceltaAttacco = -5; end; self:setsceltaAttacco(self.sceltaAttacco); end;
		if InputBinding.hasEvent(InputBinding.MENU_ACCEPT) then self.sceltaAttacco = self.sceltaAttacco + 14; self:setsceltaAttacco(self.sceltaAttacco); self:setcurrentAttacco(self.sceltaAttacco); end; end; end;
function jointsDDModPassion:updateTick(dt)
	if     self.currentAttacco ==  1 then self:playAnimation("adattatoreCSZ", 1, nil, true);
	elseif self.currentAttacco ==  7 then self:playAnimation("frontloader", 1, nil, true);
	elseif self.currentAttacco ==  8 then self:playAnimation("telehandler", 1, nil, true);
	elseif self.currentAttacco ==  9 then self:playAnimation("wheelLoader", 1, nil, true);
	elseif self.currentAttacco == 10 then self:playAnimation("frontloader", 1, nil, true);
	elseif self.currentAttacco ==  0 then self:playAnimation("nessunAttacco", 1, nil, true); end;
	if self.speedWaitingTimerJoints > 0 and self:getIsActive() then self.speedWaitingTimerJoints = self.speedWaitingTimerJoints - dt; end;
	if self.movingDirection > 0 and self.currentAttacco < 0 then self.speedViolationTimerJoints = self.speedViolationTimerJoints - dt;
	if self.isServer then if self.speedViolationTimerJoints < 0 then if self.attacherVehicle ~= nil then self.attacherVehicle:detachImplementByObject(self); end; end; end; end; end;
function jointsDDModPassion:draw()
	if self.currentAttacco < 0 then g_currentMission:addWarning(string.format(g_i18n:getText("attaccoWarning")), 0, 0);
		if     self.sceltaAttacco == -7 then self.setAttaccoF:render();
		elseif self.sceltaAttacco == -6 then self.setAttaccoT:render();
		elseif self.sceltaAttacco == -5 then self.setAttaccoW:render(); end; end; 
	if self.speedWaitingTimerJoints > 0 and self.currentAttacco > 0 and self:getIsActiveForInput() and self:getIsActive() then g_currentMission:addHelpButtonText(g_i18n:getText("jointsChange"), InputBinding.changeJoint); end; end;
function jointsDDModPassion:onAttach(attacherVehicle, jointDesc) local attacherJoint = attacherVehicle.attacherJoint;
	if attacherJoint ~= nil then if attacherJoint.jointType ~= nil then self.currentAttacco = attacherJoint.jointType; end; end;
	if self.currentAttacco == 0 then local correctLocation = false;
		for i = 1, table.maxn(attacherVehicle.specializations) do local vs = attacherVehicle.specializations[i];
			if vs ~= nil and vs == SpecializationUtil.getSpecialization("frontloader") then correctLocation = true; break; end; end;
			if correctLocation then self:setcurrentAttacco(7); end; end;
	if self.currentAttacco == 0 then local correctLocation = false;
		for i = 1, table.maxn(attacherVehicle.specializations) do local vs = attacherVehicle.specializations[i];
			if vs ~= nil and vs == SpecializationUtil.getSpecialization("aiTractor") then correctLocation = true; break; end;
			if vs ~= nil and vs == SpecializationUtil.getSpecialization("telehandler") then correctLocation = true; break; end; end;
			if correctLocation then self:setcurrentAttacco(8); end; end;
	if self.currentAttacco == 0 then local correctLocation = false;
		for i = 1, table.maxn(attacherVehicle.specializations) do local vs = attacherVehicle.specializations[i];
			if vs ~= nil and vs == SpecializationUtil.getSpecialization("articulatedAxis") then correctLocation = true; break; end;
			if vs ~= nil and vs == SpecializationUtil.getSpecialization("wheelLoader") then correctLocation = true; break; end; end;
			if correctLocation then self:setcurrentAttacco(9); end; end;
	if self.currentAttacco == 0 then self:setcurrentAttacco(-7); end;
	self.attacherVehicle = attacherVehicle;	if attacherVehicle.attacherVehicle == nil then self.attacherVehicleCopy = attacherVehicle; else self.attacherVehicleCopy = attacherVehicle.attacherVehicle; end; 
	if self.cameraState then local correctLocation = false;	
		for i = 1, table.maxn(self.attacherVehicleCopy.specializations) do local vs = self.attacherVehicleCopy.specializations[i];
		if vs ~= nil and vs == SpecializationUtil.getSpecialization("keyboardSteerMogli") then correctLocation = true; break; end; end;
		if correctLocation then self.cameraState = false; end; end; 
	if self.currentAttacco == 4 then self.jointState = false; self.cameraState = false; end;
	if not self.cameraState then for _,implement in pairs(self.attachedImplements) do if implement.object ~= nil then implement.object:attachableRemoveToolCameras(); end; end; 
	local rootAttacherVehicle = self:getRootAttacherVehicle(); if self.numToolCameras > 0 and rootAttacherVehicle.removeToolCameras ~= nil then rootAttacherVehicle:removeToolCameras(self.toolCameras); end; end; end;
function jointsDDModPassion:onDetach() self:setcurrentAttacco(0); self:setsceltaAttacco(0); self.speedWaitingTimerJoints = self.speedWaitingMaxTimeJoints; end;
function jointsDDModPassion:onDeactivate() end;
function jointsDDModPassion:isDetachAllowed(superFunc) local px, py, pz = getWorldTranslation(self.components[1].node);	local terrainHeight = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, px, py, pz);	if py-terrainHeight > 1 and self.jointState then return false; end; if superFunc ~= nil then return superFunc(self); end; return true; end; 
setcurrentAttaccoEvent = {}; setcurrentAttaccoEvent_mt = Class(setcurrentAttaccoEvent, Event); InitEventClass(setcurrentAttaccoEvent, "setcurrentAttaccoEvent");
function setcurrentAttaccoEvent:emptyNew() local self = Event:new(setcurrentAttaccoEvent_mt); self.className="setcurrentAttaccoEvent"; return self; end;
function setcurrentAttaccoEvent:new(vehicle, currentAttacco) local self = setcurrentAttaccoEvent:emptyNew() self.vehicle = vehicle; self.currentAttacco = currentAttacco; return self; end;
function setcurrentAttaccoEvent:readStream(streamId, connection) local id = streamReadInt32(streamId); self.vehicle = networkGetObject(id); 
	self.currentAttacco = streamReadInt8(streamId); if self.vehicle ~= nil then self.vehicle:setcurrentAttacco(self.currentAttacco, true); end;
	if not connection:getIsServer() then g_server:broadcastEvent(setcurrentAttaccoEvent:new(self.vehicle, self.currentAttacco), nil, connection, self.vehicle); end; end;
function setcurrentAttaccoEvent:writeStream(streamId, connection) streamWriteInt32(streamId, networkGetObjectId(self.vehicle)); streamWriteInt8(streamId, self.currentAttacco); end;
function setcurrentAttaccoEvent.sendEvent(vehicle, currentAttacco, noEventSend) if noEventSend == nil or noEventSend == false then
	if g_server ~= nil then	g_server:broadcastEvent(setcurrentAttaccoEvent:new(vehicle, currentAttacco), nil, nil, vehicle);
	else g_client:getServerConnection():sendEvent(setcurrentAttaccoEvent:new(vehicle, currentAttacco)); end; end; end;
setsceltaAttaccoEvent = {}; setsceltaAttaccoEvent_mt = Class(setsceltaAttaccoEvent, Event); InitEventClass(setsceltaAttaccoEvent, "setsceltaAttaccoEvent");
function setsceltaAttaccoEvent:emptyNew() local self = Event:new(setsceltaAttaccoEvent_mt); self.className="setsceltaAttaccoEvent"; return self; end;
function setsceltaAttaccoEvent:new(vehicle, sceltaAttacco) local self = setsceltaAttaccoEvent:emptyNew() self.vehicle = vehicle; self.sceltaAttacco = sceltaAttacco; return self; end;
function setsceltaAttaccoEvent:readStream(streamId, connection) local id = streamReadInt32(streamId); self.vehicle = networkGetObject(id); self.sceltaAttacco = streamReadInt8(streamId); if self.vehicle ~= nil then self.vehicle:setsceltaAttacco(self.sceltaAttacco, true); end;
	if not connection:getIsServer() then g_server:broadcastEvent(setsceltaAttaccoEvent:new(self.vehicle, self.sceltaAttacco), nil, connection, self.vehicle); end; end;
function setsceltaAttaccoEvent:writeStream(streamId, connection) streamWriteInt32(streamId, networkGetObjectId(self.vehicle)); streamWriteInt8(streamId, self.sceltaAttacco); end;
function setsceltaAttaccoEvent.sendEvent(vehicle, sceltaAttacco, noEventSend) if noEventSend == nil or noEventSend == false then
	if g_server ~= nil then	g_server:broadcastEvent(setsceltaAttaccoEvent:new(vehicle, sceltaAttacco), nil, nil, vehicle);
	else g_client:getServerConnection():sendEvent(setsceltaAttaccoEvent:new(vehicle, sceltaAttacco)); end; end; end;
VehicleCamera.getCollisionDistance = function() return false, -1; end;