]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/bsptool.pl
support "cnt" field in items as probability weight for teamed items; works like for...
[divverent/nexuiz.git] / misc / bsptool.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 my $msg = "";
7
8 my $fn = shift @ARGV;
9 open my $fh, "<", $fn
10         or die "$fn: $!";
11
12 read $fh, my $header, 8;
13
14 die "Invalid BSP format"
15         if $header ne "IBSP\x2e\x00\x00\x00";
16
17 my @lumpname = qw/entities textures planes nodes leafs leaffaces leafbrushes models brushes brushsides vertices triangles effects faces lightmaps lightgrid pvs advertisements/;
18 my %lumpid = map { $lumpname[$_] => $_ } 0..@lumpname-1;
19
20 my @bsp;
21
22 for(0..16)
23 {
24         read $fh, my $lump, 8;
25         my ($offset, $length) = unpack "VV", $lump;
26
27         print "BSP lump $_ ($lumpname[$_]): offset $offset length $length\n";
28         push @bsp, [$offset, $length, undef];
29 }
30
31 for(@bsp)
32 {
33         my ($offset, $length, $data) = @$_;
34         seek $fh, $offset, 0;
35         read $fh, $data, $length;
36         length $data == $length
37                 or die "Incomplete BSP lump at $offset\n";
38         $_->[2] = $data;
39 }
40
41 for(@ARGV)
42 {
43         if(/^-x(.*)$/)
44         {
45                 my $id = $lumpid{$1};
46                 die "invalid lump $1 to remove"
47                         unless defined $id;
48                 $bsp[$id]->[2] = "";
49         }
50         elsif(/^-m(.*)$/)
51         {
52                 $msg = $1;
53         }
54         elsif(/^-e(.*)$/) # extract lump
55         {
56                 my $id = $lumpid{$1};
57                 die "invalid lump $1 to extract"
58                         unless defined $id;
59                 print $bsp[$id]->[2];
60         }
61         elsif(/^-o(.*)$/)
62         {
63                 open my $fh, ">", $1
64                         or die "$1: $!";
65                 print $fh $header;
66                 my $pos = 17 * 8 + tell($fh) + length $msg;
67                 for(@bsp)
68                 {
69                         $_->[0] = $pos;
70                         $_->[1] = length $_->[2];
71                         $pos += $_->[1];
72                         print $fh pack "VV", $_->[0], $_->[1];
73                 }
74                 print $fh $msg;
75                 for(@bsp)
76                 {
77                         print $fh $_->[2];
78                 }
79         }
80 }
81
82 # TODO:
83 #   features like:
84 #     externalize lightmaps
85 #     decimate light grid
86 #     edit lightmaps/grid