root/trunk/src/game/Cell.h @ 34

Revision 2, 5.0 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19#ifndef MANGOS_CELL_H
20#define MANGOS_CELL_H
21
22#include "GameSystem/TypeContainer.h"
23#include "GameSystem/TypeContainerVisitor.h"
24#include "GridDefines.h"
25#include <cmath>
26
27class Map;
28
29enum District
30{
31    UPPER_DISTRICT = 1,
32    LOWER_DISTRICT = 1 << 1,
33    LEFT_DISTRICT  = 1 << 2,
34    RIGHT_DISTRICT = 1 << 3,
35    CENTER_DISTRICT = 1 << 4,
36    UPPER_LEFT_DISTRICT = (UPPER_DISTRICT | LEFT_DISTRICT),
37    UPPER_RIGHT_DISTRICT = (UPPER_DISTRICT | RIGHT_DISTRICT),
38    LOWER_LEFT_DISTRICT = (LOWER_DISTRICT | LEFT_DISTRICT),
39    LOWER_RIGHT_DISTRICT = (LOWER_DISTRICT | RIGHT_DISTRICT),
40    ALL_DISTRICT = (UPPER_DISTRICT | LOWER_DISTRICT | LEFT_DISTRICT | RIGHT_DISTRICT | CENTER_DISTRICT)
41};
42
43template<class T> struct CellLock;
44
45struct MANGOS_DLL_DECL Cell
46{
47    Cell() { data.All = 0; }
48    Cell(const Cell &cell) { data.All = cell.data.All; }
49    explicit Cell(CellPair const& p);
50
51    void operator|=(Cell &cell)
52    {
53        data.Part.reserved = 0;
54        cell.data.Part.reserved = 0;
55        uint32 x, y, old_x, old_y;
56        Compute(x, y);
57        cell.Compute(old_x, old_y);
58
59        if( std::abs(int(x-old_x)) > 1 || std::abs(int(y-old_y)) > 1)
60        {
61            data.Part.reserved = ALL_DISTRICT;
62            cell.data.Part.reserved = ALL_DISTRICT;
63            return;
64        }
65
66        if( x < old_x )
67        {
68            data.Part.reserved |= LEFT_DISTRICT;
69            cell.data.Part.reserved |= RIGHT_DISTRICT;
70        }
71        else if( old_x < x )
72        {
73            data.Part.reserved |= RIGHT_DISTRICT;
74            cell.data.Part.reserved |= LEFT_DISTRICT;
75        }
76        if( y < old_y )
77        {
78            data.Part.reserved |= UPPER_DISTRICT;
79            cell.data.Part.reserved |= LOWER_DISTRICT;
80        }
81        else if( old_y < y )
82        {
83            data.Part.reserved |= LOWER_DISTRICT;
84            cell.data.Part.reserved |= UPPER_DISTRICT;
85        }
86    }
87
88    void Compute(uint32 &x, uint32 &y) const
89    {
90        x = data.Part.grid_x*MAX_NUMBER_OF_CELLS + data.Part.cell_x;
91        y = data.Part.grid_y*MAX_NUMBER_OF_CELLS + data.Part.cell_y;
92    }
93
94    inline bool DiffCell(const Cell &cell) const
95    {
96        return( data.Part.cell_x != cell.data.Part.cell_x ||
97            data.Part.cell_y != cell.data.Part.cell_y );
98    }
99
100    inline bool DiffGrid(const Cell &cell) const
101    {
102        return( data.Part.grid_x != cell.data.Part.grid_x ||
103            data.Part.grid_y != cell.data.Part.grid_y );
104    }
105
106    uint32 CellX() const { return data.Part.cell_x; }
107    uint32 CellY() const { return data.Part.cell_y; }
108    uint32 GridX() const { return data.Part.grid_x; }
109    uint32 GridY() const { return data.Part.grid_y; }
110    bool NoCreate() const { return data.Part.nocreate; }
111    void SetNoCreate() { data.Part.nocreate = 1; }
112
113    CellPair cellPair() const
114    {
115        return CellPair(
116            data.Part.grid_x*MAX_NUMBER_OF_CELLS+data.Part.cell_x,
117            data.Part.grid_y*MAX_NUMBER_OF_CELLS+data.Part.cell_y);
118    }
119
120    Cell& operator=(const Cell &cell)
121    {
122        this->data.All = cell.data.All;
123        return *this;
124    }
125
126    bool operator==(const Cell &cell) const { return (data.All == cell.data.All); }
127    bool operator!=(const Cell &cell) const { return !operator==(cell); }
128    union
129    {
130        struct
131        {
132            unsigned grid_x : 6;
133            unsigned grid_y : 6;
134            unsigned cell_x : 4;
135            unsigned cell_y : 4;
136            unsigned nocreate : 1;
137            unsigned reserved : 11;
138        } Part;
139        uint32 All;
140    } data;
141
142    template<class LOCK_TYPE, class T, class CONTAINER> void Visit(const CellLock<LOCK_TYPE> &, TypeContainerVisitor<T, CONTAINER> &visitor, Map &) const;
143};
144
145template<class T>
146struct MANGOS_DLL_DECL CellLock
147{
148    const Cell& i_cell;
149    const CellPair &i_cellPair;
150    CellLock(const Cell &c, const CellPair &p) : i_cell(c), i_cellPair(p) {}
151    CellLock(const CellLock<T> &cell) : i_cell(cell.i_cell), i_cellPair(cell.i_cellPair) {}
152    const Cell* operator->(void) const { return &i_cell; }
153    const Cell* operator->(void) { return &i_cell; }
154    operator const Cell &(void) const { return i_cell; }
155    CellLock<T>& operator=(const CellLock<T> &cell)
156    {
157        this->~CellLock();
158        new (this) CellLock<T>(cell);
159        return *this;
160    }
161};
162#endif
Note: See TracBrowser for help on using the browser.