%%% -*- mode: latex; TeX-PDF-mode: t -*-
%%%
%%% Copyright (c) 2020 Mark Wooding
%%%
%%% This document is free software.  You can you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as published
%%% by the Free Software Foundation; either version 3 of the License, or (at
%%% your option) any later version.
%%%
%%% This document is distributed in the hope that it will be useful, but
%%% WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
%%% Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this document.  If not, see <https://www.gnu.org/licenses/>.
%%%
%%% Alternatively, you may share and adapt this document under the terms of
%%% the Creative Commons Attribution--ShareAlike 4.0 International License
%%% (CC BY-SA 4.0); see https://creativecommons.org/licenses/by-sa/4.0/

\documentclass[a4paper, 10pt]{article}

\usepackage[T1]{fontenc}
\usepackage[palatino, helvetica, courier, maths=cmr]{mdwfonts}
\usepackage[margin=20mm]{geometry}

\newdimen\titlewidth \titlewidth = 70mm

\pagestyle{empty}
\errorcontextlines = 50
\showboxbreadth = \maxdimen \showboxdepth = \maxdimen

\newif\iftop \toptrue
\def\doctor #1{%
  \omit
  \vrule \iftop\else height 24pt \fi width 0pt depth 6pt
  \large \sffamily \bfseries
  \ignorespaces #1\unskip \hfil
  &&&&&\cr
}
\def\season #1{%
  \omit
  \vrule width 0pt height 14pt depth 4pt
  \sffamily \bfseries
  \ignorespaces #1\unskip \hfil
  &&&&&\cr
}
\def\lost#1{\textit{#1}}

\newdimen\hrwd \hrwd=0.2pt
\def\zwd#1{\hbox to 0pt{\hss#1\hss}}

\def\nil{\nil}
\def\gobble#1{}
\def\newqueue#1{%
  \expandafter\newtoks\csname q/#1\endcsname
}

\def\enqueue#1#2{%
  \global\csname q/#1\expandafter\endcsname=
    \expandafter{\the\csname q/#1\endcsname{#2}}%
}
\def\grabhead#1#2\endstop{\def\head{#1}}
\def\gethead#1{\expandafter\grabhead\the\csname q/#1\endcsname\nil\endstop}
\def\drophead#1{%
  \gethead{#1}%
  \edef\t{\global\csname q/#1\endcsname=%
    \noexpand\expandafter{\noexpand\gobble\the\csname q/#1\endcsname}}\t
}
\def\dequeue#1#2{%
  \gethead{#1}%
  \ifx\head\nil\def\head{#2}%
  \else\drophead{#1}\fi
  \head%
}
\def\foreach#1#2{%
  \def\next{%
    \gethead{#1}%
    \ifx\head\nil\let\next=\relax
    \else\drophead{#1}#2\fi
    \next%
  }%
  \next
}

\newqueue{t0}
\newqueue{t1}
\newqueue{t2}
\newqueue{t3}
\newqueue{name}

\def\namedef#1{\expandafter\gdef\csname#1\endcsname}
\def\namelet#1{\global\expandafter\let\csname#1\endcsname=}
\def\nameuse#1{\csname#1\endcsname}

\def\emptytrack{empty}
\def\fulltrack{full}

\namelet{dflt/empty}\relax
\namedef{dflt/full}{\hfil\vrule\hfil}

\namelet{dots/empty}\strut
\namedef{dots/full}{\strut\raisebox{-2pt}{\smash{{$\vdots$}}}}

\namedef{top/empty}#1{\strut}
\namedef{top/full}#1{%
  \strut
  \nameuse{sym/#1}%
  \enqueue{name}{$\phantom+$ \nameuse{name/#1}}%
}

\namelet{trk/t0}\emptytrack
\namelet{trk/t1}\emptytrack
\namelet{trk/t2}\emptytrack
\namelet{trk/t3}\emptytrack

\def\track#1#2{\hfil\dequeue{#1}{\nameuse{dflt/\nameuse{trk/#1}}}\hfil}

\def\trkendmarker#1{%
  \hfil
  \vrule depth -2.7pt
  \kern-\hrwd
  \zwd{#1}%
  \kern\hrwd
  \hfil
}

\def\join#1#2#3{%
  \enqueue{#1}{#2}%
  \namedef{sym/#1}{#2}%
  \namedef{name/#1}{#3}%
  \namelet{trk/#1}\fulltrack
  \enqueue{name}{$+$ #3}%
}
\def\depart#1{%
  \enqueue{#1}{\trkendmarker{\vrule height 2.9pt depth -2.5pt width 4pt}}%
  \namelet{trk/#1}\emptytrack
}
\def\die#1{%
  \enqueue{#1}{\trkendmarker{$\times$}}%
  \namelet{trk/#1}\emptytrack
}
\def\joindie#1#2#3{%
  \enqueue{#1}{\zwd{#2}\zwd{\raisebox{1pt}{$\times$}}}%
  \enqueue{name}{$+$ #3}%
}

\def\namelist#1{%
  \setbox0=\vtop{\foreach{name}{\hbox{\strut\head}}}%
  \dp0=0pt \box0
}

\def\pagebreak{%
  \omit
  &\omit\hfil\nameuse{dots/\nameuse{trk/t0}}\hfil%
  &\omit\hfil\nameuse{dots/\nameuse{trk/t1}}\hfil%
  &\omit\hfil\nameuse{dots/\nameuse{trk/t2}}\hfil%
  &\omit\hfil\nameuse{dots/\nameuse{trk/t3}}\hfil%
  \cr
  \noalign{\newpage}%
  \omit
  &\omit\hfil\nameuse{top/\nameuse{trk/t0}}{t0}\hfil%
  &\omit\hfil\nameuse{top/\nameuse{trk/t1}}{t1}\hfil%
  &\omit\hfil\nameuse{top/\nameuse{trk/t2}}{t2}\hfil%
  &\omit\hfil\nameuse{top/\nameuse{trk/t3}}{t3}\hfil%
  &\cr
  \omit
  &\omit\hfil\nameuse{dots/\nameuse{trk/t0}}\hfil%
  &\omit\hfil\nameuse{dots/\nameuse{trk/t1}}\hfil%
  &\omit\hfil\nameuse{dots/\nameuse{trk/t2}}\hfil%
  &\omit\hfil\nameuse{dots/\nameuse{trk/t3}}\hfil%
  \global\toptrue
  \cr
}

\edef\oldoutput{\the\output}
%\output={\showbox255 \oldoutput}

\begin{document}

\topskip = 0pt
\baselineskip = 0pt
\lineskip = 0pt

\global\def\\{&&&&&\cr}
\halign%
  {\strut\hbox to \titlewidth{\qquad\ignorespaces#\unskip\hfil}%
  &\track{t0}{#}%
  &\track{t1}{#}%
  &\track{t2}{#}%
  &\track{t3}{#}%
  &\quad\namelist{#}%
  \global\topfalse\cr%
  %
&\omit\qquad&\omit\qquad&\omit\qquad&\omit\qquad\cr\cr
  %
\doctor{The First Doctor (1963--1966)}
  %
\season{Season 1}
  %
  An Unearthly Child            \join{t0}{S}{Susan Foreman}
                                \join{t1}{B}{Barbara Wright}
                                \join{t2}{I}{Ian Chesterton}            \\
  The Daleks                                                            \\
  The Edge of Destruction                                               \\
  \lost{Marco Polo}                                                     \\
  The Keys of Marinus                                                   \\
  The Aztecs                                                            \\
  The Sensorites                                                        \\
  The Reign of Terror                                                   \\
  %
\season{Season 2}
  Planet of Giants                                                      \\
  The Dalek Invasion of Earth   \depart{t0}                             \\
  The Rescue                    \join{t3}{V}{Vicki Pallister}           \\
  The Romans                                                            \\
  The Web Planet                                                        \\
  \lost{The Crusade}                                                    \\
  The Space Museum                                                      \\
  The Chase                     \depart{t1} \depart{t2}
                                \join{t0}{S}{Stephen Taylor}            \\
  The Time Meddler                                                      \\
  %
\season{Season 3}
  \lost{Galaxy Four}                                                    \\
  \lost{Mission to the Unknown}                                         \\
  \lost{The Myth Makers}        \depart{t3}
                                \join{t1}{K}{Katarina}                  \\
  \lost{The Daleks' Master Plan}
                                \die{t1}
                                \joindie{t2}{S}{Sara Kingdom}           \\
  \lost{The Massacre of St Bartholomew's Eve}
                                \join{t3}{D}{Dodo Chaplet}              \\
  The Ark                                                               \\
  \lost{The Celestial Toymaker}                                         \\
  The Gunfighters                                                       \\
  \lost{The Savages}            \depart{t0}                             \\
  The War Machines              \depart{t3}
                                \join{t1}{P}{Polly Wright}
                                \join{t2}{B}{Ben Jackson}               \\
  %
\season{Season 4}
  \lost{The Smugglers}                                                  \\
  The Tenth Planet                                                      \\
  %
\pagebreak
\doctor{The Second Doctor (1966--1969)}
  %
\season{Season 4 (cont.)}
  The Power of the Daleks                                               \\
  \lost{The Highlanders}        \join{t0}{J}{Jamie McCrimmon}           \\
  The Underwater Menace                                                 \\
  The Moonbase                                                          \\
  \lost{The Macra Terror}                                               \\
  \lost{The Faceless Ones}      \depart{t1} \depart{t2}                 \\
  \lost{The Evil of the Daleks} \join{t3}{V}{Victoria Waterfield}       \\
  %
\season{Season 5}
  The Tomb of the Cybermen                                              \\
  \lost{The Abominable Snowmen}                                         \\
  The Ice Warriors                                                      \\
  The Enemy of the World                                                \\
  The Web of Fear                                                       \\
  \lost{Fury from the Deep}     \depart{t3}                             \\
  \lost{The Wheel in Space}     \join{t1}{Z}{Zoe Heriot}                \\
  %
\season{Season 6}
  The Dominators                                                        \\
  The Mind Robber                                                       \\
  The Invasion                                                          \\
  The Krotons                                                           \\
  The Seeds of Death                                                    \\
  \lost{The Space Pirates}                                              \\
  The War Games                 \depart{t0} \depart{t1}                 \\
  %
\pagebreak
\doctor{The Third Doctor (1970--1974)}
  %
\season{Season 7}
  Spearhead from Space          \join{t0}{L}{Elizabeth Shaw}            \\
  Doctor Who and the Siluarians                                         \\
  The Ambassadors of Death                                              \\
  Inferno                       \depart{t0}                             \\
  %
\season{Season 8}
  Terror of the Autons          \join{t0}{J}{Jo Grant}                  \\
  The Mind of Evil                                                      \\
  The Claws of Axos                                                     \\
  Colony in Space                                                       \\
  The Daemons                                                           \\
  %
\season{Season 9}
  Day of the Daleks                                                     \\
  The Curse of Peladon                                                  \\
  The Sea Devils                                                        \\
  The Mutants                                                           \\
  The Time Monster                                                      \\
  %
\season{Season 10}
  The Three Doctors                                                     \\
  Carnival of Monsters                                                  \\
  Frontier in Space                                                     \\
  Planet of the Daleks                                                  \\
  The Green Death               \depart{t0}                             \\
  %
\season{Season 11}
  The Time Warrior              \join{t0}{S}{Sarah Jane Smith}          \\
  Invasion of the Dinosaurs                                             \\
  Death to the Daleks                                                   \\
  The Monster of Peladon                                                \\
  Planet of the Spiders                                                 \\
  %
\pagebreak
\doctor{The Fourth Doctor (1974--1981)}
  %
\season{Season 12}
  Robot                                 \join{t1}{H}{Harry Sullivan}    \\
  The Ark in Space                                                      \\
  The Sontaran Experiment                                               \\
  Genesis of the Daleks                                                 \\
  Revenge of the Cybermen                                               \\
  %
\season{Season 13}
  Terror of the Zygons                  \depart{t1}                     \\
  Planet of Evil                                                        \\
  Pyramids of Mars                                                      \\
  The Android Invasion                                                  \\
  The Brain of Morbius                                                  \\
  The Seeds of Doom                                                     \\
  %
\season{Season 14}
  The Masque of Mandragora                                              \\
  The Hand of Fear                      \depart{t0}                     \\
  The Deadly Assassin                                                   \\
  The Face of Evil                      \join{t0}{L}{Leela}             \\
  The Robots of Death                                                   \\
  The Talons of Weng-Chiang                                             \\
  %
\season{Season 15}
  Horror of Fang Rock                                                   \\
  The Invisible Enemy                   \join{t1}{K}{K--9}              \\
  Image of the Fendahl                                                  \\
  The Sun Makers                                                        \\
  Underworld                                                            \\
  The Invasion of Time                  \depart{t0}                     \\
  %
\season{Season 16 (The Key to Time)}
  The Ribos Operation                   \join{t0}{R}{Romanadvoratrelundar
                                                        (`Romana')} \\
  The Pirate Planet                                                     \\
  The Stones of Blood                                                   \\
  The Androids of Tara                                                  \\
  The Power of Kroll                                                    \\
  The Armageddon Factor                                                 \\
  %
\season{Season 17}
  Destiny of the Daleks                                                 \\
  City of Death                                                         \\
  The Creature from the Pit                                             \\
  Nightmare of Eden                                                     \\
  The Horns of Nimon                                                    \\
  \lost{Shada}                                                          \\
  %
\season{Season 18}
  The Leisure Hive                                                      \\
  Meglos                                                                \\
  Full Circle                           \join{t2}{A}{Adric}             \\
  State of Decay                                                        \\
  Warriors' Gate                        \depart{t0} \depart{t1}         \\
  The Keeper of Traken                  \join{t3}{N}{Nyssa of Traken}   \\
  Logopolis                             \join{t0}{T}{Tegan Jovanka}     \\
  %
\pagebreak
\doctor{The Fifth Doctor (1981--1984)}
  %
\season{Season 19}
  Castrovalva                                                           \\
  Four to Doomsday                                                      \\
  Kinda                                                                 \\
  The Visitation                                                        \\
  Black Orchid                                                          \\
  Earthshock                            \die{t2}                        \\
  Time-Flight                                                           \\
  %
\season{Season 20}
  Arc of Infinity                                                       \\
  Snakedance                                                            \\
  Mawdryn Undead                        \join{t1}{T}{Vislor Turlough}   \\
  Terminus                              \depart{t3}                     \\
  Enlightenment                                                         \\
  The King’s Demons                     \join{t2}{K}{Kameleon}          \\
  %
\season{(Special)}
  The Five Doctors                                                      \\
  %
\season{Season 21}
  Warriors of the Deep                                                  \\
  The Awakening                                                         \\
  Frontios                                                              \\
  Resurrection of the Daleks            \depart{t0}                     \\
  Planet of Fire                        \depart{t1}
                                        \die{t2}
                                        \join{t3}{P}{Perpugilliam
                                                        `Peri' Brown}   \\
  The Caves of Androzani                                                \\
  %
\pagebreak
\doctor{The Sixth Doctor (1984--1986)}
  %
\season{Season 21 (cont.)}
  The Twin Dilemma                                                      \\
  %
\season{Season 22}
  Attack of the Cybermen                                                \\
  Vengeance on Varos                                                    \\
  The Mark of the Rani                                                  \\
  The Two Doctors                                                       \\
  Timelash                                                              \\
  Revelation of the Daleks                                              \\
  %
\season{Season 23 (The Trial of a Time Lord)}
  The Mysterious Planet                                                 \\
  Mindwarp                              \die{t3}                        \\
  Terror of the Vervoids                \join{t0}{M}{Melanie Bush}      \\
  The Ultimate Foe                                                      \\
  %
\doctor{The Seventh Doctor (1987--1989)}
  %
\season{Season 24}
  Time and the Rani                                                     \\
  Paradise Towers                                                       \\
  Delta and the Bannerman                                               \\
  Dragonfire                            \depart{t0}
                                        \join{t1}{A}{Dorothy `Ace'
                                                        McShane Gale}   \\
  %
\season{Season 25}
  Remembrance of the Daleks                                             \\
  The Happiness Patrol                                                  \\
  Silver Nemesis                                                        \\
  The Greatest Show in the Galaxy                                       \\
  %
\season{Season 26}
  Battlefield                                                           \\
  Ghost Light                                                           \\
  The Curse of Fenric                                                   \\
  Survival                              \depart{t1}                     \\
  %
}

\end{document}
