void ConfigFile::processVcam(const xmlpp::Node* node,Vcam &vcam){
	xmlpp::Node::NodeList list = node->get_children();
	for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
		xmlpp::Node* child=dynamic_cast<const xmlpp::Node*>(*iter);

		if(child->get_name()=="resw")
			extractIntChar(child,vcam.resw);
		else if(child->get_name()=="resh")
			extractIntChar(child,vcam.resh);
		else if(child->get_name()=="position")
			extractPositionOrColor(child,vcam.position);
		else if(child->get_name()=="relativeTo")
			extractStringChar(child,vcam.linkName);
		else if(child->get_name()=="orientation")
			extractOrientation(child,vcam.orientation);
		else if(child->get_name()=="name")
			extractStringChar(child,vcam.name);
		else if(child->get_name()=="baseline"){
			extractFloatChar(child,vcam.baseLine);
		} else if(child->get_name()=="frameId"){
			extractStringChar(child,vcam.frameId);
		} else if(child->get_name()=="parameters"){
			vcam.parameters.reset(new Parameters());
			processParameters(child,vcam.parameters.get());
		} else if(child->get_name()=="showpath")
			extractFloatChar(child,vcam.showpath);
	}

}
void ConfigFile::processObject(const xmlpp::Node* node,Object &object){
	xmlpp::Node::NodeList list = node->get_children();
	for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
		xmlpp::Node* child=dynamic_cast<const xmlpp::Node*>(*iter);

		if(child->get_name()=="name")
			extractStringChar(child,object.name);
		else if(child->get_name()=="file")
			extractStringChar(child,object.file);
		else if(child->get_name()=="position")
			extractPositionOrColor(child,object.position);
		else if(child->get_name()=="orientation")
			extractOrientation(child,object.orientation);
		else if(child->get_name()=="offsetp")
			extractPositionOrColor(child,object.offsetp);
		else if(child->get_name()=="offsetr")
			extractPositionOrColor(child,object.offsetr);
		else if(child->get_name()=="physics"){
                        object.physicProperties.reset(new PhysicProperties);
			object.physicProperties->init();
			processPhysicProperties(child,*object.physicProperties);

		}

	}
}
void ConfigFile::processDVLSensor(const xmlpp::Node* node, XMLDVLSensor &s){
	xmlpp::Node::NodeList list = node->get_children();
	for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
		xmlpp::Node* child=dynamic_cast<const xmlpp::Node*>(*iter);

		if(child->get_name()=="position")
			extractPositionOrColor(child,s.position);
		else if(child->get_name()=="relativeTo")
			extractStringChar(child,s.linkName);
		else if(child->get_name()=="orientation")
			extractOrientation(child,s.orientation);
		else if(child->get_name()=="name")
			extractStringChar(child,s.name);
		else if(child->get_name()=="std")
			extractFloatChar(child,s.std);
	}
}
void ConfigFile::processMultibeamSensor(const xmlpp::Node* node, XMLMultibeamSensor &MB){
	xmlpp::Node::NodeList list = node->get_children();
	for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
		xmlpp::Node* child=dynamic_cast<const xmlpp::Node*>(*iter);

		if(child->get_name()=="position")
			extractPositionOrColor(child,MB.position);
		else if(child->get_name()=="relativeTo")
			extractStringChar(child,MB.linkName);
		else if(child->get_name()=="orientation")
			extractOrientation(child,MB.orientation);
		else if(child->get_name()=="name")
			extractStringChar(child,MB.name);
		else if(child->get_name()=="initAngle")
			extractFloatChar(child,MB.initAngle);
		else if(child->get_name()=="finalAngle")
			extractFloatChar(child,MB.finalAngle);
		else if(child->get_name()=="angleIncr")
			extractFloatChar(child,MB.angleIncr);
		else if(child->get_name()=="range")
			extractFloatChar(child,MB.range);
	}
}
void ConfigFile::processVehicle(const xmlpp::Node* node,Vehicle &vehicle){
	xmlpp::Node::NodeList list = node->get_children();
	for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
		xmlpp::Node* child=dynamic_cast<const xmlpp::Node*>(*iter);

		if(child->get_name()=="name")
			extractStringChar(child,vehicle.name);
		else if(child->get_name()=="file"){
			string aux;
			extractStringChar(child,aux);
			processURDFFile(aux,vehicle);
		}
		else if(child->get_name()=="position")
			extractPositionOrColor(child,vehicle.position);
		else if(child->get_name()=="orientation")
			extractOrientation(child,vehicle.orientation);
		else if (child->get_name()=="jointValues")
			processJointValues(child,vehicle.jointValues,vehicle.ninitJoints);
		else if (child->get_name()=="virtualCamera"){
			Vcam aux;
			aux.init();
			processVcam(child,aux);
			vehicle.Vcams.push_back(aux);
		}
		else if (child->get_name()=="virtualRangeImage"){
			Vcam aux;
			aux.init();
			aux.range=1;
			processVcam(child,aux);
			vehicle.VRangecams.push_back(aux);
		} else if (child->get_name()=="rangeSensor"){
			rangeSensor aux;
			aux.init();
			processRangeSensor(child,aux);
			vehicle.range_sensors.push_back(aux);
		} else if (child->get_name()=="objectPicker"){
			rangeSensor aux;
			aux.init();
			processRangeSensor(child,aux);
			vehicle.object_pickers.push_back(aux);
		} else if (child->get_name()=="imu"){
			Imu aux;
			aux.init();
			processImu(child,aux);
			vehicle.imus.push_back(aux);
		} else if (child->get_name()=="pressureSensor"){
			XMLPressureSensor aux;
			aux.init();
			processPressureSensor(child,aux);
			vehicle.pressure_sensors.push_back(aux);
		} else if (child->get_name()=="gpsSensor"){
			XMLGPSSensor aux;
			aux.init();
			processGPSSensor(child,aux);
			vehicle.gps_sensors.push_back(aux);
		} else if (child->get_name()=="dvlSensor"){
			XMLDVLSensor aux;
			aux.init();
			processDVLSensor(child,aux);
			vehicle.dvl_sensors.push_back(aux);
		} else if (child->get_name()=="multibeamSensor"){
			XMLMultibeamSensor aux;
			aux.init();
			processMultibeamSensor(child,aux);
			vehicle.multibeam_sensors.push_back(aux);
		}
	}
}
Beispiel #6
0
void AIPlayer::cycle()
{
  // If no falling flobo, no need to play
  if (attachedGame->getFallingFlobo() == NULL || !attachedGame->isGameRunning()) 
  {
    return;
  }
  
  int currentLine = attachedGame->getFallingY();
  int currentColumn = attachedGame->getFallingX();
  int currentNumberOfBadFlobos = attachedGame->getNeutralFlobos();
  int currentTotalNumberOfBadFlobos = attachedGame->getGameTotalNeutralFlobos();
    
  // If we start with new flobos or restart because new bad Flobos came
  if (attachedGame->isPhaseReady())
  {
    //fprintf(stderr, "Thinking\n");
    // Reset the cycle counter
    currentCycle = 0;
    // Save we did make any decision yet
    decisionMade = 0;
    // Should not decide again if we didn't decide yet !
    shouldRedecide = false;
    lastNumberOfBadFlobos = currentNumberOfBadFlobos;
    totalNumberOfBadFlobos = currentTotalNumberOfBadFlobos;
  } else {
      // If we did not start, test if we should restart because new bad flobos arrived
      if (currentNumberOfBadFlobos != lastNumberOfBadFlobos) {
          //fprintf(stderr, "RE-Thinking\n");
          // Reset the cycle counter
          currentCycle = 0;
          // Save we did make any decision yet
          decisionMade = 0;
          // Should not decide again if we didn't decide yet !
          shouldRedecide = true;
          lastNumberOfBadFlobos = currentNumberOfBadFlobos;
          totalNumberOfBadFlobos = currentTotalNumberOfBadFlobos;
      }
  }



  
  // increment the cycle counter
  currentCycle++;
  
  // Test if we have to decide where to play
  if (decisionMade < DISPATCHCYCLES)
  {
    // if so update the internal grid the first time
    if (decisionMade == 0 && !shouldRedecide) extractGrid();

    // then start to think
    decide(decisionMade, params.thinkDepth);
 
    // remember what we decided
    decisionMade++;
    
    // don't drop yet!!
    readyToDrop = false;
    
    // Do not move until we are sure
    //if (decisionMade < DISPATCHCYCLES) return;
      //if (decisionMade == DISPATCHCYCLES) fprintf(stderr, "Thought\n");
  }

  // Now move to the position we decided :

  // If we can drop, then go on
  if (readyToDrop)
  {
    if (internalGrid == NULL) extractGrid();
    if ((FLOBOBAN_DIMY-1-currentLine) - ((* internalGrid)[currentColumn][HEIGHTS_ROW] + (objective.orientation == Below)?1:0) <= params.fastDropDelta) targetView.cycleGame();
  }
  
  // Else try to move at the specified frequency
  else if ((currentCycle % params.speedFactor) == 0)
  {
    bool shouldMove;
    bool shouldRotate;
    
    bool couldntMove = false;
    bool couldntRotate = false;
  
    int curOrientation = attachedGame->getFallingCompanionDir();

    // should we move or rotate
    if (random()%2 == 0)
    {
      shouldMove = (currentColumn != objective.position.x);
      shouldRotate = (!shouldMove) && (extractOrientation(curOrientation) != objective.orientation);
    } else {
      shouldRotate = (extractOrientation(curOrientation) != objective.orientation);
      shouldMove = (!shouldRotate) && (currentColumn != objective.position.x);
    }
    // if no need to move or rotate we're ready to drop
    readyToDrop = !(shouldMove || shouldRotate);
    
    // Move if useful
    if (shouldMove)
    {
      if (currentColumn < objective.position.x) targetView.moveRight();
      else targetView.moveLeft();

      // or decide again if not possible
      couldntMove = (currentColumn == attachedGame->getFallingX());
    }
    
    // Rotate if useful
    if (shouldRotate)
    {
      if (params.rotationMethod == 0)
      {
        if (rotationMatrix[revertOrientation(objective.orientation)][curOrientation] > 0)
          targetView.rotateRight();
        else
          targetView.rotateLeft();
        
      }
      else if (params.rotationMethod < 0) targetView.rotateRight();
      else if (params.rotationMethod > 0) targetView.rotateLeft();
        
      // or decide again if not possible
      couldntRotate = (curOrientation == attachedGame->getFallingCompanionDir());
    }

    // if need to move but impossible, decide again
      if (couldntMove) {
          decisionMade = 0;
          //fprintf(stderr, "ReThink because couldn't move\n");;
      }
  }
  lastLineSeen = currentLine;
}
Beispiel #7
0
void AIPlayer::decide(int partial, int depth)
{
  //fprintf(stderr, "  Decision %d on %d - Depth = %d\n",partial+1,DISPATCHCYCLES,depth);
  if (partial == 0)
  {
    // get flobo binoms to drop
    FloboState etat;
    etat = attachedGame->getFallingState();
    if (etat == FLOBO_EMPTY) return;
    current.falling     = extractColor(etat);
    etat = attachedGame->getCompanionState();
    if (etat == FLOBO_EMPTY) return;
    current.companion   = extractColor(etat);
    current.orientation = extractOrientation(attachedGame->getFallingCompanionDir());
    current.position.x  = attachedGame->getFallingX();
    current.position.y  = FLOBOBAN_DIMY - attachedGame->getFallingY();
    
    originalFlobo = current;
    
    next.falling        = extractColor(attachedGame->getNextFalling());
    next.companion      = extractColor(attachedGame->getNextCompanion());
    next.orientation    = Left;
    next.position.x     = 0;
    next.position.y     = IA_FLOBOBAN_DIMY+1;

    bestl1=1;
    foundOne = false;
    bestEvaluation = 0;
  }
  
  switch (depth)
  {
    case 1:
      for (unsigned int l1 = 1+partial; l1 <= MAXCOMBINATION; l1+=DISPATCHCYCLES)
      {
        // set position of binom 1
        serialPosition(l1,&current);
        
        // reset evaluation
        GridEvaluation evaluation1 = nullEvaluation;
        
        GridState state1;
        
        // drop the binom (including destroying eligible groups) and continue if game not lost
        if (canReach(originalFlobo, current, internalGrid, 1) && dropBinom(current, internalGrid, &state1, &evaluation1))
        {
          evalWith(&state1, &nullEvaluation, &evaluation1);
          
          if (foundOne == false || selectIfBetterEvaluation(&bestEvaluation, &evaluation1, current, &state1))
          {
            bestl1 = l1;
          }
          foundOne = true;
        }
      }
      if (foundOne) serialPosition(bestl1,&current);
      objective = current;
      break;
      
    case 2:
      for (unsigned int l1 = 1+partial; l1 <= MAXCOMBINATION; l1+=DISPATCHCYCLES)
      {
        // set position of binom 1
        serialPosition(l1,&current);
        
        // reset evaluation
        GridEvaluation evaluation1 = nullEvaluation;
        
        GridState state1;
        
        // drop the binom (including destroying eligible groups) and continue if game not lost
        if (canReach(originalFlobo, current, internalGrid, 1) && dropBinom(current, internalGrid, &state1, &evaluation1))
        {
          for (unsigned int l2 = 1; l2 <= MAXCOMBINATION; l2++)
          {
            // set position of binom 1
            serialPosition(l2,&next);
            
            // copy evaluation
            GridEvaluation evaluation2 = evaluation1;
            
            GridState state1bis;
            GridState state2;
            
            dropNeutrals(lastNumberOfBadFlobos-evaluation1.floboSuppressed, totalNumberOfBadFlobos, &state1bis, &state1);  
            
            // drop the binom (including destroying eligible groups) and eval board if game not lost
            if (canReach(originalFlobo, next, &state1bis, 1) && dropBinom(next, &state1bis, &state2, &evaluation2))
            {
              evalWith(&state2, &evaluation1, &evaluation2);
              
              if (foundOne == false || selectIfBetterEvaluation(&bestEvaluation, &evaluation2, current, &state2))
              {
                bestl1 = l1;
              }
              foundOne = true;
            }
          }
        }
      }
      if (foundOne) serialPosition(bestl1,&current);
      objective = current;
      break;
      
    default:
      objective.position.x = (random() % IA_FLOBOBAN_DIMX);
      objective.orientation = (FloboOrientation)(random() % 4);
      break;
  }

}