All pastes #1705086 Raw Edit

Miscellany

public text v1 · immutable
#1705086 ·published 2009-12-07 03:07 UTC
rendered paste body
void GameLogic::lineOfSight(int x, int y, Map &map) {
    
    queue<Tile *> tileQueue;

    //Keep tracked of 'marked' (eg. visible) tiles
    int width = map.getWidth();
    int height = map.getHeight();
    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < height; ++j) {
            map.tile(i, j).setMarked(false);
            map.tile(i, j).setVisible(false);
        }
    }
    for (int i = 0; i <= width; ++i) {
        for (int j = 0; j <= height; ++j) {
            map.point(i, j).intensity = 0.0;
        }
    }
    //doThing(map.tile(x, y));
    makeTileVisible(x, y, map, (float)x, (float)y);
    map.tile(x, y).setMarked(true);
    map.tile(x, y).setExplored(true);

    //Queue the first 8 tiles
    tileQueue.push(&map.tile(x - 1, y));
    tileQueue.push(&map.tile(x - 1, y - 1));
    tileQueue.push(&map.tile(x, y - 1));
    tileQueue.push(&map.tile(x + 1, y - 1));
    tileQueue.push(&map.tile(x + 1, y));
    tileQueue.push(&map.tile(x + 1, y + 1));
    tileQueue.push(&map.tile(x, y + 1));
    tileQueue.push(&map.tile(x - 1, y + 1));

    bool do_it = false;
    while (!tileQueue.empty()) {
        int tx = tileQueue.front()->getx();
        int ty = tileQueue.front()->gety();
        do_it = false;

        //Test if line of sight
        if(abs(x - tx) <= 1 && abs(y - ty) <= 1){
            do_it = true;
        } else {
        if (y == ty) {
            //On left or right side
            if (x < tx) {
                if (!map.tile(tx - 1, ty).isBlocking() && map.tile(tx - 1, ty).isMarked()) {
                    do_it = true;
                }
            } else {
                if (!map.tile(tx + 1, ty).isBlocking() &&map.tile(tx + 1, ty).isMarked()) {
                    do_it = true;
                }
            }
        } else if (x == tx) {
            //On top or bottom
            if (y < ty) {
                if (!map.tile(tx, ty - 1).isBlocking() && map.tile(tx, ty - 1).isMarked()) {
                    do_it = true;
                }
            } else {
                if (!map.tile(tx, ty + 1).isBlocking() && map.tile(tx, ty + 1).isMarked()) {
                    do_it = true;
                }
            }
        } else if (abs(x - tx) == abs(y - ty)) {
            //A diagonal
            if (x < tx) {
                if (y < ty) {
                    if (!map.tile(tx - 1, ty - 1).isBlocking() && map.tile(tx - 1, ty - 1).isMarked()) {
                        do_it = true;
                    }
                } else {
                    if (!map.tile(tx - 1, ty + 1).isBlocking() && map.tile(tx - 1, ty + 1).isMarked()) {
                        do_it = true;
                    }
                }
            } else {
                if (y < ty) {
                    if (!map.tile(tx + 1, ty - 1).isBlocking() && map.tile(tx + 1, ty - 1).isMarked()) {
                        do_it = true;
                    }
                } else {
                    if (!map.tile(tx + 1, ty + 1).isBlocking() && map.tile(tx + 1, ty + 1).isMarked()) {
                        do_it = true;
                    }
                }
            }
        } else {
            //Non-straight, non-diagonal
            if (abs(x - tx) < abs(y - ty)) {
                if(y < ty){
                    if (x < tx) {
                        if (!map.tile(tx, ty - 1).isBlocking() && map.tile(tx, ty - 1).isMarked() &&
                                !map.tile(tx - 1, ty - 1).isBlocking() && map.tile(tx - 1, ty - 1).isMarked()) {
                            do_it = true;
                        }
                    } else {
                        if (!map.tile(tx, ty - 1).isBlocking() && map.tile(tx, ty - 1).isMarked() &&
                                !map.tile(tx + 1, ty - 1).isBlocking() && map.tile(tx + 1, ty - 1).isMarked()) {
                            do_it = true;
                        }
                    }
                } else {
                    if (x < tx) {
                        if (!map.tile(tx, ty + 1).isBlocking() && map.tile(tx, ty + 1).isMarked() &&
                                !map.tile(tx - 1, ty + 1).isBlocking() && map.tile(tx - 1, ty + 1).isMarked()) {
                            do_it = true;
                        }
                    } else {
                        if (!map.tile(tx, ty + 1).isBlocking() && map.tile(tx, ty + 1).isMarked() &&
                                !map.tile(tx + 1, ty + 1).isBlocking() && map.tile(tx + 1, ty + 1).isMarked()) {
                            do_it = true;
                        }
                    }
                }
            } else {
                if (x < tx) {
                    if(y < ty){
                        if (!map.tile(tx - 1, ty).isBlocking() && map.tile(tx - 1, ty).isMarked() &&
                                !map.tile(tx - 1, ty - 1).isBlocking() && map.tile(tx - 1, ty - 1).isMarked()) {
                            do_it = true;
                        }
                    } else {
                        if (!map.tile(tx - 1, ty).isBlocking() && map.tile(tx - 1, ty).isMarked() &&
                                !map.tile(tx - 1, ty + 1).isBlocking() && map.tile(tx - 1, ty + 1).isMarked()) {
                            do_it = true;
                        }
                    }
                } else {
                    if(y < ty){
                        if (!map.tile(tx + 1, ty).isBlocking() && map.tile(tx + 1, ty).isMarked() &&
                                !map.tile(tx + 1, ty - 1).isBlocking() && map.tile(tx + 1, ty - 1).isMarked()) {
                            do_it = true;
                        }
                    } else {
                        if (!map.tile(tx + 1, ty).isBlocking() && map.tile(tx + 1, ty).isMarked() &&
                                !map.tile(tx + 1, ty + 1).isBlocking() && map.tile(tx + 1, ty + 1).isMarked()) {
                            do_it = true;
                        }
                    }
                }
            }
        }
        }

        //Do thing
        if (do_it) {
            map.tile(tx, ty).setMarked(true);
            makeTileVisible(tx, ty, map, (float)x, (float)y);
            map.tile(tx, ty).setExplored(true);
        }

        
        //Add to queue
        if(do_it){
            if(abs(x - tx) == abs(y - ty)) {
                //Diagonal tile
                if (x < tx) {
                    if(tx < width - 1){
                        tileQueue.push(&map.tile(tx + 1, ty));
                    }
                    if (y < ty) {
                        if(ty < height - 1){
                            tileQueue.push(&map.tile(tx, ty + 1));
                            if(tx < width - 1){
                                tileQueue.push(&map.tile(tx + 1, ty + 1));
                            }
                        }
                    } else {
                        if(ty > 0){
                            tileQueue.push(&map.tile(tx, ty - 1));
                            if(tx < width - 1){
                                tileQueue.push(&map.tile(tx + 1, ty - 1));
                            }
                        }
                    }
                 } else {
                        if(tx > 0){
                            tileQueue.push(&map.tile(tx - 1, ty));
                        }
                        if (y < ty) {
                            if(ty < height - 1){
                                tileQueue.push(&map.tile(tx, ty + 1));
                                if(tx > 0){
                                    tileQueue.push(&map.tile(tx - 1, ty + 1));
                                }
                            }
                        } else {
                            if(ty > 0){
                                tileQueue.push(&map.tile(tx, ty - 1));
                                if(tx > 0){
                                    tileQueue.push(&map.tile(tx - 1, ty - 1));
                                }
                            }
                        }
                }
            } else {
                //Not diagonal
                if (abs(x - tx) < abs(y - ty)) {
                    if (y < ty){
                        if(ty < height - 1){
                            tileQueue.push(&map.tile(tx, ty + 1));
                        }
                    } else if(ty > 0){
                        tileQueue.push(&map.tile(tx, ty - 1));
                    }
                } else {
                    if (x < tx){
                        if(tx < width - 1){
                            tileQueue.push(&map.tile(tx + 1, ty));
                        }
                    } else if(tx > 0){
                        tileQueue.push(&map.tile(tx - 1, ty));
                    }
                }
            }
        }
        
        
        //Pop
        tileQueue.pop();
    }
}