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.3 by root, Fri Mar 17 02:40:09 2006 UTC vs.
Revision 1.15 by root, Tue Dec 12 16:59:34 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines