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