3 # converter from Type 1 MIDI files to CFG files that control bots with the Tuba and other weapons for percussion (requires g_weaponarena all)
5 # perl midi2cfg.pl filename.mid basenote walktime "x y z" "x y z" "x y z" ... "/" "x y z" "x y z" ... > filename.cfg
12 use constant MIDI_FIRST_NONCHANNEL => 17;
13 use constant MIDI_DRUMS_CHANNEL => 10;
15 my ($filename, $transpose, $walktime, $staccato, @coords) = @ARGV;
16 my @coords_percussion = ();
18 my $l = \@coords_tuba;
23 $l = \@coords_percussion;
27 push @$l, [split /\s+/, $_];
31 my $opus = MIDI::Opus->new({from_file => $filename});
32 #$opus->write_to_file("/tmp/y.mid");
33 my $ticksperquarter = $opus->ticks();
34 my $tracks = $opus->tracks_r();
35 my @tempi = (); # list of start tick, time per tick pairs (calculated as seconds per quarter / ticks per quarter)
39 for($tracks->[0]->events())
42 if($_->[0] eq 'set_tempo')
44 push @tempi, [$tick, $_->[2] * 0.000001 / $ticksperquarter];
51 my $curtempo = [0, 0.5 / $ticksperquarter];
56 # this event is in the past
57 # we add the full time since the last one then
58 $sec += ($_->[0] - $curtempo->[0]) * $curtempo->[1];
62 # if this event is in the future, we break
67 $sec += ($tick - $curtempo->[0]) * $curtempo->[1];
71 # merge all to a single track
72 my @allmidievents = ();
74 for my $track(0..@$tracks-1)
77 for($tracks->[$track]->events())
79 my ($command, $delta, @data) = @$_;
81 push @allmidievents, [$command, $tick, $sequence++, $track, @data];
84 @allmidievents = sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } @allmidievents;
90 my @busybots_percussion = map { undef } @coords_percussion;
91 my @busybots_tuba = map { undef } @coords_tuba;
94 sub busybot_findfree($$$)
96 my ($time, $vchannel, $note) = @_;
97 my $l = ($vchannel < MIDI_FIRST_NONCHANNEL) ? \@busybots_tuba : \@busybots_percussion;
98 my $c = ($vchannel < MIDI_FIRST_NONCHANNEL) ? \@coords_tuba : \@coords_percussion;
103 my $bot = {id => $_ + 1, busy => 0, busytime => 0, channel => $vchannel, curtime => -$walktime, curbuttons => 0, noteoffset => 0};
106 # let the bot walk to his place
107 printf "m $_ $c->[$_]->[0] $c->[$_]->[1] $c->[$_]->[2]\n";
112 (($vchannel < MIDI_FIRST_NONCHANNEL) || ($l->[$_]{channel} == $vchannel))
116 $time > $l->[$_]{busytime};
119 print STDERR Dumper $l;
120 die "No free channel found at time $time ($notes notes active)\n";
125 my ($vchannel, $note) = @_;
126 my $l = ($vchannel < MIDI_FIRST_NONCHANNEL) ? \@busybots_tuba : \@busybots_percussion;
134 $l->[$_]{channel} == $vchannel
136 defined $l->[$_]{note}
138 $l->[$_]{note} == $note;
143 sub busybot_advance($$)
146 my $t0 = $bot->{curtime};
149 #print "sv_cmd bot_cmd $bot->{id} wait @{[$t - $t0]}\n";
150 print "w $bot->{id} $t\n";
152 $bot->{curtime} = $t;
155 sub busybot_setbuttonsandadvance($$$)
157 my ($bot, $t, $b) = @_;
158 my $b0 = $bot->{curbuttons};
159 my $press = $b & ~$b0;
160 my $release = $b0 & ~$b;
161 busybot_advance $bot => $t - 0.10
162 if $release & (32 | 64);
163 print "r $bot->{id} attack1\n" if $release & 32;
164 print "r $bot->{id} attack2\n" if $release & 64;
165 busybot_advance $bot => $t - 0.05
166 if ($release | $press) & (1 | 2 | 4 | 8 | 16 | 128);
167 print "r $bot->{id} forward\n" if $release & 1;
168 print "r $bot->{id} backward\n" if $release & 2;
169 print "r $bot->{id} left\n" if $release & 4;
170 print "r $bot->{id} right\n" if $release & 8;
171 print "r $bot->{id} crouch\n" if $release & 16;
172 print "r $bot->{id} jump\n" if $release & 128;
173 print "p $bot->{id} forward\n" if $press & 1;
174 print "p $bot->{id} backward\n" if $press & 2;
175 print "p $bot->{id} left\n" if $press & 4;
176 print "p $bot->{id} right\n" if $press & 8;
177 print "p $bot->{id} crouch\n" if $press & 16;
178 print "p $bot->{id} jump\n" if $press & 128;
179 busybot_advance $bot => $t
180 if $press & (32 | 64);
181 print "p $bot->{id} attack1\n" if $press & 32;
182 print "p $bot->{id} attack2\n" if $press & 64;
183 $bot->{curbuttons} = $b;
233 my ($bot, $note) = @_;
234 $note_max = $note if $note_max < $note;
235 $note_min = $note if $note_min > $note;
237 $note -= $bot->{noteoffset};
238 my $s = $notes{$note};
242 sub busybot_playnoteandadvance($$$)
244 my ($bot, $t, $note) = @_;
245 my $s = getnote $bot => $note;
246 return (warn("note $note not found"), 0)
249 $buttons |= 1 if $s =~ /f/;
250 $buttons |= 2 if $s =~ /b/;
251 $buttons |= 4 if $s =~ /l/;
252 $buttons |= 8 if $s =~ /r/;
253 $buttons |= 16 if $s =~ /c/;
254 $buttons |= 32 if $s =~ /1/;
255 $buttons |= 64 if $s =~ /2/;
256 $buttons |= 128 if $s =~ /j/;
257 busybot_setbuttonsandadvance $bot => $t, $buttons;
261 sub busybot_stopnoteandadvance($$$)
263 my ($bot, $t, $note) = @_;
264 my $s = getnote $bot => $note;
267 my $buttons = $bot->{curbuttons};
268 #$buttons &= ~(32 | 64);
270 busybot_setbuttonsandadvance $bot => $t, $buttons;
276 my ($t, $channel, $note) = @_;
278 if($channel == MIDI_DRUMS_CHANNEL)
280 $channel = MIDI_FIRST_NONCHANNEL + $note; # percussion
281 return if !@coords_percussion;
283 my $bot = busybot_findfree($t, $channel, $note);
284 if($channel < MIDI_FIRST_NONCHANNEL)
286 if(busybot_playnoteandadvance $bot => $t, $note)
289 $bot->{note} = $note;
290 $bot->{busytime} = $t + 0.25;
293 busybot_stopnoteandadvance $bot => $t + 0.15, $note;
298 if($channel >= MIDI_FIRST_NONCHANNEL)
300 busybot_advance $bot => $t;
301 print "p $bot->{id} attack1\n";
302 print "r $bot->{id} attack1\n";
304 $bot->{note} = $note;
305 $bot->{busytime} = $t + 1.5;
311 my ($t, $channel, $note) = @_;
313 if($channel == MIDI_DRUMS_CHANNEL)
315 $channel = MIDI_FIRST_NONCHANNEL + $note; # percussion
317 my $bot = busybot_find($channel, $note)
320 if($channel < MIDI_FIRST_NONCHANNEL)
322 busybot_stopnoteandadvance $bot => $t, $note;
323 $bot->{busytime} = $t + 0.25;
327 print 'alias p "sv_cmd bot_cmd $1 presskey $2"' . "\n";
328 print 'alias r "sv_cmd bot_cmd $1 releasekey $2"' . "\n";
329 print 'alias w "sv_cmd bot_cmd $1 wait_until $2"' . "\n";
330 print 'alias m "sv_cmd bot_cmd $1 moveto \"$2 $3 $4\""' . "\n";
335 my $t = tick2sec $_->[1];
337 if($_->[0] eq 'note_on')
339 my $chan = $_->[4] + 1;
340 if($midinotes{$chan}{$_->[5]})
342 note_off($t, $chan, $_->[5]);
344 note_on($t, $chan, $_->[5]);
345 $midinotes{$chan}{$_->[5]} = 1;
347 elsif($_->[0] eq 'note_off')
349 my $chan = $_->[4] + 1;
350 if($midinotes{$chan}{$_->[5]})
352 note_off($t, $chan, $_->[5]);
354 $midinotes{$chan}{$_->[5]} = 0;
358 print STDERR "Range of notes: $note_min .. $note_max\n";
359 print STDERR "Safe transpose range: @{[$note_max - 19]} .. @{[$note_min + 13]}\n";
360 print STDERR "Unsafe transpose range: @{[$note_max - 24]} .. @{[$note_min + 18]}\n";
361 printf STDERR "%d bots allocated for tuba, %d for percussion\n", int scalar grep { defined $_ } @busybots_tuba, int scalar grep { defined $_ } @busybots_percussion;
364 for(@busybots_percussion, @busybots_tuba)
366 ++$n if $_ && $_->{busy};
370 die "$n channels blocked ($notes MIDI notes)";