summaryrefslogtreecommitdiff
path: root/src/navigationmanager.cpp
blob: 343876bde2a2b2ea29ae233c400730621f0c1d87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 *  The ManaPlus Client
 *  Copyright (C) 2013-2020  The ManaPlus Developers
 *  Copyright (C) 2020-2023  The ManaVerse Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "navigationmanager.h"

#include "enums/resources/map/blockmask.h"

#include "resources/map/map.h"
#include "resources/map/metatile.h"
#include "resources/map/walklayer.h"

#include "debug.h"

static const int blockWalkMask = (BlockMask::WALL |
    BlockMask::AIR |
    BlockMask::WATER);

#ifndef DYECMD
namespace
{
    struct Cell final
    {
        Cell(const int x0, const int y0) :
            x(x0),
            y(y0)
        {
        }

        A_DEFAULT_COPY(Cell)

        int x;
        int y;
    };
}  // namespace
#endif  // DYECMD

NavigationManager::NavigationManager()
{
}

NavigationManager::~NavigationManager()
{
}

#ifndef DYECMD
Resource *NavigationManager::loadWalkLayer(const Map *const map)
{
    if (map == nullptr)
        return nullptr;

    const int width = map->getWidth();
    const int height = map->getHeight();
    if (width < 2 || height < 2)
        return nullptr;
    WalkLayer *const walkLayer = new WalkLayer(width, height);

    const MetaTile *const tiles = map->getMetaTiles();
    int *const data = walkLayer->getData();
    if ((tiles == nullptr) || (data == nullptr))
        return walkLayer;

    int x = 0;
    int y = 0;
    int num = 1;
    while (findWalkableTile(x, y, width, height, tiles, data))
    {
        fillNum(x, y, width, height, num, tiles, data);
        num ++;
    }

    return walkLayer;
}

void NavigationManager::fillNum(int x, int y,
                                const int width,
                                const int height,
                                const int num,
                                const MetaTile *const tiles,
                                int *const data)
{
    STD_VECTOR<Cell> cells;
    cells.push_back(Cell(x, y));
    while (!cells.empty())
    {
        const Cell cell = cells.back();
        cells.pop_back();
        int ptr;
        x = cell.x;
        y = cell.y;
        data[x + width * y] = num;
        if (x > 0)
        {
            ptr = (x - 1) + width * y;
            if (data[ptr] == 0)
            {
                if ((tiles[ptr].blockmask & blockWalkMask) == 0)
                    cells.push_back(Cell(x - 1, y));
                else
                    data[ptr] = -num;
            }
        }
        if (x < width - 1)
        {
            ptr = (x + 1) + width * y;
            if (data[ptr] == 0)
            {
                if ((tiles[ptr].blockmask & blockWalkMask) == 0)
                    cells.push_back(Cell(x + 1, y));
                else
                    data[ptr] = -num;
            }
        }
        if (y > 0)
        {
            ptr = x + width * (y - 1);
            if (data[ptr] == 0)
            {
                if ((tiles[ptr].blockmask & blockWalkMask) == 0)
                    cells.push_back(Cell(x, y - 1));
                else
                    data[ptr] = -num;
            }
        }
        if (y < height - 1)
        {
            ptr = x + width * (y + 1);
            if (data[ptr] == 0)
            {
                if ((tiles[ptr].blockmask & blockWalkMask) == 0)
                    cells.push_back(Cell(x, y + 1));
                else
                    data[ptr] = -num;
            }
        }
    }
}

bool NavigationManager::findWalkableTile(int &x1, int &y1,
                                         const int width,
                                         const int height,
                                         const MetaTile *const tiles,
                                         const int *const data)
{
    for (int y = 0; y < height; y ++)
    {
        const int y2 = y * width;
        for (int x = 0; x < width; x ++)
        {
            const int ptr = x + y2;
            if (((tiles[ptr].blockmask & blockWalkMask) == 0) &&
                data[ptr] == 0)
            {
                x1 = x;
                y1 = y;
                return true;
            }
        }
    }
    return false;
}
#endif  // DYECMD