1 |
#!/opt/bin/perl |
2 |
|
3 |
#TODO: dwall3_3 => dwall_3_3 |
4 |
#TODO: fix dialogue |
5 |
#TODO: update file format version |
6 |
|
7 |
# this script checks, fixes and simplifies @match expressions in a map |
8 |
|
9 |
use common::sense; |
10 |
|
11 |
use Deliantra; |
12 |
use Deliantra::Map; |
13 |
|
14 |
Deliantra::load_archetypes; |
15 |
|
16 |
our $dirty; |
17 |
our $path; |
18 |
|
19 |
sub fix_msg($) { |
20 |
my ($msg) = @_; |
21 |
|
22 |
local $_ = $msg; |
23 |
|
24 |
# fx pretty common error of having "* First line of response" |
25 |
my $response = s/^\* (.*)/*/ ? "\n$1" : ""; |
26 |
|
27 |
warn "$path ($_) unexpected characters in match \n" if !/^[a-zA-Z\[\]\|\*\!\' 0-9\-\?]+$/; |
28 |
|
29 |
s/\[(.)(.)\]/(lc $1) eq (lc $2) ? lc $1 : "[$1$2]"/ge; |
30 |
|
31 |
my %alt; |
32 |
|
33 |
for my $kw (split /\|/) { |
34 |
$kw =~ s/^\s+//; |
35 |
$kw =~ s/\s+$//; |
36 |
|
37 |
$alt{lc $kw} = $kw; |
38 |
} |
39 |
|
40 |
$_ = join "|", sort keys %alt; |
41 |
|
42 |
$_ .= $response; |
43 |
|
44 |
warn "$path <$msg><$_>\n" if $_ ne $msg; |
45 |
warn "$path ($_) unexpected characters in match\n" if /[\[\]]/; |
46 |
|
47 |
$_ |
48 |
} |
49 |
|
50 |
sub patch_arch($) { |
51 |
warn "$path: references unknown archetype $_[0]{_name}\n" |
52 |
unless exists $ARCH{ $_[0]{_name} }; |
53 |
|
54 |
if ($_[0]{face} =~ /\.[1-9]\d\d$/) { |
55 |
$_[0]{face} =~ s/\.([2-9])\1\1$/.x1$1/; # 222, 333, 444 |
56 |
$_[0]{face} =~ s/\.1(\d\d)$/.x$1/; # 1AB xAB |
57 |
$dirty++; |
58 |
} |
59 |
} |
60 |
|
61 |
sub patch_inv($) { |
62 |
my ($arch) = @_; |
63 |
|
64 |
my $inv = $arch->{inventory} || []; |
65 |
|
66 |
for (@$inv) { |
67 |
patch_arch $_; |
68 |
|
69 |
if ($_->{type} == 116 || $_->{_name} =~ /^event_/) { |
70 |
# deliantra to old deliantra+ |
71 |
if ($_->{slaying} eq '/python/IPO/send.py') { |
72 |
$_->{title} = 'perl'; |
73 |
$_->{slaying} = 'ipo'; |
74 |
$dirty++ |
75 |
} elsif ($_->{slaying} eq '/python/IPO/receive.py') { |
76 |
$_->{title} = 'perl'; |
77 |
$_->{slaying} = 'ipo'; |
78 |
$dirty++; |
79 |
} elsif ($_->{slaying} eq '/python/IPO/board.py') { |
80 |
$_ = { _name => 'event_apply', title => 'perl', slaying => 'board' }; |
81 |
$arch->{msg} = '@match *'."\n".'@eval board::command $who, $msg, $npc'."\n"; |
82 |
$dirty++; |
83 |
} elsif ($_->{slaying} eq '/python/IPO/say.py') { |
84 |
$arch->{msg} = '@match *'."\n".'@eval ipo::command $who, $msg, $npc'."\n"; |
85 |
$_ = undef; |
86 |
$dirty++; |
87 |
} elsif ($_->{slaying} eq '/python/IPO/banksay.py') { |
88 |
$arch->{msg} = '@match *'."\n".'@eval bank::command $who, $msg, $npc'."\n"; |
89 |
$_ = undef; |
90 |
$dirty++; |
91 |
} |
92 |
|
93 |
# old deliantra+ to new plug-in system |
94 |
if ($_ && $_->{title} eq "perl") { |
95 |
if ($_->{slaying} eq "board") { |
96 |
push @{$arch->{attach}}, ["board"]; |
97 |
$_ = undef; |
98 |
$dirty++; |
99 |
} elsif ($_->{slaying} eq "ipo") { |
100 |
if ($_->{_name} eq "event_close") { |
101 |
push @{$arch->{attach}}, ["ipo_mailbox"]; |
102 |
} elsif ($_->{_name} eq "event_apply") { |
103 |
# |
104 |
} |
105 |
$_ = undef; |
106 |
$dirty++; |
107 |
} elsif ($_->{slaying} eq "nimbus") { |
108 |
push @{$arch->{attach}}, ["nimbus_exit", { restore => $_->{name} eq "restore"}]; |
109 |
$_ = undef; |
110 |
$dirty++; |
111 |
} elsif ($_->{slaying} eq "minesweeper") { |
112 |
push @{$arch->{attach}}, ["minesweeper", { split /(?:\s+|=)/, $_->{name} }]; |
113 |
$_ = undef; |
114 |
$dirty++; |
115 |
} elsif ($_->{slaying} eq "reseller") { |
116 |
if ($_->{_name} eq "event_drop_on") { |
117 |
push @{$arch->{attach}}, ["reseller_floor"]; |
118 |
} elsif ($_->{_name} eq "event_trigger") { |
119 |
my ($a, $b, $c) = split /,/, $_->{name}; |
120 |
push @{$arch->{attach}}, ["reseller_shopmat", {npc_name => $a, npc_x => $b, npc_y => $c}]; |
121 |
} |
122 |
$_ = undef; |
123 |
$dirty++; |
124 |
} else { |
125 |
warn "WARNING: unsupported perl event<$_->{slaying}>\n";#d# |
126 |
} |
127 |
} |
128 |
} |
129 |
} |
130 |
|
131 |
$arch->{inventory} = [grep $_, @$inv]; |
132 |
} |
133 |
|
134 |
for $path (@ARGV) { |
135 |
eval { |
136 |
open my $fh, "<:raw:perlio:utf8", $path |
137 |
or die "$path: $!\n"; |
138 |
|
139 |
<$fh> =~ /^arch \S+$/ |
140 |
or die "$path: not a deliantra map file\n"; |
141 |
|
142 |
my $map = new_from_file Deliantra::Map $path |
143 |
or die "$path: file load error\n"; |
144 |
|
145 |
$dirty = 0; |
146 |
|
147 |
for my $a (map @$_, grep $_, map @$_, grep $_, @{ $map->{map} }) { |
148 |
patch_arch $a; |
149 |
patch_inv $a if $a->{inventory}; |
150 |
|
151 |
next unless $a->{msg} =~ /^\@match /; |
152 |
|
153 |
my $old = $a->{msg}; |
154 |
|
155 |
$a->{msg} =~ s/^(\@match\s+)(.*)$/$1 . fix_msg $2/gme; |
156 |
|
157 |
$dirty ||= $old ne $a->{msg}; |
158 |
} |
159 |
|
160 |
$map->write_file ($path) |
161 |
if $dirty; |
162 |
|
163 |
1 |
164 |
} or $@ =~ /not a deliantra map/ or warn $@; |
165 |
} |
166 |
|