/* THE AVENGER HAWKS 1988    COPYRIGHT BY COUGAR
/* ---------------------------------------------
/* This utilitie take a sonix song and instruments
/* and pack them on a only one file.
/* ---------------------------------------------


#include <hardware/custom.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <devices/audio.h>
#include <stdio.h>

#define  PRIORITY          10L    /* Priority for Audio Channel usage */
#define  NBR_IOA_STRUCTS   10L    /* Number of IOAudio structures used */
#define  PV_IOA_STRUCT     0L     /* index to ioapv struct */
#define  FIN_IOA_STRUCT    9L     /* index to finishioa struct */
#define  NBR_WAVES         5L     /* number of waves per instrument */
#define  WAVES             60000L
#define  YES               1L
#define  NO                0L
#define  DELA              14700L

#define LED    (*((BYTE *)0xBFE001))

extern struct MsgPort *CreatePort();
extern struct IOAudio *GetMsg();
extern void *AllocMem();

struct IOAudio *ioa, *finishioa, *ioapv;
struct IOAudio *ioainuse[4];
struct IOAudio *freeioa[4];
struct Device *device=0;

UBYTE aMap[] = { 0x0f };                  /* allocate four channels */
long  voiceMap[] = { 1, 2, 4, 8 };
long unitno = 1;
int error, waiting[4] = { NO, NO, NO, NO };
int insactive[]={
    0,0,0,0};

long woffsets[26][5];
long wlen[26][5];
UBYTE Volume[26];
BYTE *wave[4] = { NULL, NULL, NULL, NULL };

int perval[] = {
    428, 404, 381, 360, 339, 320, 302, 285, 269, 254, 240, 226, 214 
};
int valor[] = {
    17,9,5,3,2,0,0,0,0,13,7,4
};
char Filname[128];
long TLen[4];
UBYTE song[5000][4];
BYTE *wptr, *wfp, *KILLER, *HERE;

/* THE AVENGER HAWKS 1988    COPYRIGHT BY COUGAR

char *portstring[] = {
   "Audio one",
   "Audio two",
   "Audio three",
   "Audio four",
   "Audio five",
   "Audio six",
   "Audio seven",
   "Audio eight" };     /* names for the CreatePorts */
 

InitIOA() {
int i;
 
   ioa = (struct IOAudio *)AllocMem((NBR_IOA_STRUCTS * (long)sizeof(*ioa)),
      MEMF_PUBLIC | MEMF_CLEAR);
   if (ioa == NULL)
      FinishProg(1);
 
   for (i = 0; i < 4; ++i)  {
      ioainuse[i] = &ioa[i + 1];
      freeioa[i] = &ioa[i + 5];
   }
   ioapv = &ioa[PV_IOA_STRUCT];
   finishioa = &ioa[FIN_IOA_STRUCT];
 
   ioa->ioa_Request.io_Message.mn_Node.ln_Pri = PRIORITY;
   ioa->ioa_Request.io_Message.mn_ReplyPort = CreatePort("Audio zero", 0L);
   if (ioa->ioa_Request.io_Message.mn_ReplyPort == NULL)
      FinishProg(2);
   ioa->ioa_Data = aMap;
   ioa->ioa_Length = (long)sizeof(aMap);
   error = OpenDevice(AUDIONAME, 0L, ioa, 0L);
   if (error)
      FinishProg(3);
 
   device=ioa->ioa_Request.io_Device;

   *finishioa = *ioa;
   finishioa->ioa_Request.io_Flags = IOF_QUICK;
   ioapv->ioa_Request.io_Flags = IOF_QUICK;
 
   finishioa->ioa_Request.io_Command = ADCMD_FINISH;
   ioapv->ioa_Request.io_Command = ADCMD_PERVOL;
 

   for (i = 0; i < 4; i++) {
      *freeioa[i] = *ioa;
      *ioainuse[i] = *ioa;

      freeioa[i]->ioa_Request.io_Message.mn_ReplyPort = 
          CreatePort(portstring[i]);
      ioainuse[i]->ioa_Request.io_Message.mn_ReplyPort =
          CreatePort(portstring[i+4]);

      freeioa[i]->ioa_Request.io_Device = device;
      ioainuse[i]->ioa_Request.io_Device = device;

      freeioa[i]->ioa_AllocKey = ioa->ioa_AllocKey;
      ioainuse[i]->ioa_AllocKey = ioa->ioa_AllocKey;
   }
   for (i = 0; i < 4; ++i)
      if (freeioa[i]->ioa_Request.io_Message.mn_ReplyPort == NULL ||
         ioainuse[i]->ioa_Request.io_Message.mn_ReplyPort == NULL)
         FinishProg(4);
}

/* THE AVENGER HAWKS 1988    COPYRIGHT BY COUGAR

FinishProg(finishcode)
int finishcode;
{
   int i;
 
   switch(finishcode)
   {
   case 0:

   case 4:
   case 5:

      for (i=0;i<4;++i) {
         if (freeioa[i]->ioa_Request.io_Message.mn_ReplyPort)
            DeletePort(freeioa[i]->ioa_Request.io_Message.mn_ReplyPort);
         if (ioainuse[i]->ioa_Request.io_Message.mn_ReplyPort)
            DeletePort(ioainuse[i]->ioa_Request.io_Message.mn_ReplyPort);
      }
 
      CloseDevice(ioa);
 
   case 3:
      DeletePort(ioa->ioa_Request.io_Message.mn_ReplyPort);
/*      DeletePort(ioa->ioa_Request.io_Message.mn_ReplyPort,
         (LONG)sizeof(struct MsgPort));
*/
   case 2:
      FreeMem(ioa, (NBR_IOA_STRUCTS * (long)sizeof(*ioa)));
 
      }
}

setwpv(wf, len, per, vol, channel)
char *wf;
long len;
int per, vol, channel;
{
   struct IOAudio *tmpioa;
 
   freeioa[channel]->ioa_Request.io_Command = CMD_WRITE;
   freeioa[channel]->ioa_Request.io_Flags =
           ADIOF_PERVOL | IOF_QUICK | ADIOF_SYNCCYCLE;
   freeioa[channel]->ioa_Cycles = 1;

   freeioa[channel]->ioa_Request.io_Unit = (struct Unit *)unitno;
   finishioa->ioa_Request.io_Unit = (struct Unit *)unitno;

   freeioa[channel]->ioa_Data = (UBYTE *)wf;
   freeioa[channel]->ioa_Length = len;
   freeioa[channel]->ioa_Period = per;
   freeioa[channel]->ioa_Volume = vol;

   if (waiting[channel]) {
      BeginIO(finishioa);
      WaitIO(ioainuse[channel]);
      waiting[channel] = NO;
   }
   BeginIO(freeioa[channel]);
   error = CheckIO(freeioa[channel]);
   if (error) {
      printf("Error on CMD_WRITE\n");
      WaitIO(freeioa[channel]);
   }
   waiting[channel] = YES;
 
   /* swap the pointers.  That way, the next time we pass through, we
    * will still work.
    */
   tmpioa = ioainuse[channel];
   ioainuse[channel] = freeioa[channel];
   freeioa[channel] = tmpioa;
}

/* THE AVENGER HAWKS 1988    COPYRIGHT BY COUGAR

StopVoices() {
int voice;
 
   for (voice=0;voice<4;voice++) {
      if (waiting[voice]) {
         unitno=voiceMap[voice];
         finishioa->ioa_Request.io_Unit=(struct Unit *)unitno;
         BeginIO(finishioa);
         WaitIO(ioainuse[voice]);
         waiting[voice]=NO;
      }
   }
}

LoadToRam()
{
  FILE *Filesys;
  register long l;

  wfp=(BYTE *)AllocMem( WAVES, MEMF_CHIP);
  KILLER=wfp;
  Filesys=fopen("Kurre.dat","r");
  for (l=0;!(feof(Filesys));l++) *wfp++=getc(Filesys);
  fclose(Filesys);
}

makewaves() {

/*   LoadToRam();*/
   HERE=40000L;
   TheSearchOfSpock();
   LoadElements(); 
   LoadTrack();
   Mach5();
   wptr=HERE;
   wfp=wptr;
}


strike(note, voyce)
int note, voyce;
{
   unsigned int per, oct;
 
   unitno = voiceMap[voyce];

   oct = (note/12)-3;
   per = perval[note % 12];
   wave[voyce] = wptr + woffsets[insactive[voyce]][oct];
   setwpv(wave[voyce],wlen[insactive[voyce]][oct], per,
                      Volume[insactive[voyce]], voyce);
}

int pos[4],Dr[4];

main(argc, argv)
int argc;
char *argv[];
{
   int i, j, delhay;
   UBYTE aux,bux;

   struct Task *thistask, *FindTask();
  
   if (thistask=FindTask(NULL)) SetTaskPri(thistask,127L);

   delhay = DELA;

   for (i=0;i<4;i++) {
     Dr[i]=1;
     pos[i]=0;
   }
   printf("THE AVENGER HAWKS Productions 88\n");

   makewaves();
   InitIOA();

   for (;;) {

   if (pos[0]>=TLen[0] && pos[1]>=TLen[1] && 
            pos[2]>=TLen[2] && pos[3]>=TLen[3] ) 
              if (Dr[0]<=1 && Dr[1]<=1 && Dr[2]<=1 && Dr[3]<=1) 
                  for (i=0;i<4;i++) {
                      Dr[i]=1;
                      pos[i]=0;
                  }
     if((LED & 64)==0) break;

     for (i=0;i<4;i++) {
         aux=song[pos[i]][i];
         bux=song[pos[i]+1][i];
         if (aux==129) {
              insactive[i]=bux;
              pos[i]++;
              pos[i]++;
              aux=song[pos[i]][i];
              bux=song[pos[i]+1][i];
         }
         if (Dr[i]==1 & pos[i]<TLen[i]) {
            if (aux!=128) strike(aux,i);
            Dr[i]=valor[bux];
            pos[i]++;
            pos[i]++;
         }
         if (Dr[i]>1) Dr[i]--;
     }
     for (i=0;i<delhay;i++) {  }
   }
   StopVoices();
/* FreeMem(KILLER,WAVES);*/
   FinishProg(0);
   if (thistask=FindTask(NULL)) SetTaskPri(thistask,0L);

}

/* THE AVENGER HAWKS 1988    COPYRIGHT BY COUGAR

long getLW() {

   long AMIGA=*HERE++;
   AMIGA=(AMIGA << 8)+(*HERE++ & 0xff);
   AMIGA=(AMIGA << 8)+(*HERE++ & 0xff);
   AMIGA=(AMIGA << 8)+(*HERE++ & 0xff);
   return (AMIGA);
}

LoadElements()
{
  int i,j;

  for (i=0;i<25;i++) {
     Volume[i]=getLW();
     for (j=0;j<5;j++) {
        wlen[i][j]=getLW();
        woffsets[i][j]=getLW();
/*      printf("LONG:%lx\n",wlen[i][j]);
        printf("OFFSET:%lx\n",woffsets[i][j]);
*/
     }
   }
}


TheSearchOfSpock()
{
   register long i;
   int j,f;
   char kar;
   char alfacode[8];

   alfacode[0]='E';
   alfacode[1]='N';
   alfacode[2]='O';
   alfacode[3]='Z';
   alfacode[4]='A';
   alfacode[5]='f';
   alfacode[6]='l';
   alfacode[7]='A';

   j=0l;
   for (i=0;i<500000L;i++) {
     if (j==0) kar=*HERE++;
     for (j=0;j<8;j++) {
         if (kar!=alfacode[7-j]) break;
         else kar=*HERE++;

         if (j>=7) {
/*
            printf("Found HERE %ld\n",HERE);
*/
            HERE--;
            HERE--;
            HERE--;
            *HERE++=0;
            *HERE++=0;
            return();
         }
     }
     if (HERE>512000L) break;
   }
}

LoadTrack()
{

  register long i;
  char khar;
  int j,k;
  long leng;

  for (j=0;j<4;j++) {
    khar=*HERE++;
    for (i=0;i<500000L;i++) {
           if (khar=='T') {
                khar=*HERE++; 
                if (khar=='R') {
                      khar=*HERE++; 
                 if (khar=='A') {
                      khar=*HERE++; 
                      if (khar=='K') break;
                 }
                }
           }
           else khar=*HERE++;
    }
   leng=getLW();
   TLen[j]=leng;
   for (k=0;k<leng;k++) song[k][j]=(UBYTE)*HERE++;
  }
}


Mach5()
{

  register long i;
  char khar;
  int j,k;

    khar=*HERE++;
    for (i=0;i<500000L;i++) {
           if (khar=='M') {
                khar=*HERE++; 
                if (khar=='A') {
                      khar=*HERE++; 
                 if (khar=='C') {
                      khar=*HERE++; 
                      if (khar=='H') break;
                 }
                }
           }
           else khar=*HERE++;
    }
}
/* THE AVENGER HAWKS 1988    COPYRIGHT BY COUGAR

