home *** CD-ROM | disk | FTP | other *** search
/ Deathday Collection / dday.bin / edit / doombsp / doomload.m < prev    next >
Text File  |  1994-04-06  |  5KB  |  219 lines

  1. // doomload.m
  2. #import "doombsp.h"
  3.  
  4.  
  5. int        linenum = 0;
  6.  
  7. /*
  8. =================
  9. =
  10. = ReadLine
  11. =
  12. =================
  13. */
  14.  
  15. worldline_t *ReadLine (FILE *file)
  16. {
  17.     worldline_t    *line;
  18.     NXPoint        *p1, *p2;
  19.     worldside_t    *s;
  20.     sectordef_t    *e;
  21.     int            i;
  22.     
  23.     line = malloc(sizeof(*line));
  24.     memset (line, 0, sizeof(*line));
  25.  
  26.     p1 = &line->p1;
  27.     p2 = &line->p2;
  28.     
  29.     if (fscanf (file,"(%f,%f) to (%f,%f) : %d : %d : %d\n"
  30.         ,&p1->x, &p1->y,&p2->x, &p2->y,&line->flags
  31.         , &line->special, &line->tag) != 7)
  32.         Error ("Failed ReadLine");
  33.     
  34.     for (i=0 ; i<=  ( (line->flags&ML_TWOSIDED) != 0) ; i++)
  35.     {
  36.         s = &line->side[i];    
  37.         if (fscanf (file,"    %d (%d : %s / %s / %s )\n"
  38.             ,&s->firstrow, &s->firstcollumn, s->toptexture, s->bottomtexture, s->midtexture) != 5)
  39.             Error ("Failed ReadLine (side)");
  40.         e = &s->sectordef;
  41.         if (fscanf (file,"    %d : %s %d : %s %d %d %d\n"
  42.             ,&e->floorheight, e->floorflat, &e->ceilingheight
  43.             ,e->ceilingflat,&e->lightlevel, &e->special, &e->tag) != 7)
  44.             Error ("Failed ReadLine (sector)");
  45.         if (!strcmp (e->floorflat,"-"))
  46.             printf ("WARNING: line %i has no sectordef\n",linenum);
  47.     }
  48.     
  49.     linenum++;
  50.     
  51.     return line;
  52. }
  53.  
  54. /*
  55. =================
  56. =
  57. = ReadThing
  58. =
  59. =================
  60. */
  61.  
  62. worldthing_t *ReadThing (FILE *file)
  63. {
  64.     int                x,y;
  65.     worldthing_t    *thing;
  66.     
  67.     thing = malloc(sizeof(*thing));
  68.     memset (thing, 0, sizeof(*thing));
  69.  
  70.     if (fscanf (file,"(%i,%i, %d) :%d, %d\n"
  71.         ,&x, &y, &thing->angle, &thing->type, &thing->options) != 5)
  72.         Error ("Failed ReadThing");
  73.  
  74.     thing->origin.x = x & -16;
  75.     thing->origin.y = y & -16;
  76.     
  77.     return thing;
  78. }
  79.  
  80. /*
  81. ==================
  82. =
  83. = LineOverlaid
  84. =
  85. = Check to see if the line is colinear and overlapping any previous lines
  86. ==================
  87. */
  88.  
  89. typedef struct
  90. {
  91.     float    left, right, top, bottom;
  92. } bbox_t;
  93.  
  94. void BBoxFromPoints (bbox_t *box, NXPoint *p1, NXPoint *p2)
  95. {
  96.     if (p1->x < p2->x)
  97.     {
  98.         box->left = p1->x;
  99.         box->right = p2->x;
  100.     }
  101.     else
  102.     {
  103.         box->left = p2->x;
  104.         box->right = p1->x;
  105.     }
  106.     if (p1->y < p2->y)
  107.     {
  108.         box->bottom = p1->y;
  109.         box->top = p2->y;
  110.     }
  111.     else
  112.     {
  113.         box->bottom = p2->y;
  114.         box->top = p1->y;
  115.     }
  116. }
  117.  
  118. boolean LineOverlaid (worldline_t *line)
  119. {
  120.     int        j, count;
  121.     worldline_t    *scan;
  122.     divline_t    wl;
  123.     bbox_t        linebox, scanbox;
  124.     
  125.     wl.pt = line->p1;
  126.     wl.dx = line->p2.x - line->p1.x;
  127.     wl.dy = line->p2.y - line->p1.y;
  128.  
  129.     count = [linestore_i count];
  130.     scan = [linestore_i elementAt:0];
  131.     for (j=0 ; j<count ; j++, scan++)
  132.     {
  133.         if (PointOnSide (&scan->p1, &wl) != -1)
  134.             continue;
  135.         if (PointOnSide (&scan->p2, &wl) != -1)
  136.             continue;
  137.     // line is colinear, see if it overlapps
  138.         BBoxFromPoints (&linebox, &line->p1, &line->p2);
  139.         BBoxFromPoints (&scanbox, &scan->p1, &scan->p2);
  140.                 
  141.         if (linebox.right  > scanbox.left && linebox.left < scanbox.right)
  142.             return true;
  143.         if (linebox.bottom < scanbox.top && linebox.top > scanbox.bottom)
  144.             return true;
  145.     }
  146.     return false;
  147. }
  148.  
  149. /*
  150. ===================
  151. =
  152. = LoadDoomMap
  153. =
  154. ===================
  155. */
  156.  
  157. id    linestore_i, thingstore_i;
  158.  
  159. void LoadDoomMap (char *mapname)
  160. {
  161.     FILE         *file;
  162.     int            i, version;
  163.     int            linecount, thingcount;
  164.     worldline_t    *line;
  165.     
  166.     file = fopen (mapname,"r");
  167.     if (!file)
  168.         Error ("LoadDoomMap: couldn't open %s", mapname);
  169.     
  170.     if (!fscanf (file, "WorldServer version %d\n", &version) || version != 4)
  171.         Error ("LoadDoomMap: not a version 4 doom map");
  172.     printf ( "Loading version 4 doom map: %s\n",mapname);
  173.         
  174. //
  175. // read lines
  176. //    
  177.     if (fscanf (file,"\nlines:%d\n",&linecount) != 1)
  178.         Error ("LoadDoomMap: can't read linecount");
  179.     printf ("%i lines\n", linecount);
  180.     linestore_i = [[Storage alloc]
  181.         initCount:        0
  182.         elementSize:    sizeof(worldline_t)
  183.         description:    NULL];
  184.  
  185.     for (i=0 ; i<linecount ; i++)
  186.     {
  187.         line = ReadLine (file);
  188.         if (line->p1.x == line->p2.x && line->p1.y == line->p2.y)
  189.         {
  190.             printf ("WARNING: line %i is length 0 (removed)\n",i);
  191.             continue;
  192.         }
  193.         if (LineOverlaid (line))
  194.         {
  195.             printf ("WARNING: line %i is overlaid (removed)\n",i);
  196.             continue;
  197.         }
  198.         [linestore_i addElement: line];
  199.     }
  200.         
  201. //
  202. // read things
  203. //
  204.     if (fscanf (file,"\nthings:%d\n",&thingcount) != 1)
  205.         Error ("LoadDoomMap: can't read thingcount");
  206.     printf ( "%i things\n", thingcount);
  207.     thingstore_i = [[Storage alloc]
  208.         initCount:        0
  209.         elementSize:    sizeof(worldthing_t)
  210.         description:    NULL];
  211.         
  212.     for (i=0 ; i<thingcount ; i++)
  213.         [thingstore_i addElement: ReadThing (file)];
  214.  
  215.     fclose (file);
  216.  
  217. }
  218.  
  219.