//---------------------------------------------------------------------------
// Manage all the links between characters
//---------------------------------------------------------------------------

#define  MAXLINKS  50
#define  MAXATTACH 90

#include <sys\types.h>
#include <libetc.h>
#include <libgte.h>
#include <libgpu.h>
#include <rand.h>
#include <libcd.h>
#include <memory.h>

#ifndef __player
   #define __player
   typedef struct
   {
      int Score;
      int BulletType;
      int Bullets;
      int Lifepnt;
      int Counter;
      int GunX;
      int GunY;
      int ShotDrawn;
      int ShotX;
      int ShotY;
      int Grade;
      int Shots;
      int Hits;
      int VictimsMiss;
      int PlayerNbr;
   } PLAYER;
#endif

#include "..\include\compile.h"
#include "..\include\db.h"
#include "..\include\bkgnd.h"
#include "..\include\characte.h"

#ifndef CHARACLINK
   #define CHARACLINK
   typedef struct
   {
      Character *Caller;
      Character *Receiver;
      short CallerAction;
      short ReceiverAction;
      char When;
      char Inverse;
      short CoorX;
      short CoorY;
   } LINK;
#endif

LINK LinkList[MAXLINKS];
short NbrLinks;
short NbrAttaches;

Character *Attach[MAXATTACH];
Character *AttachTo[MAXATTACH];
int       AttachDX[MAXATTACH];
int       AttachDY[MAXATTACH];

//-------------------------------------
// Initialize the link module
//-------------------------------------
void Init_Links (void)
{
   NbrLinks = 0;
}

//-------------------------------------
// Add a new link
//-------------------------------------
void AddLink (Character *caller, Character *receiver, int caction,
              int raction, int when, int inv, int coorx, int coory)
{
   LinkList[NbrLinks].Caller = caller;
   LinkList[NbrLinks].Receiver = receiver;
   LinkList[NbrLinks].CallerAction = caction;
   LinkList[NbrLinks].ReceiverAction = raction;
   LinkList[NbrLinks].When = when;
   LinkList[NbrLinks].Inverse = inv;
   LinkList[NbrLinks].CoorX = coorx;
   LinkList[NbrLinks].CoorY = coory;
   NbrLinks++;
}

//-------------------------------------
//
//-------------------------------------
void LinksDumpData (void)
{
   int i;
   FntPrint ("NbrLinks: %d\n", NbrLinks);
   for (i = 0; i < NbrLinks; i++)
      FntPrint ("Link %d:  C:0x%08x R:0x%08x CA:%d RA:%d W:%d  X:%d Y:%d\n",
                i, LinkList[i].Caller, LinkList[i].Receiver,
                LinkList[i].CallerAction, LinkList[i].ReceiverAction,
                LinkList[i].When, LinkList[i].CoorX, LinkList[i].CoorY);
}

//-------------------------------------
// Initialize the attach module
//-------------------------------------
void Init_Attaches (void)
{
   NbrAttaches = 0;
}

//-------------------------------------
// Add a new link
//-------------------------------------
void AddAttach (Character *attach, Character *attachto, int deltax, int deltay)
{
   int i = 0;

   while (i < NbrAttaches)
   {
      if ((Attach[i] == attach) && (AttachTo[i] == attachto))
         return;
      else
         i++;
   }

   Attach[NbrAttaches] = attach;
   AttachTo[NbrAttaches] = attachto;
   AttachDX[NbrAttaches] = deltax;
   AttachDY[NbrAttaches] = deltay;
   NbrAttaches++;
}

//-------------------------------------
// Dettach a character from any other
//-------------------------------------
void Dettach (Character *chara)
{
   int i;

   for (i = 0; i < NbrAttaches; i++)
   {
      if ((Attach[i] == chara) || (AttachTo[i] == chara))
      {
         AttachTo[i] == NULL;
         Attach[i] = NULL;
      }
   }
}
