Any Tips to optimize this Melee code

04/08/2017 18:28 ha.ho.a#1
Code:
{
    if (!IsRunning)
        return;
    if (CLooter::instance()->block())
        return;
    // Seek the nearest role which is a monster.
    int px, py;
    CO::CHero::instance()->getCoords(&px, &py);
    CO::CRole* nearRole = NULL;
    int minDist = INT_MAX;
    for (int i = 0; i < CO::CMobList::instance()->size(); ++i)
    {
        CO::CRole* role = (*CO::CMobList::instance())[i];
        if (role == NULL)
            break;
        if (role->alive() && !(role->isPlayer()))
        {
            int rx, ry;
            role->getCoords(&rx, &ry);
            int dx = rx - px;
            int dy = ry - py;
            int dist = dx*dx + dy*dy;
            if (dist < minDist)
            {
                minDist = dist;
                nearRole = role;
            }
        }
    }
    if (nearRole != NULL)
    {
        //if (!(CPathRecorder::instance()->isBlocked()))
        //    CPathRecorder::instance()->block();
        if (minDist < MinAttackDistance*MinAttackDistance)
        {
            CO::CHero::instance()->melee(nearRole);
        }
        else
        {
            int x, y;
            nearRole->getCoords(&x, &y);
            ++x; ++y;
            int dx = x - px;
            int dy = y - py;
            if (dx*dx+dy*dy>=10*10)
            {
                float a = 10.0 / sqrt(dx*dx+dy*dy);
                dx = (int)(a * (float)dx);
                dy = (int)(a * (float)dy);
            }
            x = px + dx;
            y = py + dy;
            CO::Functions::findClearGround(&x, &y);
            CO::CHero::instance()->jump(x, y);
        }
    }
    else
    {
        //if (CPathRecorder::instance()->isBlocked())
        //    CPathRecorder::instance()->unblock();
    }
}
any tips on how to make it faster for fatal strike??,,,
04/18/2017 08:09 donn#2
Only for fatal strike or you're looking for a code working for any melee attacks? Because for fatal strike you don't need to jump before attack, just send the attack packet.

Also, in CO2 world, jumping 18 coords or 1 coord takes the same amount of time. So finding the closest mob does not always implies a faster melee. Attacking the first mob in your mobs list that is in attack range should save a few loops.
04/30/2017 20:13 detuks#3
Just wondering... you use some public CO API or made your own?
05/01/2017 01:05 phize#4
Quote:
Originally Posted by detuks View Post
Just wondering... you use some public CO API or made your own?
[Only registered and activated users can see links. Click Here To Register...]