/*EdQuake v0.02 source code
  (c) Copyright 1996 Scott Mitting
  email:smitting@netusa1.net
  ----------------------------------
  MAPEDIT.C  -  The heart of EdQuake
		These functions do not work
		they are under developement

  __variables:
  __functions:
  ----------------------------------
  the entire source code is under renovation to make it easier
  to understand.  happy coding.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "svgacc.h"
#include "grfx.h"
#include "pak.h"
#include "disk.h"
#include "mapedit.h"
#include "input.h"
#include "ripper.h"
#include "viewer.h"
#include "wadmap.h"
#include "bsp.h"

extern void setquakecolors();
extern MouseCursor mymousecursor;

mapvertex_t mapvertex[1000];
  mapline_t mapline[1000];
  maproom_t maproom[200];

int numvertex=0;
int numlines=0;
int numrooms=0;

int curvertex=1;
int curline=1;
int curroom=1;

int curmode = 0; //0=rooms 1=line 2=vertex
long mapx=0, mapy=0;
float zoom;
//defaults
int dwalltext=0, dfloortext=0, dceiltext=0;
int dceil=128,dfloor=0;//ceiling
int dtop=128,dbottom=0;
char mapname[54] = "Untitled map";

//saves to .eqm ([E]d[Q]uake [M]ap)
void saveeqm(char *filename)
{
    FILE *f;
    int t, t2;
    int version = 0; //0 = no things
    f = fopen(filename, "wb");
    if (!f) return;
    fwrite("EdQuakeMap",10,1,f);
    fwrite(&version,2,1,f);
    fwrite(mapname,52,1,f);
    fwrite(&mapx,4,1,f);
    fwrite(&mapy,4,1,f);
    fwrite(&zoom,4,1,f);
    fwrite(&numvertex,2,1,f);
    for (t = 1; t <= numvertex; t++)
    {
      fwrite(&mapvertex[t].x,4,1,f);
      fwrite(&mapvertex[t].y,4,1,f);
    }
    fwrite(&numlines,2,1,f);
    for (t = 1; t <= numlines; t++)
    {
      fwrite(&mapline[t].v1,2,1,f);
      fwrite(&mapline[t].v2,2,1,f);
      fwrite(&mapline[t].visible,2,1,f);
      fwrite(&mapline[t].texture,2,1,f);
      fwrite(&mapline[t].top,2,1,f);
      fwrite(&mapline[t].bottom,2,1,f);
      fwrite(&mapline[t].hole,2,1,f);
    }
    fwrite(&numrooms,2,1,f);
    for (t = 1; t <= numrooms; t++)
    {
      fwrite(&maproom[t].numsides,2,1,f);
      for (t2 = 1; t2 <= maproom[t].numsides; t2++)
	 fwrite(&maproom[t].line[t2],2,1,f);
      fwrite(&maproom[t].ceiling,2,1,f);
      fwrite(&maproom[t].floor,2,1,f);
      fwrite(&maproom[t].ctext,2,1,f);
      fwrite(&maproom[t].ftext,2,1,f);
      fwrite(&maproom[t].cvis,2,1,f);
      fwrite(&maproom[t].fvis,2,1,f);
    }
}

void loadeqm(char *filename)
{
    FILE *f;
    char magic[10];
    int t;
    int version; //0 = no things
    f = fopen(filename, "rb");
    if (!f) return;
    fread(magic,10,1,f);
    if (magic != "EdQuakeMap") return;
    fread(&version,2,1,f);
    fread(mapname,52,1,f);
    fread(&mapx,4,1,f);
    fread(&mapy,4,1,f);
    fread(&zoom,4,1,f);
    fread(&numvertex,2,1,f);
    for (t = 1; t <= numvertex; t++)
      fread(&mapvertex[t],8,1,f);
    fread(&numlines,2,1,f);
    for (t = 1; t <= numlines; t++)
      fread(&mapline[t],14,1,f);
    fread(&numrooms,2,1,f);
    for (t = 1; t <= numrooms; t++)
      fread(&maproom[t],54,1,f);
}
