Example #1
0
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;
}
Example #2
0
//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;
}
Example #3
0
bool Build::doOrder(IGObject::Ptr ob)
{
  
  Planet* planet = static_cast<Planet*>(ob->getObjectBehaviour());

  uint32_t usedshipres = resources[1];
  
  if(usedshipres == 0)
    return true;

  int ownerid = planet->getOwner();
  if(ownerid == 0){
      //currently not owned by anyone, just forget about it
      return true;
  }
  
  planet->addResource(1, 1);
    
  if(planet->removeResource(1, usedshipres)){
    //create fleet
    
    Game* game = Game::getGame();
    
    
    IGObject::Ptr fleet = game->getObjectManager()->createNewObject();
    game->getObjectTypeManager()->setupObject(fleet, game->getObjectTypeManager()->getObjectTypeByName("Fleet"));
    
    //add fleet to container
    fleet->addToParent(ob->getID());
    
    fleet->setName(fleetname->getString().c_str());
    
    Fleet * thefleet = ((Fleet*)(fleet->getObjectBehaviour()));
    
    thefleet->setSize(2);
    thefleet->setOwner(ownerid); // set ownerid
    thefleet->setPosition(planet->getPosition());
    thefleet->setVelocity(Vector3d(0LL, 0ll, 0ll));
    
    uint32_t queueid = Game::getGame()->getOrderManager()->addOrderQueue(fleet->getID(), ownerid);
    OrderQueueObjectParam* oqop = static_cast<OrderQueueObjectParam*>(fleet->getParameterByType(obpT_Order_Queue));
    oqop->setQueueId(queueid);
    thefleet->setDefaultOrderTypes();
    thefleet->setIcon("common/object-icons/ship");
    thefleet->setMedia("common-2d/foreign/vegastrike/ship-small/" + ((MiniSec*)(game->getRuleset()))->getFleetMediaNames()->getName());
    
    //set ship type
    IdMap fleettype = fleetlist->getList();
    for(IdMap::iterator itcurr = fleettype.begin(); itcurr != fleettype.end(); ++itcurr){
      thefleet->addShips(itcurr->first, itcurr->second);
      Design::Ptr design = Game::getGame()->getDesignStore()->getDesign(itcurr->first);
        design->addComplete(itcurr->second);
        Game::getGame()->getDesignStore()->designCountsUpdated(design);
    }
    //add fleet to universe
    Game::getGame()->getObjectManager()->addObject(fleet);
    Game::getGame()->getPlayerManager()->getPlayer(ownerid)->getPlayerView()->addOwnedObject(fleet->getID());

    Message::Ptr msg( new Message() );
    msg->setSubject("Build Fleet order complete");
    msg->setBody(std::string("The construction of your new fleet \"") + fleetname->getString() + "\" is complete.");
    msg->addReference(rst_Action_Order, rsorav_Completion);
    msg->addReference(rst_Object, fleet->getID());
    msg->addReference(rst_Object, ob->getID());
 
    Game::getGame()->getPlayerManager()->getPlayer(ownerid)->postToBoard(msg);

    return true;
  }
  return false;
}
Example #4
0
bool Colonize::doOrder(IGObject::Ptr obj) {
   bool result = true;
   --turns;

   Planet* origin = dynamic_cast<Planet*>(obj->getObjectBehaviour());
   assert(origin);
   Logger::getLogger()->debug("Starting a Colonize::doOrder on %s.",origin->getName().c_str());
   
   //Get the list of objects and the # of units to colonize
   IdMap list = targetPlanet->getList();
   
   //Collect all of the players bids and restrain them to 1 less than the current units
   map<IGObject::Ptr ,uint32_t> bids;
   for(IdMap::iterator i = list.begin(); i != list.end(); ++i) {
      uint32_t planetID = i->first;
      uint32_t numUnits = i->second;
      IGObject::Ptr target = Game::getGame()->getObjectManager()->getObject(planetID);     
      
      //Restrain the number of units moved off of origin
      uint32_t maxUnits = origin->getResource("Army").first;
      numUnits = numUnits + bids[target];             //Add current bid on target to requests units (no tricksy bidding!)
      if ( numUnits >= maxUnits && maxUnits > 0) {
         if ( maxUnits > 0)
            numUnits = maxUnits - 1;
         else
            numUnits = 0;
      }
      
      bids[target] = numUnits;
   }
   
   //for each seperate planet bid on run the bid routine
   for(map<IGObject::Ptr ,uint32_t>::iterator i = bids.begin(); i != bids.end(); ++i) {
      Logger::getLogger()->debug("\tStarting to iterate over all players bids");
      
      Planet* biddedPlanet = dynamic_cast<Planet*>(i->first->getObjectBehaviour());
      assert(biddedPlanet);
      
      //Ensure the object IS a planet and the object is unowned
      //The object MAY be owned if a bid has occured and a winner was chosen
      //then all other bids on that planet will simply be ignored
      if ( biddedPlanet != NULL && biddedPlanet->getOwner() == 0) {
         Logger::getLogger()->debug("\tGetting the top player and bid for planet %s",biddedPlanet->getName().c_str());
        
        //Get pair <owner's planet,bid> of top bidder
         pair<IGObject::Ptr ,uint32_t> topBidder = getTopPlayerAndBid(i->first);

         //Check if players bid is bigger than top bidder elsewhere - only did this because I wasn't sure
         //BUG: this code would force the reset of bid restriction
         // if ( i->second > topBidder.second ) {
         //    topBidder.second = i->second;
         //    topBidder.first = obj;
         // }
        
         Planet* ownerPlanet = dynamic_cast<Planet*>(topBidder.first->getObjectBehaviour());
         assert(ownerPlanet);
         
         uint32_t player = ownerPlanet->getOwner(); 
         
         biddedPlanet->setOwner(player);
         biddedPlanet->addResource("Army",topBidder.second);
         ownerPlanet->removeResource("Army",topBidder.second);
         
         //Inform colonize winner, and obj owner
      }
      else //Bidded planet is now owned
      {
         Logger::getLogger()->debug("\tNot getting top bidders, planet is already owned");
         //inform player bid failed, obj is now owned by _person_, won bid with _#units_
      }
   }
   
   return result;
}
Example #5
0
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++;
    } 
}