#include "Main.h" #include "Agent.h" #include "Instance.h" #include "Player.h" //Uncomment to enable proper emulation of GW keyboard movement //#define EMULATE_KEYBOARD_MOVE //Normal stuff Agent::Agent() { this->mAgentId=0; this->mAgentType=0; this->mpController=0; this->mpData=0; } Agent::Agent(unsigned long agentId, unsigned char agentType, void* pController, void* pData) { this->mAgentId=agentId; this->mAgentType=agentType; this->mpController=pController; this->mpData=pData; SetCurrentSpeed(288); mRotating=false; mRotation=0; this->mMoveType=1; } unsigned long Agent::GetAgentId() { return mAgentId; } Agent::~Agent() { } void Agent::Destroy() { switch(mAgentType) { case AGENT_PLAYER: if (true) { Player* pPlayer=(Player*)mpController; if (pPlayer!=NULL){delete pPlayer;} mpController=NULL; BattleData* pBattle_data=(BattleData*)mpData; if (pBattle_data!=NULL){delete pBattle_data;} mpData=NULL; } break; } } //Goes through and initializes an agent...it processes things that need to be done for any extra data =p void Agent::Init() { //Output the script error log if (USES_BATTLE_DATA) { BattleData* pBattle_data=(BattleData*)mpData; int baseHP=0; int baseEnergy=0; if (true) { CallScriptFunction("base_hp_calculation") { call.AddParamInt(pBattle_data->level); call.AddParamInt(0); call.AddParamInt(0); call.End(); call.GetReturnedInt(baseHP); OutputScriptErrors(); } else{gCoreLog.error("Could not calculate base HP! Script %s could not be called.", _functName);} } if (true) { CallScriptFunction("base_energy_calculation") { call.AddParamInt(pBattle_data->level); call.AddParamInt(0); call.AddParamInt(0); call.End(); call.GetReturnedInt(baseEnergy); OutputScriptErrors(); } else{gCoreLog.error("Could not calculate base energy! Script %s call failed.", _functName);} } //Update the HP and Energy pBattle_data->maxHp=baseHP; pBattle_data->maxEnergy=baseEnergy; pBattle_data->currentEnergy=baseEnergy; pBattle_data->currentHp=baseHP; AAAgentMsg(UGV_CURRENT_HP, baseHP, AGENT_ACTION_HP, NO_EXPIRATION, RELAY_TO_ALL); AAAgentMsg(UGV_CURRENT_ENERGY, baseEnergy, AGENT_ACTION_ENERGY, NO_EXPIRATION, RELAY_TO_ALL); SetLevel(pBattle_data->level); } } void* Agent::ControllerIs(int agentType) { if (mpController!=NULL && this->mAgentType==agentType) { return mpController; } return NULL; } void Agent::Update() { //Update an AGENT_PLAYER switch(mAgentType) { case AGENT_PLAYER: if (mpController!=NULL) { ((Player*)mpController)->Update(); } break; } } //Movement functions Vertex Agent::GetCurrentPos() { float dist=mDestPosition.Distance(mLastPosition); float dx=mDestPosition.x-mLastPosition.x; float dy=mDestPosition.y-mLastPosition.y; //unsigned long elapsedTime=((unsigned long)TimerGetTime()-timeLastMoved)/1000; float elapsedTime=(TimerGetTime()-mTimeLastMoved) / 1000.0; float ntime=dist / mCurrentSpeed; if (ntime<=elapsedTime || dist==0) { return mDestPosition; } return (Vertex(dx, dy, 0)*(elapsedTime/ntime)) + mLastPosition; } void Agent::MoveKeyboard(Vertex* pStart, Vertex* pDelta, int moveType) { CompiledPacket pck; if ((pDelta)->Normalize() != mDirection || moveType != mMoveType) { pck.Add(APStartMovement(pDelta->Normalize(), moveType, NULL)); if (moveType != mMoveType) { //pck.Add(APStopMovement(1.0, moveType, NULL)); } QueueAction(AGENT_ACTION_MOVEMENT, 1777, AG_ACT_FLAG_CIH_BEFORE); } //Set the agent variables mDirection=pDelta->Normalize(); mMoveType=moveType; float distPerInterval=512;//512.0; Vertex cpos=*pStart; //Calculate the location in front of us we will try to get to Vertex projectedPos=cpos+(mDirection*distPerInterval); GotoLocation(projectedPos, true); } Vertex Agent::GotoLocation(Vertex location, bool useCih) { CompiledPacket pck; //Get the current position of the agent Vertex current_pos=GetCurrentPos(); //Set our last known position and the position we are heading to mLastPosition=current_pos; mDestPosition=location; //Set the time our movement started mTimeLastMoved=TimerGetTime(); //Update every player in the instance of this movement. pck.Add(APGotoLocation(mDestPosition, NULL)); //Calculate the amount of time it is going to take to get to the destination float dist=current_pos.Distance(mDestPosition); unsigned long time=(dist/(float)mCurrentSpeed)*1000; if (useCih) { QueueAction(AGENT_ACTION_MOVEMENT, time, AG_ACT_FLAG_CIH_BEFORE); } else { QueueAction(AGENT_ACTION_MOVEMENT, time, 0); } return mDestPosition; } Vertex Agent::StopMovement(Vertex* pStop) { gCoreLog.always("STOP !!!"); //Set the agent's stuff //mMoveType=MOVE_TYPE_STOP; //Regular code... #ifdef EMULATE_KEYBOARD_MOVE CompiledPacket pck; pck.Add(APStopMovement(1.0, mMoveType, NULL)); QueueAction(AGENT_ACTION_MOVEMENT, 1, 0); #endif Vertex cpos=*pStop; GotoLocation(cpos, true); //((Instance*)mpInstance)->SpawnGhost("Proj Point", cpos); return mDestPosition; } void Agent::SetCurrentPosition(Vertex pos) { mDestPosition=pos; mLastPosition=pos; } unsigned long Agent::GetLatency() { if (mAgentType==AGENT_PLAYER) { Player* pPlayer=(Player*)mpController; if (pPlayer!=NULL){return pPlayer->GetPing();} } return 0; } void Agent::RotateAgent(Vertex rad) { CompiledPacket pck; mRotation=rad.x; if (!mRotating) { pck.Add(APRotateAgent(Vertex(0.0, 0.0), NULL)); QueueAction(AGENT_ACTION_MOVEMENT, NO_EXPIRATION, AG_ACT_FLAG_CIH_BEFORE); mRotating=true; } else { pck.Add(APRotateAgent(rad, NULL)); QueueAction(AGENT_ACTION_MOVEMENT, 1, AG_ACT_FLAG_CIH_BEFORE); mRotating=false; } } float Agent::GetCurrentRotation() { return mRotation; } void Agent::SetCurrentSpeed(float speed) { mLastPosition=GetCurrentPos(); mCurrentSpeed=speed; mTimeLastMoved=TimerGetTime(); } float Agent::GetCurrentSpeed() { return mCurrentSpeed; }