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.12 by root, Sun Aug 27 15:23:30 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->set_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_instantiate => sub {
53 my ($event) = @_; 68 my ($self) = @_;
54 69
55 my $who = $event->{who}; 70 my %arg = %{ $self->{options} = delete $self->{minesweeper} };
71 $self->{queue} = [];
56 72
57 if (my $queue = $who->{queue}) { 73 my $map = $self->{map} = [];
74
75 for my $x (0 .. $arg{width} - 1) {
76 for my $y (0 .. $arg{height} - 1) {
77 my $ob = $map->[$x][$y] = cf::object::new "minesweeper-unknown";
78 $ob->set_name ("apply to try your luck or intelligence");
79 Scalar::Util::weaken ($ob->{meta} = $self);
80
81 $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 }
85 }
86
87 # #tiles that need to be uncovered
88 $self->{todo} = $arg{width} * $arg{height} - $arg{bombs};
89
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
96 $map->[$x][$y]{bomb} = 1;
97 }
98 },
99 on_time => sub {
100 my ($self) = @_;
101
102 my $queue = $self->{queue};
103 my $count = 4;
104
58 while (@$queue) { 105 while (@$queue) {
59 my $i = int rand @$queue; 106 my $i = int rand @$queue;
60 my $ob = splice @$queue, $i, 1, (); 107 my $ob = splice @$queue, $i, 1, ();
61 108
62 next if $ob->{visible}; 109 next if $ob->{visible};
63 110
64 apply $ob 111 apply $ob
65 or next; 112 or next;
66 113
67 warn "todo: $who->{todo}\n";#d# 114 result $self, "success"
68 last; 115 unless $self->{todo};
69 }
70 } else {
71 # generate minesweeper field
72 my %arg = split /(?:\s+|=)/, $event->{options};
73 116
74 $who->{queue} = []; 117 $count--
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 118 or last;
89 $ob->insert_ob_in_map_at ($who->map, undef, cf::INS_ABOVE_FLOOR_ONLY,
90 $who->x + $x, $who->y + $y);
91 }
92 } 119 }
93 120
94 # #tiles that need to be uncovered 121 cf::override;
95 $who->{todo} = $arg{width} * $arg{height} - $arg{bombs}; 122 },
123;
96 124
97 for (1 .. $arg{bombs}) { 125cf::register_attachment minesweeper_field =>
98 my $x = int rand $arg{width}; 126 on_apply => sub {
99 my $y = int rand $arg{height}; 127 my ($ob, $who) = @_;
100 128
101 redo if $map->[$x][$y]{bomb}; 129 $ob->{meta}{player} = $who->name;
130 push @{$ob->{meta}{queue}}, $ob;
102 131
103 warn "bomg at $x $y\n";#d# 132 cf::override 1;
104 $map->[$x][$y]{bomb} = 1;
105 }
106 } 133 },
107} 134;
108 135
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