home *** CD-ROM | disk | FTP | other *** search
/ PC Home 32 / PC_HOME_95-uncompressed-cd-image.iso / doom / dm2con / dm2conv.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-01-20  |  48.1 KB  |  1,723 lines

  1. {$A+,B-,D+,E+,F-,G+,I-,L+,N-,O-,P-,Q-,R-,S-,T-,V-,X+}
  2. {$M 16384,0,655360}
  3. { DM2CONV v1.6ß by Vincenzo Alcamo }
  4. { This program is Public Domain   }
  5. type
  6.   shortname = array[1..3] of char;
  7.   dname = array[1..8] of char;
  8.   p_string = ^string;
  9.   obj = record
  10.     id : integer;
  11.     sname : shortname;
  12.     name : p_string
  13.   end;
  14.   errors = (ERR_OPENS,ERR_READS,ERR_OPEND,ERR_WRITED,ERR_PWAD,
  15.             ERR_TOOENTRY,ERR_TOOMAPS,ERR_NOMAPS,ERR_NOEQ,ERR_BADEND,
  16.             ERR_BADNUM,ERR_NOMEM,ERR_OPEN,ERR_READ);
  17.   header= record
  18.     Sig   : Longint;
  19.     Num   : Longint;
  20.     Start : Longint;
  21.   end;
  22.   entry = record
  23.     Start : Longint;
  24.     RSize : Longint;
  25.     Name  : dname;
  26.   end;
  27.   thing = record
  28.     xpos : integer;
  29.     ypos : integer;
  30.     angle: integer;
  31.     code : integer;
  32.     flags: integer;
  33.   end;
  34.   sidedef = record
  35.     x,y  : integer;
  36.     a,b,c: dname;
  37.     sect : integer;
  38.   end;
  39.   sector = record
  40.     y1,y2: integer;
  41.     a,b  : dname;
  42.     l,f,t: integer;
  43.   end;
  44.   repname = record
  45.     before : dname;
  46.     after  : dname;
  47.   end;
  48.   repname_array = array[1..1024] of repname;
  49.   p_repname_array = ^repname_array;
  50.  
  51.  
  52. const
  53.   show_list : boolean = false;
  54.   show_example: boolean = false;
  55.   show_help : boolean = false;
  56.   show_note : boolean = false;
  57.   nocheck   : boolean = false;
  58.   debug     : boolean = false;
  59.   ignore    : boolean = false;
  60.   do_texture: boolean = false; {remap wall textures}
  61.   do_floor  : boolean = false; {remap floor textures}
  62.   remapping : boolean = false; {remap levels}
  63.   heretic   : boolean = false; {heretic mode}
  64.   savedir   : boolean = false; {save directory entries}
  65.   no_conv   : boolean = false; {no conversion}
  66.   remap_lev : integer = 1;
  67.   remap_mus : integer = 0;
  68.   replaces  : integer = 0;
  69.   BUFFSIZE = 60000;
  70.   MAXENTRY = BUFFSIZE div sizeof(entry);
  71.   MAXTHING = BUFFSIZE div sizeof(thing);
  72.   MAXSIDES = BUFFSIZE div sizeof(sidedef);
  73.   MAXSECS  = BUFFSIZE div sizeof(sector);
  74.  
  75.   IWAD_SIG = Ord('I')+(Ord('W')+(Ord('A')+Ord('D') shl 8) shl 8) shl 8;
  76.   PWAD_SIG = Ord('P')+(Ord('W')+(Ord('A')+Ord('D') shl 8) shl 8) shl 8;
  77.   N_THINGS = 'THINGS'#0#0;
  78.   N_SECTORS= 'SECTORS'#0;
  79.   N_SIDEDEFS='SIDEDEFS';
  80.   NULL_NAME= #0#0#0#0#0#0#0#0;
  81.  
  82.   REP_PERCENT=16384;
  83.   MAXREP=1024;
  84.  
  85.   mnames : array[1..32] of dname =  (
  86.     'D_RUNNIN',
  87.     'D_STALKS',
  88.     'D_COUNTD',
  89.     'D_BETWEE',
  90.     'D_DOOM'#0#0,
  91.     'D_THE_DA',
  92.     'D_SHAWN'#0,
  93.     'D_DDTBLU',
  94.     'D_IN_CIT',
  95.     'D_DEAD'#0#0,
  96.     'D_STLKS2',
  97.     'D_THEDA2',
  98.     'D_DOOM2'#0,
  99.     'D_DDTBL2',
  100.     'D_RUNNI2',
  101.     'D_DEAD2'#0,
  102.     'D_STLKS3',
  103.     'D_ROMERO',
  104.     'D_SHAWN2',
  105.     'D_MESSAG',
  106.     'D_COUNT2',
  107.     'D_DDTBL3',
  108.     'D_AMPIE'#0,
  109.     'D_THEDA3',
  110.     'D_ADRIAN',
  111.     'D_MESSG2',
  112.     'D_ROMER2',
  113.     'D_TENSE'#0,
  114.     'D_SHAWN3',
  115.     'D_OPENIN',
  116.     'D_EVIL'#0#0,
  117.     'D_ULTIMA');
  118.  
  119. var
  120.   objects    : array[1..55] of obj;
  121.   replace    : array[1..MAXREP] of word;
  122.   numobjects : integer;
  123.   source     : string;
  124.   dest       : string;
  125.   datafile   : string;
  126.   buffer     : array[1..BUFFSIZE] of byte;
  127.   dirlist    : array[1..MAXENTRY] of entry absolute buffer;
  128.   things     : array[1..MAXTHING] of thing absolute buffer;
  129.   sidedefs   : array[1..MAXSIDES] of sidedef absolute buffer;
  130.   sectors    : array[1..MAXSECS] of sector absolute buffer;
  131.   numentry   : integer;
  132.   maxside    : integer;
  133.  
  134.   reptexture : p_repname_array;
  135.   nreptexture: integer;
  136.   repfloor   : p_repname_array;
  137.   nrepfloor  : integer;
  138.   repdirs    : p_repname_array;
  139.   nrepdirs   : integer;
  140.  
  141.   repside    : word;
  142.   repfloo    : word;
  143.   repthing   : word;
  144.   replev     : word;
  145.  
  146. procedure adjust_name(var name:dname); assembler;
  147.   asm
  148.     cld
  149.     les di, name
  150.     mov cx, 8
  151.     mov al, 32
  152.     repne scasb
  153.     jnz @@FINE
  154.     xor ax, ax
  155.     dec di
  156.     inc cx
  157.     rep stosb
  158. @@FINE:
  159.   end;
  160.  
  161. procedure CopyTable(table:p_repname_array;source:p_repname_array;var num:integer);
  162.   var i,j,k:integer;
  163.       name:dname;
  164.   begin
  165.     i:=1;
  166.     j:=num;
  167.     while source^[i].before[1]<>#0 do begin
  168.       name:=source^[i].before;
  169.       adjust_name(name);
  170.       k:=1;
  171.       while (k<=j) and (table^[k].before<>name) do inc(k);
  172.       if (k>j) and (num<1024) then begin
  173.         inc(num);
  174.         table^[num].before:=name;
  175.         table^[num].after:=source^[num].after;
  176.         adjust_name(table^[num].after);
  177.       end;
  178.       inc(i);
  179.     end;
  180.   end;
  181.  
  182. function remap_name(table:p_repname_array;var name:dname;num:integer):integer; assembler;
  183.   asm
  184.     cld
  185.     les di, name
  186.     mov cx, 8
  187.     mov al, 0
  188.     repne scasb
  189.     jnz @@OK
  190.     dec di
  191.     inc cx
  192.     rep stosb
  193. @@OK:
  194.     push ds
  195.     lds si, name
  196.     les di, table
  197.     mov cx, num
  198.     cld
  199.     lodsw
  200.     mov bx, [si]
  201.     mov dx, [si+2]
  202.     mov si, [si+4]
  203. @@CICLO:
  204.     scasw
  205.     jnz @@NEXT
  206.     cmp bx, es:[di]
  207.     jnz @@NEXT
  208.     cmp dx, es:[di+2]
  209.     jnz @@NEXT
  210.     cmp si, es:[di+4]
  211.     jnz @@NEXT
  212.     mov ax, es
  213.     mov ds, ax
  214.     mov si, di
  215.     add si, 6
  216.     les di, name
  217.     mov cx, 8
  218.     rep movsb
  219.     mov ax, 1
  220.     jmp @@FINE
  221. @@NEXT:
  222.     add di, 14
  223.     loop @@CICLO
  224.     xor ax, ax
  225. @@FINE:
  226.     pop ds
  227.   end;
  228.  
  229. procedure texture_table; assembler;
  230.   asm
  231.     {TABLE OF TEXTURE REPLACEMENTS FOR DOOM}
  232.     DB 'AASTINKYDOORSTOP'
  233.     DB 'ASHWALL ASHWALL2'
  234.     DB 'BLODGR1 PIPE6   '
  235.     DB 'BLODGR2 PIPE6   '
  236.     DB 'BLODGR3 PIPE6   '
  237.     DB 'BLODGR4 PIPE6   '
  238.     DB 'BRNBIGC MIDGRATE'
  239.     DB 'BRNBIGL MIDGRATE'
  240.     DB 'BRNBIGR MIDGRATE'
  241.     DB 'BRNPOIS2BROWN96 '
  242.     DB 'BROVINE BROWN1  '
  243.     DB 'BROWNWELBROWNHUG'
  244.     DB 'CEMPOIS CEMENT1 '
  245.     DB 'COMP2   COMPTALL'
  246.     DB 'COMPOHSOCOMPWERD'
  247.     DB 'COMPTILECOMPWERD'
  248.     DB 'COMPUTE1COMPSTA1'
  249.     DB 'COMPUTE2COMPTALL'
  250.     DB 'COMPUTE3COMPTALL'
  251.     DB 'DOORHI  TEKBRON2'
  252.     DB 'GRAYDANGGRAY5   '
  253.     DB 'ICKDOOR1DOOR1   '
  254.     DB 'ICKWALL6ICKWALL5'
  255.     DB 'LITE2   BROWN1  '
  256.     DB 'LITE4   LITE5   '
  257.     DB 'LITE96  BROWN96 '
  258.     DB 'LITEBLU2LITEBLU1'
  259.     DB 'LITEBLU3LITEBLU1'
  260.     DB 'LITEMET METAL1  '
  261.     DB 'LITERED DOORRED '
  262.     DB 'LITESTONSTONE2  '
  263.     DB 'MIDVINE1MIDGRATE'
  264.     DB 'MIDVINE2MIDGRATE'
  265.     DB 'NUKESLADSLADWALL'
  266.     DB 'PLANET1 COMPSTA2'
  267.     DB 'REDWALL1REDWALL '
  268.     DB 'SKINBORDSKINMET1'
  269.     DB 'SKINTEK1SKINMET2'
  270.     DB 'SKINTEK2SKINMET2'
  271.     DB 'SKULWAL3SKSPINE1'
  272.     DB 'SKULWALLSKSPINE1'
  273.     DB 'SLADRIP1SLADSKUL'
  274.     DB 'SLADRIP2SLADSKUL'
  275.     DB 'SLADRIP3SLADSKUL'
  276.     DB 'SP_DUDE3SP_DUDE4'
  277.     DB 'SP_DUDE6SP_DUDE4'
  278.     DB 'SP_ROCK2SP_ROCK1'
  279.     DB 'STARTAN1STARTAN2'
  280.     DB 'STONGARGSTONE3  '
  281.     DB 'STONPOISSTONE   '
  282.     DB 'TEKWALL2TEKWALL1'
  283.     DB 'TEKWALL3TEKWALL1'
  284.     DB 'TEKWALL5TEKWALL1'
  285.     DB 'WOODSKULWOODGARG'
  286.     DB 0
  287.   end;
  288.  
  289. procedure htexture_table; assembler;
  290.   asm
  291.     {TABLE OF TEXTURE REPLACEMENTS FOR HERETIC}
  292.     DB 'AASTINKYREDWALL '
  293.     DB 'ASHWALL SQPEB1  '
  294.     DB 'BIGDOOR1DOORSTON'
  295.     DB 'BIGDOOR2GRSKULL2'
  296.     DB 'BIGDOOR3GRSKULL3'
  297.     DB 'BIGDOOR4SKULLSB2'
  298.     DB 'BIGDOOR5DOORWOOD'
  299.     DB 'BIGDOOR6DOORWOOD'
  300.     DB 'BIGDOOR7SKULLSB2'
  301.     DB 'BLODGR1 SPINE2  '
  302.     DB 'BLODGR2 SPINE2  '
  303.     DB 'BLODGR3 SPINE2  '
  304.     DB 'BLODGR4 SPINE2  '
  305.     DB 'BLODRIP1SPINE2  '
  306.     DB 'BLODRIP2SPINE2  '
  307.     DB 'BLODRIP3SPINE2  '
  308.     DB 'BLODRIP4SPINE2  '
  309.     DB 'BRNBIGC WDGAT64 '
  310.     DB 'BRNBIGL WDGAT64 '
  311.     DB 'BRNBIGR WDGAT64 '
  312.     DB 'BRNPOIS SNDBLCKS'
  313.     DB 'BRNPOIS2SNDCHNKS'
  314.     DB 'BRNSMAL1WDGAT64 '
  315.     DB 'BRNSMAL2WDGAT64 '
  316.     DB 'BRNSMALCWDGAT64 '
  317.     DB 'BRNSMALLWDGAT64 '
  318.     DB 'BRNSMALRWDGAT64 '
  319.     DB 'BROVINE SNDCHNKS'
  320.     DB 'BROVINE2SNDBLCKS'
  321.     DB 'BROWN1  SNDCHNKS'
  322.     DB 'BROWN144SNDPLAIN'
  323.     DB 'BROWN96 SPINE1  '
  324.     DB 'BROWNGRNSNDBLCKS'
  325.     DB 'BROWNHUGSNDPLAIN'
  326.     DB 'BROWNPIPSPINE2  '
  327.     DB 'BROWNWELSNDPLAIN'
  328.     DB 'CEMENT1 GRSKULL1'
  329.     DB 'CEMENT2 GRSKULL1'
  330.     DB 'CEMENT3 GRSKULL1'
  331.     DB 'CEMENT4 GRSKULL1'
  332.     DB 'CEMENT5 GRSKULL1'
  333.     DB 'CEMENT6 GRSKULL1'
  334.     DB 'CEMPOIS GRSKULL1'
  335.     DB 'COMP2   TRISTON1'
  336.     DB 'COMPBLUESKULLSB1'
  337.     DB 'COMPOHSOSANDSQ2 '
  338.     DB 'COMPSPANSKULLSB1'
  339.     DB 'COMPSTA1SKULLSB1'
  340.     DB 'COMPSTA2SKULLSB1'
  341.     DB 'COMPTALLTRISTON1'
  342.     DB 'COMPTILETRISTON1'
  343.     DB 'COMPUTE1TRISTON1'
  344.     DB 'COMPUTE2TRISTON1'
  345.     DB 'COMPUTE3TRISTON1'
  346.     DB 'COMPWERDTRISTON1'
  347.     DB 'CRATE1  WOODWL  '
  348.     DB 'CRATE2  WOODWL  '
  349.     DB 'CRATELITWOODWL  '
  350.     DB 'CRATINY WOODWL  '
  351.     DB 'CRATWIDEWOODWL  '
  352.     DB 'DOOR1   DOOREXIT'
  353.     DB 'DOOR3   DOOREXIT'
  354.     DB 'DOORBLU DRIPWALL'
  355.     DB 'DOORBLU2DRIPWALL'
  356.     DB 'DOORHI  DOORWOOD'
  357.     DB 'DOORRED DRIPWALL'
  358.     DB 'DOORRED2DRIPWALL'
  359.     DB 'DOORSTOPMETL2   '
  360.     DB 'DOORTRAKMETL2   '
  361.     DB 'DOORYEL DRIPWALL'
  362.     DB 'DOORYEL2DRIPWALL'
  363.     DB 'EXITDOORDOOREXIT'
  364.     DB 'EXITSIGNSNDCHNKS'
  365.     DB 'EXITSTONGRSTNPB '
  366.     DB 'FIREBLU1RCKSNMUD'
  367.     DB 'FIREBLU2RCKSNMUD'
  368.     DB 'FIRELAV2REDWALL '
  369.     DB 'FIRELAV3REDWALL '
  370.     DB 'FIRELAVAREDWALL '
  371.     DB 'FIREMAG1REDWALL '
  372.     DB 'FIREMAG2REDWALL '
  373.     DB 'FIREMAG3REDWALL '
  374.     DB 'FIREWALAREDWALL '
  375.     DB 'FIREWALBREDWALL '
  376.     DB 'FIREWALLREDWALL '
  377.     DB 'GRAY1   SQPEB1  '
  378.     DB 'GRAY2   SQPEB1  '
  379.     DB 'GRAY4   SQPEB1  '
  380.     DB 'GRAY5   SQPEB1  '
  381.     DB 'GRAY7   SQPEB1  '
  382.     DB 'GRAYBIG SQPEB1  '
  383.     DB 'GRAYDANGSQPEB1  '
  384.     DB 'GRAYPOISSQPEB1  '
  385.     DB 'GRAYTALLSQPEB1  '
  386.     DB 'GRAYVINESQPEB1  '
  387.     DB 'GSTFONT1MOSSRCK1'
  388.     DB 'GSTFONT2MOSSRCK1'
  389.     DB 'GSTFONT3MOSSRCK1'
  390.     DB 'GSTGARG MOSSRCK1'
  391.     DB 'GSTLION MOSSRCK1'
  392.     DB 'GSTONE1 MOSSRCK1'
  393.     DB 'GSTONE2 MOSSRCK1'
  394.     DB 'GSTSATYRMOSSRCK1'
  395.     DB 'GSTVINE1MOSSRCK1'
  396.     DB 'GSTVINE2MOSSRCK1'
  397.     DB 'ICKDOOR1DOORSTON'
  398.     DB 'ICKWALL1CSTLRCK '
  399.     DB 'ICKWALL2CSTLRCK '
  400.     DB 'ICKWALL3CSTLRCK '
  401.     DB 'ICKWALL4CSTLRCK '
  402.     DB 'ICKWALL5CSTLRCK '
  403.     DB 'ICKWALL6CSTLRCK '
  404.     DB 'ICKWALL7CSTLRCK '
  405.     DB 'LITE2   SNDCHNKS'
  406.     DB 'LITE3   DRIPWALL'
  407.     DB 'LITE4   DRIPWALL'
  408.     DB 'LITE5   DRIPWALL'
  409.     DB 'LITE96  SPINE1  '
  410.     DB 'LITEBLU1DRIPWALL'
  411.     DB 'LITEBLU2DRIPWALL'
  412.     DB 'LITEBLU3DRIPWALL'
  413.     DB 'LITEBLU4DRIPWALL'
  414.     DB 'LITEMET SKULLSB1'
  415.     DB 'LITERED REDWALL '
  416.     DB 'LITESTONSQPEB1  '
  417.     DB 'MARBFAC2MOSSRCK1'
  418.     DB 'MARBFAC3MOSSRCK1'
  419.     DB 'MARBFACEMOSSRCK1'
  420.     DB 'MARBLE1 MOSSRCK1'
  421.     DB 'MARBLE2 MOSSRCK1'
  422.     DB 'MARBLE3 MOSSRCK1'
  423.     DB 'MARBLOD1MOSSRCK1'
  424.     DB 'METAL   RCKSNMUD'
  425.     DB 'METAL1  SKULLSB1'
  426.     DB 'MIDBRN1 WDGAT64 '
  427.     DB 'MIDGRATEWDGAT64 '
  428.     DB 'MIDVINE1WDGAT64 '
  429.     DB 'MIDVINE2WDGAT64 '
  430.     DB 'NUKE24  SNDPLAIN'
  431.     DB 'NUKEDGE1SNDPLAIN'
  432.     DB 'NUKEPOISSNDPLAIN'
  433.     DB 'NUKESLADSNDPLAIN'
  434.     DB 'PIPE1   SPINE2  '
  435.     DB 'PIPE2   SPINE2  '
  436.     DB 'PIPE4   SPINE2  '
  437.     DB 'PIPE6   SPINE2  '
  438.     DB 'PLANET1 METL1   '
  439.     DB 'PLAT1   GRSKULL1'
  440.     DB 'REDWALL REDWALL '
  441.     DB 'REDWALL1REDWALL '
  442.     DB 'ROCKRED1REDWALL '
  443.     DB 'ROCKRED2REDWALL '
  444.     DB 'ROCKRED3REDWALL '
  445.     DB 'SHAWN1  SQPEB1  '
  446.     DB 'SHAWN2  SQPEB1  '
  447.     DB 'SHAWN3  SQPEB1  '
  448.     DB 'SKIN2   REDWALL '
  449.     DB 'SKINBORDREDWALL '
  450.     DB 'SKINCUT CTYSTCI1'
  451.     DB 'SKINEDGEREDWALL '
  452.     DB 'SKINFACEREDWALL '
  453.     DB 'SKINLOW CTYSTCI2'
  454.     DB 'SKINMET1CTYSTCI4'
  455.     DB 'SKINMET2CTYSTCI1'
  456.     DB 'SKINSCABCTYSTCI2'
  457.     DB 'SKINSYMBCTYSTCI4'
  458.     DB 'SKINTEK1CTYSTCI1'
  459.     DB 'SKINTEK2CTYSTCI2'
  460.     DB 'SKSNAKE1RCKSNMUD'
  461.     DB 'SKSNAKE2RCKSNMUD'
  462.     DB 'SKSPINE1RCKSNMUD'
  463.     DB 'SKSPINE2RCKSNMUD'
  464.     DB 'SKULWAL3RCKSNMUD'
  465.     DB 'SKULWALLRCKSNMUD'
  466.     DB 'SKY1    SKY1    '
  467.     DB 'SKY2    SKY1    '
  468.     DB 'SKY3    SKY1    '
  469.     DB 'SLADPOISGRSTNPB '
  470.     DB 'SLADRIP1GRSTNPB '
  471.     DB 'SLADRIP2GRSTNPB '
  472.     DB 'SLADRIP3GRSTNPB '
  473.     DB 'SLADSKULGRSTNPB '
  474.     DB 'SLADWALLGRSTNPB '
  475.     DB 'SP_DUDE1SAINT1  '
  476.     DB 'SP_DUDE2SAINT1  '
  477.     DB 'SP_DUDE3SAINT1  '
  478.     DB 'SP_DUDE4SAINT1  '
  479.     DB 'SP_DUDE5SAINT1  '
  480.     DB 'SP_DUDE6SAINT1  '
  481.     DB 'SP_FACE1GRSKULL1'
  482.     DB 'SP_HOT1 REDWALL '
  483.     DB 'SP_ROCK1METL1   '
  484.     DB 'SP_ROCK2METL1   '
  485.     DB 'STARBR2 CTYSTUC1'
  486.     DB 'STARG1  CTYSTUC2'
  487.     DB 'STARG2  CTYSTUC3'
  488.     DB 'STARG3  CTYSTUC4'
  489.     DB 'STARGR1 CTYSTUC5'
  490.     DB 'STARGR2 CTYSTUC1'
  491.     DB 'STARTAN1CTYSTUC2'
  492.     DB 'STARTAN2CTYSTUC3'
  493.     DB 'STARTAN3CTYSTUC4'
  494.     DB 'STEP1   TMBSTON2'
  495.     DB 'STEP2   TMBSTON2'
  496.     DB 'STEP3   TMBSTON2'
  497.     DB 'STEP4   TMBSTON2'
  498.     DB 'STEP5   TMBSTON2'
  499.     DB 'STEP6   TMBSTON2'
  500.     DB 'STEPLAD1TMBSTON2'
  501.     DB 'STEPTOP TMBSTON2'
  502.     DB 'STONE   TRISTON1'
  503.     DB 'STONE2  TRISTON1'
  504.     DB 'STONE3  TRISTON1'
  505.     DB 'STONGARGTRISTON1'
  506.     DB 'STONPOISTRISTON1'
  507.     DB 'SUPPORT2DRIPWALL'
  508.     DB 'SUPPORT3DRIPWALL'
  509.     DB 'SW1BLUE SW1OFF  '
  510.     DB 'SW1BRCOMSW1OFF  '
  511.     DB 'SW1BRN1 SW1OFF  '
  512.     DB 'SW1BRN2 SW1OFF  '
  513.     DB 'SW1BRNGNSW1OFF  '
  514.     DB 'SW1BROWNSW1OFF  '
  515.     DB 'SW1CMT  SW1OFF  '
  516.     DB 'SW1COMM SW1OFF  '
  517.     DB 'SW1COMP SW1OFF  '
  518.     DB 'SW1DIRT SW1OFF  '
  519.     DB 'SW1EXIT SW1OFF  '
  520.     DB 'SW1GARG SW1OFF  '
  521.     DB 'SW1GRAY SW1OFF  '
  522.     DB 'SW1GRAY1SW1OFF  '
  523.     DB 'SW1GSTONSW1OFF  '
  524.     DB 'SW1HOT  SW1OFF  '
  525.     DB 'SW1LION SW1OFF  '
  526.     DB 'SW1METALSW1OFF  '
  527.     DB 'SW1PIPE SW1OFF  '
  528.     DB 'SW1SATYRSW1OFF  '
  529.     DB 'SW1SKIN SW1OFF  '
  530.     DB 'SW1SLAD SW1OFF  '
  531.     DB 'SW1STARGSW1OFF  '
  532.     DB 'SW1STON1SW1OFF  '
  533.     DB 'SW1STON2SW1OFF  '
  534.     DB 'SW1STONESW1OFF  '
  535.     DB 'SW1STRTNSW1OFF  '
  536.     DB 'SW1VINE SW1OFF  '
  537.     DB 'SW1WOOD SW1OFF  '
  538.     DB 'SW2BLUE SW1ON   '
  539.     DB 'SW2BRCOMSW1ON   '
  540.     DB 'SW2BRN1 SW1ON   '
  541.     DB 'SW2BRN2 SW1ON   '
  542.     DB 'SW2BRNGNSW1ON   '
  543.     DB 'SW2BROWNSW1ON   '
  544.     DB 'SW2CMT  SW1ON   '
  545.     DB 'SW2COMM SW1ON   '
  546.     DB 'SW2COMP SW1ON   '
  547.     DB 'SW2DIRT SW1ON   '
  548.     DB 'SW2EXIT SW1ON   '
  549.     DB 'SW2GARG SW1ON   '
  550.     DB 'SW2GRAY SW1ON   '
  551.     DB 'SW2GRAY1SW1ON   '
  552.     DB 'SW2GSTONSW1ON   '
  553.     DB 'SW2HOT  SW1ON   '
  554.     DB 'SW2LION SW1ON   '
  555.     DB 'SW2METALSW1ON   '
  556.     DB 'SW2PIPE SW1ON   '
  557.     DB 'SW2SATYRSW1ON   '
  558.     DB 'SW2SKIN SW1ON   '
  559.     DB 'SW2SLAD SW1ON   '
  560.     DB 'SW2STARGSW1ON   '
  561.     DB 'SW2STON1SW1ON   '
  562.     DB 'SW2STON2SW1ON   '
  563.     DB 'SW2STONESW1ON   '
  564.     DB 'SW2STRTNSW1ON   '
  565.     DB 'SW2VINE SW1ON   '
  566.     DB 'SW2WOOD SW1ON   '
  567.     DB 'TEKWALL1WOODWL  '
  568.     DB 'TEKWALL2WOODWL  '
  569.     DB 'TEKWALL3WOODWL  '
  570.     DB 'TEKWALL4WOODWL  '
  571.     DB 'TEKWALL5WOODWL  '
  572.     DB 'WOOD1   WOODWL  '
  573.     DB 'WOOD3   WOODWL  '
  574.     DB 'WOOD4   WOODWL  '
  575.     DB 'WOOD5   WOODWL  '
  576.     DB 'WOODGARGWOODWL  '
  577.     DB 'WOODSKULWOODWL  '
  578.     DB 0
  579.   end;
  580.  
  581. procedure hfloor_table; assembler;
  582.   asm
  583.     DB 'BLOOD1  FLTLAVA1'
  584.     DB 'BLOOD2  FLTLAVA1'
  585.     DB 'BLOOD3  FLTLAVA1'
  586.     DB 'CEIL1_1 FLOOR10 '
  587.     DB 'CEIL1_2 FLOOR11 '
  588.     DB 'CEIL1_3 FLOOR11 '
  589.     DB 'CEIL3_1 FLOOR17 '
  590.     DB 'CEIL3_2 FLOOR17 '
  591.     DB 'CEIL3_3 FLOOR17 '
  592.     DB 'CEIL3_4 FLOOR17 '
  593.     DB 'CEIL3_5 FLOOR00 '
  594.     DB 'CEIL3_6 FLOOR00 '
  595.     DB 'CEIL4_1 FLOOR16 '
  596.     DB 'CEIL4_2 FLOOR16 '
  597.     DB 'CEIL4_3 FLOOR16 '
  598.     DB 'CEIL5_1 FLOOR04 '
  599.     DB 'CEIL5_2 FLOOR04 '
  600.     DB 'COMP01  FLOOR04 '
  601.     DB 'CONS1_1 FLOOR08 '
  602.     DB 'CONS1_5 FLOOR08 '
  603.     DB 'CONS1_7 FLOOR08 '
  604.     DB 'CRATOP1 FLOOR30 '
  605.     DB 'CRATOP2 FLOOR30 '
  606.     DB 'DEM1_1  FLOOR19 '
  607.     DB 'DEM1_2  FLOOR19 '
  608.     DB 'DEM1_3  FLOOR19 '
  609.     DB 'DEM1_4  FLOOR19 '
  610.     DB 'DEM1_5  FLOOR19 '
  611.     DB 'DEM1_6  FLOOR19 '
  612.     DB 'FLAT1   FLOOR00 '
  613.     DB 'FLAT10  FLOOR01 '
  614.     DB 'FLAT14  FLOOR16 '
  615.     DB 'FLAT17  FLOOR03 '
  616.     DB 'FLAT18  FLOOR03 '
  617.     DB 'FLAT19  FLOOR03 '
  618.     DB 'FLAT1_1 FLOOR03 '
  619.     DB 'FLAT1_2 FLOOR03 '
  620.     DB 'FLAT1_3 FLOOR08 '
  621.     DB 'FLAT2   FLOOR11 '
  622.     DB 'FLAT20  FLOOR04 '
  623.     DB 'FLAT22  FLOOR05 '
  624.     DB 'FLAT23  FLOOR04 '
  625.     DB 'FLAT3   FLOOR04 '
  626.     DB 'FLAT4   FLOOR08 '
  627.     DB 'FLAT5   FLOOR06 '
  628.     DB 'FLAT5_1 FLOOR10 '
  629.     DB 'FLAT5_2 FLOOR25 '
  630.     DB 'FLAT5_3 FLOOR09 '
  631.     DB 'FLAT5_4 FLOOR04 '
  632.     DB 'FLAT5_5 FLOOR27 '
  633.     DB 'FLAT5_6 FLOOR06 '
  634.     DB 'FLAT5_7 FLOOR03 '
  635.     DB 'FLAT5_8 FLOOR03 '
  636.     DB 'FLAT8   FLOOR25 '
  637.     DB 'FLAT9   FLOOR04 '
  638.     DB 'FLOOR0_1FLOOR17 '
  639.     DB 'FLOOR0_2FLOOR27 '
  640.     DB 'FLOOR0_3FLOOR18 '
  641.     DB 'FLOOR0_5FLOOR04 '
  642.     DB 'FLOOR0_6FLOOR04 '
  643.     DB 'FLOOR0_7FLOOR04 '
  644.     DB 'FLOOR1_1FLOOR16 '
  645.     DB 'FLOOR1_6FLOOR09 '
  646.     DB 'FLOOR1_7FLOOR09 '
  647.     DB 'FLOOR3_3FLOOR18 '
  648.     DB 'FLOOR4_1FLOOR25 '
  649.     DB 'FLOOR4_5FLOOR25 '
  650.     DB 'FLOOR4_6FLOOR25 '
  651.     DB 'FLOOR4_8FLOOR00 '
  652.     DB 'FLOOR5_1FLOOR01 '
  653.     DB 'FLOOR5_2FLOOR17 '
  654.     DB 'FLOOR5_3FLOOR17 '
  655.     DB 'FLOOR5_4FLOOR10 '
  656.     DB 'FLOOR6_1FLOOR09 '
  657.     DB 'FLOOR6_2FLOOR03 '
  658.     DB 'FLOOR7_1FLOOR27 '
  659.     DB 'FLOOR7_2FLOOR19 '
  660.     DB 'FWATER1 FLTWAWA1'
  661.     DB 'FWATER2 FLTWAWA1'
  662.     DB 'FWATER3 FLTWAWA1'
  663.     DB 'FWATER4 FLTWAWA1'
  664.     DB 'GATE1   FLTTELE1'
  665.     DB 'GATE2   FLTTELE1'
  666.     DB 'GATE3   FLTTELE1'
  667.     DB 'GATE4   FLTTELE1'
  668.     DB 'LAVA1   FLTLAVA1'
  669.     DB 'LAVA2   FLTLAVA1'
  670.     DB 'LAVA3   FLTLAVA1'
  671.     DB 'LAVA4   FLTLAVA1'
  672.     DB 'MFLR8_1 FLOOR03 '
  673.     DB 'MFLR8_2 FLOOR17 '
  674.     DB 'MFLR8_3 FLOOR04 '
  675.     DB 'MFLR8_4 FLOOR05 '
  676.     DB 'NUKAGE1 FLTSLUD1'
  677.     DB 'NUKAGE2 FLTSLUD1'
  678.     DB 'NUKAGE3 FLTSLUD1'
  679.     DB 'SFLR6_1 FLOOR18 '
  680.     DB 'SFLR6_4 FLOOR18 '
  681.     DB 'SFLR7_1 FLOOR18 '
  682.     DB 'SFLR7_4 FLOOR18 '
  683.     DB 'STEP1   FLOOR19 '
  684.     DB 'STEP2   FLOOR19 '
  685.     DB 'TLITE6_1FLOOR06 '
  686.     DB 'TLITE6_4FLOOR06 '
  687.     DB 'TLITE6_5FLOOR06 '
  688.     DB 'TLITE6_6FLOOR06 '
  689.     DB 0
  690.   end;
  691.  
  692. procedure CreateTable; assembler;
  693.   asm
  694.     push ds
  695.     mov ax, SEG objects
  696.     mov es, ax
  697.     lea di, objects
  698.     lea si, @@TABLE
  699.     mov ax, cs
  700.     mov ds, ax
  701.     xor cx, cx
  702.     cld
  703. @@CICLO:
  704.     lodsb
  705.     cmp al, 0
  706.     je  @@STOP
  707.     xor dx, dx
  708. @@NUM:
  709.     mov bx, dx
  710.     add dx, dx
  711.     add dx, dx
  712.     add dx, bx
  713.     add dx, dx
  714.     and ax, 15
  715.     add dx, ax
  716.     lodsb
  717.     cmp al, 32
  718.     jne @@NUM
  719.     push ax
  720.     mov ax, dx
  721.     stosw
  722.     pop ax
  723.  
  724. @@SPACES:
  725.     cmp al, 32
  726.     jne @@SHORT
  727.     lodsb
  728.     jmp @@SPACES
  729. @@SHORT:
  730.     stosb
  731.     movsb
  732.     movsb
  733.     mov bx, si
  734.     inc si
  735. @@ZERO:
  736.     lodsb
  737.     cmp al, 0
  738.     jne @@ZERO
  739.     mov ax, si
  740.     sub ax, bx
  741.     dec ax
  742.     dec ax
  743.     mov ds:[bx], al
  744.     mov ax, bx
  745.     stosw
  746.     mov ax, cs
  747.     stosw
  748.     inc cx
  749.     jmp @@CICLO
  750. @@STOP:
  751.     pop ds
  752.     mov numobjects, cx
  753.     jmp @@FINE
  754. @@TABLE:
  755.     DB '2007 AMM Ammo Clip',0
  756.     DB '68   ARA Arachnotron',0
  757.     DB '64   ARC Archvile',0
  758.     DB '2015 ARM Armor Helmet',0
  759.     DB '8    BAC Backpack',0
  760.     DB '2048 BAM Box of Ammo',0
  761.     DB '2035 BAR Barrel',0
  762.     DB '2023 BER Berserk',0
  763.     DB '2006 BFG BFG9000',0
  764.     DB '2024 BLR Blur Sphere',0
  765.     DB '2019 BLU Blue Armor',0
  766.     DB '3003 BOH Baron of Hell',0
  767.     DB '2046 BRO Box of Rockets',0
  768.     DB '2049 BSH Box of Shells',0
  769.     DB '70   BUR Burning Barrel',0
  770.     DB '3005 CAC Cacodemon',0
  771.     DB '2002 CHA Chaingun',0
  772.     DB '65   CHD Chaingun Dude',0
  773.     DB '2005 CHS Chainsaw',0
  774.     DB '2026 COM Computer Map',0
  775.     DB '16   CYB Cyberdemon',0
  776.     DB '3002 DEM Demon',0
  777.     DB '2047 ENC Energy Cell',0
  778.     DB '17   ENP Energy Pack',0
  779.     DB '2018 GRE Green Armor',0
  780.     DB '2014 HEA Health Potion',0
  781.     DB '69   HEL Hell Knight',0
  782.     DB '3001 IMP Imp',0
  783.     DB '2022 INV Invulnerability',0
  784.     DB '72   KEN Commander Keen',0
  785.     DB '2045 LIG Light Goggles',0
  786.     DB '3006 LOS Lost Soul',0
  787.     DB '67   MAN Mancubus',0
  788.     DB '2012 MED Medikit',0
  789.     DB '83   MEG Megasphere',0
  790.     DB '71   PAI Pain Elemental',0
  791.     DB '2004 PLA Plasma Gun',0
  792.     DB '2025 RAD Radiation Suit',0
  793.     DB '2010 RCK Rocket',0
  794.     DB '66   REV Revenant',0
  795.     DB '2003 ROC Rocket Launcher',0
  796.     DB '9    SER Sergeant',0
  797.     DB '2008 SHE Shells',0
  798.     DB '2001 SHO Shotgun',0
  799.     DB '2013 SOU Soul Sphere',0
  800.     DB '58   SPE Spectre',0
  801.     DB '7    SPI Spiderdemon',0
  802.     DB '82   SSH Super Shotgun',0
  803.     DB '84   SSN SS Nazi',0
  804.     DB '2011 STI Stimpack',0
  805.     DB '3004 TRO Trooper',0
  806.     DB 0
  807. @@FINE:
  808.   end;
  809.  
  810. {Return a right-padded string of N characters from a string}
  811. function StringN(s:String;n:Integer):String;
  812.   var i:Integer;
  813.   begin
  814.     StringN:=Copy(s,1,n);
  815.     StringN[0]:=Char(n);
  816.     for i:=Length(s)+1 to n do StringN[i]:=' ';
  817.   end;
  818.  
  819. {Converts string to uppercase}
  820. function Upper(s:String):String;
  821.   var i:Integer;
  822.   begin
  823.     Upper[0]:=s[0];
  824.     for i:=1 to Length(s) do Upper[i]:=UpCase(s[i]);
  825.   end;
  826.  
  827. {Add a suffix(extension) to a filename (only if the filename hasn't one)}
  828. function AddSuffix(s,n:String):String;
  829.   var i:Integer;
  830.   begin
  831.     i:=Length(s);
  832.     while i>0 do
  833.       if s[i]='.' then break
  834.       else dec(i);
  835.     if i>0 then AddSuffix:=s
  836.     else AddSuffix:=s+'.'+n;
  837.   end;
  838.  
  839. procedure Title;
  840.   begin
  841.     writeln('DM2CONV v1.6ß by Vincenzo Alcamo (alcamo@arci01.bo.cnr.it)');
  842.   end;
  843.  
  844. procedure List;
  845.   var i,j:integer;
  846.   begin
  847.     Title;
  848.     writeln;
  849.     writeln('LIST OF KNOWN OBJECTS');
  850.     for i:=1 to numobjects do begin
  851.       if i mod 3=1 then writeln
  852.       else write('  ');
  853.       with objects[1+((i-1)div 3)+((i-1)mod 3)*((numobjects+2) div 3)] do
  854.         write(id:4,#32,sname,#32,StringN(name^,15));
  855.     end;
  856.     writeln;
  857.     writeln;
  858.     writeln('You can specify an object by its number, its shortname, its name');
  859.     writeln('or even an initial fragment of its name.');
  860.   end;
  861.  
  862. procedure More;
  863.   begin
  864.     Title;
  865.     writeln;
  866.     writeln('REPLACEMENT is an expression specifying object substitution:');
  867.     writeln('  {source[:lev]}={dest[@num][:lev]}');
  868.     writeln('source is the initial object, dest is the final object,');
  869.     writeln('num is the number of substitutions (absolute or percentual)');
  870.     writeln('lev specifies the difficulty-level flags of the object.');
  871.     writeln('You can specify more than one replacement.');
  872.     writeln;
  873.     writeln('Replacement expression examples:');
  874.     writeln;
  875.     writeln('DEM=IMP             all Demons become Imps');
  876.     writeln('DEM,IMP=LOS         all Demons and Imps become Lost Souls');
  877.     writeln('DEM=IMP@5           5 Demons become Imps');
  878.     writeln('DEM=IMP@50%         50% of Demons become Imps');
  879.     writeln('DEM=IMP@5,SER       5 Demons become Imps, the rest are Sergeants');
  880.     writeln('DEM=IMP DEM=TRO     No Demons remain for the second expression');
  881.     writeln('DEM:1=IMP           All demons that appers in level 1 become Imps');
  882.     writeln('DEM=IMP:123         All demons become Imps that appear in all levels');
  883.     writeln;
  884.     writeln('Requests greater than available objects are adjusted proportionally:');
  885.     writeln('DEM=IMP@5,TRO@15    If Demons are 9 -> IMP@25%,TRO@75%');
  886.     writeln;
  887.     writeln('You can substitute the % sign with #,$,& whichever you prefer.');
  888.     writeln;
  889.   end;
  890.  
  891. procedure Help;
  892.   begin
  893.     Title;
  894.     writeln('Converts DOOM maps for use with DOOM II/HERETIC.');
  895.     writeln;
  896.     writeln('DM2CONV <input> [output] [/mapnum] [/M[=num]] [/DEBUG] [/IGNORE]');
  897.     writeln('        [/HERETIC] [/TEXTURE[=file]] [/FLOOR[=file]] [/NOCONV]');
  898.     writeln('        [/SEED[=num]] [/NOCHECK] [replacements].. [@response]...');
  899.     writeln('        [/R:name1=name2] [/R=file] [/LIST] [/EXAMPLES] [/NOTES]');
  900.     writeln;
  901.     writeln('input        name of DOOM wad file to convert ** REQUIRED **');
  902.     writeln('output       name of output file (if omitted, the input file is overwritten)');
  903.     writeln('/mapnum      number for the first level remapped (default: 1)');
  904.     writeln('/M[=num]     music remapping (num is the level for the first music)');
  905.     writeln('/DEBUG       display debug information');
  906.     writeln('/IGNORE      make replacements even if no level is remapped');
  907.     writeln('/HERETIC     DOOM->HERETIC conversion');
  908.     writeln('/TEXTURE     convert texture names  *** SEE DM2CONV.DOC ***');
  909.     writeln('/FLOOR       convert floor names (/HERETIC only)');
  910.     writeln('/SEED[=num]  random generator seed (default: 0, randomize if num is omitted)');
  911.     writeln('/NOCHECK     allow the use of object numbers not in list');
  912.     writeln('/R           renames directory entries');
  913.     writeln('/NOCONV      ignore conversion: useful for /R or object substitution');
  914.     writeln('@response    response file (text file with additional arguments)');
  915.     writeln('Use /LIST, /EXAMPLES, /NOTES to get further information.');
  916.   end;
  917.  
  918. procedure Notes;
  919.   begin
  920.     Title;
  921.     writeln;
  922.     writeln('Notes about level remapping:');
  923.     writeln('- Level remapping is performed regardless of level name:');
  924.     writeln('  the first level found becomes MAP01 (and so on)');
  925.     writeln('- No other resources are remapped (eg: M_EPI?, etc...)');
  926.     writeln('- DM2CONV acts only in one way: keep a backup of your wads.');
  927.     writeln('- Secret levels are not remapped to the proper level: don''t use wads');
  928.     writeln('  with secret levels  or, at least, avoid entering a secret level.');
  929.     writeln;
  930.     writeln('Music remapping has 3 settings (none, /M, /M=num):');
  931.     writeln('1) no music is remapped.');
  932.     writeln('2) remap musics accordingly to remapped levels');
  933.     writeln('   D_E1M1 becomes D_RUNNIN only if E1M1 was remapped');
  934.     writeln('3) the first music found becomes the music for MAP num,');
  935.     writeln('   the second becomes the music for MAP num+1, and so on.');
  936.     writeln('For 2) and 3): the end-of-level music is also remapped.');
  937.   end;
  938.  
  939. function GetWord(var s:string):string;
  940.   var i:integer;
  941.   begin
  942.     s:=s+#0;
  943.     i:=1;
  944.     while ((s[i]>='0') and (s[i]<='9')) or ((s[i]>='A') and (s[i]<='Z')) do inc(i);
  945.     GetWord:=Copy(s,1,i-1);
  946.     s:=Copy(s,i,length(s)-i);
  947.   end;
  948.  
  949. function GetNum(var s:string):integer;
  950.   var i,j,k:integer;
  951.   begin
  952.     val(s,j,k);
  953.     if k=0 then begin
  954.       if nocheck and (j>0) and (j<16384) then begin
  955.         GetNum:=j;
  956.         exit;
  957.       end;
  958.       for i:=1 to numobjects do
  959.         if objects[i].id=j then begin
  960.           GetNum:=j;
  961.           exit;
  962.         end;
  963.     end
  964.     else begin
  965.       for i:=1 to numobjects do
  966.         if s=objects[i].sname then begin
  967.           GetNum:=objects[i].id;
  968.           exit;
  969.         end;
  970.       for i:=1 to numobjects do with objects[i] do begin
  971.         j:=1;
  972.         k:=1;
  973.         repeat
  974.           if name^[k]=' ' then inc(k)
  975.           else if s[j]<>UpCase(name^[k]) then break
  976.           else begin
  977.             inc(j);
  978.             inc(k);
  979.           end;
  980.         until (j>length(s)) or (k>length(name^));
  981.         if j>length(s) then begin
  982.           GetNum:=id;
  983.           exit;
  984.         end;
  985.       end;
  986.     end;
  987.     GetNum:=0;
  988.   end;
  989.  
  990. procedure noname(s:string);
  991.   begin
  992.     writeln('No object found for ',s);
  993.     halt;
  994.   end;
  995.  
  996. procedure myhalt(code:errors);
  997.   begin
  998.     case code of
  999.       ERR_OPENS: writeln('Error opening: ',source);
  1000.       ERR_OPEND: writeln('Error opening: ',dest);
  1001.       ERR_READS: writeln('Error reading: ',source);
  1002.       ERR_WRITED:writeln('Error writing: ',dest);
  1003.       ERR_PWAD:  writeln('File is not a PWAD: ',source);
  1004.       ERR_TOOENTRY:writeln('Too many entries in file: ',source);
  1005.       ERR_TOOMAPS:writeln('Cannot remap after map 32');
  1006.       ERR_NOMAPS:writeln('No maps found in file: ',source);
  1007.       ERR_NOEQ:  writeln('Missing ''='' after list of source objects');
  1008.       ERR_BADEND:writeln('Expression incorrectly terminated');
  1009.       ERR_BADNUM:writeln('Bad number in expression');
  1010.       ERR_NOMEM: writeln('Not enough memory');
  1011.       ERR_OPEN:  writeln('Error opening: ',datafile);
  1012.       ERR_READ:  writeln('Error reading: ',datafile);
  1013.     end;
  1014.     halt(0);
  1015.   end;
  1016.  
  1017. procedure checkdatafile(table:p_repname_array;var num:integer;s:string);
  1018.   var f      :text;
  1019.       i      :integer;
  1020.       bef,aft:dname;
  1021.   function getname(var dest:dname):boolean;
  1022.     var j:integer;
  1023.         c:char;
  1024.     begin
  1025.       getname:=false;
  1026.       while (i<length(s)) and ((s[i]=' ') or (s[i]=#9)) do inc(i);
  1027.       if i<length(s) then
  1028.         case s[1] of
  1029.           '''',';','#','%','[':;
  1030.           else begin
  1031.             dest:=NULL_NAME;
  1032.             j:=8;
  1033.             c:=upcase(s[i]);
  1034.             while (j>0) and (i<=length(s)) and (
  1035.              ((c>='0') and (c<='9')) or (c='_') or
  1036.              ((c>='A') and (c<='Z')) ) do begin
  1037.               dec(j);
  1038.               dest[8-j]:=c;
  1039.               inc(i);
  1040.               c:=upcase(s[i]);
  1041.             end;
  1042.             if c='=' then inc(i);
  1043.             getname:=j<8;
  1044.           end
  1045.         end
  1046.     end;
  1047.   procedure insertname;
  1048.     var bef,aft:dname;
  1049.     begin
  1050.       if getname(bef) and getname(aft) then begin
  1051.         i:=1;
  1052.         while i<=num do
  1053.           if table^[i].before=bef then break
  1054.           else inc(i);
  1055.         if (i>num) and (num<1024) then inc(num);
  1056.         table^[i].before:=bef;
  1057.         table^[i].after:=aft;
  1058.       end;
  1059.     end;
  1060.   begin
  1061.     i:=1;
  1062.     while (i<=length(s)) and (s[i]<>':') and (s[i]<>'=') do inc(i);
  1063.     if i>=length(s) then exit;
  1064.     inc(i);
  1065.     if s[i-1]=':' then insertname
  1066.     else begin
  1067.       s:=copy(s,i,255);
  1068.       datafile:=s;
  1069.       writeln('Reading data file: ',s);
  1070.       assign(f,s);
  1071.       reset(f);
  1072.       if ioresult<>0 then myhalt(ERR_OPEN);
  1073.       while not eof(f) do begin
  1074.         readln(f,s);
  1075.         if ioresult<>0 then myhalt(ERR_READ);
  1076.         i:=1;
  1077.         insertname;
  1078.       end;
  1079.       close(f);
  1080.     end;
  1081.   end;
  1082.  
  1083. procedure Swappa(var h,k:integer);
  1084.   var i,l:integer;
  1085.   begin
  1086.     for i:=1 to 3 do begin
  1087.       l:=replace[k];
  1088.       replace[k]:=replace[h];
  1089.       replace[h]:=l;
  1090.       inc(k);
  1091.       inc(h);
  1092.     end;
  1093.   end;
  1094.  
  1095. function checklevel(var s:string):integer;
  1096.   var i,j:integer;
  1097.       t:string;
  1098.   begin
  1099.     j:=0;
  1100.     if (length(s)>1) and (s[1]=':') then begin
  1101.       s:=Copy(s,2,255);
  1102.       t:=GetWord(s);
  1103.       for i:=1 to length(t) do case t[i] of
  1104.         '1': j:=j or 1;  {skill level 1-2}
  1105.         '2': j:=j or 2;  {skill level 3}
  1106.         '3': j:=j or 4;  {skill level 4-5}
  1107.         'D': j:=j or 8;  {deaf flag}
  1108.         'M': j:=j or 16; {multiplayer}
  1109.       end;
  1110.     end;
  1111.     checklevel:=j;
  1112.   end;
  1113.  
  1114. procedure printlevel(i:integer);
  1115.   begin
  1116.     if i>0 then write(':');
  1117.     if (i and 1)=1 then write('1');
  1118.     if (i and 2)=2 then write('2');
  1119.     if (i and 4)=4 then write('3');
  1120.     if (i and 8)=8 then write('D');
  1121.     if (i and 16)=16 then write('M');
  1122.   end;
  1123.  
  1124. procedure Parse;
  1125.   var
  1126.     i,j,k,h : integer;
  1127.     s,t     : string;
  1128.     l       : longint;
  1129.     f       : boolean;
  1130.     repn    : integer;
  1131.     ri,rc,rs: integer;
  1132.     response: text;
  1133.     inresp  : boolean;
  1134.     respstr : string;
  1135.   function GetArgument:string;
  1136.     var i,j:integer;
  1137.     begin
  1138.       if respstr='' then begin
  1139.         if eof(response) then begin
  1140.           respstr:='';
  1141.           inresp:=false;
  1142.           close(response);
  1143.         end
  1144.         else begin
  1145.           Readln(response,respstr);
  1146.           if ioresult<>0 then begin
  1147.             writeln('Error reading from response file');
  1148.             respstr:='';
  1149.             inresp:=false;
  1150.             close(response);
  1151.           end;
  1152.           j:=1;
  1153.           for i:=1 to length(respstr) do
  1154.             case respstr[i] of
  1155.               #32,#9: if j>1 then begin
  1156.                         respstr[j]:=#32;
  1157.                         inc(j);
  1158.                       end;
  1159.               else begin
  1160.                 respstr[j]:=respstr[i];
  1161.                 inc(j);
  1162.               end;
  1163.             end;
  1164.           respstr[0]:=chr(j-1);
  1165.         end;
  1166.       end;
  1167.       case respstr[1] of
  1168.         '''',';','#','%','[': respstr:='';
  1169.       end;
  1170.       i:=1;
  1171.       while (i<=length(respstr)) and (respstr[i]<>#32) do inc(i);
  1172.       GetArgument:=Upper(Copy(respstr,1,i-1));
  1173.       respstr:=Copy(respstr,i+1,255);
  1174.     end;
  1175.   begin
  1176.     source:='';
  1177.     dest:='';
  1178.     RandSeed:=0;
  1179.     repn:=1;
  1180.     inresp:=false;
  1181.     i:=1;
  1182.     while i<=ParamCount do begin
  1183.       f:=not (show_help or show_example or show_list or show_note);
  1184.       if inresp then s:=GetArgument
  1185.       else s:=Upper(ParamStr(i));
  1186.       if s='' then {DO NOTHING}
  1187.       else if s[1]='@' then begin
  1188.         if inresp then writeln('Cannot use nested response file!')
  1189.         else begin
  1190.           respstr:='';
  1191.           assign(response,Copy(s,2,255));
  1192.           reset(response);
  1193.           if ioresult<>0 then writeln('Error opening response file.')
  1194.           else inresp:=true;
  1195.         end;
  1196.       end
  1197.       else if (s[1]='/') or (s[1]='-') then begin
  1198.         s:=Copy(s,2,255);
  1199.         if (s='HELP') or (s='?') or (s='H') then show_help:=f
  1200.         else if (s='NOCHECK') or (s='N') then nocheck:=True
  1201.         else if s='NOCONV' then no_conv:=True
  1202.         else if (s='LIST') or (s='L') then show_list:=f
  1203.         else if (Copy(s,1,7)='EXAMPLE') or (s='E') then show_example:=f
  1204.         else if Copy(s,1,4)='NOTE' then show_note:=f
  1205.         else if (s='DEBUG') or (s='D') then debug:=True
  1206.         else if (s='IGNORE') or (s='I') then ignore:=True
  1207.         else if s[1]='R' then checkdatafile(repdirs,nrepdirs,s)
  1208.         else if (copy(s,1,7)='TEXTURE') or (s[1]='T') then begin
  1209.           do_texture:=True;
  1210.           checkdatafile(reptexture,nreptexture,s);
  1211.         end
  1212.         else if (copy(s,1,5)='FLOOR') or (s[1]='F') then begin
  1213.           do_floor:=True;
  1214.           checkdatafile(repfloor,nrepfloor,s);
  1215.         end
  1216.         else if s='HERETIC' then heretic:=True
  1217.         else if Copy(s,1,4)='SEED' then begin
  1218.           s:=Copy(s,5,255);
  1219.           j:=0;
  1220.           if s[1]='=' then begin
  1221.             s:=Copy(s,2,255);
  1222.             Val(s,l,j);
  1223.             if j<>0 then writeln('Bad number for seed: ',s)
  1224.             else RandSeed:=l;
  1225.           end
  1226.           else Randomize;
  1227.           if j=0 then writeln('Seed for random generator is: ',RandSeed);
  1228.         end
  1229.         else if s[1]='M' then begin
  1230.           s:=Copy(s,2,255);
  1231.           if s[1]='=' then s:=Copy(s,2,255);
  1232.           if Length(s)>0 then begin
  1233.             Val(s,j,k);
  1234.             if (k<>0) or (j<1) or (j>32) then writeln('Bad number for music: ',s)
  1235.             else remap_mus:=j;
  1236.           end
  1237.           else remap_mus:=-1; {remap level&music}
  1238.         end
  1239.         else begin
  1240.           Val(s,j,k);
  1241.           if (k<>0) or (j<1) or (j>32) then writeln('Bad number for remap: ',s)
  1242.           else begin
  1243.             remap_lev:=j;
  1244.             remapping:=true;
  1245.             writeln('Remapping from level ',j);
  1246.           end;
  1247.         end
  1248.       end
  1249.       else begin
  1250.         k:=0;
  1251.         for j:=1 to length(s) do if s[j]='=' then k:=1;
  1252.         if k=0 then begin
  1253.           if source='' then source:=s
  1254.           else if dest='' then dest:=s
  1255.           else writeln('Extra parameter ignored: ',s);
  1256.         end
  1257.         else begin
  1258.           inc(replaces);
  1259.           if debug then writeln('Replacement ',replaces,': ',s);
  1260.           rs:=repn;
  1261.           s:=','+s+'';         {''=#21 is a sentinel}
  1262.           while s[1]=',' do begin
  1263.             s:=Copy(s,2,255);
  1264.             t:=GetWord(s);
  1265.             j:=GetNum(t);
  1266.             if j=0 then noname(t);
  1267.             replace[repn]:=j;
  1268.             inc(repn);
  1269.             replace[repn]:=checklevel(s);
  1270.             inc(repn);
  1271.           end;
  1272.           if s[1]<>'=' then myhalt(ERR_NOEQ);
  1273.           ri:=repn;
  1274.           inc(repn);
  1275.           rc:=0;
  1276.           s[1]:=',';
  1277.           while s[1]=',' do begin
  1278.             s:=Copy(s,2,255);
  1279.             t:=GetWord(s);
  1280.             j:=GetNum(t);
  1281.             if j=0 then noname(t);
  1282.             replace[repn]:=j;
  1283.             inc(repn);
  1284.             replace[repn]:=0;
  1285.             if s[1]='@' then begin
  1286.               s:=Copy(s,2,255);
  1287.               t:=GetWord(s);
  1288.               val(t,j,k);
  1289.               if (k<>0) or (j>=REP_PERCENT) or (j<=0) then myhalt(ERR_BADNUM);
  1290.               if (s[1]>='#') and (s[1]<='&') then begin
  1291.                 inc(j,REP_PERCENT);
  1292.                 s:=Copy(s,2,255);
  1293.               end;
  1294.               replace[repn]:=j;
  1295.             end;
  1296.             inc(repn);
  1297.             replace[repn]:=checklevel(s);
  1298.             inc(repn);
  1299.             inc(rc);
  1300.           end;
  1301.           if (s[1]<>'') or (rc=0) then myhalt(ERR_BADEND);
  1302.           replace[ri]:=REP_PERCENT+rc;
  1303.           k:=ri+1;
  1304.           h:=k;
  1305.           for j:=1 to rc do begin
  1306.             if (replace[h+1]>0) and (replace[h+1]<REP_PERCENT) then Swappa(h,k);
  1307.             inc(h,3);
  1308.           end;
  1309.           h:=k;
  1310.           for j:=1 to rc do begin
  1311.             if replace[h+1]>=REP_PERCENT then Swappa(h,k);
  1312.             inc(h,3);
  1313.           end;
  1314.           if debug then begin
  1315.             write('REPLACE');
  1316.             j:=rs;
  1317.             while j<ri do begin
  1318.               write(' ',replace[j]);
  1319.               printlevel(replace[j+1]);
  1320.               inc(j,2);
  1321.             end;
  1322.             write(' WITH');
  1323.             k:=ri+1;
  1324.             for j:=1 to rc do begin
  1325.               write(' ',replace[k]);
  1326.               if replace[k+1]>0 then
  1327.                 if replace[k+1]>=REP_PERCENT then write('@',replace[k+1]-REP_PERCENT,'%')
  1328.                 else write('@',replace[k+1]);
  1329.               printlevel(replace[k+2]);
  1330.               inc(k,3);
  1331.             end;
  1332.             writeln;
  1333.           end;
  1334.         end;
  1335.       end;
  1336.  
  1337.       if not inresp then inc(i);
  1338.     end;
  1339.     if not (show_example or show_list or show_note) and (source='') then show_help:=true;
  1340.     source:=AddSuffix(source,'WAD');
  1341.     if dest<>'' then dest:=AddSuffix(dest,'WAD');
  1342.   end;
  1343.  
  1344. procedure blockr(var f:file;var dest;size:word;var count:word);
  1345.   begin
  1346.     BlockRead(f,dest,size,count);
  1347.     if (ioresult<>0) or (size<>count) then myhalt(ERR_READS);
  1348.   end;
  1349.  
  1350. procedure blockw(var f:file;var dest;size:word;var count:word);
  1351.   begin
  1352.     BlockWrite(f,dest,size,count);
  1353.     if (ioresult<>0) or (size<>count) then myhalt(ERR_WRITED);
  1354.   end;
  1355.  
  1356. procedure CopyDest;
  1357.   var a,b     : file;
  1358.       l       : Longint;
  1359.       size,len: Word;
  1360.   begin
  1361.     writeln('Copying source to destination...');
  1362.     Assign(a,source);
  1363.     FileMode:=0;  {open for read only}
  1364.     Reset(a,1);
  1365.     FileMode:=2;  {open for read/write}
  1366.     if ioresult<>0 then myhalt(ERR_OPENS);
  1367.     Assign(b,dest);
  1368.     Rewrite(b,1);
  1369.     if ioresult<>0 then myhalt(ERR_OPEND);
  1370.     l:=FileSize(a);
  1371.     while l>0 do begin
  1372.       if l>BUFFSIZE then size:=BUFFSIZE
  1373.       else size:=l;
  1374.       BlockR(a,buffer,size,len);
  1375.       BlockW(b,buffer,size,len);
  1376.       dec(l,size);
  1377.     end;
  1378.     Close(a);
  1379.     Close(b);
  1380.   end;
  1381.  
  1382. procedure ReplaceThings(totobj:Integer);
  1383.   var index  : array[1..4000] of integer;
  1384.       numobj : integer;
  1385.       i,j,k,l: integer;
  1386.       repn,h : integer;
  1387.       numabs : integer;
  1388.       nabs   : integer;
  1389.       nrel   : integer;
  1390.       level  : integer;
  1391.       multi  : boolean;
  1392.       s      : string;
  1393.   procedure Choose(var max:integer;n,c,lev:integer);
  1394.     var i,j:integer;
  1395.     begin
  1396.       if n<max then begin
  1397.         for i:=1 to n do begin
  1398.           j:=Random(max)+1;
  1399.           with things[index[j]] do begin
  1400.             inc(repthing);
  1401.             code:=c;
  1402.             if lev<>0 then flags:=lev;
  1403.           end;
  1404.           index[j]:=index[max];
  1405.           dec(max);
  1406.         end;
  1407.       end
  1408.       else begin
  1409.         for i:=1 to max do with things[index[i]] do begin
  1410.           inc(repthing);
  1411.           code:=c;
  1412.           if lev<>0 then flags:=lev;
  1413.         end;
  1414.         max:=0;
  1415.       end;
  1416.     end;
  1417.   begin
  1418.     repn:=1;
  1419.     for i:=1 to replaces do begin
  1420.       if debug then write('REPLACEMENT=',i);
  1421.       numobj:=0;
  1422.       while replace[repn]<REP_PERCENT do begin
  1423.         j:=replace[repn];
  1424.         level:=replace[repn+1] and 7;  {level 1 or 2 or 3}
  1425.         if level=0 then level:=7;
  1426.         multi:=replace[repn+1]>=16;    {multiplayer flag}
  1427.         for k:=1 to totobj do with things[k] do
  1428.           if (code=j) and (flags and level>0) and
  1429.              (not multi or (flags and 16=16)) then begin
  1430.             inc(numobj);
  1431.             index[numobj]:=k;
  1432.           end;
  1433.         inc(repn,2);
  1434.       end;
  1435.       if debug then write('  TOTAL OBJECTS=',numobj);
  1436.       nabs:=0;
  1437.       nrel:=replace[repn]-REP_PERCENT;
  1438.       inc(repn);
  1439.       if numobj=0 then begin
  1440.         if debug then writeln('   SKIPPED');
  1441.         inc(repn,nrel*3);
  1442.         continue;
  1443.       end;
  1444.       numabs:=0;
  1445.       j:=nrel;
  1446.       l:=repn+1;
  1447.       k:=1;
  1448.       while (k<=j) do begin
  1449.         if replace[l]=0 then replace[l]:=REP_PERCENT
  1450.         else begin
  1451.           if replace[l]>=REP_PERCENT then
  1452.             replace[l]:=(longint(numobj)*(replace[l]-REP_PERCENT)+50)div 100;
  1453.           inc(numabs,replace[l]);
  1454.           inc(nabs);
  1455.           dec(nrel);
  1456.         end;
  1457.         inc(k);
  1458.         inc(l,3);
  1459.       end;
  1460.       if numabs>numobj then begin
  1461.         l:=repn+1;
  1462.         k:=numobj;
  1463.         for j:=1 to nabs do begin
  1464.           h:=replace[l];
  1465.           replace[l]:=(longint(h)*k+numabs div 2)div numabs;
  1466.           dec(numabs,h);
  1467.           dec(k,replace[l]);
  1468.           inc(l,3);
  1469.         end;
  1470.         numabs:=numobj;
  1471.       end;
  1472.       l:=repn+nabs*3+1;
  1473.       numabs:=numobj-numabs;
  1474.       while nrel>0 do begin
  1475.         j:=(numabs+nrel div 2) div nrel;
  1476.         replace[l]:=j;
  1477.         dec(numabs,j);
  1478.         inc(l,3);
  1479.         dec(nrel);
  1480.         inc(nabs);
  1481.       end;
  1482.       for j:=1 to nabs do begin
  1483.         if debug then begin
  1484.           if j mod 4=1 then writeln
  1485.           else write(#32);
  1486.           k:=numobjects;
  1487.           h:=replace[repn];
  1488.           while (k>0) and (objects[k].id<>h) do dec(k);
  1489.           if k<>0 then s:=objects[k].name^
  1490.           else begin
  1491.             Str(h,s);
  1492.             s:='Unknown #'+s;
  1493.           end;
  1494.           write(s:15,'=');
  1495.           Str(replace[repn+1],s);
  1496.           write(StringN(s,3));
  1497.         end;
  1498.         Choose(numobj,replace[repn+1],replace[repn],replace[repn+2]);
  1499.         inc(repn,3);
  1500.       end;
  1501.       if debug then writeln;
  1502.     end;
  1503.   end;
  1504.  
  1505. procedure Plural(n:integer;s:string);
  1506.   begin
  1507.     write(' ',n,' ',s);
  1508.     if n<>1 then write('s');
  1509.   end;
  1510.  
  1511. procedure Process;
  1512.   var f    : file;
  1513.       head : header;
  1514.       size : word;
  1515.       i,j,k: integer;
  1516.       l    : integer;
  1517.       numt : integer;
  1518.       fpos : longint;
  1519.       rlev : array[1..27] of integer;
  1520.   begin
  1521.     repside:=0;
  1522.     repfloo:=0;
  1523.     repthing:=0;
  1524.     replev:=0;
  1525.     for i:=1 to 27 do rlev[i]:=0;
  1526.     if dest<>'' then CopyDest
  1527.     else dest:=source;
  1528.     source:=dest;
  1529.     Assign(f,dest);
  1530.     Reset(f,1);
  1531.     if ioresult<>0 then myhalt(ERR_OPEND);
  1532.     BlockR(f,head,sizeof(header),size);
  1533.     if head.sig<>PWAD_SIG then myhalt(ERR_PWAD);
  1534.     numentry:=head.num;
  1535.     if numentry>MAXENTRY then myhalt(ERR_TOOENTRY);
  1536.     Seek(f,head.start);
  1537.     if ioresult<>0 then myhalt(ERR_READS);
  1538.     BlockR(f,dirlist,numentry*sizeof(entry),size);
  1539.  
  1540.     if not no_conv then begin
  1541.       for i:=1 to numentry do with dirlist[i] do begin
  1542.         if not heretic and (name[1]='S') and (name[2]='K') and (name[3]='Y') and
  1543.            (name[4]>='1') and (name[4]<='3') and (name[5]=#0) then begin
  1544.           {remap sky resources}
  1545.           j:=ord(name[4]);
  1546.           name:='RSKYx'#0#0#0;
  1547.           name[5]:=chr(j);
  1548.           savedir:=true;
  1549.         end;
  1550.         if (name[1]='E') and (name[3]='M') then
  1551.          if heretic then begin
  1552.            j:=(ord(name[2])-49)*9+ord(name[4])-48;
  1553.            if remapping then begin
  1554.              if remap_lev>27 then myhalt(ERR_TOOMAPS);
  1555.              rlev[j]:=remap_lev;
  1556.              name[2]:=chr((remap_lev-1) div 9+49);
  1557.              name[4]:=chr((remap_lev-1) mod 9+49);
  1558.              inc(remap_lev);
  1559.              savedir:=true;
  1560.            end
  1561.            else rlev[j]:=j;
  1562.            inc(replev);
  1563.          end
  1564.          else begin
  1565.           if remap_lev>32 then myhalt(ERR_TOOMAPS);
  1566.           rlev[(ord(name[2])-49)*9+ord(name[4])-48]:=remap_lev;
  1567.           name[1]:='M';
  1568.           name[2]:='A';
  1569.           name[3]:='P';
  1570.           name[4]:=chr(remap_lev div 10+48);
  1571.           name[5]:=chr(remap_lev mod 10+48);
  1572.           inc(remap_lev);
  1573.           inc(replev);
  1574.           savedir:=true;
  1575.         end;
  1576.       end;
  1577.       j:=0;
  1578.       if remap_mus<>0 then
  1579.         for i:=1 to numentry do with dirlist[i] do
  1580.           if (name[1]='D') and (name[2]='_') then
  1581.             if name='D_INTER'#0 then begin
  1582.               if heretic then name:='MUS_INTR'
  1583.               else name:='D_DM2INT';
  1584.               savedir:=true;
  1585.             end
  1586.             else if (name[3]='E') and (name[5]='M') then
  1587.               if remap_mus>0 then begin
  1588.                 if heretic then begin
  1589.                   if remap_mus>27 then myhalt(ERR_TOOMAPS);
  1590.                   k:=remap_mus-1;
  1591.                   name:='MUS_ExMy';
  1592.                   name[6]:=chr(k div 9+49);
  1593.                   name[8]:=chr(k mod 9+49);
  1594.                 end
  1595.                 else begin
  1596.                   if remap_mus>32 then myhalt(ERR_TOOMAPS);
  1597.                   name:=mnames[remap_mus];
  1598.                 end;
  1599.                 inc(remap_mus);
  1600.                 inc(j);
  1601.                 savedir:=true;
  1602.               end
  1603.               else begin
  1604.                 if heretic then begin
  1605.                   k:=rlev[(ord(name[4])-49)*9+ord(name[6])-48]-1;
  1606.                   if k>=0 then begin
  1607.                     name:='MUS_ExMy';
  1608.                     name[6]:=chr(k div 9+49);
  1609.                     name[8]:=chr(k mod 9+49);
  1610.                     savedir:=true;
  1611.                   end
  1612.                 end
  1613.                 else begin
  1614.                   k:=rlev[(ord(name[4])-49)*9+ord(name[6])-48];
  1615.                   if k>0 then begin
  1616.                     name:=mnames[k];
  1617.                     savedir:=true;
  1618.                   end;
  1619.                 end;
  1620.               end;
  1621.     end; {no_conv}
  1622.  
  1623.     if nrepdirs>0 then
  1624.       for i:=1 to numentry do with dirlist[i] do
  1625.         savedir:=remap_name(repdirs,name,nrepdirs)>0;
  1626.  
  1627.     if savedir then begin
  1628.       Seek(f,head.start);
  1629.       if ioresult<>0 then myhalt(ERR_WRITED);
  1630.       BlockW(f,dirlist,numentry*sizeof(entry),size);
  1631.     end;
  1632.     if (replev=0) and (j=0) and not ignore then myhalt(ERR_NOMAPS);
  1633.     numt:=MAXENTRY+1;
  1634.     for i:=numentry downto 1 do
  1635.       if ((replaces>0) and (dirlist[i].Name=N_THINGS)) or
  1636.          (do_texture and (dirlist[i].Name=N_SIDEDEFS)) or
  1637.          (do_floor and heretic and (dirlist[i].Name=N_SECTORS)) then begin
  1638.         dec(numt);
  1639.         dirlist[numt]:=dirlist[i];
  1640.       end;
  1641.     if numt<=MAXENTRY then begin
  1642.       writeln('Processing REPLACEMENTS...');
  1643.       maxside:=(longint(numt-1)*sizeof(entry))div sizeof(sidedef);
  1644.       for i:=numt to MAXENTRY do with dirlist[i] do begin
  1645.         Seek(f,start);
  1646.         if ioresult<>0 then myhalt(ERR_READS);
  1647.         if name=N_SIDEDEFS then begin
  1648.           k:=rsize div sizeof(sidedef);
  1649.           while k>0 do begin
  1650.             j:=maxside;
  1651.             if j>k then j:=k;
  1652.             fpos:=FilePos(f);
  1653.             BlockR(f,sidedefs,j*sizeof(sidedef),size);
  1654.             for l:=1 to j do with sidedefs[l] do
  1655.               inc(repside,remap_name(reptexture,a,nreptexture)+
  1656.                           remap_name(reptexture,b,nreptexture)+
  1657.                           remap_name(reptexture,c,nreptexture));
  1658.             Seek(f,fpos);
  1659.             if ioresult<>0 then myhalt(ERR_WRITED);
  1660.             BlockW(f,sidedefs,j*sizeof(sidedef),size);
  1661.             dec(k,j);
  1662.           end;
  1663.         end
  1664.         else if name=N_THINGS then begin
  1665.           BlockR(f,things,rsize,size);
  1666.           ReplaceThings(rsize div sizeof(thing));
  1667.           Seek(f,start);
  1668.           if ioresult<>0 then myhalt(ERR_WRITED);
  1669.           BlockW(f,things,rsize,size);
  1670.         end
  1671.         else if name=N_SECTORS then begin
  1672.           BlockR(f,sectors,rsize,size);
  1673.           for j:=1 to rsize div sizeof(sector) do with sectors[j] do
  1674.             inc(repfloo,remap_name(repfloor,a,nrepfloor)+
  1675.                         remap_name(repfloor,b,nrepfloor));
  1676.           Seek(f,start);
  1677.           if ioresult<>0 then myhalt(ERR_WRITED);
  1678.           BlockW(f,sectors,rsize,size);
  1679.         end;
  1680.       end;
  1681.     end;
  1682.     Close(f);
  1683.     write('OK, Remapped:');
  1684.     Plural(replev,'level');
  1685.     write(',');
  1686.     Plural(repside,'texture');
  1687.     write(',');
  1688.     if heretic then begin
  1689.       Plural(repfloo,'floor');
  1690.       write(',');
  1691.     end;
  1692.     Plural(repthing,'object');
  1693.     writeln('.');
  1694.   end;
  1695.  
  1696. function HeapCheck(size:Word):Integer; far;
  1697.   begin
  1698.     HeapCheck:=1;
  1699.   end;
  1700.  
  1701. begin
  1702.   HeapError:=@HeapCheck;
  1703.   new(reptexture);
  1704.   new(repfloor);
  1705.   new(repdirs);
  1706.   if (reptexture=nil) or (repfloor=nil) or (repdirs=nil) then myhalt(ERR_NOMEM);
  1707.   nreptexture:=0;
  1708.   nrepfloor:=0;
  1709.   nrepdirs:=0;
  1710.   CreateTable;
  1711.   Parse;
  1712.   if heretic then begin
  1713.     CopyTable(reptexture,@htexture_table,nreptexture);
  1714.     CopyTable(repfloor,@hfloor_table,nrepfloor);
  1715.   end
  1716.   else CopyTable(reptexture,@texture_table,nreptexture);
  1717.   if show_help then Help
  1718.   else if show_list then List
  1719.   else if show_example then More
  1720.   else if show_note then Notes
  1721.   else Process;
  1722. end.
  1723.