Vehicles/VehicleScripts/SnowDisplacement.lua

Handles the displacement of snow under vehicles. 4 points get set and snow abouve the area spanned by those points gets cancelled. The vehicle's data table always contains a structure of 4 points for each snow displacement area. The data table can contain any desired number of those structures.

Please mind following: when looking in vehicle forward direction, P1 should be the front left corner, P2 the front right, P3 the rear left and P4 the rear right corner.

  28  SnowDisplacement                                = SnowDisplacement or {};

SnowDisplacement:load(dataTable)

Loads all values from the vehicle's data table.

  32  function SnowDisplacement:load(dataTable)
  33      if dataTable.snowDisplacement ~= nil then
  34          self.snowDisplacements                  = {};
  35          self.snowDisplLastPosition              = {};
  36  
  37          for k, v in pairs(dataTable.snowDisplacement) do
  38              local snowDisplacementP1            = getChild(self.id, v.snowDisplacementLowerLeft);
  39              local snowDisplacementP2            = getChild(self.id, v.snowDisplacementLowerRight);
  40              local snowDisplacementP3            = getChild(self.id, v.snowDisplacementUpperLeft);
  41              local snowDisplacementP4            = getChild(self.id, v.snowDisplacementUpperRight);
  42  
  43              self.snowDisplacements[k]           = { snowDisplacementP1, snowDisplacementP2, snowDisplacementP3, snowDisplacementP4 };
  44              self.snowDisplLastPosition[k]       = {
  45                  VectorUtils.getWorldPosition(snowDisplacementP1),
  46                  VectorUtils.getWorldPosition(snowDisplacementP2),
  47                  VectorUtils.getWorldPosition(snowDisplacementP3),
  48                  VectorUtils.getWorldPosition(snowDisplacementP4)
  49              };
  50          end;
  51      end;
  52  
  53      self.snowDisplLastReference                 = Vector3.zero:clone();
  54  end;

SnowDisplacement:update(dt)

In general, the script's update will only be executed if:

  • this is a server instance (or singleplayer) AND
  • the vehicle is active (i.e. someone is sitting in it) AND
  • the vehicle has moved more than 10 cm since the last update AND
  • snow displacement has been configured properly.
  62  function SnowDisplacement:update(dt)
  63      if g_isClient or not self.isActive or self.snowDisplacements == nil or self.snowDisplacements[1] == nil or self.snowDisplacements[1][1] == nil then return end;
  64  
  65      local travelledDistanceSinceLastUpdate      = Vector3.magnitude(self.snowDisplLastReference - VectorUtils.getWorldPosition(self.snowDisplacements[1][1]));
  66      local isGoingForward                        = self.currentSpeed >= 0.1;
  67      local isGoingBackward                       = self.currentSpeed <= -0.1;
  68  
  69      if travelledDistanceSinceLastUpdate > 0.1 then
  70  
  71          for k, v in pairs(self.snowDisplacements) do
  72              local pos1                          = VectorUtils.getWorldPosition(self.snowDisplacements[k][1]);
  73              local pos2                          = VectorUtils.getWorldPosition(self.snowDisplacements[k][2]);
  74              local pos3                          = VectorUtils.getWorldPosition(self.snowDisplacements[k][3]);
  75              local pos4                          = VectorUtils.getWorldPosition(self.snowDisplacements[k][4]);
  76  
  77              if isGoingForward then
  78                  SnowSystem.displaceSnowUnderVehicle(self.snowDisplLastPosition[k][1], self.snowDisplLastPosition[k][2], pos3, pos4);
  79  
  80              elseif isGoingBackward then
  81                  SnowSystem.displaceSnowUnderVehicle(pos1, pos2, self.snowDisplLastPosition[k][3], self.snowDisplLastPosition[k][4]);
  82              end;
  83  
  84              if g_isServer then
  85                  if isGoingForward then
  86                      EventDisplaceSnow:send(self.snowDisplLastPosition[k][1], self.snowDisplLastPosition[k][2], pos3, pos4);
  87  
  88                  elseif isGoingBackward then
  89                      EventDisplaceSnow:send(pos1, pos2, self.snowDisplLastPosition[k][3], self.snowDisplLastPosition[k][4]);
  90                  end;
  91              end;
  92  
  93              self.snowDisplLastPosition[k][1]    = pos1;
  94              self.snowDisplLastPosition[k][2]    = pos2;
  95              self.snowDisplLastPosition[k][3]    = pos3;
  96              self.snowDisplLastPosition[k][4]    = pos4;
  97          end;
  98  
  99          self.snowDisplLastReference = VectorUtils.getWorldPosition(self.snowDisplacements[1][1]);
 100      end;
 101  end;

All contents of this page may be used for modding use for Winter Resort Simulator - Season 2 only. Any use exceeding this regulation is not permitted.

Copyright (C) HR Innoways, 2021. All Rights Reserved.