Posicion* Matriz::adyacenteCercana(Entidad* destino, Entidad* origen) { list<Posicion> adyacentes = this->adyacentesVacias(destino); Posicion pos; if (adyacentes.empty()) return nullptr; pos = adyacentes.front(); adyacentes.pop_front(); int dist = this->distanciaEntre(pos, origen); while (!adyacentes.empty()) { Posicion aux = adyacentes.front(); adyacentes.pop_front(); int distTmp = this->distanciaEntre(aux, origen); if (distTmp < dist) { pos = aux; dist = distTmp; } } return new Posicion(pos.getX(), pos.getY()); }
bool Matriz::ubicarEntidad(Entidad* elemento, Posicion* pos){ // TODO: levantar error o warning cuando pase esto if (!elemento) { return false; // ErrorLog::getInstance()->escribirLog("Se quiso agregar un elemento vacio al mapa.", LOG_WARNING); } if (!posicionPertenece(pos)) { // ErrorLog::getInstance()->escribirLog("Error al querer asignar posicion a [" + elemento->verNombre() + "]. Posicion " + pos->toStrRound() + " inexistente.", LOG_WARNING); return false; } // Obtengo el tamanio de la entidad int altura_x = elemento->verTamX(); int ancho_y = elemento->verTamY(); // Primero chequeo que las posiciones esten libres, y que caigan dentro del mapa for(int i = 0; i < altura_x; i++) for(int j = 0; j < ancho_y; j++) { Posicion act = Posicion(pos->getX() + i, pos->getRoundY() + j); if ((!posicionPertenece(&act)) || (this->filas == act.getX()) || (this->columnas == act.getY())) { // ErrorLog::getInstance()->escribirLog("No se puede agregar entidad [" + elemento->verNombre() +"] en " + pos->toStrRound() + ". Posición no pertenece al mapa.", LOG_WARNING); return false; } if(casillas[pos->getRoundX() + i][pos->getRoundY() + j] != nullptr) { Posicion tmp = Posicion(pos->getRoundX() + i,pos->getRoundY() + j); // ErrorLog::getInstance()->escribirLog("No se puede agregar entidad [" + elemento->verNombre() +"] en " + pos->toStrRound() + ". Colisiona con otra entidad en" + tmp.toStrRound() +".", LOG_WARNING); return false; } } // Si llego acá es porque todas las posiciones están libres for(int i = 0; i < altura_x; i++) for(int j = 0; j < ancho_y; j++){ casillas[pos->getRoundX() + i][pos->getRoundY() + j] = elemento; mapDeOcupaciones[pos->getRoundX() + i][pos->getRoundY() + j]= 1; } this->cantidad_entidades++; return true; }
void GameControllerServer::generarRecursoRandom(SDL_mutex *mutex) { for (int i = 0; i <= MAX_RECURSOS; i++) { Posicion pos = this->modelo->mapa->posicionVacia(); recurso_t tipo = this->modelo->generarRecursoRandom(pos); //creacion mensaje si creo recurso if (tipo.cantidad > 0) { msg_t mensaje; mensaje.type = CREAR_RECURSO; memcpy(mensaje.paramNombre, string_to_char_array(tipo.nombre), sizeof(mensaje.paramNombre)); mensaje.paramInt1 = tipo.cantidad; mensaje.paramDouble1 = pos.getX(); mensaje.paramDouble2 = pos.getY(); this->agregarMensaje(mensaje, mutex); mensaje.type = SET_ID_RECURSO; mensaje.paramInt1 = this->modelo->mapa->entidad_celda( pos.get_x_exacta(), pos.get_y_exacta())->getId(); this->agregarMensaje(mensaje, mutex); } } }