--
-- T150K
-- Specialization for T150K
--
-- @author      Manuel Leithner (SFM-Modding)
-- @version     v2.0
-- @date          15/10/10
-- @history:    v1.0 - Initial version
--                v2.0 - added network support, changed update to updateTick
--

T150K = {};

function T150K.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Motorized, specializations);
end;

function T150K:load(savegame)

local rotationPartNodeKmh1 = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.rotationPartKmh1#index"));
-- fan
    self.fan1 = Utils.indexToObject(self.rootNode, getXMLString(self.xmlFile, "vehicle.fan1#index"));
    self.fan2 = Utils.indexToObject(self.rootNode, getXMLString(self.xmlFile, "vehicle.fan2#index"));
    self.fan3 = Utils.indexToObject(self.rootNode, getXMLString(self.xmlFile, "vehicle.fan3#index"));
    self.fan4 = Utils.indexToObject(self.rootNode, getXMLString(self.xmlFile, "vehicle.fan4#index"));
    self.fan5 = Utils.indexToObject(self.rootNode, getXMLString(self.xmlFile, "vehicle.fan5#index"));
    self.fan6 = Utils.indexToObject(self.rootNode, getXMLString(self.xmlFile, "vehicle.fan6#index"));
-- motor
    self.motor1Active = false;
    self.motor2Active = false;
    self.motor1Max = false;
    self.motor2Max = false;
    self.delayKurs1 = 0;
    self.delayKurs2 = 0;
    local motor1Node = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.motor1#index"));
    if motor1Node ~= nil then
        self.motor1 = {};
        self.motor1.node = motor1Node;
        local x, y, z = Utils.getVectorFromString(getXMLString(self.xmlFile, "vehicle.motor1#minRot"));
        self.motor1.minRot = {};
        self.motor1.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.motor1.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.motor1.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));
        x, y, z = Utils.getVectorFromString(getXMLString(self.xmlFile, "vehicle.motor1#maxRot"));
        self.motor1.maxRot = {};
        self.motor1.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.motor1.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.motor1.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));
        self.motor1.rotTime = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.motor1#rotTime"), 2)*70;
        self.motor1.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.motor1#touchRotLimit"), 10));
    end;
    local motor2Node = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.motor2#index"));
    if motor2Node ~= nil then
        self.motor2 = {};
        self.motor2.node = motor2Node;
        local x, y, z = Utils.getVectorFromString(getXMLString(self.xmlFile, "vehicle.motor2#minRot"));
        self.motor2.minRot = {};
        self.motor2.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.motor2.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.motor2.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));
        x, y, z = Utils.getVectorFromString(getXMLString(self.xmlFile, "vehicle.motor2#maxRot"));
        self.motor2.maxRot = {};
        self.motor2.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.motor2.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.motor2.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));
        self.motor2.rotTime = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.motor2#rotTime"), 2)*70;
        self.motor2.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.motor2#touchRotLimit"), 10));
    end;
end;

function T150K:delete()
end;

function T150K:readStream(streamId, connection)
end;

function T150K:writeStream(streamId, connection)
end;

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

function T150K:keyEvent(unicode, sym, modifier, isDown)
end;

function T150K:update(dt)
-- fan
    if self.isMotorStarted then
        if self.fan1 ~= nil then
            rotate(self.fan1, 0, 0, dt*0.05);
        end;
        if self.fan2 ~= nil then
            rotate(self.fan2, 0, 0, dt*0.05);
        end;
        if self.fan3 ~= nil then
            rotate(self.fan3, 0, 0, dt*0.05);
        end;
        if self.fan4 ~= nil then
            rotate(self.fan4, 0, 0, dt*0.05);
        end;
        if self.fan5 ~= nil then
            rotate(self.fan5, 0, 0, dt*0.05);
        end;
        if self.fan6 ~= nil then
            rotate(self.fan6, 0, 0, dt*0.05);
        end;
    end;
-- motor
    if self.motor1Active ~= nil and self.isMotorStarted then
        if self.delayKurs1 >= 0 then
            self.delayKurs1 = self.delayKurs1 -dt;
        end;
        if self.delayKurs2 >= 0 then
            self.delayKurs2 = self.delayKurs2 -dt;
        end;
        if not self.motor1Max then
            if self.delayKurs1 < 0 then
                self.delayKurs2 = 70;
                self.motor1Max = true;
                self.motor2Max = true;
            end;
        end;
        if self.motor1Max then
            if self.delayKurs2 < 0 then
                self.delayKurs1 = 70;
                self.motor1Max = false;
                self.motor2Max = false;
            end;
        end;
    else
        self.motor1Max = false;
        self.motor2Max = false;
    end;
    if self.motor1 ~= nil then
        local x, y, z = getRotation(self.motor1.node);
        local rot = {x,y,z};
        local newRot = Utils.getMovedLimitedValues(rot, self.motor1.maxRot, self.motor1.minRot, 3, self.motor1.rotTime, dt, not self.motor1Max);
        setRotation(self.motor1.node, unpack(newRot));
    end;
    if self.motor2 ~= nil then
        local x, y, z = getRotation(self.motor2.node);
        local rot = {x,y,z};
        local newRot = Utils.getMovedLimitedValues(rot, self.motor2.maxRot, self.motor2.minRot, 3, self.motor2.rotTime, dt, not self.motor2Max);
        setRotation(self.motor2.node, unpack(newRot));
    end;
end;

function T150K:updateTick(dt)
end;

function T150K:draw()
end;

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