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.2 by root, Sun Mar 5 18:48:56 2006 UTC vs.
Revision 1.14 by root, Fri Sep 8 16:22:14 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines