

Godet = {
    prerequisitesPresent = function (specializations)
        return true
    end
}

source("dataS/scripts/vehicles/specializations/SetTurnedOnEvent.lua");
source("dataS/scripts/vehicles/specializations/PloughAreaEvent.lua");

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

function Godet:load(savegame)


          assert(self.setIsTurnedOn == nil, "Sprayer needs to be the first specialization which implements setIsTurnedOn");
        self.setIsTurnedOn = Godet.setIsTurnedOn;
        self.isTurnedOn = false;
        self.activation = false;
        
        
        
        self.terrassement = false;
        self.terrassementNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassement#index"));

        self.terrassementNodeA = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementa#index"));
        self.terrassementNodeB = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementb#index"));
        self.terrassementNodeC = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementc#index"));
        self.terrassementNodeD = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementd#index"));
        self.terrassementNodeE = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassemente#index"));
        self.terrassementNodeF = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementf#index"));
        self.terrassementNodeG = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementg#index"));
        self.terrassementNodeH = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementh#index"));
        self.terrassementNodeI = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementi#index"));
        self.terrassementNodeJ = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementj#index"));
        self.terrassementNodeK = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementk#index"));
        self.terrassementNodeL = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementl#index"));
        self.terrassementNodeM = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementm#index"));
        self.terrassementNodeN = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassementn#index"));
        self.terrassementNodeO = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.terrassemento#index"));

      
        self.aiTerrainDetailChannel1 = g_currentMission.ploughChannel;
        self.aiTerrainDetailChannel2 = g_currentMission.sowingChannel;
    
        self.fillremplissage = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.fillremplissage"), 500);
        self.fillvidage = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.fillvidage"), 5);
        
        self.godet = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.godet#index"));

            
end;
  
function Godet:delete()


   end;
   
   
   function Godet:readStream(streamId, connection)
          local turnedOn = streamReadBool(streamId);
       self:setIsTurnedOn(turnedOn, true);
end;
 
function Godet:writeStream(streamId, connection)
streamWriteBool(streamId, self.isTurnedOn);
end;
 
 
   function Godet:mouseEvent(posX, posY, isDown, isUp, button)
   end;
 
   function Godet:keyEvent(unicode, sym, modifier, isDown)

   end;
 
   function Godet:update(dt)
   
    local px, py, pz = getWorldRotation(self.terrassementNodeA);

    
    
          for i=1, table.getn(g_currentMission.vehicles) do
       local fred = g_currentMission.vehicles[i];
       if fred.terrassementNode ~= nil then
       end;
       end;  
       
       
   
   for i=1, table.getn(g_currentMission.attachables) do
   local attachable = g_currentMission.attachables[i];
   if attachable.shovelEmptySpeed ~= nil then
       if attachable.shovelTipReferenceNode ~= nil then
       local dx, dy, dz = getWorldTranslation(attachable.shovelTipReferenceNode, 0, 0, 1)
       
       if dy < attachable.shovelEmptyStartCosAngle then
       local scale = math.min((attachable.shovelEmptyStartCosAngle - dy)/(attachable.shovelEmptyStartCosAngle - attachable.shovelEmptyFullCosAngle), 1)
       local fred =  attachable.shovelEmptySpeed*scale

                   end;
               end;
           end;
          end;
   
     
             if self.isClient then
           if self:getIsActiveForInput() then
               if InputBinding.hasEvent(InputBinding.Godet) then
                   self:setIsTurnedOn(not self.isTurnedOn);
               end;
           end;
       end;
       
        if self.isTurnedOn then
            self.activation = true ;
        else    
             self.activation = false;
        end;
   end;
 
   function Godet:updateTick(dt)
   
   
          for i=1, table.getn(g_currentMission.vehicles) do
       local fred = g_currentMission.vehicles[i];
       if fred.terrassementNode ~= nil then
       end;
       end; 
       
       
       if self:getIsActiveForInput() then
        if InputBinding.hasEvent(InputBinding.Godet) then
            self.activation = not self.activation;
        end;
    end;

   
      


   



                        local x,y,z = getWorldTranslation(self.terrassementNode);
                        local terrainHeight = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x, 0, z);
                        if y<= terrainHeight then
                            self.terrassement = true;
                        else
                            self.terrassement = false;
                        end;


           if self.terrassement then
            if self.movingDirection == 1 then
               if self.activation then
                if self.fillLevel ~= self.capacity then
                          local workAreasSend = {};
                          for k, workArea in pairs(self.workAreas) do
                               if self:getIsAreaActive(workArea) then

                                   local x,y,z = getWorldTranslation(workArea.start);
                                   local x1,y1,z1 = getWorldTranslation(workArea.width);
                                   local x2,y2,z2 = getWorldTranslation(workArea.height);
                                   --Utils.updatePloughArea(x, z, x1, z1, x2, z2, not self.PloughLimitToField);
                                   table.insert(workAreasSend, {x,z,x1,z1,x2,z2});
                               end;
                           end;
                           if (table.getn(workAreasSend) > 0) then
                               local realArea = PloughAreaEvent.runLocally(workAreasSend, limitToField, limitGrassDestructionToField, angle);
                               local usage = self.fillremplissage * dt*0.001;
                               local usedFruitType = FruitUtil.FRUITTYPE_DIRT;
                               local usedFillType = FruitUtil.fruitTypeToFillType[usedFruitType];                                                                             
                               self:setFillLevel(self.fillLevel + usage, usedFillType);
                              end; 
                        
                        end;
                         end;
                            end;
                               end;
         
   end;
 
   function Godet:draw()
       if self.activation then
        g_currentMission:addHelpButtonText(g_i18n:getText("TerrOff"), InputBinding.Godet);
        else
        g_currentMission:addHelpButtonText(g_i18n:getText("TerrOn"), InputBinding.Godet);
    end;
    
   end;
 
 Godet.loadFromAttributesAndNodes = function (self, xmlFile, key, resetVehicles)
    return BaseMission.VEHICLE_LOAD_OK
end
Godet.getSaveAttributesAndNodes = function (self, nodeIdent)
    local attributes = ""
    local nodes = ""

    return attributes, nodes
end

   function Godet:onAttach(attacherVehicle)
   setVisibility(self.godet, false);
  end;
  
   function Godet:onDetach()
   setVisibility(self.godet, true);
   end;
 
   function Godet:onActivate()
   end;
 
   function Godet:onDeactivate()       
   end;
 
   function Godet:onDeactivateSounds()
   end;
 
   function Godet:aiRotateLeft()
   end;
 
   function Godet:aiRotateRight()
   end;
 
   function Godet:aiTurnOn()
   end;
 
   function Godet:aiTurnOff()
   end;
 
   function Godet:aiInvertsMarkerOnTurn(turnLeft)
   end;
 
 
 

   function Godet:setRotationMax(rotationMax, noEventSend)
   end;
   
   
      function Godet:setIsTurnedOn(isTurnedOn, noEventSend)

               SetTurnedOnEvent.sendEvent(self, isTurnedOn, noEventSend)
   
               self.isTurnedOn = isTurnedOn;
 
              

   end;
   
      function Godet:onDeactivate()
   self.isTurnedOn = false;
   end;
   
      function Godet:onLeave()
      self.isTurnedOn = false;
    end;  



