\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{buildinggraph}[2016/09/13 Random building graph visualization]

\RequirePackage{tikz}
\RequirePackage{xcolor}

\usetikzlibrary{shapes,calc,decorations,decorations.pathmorphing}

\newcommand{\openwall}[4]{%
  \pgfpathlineto{\pgfpointlineattime{.33}{\pgfpoint{#1}{#2}}{\pgfpoint{#3}{#4}}}
  \pgfpathmoveto{\pgfpointlineattime{.67}{\pgfpoint{#1}{#2}}{\pgfpoint{#3}{#4}}}
  \pgfpathlineto{\pgfpoint{#3}{#4}}
}

\newcommand{\closedwall}[4]{%
  \pgfpathlineto{\pgfpoint{#3}{#4}}
}

\newcommand{\opencwall}[4]{%
  \pgfpathlineto{\pgfpoint{#1}{#4}}
  \pgfpathmoveto{\pgfpoint{#3}{#4}}
  \pgfpathlineto{\pgfpoint{#3}{#2}}
}

\newcommand{\opencwallf}[4]{%
  \pgfpathlineto{\pgfpoint{#1}{#4}}
  \pgfpathlineto{\pgfpoint{#3}{#4}}
  \pgfpathlineto{\pgfpoint{#3}{#2}}
}


\newcommand{\closedcwall}[4]{%
  \pgfpathlineto{\pgfpoint{#3}{#2}}
}

\newcommand{\opencwallv}[4]{%
  \pgfpathlineto{\pgfpoint{#3}{#2}}
  \pgfpathmoveto{\pgfpoint{#3}{#4}}
  \pgfpathlineto{\pgfpoint{#1}{#4}}
}

\newcommand{\opencwallvf}[4]{%
  \pgfpathlineto{\pgfpoint{#3}{#2}}
  \pgfpathlineto{\pgfpoint{#3}{#4}}
  \pgfpathlineto{\pgfpoint{#1}{#4}}
}


\newcommand{\closedcwallv}[4]{%
  \pgfpathlineto{\pgfpoint{#1}{#4}}
}




\newif\ifnopen
\pgfkeys{/tikz/nopen/.is if=nopen}
\newif\ifsopen
\pgfkeys{/tikz/sopen/.is if=sopen}
\newif\ifeopen
\pgfkeys{/tikz/eopen/.is if=eopen}
\newif\ifwopen
\pgfkeys{/tikz/wopen/.is if=wopen}

    \newlength{\south}
    \newlength{\west}
    \newlength{\north}
    \newlength{\east}

    \newlength{\xda}
    \newlength{\xdb}
    \newlength{\xdc}
    \newlength{\xdd}

    \newlength{\yda}
    \newlength{\ydb}
    \newlength{\ydc}
    \newlength{\ydd}





\pgfdeclareshape{room}{%
  \inheritsavedanchors[from=rectangle] % this is nearly a rectangle
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}

  \backgroundpath{%

    \pgfextracty{\south}{\southwest}
    \pgfextractx{\west}{\southwest}
    \pgfextracty{\north}{\northeast}
    \pgfextractx{\east}{\northeast}



    \begingroup
    \tikz@mode
    \iftikz@mode@fill

    \pgfpathmoveto{\pgfpoint{\west}{\north}}
    \pgfpathlineto{\pgfpoint{\west}{\south}}
    \pgfpathlineto{\pgfpoint{\east}{\south}}
    \pgfpathlineto{\pgfpoint{\east}{\north}}
    \pgfpathclose{}
    \pgfusepath{fill}
    \fi

    \iftikz@mode@draw
    \pgfpathmoveto{\pgfpoint{\west}{\north}}

    \ifnopen
    \openwall{\west}{\north}{\east}{\north}
    \else
    \closedwall{\west}{\north}{\east}{\north}
    \fi
 
    \ifeopen
    \openwall{\east}{\north}{\east}{\south}
    \else
    \closedwall{\east}{\north}{\east}{\south}
    \fi

    \ifsopen
    \openwall{\east}{\south}{\west}{\south}
    \else
    \closedwall{\east}{\south}{\west}{\south}
    \fi
    
    \ifwopen
    \openwall{\west}{\south}{\west}{\north}
    \else
    \closedwall{\west}{\south}{\west}{\north}
    \fi

    \ifnopen
    \openwall{\west}{\north}{\east}{\north}
    \else
    \closedwall{\west}{\north}{\east}{\north}
    \fi
    \pgfusepath{stroke}
    \fi
    \endgroup
  }
}

\pgfdeclareshape{elevator}{%
  \inheritsavedanchors[from=rectangle] % this is nearly a rectangle
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}

  \backgroundpath{%

    \pgfextracty{\south}{\southwest}
    \pgfextractx{\west}{\southwest}
    \pgfextracty{\north}{\northeast}
    \pgfextractx{\east}{\northeast}



    \begingroup
    \tikz@mode
    \iftikz@mode@fill

    \pgfpathmoveto{\pgfpointlineattime{0.66}{\pgfpoint{\west}{\south}}{\pgfpoint{\west}{\north}}}

    \pgfpathlineto{\pgfpointlineattime{0.33}{\pgfpoint{\west}{\north}}{\pgfpoint{\east}{\north}}}
    \pgfpathlineto{\pgfpointlineattime{0.66}{\pgfpoint{\west}{\north}}{\pgfpoint{\east}{\north}}}

    \pgfpathlineto{\pgfpointlineattime{0.33}{\pgfpoint{\east}{\north}}{\pgfpoint{\east}{\south}}}
    \pgfpathlineto{\pgfpointlineattime{0.66}{\pgfpoint{\east}{\north}}{\pgfpoint{\east}{\south}}}

    \pgfpathlineto{\pgfpointlineattime{0.33}{\pgfpoint{\east}{\south}}{\pgfpoint{\west}{\south}}}
    \pgfpathlineto{\pgfpointlineattime{0.66}{\pgfpoint{\east}{\south}}{\pgfpoint{\west}{\south}}}

    \pgfpathlineto{\pgfpointlineattime{0.33}{\pgfpoint{\west}{\south}}{\pgfpoint{\west}{\north}}}
    \pgfpathlineto{\pgfpointlineattime{0.66}{\pgfpoint{\west}{\south}}{\pgfpoint{\west}{\north}}}

    \pgfpathclose{}
    \pgfusepath{fill}
    \fi

    \iftikz@mode@draw
    \pgfpathmoveto{\pgfpoint{\west}{\north}}

    \pgfpathmoveto{\pgfpointlineattime{0.66}{\pgfpoint{\west}{\south}}{\pgfpoint{\west}{\north}}}

    \pgfpathlineto{\pgfpointlineattime{0.33}{\pgfpoint{\west}{\north}}{\pgfpoint{\east}{\north}}}
    \ifnopen
    \pgfpathmoveto{\pgfpointlineattime{0.66}{\pgfpoint{\west}{\north}}{\pgfpoint{\east}{\north}}}
    \else
    \pgfpathlineto{\pgfpointlineattime{0.66}{\pgfpoint{\west}{\north}}{\pgfpoint{\east}{\north}}}
    \fi

    \pgfpathlineto{\pgfpointlineattime{0.33}{\pgfpoint{\east}{\north}}{\pgfpoint{\east}{\south}}}
    \ifeopen
    \pgfpathmoveto{\pgfpointlineattime{0.66}{\pgfpoint{\east}{\north}}{\pgfpoint{\east}{\south}}}
    \else
    \pgfpathlineto{\pgfpointlineattime{0.66}{\pgfpoint{\east}{\north}}{\pgfpoint{\east}{\south}}}
    \fi

    \pgfpathlineto{\pgfpointlineattime{0.33}{\pgfpoint{\east}{\south}}{\pgfpoint{\west}{\south}}}
    \ifsopen
    \pgfpathmoveto{\pgfpointlineattime{0.66}{\pgfpoint{\east}{\south}}{\pgfpoint{\west}{\south}}}
    \else
    \pgfpathlineto{\pgfpointlineattime{0.66}{\pgfpoint{\east}{\south}}{\pgfpoint{\west}{\south}}}
    \fi

    \pgfpathlineto{\pgfpointlineattime{0.33}{\pgfpoint{\west}{\south}}{\pgfpoint{\west}{\north}}}
    \ifwopen
    \pgfpathmoveto{\pgfpointlineattime{0.66}{\pgfpoint{\west}{\south}}{\pgfpoint{\west}{\north}}}
    \else
    \pgfpathlineto{\pgfpointlineattime{0.66}{\pgfpoint{\west}{\south}}{\pgfpoint{\west}{\north}}}
    \fi
    \pgfusepath{stroke}
    \fi
    \endgroup
  }
}


\pgfdeclareshape{corrsegment}{%
  \inheritsavedanchors[from=rectangle] % this is nearly a rectangle
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}

  \backgroundpath{%

    \pgfextracty{\south}{\southwest}
    \pgfextractx{\west}{\southwest}
    \pgfextracty{\north}{\northeast}
    \pgfextractx{\east}{\northeast}



    \begingroup


    \pgfextractx{\xda}{\pgfpointlineattime{0}{\pgfpoint{\west}{\south}}{\pgfpoint{\east}{\south}}}
    \pgfextractx{\xdb}{\pgfpointlineattime{.33}{\pgfpoint{\west}{\south}}{\pgfpoint{\east}{\south}}}
    \pgfextractx{\xdc}{\pgfpointlineattime{.66}{\pgfpoint{\west}{\south}}{\pgfpoint{\east}{\south}}}
    \pgfextractx{\xdd}{\pgfpointlineattime{1}{\pgfpoint{\west}{\south}}{\pgfpoint{\east}{\south}}}

    \pgfextracty{\yda}{\pgfpointlineattime{0}{\pgfpoint{\west}{\south}}{\pgfpoint{\west}{\north}}}
    \pgfextracty{\ydb}{\pgfpointlineattime{.33}{\pgfpoint{\west}{\south}}{\pgfpoint{\west}{\north}}}
    \pgfextracty{\ydc}{\pgfpointlineattime{.66}{\pgfpoint{\west}{\south}}{\pgfpoint{\west}{\north}}}
    \pgfextracty{\ydd}{\pgfpointlineattime{1}{\pgfpoint{\west}{\south}}{\pgfpoint{\west}{\north}}}

    \tikz@mode
    \iftikz@mode@fill

    \pgfpathmoveto{\pgfpoint{\xdb}{\ydc}}
    \ifnopen
    \opencwallf{\xdb}{\ydc}{\xdc}{\ydd}
    \else
    \closedcwall{\xdb}{\ydc}{\xdc}{\ydd}
    \fi
 
    \ifeopen
    \opencwallvf{\xdc}{\ydc}{\xdd}{\ydb}
    \else
    \closedcwallv{\xdc}{\ydc}{\xdd}{\ydb}
    \fi

    \ifsopen
    \opencwallf{\xdc}{\ydb}{\xdb}{\yda}
    \else
    \closedcwall{\xdc}{\ydb}{\xdb}{\yda}
    \fi
    
    \ifwopen
    \opencwallvf{\xdb}{\ydb}{\xda}{\ydc}
    \else
    \closedcwallv{\xdb}{\ydb}{\xda}{\ydc}
    \fi
    \pgfpathclose{}

    \pgfusepath{fill}
    \fi

    \iftikz@mode@draw
    \pgfpathmoveto{\pgfpoint{\xdb}{\ydc}}

    \ifnopen
    \opencwall{\xdb}{\ydc}{\xdc}{\ydd}
    \else
    \closedcwall{\xdb}{\ydc}{\xdc}{\ydd}
    \fi
 
    \ifeopen
    \opencwallv{\xdc}{\ydc}{\xdd}{\ydb}
    \else
    \closedcwallv{\xdc}{\ydc}{\xdd}{\ydb}
    \fi

    \ifsopen
    \opencwall{\xdc}{\ydb}{\xdb}{\yda}
    \else
    \closedcwall{\xdc}{\ydb}{\xdb}{\yda}
    \fi
    
    \ifwopen
    \opencwallv{\xdb}{\ydb}{\xda}{\ydc}
    \else
    \closedcwallv{\xdb}{\ydb}{\xda}{\ydc}
    \fi

    \ifnopen
    \opencwall{\xdb}{\ydc}{\xdc}{\ydd}
    \else
    \closedcwall{\xdb}{\ydc}{\xdc}{\ydd}
    \fi

    \pgfusepath{stroke}
    \fi
    \endgroup
  }
}


\newcommand{\connectnodesh}[2]{%
%\draw [corridor wall] ($ (n#1.east) +(0,.58em) $) -- 
%($ (n#2.west)+(0,.58em) $);
%\draw [corridor wall] ($ (n#1.east) +(0,-.58em) $) -- 
%($ (n#2.west)+(0,-.58em) $);
}


\newcommand{\connectnodesv}[2]{%
%\draw [corridor wall] ($ (n#1.south) +(.58em,0) $) -- 
%($ (n#2.north)+(.58em,0) $);
%\draw [corridor wall] ($ (n#1.south) +(-.6em,0) $) -- 
%($ (n#2.north)+(-.6em,0) $);
}


\tikzset{building/.style={%
every node/.style={line width=.2em,
minimum height=4em,
minimum width=4em,
inner sep=.05em, 
outer sep=0.01em},
Corridor/.style={draw=black, shape=corrsegment},
Office/.style={draw=gray, shape=room},
Entrance/.style={draw=red, fill=red!30, shape=corrsegment},
Elevator/.style={draw=blue,shape=elevator},
corridor wall/.style={line width=.2em},
target/.style={fill=green!30, align=center,shape=room, draw=gray},
spawn/.style={fill=red!30,shape=corrsegment},
start/.style={shape=room},
dstart/.style={shape=circle,draw=blue,minimum width=2em, minimum
  height=2em}
}
}

\newcommand{\buildinggraph}[1]{\begin{tikzpicture}[building]
\input{#1}
\end{tikzpicture}}
