Use min/max from stdlib

Until now, tools.h had it's own definition of the min and
max functions. We replace those with the ones from the
stdlib.

In order for this to work we remove the functions from
tools.h and replace all the invocations with ones that use
the std namespace `std::max(a,b)`.

This was done mostly via automatic replacement with few
minor manual adjustments. It would not be feasable to do
that manually, but the semi automatic approach might have
created some artifacts.

== minmax.sh ===============================================

 # ag is a more clever grep
ag 'max|min' -l | xargs sed -i '
  s@[-]max[(]@-std::max(@g;
  s@[+]max[(]@+std::max(@g;
  s@[*]max[(]@*std::max(@g;
  s@[/]max[(]@/std::max(@g;
  s@[=]max[(]@=std::max(@g;
  s@[(]max[(]@(std::max(@g;
  s@[)]max[(]@)std::max(@g;
  s@[[]max[(]@[std::max(@g;
  s@ max[(]@ std::max(@g;

  s@[-]::max[(]@-std::max(@g;
  s@[+]::max[(]@+std::max(@g;
  s@[*]::max[(]@*std::max(@g;
  s@[/]::max[(]@/std::max(@g;
  s@[=]::max[(]@=std::max(@g;
  s@[(]::max[(]@(std::max(@g;
  s@[)]::max[(]@)std::max(@g;
  s@ ::max[(]@ std::max(@g;

  s@[-]min[(]@-std::min(@g;
  s@[+]min[(]@+std::min(@g;
  s@[*]min[(]@*std::min(@g;
  s@[/]min[(]@/std::min(@g;
  s@[=]min[(]@=std::min(@g;
  s@[(]min[(]@(std::min(@g;
  s@[)]min[(]@)std::min(@g;
  s@[[]min[(]@[std::min(@g;
  s@ min[(]@ std::min(@g;

  s@[-]::min[(]@-std::min(@g;
  s@[+]::min[(]@+std::min(@g;
  s@[*]::min[(]@*std::min(@g;
  s@[/]::min[(]@/std::min(@g;
  s@[=]::min[(]@=std::min(@g;
  s@[(]::min[(]@(std::min(@g;
  s@[)]::min[(]@)std::min(@g;
  s@ ::min[(]@ std::min(@g;
'
Remove loop macros

Until now tools.h contained a few macros for doing for
loops:

  #define loop(v,m) for(int v = 0; v<int(m); v++)
  #define loopi(m) loop(i,m)
  #define loopj(m) loop(j,m)
  #define loopk(m) loop(k,m)
  #define loopl(m) loop(l,m)
  #define loopirev(v) for(int i = v-1; i>=0; i--)

This is bad because it is very peculiar syntax and it does
not fit half of the cases it is used for well:
* In many cases size_t should be used rather than int
* It hides a lot of complexity
* It is in few cases appropriate to cast to int (implicitly)
* It is non standard and rather confusing; it is also not
  a good to replace a for loop, because, while using it you
  still have to be aware that you are creating a for loop in
  a few instances
* In multiple instances stuff like this was done, hiding
  multiple levels of indentation

  loopi(20) loopk(30) loopj(k) if (k>i)
  {
    ...
  }

Generally I would argue that it is bad practice to use more
that 100 characters wide lines or four levels of
indentation; but this is not improved by hiding your
indentation or long lines, because this does not remove
complexity.
LZMA is better at compression than you are.
If your code gets more complex, to deeply nested or your
functions to deep, improve the structure of your code!

Because of those reasons, these macros have been removed and
expanded:
First the macros them self have been removed and then every
call to those has been expanded. This was done using
a script. Manual intervention was necessary in sound.cpp in
line 329 and 340, because the dots had been taken out of
their char quotes.

By using a script to do the refactoring some artefacts are
probably left, but most code should functionally be the
same.
A bit of testing has been done: It compiles and you can play
a map.

This does not actually fix the underlying problem: The code
is still to complex and ugly, it is still not appropriate to
cast to an int, it is still not appropriate to always use
int, but this commit represents the groundwork to not adding
to the pile of shit and for doing the actual refactoring.

== sani_loop.sh ============================================

 #! /bin/bash

dir="`dirname "$0"`"
tmp="/tmp/sani_loop`uuidgen`"

ag 'loop' -l | while read f; do
  dst="$tmp/$f"
  mkdir -pv "`dirname "$dst"`"
  echo "| $f -> $dst" >&2

  < "$f" awk -f"$dir"/sani_loop.awk -v FILENAME="$f" -v header="$dir"/sani_loop.h > "$dst"
done

== sani_loop.awk ===========================================

function er(txt) {
  print(txt) > "/dev/stderr"
}

{
  if (match($0,/(loop|loop[ijklv]|loop[vi]rev|loopv(j|k|ref))\s*\(/)) {
    back=sub(/\\$/, "");
    front=sub(/^\s*#/, "");

    cmd = "echo '#include \"" header "\"\n" $0 "' | cpp -P -C | tail -n 1";
    R = cmd | getline out;
    close(cmd);

    if (back)
      out = out "\\";

    if (front)
      out = "#" out;

    #er("CMD " FILENAME ":" NR "  " cmd);
    #er("IN  " FILENAME ":" NR "  " $0);
    #er("| " FILENAME ":" NR "  " out);

    print out;
  } else {
    print;
  }
}

== sani_loop.h =============================================

 #define loop(v,m) for(int v = 0; v < int(m); v++)
 #define loopi(m) loop(i,m)
 #define loopj(m) loop(j,m)
 #define loopk(m) loop(k,m)
 #define loopl(m) loop(l,m)
 #define loopirev(v) for(int i = (v)-1; i >= 0; i--)