aboutsummaryrefslogtreecommitdiff
path: root/src/game/Cell.h
blob: 802c7bd8c45cec8e315a0f583f2e6bb793d1adba (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
 *
 * Copyright (C) 2008-2009 Trinity <http://www.trinitycore.org/>
 *
 * 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
 * (at your option) 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef TRINITY_CELL_H
#define TRINITY_CELL_H

#include <cmath>

#include "GameSystem/TypeContainer.h"
#include "GameSystem/TypeContainerVisitor.h"

#include "GridDefines.h"

class Map;
class WorldObject;

enum District
{
    UPPER_DISTRICT = 1,
    LOWER_DISTRICT = 1 << 1,
    LEFT_DISTRICT  = 1 << 2,
    RIGHT_DISTRICT = 1 << 3,
    CENTER_DISTRICT = 1 << 4,
    UPPER_LEFT_DISTRICT = (UPPER_DISTRICT | LEFT_DISTRICT),
    UPPER_RIGHT_DISTRICT = (UPPER_DISTRICT | RIGHT_DISTRICT),
    LOWER_LEFT_DISTRICT = (LOWER_DISTRICT | LEFT_DISTRICT),
    LOWER_RIGHT_DISTRICT = (LOWER_DISTRICT | RIGHT_DISTRICT),
    ALL_DISTRICT = (UPPER_DISTRICT | LOWER_DISTRICT | LEFT_DISTRICT | RIGHT_DISTRICT | CENTER_DISTRICT)
};

template<class T> struct CellLock;

struct TRINITY_DLL_DECL CellArea
{
    CellArea() : right_offset(0), left_offset(0), upper_offset(0), lower_offset(0) {}
    CellArea(int right, int left, int upper, int lower) : right_offset(right), left_offset(left), upper_offset(upper), lower_offset(lower) {}
    bool operator!() const { return !right_offset && !left_offset && !upper_offset && !lower_offset; }

    void ResizeBorders(CellPair& begin_cell, CellPair& end_cell) const
    {
        begin_cell << left_offset;
        begin_cell -= lower_offset;
        end_cell >> right_offset;
        end_cell += upper_offset;
    }

    int right_offset;
    int left_offset;
    int upper_offset;
    int lower_offset;
};

struct TRINITY_DLL_DECL Cell
{
    Cell() { data.All = 0; }
    Cell(const Cell &cell) { data.All = cell.data.All; }
    explicit Cell(CellPair const& p);

    void operator|=(Cell &cell)
    {
        data.Part.reserved = 0;
        cell.data.Part.reserved = 0;
        uint32 x, y, old_x, old_y;
        Compute(x, y);
        cell.Compute(old_x, old_y);

        if( std::abs(int(x-old_x)) > 1 || std::abs(int(y-old_y)) > 1)
        {
            data.Part.reserved = ALL_DISTRICT;
            cell.data.Part.reserved = ALL_DISTRICT;
            return;
        }

        if( x < old_x )
        {
            data.Part.reserved |= LEFT_DISTRICT;
            cell.data.Part.reserved |= RIGHT_DISTRICT;
        }
        else if( old_x < x )
        {
            data.Part.reserved |= RIGHT_DISTRICT;
            cell.data.Part.reserved |= LEFT_DISTRICT;
        }
        if( y < old_y )
        {
            data.Part.reserved |= UPPER_DISTRICT;
            cell.data.Part.reserved |= LOWER_DISTRICT;
        }
        else if( old_y < y )
        {
            data.Part.reserved |= LOWER_DISTRICT;
            cell.data.Part.reserved |= UPPER_DISTRICT;
        }
    }

    void Compute(uint32 &x, uint32 &y) const
    {
        x = data.Part.grid_x*MAX_NUMBER_OF_CELLS + data.Part.cell_x;
        y = data.Part.grid_y*MAX_NUMBER_OF_CELLS + data.Part.cell_y;
    }

    bool DiffCell(const Cell &cell) const
    {
        return( data.Part.cell_x != cell.data.Part.cell_x ||
            data.Part.cell_y != cell.data.Part.cell_y );
    }

    bool DiffGrid(const Cell &cell) const
    {
        return( data.Part.grid_x != cell.data.Part.grid_x ||
            data.Part.grid_y != cell.data.Part.grid_y );
    }

    uint32 CellX() const { return data.Part.cell_x; }
    uint32 CellY() const { return data.Part.cell_y; }
    uint32 GridX() const { return data.Part.grid_x; }
    uint32 GridY() const { return data.Part.grid_y; }
    bool NoCreate() const { return data.Part.nocreate; }
    void SetNoCreate() { data.Part.nocreate = 1; }

    CellPair cellPair() const
    {
        return CellPair(
            data.Part.grid_x*MAX_NUMBER_OF_CELLS+data.Part.cell_x,
            data.Part.grid_y*MAX_NUMBER_OF_CELLS+data.Part.cell_y);
    }

    Cell& operator=(const Cell &cell)
    {
        this->data.All = cell.data.All;
        return *this;
    }

    bool operator==(const Cell &cell) const { return (data.All == cell.data.All); }
    bool operator!=(const Cell &cell) const { return !operator==(cell); }
    union
    {
        struct
        {
            unsigned grid_x : 6;
            unsigned grid_y : 6;
            unsigned cell_x : 6;
            unsigned cell_y : 6;
            unsigned nocreate : 1;
            unsigned reserved : 7;
        } Part;
        uint32 All;
    } data;

    template<class LOCK_TYPE, class T, class CONTAINER> void Visit(const CellLock<LOCK_TYPE> &, TypeContainerVisitor<T, CONTAINER> &visitor, Map &) const;
    template<class LOCK_TYPE, class T, class CONTAINER> void Visit(const CellLock<LOCK_TYPE> &, TypeContainerVisitor<T, CONTAINER> &visitor, Map &m, const WorldObject &obj, float radius) const;
    template<class LOCK_TYPE, class T, class CONTAINER> void Visit(const CellLock<LOCK_TYPE> &, TypeContainerVisitor<T, CONTAINER> &visitor, Map &, float radius, float x_off, float y_off) const;

    static CellArea CalculateCellArea(const WorldObject &obj, float radius);
    static CellArea CalculateCellArea(float x, float y, float radius);

private:
    template<class LOCK_TYPE, class T, class CONTAINER> void VisitCircle(const CellLock<LOCK_TYPE> &, TypeContainerVisitor<T, CONTAINER> &, Map &, const CellPair& , const CellPair& ) const;
};

template<class T>
struct TRINITY_DLL_DECL CellLock
{
    const Cell& i_cell;
    const CellPair &i_cellPair;
    CellLock(const Cell &c, const CellPair &p) : i_cell(c), i_cellPair(p) {}
    CellLock(const CellLock<T> &cell) : i_cell(cell.i_cell), i_cellPair(cell.i_cellPair) {}
    const Cell* operator->(void) const { return &i_cell; }
    const Cell* operator->(void) { return &i_cell; }
    operator const Cell &(void) const { return i_cell; }
    CellLock<T>& operator=(const CellLock<T> &cell)
    {
        this->~CellLock();
        new (this) CellLock<T>(cell);
        return *this;
    }
};
#endif