d2mercs.c 9.24 KB
#include "d2mercs.h"

#include <stdio.h>
#include <stdlib.h>

const char* getMercName(uint16_t mercID, uint16_t mercNameID) {
    int offset = getMercType(mercID);
    if(offset == D2S_MERCTYPE_UNKNOWN) {
        fprintf(stderr,"libd2char error: Unable to retrieve name for mercID %d, mercNameID %d\n",mercID, mercNameID);
        return NULL;
    }
    return mercNames[mercNameID + offset];
}

D2S_MERCTYPE getMercType(uint16_t mercID) {
    if(mercID >= 0 && mercID <= 5) {
        return D2S_MERCTYPE_ROGUE;
    } else if(mercID >= 6 && mercID <= 14) {
        return D2S_MERCTYPE_DESERT;
    } else if(mercID >= 15 && mercID <= 23) {
        return D2S_MERCTYPE_SORCEROR;
    } else if(mercID >= 24 && mercID <= 29) {
        return D2S_MERCTYPE_BARBARIAN;
    } else {
        return D2S_MERCTYPE_UNKNOWN;
    }
}

D2S_MERCSUBTYPE getMercSubType(uint16_t mercID) {
    D2S_MERCSUBTYPE subtype;
    switch(getMercType(mercID)) {
        case D2S_MERCTYPE_ROGUE:
            if(mercID == 0 || mercID == 2 || mercID == 4) {
                subtype = D2S_MERCSUBTYPE_FIRE_ARROW;
            } else {
                subtype = D2S_MERCSUBTYPE_COLD_ARROW;
            }
        break;
        case D2S_MERCTYPE_DESERT:
            if(mercID == 6 || mercID == 9 || mercID == 12) {
                subtype = D2S_MERCSUBTYPE_COMBAT;
            } if(mercID == 7 || mercID == 10 || mercID == 13) {
                subtype = D2S_MERCSUBTYPE_DEFENSIVE;
            } else {
                subtype = D2S_MERCSUBTYPE_OFFENSIVE;
            }
        break;
        case D2S_MERCTYPE_SORCEROR:
            if(mercID == 15 || mercID == 18 || mercID == 21) {
                subtype = D2S_MERCSUBTYPE_FIRE;
            } if(mercID == 16 || mercID == 19 || mercID == 22) {
                subtype = D2S_MERCSUBTYPE_COLD;
            } else {
                subtype = D2S_MERCSUBTYPE_LIGHTNING;
            }
        break;
        case D2S_MERCTYPE_BARBARIAN:
        case D2S_MERCTYPE_UNKNOWN:
            subtype = D2S_MERCSUBTYPE_NONE;
        break;
    }
    return subtype;
}

D2S_DIFFICULTY getMercDifficulty(uint16_t mercID) {
    if(mercID == 0 || mercID == 1 || 
       mercID == 6 || mercID == 7 || mercID == 8 ||
       mercID == 15 || mercID == 16 || mercID == 17 ||
       mercID == 24 || mercID == 25) {
           return D2S_DIFFICULTY_NORMAL;
    } else if (mercID == 2 || mercID == 3 || 
       mercID == 9 || mercID == 10 || mercID == 11 ||
       mercID == 18 || mercID == 19 || mercID == 20 ||
       mercID == 26 || mercID == 27) {
        return D2S_DIFFICULTY_NIGHTMARE;
    } else if (mercID == 4 || mercID == 5 || 
       mercID == 12 || mercID == 13 || mercID == 14 ||
       mercID == 21 || mercID == 22 || mercID == 23 ||
       mercID == 28 || mercID == 29) {
        return D2S_DIFFICULTY_HELL;
    }
    return D2S_DIFFICULTY_UNKNOWN;   
}

int setMerc(D2CharHeader* c, D2S_MERCTYPE type, D2S_MERCSUBTYPE subtype, D2S_DIFFICULTY difficulty) {
    switch(type) {
        case D2S_MERCTYPE_ROGUE:
            switch(subtype) {
                case D2S_MERCSUBTYPE_FIRE_ARROW:
                    switch(difficulty) {
                        case D2S_DIFFICULTY_NORMAL:
                            c->mercenaryType = 0;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_NIGHTMARE:
                            c->mercenaryType = 2;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_HELL:
                            c->mercenaryType = 4;
                            return 0;
                        break;
                    }
                break;
                case D2S_MERCSUBTYPE_COLD_ARROW:
                    switch(difficulty) {
                        case D2S_DIFFICULTY_NORMAL:
                            c->mercenaryType = 1;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_NIGHTMARE:
                            c->mercenaryType = 3;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_HELL:
                            c->mercenaryType = 5;
                            return 0;
                        break;
                    }
                break;
            }
        break;
        case D2S_MERCTYPE_DESERT:
            switch(subtype) {
                case D2S_MERCSUBTYPE_COMBAT:
                    switch(difficulty) {
                        case D2S_DIFFICULTY_NORMAL:
                            c->mercenaryType = 6;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_NIGHTMARE:
                            c->mercenaryType = 9;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_HELL:
                            c->mercenaryType = 12;
                            return 0;
                        break;
                    }
                break;
                case D2S_MERCSUBTYPE_DEFENSIVE:
                    switch(difficulty) {
                        case D2S_DIFFICULTY_NORMAL:
                            c->mercenaryType = 7;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_NIGHTMARE:
                            c->mercenaryType = 10;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_HELL:
                            c->mercenaryType = 13;
                            return 0;
                        break;
                    }
                break;
                case D2S_MERCSUBTYPE_OFFENSIVE:
                    switch(difficulty) {
                        case D2S_DIFFICULTY_NORMAL:
                            c->mercenaryType = 8;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_NIGHTMARE:
                            c->mercenaryType = 11;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_HELL:
                            c->mercenaryType = 14;
                            return 0;
                        break;
                    }
                break;
            }
        break;
        case D2S_MERCTYPE_SORCEROR:
            switch(subtype) {
                case D2S_MERCSUBTYPE_FIRE:
                    switch(difficulty) {
                        case D2S_DIFFICULTY_NORMAL:
                            c->mercenaryType = 15;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_NIGHTMARE:
                            c->mercenaryType = 18;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_HELL:
                            c->mercenaryType = 21;
                            return 0;
                        break;
                    }
                break;
                case D2S_MERCSUBTYPE_COLD:
                    switch(difficulty) {
                        case D2S_DIFFICULTY_NORMAL:
                            c->mercenaryType = 16;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_NIGHTMARE:
                            c->mercenaryType = 19;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_HELL:
                            c->mercenaryType = 22;
                            return 0;
                        break;
                    }
                break;
                case D2S_MERCSUBTYPE_LIGHTNING:
                    switch(difficulty) {
                        case D2S_DIFFICULTY_NORMAL:
                            c->mercenaryType = 17;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_NIGHTMARE:
                            c->mercenaryType = 20;
                            return 0;
                        break;
                        case D2S_DIFFICULTY_HELL:
                            c->mercenaryType = 23;
                            return 0;
                        break;
                    }
                break;
            }
        break;
        case D2S_MERCTYPE_BARBARIAN:
            // There's actually 2 identifiers for barbarians in each difficulty, they don't seem to have 
            // noticeable effect in game, so I'm considering only one of them. If you want to specifcially
            // change to the other type, you probably know already what the mercID is so change it directly
            // in the header instead of using this function
            switch(difficulty) {
                case D2S_DIFFICULTY_NORMAL:
                    c->mercenaryType = 24;
                    return 0;
                break;
                case D2S_DIFFICULTY_NIGHTMARE:
                    c->mercenaryType = 26;
                    return 0;
                break;
                case D2S_DIFFICULTY_HELL:
                    c->mercenaryType = 28;
                    return 0;
                break;
            }
        break;
    }
    fprintf(stderr,"libd2char error: invalid combination of parameters for setMerc()\n");
    return -1;
}