home *** CD-ROM | disk | FTP | other *** search
/ 4-Ever Doomed / 4-everdoomedcypresssoftware1994.iso / text / wadfile.txt < prev   
Text File  |  1994-03-01  |  19KB  |  557 lines

  1.  
  2. 4/1/94 Chris Thomas
  3.  
  4. DOOM WAD File Format
  5. --------------------
  6.  
  7. ********** DISCLAIMER ******************** DISCLAIMER ***********************
  8.   In no event shall I be liable for any errors contained herein or for 
  9.   incidental or consequential damages in connection with the furnishing, 
  10.   performance, or use of this information.
  11.  
  12.   Feel free to do what you want with this information.  Copy it freely,
  13.   in any media you desire, provided that this disclaimer notice and my
  14.   name remain intact.
  15.  
  16. * 4/1/94 Chris Thomas, CSID [75410,2627].
  17. ********** DISCLAIMER ******************** DISCLAIMER ***********************
  18.  
  19.  
  20. This is an attempt to write down everything I have learned/guessed about
  21. the DOOM WAD file format.  I don't know how correct some of this is, as I
  22. have yet to construct new levels <g>.  I am writing this in a hurry, so
  23. please ignore spelling misteaks <g>.
  24.  
  25. BTW, I bought DOOM.  Wouldn't consider mucking with it otherwise.  Also
  26. I must thank Id for their willingness to let people hack.  Thank you Id.
  27.  
  28. I starting figuring out the format with the goal of writing a DOOM level
  29. editor, with a raycasting level previewer.  This is an ambitious goal.
  30. I have since shelved the project, hopefully someday I will start it up
  31. again, but I doubt it.  However I am becoming bored with DOOM, so perhaps 
  32. this boredom might spark some renewed interest in a level editor.  I shelved
  33. it because it is a lot of work for little return.  Sure, I could see my name
  34. up in lights on CompuServe, as the "Level Editor Guru", and I would be
  35. famous with all 5 of the forum regulars for about 15 minutes.  Fame does
  36. not interest me.  Money might, but this is Id's product and I wouldn't feel
  37. right about making money off their product (without their endorsement <g>).
  38. Charging money for it implies tech support etc.  Blah.
  39.  
  40. Anyhow, I started this effort by downloading some of the existing DOOM
  41. tools from Cserve.  WADTLS.ZIP and IWADDU.ZIP stick out as being particularly
  42. helpful.  IWADDU (I think that's it...) contained a .H file that the 
  43. author had used for that tool, and I was able to use that information
  44. for the WAD file.  WADTLS (I think...) contained a .DOC file with even
  45. more bits of information.
  46.  
  47. Enough background blather, here it goes....
  48.  
  49.  
  50.  
  51. The basic WAD format
  52. --------------------
  53.  
  54. The WAD file is referred to as a "resource file".   It contains a list 
  55. of resources, stored as chunks of binary data, and a directory table
  56. listing the name and location of each of these resources.
  57.  
  58. The WAD file starts out with a 4 character signature, followed by the
  59. number of directory entries (a long), followed by the directories file
  60. offset.  For the registered real version the signature is IWAD.  I don't
  61. know if the shareware version has a different signature.
  62.  
  63.         typedef struct {      
  64.                 char signature[4];    //DOOM WAD signature
  65.                 long num_entries;    //number of directory entries
  66.                 long offset;        //file offset of directory
  67.         } WAD_Header;
  68.  
  69. The resource data starts immediately after this header.
  70.  
  71.  
  72.  
  73.  
  74. Directory Table
  75. ---------------
  76.  
  77. The directory table is list of structures, each consisting of a file offset,
  78. byte size, and a resource name.
  79.  
  80.         typedef struct {
  81.            long offset;            //file offset of this resource
  82.            long size;            //byte size of this resource
  83.            char name[8];        //this resource name, always 8
  84.                     //chars, padded out with zeros.  Can
  85.                     //be exactly 8, in which case there
  86.                     //is no trailing zero.
  87.         } DirectoryEntry; 
  88.  
  89. That's all to this.  BTW, I just malloc() and read in the entire directory,
  90. as it is not all that big.  I'm using large model for my C programs.  In 
  91. fact, there appear to be no resources greater than 64k, so you can malloc 
  92. and read all the resources you want, until you run out of memory.
  93.  
  94. If you were to use a byte editor to view the directory, you would see
  95. something like this (showing only the resource names):
  96.  
  97.         PLAYPAL
  98.         COLORMAP
  99.         ENDOOM
  100.         DEMO1
  101.         DEMO2
  102.         DEMO3
  103.         E1M1            <- game 1, level 1
  104.         THINGS
  105.         LINEDEFS
  106.         SIDEDEFS
  107.         VERTEXES
  108.         SEGS
  109.         SSECTORS
  110.         NODES
  111.         SECTORS
  112.         REJECT
  113.         BLOCKMAP
  114.         E1M2            <- game 1, level 2
  115.         THINGS
  116.         LINEDEFS
  117.         SIDEDEFS
  118.         VERTEXES
  119.         SEGS
  120.         SSECTORS
  121.         NODES
  122.         SECTORS
  123.         REJECT
  124.         BLOCKMAP
  125.         
  126.         .
  127.         .               <- skip a whole bunch
  128.         .
  129.  
  130.         SECTORS
  131.         REJECT
  132.         BLOCKMAP        <- tail end of last game, last level
  133.         TEXTURE1        <- start of non-level data.
  134.         TEXTURE2
  135.         PNAMES
  136.         GENMIDI
  137.  
  138.         .
  139.         .                               <- rest of non-level data.  all 
  140.         .                                  bitmaps are listed here.
  141.  
  142.  
  143.  
  144. Each level is defined by the resource sequence:
  145.         
  146.         ExMy            <- game x, level y
  147.         THINGS
  148.         LINEDEFS
  149.         SIDEDEFS
  150.         VERTEXES
  151.         SEGS
  152.         SSECTORS
  153.         NODES
  154.         SECTORS
  155.         REJECT
  156.         BLOCKMAP
  157.         
  158. Let's examine each of these resource types in more detail.
  159.  
  160.  
  161. Global Data
  162. -----------
  163.  
  164.         PLAYPAL
  165.         COLORMAP
  166.         ENDOOM
  167.         DEMO1
  168.         DEMO2
  169.         DEMO3
  170.  
  171. What else to call these?  I haven't figured out these yet, but I think
  172. that PLAYPAL is a list of palettes (256 red,green,blue triplets?), each
  173. used for various situations in the game (ie, radiation suit, beserker mode,
  174. gun flashes?).  COLORMAP is a list of color index permutations, one list 
  175. for different game situations (?).  Many thanks to Mike Rice for posting 
  176. the PLAYPAL and COLORMAP info.
  177.  
  178. ENDOOM?  Beats me. 
  179. DEMO1, 2, 3?  Must be recorded demos, I guess.
  180.  
  181.  
  182.  
  183. Level Data
  184. ----------
  185.  
  186. A level is defined with these resources:
  187.  
  188.         ExMy
  189.         THINGS
  190.         LINEDEFS
  191.         SIDEDEFS
  192.         VERTEXES
  193.         SEGS
  194.         SSECTORS
  195.         NODES
  196.         SECTORS
  197.         REJECT
  198.         BLOCKMAP
  199.  
  200. Let's dissect each of these resources.  They are too complex to explain
  201. with one sentence each, so I will explain each in turn.  If I leave a 
  202. structure's field uncommented that usually means I don't know what the
  203. field means.  
  204.  
  205. I'm sorry for the haphazard arrangement of this information.  This is very
  206. much like a chicken-egg thing, in that many of these resources are 
  207. intertwined and linked in interesting ways.
  208.  
  209.  
  210. ExMy
  211.         Marker for game x level y.  I don't know if this is just a 
  212.         placeholder in the directory list, or if this is a real resource
  213.         with real data.
  214.  
  215.  
  216. THINGS
  217.         Resource consists of a list of "sprites", one for each enemy.
  218.         They are defined with a x,y coordinate, a facing angle, and some
  219.         flag data.
  220.  
  221.         typedef struct {
  222.            short x,y;         //not VERTEX indicies!
  223.            short angle;       //facing angle.  degrees not fractional.
  224.            short type;
  225.            short options;
  226.         } THINGS;
  227.  
  228.     I don't know how sequences of animated things are specified with
  229.     this scheme.  Obviously something like an imp is just a sequence
  230.     of bitmaps, with animation accomplished by drawing at various
  231.     zoom levels and mirrored to walk the other way if needed.
  232.  
  233.     These can also be transparent bitmaps.  I don't know if DOOM 
  234.     just uses color 0 for the transparent key, or if it is programmable
  235.     via the bitmap resource format.   There also might be a mask table
  236.     associated with each bitmap.
  237.  
  238.  
  239. LINEDEFS
  240.         Resource consists of a list of lines used to define walls.
  241.  
  242.         typedef struct {
  243.                 short from,to;          //indices into VERTEX list.
  244.                 short flags,special,tag; 
  245.                 short side1,side2;    //indices into SIDEDEFS list.
  246.         } Line;
  247.  
  248.     flags might be for doors, etc.
  249.  
  250.     side1, side2 are indices into the SIDEDEFS list.  One for each
  251.     "side" of the wall?  side1 is always filled, but side2 sometimes
  252.     has a -1, meaning no bitmap on that side?
  253.  
  254.     Perhaps walls with filled side1 and side2 are connecting walls,
  255.     perhaps connecting sectors (and nodes?)?  See below.
  256.  
  257.  
  258. SIDEDEFS
  259.         Resource consists of a list of wall texture definitions.  
  260.  
  261.         typedef struct {
  262.         short flags1;
  263.         short flags2;
  264.         char texture1[8];    //name of first bitmap resource
  265.         char texture2[8];    //name of second bitmap resource
  266.         char texture3[8];    //name third bitmap resource
  267.         short sector_index;    //index into SECTOR list.
  268.  
  269.     sector_index specifies the sector for which this SIDEDEF belongs to.
  270.     The SECTOR specifies the floor and ceiling attributes of an area
  271.     bounded by SIDEDEFS.  See SECTOR below.
  272.  
  273.  
  274. VERTEXES
  275.         Resource consists of x,y coordinates.  
  276.  
  277.         typedef struct {
  278.                 short x,y;
  279.         } Vertex;
  280.  
  281.  
  282. SEGS
  283.         Resource consists of a list of walls.  
  284.  
  285.         typedef struct {
  286.             short from,to;          //indices into VERTEX list.
  287.             unsigned short angle;   //fractional angle, wall is facing.
  288.             short linedef;          //LINEDEF index for this line.
  289.             short flag;             //0 for visible, 1 for invisible.
  290.                     //or perhaps 0 for solid, 1 for open?
  291.             short flag_unknown;        //doors? (usually zero).
  292.         } SEGS;
  293.  
  294.         Angles in DOOM are 16-bit words representing a 16-bit fraction
  295.         (with 180 degrees being 0.5, 90 degrees being 0.25, etc).
  296.         Facing the screen (in a normal sitting position <g>), 0x0000 is
  297.         down, 0x4000 is right, 0x8000 is up, 0xC000 is right.  Therefore
  298.         0x4000 is really 0x4000/0x10000 = 0.25.  0.25 * 360.0 = 90.0. QED.
  299.  
  300.  
  301. SSECTORS
  302.         Resource consists of a list of sectors.   Sectors are areas that
  303.         share a floor and ceiling textures.  Open areas in DOOM, such as
  304.         the acid pit room in game 1 level 1, are modeled using multiple
  305.         sectors.  This resource identifies sectors by specifiying the
  306.         sector starting index (in SEGS) and the number of "edges"
  307.         (counting from the starting index in SEGS).
  308.  
  309.         typedef struct {
  310.             short count;
  311.             short index;
  312.         } SSECTORS;
  313.  
  314.     This table is used by the NODES resource,  see NODES below.
  315.  
  316.  
  317. NODES
  318.         Resource consists of a list of structures that collectively define
  319.         a hierarchical "space" tree.
  320.  
  321.         typedef struct {
  322.                 short x,y;     
  323.                 short dx,dy;
  324.                 short bound[8];   //maxy,miny,minx,maxx, maxy,miny,minx,maxx 
  325.                 unsigned short index1,index2;
  326.         } NODES;
  327.  
  328.         This resource is quite complex to explain, so I will defer
  329.         explaining it until later (see "More about NODES" later).
  330.  
  331.  
  332. SECTORS
  333.         Resource consists of a list of sectors.  Each entry describes 
  334.         the properties of a single sector.  These properties include
  335.         floor height, ceiling height, floor bitmap texture, ceiling
  336.         bitmap texture.
  337.  
  338.     typedef struct {
  339.         short floor_height;    //
  340.         short ceiling_height;    //
  341.         char floor_texture[8];    //bitmap resource for floor
  342.         char ceiling_texture[8];  //bitmap resource for ceiling
  343.         short light;        //initial palette AND mask for this
  344.                     //sector area?
  345.         short flag1;        //crushing ceilings, etc?  (mostly
  346.                       zeros).
  347.         short flag2;        //the exit flag?  (in E1M1, all zeros
  348.                       except for 1 entry).
  349.     } SECTORS;
  350.  
  351.     This describes an area with a floor and ceiling with certain
  352.     attributes, including an initial lighting factor.
  353.         
  354.         
  355.  
  356.  
  357. REJECT
  358.         Unknown.  I have no idea.  Using my byte editor I filled this
  359.         entire resource with 0x00.  No effect on DOOM.  I also tried 0x30,
  360.         0xFF, also no effect.  However, network play seems to be affected,
  361.         so perhap this stores the DEATHMATCH teleport coordinates, etc?
  362.  
  363.  
  364. BLOCKMAP
  365.         Resource is in two parts.  The first is a 2d array:
  366.  
  367.                 {
  368.                         short w,h;
  369.                         short x[w*h];
  370.                 }
  371.  
  372.         Following this array is a list of numbers.  The array represents
  373.         a grid for the level, on a 128 pixel grid size.  Each element in 
  374.         the array is a near pointer to a list of LINEDEF indices that
  375.         originate or cross that particular 128x128 block in the map.
  376.         The LINEDEF lists always start with a 0 and end with a 0xffff,
  377.         even for empty lists.  Therefore DOOM's empty lists are always
  378.         0, 0xffff.  A non-empty DOOM list might be 0, 100, 101, 0xffff.  
  379.         This structure appears to be used only for clipping.  Modifying
  380.         it has no effect on the display of the model, only in the clipping.  
  381.         One can walk through walls, and bullets pass right through.
  382.  
  383.         Using BLOCKMAP's linedefs DOOM might figure the players position 
  384.         before the move, then figure the position after the move.  If the 
  385.         player crosses a wall, or comes too close (a more likely criteria), 
  386.         the move is prohibited.  It could use fixed point math with a 7-bit 
  387.         fraction, which would automatically index them into this array.  But 
  388.         then a single unit move would be 1/128 of the grid size, which 
  389.         actually seems to be a reasonably small unit given that bitmaps 
  390.         are 64x64.  But a better choice would be 8-bit, or better yet 16-bit,
  391.         for ease of programming.
  392.  
  393.         The array "near pointer" elements are actually WORD indices from
  394.         the start of the array, so they have to munged slightly before
  395.     use.
  396.  
  397.  
  398.  
  399. More about NODES
  400. ----------------
  401.  
  402. [note: I had written this long before I started this DOC file, so if it
  403. appears a little out of place that's because it is!]
  404.  
  405. This is how I think it works.  The problem for DOOM is to quickly
  406. generate a view given the players coordinates, a problem that
  407. screams for a hierarchical "quadtree" type of approach.  What
  408. the tree helps find is the "SSECTOR" information for whatever
  409. sectors the player is in or near.  SSECTOR is a list of these
  410. structures:  
  411.  
  412.    struct {
  413.         short count;     //number of sides
  414.         short index;     //index in SEGS of the first side.
  415.    }
  416.  
  417. Using this DOOM can render the image in "sector" chunks, using
  418. the view angle info in SEGS to reject walls that are not facing
  419. the player.
  420.  
  421. Anyway, the sectors are organized using the NODES structures.
  422. Node element index1 is associated with the bounds[0..3] and
  423. element index2 is associated with the bounds[4..7].  If an
  424. index high bit (0x8000) is set, then the corresponding 
  425. bounds[] is a tree leaf, representing the bounding box for
  426. the sector SSECTOR[index].  If the high bit is clear, then
  427. the corresponding bounds[] is a tree node, representing the
  428. bounding box for the entire referenced NODE[index] (that is
  429. the min/max for all bounds[0..8] in NODE[index]).
  430.  
  431. The last node is the root node in the tree.
  432.  
  433. Using game 1, level 1, here is the first node from NODES:
  434.  
  435. Node index 0:
  436. x,y,dx,dy:      2632 -3792    56  -128
  437. bounds[0..8]:  -3792 -3920  2435  2688 -3776 -3920  2632  2720
  438. index1,index2:  8001 8002
  439.  
  440. This a pure leaf node, with bound[0..3], -3792 -3920  2435  2688,
  441. the bounding box for SSECTOR[1], and with bound[4..7],
  442. -3776 -3920  2632  2720,  the bounding box for SSECTOR[2].
  443.  
  444. Now here's node 3, index 2, from NODES:
  445.  
  446. Node index 2:
  447. x,y,dx,dy:     2688 -3776    32  -128
  448. bounds[0..8]:  -3776 -3920  2435  2720 -3776 -3904  2688  2784
  449. index1,index2:    0    1
  450.  
  451. This is a pure node node<g>.  Note that index1 has the high
  452. bit clear, indicating bounds[0..3] is the bounding box for
  453. all of node index 0.  Sure enough, -3776 -3920  2435  2720,
  454. is the bounding box for all of node 0's bound[0..8],
  455. -3792 -3920  2435  2688 -3776 -3920  2632  2720.
  456.  
  457. If you checked index2, 1, you'd see the same thing.  And there
  458. are also mixed nodes, with the one child a leaf and the other
  459. a node.  NODE index 3 is such a mixed node, with index1==8000
  460. and index2==2.
  461.  
  462. BTW, I wrote a program to verify this, and it verified 100%.
  463.  
  464. I'm not sure about the node x,y,dx,dy stuff.  x,y appears to 
  465. be a point on the intersection of bound[0..3] and bound[4..7],
  466. inside the bounds of the entire node.  I don't know about
  467. dx and dy.
  468.  
  469. Here are some more observations about NODES:
  470.  
  471.         * each x,y entry is also defined as a VERTEX.
  472.         
  473.         * it appears, and I have not really verified this, that x,y,dx,dy
  474.           defines a SECTOR wall that is marked as invisible.  This
  475.           would link "visible" sectors together by their common "invisible"
  476.           walls (ie, two rooms connected by a window would have the
  477.           common window wall being a "node" in this tree).  This is all
  478.           rather nebulous at this point.
  479.  
  480.  
  481.  
  482. Game 1 Level 1 Information
  483. --------------------------
  484.  
  485. All of my data gathering has been done using E1M1.  My initial work
  486. was done using the shareware version, and then I moved up to the 
  487. retail version as soon as it arrived.
  488.  
  489. Here are some numbers from this level, for the number of entries in each
  490. resource.  I might be off a little, but these will give you a rough idea.
  491.  
  492.         THINGS        130
  493.         LINEDEFS    452
  494.         SIDEDEFS    622
  495.         VERTEXES     437
  496.         SEGS        695
  497.         SSECTORS    212
  498.         NODES        211
  499.         SECTORS        83
  500.         REJECT        no data collected on this puppy.
  501.         BLOCKMAP    a 34x23 block array, followed by mucho data.
  502.  
  503.  
  504.  
  505. Curios and Other Cool Stuff
  506. ---------------------------
  507.  
  508. Some of the DOOM cheat keys are really interesting:
  509.  
  510.         IDMYPOS    - DOOM displays current angle and x,y information.
  511.         IDSPISPOPD - disables clipping.  interesting when trying to 
  512.                      figure out what DOOM is drawing (or not drawing<g>!)
  513.  
  514.  
  515.  
  516. What I don't Know
  517. -----------------
  518.  
  519. Here is a list of what I don't know about the WAD format:
  520.  
  521.         * how would I insert my own level data?  I know I could just simply
  522.           replace an entire level with my own, but how would I add another
  523.           level.
  524.  
  525.         * how does one determine if DOOM is shareware or not?  Id doesn't
  526.           want people providing level editors for the shareware version,
  527.           and I certainly agree with that.
  528.  
  529.         * More about PLAYPAL, COLORMAP resources.
  530.  
  531.         * The D_E1M1,D_E1M2,etc, resources.  More level information?
  532.           These appear shortly after the end of the last game's level's
  533.           resources.
  534.  
  535.         * The exact format of a bitmap resource.  I'm assuming something
  536.           like the following:
  537.  
  538.                 short image_w;
  539.                 short image_h;
  540.                 byte image_data[];
  541.  
  542.           Data stored in column major format.  But I haven't even looked.
  543.  
  544.  
  545.  
  546. What Now?
  547. ---------
  548.  
  549. There!  That's everything I know about the WAD format.
  550.  
  551. Now What?  Well, I just don't know.  Perhaps one of you will take this 
  552. to the next level (!).
  553.  
  554. Good Luck!
  555.  
  556.  
  557.