//**************************************************************************//
// Das hier wird die DEFKON17 Konvertierung auf WATCOM C                    //
// -----------------------------------------------------                    //
// von Stephan Meiáner                                                      //
// ab 25. M„rz 1996                                                         //
//**************************************************************************//
#include <stdio.h>
#include <conio.h>
#include <graph.h>
#include <dos.h>
#include <i86.h>
#include <string.h>
#include <gfklib.h>
#include <pcxlib.h>
#include <maus.h>
#include <font.h>
#include <button.h>
#include <mapedit.h>

typedef struct ini
{
  int   start;
  int   end;
  char  msg[11];
} PARTS;


FILE *Datei;
char puffer[14];
PARTS Msg[12];
int parts, p_count=0, p_start=0, p_end, select, Anzahl=0;


void main ( void )
{
  int i, j;
  char c=0, pal[768], PartName[10] = "Alle";

  init_Spiel();

  Screen_Off();
  LoadPalette("mr-005.dbl", pal); SetPalette(pal); Set_Overscan_Color(0);
  UpdateScreen(); show_parts();
  write(3, 194, "(c)wild Synaps 1995-1996", 107);
  cpy2screen(); Screen_On();

  while ( c != 27 )                 
  {
    m_show_image= 1;
    maus_pos(); maus_show();

    if ( kbhit() )
    {
      c = getch();
      if ( c == 0 )
      {
        c = getch();
        if ( (c==59)&&(Anzahl>=1) ) { p_start=Msg[0].start; p_end=Msg[0].end;; p_count=Msg[0].start; show_parts(); strcpy(PartName, Msg[0].msg); }
        if ( (c==60)&&(Anzahl>=2) ) { p_start=Msg[1].start; p_end=Msg[1].end;  p_count=Msg[1].start; show_parts(); strcpy(PartName, Msg[1].msg); }
        if ( (c==61)&&(Anzahl>=3) ) { p_start=Msg[2].start; p_end=Msg[2].end;; p_count=Msg[2].start; show_parts(); strcpy(PartName, Msg[2].msg); }
        if ( (c==62)&&(Anzahl>=4) ) { p_start=Msg[3].start; p_end=Msg[3].end;; p_count=Msg[3].start; show_parts(); strcpy(PartName, Msg[3].msg); }
        if ( (c==63)&&(Anzahl>=5) ) { p_start=Msg[4].start; p_end=Msg[4].end;  p_count=Msg[4].start; show_parts(); strcpy(PartName, Msg[4].msg); }
        if ( (c==64)&&(Anzahl>=6) ) { p_start=Msg[5].start; p_end=Msg[5].end;; p_count=Msg[5].start; show_parts(); strcpy(PartName, Msg[5].msg); }
        if ( (c==65)&&(Anzahl>=7) ) { p_start=Msg[6].start; p_end=Msg[6].end;; p_count=Msg[6].start; show_parts(); strcpy(PartName, Msg[6].msg); }
        if ( (c==66)&&(Anzahl>=8) ) { p_start=Msg[7].start; p_end=Msg[7].end;  p_count=Msg[7].start; show_parts(); strcpy(PartName, Msg[7].msg); }
        if ( (c==67)&&(Anzahl>=9) ) { p_start=Msg[8].start; p_end=Msg[8].end;; p_count=Msg[8].start; show_parts(); strcpy(PartName, Msg[8].msg); }
      }
      else
      {
        if ( c ==109 ) { if ( MannAnzahl > -1 ) { MF[ML[0].feldy][ML[0].feldx]=0; mann_back_restore(0); MannDelete(0); }}
        if ( c ==102 ) { if ( FeindAnzahl > -1 ) { MF[FL[0].feldy][FL[0].feldx]=0;feind_back_restore(0); FeindDelete(0); }}
        if ( c == 13 )
        {
          m_back_restore();
          MannInsert((f_x*20)+m_x, (f_y*20)+m_y, 26); mann_set(MannAnzahl); m_back_save(); m_place();
        }
        if ( c == 32 )                                                  // d
        {
          m_back_restore();
          FeindInsert((f_x*20)+m_x, (f_y*20)+m_y, 0); feind_set(FeindAnzahl); m_back_save(); m_place();
        }
      }
    }
    //-----------------------------------------------------------
    if ( (m_y>180)&&(f_y<FELD_Y) ) { scroll_down();  delay(10); }
    if ( (m_y< 10)&&(f_y>0) )      { scroll_up();    delay(10); }
    if ( (m_x>310)&&(f_x<FELD_X) ) { scroll_right(); delay(10); }
    if ( (m_x< 10)&&(f_x>0) )      { scroll_left();  delay(10); }
    //-----------------------------------------------------------
    m_back_restore(); 
    fill_box(269, 49, 301, 181, 255); draw_box(270, 50, 300, 180, 247);
    button_up(273, 164, 11, "h"); button_up(289, 164, 11, "r");
    button_up(248, 0, 70, PartName);
    show_parts();
    m_back_save(); m_place(); m_show_image= 1;
    Iwrite(100, 3, MannAnzahl, 106); Iwrite(115, 3, FeindAnzahl, 96);

    if ( m_taste == 1 ) maus_links1();
    if ( m_taste == 2 ) select = 0;

    if ( screen_refresh ) { wait_retrace(); cpy2screen(); }
  }
  quit();
}

/****************************************************************************/
/* put_sprite1 bring ein Sprite von 20x20 auf den Bildschirm. Zuvor muá ihr  */
/* aber die Position des Sprites im XMS bergeben werden, sowie die X- und  */
/* Y-Koordinaten.                                                           */
/*--------------------------------------------------------------------------*/
/* Parameter:                                                               */
/*            x................................... X-Koordinate des Sprites */
/*            y................................... Y-Koordinate des Sprites */
/*            pos................................... Sprite-Position im XMS */
/****************************************************************************/
void put_sprite( int x, int y, unsigned int pos )
{
  int i= y, j= x, h= pos;

  for ( i=0; i<20; i++ )
  {
    for ( j=0; j<20; j++ ) vsetpix(x+j, y+i, LandData[h++]);
  }
}

/****************************************************************************/
/* Dies Funktion stellt das Grundgrst fr diese Spiel dar. Sie berechnet   */
/* welche Teile der Landschaft (Karte) angezeigt werden soll.               */
/* Gesteuert wird per Maus.						    */
/*--------------------------------------------------------------------------*/
/* Parameter:                                                               */
/*            weiter_x..................... wie weit in X-Richtung scroll1en */
/*            weiter_y..................... wie weit in Y_Richtung scroll1en */
/****************************************************************************/
void Landschaft ( int weiter_x, int weiter_y )
{
  int i, j;

  for (i=weiter_y; i<weiter_y+SPIEL_LANG; i++)          
  {
    for (j=weiter_x; j<weiter_x+SPIEL_BREIT; j++)     
    {
      put_sprite((j-weiter_x)*20, (i-weiter_y)*20, Feld[i] [j]*400);
    }
  }
}

/****************************************************************************/
/* Initialisiert das Spiel.                                                 */
/* ------------------------------------------------------------------------ */
/* keine Parameter                                                          */
/****************************************************************************/
void init_Spiel( void )
{
  int i, Zeichen;
  FILE *Datei;
  char chose, DateiName[127];

  Screen_On();
  if ( maus_reset() )        // berprfen ob ein Maustreiber installiert ist
  {
   printf("\nERROR: Es wurde kein Maustreiber gefunden!\n\n");
   exit(0);
  }
  load_ini();
  if ( chdir("DATA") )                       // ins DATA-Verzeichnis wechseln
  {
    printf("\nERROR: Kann Verzeichnis DATA nicht finden!\n\n");
    exit(0);
  }                                          
  Datei = fopen("terrain.ref", "rb");                   // Landschaft laden
  i= 0;
  while ((Zeichen = fgetc(Datei)) != EOF) LandData[i++] = (BYTE)Zeichen;
  fclose(Datei); parts = i/400; p_end = parts;

  Datei = fopen("mann.ref", "rb");                             // Leute laden
  i= 0;
  while ((Zeichen = fgetc(Datei)) != EOF) MannData[i++] = (BYTE)Zeichen;
  fclose(Datei);
  loadFont(); 

  printf("\nAuswahl der Aktion: ");
  printf("\n-------------------");
  printf("\n(N)neu Karte erstellen, oder Karte (L)aden : \n"); chose = getch();

  if ( (chose == 'n')||(chose == 'N') )
  {
    printf("\nGr”áe der Karte (max. 200x200): \n");
    printf("----------------------------------\n");
    printf("X-Wert: "); scanf("%d", &x_lang);  FELD_X = x_lang-12;
    printf("Y-Wert: "); scanf("%d", &y_breit); FELD_Y = y_breit-10;
  }
  if ( (chose == 'l')||(chose == 'L') )
  {
    printf("\nName der Karte die geladen werden soll: ");
    printf("\n---------------------------------------\n");
    printf("Dateiname: "); scanf("%s", DateiName);
    load_game(DateiName);
  }

  delay(100); fade_out();
  init320x200();
  delay(200);
  maus_off(); maus_put(160, 100);                              // Maus setzen
}


void scroll_down ( void )
{
  int i;

  f_y++;
  Landschaft(f_x, f_y); screen_refresh= 1;
  if ( MannAnzahl > -1 )
  {
    for ( i=0; i<MannAnzahl+1; i++ )
    {
      ML[i].y1=ML[i].y-=20; 
      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++ )
    {
      FL[i].y1=FL[i].y-=20;
      if ( (FL[i].x<320)&&(FL[i].x>-20)&&(FL[i].y<200)&&(FL[i].y>-20) ) feind_set(i);
    }
  }
  m_show_image=m_down;
}

void scroll_up ( void )
{
  int i;

  f_y--;
  Landschaft(f_x, f_y); screen_refresh= 1;
  if ( MannAnzahl > -1 )
  {
    for ( i=0; i<MannAnzahl+1; i++ )
    {
      ML[i].y1=ML[i].y+=20; 
      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++ )
    {
      FL[i].y1=FL[i].y+=20;
      if ( (FL[i].x<320)&&(FL[i].x>-20)&&(FL[i].y<200)&&(FL[i].y>-20) ) feind_set(i);
    }
  }
  m_show_image=m_up; 
}

void scroll_right ( void )
{
  int i;

  f_x++;
  Landschaft(f_x, f_y); screen_refresh= 1;
  if ( MannAnzahl > -1 )
  {
    for ( i=0; i<MannAnzahl+1; i++ )
    {
      ML[i].x1=ML[i].x-=20;
      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++ )
    {
      FL[i].x1=FL[i].x-=20;
      if ( (FL[i].x<320)&&(FL[i].x>-20)&&(FL[i].y<200)&&(FL[i].y>-20) ) feind_set(i);
    }
  }
  m_show_image=m_right; 
}

void scroll_left ( void )
{
  int i;

  f_x--;
  Landschaft(f_x, f_y); screen_refresh= 1;
  if ( MannAnzahl > -1 )
  {
    for ( i=0; i<MannAnzahl+1; i++ )
    {
      ML[i].x1=ML[i].x+=20; 
      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++ )
    {
      FL[i].x1=FL[i].x+=20;
      if ( (FL[i].x<320)&&(FL[i].x>-20)&&(FL[i].y<200)&&(FL[i].y>-20) ) feind_set(i);
    }
  }
  m_show_image=m_left;
}

void show_parts ( void )
{
  int i;

  for ( i=p_count; i<p_count+5; i++ ) put_sprite(275, 57+((i-p_count)*21), i*400);
}

void maus_links1 ( void )
{
  if ( ((m_x<285)&&(m_x>270)&&(m_y>161)&&(m_y<174)) )
  {
    if ( p_count > p_start ) { p_count--; show_parts(); delay(30); }
    return;
  }
  if ( ((m_x<298)&&(m_x>286)&&(m_y>161)&&(m_y<174)) )
  {
    if ( p_count < p_end-5 ) { p_count++; show_parts(); delay(30); }
    return;
  }
  if ( (m_x<295)&&(m_x>274)&&(m_y>15 )&&(m_y<160) ) { select = ((m_y-57)/21)+p_count; return; }

  //-------------------------------------------------------------------------------------------
  Feld[f_y+(m_y/21)][f_x+((m_x+3)/21)] = select;
  screen_refresh = 1;
  UpdateScreen(); 
}

int get_ini_value ( FILE *D )
{
  char ini[5];
  int i= 0;

  memset(ini, 32, 5);
  while ( fgetc(D) != '=' );
  while ( (ini[i++] = fgetc(D)) != ':');
  return atoi(ini); 
}

void load_ini ( void )
{
  int i, j=0;
  FILE *Datei;

  if ( (Datei = fopen("mapedit.ini", "rb")) == NULL )
  {
    Screen_On(); textmodus();
    printf("\n\nERROR: Kann 'mapedit.ini' nicht laden!\n\n");
    exit(0);
  }

  Anzahl = get_ini_value(Datei);
  while ( j < Anzahl )
  {
    Msg[j].start = get_ini_value(Datei);
    Msg[j].end   = get_ini_value(Datei); i = 0;
    while ( fgetc(Datei) != '=' ); while ( (Msg[j].msg[i++] = fgetc(Datei)) != ':');
    Msg[j].msg[--i] = 0x00; j++;
  }

  fclose(Datei);
}


void save_game ( char *Datei )
{
  int i, j;
  FILE *D;
  char sign[2];

  D = fopen(Datei, "wb");
  sign[0] = 'S'; sign[1] = 'M';
//------------------------------ Header --------------------------------------
  fputc(sign[0], D); fputc(sign[1], D);
  fputc(FELD_X, D);
  fputc(FELD_Y, D);
  fputc(MannAnzahl+1, D);
  fputc(FeindAnzahl+1, D);
//---------------------------- Karte speichern -------------------------------
  for ( i=0; i<(FELD_Y+11); i++)
  {
    for ( j=0; j<(FELD_X+13); j++) fputc(Feld[i][j], D);
  }
//---------------------------- Kennung fr Leute -----------------------------
  sign[0] = 0xEF;
  fputc(sign[0], D);
  if ( (MannAnzahl > -1) )  for(i=0; i<MannAnzahl+1; i++)  fput_Mann(D, &ML[i]);
  if ( (FeindAnzahl > -1) ) for(i=0; i<FeindAnzahl+1; i++) fput_Mann(D, &FL[i]);

//-----------------------------------------------------------------------------
  fclose(D);
}


void load_game ( char *Datei )
{
  int i, j;
  FILE *D;
  char sign[2];

  f_x = f_y = 0;

  if ( (D = fopen(Datei, "rb")) == NULL )
  {
    printf("\n\nERROR: Kann Datei '%s' nicht laden!\n\n", Datei);
    exit(0);
  }
  sign[0] = 'S'; sign[1] = 'M';
//------------------------------ Header --------------------------------------
  sign[0] = fgetc(D); sign[1] = fgetc(D);
  FELD_X = fgetc(D); x_lang = FELD_X + 12;
  FELD_Y = fgetc(D); y_breit = FELD_Y + 10;
  MannAnzahl =  fgetc(D); MannAnzahl--;
  FeindAnzahl = fgetc(D); FeindAnzahl--;
//---------------------------- Karte speichern -------------------------------
  for ( i=0; i<(FELD_Y+11); i++)
  {
    for ( j=0; j<(FELD_X+13); j++) Feld[i][j] = fgetc(D);
  }
//---------------------------- Kennung fr Leute -----------------------------
  sign[0] = fgetc(D);
  if ( (MannAnzahl > -1) )
    for(i=0; i<MannAnzahl+1; i++)
    {
      fget_Mann(D, &ML[i]);
      ML[i].ax=ML[i].feldx*20; ML[i].ay=ML[i].feldy*20;
      ML[i].y=ML[i].y1=ML[i].ay-(f_y*20); ML[i].x=ML[i].x1=ML[i].ax-(f_x*20);
    }
  if ( (FeindAnzahl > -1) )
    for(i=0; i<FeindAnzahl+1; i++)
    {
      fget_Mann(D, &FL[i]);
      FL[i].ax=FL[i].feldx*20; FL[i].ay=FL[i].feldy*20;
      FL[i].y=FL[i].y1=FL[i].ay-(f_y*20); FL[i].x=FL[i].x1= FL[i].ax-(f_x*20);
    }
//-----------------------------------------------------------------------------
  fclose(D);
}


/****************************************************************************/
/* Beendet das Spiel.                                                       */
/* ------------------------------------------------------------------------ */
/* keine Parameter                                                          */
/****************************************************************************/
void quit ( void )
{
  int i, j;
  char Dateiname[127], chose;

  fade_out();
  chdir(".."); chdir("data");
  textmodus();

  printf("\nAuswahl der Aktion: ");
  printf("\n-------------------");
  printf("\nKarte (S)ichern oder (V)erwerfen : \n"); chose = getch();

  if ( (chose == 's')||(chose == 'S') )
  {
    printf("\nDateiname fr die DEKON17-Karte: \n");
    printf("--------------------------------\n");
    printf("Datei: "); scanf("%s", Dateiname);
  
    save_game(Dateiname); 
    printf("Gesichert!\n\n");
    exit(0);
  }
  else { chdir(".."); exit(0); }
}











