]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/nexuiz-map-compiler
updated the quality configs to be much more appropriate
[divverent/nexuiz.git] / misc / nexuiz-map-compiler
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use POSIX;
6
7 # change these to match your system, or define them in ~/.nexuiz-map-compiler
8 # (just copy paste this part to the file ~/.nexuiz-map-compiler)
9
10         # Path to Nexuiz (where the data directory is in)
11         our $NEXUIZDIR   = '/home/polzer/Nexvn/nexuiz';
12
13         # Path to your q3map2 program. You find it in your GtkRadiant/install
14         # directory.
15         our $Q3MAP2      = '/home/users4/ommz/polzer/bin/q3map2.x86';
16
17         # General flags for q3map2 (for example -threads 4)
18         our $Q3MAP2FLAGS = '';
19
20         # Default flags for the -bsp stage
21         our $BSPFLAGS    = '-samplesize 8';
22
23         # Default flags for the -vis stage
24         our $VISFLAGS    = '';
25
26         # Default flags for the -light stage
27         our $LIGHTFLAGS  = '-deluxe -patchshadows -samples 3';
28
29 # end of user changable part
30
31 do "$ENV{HOME}/.nexuiz-map-compiler";
32
33 sub Usage()
34 {
35         print <<EOF;
36 Usage:
37 $0 mapname [-bsp bspflags...] [-vis visflags...] [-light lightflags...]
38 EOF
39         exit 1;
40 }
41
42 my $options =
43 {
44         bsp => [split /\s+/, $BSPFLAGS],
45         vis => [split /\s+/, $VISFLAGS],
46         light => [split /\s+/, $LIGHTFLAGS],
47         maps => [],
48         scale => 1
49 };
50
51 my $curmode = 'maps';
52
53 while(@ARGV)
54 {
55         $_ = shift @ARGV;
56         my $enterflags = undef;
57         if($_ eq '-bsp' or $_ eq '-meta')
58         {
59                 $enterflags = 'bsp';
60         }
61         elsif($_ eq '-vis')
62         {
63                 $enterflags = 'vis';
64         }
65         elsif($_ eq '-light')
66         {
67                 $enterflags = 'light';
68         }
69         elsif($_ eq '-map')
70         {
71                 $curmode = 'maps';
72         }
73         elsif($_ eq '-scale')
74         {
75                 $options->{scale} = (shift @ARGV) || 1;
76         }
77         elsif($_ =~ /^-/ and $curmode eq 'maps')
78         {
79                 $curmode = 'bsp';
80                 push @{$options->{$curmode}}, $_;
81         }
82         else
83         {
84                 push @{$options->{$curmode}}, $_;
85         }
86         if(defined $enterflags)
87         {
88                 $curmode = $enterflags;
89                 if($ARGV[0] eq '+')
90                 {
91                         shift @ARGV;
92                 }
93                 else
94                 {
95                         $options->{$curmode} = [];
96                 }
97         }
98 }
99
100 sub q3map2(@)
101 {
102         my @args = ($Q3MAP2, split(/\s+/, $Q3MAP2FLAGS), '-game', 'quake3', '-fs_basepath', $NEXUIZDIR, '-fs_game', 'data', '-v', @_);
103         print "\$ @args\n";
104         return !system @args;
105 }
106
107 (my $mapdir = getcwd()) =~ s!/[^/]*(?:$)!!;
108
109 unlink "$ENV{HOME}/.q3a/data";
110 mkdir "$ENV{HOME}/.q3a";
111 symlink "$mapdir", "$ENV{HOME}/.q3a/data"
112         or die "Setting up directory structure, $mapdir -> $ENV{HOME}/.q3a/data: $!";
113
114 my ($prescale, $postscale) = ($options->{scale} =~ /^([0-9.]+)(?::([0-9.]+))?$/);
115 $postscale = 1 if not defined $postscale;
116
117 for my $m(@{$options->{maps}})
118 {
119         if($prescale != 1 or $postscale != 1)
120         {
121                 open my $checkfh, "<", "$m.map"
122                         or die "open $m.map: $!";
123                 my $keeplights = 0;
124                 while(<$checkfh>)
125                 {
126                         /^\s*"_keeplights"\s+"1"\s*$/
127                                 or next;
128                         $keeplights = 1;
129                 }
130                 close $checkfh;
131                 die "$m does not define _keeplights to 1"
132                         unless $keeplights;
133         }
134
135         my %shaders = map { m!/([^/.]*)\.shader(?:$)! ? ($1 => 1) : () } glob "../scripts/*.shader";
136         open my $shaderlist, "+<", "$NEXUIZDIR/data/scripts/shaderlist.txt"
137                 or die "open $NEXUIZDIR/data/scripts/shaderlist.txt: $!";
138         my $previous_shaderlist = "";
139         while(<$shaderlist>)
140         {
141                 $previous_shaderlist .= $_;
142                 y/\r\n//d;
143                 delete $shaders{$_};
144         }
145         my $restore_shaderlist = sub
146         {
147                 open $shaderlist, ">", "$NEXUIZDIR/data/scripts/shaderlist.txt";
148                 print $shaderlist $previous_shaderlist;
149                 close $shaderlist;
150         };
151         local $SIG{INT} = sub
152         {
153                 print "SIGINT caught, cleaning up...\n";
154                 $restore_shaderlist->();
155                 exit 0;
156         };
157         eval
158         {
159                 for(keys %shaders)
160                 {
161                         print $shaderlist "$_\n";
162                 }
163                 close $shaderlist;
164
165                 q3map2 '-bsp', '-meta', @{$options->{bsp}},   "$m.map"
166                         or die "-bsp: $?";
167                 if($prescale != 1)
168                 {
169                         q3map2 '-scale', $prescale, "$m.bsp"
170                                 or die "-scale: $?";
171                         rename "${m}_s.bsp", "$m.bsp"
172                                 or die "rename ${m}_s.bsp $m.bsp: $!";
173                 }
174                 q3map2 '-vis',          @{$options->{vis}},   "$m.map"
175                         or die "-vis: $?";
176                 q3map2 '-light',        @{$options->{light}}, "$m.map"
177                         or die "-light: $?";
178                 if($postscale != 1)
179                 {
180                         q3map2 '-scale', $postscale, "$m.bsp"
181                                 or die "-scale: $?";
182                         rename "${m}_s.bsp", "$m.bsp"
183                                 or die "rename ${m}_s.bsp $m.bsp: $!";
184                 }
185
186                 unlink "$m.srf";
187
188                 $restore_shaderlist->();
189                 1;
190         }
191         or do
192         {
193                 $restore_shaderlist->();
194                 die $@;
195         };
196 }