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