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)
11 # Path to Xonotic (where the data directory is in)
12 our $XONOTICDIR = '/home/rpolzer/Games/Xonotic';
14 # Path to your q3map2 program. You find it in your GtkRadiant/install
16 our $Q3MAP2 = '/home/rpolzer/Games/Xonotic/netradiant/install/q3map2.x86';
18 # General flags for q3map2 (for example -threads 4)
19 our $Q3MAP2FLAGS = '-fs_forbiddenpath xonotic-data.pk3 -fs_forbiddenpath xonotic-data.pk3dir -fs_forbiddenpath xonotic-nexcompat.pk3 -fs_forbiddenpath xonotic-nexcompat.pk3dir';
21 # Default flags for the -bsp stage
22 our $BSPFLAGS = '-meta -maxarea -samplesize 8 -minsamplesize 4 -mv 1000000 -mi 6000000';
24 # Default flags for the -vis stage
27 # Default flags for the -light stage
28 our $LIGHTFLAGS = '-lightmapsearchpower 3 -deluxe -patchshadows -samples 3 -lightmapsize 512 -fast -fastbounce -dirty -bouncegrid';
30 # Default flags for the -minimap stage
31 our $MINIMAPFLAGS = '';
33 # Default order of commands
34 our $ORDER = 'light,vis,minimap';
36 # end of user changable part
38 do "$ENV{HOME}/.xonotic-map-compiler";
44 $0 mapname [-bsp bspflags...] [-vis visflags...] [-light lightflags...] [-minimap minimapflags]
51 bsp => [split /\s+/, $BSPFLAGS],
52 vis => [split /\s+/, $VISFLAGS],
53 light => [split /\s+/, $LIGHTFLAGS],
54 minimap => [split /\s+/, $MINIMAPFLAGS],
55 scale => [], # can't have defaults atm
56 order => [split /\s*,\s*/, $ORDER],
71 my $enterflags = undef;
82 $enterflags = 'light';
84 elsif($_ eq '-minimap')
86 $enterflags = 'minimap';
94 $options->{scale} = (shift @ARGV) || 1;
95 $enterflags = 'scale';
99 $options->{vis} = undef;
101 elsif($_ eq '-nolight')
103 $options->{light} = undef;
105 elsif($_ eq '-nominimap')
107 $options->{minimap} = undef;
109 elsif($_ eq '-noshaderlist')
111 $options->{noshaderlist} = 1;
113 elsif($_ eq '-bsp_timeout')
115 $options->{bsp_timeout} = shift @ARGV;
117 elsif($_ eq '-vis_timeout')
119 $options->{vis_timeout} = shift @ARGV;
121 elsif($_ eq '-light_timeout')
123 $options->{light_timeout} = shift @ARGV;
125 elsif($_ eq '-minimap_timeout')
127 $options->{minimap_timeout} = shift @ARGV;
129 elsif($_ eq '-scale_timeout')
131 $options->{minimap_timeout} = shift @ARGV;
133 elsif($_ eq '-order')
135 $options->{order} = [split /\s*,\s*/, shift @ARGV];
137 elsif($_ =~ /^--no(-.*)/)
139 if($curmode eq 'maps')
144 @{$options->{$curmode}} = grep { (($_ eq $flag) ... /^-/) !~ /^[0-9]+$/ } @{$options->{$curmode}};
145 # so, e.g. --no-samplesize removes "-samplesize" and a following "3"
147 elsif($_ =~ /^-(-.*)/)
149 if($curmode eq 'maps')
153 push @{$options->{$curmode}}, $1;
155 elsif($_ =~ /^-/ and $curmode eq 'maps')
158 push @{$options->{$curmode}}, $_;
162 push @{$options->{$curmode}}, $_;
164 if(defined $enterflags)
166 $curmode = $enterflags;
173 $options->{$curmode} = [];
178 my $linkdir = File::Temp::tempdir("xonotic-map-compiler.XXXXXX", TMPDIR => 1, CLEANUP => 1);
184 $timeout = $options->{bsp_timeout} if $mode eq '-bsp';
185 $timeout = $options->{vis_timeout} if $mode eq '-vis';
186 $timeout = $options->{light_timeout} if $mode eq '-light';
187 $timeout = $options->{minimap_timeout} if $mode eq '-minimap';
188 $timeout = $options->{scale_timeout} if $mode eq '-scale';
189 die "Invalid call: not a standard q3map2 stage" if not defined $timeout;
190 my @args = ($Q3MAP2, split(/\s+/, $Q3MAP2FLAGS), '-game', 'xonotic', '-fs_basepath', $XONOTICDIR, '-fs_basepath', $linkdir, '-v', @_);
192 defined(my $pid = fork())
196 local $SIG{ALRM} = sub { warn "SIGALRM caught\n"; kill TERM => $pid; };
199 if(waitpid($pid, 0) != $pid)
201 die "waitpid: did not return our child process $pid: $!";
213 (my $mapdir = getcwd()) =~ s!/[^/]*(?:$)!!;
214 $mapdir = "/" if $mapdir eq "";
215 symlink "$mapdir", "$linkdir/data";
217 my ($prescale, $postscale) = ($options->{scale} =~ /^([0-9.]+)(?::([0-9.]+))?$/);
218 $postscale = 1 if not defined $postscale;
220 for my $m(@{$options->{maps}})
222 $m =~ s/\.(?:map|bsp)$//;
225 open my $checkfh, "<", "$m.map"
226 or die "open $m.map: $!";
230 /^\s*"_keeplights"\s+"1"\s*$/
235 die "$m does not define _keeplights to 1"
239 my %shaders = map { m!/([^/.]*)\.shader(?:$)! ? ($1 => 1) : () } glob "../scripts/*.shader";
241 my $restore_shaderlist = sub { };
242 if(!$options->{noshaderlist})
244 my $previous_shaderlist = undef;
246 if(open my $fh, "<", "$XONOTICDIR/data/scripts/shaderlist.txt")
253 # we may have to restore the file on exit
254 $previous_shaderlist = $shaderlist
255 if "$XONOTICDIR/data" eq $mapdir;
259 # possibly extract the shader list from a pk3?
260 local $ENV{N} = $XONOTICDIR;
261 $shaderlist = `cd "\$N" && for X in "\$N"/data/data*.pk3; do Y=\$X; done; unzip -p "\$Y" scripts/shaderlist.txt`;
264 my $shaderlist_new = "";
265 for(split /\r?\n|\r/, $shaderlist)
268 $shaderlist_new .= "$_\n";
272 for(sort keys %shaders)
274 $shaderlist_new .= "$_\n";
279 $shaderlist_new = undef;
282 $restore_shaderlist = sub
284 if(defined $shaderlist_new)
286 if(defined $previous_shaderlist)
288 open my $fh, ">", "$mapdir/scripts/shaderlist.txt";
289 print $fh $previous_shaderlist;
294 unlink "$mapdir/scripts/shaderlist.txt";
299 if(defined $shaderlist_new)
301 mkdir "$mapdir/scripts";
302 open my $fh, ">", "$mapdir/scripts/shaderlist.txt";
303 print $fh $shaderlist_new;
308 local $SIG{INT} = sub
310 print "SIGINT caught, cleaning up...\n";
311 $restore_shaderlist->();
317 unlink <$m/lm_*>; # delete old external lightmaps
318 q3map2 '-bsp', @{$options->{bsp}}, "$m.map"
322 q3map2 '-scale', @{$options->{scale}}, $prescale, "$m.bsp"
324 rename "${m}_s.bsp", "$m.bsp"
325 or die "rename ${m}_s.bsp $m.bsp: $!";
327 my @o = @{$options->{order}};
328 push @o, qw/light vis minimap/;
336 if(defined $options->{light})
338 q3map2 '-light', @{$options->{light}}, "$m.map"
344 if(defined $options->{vis})
346 q3map2 '-vis', @{$options->{vis}}, "$m.map"
352 if(defined $options->{minimap})
354 q3map2 '-minimap', @{$options->{minimap}}, "$m.map"
355 or die "-minimap: $?";
362 q3map2 '-scale', @{$options->{scale}}, $postscale, "$m.bsp"
364 rename "${m}_s.bsp", "$m.bsp"
365 or die "rename ${m}_s.bsp $m.bsp: $!";
371 $restore_shaderlist->();
376 $restore_shaderlist->();