//The colonize order checks to make sure the system is valid, then adds a resource to the planet, checks for //external combat, and awards one point to the player with the leader in the region of the new colony. bool Colonize::doOrder(IGObject::Ptr obj) { ObjectManager* obm = Game::getGame()->getObjectManager(); ObjectTypeManager* obtm = Game::getGame()->getObjectTypeManager(); Fleet* fleetData = (Fleet*)(obj->getObjectBehaviour()); Player::Ptr player = Game::getGame()->getPlayerManager()->getPlayer(fleetData->getOwner()); IGObject::Ptr newStarSys = obm->getObject(starSys->getObjectId()); //Perform last minute checks to make sure the system can be colonized if(newStarSys->getType() != obtm->getObjectTypeByName("Star System")) { //Not a star system Logger::getLogger()->debug("Trying to colonize to an object which is not a star system"); Message::Ptr msg( new Message() ); msg->setSubject("Colonize order failed"); msg->setBody(string("You're fleet, \"" + obj->getName() + "\" tried to colonize an object which is not a star system!")); msg->addReference(rst_Object, obj->getID()); player->postToBoard(msg); return false; } if(!((StarSystem*)(newStarSys->getObjectBehaviour()))->canBeColonized(isMining)) { //Not colonizable Logger::getLogger()->debug("Player tried to colonize a system which cannot be colonized."); Message::Ptr msg( new Message() ); msg->setSubject("Colonize order failed"); msg->setBody(string("You're fleet, \"" + obj->getName() + "\" tried to colonize a fleet which cannot be colonized!")); msg->addReference(rst_Object, obj->getID()); player->postToBoard(msg); return false; } //Find the star system's planet set<uint32_t> children = newStarSys->getContainedObjects(); uint32_t pid; bool planetFound = false; for(set<uint32_t>::iterator i=children.begin(); i != children.end(); i++) { if(obm->getObject(*i)->getType() == obtm->getObjectTypeByName("Planet")) { pid = *i; planetFound = true; } } if(!planetFound) { Logger::getLogger()->debug("Colonize Order: No planet found in target star system"); return false; } //Add resource to planet Planet* planet = (Planet*)(obm->getObject(pid)->getObjectBehaviour()); Design::Ptr ship = Game::getGame()->getDesignStore()->getDesign(fleetData->getShips().begin()->first); uint32_t scoreType = 0; string leaderName; if(ship->getName().compare("MerchantShip") == 0) { planet->addResource(4, 1); scoreType = 1; leaderName = "MerchantLeaderShip"; } else if(ship->getName().compare("ScientistShip") == 0) { planet->addResource(5, 1); scoreType = 2; leaderName = "ScientistLeaderShip"; } else if(ship->getName().compare("SettlerShip") == 0) { planet->addResource(6, 1); scoreType = 3; leaderName = "SettlerLeaderShip"; } else if(ship->getName().compare("MiningShip") == 0) { planet->addResource(7, 1); scoreType = 4; leaderName = "MiningLeaderShip"; } //Get bordering star systems' regions StarSystem* starSysData = (StarSystem*)(newStarSys->getObjectBehaviour()); set<uint32_t> regions = getBorderingRegions(); //Put the newly colonized ship into the proper region //If it does not border any regions, then set it to a new one with a unique id if(regions.size() == 0) { starSysData->setRegion(starSys->getObjectId()); stringstream out; out << starSysData->getRegion(); Logger::getLogger()->debug(string("System " + newStarSys->getName() + " added to region " + out.str()).c_str()); } else if(regions.size() == 1) { // Add +1 to the resource score of the player with the correct leader // in this region int leaderID = getLeaderInRegion(*(regions.begin()), leaderName); if(leaderID < 0 && leaderName.compare("SettlerLeaderShip") != 0) { leaderID = getLeaderInRegion(*(regions.begin()), "SettlerLeaderShip"); } if(leaderID >= 0) { Fleet* leader = (Fleet*) ((obm->getObject((uint32_t) leaderID))->getObjectBehaviour()); Player::Ptr owner = Game::getGame()->getPlayerManager()->getPlayer(leader->getOwner()); owner->setScore(scoreType, owner->getScore(scoreType) + 1); } //Add the newly colonized system to the region starSysData->setRegion(*(regions.begin())); stringstream out; out << starSysData->getRegion(); Logger::getLogger()->debug(string("System " + newStarSys->getName() + " added to region " + out.str()).c_str()); } else { // This means that 2 regions are being connected. Perform check // for external combat map<uint32_t, uint32_t> settlers; map<uint32_t, uint32_t> scientists; map<uint32_t, uint32_t> merchants; map<uint32_t, uint32_t> miners; for(set<uint32_t>::iterator i = regions.begin(); i != regions.end(); i++) { int temp = getLeaderInRegion(*i, "SettlerLeaderShip"); if(temp != -1) { settlers[temp] = *i; } temp = getLeaderInRegion(*i, "ScientistLeaderShip"); if(temp != -1) { scientists[temp] = *i; } temp = getLeaderInRegion(*i, "MerchantLeaderShip"); if(temp != -1) { merchants[temp] = *i; } temp = getLeaderInRegion(*i, "MiningLeaderShip"); if(temp != -1) { miners[temp] = *i; } } TaeTurn* turn = (TaeTurn*) Game::getGame()->getTurnProcess(); bool conflict = false; if(settlers.size() > 1) { //Settler Conflict! turn->queueCombatTurn(false, settlers); conflict = true; } if(scientists.size() > 1) { //Scientist Conflict! turn->queueCombatTurn(false, scientists); conflict = true; } if(merchants.size() > 1) { //Merchant Conflict! turn->queueCombatTurn(false, merchants); conflict = true; } if(miners.size() > 1) { //Miner Conflict! turn->queueCombatTurn(false, miners); conflict = true; } if(!conflict) { uint32_t region = *(regions.begin()); set<uint32_t> objects = obm->getAllIds(); for(set<uint32_t>::iterator i = objects.begin(); i != objects.end(); i++) { IGObject::Ptr ob = obm->getObject(*i); if(ob->getType() == obtm->getObjectTypeByName("Star System")) { StarSystem* sys = (StarSystem*) ob->getObjectBehaviour(); uint32_t r = sys->getRegion(); if(r != region && regions.count(r) > 0) { sys->setRegion(region); } } } } } obm->doneWithObject(newStarSys->getID()); // post completion message Message::Ptr msg( new Message() ); msg->setSubject("Colonize fleet order complete"); msg->setBody(string("You're fleet, \"" + obj->getName() + "\" has colonized ") + newStarSys->getName() + "."); msg->addReference(rst_Action_Order, rsorav_Completion); msg->addReference(rst_Object, starSys->getObjectId()); msg->addReference(rst_Object, obj->getID()); player->postToBoard(msg); //Remove fleet obj->removeFromParent(); obm->scheduleRemoveObject(obj->getID()); return true; }
//End the game void gameOver() { Game* game = Game::getGame(); PlayerManager::Ptr pm = game->getPlayerManager(); map<uint32_t, uint32_t> finalScore; set<uint32_t>::iterator itcurr; set<uint32_t> players = pm->getAllIds(); //Find each player's final score for(itcurr = players.begin(); itcurr != players.end(); itcurr++) { Player::Ptr p = pm->getPlayer(*itcurr); uint32_t artifacts = p->getScore(5); //change artifact points to improve the types with the lowest //points while(artifacts > 0) { int lowest = 1; for(int i = 2; i < 5; i++) { if(p->getScore(i) < p->getScore(lowest)) { lowest = i; } } p->setScore(lowest, p->getScore(lowest) + 1); artifacts--; } //Find the final score int low = 1; for(int i = 2; i < 5; i++) { if(p->getScore(i) < p->getScore(low)) { low = i; } } finalScore[*itcurr] = p->getScore(low); } //Find the winner uint32_t winner = 0; for(map<uint32_t, uint32_t>::iterator i = finalScore.begin(); i != finalScore.end(); i++) { if(winner == 0 || i->second > finalScore[winner]) { winner = i->first; } } for(itcurr = players.begin(); itcurr != players.end(); itcurr++) { Player::Ptr p = pm->getPlayer(*itcurr); //Send message to each player Message::Ptr msg( new Message() ); msg->setSubject("GAME OVER!"); stringstream out; Player::Ptr win = pm->getPlayer(winner); out << win->getName() << " has won the game with a score of "; out << finalScore[winner] << "! "; out << "Your final score after distributing your alien artifacts was: "; out << finalScore[*itcurr]; msg->setBody(out.str()); p->postToBoard(msg); p->setIsAlive(false); } }
//Award artifacts to any players who have a merchant leader //in a region with two artifacts void TaeTurn::awardArtifacts() { Game* game = Game::getGame(); ObjectTypeManager* obtm = game->getObjectTypeManager(); ObjectManager* objectmanager = game->getObjectManager(); std::set<uint32_t> artifacts; std::set<uint32_t> regions; std::set<uint32_t> objects = objectmanager->getAllIds(); std::set<uint32_t>::iterator itcurr; //Find any regions with 2 or more alien artifacts for(itcurr = objects.begin(); itcurr != objects.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); if(ob->getType() == obtm->getObjectTypeByName("Planet")) { Planet* p = (Planet*) ob->getObjectBehaviour(); if(p->getResource(3) > 0) { StarSystem* sys = (StarSystem*)(objectmanager->getObject(ob->getParent())->getObjectBehaviour()); if(sys->getRegion() != 0) { if(regions.count(sys->getRegion()) > 0) { artifacts.insert(*itcurr); } else { regions.insert(sys->getRegion()); } } } } } if(!artifacts.empty()) { uint32_t type; DesignStore::Ptr ds = game->getDesignStore(); PlayerManager::Ptr pm = game->getPlayerManager(); std::set<uint32_t> designs = ds->getDesignIds(); //get leader ID for(itcurr = designs.begin(); itcurr != designs.end(); ++itcurr) { if(ds->getDesign(*itcurr)->getName().compare("MerchantLeaderShip")) { type = *itcurr; } } //Search the objects for a merchant leader for(itcurr = objects.begin(); itcurr != objects.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); if(ob->getType() == obtm->getObjectTypeByName("Fleet")) { Fleet* f = (Fleet*) (ob->getObjectBehaviour()); if(f->getShips().count(type) > 0) { IGObject::Ptr parent = objectmanager->getObject(ob->getParent()); if(parent->getType() == obtm->getObjectTypeByName("Star System")) { StarSystem* parentData = (StarSystem*) (parent->getObjectBehaviour()); //See if this leader is in a region with //2 or more alien artifacts for(std::set<uint32_t>::iterator i = artifacts.begin(); i != artifacts.end(); ++i) { IGObject::Ptr obj = objectmanager->getObject(*i); Planet* p = (Planet*) obj->getObjectBehaviour(); StarSystem* sys = (StarSystem*)(objectmanager->getObject(obj->getParent())->getObjectBehaviour()); if(sys->getRegion() == parentData->getRegion()) { //+1 to leader's owner's artifact score Player::Ptr owner = pm->getPlayer(f->getOwner()); owner->setScore(5, owner->getScore(5) + 1); p->removeResource(3, 1); artifacts.erase(*i); } } } } } } } }
void TaeTurn::doTurn() { //check to make sure the game is still ging and this is not a combat turn if(isGameOver) { gameOver(); return; } else if(combat) { doCombatTurn(); return; } std::set<uint32_t>::iterator itcurr; Game* game = Game::getGame(); OrderManager* ordermanager = game->getOrderManager(); ObjectManager* objectmanager = game->getObjectManager(); ObjectTypeManager* obtm = game->getObjectTypeManager(); PlayerManager::Ptr playermanager = game->getPlayerManager(); //build map for storing orders std::map<uint32_t, std::list<IGObject::Ptr > > playerOrders; containerids.clear(); //separate orders by player std::set<uint32_t> objects = objectmanager->getAllIds(); for(itcurr = objects.begin(); itcurr != objects.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); if(ob->getType() == planettype || ob->getType() == fleettype) { OrderQueueObjectParam* oqop = dynamic_cast<OrderQueueObjectParam*>(ob->getParameterByType(obpT_Order_Queue)); if(oqop != NULL) { OrderQueue::Ptr orderqueue = ordermanager->getOrderQueue(oqop->getQueueId()); if(orderqueue != NULL) { Order * currOrder = orderqueue->getFirstOrder(); if(currOrder != NULL) { uint32_t owner = ((OwnedObject*)(ob->getObjectBehaviour()))->getOwner(); std::list<IGObject::Ptr >::iterator i = playerOrders[owner].end(); playerOrders[owner].insert(i, ob); } } } } } //Do orders for players in the correct order //The order in which orders are processed is rotated each turn std::set<uint32_t> players = playermanager->getAllIds(); itcurr = players.begin(); for(int i = 0; i < playerTurn; i++) { itcurr++; } for(uint32_t it = 0; it < players.size(); it++) { if(itcurr == players.end()) { itcurr = players.begin(); } if(playerOrders[*itcurr].size() > 0) { for(std::list<IGObject::Ptr >::iterator i = playerOrders[*itcurr].begin(); i != playerOrders[*itcurr].end(); i++) { OrderQueue::Ptr orderqueue = ordermanager->getOrderQueue(((OrderQueueObjectParam*)((*i)->getParameterByType(obpT_Order_Queue)))->getQueueId()); Order* currOrder = orderqueue->getFirstOrder(); if(currOrder!= NULL) { if(currOrder->doOrder(*i)) { orderqueue->removeFirstOrder(); } else { orderqueue->updateFirstOrder(); } } if((*i)->getContainerType() >= 1) { containerids.insert((*i)->getID()); } objectmanager->doneWithObject((*i)->getID()); } } itcurr++; } awardArtifacts(); //Check for end game condition of less than 3 artifacts remaining objects = objectmanager->getAllIds(); int numArtifacts = 0; for(itcurr = objects.begin(); itcurr != objects.end(); itcurr++) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); if(ob->getType() == obtm->getObjectTypeByName("Planet")) { Planet* p = (Planet*) ob->getObjectBehaviour(); if(p->getResource(3) > 0) { numArtifacts++; } } } if(numArtifacts < 3) { isGameOver = true; gameOver(); return; } //Update which player's turn it is playerTurn = (playerTurn + 1) % playermanager->getNumPlayers(); objectmanager->clearRemovedObjects(); // to once a turn for(itcurr = objects.begin(); itcurr != objects.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); if(ob->isAlive()) { ob->getObjectBehaviour()->doOnceATurn(); } objectmanager->doneWithObject(ob->getID()); } // find the objects that are visible to each player std::set<uint32_t> vis = objectmanager->getAllIds(); for(std::set<uint32_t>::iterator itplayer = players.begin(); itplayer != players.end(); ++itplayer) { Player::Ptr player = playermanager->getPlayer(*itplayer); PlayerView::Ptr playerview = player->getPlayerView(); for(std::set<uint32_t>::iterator itob = vis.begin(); itob != vis.end(); ++itob) { ObjectView::Ptr obv = playerview->getObjectView(*itob); if(obv == NULL) { if(objectmanager->getObject(*itob)->isAlive()) { playerview->addVisibleObject( *itob, true ); } objectmanager->doneWithObject(*itob); } else { IGObject::Ptr ro = objectmanager->getObject(*itob); uint64_t obmt = ro->getModTime(); objectmanager->doneWithObject(*itob); if(obmt > obv->getModTime()) { obv->setModTime(obmt); playerview->updateObjectView(*itob); } } } // remove dead objects std::set<uint32_t> goneobjects; std::set<uint32_t> knownobjects = playerview->getVisibleObjects(); set_difference(knownobjects.begin(), knownobjects.end(), vis.begin(), vis.end(), inserter(goneobjects, goneobjects.begin())); for(std::set<uint32_t>::iterator itob = goneobjects.begin(); itob != goneobjects.end(); ++itob) { ObjectView::Ptr obv = playerview->getObjectView(*itob); if(!obv->isGone()) { obv->setGone(true); playerview->updateObjectView(*itob); } } //Replace colonist fleets int fleets = 0; IGObject::Ptr homePlanet; for(std::set<uint32_t>::iterator itob = objects.begin(); itob != objects.end(); ++itob) { IGObject::Ptr ob = objectmanager->getObject(*itob); if(ob->getName().compare("Colonist Fleet") == 0) { uint32_t owner = ((OwnedObject*)(ob->getObjectBehaviour()))->getOwner(); if(owner == *itplayer) { fleets++; } } if(ob->getName().compare(string(player->getName() + "'s Home Planet")) == 0) { homePlanet = ob; } } for(int i = fleets; i < 6; i++) { IGObject::Ptr fleet = fleetBuilder->createFleet(FleetBuilder::PASSENGER_FLEET, FleetBuilder::RANDOM_SHIP, player, homePlanet, "Colonist Fleet"); game->getObjectManager()->addObject(fleet); player->getPlayerView()->addVisibleObject(fleet->getID(), true); } //Send end of turn message to each player Message::Ptr msg( new Message() ); msg->setSubject("Turn complete"); stringstream out; out << "Your Current Score: \n"; out << "Money: " << player->getScore(1) << "\n"; out << "Technology: " << player->getScore(2) << "\n"; out << "People: " << player->getScore(3) << "\n"; out << "Raw Materials: " << player->getScore(4) << "\n"; out << "Alien Artifacts: " << player->getScore(5); msg->setBody(out.str()); player->postToBoard(msg); //Alert players to the turn order for next round msg.reset( new Message() ); msg->setSubject("Turn Order"); string body = "The order for the next turn is: "; itcurr = players.begin(); for(int i = 0; i < playerTurn; i++) { itcurr++; } for(uint32_t it = 0; it < players.size(); it++) { if(itcurr == players.end()) { itcurr = players.begin(); } body += playermanager->getPlayer(*itcurr)->getName(); body += " "; itcurr++; } msg->setBody(body); player->postToBoard(msg); } //Initialize combat if the next turn is a combat turn if(combat) { initCombat(); } playermanager->updateAll(); }
void TaeTurn::doCombatTurn() { std::set<uint32_t>::iterator itcurr; Game* game = Game::getGame(); OrderManager* ordermanager = game->getOrderManager(); ObjectManager* objectmanager = game->getObjectManager(); ObjectTypeManager* obtm = game->getObjectTypeManager(); PlayerManager::Ptr playermanager = game->getPlayerManager(); containerids.clear(); // Do orders std::set<uint32_t> objects = objectmanager->getAllIds(); for(itcurr = objects.begin(); itcurr != objects.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); if(ob->getType() == planettype || ob->getType() == fleettype) { OrderQueueObjectParam* oqop = dynamic_cast<OrderQueueObjectParam*>(ob->getParameterByType(obpT_Order_Queue)); if(oqop != NULL) { OrderQueue::Ptr orderqueue = ordermanager->getOrderQueue(oqop->getQueueId()); if(orderqueue != NULL) { Order * currOrder = orderqueue->getFirstOrder(); if(currOrder != NULL) { if(currOrder->doOrder(ob)) { orderqueue->removeFirstOrder(); } else { orderqueue->updateFirstOrder(); } } if(ob->getContainerType() >= 1) { containerids.insert(ob->getID()); } objectmanager->doneWithObject((ob)->getID()); } } } } //Determine winner uint32_t winner = 0; uint32_t loser = 0; for(map<uint32_t, int>::iterator i = strength.begin(); i != strength.end(); ++i) { if(winner == 0) { winner = i->first; } else { if(strength[winner] < i->second) { winner = i->first; } else { loser = i->first; } } } //Remove losing combatants uint32_t losingRegion; uint32_t winningRegion; set<uint32_t> removedSystems; if(isInternal) { //Internal combat removes losing leader and awards one point to the winner for(map<uint32_t, uint32_t>::iterator i = combatants.begin(); i != combatants.end(); ++i) { IGObject::Ptr ob = objectmanager->getObject(i->first); Fleet* f = (Fleet*) ob->getObjectBehaviour(); if(f->getOwner() != winner) { sendHome(i->first); Player::Ptr p = playermanager->getPlayer(winner); p->setScore(2, p->getScore(2) + 1); losingRegion = i->second; winningRegion = losingRegion; } } objects = objectmanager->getAllIds(); for(itcurr = objects.begin(); itcurr!= objects.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); if(ob->getType() == obtm->getObjectTypeByName("Star System")) { StarSystem* sysData = (StarSystem*) ob->getObjectBehaviour(); if(sysData->getRegion() == losingRegion) { sysData->setRegion(0); removedSystems.insert(*itcurr); } } } } else { //External combat removes the losing leader and losing colonies. //1 point is awarded for the leader and each colony removed string shipType; //set shiptype, losing/winning regions, and send home the losing leader for(map<uint32_t, uint32_t>::iterator i = combatants.begin(); i != combatants.end(); ++i) { IGObject::Ptr ob = objectmanager->getObject(i->first); Fleet* f = (Fleet*) ob->getObjectBehaviour(); if(f->getOwner() != winner) { losingRegion = i->second; shipType = ob->getName(); sendHome(i->first); } else { winningRegion = i->second; } } //set the correct resource type int resourceType; if(shipType.compare("Merchant Leader") == 0) { resourceType = 4; } else if(shipType.compare("Scientist Leader") == 0) { resourceType = 5; } else if(shipType.compare("Settler Leader") == 0) { resourceType = 6; } else { resourceType = 7; } //add 1 to the winner for removing the leader Player::Ptr player = playermanager->getPlayer(winner); player->setScore(resourceType - 3, player->getScore(resourceType-3) + 1); //remove all losing colonies objects = objectmanager->getAllIds(); for(itcurr = objects.begin(); itcurr!= objects.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); if(ob->getType() == obtm->getObjectTypeByName("Planet")) { IGObject::Ptr sys = objectmanager->getObject(ob->getParent()); StarSystem* sysData = (StarSystem*) sys->getObjectBehaviour(); if(sysData->getRegion() == losingRegion) { Planet* p = (Planet*) ob->getObjectBehaviour(); sysData->setRegion(0); if(p->getResource(resourceType) > 0) { p->removeResource(resourceType, 1); player->setScore(resourceType - 3, player->getScore(resourceType-3) + 1); } else { removedSystems.insert(sys->getID()); } } else if(sysData->getRegion() == winningRegion) { sysData->setRegion(0); removedSystems.insert(sys->getID()); } } } } //rebuild region for(itcurr = removedSystems.begin(); itcurr!= removedSystems.end(); ++itcurr) { rebuildRegion(*itcurr); } //Check for combat combat = false; while(!combatQueue.empty() && !combat) { pair<bool, map<uint32_t, uint32_t> > temp = combatQueue.front(); combatQueue.pop(); isInternal = temp.first; combatants = temp.second; combat = true; uint32_t region = 0; for(map<uint32_t, uint32_t>::iterator i = combatants.begin(); i != combatants.end() && combat; i++) { IGObject::Ptr ob = objectmanager->getObject(i->first); IGObject::Ptr sys = objectmanager->getObject(ob->getParent()); StarSystem* sysData = (StarSystem*) sys->getObjectBehaviour(); i->second = sysData->getRegion(); if(region == 0) { region = sysData->getRegion(); } else if(region != sysData->getRegion()) { combat = false; } } } //Cleanup and Set visibility objects = objectmanager->getAllIds(); set<uint32_t> views; for(itcurr = objects.begin(); itcurr != objects.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); if(ob->getType() == obtm->getObjectTypeByName("Fleet") && !combat) { Fleet* f = (Fleet*) ob->getObjectBehaviour(); f->toggleCombat(); f->setCombatant(false); } //Set visibility views.insert(ob->getID()); } std::set<uint32_t> players = playermanager->getAllIds(); std::set<uint32_t> vis = objectmanager->getAllIds(); //clear the removed objecs objectmanager->clearRemovedObjects(); objects = objectmanager->getAllIds(); for(itcurr = objects.begin(); itcurr != objects.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); if(ob->isAlive()) { ob->getObjectBehaviour()->doOnceATurn(); } objectmanager->doneWithObject(ob->getID()); } for(std::set<uint32_t>::iterator itplayer = players.begin(); itplayer != players.end(); ++itplayer) { Player::Ptr player = playermanager->getPlayer(*itplayer); PlayerView::Ptr playerview = player->getPlayerView(); //Update visibility playerview->addVisibleObjects(views); for(std::set<uint32_t>::iterator itob = vis.begin(); itob != vis.end(); ++itob) { ObjectView::Ptr obv = playerview->getObjectView(*itob); if(obv == NULL) { if(objectmanager->getObject(*itob)->isAlive()) { playerview->addVisibleObject(*itob, true); } objectmanager->doneWithObject(*itob); } else { IGObject::Ptr ro = objectmanager->getObject(*itob); uint64_t obmt = ro->getModTime(); objectmanager->doneWithObject(*itob); if(obmt > obv->getModTime()) { obv->setModTime(obmt); playerview->updateObjectView(*itob); } } } // remove dead objects std::set<uint32_t> goneobjects; std::set<uint32_t> knownobjects = playerview->getVisibleObjects(); set_difference(knownobjects.begin(), knownobjects.end(), vis.begin(), vis.end(), inserter(goneobjects, goneobjects.begin())); for(std::set<uint32_t>::iterator itob = goneobjects.begin(); itob != goneobjects.end(); ++itob) { ObjectView::Ptr obv = playerview->getObjectView(*itob); if(!obv->isGone()) { obv->setGone(true); playerview->updateObjectView(*itob); } } //Replace colonist fleets int fleets = 0; IGObject::Ptr homePlanet; for(std::set<uint32_t>::iterator itob = objects.begin(); itob != objects.end(); ++itob) { IGObject::Ptr ob = objectmanager->getObject(*itob); if(ob->getName().compare("Colonist Fleet") == 0) { uint32_t owner = ((OwnedObject*)(ob->getObjectBehaviour()))->getOwner(); if(owner == *itplayer) { fleets++; } } if(ob->getName().compare(string(player->getName() + "'s Home Planet")) == 0) { homePlanet = ob; } } for(int i = fleets; i < 6; i++) { IGObject::Ptr fleet = fleetBuilder->createFleet(FleetBuilder::PASSENGER_FLEET, FleetBuilder::RANDOM_SHIP, player, homePlanet, "Colonist Fleet"); game->getObjectManager()->addObject(fleet); player->getPlayerView()->addVisibleObject(fleet->getID(), true); } //Send end of turn message to each player Message::Ptr msg( new Message() ); msg->setSubject("Combat Complete!"); stringstream out; Player::Ptr win = playermanager->getPlayer(winner); out << win->getName() << " has won the battle with a strength of "; out << strength[winner] << "!"; msg->setBody(out.str()); player->postToBoard(msg); msg.reset( new Message() ); msg->setSubject("Turn complete"); stringstream out2; out2 << "Your Current Score: \n"; out2 << "Money: " << player->getScore(1) << "\n"; out2 << "Technology: " << player->getScore(2) << "\n"; out2 << "People: " << player->getScore(3) << "\n"; out2 << "Raw Materials: " << player->getScore(4) << "\n"; out2 << "Alien Artifacts: " << player->getScore(5); msg->setBody(out2.str()); player->postToBoard(msg); } if(combat) { initCombat(); } playermanager->updateAll(); }
void MinisecTurn::doTurn(){ std::set<uint32_t>::iterator itcurr; Game* game = Game::getGame(); OrderManager* ordermanager = game->getOrderManager(); ObjectManager* objectmanager = game->getObjectManager(); RSPCombat* combatstrategy = new RSPCombat(); PlayerManager::Ptr playermanager = game->getPlayerManager(); //sort by order type std::set<uint32_t> movers; std::set<uint32_t> otherorders; std::set<uint32_t> interceptors; containerids.clear(); std::set<uint32_t> possiblecombatants; std::set<uint32_t> objects = objectmanager->getAllIds(); for(itcurr = objects.begin(); itcurr != objects.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); if(ob->getType() == planettype || ob->getType() == fleettype){ possiblecombatants.insert(ob->getID()); OrderQueueObjectParam* oqop = dynamic_cast<OrderQueueObjectParam*>(ob->getParameterByType(obpT_Order_Queue)); if(oqop != NULL){ OrderQueue::Ptr orderqueue = ordermanager->getOrderQueue(oqop->getQueueId()); if(orderqueue != NULL){ Order * currOrder = orderqueue->getFirstOrder(); if(currOrder != NULL){ if(currOrder->getType() == ordermanager->getOrderTypeByName("Move")){ movers.insert(ob->getID()); } else if (currOrder->getType() == ordermanager->getOrderTypeByName("Intercept")){ interceptors.insert(ob->getID()); }else{ otherorders.insert(ob->getID()); } } } } } if(ob->getContainerType() >= 1){ containerids.insert(ob->getID()); } objectmanager->doneWithObject(ob->getID()); } // do move for(itcurr = movers.begin(); itcurr != movers.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); OrderQueueObjectParam* oqop = dynamic_cast<OrderQueueObjectParam*>(ob->getParameterByType(obpT_Order_Queue)); OrderQueue::Ptr orderqueue = ordermanager->getOrderQueue(oqop->getQueueId()); Order * currOrder = orderqueue->getFirstOrder(); if(currOrder->doOrder(ob)){ orderqueue->removeFirstOrder(); }else{ orderqueue->updateFirstOrder(); } objectmanager->doneWithObject(ob->getID()); } // do interceptions for(itcurr = interceptors.begin(); itcurr != interceptors.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); OrderQueueObjectParam* oqop = dynamic_cast<OrderQueueObjectParam*>(ob->getParameterByType(obpT_Order_Queue)); OrderQueue::Ptr orderqueue = ordermanager->getOrderQueue(oqop->getQueueId()); Order * currOrder = orderqueue->getFirstOrder(); if(currOrder->doOrder(ob)){ orderqueue->removeFirstOrder(); }else{ orderqueue->updateFirstOrder(); } objectmanager->doneWithObject(ob->getID()); } // do combat std::list<std::map<uint32_t, std::set<uint32_t> > > combats; for(itcurr = possiblecombatants.begin(); itcurr != possiblecombatants.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); uint32_t playerid1; Vector3d pos1; uint32_t size1; if(ob->getType() == planettype){ Planet* planet = (Planet*)(ob->getObjectBehaviour()); playerid1 = planet->getOwner(); pos1 = planet->getPosition(); size1 = planet->getSize(); }else{ Fleet* fleet = (Fleet*)(ob->getObjectBehaviour()); playerid1 = fleet->getOwner(); pos1 = fleet->getPosition(); size1 = fleet->getSize(); } if(playerid1 == 0){ objectmanager->doneWithObject(ob->getID()); continue; } bool placed = false; for(std::list<std::map<uint32_t, std::set<uint32_t> > >::iterator itlist = combats.begin(); itlist != combats.end(); ++itlist){ std::map<uint32_t, std::set<uint32_t> > themap = *itlist; for(std::map<uint32_t, std::set<uint32_t> >::iterator itmap = themap.begin(); itmap != themap.end(); ++itmap){ std::set<uint32_t> theset = itmap->second; for(std::set<uint32_t>::iterator itset = theset.begin(); itset != theset.end(); ++itset){ IGObject::Ptr itbobj = objectmanager->getObject(*itset); uint32_t playerid2; Vector3d pos2; uint32_t size2; if(itbobj->getType() == planettype){ Planet* planet = (Planet*)(itbobj->getObjectBehaviour()); playerid2 = planet->getOwner(); pos2 = planet->getPosition(); size2 = planet->getSize(); }else{ Fleet* fleet = (Fleet*)(itbobj->getObjectBehaviour()); playerid2 = fleet->getOwner(); pos2 = fleet->getPosition(); size2 = fleet->getSize(); } if(playerid2 == 0){ objectmanager->doneWithObject(itbobj->getID()); continue; } uint64_t diff = pos1.getDistance(pos2); if(diff <= size1 / 2 + size2 / 2){ themap[playerid1].insert(ob->getID()); *itlist = themap; placed = true; objectmanager->doneWithObject(itbobj->getID()); break; } objectmanager->doneWithObject(itbobj->getID()); } if(placed) break; } if(placed) break; } if(!placed){ std::map<uint32_t, std::set<uint32_t> > themap; std::set<uint32_t> theset; theset.insert(ob->getID()); themap[playerid1] = theset; combats.push_back(themap); } objectmanager->doneWithObject(ob->getID()); } for(std::list<std::map<uint32_t, std::set<uint32_t> > >::iterator itlist = combats.begin(); itlist != combats.end(); ++itlist){ std::map<uint32_t, std::set<uint32_t> > themap = *itlist; if(themap.size() >= 2){ combatstrategy->doCombat(themap); } } objectmanager->clearRemovedObjects(); // do other orders (nop, buildfleet, colonise) for(itcurr = otherorders.begin(); itcurr != otherorders.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); if(ob != NULL){ if(ob->getType() == planettype || ob->getType() == fleettype){ OrderQueueObjectParam* oqop = dynamic_cast<OrderQueueObjectParam*>(ob->getParameterByType(obpT_Order_Queue)); if(oqop != NULL){ OrderQueue::Ptr orderqueue = ordermanager->getOrderQueue(oqop->getQueueId()); if(orderqueue != NULL){ Order * currOrder = orderqueue->getFirstOrder(); if(currOrder != NULL){ if(currOrder->doOrder(ob)){ orderqueue->removeFirstOrder(); }else{ orderqueue->updateFirstOrder(); } } } } } objectmanager->doneWithObject(ob->getID()); } } objectmanager->clearRemovedObjects(); // to once a turn (right at the end) objects = objectmanager->getAllIds(); for(itcurr = objects.begin(); itcurr != objects.end(); ++itcurr) { IGObject::Ptr ob = objectmanager->getObject(*itcurr); if(ob->isAlive()){ ob->getObjectBehaviour()->doOnceATurn(); } objectmanager->doneWithObject(ob->getID()); } // find the objects that are visible to each player std::set<uint32_t> vis = objectmanager->getAllIds(); std::set<uint32_t> players = playermanager->getAllIds(); uint32_t numaliveplayers = 0; uint32_t numdeadplayers = 0; for(std::set<uint32_t>::iterator itplayer = players.begin(); itplayer != players.end(); ++itplayer){ Player::Ptr player = playermanager->getPlayer(*itplayer); PlayerView::Ptr playerview = player->getPlayerView(); for(std::set<uint32_t>::iterator itob = vis.begin(); itob != vis.end(); ++itob){ ObjectView::Ptr obv = playerview->getObjectView(*itob); if(!obv){ if(objectmanager->getObject(*itob)->isAlive()){ playerview->addVisibleObject( *itob, true ); } objectmanager->doneWithObject(*itob); }else{ IGObject::Ptr ro = objectmanager->getObject(*itob); uint64_t obmt = ro->getModTime(); objectmanager->doneWithObject(*itob); if(obmt > obv->getModTime()){ obv->setModTime(obmt); playerview->updateObjectView(*itob); } } } // remove dead objects std::set<uint32_t> goneobjects; std::set<uint32_t> knownobjects = playerview->getVisibleObjects(); set_difference(knownobjects.begin(), knownobjects.end(), vis.begin(), vis.end(), inserter(goneobjects, goneobjects.begin())); for(std::set<uint32_t>::iterator itob = goneobjects.begin(); itob != goneobjects.end(); ++itob){ ObjectView::Ptr obv = playerview->getObjectView(*itob); if(!obv->isGone()){ obv->setGone(true); playerview->updateObjectView(*itob); } } if(!player->isAlive() || playerview->getNumberOwnedObjects() == 0){ if(player->isAlive()){ Message::Ptr msg( new Message() ); msg->setSubject("You lost"); msg->setBody("You do not own any objects, therefore you game has finished."); msg->addReference(rst_Action_Player, rspav_Eliminated); player->postToBoard(msg); player->setIsAlive(false); } numdeadplayers++; }else{ numaliveplayers++; } } if(numaliveplayers == 1){ //find alive player Player::Ptr player; for(std::set<uint32_t>::iterator itplayer = players.begin(); itplayer != players.end(); ++itplayer){ player = playermanager->getPlayer(*itplayer); if(player->isAlive()) break; } if(player->getScore(0) != numdeadplayers - 1){ Message::Ptr msg( new Message() ); msg->setSubject("You won!"); msg->setBody("You have eliminated all the competing players. Congratulations!"); player->postToBoard(msg); player->setScore(0, numdeadplayers - 1); } } playermanager->updateAll(); delete combatstrategy; }