// *************************************************************** // Function: AgentActionList.cpp // *************************************************************** #include "Instance.h" #include "Main.h" //************************************ // Name: ProcessAction // // Checks flags and executes actions before the action is sent to players. // // Parameters: // pAgentAction - Agent Action // pDestPlayer - Player to send the action to. // // Return: // //************************************ void Instance::ProcessAction(AGENT_ACTION* pAgentAction, Player* pDestPlayer) { //Return if action expired if (BIT_TEST(pAgentAction->flags, AG_ACT_FLAG_EXPIRED)) { return; } NetMessage s; s.networkId=pDestPlayer->GetnetworkId(); //If we must send a CIH packet before this packet if (BIT_TEST(pAgentAction->flags, AG_ACT_FLAG_CIH_BEFORE)) { CompiledPacket pck; pDestPlayer->CreateCih(&pck, &s); } //Send the action packet s.pData=pAgentAction->pPckData; s.dataSize=pAgentAction->dataSize; gpGameServer->SendData(&s); if (BIT_TEST(pAgentAction->flags, AG_ACT_FLAG_CIH_AFTER)) { CompiledPacket pck; pDestPlayer->CreateCih(&pck, &s); } } //************************************ // Name: RelayAction // // Sends the action to every player in the instance. Will add to the agent action queue if needed. // // Parameters: // pAgentAction - Agent action // // Return: // //************************************ void Instance::RelayAction(AGENT_ACTION* pAgentAction) { //Make sure the agent exists before relaying the action. bool exists=false; if (AccessAgent(pAgentAction->agentId)!=NULL) { //Relay this action to every player hash_map::iterator cycleItr; for (cycleItr=mAgents.begin(); cycleItr!=mAgents.end(); cycleItr++) { Player* pController=(Player*)cycleItr->second.mpController; if (pController!=NULL && cycleItr->second.mAgentType==AGENT_PLAYER && pController->GetFlag(PLAYER_IS_CONNECTED) && !pController->GetFlag(PLAYER_IS_GHOST)) { Player* pCurrent_player=pController; ProcessAction(pAgentAction, pCurrent_player); } } exists=true; } //If the agent is NOT being destroyed and it DOES exist we can add it to the list if (pAgentAction->exclusionId!=AGENT_ACTION_DESTROY && exists) { //Remove any conflicting actions for this agent list::iterator itr; for (itr=mAgentActionQueue.begin(); itr!=mAgentActionQueue.end(); itr++) { //Delete actions that cancel each other out if (itr->agentId==pAgentAction->agentId) { if (pAgentAction->exclusionId>0 && itr->exclusionId==pAgentAction->exclusionId) { if (itr->pPckData!=NULL) { delete [] itr->pPckData; } mAgentActionQueue.erase(itr); break; } } } //Add this new action to the back if (pAgentAction->expiresIn!=1) { pAgentAction->startTime=(unsigned long)TimerGetTime(); mAgentActionQueue.push_back(*pAgentAction); } //This action expires instantly...just delete it. else { if (pAgentAction->pPckData!=NULL) { delete [] pAgentAction->pPckData; } } } else { delete [] pAgentAction->pPckData; pAgentAction->pPckData=NULL; //Set all actions for this agent to expired. list::iterator itr; for (itr=mAgentActionQueue.begin(); itr!=mAgentActionQueue.end(); itr++) { if (itr->agentId==pAgentAction->agentId) { BIT_SET(itr->flags, AG_ACT_FLAG_EXPIRED); } } } } //************************************ // Name: ActionHasExpired // // Checks if an agent action has expired. // // Parameters: // pAgentAction - Agent action to check // // Return: // true - Agent action has expired // false - Agent action has not expired. //************************************ bool Instance::ActionHasExpired(AGENT_ACTION* pAgentAction) { bool expired=false; //This action has expired due to time if (pAgentAction->expiresIn>0 && TimerGetTime()-pAgentAction->startTime>=pAgentAction->expiresIn) { expired=true; } //Check that the agent still exists if (AccessAgent(pAgentAction->agentId)==NULL) { expired=true; } //Delete if (expired) { BIT_SET(pAgentAction->flags, AG_ACT_FLAG_EXPIRED); } return expired; }