| 1 | /* |
|---|
| 2 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
|---|
| 3 | * |
|---|
| 4 | * Thanks to the original authors: MaNGOS <http://www.mangosproject.org/> |
|---|
| 5 | * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | * it under the terms of the GNU General Public License as published by |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | * (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with this program; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #ifndef TRINITY_HATEMATRIX_H |
|---|
| 22 | #define TRINITY_HATEMATRIX_H |
|---|
| 23 | |
|---|
| 24 | #include "Utilities/HashMap.h" |
|---|
| 25 | #include <cassert> |
|---|
| 26 | |
|---|
| 27 | class Unit; |
|---|
| 28 | |
|---|
| 29 | struct TRINITY_DLL_DECL HateMatrix |
|---|
| 30 | { |
|---|
| 31 | typedef hash_map<Unit *, uint32> HateMatrixMapType; |
|---|
| 32 | |
|---|
| 33 | inline uint32 operator[](Unit *unit) const |
|---|
| 34 | { |
|---|
| 35 | HateMatrixMapType::const_iterator iter = i_hateValues.find(unit); |
|---|
| 36 | return (iter == i_hateValues.end() ? 0 : iter->second); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | inline uint32& operator[](Unit *unit) |
|---|
| 40 | { |
|---|
| 41 | HateMatrixMapType::iterator iter = i_hateValues.find(unit); |
|---|
| 42 | if( iter == i_hateValues.end() ) |
|---|
| 43 | { |
|---|
| 44 | std::pair<HateMatrixMapType::iterator, bool> p = i_hateValues.insert( HateMatrixMapType::value_type(unit, 0) ); |
|---|
| 45 | assert(p.second); |
|---|
| 46 | iter = p.first; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | return iter->second; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | inline void ClearMatrix(void) { i_hateValues.clear(); } |
|---|
| 53 | |
|---|
| 54 | inline void RemoveValue(Unit *unit) |
|---|
| 55 | { |
|---|
| 56 | HateMatrixMapType::iterator iter = i_hateValues.find(unit); |
|---|
| 57 | if( iter != i_hateValues.end() ) |
|---|
| 58 | i_hateValues.erase( iter ); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | inline void AddValue(Unit *unit, uint32 val) |
|---|
| 62 | { |
|---|
| 63 | (*this)[unit] += val; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | private: |
|---|
| 67 | HateMatrixMapType i_hateValues; |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | struct HateBinder |
|---|
| 71 | { |
|---|
| 72 | static uint32 si_noHateValue; |
|---|
| 73 | uint32 &i_hateValue; |
|---|
| 74 | Unit *i_unit; |
|---|
| 75 | HateBinder(uint32 &val, Unit *u) : i_hateValue(val), i_unit(u) {} |
|---|
| 76 | HateBinder() : i_hateValue(si_noHateValue), i_unit(NULL) {} |
|---|
| 77 | HateBinder(const HateBinder &obj) : i_hateValue(obj.i_hateValue), i_unit(obj.i_unit) {} |
|---|
| 78 | |
|---|
| 79 | HateBinder& operator=(const HateBinder &obj) |
|---|
| 80 | { |
|---|
| 81 | i_hateValue = obj.i_hateValue; |
|---|
| 82 | i_unit = obj.i_unit; |
|---|
| 83 | return *this; |
|---|
| 84 | } |
|---|
| 85 | }; |
|---|
| 86 | #endif |
|---|