ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/minesweeper.ext
Revision: 1.11
Committed: Fri Aug 25 17:08:20 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
Changes since 1.10: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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