]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/nexuiz-map-compiler
leileilol's effect updates
[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 -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';
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
117 unlink "$ENV{HOME}/.q3a/data";
118 mkdir "$ENV{HOME}/.q3a";
119 symlink "$mapdir", "$ENV{HOME}/.q3a/data"
120         or die "Setting up directory structure, $mapdir -> $ENV{HOME}/.q3a/data: $!";
121
122 my ($prescale, $postscale) = ($options->{scale} =~ /^([0-9.]+)(?::([0-9.]+))?$/);
123 $postscale = 1 if not defined $postscale;
124
125 for my $m(@{$options->{maps}})
126 {
127         if($prescale != 1 or $postscale != 1)
128         {
129                 open my $checkfh, "<", "$m.map"
130                         or die "open $m.map: $!";
131                 my $keeplights = 0;
132                 while(<$checkfh>)
133                 {
134                         /^\s*"_keeplights"\s+"1"\s*$/
135                                 or next;
136                         $keeplights = 1;
137                 }
138                 close $checkfh;
139                 die "$m does not define _keeplights to 1"
140                         unless $keeplights;
141         }
142
143         my %shaders = map { m!/([^/.]*)\.shader(?:$)! ? ($1 => 1) : () } glob "../scripts/*.shader";
144         open my $shaderlist, "+<", "$NEXUIZDIR/data/scripts/shaderlist.txt"
145                 or die "open $NEXUIZDIR/data/scripts/shaderlist.txt: $!";
146         my $previous_shaderlist = "";
147         while(<$shaderlist>)
148         {
149                 $previous_shaderlist .= $_;
150                 y/\r\n//d;
151                 delete $shaders{$_};
152         }
153         my $restore_shaderlist = sub
154         {
155                 open $shaderlist, ">", "$NEXUIZDIR/data/scripts/shaderlist.txt";
156                 print $shaderlist $previous_shaderlist;
157                 close $shaderlist;
158         };
159         local $SIG{INT} = sub
160         {
161                 print "SIGINT caught, cleaning up...\n";
162                 $restore_shaderlist->();
163                 exit 0;
164         };
165         eval
166         {
167                 for(keys %shaders)
168                 {
169                         print $shaderlist "$_\n";
170                 }
171                 close $shaderlist;
172
173                 q3map2 '-bsp', '-meta', @{$options->{bsp}},   "$m.map"
174                         or die "-bsp: $?";
175                 if($prescale != 1)
176                 {
177                         q3map2 '-scale', $prescale, "$m.bsp"
178                                 or die "-scale: $?";
179                         rename "${m}_s.bsp", "$m.bsp"
180                                 or die "rename ${m}_s.bsp $m.bsp: $!";
181                 }
182                 if(defined $options->{vis})
183                 {
184                         q3map2 '-vis',          @{$options->{vis}},   "$m.map"
185                                 or die "-vis: $?";
186                 }
187                 if(defined $options->{light})
188                 {
189                         q3map2 '-light',        @{$options->{light}}, "$m.map"
190                                 or die "-light: $?";
191                 }
192                 if($postscale != 1)
193                 {
194                         q3map2 '-scale', $postscale, "$m.bsp"
195                                 or die "-scale: $?";
196                         rename "${m}_s.bsp", "$m.bsp"
197                                 or die "rename ${m}_s.bsp $m.bsp: $!";
198                 }
199
200                 unlink "$m.srf";
201
202                 $restore_shaderlist->();
203                 1;
204         }
205         or do
206         {
207                 $restore_shaderlist->();
208                 die $@;
209         };
210 }