int simplehero::reachOthers(GraphMap* map, int x, int y) { int a, b; for(int i = 0; i < map->getNumActors(); i++) { if(map->getActorType(i) & ACTOR_EATABLE) { if(map->getActorType(i) & ACTOR_DEAD) { continue; } map->getActorPosition(i, a, b); if(x == a && y == b) { continue; } int v = map->getVertex(a, b); int s = BFSearch(map, x, y, v); if(s == -1) { return -1; } } } return 0; }
int simplehero::selectNeighbor(GraphMap* map, int cur_x, int cur_y) { // printf("Selecting neighbor\n"); int x, y, a, b; Pos* goal = findGoal(map, cur_x, cur_y); int g; if(goal == 0) { printf("Couldn't find goal\n"); return 0; } g = map->getVertex(goal->getX(), goal->getY()); int toGo = BFSearch(map, cur_x, cur_y, g); //printf("Done searching\n"); if(toGo == -1) { // printf("No target found\n"); delete goal; return 0; } /* if(p == 0 || p[1]->getX() < 0) { printf("ERRRORRORROR\n"); goal = findGoal(map, cur_x, cur_y); toGo = BFSearch(map ,cur_x, cur_y, goal); }*/ map->getPosition(toGo, x, y); for(int i = 0; i < map->getNumNeighbors(cur_x, cur_y); i++) { map->getNeighbor(cur_x, cur_y, i, a, b); if(x == a && y == b) { delete goal; return i; } } printf("Shouldn't get here"); return 0; }
bool HelloWorld::checkNeedEliminate() { if (_elements[_touchElements[0]]->getName() == _elements[_touchElements[1]]->getName()) { std::vector<int> path; if (BFSearch(_touchElements[0], _touchElements[1], path, 0)){ for (unsigned int i = 0; i < path.size() - 1; ++i) { int rowi = path[i] / MAX_CAPACITY_NUM_IN_LINE; int coli = path[i] - (rowi * MAX_CAPACITY_NUM_IN_LINE); int rowi1 = path[i + 1] / MAX_CAPACITY_NUM_IN_LINE; int coli1 = path[i + 1] - (rowi1 * MAX_CAPACITY_NUM_IN_LINE); Vec3 posi = Vec3((-(MAX_CAPACITY_NUM_IN_LINE / 2 - _elementSize.width / 2.0f) + rowi) * _elementSize.width, 0.0f, (-(MAX_CAPACITY_NUM_IN_LINE / 2 - _elementSize.height / 2.0f) + coli) * _elementSize.height); Vec3 posi1 = Vec3((-(MAX_CAPACITY_NUM_IN_LINE / 2 - _elementSize.width / 2.0f) + rowi1) * _elementSize.width, 0.0f, (-(MAX_CAPACITY_NUM_IN_LINE / 2 - _elementSize.height / 2.0f) + coli1) * _elementSize.height); _drawNode->drawLine(posi, posi1, Color4F(0.0f, 1.0f, 0.0f, 1.0f)); } return true; } } return false; }
Pos* simplehero::findGoal(GraphMap* map, int x, int y) { int a, b; Pos* goal; int goalX, goalY; int nX, nY; int goBack; for(int j = 0; j < map->getNumNeighbors(x, y); j++) { map->getNeighbor(x, y, j, nX, nY); for(int i = 0; i < map->getNumActors(); i++) { if(map->getActorType(i) & ACTOR_EATABLE) { if(map->getActorType(i) & ACTOR_DEAD) { continue; } map->getActorPosition(i, goalX, goalY); if(goalX == nX && goalY == nY) { goal = new Pos(goalX, goalY); return goal; } } } } for(int i = 0; i < map->getNumActors(); i++) { if(map->getActorType(i) & ACTOR_EATABLE) { if(map->getActorType(i) & ACTOR_DEAD) { continue; } map->getActorPosition(i, a, b); int v = map->getVertex(a,b); if(!BFSearch(map, x, y, v)) { continue; } goal = new Pos(a, b); return goal; } } for(int i = 0; i < map->getNumActors(); i++) { if(map->getActorType(i) & 4) { if(map->getActorType(i) & ACTOR_DEAD) { continue; } map->getActorPosition(i, goalX, goalY); goBack = reachOthers(map, goalX, goalY); if(goBack == -1) { continue; } goal = new Pos(goalX, goalY); return goal; } } for(int i = 0; i <= map->getNumActors(); i++) { if(i == map->getNumActors()) { return 0; } if(map->getActorType(i) & 4) { if(map->getActorType(i) & 16) { continue; } map->getActorPosition(i, goalX, goalY); if(goalX == -1 && goalY == -1) { continue; } break; } } goal = new Pos(goalX, goalY); return goal; }
bool HelloWorld::BFSearch( int start, int end, std::vector<int> &path, int searchCount) { if (searchCount > 2) return false; bool searchState = false; do { path.push_back(start); int row = start / MAX_CAPACITY_NUM_IN_LINE; //int col = start - (row * MAX_CAPACITY_NUM_IN_LINE); //left row for (int i = start - 1; i >= (row * MAX_CAPACITY_NUM_IN_LINE); --i) { if (!canReached(i)){ if (i == end) { path.push_back(i); searchState = true; } break; } if (BFSearch(i, end, path, searchCount + 1)){ searchState = true; break; } } if (searchState) break; //right row for (int i = start + 1; i < ((row + 1) * MAX_CAPACITY_NUM_IN_LINE); ++i) { if (!canReached(i)){ if (i == end) { path.push_back(i); searchState = true; } break; } if (BFSearch(i, end, path, searchCount + 1)){ searchState = true; break; } } if (searchState) break; //top col for (int i = start - MAX_CAPACITY_NUM_IN_LINE; i >= 0; i -= MAX_CAPACITY_NUM_IN_LINE) { if (!canReached(i)){ if (i == end) { path.push_back(i); searchState = true; } break; } if (BFSearch(i, end, path, searchCount + 1)){ searchState = true; break; } } if (searchState) break; //button row for (int i = start + MAX_CAPACITY_NUM_IN_LINE; i < MAX_CAPACITY_NUM; i += MAX_CAPACITY_NUM_IN_LINE) { if (!canReached(i)){ if (i == end) { path.push_back(i); searchState = true; } break; } if (BFSearch(i, end, path, searchCount + 1)){ searchState = true; break; } } if (!searchState) path.pop_back(); } while (false); return searchState; }