#include "ext.h"

static char *odd_names[] =
{
   "0/1", "1/2", "1/1", "2/1", "3/1", "4/1", "+",
};

static char crt[6][7] =
{
   OK, OK,        OK,        OK,        DISABLED,  DISABLED,  DESTROYED,
   OK, OK,        OK,        DISABLED,  DISABLED,  DESTROYED, DESTROYED,
   OK, OK,        DISABLED,  DISABLED,  DESTROYED, DESTROYED, DESTROYED,
   OK, OK,        DISABLED,  DESTROYED, DESTROYED, DESTROYED, DESTROYED,
   OK, DISABLED,  DESTROYED, DESTROYED, DESTROYED, DESTROYED, DESTROYED,
   OK, DESTROYED, DESTROYED, DESTROYED, DESTROYED, DESTROYED, DESTROYED,
};

odds(attack, defend)
int  attack, defend;
{
   int result;

   result = (defend > 0) ? attack / defend + 1 : 6;

   if (result > 6) result = 6;
   if (result == 1)
      result = (2 * attack < defend) ? 0 : 1;
   return(result);
}

char *odd_str(attack, defend)
int attack, defend;
{
   return(odd_names[odds(attack, defend)]);
}

ogre_resolve(allocations)
OGRE *allocations;
{
   static char blanks[12] = {"           "};

   eeol(16);
   Amiga_puts("Resolving...");
   cycle();

   if (allocations->missiles > 0)
   {
      if (crt[roll()][odds(allocations->missiles, DEF_MISSILES)] ==
         DESTROYED) ogre.missiles -= 1;
   }

   if (allocations->main_bats > 0)
   {
      if (crt[roll()][odds(allocations->main_bats, DEF_MAIN)] ==
         DESTROYED) ogre.main_bats -= 1;
   }

   if (allocations->sec_bats > 0)
   {
      if (crt[roll()][odds(allocations->sec_bats, DEF_SECONDARY)] ==
         DESTROYED) ogre.sec_bats -= 1;
   }

   if (allocations->ap > 0)
   {
      if (crt[roll()][odds(allocations->ap, DEF_AP)] ==
         DESTROYED) ogre.ap -= 1;
   }

   if (allocations->treads > 0)
   {
      if (crt[roll()][odds(1,1)] == DESTROYED)
         decrease_treads(allocations->treads);
   }
   movecur(18, 40);
   Amiga_puts(blanks);
   movecur(19, 40);
   Amiga_puts(blanks);
   movecur(19, 40);
   Amiga_puts(blanks);
   movecur(20, 40);
   Amiga_puts(blanks);
   movecur(21, 40);
   Amiga_puts(blanks);
   movecur(22, 40);
   Amiga_puts(blanks);

   disp_ogre_status(FALSE);
}

def_resolve(i)
int i;
{
   char result;

   if (unit[i].status != DESTROYED && unit[i].fired > 0)
   {
      result = crt[roll()][odds(unit[i].fired, unit[i].defend)];
      if (unit[i].type == INFANTRY)
         if (result == DISABLED)
         {
            unit[i].attack -= 1;
            if (unit[i].attack == 0) unit[i].status = DESTROYED;
            update_hex(unit[i].l_hex, unit[i].r_hex);
            return;
         }
         switch (unit[i].status)
         {
            case OK:
               unit[i].status = result;
               break;
            case DISABLED:
               if (result != OK) unit[i].status = DESTROYED;
               break;
         }
         if (unit[i].status != OK)
            update_hex(unit[i].l_hex, unit[i].r_hex);
    }
}

roll()
{
   long lrand48(), k = 0;

   while (k == 0)
      k = (lrand48()) % 6;
   return(k);
}

ogre_ram()
{
   int i;

   for (i = 0; i < n_units; ++i)
      if (unit[i].l_hex ==  ogre.l_hex &&
      unit[i].r_hex == ogre.r_hex &&
      unit[i].status != DESTROYED)

      switch (unit[i].type)
      {
         case  INFANTRY:
            if (ogre.ap > 0)
            {
               unit[i].attack -= 1;
               if (unit[i].attack == 0)
                  unit[i].status = DESTROYED;
            }
            break;
         default:
            if (unit[i].movement == 0 || unit[i].status == DISABLED)
            {
               unit[i].status = DESTROYED;
               decrease_treads((unit[i].type == HVYTANK) ? 2 : 1);
               disp_ogre_status(FALSE);
            }
            else
            {
               unit[i].status = (roll() > 3) ? DESTROYED : DISABLED;
               decrease_treads((unit[i].type == HVYTANK) ? 2 : 1);
               disp_ogre_status(FALSE);
            }
            break;
      }
}

def_ram(i)
int i;
{
   if (unit[i].l_hex == ogre.l_hex && unit[i].r_hex == ogre.r_hex &&
     unit[i].type != INFANTRY)
   {
      if (unit[i].type == HVYTANK)
         decrease_treads(2);
      else
         decrease_treads(1);
      unit[i].status = DESTROYED;
      disp_ogre_status(FALSE);
   }
}

decrease_treads(attack)
int attack;
{
   ogre.treads -= attack;
   ogre.movement = 0;
   if (ogre.treads > 0) ogre.movement = 1;
   if (ogre.treads > ogre.init_treads / 3) ogre.movement = 2;
   if (ogre.treads > 2 * ogre.init_treads / 3) ogre.movement = 3;
}
