int CGlobalAI::HandleEvent(int msg, const void* data) { // unit steal and donate support L("msg: " << msg); if (msg == AI_EVENT_UNITGIVEN) { const IGlobalAI::ChangeTeamEvent* cte = (const IGlobalAI::ChangeTeamEvent*) data; if (cte -> newteam == ai -> cb -> GetMyTeam()) { // just got a unit UnitCreated(cte -> unit); UnitFinished(cte -> unit); ai -> MyUnits[cte -> unit] -> Stop(); } } else if (msg == AI_EVENT_UNITCAPTURED) { const IGlobalAI::ChangeTeamEvent* cte = (const IGlobalAI::ChangeTeamEvent*) data; if (cte -> oldteam == ai -> cb -> GetMyTeam()) { // just lost a unit UnitDestroyed(cte -> unit, 0); } } L("msg end"); return 0; }
void cRAI::UnitFinished(int unit) { if( RAIDEBUGGING ) *l<<"\nUnitFinished("<<unit<<")"; if( Units.find(unit) == Units.end() ) // Occurs if a player canceled a build order with more than one quaried and something still finished UnitCreated(unit); UnitInfo* U = &Units.find(unit)->second; U->unitBeingBuilt = false; if( U->AIDisabled ) { if( RAIDEBUGGING ) *l<<"#"; return; } B->UnitFinished(unit,U); if( U->AIDisabled ) { if( RAIDEBUGGING ) *l<<"#"; return; } B->PM->UnitFinished(unit,U); SWM->UnitFinished(unit,U->udr); UM->UnitFinished(unit,U); if( U->ud->highTrajectoryType == 2 && rand()%4 == 0 ) { // Nothing too meanful, just a degree of randomness Command c; c.id = CMD_TRAJECTORY; c.params.push_back(1); cb->GiveOrder(unit,&c); } if( U->ud->speed == 0 ) UnitIdle(unit); if( RAIDEBUGGING ) *l<<"#"; }
int CKAIK::HandleEvent(int msg, const void* data) { switch (msg) { case AI_EVENT_UNITGIVEN: { const ChangeTeamEvent* cte = (const ChangeTeamEvent*) data; if ((cte->newteam) == (ai->cb->GetMyTeam())) { // got a unit UnitCreated(cte->unit, -1); UnitFinished(cte->unit); ai->uh->IdleUnitAdd(cte->unit, ai->cb->GetCurrentFrame()); } } break; case AI_EVENT_UNITCAPTURED: { const ChangeTeamEvent* cte = (const ChangeTeamEvent*) data; if ((cte->oldteam) == (ai->cb->GetMyTeam())) { // lost a unit UnitDestroyed(cte->unit, 0); // FIXME: multiple units captured during same frame? ai->uh->lastCapturedUnitFrame = ai->cb->GetCurrentFrame(); ai->uh->lastCapturedUnitID = cte->unit; } } break; } return 0; }
void XAICUnitHandler::OnEvent(const XAIIEvent* e) { switch (e->type) { case XAI_EVENT_UNIT_CREATED: { UnitCreated(dynamic_cast<const XAIUnitCreatedEvent*>(e)); } break; case XAI_EVENT_UNIT_FINISHED: { UnitFinished(dynamic_cast<const XAIUnitFinishedEvent*>(e)); } break; case XAI_EVENT_UNIT_DESTROYED: { UnitDestroyed(dynamic_cast<const XAIUnitDestroyedEvent*>(e)); } break; case XAI_EVENT_UNIT_DAMAGED: { UnitDamaged(dynamic_cast<const XAIUnitDamagedEvent*>(e)); } break; case XAI_EVENT_UNIT_IDLE: { UnitIdle(dynamic_cast<const XAIUnitIdleEvent*>(e)); } break; case XAI_EVENT_UNIT_GIVEN: { UnitGiven(dynamic_cast<const XAIUnitGivenEvent*>(e)); } break; case XAI_EVENT_UNIT_CAPTURED: { UnitCaptured(dynamic_cast<const XAIUnitCapturedEvent*>(e)); } break; case XAI_EVENT_INIT: { unitsByID.resize(MAX_UNITS, NULL); for (int i = 0; i < MAX_UNITS; i++) { unitsByID[i] = new XAICUnit(i, xaih); } } break; case XAI_EVENT_UPDATE: { Update(); } break; case XAI_EVENT_RELEASE: { for (int i = 0; i < MAX_UNITS; i++) { delete unitsByID[i]; unitsByID[i] = NULL; } unitsByID.clear(); } break; default: { } break; } }
void CEngineOutHandler::UnitCreated(const CUnit& unit, const CUnit* builder) { AI_EVT_MTH(); const int teamId = unit.team; const int allyTeamId = unit.allyteam; const int unitId = unit.id; const int builderId = builder? builder->id: -1; DO_FOR_TEAM_SKIRMISH_AIS(UnitCreated(unitId, builderId), teamId); DO_FOR_ENEMY_SKIRMISH_AIS(EnemyCreated(unitId), allyTeamId, unit); }
bool CSkirmishAIWrapper::LoadSkirmishAI(bool postLoad) { ai = new CSkirmishAI(skirmishAIId, teamId, key, GetCallback()); ai->Init(); // check if initialization went ok if (skirmishAIHandler.IsLocalSkirmishAIDieing(skirmishAIId)) { return false; } IAILibraryManager* libManager = IAILibraryManager::GetInstance(); libManager->FetchSkirmishAILibrary(key); const CSkirmishAILibraryInfo* infos = &*libManager->GetSkirmishAIInfos().find(key)->second; bool loadSupported = (infos->GetInfo(SKIRMISH_AI_PROPERTY_LOAD_SUPPORTED) == "yes"); libManager->ReleaseSkirmishAILibrary(key); if (postLoad && !loadSupported) { // fallback code to help the AI if it // doesn't implement load/save support Init(); for (size_t a = 0; a < unitHandler->MaxUnits(); a++) { if (!unitHandler->units[a]) continue; if (unitHandler->units[a]->team == teamId) { try { UnitCreated(a, -1); } CATCH_AI_EXCEPTION; if (!unitHandler->units[a]->beingBuilt) try { UnitFinished(a); } CATCH_AI_EXCEPTION; } else { if ((unitHandler->units[a]->allyteam == teamHandler->AllyTeam(teamId)) || teamHandler->Ally(teamHandler->AllyTeam(teamId), unitHandler->units[a]->allyteam)) { // do nothing } else { if (unitHandler->units[a]->losStatus[teamHandler->AllyTeam(teamId)] & (LOS_INRADAR | LOS_INLOS)) { try { EnemyEnterRadar(a); } CATCH_AI_EXCEPTION; } if (unitHandler->units[a]->losStatus[teamHandler->AllyTeam(teamId)] & LOS_INLOS) { try { EnemyEnterLOS(a); } CATCH_AI_EXCEPTION; } } } }
int CKAIK::HandleEvent(int msg, const void* data) { if (ai->Initialized()) { switch (msg) { case AI_EVENT_UNITGIVEN: case AI_EVENT_UNITCAPTURED: { const ChangeTeamEvent* cte = (const ChangeTeamEvent*) data; const int myAllyTeamId = ai->cb->GetMyAllyTeam(); const bool oldEnemy = !ai->cb->IsAllied(myAllyTeamId, ai->cb->GetTeamAllyTeam(cte->oldteam)); const bool newEnemy = !ai->cb->IsAllied(myAllyTeamId, ai->cb->GetTeamAllyTeam(cte->newteam)); if (oldEnemy && !newEnemy) { // unit changed from an enemy to an allied team // we got a new friend! :) EnemyDestroyed(cte->unit, -1); } else if (!oldEnemy && newEnemy) { // unit changed from an ally to an enemy team // we lost a friend! :( EnemyCreated(cte->unit); if (!ai->cb->UnitBeingBuilt(cte->unit)) { EnemyFinished(cte->unit); } } if (cte->oldteam == ai->cb->GetMyTeam()) { // we lost a unit UnitDestroyed(cte->unit, -1); // FIXME: multiple units given during same frame? ai->uh->lastCapturedUnitFrame = ai->cb->GetCurrentFrame(); ai->uh->lastCapturedUnitID = cte->unit; } else if (cte->newteam == ai->cb->GetMyTeam()) { // we have a new unit UnitCreated(cte->unit, -1); if (!ai->cb->UnitBeingBuilt(cte->unit)) { UnitFinished(cte->unit); ai->uh->IdleUnitAdd(cte->unit, ai->cb->GetCurrentFrame()); } } } break; } } return 0; }
void XAICUnitHandler::UnitFinished(const XAIUnitFinishedEvent* ee) { // UnitCreated is _always_ triggered // before a possible UnitFinished is // nevertheless, play it safe // // assert(createdUnitsByID.find(ee->unitID) != createdUnitsByID.end()); // if (createdUnitsByID.find(ee->unitID) == createdUnitsByID.end()) { XAIUnitCreatedEvent e; e.type = XAI_EVENT_UNIT_CREATED; e.unitID = ee->unitID; e.builderID = -1; e.frame = ee->frame; UnitCreated(&e); } createdUnitsByID.erase(ee->unitID); finishedUnitsByID.insert(ee->unitID); }
int AAI::HandleEvent(int msg, const void* data) { switch (msg) { case AI_EVENT_UNITGIVEN: // 1 case AI_EVENT_UNITCAPTURED: // 2 { const IGlobalAI::ChangeTeamEvent* cte = (const IGlobalAI::ChangeTeamEvent*) data; const int myAllyTeamId = cb->GetMyAllyTeam(); const bool oldEnemy = !cb->IsAllied(myAllyTeamId, cb->GetTeamAllyTeam(cte->oldteam)); const bool newEnemy = !cb->IsAllied(myAllyTeamId, cb->GetTeamAllyTeam(cte->newteam)); if (oldEnemy && !newEnemy) { // unit changed from an enemy to an allied team // we got a new friend! :) EnemyDestroyed(cte->unit, -1); } else if (!oldEnemy && newEnemy) { // unit changed from an ally to an enemy team // we lost a friend! :( EnemyCreated(cte->unit); if (!cb->UnitBeingBuilt(cte->unit)) { EnemyFinished(cte->unit); } } if (cte->oldteam == cb->GetMyTeam()) { // we lost a unit UnitDestroyed(cte->unit, -1); } else if (cte->newteam == cb->GetMyTeam()) { // we have a new unit UnitCreated(cte->unit, -1); if (!cb->UnitBeingBuilt(cte->unit)) { UnitFinished(cte->unit); UnitIdle(cte->unit); } } break; } } return 0; }
int cRAI::HandleEvent(int msg,const void* data) { if( RAIDEBUGGING ) *l<<"\nHandleEvent("<<msg<<","<<"~"<<")"; switch (msg) { case AI_EVENT_UNITGIVEN: { const IGlobalAI::ChangeTeamEvent* cte = (const IGlobalAI::ChangeTeamEvent*) data; if( cte->newteam != cb->GetMyTeam() ) { cb->SendTextMsg("cRAI::HandleEvent(AI_EVENT_UNITGIVEN): This AI is out of date, check for a more recent one.",0); *l<<"\nERROR: cRAI::HandleEvent(AI_EVENT_UNITGIVEN): This AI is out of date, check for a more recent one.\n"; } if( Enemies.find(cte->unit) != Enemies.end() ) EnemyDestroyed(cte->unit,-1); if( cb->GetUnitHealth(cte->unit) <= 0 ) // ! Work Around: Spring-Version(v0.74b1-0.75b2) { *l<<"\nERROR: HandleEvent(AI_EVENT_UNITGIVEN): given unit is dead or does not exist"; return 0; } UnitCreated(cte->unit); Units.find(cte->unit)->second.AIDisabled=false; if( !cb->UnitBeingBuilt(cte->unit) ) { UnitFinished(cte->unit); UnitIdle(cte->unit); } } break; case AI_EVENT_UNITCAPTURED: { const IGlobalAI::ChangeTeamEvent* cte = (const IGlobalAI::ChangeTeamEvent*) data; if( cte->oldteam != cb->GetMyTeam() ) { cb->SendTextMsg("cRAI::HandleEvent(AI_EVENT_UNITCAPTURED): This AI is out of date, check for a more recent one.",0); *l<<"\nERROR: cRAI::HandleEvent(AI_EVENT_UNITCAPTURED): This AI is out of date, check for a more recent one.\n"; } UnitDestroyed(cte->unit,-1); } break; case AI_EVENT_PLAYER_COMMAND: { const IGlobalAI::PlayerCommandEvent* pce = (const IGlobalAI::PlayerCommandEvent*) data; bool ImportantCommand=false; if( pce->command.id < 0 ) ImportantCommand = true; switch( pce->command.id ) { case CMD_MOVE: case CMD_PATROL: case CMD_FIGHT: case CMD_ATTACK: case CMD_AREA_ATTACK: case CMD_GUARD: case CMD_REPAIR: case CMD_LOAD_UNITS: case CMD_UNLOAD_UNITS: case CMD_UNLOAD_UNIT: case CMD_RECLAIM: case CMD_DGUN: case CMD_RESTORE: case CMD_RESURRECT: case CMD_CAPTURE: ImportantCommand = true; } for( int i=0; i<int(pce->units.size()); i++ ) { if( Units.find(pce->units.at(i)) == Units.end() ) // ! Work Around: Spring-Version(v0.75b2) { *l<<"\nERROR: HandleEvent(AI_EVENT_PLAYER_COMMAND): unknown unit id="<<pce->units.at(i); // pce->units.erase(pce->units.begin()+i); // i--; } else if( ImportantCommand ) Units.find(pce->units.at(i))->second.humanCommand = true; } if( ImportantCommand ) { B->HandleEvent(pce); } else if( pce->command.id == CMD_SELFD ) { for( vector<int>::const_iterator i=pce->units.begin(); i!=pce->units.end(); i++ ) UnitDestroyed(*i,-1); } } break; } if( RAIDEBUGGING ) *l<<"#"; return 0; }
int cRAI::HandleEvent(int msg,const void* data) { if( RAIDEBUGGING ) *l<<"\nHandleEvent("<<msg<<","<<"~"<<")"; switch (msg) { case AI_EVENT_UNITGIVEN: case AI_EVENT_UNITCAPTURED: { const IGlobalAI::ChangeTeamEvent* cte = (const IGlobalAI::ChangeTeamEvent*) data; const int myAllyTeamId = cb->GetMyAllyTeam(); const bool oldEnemy = !cb->IsAllied(myAllyTeamId, cb->GetTeamAllyTeam(cte->oldteam)); const bool newEnemy = !cb->IsAllied(myAllyTeamId, cb->GetTeamAllyTeam(cte->newteam)); if ( oldEnemy && !newEnemy ) { { if( Enemies.find(cte->unit) != Enemies.end() ) EnemyDestroyed(cte->unit,-1); } } else if( !oldEnemy && newEnemy ) { // unit changed from an ally to an enemy team // we lost a friend! :( EnemyCreated(cte->unit); if (!cb->UnitBeingBuilt(cte->unit)) { EnemyFinished(cte->unit); } } if( cte->oldteam == cb->GetMyTeam() ) { UnitDestroyed(cte->unit,-1); } else if( cte->newteam == cb->GetMyTeam() ) { if( cb->GetUnitHealth(cte->unit) <= 0 ) // ! Work Around: Spring-Version(v0.74b1-0.75b2) { *l<<"\nERROR: HandleEvent(AI_EVENT_(UNITGIVEN|UNITCAPTURED)): given unit is dead or does not exist"; return 0; } UnitCreated(cte->unit, -1); Units.find(cte->unit)->second.AIDisabled=false; if( !cb->UnitBeingBuilt(cte->unit) ) { UnitFinished(cte->unit); UnitIdle(cte->unit); } } } break; case AI_EVENT_PLAYER_COMMAND: { const IGlobalAI::PlayerCommandEvent* pce = (const IGlobalAI::PlayerCommandEvent*) data; bool ImportantCommand=false; if( pce->command.id < 0 ) ImportantCommand = true; switch( pce->command.id ) { case CMD_MOVE: case CMD_PATROL: case CMD_FIGHT: case CMD_ATTACK: case CMD_AREA_ATTACK: case CMD_GUARD: case CMD_REPAIR: case CMD_LOAD_UNITS: case CMD_UNLOAD_UNITS: case CMD_UNLOAD_UNIT: case CMD_RECLAIM: case CMD_DGUN: case CMD_RESTORE: case CMD_RESURRECT: case CMD_CAPTURE: ImportantCommand = true; } for( int i=0; i<int(pce->units.size()); i++ ) { if( Units.find(pce->units.at(i)) == Units.end() ) // ! Work Around: Spring-Version(v0.75b2) { *l<<"\nERROR: HandleEvent(AI_EVENT_PLAYER_COMMAND): unknown unit id="<<pce->units.at(i); // pce->units.erase(pce->units.begin()+i); // i--; } else if( ImportantCommand ) Units.find(pce->units.at(i))->second.humanCommand = true; } if( ImportantCommand ) { B->HandleEvent(pce); } else if( pce->command.id == CMD_SELFD ) { for( vector<int>::const_iterator i=pce->units.begin(); i!=pce->units.end(); ++i ) UnitDestroyed(*i,-1); } } break; } if( RAIDEBUGGING ) *l<<"#"; return 0; }