]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/nexuiz-map-compiler
bugfix target_spawn, and change docs to match it
[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 my $game = 'compile.' . int rand 99999999; # mktemp on win32?
109
110 sub q3map2(@)
111 {
112         my @args = ($Q3MAP2, split(/\s+/, $Q3MAP2FLAGS), '-game', 'nexuiz', '-fs_basepath', $NEXUIZDIR, '-fs_game', $game, '-v', @_);
113         print "\$ @args\n";
114         return !system @args;
115 }
116
117 (my $mapdir = getcwd()) =~ s!/[^/]*(?:$)!!;
118 $mapdir = "/" if $mapdir eq "";
119
120 unlink "$NEXUIZDIR/$game";
121 mkdir "$NEXUIZDIR";
122 eval
123 {
124         symlink "$mapdir", "$NEXUIZDIR/$game"
125                 or die "THROW: Setting up directory structure, $mapdir -> $NEXUIZDIR/$game: $!";
126         1;
127 }
128 or do
129 {
130         die $1
131                 if $@ =~ /^THROW: (.*)/s;
132         # if we get here, symlinks do not work, as symlink() itself raised the exception
133         # this should only happen on Win32
134         if($^O ne "MSWin32")
135         {
136                 die "Symlinks not supported ($@) but invalid OS signature, this must be MSWin32, but $^O got reported";
137         }
138
139         # TODO support Windows here somehow
140         # maybe by copying all files and copying back the finished compile? (ugh)
141         die "Windows is not supported yet.";
142 };
143
144 my ($prescale, $postscale) = ($options->{scale} =~ /^([0-9.]+)(?::([0-9.]+))?$/);
145 $postscale = 1 if not defined $postscale;
146
147 for my $m(@{$options->{maps}})
148 {
149         if($prescale != 1 or $postscale != 1)
150         {
151                 open my $checkfh, "<", "$m.map"
152                         or die "open $m.map: $!";
153                 my $keeplights = 0;
154                 while(<$checkfh>)
155                 {
156                         /^\s*"_keeplights"\s+"1"\s*$/
157                                 or next;
158                         $keeplights = 1;
159                 }
160                 close $checkfh;
161                 die "$m does not define _keeplights to 1"
162                         unless $keeplights;
163         }
164
165         my %shaders = map { m!/([^/.]*)\.shader(?:$)! ? ($1 => 1) : () } glob "../scripts/*.shader";
166         open my $shaderlist, "+<", "$NEXUIZDIR/data/scripts/shaderlist.txt"
167                 or die "open $NEXUIZDIR/data/scripts/shaderlist.txt: $!";
168         my $previous_shaderlist = "";
169         while(<$shaderlist>)
170         {
171                 $previous_shaderlist .= $_;
172                 y/\r\n//d;
173                 delete $shaders{$_};
174         }
175         my $restore_shaderlist = sub
176         {
177                 open $shaderlist, ">", "$NEXUIZDIR/data/scripts/shaderlist.txt";
178                 print $shaderlist $previous_shaderlist;
179                 close $shaderlist;
180         };
181         local $SIG{INT} = sub
182         {
183                 print "SIGINT caught, cleaning up...\n";
184                 $restore_shaderlist->();
185                 exit 0;
186         };
187         eval
188         {
189                 for(keys %shaders)
190                 {
191                         print $shaderlist "$_\n";
192                 }
193                 close $shaderlist;
194
195                 unlink <$m/lm_*>; # delete old external lightmaps
196                 q3map2 '-bsp', '-meta', @{$options->{bsp}},   "$m.map"
197                         or die "-bsp: $?";
198                 if($prescale != 1)
199                 {
200                         q3map2 '-scale', $prescale, "$m.bsp"
201                                 or die "-scale: $?";
202                         rename "${m}_s.bsp", "$m.bsp"
203                                 or die "rename ${m}_s.bsp $m.bsp: $!";
204                 }
205                 if(defined $options->{vis})
206                 {
207                         q3map2 '-vis',          @{$options->{vis}},   "$m.map"
208                                 or die "-vis: $?";
209                 }
210                 if(defined $options->{light})
211                 {
212                         q3map2 '-light',        @{$options->{light}}, "$m.map"
213                                 or die "-light: $?";
214                 }
215                 if($postscale != 1)
216                 {
217                         q3map2 '-scale', $postscale, "$m.bsp"
218                                 or die "-scale: $?";
219                         rename "${m}_s.bsp", "$m.bsp"
220                                 or die "rename ${m}_s.bsp $m.bsp: $!";
221                 }
222
223                 unlink "$m.srf";
224
225                 $restore_shaderlist->();
226                 1;
227         }
228         or do
229         {
230                 $restore_shaderlist->();
231                 die $@;
232         };
233 }