ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/minesweeper.ext
Revision: 1.14
Committed: Fri Sep 8 16:22:14 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
Changes since 1.13: +2 -2 lines
Log Message:
new accessor methods

File Contents

# Content
1 #! perl
2
3 use Scalar::Util;
4
5 # minesweeper extension. dumb.
6
7 sub result {
8 my ($ob, $status) = @_;
9
10 if (my $teleport = $ob->{options}{"teleport_$status"}) {
11 my ($x, $y, $damned) = split /,/, $teleport;
12 my $pl = cf::player::find $ob->{player};
13
14 $pl->ob->transfer ($x, $y);
15 $pl->savebed ($pl->ob->map->path, $x, $y)
16 if $ob->{options}{"set_savebed_$status"};
17 }
18 }
19
20 sub apply {
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;
61 }
62
63 1
64 }
65
66 cf::register_attachment minesweeper =>
67 on_tick => sub {
68 my ($self) = @_;
69
70 if (my $queue = $self->{queue}) {
71 my $count = 4;
72
73 while (@$queue) {
74 my $i = int rand @$queue;
75 my $ob = splice @$queue, $i, 1, ();
76
77 next if $ob->{visible};
78
79 apply $ob
80 or next;
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
123 cf::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