//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()); } } }
bool UnloadArmament::doOrder(IGObject::Ptr ob){ Fleet* fleet = dynamic_cast<Fleet*>(ob->getObjectBehaviour()); ObjectManager* obman = Game::getGame()->getObjectManager(); ObjectTypeManager* otman = Game::getGame()->getObjectTypeManager(); ResourceManager::Ptr resman = Game::getGame()->getResourceManager(); std::set<uint32_t>objs = obman->getObjectsByPos(fleet->getPosition(), 10000); IGObject::Ptr planetObj; Planet* planet; for (std::set<uint32_t>::const_iterator itcurr = objs.begin(); itcurr != objs.end(); ++itcurr) { if (obman->getObject(*itcurr)->getType() == otman->getObjectTypeByName("Planet")) { planetObj = obman->getObject(*itcurr); planet = dynamic_cast<Planet*>(planetObj->getObjectBehaviour()); Logger::getLogger()->debug("UnloadArmaments::doOrder Found Planet %s for Unload Armaments Order", planetObj->getName().c_str()); const uint32_t factoryType = resman->getResourceDescription("Factories")->getResourceType(); IdMap weapontype = weaponlist->getList(); for(IdMap::iterator weaponit = weapontype.begin(); weaponit != weapontype.end(); ++weaponit) { if (planet->removeResource(factoryType, 1)) { if (fleet->removeResource(weaponit->first, weaponit->second)) { Logger::getLogger()->debug("UnloadArmaments::doOrder success, adding to resource %d: #:%d", weaponit->first, weaponit->second); planet->addResource(weaponit->first, weaponit->second); planetObj->touchModTime(); return true; } } else { turns = 1; return true; } } } } return false; }
std::map<uint32_t, std::pair<std::string, uint32_t> > LoadArmament::generateListOptions(){ Logger::getLogger()->debug("Entering LoadArmament::generateListOptions"); std::map<uint32_t, std::pair<std::string, uint32_t> > options; Game* game = Game::getGame(); IGObject::Ptr selectedObj = game->getObjectManager()->getObject( game->getOrderManager()->getOrderQueue(orderqueueid)->getObjectId()); Fleet* fleet = dynamic_cast<Fleet*>(selectedObj->getObjectBehaviour()); ObjectManager* obman = Game::getGame()->getObjectManager(); ObjectTypeManager* otman = Game::getGame()->getObjectTypeManager(); ResourceManager::Ptr resman = Game::getGame()->getResourceManager(); std::set<uint32_t>objs = obman->getObjectsByPos(fleet->getPosition(), 10000); for (std::set<uint32_t>::iterator itcurr = objs.begin(); itcurr != objs.end(); ++itcurr) { if (obman->getObject(*itcurr)->getType() == otman->getObjectTypeByName("Planet")) { IGObject::Ptr planetObj = obman->getObject(*itcurr); Planet* planet = dynamic_cast<Planet*>(planetObj->getObjectBehaviour()); Logger::getLogger()->debug("Found Planet %s for Load Armaments Order", planetObj->getName().c_str()); std::map<uint32_t, std::pair<uint32_t, uint32_t> > reslist = planet->getResources(); for (std::map<uint32_t, std::pair<uint32_t, uint32_t> >::iterator it = reslist.begin(); it != reslist.end(); ++it) { if (resman->getResourceDescription(it->first)->getUnitSingular() == "weapon") { options[it->first] = std::pair<std::string, uint32_t>(resman->getResourceDescription(it->first)->getNamePlural(), planet->getResourceSurfaceValue(it->first)); } } } } Logger::getLogger()->debug("Exiting LoadArmament::generateListOptions"); return options; }
bool Bombard::doOrder(IGObject *fleet) { Game *game = Game::getGame(); ObjectManager *om = game->getObjectManager(); ObjectTypeManager *odm = game->getObjectTypeManager(); Random *rand = game->getRandom(); IGObject *planetObj = om->getObject(planet->getObjectId()); if(planetObj->getType() != odm->getObjectTypeByName("Planet")) { Logger::getLogger()->debug("Player tried to bombard something illogical"); return true; } Fleet *fleetData = dynamic_cast<Fleet*>(fleet->getObjectBehaviour()); Planet *planetData = dynamic_cast<Planet*>(planetObj->getObjectBehaviour()); char planetShipTech = PlayerInfo::getPlayerInfo(planetData->getOwner()).getShipTechLevel(); double attack = fleetData->getAttack(); planetData->removeResource("Industry", static_cast<uint32_t>(attack * INDUSTRY_DMG * (1+rand->getInRange(-2,2)/100.) )); planetData->removeResource("Social Environment", static_cast<uint32_t>(attack * SOCIAL_DMG * (1+rand->getInRange(-2,2)/100.) )); planetData->removeResource("Planetary Environment", static_cast<uint32_t>(attack * PLANETARY_DMG * (1+rand->getInRange(-2,2)/100.) )); planetData->removeResource(string("PDB") + planetShipTech, static_cast<uint32_t>(attack * PDB_DMG / 10. * (1+rand->getInRange(-2,2)/100.) )); PlayerInfo::getPlayerInfo(fleetData->getOwner()).addVictoryPoints(VICTORY_POINTS); // PDBs counter-attack at: PDB level * 6 * numPDBs fleetData->takeDamage( (planetShipTech - '0') * 6 * planetData->getResource(string("PDB") + planetShipTech).first); Message *msg = new Message(); msg->setSubject("Bombard complete"); string body = "Fleet \"" + fleet->getName() + "\" bombarded " + planetObj->getName(); msg->setBody(PlayerInfo::appAllVictoryPoints(body)); msg->addReference(rst_Action_Order, rsorav_Completion); msg->addReference(rst_Object, fleet->getID()); msg->addReference(rst_Object, planetObj->getID()); game->getPlayerManager()->getPlayer(fleetData->getOwner())->postToBoard(msg); game->getPlayerManager()->getPlayer(planetData->getOwner())->postToBoard(msg); return true; }
//System cannot be destroyed, occupied, or colonized in order to be able to be colonized. //It also must be inhabitable for the colony type. bool StarSystem::canBeColonized(bool mining) { ObjectManager* obm = Game::getGame()->getObjectManager(); ObjectTypeManager* obtm = Game::getGame()->getObjectTypeManager(); if(isDestroyed()) { Logger::getLogger()->debug("StarSystem->canBeColonized: System is destroyed"); return false; } //Grab the children and see if the system is occupied set<uint32_t> children = obj->getContainedObjects(); uint32_t pid; bool isOccupied = false; for(set<uint32_t>::iterator i = children.begin(); i != children.end(); i++) { if(obm->getObject(*i)->getType() == obtm->getObjectTypeByName("Planet")) { pid = *i; } else if (obm->getObject(*i)->getType() == obtm->getObjectTypeByName("Fleet")) { isOccupied = true; } } if(isOccupied) { Logger::getLogger()->debug("StarSystem->canBeColonized: System is occupied"); return false; } //Check to see if the system has already been colonized Planet* planet = (Planet*)(obm->getObject(pid)->getObjectBehaviour()); if(planet->getResource(4) > 0) { Logger::getLogger()->debug("StarSystem->canBeColonized: System has been colonized by merchants."); return false; } else if(planet->getResource(5) > 0) { Logger::getLogger()->debug("StarSystem->canBeColonized: System has been colonized by scientists."); return false; } else if(planet->getResource(6) > 0) { Logger::getLogger()->debug("StarSystem->canBeColonized: System has been colonized by settlers."); return false; } else if(planet->getResource(7) > 0) { Logger::getLogger()->debug("StarSystem->canBeColonized: System has been colonized by mining robots."); return false; } //Check to make sure the system is inhabitable by the fleet if(mining && planet->getResource(1) < 1) { Logger::getLogger()->debug("StarSystem->canBeColonized: System is inhabitable and cannot be colonized by mining robots"); return false; } else if(!mining && planet->getResource(1) > 0) { Logger::getLogger()->debug("StarSystem->canBeColonized: System is Uninhabitable and can ONLY be colonized by mining robots"); return false; } return true; }
void taeRuleset::createGame() { Game* game = Game::getGame(); ObjectManager* obm = game->getObjectManager(); ObjectTypeManager* obtm = game->getObjectTypeManager(); //Create ship design category Category* cat = new Category(); cat->setName("Ships"); cat->setDescription("Ship components"); game->getDesignStore()->addCategory(cat); //Create properties createProperties(); //Create components createComponents(); //Create resources setupResources(); uint32_t obT_Universe = obtm->getObjectTypeByName("Universe"); uint32_t obT_Galaxy = obtm->getObjectTypeByName("Galaxy"); //Create the universe IGObject* universe = obm->createNewObject(); obtm->setupObject(universe, obT_Universe); Universe* theUniverse = (Universe*)(universe->getObjectBehaviour()); theUniverse->setSize(1000000000000ll); universe->setName("The Universe"); theUniverse->setPosition(Vector3d(0ll,0ll,0ll)); obm->addObject(universe); //Create the galaxy IGObject* gal = obm->createNewObject(); obtm->setupObject(gal, obT_Galaxy); EmptyObject* galob = (EmptyObject*)(gal->getObjectBehaviour()); galob->setSize(100000000000ll); gal->setName("The Fertile Galaxy"); galob->setPosition(Vector3d(0ll, -6000ll, 0ll)); gal->addToParent(universe->getID()); obm->addObject(gal); string path = string(Settings::getSettings()->get("board_path")); Logger::getLogger()->debug(path.c_str()); createBoard(path, gal->getID()); Logger::getLogger()->info("TaE created"); }
void Colonize::inputFrame(InputFrame::Ptr f, uint32_t playerid) { FleetOrder::inputFrame(f, playerid); Game *game = Game::getGame(); ObjectManager *obm = game->getObjectManager(); ObjectTypeManager *obtm = game->getObjectTypeManager(); IGObject::Ptr starSysObj = obm->getObject(starSys->getObjectId()); StarSystem* starSysData = (StarSystem*) starSysObj->getObjectBehaviour(); // Check to see if it is a legal system to colonize if(starSysObj->getType() == obtm->getObjectTypeByName("Star System") && !starSysData->canBeColonized(isMining)) { starSys->setObjectId(0); Logger::getLogger()->debug("Player tried to colonize a system which cannot be colonized."); } }
void MTSec::initGame() { Game* game = Game::getGame(); MTSecTurn* turn = new MTSecTurn(); game->setTurnProcess(turn); ObjectTypeManager* obdm = game->getObjectTypeManager(); obdm->addNewObjectType( new UniverseType()); obdm->addNewObjectType( new EmptyObjectType( "Galaxy", "The Galaxy Object type") ); obdm->addNewObjectType( new EmptyObjectType( "Star System","The Star System Object type") ); uint32_t pt = obdm->addNewObjectType( new PlanetType()); uint32_t ft = obdm->addNewObjectType( new FleetType()); turn->setPlanetType(pt); turn->setFleetType(ft); OrderManager* ordm = game->getOrderManager(); ordm->addOrderType(new Nop()); ordm->addOrderType(new Move()); ordm->addOrderType(new BuildFleet()); ordm->addOrderType(new BuildWeapon()); ordm->addOrderType(new Colonise()); ordm->addOrderType(new SplitFleet()); ordm->addOrderType(new MergeFleet()); ordm->addOrderType(new Enhance()); ordm->addOrderType(new SendPoints()); ordm->addOrderType(new LoadArmament()); ordm->addOrderType(new UnloadArmament()); }
void taeRuleset::initGame() { Game* game = Game::getGame(); fleetBuilder = new FleetBuilder(); //Setup turns TaeTurn* turn = new TaeTurn(fleetBuilder); game->setTurnProcess(turn); //Add universe object type ObjectTypeManager* obtm = game->getObjectTypeManager(); obtm->addNewObjectType(new UniverseType()); //Add Galaxy object type EmptyObjectType * eo = new EmptyObjectType(); eo->setTypeName("Galaxy"); eo->setTypeDescription("The Galaxy Object type"); obtm->addNewObjectType(eo); //Add Solar system object type obtm->addNewObjectType(new StarSystemType()); //Add Planet object type uint32_t pt = obtm->addNewObjectType(new PlanetType()); turn->setPlanetType(pt); //Add Fleet object type uint32_t ft = obtm->addNewObjectType(new FleetType()); turn->setFleetType(ft); //Set Order types OrderManager* orm = game->getOrderManager(); orm->addOrderType(new Colonize(true)); orm->addOrderType(new Colonize(false)); orm->addOrderType(new Move()); orm->addOrderType(new Attack()); orm->addOrderType(new Reinforce()); Logger::getLogger()->info("TaE initialised"); }
//Setup the next turn to be ready for combat void TaeTurn::initCombat() { std::set<uint32_t>::iterator itcurr; Game* game = Game::getGame(); ObjectManager* objectmanager = game->getObjectManager(); PlayerManager::Ptr playermanager = game->getPlayerManager(); ObjectTypeManager* obtm = game->getObjectTypeManager(); DesignStore::Ptr ds = game->getDesignStore(); //Pop the combat queue and set the combatants pair<bool, map<uint32_t, uint32_t> > temp; temp = combatQueue.front(); combatQueue.pop(); isInternal = temp.first; combatants = temp.second; strength.clear(); for(map<uint32_t, uint32_t>::iterator i = combatants.begin(); i != combatants.end(); ++i) { IGObject::Ptr ob = Game::getGame()->getObjectManager()->getObject(i->first); Fleet* f = (Fleet*) ob->getObjectBehaviour(); strength[f->getOwner()] = 0; } set<uint32_t> owners; set<uint32_t> regions; string shipType; for(map<uint32_t, uint32_t>::iterator i = combatants.begin(); i != combatants.end(); ++i) { IGObject::Ptr ob = objectmanager->getObject(i->first); Fleet* leader = (Fleet*) (ob)->getObjectBehaviour(); //look for the shiptype which this combat is associated with if(shipType.empty()) { if(isInternal) { shipType = "ScientistShip"; } else { //Set shiptype to the type corresponding to the leader uint32_t ship = leader->getShips().begin()->first; shipType = ds->getDesign(ship)->getName(); size_t pos = shipType.find("Leader"); if(pos != shipType.npos) { shipType.erase(pos, 6); } } } owners.insert(leader->getOwner()); //Set which regions are involved in combat if(regions.count(i->second) <= 0) { regions.insert(i->second); } //Set initial internal combat strength if(isInternal) { IGObject::Ptr starSys = objectmanager->getObject(ob->getParent()); StarSystem* starSysData = (StarSystem*)(starSys->getObjectBehaviour()); Vector3d pos = starSysData->getPosition(); //Search for bordering science colonies //east-west neighbors for(int i = -1; i < 2; i+=2) { set<uint32_t> ids = objectmanager->getObjectsByPos(pos+Vector3d(80000*i,0,0), 1); for(set<uint32_t>::iterator j=ids.begin(); j != ids.end(); j++) { IGObject::Ptr tempObj = objectmanager->getObject(*j); if(tempObj->getType() == obtm->getObjectTypeByName("Planet")) { Planet* p = (Planet*)(tempObj->getObjectBehaviour()); if(p->getResource(5) > 0) { addReinforcement(leader->getOwner()); } } } } //north-south neighbors for(int i = -1; i < 2; i+=2) { set<uint32_t> ids = objectmanager->getObjectsByPos(pos+Vector3d(0,80000*i,0), 1); for(set<uint32_t>::iterator j=ids.begin(); j != ids.end(); j++) { IGObject::Ptr tempObj = objectmanager->getObject(*j); if(tempObj->getType() == obtm->getObjectTypeByName("Planet")) { Planet* p = (Planet*)(tempObj->getObjectBehaviour()); if(p->getResource(5) > 0) { addReinforcement(leader->getOwner()); } } } } } } //Set which resource will be awarded int resourceType; if(shipType.compare("MerchantShip") == 0) { resourceType = 4; } else if(shipType.compare("ScientistShip") == 0) { resourceType = 5; } else if(shipType.compare("SettlerShip") == 0) { resourceType = 6; } else { resourceType = 7; } //Set all fleets to combat mode. Flag the fleets whose owners are //directly involved in combat. std::set<uint32_t> views; std::set<uint32_t> objects = objectmanager->getAllIds(); 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(); f->toggleCombat(); //check to see if this fleet is a combatant if(owners.count(f->getOwner()) > 0) { uint32_t ship = f->getShips().begin()->first; string name = ds->getDesign(ship)->getName(); if(name.compare(shipType) == 0) { f->setCombatant(true); } } //Set visibility views.insert(ob->getID()); } //Set initial external combat strength else if(ob->getType() == obtm->getObjectTypeByName("Planet") && !isInternal) { Planet* p = (Planet*) ob->getObjectBehaviour(); StarSystem* sys = (StarSystem*) objectmanager->getObject(ob->getParent())->getObjectBehaviour(); if(regions.count(sys->getRegion()) > 0) { if(p->getResource(resourceType) > 0) { for(map<uint32_t, uint32_t>::iterator i = combatants.begin(); i != combatants.end(); ++i) { if(i->second == sys->getRegion()) { Fleet* leader = (Fleet*) (objectmanager->getObject(i->first))->getObjectBehaviour(); addReinforcement(leader->getOwner()); } } } } } } //Send message to players letting them know about combat Message::Ptr msg( new Message() ); msg->setSubject("COMBAT!"); stringstream out; out << "The next turn is an "; if(isInternal) { out << "INTERNAL "; } else { out << "EXTERNAL "; } out << "combat turn! Combatants are: "; out << playermanager->getPlayer(*owners.begin())->getName(); out << " with an initial strength of "; out << strength[*owners.begin()] << " and "; out << playermanager->getPlayer(*owners.end())->getName(); out << " with an initial strength of "; out << strength[*owners.end()]; msg->setBody(out.str()); std::set<uint32_t> players = playermanager->getAllIds(); for(itcurr = players.begin(); itcurr != players.end(); ++itcurr) { Player::Ptr player = playermanager->getPlayer(*itcurr); player->postToBoard(Message::Ptr( new Message(*msg))); player->getPlayerView()->addVisibleObjects( views ); } }
//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; }
void taeRuleset::createBoard(string path, uint32_t galaxy) { Game* game = Game::getGame(); ObjectManager* obm = game->getObjectManager(); ObjectTypeManager* obtm = game->getObjectTypeManager(); stack<string> lines; string line; int row = 0; ifstream file (path.c_str()); //Attempt to read in a custom map if (file.is_open()) { while(!file.eof()) { getline (file,line); lines.push(line); } if(lines.top().compare("") == 0) { lines.pop(); } file.close(); } else { //Build default map lines.push(string("....MMMMM.A.M...")); lines.push(string(".A..M.......M..A")); lines.push(string("...MMA......MM..")); lines.push(string("MMMM.........MMM")); lines.push(string(".............AMM")); lines.push(string("..............M.")); lines.push(string("MMMM....A...MMM.")); lines.push(string(".A.MMMM.....M...")); lines.push(string("......MMMMMMM.A.")); lines.push(string(".....A..........")); lines.push(string("..........A.....")); } while(!lines.empty()) { line = lines.top(); lines.pop(); for(int i = 0; i < line.length(); i++) { //Create a star system IGObject* sys1 = obm->createNewObject(); obtm->setupObject(sys1, obtm->getObjectTypeByName("Star System")); StarSystem* sys1ob = (StarSystem*)(sys1->getObjectBehaviour()); sys1ob->setSize(60000ll); char* name = new char[8]; sprintf(name, "%d, %d", i, row); sys1->setName(name); sys1ob->setPosition(Vector3d(1ll + 80000ll*i, 1ll+80000ll*(row+1), 0ll)); sys1ob->setDestroyed(false); sys1->addToParent(galaxy); obm->addObject(sys1); //Create a planet IGObject *p = obm->createNewObject(); obtm->setupObject(p, obtm->getObjectTypeByName("Planet")); Planet * pob = (Planet*)(p->getObjectBehaviour()); pob->setSize(2); char* planetName = new char[20]; sprintf(planetName, "Planet %d, %d", i, row); p->setName(planetName); pob->setPosition(sys1ob->getPosition()); OrderQueue *planetoq = new OrderQueue(); planetoq->setObjectId(p->getID()); planetoq->addOwner(0); game->getOrderManager()->addOrderQueue(planetoq); OrderQueueObjectParam* oqop = static_cast<OrderQueueObjectParam*>(p->getParameterByType(obpT_Order_Queue)); oqop->setQueueId(planetoq->getQueueId()); pob->setDefaultOrderTypes(); p->addToParent(sys1->getID()); obm->addObject(p); if(line[i] == '.') { sys1ob->setInhabitable(true); } else if (line[i] == 'M') { sys1ob->setInhabitable(false); pob->addResource(1,1); } else if (line[i] == 'A') { sys1ob->setInhabitable(true); sys1ob->setRegion(sys1->getID()); pob->addResource(3,1); pob->addResource(5,1); } } row++; } }
//Recurrsively rebuilds regions given a system. //If the system already has a region or is not occupied or //colonized, then this function returns. Otherwise it //sets the region based on its neighbors. void TaeTurn::rebuildRegion(uint32_t system) { Game* game = Game::getGame(); ObjectManager* obm = game->getObjectManager(); ObjectTypeManager* obtm = game->getObjectTypeManager(); set<uint32_t>::iterator itcurr; IGObject::Ptr sys = obm->getObject(system); StarSystem* sysData = (StarSystem*) sys->getObjectBehaviour(); //Check to make sure the system doesnt already have a region if(sysData->getRegion() != 0) { return; } //Check to make sure it is colonized or occupied set<uint32_t> children = sys->getContainedObjects(); for(itcurr = children.begin(); itcurr != children.end(); itcurr++) { IGObject::Ptr ob = obm->getObject(*itcurr); bool resource = true; bool occupied = false; if(ob->getType() == obtm->getObjectTypeByName("Planet")) { Planet* p = (Planet*) ob->getObjectBehaviour(); if(p->getResource(4) == 0 && p->getResource(5) == 0 && p->getResource(6) == 0 && p->getResource(7) == 0) { resource = false; } } else if(ob->getType() == obtm->getObjectTypeByName("Fleet")) { if(!Move::isBorderingScienceColony(sysData)) { sendHome(*itcurr); occupied = false; } else { occupied = true; } } if(!(resource || occupied)) { return; } } //Check to make sure this isnt a home system if(sys->getName().find("'s System") != string::npos) { return; } //Get neighbors set<uint32_t> regions; set<uint32_t> emptyNeighbors; Vector3d pos = sysData->getPosition(); //east-west neighbors for(int i = -1; i < 2; i+=2) { set<uint32_t> ids = obm->getObjectsByPos(pos+Vector3d(80000*i,0,0), 1); for(set<uint32_t>::iterator j=ids.begin(); j != ids.end(); j++) { IGObject::Ptr tempObj = obm->getObject(*j); if(tempObj->getType() == obtm->getObjectTypeByName("Star System")) { uint32_t r = ((StarSystem*)(tempObj->getObjectBehaviour()))->getRegion(); if(r == 0) { emptyNeighbors.insert(*j); } else if (regions.count(r) == 0) { regions.insert(r); } } } } //north-south neighbors for(int i = -1; i < 2; i+=2) { set<uint32_t> ids = obm->getObjectsByPos(pos+Vector3d(0,80000*i,0), 1); for(set<uint32_t>::iterator j=ids.begin(); j != ids.end(); j++) { IGObject::Ptr tempObj = obm->getObject(*j); if(tempObj->getType() == obtm->getObjectTypeByName("Star System")) { uint32_t r = ((StarSystem*)(tempObj->getObjectBehaviour()))->getRegion(); if(r == 0) { emptyNeighbors.insert(*j); } else if (regions.count(r) == 0) { regions.insert(r); } } } } //Set Region if(regions.size() == 0) { //If neighbors have no region, then create a new region sysData->setRegion(sys->getID()); stringstream out; out << sysData->getRegion(); Logger::getLogger()->debug(string("System " + sys->getName() + " added to region " + out.str()).c_str()); } else if(regions.size() == 1) { //If it neighbors 1 region, then set as that region sysData->setRegion(*(regions.begin())); stringstream out; out << sysData->getRegion(); Logger::getLogger()->debug(string("System " + sys->getName() + " added to region " + out.str()).c_str()); } else { //It should never border more than one region. If it reaches this point, then there is an error somewhere Logger::getLogger()->debug(string("** Unable to rebuild region! System, " + sys->getName() + " is bordering more than one region! ***").c_str()); } //Recurse on neighbors for(itcurr = emptyNeighbors.begin(); itcurr != emptyNeighbors.end(); itcurr++) { rebuildRegion(*itcurr); } }
void taeRuleset::onPlayerAdded(Player* player) { Logger::getLogger()->debug("TaE onPlayerAdded"); Game *game = Game::getGame(); PlayerView* playerview = player->getPlayerView(); ObjectTypeManager* obtm = game->getObjectTypeManager(); //Set Player's score to 0 player->setScore(1,0); player->setScore(2,0); player->setScore(3,0); player->setScore(4,0); player->setScore(5,0); //Set visibility of current designs std::set<uint32_t> allotherdesigns = Game::getGame()->getDesignStore()->getDesignIds(); for(std::set<uint32_t>::const_iterator desid = allotherdesigns.begin(); desid != allotherdesigns.end(); ++desid){ DesignView* dv = new DesignView(); dv->setDesignId(*desid); playerview->addVisibleDesign(dv); } for(uint32_t i = 1; i <= game->getDesignStore()->getMaxComponentId(); ++i){ playerview->addUsableComponent(i); } std::set<uint32_t> mydesignids; //Add system and planet to hold player's fleets IGObject* sys1 = game->getObjectManager()->createNewObject(); obtm->setupObject(sys1, obtm->getObjectTypeByName("Star System")); StarSystem* sys1ob = (StarSystem*)(sys1->getObjectBehaviour()); sys1ob->setSize(60000ll); sys1->setName(player->getName() + "'s System"); sys1ob->setPosition(Vector3d(1ll+80000*player->getID(), 1ll, 0ll)); sys1ob->setInhabitable(true); sys1ob->setDestroyed(true); sys1->addToParent(1); game->getObjectManager()->addObject(sys1); IGObject *p = game->getObjectManager()->createNewObject(); obtm->setupObject(p, obtm->getObjectTypeByName("Planet")); Planet * pob = (Planet*)(p->getObjectBehaviour()); pob->setSize(2); p->setName(player->getName() + "'s Home Planet"); pob->setPosition(sys1ob->getPosition()); OrderQueue *planetoq = new OrderQueue(); planetoq->setObjectId(p->getID()); planetoq->addOwner(player->getID()); game->getOrderManager()->addOrderQueue(planetoq); OrderQueueObjectParam* oqop = static_cast<OrderQueueObjectParam*>(p->getParameterByType(obpT_Order_Queue)); oqop->setQueueId(planetoq->getQueueId()); pob->setDefaultOrderTypes(); p->addToParent(sys1->getID()); game->getObjectManager()->addObject(p); //Setup starting fleets IGObject* fleet; //Colonist fleets for(int i = 0; i < 6; i++) { fleet = fleetBuilder->createFleet(FleetBuilder::PASSENGER_FLEET, FleetBuilder::RANDOM_SHIP, player, p, "Colonist Fleet"); game->getObjectManager()->addObject(fleet); } //Leader fleets fleet = fleetBuilder->createFleet(FleetBuilder::VIP_FLEET, FleetBuilder::MERCHANT_SHIP, player, p, "Merchant Leader"); game->getObjectManager()->addObject(fleet); fleet = fleetBuilder->createFleet(FleetBuilder::VIP_FLEET, FleetBuilder::SCIENTIST_SHIP, player, p, "Scientist Leader"); game->getObjectManager()->addObject(fleet); fleet = fleetBuilder->createFleet(FleetBuilder::VIP_FLEET, FleetBuilder::SETTLER_SHIP, player, p, "Settler Leader"); game->getObjectManager()->addObject(fleet); fleet = fleetBuilder->createFleet(FleetBuilder::VIP_FLEET, FleetBuilder::MINING_SHIP, player, p, "Mining Leader"); game->getObjectManager()->addObject(fleet); //Bomber fleets for(int i = 0; i < 2; i++) { fleet = fleetBuilder->createFleet(FleetBuilder::BOMBER_FLEET, NULL, player, p, "Bomber"); game->getObjectManager()->addObject(fleet); } //Set visibility of new designs and objects std::set<uint32_t> objids = game->getObjectManager()->getAllIds(); for(std::set<uint32_t>::iterator itcurr = objids.begin(); itcurr != objids.end(); ++itcurr){ ObjectView* obv = new ObjectView(); obv->setObjectId(*itcurr); obv->setCompletelyVisible(true); playerview->addVisibleObject(obv); } std::set<uint32_t> playerids = game->getPlayerManager()->getAllIds(); for(std::set<uint32_t>::iterator playerit = playerids.begin(); playerit != playerids.end(); ++playerit){ Player* oplayer = game->getPlayerManager()->getPlayer(*playerit); game->getPlayerManager()->updatePlayer(oplayer->getID()); } }
//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(); }