Debian Codegraph @

src/level/MarkMask.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
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 #include "MarkMask.h"
10
11 #include "Cube.h"
12 #include "Shape.h"
13 #include "Field.h"
14 #include "Rules.h"
15
16 #include <algorithm>
17
18 //-----------------------------------------------------------------
19 MarkMask::MarkMask(Cube *model, Field *field)
20 {
21 m_model = model;
22 m_field = field;
23 }
24 //-----------------------------------------------------------------
25 /**
26 * Return others which resist us to move this direction.
27 * Pointers to NULL and own model are not included.
28 *
29 * @return unique pointers, not NULL
30 */
31 Cube::t_models
32 MarkMask::getResist(Dir::eDir dir) const
33 {
34 V2 shift = Dir::dir2xy(dir);
35 V2 shift_loc = shift.plus(m_model->getLocation());
36
37 return getPlacedResist(shift_loc);
38 }
39 //-----------------------------------------------------------------
40 /**
41 * Return others which resist at given location.
42 * Pointers to NULL and own model are not included.
43 *
44 * @return unique pointers, not NULL
45 */
46 Cube::t_models
47 MarkMask::getPlacedResist(const V2 &loc) const
48 {
49 Cube::t_models models;
50 const Shape *shape = m_model->shape();
51 Shape::const_iterator end = shape->marksEnd();
52 for (Shape::const_iterator i = shape->marksBegin(); i != end; ++i) {
53 V2 mark = loc.plus(*i);
54
55 Cube *resist = m_field->getModel(mark);
56 if (NULL != resist && m_model != resist) {
57 models.push_back(resist);
58 }
59 }
60
61 unique(&models);
62 return models;
63 }
64 //-----------------------------------------------------------------
65 /**
66 * Write our position to the field.
67 */
68 void
69 MarkMask::mask()
70 {
71 writeModel(m_model, NULL);
72 }
73 //-----------------------------------------------------------------
74 /**
75 * Clear our position from the field.
76 */
77 void
78 MarkMask::unmask()
79 {
80 writeModel(NULL, m_model);
81 }
82 //-----------------------------------------------------------------
83 void
84 MarkMask::writeModel(Cube *model, Cube *toOverride)
85 {
86 V2 loc = m_model->getLocation();
87 const Shape *shape = m_model->shape();
88 Shape::const_iterator end = shape->marksEnd();
89 for (Shape::const_iterator i = shape->marksBegin(); i != end; ++i) {
90 V2 mark = loc.plus(*i);
91 m_field->setModel(mark, model, toOverride);
92 }
93 }
94
95 //-----------------------------------------------------------------
96 /**
97 * Returns dir to out of room.
98 * The direction must be without resist.
99 * @return return dir or DIR_NO when model is not at the border.
100 */
101 Dir::eDir
102 MarkMask::getBorderDir() const
103 {
104 V2 loc = m_model->getLocation();
105 const Shape *shape = m_model->shape();
106 Shape::const_iterator end = shape->marksEnd();
107 for (Shape::const_iterator i = shape->marksBegin(); i != end; ++i) {
108 V2 mark = loc.plus(*i);
109 if (mark.getX() == 0 && canGo(Dir::DIR_LEFT)) {
110 return Dir::DIR_LEFT;
111 }
112 else if (mark.getX() == m_field->getW() - 1 &&
113 canGo(Dir::DIR_RIGHT))
114 {
115 return Dir::DIR_RIGHT;
116 }
117 else if (mark.getY() == 0 && canGo(Dir::DIR_UP)) {
118 return Dir::DIR_UP;
119 }
120 else if (mark.getY() == m_field->getH() - 1 &&
121 canGo(Dir::DIR_DOWN))
122 {
123 return Dir::DIR_DOWN;
124 }
125 }
126
127 return Dir::DIR_NO;
128 }
129 //-----------------------------------------------------------------
130 /**
131 * Test whether there is only border
132 * or other unblocked objects that should go out
133 * in the given direction.
134 */
135 bool
136 MarkMask::canGo(Dir::eDir dir) const
137 {
138 return m_model->rules()->canMoveOthers(dir, Cube::FIXED);
139 }
140 //-----------------------------------------------------------------
141 /**
142 * Returns true when the object is fully out of the field.
143 */
144 bool
145 MarkMask::isFullyOut() const
146 {
147 V2 loc = m_model->getLocation();
148 const Shape *shape = m_model->shape();
149 Shape::const_iterator end = shape->marksEnd();
150 for (Shape::const_iterator i = shape->marksBegin(); i != end; ++i) {
151 V2 mark = loc.plus(*i);
152 Cube *place = m_field->getModel(mark);
153 if (place == NULL || !place->isBorder()) {
154 return false;
155 }
156 }
157 return true;
158 }
159 //-----------------------------------------------------------------
160 /**
161 * Removes duplicities from the given models.
162 */
163 void
164 MarkMask::unique(Cube::t_models *models)
165 {
166 std::sort(models->begin(), models->end());
167 Cube::t_models::iterator last = std::unique(models->begin(), models->end());
168 models->erase(last, models->end());
169 }