]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/nexuiz-map-compiler
make nexuiz-map-compiler use external lightmaps
[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 -minsamplesize 4 -mv 1000000 -mi 6000000';
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 -lightmapsize 512 -bounce 8 -fastbounce -bouncegrid';
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($_ eq '-novis')
78         {
79                 $options->{vis} = undef;
80         }
81         elsif($_ eq '-nolight')
82         {
83                 $options->{light} = undef;
84         }
85         elsif($_ =~ /^-/ and $curmode eq 'maps')
86         {
87                 $curmode = 'bsp';
88                 push @{$options->{$curmode}}, $_;
89         }
90         else
91         {
92                 push @{$options->{$curmode}}, $_;
93         }
94         if(defined $enterflags)
95         {
96                 $curmode = $enterflags;
97                 if($ARGV[0] eq '+')
98                 {
99                         shift @ARGV;
100                 }
101                 else
102                 {
103                         $options->{$curmode} = [];
104                 }
105         }
106 }
107
108 sub q3map2(@)
109 {
110         my @args = ($Q3MAP2, split(/\s+/, $Q3MAP2FLAGS), '-game', 'quake3', '-fs_basepath', $NEXUIZDIR, '-fs_game', 'data', '-v', @_);
111         print "\$ @args\n";
112         return !system @args;
113 }
114
115 (my $mapdir = getcwd()) =~ s!/[^/]*(?:$)!!;
116 $mapdir = "/" if $mapdir eq "";
117
118 unlink "$ENV{HOME}/.q3a/data";
119 mkdir "$ENV{HOME}/.q3a";
120 symlink "$mapdir", "$ENV{HOME}/.q3a/data"
121         or die "Setting up directory structure, $mapdir -> $ENV{HOME}/.q3a/data: $!";
122
123 my ($prescale, $postscale) = ($options->{scale} =~ /^([0-9.]+)(?::([0-9.]+))?$/);
124 $postscale = 1 if not defined $postscale;
125
126 for my $m(@{$options->{maps}})
127 {
128         if($prescale != 1 or $postscale != 1)
129         {
130                 open my $checkfh, "<", "$m.map"
131                         or die "open $m.map: $!";
132                 my $keeplights = 0;
133                 while(<$checkfh>)
134                 {
135                         /^\s*"_keeplights"\s+"1"\s*$/
136                                 or next;
137                         $keeplights = 1;
138                 }
139                 close $checkfh;
140                 die "$m does not define _keeplights to 1"
141                         unless $keeplights;
142         }
143
144         my %shaders = map { m!/([^/.]*)\.shader(?:$)! ? ($1 => 1) : () } glob "../scripts/*.shader";
145         open my $shaderlist, "+<", "$NEXUIZDIR/data/scripts/shaderlist.txt"
146                 or die "open $NEXUIZDIR/data/scripts/shaderlist.txt: $!";
147         my $previous_shaderlist = "";
148         while(<$shaderlist>)
149         {
150                 $previous_shaderlist .= $_;
151                 y/\r\n//d;
152                 delete $shaders{$_};
153         }
154         my $restore_shaderlist = sub
155         {
156                 open $shaderlist, ">", "$NEXUIZDIR/data/scripts/shaderlist.txt";
157                 print $shaderlist $previous_shaderlist;
158                 close $shaderlist;
159         };
160         local $SIG{INT} = sub
161         {
162                 print "SIGINT caught, cleaning up...\n";
163                 $restore_shaderlist->();
164                 exit 0;
165         };
166         eval
167         {
168                 for(keys %shaders)
169                 {
170                         print $shaderlist "$_\n";
171                 }
172                 close $shaderlist;
173
174                 unlink <$m/lm_*>; # delete old external lightmaps
175                 q3map2 '-bsp', '-meta', @{$options->{bsp}},   "$m.map"
176                         or die "-bsp: $?";
177                 if($prescale != 1)
178                 {
179                         q3map2 '-scale', $prescale, "$m.bsp"
180                                 or die "-scale: $?";
181                         rename "${m}_s.bsp", "$m.bsp"
182                                 or die "rename ${m}_s.bsp $m.bsp: $!";
183                 }
184                 if(defined $options->{vis})
185                 {
186                         q3map2 '-vis',          @{$options->{vis}},   "$m.map"
187                                 or die "-vis: $?";
188                 }
189                 if(defined $options->{light})
190                 {
191                         q3map2 '-light',        @{$options->{light}}, "$m.map"
192                                 or die "-light: $?";
193                 }
194                 if($postscale != 1)
195                 {
196                         q3map2 '-scale', $postscale, "$m.bsp"
197                                 or die "-scale: $?";
198                         rename "${m}_s.bsp", "$m.bsp"
199                                 or die "rename ${m}_s.bsp $m.bsp: $!";
200                 }
201
202                 unlink "$m.srf";
203
204                 $restore_shaderlist->();
205                 1;
206         }
207         or do
208         {
209                 $restore_shaderlist->();
210                 die $@;
211         };
212 }