-
Notifications
You must be signed in to change notification settings - Fork 20
/
Battleship.pm
1492 lines (1201 loc) · 47.9 KB
/
Battleship.pm
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# File: Battleship.pm
#
# Purpose: Simplified version of the Battleship board game. In this variant,
# there is one game grid/board and every player's ships share it without
# overlapping. This adds an element of strategy: everybody knows where their
# own ships are located, ergo they know where NOT to aim. This helps to speed
# games up by removing some randomness.
#
# Note: This code was written circa 1993 for a DikuMUD fork. It was originally
# written in C, as I was teaching the language to myself in my early teens. Two
# decades or so later, I transliterated this code from C to Perl for PBot. Much
# of the "ugly" C-style design of this code has been preserved for personal
# historical reasons -- I was inspired by the IOCCC and I attempted to be clever
# with nested conditional operators and other silliness. Please be gentle if you
# read this code. :)
#
# Update: Much of this code has now been refactored to support more than two
# players on a single board. The board grows in size for each additional player,
# to accomodate their ships. Whirlpools have also been added. They are initially
# hidden by the ocean. When shot, they reveal themselves on the map and deflect
# the shot to a random tile. Much of the IOCCC silliness has been removed so that
# I can maintain this code without going insane.
# SPDX-FileCopyrightText: 1993-2023 Pragmatic Software <[email protected]>
# SPDX-License-Identifier: MIT
package PBot::Plugin::Battleship;
use parent 'PBot::Plugin::Base';
use PBot::Imports;
use Time::Duration;
use Time::HiRes qw/time/;
use Data::Dumper;
# some colors for IRC messages
my %color = (
white => "\x0300",
black => "\x0301",
blue => "\x0302",
green => "\x0303",
red => "\x0304",
maroon => "\x0305",
purple => "\x0306",
orange => "\x0307",
yellow => "\x0308",
lightgreen => "\x0309",
teal => "\x0310",
cyan => "\x0311",
lightblue => "\x0312",
magneta => "\x0313",
gray => "\x0314",
lightgray => "\x0315",
bold => "\x02",
italics => "\x1D",
underline => "\x1F",
reverse => "\x16",
reset => "\x0F",
);
sub initialize($self, %conf) {
# register `battleship` bot command
$self->{pbot}->{commands}->add(
name => 'battleship',
help => 'Battleship board game, simplified for IRC',
subref => sub { $self->cmd_battleship(@_) },
);
# set the channel where to send game messages
$self->{channel} = $self->{pbot}->{registry}->get_value('battleship', 'channel') // '##battleship';
# debugging flag
$self->{debug} = $self->{pbot}->{registry}->get_value('battleship', 'debug') // 0;
# player limit per game
$self->{MAX_PLAYERS} = 5;
# max missed moves before player is ejected from game
$self->{MAX_MISSED_MOVES} = 5;
# types of board tiles
$self->{TYPE_OCEAN} = 0;
$self->{TYPE_WHIRLPOOL} = 1;
$self->{TYPE_SHIP} = 2;
# battleship tile symbols
$self->{TILE_HIT} = ['1' .. $self->{MAX_PLAYERS}];
$self->{TILE_OCEAN} = "$color{blue}~";
$self->{TILE_MISS} = "$color{cyan}o";
$self->{TILE_WHIRLPOOL} = "$color{cyan}@";
# personal ship tiles shown on player board
$self->{TILE_SHIP_VERT} = "$color{white}|";
$self->{TILE_SHIP_HORIZ} = "$color{white}—";
# all player ship tiles shown on final/full board
$self->{TILE_SHIP} = ['A' .. chr ord('A') + $self->{MAX_PLAYERS} - 1];
# default board dimensions
$self->{BOARD_X} = 12;
$self->{BOARD_Y} = 8;
# number of ships per player
$self->{SHIP_COUNT} = 6;
# modifiers for show_battlefield()
$self->{BOARD_SPECTATOR} = -1;
$self->{BOARD_FINAL} = -2;
$self->{BOARD_FULL} = -3;
# ship orientation
$self->{ORIENT_VERT} = 0;
$self->{ORIENT_HORIZ} = 1;
# paused state (0 is unpaused)
$self->{PAUSED_BY_PLAYER} = 1;
$self->{PAUSED_FOR_OUTPUT_QUEUE} = 2;
# create game state machine
$self->create_states;
# receive notification when all messages in IRC output queue have been sent
$self->{pbot}->{event_dispatcher}->register_handler(
'pbot.output_queue_empty', sub { $self->on_output_queue_empty(@_) }
);
}
sub unload($self) {
# unregister `battleship` bot command
$self->{pbot}->{commands}->remove('battleship');
# remove battleship loop event from event queue
$self->end_game_loop;
# remove event handler
$self->{pbot}->{event_dispatcher}->remove_handler('pbot.output_queue_empty');
}
# the game is paused at the beginning when sending the player boards to all
# the players and then resumed when the output queue has depleted. this prevents
# game events from queuing up while the board messages are being slowly
# trickled out to the ircd to avoid filling up its message queue (and getting
# disconnected with 'excess flood'). this event handler resumes the game once
# the boards have finished transmitting, unless the game was manually paused
# by a player.
sub on_output_queue_empty($self, $event_type, $event) {
# if we're paused waiting for the output queue, go ahead and unpause
if ($self->{state_data}->{paused} == $self->{PAUSED_FOR_OUTPUT_QUEUE}) {
$self->{state_data}->{paused} = 0;
}
return 0;
}
# `battleship` bot command
sub cmd_battleship($self, $context) {
my $usage = "Usage: battleship challenge|accept|decline|ready|unready|bomb|board|score|players|pause|quit|kick|abort; see also: battleship help <command>";
# strip leading and trailing whitespace
$context->{arguments} =~ s/^\s+|\s+$//g;
my ($command, $arguments) = split / /, $context->{arguments}, 2;
$command //= '';
$command = lc $command;
$arguments //= '';
$arguments = lc $arguments;
# shorter aliases
my ($nick, $user, $host, $hostmask, $channel) = (
$context->{nick},
$context->{user},
$context->{host},
$context->{hostmask},
$self->{channel},
);
given ($command) {
# help doesn't do much yet
when ('help') {
given ($arguments) {
when ('help') {
return "Seriously?";
}
default {
if (length $arguments) {
return "Battleship help is coming soon.";
} else {
return "Usage: battleship help <command>";
}
}
}
}
# issue a challenge to begin a game
when ('challenge') {
if ($self->{current_state} ne 'nogame') {
return "There is already a game of Battleship underway.";
}
# set game to the `challenge` state to begin accepting challenge
$self->set_state('challenge');
# add player 0, the challenger, to the game
my $id = $self->get_player_id($nick, $user, $host);
my $player = $self->new_player($id, $nick);
# clear out player data
$self->{state_data}->{players} = [];
# add player 0
push @{$self->{state_data}->{players}}, $player;
# start the battleship game loop
$self->begin_game_loop;
return "/msg $channel $nick has issued a Battleship challenge! Use `accept` to accept their challenge.";
}
# accept a challenge
when (['accept', 'join']) {
if ($self->{current_state} ne 'challenge') {
return "This is not the time to use `$command`.";
}
if (@{$self->{state_data}->{players}} >= $self->{MAX_PLAYERS}) {
return "/msg $channel $nick: The player limit has been reached. Try again next game.";
}
my $id = $self->get_player_id($nick, $user, $host);
# check that player hasn't already accepted/joined
if (grep { $_->{id} == $id } @{$self->{state_data}->{players}}) {
return "$nick: You have already joined this Battleship game.";
}
# add another player
my $player = $self->new_player($id, $nick);
$player->{index} = @{$self->{state_data}->{players}};
push @{$self->{state_data}->{players}}, $player;
return "/msg $channel $nick has joined the game. Use `ready` to ready-up.";
}
# ready/unready
when (['ready', 'unready']) {
if ($self->{current_state} ne 'challenge') {
return "This is not the time to use `$command`.";
}
my $id = $self->get_player_id($nick, $user, $host);
my ($player) = grep { $_->{id} == $id } @{$self->{state_data}->{players}};
if (not defined $player) {
return "$nick: You have not joined this game of Battleship. Use `accept` to join the game.";
}
if ($command eq 'ready') {
$player->{ready} = 1;
return "/msg $channel $nick is ready!";
} else {
$player->{ready} = 0;
return "/msg $channel $nick is no longer ready.";
}
}
# decline a challenge or forfeit/concede a game
when (['decline', 'quit', 'forfeit', 'concede']) {
my $id = $self->get_player_id($nick, $user, $host);
for (my $i = 0; $i < @{$self->{state_data}->{players}}; $i++) {
if ($self->{state_data}->{players}->[$i]->{id} == $id) {
if ($self->{current_state} eq 'challenge') {
# remove from player list now since this is only the accept
# stage and a game hasn't yet begun
splice @{$self->{state_data}->{players}}, $i, 1;
} else {
# there is an on-going game, just mark them as removed
$self->{state_data}->{players}->[$i]->{removed} = 1;
}
return "/msg $channel $nick has left the game!";
}
}
return "There is nothing to $command.";
}
when ('abort') {
if (not $self->{pbot}->{users}->loggedin_admin($channel, $hostmask)) {
return "$nick: Only admins may abort the game.";
}
if ($self->{current_state} eq 'nogame') {
return "/msg $channel $nick: There is no ongoing game to abort.";
}
# jump directly to the `gameover` state to
# show the final board and reset the game
$self->set_state('gameover');
return "/msg $channel $nick: The game has been aborted.";
}
when (['pause', 'unpause']) {
if ($command eq 'pause') {
$self->{state_data}->{paused} = $self->{PAUSED_BY_PLAYER};
} else {
$self->{state_data}->{paused} = 0;
}
return "/msg $channel $nick has " . ($self->{state_data}->{paused} ? 'paused' : 'unpaused') . " the game!";
}
when ('score') {
if ($self->{current_state} ne 'move' and $self->{current_state} ne 'attack') {
return "There is no Battleship score to show right now.";
}
$self->show_scoreboard;
return '';
}
when ('players') {
if (not @{$self->{state_data}->{players}}) {
return "There are no players playing Battleship right now. Start a game with the `challenge` command!";
}
$self->list_players;
return '';
}
when ('kick') {
if (not $self->{pbot}->{users}->loggedin_admin($channel, $hostmask)) {
return "$nick: Only admins may kick players from the game.";
}
if (not length $arguments) {
return "Usage: battleship kick <nick>";
}
# get the id associated with this nick, in case the current player has changed nick while playing
my ($id) = $self->{pbot}->{messagehistory}->{database}->find_message_account_by_nick($arguments);
if (not defined $id) {
return "I don't know anybody named $arguments.";
}
$id = $self->{pbot}->{messagehistory}->{database}->get_ancestor_id($id);
for (my $i = 0; $i < @{$self->{state_data}->{players}}; $i++) {
if (lc $self->{state_data}->{players}->[$i]->{id} == $id) {
$self->{state_data}->{players}->[$i]->{removed} = 1;
return "/msg $channel $nick: $arguments has been kicked from the game.";
}
}
return "$nick: $arguments isn't even in the game.";
}
when ('bomb') {
if ($self->{current_state} ne 'move' and $self->{current_state} ne 'attack') {
return "$nick: It's not time to do that now.";
}
my $id = $self->get_player_id($nick, $user, $host);
my ($player) = grep { $_->{id} == $id } @{$self->{state_data}->{players}};
if (not defined $player) {
return "You are not playing in this game.";
}
if ($player->{lost}) {
return "You have been knocked out of this game. Try again next game.";
}
if ($player->{removed}) {
return "You have been removed from this game. Try again next game.";
}
# no arguments provided
if (not length $arguments) {
if (delete $player->{location}) {
return "$nick: Attack location cleared.";
} else {
return "$nick: Usage: bomb <location>";
}
}
# validate arguments
$arguments = uc $arguments;
if ($arguments !~ m/^[A-Z][0-9]+$/) {
return "$nick: Usage: bomb <location>; <location> must be in the form of A15, B3, C9, etc.";
}
# ensure arguments are within range of battlefield
my ($x, $y) = $arguments =~ m/^(.)(.*)/;
$x = ord($x) - 65;
if ($x < 0 || $x > $self->{N_Y} - 1 || $y < 1 || $y > $self->{N_X}) {
return "$nick: Target out of range, try again.";
}
my $msg;
if (not exists $player->{location}) {
$msg = "/msg $channel $nick aims somewhere.";
}
elsif (lc $player->{location} eq lc $arguments) {
return '';
}
else {
$msg = "/msg $channel $nick aims somewhere else.";
}
$player->{location} = $arguments;
$player->{time} = time;
return $msg;
}
when (['specboard', 'board']) {
if (grep { $_ eq $self->{current_state} } qw/nogame challenge genboard gameover/) {
return "$nick: There is no board to show right now.";
}
# specifically show spectator board, even if invoked by a player
if ($_ eq 'specboard') {
$self->show_battlefield($self->{BOARD_SPECTATOR});
return '';
}
my $id = $self->get_player_id($nick, $user, $host);
# show player's personal board if playing
for (my $i = 0; $i < @{$self->{state_data}->{players}}; $i++) {
if ($self->{state_data}->{players}->[$i]->{id} == $id) {
if ($self->{state_data}->{players}->[$i]->{removed}) {
return "$nick: You have been removed from this game. Try again next game.";
}
$self->send_message($channel, "$nick surveys the battlefield!");
$self->show_battlefield($i);
return '';
}
}
# otherwise show spectator board
$self->show_battlefield($self->{BOARD_SPECTATOR});
return '';
}
# this command shows the entire battlefield
when ('fullboard') {
if (not $self->{pbot}->{users}->loggedin_admin($channel, $hostmask)) {
return "$nick: Only admins may see the full board.";
}
if (grep { $_ eq $self->{current_state} } qw/nogame challenge genboard gameover/) {
return "$nick: There is no board to show right now.";
}
# show real board if admin is in the game ... no cheating!
my $id = $self->get_player_id($nick, $user, $host);
for (my $i = 0; $i < @{$self->{state_data}->{players}}; $i++) {
if ($self->{state_data}->{players}->[$i]->{id} == $id) {
$self->send_message($channel, "$nick surveys the battlefield!");
$self->show_battlefield($i);
return '';
}
}
# show full board
$self->show_battlefield($self->{BOARD_FULL}, $nick);
}
default { return $usage; }
}
}
# add a message to PBot output queue, optionally with a delay
sub send_message($self, $to, $text, $delay = 0) {
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
my $message = {
nick => $botnick,
user => 'battleship',
host => 'localhost',
hostmask => "$botnick!battleship\@localhost",
command => 'battleship',
checkflood => 1,
message => $text
};
$self->{pbot}->{interpreter}->add_message_to_output_queue($to, $message, $delay);
}
# get unambiguous internal id for player hostmask
sub get_player_id($self, $nick, $user, $host) {
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account($nick, $user, $host);
return $self->{pbot}->{messagehistory}->{database}->get_ancestor_id($id);
}
# create a new player hash
sub new_player($self, $id, $nick) {
return {
id => $id,
name => $nick,
index => 0,
ready => 0,
health => 0,
ships => 0,
shots => 0,
hit => 0,
miss => 0,
sunk => 0,
lost => 0,
missedinputs => 0,
};
}
# get a random number interval [lower, upper)
sub number($self, $lower, $upper) {
return int rand($upper - $lower) + $lower;
}
# battleship stuff
sub begin_game_loop($self) {
# add `battleship loop` event repeating at 1s interval
$self->{pbot}->{event_queue}->enqueue_event(
sub {
$self->run_one_state;
},
1, 'battleship loop', 1
);
}
sub end_game_loop($self) {
# remove `battleship loop` event
# repeating events get added back to event queue if we attempt to
# dequeue_event() from within the event itself. we turn repeating
# off to ensure the event gets removed when it completes.
$self->{pbot}->{event_queue}->update_repeating('battleship loop', 0);
# dequeue event.
$self->{pbot}->{event_queue}->dequeue_event('battleship loop', 0);
}
sub init_game($self, $state) {
# default board dimensions
$self->{N_X} = $self->{BOARD_X};
$self->{N_Y} = $self->{BOARD_Y};
# increase board width by player count
$self->{N_X} += @{$state->{players}} * 2;
# default count of ships per player
$self->{SHIPS} = $self->{SHIP_COUNT};
# initialize ship length fields
for (my $ship = 0; $ship < $self->{SHIPS}; $ship++) {
$self->{ship_length}->[$ship] = 0;
}
# the battleship board/grid. all ships share the same grid to add an
# element of strategy (namely, ships cannot overlap thus you know
# where your enemy ships are NOT located, which narrows the battle
# field and helps speed games up)
$self->{board} = [];
# reset winner flag
$self->{got_winner} = 0;
# place ships and ocean tiles
return $self->generate_battlefield;
}
# ensures a ship can be placed at this location (all desired tiles are ocean)
sub check_ship_placement($self, $x, $y, $o, $l, $player_id) {
my ($xd, $yd, $i);
if ($o == $self->{ORIENT_VERT}) {
if ($y + $l >= $self->{N_Y}) {
return 0;
}
if ($y >= 2) {
my $tile = $self->{board}->[$x][$y - 1];
if ($tile->{type} == $self->{TYPE_SHIP} && $tile->{player_id} == $player_id && $tile->{orientation} == $o) {
return 0;
}
}
if ($y + $l <= $self->{N_Y} - 3) {
my $tile = $self->{board}->[$x][$y + $l + 1];
if ($tile->{type} == $self->{TYPE_SHIP} && $tile->{player_id} == $player_id && $tile->{orientation} == $o) {
return 0;
}
}
$xd = 0;
$yd = 1;
} else {
if ($x + $l >= $self->{N_X}) {
return 0;
}
if ($x >= 2) {
my $tile = $self->{board}->[$x - 1][$y];
if ($tile->{type} == $self->{TYPE_SHIP} && $tile->{player_id} == $player_id && $tile->{orientation} == $o) {
return 0;
}
}
if ($x + $l <= $self->{N_X} - 3) {
my $tile = $self->{board}->[$x + $l + 1][$y];
if ($tile->{type} == $self->{TYPE_SHIP} && $tile->{player_id} == $player_id && $tile->{orientation} == $o) {
return 0;
}
}
$xd = 1;
$yd = 0;
}
for (my $i = 0; $i < $l; $i++) {
if ($self->{board}->[$x += $xd][$y += $yd]->{type} != $self->{TYPE_OCEAN}) {
return 0;
}
}
return 1;
}
# attempt to place a ship on the battlefield
sub place_ship($self, $player_id, $player_index, $ship) {
my ($x, $y, $o, $i, $l);
my ($yd, $xd) = (0, 0);
for (my $attempt = 0; $attempt < 1000; $attempt++) {
$x = $self->number(0, $self->{N_X});
$y = $self->number(0, $self->{N_Y});
$o = $self->number(1, 10) < 6;
if ($self->{ship_length}->[$ship]) {
# reuse saved length so all players have equal sized ships.
# perfectly balanced as all things must be.
$l = $self->{ship_length}->[$ship];
} else {
# generate a random length ship
# TODO: perhaps use a fixed array of guaranteed ship lengths?
# i think random is more exciting because you never know what
# kinds of ships are going to be out there.
$l = $self->number(2, 6);
}
if ($self->{debug}) {
$self->{pbot}->{logger}->log("attempt to place ship for player $player_index: ship $ship x,y: $x,$y o: $o length: $l\n");
}
if ($self->check_ship_placement($x, $y, $o, $l, $player_id)) {
if (!$o) {
$yd = 1;
$xd = 0;
} else {
$xd = 1;
$yd = 0;
}
for (my $i = 0; $i < $l; $i++) {
my $tile_data = {
type => $self->{TYPE_SHIP},
player_id => $player_id,
player_index => $player_index,
orientation => $o,
length => $l,
index => $i,
hit_by => 0,
};
$self->{board}->[$x += $xd][$y += $yd] = $tile_data;
}
$self->{ship_length}->[$ship] = $l;
$self->{state_data}->{players}->[$player_index]->{health} += $l;
$self->{state_data}->{players}->[$player_index]->{ships} += 1;
return 1;
}
}
return 0;
}
sub place_whirlpool($self) {
for (my $attempt = 0; $attempt < 1000; $attempt++) {
my $x = $self->number(0, $self->{N_X});
my $y = $self->number(0, $self->{N_Y});
# skip non-ocean tiles
if ($self->{board}->[$x][$y]->{type} != $self->{TYPE_OCEAN}) {
next;
}
# replace ocean tile with whirlpool
$self->{board}->[$x][$y]->{type} = $self->{TYPE_WHIRLPOOL};
$self->{board}->[$x][$y]->{tile} = $self->{TILE_OCEAN}; # whirlpools hidden initially, until shot
return 1;
}
$self->{pbot}->{logger}->log("Failed to place whirlpool.\n");
return 0;
}
sub generate_battlefield($self) {
# fill board with ocean
for (my $x = 0; $x < $self->{N_X}; $x++) {
for (my $y = 0; $y < $self->{N_Y}; $y++) {
$self->{board}->[$x][$y] = {
type => $self->{TYPE_OCEAN},
tile => $self->{TILE_OCEAN},
};
}
}
# place ships
for (my $player_index = 0; $player_index < @{$self->{state_data}->{players}}; $player_index++) {
for (my $ship = 0; $ship < $self->{SHIPS}; $ship++) {
if (!$self->place_ship($self->{state_data}->{players}->[$player_index]->{id}, $player_index, $ship)) {
return 0;
}
}
}
# place whirlpools (2 whirlpools per player)
for (my $whirlpool = 0; $whirlpool < @{$self->{state_data}->{players}} * 2; $whirlpool++) {
if (!$self->place_whirlpool) {
return 0;
}
}
return 1;
}
# we hit a ship; check if the ship has sunk
sub check_sunk($self, $x, $y) {
# alias to the tile we hit
my $tile = $self->{board}->[$x][$y];
if ($tile->{orientation} == $self->{ORIENT_VERT}) {
my $top = $y - $tile->{index};
my $bottom = $y + ($tile->{length} - ($tile->{index} + 1));
for (my $i = $bottom; $i >= $top; $i--) {
if (not $self->{board}->[$x][$i]->{hit_by}) {
return 0;
}
}
return 1;
} else {
my $left = $x - $tile->{index};
my $right = $x + ($tile->{length} - ($tile->{index} + 1));
for (my $i = $right; $i >= $left; $i--) {
if (not $self->{board}->[$i][$y]->{hit_by}) {
return 0;
}
}
return 1;
}
}
sub get_attack_text($self) {
my @attacks = (
"launches torpedoes at",
"launches nukes at",
"fires cannons at",
"fires torpedoes at",
"fires nukes at",
"launches tomahawk missiles at",
"fires a gatling gun at",
"launches ballistic missiles at",
);
return $attacks[rand @attacks];
}
# checks if we hit whirlpool, ocean, ship, etc
# reveals struck whirlpools
sub check_hit($self, $state, $player, $location_data) {
my ($x, $y, $location) = (
$location_data->{x},
$location_data->{y},
$location_data->{location},
);
# check if we hit a whirlpool. if so, reveal whirlpool on the
# battlefield and deflect the shot
if ($self->{board}->[$x][$y]->{type} == $self->{TYPE_WHIRLPOOL}) {
# reveal this whirlpool
$self->{board}->[$x][$y]->{tile} = $self->{TILE_WHIRLPOOL};
my $attack = $self->get_attack_text;
# keep trying until we don't hit another whirlpool
while (1) {
$self->send_message($self->{channel}, "$player->{name} $attack $location! $color{cyan}--- SPLASH! ---$color{reset}");
$x = $self->number(0, $self->{N_X});
$y = $self->number(0, $self->{N_Y});
$location = ('A' .. 'Z')[$y] . ($x + 1);
$self->send_message($self->{channel}, "$player->{name} hit a whirlpool! It deflects their attack to $location!");
if ($self->{board}->[$x][$y]->{type} == $self->{TYPE_WHIRLPOOL}) {
# hit another whirlpool
next;
}
# update new location for caller
$location_data->{x} = $x;
$location_data->{y} = $y;
$location_data->{location} = $location;
last;
}
}
# hit a ship, damage self or enemy alike
if ($self->{board}->[$x][$y]->{type} == $self->{TYPE_SHIP}) {
my $player_index = $self->{board}->[$x][$y]->{player_index};
if ($state->{players}->[$player_index]->{removed}) {
# removed players no longer exist
return 0;
}
if ($self->{board}->[$x][$y]->{hit_by}) {
# this piece has already been struck
return 0;
} else {
# a hit! a very palpable hit.
return 1;
}
}
# no hit
return 0;
}
sub perform_attack($self, $state, $player) {
$player->{shots}++;
# random attack verb
my $attack = $self->get_attack_text;
# attack location
my $location = delete $player->{location};
# convert attack location to board coordinates
my ($y, $x) = $location =~ /^(.)(.*)/;
$y = ord($y) - 65;
$x--;
# set location data reference so check_hit can update values
my $location_data = {
x => $x,
y => $y,
location => $location,
};
# launch a shot and see if it hit a ship (handles hitting whirlpools, ocean, etc)
my $hit_ship = $self->check_hit($state, $player, $location_data);
# location_data can be updated by whirlpools, etc
$x = $location_data->{x};
$y = $location_data->{y};
$location = $location_data->{location};
if ($hit_ship) {
# player hit a ship!
$self->send_message($self->{channel}, "$player->{name} $attack $location! $color{red}--- HIT! --- $color{reset}");
$player->{hit}++;
# place hit marker
$self->{board}->[$x][$y]->{tile} = $color{red} . $self->{TILE_HIT}->[$player->{index}];
$self->{board}->[$x][$y]->{hit_by} = $player->{id};
my $victim = $self->{state_data}->{players}->[$self->{board}->[$x][$y]->{player_index}];
# deduct hit points from victim
$victim->{health} -= 1;
# check if ship has sunk (reveal what kind and whose ship it is)
if ($self->check_sunk($x, $y)) {
$player->{sunk}++;
$victim->{ships}--;
my $length = $self->{board}->[$x][$y]->{length};
my %ship_names = (
5 => 'battleship',
4 => 'destroyer',
3 => 'submarine',
2 => 'patrol boat',
);
my $ships_left = $victim->{ships};
my $sections_left = $victim->{health};
my $ships = 'ship' . ($ships_left != 1 ? 's' : '');
my $sections = 'section' . ($sections_left != 1 ? 's' : '');
if ($sections_left > 0) {
$self->send_message($self->{channel}, "$color{red}$player->{name} has sunk $victim->{name}'s $ship_names{$length}! $victim->{name} has $ships_left $ships and $sections_left $sections remaining!$color{reset}");
} else {
$self->send_message($self->{channel}, "$color{red}$player->{name} has sunk $victim->{name}'s final $ship_names{$length}! $victim->{name} is out of the game!$color{reset}");
$victim->{lost} = 1;
# check if there is only one player still standing
my $still_alive = 0;
my $winner;
foreach my $p (@{$state->{players}}) {
next if $p->{removed} or $p->{lost};
$still_alive++;
$winner = $p;
}
if ($still_alive == 1) {
$self->send_message($self->{channel}, "$color{yellow}$winner->{name} has won the game of Battleship!$color{reset}");
$self->{got_winner} = 1;
}
}
}
} else {
# player missed
$self->send_message($self->{channel}, "$player->{name} $attack $location! --- miss ---");
$player->{miss}++;
# place miss marker
if ($self->{board}->[$x][$y]->{type} == $self->{TYPE_OCEAN}) {
$self->{board}->[$x][$y]->{tile} = $self->{TILE_MISS};
$self->{board}->[$x][$y]->{missed_by} = $player->{id};
}
}
}
sub list_players($self) {
my @players;
foreach my $player (@{$self->{state_data}->{players}}) {
push @players, $player->{name} . ($player->{ready} ? '' : " $color{red}(not ready)$color{reset}");
}
if (@players) {
$self->send_message($self->{channel}, "Current players: " . (join ', ', @players) . ". Use `ready` when you are.");
}
}
sub show_scoreboard($self) {
foreach my $player (sort { $b->{health} <=> $a->{health} } @{$self->{state_data}->{players}}) {
next if $player->{removed};
my $buf = sprintf("%-10s shots: %2d, hit: %2d, miss: %2d, acc: %3d%%, sunk: %2d, ships left: %d, sections left: %2d",
"$player->{name}:",
$player->{shots},
$player->{hit},
$player->{miss},
int (($player->{hit} / ($player->{shots} ? $player->{shots} : 1)) * 100),
$player->{sunk},
$player->{ships},
$player->{health},
);
$self->send_message($self->{channel}, $buf);
}
}
sub show_battlefield($self, $player_index, $nick = undef) {
$self->{pbot}->{logger}->log("Showing battlefield for player $player_index\n");
my $player;
if ($player_index >= 0) {
$player = $self->{state_data}->{players}->[$player_index];