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.7 by root, Fri Mar 31 22:47:19 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 =>
63 my ($event, $who) = @_; 66 on_tick => sub {
67 my ($self) = @_;
64 68
65 if (my $queue = $who->{queue}) { 69 if (my $queue = $self->{queue}) {
66 my $count = 4; 70 my $count = 4;
67 71
68 while (@$queue) { 72 while (@$queue) {
69 my $i = int rand @$queue; 73 my $i = int rand @$queue;
70 my $ob = splice @$queue, $i, 1, (); 74 my $ob = splice @$queue, $i, 1, ();
71 75
72 next if $ob->{visible}; 76 next if $ob->{visible};
73 77
74 apply $ob 78 apply $ob
75 or next; 79 or next;
76 80
77 result $who, "success" 81 result $self, "success"
78 unless $who->{todo}; 82 unless $self->{todo};
79 83
80 $count-- 84 $count--
81 or last; 85 or last;
82 } 86 }
83 } else { 87 } else {
84 # generate minesweeper field 88 my %arg = %{ $self->{options} = delete $self->{minesweeper} };
85 my %arg = split /(?:\s+|=)/, $event->options; 89 $self->{queue} = [];
86 90
87 $who->{options} = \%arg; 91 my $map = $self->{map} = [];
88 $who->{queue} = [];
89 92
90 my $map = $who->{map} = []; 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);
91 98
92 for my $x (0 .. $arg{width} - 1) { 99 $ob->attach ("minesweeper_field");
93 for my $y (0 .. $arg{height} - 1) { 100 $ob->insert_ob_in_map_at ($self->map, undef, cf::INS_ABOVE_FLOOR_ONLY,
94 my $ob = $map->[$x][$y] = cf::object::new "minesweeper-unknown"; 101 $self->x + $x, $self->y + $y);
95 $ob->set_name ("apply to try your luck or intelligence"); 102 }
96 Scalar::Util::weaken ($ob->{meta} = $who); 103 }
97 104
98 my $ev = cf::object::new "event_apply"; 105 # #tiles that need to be uncovered
99 $ev->set_title ("perl"); 106 $self->{todo} = $arg{width} * $arg{height} - $arg{bombs};
100 $ev->set_slaying ("minesweeper"); 107
101 $ev->insert_in_ob ($ob); 108 for (1 .. $arg{bombs}) {
102 109 my $x = int rand $arg{width};
103 $ob->insert_ob_in_map_at ($who->map, undef, cf::INS_ABOVE_FLOOR_ONLY, 110 my $y = int rand $arg{height};
104 $who->x + $x, $who->y + $y); 111
112 redo if $map->[$x][$y]{bomb};
113
114 $map->[$x][$y]{bomb} = 1;
105 } 115 }
106 } 116 }
107 117
108 # #tiles that need to be uncovered 118 cf::override;
109 $who->{todo} = $arg{width} * $arg{height} - $arg{bombs}; 119 },
120;
110 121
111 for (1 .. $arg{bombs}) { 122cf::register_attachment minesweeper_field =>
112 my $x = int rand $arg{width}; 123 on_apply => sub {
113 my $y = int rand $arg{height}; 124 my ($ob, $who) = @_;
114 125
115 redo if $map->[$x][$y]{bomb}; 126 $ob->{meta}{player} = $who->name;
127 push @{$ob->{meta}{queue}}, $ob;
116 128
117 $map->[$x][$y]{bomb} = 1; 129 cf::override 1;
118 }
119 } 130 },
120} 131;
121 132
122sub on_apply {
123 my ($event, $ob, $who) = @_;
124
125 $ob->{meta}{player} = $who->name;
126 push @{$ob->{meta}{queue}}, $ob;
127
128 1
129}
130
131

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines