home *** CD-ROM | disk | FTP | other *** search
/ Doom 2 Explosion / Doom2Explosion.bin / doom2exp / programs / ibsp101s / doomload.c < prev    next >
C/C++ Source or Header  |  1994-07-10  |  7KB  |  277 lines

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