void PopulationScreen(Player *aPlayer) { char input[80]; Country *country; int population; int populationGain; int born; int immigrated; int merchantsImmigrated; int noblesImmigrated; int diedDisease; int diedMalnutrition; int diedStarvation; int armyDiedStarvation; int armyDeserted; /* Get the player country. */ country = aPlayer->country; /* Reset screen. */ clear(); move(0, 0); /* Display the ruler and country. */ printw("%s %s OF %s:\n", aPlayer->title, aPlayer->name, country->name); /* Display the year. */ printw("IN YEAR %d,\n\n", year); /* Determine the total population. */ population = aPlayer->serfCount + aPlayer->merchantCount + aPlayer->nobleCount; /* Determine the number of babies born. */ born = RandRange(((int) (((float) population) / 9.5))); /* Determine the number of people who died from disease. */ diedDisease = RandRange(population / 22); /* Determine the number of people who died of starvation and */ /* malnutrition. */ diedStarvation = 0; diedMalnutrition = 0; if (aPlayer->peopleGrainNeed > (2 * aPlayer->peopleGrainFeed)) { diedMalnutrition = RandRange(population/12 + 1); diedStarvation = RandRange(population/16 + 1); } else if (aPlayer->peopleGrainNeed > aPlayer->peopleGrainFeed) { diedMalnutrition = RandRange(population/15 + 1); } aPlayer->diedStarvation = diedStarvation; /* Determine the number of people who immigrated. */ if (((float) aPlayer->peopleGrainFeed) > (1.5 * ((float) aPlayer->peopleGrainNeed))) { immigrated = ((int) sqrt(aPlayer->peopleGrainFeed - aPlayer->peopleGrainNeed)) - RandRange((int) (1.5 * ((float) aPlayer->customsTax))); if (immigrated > 0) immigrated = RandRange(((2 * immigrated) + 1)); else immigrated = 0; } else { immigrated = 0; } aPlayer->immigrated = immigrated; /* Determine the number of merchants and nobles who immigrated. */ merchantsImmigrated = 0; noblesImmigrated = 0; if ((immigrated / 5) > 0) merchantsImmigrated = RandRange(immigrated / 5); if ((immigrated / 25) > 0) noblesImmigrated = RandRange(immigrated / 25); /* Determine the number of soldiers who died of starvation or deserted. */ armyDiedStarvation = 0; armyDeserted = 0; if (aPlayer->armyGrainNeed > (2 * aPlayer->armyGrainFeed)) { armyDiedStarvation = RandRange(aPlayer->soldierCount/2 + 1); aPlayer->soldierCount -= armyDiedStarvation; armyDeserted = RandRange(aPlayer->soldierCount / 5); aPlayer->soldierCount -= armyDeserted; } /* Determine the army's efficiency. */ aPlayer->armyEfficiency = (10 * aPlayer->armyGrainFeed) / aPlayer->armyGrainNeed; if (aPlayer->armyEfficiency < 5) aPlayer->armyEfficiency = 5; else if (aPlayer->armyEfficiency > 15) aPlayer->armyEfficiency = 15; /* Display the number of babies born. */ printw(" %d BABIES WERE BORN\n", born); /* Display the number of people who died of disease. */ printw(" %d PEOPLE DIED OF DISEASE\n", diedDisease); /* Display the number of people who immigrated. */ if (immigrated > 0) printw(" %d PEOPLE IMMIGRATED INTO YOUR COUNTRY.\n", immigrated); /* Display the number of people who died of starvation and malnutrition. */ if (diedMalnutrition > 0) printw(" %d PEOPLE DIED OF MALNUTRITION.\n", diedMalnutrition); if (diedStarvation > 0) printw(" %d PEOPLE STARVED TO DEATH.\n", diedStarvation); /* Display the number of soldiers who starved to death. */ if (armyDiedStarvation > 0) printw(" %d SOLDIERS STARVED TO DEATH.\n", armyDiedStarvation); /* Display the army efficiency. */ printw("YOUR ARMY WILL FIGHT AT %d%% EFFICIENCY.\n", 10 * aPlayer->armyEfficiency); /* Display the population gain or loss. */ populationGain = born + immigrated - diedDisease - diedMalnutrition - diedStarvation; if (populationGain >= 0) printw("YOUR POPULATION GAINED %d CITIZENS.\n", populationGain); else printw("YOUR POPULATION LOST %d CITIZENS.\n", -populationGain); /* Update population. */ aPlayer->serfCount += populationGain - merchantsImmigrated - noblesImmigrated; aPlayer->merchantCount += merchantsImmigrated; aPlayer->nobleCount += noblesImmigrated; /* Wait for player to be done. */ printw("\n\n<ENTER>? "); getnstr(input, 80); /* Check if player died. */ PlayerDeath(aPlayer); }
void GameplayScreen::HandleCollisions() { // Check which node the player/AI occupies for (std::vector<Node*>::iterator iter = levelManager->GetLegalNodes().begin(); iter != levelManager->GetLegalNodes().end(); ++iter) { // Check if player is inside a node if (Utils::CollisionChecker(levelManager->GetPlayer()->GetBoundingRect(), (*iter)->GetBoundingRect())) { levelManager->GetPlayer()->UpdateNodes(*iter); if (levelManager->GetPlayer()->GetCurrentNode() != levelManager->GetPlayer()->GetPreviousNode()) { // Print the node ID that the player enters printf("Player is in node ID : %d\n", (*iter)->GetNodeId()); levelManager->GetPlayer()->SetPreviousDirection(levelManager->GetPlayer()->GetDirection()); } } // Iterate over each ghost for (std::vector<Ghost*>::iterator aiIter = levelManager->GetGhosts().begin(); aiIter != levelManager->GetGhosts().end(); ++aiIter) { // Check if a ghost is inside a node if (Utils::CollisionChecker((*aiIter)->GetBoundingRect(), (*iter)->GetBoundingRect())) { (*aiIter)->UpdateNodes(*iter); if ((*aiIter)->GetCurrentNode() != (*aiIter)->GetPreviousNode()) { // Print the node ID that the player enters //printf("blinky is in node ID : %d\n", (*iter)->GetNodeId()); (*aiIter)->SetPreviousDirection((*aiIter)->GetDirection()); } } } } // Check for collisions between the player/AI and the wall for (std::vector<Sprite*>::iterator iter = levelManager->GetWalls().begin(); iter != levelManager->GetWalls().end(); ++iter) { // Check if the player collides with a wall if (Utils::CollisionChecker(levelManager->GetPlayer()->GetSpriteRect(), (*iter)->GetBoundingRect())) { if (((levelManager->GetPlayer()->GetDirection() == DirectionEnum::Up) || (levelManager->GetPlayer()->GetDirection() == DirectionEnum::Down)) && ((levelManager->GetPlayer()->GetPreviousDirection() == DirectionEnum::Left) || (levelManager->GetPlayer()->GetPreviousDirection() == DirectionEnum::Right))) { levelManager->GetPlayer()->SetDirection(levelManager->GetPlayer()->GetPreviousDirection()); } else if (((levelManager->GetPlayer()->GetDirection() == DirectionEnum::Left) || (levelManager->GetPlayer()->GetDirection() == DirectionEnum::Right)) && ((levelManager->GetPlayer()->GetPreviousDirection() == DirectionEnum::Up) || (levelManager->GetPlayer()->GetPreviousDirection() == DirectionEnum::Down))) { levelManager->GetPlayer()->SetDirection(levelManager->GetPlayer()->GetPreviousDirection()); } else { levelManager->GetPlayer()->SetDirection(DirectionEnum::None); } levelManager->GetPlayer()->SetPosition(levelManager->GetPlayer()->GetCurrentNode()); } // Check if each ghost collides with a wall /*for (std::vector<Ghost*>::iterator aiIter = levelManager->GetGhosts().begin(); aiIter != levelManager->GetGhosts().end(); ++aiIter) { if (Utils::CollisionChecker((*aiIter)->GetBoundingRect(), (*iter)->GetBoundingRect())) { //printf("A collision has occured between the AI and a wall!\n"); int randVal = std::rand() % 4; // 4 is the number of moving states (*aiIter)->SetPosition((*aiIter)->GetCurrentNode()); (*aiIter)->SetDirection((DirectionEnum)randVal); } }*/ } // Check for collisions between player and consumable // NOTE: we must use iter++ instead of ++iter for (std::vector<Pellet*>::iterator iter = levelManager->GetPellets().begin(); iter != levelManager->GetPellets().end(); iter++) { if (Utils::CollisionChecker(levelManager->GetPlayer()->GetBoundingRect(), (*iter)->GetBoundingRect())) { if ((*iter)->GetType() == NodeTypeEnum::PowerPelletNode) { // Reverse the ghosts directions for (std::vector<Ghost*>::iterator iter = levelManager->GetGhosts().begin(); iter != levelManager->GetGhosts().end(); ++iter) { (*iter)->ReverseDirection(); (*iter)->EnterFrightenedState(0.55f); Ghost::ChangeState(GhostStateEnum::Frightened); Mix_PlayMusic(backgroundPower, -1); isPowerUpState = true; } } // Increment the score score += (*iter)->GetValue(); // Play the appropriate sound clip if ((*iter)->GetType() == PelletNode) { Mix_PlayChannel(-1, pop, 0); } else if ((*iter)->GetType() == PowerPelletNode) { Mix_PlayChannel(-1, powerUp, 0); } // Free memory, cleat the list, and break delete (*iter); levelManager->GetPellets().erase(iter); // The break is necessary for two reasons // 1) we stop checking for nodes once we found we collided with one // 2) if we allow iter to increment we will crash because we are erasing elements break; } } // Check player collision against the AI if (levelManager->GetPlayer()->IsAlive()) { for (std::vector<Ghost*>::iterator iter = levelManager->GetGhosts().begin(); iter != levelManager->GetGhosts().end(); ++iter) { if (Utils::CollisionChecker(levelManager->GetPlayer()->GetBoundingRect(), (*iter)->GetBoundingRect())) { if ((*iter)->IsFrightened()) { // Eat the ghost (*iter)->Respawn(); score += 500; // TODO adjust this Mix_PlayChannel(-1, eatEnemy, 0); } else { PlayerDeath(); } } } } }