int populateGrid(dungeon_t* dungeonPtr) { if (malloc2DArray((void ***) &dungeonPtr->grid, sizeof(**dungeonPtr->grid), WIDTH, HEIGHT)) { return -1; } for (int y = 1; y < HEIGHT - 1; y++) { for (int x = 1; x < WIDTH - 1; x++) { dungeonPtr->grid[y][x].material = rock; dungeonPtr->grid[y][x].hardness = (uint8_t)(rand() % (ROCK_HARDNESS_MAX - 1) + 1); dungeonPtr->grid[y][x].monsterPtr = NULL; } } // Immutable border for (int y = 0; y < HEIGHT; y++) { dungeonPtr->grid[y][0].material = rock; dungeonPtr->grid[y][0].hardness = ROCK_HARDNESS_IMMUTABLE; dungeonPtr->grid[y][0].monsterPtr = NULL; dungeonPtr->grid[y][WIDTH - 1].material = rock; dungeonPtr->grid[y][WIDTH - 1].hardness = ROCK_HARDNESS_IMMUTABLE; dungeonPtr->grid[y][WIDTH - 1].monsterPtr = NULL; } for (int x = 0; x < WIDTH; x++) { dungeonPtr->grid[0][x].material = rock; dungeonPtr->grid[0][x].hardness = ROCK_HARDNESS_IMMUTABLE; dungeonPtr->grid[0][x].monsterPtr = NULL; dungeonPtr->grid[HEIGHT - 1][x].material = rock; dungeonPtr->grid[HEIGHT - 1][x].hardness = ROCK_HARDNESS_IMMUTABLE; dungeonPtr->grid[HEIGHT - 1][x].monsterPtr = NULL; } populateRooms(*dungeonPtr); return 0; }
int main(int argc, char* argv[]) { char* fileName; int errLevel; gridCell_t** dungeonGrid; room_t* rooms; int roomCount; // parse arguments int save = 0; int load = 0; for (int i = 0; i < argc; i++) { if (strcmp(argv[1], "--save") == 0) { save = 1; } else if (strcmp(argv[1], "--load") == 0) { load = 1; } else { showUsage(argv[0]); return 0; } } // load or generate dungeon if (load) { fileName = dungeonFileName(); errLevel = loadDungeon(&dungeonGrid, &roomCount, &rooms, fileName); free(fileName); if (errLevel) { printf("Failed to load the dungeon. Read error %d\n", errLevel); return -1; } populateRooms(dungeonGrid, rooms, roomCount); } else { roomCount = generateDungeon(&dungeonGrid, &rooms); if (roomCount < 0) { printf("Failed to allocate memory for the dungeon grid.\n"); return roomCount; } } // print dungeon printRooms(roomCount, rooms); printGrid(dungeonGrid); // save dungeon if (save) { fileName = dungeonFileName(); errLevel = saveDungeon(dungeonGrid, roomCount, rooms, fileName); free(fileName); if (errLevel) { printf("Failed to save the dungeon. Save error %d\n", errLevel); return -1; } } // Clean up free2DGrid(dungeonGrid, HEIGHT); free(rooms); return 0; }
int main(int argc, char* argv[]) { int errLevel; dungeon_t dungeon; int numMonSpecified = 0; //init random unsigned int seed; seed = (unsigned int)time(NULL); //1453848819; srand(seed); printf("Seed: %d\n", seed); // parse arguments int save = 0; int load = 0; for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "--save") == 0) { save = 1; } else if (strcmp(argv[i], "--load") == 0) { load = 1; } else if (strcmp(argv[i], "--nummon") == 0) { if (i + 1 >= argc) { showUsage(argv[0]); return 0; } dungeon.monsterCount = atoi(argv[++i]); numMonSpecified = 1; } else { showUsage(argv[0]); return 0; } } if (!numMonSpecified) { dungeon.monsterCount = rand() % (42 / 2); } // load or generate dungeon if (load) { errLevel = loadDungeon(&dungeon, dungeonFileName()); if (errLevel) { printf("Failed to load the dungeon. Read error %d\n", errLevel); return -1; } populateRooms(dungeon); } else { errLevel = generateDungeon(&dungeon); if (errLevel) { printf("Failed to allocate memory for the dungeon grid.\n"); return errLevel; } } pathMallocDistGrid(&dungeon.tunnelingDist); pathMallocDistGrid(&dungeon.nontunnelingDist); // save dungeon if (save) { errLevel = saveDungeon(dungeon, dungeonFileName()); if (errLevel) { printf("Failed to save the dungeon. Save error %d\n", errLevel); return -1; } } // make calculations pathTunneling(&dungeon); pathNontunneling(&dungeon); // print dungeon initTerminal(); printDungeon(&dungeon); // do move PC_action userAction = actionMovement; while (dungeon.PC.alive && dungeon.monsterCount > 1 && userAction != actionSave) { int PCTurn = turnIsPC(&dungeon); if (PCTurn) { printDungeon(&dungeon); userAction = turnDoPC(&dungeon); switch (userAction) { case actionStairsUp: case actionStairsDn: // Destroy old dungeon pathFreeDistGrid(dungeon.nontunnelingDist); pathFreeDistGrid(dungeon.tunnelingDist); destroyDungeon(dungeon); dungeon.monsterCount--; // PC was counted as a "monster". He must be removed for reinitialisation. // Build new dungeon errLevel = generateDungeon(&dungeon); if (errLevel) { endwin(); printf("Failed to allocate memory for the dungeon grid.\n"); return errLevel; } pathMallocDistGrid(&dungeon.tunnelingDist); pathMallocDistGrid(&dungeon.nontunnelingDist); pathTunneling(&dungeon); pathNontunneling(&dungeon); break; case actionListMonsters: monsterList(&dungeon); break; default: break; } } else { turnDo(&dungeon); } } screenClearRow(0); if (userAction == actionSave) { mvprintw(0, 0, "Game saved (haha, not really!). Press any key to exit."); } else if (!dungeon.PC.alive) { mvprintw(0, 0, "You died! Press any key to exit."); } else { mvprintw(0, 0, "Yay! You defeated all the monsters. Press any key to exit."); } // Clean up getch(); // "press any key" endwin(); pathFreeDistGrid(dungeon.nontunnelingDist); pathFreeDistGrid(dungeon.tunnelingDist); destroyDungeon(dungeon); return 0; }
void MainWindow::processData(QString data) { if (data == "NC") { this->setWindowTitle("IT Equipment Reservations - [NETWORK ERROR DETECTED]"); ui->connectionIndicator->setText("API can not reach server"); } else if ( data != EMPTY_DB_ENTRY ) { // Work around for an error that happenes under very specific conditions ui->connectionIndicator->setText("Connected to server"); this->setWindowTitle("IT Equipment Reservations"); ui->deliveriesTable->selectRow(-1); ui->pickupsTable->selectRow(-1); QStringList dataList = data.split(INCOMING_RESULT_DELIMETER); switch( socketConn.lastQuery.first ) { case DELIVERY_QUERY: switch( socketConn.lastQuery.second ) { case 1: // Daily deliveries update populateReservations(dataList); break; case 2: // Grab Links table populateLinks(dataList); break; case 3: // Grab Inventory populateInventory(dataList); break; case 4: // Grab Schedule populateSchedule(dataList); break; case 5: // Grab Room Names populateRooms(dataList); break; case 6: // Grab Pickups populatePickups(dataList); break; default: qDebug() << "Error in delivery query [ lastQuery.second ]"; break; } break; // END DELIVERY QUERY CASE case DEVICE_QUERY: switch( socketConn.lastQuery.second ) { case 1: // All Devices default: qDebug() << "Error in devie query [ lastQuery.second ]"; break; } break; // END DEVICE QUERY CASE case CONN_QUERY: switch( socketConn.lastQuery.second ) { case 1: if (data == "y") { this->setWindowTitle("IT Equipment Reservations"); ui->connectionIndicator->setText("Connected to API"); connectedToApi = true; } else { this->setWindowTitle("IT Equipment Reservations - [NETWORK ERROR DETECTED]"); ui->connectionIndicator->setText("Not connected to API"); connectedToApi = false; } break; case 2: if (data == "y") { this->setWindowTitle("IT Equipment Reservations"); ui->connectionIndicator->setText("Connected to server"); apiConnectedToServer = true; } else { if(connectedToApi) { this->setWindowTitle("IT Equipment Reservations - [NETWORK ERROR DETECTED]"); ui->connectionIndicator->setText("API not connected to server"); apiConnectedToServer = false; } else { apiConnectedToServer = false; } } break; default: qDebug() << "Error in conn query [ lastQuery.second ]"; break; } break; // END CONN QUERY default: qDebug() << "Error Processing Data"; break; } } else { this->setWindowTitle("IT Equipment Reservations - [NETWORK ERROR DETECTED]"); qDebug() << "No data returned from server"; ui->connectionIndicator->setText("API Unreachable"); } }