]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/nexuiz-map-compiler
minor optimization, maybe safer for unreasonable values of g_maplist_shuffle - thanks...
[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 = @_;
103         my @args = ($Q3MAP2, '-game', 'quake3', '-fs_basepath', $NEXUIZDIR, '-fs_game', 'data', '-v', @_);
104         print "\$ @args\n";
105         return !system @args;
106 }
107
108 (my $mapdir = getcwd()) =~ s!/[^/]*(?:$)!!;
109
110 unlink "$ENV{HOME}/.q3a/data";
111 mkdir "$ENV{HOME}/.q3a";
112 symlink "$mapdir", "$ENV{HOME}/.q3a/data"
113         or die "Setting up directory structure, $mapdir -> $ENV{HOME}/.q3a/data: $!";
114
115 for my $m(@{$options->{maps}})
116 {
117         if($options->{scale} != 1)
118         {
119                 open my $checkfh, "<", "$m.map"
120                         or die "open $m.map: $!";
121                 my $keeplights = 0;
122                 while(<$checkfh>)
123                 {
124                         /^\s*"_keeplights"\s+"1"\s*$/
125                                 or next;
126                         $keeplights = 1;
127                 }
128                 close $checkfh;
129                 die "$m does not define _keeplights to 1"
130                         unless $keeplights;
131         }
132
133         my %shaders = map { m!/([^/.]*)\.shader(?:$)! ? ($1 => 1) : () } glob "../scripts/*.shader";
134         open my $shaderlist, "+<", "$NEXUIZDIR/data/scripts/shaderlist.txt"
135                 or die "open $NEXUIZDIR/data/scripts/shaderlist.txt: $!";
136         my $previous_shaderlist = "";
137         while(<$shaderlist>)
138         {
139                 $previous_shaderlist .= $_;
140                 y/\r\n//d;
141                 delete $shaders{$_};
142         }
143         my $restore_shaderlist = sub
144         {
145                 open $shaderlist, ">", "$NEXUIZDIR/data/scripts/shaderlist.txt";
146                 print $shaderlist $previous_shaderlist;
147                 close $shaderlist;
148         };
149         local $SIG{INT} = sub
150         {
151                 print "SIGINT caught, cleaning up...\n";
152                 $restore_shaderlist->();
153                 exit 0;
154         };
155         eval
156         {
157                 for(keys %shaders)
158                 {
159                         print $shaderlist "$_\n";
160                 }
161                 close $shaderlist;
162
163                 q3map2 '-bsp', '-meta', @{$options->{bsp}},   "$m.map"
164                         or die "-bsp: $?";
165                 if($options->{scale} != 1)
166                 {
167                         q3map2 '-scale', $options->{scale}, "$m.bsp"
168                                 or die "-scale: $?";
169                         rename "${m}_s.bsp", "$m.bsp"
170                                 or die "rename ${m}_s.bsp $m.bsp: $!";
171                 }
172                 q3map2 '-vis',          @{$options->{vis}},   "$m.map"
173                         or die "-vis: $?";
174                 q3map2 '-light',        @{$options->{light}}, "$m.map"
175                         or die "-light: $?";
176
177                 unlink "$m.srf";
178
179                 $restore_shaderlist->();
180                 1;
181         }
182         or do
183         {
184                 $restore_shaderlist->();
185                 die $@;
186         };
187 }