]> de.git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/xonotic-map-compiler
nexuiz-map-compiler -> xonotic-map-compiler
[xonotic/xonotic.git] / misc / tools / xonotic-map-compiler
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use POSIX;
6 use File::Temp;
7
8 # change these to match your system, or define them in ~/.xonotic-map-compiler
9 # (just copy paste this part to the file ~/.xonotic-map-compiler)
10
11         # Path to Xonotic (where the data directory is in)
12         our $XONOTICDIR   = '/home/rpolzer/Games/Xonotic';
13
14         # Path to your q3map2 program. You find it in your GtkRadiant/install
15         # directory.
16         our $Q3MAP2      = '/home/rpolzer/Games/Xonotic/netradiant/install/q3map2.x86';
17
18         # General flags for q3map2 (for example -threads 4)
19         our $Q3MAP2FLAGS = '';
20
21         # Default flags for the -bsp stage
22         our $BSPFLAGS    = '-meta -samplesize 8 -minsamplesize 4 -mv 1000000 -mi 6000000';
23
24         # Default flags for the -vis stage
25         our $VISFLAGS    = '';
26
27         # Default flags for the -light stage
28         our $LIGHTFLAGS  = '-deluxe -patchshadows -samples 3 -lightmapsize 512 -bounce 8 -fastbounce -bouncegrid';
29
30         # Default flags for the -minimap stage
31         our $MINIMAPFLAGS = '';
32
33         # Default order of commands
34         our $ORDER = 'light,vis,minimap';
35
36 # end of user changable part
37
38 do "$ENV{HOME}/.xonotic-map-compiler";
39
40 sub Usage()
41 {
42         print <<EOF;
43 Usage:
44 $0 mapname [-bsp bspflags...] [-vis visflags...] [-light lightflags...]
45 EOF
46         exit 1;
47 }
48
49 my $options =
50 {
51         bsp => [split /\s+/, $BSPFLAGS],
52         vis => [split /\s+/, $VISFLAGS],
53         light => [split /\s+/, $LIGHTFLAGS],
54         minimap => [split /\s+/, $MINIMAPFLAGS],
55         order => [split /\s*,\s*/, $ORDER],
56         maps => [],
57         scale => 1
58 };
59
60 my $curmode = 'maps';
61
62 while(@ARGV)
63 {
64         $_ = shift @ARGV;
65         my $enterflags = undef;
66         if($_ eq '-bsp')
67         {
68                 $enterflags = 'bsp';
69         }
70         elsif($_ eq '-vis')
71         {
72                 $enterflags = 'vis';
73         }
74         elsif($_ eq '-light')
75         {
76                 $enterflags = 'light';
77         }
78         elsif($_ eq '-minimap')
79         {
80                 $enterflags = 'minimap';
81         }
82         elsif($_ eq '-map')
83         {
84                 $curmode = 'maps';
85         }
86         elsif($_ eq '-scale')
87         {
88                 $options->{scale} = (shift @ARGV) || 1;
89         }
90         elsif($_ eq '-novis')
91         {
92                 $options->{vis} = undef;
93         }
94         elsif($_ eq '-nolight')
95         {
96                 $options->{light} = undef;
97         }
98         elsif($_ eq '-nominimap')
99         {
100                 $options->{minimap} = undef;
101         }
102         elsif($_ eq '-order')
103         {
104                 $options->{order} = [split /\s*,\s*/, shift @ARGV];
105         }
106         elsif($_ =~ /^-(-.*)/)
107         {
108                 if($curmode eq 'maps')
109                 {
110                         $curmode = 'bsp';
111                 }
112                 push @{$options->{$curmode}}, $1;
113         }
114         elsif($_ =~ /^-/ and $curmode eq 'maps')
115         {
116                 $curmode = 'bsp';
117                 push @{$options->{$curmode}}, $_;
118         }
119         else
120         {
121                 push @{$options->{$curmode}}, $_;
122         }
123         if(defined $enterflags)
124         {
125                 $curmode = $enterflags;
126                 if($ARGV[0] eq '+')
127                 {
128                         shift @ARGV;
129                 }
130                 else
131                 {
132                         $options->{$curmode} = [];
133                 }
134         }
135 }
136
137 my $linkdir = File::Temp::tempdir("xonotic-map-compiler.XXXXXX", TMPDIR => 1, CLEANUP => 1);
138
139 sub q3map2(@)
140 {
141         my @args = ($Q3MAP2, split(/\s+/, $Q3MAP2FLAGS), '-game', 'xonotic', '-fs_basepath', $XONOTICDIR, '-fs_basepath', $linkdir, '-v', @_);
142         print "\$ @args\n";
143         return !system @args;
144 }
145
146 (my $mapdir = getcwd()) =~ s!/[^/]*(?:$)!!;
147 $mapdir = "/" if $mapdir eq "";
148 symlink "$mapdir", "$linkdir/data";
149
150 my ($prescale, $postscale) = ($options->{scale} =~ /^([0-9.]+)(?::([0-9.]+))?$/);
151 $postscale = 1 if not defined $postscale;
152
153 for my $m(@{$options->{maps}})
154 {
155         $m =~ s/\.(?:map|bsp)$//;
156         if($prescale != 1)
157         {
158                 open my $checkfh, "<", "$m.map"
159                         or die "open $m.map: $!";
160                 my $keeplights = 0;
161                 while(<$checkfh>)
162                 {
163                         /^\s*"_keeplights"\s+"1"\s*$/
164                                 or next;
165                         $keeplights = 1;
166                 }
167                 close $checkfh;
168                 die "$m does not define _keeplights to 1"
169                         unless $keeplights;
170         }
171
172         my %shaders = map { m!/([^/.]*)\.shader(?:$)! ? ($1 => 1) : () } glob "../scripts/*.shader";
173
174         my $previous_shaderlist = undef;
175         my $shaderlist = "";
176         if(open my $fh, "<", "$XONOTICDIR/data/xonotic-maps.pk3dir/scripts/shaderlist.txt")
177         {
178                 while(<$fh>)
179                 {
180                         $shaderlist .= $_;
181                 }
182
183                 # we may have to restore the file on exit
184                 $previous_shaderlist = $shaderlist
185                         if "$XONOTICDIR/data" eq $mapdir;
186         }
187         else
188         {
189                 # possibly extract the shader list from a pk3?
190                 local $ENV{N} = $XONOTICDIR;
191                 $shaderlist = `cd "\$N" && for X in "\$N"/data/data*.pk3; do Y=\$X; done; unzip -p "\$Y" scripts/shaderlist.txt`;
192         }
193
194         my $shaderlist_new = "";
195         for(split /\r?\n|\r/, $shaderlist)
196         {
197                 delete $shaders{$_};
198                 $shaderlist_new .= "$_\n";
199         }
200         if(%shaders)
201         {
202                 for(sort keys %shaders)
203                 {
204                         $shaderlist_new .= "$_\n";
205                 }
206         }
207         else
208         {
209                 $shaderlist_new = undef;
210         }
211
212         my $restore_shaderlist = sub
213         {
214                 if(defined $shaderlist_new)
215                 {
216                         if(defined $previous_shaderlist)
217                         {
218                                 open my $fh, ">", "$mapdir/scripts/shaderlist.txt";
219                                 print $fh $previous_shaderlist;
220                                 close $fh;
221                         }
222                         else
223                         {
224                                 unlink "$mapdir/scripts/shaderlist.txt";
225                         }
226                 }
227         };
228         local $SIG{INT} = sub
229         {
230                 print "SIGINT caught, cleaning up...\n";
231                 $restore_shaderlist->();
232                 exit 0;
233         };
234
235         eval
236         {
237                 if(defined $shaderlist_new)
238                 {
239                         mkdir "$mapdir/scripts";
240                         open my $fh, ">", "$mapdir/scripts/shaderlist.txt";
241                         print $fh $shaderlist_new;
242                         close $fh;
243                 }
244
245                 unlink <$m/lm_*>; # delete old external lightmaps
246                 q3map2 '-bsp', @{$options->{bsp}},   "$m.map"
247                         or die "-bsp: $?";
248                 if($prescale != 1)
249                 {
250                         q3map2 '-scale', $prescale, "$m.bsp"
251                                 or die "-scale: $?";
252                         rename "${m}_s.bsp", "$m.bsp"
253                                 or die "rename ${m}_s.bsp $m.bsp: $!";
254                 }
255                 my @o = @{$options->{order}};
256                 push @o, qw/light vis minimap/;
257                 my %o = ();
258
259                 for(@o)
260                 {
261                         next if $o{$_}++;
262                         if($_ eq 'light')
263                         {
264                                 if(defined $options->{light})
265                                 {
266                                         q3map2 '-light',        @{$options->{light}}, "$m.map"
267                                                 or die "-light: $?";
268                                 }
269                         }
270                         if($_ eq 'vis')
271                         {
272                                 if(defined $options->{vis})
273                                 {
274                                         q3map2 '-vis',          @{$options->{vis}},   "$m.map"
275                                                 or die "-vis: $?";
276                                 }
277                         }
278                         if($_ eq 'minimap')
279                         {
280                                 if(defined $options->{minimap})
281                                 {
282                                         q3map2 '-minimap',      @{$options->{minimap}}, "$m.map"
283                                                 or die "-minimap: $?";
284                                 }
285                         }
286                 }
287
288                 if($postscale != 1)
289                 {
290                         q3map2 '-scale', $postscale, "$m.bsp"
291                                 or die "-scale: $?";
292                         rename "${m}_s.bsp", "$m.bsp"
293                                 or die "rename ${m}_s.bsp $m.bsp: $!";
294                 }
295
296                 unlink "$m.srf";
297                 unlink "$m.prt";
298
299                 $restore_shaderlist->();
300                 1;
301         }
302         or do
303         {
304                 $restore_shaderlist->();
305                 die $@;
306         };
307 }