Thursday, August 11, 2022
HomeGame Developmentc++ - A variety assault algorithm for turn-based ways

c++ – A variety assault algorithm for turn-based ways


I’ve tried Bresenham line and Raycasting, however they do not fairly give the outcomes I want (examples within the screenshot).

‘&’ – character

‘+’ – cells that may be attacked by the
character

‘#’ – partitions

Supercover:

Supercover

Supercover

Bresenham:

Bresenham

Bresenham

As could be seen within the screenshot, a personality can assault a cell, but when moved to it, it can’t assault from there to its authentic place.

Is there a good algorithm so {that a} character who can assault an enemy could be attacked by the enemy in return.

Maybe the issue is in my code?

void FieldOfSight(Subject &area, const Vector2i &participant, float distance,
                  int sort)
{
    Vector2i endPosition;
    float    radians = 0;
    float    step    = M_PI / (distance * 10);

    whereas (radians < 2 * M_PI) {
        endPosition.x = participant.x + spherical(cos(radians) * (distance));
        endPosition.y = participant.y + spherical(sin(radians) * (distance));
        if (endPosition.y >= area.GetHeight())
            endPosition.y = area.GetHeight() - 1;
        else if (endPosition.y <= 0)
            endPosition.y = 0;
        if (endPosition.x >= area.GetWidth())
            endPosition.x = area.GetWidth() - 1;
        else if (endPosition.x <= 0)
            endPosition.x = 0;
        radians += step;

        change (sort) {
        case (0):
            SupercoverLine(area, participant, endPosition);
            break;
        case (1):
            WalkLine(area, participant, endPosition);
            break;
        case (2):
            BresenhamLine(area, participant, endPosition);
            break;
        }
    }
}

void SupercoverLine(Subject &area, int x0, int y0, int x1, int y1)
{
    int dx = x1 - x0, dy = y1 - y0;
    int nx = abs(dx), ny = abs(dy);
    int sign_x = dx > 0 ? 1 : -1, sign_y = dy > 0 ? 1 : -1;

    int px = x0, py = y0;

    area.SetFG(px, py, 8);

    for (int ix = 0, iy = 0; ix < nx || iy < ny;) {
        int choice = (1 + 2 * ix) * ny - (1 + 2 * iy) * nx;
        if (choice == 0) {
            px += sign_x;
            py += sign_y;
            if (area[py - sign_y][px] == 1 && area[py][px - sign_x] == 1)
                break;
            ++ix;
            ++iy;
        } else if (choice < 0) {
            px += sign_x;
            ++ix;
        } else {
            py += sign_y;
            ++iy;
        }
        if (sqrt((x0 - px) * (x0 - px) + (y0 - py) * (y0 - py)) > 5 ||
            area[py][px] == 1)
            break;
        area.SetFG(px, py, 8);
    }
}

void WalkLine(Subject &area, int x0, int y0, int x1, int y1)
{
    int dx = x1 - x0, dy = y1 - y0;
    int nx = abs(dx), ny = abs(dy);
    int sign_x = dx > 0 ? 1 : -1, sign_y = dy > 0 ? 1 : -1;

    int px = x0, py = y0;
    for (int ix = 0, iy = 0; ix < nx || iy < ny;) {
        if ((0.5 + ix) / nx < (0.5 + iy) / ny) {
            px += sign_x;
            ix++;
        } else {
            py += sign_y;
            iy++;
        }
        if (sqrt((x0 - px) * (x0 - px) + (y0 - py) * (y0 - py)) > 5 ||
            area[py][px] == 1)
            break;
        area.SetFG(px, py, 8);
    }
}

void BresenhamLine(Subject &area, int x0, int y0, int x1, int y1)
{
    int dx    = abs(x1 - x0);
    int sx    = x0 < x1 ? 1 : -1;
    int dy    = -abs(y1 - y0);
    int sy    = y0 < y1 ? 1 : -1;
    int error = dx + dy;
    int x = x0, y = y0;

    area.SetFG(x0, y0, 8);

    whereas (true) {
        if (x0 == x1 && y0 == y1)
            break;

        int e2 = 2 * error;

        if (e2 >= dy) {
            if (x == x1)
                break;
            error += dy;
            x += sx;
        }
        if (e2 <= dx) {
            if (y == y1)
                break;
            error += dx;
            y += sy;
        }

        if ((area[y - sy][x] == 1 && area[y][x - sx] == 1) ||
            sqrt((x0 - x) * (x0 - x) + (y0 - y) * (y0 - y)) > 5 ||
            area[y][x] == 1)
            break;
        area.SetFG(x, y, 8);
    }
}

Sorry for the code with out feedback

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments