-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
2224 lines (1969 loc) · 63.8 KB
/
main.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
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -------------------------------------------------------------------------------
Copyright (C) 1999-2006 Id Software, Inc. and contributors.
For a list of contributors, see the accompanying CONTRIBUTORS file.
This file is part of GtkRadiant.
GtkRadiant 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.
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-------------------------------------------------------------------------------
This code has been altered significantly from its original form, to support
several games based on the Quake III Arena engine, in the form of "Q3Map2."
------------------------------------------------------------------------------- */
/* marker */
#define MAIN_C
/* dependencies */
#include "kmap2.h"
convertType_t convertType = CONVERT_NOTHING;
/*
Random()
returns a pseudorandom number between 0 and 1
*/
vec_t Random(void)
{
return (vec_t) rand() / RAND_MAX;
}
/*
ExitQ3Map()
cleanup routine
*/
static void ExitQ3Map(void)
{
BSPFilesCleanup();
if(mapDrawSurfs != NULL)
free(mapDrawSurfs);
}
/* minimap stuff */
typedef struct minimap_s
{
bspModel_t *model;
int width;
int height;
int samples;
float *sample_offsets;
float sharpen_boxmult;
float sharpen_centermult;
float boost;
float *data1f;
float *sharpendata1f;
vec3_t mins, size;
}
minimap_t;
static minimap_t minimap;
qboolean BrushIntersectionWithLine(bspBrush_t * brush, vec3_t start, vec3_t dir, float *t_in, float *t_out)
{
int i;
qboolean in = qfalse, out = qfalse;
bspBrushSide_t *sides = &bspBrushSides[brush->firstSide];
for(i = 0; i < brush->numSides; ++i)
{
bspPlane_t *p = &bspPlanes[sides[i].planeNum];
float sn = DotProduct(start, p->normal);
float dn = DotProduct(dir, p->normal);
if(dn == 0)
{
if(sn > p->dist)
return qfalse; // outside!
}
else
{
float t = (p->dist - sn) / dn;
if(dn < 0)
{
if(!in || t > *t_in)
{
*t_in = t;
in = qtrue;
// as t_in can only increase, and t_out can only decrease, early out
if(out && *t_in >= *t_out)
return qfalse;
}
}
else
{
if(!out || t < *t_out)
{
*t_out = t;
out = qtrue;
// as t_in can only increase, and t_out can only decrease, early out
if(in && *t_in >= *t_out)
return qfalse;
}
}
}
}
return in && out;
}
static float MiniMapSample(float x, float y)
{
vec3_t org, dir;
int i, bi;
float t0, t1;
float samp;
bspBrush_t *b;
bspBrushSide_t *s;
int cnt;
org[0] = x;
org[1] = y;
org[2] = 0;
dir[0] = 0;
dir[1] = 0;
dir[2] = 1;
cnt = 0;
samp = 0;
for(i = 0; i < minimap.model->numBSPBrushes; ++i)
{
bi = minimap.model->firstBSPBrush + i;
if(opaqueBrushes[bi >> 3] & (1 << (bi & 7)))
{
b = &bspBrushes[bi];
// sort out mins/maxs of the brush
s = &bspBrushSides[b->firstSide];
if(x < -bspPlanes[s[0].planeNum].dist)
continue;
if(x > +bspPlanes[s[1].planeNum].dist)
continue;
if(y < -bspPlanes[s[2].planeNum].dist)
continue;
if(y > +bspPlanes[s[3].planeNum].dist)
continue;
if(BrushIntersectionWithLine(b, org, dir, &t0, &t1))
{
samp += t1 - t0;
++cnt;
}
}
}
return samp;
}
void RandomVector2f(float v[2])
{
do
{
v[0] = 2 * Random() - 1;
v[1] = 2 * Random() - 1;
}
while(v[0] * v[0] + v[1] * v[1] > 1);
}
static void MiniMapRandomlySupersampled(int y)
{
int x, i;
float *p = &minimap.data1f[y * minimap.width];
float ymin = minimap.mins[1] + minimap.size[1] * (y / (float)minimap.height);
float dx = minimap.size[0] / (float)minimap.width;
float dy = minimap.size[1] / (float)minimap.height;
float uv[2];
float thisval;
for(x = 0; x < minimap.width; ++x)
{
float xmin = minimap.mins[0] + minimap.size[0] * (x / (float)minimap.width);
float val = 0;
for(i = 0; i < minimap.samples; ++i)
{
RandomVector2f(uv);
thisval = MiniMapSample(xmin + (uv[0] + 0.5) * dx, /* exaggerated random pattern for better results */
ymin + (uv[1] + 0.5) * dy /* exaggerated random pattern for better results */
);
val += thisval;
}
val /= minimap.samples * minimap.size[2];
*p++ = val;
}
}
static void MiniMapSupersampled(int y)
{
int x, i;
float *p = &minimap.data1f[y * minimap.width];
float ymin = minimap.mins[1] + minimap.size[1] * (y / (float)minimap.height);
float dx = minimap.size[0] / (float)minimap.width;
float dy = minimap.size[1] / (float)minimap.height;
for(x = 0; x < minimap.width; ++x)
{
float xmin = minimap.mins[0] + minimap.size[0] * (x / (float)minimap.width);
float val = 0;
for(i = 0; i < minimap.samples; ++i)
{
float thisval = MiniMapSample(xmin + minimap.sample_offsets[2 * i + 0] * dx,
ymin + minimap.sample_offsets[2 * i + 1] * dy);
val += thisval;
}
val /= minimap.samples * minimap.size[2];
*p++ = val;
}
}
static void MiniMapNoSupersampling(int y)
{
int x;
float *p = &minimap.data1f[y * minimap.width];
float ymin = minimap.mins[1] + minimap.size[1] * ((y + 0.5) / (float)minimap.height);
for(x = 0; x < minimap.width; ++x)
{
float xmin = minimap.mins[0] + minimap.size[0] * ((x + 0.5) / (float)minimap.width);
*p++ = MiniMapSample(xmin, ymin) / minimap.size[2];
}
}
static void MiniMapSharpen(int y)
{
int x;
qboolean up = (y > 0);
qboolean down = (y < minimap.height - 1);
float *p = &minimap.data1f[y * minimap.width];
float *q = &minimap.sharpendata1f[y * minimap.width];
for(x = 0; x < minimap.width; ++x)
{
qboolean left = (x > 0);
qboolean right = (x < minimap.width - 1);
float val = p[0] * minimap.sharpen_centermult;
if(left && up)
val += p[-1 - minimap.width] * minimap.sharpen_boxmult;
if(left && down)
val += p[-1 + minimap.width] * minimap.sharpen_boxmult;
if(right && up)
val += p[+1 - minimap.width] * minimap.sharpen_boxmult;
if(right && down)
val += p[+1 + minimap.width] * minimap.sharpen_boxmult;
if(left)
val += p[-1] * minimap.sharpen_boxmult;
if(right)
val += p[+1] * minimap.sharpen_boxmult;
if(up)
val += p[-minimap.width] * minimap.sharpen_boxmult;
if(down)
val += p[+minimap.width] * minimap.sharpen_boxmult;
++p;
*q++ = val;
}
}
static void MiniMapContrastBoost(int y)
{
int x;
float *q = &minimap.data1f[y * minimap.width];
for(x = 0; x < minimap.width; ++x)
{
*q = *q * minimap.boost / ((minimap.boost - 1) * *q + 1);
++q;
}
}
void MiniMapMakeMinsMaxs(vec3_t mins_in, vec3_t maxs_in, float border, qboolean keepaspect)
{
vec3_t mins, maxs, extend;
VectorCopy(mins_in, mins);
VectorCopy(maxs_in, maxs);
// line compatible to nexuiz mapinfo
Sys_Printf("size %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
if(keepaspect)
{
VectorSubtract(maxs, mins, extend);
if(extend[1] > extend[0])
{
mins[0] -= (extend[1] - extend[0]) * 0.5;
maxs[0] += (extend[1] - extend[0]) * 0.5;
}
else
{
mins[1] -= (extend[0] - extend[1]) * 0.5;
maxs[1] += (extend[0] - extend[1]) * 0.5;
}
}
/* border: amount of black area around the image */
/* input: border, 1-2*border, border but we need border/(1-2*border) */
VectorSubtract(maxs, mins, extend);
VectorScale(extend, border / (1 - 2 * border), extend);
VectorSubtract(mins, extend, mins);
VectorAdd(maxs, extend, maxs);
VectorCopy(mins, minimap.mins);
VectorSubtract(maxs, mins, minimap.size);
// line compatible to nexuiz mapinfo
Sys_Printf("size_texcoords %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
}
/*
MiniMapSetupBrushes()
determines solid non-sky brushes in the world
*/
void MiniMapSetupBrushes(void)
{
int i, b, compileFlags;
bspBrush_t *brush;
bspShader_t *shader;
shaderInfo_t *si;
/* note it */
Sys_FPrintf(SYS_VRB, "--- MiniMapSetupBrushes ---\n");
/* allocate */
if(opaqueBrushes == NULL)
opaqueBrushes = safe_malloc(numBSPBrushes / 8 + 1);
/* clear */
memset(opaqueBrushes, 0, numBSPBrushes / 8 + 1);
numOpaqueBrushes = 0;
/* walk the list of worldspawn brushes */
for(i = 0; i < minimap.model->numBSPBrushes; i++)
{
/* get brush */
b = minimap.model->firstBSPBrush + i;
brush = &bspBrushes[b];
#if 0
/* check all sides */
compileFlags = 0;
for(j = 0; j < brush->numSides; j++)
{
/* do bsp shader calculations */
side = &bspBrushSides[brush->firstSide + j];
shader = &bspShaders[side->shaderNum];
/* get shader info */
si = ShaderInfoForShader(shader->shader);
if(si == NULL)
continue;
/* or together compile flags */
compileFlags |= si->compileFlags;
}
#else
shader = &bspShaders[brush->shaderNum];
si = ShaderInfoForShader(shader->shader);
if(si == NULL)
compileFlags = 0;
else
compileFlags = si->compileFlags;
#endif
/* determine if this brush is solid */
if((compileFlags & (C_SOLID | C_SKY)) == C_SOLID)
{
opaqueBrushes[b >> 3] |= (1 << (b & 7));
numOpaqueBrushes++;
maxOpaqueBrush = i;
}
}
/* emit some statistics */
Sys_FPrintf(SYS_VRB, "%9d solid brushes\n", numOpaqueBrushes);
}
qboolean MiniMapEvaluateSampleOffsets(int *bestj, int *bestk, float *bestval)
{
float val, dx, dy;
int j, k;
*bestj = *bestk = -1;
*bestval = 3; /* max possible val is 2 */
for(j = 0; j < minimap.samples; ++j)
for(k = j + 1; k < minimap.samples; ++k)
{
dx = minimap.sample_offsets[2 * j + 0] - minimap.sample_offsets[2 * k + 0];
dy = minimap.sample_offsets[2 * j + 1] - minimap.sample_offsets[2 * k + 1];
if(dx > +0.5)
dx -= 1;
if(dx < -0.5)
dx += 1;
if(dy > +0.5)
dy -= 1;
if(dy < -0.5)
dy += 1;
val = dx * dx + dy * dy;
if(val < *bestval)
{
*bestj = j;
*bestk = k;
*bestval = val;
}
}
return *bestval < 3;
}
void MiniMapMakeSampleOffsets(void)
{
int i, j, k, jj, kk;
float val, valj, valk, sx, sy, rx, ry;
Sys_Printf("Generating good sample offsets (this may take a while)...\n");
/* start with entirely random samples */
for(i = 0; i < minimap.samples; ++i)
{
minimap.sample_offsets[2 * i + 0] = Random();
minimap.sample_offsets[2 * i + 1] = Random();
}
for(i = 0; i < 1000; ++i)
{
if(MiniMapEvaluateSampleOffsets(&j, &k, &val))
{
sx = minimap.sample_offsets[2 * j + 0];
sy = minimap.sample_offsets[2 * j + 1];
minimap.sample_offsets[2 * j + 0] = rx = Random();
minimap.sample_offsets[2 * j + 1] = ry = Random();
if(!MiniMapEvaluateSampleOffsets(&jj, &kk, &valj))
valj = -1;
minimap.sample_offsets[2 * j + 0] = sx;
minimap.sample_offsets[2 * j + 1] = sy;
sx = minimap.sample_offsets[2 * k + 0];
sy = minimap.sample_offsets[2 * k + 1];
minimap.sample_offsets[2 * k + 0] = rx;
minimap.sample_offsets[2 * k + 1] = ry;
if(!MiniMapEvaluateSampleOffsets(&jj, &kk, &valk))
valk = -1;
minimap.sample_offsets[2 * k + 0] = sx;
minimap.sample_offsets[2 * k + 1] = sy;
if(valj > valk)
{
if(valj > val)
{
/* valj is the greatest */
minimap.sample_offsets[2 * j + 0] = rx;
minimap.sample_offsets[2 * j + 1] = ry;
i = -1;
}
else
{
/* valj is the greater and it is useless - forget it */
}
}
else
{
if(valk > val)
{
/* valk is the greatest */
minimap.sample_offsets[2 * k + 0] = rx;
minimap.sample_offsets[2 * k + 1] = ry;
i = -1;
}
else
{
/* valk is the greater and it is useless - forget it */
}
}
}
else
break;
}
}
void MergeRelativePath(char *out, const char *absolute, const char *relative)
{
const char *endpos = absolute + strlen(absolute);
while(endpos != absolute && (endpos[-1] == '/' || endpos[-1] == '\\'))
--endpos;
while(relative[0] == '.' && relative[1] == '.' && (relative[2] == '/' || relative[2] == '\\'))
{
relative += 3;
while(endpos != absolute)
{
--endpos;
if(*endpos == '/' || *endpos == '\\')
break;
}
while(endpos != absolute && (endpos[-1] == '/' || endpos[-1] == '\\'))
--endpos;
}
memcpy(out, absolute, endpos - absolute);
out[endpos - absolute] = '/';
strcpy(out + (endpos - absolute + 1), relative);
}
int MiniMapBSPMain(int argc, char **argv)
{
char minimapFilename[1024];
char basename[1024];
char path[1024];
char relativeMinimapFilename[1024];
float minimapSharpen;
float border;
byte *data4b, *p;
float *q;
int x, y;
int i;
miniMapMode_t mode;
vec3_t mins, maxs;
qboolean keepaspect;
/* arg checking */
if(argc < 2)
{
Sys_Printf
("Usage: kmap2 [-v] -minimap [-size n] [-sharpen f] [-samples n | -random n] [-o filename.png] [-minmax Xmin Ymin Zmin Xmax Ymax Zmax] <mapname>\n");
return 0;
}
/* load the BSP first */
strcpy(source, ExpandArg(argv[argc - 1]));
StripExtension(source);
DefaultExtension(source, ".bsp");
Sys_Printf("Loading %s\n", source);
BeginMapShaderFile(source);
LoadShaderInfo();
LoadBSPFile(source);
minimap.model = &bspModels[0];
VectorCopy(minimap.model->mins, mins);
VectorCopy(minimap.model->maxs, maxs);
*minimapFilename = 0;
minimapSharpen = game->miniMapSharpen;
minimap.width = minimap.height = game->miniMapSize;
border = game->miniMapBorder;
keepaspect = game->miniMapKeepAspect;
mode = game->miniMapMode;
minimap.samples = 1;
minimap.sample_offsets = NULL;
minimap.boost = 1.0;
/* process arguments */
for(i = 1; i < (argc - 1); i++)
{
if(!strcmp(argv[i], "-size"))
{
minimap.width = minimap.height = atoi(argv[i + 1]);
i++;
Sys_Printf("Image size set to %i\n", minimap.width);
}
else if(!strcmp(argv[i], "-sharpen"))
{
minimapSharpen = atof(argv[i + 1]);
i++;
Sys_Printf("Sharpening coefficient set to %f\n", minimapSharpen);
}
else if(!strcmp(argv[i], "-samples"))
{
minimap.samples = atoi(argv[i + 1]);
i++;
Sys_Printf("Samples set to %i\n", minimap.samples);
if(minimap.sample_offsets)
free(minimap.sample_offsets);
minimap.sample_offsets = malloc(2 * sizeof(*minimap.sample_offsets) * minimap.samples);
MiniMapMakeSampleOffsets();
}
else if(!strcmp(argv[i], "-random"))
{
minimap.samples = atoi(argv[i + 1]);
i++;
Sys_Printf("Random samples set to %i\n", minimap.samples);
if(minimap.sample_offsets)
free(minimap.sample_offsets);
minimap.sample_offsets = NULL;
}
else if(!strcmp(argv[i], "-border"))
{
border = atof(argv[i + 1]);
i++;
Sys_Printf("Border set to %f\n", border);
}
else if(!strcmp(argv[i], "-keepaspect"))
{
keepaspect = qtrue;
Sys_Printf("Keeping aspect ratio by letterboxing\n", border);
}
else if(!strcmp(argv[i], "-nokeepaspect"))
{
keepaspect = qfalse;
Sys_Printf("Not keeping aspect ratio\n", border);
}
else if(!strcmp(argv[i], "-o"))
{
strcpy(minimapFilename, argv[i + 1]);
i++;
Sys_Printf("Output file name set to %s\n", minimapFilename);
}
else if(!strcmp(argv[i], "-minmax") && i < (argc - 7))
{
mins[0] = atof(argv[i + 1]);
mins[1] = atof(argv[i + 2]);
mins[2] = atof(argv[i + 3]);
maxs[0] = atof(argv[i + 4]);
maxs[1] = atof(argv[i + 5]);
maxs[2] = atof(argv[i + 6]);
i += 6;
Sys_Printf("Map mins/maxs overridden\n");
}
else if(!strcmp(argv[i], "-black"))
{
mode = MINIMAP_MODE_BLACK;
Sys_Printf("Writing as black alpha image\n");
}
else if(!strcmp(argv[i], "-white"))
{
mode = MINIMAP_MODE_WHITE;
Sys_Printf("Writing as white alpha image\n");
}
else if(!strcmp(argv[i], "-boost"))
{
minimap.boost = atof(argv[i + 1]);
i++;
Sys_Printf("Contrast boost set to %f\n", minimap.boost);
}
}
MiniMapMakeMinsMaxs(mins, maxs, border, keepaspect);
if(!*minimapFilename)
{
ExtractFileBase(source, basename);
ExtractFilePath(source, path);
sprintf(relativeMinimapFilename, game->miniMapNameFormat, basename);
MergeRelativePath(minimapFilename, path, relativeMinimapFilename);
Sys_Printf("Output file name automatically set to %s\n", minimapFilename);
}
ExtractFilePath(minimapFilename, path);
if (path[0])
Q_mkdir(path);
if(minimapSharpen >= 0)
{
minimap.sharpen_centermult = 8 * minimapSharpen + 1;
minimap.sharpen_boxmult = -minimapSharpen;
}
minimap.data1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
data4b = safe_malloc(minimap.width * minimap.height * 4);
if(minimapSharpen >= 0)
minimap.sharpendata1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
MiniMapSetupBrushes();
if(minimap.samples <= 1)
{
Sys_Printf("\n--- MiniMapNoSupersampling (%d) ---\n", minimap.height);
RunThreadsOnIndividual(minimap.height, qtrue, MiniMapNoSupersampling);
}
else
{
if(minimap.sample_offsets)
{
Sys_Printf("\n--- MiniMapSupersampled (%d) ---\n", minimap.height);
RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSupersampled);
}
else
{
Sys_Printf("\n--- MiniMapRandomlySupersampled (%d) ---\n", minimap.height);
RunThreadsOnIndividual(minimap.height, qtrue, MiniMapRandomlySupersampled);
}
}
if(minimap.boost != 1.0)
{
Sys_Printf("\n--- MiniMapContrastBoost (%d) ---\n", minimap.height);
RunThreadsOnIndividual(minimap.height, qtrue, MiniMapContrastBoost);
}
if(minimap.sharpendata1f)
{
Sys_Printf("\n--- MiniMapSharpen (%d) ---\n", minimap.height);
RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSharpen);
q = minimap.sharpendata1f;
}
else
{
q = minimap.data1f;
}
Sys_Printf("\nConverting...");
switch (mode)
{
case MINIMAP_MODE_BLACK:
p = data4b;
for(y = 0; y < minimap.height; ++y)
for(x = 0; x < minimap.width; ++x)
{
byte b;
float v = *q++;
if(v < 0)
v = 0;
if(v > 255.0 / 256.0)
v = 255.0 / 256.0;
b = v * 256;
*p++ = 0;
*p++ = 0;
*p++ = 0;
*p++ = b;
}
Sys_Printf(" writing to %s...", minimapFilename);
WritePNG(minimapFilename, data4b, minimap.width, minimap.height, qtrue);
break;
case MINIMAP_MODE_WHITE:
p = data4b;
for(y = 0; y < minimap.height; ++y)
for(x = 0; x < minimap.width; ++x)
{
byte b;
float v = *q++;
if(v < 0)
v = 0;
if(v > 255.0 / 256.0)
v = 255.0 / 256.0;
b = v * 256;
*p++ = 255;
*p++ = 255;
*p++ = 255;
*p++ = b;
}
Sys_Printf(" writing to %s...", minimapFilename);
WritePNG(minimapFilename, data4b, minimap.width, minimap.height, qtrue);
break;
}
Sys_Printf(" done.\n");
/* return to sender */
return 0;
}
/*
AnalyzeBSP() - ydnar
analyzes a Quake engine BSP file
*/
typedef struct abspHeader_s
{
char ident[4];
int version;
bspLump_t lumps[1]; /* unknown size */
}
abspHeader_t;
typedef struct abspLumpTest_s
{
int radix, minCount;
char *name;
}
abspLumpTest_t;
int AnalyzeBSP(int argc, char **argv)
{
abspHeader_t *header;
int size, i, version, offset, length, lumpInt, count;
char ident[5];
void *lump;
float lumpFloat;
char lumpString[1024], source[1024];
qboolean lumpSwap = qfalse;
abspLumpTest_t *lumpTest;
static abspLumpTest_t lumpTests[] = {
{sizeof(bspPlane_t), 6, "IBSP LUMP_PLANES"}
,
{sizeof(bspBrush_t), 1, "IBSP LUMP_BRUSHES"}
,
{8, 6, "IBSP LUMP_BRUSHSIDES"}
,
{sizeof(bspBrushSide_t), 6, "RBSP LUMP_BRUSHSIDES"}
,
{sizeof(bspModel_t), 1, "IBSP LUMP_MODELS"}
,
{sizeof(bspNode_t), 2, "IBSP LUMP_NODES"}
,
{sizeof(bspLeaf_t), 1, "IBSP LUMP_LEAFS"}
,
{104, 3, "IBSP LUMP_DRAWSURFS"}
,
{44, 3, "IBSP LUMP_DRAWVERTS"}
,
{4, 6, "IBSP LUMP_DRAWINDEXES"}
,
{128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS"}
,
{256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)"}
,
{512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)"}
,
{0, 0, NULL}
};
/* arg checking */
if(argc < 1)
{
Sys_Printf("Usage: kmap -analyze [-lumpswap] [-v] <mapname>\n");
return 0;
}
/* process arguments */
for(i = 1; i < (argc - 1); i++)
{
/* -format map|ase|... */
if(!strcmp(argv[i], "-lumpswap"))
{
Sys_Printf("Swapped lump structs enabled\n");
lumpSwap = qtrue;
}
}
/* clean up map name */
strcpy(source, ExpandArg(argv[i]));
Sys_Printf("Loading %s\n", source);
/* load the file */
size = LoadFile(source, (void **)&header);
if(size == 0 || header == NULL)
{
Sys_Printf("Unable to load %s.\n", source);
return -1;
}
/* analyze ident/version */
memcpy(ident, header->ident, 4);
ident[4] = '\0';
version = LittleLong(header->version);
Sys_Printf("Identity: %s\n", ident);
Sys_Printf("Version: %d\n", version);
Sys_Printf("---------------------------------------\n");
/* analyze each lump */
for(i = 0; i < 100; i++)
{
/* call of duty swapped lump pairs */
if(lumpSwap)
{
offset = LittleLong(header->lumps[i].length);
length = LittleLong(header->lumps[i].offset);
}
/* standard lump pairs */
else
{
offset = LittleLong(header->lumps[i].offset);
length = LittleLong(header->lumps[i].length);
}
/* extract data */
lump = (byte *) header + offset;
lumpInt = LittleLong((int)*((int *)lump));
lumpFloat = LittleFloat((float)*((float *)lump));
memcpy(lumpString, (char *)lump, (length < 1024 ? length : 1024));
lumpString[1024] = '\0';
/* print basic lump info */
Sys_Printf("Lump: %d\n", i);
Sys_Printf("Offset: %d bytes\n", offset);
Sys_Printf("Length: %d bytes\n", length);
/* only operate on valid lumps */
if(length > 0)
{
/* print data in 4 formats */
Sys_Printf("As hex: %08X\n", lumpInt);
Sys_Printf("As int: %d\n", lumpInt);
Sys_Printf("As float: %f\n", lumpFloat);
Sys_Printf("As string: %s\n", lumpString);
/* guess lump type */
if(lumpString[0] == '{' && lumpString[2] == '"')
Sys_Printf("Type guess: IBSP LUMP_ENTITIES\n");
else if(strstr(lumpString, "textures/"))
Sys_Printf("Type guess: IBSP LUMP_SHADERS\n");
else
{
/* guess based on size/count */
for(lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++)
{
if((length % lumpTest->radix) != 0)
continue;
count = length / lumpTest->radix;
if(count < lumpTest->minCount)
continue;
Sys_Printf("Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix);
}
}
}
Sys_Printf("---------------------------------------\n");
/* end of file */
if(offset + length >= size)
break;
}
/* last stats */
Sys_Printf("Lump count: %d\n", i + 1);
Sys_Printf("File size: %d bytes\n", size);
/* return to caller */
return 0;
}
/*
BSPInfo()
emits statistics about the bsp file
*/
int BSPInfo(int count, char **fileNames)
{
int i;
char source[1024], ext[64];
int size;
FILE *f;
/* dummy check */
if(count < 1)
{
Sys_Printf("No files to dump info for.\n");
return -1;
}
/* enable info mode */
infoMode = qtrue;
/* walk file list */
for(i = 0; i < count; i++)
{
Sys_Printf("---------------------------------\n");
/* mangle filename and get size */
strcpy(source, fileNames[i]);
ExtractFileExtension(source, ext);
if(!Q_stricmp(ext, "map"))
StripExtension(source);
DefaultExtension(source, ".bsp");
f = fopen(source, "rb");
if(f)
{
size = Q_filelength(f);
fclose(f);
}
else
size = 0;
/* load the bsp file and print lump sizes */
Sys_Printf("%s\n", source);