vendredi 23 novembre 2018

Switch statement for multiple "If options"

I am experimenting with the source code of a game and I want to add something. I have the following piece of code.

        switch (flag)
        {
        case HITTYPE_WEAK:
            ret = (int)(usingstat *  1000 / statFactor + (awdamage * 7 / 10) - (ddefense + ddefense2) / damFactor);
            break;
        case HITTYPE_NORMAL:
            ret = (int)(usingstat *  1000 / statFactor + awdamage - (ddefense + ddefense2) / damFactor);
            break;
        case HITTYPE_STRONG:
            ret = (int)(usingstat *  1000 / statFactor + (awdamage * 13 / 10) - (ddefense + ddefense2) / damFactor);
            break;
        case HITTYPE_HARD:
            ret = (int)(usingstat *  1000 / statFactor + (awdamage * 15 / 10) - (ddefense + ddefense2) / damFactor);
            break;
        case HITTYPE_CRITICAL:
            ret = (int)(usingstat *  1000 / statFactor + (awdamage * 20 / 10) - (ddefense + ddefense2) / damFactor);
            break;
        case HITTYPE_DEADLY:
            ret = (int)(usingstat *  1000 / statFactor + (awdamage * 25 / 10) - (ddefense + ddefense2) / damFactor);
            break;
        }

flag is a char. I want to do something like the code below. (Which is obviously wrong, but should show what I want to do) The if checks for some things, in this case if the attacker and defender is a pc player, and if they are over a certain level. I want to make a different damage calculation for certain levels / or if the defender/Attacker is an npc. But I dont know how to implement it the right way. Could I use IF statements in the different HITTYPE Cases?

    if (IS_PC(of) && IS_PC(df) && pPClvl->m_level >= 100 && pPClvl2->m_level >= 90 )
{

        switch (flag)
        {
        case HITTYPE_WEAK:
            ret = (int)(usingstat *  10 / statFactor + ((awdamage * 7 / 10 ) * 7 / 10) - ((ddefense * 12 / 10) + (ddefense2 * 12 / 10)) / damFactor);  
            break;
        case HITTYPE_NORMAL:
            ret = (int)(usingstat *  10 / statFactor + (awdamage * 7 / 10 ) - ((ddefense * 12 / 10) + (ddefense2 * 12 / 10)) / damFactor); 
            break;
        case HITTYPE_STRONG:
            ret = (int)(usingstat *  10 / statFactor + ((awdamage * 7 / 10 ) * 12 / 10) - ((ddefense * 12 / 10) + (ddefense2 * 12 / 10)) / damFactor);
            break;
        case HITTYPE_HARD:
            ret = (int)(usingstat *  10 / statFactor + ((awdamage * 7 / 10 ) * 13 / 10) - ((ddefense * 12 / 10) + (ddefense2 * 12 / 10)) / damFactor);
            break;
        case HITTYPE_CRITICAL:
            ret = (int)(usingstat *  10 / statFactor + ((awdamage * 7 / 10 ) * 17 / 10) - ((ddefense * 12 / 10) + (ddefense2 * 12 / 10)) / damFactor); 
            break;
        case HITTYPE_DEADLY:
            ret = (int)(usingstat *  10 / statFactor + ((awdamage * 7 / 10 ) * 20 / 10) - ((ddefense * 12 / 10) + (ddefense2 * 12 / 10)) / damFactor);
            break;
        }

}
        else if  (IS_PC(of) && IS_PC(df) )
{

        switch (flag)
        {
        case HITTYPE_WEAK:
            ret = (int)(usingstat *  10 / statFactor + (awdamage * 7 / 10) - (ddefense + ddefense2) / damFactor);
            break;
        case HITTYPE_NORMAL:
            ret = (int)(usingstat *  10 / statFactor + awdamage - (ddefense + ddefense2) / damFactor); 
            break;
        case HITTYPE_STRONG:
            ret = (int)(usingstat *  10 / statFactor + (awdamage * 12 / 10) - (ddefense + ddefense2) / damFactor);
            break;
        case HITTYPE_HARD:
            ret = (int)(usingstat *  10 / statFactor + (awdamage * 13 / 10) - (ddefense + ddefense2) / damFactor);
            break;
        case HITTYPE_CRITICAL:
            ret = (int)(usingstat *  10 / statFactor + (awdamage * 17 / 10) - (ddefense + ddefense2) / damFactor); 
            break;
        case HITTYPE_DEADLY:
            ret = (int)(usingstat *  10 / statFactor + (awdamage * 20 / 10) - (ddefense + ddefense2) / damFactor);
            break;
        }

}

        else

        switch (flag)
        {
        case HITTYPE_WEAK:
            ret = (int)(usingstat *  10 / statFactor + (awdamage * 8 / 10) - (ddefense + ddefense2) / damFactor);
            break;
        case HITTYPE_NORMAL:
            ret = (int)(usingstat *  10 / statFactor + awdamage - (ddefense + ddefense2) / damFactor); 
            break;
        case HITTYPE_STRONG:
            ret = (int)(usingstat *  10 / statFactor + (awdamage * 13 / 10) - (ddefense + ddefense2) / damFactor);
            break;
        case HITTYPE_HARD:
            ret = (int)(usingstat *  10 / statFactor + (awdamage * 15 / 10) - (ddefense + ddefense2) / damFactor);
            break;
        case HITTYPE_CRITICAL:
            ret = (int)(usingstat *  10 / statFactor + (awdamage * 20 / 10) - (ddefense + ddefense2) / damFactor); //
            break;
        case HITTYPE_DEADLY:
            ret = (int)(usingstat *  10 / statFactor + (awdamage * 25 / 10) - (ddefense + ddefense2) / damFactor);
            break;
        }

Aucun commentaire:

Enregistrer un commentaire