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 my ($filename, $transpose, $walktime, @coords) = @ARGV;
13 my @coords_percussion = ();
15 my $l = \@coords_tuba;
20 $l = \@coords_percussion;
24 push @$l, [split /\s+/, $_];
28 my $opus = MIDI::Opus->new({from_file => $filename});
29 my $ticksperquarter = $opus->ticks();
30 my $tracks = $opus->tracks_r();
31 my @tempi = (); # list of start tick, time per tick pairs (calculated as seconds per quarter / ticks per quarter)
35 for($tracks->[0]->events())
38 if($_->[0] eq 'set_tempo')
40 push @tempi, [$_->[1], $_->[2] * 0.000001 / $ticksperquarter];
47 my $curtempo = [0, 0.5 / $ticksperquarter];
52 # this event is in the past
53 # we add the full time since the last one then
54 $sec += ($_->[0] - $curtempo->[0]) * $curtempo->[1];
58 # if this event is in the future, we break
63 $sec += ($tick - $curtempo->[0]) * $curtempo->[1];
67 # merge all to a single track
68 my @allmidievents = ();
70 for my $track(0..@$tracks-1)
73 for($tracks->[$track]->events())
75 my ($command, $delta, @data) = @$_;
77 push @allmidievents, [$command, $tick, $sequence++, $track, @data];
80 @allmidievents = sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } @allmidievents;
86 my @busybots_percussion = map { undef } @coords_percussion;
87 my @busybots_tuba = map { undef } @coords_tuba;
90 sub busybot_findfree($$$)
92 my ($time, $vchannel, $note) = @_;
93 my $l = ($vchannel < 16) ? \@busybots_tuba : \@busybots_percussion;
98 my $bot = {id => $_ + 1, busy => 0, busytime => 0, channel => $vchannel, curtime => -$walktime, curbuttons => 0, noteoffset => 0};
103 $l->[$_]{channel} == $vchannel
107 $time > $l->[$_]{busytime};
109 die "No free channel found ($notes notes active)\n";
114 my ($vchannel, $note) = @_;
115 my $l = ($vchannel < 16) ? \@busybots_tuba : \@busybots_percussion;
123 $l->[$_]{channel} == $vchannel
125 defined $l->[$_]{note}
127 $l->[$_]{note} == $note;
132 sub busybot_advance($$)
135 my $t0 = $bot->{curtime};
138 #print "sv_cmd bot_cmd $bot->{id} wait @{[$t - $t0]}\n";
139 print "sv_cmd bot_cmd $bot->{id} wait_until $t\n";
141 $bot->{curtime} = $t;
144 sub busybot_setbuttonsandadvance($$$)
146 my ($bot, $t, $b) = @_;
147 my $b0 = $bot->{curbuttons};
148 my $press = $b & ~$b0;
149 my $release = $b0 & ~$b;
150 busybot_advance $bot => $t - 0.1;
151 print "sv_cmd bot_cmd $bot->{id} releasekey attack1\n" if $release & 32;
152 print "sv_cmd bot_cmd $bot->{id} releasekey attack2\n" if $release & 64;
153 busybot_advance $bot => $t - 0.05;
154 print "sv_cmd bot_cmd $bot->{id} releasekey forward\n" if $release & 1;
155 print "sv_cmd bot_cmd $bot->{id} releasekey backward\n" if $release & 2;
156 print "sv_cmd bot_cmd $bot->{id} releasekey left\n" if $release & 4;
157 print "sv_cmd bot_cmd $bot->{id} releasekey right\n" if $release & 8;
158 print "sv_cmd bot_cmd $bot->{id} releasekey crouch\n" if $release & 16;
159 print "sv_cmd bot_cmd $bot->{id} releasekey jump\n" if $release & 128;
160 print "sv_cmd bot_cmd $bot->{id} presskey forward\n" if $press & 1;
161 print "sv_cmd bot_cmd $bot->{id} presskey backward\n" if $press & 2;
162 print "sv_cmd bot_cmd $bot->{id} presskey left\n" if $press & 4;
163 print "sv_cmd bot_cmd $bot->{id} presskey right\n" if $press & 8;
164 print "sv_cmd bot_cmd $bot->{id} presskey crouch\n" if $press & 16;
165 print "sv_cmd bot_cmd $bot->{id} presskey jump\n" if $press & 128;
166 busybot_advance $bot => $t;
167 print "sv_cmd bot_cmd $bot->{id} presskey attack1\n" if $press & 32;
168 print "sv_cmd bot_cmd $bot->{id} presskey attack2\n" if $press & 64;
169 $bot->{curbuttons} = $b;
219 my ($bot, $note) = @_;
220 $note_max = $note if $note_max < $note;
221 $note_min = $note if $note_min > $note;
223 $note -= $bot->{noteoffset};
224 my $s = $notes{$note};
228 sub busybot_playnoteandadvance($$$)
230 my ($bot, $t, $note) = @_;
231 my $s = getnote $bot => $note;
232 return (warn("note $note not found"), 0)
235 $buttons |= 1 if $s =~ /f/;
236 $buttons |= 2 if $s =~ /b/;
237 $buttons |= 4 if $s =~ /l/;
238 $buttons |= 8 if $s =~ /r/;
239 $buttons |= 16 if $s =~ /c/;
240 $buttons |= 32 if $s =~ /1/;
241 $buttons |= 64 if $s =~ /2/;
242 $buttons |= 128 if $s =~ /j/;
243 busybot_setbuttonsandadvance $bot => $t, $buttons;
247 sub busybot_stopnoteandadvance($$$)
249 my ($bot, $t, $note) = @_;
250 my $s = getnote $bot => $note;
253 my $buttons = $bot->{curbuttons};
254 $buttons &= ~(32 | 64);
256 busybot_setbuttonsandadvance $bot => $t, $buttons;
262 my ($t, $channel, $note) = @_;
263 if(busybot_find($channel, $note))
265 note_off($t, $channel, $note); # MIDI allows redoing a note-on for the same note
270 $channel = 16 + $note; # percussion
272 my $bot = busybot_findfree($t, $channel, $note);
275 if(busybot_playnoteandadvance $bot => $t, $note)
278 $bot->{note} = $note;
279 $bot->{busytime} = $t + 0.25;
280 busybot_stopnoteandadvance $bot => $t + 0.15, $note;
285 busybot_advance $bot => $t;
286 print "p $bot->{id} attack1\n";
287 print "r $bot->{id} attack1\n";
289 $bot->{note} = $note;
290 $bot->{busytime} = $t + 1.5;
296 my ($t, $channel, $note) = @_;
300 $channel = 16 + $note; # percussion
302 my $bot = busybot_find($channel, $note)
307 busybot_stopnoteandadvance $bot => $t, $note;
308 $bot->{busytime} = $t + 0.25;
312 print 'alias p "sv_cmd bot_cmd $1 presskey $2"' . "\n";
313 print 'alias r "sv_cmd bot_cmd $1 releasekey $2"' . "\n";
317 my $t = tick2sec $_->[1];
319 if($_->[0] eq 'note_on')
322 note_on($t, $chan, $_->[5]);
324 elsif($_->[0] eq 'note_off')
327 note_off($t, $chan, $_->[5]);
331 print STDERR "Range of notes: $note_min .. $note_max\n";
332 print STDERR "Safe transpose range: @{[$note_max - 19]} .. @{[$note_min + 13]}\n";
333 print STDERR "Unsafe transpose range: @{[$note_max - 24]} .. @{[$note_min + 18]}\n";
334 printf STDERR "%d bots allocated for tuba, %d for percussion\n", int scalar grep { defined $_ } @busybots_tuba, int scalar grep { defined $_ } @busybots_percussion;
337 for(@busybots_percussion, @busybots_tuba)
339 ++$n if $_ && $_->{busy};
343 die "$n channels blocked ($notes MIDI notes)";