// File: /workspace/github_cpp_makefile_star2_O2/Thilotilo[P]DoaERando/main.cpp
// Function: main
int main(void)
{
    ROM rom;
    rom.ReadRom("./SourceRom/Destiny_of_an_Emperor_USA.nes");
    ifstream fin("./config/seed.cfg");
    uint64_t seed;
    fin >> seed;
    ifstream modeFile("./config/newShuffle");
    bool useNewShuffle = modeFile.good();
    Randomizer randomizer(&rom, seed);

    for (uint8_t i=0; i<0xD9; ++i)
    {
        printf("General 0x%0x is ", i);
        randomizer.PrintName(i);
        printf("\n");
    }

    // This is a non-negotiable - it changes no behavior, but enables other features.
    randomizer.ImproveInitialBattlesAndFlags();
    randomizer.FixNMIGlitch();

    // Optional items
    randomizer.ImproveMap();

    // Need to do this before randomizing the starting generals
    randomizer.MakeAllGeneralsRecruitableAndEncounterable();

    // Need to do this before altering location-exit behavior
    randomizer.RemoveFinalIntroScreen();

    // Non-random fix, but needs to come before
    // ReturnToEntryDirectionOnFlagClear
    randomizer.DisableChapter1LockForSiShui();
    // Randomized features
    if (useNewShuffle)
    {
        printf("Using new shuffle algorithm...\n");
        randomizer.RemoveUneededEntranceTriggerCode();
        randomizer.ReconfigureQingZhouCave();
        randomizer.ReconfigureLiuKuiTentGeneral();
        randomizer.NewGeneralAndBattleShuffle();
        randomizer.MakeZhangBaoAndGuanXingAvailable();
        randomizer.MakeZone0GeneralsUnfirable();
        randomizer.UpdateLiuBeiTurnInTrigger();
        randomizer.ReturnToEntryDirectionOnFlagClear();
        randomizer.MakeAllGeneralsBilletable();
        randomizer.MakeWarlordsImmuneToAnSha();
    }
    else
    {
        printf("Using old shuffle algorithm...\n");
        randomizer.RandomizeStartingGenerals();
        randomizer.RandomizeGenerals();
        randomizer.RandomizeBattles();
    }

    randomizer.RandomizeTacticLevels();

    randomizer.ItemShuffle();
    randomizer.CaveShuffle();

    // QoL and other non-random features
    randomizer.IncreaseTacticGains();
    randomizer.MakeIronOreUseful();
    randomizer.MoveGemswordToSimaHuisHut();
    randomizer.MakeIntroLetterUseful();
    randomizer.LetWangGuiTravelToYangZhou();
    randomizer.DoubleXpGain();
    randomizer.DisableNonGeneralBattles();
    randomizer.ResurrectAllAfterDeath();
    randomizer.GivePartyMaxFoodFromNPC();
    randomizer.ModifyEncounterRate();
    randomizer.FixSlot7Glitch();
    randomizer.OnlyLaunchShuLuBuOnce();
    randomizer.RemoveZhugeLiangFetchQuest();
    randomizer.MoveGuiYangBattle();
    randomizer.ProtectChapter1Generals();
    randomizer.UpdateGeneralNames();

    randomizer.RewriteTitleScreen(MAJOR_RELEASE, MINOR_RELEASE, PATCH, seed);
    printf("Saving file...\n");
    rom.WriteRom("./RandoRoms/", MAJOR_RELEASE, MINOR_RELEASE, PATCH, seed);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xeb8, %rsp            # imm = 0xEB8
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
andq $0x0, -0x40(%rbp)
leaq 0x180d8a(%rip), %rsi    # 0x58b551
leaq -0x90(%rbp), %rdi
leaq -0xad0(%rbp), %rdx
callq 0x48b330
leaq -0x50(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x404728
leaq -0x90(%rbp), %rdi
callq 0x488900
leaq 0x180d7e(%rip), %rsi    # 0x58b57b
leaq -0xee0(%rbp), %rdi
pushq $0x8
popq %rdx
callq 0x4553a0
leaq -0xee0(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x45ed70
leaq 0x180d6a(%rip), %rsi    # 0x58b58d
leaq -0xcd8(%rbp), %rdi
pushq $0x8
popq %rdx
callq 0x4553a0
movq -0xcd8(%rbp), %rax
movq -0x18(%rax), %rax
movl -0xcb8(%rbp,%rax), %r13d
movl -0x38(%rbp), %edx
leaq -0xad0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x404e00
leaq 0x180d42(%rip), %rbx    # 0x58b5a1
leaq -0xad0(%rbp), %r14
pushq $0xa
popq %r15
xorl %r12d, %r12d
cmpl $0xd9, %r12d
je 0x40a89c
movq %rbx, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x4e1140
movzbl %r12b, %esi
movq %r14, %rdi
callq 0x404eae
movl %r15d, %edi
callq 0x4e8da0
incl %r12d
jmp 0x40a86d
leaq -0xad0(%rbp), %rdi
callq 0x406570
leaq -0xad0(%rbp), %rdi
callq 0x405e28
leaq -0xad0(%rbp), %rdi
callq 0x406694
leaq -0xad0(%rbp), %rdi
callq 0x406196
leaq -0xad0(%rbp), %rdi
callq 0x4073b4
leaq -0xad0(%rbp), %rdi
callq 0x405fc8
testl %r13d, %r13d
je 0x40a91e
leaq 0x180cd0(%rip), %rdi    # 0x58b5c0
callq 0x4e8680
leaq -0xad0(%rbp), %rdi
callq 0x404f36
leaq -0xad0(%rbp), %rdi
callq 0x4062ea
leaq -0xad0(%rbp), %rdi
callq 0x4064cc
jmp 0x40a9a2
leaq 0x180cba(%rip), %rdi    # 0x58b5df
callq 0x4e8680
leaq -0xad0(%rbp), %rdi
callq 0x406c28
leaq -0xad0(%rbp), %rdi
callq 0x4069e2
leaq -0xad0(%rbp), %rdi
callq 0x406ace
leaq -0xad0(%rbp), %rdi
callq 0x408614
leaq -0xad0(%rbp), %rdi
callq 0x406046
leaq -0xad0(%rbp), %rdi
callq 0x406b76
leaq -0xad0(%rbp), %rdi
callq 0x406ca2
leaq -0xad0(%rbp), %rdi
callq 0x40742a
leaq -0xad0(%rbp), %rdi
callq 0x40761e
leaq -0xad0(%rbp), %rdi
callq 0x40765c
leaq -0xad0(%rbp), %rdi
callq 0x4061e0
leaq -0xad0(%rbp), %rdi
callq 0x40596c
leaq -0xad0(%rbp), %rdi
callq 0x4050ca
leaq -0xad0(%rbp), %rdi
callq 0x4060a4
leaq -0xad0(%rbp), %rdi
callq 0x406ce6
leaq -0xad0(%rbp), %rdi
callq 0x406f40
leaq -0xad0(%rbp), %rdi
callq 0x406fa2
leaq -0xad0(%rbp), %rdi
callq 0x407244
leaq -0xad0(%rbp), %rdi
callq 0x406148
leaq -0xad0(%rbp), %rdi
callq 0x406076
leaq -0xad0(%rbp), %rdi
callq 0x405eb2
leaq -0xad0(%rbp), %rdi
callq 0x405f32
leaq -0xad0(%rbp), %rdi
callq 0x405f7e
leaq -0xad0(%rbp), %rdi
callq 0x405e10
leaq -0xad0(%rbp), %rdi
callq 0x405ef0
leaq -0xad0(%rbp), %rdi
callq 0x406016
leaq -0xad0(%rbp), %rdi
callq 0x4068f0
leaq -0xad0(%rbp), %rdi
callq 0x4075a0
leaq -0xad0(%rbp), %rdi
callq 0x4076c2
movl -0x38(%rbp), %r8d
leaq -0xad0(%rbp), %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x409668
leaq 0x180b5a(%rip), %rdi    # 0x58b5fe
callq 0x4e8680
leaq 0x180b03(%rip), %rsi    # 0x58b5b3
leaq -0x70(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x48b330
movl -0x38(%rbp), %r9d
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
pushq $0x1
popq %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x4047de
leaq -0x70(%rbp), %rdi
callq 0x488900
leaq -0xad0(%rbp), %rdi
callq 0x40ab8e
leaq -0xcd8(%rbp), %rdi
callq 0x455d00
leaq -0xee0(%rbp), %rdi
callq 0x455d00
leaq -0x50(%rbp), %rdi
callq 0x404baa
xorl %eax, %eax
addq $0xeb8, %rsp            # imm = 0xEB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x488900
jmp 0x40ab59
jmp 0x40ab56
movq %rax, %rbx
jmp 0x40ab65
jmp 0x40ab37
movq %rax, %rbx
jmp 0x40ab71
jmp 0x40ab4f
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x488900
jmp 0x40ab7d
movq %rax, %rbx
jmp 0x40ab7d
jmp 0x40ab56
movq %rax, %rbx
leaq -0xad0(%rbp), %rdi
callq 0x40ab8e
leaq -0xcd8(%rbp), %rdi
callq 0x455d00
leaq -0xee0(%rbp), %rdi
callq 0x455d00
leaq -0x50(%rbp), %rdi
callq 0x404baa
movq %rbx, %rdi
callq 0x4ba600