//Sends a fleet back to it's home planet void TaeTurn::sendHome(uint32_t fleet) { Game* game = Game::getGame(); ObjectManager* obm = game->getObjectManager(); ObjectTypeManager* obtm = game->getObjectTypeManager(); PlayerManager::Ptr pm = game->getPlayerManager(); IGObject::Ptr fleetobj = obm->getObject(fleet); //Check to make sure it is really a fleet if(fleetobj->getType() != obtm->getObjectTypeByName("Fleet")) { return; } //Get all the required objects Fleet* f = (Fleet*) fleetobj->getObjectBehaviour(); Player::Ptr p = pm->getPlayer(f->getOwner()); IGObject::Ptr sys = obm->getObject(fleetobj->getParent()); StarSystem* sysData = (StarSystem*) sys->getObjectBehaviour(); //Remove fleet from system sysData->setRegion(0); fleetobj->removeFromParent(); //Find it's home planet std::set<uint32_t> objects = obm->getAllIds(); std::set<uint32_t>::iterator itcurr; for(itcurr = objects.begin(); itcurr != objects.end(); ++itcurr) { IGObject::Ptr ob = obm->getObject(*itcurr); if(ob->getName().compare(string(p->getName() + "'s Home Planet")) == 0) { Planet* p = (Planet*) ob->getObjectBehaviour(); f->setPosition(p->getPosition()); fleetobj->addToParent(ob->getID()); } } }
//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; }
bool MergeFleet::doOrder(IGObject::Ptr ob){ IGObject::Ptr parent = Game::getGame()->getObjectManager()->getObject(ob->getParent()); Fleet *myfleet = (Fleet*)(ob->getObjectBehaviour()); //find fleet to merge with uint32_t targetid = 0; std::set<uint32_t> oblist = parent->getContainedObjects(); for(std::set<uint32_t>::iterator itcurr = oblist.begin(); itcurr != oblist.end(); ++itcurr){ if(*itcurr == ob->getID()) continue; IGObject::Ptr ptarget = Game::getGame()->getObjectManager()->getObject(*itcurr); if(ptarget->getType() == ob->getType()){ Fleet* pfleet = (Fleet*)(ptarget->getObjectBehaviour()); if(pfleet->getOwner() == myfleet->getOwner()){ if(pfleet->getSize() + myfleet->getSize() > pfleet->getPosition().getDistance(myfleet->getPosition())){ targetid = *itcurr; Game::getGame()->getObjectManager()->doneWithObject(*itcurr); break; } } } Game::getGame()->getObjectManager()->doneWithObject(*itcurr); } if(targetid == 0){ Message::Ptr msg( new Message() ); msg->setSubject("Merge Fleet order canceled"); msg->setBody("No target fleet at this location"); msg->addReference(rst_Action_Order, rsorav_Canceled); msg->addReference(rst_Object, ob->getID()); Game::getGame()->getPlayerManager()->getPlayer(((Fleet*)(ob->getObjectBehaviour()))->getOwner())->postToBoard(msg); }else{ IGObject::Ptr target = Game::getGame()->getObjectManager()->getObject(targetid); Message::Ptr msg( new Message() ); msg->setSubject("Merge Fleet order complete"); msg->setBody("The two fleets have been merged"); msg->addReference(rst_Action_Order, rsorav_Completion); msg->addReference(rst_Object, ob->getID()); msg->addReference(rst_Object, target->getID()); Fleet *tfleet = (Fleet*)(target->getObjectBehaviour()); IdMap ships = myfleet->getShips(); for(IdMap::iterator itcurr = ships.begin(); itcurr != ships.end(); ++itcurr){ tfleet->addShips(itcurr->first, itcurr->second); } //remove the fleet from the physical universe ob->removeFromParent(); Game::getGame()->getObjectManager()->scheduleRemoveObject(ob->getID()); Game::getGame()->getPlayerManager()->getPlayer(myfleet->getOwner())->getPlayerView()->removeOwnedObject(ob->getID()); Game::getGame()->getObjectManager()->doneWithObject(target->getID()); Game::getGame()->getPlayerManager()->getPlayer(myfleet->getOwner())->postToBoard(msg); } return true; }