balloon_formation: posat ORDEN en el CAOS de tipos, structs i noms de variables que aci ningú sabia ja qui feia que. De paso llevades coses que sobraven i fetes les coses com toca. Este codi era del CC encara

balloon_formation: els pools de formacions es carreguen ara desde fitxer i ja no hi ha ni llimit de pools ni llimit de formacions per pool
falta: revisar les formacions i els pools que algo no quadra
This commit is contained in:
2025-07-24 15:58:04 +02:00
parent 2932664b9f
commit 1233b27eb6
10 changed files with 542 additions and 483 deletions

View File

@@ -19,43 +19,38 @@ void BalloonFormations::initFormations() {
// Calcular posiciones base
const int DEFAULT_POS_Y = param.game.play_area.rect.h - BALLOON_SPAWN_HEIGHT;
const int X4_0 = param.game.play_area.rect.x;
const int X4_100 = param.game.play_area.rect.w - Balloon::SIZE.at(3);
const int X3_0 = param.game.play_area.rect.x;
const int X3_100 = param.game.play_area.rect.w - Balloon::SIZE.at(2);
const int X2_0 = param.game.play_area.rect.x;
const int X2_100 = param.game.play_area.rect.w - Balloon::SIZE.at(1);
const int X1_0 = param.game.play_area.rect.x;
const int X1_50 = param.game.play_area.center_x - (Balloon::SIZE.at(0) / 2);
const int X1_100 = param.game.play_area.rect.w - Balloon::SIZE.at(0);
const int X3_25 = param.game.play_area.first_quarter_x - (Balloon::WIDTH.at(3) / 2);
const int X3_75 = param.game.play_area.third_quarter_x - (Balloon::WIDTH.at(3) / 2);
const int X3_100 = param.game.play_area.rect.w - Balloon::WIDTH.at(3);
// Variables calculadas para posiciones especiales
const int QUARTER1_X4 = param.game.play_area.first_quarter_x - (Balloon::SIZE.at(3) / 2);
const int QUARTER3_X4 = param.game.play_area.third_quarter_x - (Balloon::SIZE.at(3) / 2);
const int X2_0 = param.game.play_area.rect.x;
const int X2_100 = param.game.play_area.rect.w - Balloon::WIDTH.at(2);
const int X1_0 = param.game.play_area.rect.x;
const int X1_100 = param.game.play_area.rect.w - Balloon::WIDTH.at(1);
const int X0_0 = param.game.play_area.rect.x;
const int X0_50 = param.game.play_area.center_x - (Balloon::WIDTH.at(0) / 2);
const int X0_100 = param.game.play_area.rect.w - Balloon::WIDTH.at(0);
// Mapa de variables para reemplazar en el archivo
std::map<std::string, float> variables = {
{"X1_0", X1_0},
{"X1_50", X1_50},
{"X1_100", X1_100},
{"X2_0", X2_0},
{"X2_100", X2_100},
{"X3_0", X3_0},
{"X3_100", X3_100},
{"X4_0", X4_0},
{"X4_100", X4_100},
{"QUARTER1_X4", QUARTER1_X4},
{"QUARTER3_X4", QUARTER3_X4},
{"X1_0", X0_0},
{"X1_50", X0_50},
{"X1_100", X0_100},
{"X2_0", X1_0},
{"X2_100", X1_100},
{"X3_0", X2_0},
{"X3_100", X2_100},
{"X4_0", X3_0},
{"X4_100", X3_100},
{"X3_25", X3_25},
{"X3_75", X3_75},
{"DEFAULT_POS_Y", DEFAULT_POS_Y},
{"BALLOON_SIZE_0", Balloon::SIZE.at(0)},
{"BALLOON_SIZE_1", Balloon::SIZE.at(1)},
{"BALLOON_SIZE_2", Balloon::SIZE.at(2)},
{"BALLOON_SIZE_3", Balloon::SIZE.at(3)},
{"RIGHT", Balloon::VELX_POSITIVE},
{"LEFT", Balloon::VELX_NEGATIVE}};
balloon_formation_.reserve(NUMBER_OF_BALLOON_FORMATIONS);
if (!loadFormationsFromFile(Asset::get()->get("balloon_formations.txt"), variables)) {
// Fallback: cargar formaciones por defecto si falla la carga del archivo
loadDefaultFormations();
@@ -70,7 +65,7 @@ auto BalloonFormations::loadFormationsFromFile(const std::string& filename, cons
std::string line;
int current_formation = -1;
std::vector<BalloonFormationParams> current_params;
std::vector<SpawnParams> current_params;
while (std::getline(file, line)) {
// Eliminar espacios en blanco al inicio y final
@@ -85,7 +80,7 @@ auto BalloonFormations::loadFormationsFromFile(const std::string& filename, cons
if (line.substr(0, 10) == "formation:") {
// Guardar formación anterior si existe
if (current_formation >= 0 && !current_params.empty()) {
balloon_formation_.emplace_back(current_params.size(), current_params);
formations_.emplace_back(current_params);
}
// Iniciar nueva formación
@@ -105,7 +100,7 @@ auto BalloonFormations::loadFormationsFromFile(const std::string& filename, cons
// Guardar última formación
if (current_formation >= 0 && !current_params.empty()) {
balloon_formation_.emplace_back(current_params.size(), current_params);
formations_.emplace_back(current_params);
}
// Crear variantes flotantes (formaciones 50-99)
@@ -120,7 +115,7 @@ auto BalloonFormations::loadFormationsFromFile(const std::string& filename, cons
return true;
}
auto BalloonFormations::parseBalloonLine(const std::string& line, const std::map<std::string, float>& variables) -> std::optional<BalloonFormationParams> {
auto BalloonFormations::parseBalloonLine(const std::string& line, const std::map<std::string, float>& variables) -> std::optional<SpawnParams> {
std::istringstream iss(line);
std::string token;
std::vector<std::string> tokens;
@@ -136,7 +131,7 @@ auto BalloonFormations::parseBalloonLine(const std::string& line, const std::map
try {
int x = evaluateExpression(tokens.at(0), variables);
int desp = evaluateExpression(tokens.at(1), variables);
int offset = evaluateExpression(tokens.at(1), variables);
int y = evaluateExpression(tokens.at(2), variables);
float vel_x = evaluateExpression(tokens.at(3), variables);
@@ -145,23 +140,23 @@ auto BalloonFormations::parseBalloonLine(const std::string& line, const std::map
Balloon::Size size;
if (tokens.at(5) == "SMALL") {
size = Balloon::Size::SMALL;
desp = desp * (Balloon::SIZE.at(0) + 1);
offset = offset * (Balloon::WIDTH.at(0) + 1);
} else if (tokens.at(5) == "MEDIUM") {
size = Balloon::Size::MEDIUM;
desp = desp * (Balloon::SIZE.at(1) + 1);
offset = offset * (Balloon::WIDTH.at(1) + 1);
} else if (tokens.at(5) == "LARGE") {
size = Balloon::Size::LARGE;
desp = desp * (Balloon::SIZE.at(2) + 1);
offset = offset * (Balloon::WIDTH.at(2) + 1);
} else if (tokens.at(5) == "EXTRALARGE") {
size = Balloon::Size::EXTRALARGE;
desp = desp * (Balloon::SIZE.at(3) + 1);
offset = offset * (Balloon::WIDTH.at(3) + 1);
} else {
return std::nullopt;
}
int creation_time = CREATION_TIME + evaluateExpression(tokens.at(6), variables);
int creation_time = DEFAULT_CREATION_TIME + evaluateExpression(tokens.at(6), variables);
return BalloonFormationParams(x + desp, y, vel_x, type, size, creation_time);
return SpawnParams(x + offset, y, vel_x, type, size, creation_time);
} catch (const std::exception&) {
return std::nullopt;
}
@@ -222,32 +217,30 @@ auto BalloonFormations::trim(const std::string& str) -> std::string {
}
void BalloonFormations::createFloaterVariants() {
balloon_formation_.resize(100);
formations_.resize(100);
// Crear variantes flotantes de las primeras 50 formaciones
for (size_t k = 0; k < 50 && k < balloon_formation_.size(); k++) {
std::vector<BalloonFormationParams> floater_params;
floater_params.reserve(balloon_formation_.at(k).number_of_balloons);
for (size_t k = 0; k < 50 && k < formations_.size(); k++) {
std::vector<SpawnParams> floater_params;
floater_params.reserve(formations_.at(k).balloons.size());
for (int i = 0; i < balloon_formation_.at(k).number_of_balloons; i++) {
const auto& original = balloon_formation_.at(k).init.at(i);
floater_params.emplace_back(
original.x, original.y, original.vel_x, Balloon::Type::FLOATER, original.size, original.creation_counter);
for (size_t i = 0; i < formations_.at(k).balloons.size(); i++) {
const auto& original = formations_.at(k).balloons.at(i);
floater_params.emplace_back(original.x, original.y, original.vel_x, Balloon::Type::FLOATER, original.size, original.creation_counter);
}
balloon_formation_.at(k + 50) = BalloonFormationUnit(
balloon_formation_.at(k).number_of_balloons, floater_params);
formations_.at(k + 50) = Formation(floater_params);
}
}
#ifdef _DEBUG
void BalloonFormations::addTestFormation() {
std::vector<BalloonFormationParams> test_params = {
std::vector<SpawnParams> test_params = {
{10, -BLOCK, 0, Balloon::Type::FLOATER, Balloon::Size::SMALL, 200},
{50, -BLOCK, 0, Balloon::Type::FLOATER, Balloon::Size::MEDIUM, 200},
{90, -BLOCK, 0, Balloon::Type::FLOATER, Balloon::Size::LARGE, 200},
{140, -BLOCK, 0, Balloon::Type::FLOATER, Balloon::Size::EXTRALARGE, 200}};
balloon_formation_.at(99) = BalloonFormationUnit(4, test_params);
formations_.at(99) = Formation(test_params);
}
#endif
void BalloonFormations::loadDefaultFormations() {
@@ -256,147 +249,175 @@ void BalloonFormations::loadDefaultFormations() {
const int DEFAULT_POS_Y = param.game.play_area.rect.h - BALLOON_SPAWN_HEIGHT;
const int X4_0 = param.game.play_area.rect.x;
const int X4_100 = param.game.play_area.rect.w - Balloon::SIZE.at(3);
const int X4_100 = param.game.play_area.rect.w - Balloon::WIDTH.at(3);
// Formación básica #00
std::vector<BalloonFormationParams> basic_formation = {
BalloonFormationParams(X4_0, DEFAULT_POS_Y, Balloon::VELX_POSITIVE, Balloon::Type::BALLOON, Balloon::Size::EXTRALARGE, CREATION_TIME),
BalloonFormationParams(X4_100, DEFAULT_POS_Y, Balloon::VELX_NEGATIVE, Balloon::Type::BALLOON, Balloon::Size::EXTRALARGE, CREATION_TIME)};
balloon_formation_.emplace_back(2, basic_formation);
std::vector<SpawnParams> basic_formation = {
SpawnParams(X4_0, DEFAULT_POS_Y, Balloon::VELX_POSITIVE, Balloon::Type::BALLOON, Balloon::Size::EXTRALARGE, DEFAULT_CREATION_TIME),
SpawnParams(X4_100, DEFAULT_POS_Y, Balloon::VELX_NEGATIVE, Balloon::Type::BALLOON, Balloon::Size::EXTRALARGE, DEFAULT_CREATION_TIME)};
formations_.emplace_back(basic_formation);
}
// Inicializa los conjuntos de formaciones
// Nuevas implementaciones para el sistema de pools flexible
void BalloonFormations::initFormationPools() {
// Reserva espacio para cada pool de formaciones
balloon_formation_pool_.resize(NUMBER_OF_SETS_PER_POOL);
// Set #0
balloon_formation_pool_.at(0) = {
&balloon_formation_.at(0),
&balloon_formation_.at(1),
&balloon_formation_.at(2),
&balloon_formation_.at(3),
&balloon_formation_.at(4),
&balloon_formation_.at(5),
&balloon_formation_.at(6),
&balloon_formation_.at(7),
&balloon_formation_.at(8),
&balloon_formation_.at(9)};
// Set #1
balloon_formation_pool_.at(1) = {
&balloon_formation_.at(10),
&balloon_formation_.at(11),
&balloon_formation_.at(12),
&balloon_formation_.at(13),
&balloon_formation_.at(14),
&balloon_formation_.at(15),
&balloon_formation_.at(16),
&balloon_formation_.at(17),
&balloon_formation_.at(18),
&balloon_formation_.at(19)};
// Set #2
balloon_formation_pool_.at(2) = {
&balloon_formation_.at(0),
&balloon_formation_.at(1),
&balloon_formation_.at(2),
&balloon_formation_.at(3),
&balloon_formation_.at(4),
&balloon_formation_.at(55),
&balloon_formation_.at(56),
&balloon_formation_.at(57),
&balloon_formation_.at(58),
&balloon_formation_.at(59)};
// Set #3
balloon_formation_pool_.at(3) = {
&balloon_formation_.at(50),
&balloon_formation_.at(51),
&balloon_formation_.at(52),
&balloon_formation_.at(53),
&balloon_formation_.at(54),
&balloon_formation_.at(5),
&balloon_formation_.at(6),
&balloon_formation_.at(7),
&balloon_formation_.at(8),
&balloon_formation_.at(9)};
// Set #4
balloon_formation_pool_.at(4) = {
&balloon_formation_.at(60),
&balloon_formation_.at(61),
&balloon_formation_.at(62),
&balloon_formation_.at(63),
&balloon_formation_.at(64),
&balloon_formation_.at(65),
&balloon_formation_.at(66),
&balloon_formation_.at(67),
&balloon_formation_.at(68),
&balloon_formation_.at(69)};
// Set #5
balloon_formation_pool_.at(5) = {
&balloon_formation_.at(10),
&balloon_formation_.at(61),
&balloon_formation_.at(12),
&balloon_formation_.at(63),
&balloon_formation_.at(14),
&balloon_formation_.at(65),
&balloon_formation_.at(16),
&balloon_formation_.at(67),
&balloon_formation_.at(18),
&balloon_formation_.at(69)};
// Set #6
balloon_formation_pool_.at(6) = {
&balloon_formation_.at(60),
&balloon_formation_.at(11),
&balloon_formation_.at(62),
&balloon_formation_.at(13),
&balloon_formation_.at(64),
&balloon_formation_.at(15),
&balloon_formation_.at(66),
&balloon_formation_.at(17),
&balloon_formation_.at(68),
&balloon_formation_.at(19)};
// Set #7
balloon_formation_pool_.at(7) = {
&balloon_formation_.at(20),
&balloon_formation_.at(21),
&balloon_formation_.at(22),
&balloon_formation_.at(23),
&balloon_formation_.at(24),
&balloon_formation_.at(65),
&balloon_formation_.at(66),
&balloon_formation_.at(67),
&balloon_formation_.at(68),
&balloon_formation_.at(69)};
// Set #8
balloon_formation_pool_.at(8) = {
&balloon_formation_.at(70),
&balloon_formation_.at(71),
&balloon_formation_.at(72),
&balloon_formation_.at(73),
&balloon_formation_.at(74),
&balloon_formation_.at(15),
&balloon_formation_.at(16),
&balloon_formation_.at(17),
&balloon_formation_.at(18),
&balloon_formation_.at(19)};
// Set #9
balloon_formation_pool_.at(9) = {
&balloon_formation_.at(20),
&balloon_formation_.at(21),
&balloon_formation_.at(22),
&balloon_formation_.at(23),
&balloon_formation_.at(24),
&balloon_formation_.at(70),
&balloon_formation_.at(71),
&balloon_formation_.at(72),
&balloon_formation_.at(73),
&balloon_formation_.at(74)};
// Intentar cargar pools desde archivo
if (!loadPoolsFromFile(Asset::get()->get("balloon_pools.txt"))) {
// Fallback: cargar pools por defecto si falla la carga del archivo
loadDefaultPools();
}
}
auto BalloonFormations::loadPoolsFromFile(const std::string& filename) -> bool {
std::ifstream file(filename);
if (!file.is_open()) {
return false;
}
std::string line;
pools_.clear(); // Limpiar pools existentes
// Map temporal para ordenar los pools por ID
std::map<int, std::vector<int>> temp_pools;
while (std::getline(file, line)) {
// Eliminar espacios en blanco al inicio y final
line = trim(line);
// Saltar líneas vacías y comentarios
if (line.empty() || line.at(0) == '#') {
continue;
}
// Procesar línea de pool
auto pool_data = parsePoolLine(line);
if (pool_data.has_value()) {
temp_pools[pool_data->first] = pool_data->second;
}
}
file.close();
// Convertir el map ordenado a vector
// Redimensionar el vector para el pool con ID más alto
if (!temp_pools.empty()) {
int max_pool_id = temp_pools.rbegin()->first;
pools_.resize(max_pool_id + 1);
for (const auto& [pool_id, formations] : temp_pools) {
pools_[pool_id] = formations;
}
}
return !pools_.empty();
}
auto BalloonFormations::parsePoolLine(const std::string& line) -> std::optional<std::pair<int, std::vector<int>>> {
// Formato esperado: "POOL: 0 FORMATIONS: 1, 2, 14, 3, 5, 5"
// Buscar "POOL:"
size_t pool_pos = line.find("POOL:");
if (pool_pos == std::string::npos) {
return std::nullopt;
}
// Buscar "FORMATIONS:"
size_t formations_pos = line.find("FORMATIONS:");
if (formations_pos == std::string::npos) {
return std::nullopt;
}
try {
// Extraer el ID del pool
std::string pool_id_str = trim(line.substr(pool_pos + 5, formations_pos - pool_pos - 5));
int pool_id = std::stoi(pool_id_str);
// Extraer la lista de formaciones
std::string formations_str = trim(line.substr(formations_pos + 11));
std::vector<int> formation_ids;
// Parsear la lista de formaciones separadas por comas
std::istringstream iss(formations_str);
std::string token;
while (std::getline(iss, token, ',')) {
token = trim(token);
if (!token.empty()) {
int formation_id = std::stoi(token);
// Validar que el ID de formación existe
if (formation_id >= 0 && formation_id < static_cast<int>(formations_.size())) {
formation_ids.push_back(formation_id);
}
}
}
if (!formation_ids.empty()) {
return std::make_pair(pool_id, formation_ids);
}
} catch (const std::exception&) {
// Error de conversión o parsing
return std::nullopt;
}
return std::nullopt;
}
void BalloonFormations::loadDefaultPools() {
// Pools por defecto como fallback
pools_.clear();
// Crear algunos pools básicos si tenemos formaciones disponibles
if (formations_.empty()) {
return;
}
size_t total_formations = formations_.size();
// Pool 0: Primeras 10 formaciones (o las que haya disponibles)
Pool pool0;
for (size_t i = 0; i < std::min(size_t(10), total_formations); ++i) {
pool0.push_back(static_cast<int>(i));
}
if (!pool0.empty()) {
pools_.push_back(pool0);
}
// Pool 1: Formaciones 10-19 (si existen)
if (total_formations > 10) {
Pool pool1;
for (size_t i = 10; i < std::min(size_t(20), total_formations); ++i) {
pool1.push_back(static_cast<int>(i));
}
if (!pool1.empty()) {
pools_.push_back(pool1);
}
}
// Pool 2: Mix de formaciones normales y floaters (50+)
if (total_formations > 50) {
Pool pool2;
// Agregar algunas formaciones básicas
for (size_t i = 0; i < std::min(size_t(5), total_formations); ++i) {
pool2.push_back(static_cast<int>(i));
}
// Agregar algunas floaters si existen
for (size_t i = 50; i < std::min(size_t(55), total_formations); ++i) {
pool2.push_back(static_cast<int>(i));
}
if (!pool2.empty()) {
pools_.push_back(pool2);
}
}
// Pool 3: Solo floaters (si existen formaciones 50+)
if (total_formations > 50) {
Pool pool3;
for (size_t i = 50; i < std::min(size_t(70), total_formations); ++i) {
pool3.push_back(static_cast<int>(i));
}
if (!pool3.empty()) {
pools_.push_back(pool3);
}
}
}