--- deliantra/maps/perl/minesweeper.ext 2006/03/17 02:40:09 1.3 +++ deliantra/maps/perl/minesweeper.ext 2006/09/08 16:22:14 1.14 @@ -8,9 +8,12 @@ my ($ob, $status) = @_; if (my $teleport = $ob->{options}{"teleport_$status"}) { - my ($x, $y) = split /,/, $teleport; + my ($x, $y, $damned) = split /,/, $teleport; + my $pl = cf::player::find $ob->{player}; - (cf::player::find $ob->{player})->ob->transfer ($x, $y); + $pl->ob->transfer ($x, $y); + $pl->savebed ($pl->ob->map->path, $x, $y) + if $ob->{options}{"set_savebed_$status"}; } } @@ -27,10 +30,11 @@ $who->{visible} = 1; if ($who->{bomb}) { - result $meta, "failure"; + result $meta, "failure" + if $meta->{todo}; } else { $meta->{todo}--; - # if zero, finished + # if <= 0, finished my @neigh; @@ -59,76 +63,71 @@ 1 } -sub on_time { - my ($event) = @_; +cf::register_attachment minesweeper => + on_tick => sub { + my ($self) = @_; - my $who = $event->{who}; + if (my $queue = $self->{queue}) { + my $count = 4; - if (my $queue = $who->{queue}) { - my $count = 4; + while (@$queue) { + my $i = int rand @$queue; + my $ob = splice @$queue, $i, 1, (); - while (@$queue) { - my $i = int rand @$queue; - my $ob = splice @$queue, $i, 1, (); + next if $ob->{visible}; - next if $ob->{visible}; + apply $ob + or next; - apply $ob - or next; + result $self, "success" + unless $self->{todo}; - result $who, "success" - unless $who->{todo}; - - $count-- - or last; - } - } else { - # generate minesweeper field - my %arg = split /(?:\s+|=)/, $event->{options}; - - $who->{options} = \%arg; - $who->{queue} = []; - - my $map = $who->{map} = []; - - for my $x (0 .. $arg{width} - 1) { - for my $y (0 .. $arg{height} - 1) { - my $ob = $map->[$x][$y] = cf::object::new "minesweeper-unknown"; - $ob->set_name ("apply to try your luck or intelligence"); - Scalar::Util::weaken ($ob->{meta} = $who); - - my $ev = cf::object::new "event_apply"; - $ev->set_title ("perl"); - $ev->set_slaying ("minesweeper"); - $ev->insert_in_ob ($ob); - - $ob->insert_ob_in_map_at ($who->map, undef, cf::INS_ABOVE_FLOOR_ONLY, - $who->x + $x, $who->y + $y); + $count-- + or last; + } + } else { + my %arg = %{ $self->{options} = delete $self->{minesweeper} }; + $self->{queue} = []; + + my $map = $self->{map} = []; + + for my $x (0 .. $arg{width} - 1) { + for my $y (0 .. $arg{height} - 1) { + my $ob = $map->[$x][$y] = cf::object::new "minesweeper-unknown"; + $ob->name ("apply to try your luck or intelligence"); + Scalar::Util::weaken ($ob->{meta} = $self); + + $ob->attach ("minesweeper_field"); + $ob->insert_ob_in_map_at ($self->map, undef, cf::INS_ABOVE_FLOOR_ONLY, + $self->x + $x, $self->y + $y); + } } - } - # #tiles that need to be uncovered - $who->{todo} = $arg{width} * $arg{height} - $arg{bombs}; + # #tiles that need to be uncovered + $self->{todo} = $arg{width} * $arg{height} - $arg{bombs}; - for (1 .. $arg{bombs}) { - my $x = int rand $arg{width}; - my $y = int rand $arg{height}; + for (1 .. $arg{bombs}) { + my $x = int rand $arg{width}; + my $y = int rand $arg{height}; - redo if $map->[$x][$y]{bomb}; + redo if $map->[$x][$y]{bomb}; - $map->[$x][$y]{bomb} = 1; + $map->[$x][$y]{bomb} = 1; + } } - } -} - -sub on_apply { - my ($event) = @_; - - my $who = $event->{who}; - my $activator = $event->{activator}; - - $who->{meta}{player} = $activator->name; - push @{$who->{meta}{queue}}, $who; -} + cf::override; + }, +; + +cf::register_attachment minesweeper_field => + on_apply => sub { + my ($ob, $who) = @_; + + $ob->{meta}{player} = $who->name; + push @{$ob->{meta}{queue}}, $ob; + + cf::override 1; + }, +;