COMMAND & CONQUER FILE FORMATS Revision 4 by Vladan Bato (bat22@geocities.com) This document explains the file formats used by Command & Conquer. Command & Conquer is a tradmark of Westwood Studios, Inc. Command & Conquer is Copyright (C)1995 Westwood Studios, Inc. The information provided here is meant for programmers that want to make editor and utilites for Command & Conquer. My explanation might not be the best one, but it should be enough. I can't guarantee that the information in here is correct. If you find any errors, please report them to me. In this document I'll use Pascal notation, and any code samples will be in Pascal.... I wanted to rewrite them in C, but I don't have the time to test the code. So, to avoid any risks, I'll use the code from Mixman. In case you don't know, the information contained here has been used to make the program Mixman, which contains a lot of conversion utilities for the various formats. For more info, check my homepage (see the end of the document). =================== 1. THE .MIX FILES =================== You probably already know the format of these files, but I will add a description here for completeness. The MIX file consists of two parts : -A header including the index of all the files contained within -A body containing all the files It's format is : Header : record NumFiles : word; {Number of files in MIX} DataSize : longint; {Size of body} Index : array [1..NumFiles] of record ID : longint; {File ID} Start : longint; {Offset of file from the start of the body} Size : longint; {file size} end; end; The ID field is computed from the original filename, which is not stored in the MIX. The records are always sorted by the ID field (the numbers are signed longints). Note that the offsets are relative to the start of the body so to find the actual offset in the MIX you have to add the size of the header which is NumFiles*12+6 =================== 2. THE .PAL FILES =================== The most easiest files.... These files contain the palette in the same format used by VGA cards. Palette : array [0..255] of record red,green,blue:byte; end; Note that only the first 6 bits of each number are used, giving a total of 262144 possible colors (as opposed to the 8 bits used by .PCX files for example). ================================= 3. THE TEMPLATE AND .BIN FILES ================================= The Template files contain the map graphics, and can be found in the theater specific MIX files (TEMPERAT.MIX, WINTER.MIX, DESERT.MIX). The .BIN files contain the maps for the missions and are used in conjunction with the .INI files. I won't explain them here. They are explained with great detail in the document titled "Command & Conquer maps" I wrote some time ago. The said document can be found on my homepage. =================== 5. THE .SHP FILES =================== The .SHP files contain almost all the graphics : units, structures, trees,... The header has the following structure : Header : record NumImages : word; {Number of images} A,B : word; {Unknown} Width, Height : word; {Width and Height of the images} C : longint; {Unknown} end; If you know something about those unknown fields, please e-mail me. Following that there's an array of records, one for each image : Offsets : array [0..NumImages+1] of record Offset : longint; {Offset and format of image in file} RefOffs : longint; {Offset and format of image on which it is based} end; The most significant byte (last) of the Offset and RefOffs fields contains the format, while the lower three are used for the offset. The format byte can have one of the three values : 80h, 40h, 20h. I will call the three image formats Format80, Format40 and Format20. The Format80 images are compressed with a compression method I'll explain later. The Format40 images must be xor-ed with a Format80 image. That's what the RefOffs field is used for. It tells which Format80 image they are based upon. The Format40 will be explained in detail later. The Format20 images use the same format as the Format40, the difference is that they are xor-ed with the image that precedes them in the file. That can be either in Format20 or in Format40. The offset part of the RefOffs field contains the number of the first Format40 image in the chain, and the format field is always 48h. Here's an example : 0) Off0(three bytes) 80h 000000h 00h 1) Off1(three bytes) 80h 000000h 00h 2) Off2(three bytes) 40h Off1 80h 3) Off3(three bytes) 80h 000000h 00h 4) Off4(three bytes) 40h Off1 80h 5) Off5(three bytes) 20h 000400h 48h 6) Off6(three bytes) 20h 000400h 48h 7) Off7(three bytes) 40h Off3 80h For example to draw image 7, you have to draw the image 3 first (whose offset and format are given) and then xor image 7 over it. To draw image 6, you have to xor it over the previous image, i.e. 5, which is format20 again, that means that it has to be xor-ed over image 4, which is in format40, i.e. it must be xor-ed over the image in format80 it has a reference to. In this case it's image 1. Thus the chain is 1,4,5,6. This is one way to see it, the other could be : Image 6 is in Format20, the RefOffs field contains the number of the first Format40 image in the chain, in this case image 4. To draw Image 4, the Image 1 has to be drawn first, next is image 4, and then all the images from the 4th to the 6th have to be xor-ed over the previous. I made some experiments and found out that you don't have to use the Format40 and Format20 images. I tried converting all of them into Format80 and it worked. Also, when changing graphics, note that all the unit and structure graphics should be drawn using the GDI colors, which will be automatically converted for the other sides. The palette you should use is one of those found in DESERT.MIX, WINTER.MIX and TEMPERAT.MIX. The GDI colors are colors 0B0h-0BFh. The other colors won't be converted and will remain the same for all the sides (be sure to use only the colors that are the same all three palettes). The above applies only to the graphics that appear in all three theaters (the .SHP file found in CONQUER.MIX). The graphics for the structures and overlays that appear in a single theater (found inside the theater specific MIX) can use the palette entries that are unique for that theater (and will be shown with garbled colors in the others). Also a special color is used for shadows. It's color 04h. In the palettes it's bright green, but C&C puts a shadow instead of it. I don't know how the shadows are calculated however. You should've noticed that the array has NumImages+2 elements when only NumImages elements are needed. The last one contains zeros, and the one before that points to the end of the file. These two can be used to identify the file as a .SHP. Here's the description of the compression formats : Format80 and Format40. ---------- Format80 ---------- There are several different commands, with different sizes : form 1 to 5 bytes. The positions mentioned below always refer to the destination buffer (i.e. the uncompressed image). The relative positions are relative to the current position in the destination buffer, which is one byte beyond the last written byte. I will give some sample code at the end. (1) 1 byte +---+---+---+---+---+---+---+---+ | 1 | 0 | | | | | | | +---+---+---+---+---+---+---+---+ \_______________________/ | Count This one means : copy next Count bytes as is from Source to Dest. (2) 2 bytes +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | 0 | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ \___________/\__________________________________________________/ | | Count-3 Relative Pos. This means copy Count bytes from Dest at Current Pos.-Rel. Pos. to Current position. Note that you have to add 3 to the number you find in the bits 4-6 of the first byte to obtain the Count. Note that if the Rel. Pos. is 1, that means repeat Count times the previous byte. (3) 3 bytes +---+---+---+---+---+---+---+---+ +---------------+---------------+ | 1 | 1 | | | | | | | | | | +---+---+---+---+---+---+---+---+ +---------------+---------------+ \_______________________/ Pos | Count-3 Copy Count bytes from Pos, where Pos is absolute from the start of the destination buffer. (Pos is a word, that means that the images can't be larger than 64K) (4) 4 bytes +---+---+---+---+---+---+---+---+ +-------+-------+ +-------+ | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | | | | | | +---+---+---+---+---+---+---+---+ +-------+-------+ +-------+ Count Color Write Color Count times. (Count is a word, color is a byte) (5) 5 bytes +---+---+---+---+---+---+---+---+ +-------...
TolekBananas