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.15 by root, Tue Dec 12 16:59:34 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->destroy;
41 60 }
42 warn "$meta $x $y\n";#d# 61
62 1
43} 63}
44 64
45sub on_apply { 65cf::register_attachment minesweeper =>
66 on_tick => sub {
46 my ($event) = @_; 67 my ($self) = @_;
47 68
48 my $who = $event->{who}; 69 if (my $queue = $self->{queue}) {
70 my $count = 4;
49 71
50 apply $who->{meta}, $who->x, $who->y; 72 while (@$queue) {
73 my $i = int rand @$queue;
74 my $ob = splice @$queue, $i, 1, ();
51 75
52 $who->remove; 76 next if $ob->{visible};
53 $who->free;
54}
55 77
78 apply $ob
79 or next;
56 80
81 result $self, "success"
82 unless $self->{todo};
83
84 $count--
85 or last;
86 }
87 } else {
88 my %arg = %{ $self->{options} = delete $self->{minesweeper} };
89 $self->{queue} = [];
90
91 my $map = $self->{map} = [];
92
93 for my $x (0 .. $arg{width} - 1) {
94 for my $y (0 .. $arg{height} - 1) {
95 my $ob = $map->[$x][$y] = cf::object::new "minesweeper-unknown";
96 $ob->name ("apply to try your luck or intelligence");
97 Scalar::Util::weaken ($ob->{meta} = $self);
98
99 $ob->attach ("minesweeper_field");
100 $ob->insert_ob_in_map_at ($self->map, undef, cf::INS_ABOVE_FLOOR_ONLY,
101 $self->x + $x, $self->y + $y);
102 }
103 }
104
105 # #tiles that need to be uncovered
106 $self->{todo} = $arg{width} * $arg{height} - $arg{bombs};
107
108 for (1 .. $arg{bombs}) {
109 my $x = int rand $arg{width};
110 my $y = int rand $arg{height};
111
112 redo if $map->[$x][$y]{bomb};
113
114 $map->[$x][$y]{bomb} = 1;
115 }
116 }
117
118 cf::override;
119 },
120;
121
122cf::register_attachment minesweeper_field =>
123 on_apply => sub {
124 my ($ob, $who) = @_;
125
126 $ob->{meta}{player} = $who->name;
127 push @{$ob->{meta}{queue}}, $ob;
128
129 cf::override 1;
130 },
131;
132

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines