#include <coleco.h>
#include <getput1.h>

extern void pacdd (void);
extern void pacgob (void);

void bonus_game(void)
{

  byte winner;

  byte number;

  byte ballex;
  byte balley;
  byte balledx;
  byte balledy;

  byte ballego;

  byte palette1x;
  byte palette1y;
  byte palette2x;
  byte palette2y;
  byte palettedy;

  byte c;

debut:
  
  cls();

  center_string(1,"DACPONG!");

  center_string(3,"2000 BY DANIEL BIENVENU");

  center_string(12,"TWO PLAYERS ONLY");

  center_string(22,"PRESS FIRE TO START");

  pause();

  while((joypad_1 | joypad_2)&0xf0);

  cls();

  palette1x=1;
  palette1y=1;
  palette2x=30;
  palette2y=20;

  winner = 0;

  number = get_random() & 0x01;
  if (number == 0x00)
  {
   ballex=palette1x+1;
   balley=palette1y+1;
   balledx=1;
   balledy=1;
   ballego = 1;
  }
  else
  {
   ballex=palette2x-1;
   balley=palette2y+1;
   balledx=2;
   balledy=1;
   ballego = 2;
  }

  /* Balle */
  put_char(ballex,balley,0xa0+2*balledx);

  /* Palettes */
  put_char(palette1x,palette1y,0x82);
  put_char(palette1x,palette1y+1,0x8a);
  put_char(palette1x,palette1y+2,0x88);
  put_char(palette2x,palette2y,0x82);
  put_char(palette2x,palette2y+1,0x8a);
  put_char(palette2x,palette2y+2,0x88);

  while(winner==0)
  {

/* Joypad : 0x80 = fire (right), 0x40 = fire (left), 0x8 = left, 0x4 = down, 0x2 = right, 0x1 = up */

   palettedy=0;

   if (joypad_1 & 0x1)
   {
    if (palette1y>0)
    {
     palettedy=2;
    }
   }
   if (joypad_1 & 0x4)
   {
    if (palette1y<21)
    {
     palettedy=1;
    }
   }

   if (palettedy!=0)
   {

    put_char(palette1x,palette1y,0x20);
    put_char(palette1x,palette1y+1,0x20);
    put_char(palette1x,palette1y+2,0x20);

    if (palettedy==1) palette1y++;
    if (palettedy==2) palette1y--;

    if (ballego==1)
    {
     put_char(ballex,balley,0x20);
     if (palettedy==1) {balley++; balledy=1;}
     if (palettedy==2) {balley--; balledy=2;}
     put_char(ballex,balley,0xa0+2*balledx);
    }

    put_char(palette1x,palette1y,0x82);
    put_char(palette1x,palette1y+1,0x8a);
    put_char(palette1x,palette1y+2,0x88);
    
   }

   if (joypad_1 & 0xf0) {if (ballego==1) ballego=0;}

   palettedy=0;

   if (joypad_2 & 0x1)
   {
    if (palette2y>0)
    {
     palettedy=2;
    }
   }
   if (joypad_2 & 0x4)
   {
    if (palette2y<21)
    {
     palettedy=1;
    }
   }

   if (palettedy!=0)
   {

    put_char(palette2x,palette2y,0x20);
    put_char(palette2x,palette2y+1,0x20);
    put_char(palette2x,palette2y+2,0x20);

    if (palettedy==1) palette2y++;
    if (palettedy==2) palette2y--;

    if (ballego==2)
    {
     put_char(ballex,balley,0x20);
     if (palettedy==1) {balley++; balledy=1;}
     if (palettedy==2) {balley--; balledy=2;}
     put_char(ballex,balley,0xa0+2*balledx);
    }

    put_char(palette2x,palette2y,0x82);
    put_char(palette2x,palette2y+1,0x8a);
    put_char(palette2x,palette2y+2,0x88);
    
   }

   if (joypad_2 & 0xf0) {if (ballego==2) ballego=0;}

   delay(1);

   if (ballego==0)
   {

    c = get_char(ballex,balley);

    if (c==0xa4 || c==0xa2)
    {
     put_char(ballex,balley,0x20);
    }

    if ((c & 0xf0)==0x80)
    {
     pacgob();
     if (ballex<16)
     {
      balledx=1; 
     }
     else
     {
      balledx=2;
     }
    }
    
    if (balley==1)
    {
     balledy=1;
    }
    if (balley==23)
    {
     balledy=2;
    }

    if (balledy==1)
    {
     balley++;
    }
    if (balledy==2)
    {
     balley--;
    }

    if (balledx==1)
    {
     ballex++;
    }
    if (balledx==2)
    {
     ballex--;
    }

    c = get_char(ballex,balley);
    if ((c & 0xf0)==0x80)
    {
     pacgob();
     if (ballex<16)
     {
      balledx=1; 
      ballex++;
     }
     else
     {
      balledx=2;
      ballex--;
     }
    }

    if (ballex==0)
    {
     winner=2;
    }
    if (ballex==31)
    {
     winner=1;
    }

    put_char(ballex,balley,0xa0+2*balledx);

   }

   delay(1);
 
  }

  pacdd();

  if (winner==1)
  {
   center_string(10,"PLAYER ONE WIN");
  }
  else
  {
   center_string(10,"PLAYER TWO WIN");
  }

  delay(30);

  center_string(19,"PLAY AGAIN?");

  center_string(21,"* YES  # NO");

  while (!(keypad_1==10 || keypad_1==11));

  if (keypad_1==10) {goto debut;}

}

/*
   if (keypad_1 == 3) {pacgob(); delay(5);}
   if (keypad_1 == 7) {pacdd(); delay(52);}
*/
