summaryrefslogtreecommitdiffstats
path: root/lib/Games/SMTNocturne/Demons/Fusion.pm
blob: 9ffe9bbf6f04b67683a19ddb5d242a336b677fe5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package Games::SMTNocturne::Demons::Fusion;
use strict;
use warnings;
use overload '""' => 'to_string';

use Games::SMTNocturne::Demons::Demon;

sub new {
    my ($class, $options, $demon1, $demon2, $sacrifice, $kagatsuchi) = @_;

    my $attrs = {};

    $demon1 = Games::SMTNocturne::Demons::Demon->from_name($demon1)
        unless ref($demon1);
    $demon2 = Games::SMTNocturne::Demons::Demon->from_name($demon2)
        unless ref($demon2);
    $attrs->{demons} = [ sort { $a->name cmp $b->name } $demon1, $demon2 ];

    if ($sacrifice) {
        if ($sacrifice eq '<deathstone>') {
            $attrs->{deathstone} = 1;
        }
        else {
            $sacrifice = Games::SMTNocturne::Demons::Demon->from_name($sacrifice)
                unless ref($sacrifice);
            $attrs->{sacrifice} = $sacrifice;
        }
    }

    $attrs->{kagatsuchi} = $kagatsuchi;
    $attrs->{options} = $options || {};

    return bless $attrs, $class;
}

sub options    { $_[0]->{options} }
sub demons     { $_[0]->{demons} }
sub sacrifice  { $_[0]->{sacrifice} }
sub deathstone { $_[0]->{deathstone} }
sub kagatsuchi { $_[0]->{kagatsuchi} }
sub result {
    my $self = shift;
    require Games::SMTNocturne::Demons;
    $self->{result} ||= Games::SMTNocturne::Demons::fuse(
        @{ $self->demons },
        {
            %{ $self->options },
            sacrifice  => $self->sacrifice,
            deathstone => $self->deathstone,
            kagatsuchi => @{ $self->kagatsuchi || [] }[0],
        }
    );
}

sub all_demons {
    my $self = shift;

    return (
        @{ $self->demons },
        ($self->sacrifice ? ($self->sacrifice) : ()),
    );
}

sub raw {
    my $self = shift;
    my $array = [ $self->options, @{ $self->demons } ];
    push @$array, $self->sacrifice
        if $self->sacrifice;
    push @$array, "<deathstone>"
        if $self->deathstone;
    push @$array, $self->kagatsuchi
        if defined $self->kagatsuchi;
    return $array;
}

sub to_string {
    my $self = shift;

    my $str = "Fuse " . $self->demons->[0]
            . " with " . $self->demons->[1];

    $str .= " while sacrificing " . $self->sacrifice
        if $self->sacrifice;
    $str .= " while sacrificing a deathstone"
        if $self->deathstone;
    $str .= " when Kagutsuchi is at phase "
          . join(", ", map { "$_/8" } @{ $self->kagatsuchi })
        if defined $self->kagatsuchi;
    $str .= " resulting in " . $self->result;

    return $str;
}

1;