-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrecordWarpNpc.pl
More file actions
222 lines (190 loc) · 5.24 KB
/
recordWarpNpc.pl
File metadata and controls
222 lines (190 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
############################
# recordWarpNpc plugin for OpenKore by Damokles
#
# This software is open source, licensed under the GNU General Public
# License, version 2.
#
# To use it just type 'warprec' before you talk to a warp npc. Don't do
# anything between it. It'll record the NPC and the conversation seq and
# destination. They'll be combined in warpportals.txt.
#
# You can use 'warprec save' to trigger the save routine. You may need this
# if you only warp on the same map. Eg. Guild Warp
#
# This plugin should be in a subfolder of plugins like 'plugins/recordWarpNpc'.
#
# Config Options:
#
# recordWarpNpc_integrate (flag) - if is set to 1 it'll integrate the portals in
# portals.txt
# recordWarpNpc_recompile (flag) - integrate has to be enabled. Will reload and
# recompile the portals and portalsLOS.
############################
package recordWarpNpc;
use strict;
use Plugins;
use Globals qw(%config %npcs @npcsID %field $char);
use Settings;
use Log qw(message warning error debug);
use Utils;
Plugins::register('recordWarpNpc', 'Records Warp Npcs', \&onUnload);
my $hooks = Plugins::addHooks(
['packet/map_change', \&onMapChange, undef],
['packet/map_changed', \&onMapChanged, undef],
['packet/map_loaded', \&onMapLoaded, undef]
);
my $cmd = Commands::register(
["warprec", ['Records Warpnpc and conversation seq.',['','toggles record on/off'],['save','triggers save routine']], \&cmdWarprec],
["talk", "extends the Talk command", \&cmdTalk]
);
my $pluginDir = $Plugins::current_plugin_folder;
my %info;
sub onUnload {
Plugins::delHooks($hooks);
Commands::unregister($cmd);
undef %info;
}
sub cmdWarprec {
my ($self,$args) = @_;
my ($arg1) = $args =~ /^(\w+)/;
if ($info{recording}){
if ($arg1 eq 'save') {
saveDest({map=>$field{name},x=>$char->{pos}{x},y=>$char->{pos}{y}});
} else {
message "Warpnpc Recording OFF.\n",'recordWarpNpc';
%info = undef;
}
} else {
$info{recording} = 1;
message "Warpnpc Recording ON.\n",'recordWarpNpc';
}
}
sub onMapChange {
my (undef,$args) = @_;
return 1 unless $info{recording};
my %param = %{$args};
$param{map} =~ s/.gat$//;
saveDest(\%param);
}
sub onMapChanged {
$info{mapChanged} = 1 if $info{recording};
}
sub onMapLoaded {
return 1 unless $info{recording};
saveDest({map=>$field{name},x=>$char->{pos}{x},y=>$char->{pos}{y}});
}
sub cmdTalk {
my ($switch,$args) = @_;
return Commands::cmdTalk($switch,$args) unless $info{recording};
my ($arg1) = $args =~ /^(\w+)/;
my ($arg2) = $args =~ /^\w+ (\d+)/;
if ($arg1 =~ /^\d+$/ && exists $npcsID[$arg1]){
open NPC, ">>$pluginDir/warpnpcs.txt";
print NPC "$field{name} $npcs{$npcsID[$arg1]}{pos}{x} $npcs{$npcsID[$arg1]}{pos}{y}\n";
close NPC;
debug "Warpnpc added: $field{name} $npcs{$npcsID[$arg1]}{pos}{x} $npcs{$npcsID[$arg1]}{pos}{y}\n",'recordWarpNpc',2;
} elsif ($arg1 eq "resp" && $arg2 ne "") {
$info{seq} .= " r$arg2";
debug "Added conversation seq: r$arg2, compl:$info{seq}\n",'recordWarpNpc',2;
}
Commands::cmdTalk($switch,$args); #call real Handler
}
sub saveDest {
my $args = shift;
open DEST, ">>$pluginDir/dest.txt";
print DEST "$args->{map} $args->{x} $args->{y} 0$info{seq}\n";
close DEST;
debug "Warpnpc Dest: $args->{map} $args->{x} $args->{y} 0$info{seq}\n",'recordWarpNpc',2;
message "Warpnpc Recorded.\n",'recordWarpNpc';
%info = undef;
combine();
if ($config{recordWarpNpc_integrate}) {
integrate();
message "Warpnpcs added to portals.txt.\n",'recordWarpNpc';
if ($config{recordWarpNpc_recompile}) {
message "Recompiling Portals.\n",'recordWarpNpc';
Settings::parseReload("portals");
Misc::compilePortals() if Misc::compilePortals_check();
}
}
}
sub combine {
my @dest;
my $source;
my $src_tmp;
my $desc_tmp;
removeDupes("$pluginDir/dest.txt");
removeDupes("$pluginDir/warpnpcs.txt");
open (IN, "<$pluginDir/dest.txt");
@dest = <IN>;
chomp (@dest);
close (IN);
open (IN, "<$pluginDir/warpnpcs.txt");
open (OUT, ">$pluginDir/warpportals.txt");
while (<IN>){
chomp ($_);
$source = $src_tmp = $_;
$src_tmp =~ /(^\w+)\s/;
$src_tmp = $1;
foreach (@dest){
$desc_tmp = $_;
$desc_tmp =~ /(^\w+)\s/;
print OUT "$source $_\n" unless ($1 eq $src_tmp);
}
}
close (IN);
close (OUT);
}
sub removeDupes {
my $file = shift;
my @data;
my @output;
my $temp;
my $dupe;
open IN, "<$file";
@data = <IN>;
close IN;
while ($temp = shift @data){
$dupe = 0;
foreach (@data){
if ($temp eq $_){
$dupe = 1;
last;
}
}
push (@output,$temp) unless $dupe;
}
@output = sort (@output);
open OUT, ">$file";
print OUT @output;
close OUT;
}
sub integrate {
my $start;
my $portal;
my @portals;
my @portals_temp;
open PORTALS, "<$Settings::tables_folder/portals.txt";
@portals = <PORTALS>;
close PORTALS;
while($portal = shift @portals) {
next if $portal eq "\n";
if ($portal eq "#####[WarpNPCs]#####\n"){
$start = 1;
}
if ($portal eq "#####[/WarpNPCs]#####\n"){
push (@portals_temp,@portals);
last;
}
push (@portals_temp,$portal) unless $start;
}
open PORTALS, ">$Settings::tables_folder/portals.txt";
open WARP, "<$pluginDir/warpportals.txt";
print PORTALS @portals_temp;
print PORTALS "\n#####[WarpNPCs]#####\n";
print PORTALS <WARP>;
print PORTALS "#####[/WarpNPCs]#####\n";
close PORTALS;
close WARP;
}
1;