---------------------------------
-----------Lama-----------
---------------------------------
---------------------------------
--------- @matteo135 ---------
-------- @date  3/03/2011 --------
---------------------------------

Lama = {};

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

function Lama:load(xmlFile)

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

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

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

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

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

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

        self.rotation0.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation0#rotTime"), 2)*1000;
        self.rotation0.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation0#touchRotLimit"), 10));
    end;
	--rotazione pistone2
	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;
	--rotazione lama
	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;

end;

function Lama:delete()
end;

function Lama:mouseEvent(posX, posY, isDown, isUp, button)
end;

function Lama:keyEvent(unicode, sym, modifier, isDown)
         if sym == Input.KEY_KP_1 then
		self.translation1Max = isDown;
		self.translation2Max = isDown;
		self.rotation0Max = isDown;
		self.rotation1Max = isDown;
		self.rotation2Max = isDown;
	    end;
	     if sym == Input.KEY_KP_2 then
		self.translation1Min = isDown;
		self.translation2Min = isDown;
		self.rotation0Min = isDown;
		self.rotation1Min = isDown;
		self.rotation2Min = isDown;
	    end;
	 end;

function Lama:update(dt)
       	
        --stelo
		local doTranslate = self.translation1Max or self.translation1Min
		if self.translation1 ~= nil and doTranslate then
			local x, y, z = getTranslation(self.translation1.node);
			local trans = {x,y,z};
			local newTrans = Utils.getMovedLimitedValues(trans, self.translation1.maxTrans, self.translation1.minTrans, 3, self.translation1.transTime, dt, not self.translation1Max);
			setTranslation(self.translation1.node, unpack(newTrans));
		end;
		local doTranslate = self.translation2Max or self.translation2Min
		if self.translation2 ~= nil and doTranslate then
			local x, y, z = getTranslation(self.translation2.node);
			local trans = {x,y,z};
			local newTrans = Utils.getMovedLimitedValues(trans, self.translation2.maxTrans, self.translation2.minTrans, 3, self.translation2.transTime, dt, not self.translation2Max);
			setTranslation(self.translation2.node, unpack(newTrans));
		end;
		--pistone
     local doRotate = self.rotation0Max or self.rotation0Min
	 if self.rotation0 ~= nil and doRotate then
		local x, y, z = getRotation(self.rotation0.node);
		local rot = {x,y,z};
		local newRot = Utils.getMovedLimitedValues(rot, self.rotation0.maxRot, self.rotation0.minRot, 3, self.rotation0.rotTime, dt, not self.rotation0Max);
		setRotation(self.rotation0.node, unpack(newRot));
	end;
	--Pistone
	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));
	end;
	--Lama
	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));
	end;
end;

function Lama:draw()
    g_currentMission:addExtraPrintText("Tasti KP 1/2 : alza/abbassa lama");
end;

function Lama:onAttach(attacherVehicle)
end;

function Lama:onDetach()
end;

function Lama:onEnter()
end;

function Lama:onLeave()
end;

function Lama:onActivate()
end;

function Lama:onDeactivate()
end;

function Lama:onDeactivateSounds()
end;

function Lama:addContactReports()
end;

function Lama:removeContactReports()
end;

function Lama:groundContactReport(objectId, otherObjectId, isStart, normalForce, tangentialForce)

end;

function Lama:validateAttacherJoint(implement, jointDesc, dt)
end;
