summaryrefslogtreecommitdiffstats
path: root/lib/Games/SMTNocturne/Demons.pm
blob: 4ec1421191ffbe4bed64de388c653e2347778d32 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package Games::SMTNocturne::Demons;

use strict;
use warnings;
# ABSTRACT: look up information about demon fusion in Shin Megami Tensei: Nocturne

use Exporter 5.58 'import';
our @EXPORT_OK = qw(demon demons_of_type all_demons fuse fusions_for);

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

=head1 SYNOPSIS

  use Games::SMTNocturne::Demons qw(fuse fusions_for);

  say fuse('Rangda', 'Barong');
  # <Fury Shiva (95)>

  say for fusions_for('Shiva');
  # Fuse <Mitama Ara Mitama (25)> with <Fury Shiva (95)> resulting in <Fury Shiva (95)>
  # Fuse <Mitama Nigi Mitama (29)> with <Fury Shiva (95)> resulting in <Fury Shiva (95)>
  # Fuse <Mitama Kusi Mitama (32)> with <Fury Shiva (95)> resulting in <Fury Shiva (95)>
  # Fuse <Mitama Saki Mitama (35)> with <Fury Shiva (95)> resulting in <Fury Shiva (95)>
  # Fuse <Avatar Barong (60)> with <Femme Rangda (72)> resulting in <Fury Shiva (95)>

=head1 DESCRIPTION

This module implements various routines for modeling demon fusion in the
PlayStation 2 game Shin Megami Tensei: Nocturne. Note that it also comes with a
command line script called C<smt> which implements some more useful commands on
top of the basic functionality given here; see its documentation for more
information. All of the functions listed below are exported on request.

=cut

=func demon($name)

Returns an instance of L<Games::SMTNocturne::Demons::Demon> for the named
demon. Throws an exception if no such demon exists.

=cut

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

    return Games::SMTNocturne::Demons::Demon->from_name($demon);
}

=func demons_of_type($name)

Returns a list of all demons of a given type. Throws an exception if no such
type exists.

=cut

sub demons_of_type {
    my ($type) = @_;

    return Games::SMTNocturne::Demons::Demon->from_type($type);
}

=func all_demons

Returns a list of all demons in the game.

=cut

sub all_demons {
    return Games::SMTNocturne::Demons::Demon->all_demons;
}

=func fuse($demon1, $demon2, $options)

Returns the demon that will be created when fusing C<$demon1> with C<$demon2>.
Possible options (all optional) are:

=over 4

=item sacrifice

A third demon to be sacrificed (at full Kagutsuchi).

=item max_level

The level of your main character (so fusions that would result in a demon of a
higher level than this will be ignored).

=item bosses

An arrayref of boss demons which have been defeated. Any boss demon not listed
here will be unavailable for fusion.

=item deathstone

Whether or not you own any deathstones.

=item kagutsuchi

The current Kagutsuchi phase.

=back

=cut

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

    $demon1 = demon($demon1) unless ref($demon1);
    $demon2 = demon($demon2) unless ref($demon2);
    if ($options->{sacrifice}) {
        $options->{sacrifice} = demon($options->{sacrifice})
            unless ref($options->{sacrifice});
    }

    if (!$options->{basic}) {
        if (my $demon = _try_special_fusion($demon1, $demon2, $options)) {
            # XXX this is the wrong place for this, but not sure how to do
            # it better
            return if $demon->type eq 'Fiend'
                   && ($demon1->type eq 'Fiend' || $demon2->type eq 'Fiend');
            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') {
        return;
    }
    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);
    }
}

=func fusions_for($demon, $options)

Returns a list of all possible demons fusions which can result in the given
demon. Possible options (all optional) are:

=over 4

=item max_level

The level of your main character (so fusions that would result in a demon of a
higher level than this will be ignored).

=item bosses

An arrayref of boss demons which have been defeated. Any boss demon not listed
here will be unavailable for fusion.

=back

=cut

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

    $demon = demon($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)) {
            next if defined $options->{max_level}
                 && $options->{max_level} < $demon1->level;
            for my $demon2 (Games::SMTNocturne::Demons::Demon->from_type($type2)) {
                next if defined $options->{max_level}
                     && $options->{max_level} < $demon2->level;
                push @fusions, [ $options, $demon1, $demon2 ]
                    if (fuse($demon1, $demon2, $options) || '') eq $demon;
            }
        }
    }

    my $special = Games::SMTNocturne::Demons::FusionChart::special_fusion_for(
        $demon->name
    );
    my @special_fusions;
    if ($special) {
        for my $key (qw(demon1 demon2 demon3 target sacrifice)) {
            next unless $special->{$key};
            if (my $name = $special->{$key}{name}) {
                $special->{$key} = [ demon($name) ];
            }
            elsif (my $type = $special->{$key}{type}) {
                my @types = ref($type) ? (@$type) : ($type);
                $special->{$key} = [
                    map {
                        Games::SMTNocturne::Demons::Demon->from_type($_)
                    } @types
                ];
            }
            $special->{$key} = [
                grep { $_->level <= $options->{max_level} }
                     @{ $special->{$key} }
            ] if $key ne 'target' && defined $options->{max_level};
        }

        if ($special->{demon3}) {
            for my $demon1 (@{ $special->{demon1} }) {
                for my $demon2 (@{ $special->{demon2} }) {
                    for my $demon3 (@{ $special->{demon3} }) {
                        push @special_fusions, [
                            $options, $demon1, $demon2, $demon3
                        ];
                        push @special_fusions, [
                            $options, $demon1, $demon3, $demon2
                        ];
                        push @special_fusions, [
                            $options, $demon2, $demon3, $demon1
                        ];
                    }
                }
            }
        }
        elsif ($special->{demon2}) {
            for my $demon1 (@{ $special->{demon1} }) {
                for my $demon2 (@{ $special->{demon2} }) {
                    push @special_fusions, [ $options, $demon1, $demon2 ];
                }
            }
        }
        elsif ($special->{demon1}) {
            if ($special->{target}) {
                my @target_fusions = map {
                    $_->raw
                } map {
                    fusions_for($_, $options)
                } @{ $special->{target} };
                push @special_fusions, grep {
                    my $fusion = $_;
                    grep { $_ eq $fusion->[0] || $_ eq $fusion->[1] }
                         @{ $special->{demon1} }
                } @target_fusions;
            }
            else {
                die "???";
            }
        }
        else {
            if ($special->{target}) {
                my @new_special = map {
                    $_->raw
                } map {
                    fusions_for($_, $options)
                } @{ $special->{target} };
                if ($demon->type eq 'Fiend') {
                    @new_special = grep {
                        $_->[1]->type ne 'Fiend' && $_->[2]->type ne 'Fiend'
                    } @new_special;
                }
                push @special_fusions, @new_special;
            }
            else {
                die "???";
            }
        }

        if ($special->{sacrifice}) {
            @special_fusions = map {
                my $sac = $_;
                map { [ @$_, $sac ] } @special_fusions
            } @{ $special->{sacrifice} };
        }

        if ($special->{deathstone}) {
            push @$_, '<deathstone>' for @special_fusions;
        }

        if ($special->{kagutsuchi}) {
            push @$_, $special->{kagutsuchi} for @special_fusions;
        }
    }

    return map { Games::SMTNocturne::Demons::Fusion->new(@$_) }
               @fusions, @special_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 = demon($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 demon($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 demon($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 || {} },
    });
}

=head1 BUGS

Probably a lot, since I just wrote this on the fly as I was playing. It was
reasonably accurate enough to get me through the game, but it probably needs a
lot more cleaning around the edges. Failing tests welcome!

One notable omission (that I would be interested in fixing) is that this module
does not handle cursed fusions (mostly since taking advantage of cursed fusions
is so difficult in the game to begin with).

Please report any bugs to GitHub Issues at
L<https://github.com/doy/games-smtnocturne-demons/issues>.

=head1 SEE ALSO

L<http://megamitensei.wikia.com/wiki/Shin_Megami_Tensei_III:_Nocturne>

L<http://www.gamefaqs.com/ps2/582958-shin-megami-tensei-nocturne/faqs/35110>

=head1 SUPPORT

You can find this documentation for this module with the perldoc command.

    perldoc Games::SMTNocturne::Demons

You can also look for information at:

=over 4

=item * MetaCPAN

L<https://metacpan.org/release/Games-SMTNocturne-Demons>

=item * Github

L<https://github.com/doy/games-smtnocturne-demons>

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-SMTNocturne-Demons>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Games-SMTNocturne-Demons>

=back

=cut

1;