#include "stdafx.h"
#include "VreathMagic.h"

#include "jp/ggaf/dxcore/actor/supporter/GgafDxKuroko.h"
#include "jp/ggaf/dxcore/actor/supporter/GgafDxScaler.h"
#include "jp/gecchi/VioletVreath/actor/my/MagicMeter/magic/effect/EffectVreathMagic001.h"
#include "jp/gecchi/VioletVreath/actor/my/MyShip.h"
#include "jp/gecchi/VioletVreath/God.h"
#include "jp/gecchi/VioletVreath/scene/Universe/World/GameScene/MyShipScene.h"

using namespace GgafCore;
using namespace GgafDxCore;
using namespace GgafLib;
using namespace VioletVreath;

VreathMagic::VreathMagic(const char* prm_name, int* prm_pMP)
    : Magic(prm_name, prm_pMP,
            5,                       //max_level
            1000      , 1.0, 0.9,    //婎杮杺朄僐僗僩, 亄侾儗儀儖枅偺僐僗僩憹壛棪  , 旘傃儗儀儖帪偺僐僗僩嶍尭棪
            60        , 1.0, 0.9,    //婎杮塺彞帪娫  , 亄侾儗儀儖枅偺塺彞帪娫憹壛棪, 旘傃儗儀儖帪偺塺彞帪娫嶍尭棪
            60        , 1.0, 0.9,    //婎杮敪摦帪娫  , 亄侾儗儀儖枅偺敪摦帪娫憹壛棪, 旘傃儗儀儖帪偺敪摦帪娫嶍尭棪
            60*60*3   , 0.3,         //婎杮帩懕帪娫  , 亄侾儗儀儖枅偺帩懕帪娫偺忔棪
            0         , 0.0          //婎杮堐帩僐僗僩, 亄侾儗儀儖枅偺堐帩僐僗僩偺忔棪
        ) {

    //嵍楍慖戰壜擻忬懺丄塃楍慖戰晄壜忬懺
    //    | 0          |  1         |  2 Lockon嘮 |  3 Lockon嘮 |  4 Vreath嘮|  5 Vreath嘮|  6 Tractor嘮|  7 Tractor嘮|
    //    | 8          |  9         | 10 Lockon嘫 | 11 Lockon嘫 | 12 Vreath嘫| 13 Vreath嘫| 14 Tractor嘫| 15 Tractor嘫|
    //    | 16         | 17         | 18 Lockon嘪 | 19 Lockon嘪 | 20 Vreath嘪| 21 Vreath嘪| 22 Tractor嘪| 23 Tractor嘪|
    //    | 24         | 25         | 26 Lockon嘩 | 27 Lockon嘩 | 28 Vreath嘩| 29 Vreath嘩| 30 Tractor嘩| 31 Tractor嘩|
    //    | 32         | 33         | 34 Lockon嘨 | 35 Lockon嘨 | 36 Vreath嘨| 37 Vreath嘨| 38 Tractor嘨| 39 Tractor嘨|
    //    | 40 Option嘳| 41 Option嘳| 42 Lockon嘦 | 43 Lockon嘦 | 44 Vreath嘦| 45 Vreath嘦| 46 Tractor嘦| 47 Tractor嘦|
    //    | 48 Option嘰| 49 Option嘰| 50 Lockon嘥 | 51 Lockon嘥 | 52 Vreath嘥| 53 Vreath嘥| 54 Tractor嘥| 55 Tractor嘥|
    //    | 56 Option嘯| 57 Option嘯| 58 Empty    | 59 Empty    | 60 Empty   | 61 Empty   | 62 Empty    | 63 Empty    |
    //    | 64 Option嘮| 65 Option嘮| 66 Torpedo嘮| 67 Torpedo嘮| 68 Laser嘮 | 69 Laser嘮 | 70 Speed嘮  | 71 Speed嘮  |
    //    | 72 Option嘫| 73 Option嘫| 74 Torpedo嘫| 75 Torpedo嘫| 76 Laser嘫 | 77 Laser嘫 | 78 Speed嘫  | 79 Speed嘫  |
    //    | 80 Option嘪| 81 Option嘪| 82 Torpedo嘪| 83 Torpedo嘪| 84 Laser嘪 | 85 Laser嘪 | 86 Speed嘪  | 87 Speed嘪  |
    //    | 88 Option嘩| 89 Option嘩| 90 Torpedo嘩| 91 Torpedo嘩| 92 Laser嘩 | 93 Laser嘩 | 94 Speed嘩  | 95 Speed嘩  |
    //    | 96 Option嘨| 97 Option嘨| 98 Torpedo嘨| 99 Torpedo嘨|100 Laser嘨 |101 Laser嘨 |102 Speed嘨  |103 Speed嘨  |
    //    |104 Option嘦|105 Option嘦|106 Torpedo嘦|107 Torpedo嘦|108 Laser嘦 |109 Laser嘦 |110 Speed嘦  |111 Speed嘦  |
    //    |112 Option嘥|113 Option嘥|114 Torpedo嘥|115 Torpedo嘥|116 Laser嘥 |117 Laser嘥 |118 Speed嘥  |119 Speed嘥  |
    //    |120 Empty   |121 Empty   |122 Empty    |123 Empty    |124 Empty   |125 Empty   |126 Empty    |127 Empty    |



//    | 0              | 1              | 2   Tractor 嘦 | 3   Tractor 嘦 | 4   Tractor 嘥 | 5   Tractor 嘥 | 6   Vreath  嘫 | 7   Vreath  嘫 |
//    | 8              | 9              | 10             | 11             | 12  Empty      | 13  Empty      | 14  Vreath  嘪 | 15  Vreath  嘪 |
//    | 16  Laser   嘩 | 17  Laser   嘩 | 18  Smile   嘩 | 19  Smile   嘩 | 20  Shot    嘩 | 21  Shot    嘩 | 22  Vreath  嘩 | 23  Vreath  嘩 |
//    | 24  Laser   嘨 | 25  Laser   嘨 | 26  Smile   嘨 | 27  Smile   嘨 | 28  Shot    嘨 | 29  Shot    嘨 | 30  Vreath  嘨 | 31  Vreath  嘨 |
//    | 32  Laser   嘦 | 33  Laser   嘦 | 34  Smile   嘦 | 35  Smile   嘦 | 36  Shot    嘦 | 37  Shot    嘦 | 38  Vreath  嘦 | 39  Vreath  嘦 |
//    | 40  Laser   嘥 | 41  Laser   嘥 | 42  Smile   嘥 | 43  Smile   嘥 | 44  Shot    嘥 | 45  Shot    嘥 | 46  Vreath  嘥 | 47  Vreath  嘥 |
//    | 48  Empty      | 49  Empty      | 50  Empty      | 51  Empty      | 52  Empty      | 53  Empty      | 54  Empty      | 55  Empty      |
//    | 56  Option  嘳 | 57  Option  嘳 | 58  Torpedo 嘳 | 59  Torpedo 嘳 | 60  Lockon  嘳 | 61  Lockon  嘳 | 62  Speed   嘳 | 63  Speed   嘳 |
//    | 64  Option  嘰 | 65  Option  嘰 | 66  Torpedo 嘰 | 67  Torpedo 嘰 | 68  Lockon  嘰 | 69  Lockon  嘰 | 70  Speed   嘰 | 71  Speed   嘰 |
//    | 72  Option  嘯 | 73  Option  嘯 | 74  Torpedo 嘯 | 75  Torpedo 嘯 | 76  Lockon  嘯 | 77  Lockon  嘯 | 78  Speed   嘯 | 79  Speed   嘯 |
//    | 80  Option  嘮 | 81  Option  嘮 | 82  Torpedo 嘮 | 83  Torpedo 嘮 | 84  Lockon  嘮 | 85  Lockon  嘮 | 86  Speed   嘮 | 87  Speed   嘮 |
//    | 88  Option  嘫 | 89  Option  嘫 | 90  Torpedo 嘫 | 91  Torpedo 嘫 | 92  Lockon  嘫 | 93  Lockon  嘫 | 94  Speed   嘫 | 95  Speed   嘫 |
//    | 96  Option  嘪 | 97  Option  嘪 | 98  Torpedo 嘪 | 99  Torpedo 嘪 | 100 Lockon  嘪 | 101 Lockon  嘪 | 102 Speed   嘪 | 103 Speed   嘪 |
//    | 104 Option  嘩 | 105 Option  嘩 | 106 Torpedo 嘩 | 107 Torpedo 嘩 | 108 Lockon  嘩 | 109 Lockon  嘩 | 110 Speed   嘩 | 111 Speed   嘩 |
//    | 112 Option  嘨 | 113 Option  嘨 | 114 Torpedo 嘨 | 115 Torpedo 嘨 | 116 Lockon  嘨 | 117 Lockon  嘨 | 118 Speed   嘨 | 119 Speed   嘨 |
//    | 120 Option  嘦 | 121 Option  嘦 | 122 Torpedo 嘦 | 123 Torpedo 嘦 | 124 Lockon  嘦 | 125 Lockon  嘦 | 126 Speed   嘦 | 127 Speed   嘦 |
//    | 128 Option  嘥 | 129 Option  嘥 | 130 Torpedo 嘥 | 131 Torpedo 嘥 | 132 Lockon  嘥 | 133 Lockon  嘥 | 134 Speed   嘥 | 135 Speed   嘥 |
//    | 136 Empty      | 137 Empty      | 138 Empty      | 139 Empty      | 140 Empty      | 141 Empty      | 142 Empty      | 143 Empty      |


//    lvinfo_[7].pno_ =  4;
//    lvinfo_[6].pno_ = 12;
    lvinfo_[5].pno_ = 20;
    lvinfo_[4].pno_ = 28;
    lvinfo_[3].pno_ = 36;
    lvinfo_[2].pno_ = 44;
    lvinfo_[1].pno_ = 52;
    lvinfo_[0].pno_ = 60;

    pEffect_ = NEW EffectVreathMagic001("EffectVreathMagic001");
    pEffect_->inactivate();
    addSubGroup(pEffect_);

    for (int i = 0; i <= MMETER_MAX_LEVEL; i++) {
        beat_time_[i]               = 0;
        apaInt_vreath_per_frame_[i] = 0;
        aInt_total_vreath_[i]       = 0;
        r_add_vreath_[i]            = 0.0;
    }
    //侾夞偺屇媧帪娫僥乕僽儖
    beat_time_[0] = 0.0;
    beat_time_[1] = 180;
    beat_time_[2] = beat_time_[1]/2; //偩傫偩傫
    beat_time_[3] = beat_time_[2]/2; //屇媧偑
    beat_time_[4] = beat_time_[3]/2; //憗偔
    beat_time_[5] = beat_time_[4]/2; //側傞両
//    beat_time_[6] = beat_time_[5]/2;
//    beat_time_[7] = beat_time_[6]/2;

    //枅僼儗乕儉壛嶼偝傟傞Vreath忔棪僥乕僽儖
    for (int lv = 0; lv <= max_level_; lv++) {
        //r_add_vreath_ = - sqrt((max_lv-1)^2 - (lv-1)^2) + max_lv
        //max_level=7 偺応崌
        //lv =7.0 偱 棪 7.0
        //lv =1.0 偱 棪 1.0
        //偲側傞傛偆側1/4墌
        r_add_vreath_[lv] =  -1.0*sqrt((double)(((max_level_-1)*(max_level_-1)) - ((lv-1)*(lv-1)))) + max_level_;
    }

    for (int lv = 0; lv <= max_level_; lv++) {
        if (beat_time_[lv] == 0) {
            apaInt_vreath_per_frame_[lv] = NEW int[1];
            apaInt_vreath_per_frame_[lv][0] = 0;
        } else {
            apaInt_vreath_per_frame_[lv] = NEW int[beat_time_[lv]];
            //慜敿
            for (frame f = 0; f <= beat_time_[lv]/2; f++) {
                int t = f*3600.0/beat_time_[lv];
                apaInt_vreath_per_frame_[lv][f] = MY_SHIP_VREATH_COST*2.0*UTIL::SMOOTH_DV[t];
            }
            //屻敿
            for (frame f = beat_time_[lv]/2 + 1; f < beat_time_[lv]; f++) {
                apaInt_vreath_per_frame_[lv][f] = 0; //媧傢側偄亖vreath偼枅僼儗乕儉MY_SHIP_VREATH_COST嶍傜傟傞
            }
        }
    }
    //奺儗儀儖偺憤屇媧検(aInt_total_vreath_[])傪寁嶼
    for (int lv = 0; lv <= max_level_; lv++) {
        if (beat_time_[lv] == 0) {
            aInt_total_vreath_[lv] = 0;
        } else {
            int sum_v_onebeat = 0; //1屇媧偺懅偺検
            for (frame f = 0; f < beat_time_[lv]; f++) {
                sum_v_onebeat += apaInt_vreath_per_frame_[lv][f] * r_add_vreath_[lv];
            }
            for (frame f = 0; f < beat_time_[lv]; f++) {
                sum_v_onebeat -= MY_SHIP_VREATH_COST;
            }
            //屇媧夞悢傪忔偠偰憤屇媧検傪寁嶼
            aInt_total_vreath_[lv] = sum_v_onebeat * ((1.0 * lvinfo_[lv].time_of_effect_) / beat_time_[lv]);
        }
    }
}

void VreathMagic::onReset() {
    Magic::onReset();
    pEffect_->inactivate();
}

void VreathMagic::processCastBegin(int prm_now_level, int prm_new_level) {
    if (prm_new_level > prm_now_level) {
        pEffect_->positionAs(P_MYSHIP);
        pEffect_->setAlpha(0.9);
        pEffect_->getKuroko()->setFaceAngVelo(AXIS_Z, 100);
        pEffect_->setScale(1000);
        pEffect_->activate();
    }
}

void VreathMagic::processCastingBehavior(int prm_now_level, int prm_new_level) {
    if (prm_new_level > prm_now_level) {
        pEffect_->positionAs(P_MYSHIP);
        pEffect_->addScale(10);
    }
}

void VreathMagic::processCastingCancel(int prm_now_level) {
    pEffect_->inactivate();
}

void VreathMagic::processCastFinish(int prm_now_level, int prm_new_level, int prm_result_invoke) {
}

void VreathMagic::processInvokeBegin(int prm_now_level, int prm_new_level) {
    if (prm_new_level > prm_now_level) {
        pEffect_->setScale(1000);
        pEffect_->getKuroko()->setFaceAngVelo(AXIS_Z, 3000);
    }
}

void VreathMagic::processInvokingBehavior(int prm_now_level, int prm_new_level) {
    if (prm_new_level > prm_now_level) {
        pEffect_->addScale(100);
    }
}

void VreathMagic::processInvokingCancel(int prm_now_level) {
    pEffect_->inactivate();
}

void VreathMagic::processInvokeFinish(int prm_now_level, int prm_new_level, int prm_result_effect) {
    pEffect_->inactivate();
}

void VreathMagic::processEffectBegin(int prm_last_level, int prm_now_level) {
    if ( prm_now_level > 0) {
    } else {
        //儗儀儖0傊儗儀儖僟僂儞帪
        pEffect_->inactivate();
    }
}
void VreathMagic::processEffectingBehavior(int prm_last_level, int prm_now_level) {
    if (beat_time_[prm_now_level] > 0) {
        frame f = getActiveFrame() % beat_time_[prm_now_level];
#ifdef MY_DEBUG
        if (f > beat_time_[prm_now_level]) {
            throwGgafCriticalException("VreathMagic::processEffectingBehavior 斖埻奜偱偡丅 f="<<f<<" beat_time_["<<prm_now_level<<"]");
        }
#endif
        int add_vreath = apaInt_vreath_per_frame_[prm_now_level][f] * r_add_vreath_[prm_now_level];
        P_MYSHIP->getStatus()->plus(STAT_Stamina, add_vreath);
    } else {

    }
}

int VreathMagic::calcTotalVreath(int prm_now_level, int prm_target_up_level) {
    int sum_vreath = 0;
    for (int lv = prm_now_level+1; lv <= prm_target_up_level; lv++) {
        sum_vreath += aInt_total_vreath_[lv];
    }
    return sum_vreath;
}

VreathMagic::~VreathMagic() {
    for (int lv = 0; lv <= max_level_; lv++) {
        GGAF_DELETEARR(apaInt_vreath_per_frame_[lv]);
    }
}
