Source

srb2deutex / src / compose.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
/*
This file is part of DeuTex.

DeuTex incorporates code derived from DEU 5.21 that was put in the public
domain in 1994 by Raphaël Quinet and Brendon Wyber.

DeuTex is Copyright Š 1994-1995 Olivier Montanuy,
          Copyright Š 1999-2000 André Majorel.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this library; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307, USA.
*/


#include "deutex.h"
#include "tools.h"
#include "mkwad.h"
#include "texture.h"
#include "ident.h"
#include "color.h"
#include "picture.h"
#include "sound.h"
#include "text.h"

/*compile only for DeuTex*/
#if defined DeuTex

static void AddSomeJunk(const char *file);

/************Begin Tex2Ascii module *************************
**
** Translate TEXTURE1,TEXTURE2 and PNAME in
** a texture list for future modifications
*/
extern char file[MAX_PATH];

/***************End Tex2ascii module ******************/

/*
**
** make a PWAD from creation directives
** load levels,lumps,
**  create textures
** load sounds, pics, sprites, patches, flats,
*/


/*
** Can't handle PATCHES redefined from WAD
*/
Bool CMPOcopyFromWAD(Int32 *size,struct WADINFO *rwad,const char *DataDir,
	const char *Dir,const char *nam, const char *filenam)
{
	static struct WADINFO pwad;
	Int16 entry;

	if (MakeFileName(file, DataDir, Dir,"", filenam, "wad") != TRUE)
		return FALSE;

	WADRopenR(&pwad, file);
	entry = WADRfindEntry(&pwad,nam);
	if (entry >= 0)
	{
		*size = WADRwriteWADentry(rwad,&pwad,entry);
	}
	WADRclose(&pwad);

	if (entry <= 0)
		return FALSE;

	return TRUE;
}

/*
** find a picture.
** must=TRUE is picture must exist
** returns picture type
*/
Int16 CMPOloadPic(Int32 *size, struct WADINFO *rwad, char *file,
	const char *DataDir, const char *Dir, const char *nam, const char *filenam,
	Int16 Type, Int16 OfsX, Int16 OfsY)
{
	int res = PICNONE;

	if (MakeFileName(file, DataDir, Dir, "", filenam, "ppm") == TRUE)
		res = PICPPM;
	else if (MakeFileName(file, DataDir, Dir, "", filenam, "bmp") == TRUE)
		res = PICBMP;
	else if (MakeFileName(file, DataDir, Dir, "", filenam, "gif") == TRUE)
	{
#ifndef NOGIFWARNING
		static int gif_warning = 0;
#endif
		res = PICGIF;
#ifndef NOGIFWARNING
		if (!gif_warning)
		{
			Warning("GIF support may go away in the future (see");
			Warning("http://lpf.ai.mit.edu/Patents/Gif/Gif.html).");
			Warning("Switch to PPM or BMP.");
			gif_warning = 1;
		}
#endif
	}
	else if (CMPOcopyFromWAD(size, rwad, DataDir, Dir, nam, filenam) == TRUE)
		return PICWAD;

	if (res != PICNONE)
		*size = PICsaveInWAD(rwad, file, Type, OfsX, OfsY, res);
	else if (Type != PLUMP)
		Warning("Could not find file %s, .ppm, .bmp or .gif", file);

	return res;
}

struct WADINFO *CMPOrwad;
const char *CMPOwadout = NULL;

void CMPOerrorAction(void)
{
	if (CMPOwadout == NULL)
		return;
	WADRclose(CMPOrwad); /*close file*/
	Unlink(CMPOwadout);  /*delete file*/
}

void CMPOmakePWAD(const char *doomwad, WADTYPE type, const char *PWADname,
			const char *DataDir, const char *texin, NTRYB select,
			char trnR, char trnG, char trnB
#ifndef SRB2DEUTEX
			, Bool George
#endif
			)
{
	/*
	** type PWAD as we are generating a real PWAD
	*/
	Int32 start = 0, size = 0;
	static char name[8];
	static char filenam[8];
	/*PNAMES */
	Int16  nbPatchs, p;
	Bool NeedPNAME = FALSE;
	Bool FoundOne = FALSE;
	Bool Repeat;
	IMGTYPE Picture;
	/*optional insertion point*/
	Int16 X, Y;
	/*text file to read*/
	static struct TXTFILE *TXT;
	/*DOOM wad*/
	static struct WADINFO iwad, pwad;
	/*result wad file*/
	static struct WADINFO rwad;
	/*for Pnames*/
	Int16 entry;
	char *EntryP;
	Int32 EntrySz = 0;
	char *Colors;
#ifdef SRB2WAD
	Bool loadedTEXTURE1 = FALSE;
#endif
	/* initialisation*/

	Info("Translating %s into a %cWAD %s\n", texin, (type == IWAD) ? 'I' : 'P', PWADname);

	/*open iwad,get iwad directory*/
	iwad.ok = 0;
	WADRopenR(&iwad, doomwad);

	TXT = TXTopenR(texin);
	WADRopenW(&rwad, PWADname, type, 1); /* fake IWAD or real PWAD */

	/*
	** dirty: set error handler to delete the wad out file,
	** if an error occurs.
	*/
	CMPOrwad = &rwad;
	CMPOwadout = PWADname;
	ProgErrorAction(CMPOerrorAction);

	/*
		Turtle Man 11/10/07:
			I changed the order so its the same as srb2.srb
	*/

	/*
	** prepare palette for graphics
	*/
	/*find PLAYPAL*/
	if (select & (BGRAPHIC|BSPRITE|BPATCH|BFLAT))
	{
		/*should read playpal file if exist*/
		entry = WADRfindEntry(&iwad,"PLAYPAL");
		if (entry < 0)
			ProgError("Can't find PLAYPAL in IWAD");
		Colors = WADRreadEntry(&iwad, entry, &EntrySz);
		COLinit(trnR, trnG, trnB, Colors, (Int16)EntrySz);
		Free(Colors);
	}

	/*
	** initialise list of patch names
	*/
	if (select & (BTEXTUR|BPATCH))
	{
		entry = WADRfindEntry(&iwad,"PNAMES");
		if (entry < 0)
			ProgError("Can't find PNAMES in IWAD");
		EntryP = WADRreadEntry(&iwad, entry, &EntrySz);
		PNMinit(EntryP, EntrySz);
		Free(EntryP);
		NeedPNAME = FALSE;
	}

	/*
	** read texture1 and texture2
	*/
	if (select & BTEXTUR)
	{
		/*
		** read texture1
		*/
		if (TXTseekSection(TXT, "TEXTURE1"))
		{
			Phase("Making Texture1\n");
			TXUinit();
			entry = WADRfindEntry(&iwad, "TEXTURE1");
			if (entry >= 0)
			{
				EntryP=WADRreadEntry(&iwad, entry, &EntrySz);
				TXUreadTEXTURE(EntryP, EntrySz, NULL, 0, TRUE);
				Free(EntryP);
			}
			else
				Warning("Can't find TEXTURE1 in main WAD");
			FoundOne = FALSE;
			/*read TEXTURES composing TEXTURE1*/
			while (TXTentryParse(name, filenam, &X, &Y, &Repeat, TXT, FALSE) == TRUE)
			{
				if (MakeFileName(file, DataDir, "TEXTURES", "", name, "txt") == TRUE)
				{
					Detail("Reading texture file %s\n", file);
					TXUreadTexFile(file, TRUE);
					NeedPNAME = TRUE;
					FoundOne = TRUE;
				}
				else if (MakeFileName(file, DataDir, "TEXTURES", "", name, "wad") == TRUE)
				{
					Detail("Reading texture WAD %s\n", file);
					WADRopenR(&pwad, file);
					entry = WADRfindEntry(&pwad, "TEXTURE1");
					if (entry >= 0)
					{
						EntryP=WADRreadEntry(&pwad, entry, &EntrySz);
						TXUreadTEXTURE(EntryP, EntrySz, NULL, 0, TRUE);
						Free(EntryP);
						NeedPNAME=TRUE;
						FoundOne=TRUE;
					}
					WADRclose(&pwad);
				}
				else
					ProgError("Can't find texture list %s", file);
			}

			/*write texture*/
			if (FoundOne == TRUE)
			{
				WADRalign4(&rwad); /*align entry on Int32 word*/
				start = WADRposition(&rwad);
#ifdef SRB2WAD // Turtle Man: NOTE: Can't in PWAD because it breaks skin wads
				if (type == IWAD)
				{
					WADRdirAddEntry(&rwad, start, 0L, "DT_START");
					loadedTEXTURE1 = TRUE;
				}
#endif
				size = TXUwriteTEXTUREtoWAD(&rwad);
				WADRdirAddEntry(&rwad, start, size, "TEXTURE1");
			}
			TXUfree();
		}

		/*
		** read texture2
		*/
		if (TXTseekSection(TXT,"TEXTURE2"))
		{
			Phase("Making Texture2\n");
			TXUinit();
			entry = WADRfindEntry(&iwad,"TEXTURE2");
			if(entry >= 0)
			{
				EntryP = WADRreadEntry(&iwad, entry, &EntrySz);
				TXUreadTEXTURE(EntryP, EntrySz, NULL, 0, TRUE);
				Free(EntryP);
			}
			else
				Warning("Can't find TEXTURE2 in main WAD");

			FoundOne = FALSE;
			/*read TEXTURES composing TEXTURE2*/
			while (TXTentryParse(name,filenam,&X,&Y,&Repeat,TXT,FALSE) == TRUE)
			{
				if (MakeFileName(file, DataDir, "TEXTURES", "", name, "txt") == TRUE)
				{
					Detail("Reading texture file %s\n", file);
					TXUreadTexFile(file, TRUE);
					NeedPNAME = TRUE;
					FoundOne = TRUE;
				}
				else if (MakeFileName(file,DataDir,"TEXTURES","",name,"wad") == TRUE)
				{
					Detail("Reading texture WAD %s\n", file);
					WADRopenR(&pwad, file);
					entry = WADRfindEntry(&pwad, "TEXTURE2");
					if(entry >= 0)
					{
						EntryP = WADRreadEntry(&pwad, entry, &EntrySz);
						TXUreadTEXTURE(EntryP, EntrySz, NULL, 0, TRUE);
						Free(EntryP);
						NeedPNAME = TRUE;
						FoundOne = TRUE;
					}
					WADRclose(&pwad);
				}
				else
					ProgError("Can't find texture list %s",file);
			}

			/*write texture*/
			if (FoundOne == TRUE)
			{
				WADRalign4(&rwad); /*align entry on Int32 word*/
				start = WADRposition(&rwad);
#ifdef SRB2WAD
				if (type == IWAD && !loadedTEXTURE1)
					WADRdirAddEntry(&rwad, start, 0L, "DT_START");
#endif
				size = TXUwriteTEXTUREtoWAD(&rwad);
				WADRdirAddEntry(&rwad, start, size, "TEXTURE2");
			}
			TXUfree();
		}

		/*
		** PNAMES
		*/
		if (NeedPNAME) /* write PNAMES in PWAD */
		{
			/* write entry PNAMES */
			Phase("Making Pnames\n");
			WADRalign4(&rwad); /* align entry on Int32 word */
			start = WADRposition(&rwad);
			size = PNMwritePNAMEtoWAD(&rwad);
			WADRdirAddEntry(&rwad, start, size, "PNAMES");
		}
	}

	/*
	**
	**   lumps. non graphic raw data for DOOM
	*/
	if (select&BLUMP)
	{
		start = size = 0;
		if (TXTseekSection(TXT, "LUMPS"))
		{
			Phase("Making Lumps\n");
#ifdef SRB2WAD
			FoundOne = FALSE;
#endif
			while (TXTentryParse(name, filenam, &X, &Y, &Repeat, TXT, FALSE) == TRUE)
			{
#ifdef SRB2WAD
				if ((Repeat != TRUE) || (FoundOne != TRUE))
#else
				if (Repeat != TRUE)
#endif
				{
					WADRalign4(&rwad); /*align entry on Int32 word*/
					start = WADRposition(&rwad);
#ifdef SRB2WAD
					if (type == IWAD
						&& FoundOne != TRUE && !NeedPNAME) // hasn't loaded TEXTUREx
						WADRdirAddEntry(&rwad,start,0L,"DT_START");
					FoundOne=TRUE;
#endif

					{
						char dir[MAX_PATH];

						strcpy(dir, "LUMPS");
#ifdef CASESEN2
						appendLumpCase(dir, name);
#endif

						if (MakeFileName(file, DataDir, dir, "", filenam, "lmp") == TRUE)
							size = WADRwriteLump(&rwad,file);
						else
						{
							Picture=CMPOloadPic(&size, &rwad, file, DataDir, dir, name, filenam, PLUMP, X, Y);
							if (Picture == PICNONE)
								if (CMPOcopyFromWAD(&size, &rwad, DataDir, dir, name, filenam) != TRUE)
									ProgError("Can't find Lump or picture file %s.", file);
						}
					}
				}
				WADRdirAddEntry(&rwad,start,size,name);
			}
		}
	}

#ifdef SRB2WAD
	if (type == IWAD && (FoundOne == TRUE || NeedPNAME == TRUE))
	{
		start=WADRposition(&rwad);
		WADRdirAddEntry(&rwad,start,0L,"DT_END");
	}
#endif

	/*
	**  ordinary graphics
	*/
	if (select & BGRAPHIC)
	{
		start = size = 0;
		if (TXTseekSection(TXT, "GRAPHICS"))
		{
			Phase("Making Graphics\n");
#ifdef SRB2WAD
			FoundOne = FALSE;
#endif
			while (TXTentryParse(name, filenam, &X, &Y, &Repeat, TXT, TRUE) == TRUE)
			{
#ifdef SRB2WAD
				if ((Repeat != TRUE) || (FoundOne != TRUE))
#else
				if (Repeat != TRUE)
#endif
				{
					WADRalign4(&rwad); /*align entry on Int32 word*/
					start = WADRposition(&rwad);
#ifdef SRB2WAD
					if (type == IWAD && FoundOne != TRUE)
					{
						WADRdirAddEntry(&rwad, start, 0L, "GX_START");
						FoundOne = TRUE;
					}
#endif
					{
						char dir[MAX_PATH];

						strcpy(dir, "GRAPHICS");
#ifdef CASESEN2
						appendLumpCase(dir, name);
#endif
						Picture = CMPOloadPic(&size, &rwad, file, DataDir, dir,
							name, filenam, PGRAPH, X, Y);
					}
				}
				WADRdirAddEntry(&rwad, start, size, name);
			}
#ifdef SRB2WAD
			if (type == IWAD && FoundOne == TRUE)
			{
				start = WADRposition(&rwad);
				WADRdirAddEntry(&rwad, start, 0L, "GX_END");
			}
#endif
		}
	}

	/*
	** levels! add your own new levels to DOOM!
	** read level from a PWAD file
	*/
	if (select & BLEVEL)
	{
		if (TXTseekSection(TXT, "LEVELS"))
		{
#ifdef SRB2WAD
			FoundOne = FALSE;
#endif
			while (TXTentryParse(name, filenam, &X, &Y, &Repeat, TXT, FALSE) == TRUE)
			{
				p = IDENTlevel(name);
				if (p < 0)
					ProgError("Illegal level name %s", lump_name(name));
				if (MakeFileName(file, DataDir, "LEVELS", "", filenam, "wad") != TRUE)
					ProgError("Can't find Level WAD %s", file);
				Detail("Reading level WAD file %s\n",file);
#ifdef SRB2WAD
				if (FoundOne == FALSE)
					WADRdirAddEntry(&rwad, start, 0L, "LV_START");
				FoundOne = TRUE;
#endif
				WADRwriteWADlevel(&rwad, file, name);
			}
#ifdef SRB2WAD
			if (FoundOne == TRUE)
			{
				start = WADRposition(&rwad);
				WADRdirAddEntry(&rwad, start, 0L, "LV_END");
			}
#endif
		}
	}

	/*  F_START TM (was FF_START)
	**  Flats
	**  F_END   AYM 1998-12-22 (was FF_END)
	*/
	if (select & BFLAT)
	{
		if (TXTseekSection(TXT, "FLATS"))
		{
			Phase("Making Flats\n");
			FoundOne = FALSE;
			while (TXTentryParse(name, filenam, &X, &Y, &Repeat, TXT, FALSE) == TRUE)
			{
				if ((Repeat != TRUE) || (FoundOne != TRUE))
				{
					WADRalign4(&rwad); /*align entry on Int32 word*/
					start = WADRposition(&rwad);
					if (FoundOne == FALSE)
#ifdef SRB2WAD
						WADRdirAddEntry(&rwad,start,0L,"F_START");
#else
					{
						if(type==IWAD)
						{
							WADRdirAddEntry(&rwad,start,0L,"F_START");
							WADRdirAddEntry(&rwad,start,0L,"F1_START");
						}
						else
							WADRdirAddEntry(&rwad,start,0L,"FF_START");
					}
#endif
					FoundOne=TRUE;
					{
						char dir[MAX_PATH];

						strcpy(dir, "FLATS");
#ifdef CASESEN2
						appendLumpCase(dir, name);
#endif
						CMPOloadPic(&size,&rwad,file,DataDir,dir,name,
							filenam,PFLAT,INVALIDINT,INVALIDINT);
					}
				}
				WADRdirAddEntry(&rwad,start,size,name);
			}
			if (FoundOne == TRUE)
			{
				start = WADRposition(&rwad);
#ifndef SRB2WAD
				if (type == IWAD)
				{
					WADRdirAddEntry(&rwad,start,0L,"F1_END");
					WADRdirAddEntry(&rwad,start,0L,"F2_START");
					WADRdirAddEntry(&rwad,start,0L,"F2_END");
					WADRdirAddEntry(&rwad,start,0L,"F3_START");
					WADRdirAddEntry(&rwad,start,0L,"F3_END");
					WADRdirAddEntry(&rwad,start,0L,"F_END");
				}
				else
#endif
					WADRdirAddEntry(&rwad,start,0L,"F_END");  /* AYM 1998-12-22 */
			}
		}
	}

	/*
	** Try to load WALL patches
	**   even if no new textures (old patches could be redefined)
	*/
	/* write new patches  in PWAD*/
	/* read the name of the new textures and insert them*/
	/* between P_START and P_END for future completion*/

	if (select & BPATCH)
	{
		start = size = 0;
		FoundOne = FALSE;
		/*
		** First look for patches in [PATCHES]
		*/
		if (TXTseekSection(TXT, "PATCHES"))
		{
			Phase("Making Wall Patches\n");
			while (TXTentryParse(name, filenam, &X, &Y, &Repeat, TXT, TRUE) == TRUE)
			{
				if ((Repeat != TRUE) || (FoundOne != TRUE))
				{
					WADRalign4(&rwad); /*align entry on Int32 word*/
					start = WADRposition(&rwad);
					if (FoundOne == FALSE)
#ifdef SRB2WAD
						WADRdirAddEntry(&rwad,start,0L,"P_START");
#else
					{
						if (type == IWAD)
						{
							WADRdirAddEntry(&rwad,start,0L,"P_START");
							WADRdirAddEntry(&rwad,start,0L,"P1_START");
						}
						else
						{
							WADRdirAddEntry(&rwad,start,0L,"PP_START");
						}
					}
#endif
					FoundOne=TRUE;
					{
						char dir[MAX_PATH];

						strcpy(dir, "PATCHES");
#ifdef CASESEN2
						appendLumpCase(dir, name);
#endif
						CMPOloadPic(&size, &rwad, file, DataDir, dir,
							name, filenam, PPATCH, X, Y);
					}
				}
				WADRdirAddEntry(&rwad,start,size,name);
			}
		}

		/*
		** Check if all the needed patches are defined.
		*/
		nbPatchs = PNMgetNbOfPatch();
		for (p = 0; p < nbPatchs; p++)
		{
			if (PNMisNew(p) != TRUE)
			{
				continue; /*if old patch, forget it*/
			}

			PNMgetPatchName(name,p);
			Normalise(filenam,name,TRUE);
			/*search in main IWAD directory*/
			if(WADRfindEntry(&iwad, name) >= 0)
			{
				Output("Reusing DOOM entry %s as patch\n", lump_name (name));
			}
			/*search in current PWAD*/
			else if(WADRfindEntry(&rwad,name) < 0)
			{
				/*PATCH not found in current WAD, load automatically
				**from the PATCH directory
				*/
				WADRalign4(&rwad); /* align entry on Int32 word */
				start = WADRposition(&rwad);
				Picture = CMPOloadPic(&size, &rwad, file, DataDir, "PATCHES", name,
					filenam, PPATCH, INVALIDINT, INVALIDINT);
				if (Picture != PICNONE)
				{
					if(FoundOne==FALSE)
					{
						Phase("Making Wall Patches\n");
#ifdef SRB2WAD
						WADRdirAddEntry(&rwad,start,0L,"P_START");
#else
						if (type == IWAD)
						{
							WADRdirAddEntry(&rwad,start,0L,"P_START");
							WADRdirAddEntry(&rwad,start,0L,"P1_START");
						}
						else
							WADRdirAddEntry(&rwad,start,0L,"PP_START");
#endif
					}
					FoundOne = TRUE;
					WADRdirAddEntry(&rwad, start, size, name);
				}
			}
		}

		if (FoundOne == TRUE)
		{
			WADRalign4(&rwad); /*align entry on Int32 word*/
			start = WADRposition(&rwad);
#ifdef SRB2WAD
			WADRdirAddEntry(&rwad, start, 0L, "P_END");
#else
			if(type==IWAD)
			{
				WADRdirAddEntry(&rwad,start,0L,"P1_END");
				WADRdirAddEntry(&rwad,start,0L,"P2_START");
				WADRdirAddEntry(&rwad,start,0L,"P2_END");
				WADRdirAddEntry(&rwad,start,0L,"P3_START");
				WADRdirAddEntry(&rwad,start,0L,"P3_END");
				WADRdirAddEntry(&rwad,start,0L,"P_END");
			}
			else
				WADRdirAddEntry(&rwad,start,0L,"PP_END");
#endif
		}
	}

	/*
	** clear off Pnames
	*/
	if (select & (BTEXTUR|BPATCH))
	{
		PNMfree();
	}

	/*
	**   sounds. all sounds entries
	*/
	if (select & BSOUND)
	{
		start = size = 0;
		if (TXTseekSection(TXT, "SOUNDS"))
		{
			Phase("Making Sounds\n");
#ifdef SRB2WAD
			FoundOne=FALSE;
#endif
			while (TXTentryParse(name, filenam, &X, &Y, &Repeat, TXT, FALSE) == TRUE)
			{
				if (Repeat != TRUE)
				{
					WADRalign4(&rwad); /*align entry on Int32 word*/
					start = WADRposition(&rwad);
					{
						char dir[MAX_PATH];

						strcpy(dir, "SOUNDS");
#ifdef CASESEN2
						appendLumpCase(dir, name);
#endif

						if (MakeFileName(file, DataDir, dir, "", filenam, "txt") == TRUE)
						{
							size = SNDcopyPCSoundInWAD(&rwad,file);
							Detail("Read PC Sound as file %s\n", file);
						}
						else
						{
							if(MakeFileName(file, DataDir, dir, "", filenam, "wav") == TRUE)
							{
								size=SNDcopyInWAD(&rwad, file, SNDWAV);
							}
							else if(MakeFileName(file, DataDir, dir, "", filenam, "au") == TRUE)
							{
								size=SNDcopyInWAD(&rwad, file, SNDAU);
							}
							else if(MakeFileName(file, DataDir, dir, "", filenam, "voc") == TRUE)
							{
								size=SNDcopyInWAD(&rwad, file, SNDVOC);
							}
							else if(CMPOcopyFromWAD(&size, &rwad, DataDir, dir,
								name, filenam) != TRUE)
							{
								ProgError("Can't find Sound %s, AU or WAV or WAD", file);
							}
							Detail("Read Sound in file %s\n",file);
						}
					}
				}
#ifdef SRB2WAD
				if (FoundOne != TRUE)
					WADRdirAddEntry(&rwad,start,0L,"DS_START");
				FoundOne=TRUE;
#endif
				WADRdirAddEntry(&rwad,start,size,name);
			}
#ifdef SRB2WAD
			if (FoundOne == TRUE)
			{
				WADRalign4(&rwad); /*align entry on Int32 word*/
				start = WADRposition(&rwad);
				WADRdirAddEntry(&rwad,start,0L,"DS_END");
			}
#endif
		}
	}

	/*
	**
	**   Musics
	*/
	if (select & BMUSIC)
	{
		start = size = 0;
		if (TXTseekSection(TXT, "MUSICS"))
		{
			Phase("Making Musics\n");
			while (TXTentryParse(name, filenam, &X, &Y, &Repeat, TXT, FALSE) == TRUE)
			{
				if (Repeat != TRUE)
				{
					WADRalign4(&rwad); /*align entry on Int32 word*/
					start = WADRposition(&rwad);
					{
						char dir[MAX_PATH];

						strcpy(dir, "MUSICS");
#ifdef CASESEN2
						appendLumpCase(dir, name);
#endif
						/*Music*/
						if(MakeFileName(file,DataDir,dir,"",filenam,"mus")==TRUE)
						{
							size = WADRwriteLump(&rwad,file);
							Detail("Read Music as MUS file %s\n",file);
						}
#ifdef MOREMUSIC
						else if (MakeFileName(file,DataDir,dir,"",filenam,"mid")==TRUE)
						{
							size = WADRwriteLump(&rwad,file);
							Detail("Read Music as Midi file %s\n",file);
						}
						else if (MakeFileName(file,DataDir,dir,"",filenam,"ogg")==TRUE)
						{
							size = WADRwriteLump(&rwad,file);
							Detail("Read Music as OGG file %s\n",file);
						}
						else if (MakeFileName(file,DataDir,dir,"",filenam,"mp3")==TRUE)
						{
							size = WADRwriteLump(&rwad,file);
							Detail("Read Music as MP3 file %s\n",file);
						}
#endif
						else if(CMPOcopyFromWAD(&size,&rwad,DataDir,dir,name,
							filenam)!=TRUE)
							ProgError("Can't find Music %s",file);
					}
				}
				WADRdirAddEntry(&rwad,start,size,name);
			}
		}
	}

	/*
	**  SS_START
	**  sprites
	**  SS_END
	*/
	if(select&BSPRITE)
	{
		start=size=0;
		if(TXTseekSection(TXT,"SPRITES"))
		{
			Phase("Making Sprites\n");
			FoundOne=FALSE;
			while(TXTentryParse(name, filenam, &X, &Y, &Repeat, TXT, TRUE) == TRUE)
			{
				/* first sprite seen? */
				if ((Repeat != TRUE) || (FoundOne != TRUE))
				{
					WADRalign4(&rwad); /*align entry on Int32 word*/
					start = WADRposition(&rwad);
					if (FoundOne != TRUE)
#ifdef SRB2WAD
						WADRdirAddEntry(&rwad, start, 0L, "S_START");
#else
					{
						if (type == IWAD)
							WADRdirAddEntry(&rwad, start, 0L, "S_START");
						else
							WADRdirAddEntry(&rwad, start, 0L, "SS_START");
					}
#endif
					FoundOne = TRUE;

					{
						char dir[MAX_PATH];

						strcpy(dir, "SPRITES");
#ifdef CASESEN2
						appendLumpCase(dir, name);
#endif
						CMPOloadPic(&size, &rwad, file, DataDir, dir, name,
								filenam, PSPRIT, X, Y);
					}
				}
				WADRdirAddEntry(&rwad, start, size, name);
			}
			if (FoundOne == TRUE)
			{
				WADRalign4(&rwad);
				start = WADRposition(&rwad);
#ifdef SRB2WAD
				WADRdirAddEntry(&rwad,start,0L,"S_END");
#else
				if ((type == IWAD) || (George == TRUE))
					WADRdirAddEntry(&rwad,start,0L,"S_END");
				else
					WADRdirAddEntry(&rwad,start,0L,"SS_END");
#endif
			}
		}
	}

	/*
	** exit from graphic
	*/
	if (select & (BGRAPHIC|BSPRITE|BPATCH|BFLAT))
		COLfree();

	/*
	** iwad not needed anymore
	*/
	WADRclose(&iwad);

	/*
	** the end
	*/
	TXTcloseR(TXT);
	WADRwriteDir(&rwad, 1);  /* write the WAD directory */
	ProgErrorCancel();
	WADRclose(&rwad);

#if 1 // #ifndef SRB2WAD // Turtle Man: is this needed?
	/*add some junk at end of wad file, for DEU 5.21*/
	if (type == PWAD)
		AddSomeJunk(PWADname);
#endif
}

/***************** Hack the IWAD *********************/
extern Int16 HowMuchJunk;
static char Junk[] = "This junk is here for DEU 5.21. I am repeating myself anyway... ";
static void AddSomeJunk(const char *file)
{
	FILE *out;
	Int16 n;

	out = fopen(file, FOPEN_AB); /*open R/W at the end*/

	if (out == NULL)
		ProgError("Can't write file %s\n",file);

	for (n = 0; n < HowMuchJunk; n++)
	{
		if (fwrite(Junk, 1, 64, out) < 64)
		{
			Warning("can't insert my junk!");
		}
	}
	fclose(out);
}
/**************** End Hack the IWAD *******************/
#endif /*DeuTex*/