//
//  Mapedit.h
//
#include "deftype.h"

#define MANN_SIZE       26
#define STRG            36
#define ML              MannListe
#define FL              FeindListe
#define MF              MannFeld
#define Mlinks          0   //73
#define Mrechts         319 //315
#define Moben           0   //11
#define Munten          200 //191


BYTE LandData[500000], MannData[500000], MannBack[500000], FeindBack[500000]; 
BYTE Feld[200][200], MannFeld[200][200];
MANN MannListe[200], FeindListe[200];
int f_x=0; f_y=0, MannAnzahl= -1, FeindAnzahl= -1, scroll_del, mann_del, ss_del, sm_del;
int SPIEL_LANG  = 10;
int SPIEL_BREIT = 16;
int FELD_X;
int FELD_Y;
int x_lang, y_breit, aktzaehler;
char karte[13];
char keyb_flag;


/****************************************************************************/
/* "Erschafft" einen neuen Mann.                                            */
/* ------------------------------------------------------------------------ */
/* Parameter: globale Koordinaten und das momentane Aussehen.               */
/****************************************************************************/
int MannInsert ( int ax, int ay, BYTE shape )
{

  MannAnzahl++;
  MannListe[MannAnzahl].del     = 0;
  MannListe[MannAnzahl].x       = ax-(f_x*20);
  MannListe[MannAnzahl].y       = ay-(f_y*20);
  MannListe[MannAnzahl].x1      = ax-(f_x*20);
  MannListe[MannAnzahl].y1      = ay-(f_y*20);
  MannListe[MannAnzahl].ax      = ax;
  MannListe[MannAnzahl].ay      = ay;
  MannListe[MannAnzahl].dx      = ax;
  MannListe[MannAnzahl].dy      = ay;
  MannListe[MannAnzahl].dx1     = ax;
  MannListe[MannAnzahl].dy1     = ay;
  MannListe[MannAnzahl].shape   = shape;
  MannListe[MannAnzahl].energie = 10;
  MannListe[MannAnzahl].stop    = 0;
  MannListe[MannAnzahl].feldx   = ax/20;
  MannListe[MannAnzahl].feldy   = ay/20;

  return 1;
}

void MannDelete( BYTE index )
{
  int i;

  for ( i=index; i<MannAnzahl; i++ ) ML[i] = ML[i+1];     // index korrigieren
  memmove(&MannBack[index*400], &MannBack[(index+1)*400], (MannAnzahl-index)*400);
  MannAnzahl--;
}

/****************************************************************************/
/* "Erschafft" einen neuen Feind.                                           */
/* ------------------------------------------------------------------------ */
/* Parameter: globale Koordinaten und das momentane Aussehen.               */
/****************************************************************************/
int FeindInsert ( int ax, int ay, BYTE shape )
{

  FeindAnzahl++;
  FL[FeindAnzahl].del     = 0;
  FL[FeindAnzahl].x       = ax-(f_x*20);
  FL[FeindAnzahl].y       = ay-(f_y*20);
  FL[FeindAnzahl].x1      = ax-(f_x*20);
  FL[FeindAnzahl].y1      = ay-(f_y*20);
  FL[FeindAnzahl].ax      = ax;
  FL[FeindAnzahl].ay      = ay;
  FL[FeindAnzahl].dx      = ax;
  FL[FeindAnzahl].dy      = ay;
  FL[FeindAnzahl].dx1     = ax;
  FL[FeindAnzahl].dy1     = ay;
  FL[FeindAnzahl].shape   = shape;
  FL[FeindAnzahl].feldx   = ax/20;
  FL[FeindAnzahl].feldy   = ay/20;
  FL[FeindAnzahl].energie = 10;
  FL[FeindAnzahl].stop    = 0;

  return 1;
}

void FeindDelete( BYTE index )
{
  int i;

  for ( i=index; i<FeindAnzahl; i++ ) FL[i] = FL[i+1];
  memmove(&FeindBack[index*400], &FeindBack[(index+1)*400], (FeindAnzahl-index)*400);
  FeindAnzahl--;
}

void Mann_back_restore ( BYTE index )
{
  int i, j, h=index*400;

  for ( i=0; i<20; i++ )
  {
    for ( j=0; j<20; j++ )
    {
      if ( ((ML[index].x1+j)<Mrechts)&&((ML[index].x1+j)>Mlinks)&&((ML[index].y1+i)>Moben)&&((ML[index].y1+i)<Munten) )
      {
        vsetpix(ML[index].x1+j, ML[index].y1+i, MannBack[h]);
      }
      h++;
    }
  }
}

void mann_set ( BYTE index )
{
  int i, j, h=index*400, k=ML[index].shape*400;

  for ( i=0; i<20; i++ )
  {
    for ( j=0; j<20; j++ )
    {
      if ( ((ML[index].x1+j)<Mrechts)&&((ML[index].x1+j)>Mlinks)&&((ML[index].y1+i)>Moben)&&((ML[index].y1+i)<Munten) )
      {
        MannBack[h] = vgetpix( ML[index].x+j, ML[index].y+i);
        if ( ((MannData[k]>5)&&(MannBack[h]<120)) ) vsetpix( ML[index].x+j, ML[index].y+i, MannData[k]); 
      }
    h++; k++;
    }
  }
}

void Feind_back_restore ( BYTE index )
{
  int i, j, h=index*400;

  for ( i=0; i<20; i++ )
  {
    for ( j=0; j<20; j++ )
    {
      if ( ((FL[index].x1+j)<Mrechts)&&((FL[index].x1+j)>Mlinks)&&((FL[index].y1+i)>Moben)&&((FL[index].y1+i)<Munten) )
        vsetpix(FL[index].x1+j, FL[index].y1+i, FeindBack[h]);
      h++;
    }
  }
}

void Feind_set ( BYTE index )
{
  int i, j, h=index*400, k=FL[index].shape*400;

  for ( i=0; i<20; i++ )
  {
    for ( j=0; j<20; j++ )
    {
      if ( ((FL[index].x1+j)<Mrechts)&&((FL[index].x1+j)>Mlinks)&&((FL[index].y1+i)>Moben)&&((FL[index].y1+i)<Munten) )
      {
        FeindBack[h] = vgetpix( FL[index].x+j, FL[index].y+i);
        if ( ((MannData[k]>5)&&(FeindBack[h]<96)) ) vsetpix( FL[index].x+j, FL[index].y+i, MannData[k]); 
      }
    h++; k++;
    }
  }
}

void UpdateScreen ( void )
{
  int i, j;

  for (i=f_y; i<f_y+SPIEL_LANG; i++)          
  {
    for (j=f_x; j<f_x+SPIEL_BREIT; j++)
    {
      put_sprite((j-f_x)*20, (i-f_y)*20, Feld[i][j]*400);
    }
  }

  if ( MannAnzahl > -1 )
  {
    for ( i=0; i<MannAnzahl+1; i++ )
    {
      if ( (ML[i].x<320)&&(ML[i].x>-20)&&(ML[i].y<200)&&(ML[i].y>-20) )
      {
        mann_set(i);
      }
    }
  }
  if ( FeindAnzahl > -1 )
  {
    for ( i=0; i<FeindAnzahl+1; i++ )
    {
      if ( (FL[i].x<320)&&(FL[i].x>-20)&&(FL[i].y<200)&&(FL[i].y>-20) ) feind_set(i);
    }
  }
}
