root/trunk/src/game/HateMatrix.h @ 19

Revision 2, 2.4 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_HATEMATRIX_H
20#define MANGOS_HATEMATRIX_H
21
22#include "Utilities/HashMap.h"
23#include <cassert>
24
25class Unit;
26
27struct MANGOS_DLL_DECL HateMatrix
28{
29    typedef hash_map<Unit *, uint32> HateMatrixMapType;
30
31    inline uint32 operator[](Unit *unit) const
32    {
33        HateMatrixMapType::const_iterator iter = i_hateValues.find(unit);
34        return (iter == i_hateValues.end() ? 0 : iter->second);
35    }
36
37    inline uint32& operator[](Unit *unit)
38    {
39        HateMatrixMapType::iterator iter = i_hateValues.find(unit);
40        if( iter == i_hateValues.end() )
41        {
42            std::pair<HateMatrixMapType::iterator, bool> p = i_hateValues.insert( HateMatrixMapType::value_type(unit, 0) );
43            assert(p.second);
44            iter = p.first;
45        }
46
47        return iter->second;
48    }
49
50    inline void ClearMatrix(void) { i_hateValues.clear(); }
51
52    inline void RemoveValue(Unit *unit)
53    {
54        HateMatrixMapType::iterator iter = i_hateValues.find(unit);
55        if( iter != i_hateValues.end() )
56            i_hateValues.erase( iter );
57    }
58
59    inline void AddValue(Unit *unit, uint32 val)
60    {
61        (*this)[unit] += val;
62    }
63
64    private:
65        HateMatrixMapType i_hateValues;
66};
67
68struct HateBinder
69{
70    static uint32 si_noHateValue;
71    uint32 &i_hateValue;
72    Unit *i_unit;
73    HateBinder(uint32 &val, Unit *u) : i_hateValue(val), i_unit(u) {}
74    HateBinder() : i_hateValue(si_noHateValue), i_unit(NULL) {}
75    HateBinder(const HateBinder &obj) : i_hateValue(obj.i_hateValue), i_unit(obj.i_unit) {}
76
77    HateBinder& operator=(const HateBinder &obj)
78    {
79        i_hateValue = obj.i_hateValue;
80        i_unit = obj.i_unit;
81        return *this;
82    }
83};
84#endif
Note: See TracBrowser for help on using the browser.