--
-- Iveco Crane Working Script
--
-- by Zippo
-- 


ICrane = {}

function ICrane.prerequisitesPresent(specializations)
	Vehicle.registerJointType("frontloader");
    return SpecializationUtil.hasSpecialization(Steerable, specializations);
end;

function ICrane:load(xmlFile)
	
	local translationPartNode = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.translationPart#index"));
    if translationPartNode ~= nil then
        self.translationPart = {};
        self.translationPart.node = translationPartNode;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.translationPart#minTrans"));
        self.translationPart.minTrans = {};
        self.translationPart.minTrans[1] = Utils.getNoNil(x, 0);
        self.translationPart.minTrans[2] = Utils.getNoNil(y, 0);
        self.translationPart.minTrans[3] = Utils.getNoNil(z, 0);

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.translationPart#maxTrans"));
        self.translationPart.maxTrans = {};
        self.translationPart.maxTrans[1] = Utils.getNoNil(x, 0);
        self.translationPart.maxTrans[2] = Utils.getNoNil(y, 0);
        self.translationPart.maxTrans[3] = Utils.getNoNil(z, 0);

        self.translationPart.transTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.translationPart#transTime"), 2)*1000;
        self.translationPart.touchTransLimit = Utils.getNoNil(getXMLString(xmlFile, "vehicle.translationPart#touchTransLimit"), 10);
    end;

        local translationHookNode = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.translationHook#index"));
    if translationHookNode ~= nil then
        self.translationHook = {};
        self.translationHook.node = translationHookNode;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.translationHook#minTrans"));
        self.translationHook.minTrans = {};
        self.translationHook.minTrans[1] = Utils.getNoNil(x, 0);
        self.translationHook.minTrans[2] = Utils.getNoNil(y, 0);
        self.translationHook.minTrans[3] = Utils.getNoNil(z, 0);

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.translationHook#maxTrans"));
        self.translationHook.maxTrans = {};
        self.translationHook.maxTrans[1] = Utils.getNoNil(x, 0);
        self.translationHook.maxTrans[2] = Utils.getNoNil(y, 0);
        self.translationHook.maxTrans[3] = Utils.getNoNil(z, 0);

        self.translationHook.transTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.translationHook#transTime"), 2)*1000;
        self.translationHook.touchTransLimit = Utils.getNoNil(getXMLString(xmlFile, "vehicle.translationHook#touchTransLimit"), 10);
    end;        

        local translationHydNode = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.translationHyd#index"));
    if translationHydNode ~= nil then
        self.translationHyd = {};
        self.translationHyd.node = translationHydNode;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.translationHyd#minTrans"));
        self.translationHyd.minTrans = {};
        self.translationHyd.minTrans[1] = Utils.getNoNil(x, 0);
        self.translationHyd.minTrans[2] = Utils.getNoNil(y, 0);
        self.translationHyd.minTrans[3] = Utils.getNoNil(z, 0);

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.translationHyd#maxTrans"));
        self.translationHyd.maxTrans = {};
        self.translationHyd.maxTrans[1] = Utils.getNoNil(x, 0);
        self.translationHyd.maxTrans[2] = Utils.getNoNil(y, 0);
        self.translationHyd.maxTrans[3] = Utils.getNoNil(z, 0);

        self.translationHyd.transTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.translationHyd#transTime"), 2)*1000;
        self.translationHyd.touchTransLimit = Utils.getNoNil(getXMLString(xmlFile, "vehicle.translationHyd#touchTransLimit"), 10);
    end;
    
	local rotationNode1 = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.rotation1#index"));
    if rotationNode1 ~= nil then
        self.rotation1 = {};
        self.rotation1.node = rotationNode1;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation1#minRot"));
        self.rotation1.minRot = {};
        self.rotation1.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation1.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation1.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation1#maxRot"));
        self.rotation1.maxRot = {};
        self.rotation1.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation1.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation1.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        self.rotation1.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation1#rotTime"), 2)*1000;
        self.rotation1.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation1#touchRotLimit"), 10));
    end;
	
	local rotationNode2 = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.rotation2#index"));
    if rotationNode2 ~= nil then
        self.rotation2 = {};
        self.rotation2.node = rotationNode2;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation2#minRot"));
        self.rotation2.minRot = {};
        self.rotation2.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation2.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation2.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation2#maxRot"));
        self.rotation2.maxRot = {};
        self.rotation2.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation2.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation2.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        self.rotation2.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation2#rotTime"), 2)*1000;
        self.rotation2.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation2#touchRotLimit"), 10));
    end;
    local rotationNode3 = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.rotation3#index"));
    if rotationNode3 ~= nil then
        self.rotation3 = {};
        self.rotation3.node = rotationNode3;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation3#minRot"));
        self.rotation3.minRot = {};
        self.rotation3.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation3.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation3.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation3#maxRot"));
        self.rotation3.maxRot = {};
        self.rotation3.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation3.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation3.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        self.rotation3.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation3#rotTime"), 2)*1000;
        self.rotation3.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation3#touchRotLimit"), 10));
    end;
    
      local rotationNode4 = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.rotation4#index"));
    if rotationNode4 ~= nil then
        self.rotation4 = {};
        self.rotation4.node = rotationNode4;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation4#minRot"));
        self.rotation4.minRot = {};
        self.rotation4.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation4.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation4.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation4#maxRot"));
        self.rotation4.maxRot = {};
        self.rotation4.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation4.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation4.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        self.rotation4.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation4#rotTime"), 2)*1000;
        self.rotation4.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation4#touchRotLimit"), 10));
    end;
 
         local rotationNode5 = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.rotation5#index"));
    if rotationNode5 ~= nil then
        self.rotation5 = {};
        self.rotation5.node = rotationNode5;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation5#minRot"));
        self.rotation5.minRot = {};
        self.rotation5.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation5.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation5.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation5#maxRot"));
        self.rotation5.maxRot = {};
        self.rotation5.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation5.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation5.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        self.rotation5.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation5#rotTime"), 2)*1000;
        self.rotation5.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation5#touchRotLimit"), 10));
    end;
 
	local rotationNode6 = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.rotation6#index"));
    if rotationNode6 ~= nil then
        self.rotation6 = {};
        self.rotation6.node = rotationNode6;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation6#minRot"));
        self.rotation6.minRot = {};
        self.rotation6.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation6.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation6.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation6#maxRot"));
        self.rotation6.maxRot = {};
        self.rotation6.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation6.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation6.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        self.rotation6.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation6#rotTime"), 2)*1000;
        self.rotation6.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation6#touchRotLimit"), 10));
    end;

hydra1SoundFile = Utils.getFilename("Sounds/hydra1.wav", self.baseDirectory);
    self.hydra1SoundId = createSample("HYD1_hydra1");
    loadSample(self.hydra1SoundId, hydra1SoundFile, false);
    self.hydra1Playing = false;
    
hydra2SoundFile = Utils.getFilename("Sounds/hydra2.wav", self.baseDirectory);
    self.hydra2SoundId = createSample("HYD2_hydra2");
    loadSample(self.hydra2SoundId, hydra2SoundFile, false);
    self.hydra2Playing = false;

hydra3SoundFile = Utils.getFilename("Sounds/hydra3.wav", self.baseDirectory);
    self.hydra3SoundId = createSample("HYD3_hydra3");
    loadSample(self.hydra3SoundId, hydra3SoundFile, false);
    self.hydra3Playing = false;

hydraRotSoundFile = Utils.getFilename("Sounds/hydraRot.wav", self.baseDirectory);
    self.hydraRotSoundId = createSample("HYDRot_hydraRot");
    loadSample(self.hydraRotSoundId, hydraRotSoundFile, false);
    self.hydraRotPlaying = false;
    
hydraRotBSoundFile = Utils.getFilename("Sounds/hydraRotB.wav", self.baseDirectory);
    self.hydraRotBSoundId = createSample("HYDRotB_hydraRotB");
    loadSample(self.hydraRotBSoundId, hydraRotBSoundFile, false);
    self.hydraRotBPlaying = false;    

end;
      
function ICrane:delete()
end;

function ICrane:mouseEvent(posX, posY, isDown, isUp, button)
end;
	  
function ICrane:keyEvent(unicode, sym, modifier, isDown)

    if sym == Input.KEY_KP_3 then
		self.translationMin = isDown;
		self.translationHookMin = isDown;
	end;
	if sym == Input.KEY_KP_9 then
		self.translationMax = isDown;
		self.translationHookMax = isDown;
	end;
	if sym == Input.KEY_KP_2 then
		self.rotation1Min = isDown;
		self.translationHydMin = isDown;
	end;
	if sym == Input.KEY_KP_8 then
		self.rotation1Max = isDown;
		self.translationHydMax = isDown;
	end;
	if sym == Input.KEY_KP_7 then
		self.rotation2Min = isDown;
	end;
	if sym == Input.KEY_KP_1 then
		self.rotation2Max = isDown;
	end;
	if sym == Input.KEY_v then
		self.rotation5Max = isDown;
	end;
	if sym == Input.KEY_b then
		self.rotation5Min = isDown;
	end;
	if sym == Input.KEY_KP_4 then
		self.rotation6Max = isDown;
	end;
	if sym == Input.KEY_KP_6 then
		self.rotation6Min = isDown;
	end;
	if sym == Input.KEY_n then
		self.rotation4Max = isDown;
	end;
	if sym == Input.KEY_m then
		self.rotation4Min = isDown;
	end;
end;

function ICrane:update(dt)
	
	local doTranslate = self.translationMax or self.translationMin 
	if self.translationPart ~= nil and doTranslate then 
		local x, y, z = getTranslation(self.translationPart.node); 
		local trans = {x,y,z}; 
		local newTrans = Utils.getMovedLimitedValues(trans, self.translationPart.maxTrans, self.translationPart.minTrans, 3, self.translationPart.transTime, dt, not self.translationMax); 
		setTranslation(self.translationPart.node, unpack(newTrans)); 
	end; 
	
	local doTranslate = self.translationHookMax or self.translationHookMin 
	if self.translationHook ~= nil and doTranslate then 
		local x, y, z = getTranslation(self.translationHook.node); 
		local trans = {x,y,z}; 
		local newTrans = Utils.getMovedLimitedValues(trans, self.translationHook.maxTrans, self.translationHook.minTrans, 3, self.translationHook.transTime, dt, not self.translationHookMax); 
		setTranslation(self.translationHook.node, unpack(newTrans)); 
	end;	

	local doTranslate = self.translationHydMax or self.translationHydMin 
	if self.translationHyd ~= nil and doTranslate then 
		local x, y, z = getTranslation(self.translationHyd.node); 
		local trans = {x,y,z}; 
		local newTrans = Utils.getMovedLimitedValues(trans, self.translationHyd.maxTrans, self.translationHyd.minTrans, 3, self.translationHyd.transTime, dt, not self.translationHydMax); 
		setTranslation(self.translationHyd.node, unpack(newTrans)); 
	end;

	local doRotate = self.rotation1Max or self.rotation1Min 
	if self.rotation1 ~= nil and doRotate then
        local x, y, z = getRotation(self.rotation1.node);
        local rot = {x,y,z};
        local newRot = Utils.getMovedLimitedValues(rot, self.rotation1.maxRot, self.rotation1.minRot, 3, self.rotation1.rotTime, dt, not self.rotation1Max);
        setRotation(self.rotation1.node, unpack(newRot));
        if not self.hydra1Playing then
            playSample(self.hydra1SoundId, 0, 1, 0);
            self.hydra1Playing = true;
        end;
    else
        if self.hydra1Playing then
            stopSample(self.hydra1SoundId);
            self.hydra1Playing = false;
        end;
    end;
	
	local doRotate = self.rotation2Max or self.rotation2Min 
	if self.rotation2 ~= nil and doRotate then
        local x, y, z = getRotation(self.rotation2.node);
        local rot = {x,y,z};
        local newRot = Utils.getMovedLimitedValues(rot, self.rotation2.maxRot, self.rotation2.minRot, 3, self.rotation2.rotTime, dt, not self.rotation2Max);
        setRotation(self.rotation2.node, unpack(newRot));
        if not self.hydra2Playing then
            playSample(self.hydra2SoundId, 0, 1, 0);
            self.hydra2Playing = true;
        end;
    else
        if self.hydra2Playing then
            stopSample(self.hydra2SoundId);
            self.hydra2Playing = false;
        end;
    end;
    
    local doRotate = self.rotation3Max or self.rotation3Min 
	if self.rotation3 ~= nil and doRotate then
        local x, y, z = getRotation(self.rotation3.node);
        local rot = {x,y,z};
        local newRot = Utils.getMovedLimitedValues(rot, self.rotation3.maxRot, self.rotation3.minRot, 3, self.rotation3.rotTime, dt, not self.rotation3Max);
        setRotation(self.rotation3.node, unpack(newRot));
    end;
    
       local doRotate = self.rotation4Max or self.rotation4Min 
	if self.rotation4 ~= nil and doRotate then
        local x, y, z = getRotation(self.rotation4.node);
        local rot = {x,y,z};
        local newRot = Utils.getMovedLimitedValues(rot, self.rotation4.maxRot, self.rotation4.minRot, 3, self.rotation4.rotTime, dt, not self.rotation4Max);
        setRotation(self.rotation4.node, unpack(newRot));
        if not self.hydra3Playing then
            playSample(self.hydra3SoundId, 0, 1, 0);
            self.hydra3Playing = true;
        end;
    else
        if self.hydra3Playing then
            stopSample(self.hydra3SoundId);
            self.hydra3Playing = false;
        end;
    end;
	
	   local doRotate = self.rotation5Max or self.rotation5Min 
	if self.rotation5 ~= nil and doRotate then
        local x, y, z = getRotation(self.rotation5.node);
        local rot = {x,y,z};
        local newRot = Utils.getMovedLimitedValues(rot, self.rotation5.maxRot, self.rotation5.minRot, 3, self.rotation5.rotTime, dt, not self.rotation5Max);
        setRotation(self.rotation5.node, unpack(newRot));
        if not self.hydraRotBPlaying then
            playSample(self.hydraRotBSoundId, 0, 1, 0);
            self.hydraRotBPlaying = true;
        end;
    else
        if self.hydraRotBPlaying then
            stopSample(self.hydraRotBSoundId);
            self.hydraRotBPlaying = false;
        end;
    end;
	
	local doRotate = self.rotation6Max or self.rotation6Min 
	if self.rotation6 ~= nil and doRotate then
        local x, y, z = getRotation(self.rotation6.node);
        local rot = {x,y,z};
        local newRot = Utils.getMovedLimitedValues(rot, self.rotation6.maxRot, self.rotation6.minRot, 3, self.rotation6.rotTime, dt, not self.rotation6Max);
        setRotation(self.rotation6.node, unpack(newRot));
        if not self.hydraRotPlaying then
            playSample(self.hydraRotSoundId, 0, 1, 0);
            self.hydraRotPlaying = true;
        end;
    else
        if self.hydraRotPlaying then
            stopSample(self.hydraRotSoundId);
            self.hydraRotPlaying = false;
        end;
    end;
end;

function ICrane:draw()
end;

function ICrane:validateAttacherJoint(implement, jointDesc, dt)
    return true;
end;
