/* adv02.c: Cloak of Darkness - copyright Mike Arnautov 2016. licensed under
 * GPL (version 3 or later) or the Modified BSD Licence, whichever
 * is asserted by the supplied LICENCE file.*/

#include "adv0.h"
#include "adv3.h"
int
p5 (void)
{
  int lval[1];
  short lbts[1];
  lval[0] = 0;
  lbts[0] = 0;
  value[0] = (bitest (evar (29), 3));
  if (value[0])
  {
    say (2, 29, 0);
    lbts[0 * VARSIZE] = -1;
    lval[0] = FOBJ - 1;
    while (++lval[0] <= LOBJ)
    {
      value[0] = (ishere (lval[0], -1, -1));
      if (value[0])
      {
        say (0, lval[0], 0);
      }
    }
  }
  else
  {
    say (0, 41, 0);
  }
  return 0;
}

int
p3 (void)
{
  move (6, 0, 11, 19, -6);
  if (loop)
    return (0);
  value[0] = (value[5] > 0);
  if (value[0])
  {
    value[5] -= 1;
  }
  value[0] = (bitest (evar (29), 3));
  if (value[0])
  {
    value[0] = (KEY (20));
    if (value[0])
    {
      if (anyof (26, -1))
        say (72, 52, 1);
      if (loop)
        return (0);
      value[0] = (KEY (5));
      if (value[0])
      {
        (*procs[21]) ();
        if (loop)
          return (0);
      }
    }
    return 0;
  }
  value[0] = (KEY (25));
  if (value[0])
  {
    (*procs[25]) ();
    if (loop)
      return (0);
  }
  value[0] = (value[5] > 0);
  if (value[0])
  {
    value[5] -= 1;
  }
  say (64, 40, 0);
  if (loop)
    return (0);
  return 0;
}

int
p4 (void)
{
  move (6, 0, 13, 19, -6);
  if (loop)
    return (0);
  value[0] = (keyword (24, 4, -1));
  if (value[0])
  {
    value[0] = (value[4] == 1);
    if (value[0])
    {
      say (64, 39, 0);
      if (loop)
        return (0);
    }
    set ('E', 4, 'c', 1, NULL, NULL);
    set ('E', 3, 'c', 1, NULL, NULL);
    apport (4, HERE);
    bitmod ('s', 8, 3);
    say (64, 36, 0);
    if (loop)
      return (0);
  }
  return 0;
}

int
p8 (void)
{
  move (7, 0, -17);
  if (loop)
    return (0);
  move (8, 0, -15);
  if (loop)
    return (0);
  if (anyof (11, 19, -1))
    say (64, 35, 0);
  if (loop)
    return (0);
  return 0;
}

int
p9 (void)
{
  if (!KEY (4))
    return 0;
  value[0] = (isnear (4, -1, -1));
  if (value[0])
  {
    say (64, 38, 0);
    if (loop)
      return (0);
  }
  return 0;
}

int
p6 (void)
{
  say (64, 43, 0);
  if (loop)
    return (0);
  return 0;
}

int
p10 (void)
{
  int lval[2];
  short lbts[2];
  lval[0] = 0;
  lbts[0] = 0;
  lval[1] = 0;
  lbts[1] = 0;
  set ('L', 0, 'c', 0, lval, lbts);
  lbts[1 * VARSIZE] = -1;
  lval[1] = FOBJ - 1;
  while (++lval[1] <= LOBJ)
  {
    value[0] = (have (lval[1], -1, -1));
    if (value[0])
    {
      value[0] = (lval[0] == 0);
      if (value[0])
      {
        say (0, 45, 0);
      }
      say (0, lval[1], 0);
      lval[0] += 1;
    }
  }
  value[0] = (lval[0] == 0);
  if (value[0])
  {
    say (0, 44, 0);
  }
  loop = 1;
  return (0);
  return 0;
}

int
p11 (void)
{
  value[0] = (bitest (evar (29), 3));
  if (!value[0])
  {
    say (64, 41, 0);
    if (loop)
      return (0);
  }
  value[0] = (value[27] == 1);
  if (value[0])
  {
    p5 ();
    if (loop)
      return (0);
    loop = 1;
    return (0);
  }
  if (anyof (26, -1))
    say (72, 52, 0);
  if (loop)
    return (0);
  value[0] = (isnear (value[32], -1, -1));
  if (value[0])
  {
    say (34, 32, 0);
  }
  else
  {
    say (12, 48, 32);
  }
  loop = 1;
  return (0);
  return 0;
}

int
p13 (void)
{
  value[0] = (bitest (evar (29), 3));
  if (!value[0])
  {
    say (64, 41, 0);
    if (loop)
      return (0);
  }
  value[0] = (KEY (5));
  value[0] |= (value[27] == 1);
  value[0] &= (isnear (5, -1, -1));
  if (value[0])
  {
    say (12, 37, 5);
    finita ();
  }
  value[0] = (value[27] == 1);
  if (value[0])
  {
    say (64, 42, 0);
    if (loop)
      return (0);
  }
  return 0;
}

int
p7 (void)
{
  value[0] = (value[27] == 1);
  if (value[0])
  {
    say (76, 50, 31);
    if (loop)
      return (0);
  }
  return 0;
}

int
p12 (void)
{
  say (0, 49, 0);
  finita ();
  return 0;
}

int
p1 (void)
{
  bitmod ('s', 6, 3);
  bitmod ('s', 7, 3);
  apport (5, 8);
  apport (3, 7);
  apport (4, INHAND);
  set ('E', 5, 'c', 4, NULL, NULL);
  set ('V', 28, 'c', 0, NULL, NULL);
  move (6, -1);
  say (0, 34, 0);
  return 0;
}

int
p2 (void)
{
  value[0] = (bitest (evar (27), 0));
  if (value[0])
  {
    bitmod ('c', evar (27), 0);
    p5 ();
    if (loop)
      return (0);
  }
  input (0);
  if (loop)
    return (0);
  value[0] = (value[31] == -2);
  value[0] |= (value[31] == -3);
  if (value[0])
  {
    say (76, 46, 31);
    if (loop)
      return (0);
  }
  value[0] = (value[32] == -2);
  value[0] |= (value[32] == -3);
  if (value[0])
  {
    say (76, 46, 32);
    if (loop)
      return (0);
  }
  value[0] = (anyof (12, 14, 16, 18, -1));
  if (value[0])
  {
    value[0] = (value[27] == 1);
    if (value[0])
    {
      say (76, 51, 31);
      if (loop)
        return (0);
    }
    else
    {
      say (76, 51, 32);
      if (loop)
        return (0);
    }
  }
  if (value[29] < LPROC && value[29] >= 0)
    (*procs[value[29]]) ();
  else
    pcall (value[29]);
  if (loop)
    return (0);
  if (value[31] < LPROC && value[31] >= 0)
    (*procs[value[31]]) ();
  else
    pcall (value[31]);
  if (loop)
    return (0);
  value[0] = (value[27] > 1);
  value[0] &= !(isnear (value[32], -1, -1));
  if (value[0])
  {
    say (76, 48, 32);
    if (loop)
      return (0);
  }
  value[0] = (value[27] == 1);
  value[0] &= (11 <= value[31] && value[31] <= 18);
  if (value[0])
  {
    say (76, 51, 31);
    if (loop)
      return (0);
  }
  say (64, 47, 0);
  if (loop)
    return (0);
  return 0;
}
