home *** CD-ROM | disk | FTP | other *** search
/ FMI Superhry 1 / Superhry-I.bin / bonus / doom / programs / dmwbsp / source / doomload.c < prev    next >
Text File  |  1994-06-02  |  7KB  |  266 lines

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