xonotic/misc/tools/xonotic-map-compiler

317 lines
6.8 KiB
Plaintext
Raw Normal View History

2010-03-18 13:22:15 +00:00
#!/usr/bin/perl
use strict;
use warnings;
use POSIX;
use File::Temp;
# change these to match your system, or define them in ~/.xonotic-map-compiler
# (just copy paste this part to the file ~/.xonotic-map-compiler)
2010-03-18 13:22:15 +00:00
# Path to Xonotic (where the data directory is in)
2013-06-23 16:35:39 +00:00
our $XONOTICDIR = getcwd();
2010-03-18 13:22:15 +00:00
# Path to your q3map2 program. You find it in your GtkRadiant/install
# directory.
2013-07-06 17:00:47 +00:00
our $Q3MAP2 = getcwd() . '/netradiant/install/q3map2';
2010-03-18 13:22:15 +00:00
# General flags for q3map2 (for example -threads 4)
2011-07-01 09:43:32 +00:00
our $Q3MAP2FLAGS = '-fs_forbiddenpath xonotic*-data*.pk3* -fs_forbiddenpath xonotic*-nexcompat*.pk3*';
2010-03-18 13:22:15 +00:00
# Default flags for the -bsp stage
our $BSPFLAGS = '-meta -maxarea -samplesize 8 -mv 1000000 -mi 6000000';
2010-03-18 13:22:15 +00:00
# Default flags for the -vis stage
our $VISFLAGS = '';
# Default flags for the -light stage
our $LIGHTFLAGS = '-lightmapsize 1024 -lightmapsearchpower 4 -deluxe -patchshadows -randomsamples -samples 4 -fast -fastbounce -dirty -bouncegrid -fill';
2010-03-18 13:22:15 +00:00
# Default flags for the -minimap stage
our $MINIMAPFLAGS = '';
# Default order of commands
our $ORDER = 'vis,light,scale';
2010-03-18 13:22:15 +00:00
# end of user changable part
do "$ENV{HOME}/.xonotic-map-compiler";
2010-03-18 13:22:15 +00:00
sub Usage()
{
print <<EOF;
Usage:
2010-07-04 17:14:39 +00:00
$0 mapname [-bsp bspflags...] [-vis visflags...] [-light lightflags...] [-minimap minimapflags]
2010-03-18 13:22:15 +00:00
EOF
exit 1;
}
my $options =
{
bsp => [split /\s+/, $BSPFLAGS],
vis => [split /\s+/, $VISFLAGS],
light => [split /\s+/, $LIGHTFLAGS],
minimap => [split /\s+/, $MINIMAPFLAGS],
2010-07-04 17:14:39 +00:00
scale => [], # can't have defaults atm
2010-03-18 13:22:15 +00:00
order => [split /\s*,\s*/, $ORDER],
maps => [],
2010-11-06 15:37:35 +00:00
scalefactor => 1,
bsp_timeout => 0,
vis_timeout => 0,
light_timeout => 0,
minimap_timeout => 0,
2013-09-13 09:51:07 +00:00
scale_timeout => 0,
timeout_stealing => 0,
2010-03-18 13:22:15 +00:00
};
my $curmode = 'maps';
while(@ARGV)
{
$_ = shift @ARGV;
my $enterflags = undef;
if($_ eq '-bsp')
{
$enterflags = 'bsp';
}
elsif($_ eq '-vis')
{
$enterflags = 'vis';
}
elsif($_ eq '-light')
{
$enterflags = 'light';
}
elsif($_ eq '-minimap')
{
$enterflags = 'minimap';
}
elsif($_ eq '-map')
{
$curmode = 'maps';
}
elsif($_ eq '-scale')
{
2010-11-06 15:37:35 +00:00
$options->{scalefactor} = @ARGV ? shift(@ARGV) : 1;
$enterflags = 'scale';
2010-03-18 13:22:15 +00:00
}
elsif($_ eq '-novis')
{
$options->{vis} = undef;
}
elsif($_ eq '-nolight')
{
$options->{light} = undef;
}
elsif($_ eq '-nominimap')
{
$options->{minimap} = undef;
}
elsif($_ eq '-bsp_timeout')
{
$options->{bsp_timeout} = shift @ARGV;
}
elsif($_ eq '-vis_timeout')
{
$options->{vis_timeout} = shift @ARGV;
}
elsif($_ eq '-light_timeout')
{
$options->{light_timeout} = shift @ARGV;
}
elsif($_ eq '-minimap_timeout')
{
$options->{minimap_timeout} = shift @ARGV;
}
elsif($_ eq '-scale_timeout')
{
2010-11-06 15:37:35 +00:00
$options->{scale_timeout} = shift @ARGV;
}
2013-09-13 09:51:07 +00:00
elsif($_ eq '-timeout_stealing')
{
$options->{timeout_stealing} = shift @ARGV;
}
2010-03-18 13:22:15 +00:00
elsif($_ eq '-order')
{
$options->{order} = [split /\s*,\s*/, shift @ARGV];
}
elsif($_ eq '-sRGB')
{
push @{$options->{bsp}}, "-sRGBtex", "-sRGBcolor";
2012-01-16 14:40:08 +00:00
push @{$options->{light}}, "-sRGBtex", "-sRGBcolor", "-sRGBlight"
if defined $options->{light};
}
elsif($_ eq '-nosRGB')
{
push @{$options->{bsp}}, "-nosRGBtex", "-nosRGBcolor";
2012-01-16 14:40:08 +00:00
push @{$options->{light}}, "-nosRGBtex", "-nosRGBcolor", "-nosRGBlight"
if defined $options->{light};
}
elsif($_ =~ /^--no(-.*)/)
{
if($curmode eq 'maps')
{
$curmode = 'bsp';
}
my $flag = $1;
@{$options->{$curmode}} = grep { (($_ eq $flag) ... /^-/) !~ /^[0-9]+$/ } @{$options->{$curmode}};
# so, e.g. --no-samplesize removes "-samplesize" and a following "3"
}
2010-03-18 13:22:15 +00:00
elsif($_ =~ /^-(-.*)/)
{
if($curmode eq 'maps')
{
$curmode = 'bsp';
}
push @{$options->{$curmode}}, $1;
}
elsif($_ =~ /^-/ and $curmode eq 'maps')
{
$curmode = 'bsp';
push @{$options->{$curmode}}, $_;
}
else
{
push @{$options->{$curmode}}, $_;
}
if(defined $enterflags)
{
$curmode = $enterflags;
if($ARGV[0] eq '+')
{
shift @ARGV;
}
else
{
$options->{$curmode} = [];
}
}
}
my $linkdir = File::Temp::tempdir("xonotic-map-compiler.XXXXXX", TMPDIR => 1, CLEANUP => 1);
2013-09-13 09:51:07 +00:00
my $starttime = time;
my $endtime = time;
2010-03-18 13:22:15 +00:00
sub q3map2(@)
{
my $mode = $_[0];
my $timeout = undef;
$timeout = $options->{bsp_timeout} if $mode eq '-bsp';
$timeout = $options->{vis_timeout} if $mode eq '-vis';
$timeout = $options->{light_timeout} if $mode eq '-light';
$timeout = $options->{minimap_timeout} if $mode eq '-minimap';
$timeout = $options->{scale_timeout} if $mode eq '-scale';
die "Invalid call: not a standard q3map2 stage" if not defined $timeout;
2013-09-13 09:51:07 +00:00
$endtime += $timeout;
my $stolen_timeout = $endtime - time;
if ($stolen_timeout > $timeout)
{
$timeout += ($stolen_timeout - $timeout) * $options->{timeout_stealing};
}
my @args = ($Q3MAP2, split(/\s+/, $Q3MAP2FLAGS), '-game', 'xonotic', '-fs_basepath', $XONOTICDIR, '-fs_basepath', $linkdir, '-v', @_);
2010-03-18 13:22:15 +00:00
print "\$ @args\n";
2013-09-13 09:51:07 +00:00
print "Using timeout: $timeout\n";
defined(my $pid = fork())
or die "fork: $!";
if($pid) # parent
{
2010-07-21 13:03:35 +00:00
local $SIG{ALRM} = sub { warn "SIGALRM caught\n"; kill TERM => $pid; };
2010-07-19 06:34:34 +00:00
alarm $timeout
if $timeout;
2010-07-19 07:16:54 +00:00
if(waitpid($pid, 0) != $pid)
{
die "waitpid: did not return our child process $pid: $!";
}
2010-07-19 07:16:54 +00:00
alarm 0;
2010-07-19 12:39:21 +00:00
return ($? == 0);
}
else # child
{
exec @args
or die "exec: $!";
}
2010-03-18 13:22:15 +00:00
}
if ($options->{scalefactor} =~ /^([0-9.]+):([0-9.]+)$/)
{
die "Two-scale isn't supported"
if $1 != 1 and $2 != 1;
$options->{scalefactor} = $1
if $2 == 1;
$options->{scalefactor} = $2
if $1 == 1;
}
2010-03-18 13:22:15 +00:00
my $origcwd = getcwd();
2010-03-18 13:22:15 +00:00
for my $m(@{$options->{maps}})
{
chdir $origcwd
or die "chdir $origcwd: $!";
if($m =~ s!(.*)/!!)
2010-03-18 13:22:15 +00:00
{
my $predir = $1;
chdir $predir
or die "chdir $predir: $!";
2010-03-18 13:22:15 +00:00
}
symlink getcwd() . "/..", "$linkdir/data"
or die "symlink $linkdir/data: $!";
2010-03-18 13:22:15 +00:00
$m =~ s/\.(?:map|bsp)$//;
2010-03-18 13:22:15 +00:00
# never hurts, may help with rtlights
unshift @{$options->{bsp}}, "-keeplights";
2010-05-06 14:58:03 +00:00
2010-03-18 13:22:15 +00:00
local $SIG{INT} = sub
{
print "SIGINT caught, cleaning up...\n";
exit 0;
};
unlink <$m/lm_*>; # delete old external lightmaps
q3map2 '-bsp', @{$options->{bsp}}, "$m.map"
or die "-bsp: $?";
my @o = @{$options->{order}};
push @o, qw/vis scale light/;
my %o = ();
2010-03-18 13:22:15 +00:00
for(@o)
{
next if $o{$_}++;
if($_ eq 'light')
2010-03-18 13:22:15 +00:00
{
if(defined $options->{light})
2010-03-18 13:22:15 +00:00
{
q3map2 '-light', @{$options->{light}}, "$m.map"
or die "-light: $?";
2010-03-18 13:22:15 +00:00
}
}
if($_ eq 'vis')
{
if(defined $options->{vis})
{
q3map2 '-vis', @{$options->{vis}}, "$m.map"
or die "-vis: $?";
}
}
if($_ eq 'scale')
{
if ($options->{scalefactor} != 1)
{
q3map2 '-scale', @{$options->{scale}}, $options->{scalefactor}, "$m.bsp"
or die "-scale: $?";
rename "${m}_s.bsp", "$m.bsp"
or die "rename ${m}_s.bsp $m.bsp: $!";
}
}
2010-03-18 13:22:15 +00:00
}
if(defined $options->{minimap})
2010-03-18 13:22:15 +00:00
{
q3map2 '-minimap', @{$options->{minimap}}, "$m.map"
or die "-minimap: $?";
}
unlink "$m.srf";
unlink "$m.prt";
2010-03-18 13:22:15 +00:00
}