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 = '';
21 # Default flags for the -bsp stage
22 our $BSPFLAGS = '-meta -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 = '-deluxe -patchshadows -samples 3 -lightmapsize 512 -bounce 8 -fastbounce -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...]
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],
65 my $enterflags = undef;
76 $enterflags = 'light';
78 elsif($_ eq '-minimap')
80 $enterflags = 'minimap';
88 $options->{scale} = (shift @ARGV) || 1;
92 $options->{vis} = undef;
94 elsif($_ eq '-nolight')
96 $options->{light} = undef;
98 elsif($_ eq '-nominimap')
100 $options->{minimap} = undef;
102 elsif($_ eq '-order')
104 $options->{order} = [split /\s*,\s*/, shift @ARGV];
106 elsif($_ =~ /^-(-.*)/)
108 if($curmode eq 'maps')
112 push @{$options->{$curmode}}, $1;
114 elsif($_ =~ /^-/ and $curmode eq 'maps')
117 push @{$options->{$curmode}}, $_;
121 push @{$options->{$curmode}}, $_;
123 if(defined $enterflags)
125 $curmode = $enterflags;
132 $options->{$curmode} = [];
137 my $linkdir = File::Temp::tempdir("xonotic-map-compiler.XXXXXX", TMPDIR => 1, CLEANUP => 1);
141 my @args = ($Q3MAP2, split(/\s+/, $Q3MAP2FLAGS), '-game', 'xonotic', '-fs_basepath', $XONOTICDIR, '-fs_basepath', $linkdir, '-v', @_);
143 return !system @args;
146 (my $mapdir = getcwd()) =~ s!/[^/]*(?:$)!!;
147 $mapdir = "/" if $mapdir eq "";
148 symlink "$mapdir", "$linkdir/data";
150 my ($prescale, $postscale) = ($options->{scale} =~ /^([0-9.]+)(?::([0-9.]+))?$/);
151 $postscale = 1 if not defined $postscale;
153 for my $m(@{$options->{maps}})
155 $m =~ s/\.(?:map|bsp)$//;
158 open my $checkfh, "<", "$m.map"
159 or die "open $m.map: $!";
163 /^\s*"_keeplights"\s+"1"\s*$/
168 die "$m does not define _keeplights to 1"
172 my %shaders = map { m!/([^/.]*)\.shader(?:$)! ? ($1 => 1) : () } glob "../scripts/*.shader";
174 my $previous_shaderlist = undef;
176 if(open my $fh, "<", "$XONOTICDIR/data/xonotic-maps.pk3dir/scripts/shaderlist.txt")
183 # we may have to restore the file on exit
184 $previous_shaderlist = $shaderlist
185 if "$XONOTICDIR/data" eq $mapdir;
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`;
194 my $shaderlist_new = "";
195 for(split /\r?\n|\r/, $shaderlist)
198 $shaderlist_new .= "$_\n";
202 for(sort keys %shaders)
204 $shaderlist_new .= "$_\n";
209 $shaderlist_new = undef;
212 my $restore_shaderlist = sub
214 if(defined $shaderlist_new)
216 if(defined $previous_shaderlist)
218 open my $fh, ">", "$mapdir/scripts/shaderlist.txt";
219 print $fh $previous_shaderlist;
224 unlink "$mapdir/scripts/shaderlist.txt";
228 local $SIG{INT} = sub
230 print "SIGINT caught, cleaning up...\n";
231 $restore_shaderlist->();
237 if(defined $shaderlist_new)
239 mkdir "$mapdir/scripts";
240 open my $fh, ">", "$mapdir/scripts/shaderlist.txt";
241 print $fh $shaderlist_new;
245 unlink <$m/lm_*>; # delete old external lightmaps
246 q3map2 '-bsp', @{$options->{bsp}}, "$m.map"
250 q3map2 '-scale', $prescale, "$m.bsp"
252 rename "${m}_s.bsp", "$m.bsp"
253 or die "rename ${m}_s.bsp $m.bsp: $!";
255 my @o = @{$options->{order}};
256 push @o, qw/light vis minimap/;
264 if(defined $options->{light})
266 q3map2 '-light', @{$options->{light}}, "$m.map"
272 if(defined $options->{vis})
274 q3map2 '-vis', @{$options->{vis}}, "$m.map"
280 if(defined $options->{minimap})
282 q3map2 '-minimap', @{$options->{minimap}}, "$m.map"
283 or die "-minimap: $?";
290 q3map2 '-scale', $postscale, "$m.bsp"
292 rename "${m}_s.bsp", "$m.bsp"
293 or die "rename ${m}_s.bsp $m.bsp: $!";
299 $restore_shaderlist->();
304 $restore_shaderlist->();