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