
(********************************************************************)
(*                                                                  *)
(*  dnafight.sd7  Bacterial dna fight programming game              *)
(*  Copyright (C) 1985  Thomas Mertes                               *)
(*                                                                  *)
(*  This program is free software; 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 2 of  *)
(*  the License, or (at your option) any later version.             *)
(*                                                                  *)
(*  This program 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 program; if not, write to the           *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


const proc: dna (TAN) is func

  local
    const bactColor: SELF is TAN;
    var power: maxFood is 0;
    var power: maxEdFoe is 0;
    var power: localFood is 0;
    var power: localFoe is 0;
    var power: ownSize is 0;
    var directSet: maxFDir is directSet.EMPTY_SET;
    var directSet: maxEdFDir is directSet.EMPTY_SET;
    var direction: dir is HERE;

  begin (* dna (TAN) *)
    maxFood := food(HERE);
    maxFDir := {HERE};
    maxEdFoe := 0;
    maxEdFDir := directSet.EMPTY_SET;
    ownSize := strength(HERE);

    for dir range NORTH to EAST do
      localFood:= food(dir);
      localFoe:= strength(dir);
      case view(dir) of
        when {SELF}: noop;
        when {EDGE}: noop;
        when {CLEAR}:
          if localFood > maxFood then
            maxFood := localFood;
            maxFDir := {dir};
          elsif localFood = maxFood then
            incl(maxFDir, dir);
          end if;
        otherwise:
          if localFoe <= ownSize then
            localFood:= localFoe + localFood div 2;
            if localFood > maxEdFoe then
              maxEdFoe := localFood;
              maxEdFDir := {dir};
            elsif localFood = maxEdFoe then
              incl(maxEdFDir, dir);
            end if;
          end if;
      end case;
    end for;

    if maxEdFoe >= maxFood and maxEdFDir <> directSet.EMPTY_SET then
      kill(ranDir(maxEdFDir));
    else
      if maxFood <= ownSize and maxFDir <> {HERE} then
        split(ranDir(maxFDir - {HERE}), succ(ownSize) div 2, ownSize div 2);
      else
        eat(ranDir(maxFDir), ownSize);
      end if;
    end if;
  end func; (* dna (TAN) *)
