1 /* Copyright (C) 2013-2014 Michal Brzozowski (rusolis@poczta.fm)
2 
3    This file is part of KeeperRL.
4 
5    KeeperRL is free software; you can redistribute it and/or modify it under the terms of the
6    GNU General Public License as published by the Free Software Foundation; either version 2
7    of the License, or (at your option) any later version.
8 
9    KeeperRL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10    even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License along with this program.
14    If not, see http://www.gnu.org/licenses/ . */
15 
16 #include "stdafx.h"
17 
18 #include "view_index.h"
19 #include "view_object.h"
20 
SERIALIZE_DEF(ViewIndex,objIndex,highlight,objects,anyHighlight)21 SERIALIZE_DEF(ViewIndex, objIndex, highlight, objects, anyHighlight)
22 
23 ViewIndex::ViewIndex() {
24   for (auto& elem : objIndex)
25     elem = 100;
26 }
27 
~ViewIndex()28 ViewIndex::~ViewIndex() {
29 }
30 
insert(const ViewObject & obj)31 void ViewIndex::insert(const ViewObject& obj) {
32   int ind = objIndex[int(obj.layer())];
33   if (ind < 100)
34     objects[ind] = obj;
35   else {
36     objIndex[int(obj.layer())] = objects.size();
37     objects.push_back(obj);
38   }
39 }
40 
hasObject(ViewLayer l) const41 bool ViewIndex::hasObject(ViewLayer l) const {
42   return objIndex[int(l)] < 100;
43 }
44 
removeObject(ViewLayer l)45 void ViewIndex::removeObject(ViewLayer l) {
46   objIndex[int(l)] = 100;
47 }
48 
isEmpty() const49 bool ViewIndex::isEmpty() const {
50   return objects.empty() && !anyHighlight;
51 }
52 
hasAnyHighlight() const53 bool ViewIndex::hasAnyHighlight() const {
54   return anyHighlight;
55 }
56 
noObjects() const57 bool ViewIndex::noObjects() const {
58   return objects.empty();
59 }
60 
getObject(ViewLayer l) const61 const ViewObject& ViewIndex::getObject(ViewLayer l) const {
62   int ind = objIndex[int(l)];
63   CHECK(ind >= 0 && ind < objects.size()) << "No object on layer " << int(l) << " " << ind;
64   return objects[ind];
65 }
66 
getObject(ViewLayer l)67 ViewObject& ViewIndex::getObject(ViewLayer l) {
68   int ind = objIndex[int(l)];
69   CHECK(ind < 100) << "No object on layer " << int(l);
70   return objects[ind];
71 }
72 
getTopObject(const vector<ViewLayer> & layers) const73 const ViewObject* ViewIndex::getTopObject(const vector<ViewLayer>& layers) const {
74   for (int i = layers.size() - 1; i >= 0; --i)
75     if (hasObject(layers[i]))
76       return &getObject(layers[i]);
77   return nullptr;
78 }
79 
setHighlight(HighlightType h,double amount)80 void ViewIndex::setHighlight(HighlightType h, double amount) {
81   CHECK(amount >= 0 && amount <= 1);
82   if (amount > 0)
83     anyHighlight = true;
84   highlight[h] = amount;
85 }
86 
getHighlight(HighlightType h) const87 double ViewIndex::getHighlight(HighlightType h) const {
88   return highlight[h];
89 }
90 
getHiddenId() const91 optional<ViewId> ViewIndex::getHiddenId() const {
92   return hiddenId;
93 }
94 
setHiddenId(ViewId id)95 void ViewIndex::setHiddenId(ViewId id) {
96   hiddenId = id;
97 }
98 
getHighlightMap() const99 const EnumMap<HighlightType, double>& ViewIndex::getHighlightMap() const {
100   return highlight;
101 }
102 
mergeFromMemory(const ViewIndex & memory)103 void ViewIndex::mergeFromMemory(const ViewIndex& memory) {
104   if (isEmpty())
105     *this = memory;
106   else if (!hasObject(ViewLayer::FLOOR) && !hasObject(ViewLayer::FLOOR_BACKGROUND) && !isEmpty()) {
107     // special case when monster or item is visible but floor is only in memory
108     if (memory.hasObject(ViewLayer::FLOOR))
109       insert(memory.getObject(ViewLayer::FLOOR));
110     if (memory.hasObject(ViewLayer::FLOOR_BACKGROUND))
111       insert(memory.getObject(ViewLayer::FLOOR_BACKGROUND));
112   }
113 }
114