Table of Contents

Traffic Lights

Traffic lights are used at urban street intersections. They are visual indicators of the traffic control regulations.

Configuration

Traffic lights are configurated in a .lua file in the res/config/traffic_light/ folder. It's content is:

function data()
  return {
    yearFrom = 1980,
    tryOpposite = true,
    modelFn = function,
  }
end

There are three properties that are returned:

modelFn

The modelFn is a function that is called for every incoming street at an intersection to compute the traffic lights models. It gets several parameters in a data struct:

key type description
needsLight list of boolean from right to left
true for every lane that needs a light, including sidewalk lanes
false for every lane that needs no light at that position.
pedestrian list of boolean type of lane from right to left
true for every sidewalk lane
false for ever car lane
position transf reference position at the right side of the street next to the outermost lane
widths list of float the width of each lane including sidewalks from right to left
oppositePosition transf reference position if the location is behind the intersection instead of before the intersection.

The expected return data struct contains a list with n nested lists where n is the number of lanes with needsLight == true. Each of the nested lists contains pairs with id and transf where

An exemplaric result data struct for a one-way street with one car lane could look like this:

{
  { -- sidewalk right
    { id = "street/traffic_light_eu_c/pedestrian_light.mdl", transf = { ... } } 
  }, 
  { --car lane
    { id = "street/traffic_light_eu_c/traffic_light_1.mdl", transf = { ... } }, 
    { id = "street/traffic_light_eu_c/pole.mdl", transf = { ... } }
  }, 
  { --sidewalk left
    { id = "street/traffic_light_eu_c/pedestrian_light.mdl", transf = { ... } }, 
    { id = "street/traffic_light_eu_c/pedestrian_pole.mdl", transf = { ... } }
  }
}

trafficlightutil.lua

There is a prebuilt modelFn function available in the res/scripts/trafficlightutil.lua script. It requires a data struct with several properties:

local input = {
  models = {
    trafficLightPole = "street/traffic_light_us_c/pole.mdl",
    pedestrianPole = "street/traffic_light_us_c/pedestrian_pole.mdl",
    beam = {
      "street/traffic_light_us_c/beam_1.mdl",
      "street/traffic_light_us_c/beam_2.mdl",
      "street/traffic_light_us_c/beam_r.mdl",
      -- last model is repeated
    },
    trafficLight = {
      "street/traffic_light_us_c/traffic_light_1.mdl",
      "street/traffic_light_us_c/traffic_light_r.mdl",
      -- last model is repeated
    },
    pedestrianLight = "street/traffic_light_us_c/pedestrian_light.mdl",
  },
  params = {
    offset = -0.25,
    beamWidth = { 4 + .25, 4, 4 }, -- on model repetition last entry is used
    lightOffset = { 2, 2 }, -- offset is relative to beam -- on model repetition last entry is used
    poleTrafficLight = false
  }
}
 
...
 
  modelFn = trafficlightutil.standardLights(input),

The models that should be used by the script are provided in the models block:

Additional parameters are provided in the params block:

Traffic light models

The traffic light models as well as the pedestriant light models can use the following events:

Usually a traffic light is in red state and the idle_red animation is performed. As soon as it switches to green, green is performed once. Then idle_green is looped as long as the traffic light stays green. When the traffic light turns back to red, red is performed once before idle_red is looped again.

The idle animations could be used e.g. for blinking traffic lights.