Exemple #1
0
  //----------------------------------- TAKE OVER -------------------------------------------------
  BWSL_EXPORT bool takeover(AIModule& aiModule)
  {
    resetError();
    try
    {
      for(;;)
      {
        BWAPI::CallTypeId purpose = BWAPI::waitForEvent();

        // react upon bridge state
        switch(purpose)
        {
        case BWAPI::CallTypeIds::OnFrame:
          {
            if (BWAPI::getStaticGameData()->isOnMatchStart)
            {
              Game::onStart();
              aiModule.onStart();
            }
            if (!BWAPI::getStaticGameData()->userInput.isEmpty())
            {
              std::string text(BWAPI::getStaticGameData()->userInput.c_str());
              aiModule.onSendText(text);
            }
            // TODO: implement
            //BWAPI::GetUserInput()
            //aiModule.onSendText(input);
            Game::onFrame();
            aiModule.onFrame();
          }break;
        case BWAPI::CallTypeIds::OnDisconnect:
          {
          }return true;
        
        }
      }
    }
    catch(GeneralException &)
    {
      lastError = Errors::Connection_Failure;
      return false;
    }
    return true;
  }
/**
 * Initializes the sequence.
 * does a lot of validity checking.
 */
void MeleeAttackBState::init()
{
	if (_initialized) return;
	_initialized = true;

	_weapon = _action.weapon;
	if (!_weapon) // can't hit without weapon
	{
		_parent->popState();
		return;
	}

	_unit = _action.actor;

	bool reactionShoot = _unit->getFaction() != _parent->getSave()->getSide();
	_ammo = _action.weapon->getAmmoForAction(BA_HIT, reactionShoot ? nullptr : &_action.result);
	if (!_ammo)
	{
		_parent->popState();
		return;
	}

	if (!_parent->getSave()->getTile(_action.target)) // invalid target position
	{
		_parent->popState();
		return;
	}

	if (_unit->isOut() || _unit->isOutThresholdExceed())
	{
		// something went wrong - we can't shoot when dead or unconscious, or if we're about to fall over.
		_parent->popState();
		return;
	}

	// reaction fire
	if (reactionShoot)
	{
		// no ammo or target is dead: give the time units back and cancel the shot.
		auto target = _parent->getSave()->getTile(_action.target)->getUnit();
		if (!target || target->isOut() || target->isOutThresholdExceed() || target != _parent->getSave()->getSelectedUnit())
		{
			_parent->popState();
			return;
		}
		_unit->lookAt(_action.target, _unit->getTurretType() != -1);
		while (_unit->getStatus() == STATUS_TURNING)
		{
			_unit->turn();
		}
	}

	//spend TU
	if (!_action.spendTU(&_action.result))
	{
		_parent->popState();
		return;
	}


	AIModule *ai = _unit->getAIModule();

	if (_unit->getFaction() == _parent->getSave()->getSide() &&
		_unit->getFaction() != FACTION_PLAYER &&
		_parent->_debugPlay == false &&
		ai && ai->getTarget())
	{
		_target = ai->getTarget();
	}
	else
	{
		_target = _parent->getSave()->getTile(_action.target)->getUnit();
	}

	int height = _target->getFloatHeight() + (_target->getHeight() / 2) - _parent->getSave()->getTile(_action.target)->getTerrainLevel();
	_voxel = _action.target.toVexel() + Position(8, 8, height);

	if (_unit->getFaction() == FACTION_HOSTILE)
	{
		_hitNumber = _weapon->getRules()->getAIMeleeHitCount() - 1;
	}

	performMeleeAttack();
}
Exemple #3
0
int main(int argc, const char* argv[])
{
  std::string dllPath;

  dllPath = "RushBot.dll";
  
  std::cout << "Connecting..." << std::endl;

  assert(BWAPIClient.isConnected() == false);
  reconnect();
  assert(BroodwarPtr != nullptr);

  while( true )
  {
    std::cout << "waiting to enter match" << std::endl;
    while ( !Broodwar->isInGame() )
    {
      //std::cout << "attempting update" << std::endl;
      BWAPI::BWAPIClient.update();
      if (!BWAPI::BWAPIClient.isConnected())
      {
        std::cout << "Reconnecting..." << std::endl;
        reconnect();
      }
    }
    std::cout << "entered match" << std::endl;

    AIModule* client = NULL;
    HMODULE hMod = LoadLibraryA(dllPath.c_str());
    if (hMod == NULL)
    {
      std::cerr << "ERROR: Failed to load the AI Module" << std::endl;
      client = new AIModule();
      Broodwar->sendText("Error: Failed to load the AI Module");
    }
    else
    {
      typedef AIModule* (*PFNCreateA1)();
      typedef void (*PFNGameInit)(Game *);

      PFNGameInit newGame = (PFNGameInit)GetProcAddress(hMod, "gameInit");
      PFNCreateA1 newAIModule = (PFNCreateA1)GetProcAddress(hMod, "newAIModule");

      if ( !newGame || !newAIModule )
      {
        std::cerr << "ERROR: Failed to find AI Module exports" << std::endl;
        client = new AIModule();
        Broodwar->sendText("Error: Failed to find AI Module exports");
      }
      else
      {
        newGame(BroodwarPtr);
        client = newAIModule();
      }
    }
    std::cout << "starting match!" << std::endl;
    while ( Broodwar->isInGame() )
    {
      for ( std::list<Event>::const_iterator e = Broodwar->getEvents().begin(); e != Broodwar->getEvents().end(); ++e )
      {
        EventType::Enum et=e->getType();
        switch (et)
        {
          case EventType::MatchStart:
            client->onStart();
            break;
          case EventType::MatchEnd:
            client->onEnd(e->isWinner());
            break;
          case EventType::MatchFrame:
            client->onFrame();
            break;
          case EventType::MenuFrame:
            break;
          case EventType::SendText:
            client->onSendText(e->getText());
            break;
          case EventType::ReceiveText:
            client->onReceiveText(e->getPlayer(), e->getText());
            break;
          case EventType::PlayerLeft:
            client->onPlayerLeft(e->getPlayer());
            break;
          case EventType::NukeDetect:
            client->onNukeDetect(e->getPosition());
            break;
          case EventType::UnitDiscover:
            client->onUnitDiscover(e->getUnit());
            break;
          case EventType::UnitEvade:
            client->onUnitEvade(e->getUnit());
            break;
          case EventType::UnitShow:
            client->onUnitShow(e->getUnit());
            break;
          case EventType::UnitHide:
            client->onUnitHide(e->getUnit());
            break;
          case EventType::UnitCreate:
            client->onUnitCreate(e->getUnit());
            break;
          case EventType::UnitDestroy:
            client->onUnitDestroy(e->getUnit());
            break;
          case EventType::UnitMorph:
            client->onUnitMorph(e->getUnit());
            break;
          case EventType::UnitRenegade:
            client->onUnitRenegade(e->getUnit());
            break;
          case EventType::SaveGame:
            client->onSaveGame(e->getText());
            break;
          case EventType::UnitComplete:
            client->onUnitComplete(e->getUnit());
            break;
          default:
            break;
        }
      }
      BWAPI::BWAPIClient.update();
      if (!BWAPI::BWAPIClient.isConnected())
      {
        std::cout << "Reconnecting..." << std::endl;
        reconnect();
      }
    }
    delete client;
    FreeLibrary(hMod);
    std::cout << "Game ended" << std::endl;
  }
  std::cout << "Press ENTER to continue..." << std::endl;
  std::cin.ignore();
  return 0;
}