/*
** geschrieben von
**  ssch0098@rz.uni-hildesheim.de (Stefan Schwoon)
**
** Datei: dumplab.c
**
** Kommentar:
**  Gibt ein Labyrinth als Text aus
**    Ausgabe kann z.B. in Datei umgeleitet werden,
**    um nach Bearbeitung wieder durch makelab in
**    eine Labyrinthdatei zurueckgewandelt zu werden.
*/


#include <stdio.h>
#include <string.h>
#include <time.h>

#include "global.h"
#include "speicher.h"
#include "labyrinth.h"
#include "farben.h"


char MAGIC[11]="iMazeLab1\n";


#define BASISFARBE 1
#define BASISTUERFARBE 8

#ifndef NORMALFARBE
#define NORMALFARBE 5
#endif


extern char *optarg;
extern int optind;

u_int feldlaenge, feldbreite;
block **spielfeld;

static char *av0;

static int wanddichte, tuerdichte, startwert;
static int suchen, laber;

static int ladelab(char *fname)
{
  FILE *f;
  u_char magic[80],daten[4];
  int i,j,x,y;
  struct wand *wand;
  
  if (!(f = fopen(fname,"rb")))
  {
    fprintf(stderr,"Couldn't open %s !\n",fname);
    exit(1);
  }  
    
  if (fread(magic,10,1,f)!=1)
  {
    fprintf(stderr,"Couldn't read MAGIC !\n");
    fclose(f);
    exit(1);
  }
  
  if (strncmp(MAGIC,magic,10)!=0)
  {
    fprintf(stderr,"No Imaze-lab file !\n");
    fclose(f);
    exit(1);
  }
  fread(daten,1,2,f);
  feldbreite = daten[0];
  feldlaenge = daten[1];

  speicher_belegen((void **)&spielfeld, feldbreite * sizeof(block *));

  for (j = 0; j < feldbreite; j++)
    speicher_belegen((void **)&spielfeld[j],feldlaenge * sizeof(block));
  
  for (y = 0; y < feldlaenge; y++)
    for (x = 0; x < feldbreite; x++)
    {
      fread(daten,1,4,f);
      for (i = 0; i < 4; i++)
      {
        wand = &spielfeld[x][y][i];    
        wand->unbegehbar = daten[i] >> 7;
        wand->schusssicher = (daten[i] >> 6) & 1;
        wand->farbe = (daten[i] & 0x3f);
      }
    } 
  fgets(magic,80,f); 
  printf("%s\n",magic);
  fclose(f); 
  
}
  

static void dumpit()
{
  int x,y,i,j;
  struct wand *wand,*wand1;  
  for (y=0; y < feldlaenge; y++)
  {
    for (x=0; x<feldbreite; x++)
    {
      wand = wand1 = &spielfeld[x][y][NORD];
      if (y) wand1 = &spielfeld[x][y-1][SUED];
      if (wand->unbegehbar && wand1->unbegehbar) printf("+-");
      else if (IST_TUER(wand->farbe) && IST_TUER(wand1->farbe)) printf("+|");
      else if (wand->unbegehbar && !wand1->unbegehbar) printf("+v");
      else if (!wand->unbegehbar && wand1->unbegehbar) printf("+^");
      else printf("+ "); 
    }
    printf("+\n");
    for (x=0; x<feldbreite; x++)
    {
      wand = wand1 = &spielfeld[x][y][WEST];
      if (x) wand1 = &spielfeld[x-1][y][OST];
      if (wand->unbegehbar && wand1->unbegehbar) printf("| ");
      else if (IST_TUER(wand->farbe) && IST_TUER(wand1->farbe)) printf("- ");
      else if (wand->unbegehbar && !wand1->unbegehbar) printf("> ");
      else if (!wand->unbegehbar && wand1->unbegehbar) printf("< ");
      else printf("  ");
    }
    printf("|\n");
  }
  for (x=0; x<feldbreite; x++)
    printf("+-");
  printf("+\n");  
}
    



/* bis hier lokaler Teil                       */
/***********************************************/
/* ab hier globaler Teil                       */


void uebler_fehler(meldung, knopf)
char **meldung;
char *knopf;
{
	fprintf(stderr, "fatal error\n");
	exit(1);
}


int main(argc, argv)
int argc;
char **argv;
{
  if (argc!=2)
  {
    printf("Usage : %s [lab-file]\n",argv[0]);
    exit(1);
  }
  ladelab(argv[1]);
  dumpit();
  return 0;
}

