ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/minesweeper.ext
Revision: 1.2
Committed: Sun Mar 5 18:48:56 2006 UTC (18 years, 2 months ago) by root
Branch: MAIN
Changes since 1.1: +79 -18 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #! perl
2
3 use Scalar::Util;
4
5 # minesweeper extension. dumb.
6
7 sub apply {
8 my ($who) = @_;
9
10 my $meta = $who->{meta}
11 or return;
12
13 my $map = $meta->{map};
14
15 my ($x, $y) = ($who->x - $meta->x, $who->y - $meta->y);
16
17 $who->{visible} = 1;
18
19 if ($who->{bomb}) {
20 warn "poof\n";
21 } else {
22 $meta->{todo}--;
23 # if zero, finished
24
25 my @neigh;
26
27 for my $y ($y - 1 .. $y + 1) {
28 next if $y < 0 || $y > $#{$map->[0]};
29 for my $x ($x - 1 .. $x + 1) {
30 next if $x < 0 || $x > $#$map;
31 push @neigh, $map->[$x][$y];
32 }
33 }
34
35 my $bombs = grep $_->{bomb}, @neigh;
36
37 my $ob = $map->[$x][$y] = cf::object::new "minesweeper-$bombs";
38
39 $ob->insert_ob_in_map_at ($who->map, undef, cf::INS_ABOVE_FLOOR_ONLY,
40 $who->x, $who->y);
41
42 push @{ $meta->{queue} }, grep !$_->{visible}, @neigh
43 unless $bombs;
44
45 $who->remove;
46 $who->free;
47 }
48
49 1
50 }
51
52 sub on_time {
53 my ($event) = @_;
54
55 my $who = $event->{who};
56
57 if (my $queue = $who->{queue}) {
58 while (@$queue) {
59 my $i = int rand @$queue;
60 my $ob = splice @$queue, $i, 1, ();
61
62 next if $ob->{visible};
63
64 apply $ob
65 or next;
66
67 warn "todo: $who->{todo}\n";#d#
68 last;
69 }
70 } else {
71 # generate minesweeper field
72 my %arg = split /(?:\s+|=)/, $event->{options};
73
74 $who->{queue} = [];
75
76 my $map = $who->{map} = [];
77
78 for my $x (0 .. $arg{width} - 1) {
79 for my $y (0 .. $arg{height} - 1) {
80 my $ob = $map->[$x][$y] = cf::object::new "minesweeper-unknown";
81 $ob->set_name ("apply to try your luck or intelligence");
82 Scalar::Util::weaken ($ob->{meta} = $who);
83
84 my $ev = cf::object::new "event_apply";
85 $ev->set_title ("perl");
86 $ev->set_slaying ("minesweeper");
87 $ev->insert_in_ob ($ob);
88
89 $ob->insert_ob_in_map_at ($who->map, undef, cf::INS_ABOVE_FLOOR_ONLY,
90 $who->x + $x, $who->y + $y);
91 }
92 }
93
94 # #tiles that need to be uncovered
95 $who->{todo} = $arg{width} * $arg{height} - $arg{bombs};
96
97 for (1 .. $arg{bombs}) {
98 my $x = int rand $arg{width};
99 my $y = int rand $arg{height};
100
101 redo if $map->[$x][$y]{bomb};
102
103 warn "bomg at $x $y\n";#d#
104 $map->[$x][$y]{bomb} = 1;
105 }
106 }
107 }
108
109 sub on_apply {
110 my ($event) = @_;
111
112 my $who = $event->{who};
113
114 push @{$who->{meta}{queue}}, $who;
115 }
116
117