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, $staccato, @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 #$opus->write_to_file("/tmp/y.mid");
30 my $ticksperquarter = $opus->ticks();
31 my $tracks = $opus->tracks_r();
32 my @tempi = (); # list of start tick, time per tick pairs (calculated as seconds per quarter / ticks per quarter)
36 for($tracks->[0]->events())
39 if($_->[0] eq 'set_tempo')
41 push @tempi, [$_->[1], $_->[2] * 0.000001 / $ticksperquarter];
48 my $curtempo = [0, 0.5 / $ticksperquarter];
53 # this event is in the past
54 # we add the full time since the last one then
55 $sec += ($_->[0] - $curtempo->[0]) * $curtempo->[1];
59 # if this event is in the future, we break
64 $sec += ($tick - $curtempo->[0]) * $curtempo->[1];
68 # merge all to a single track
69 my @allmidievents = ();
71 for my $track(0..@$tracks-1)
74 for($tracks->[$track]->events())
76 my ($command, $delta, @data) = @$_;
78 push @allmidievents, [$command, $tick, $sequence++, $track, @data];
81 @allmidievents = sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } @allmidievents;
87 my @busybots_percussion = map { undef } @coords_percussion;
88 my @busybots_tuba = map { undef } @coords_tuba;
91 sub busybot_findfree($$$)
93 my ($time, $vchannel, $note) = @_;
94 my $l = ($vchannel < 16) ? \@busybots_tuba : \@busybots_percussion;
99 my $bot = {id => $_ + 1, busy => 0, busytime => 0, channel => $vchannel, curtime => -$walktime, curbuttons => 0, noteoffset => 0};
104 $l->[$_]{channel} == $vchannel
108 $time > $l->[$_]{busytime};
110 die "No free channel found ($notes notes active)\n";
115 my ($vchannel, $note) = @_;
116 my $l = ($vchannel < 16) ? \@busybots_tuba : \@busybots_percussion;
124 $l->[$_]{channel} == $vchannel
126 defined $l->[$_]{note}
128 $l->[$_]{note} == $note;
133 sub busybot_advance($$)
136 my $t0 = $bot->{curtime};
139 #print "sv_cmd bot_cmd $bot->{id} wait @{[$t - $t0]}\n";
140 print "w $bot->{id} $t\n";
142 $bot->{curtime} = $t;
145 sub busybot_setbuttonsandadvance($$$)
147 my ($bot, $t, $b) = @_;
148 my $b0 = $bot->{curbuttons};
149 my $press = $b & ~$b0;
150 my $release = $b0 & ~$b;
151 busybot_advance $bot => $t - 0.1
152 if $release & (32 | 64);
153 print "r $bot->{id} attack1\n" if $release & 32;
154 print "r $bot->{id} attack2\n" if $release & 64;
155 busybot_advance $bot => $t - 0.05
156 if ($release | $press) & (1 | 2 | 4 | 8 | 16 | 128);
157 print "r $bot->{id} forward\n" if $release & 1;
158 print "r $bot->{id} backward\n" if $release & 2;
159 print "r $bot->{id} left\n" if $release & 4;
160 print "r $bot->{id} right\n" if $release & 8;
161 print "r $bot->{id} crouch\n" if $release & 16;
162 print "r $bot->{id} jump\n" if $release & 128;
163 print "p $bot->{id} forward\n" if $press & 1;
164 print "p $bot->{id} backward\n" if $press & 2;
165 print "p $bot->{id} left\n" if $press & 4;
166 print "p $bot->{id} right\n" if $press & 8;
167 print "p $bot->{id} crouch\n" if $press & 16;
168 print "p $bot->{id} jump\n" if $press & 128;
169 busybot_advance $bot => $t
170 if $press & (32 | 64);
171 print "p $bot->{id} attack1\n" if $press & 32;
172 print "p $bot->{id} attack2\n" if $press & 64;
173 $bot->{curbuttons} = $b;
223 my ($bot, $note) = @_;
224 $note_max = $note if $note_max < $note;
225 $note_min = $note if $note_min > $note;
227 $note -= $bot->{noteoffset};
228 my $s = $notes{$note};
232 sub busybot_playnoteandadvance($$$)
234 my ($bot, $t, $note) = @_;
235 my $s = getnote $bot => $note;
236 return (warn("note $note not found"), 0)
239 $buttons |= 1 if $s =~ /f/;
240 $buttons |= 2 if $s =~ /b/;
241 $buttons |= 4 if $s =~ /l/;
242 $buttons |= 8 if $s =~ /r/;
243 $buttons |= 16 if $s =~ /c/;
244 $buttons |= 32 if $s =~ /1/;
245 $buttons |= 64 if $s =~ /2/;
246 $buttons |= 128 if $s =~ /j/;
247 busybot_setbuttonsandadvance $bot => $t, $buttons;
251 sub busybot_stopnoteandadvance($$$)
253 my ($bot, $t, $note) = @_;
254 my $s = getnote $bot => $note;
257 my $buttons = $bot->{curbuttons};
258 #$buttons &= ~(32 | 64);
260 busybot_setbuttonsandadvance $bot => $t, $buttons;
266 my ($t, $channel, $note) = @_;
267 if(busybot_find($channel, $note))
269 note_off($t, $channel, $note); # MIDI allows redoing a note-on for the same note
274 $channel = 16 + $note; # percussion
276 my $bot = busybot_findfree($t, $channel, $note);
279 if(busybot_playnoteandadvance $bot => $t, $note)
282 $bot->{note} = $note;
283 $bot->{busytime} = $t + 0.25;
284 busybot_stopnoteandadvance $bot => $t + 0.15, $note
290 busybot_advance $bot => $t;
291 print "p $bot->{id} attack1\n";
292 print "r $bot->{id} attack1\n";
294 $bot->{note} = $note;
295 $bot->{busytime} = $t + 1.5;
301 my ($t, $channel, $note) = @_;
305 $channel = 16 + $note; # percussion
307 my $bot = busybot_find($channel, $note)
312 busybot_stopnoteandadvance $bot => $t, $note;
313 $bot->{busytime} = $t + 0.25;
317 print 'alias p "sv_cmd bot_cmd $1 presskey $2"' . "\n";
318 print 'alias r "sv_cmd bot_cmd $1 releasekey $2"' . "\n";
319 print 'alias w "sv_cmd bot_cmd $1 wait_until $2"' . "\n";
323 my $t = tick2sec $_->[1];
325 if($_->[0] eq 'note_on')
329 note_on($t, $chan, $_->[5]);
331 elsif($_->[0] eq 'note_off')
335 note_off($t, $chan, $_->[5]);
339 print STDERR "Range of notes: $note_min .. $note_max\n";
340 print STDERR "Safe transpose range: @{[$note_max - 19]} .. @{[$note_min + 13]}\n";
341 print STDERR "Unsafe transpose range: @{[$note_max - 24]} .. @{[$note_min + 18]}\n";
342 printf STDERR "%d bots allocated for tuba, %d for percussion\n", int scalar grep { defined $_ } @busybots_tuba, int scalar grep { defined $_ } @busybots_percussion;
345 for(@busybots_percussion, @busybots_tuba)
347 ++$n if $_ && $_->{busy};
351 die "$n channels blocked ($notes MIDI notes)";