Dynamical System Framework
Loading...
Searching...
No Matches
TrafficLight.hpp File Reference

Phase-based traffic light state machine. More...

#include "Intersection.hpp"
#include "../utility/Typedef.hpp"
#include <format>
#include <numeric>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

Classes

class  dsf::mobility::TrafficLightPhase
 One phase in a TrafficLight program. More...
class  dsf::mobility::TrafficLight
struct  std::formatter< dsf::mobility::TrafficLightPhase >
struct  std::formatter< dsf::mobility::TrafficLight >

Namespaces

namespace  dsf::mobility

Detailed Description

Phase-based traffic light state machine.

Design overview ─────────────── A TrafficLight cycles through an ordered sequence of TrafficLightPhase objects. Each phase has a fixed duration (in simulation ticks) and an explicit green set: the collection of (streetId, Direction) pairs that are green while that phase is active. Every street/direction pair that is absent from the green set is implicitly red — no arithmetic offsets or wraparound logic is required.

State-machine transition (operator++): m_counter is incremented every tick. When m_counter reaches m_phases[m_currentPhaseIndex].duration() the machine moves to the next phase (wrapping) and resets m_counter to 0.

Query (isGreen): Looks up the active phase's green set and applies the standard direction-fallback ladder (RIGHT → RIGHTANDSTRAIGHT → ANY, etc.). If the street is absent entirely, returns m_allowFreeTurns.

Reset API: snapshot() — saves current phase sequence as a restore point. restore() — returns to the last snapshot. reset() — returns to construction-time defaults (set by the last setPhases() / addPhase() calls).

Optimiser interface: phase(i).setDuration(d) — mutates a single phase duration in-place. defaultPhases() — read-only view of baseline durations. advanceBy(offset) — fast-forwards the state machine by offset ticks (used for green-wave synchronisation).