summaryrefslogtreecommitdiffstats
path: root/lib/Games/SMTNocturne/Demons.pm
blob: 6a0627212854905b7354de80ad7a89afe98478df (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package Games::SMTNocturne::Demons;
use strict;
use warnings;

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

sub fuse {
    my ($demon1, $demon2, $options) = @_;
    $options = { %$options };

    $demon1 = Games::SMTNocturne::Demons::Demon->from_name($demon1)
        unless ref($demon1);
    $demon2 = Games::SMTNocturne::Demons::Demon->from_name($demon2)
        unless ref($demon2);
    if ($options->{sacrifice}) {
        $options->{sacrifice} = Games::SMTNocturne::Demons::Demon->from_name(
            $options->{sacrifice}
        ) unless ref($options->{sacrifice});
    }

    if (!$options->{basic}) {
        if (my $demon = _try_special_fusion($demon1, $demon2, $options)) {
            return $demon;
        }
        else {
            $options->{fusion_type} = 'normal';
        }
    }

    if ($demon1->type eq 'Element' && $demon2->type eq 'Element') {
        return _fuse_mitama($demon1, $demon2, $options);
    }
    elsif ($demon1->type eq 'Element' || $demon2->type eq 'Element') {
        return _element_fusion(
            ($demon1->type eq 'Element'
                ? ($demon1, $demon2) : ($demon2, $demon1)),
            $options
        );
    }
    elsif ($demon1->type eq 'Mitama' && $demon2->type eq 'Mitama') {
        # XXX what does this do?
        die "mitama fusion nyi";
    }
    elsif ($demon1->type eq 'Mitama' || $demon2->type eq 'Mitama') {
        return _mitama_fusion(
            ($demon1->type eq 'Mitama'
                ? ($demon1, $demon2) : ($demon2, $demon1)),
            $options
        );
    }
    elsif ($demon1->type eq $demon2->type) {
        return _fuse_element($demon1, $demon2, $options);
    }
    else {
        return _normal_fusion($demon1, $demon2, $options);
    }
}

sub fusions_for {
    my ($demon) = @_;

    $demon = Games::SMTNocturne::Demons::Demon->from_name($demon)
        unless ref($demon);

    my @fusions;
    my %seen;
    for my $types (Games::SMTNocturne::Demons::FusionChart::unfuse($demon->type)) {
        my ($type1, $type2) = @$types;
        for my $demon1 (Games::SMTNocturne::Demons::Demon->from_type($type1)) {
            for my $demon2 (Games::SMTNocturne::Demons::Demon->from_type($type2)) {
                push @fusions, [ $demon1, $demon2 ]
                    if (fuse($demon1, $demon2) || '') eq $demon;
            }
        }
    }

    return @fusions;
}

sub _try_special_fusion {
    my ($demon1, $demon2, $options) = @_;

    my $fused = Games::SMTNocturne::Demons::FusionChart::special_fusion(
        $demon1, $demon2, $options
    );
    return unless $fused;

    my $demon = Games::SMTNocturne::Demons::Demon->from_name($fused);

    my %bosses = map { $_ => 1 } @{ $options->{bosses} || [] };
    return if $demon->boss && !$bosses{$demon->name};

    return $demon;
}

sub _fuse_mitama {
    my ($element1, $element2) = @_;

    my $mitama = Games::SMTNocturne::Demons::FusionChart::fuse_mitama(
        $element1->name, $element2->name
    );
    return unless $mitama;
    return Games::SMTNocturne::Demons::Demon->from_name($mitama);
}

sub _element_fusion {
    my ($element, $demon, $options) = @_;

    my $direction = Games::SMTNocturne::Demons::FusionChart::element_fusion(
        $demon->type, $element->name
    );
    return unless $direction;

    return Games::SMTNocturne::Demons::Demon->from_fusion_stats({
        type   => $demon->type,
        level  => $demon->level,
        offset => $direction,
        %{ $options || {} },
    });
}

sub _mitama_fusion {
    my ($mitama, $demon) = @_;

    return $demon;
}

sub _fuse_element {
    my ($demon1, $demon2) = @_;

    my $element = Games::SMTNocturne::Demons::FusionChart::fuse_element(
        $demon1->type
    );
    return unless $element;
    return Games::SMTNocturne::Demons::Demon->from_name($element);
}

sub _normal_fusion {
    my ($demon1, $demon2, $options) = @_;

    my $new_type = Games::SMTNocturne::Demons::FusionChart::fuse(
        $demon1->type, $demon2->type
    );
    return unless $new_type;

    my $new_level = ($demon1->level + $demon2->level) / 2 + 1;

    return Games::SMTNocturne::Demons::Demon->from_fusion_stats({
        type  => $new_type,
        level => $new_level,
        %{ $options || {} },
    });
}

1;