//---------------------------------------------------------------------------------------------- bool TrainAction::AliveConditionsSatisfied(RtsGame& game) { bool trainerExist = false; bool traineeExist = false; bool trainerBusy = false; bool traineeBeingTrained = false; bool success = false; // 1. Trainer building exist trainerExist = g_Assist.DoesEntityObjectExist(m_trainerId); if (trainerExist) { if (m_trainStarted) { // 2. Trainer building is busy or in the training state GameEntity* pTrainer = game.Self()->GetEntity(m_trainerId); _ASSERTE(pTrainer); ObjectStateType trainerState = (ObjectStateType)pTrainer->Attr(EOATTR_State); trainerBusy = trainerState == OBJSTATE_Training; // 3. The trainee unit object exist, i.e not cancel traineeExist = g_Assist.DoesEntityObjectExist(m_traineeId); if (traineeExist && !trainerBusy) { success = true; } else if (trainerBusy) { if (traineeExist) { // 4. Trainee is still being trained GameEntity* pTrainee = game.Self()->GetEntity(m_traineeId); _ASSERTE(pTrainee); ObjectStateType traineeState = (ObjectStateType)pTrainee->Attr(EOATTR_State); traineeBeingTrained = traineeState == OBJSTATE_BeingConstructed; if (traineeBeingTrained || traineeState == OBJSTATE_Idle) success = true; } } } else { success = true; } } else { ConditionEx* failedCondition = new EntityClassExist(PLAYER_Self, m_trainerType, 1); m_history.Add(ESTATE_Failed, failedCondition); } return success; }
Vector2 AdapterEx::GetBotColonyCenter() { // We didn't calculate player colony center yet ? if (m_botColonyCenter == Vector2::Null()) { GameEntity *pPlayerBase = nullptr; vector<TID> playerBases; g_Game->Self()->GetBases(playerBases); // Player has at least one base, then we use the first one // Note that player having many bases it not supported by the engine if (!playerBases.empty()) pPlayerBase = g_Game->Self()->GetEntity(playerBases[0]); // No base! This is weird but for the case, we will select the first unit position as the player coloney center else { vector<TID> playerEntities; g_Game->Self()->Entities(playerEntities); // This can't happen, If the player has no entities, then he must be losing assert(!playerEntities.empty()); pPlayerBase = g_Game->Self()->GetEntity(playerEntities[0]); } m_botColonyCenter.X = pPlayerBase->Attr(EOATTR_PosX); m_botColonyCenter.Y = pPlayerBase->Attr(EOATTR_PosY); } return m_botColonyCenter; }
TID AdapterEx::AdaptBuildingForTraining(EntityClassType p_traineeType) { // Gets first building to train entity from type p_traineeType // If no empty building is found, last non-idle building will be returned GamePlayer *pPlayer; GameEntity *pEntity; vector<TID> entityIds; EntityClassType trainerType; TID id = INVALID_TID; trainerType = g_Game->Self()->TechTree()->SourceEntity(p_traineeType); 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 (trainerType == pEntity->Type()) { id = pEntity->Id(); if (pEntity->Attr(EOATTR_State) == OBJSTATE_Idle) { id = pEntity->Id(); break; } } } return id; }
//---------------------------------------------------------------------------------------------- bool TrainAction::SuccessConditionsSatisfied(RtsGame& game) { bool success = false; bool traineeBeingTrained = false; if (m_trainStarted) { // 1. Trainee unit object exist bool traineeExist = g_Assist.DoesEntityObjectExist(m_traineeId); if (traineeExist) { // 2. Trainee is ready and no more being constructed GameEntity* pTrainee = game.Self()->GetEntity(m_traineeId); _ASSERTE(pTrainee); ObjectStateType traineeState = (ObjectStateType)pTrainee->Attr(EOATTR_State); traineeBeingTrained = traineeState == OBJSTATE_BeingConstructed; if (!traineeBeingTrained) { LogInfo("Action %s succeeded to train trainee=%d from trainer=%d", ToString().c_str(), m_traineeId, m_trainerId); success = true; } } } return success; }
void GamePlayer::OnEntityCreate(Message* p_pMessage) { GameEntity *pEntity = nullptr; TID entityId; EntityCreateMessage *pCreateMsg = nullptr; pCreateMsg = (EntityCreateMessage*)p_pMessage; if (pCreateMsg->Data()->OwnerId == m_id) { entityId = pCreateMsg->Data()->EntityId; if (m_entities.Contains(entityId)) { LogError("Entity %d already exist in Player %s units", entityId, Enums[m_id]); return; } pEntity = FetchEntity(entityId); assert(pEntity); m_entities[entityId] = pEntity; LogInfo("[%s] Unit '%s':%d created at <%d, %d>", Enums[m_id], Enums[pEntity->Type()], pEntity->Id(), pEntity->Attr(EOATTR_PosX), pEntity->Attr(EOATTR_PosY)); g_IMSysMgr.RegisterGameObj(entityId, pCreateMsg->Data()->OwnerId); } }
bool IStrategizer::GatherResourceAction::AliveConditionsSatisfied(RtsGame* pRtsGame) { assert(PlanStepEx::State() == ESTATE_Executing); bool b_gathererExist = false; bool b_resourceExist = false; bool b_alive = false; // 1. Gatherer is still alive b_gathererExist = g_Assist.DoesEntityObjectExist(_gathererId); if (b_gathererExist) { GameEntity* pGameGatherer = pRtsGame->Self()->GetEntity(_gathererId); ObjectStateType gathererState = (ObjectStateType)pGameGatherer->Attr(EOATTR_State); // 2. Gatherer is gathering resource if(gathererState == OBJSTATE_Gathering) { // 3. There is still remaining resource to be gathered b_resourceExist = g_Assist.DoesEntityObjectExist(_resourceId, PLAYER_Neutral); // If resource doesn't exist a new unit should have been gathered by handleMessage // if no resource has been assigned, this means it failed to find suitable resource b_alive = b_resourceExist; } else { b_alive = true; } } return b_alive; }
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; }
bool BuildActionEx::SuccessConditionsSatisfied(RtsGame& game) { assert(PlanStepEx::State() == ESTATE_Executing); if (_buildStarted) { int entityState; GameEntity *pEntity; pEntity = game.Self()->GetEntity(_buildingId); entityState = pEntity->Attr(EOATTR_State); return entityState != OBJSTATE_BeingConstructed; } return false; }
bool BuildActionEx::AliveConditionsSatisfied(RtsGame& game) { bool builderExist = false; bool buildingExist = false; bool isBuilderConstructing = false; bool success = false; GameEntity *pEntity = nullptr; assert(PlanStepEx::State() == ESTATE_Executing); builderExist = g_Assist.DoesEntityObjectExist(_builderId); if (builderExist) { success = true; pEntity = game.Self()->GetEntity(_builderId); assert(pEntity); isBuilderConstructing = (pEntity->Attr(EOATTR_State) == OBJSTATE_Constructing); if (!isBuilderConstructing) { LogInfo("Builder with ID=%d of action %s is not in the constructing state", _builderId, ToString().c_str()); } else { if (_buildStarted) { buildingExist = g_Assist.DoesEntityObjectExist(_buildingId); if (!buildingExist) { success = false; } } } } else { LogInfo("Builder with ID=%d of action %s does not exist", _builderId, ToString().c_str()); } return success; }
//---------------------------------------------------------------------------------------------- bool MoveAction::AliveConditionsSatisfied(RtsGame& game) { bool satisfied = false; if (g_Assist.DoesEntityObjectExist(_entityId)) { GameEntity* pEntity = game.Self()->GetEntity(_entityId); _ASSERTE(pEntity); satisfied = (pEntity->Attr(EOATTR_IsMoving) > 0 ? true : false); } else { ConditionEx* failedCondition = new EntityClassExist( PLAYER_Self, (EntityClassType)_params[PARAM_EntityClassId], 1); m_history.Add(ESTATE_Failed, failedCondition); } return satisfied; }
bool IStrategizer::GatherResourceAction::SuccessConditionsSatisfied(RtsGame* pRtsGame) { assert(PlanStepEx::State() == ESTATE_Executing); // Check that the worker has gathered needed amount of the resource bool success = false; if(_gatherStarted) { GameEntity* pGameGatherer = pRtsGame->Self()->GetEntity(_gathererId); ObjectStateType gathererState = (ObjectStateType)pGameGatherer->Attr(EOATTR_State); if (gathererState == OBJSTATE_Gathering) { _gatheredAmount += pRtsGame->GetResourceConsumbtionRatePerWorker((ResourceType)_params[PARAM_ResourceId]); } } success = (_gatheredAmount >= _params[PARAM_Amount]); return success; }