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; }
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; } }
bool cRAI::ValidateUnit(const int& unitID) { if( cb->GetUnitDef(unitID) == 0 ) // ! Work Around: Spring-Version(v0.74b1-0.75b2) { *l<<"\nERROR: ValidateUnit(): iU->first="<<unitID; UnitDestroyed(unitID,-1); return false; } return true; }
cRAI::~cRAI() { *l<<"\n\nShutting Down ..."; if( RAIDEBUGGING ) { *l<<"\n cRAI Debug:"; *l<<"\n clearing Units size="<<int(Units.size())<<": "; *l<<"\n clearing Enemies size="<<Enemies.size()<<": "; } while( int(Units.size()) > 0 ) UnitDestroyed(Units.begin()->first,-1); while( int(Enemies.size()) > 0 ) EnemyDestroyed(Enemies.begin()->first,-1); if( RAIDEBUGGING ) { int ERadar,ELOS; *l<<"\n Enemys (Enter LOS - Errors) = "<<DebugEnemyEnterLOS<<" = "<<DebugEnemyEnterLOS+DebugEnemyEnterLOSError<<"-"<<DebugEnemyEnterLOSError; *l<<"\n Enemys (Leave LOS + Destroyed in LOS - Errors) = "<<DebugEnemyLeaveLOS+DebugEnemyDestroyedLOS<<" = "<<DebugEnemyLeaveLOS+DebugEnemyLeaveLOSError<<"+"<<DebugEnemyDestroyedLOS<<"-"<<DebugEnemyLeaveLOSError; *l<<"\n Enemies Remaining in LOS = "<<(ELOS=DebugEnemyEnterLOS-DebugEnemyLeaveLOS-DebugEnemyDestroyedLOS); *l<<"\n Enemys (Enter Radar - Errors) = "<<DebugEnemyEnterRadar<<" = "<<DebugEnemyEnterRadar+DebugEnemyEnterRadarError<<"-"<<DebugEnemyEnterRadarError; *l<<"\n Enemys (Leave Radar + Destroyed in Radar - Errors) = "<<DebugEnemyLeaveRadar+DebugEnemyDestroyedRadar<<" = "<<DebugEnemyLeaveRadar+DebugEnemyLeaveRadarError<<"+"<<DebugEnemyDestroyedRadar<<"-"<<DebugEnemyLeaveRadarError; *l<<"\n Enemies Remaining in Radar = "<<(ERadar=DebugEnemyEnterRadar-DebugEnemyLeaveRadar-DebugEnemyDestroyedRadar); *l<<"\n Enemies Remaining = "<<int(Enemies.size()); *l<<"\n Units Remaining = "<<UM->GroupSize; *l<<"\n Groups Remaining = "<<UM->GroupSize; if( UM->GroupSize != 0 || ELOS != 0 || ERadar != 0 ) *l<<"\n (ERROR)"; } delete UM; delete B; delete SWM; delete CM; delete UDH; RAIs--; if( RAIs == 0 ) { *l<<"\n Global RAI Shutting Down"; // double closingTimer = clock(); delete RM; RM = 0; // *l<<"\n Resource-Map Closing Time: "<<(clock()-closingTimer)/(double)CLOCKS_PER_SEC<<" seconds"; delete TM; TM = 0; *l<<"\n Global RAI Shutdown Complete."; } *l<<"\nShutdown Complete."; delete l; }
int AAI::HandleEvent(int msg, const void* data) { switch (msg) { case AI_EVENT_UNITCAPTURED: // 2 { const IGlobalAI::ChangeTeamEvent* cte = (const IGlobalAI::ChangeTeamEvent*) data; UnitDestroyed(cte->unit,-1); } break; } return 0; }
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; }
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; }