… | |
… | |
2 | |
2 | |
3 | use Scalar::Util; |
3 | use Scalar::Util; |
4 | |
4 | |
5 | # minesweeper extension. dumb. |
5 | # minesweeper extension. dumb. |
6 | |
6 | |
|
|
7 | sub apply { |
|
|
8 | my ($who) = @_; |
|
|
9 | |
|
|
10 | my $meta = $who->{meta} |
|
|
11 | or return; |
|
|
12 | |
|
|
13 | my $map = $meta->{map}; |
|
|
14 | |
|
|
15 | my ($x, $y) = ($who->x - $meta->x, $who->y - $meta->y); |
|
|
16 | |
|
|
17 | $who->{visible} = 1; |
|
|
18 | |
|
|
19 | if ($who->{bomb}) { |
|
|
20 | warn "poof\n"; |
|
|
21 | } else { |
|
|
22 | $meta->{todo}--; |
|
|
23 | # if zero, finished |
|
|
24 | |
|
|
25 | my @neigh; |
|
|
26 | |
|
|
27 | for my $y ($y - 1 .. $y + 1) { |
|
|
28 | next if $y < 0 || $y > $#{$map->[0]}; |
|
|
29 | for my $x ($x - 1 .. $x + 1) { |
|
|
30 | next if $x < 0 || $x > $#$map; |
|
|
31 | push @neigh, $map->[$x][$y]; |
|
|
32 | } |
|
|
33 | } |
|
|
34 | |
|
|
35 | my $bombs = grep $_->{bomb}, @neigh; |
|
|
36 | |
|
|
37 | my $ob = $map->[$x][$y] = cf::object::new "minesweeper-$bombs"; |
|
|
38 | |
|
|
39 | $ob->insert_ob_in_map_at ($who->map, undef, cf::INS_ABOVE_FLOOR_ONLY, |
|
|
40 | $who->x, $who->y); |
|
|
41 | |
|
|
42 | push @{ $meta->{queue} }, grep !$_->{visible}, @neigh |
|
|
43 | unless $bombs; |
|
|
44 | |
|
|
45 | $who->remove; |
|
|
46 | $who->free; |
|
|
47 | } |
|
|
48 | |
|
|
49 | 1 |
|
|
50 | } |
|
|
51 | |
7 | sub on_time { |
52 | sub on_time { |
8 | my ($event) = @_; |
53 | my ($event) = @_; |
9 | |
54 | |
10 | my $who = $event->{who}; |
55 | my $who = $event->{who}; |
11 | |
56 | |
12 | unless ($who->{map}) { |
57 | if (my $queue = $who->{queue}) { |
|
|
58 | while (@$queue) { |
|
|
59 | my $i = int rand @$queue; |
|
|
60 | my $ob = splice @$queue, $i, 1, (); |
|
|
61 | |
|
|
62 | next if $ob->{visible}; |
|
|
63 | |
|
|
64 | apply $ob |
|
|
65 | or next; |
|
|
66 | |
|
|
67 | warn "todo: $who->{todo}\n";#d# |
|
|
68 | last; |
|
|
69 | } |
|
|
70 | } else { |
|
|
71 | # generate minesweeper field |
13 | my %arg = split /(?:\s+|=)/, $event->{options}; |
72 | my %arg = split /(?:\s+|=)/, $event->{options}; |
14 | |
73 | |
15 | warn join ":", %arg; |
74 | $who->{queue} = []; |
16 | |
75 | |
17 | my $map = $who->{map} = []; |
76 | my $map = $who->{map} = []; |
18 | |
77 | |
19 | for my $x ($who->x .. $who->x + $arg{width} - 1) { |
78 | for my $x (0 .. $arg{width} - 1) { |
20 | for my $y ($who->y .. $who->y + $arg{height} - 1) { |
79 | for my $y (0 .. $arg{height} - 1) { |
21 | my $ob = $map->[$x][$y] = cf::object::new "minesweeper-unknown"; |
80 | my $ob = $map->[$x][$y] = cf::object::new "minesweeper-unknown"; |
22 | $ob->set_name ("apply to try your luck and intelligence"); |
81 | $ob->set_name ("apply to try your luck or intelligence"); |
23 | Scalar::Util::weaken ($ob->{meta} = $who); |
82 | Scalar::Util::weaken ($ob->{meta} = $who); |
24 | |
83 | |
25 | my $ev = cf::object::new "event_apply"; |
84 | my $ev = cf::object::new "event_apply"; |
26 | $ev->set_title ("perl"); |
85 | $ev->set_title ("perl"); |
27 | $ev->set_slaying ("minesweeper"); |
86 | $ev->set_slaying ("minesweeper"); |
28 | $ev->insert_in_ob ($ob); |
87 | $ev->insert_in_ob ($ob); |
29 | |
88 | |
30 | $ob->insert_ob_in_map_at ($who->map, undef, cf::INS_ABOVE_FLOOR_ONLY, $x, $y); |
89 | $ob->insert_ob_in_map_at ($who->map, undef, cf::INS_ABOVE_FLOOR_ONLY, |
|
|
90 | $who->x + $x, $who->y + $y); |
31 | } |
91 | } |
32 | } |
92 | } |
|
|
93 | |
|
|
94 | # #tiles that need to be uncovered |
|
|
95 | $who->{todo} = $arg{width} * $arg{height} - $arg{bombs}; |
|
|
96 | |
|
|
97 | for (1 .. $arg{bombs}) { |
|
|
98 | my $x = int rand $arg{width}; |
|
|
99 | my $y = int rand $arg{height}; |
|
|
100 | |
|
|
101 | redo if $map->[$x][$y]{bomb}; |
|
|
102 | |
|
|
103 | warn "bomg at $x $y\n";#d# |
|
|
104 | $map->[$x][$y]{bomb} = 1; |
|
|
105 | } |
33 | } |
106 | } |
34 | |
|
|
35 | # $who->remove; |
|
|
36 | # $who->free; |
|
|
37 | } |
|
|
38 | |
|
|
39 | sub apply { |
|
|
40 | my ($meta, $x, $y) = @_; |
|
|
41 | |
|
|
42 | warn "$meta $x $y\n";#d# |
|
|
43 | } |
107 | } |
44 | |
108 | |
45 | sub on_apply { |
109 | sub on_apply { |
46 | my ($event) = @_; |
110 | my ($event) = @_; |
47 | |
111 | |
48 | my $who = $event->{who}; |
112 | my $who = $event->{who}; |
49 | |
113 | |
50 | apply $who->{meta}, $who->x, $who->y; |
114 | push @{$who->{meta}{queue}}, $who; |
51 | |
|
|
52 | $who->remove; |
|
|
53 | $who->free; |
|
|
54 | } |
115 | } |
55 | |
116 | |
56 | |
117 | |