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