ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/minesweeper.ext
(Generate patch)

Comparing deliantra/maps/perl/minesweeper.ext (file contents):
Revision 1.1 by root, Sun Mar 5 00:27:28 2006 UTC vs.
Revision 1.14 by root, Fri Sep 8 16:22:14 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines