Example #1
0
/**
 * fight between units
 * assumes that they are on the same field 
 * returns : 0 - draw, 1 - first won, 2 - second won
 */
static int walka(UnitsList unitFirst, UnitsList unitSecond) {
	if (unitFirst->priority > unitSecond->priority)  {
		removeUnit(unitSecond);
		return 1;
	}
	
	if(unitFirst->priority < unitSecond->priority) {
		 removeUnit(unitFirst);
		 return 2;
	 }
	 
	// both units die 
	removeUnit(unitFirst);
	removeUnit(unitSecond);
	return 0;
}
Example #2
0
void Stage::perform(double dt, Unit* hero)
{

  for(int i = 0; i < units.size(); i++)
    {
      if(!isOffScreen(units[i]))
	{
	  units[i]->increment(dt); //if it's on screen draw it
	}
      else
	{
	  if(units[i]->isBullet())
	    {
	      units[i]->setHealth(0); //if a bullet goes off screen don't draw it
	    }
	}
      if (units[i]->getHealth() <= 0)
	{
	  removeUnit(units[i]);
	  i--;
	}
      //checkCollisions(units[i],dt);
      checkCollisions(units[i]);
    }
  adjustUnits();
  xoffset = -hero->getx() + SCREEN_WIDTH/2;
  yoffset = -hero->gety() + SCREEN_HEIGHT/2;
}
void UnitInterface::unitDestroyMessage(const NetMessage *net_message)
{
    const UnitRemoteDestroy* remote_destroy 
        = (const UnitRemoteDestroy *) net_message;

    Units::iterator i = units.find(remote_destroy->getUnitToDestroy());
    if(i != units.end()) {
        removeUnit(i);
    }
}
void UnitInterface::updateUnitStatus()
{
    for(Units::iterator i = units.begin(); i != units.end(); /*nothing*/ ) {
        UnitBase* unit = i->second;
	    
        if (unit->unit_state.lifecycle_state == _UNIT_LIFECYCLE_INACTIVE) {
            Units::iterator next = i;
            ++next;
            removeUnit(i);
            i = next;
            continue;
        }
	    
        unsigned long pre_update_bucket_index;
        unsigned long post_update_bucket_index;

        pre_update_bucket_index 
            = unit_bucket_array.worldLocToBucketIndex(
                    unit->unit_state.location );
        unit->updateState();

        post_update_bucket_index 
            = unit_bucket_array.worldLocToBucketIndex(
                    unit->unit_state.location );

        if ( post_update_bucket_index != pre_update_bucket_index ) {
            unit_bucket_array.moveUnit(unit->id,
                    pre_update_bucket_index, post_update_bucket_index );
        }
        ++i;
    }

    if ( NetworkState::status == _network_state_server ) {
        if (message_timer.count()) {
            opcode_encoder.send();
        }
    }
}
SpatialDomainWidget::SpatialDomainWidget(QWidget* Parent, openfluid::fluidx::AdvancedFluidXDescriptor& AFXDesc):
  WorkspaceWidget(Parent,AFXDesc), ui(new Ui::SpatialDomainWidget),
  m_Domain(AFXDesc.spatialDomain()), m_Datastore(AFXDesc.datastore()),
  m_ActiveClass("")
{
  ui->setupUi(this);

  ui->StructureTabWidget->setCurrentIndex(0);
  ui->DataTabWidget->setCurrentIndex(0);

  mp_MapScene = new MapScene(m_Domain,this);
  ui->GlobalMapView->setScene(mp_MapScene);

  ui->AddUnitsClassButton->setIcon(QIcon(":/ui/common/icons/add.png"));
  ui->AddUnitsClassButton->setIconSize(QSize(20,20));

  ui->AddUnitButton->setIcon(QIcon(":/ui/common/icons/add.png"));
  ui->AddUnitButton->setIconSize(QSize(20,20));

  ui->RemoveUnitButton->setIcon(QIcon(":/ui/common/icons/remove.png"));
  ui->RemoveUnitButton->setIconSize(QSize(20,20));

  ui->AddConnectionButton->setIcon(QIcon(":/ui/common/icons/add.png"));
  ui->AddConnectionButton->setIconSize(QSize(20,20));

  ui->RemoveConnectionButton->setIcon(QIcon(":/ui/common/icons/remove.png"));
  ui->RemoveConnectionButton->setIconSize(QSize(20,20));

  ui->AddAttributeButton->setIcon(QIcon(":/ui/common/icons/add.png"));
  ui->AddAttributeButton->setIconSize(QSize(20,20));

  ui->EditAttributesButton->setIcon(QIcon(":/ui/common/icons/modify.png"));
  ui->EditAttributesButton->setIconSize(QSize(20,20));

  ui->RenameAttributeButton->setIcon(QIcon(":/icons/rename-attribute.png"));
  ui->RenameAttributeButton->setIconSize(QSize(20,20));

  ui->RemoveAttributeButton->setIcon(QIcon(":/ui/common/icons/remove.png"));
  ui->RemoveAttributeButton->setIconSize(QSize(20,20));

  ui->AddEventButton->setIcon(QIcon(":/ui/common/icons/add.png"));
  ui->AddEventButton->setIconSize(QSize(20,20));

  ui->EditEventButton->setIcon(QIcon(":/ui/common/icons/modify.png"));
  ui->EditEventButton->setIconSize(QSize(20,20));

  ui->RemoveEventsButton->setIcon(QIcon(":/ui/common/icons/remove.png"));
  ui->RemoveEventsButton->setIconSize(QSize(20,20));


#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
  ui->ConnectionsTableWidget->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
#else
  ui->ConnectionsTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
#endif


  connect(ui->AddUnitsClassButton,SIGNAL(clicked()),this,SLOT(addUnitsClass()));

  connect(ui->AddUnitButton,SIGNAL(clicked()),this,SLOT(addUnit()));
  connect(ui->RemoveUnitButton,SIGNAL(clicked()),this,SLOT(removeUnit()));

  connect(ui->AddConnectionButton,SIGNAL(clicked()),this,SLOT(addConnection()));
  connect(ui->RemoveConnectionButton,SIGNAL(clicked()),this,SLOT(removeConnection()));

  connect(ui->AddAttributeButton,SIGNAL(clicked()),this,SLOT(addAttribute()));
  connect(ui->EditAttributesButton,SIGNAL(clicked()),this,SLOT(editAttributesValues()));
  connect(ui->RenameAttributeButton,SIGNAL(clicked()),this,SLOT(renameAttribute()));
  connect(ui->RemoveAttributeButton,SIGNAL(clicked()),this,SLOT(removeAttribute()));

  connect(ui->AddEventButton,SIGNAL(clicked()),this,SLOT(addEvent()));
  connect(ui->EditEventButton,SIGNAL(clicked()),this,SLOT(editEvent()));
  connect(ui->EventsTableWidget,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(editEvent()));
  connect(ui->RemoveEventsButton,SIGNAL(clicked()),this,SLOT(removeEvents()));

  connect(ui->IDsListWidget,SIGNAL(currentRowChanged(int)),this,SLOT(updateUnitSelection(int)));

  // map view
  // TODO to reactivate when units IDs display on map will be fully functional
  ui->UnitsIDsCheckBox->setVisible(false);
  ui->GlobalMapView->enableAutomaticView(ui->AutomaticViewCheckBox->isChecked());

  connect(ui->UnitsIDsCheckBox,SIGNAL(toggled(bool)),mp_MapScene,SLOT(enableUnitsIDs(bool)));
  connect(ui->FitViewButton,SIGNAL(clicked()),ui->GlobalMapView,SLOT(fitViewToItems()));
  connect(ui->AutomaticViewCheckBox,SIGNAL(toggled(bool)),this,SLOT(enableAutomaticView(bool)));
  connect(ui->GlobalMapView,SIGNAL(automaticViewEnabled(bool)),
          ui->AutomaticViewCheckBox,SLOT(setChecked(bool)));

  connect(mp_MapScene,SIGNAL(selectionChanged()),this,SLOT(updateSelectionFromMap()));


  connect(ui->AttributesTableWidget,SIGNAL(cellChanged(int,int)),this,
          SLOT(updateFluidXAttributeFromCellValue(int,int)));
  connect(ui->PcsOrderSpinBox,SIGNAL(valueChanged(int)),this,
          SLOT(updateFluidXProcessOrder(int)));

  refresh();

  // all map layers are set visible at startup only
  setAllMapLayersVisible();

}
void Calculator::applyAssault(UnitsMap& attacker, PlayerId attackerId, pTerritory defenderTerritory){
    UnitsMap defender;
    PlayerId defenderId = defenderTerritory->ownership();
    int defenderDefBonus = DEFENDER_DEF_BONUS + defenderTerritory->getDefenseBonus();
    int defenderAttBonus = DEFENDER_ATT_BONUS;

    for(int i=0; i<defenderTerritory->units().size(); i++){
        pUnit u = defenderTerritory->units()[i];
        defender.insert(u->type(), u->noUnits());
    }

    int a[2];
    int d[2];
    int an = noUnits(attacker);
    int dn = noUnits(defender);
    int sum;
    int terrId = defenderTerritory->id();

    UnitsMap* m[] = {&attacker, &defender};

    while(true){
        a[0]=a[1]=d[0]=d[1]=0;

        
        a[0] = calculateAttack(*m[0]);
        d[0] = calculateDefence(*m[0]);

        a[1] = int(calculateAttack(*m[1]) * (100 + defenderAttBonus)/100);
        d[1] = int(calculateDefence(*m[1]) * (100 + defenderDefBonus)/100);


        for(int i=0; i<2; i++){
            sum = a[i] + d[(i+1)%2];
            int r = rand(sum);
                FileLogger::write(QString(i==0?"A ":"D ")
                    .append(QString::number(float(a[i])*100/sum))
                    .append("%"));
            if( r <=a[i]){  
                removeUnit(*m[(i+1)%2],rand(noUnits(*m[(i+1)%2])));
                FileLogger::write(QString("Killed. ")
                    .append(QString::number(noUnits(*m[0])))
                    .append("vs")
                    .append(QString::number(noUnits(*m[1]))));
            }
        }

        if(noUnits(attacker) == 0){
            FileLogger::write("defender won");
            for(UnitsMap::iterator i = defender.begin(); i!=defender.end(); ++i){
                raports_.push_back(
                    RaportFactory::getInstance().create(
                    OBJECT, UNIT, terrId, i.key(), i.value()));
                defenderTerritory->setUnit(i.key(), i.value());
            }
            
            raports_.push_back(
                RaportFactory::getInstance().create(
                    BATTLE, TERRITORY_BATTLE_DEFENDED, terrId, dn - noUnits(defender), an));

            
            return;
        }
        if(noUnits(defender) == 0){
            FileLogger::write("attacker won");
            for(UnitsMap::iterator i = defender.begin(); i!=defender.end(); ++i){
                raports_.push_back(
                    RaportFactory::getInstance().create(
                    OBJECT, UNIT, terrId, i.key(), i.value()));
                defenderTerritory->setUnit(i.key(), i.value());
            }
            for(UnitsMap::iterator i = attacker.begin(); i!=attacker.end(); ++i){
                raports_.push_back(
                    RaportFactory::getInstance().create(
                    OBJECT, UNIT, terrId, i.key(), i.value()));
                defenderTerritory->setUnit(i.key(), i.value());
            }
            
            raports_.push_back(
                RaportFactory::getInstance().create(
                BATTLE, TERRITORY_BATTLE_CONQUERED, terrId, an - noUnits(attacker), dn));
            raports_.push_back(
                RaportFactory::getInstance().create(
                OWNERSHIP, terrId, defenderId, attackerId, 0));

            return;
        }

    }
}
void PlayerController::enemyTurn() {
  for (Unit *unit : units) {
    if (!unit->isPlayer) {
      MapSquare *selection = nullptr;
      int min = mapSize * mapSize;

      if (unit->getX() - 1 >= 0) {
        Unit *unitLeft = getUnit(unit->getX() - 1, unit->getY());

        if (unitLeft == nullptr || unitLeft->isPlayer) {
          MapSquare *left = getMapSquare(unit->getX() - 1, unit->getY());
          int distance = abs(left->x - player->getX()) + abs(left->y - player->getY());

          if (distance < min) {
            selection = left;
            min = distance;
          }
        }
      }

      if (unit->getX() + 1 < mapSize) {
        Unit *unitRight = getUnit(unit->getX() + 1, unit->getY());

        if (unitRight == nullptr || unitRight->isPlayer) {
          MapSquare *right = getMapSquare(unit->getX() + 1, unit->getY());
          int distance = abs(right->x - player->getX()) + abs(right->y - player->getY());

          if (distance < min) {
            selection = right;
            min = distance;
          }
        }
      }

      if (unit->getY() - 1 >= 0) {
        Unit *unitDown = getUnit(unit->getX(), unit->getY() - 1);

        if (unitDown == nullptr || unitDown->isPlayer) {
          MapSquare *down = getMapSquare(unit->getX(), unit->getY() - 1);
          int distance = abs(down->x - player->getX()) + abs(down->y - player->getY());

          if (distance < min) {
            selection = down;
            min = distance;
          }
        }
      }

      if (unit->getY() + 1 < mapSize) {
        Unit *unitUp = getUnit(unit->getX(), unit->getY() + 1);

        if (unitUp == nullptr || unitUp->isPlayer) {
          MapSquare *up = getMapSquare(unit->getX(), unit->getY() + 1);
          int distance = abs(up->x - player->getX()) + abs(up->y - player->getY());

          if (distance < min) {
            selection = up;
            min = distance;
          }
        }
      }

      if (selection != nullptr) {
        if (player->getX() == selection->x && player->getY() == selection->y) {
          if (player->damaged(unit->damage)) {
            removeUnit(player);
          }
        } else {
          unit->move(selection);
        }
      }
    }
  }
}