Excavatorrpm = {};


    function Excavatorrpm.prerequisitesPresent(specializations)
        return SpecializationUtil.hasSpecialization(Drivable, specializations);
    end;
    
    function Excavatorrpm:load(savegame)
        self.regime2 = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.regime2#index"));
        self.regime3 = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.regime3#index"));
        self.playExcavatorrpm = SpecializationUtil.callSpecializationsFunction("playExcavatorrpm");
    
        if self.isClient then
            self.sampleExcavatorrpm = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.ExcavatorrpmSound", nil, self.baseDirectory, self.components[1].node);
            self.ExcavatorrpmSoundPitchOffset = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.ExcavatorrpmSound#pitchOffset"), 0);
            self.ExcavatorrpmSoundPitchScale = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.ExcavatorrpmSound#pitchScale"), 0.05);
            self.ExcavatorrpmSoundPitchMax = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.ExcavatorrpmSound#pitchMax"), 2.0);
            self.ExcavatorrpmSoundVolume = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.ExcavatorrpmSound#volume"), 1.0);
        end;
    end;
    
    function Excavatorrpm:delete()
        if self.isClient then
            SoundUtil.deleteSample(self.sampleExcavatorrpm);
        end;
    end;
    
    function Excavatorrpm:readStream(streamId, connection)
    end;
    
    function Excavatorrpm:writeStream(streamId, connection)
    end;
    
    function Excavatorrpm:mouseEvent(posX, posY, isDown, isUp, button)
    end;
    
    function Excavatorrpm:keyEvent(unicode, sym, modifier, isDown)
    end;
    


function Excavatorrpm:update(dt)
    local speedFactor = Utils.clamp(self:getLastSpeed() / math.ceil(self.motor:getMaximumForwardSpeed()*3.6), 0, 1);
    if self.isMotorStarted then
        if self.isEntered and self.isClient then
            if self.sampleExcavatorrpm ~= nil then
                if SoundUtil.isSamplePlaying(self.sampleMotorStart, 1.5*dt) then
                            self:playExcavatorrpm(false);
                    else
                            self:playExcavatorrpm(true);    
                end;
            end;
        end;
    end;
end;
    
    function Excavatorrpm:updateTick(dt)
    end;
    
    function Excavatorrpm:draw()
        if GS_IS_CONSOLE_VERSION then 
            if self.isClient and self:getIsActiveForInput(true) then
                g_currentMission:addHelpButtonText(g_i18n:getText("Excavatorrpm"), InputBinding.Regime);
            end;
        end;
    end;
    
    function Excavatorrpm:onDeactivateSounds()
        if self.isClient then
            SoundUtil.stopSample(self.sampleExcavatorrpm, true);
            SoundUtil.stop3DSample(self.sampleExcavatorrpm);
        end;
    end;
    
    function Excavatorrpm:playExcavatorrpm(isPlaying, noEventSend)
        if self.sampleExcavatorrpm ~= nil then
            ExcavatorrpmEvent.sendEvent(self, isPlaying, noEventSend);

            if isPlaying then
                if self:getIsActive() then
                    if self:getIsActiveForSound() then
                        SoundUtil.playSample(self.sampleExcavatorrpm, 0, 0, nil);
                    local x,y,z = getWorldTranslation(self.regime3);
                    local speedFactor = Utils.clamp(self:getLastSpeed() / math.ceil(self.motor:getMaximumForwardSpeed()*3.6), 0, 1);
                    local a,b,c = getWorldTranslation(self.regime2);
                    local benne = Utils.vector3Length(x-a, y-b, z-c);
                    if self.sampleExcavatorrpm ~= nil then                
                        local ExcavatorrpmSoundPitch = math.min(self.ExcavatorrpmSoundPitchOffset + self.ExcavatorrpmSoundPitchScale*math.abs(benne*4), self.ExcavatorrpmSoundPitchMax)
                        SoundUtil.setSamplePitch(self.sampleExcavatorrpm, ExcavatorrpmSoundPitch);
                    end;    
                    elseif self.isControlled then
                        -- enable 3d sound to vehicle on another player's game
                        SoundUtil.play3DSample(self.sampleExcavatorrpm);
                        local x,y,z = getWorldTranslation(self.regime3);
                        local a,b,c = getWorldTranslation(self.regime2);
                        local benne = Utils.vector3Length(x-a, y-b, z-c);
                        if self.sampleExcavatorrpm ~= nil then                
                            local ExcavatorrpmSoundPitch = math.min(self.ExcavatorrpmSoundPitchOffset + self.ExcavatorrpmSoundPitchScale*math.abs(benne*4), self.ExcavatorrpmSoundPitchMax)
                            SoundUtil.setSamplePitch(self.sampleExcavatorrpm, ExcavatorrpmSoundPitch);
                        end;
                    end;
                end;
            else
                Excavatorrpm.onDeactivateSounds(self);
            end;
        end;
    end;
    
    ExcavatorrpmEvent = {};
    ExcavatorrpmEvent_mt = Class(ExcavatorrpmEvent, Event);
    
    InitEventClass(ExcavatorrpmEvent, "ExcavatorrpmEvent");
    
    
    function ExcavatorrpmEvent:emptyNew()
    
    local self = Event:new(ExcavatorrpmEvent_mt);
        return self;
    end;
    
    function ExcavatorrpmEvent:new(object, isPlaying)
    
        local self = ExcavatorrpmEvent:emptyNew()
        self.object = object;
        self.isPlaying = isPlaying;
        return self;
    end;
    
    function ExcavatorrpmEvent:readStream(streamId, connection)
    
        local id = streamReadInt32(streamId);
        self.isPlaying = streamReadBool(streamId);
        self.object = networkGetObject(id);
        self:run(connection);
    end;
    
    function ExcavatorrpmEvent:writeStream(streamId, connection)
    
        streamWriteInt32(streamId, networkGetObjectId(self.object));
        streamWriteBool(streamId, self.isPlaying);
    end;
    
    function ExcavatorrpmEvent:run(connection)
    
        self.object:playExcavatorrpm(self.isPlaying, true);
        
        if not connection:getIsServer() then
            g_server:broadcastEvent(ExcavatorrpmEvent:new(self.object, self.isPlaying), nil, connection, self.object);
        end;
    end;
    
    function ExcavatorrpmEvent.sendEvent(vehicle, isPlaying, noEventSend)
    
        if isPlaying ~= vehicle.honkPlaying then
            if noEventSend == nil or noEventSend == false then
                if g_server ~= nil then
                    g_server:broadcastEvent(ExcavatorrpmEvent:new(vehicle, isPlaying), nil, nil, vehicle);
                else
                    g_client:getServerConnection():sendEvent(ExcavatorrpmEvent:new(vehicle, isPlaying));
                end;
            end;
        end;
    end;
    
