ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/minesweeper.ext
Revision: 1.12
Committed: Sun Aug 27 15:23:30 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
Changes since 1.11: +44 -45 lines
Log Message:
further conversion

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3     use Scalar::Util;
4    
5     # minesweeper extension. dumb.
6    
7 root 1.3 sub result {
8     my ($ob, $status) = @_;
9    
10     if (my $teleport = $ob->{options}{"teleport_$status"}) {
11 root 1.9 my ($x, $y, $damned) = split /,/, $teleport;
12     my $pl = cf::player::find $ob->{player};
13 root 1.3
14 root 1.9 $pl->ob->transfer ($x, $y);
15     $pl->set_savebed ($pl->ob->map->path, $x, $y)
16     if $ob->{options}{"set_savebed_$status"};
17 root 1.3 }
18     }
19    
20 root 1.2 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 root 1.8 result $meta, "failure"
34     if $meta->{todo};
35 root 1.2 } else {
36     $meta->{todo}--;
37 root 1.8 # if <= 0, finished
38 root 1.2
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 root 1.6
63     1
64 root 1.2 }
65    
66 root 1.12 cf::register_attachment minesweeper =>
67     on_instantiate => sub {
68     my ($self) = @_;
69 root 1.1
70 root 1.12 my %arg = %{ $self->{options} = delete $self->{minesweeper} };
71     $self->{queue} = [];
72 root 1.1
73 root 1.12 my $map = $self->{map} = [];
74 root 1.1
75 root 1.2 for my $x (0 .. $arg{width} - 1) {
76     for my $y (0 .. $arg{height} - 1) {
77 root 1.1 my $ob = $map->[$x][$y] = cf::object::new "minesweeper-unknown";
78 root 1.2 $ob->set_name ("apply to try your luck or intelligence");
79 root 1.12 Scalar::Util::weaken ($ob->{meta} = $self);
80 root 1.1
81 root 1.12 $ob->attach ("minesweeper_field");
82     $ob->insert_ob_in_map_at ($self->map, undef, cf::INS_ABOVE_FLOOR_ONLY,
83     $self->x + $x, $self->y + $y);
84 root 1.1 }
85     }
86    
87 root 1.2 # #tiles that need to be uncovered
88 root 1.12 $self->{todo} = $arg{width} * $arg{height} - $arg{bombs};
89 root 1.2
90     for (1 .. $arg{bombs}) {
91     my $x = int rand $arg{width};
92     my $y = int rand $arg{height};
93    
94     redo if $map->[$x][$y]{bomb};
95 root 1.1
96 root 1.2 $map->[$x][$y]{bomb} = 1;
97     }
98 root 1.12 },
99     on_time => sub {
100     my ($self) = @_;
101    
102     my $queue = $self->{queue};
103     my $count = 4;
104    
105     while (@$queue) {
106     my $i = int rand @$queue;
107     my $ob = splice @$queue, $i, 1, ();
108    
109     next if $ob->{visible};
110 root 1.1
111 root 1.12 apply $ob
112     or next;
113 root 1.1
114 root 1.12 result $self, "success"
115     unless $self->{todo};
116 root 1.5
117 root 1.12 $count--
118     or last;
119     }
120 root 1.1
121 root 1.12 cf::override;
122     },
123     ;
124    
125     cf::register_attachment minesweeper_field =>
126     on_apply => sub {
127     my ($ob, $who) = @_;
128    
129     $ob->{meta}{player} = $who->name;
130     push @{$ob->{meta}{queue}}, $ob;
131    
132     cf::override 1;
133     },
134     ;
135 root 1.1