| 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 "LevelNode.h" |
| 10 |
|
| 11 |
#include "Level.h" |
| 12 |
#include "NodeDrawer.h" |
| 13 |
|
| 14 |
#include <math.h> |
| 15 |
#include <assert.h> |
| 16 |
|
| 17 |
//----------------------------------------------------------------- |
| 18 |
/** |
| 19 |
* Default state is STATE_FAR. |
| 20 |
*/ |
| 21 |
LevelNode::LevelNode(const std::string &codename, const Path &datafile, |
| 22 |
const V2 &loc, const std::string &poster) |
| 23 |
: m_codename(codename), m_poster(poster), m_datafile(datafile), m_loc(loc) |
| 24 |
{ |
| 25 |
m_state = STATE_FAR; |
| 26 |
m_depth = 1; |
| 27 |
m_bestMoves = -1; |
| 28 |
} |
| 29 |
//----------------------------------------------------------------- |
| 30 |
/** |
| 31 |
* Free self and all children. |
| 32 |
*/ |
| 33 |
LevelNode::~LevelNode() |
| 34 |
{ |
| 35 |
t_children::iterator end = m_children.end(); |
| 36 |
for (t_children::iterator i = m_children.begin(); i != end; ++i) { |
| 37 |
delete *i; |
| 38 |
} |
| 39 |
} |
| 40 |
//----------------------------------------------------------------- |
| 41 |
/** |
| 42 |
* Set state for this node and open his followers. |
| 43 |
*/ |
| 44 |
void |
| 45 |
LevelNode::setState(eState state) |
| 46 |
{ |
| 47 |
t_children::iterator end = m_children.end(); |
| 48 |
switch (state) { |
| 49 |
case STATE_HIDDEN: |
| 50 |
case STATE_FAR: |
| 51 |
case STATE_OPEN: |
| 52 |
break; |
| 53 |
case STATE_SOLVED: |
| 54 |
for (t_children::iterator i = m_children.begin(); |
| 55 |
i != end; ++i) |
| 56 |
{ |
| 57 |
if ((*i)->getState() < STATE_OPEN) { |
| 58 |
(*i)->setState(STATE_OPEN); |
| 59 |
} |
| 60 |
} |
| 61 |
break; |
| 62 |
default: |
| 63 |
assert(!"unknown level node state"); |
| 64 |
break; |
| 65 |
} |
| 66 |
m_state = state; |
| 67 |
} |
| 68 |
//----------------------------------------------------------------- |
| 69 |
void |
| 70 |
LevelNode::bestSolution(int moves, const std::string &author) |
| 71 |
{ |
| 72 |
m_bestMoves = moves; |
| 73 |
m_bestAuthor = author; |
| 74 |
} |
| 75 |
//----------------------------------------------------------------- |
| 76 |
/** |
| 77 |
* Returns true when cursor is in radius around node. |
| 78 |
*/ |
| 79 |
bool |
| 80 |
LevelNode::isUnder(const V2 &cursor) const |
| 81 |
{ |
| 82 |
double dx = m_loc.getX() - cursor.getX(); |
| 83 |
double dy = m_loc.getY() - cursor.getY(); |
| 84 |
|
| 85 |
return (sqrt(dx * dx + dy * dy) < DOT_RADIUS); |
| 86 |
} |
| 87 |
//----------------------------------------------------------------- |
| 88 |
/** |
| 89 |
* Find selected node under cursor. |
| 90 |
* Only solved and open nodes are traversed. |
| 91 |
* @return selected node or NULL |
| 92 |
*/ |
| 93 |
LevelNode * |
| 94 |
LevelNode::findSelected(const V2 &cursor) |
| 95 |
{ |
| 96 |
if (m_state >= STATE_OPEN) { |
| 97 |
if (isUnder(cursor)) { |
| 98 |
return this; |
| 99 |
} |
| 100 |
else { |
| 101 |
t_children::const_iterator end = m_children.end(); |
| 102 |
for (t_children::const_iterator i = m_children.begin(); |
| 103 |
i != end; ++i) |
| 104 |
{ |
| 105 |
LevelNode *selected = (*i)->findSelected(cursor); |
| 106 |
if (selected) { |
| 107 |
return selected; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
//----------------------------------------------------------------- |
| 115 |
/** |
| 116 |
* Find next OPEN node. |
| 117 |
* @param current current selected node (could be NULL) |
| 118 |
* @return next OPEN node or NULL |
| 119 |
*/ |
| 120 |
LevelNode * |
| 121 |
LevelNode::findNextOpen(const LevelNode *current) |
| 122 |
{ |
| 123 |
bool beforeCurrent = true; |
| 124 |
|
| 125 |
t_children opened = findOpenNodes(); |
| 126 |
t_children::const_iterator end = opened.end(); |
| 127 |
for (t_children::const_iterator i = opened.begin(); i != end; ++i) { |
| 128 |
if (*i == current) { |
| 129 |
beforeCurrent = false; |
| 130 |
} |
| 131 |
else if (!beforeCurrent) { |
| 132 |
return *i; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if (opened.size() > 0) { |
| 137 |
return opened[0]; |
| 138 |
} |
| 139 |
else { |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
} |
| 143 |
//----------------------------------------------------------------- |
| 144 |
/** |
| 145 |
* Find all OPEN child nodes. |
| 146 |
* @return vector of shared pointers |
| 147 |
*/ |
| 148 |
LevelNode::t_children |
| 149 |
LevelNode::findOpenNodes() |
| 150 |
{ |
| 151 |
t_children opened; |
| 152 |
if (m_state >= STATE_OPEN) { |
| 153 |
if (m_state == STATE_OPEN) { |
| 154 |
opened.push_back(this); |
| 155 |
} |
| 156 |
|
| 157 |
t_children::const_iterator end = m_children.end(); |
| 158 |
for (t_children::const_iterator i = m_children.begin(); i != end; ++i) { |
| 159 |
t_children nodes = (*i)->findOpenNodes(); |
| 160 |
opened.insert(opened.end(), nodes.begin(), nodes.end()); |
| 161 |
} |
| 162 |
} |
| 163 |
return opened; |
| 164 |
} |
| 165 |
//----------------------------------------------------------------- |
| 166 |
/** |
| 167 |
* Find named node in whole tree. |
| 168 |
* @return named node or NULL |
| 169 |
*/ |
| 170 |
LevelNode * |
| 171 |
LevelNode::findNamed(const std::string &codename) |
| 172 |
{ |
| 173 |
if (m_codename == codename) { |
| 174 |
return this; |
| 175 |
} |
| 176 |
else { |
| 177 |
t_children::const_iterator end = m_children.end(); |
| 178 |
for (t_children::const_iterator i = m_children.begin(); |
| 179 |
i != end; ++i) |
| 180 |
{ |
| 181 |
LevelNode *named = (*i)->findNamed(codename); |
| 182 |
if (named) { |
| 183 |
return named; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
//----------------------------------------------------------------- |
| 190 |
/** |
| 191 |
* Returns true when all child nodes are solved. |
| 192 |
*/ |
| 193 |
bool |
| 194 |
LevelNode::areAllSolved() const |
| 195 |
{ |
| 196 |
if (m_state != STATE_SOLVED) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
t_children::const_iterator end = m_children.end(); |
| 200 |
for (t_children::const_iterator i = m_children.begin(); i != end; ++i) { |
| 201 |
if (!(*i)->areAllSolved()) { |
| 202 |
return false; |
| 203 |
} |
| 204 |
} |
| 205 |
return true; |
| 206 |
} |
| 207 |
//----------------------------------------------------------------- |
| 208 |
Level * |
| 209 |
LevelNode::createLevel() const |
| 210 |
{ |
| 211 |
return new Level(m_codename, m_datafile, m_depth); |
| 212 |
} |
| 213 |
//----------------------------------------------------------------- |
| 214 |
/** |
| 215 |
* Add child node. |
| 216 |
* NOTE: cycles in graph are not supported. |
| 217 |
*/ |
| 218 |
void |
| 219 |
LevelNode::addChild(LevelNode *new_node) |
| 220 |
{ |
| 221 |
m_children.push_back(new_node); |
| 222 |
|
| 223 |
new_node->setDepth(m_depth + 1); |
| 224 |
if (m_state == STATE_SOLVED && new_node->getState() < STATE_OPEN) { |
| 225 |
new_node->setState(STATE_OPEN); |
| 226 |
} |
| 227 |
} |
| 228 |
//----------------------------------------------------------------- |
| 229 |
/** |
| 230 |
* Draws self and path to all children. |
| 231 |
* Children are drawed recursive. |
| 232 |
*/ |
| 233 |
void |
| 234 |
LevelNode::drawPath(const NodeDrawer *drawer) const |
| 235 |
{ |
| 236 |
if (m_state > STATE_HIDDEN) { |
| 237 |
t_children::const_iterator end = m_children.end(); |
| 238 |
for (t_children::const_iterator i = m_children.begin(); |
| 239 |
i != end; ++i) |
| 240 |
{ |
| 241 |
if ((*i)->getState() > STATE_HIDDEN) { |
| 242 |
drawer->drawEdge(this, *i); |
| 243 |
(*i)->drawPath(drawer); |
| 244 |
} |
| 245 |
} |
| 246 |
drawer->drawNode(this); |
| 247 |
} |
| 248 |
} |
| 249 |
|