void BuildActionEx::FreeResources(RtsGame &game) { if (_builderId != INVALID_TID) { if (!_buildArea.IsNull() && _buildArea.IsLocked()) { // Special buildings (for example addons) are not associated with build positions so no need to assert in that case. _ASSERTE(game.GetEntityType((EntityClassType)_params[PARAM_EntityClassId])->P(TP_IsSpecialBuilding) || !_buildArea.IsNull()); _buildArea.Unlock(this); } if (!_requiredResources.IsNull() && _requiredResources.IsLocked()) { _requiredResources.Unlock(this); } if (_builderId != INVALID_TID) { GameEntity *pEntity = game.Self()->GetEntity(_builderId); if (pEntity && pEntity->IsLocked()) { pEntity->Unlock(this); } _builderId = INVALID_TID; } } }
//---------------------------------------------------------------------------------------------- void AttackEntityAction::OnFailure(RtsGame& game, const WorldClock& p_clock) { GameEntity* pAttacker = game.Self()->GetEntity(m_attackerId); if (pAttacker && pAttacker->IsLocked() && pAttacker->Owner() == this) pAttacker->Unlock(this); }
void ResearchAction::FreeResources(RtsGame& game) { if (m_researcherId != INVALID_TID) { GameEntity* pResearcher = game.Self()->GetEntity(m_researcherId); if (pResearcher && pResearcher->IsLocked()) pResearcher->Unlock(this); m_researcherId = INVALID_TID; } }
void TrainAction::FreeResources() { if (m_traineeId != INVALID_TID) { GameEntity* pTrainee = g_Game->Self()->GetEntity(m_traineeId); if (pTrainee && pTrainee->IsLocked()) pTrainee->Unlock(this); m_traineeId = INVALID_TID; } if (m_trainerId != INVALID_TID) { GameEntity* pTrainer = g_Game->Self()->GetEntity(m_trainerId); if (pTrainer && pTrainer->IsLocked()) pTrainer->Unlock(this); m_trainerId = INVALID_TID; } }
//---------------------------------------------------------------------------------------------- void TrainAction::FreeResources(RtsGame& game) { if (m_traineeId != INVALID_TID) { GameEntity* pTrainee = game.Self()->GetEntity(m_traineeId); if (pTrainee && pTrainee->IsLocked()) pTrainee->Unlock(this); if (!m_requiredResources.IsNull() && m_requiredResources.IsLocked()) m_requiredResources.Unlock(this); m_traineeId = INVALID_TID; m_trainerId = INVALID_TID; } }
TID AdapterEx::GetEntityObjectId(EntityClassType p_entityType,const RankedStates& p_rankedStates) { /* Entity Object Adaptation Algorithm: IF player has Type THEN return the entity with the best state (based on input ranked states) ELSE adaptation failed and return nullptr entity Id */ GamePlayer *pPlayer; GameEntity *pEntity; vector<TID> entityIds; EntityClassType entityTypeId; ObjectStateType curEntityState; TID adaptedEntityId = INVALID_TID; vector<UnitEntry> validEntities; if(!IsRankedStatesInitialized) initializePredefinedRankedStates(); pPlayer = g_Game->Self(); assert(pPlayer); pPlayer->Entities(entityIds); for (size_t i = 0, size = entityIds.size(); i < size; ++i) { pEntity = pPlayer->GetEntity(entityIds[i]); assert(pEntity); if (p_entityType == pEntity->Type() && !pEntity->IsLocked()) { curEntityState = (ObjectStateType)pEntity->Attr(EOATTR_State); if (IsValidEntityState(curEntityState,p_rankedStates)) validEntities.push_back(MakePair(pEntity->Id(), curEntityState)); } } if (!validEntities.empty()) { sort(validEntities.begin(), validEntities.end(), [p_rankedStates](UnitEntry leftEntity,UnitEntry rightEntity) { return GetEntityStateIndex(leftEntity.second,p_rankedStates) < GetEntityStateIndex(rightEntity.second,p_rankedStates); }); adaptedEntityId = validEntities[0].first; } return adaptedEntityId; }
//---------------------------------------------------------------------------------------------- void TrainAction::HandleMessage(RtsGame& game, Message* pMsg, bool& consumed) { if (PlanStepEx::State() == ESTATE_Executing && pMsg->MessageTypeID() == MSG_EntityCreate) { EntityCreateMessage* pEntityMsg = static_cast<EntityCreateMessage*>(pMsg); _ASSERTE(pEntityMsg && pEntityMsg->Data()); if (pEntityMsg->Data()->OwnerId != PLAYER_Self) return; TID entityId = pEntityMsg->Data()->EntityId; GameEntity *pEntity = game.Self()->GetEntity(entityId); _ASSERTE(pEntity); // We are interested only in free trainees that have not been locked before if (!m_trainStarted && m_traineeId == INVALID_TID && pEntity->Type() == _params[PARAM_EntityClassId] && !pEntity->IsLocked()) { // Check if the trainer is training that entity GameEntity* pTrainer = game.Self()->GetEntity(m_trainerId); _ASSERTE(pTrainer); if (pTrainer->IsTraining(entityId)) { m_trainStarted = true; m_traineeId = entityId; m_pTrainee = pEntity; // Lock that trainee and bound it to this action because if we don't // other ready actions in the same update cycle will receive the same message // and they may bind to the same trainee pEntity->Lock(this); consumed = true; LogInfo("Action %s has bound trainee=%d to trainer=%d", ToString().c_str(), m_traineeId, m_trainerId); } } } }
IStrategizer::TID IStrategizer::AdapterEx::GetEntityObjectId(EntityClassType p_entityType ) { GamePlayer *pPlayer; GameEntity *pEntity; vector<TID> entityIds; EntityClassType entityTypeId; TID adaptedEntityId = INVALID_TID; pPlayer = g_Game->Self(); assert(pPlayer); pPlayer->Entities(entityIds); for (size_t i = 0, size = entityIds.size(); i < size; ++i) { pEntity = pPlayer->GetEntity(entityIds[i]); assert(pEntity); if (p_entityType == pEntity->Type() && !pEntity->IsLocked()) { return pEntity->Id(); } } return adaptedEntityId; }