예제 #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;
}
예제 #2
0
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;
}
예제 #3
0
int Move::getETA(IGObject *ob) const{
  Fleet* fleet = ((Fleet*)(ob->getObjectBehaviour()));
  unsigned long long distance = coords->getPosition().getDistance(fleet->getPosition());
  unsigned long max_speed = 3000000;
  
  if(distance == 0) 
    return 1;
  return (int)((distance - 1) / max_speed) + 1;
}
예제 #4
0
bool Move::doOrder(IGObject * ob){
  Vector3d dest = coords->getPosition();
  Fleet* fleet = ((Fleet*)(ob->getObjectBehaviour()));
  unsigned long long distance = dest.getDistance(fleet->getPosition());
  unsigned long long max_speed = 3000000;

  Logger::getLogger()->debug("Moving %lld at %lld speed", distance, max_speed);

  if(distance <= max_speed){
    uint32_t parentid;

    Logger::getLogger()->debug("Object(%d)->Move->doOrder(): Is arriving at [%lld, %lld, %lld] ", 
      ob->getID(), dest.getX(), dest.getY(), dest.getZ());
  
    fleet->setVelocity(Vector3d(0,0,0));
    parentid = ob->getParent();

    // recontainerise if necessary
    int containertype = Game::getGame()->getObjectManager()->getObject(parentid)->getContainerType();
    Game::getGame()->getObjectManager()->doneWithObject(parentid);
  
    if(fleet->getPosition() != dest && containertype >= 1){
      //removeFromParent();
      std::set<unsigned int> oblist = ((MTSecTurn*)(Game::getGame()->getTurnProcess()))->getContainerIds();
      for(std::set<unsigned int>::reverse_iterator itcurr = oblist.rbegin(); itcurr != oblist.rend(); ++itcurr){
        IGObject* testedobject = Game::getGame()->getObjectManager()->getObject(*itcurr);
        
        Position3dObjectParam * pos = dynamic_cast<Position3dObjectParam*>(testedobject->getParameterByType(obpT_Position_3D));
        SizeObjectParam * size = dynamic_cast<SizeObjectParam*>(testedobject->getParameterByType(obpT_Size));
        if(pos == NULL || size == NULL){
          Game::getGame()->getObjectManager()->doneWithObject(*itcurr);
          continue;
        }
        Vector3d pos1 = pos->getPosition();
        uint64_t size1 = size->getSize();
        
        uint64_t diff = dest.getDistance(pos1);
        if(diff <= fleet->getSize() / 2 + size1 / 2){
        
          Logger::getLogger()->debug("Container object %d", *itcurr);
          //if(Game::getGame()->getObject(*itcurr)->getType() <= 2){
          //if(*itcurr != id){
          
          if(size1 >= fleet->getSize()){
            if(*itcurr != parentid){
                ob->removeFromParent();
                ob->addToParent(*itcurr);
            }
            Game::getGame()->getObjectManager()->doneWithObject(*itcurr);
            parentid = *itcurr;
            break;
          }
        }
        Game::getGame()->getObjectManager()->doneWithObject(*itcurr);
        //}
      }
      if(parentid == 0){
          ob->removeFromParent();
          ob->addToParent(0);
      }
    }
    
    fleet->setPosition(dest);
    
    Message * msg = new Message();
    msg->setSubject("Move order complete");
    msg->setBody("The fleet '" +  ob->getName() + "' has reached it's destination.");
    msg->addReference(rst_Action_Order, rsorav_Completion);
    msg->addReference(rst_Object, ob->getID());
    msg->addReference(rst_Object, parentid); /* It's parent */
    Game::getGame()->getPlayerManager()->getPlayer(fleet->getOwner())->postToBoard(msg);

    return true;

  }else{
    Vector3d velo = (dest - fleet->getPosition()).makeLength(max_speed);
    Vector3d arriveat = fleet->getPosition()+velo;
    Logger::getLogger()->debug("Move->doOrder(%d): Velocity is [%lld, %lld, %lld] (will arrive at [%lld, %lld, %lld])", 
      ob->getID(), velo.getX(), velo.getY(), velo.getZ(), arriveat.getX(), arriveat.getY(), arriveat.getZ());

    fleet->setVelocity(velo);

    uint32_t parentid = ob->getParent();
    // recontainerise if necessary
    uint32_t containertype = Game::getGame()->getObjectManager()->getObject(parentid)->getContainerType();
    Game::getGame()->getObjectManager()->doneWithObject(parentid);
  
    if(fleet->getPosition() != arriveat && containertype >= 1){
      //removeFromParent();
      std::set<uint32_t> oblist = ((MTSecTurn*)(Game::getGame()->getTurnProcess()))->getContainerIds();
      for(std::set<uint32_t>::reverse_iterator itcurr = oblist.rbegin(); itcurr != oblist.rend(); ++itcurr){
        IGObject* testedobject = Game::getGame()->getObjectManager()->getObject(*itcurr);
        
        Position3dObjectParam * pos = dynamic_cast<Position3dObjectParam*>(testedobject->getParameterByType(obpT_Position_3D));
        SizeObjectParam * size = dynamic_cast<SizeObjectParam*>(testedobject->getParameterByType(obpT_Size));
        if(pos == NULL || size == NULL){
          Game::getGame()->getObjectManager()->doneWithObject(*itcurr);
          continue;
        }
        Vector3d pos1 = pos->getPosition();
        uint64_t size1 = size->getSize();
        
        uint64_t diff = arriveat.getDistance(pos1);
        if(diff <= fleet->getSize() / 2 + size1 / 2){
        
          Logger::getLogger()->debug("Container object %d", *itcurr);
          //if(Game::getGame()->getObject(*itcurr)->getType() <= 2){
          //if(*itcurr != id){
          
          if(size1 >= fleet->getSize()){
            if(*itcurr != parentid){
                ob->removeFromParent();
                ob->addToParent(*itcurr);
            }
            Game::getGame()->getObjectManager()->doneWithObject(*itcurr);
            parentid = *itcurr;
            break;
          }
        }
        Game::getGame()->getObjectManager()->doneWithObject(*itcurr);
        //}
      }
      if(parentid == 0){
          ob->removeFromParent();
          ob->addToParent(0);
      }
    }
    
    fleet->setPosition(arriveat);
    
    return false;
  }
}
예제 #5
0
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;
}
예제 #6
0
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;
}