// *************************************************************** // Function: Instance.cpp // *************************************************************** #include #include "Instance.h" #include "Main.h" #include "../../src/SharedStructures.h" #include "../../src/IC.h" void CommandProcessing(char *commandString, bool adminConsole, void *lir, Player *p); using namespace std; vector ZoneInfos; //hash_map agents; //************************************ // Name: Instance // // Instance constructor. // // Parameters: // // Return: // //************************************ Instance::Instance() { this->mZoneId=0; this->mCapacity=10; } //************************************ // Name: ~Instance // // Instance destructor. // // Parameters: // // Return: // //************************************ Instance::~Instance() { hash_map::iterator i; for (i=mAgents.begin(); i!=mAgents.end(); i++) { i->second.Destroy(); } mAgents.clear(); list::iterator aar; for (aar=mAgentActionQueue.begin(); aar!=mAgentActionQueue.end(); aar++) { if (aar->pPckData!=NULL) { delete [] aar->pPckData; } } mLocalIdMan.FreeAll(); mStreamMan.FreeAll(); mAgentIdMan.FreeAll(); mIsaMan.FreeAll(); mEisaMan.FreeAll(); } //************************************ // Name: Instance // // Instance constructor with zone id // // Parameters: // zoneId - Zone id this instance uses. // // Return: // //************************************ Instance::Instance(unsigned short zoneId) { //Initialize some variables this->mZoneId=zoneId; assert(this->mZoneId>0); mZoneInfoSlot=Instance::GetZoneInfo(this->mZoneId); ///*DEBUG*/zoneInfoSlot=Instance::GetZoneInfo(148); mStreamMan.Init(); mLocalIdMan.Init(); mAgentIdMan.Init(); mIsaMan.Init(); mEisaMan.Init(); mLocalIdMan.SetBounds(5, 250); mStreamMan.SetBounds (4001, 8001); mAgentIdMan.SetBounds(8002, 68002);//8002, 68002 mIsaMan.SetBounds (68003, 128002);//68003, 128002 mEisaMan.SetBounds (128003, 188002);//128003, 188002 mCurrentNumPlayers=0; gCoreLog.notice("Created new Instance #%d", zoneId); } //************************************ // Name: GetZoneInfo // // Gets the zone info slot for the specified zone. Static function. // // Parameters: // zoneId - Zone id to get info for. // // Return: // >-1 - zone info slot // -1 - No zone info found. //************************************ long Instance::GetZoneInfo(unsigned short zoneId) { for (unsigned long i=0; ilsnetworkId; rmc.zoneId=zoneId; rmc.gsNetworkId=networkId; NetMessage s; unsigned char pck[sizeof(REQUEST_MAP_CHANGE)+2]; s.pData=pck; s.networkId=gpInternalComm->GetClientModeId(); s.dataSize=PreparePacket(GS_REQUEST_MAP_CHANGE, &rmc, sizeof(REQUEST_MAP_CHANGE), pck); gpInternalComm->SendData(&s); } } void Instance::Broadcast(char* pMessage, int color, int localId) { unsigned char* pUnicode=new unsigned char[(strlen(pMessage)+1)*2]; strcpy((char*)pUnicode, pMessage); Util::ConvertToUnicode(pUnicode); PLAYER_CYCLE_START() WriteGeneralChat(pUnicode, strlen(pMessage), color, localId, curPlayer->GetnetworkId()); PLAYER_CYCLE_END() delete [] pUnicode; } bool Instance::KillMe() { //TODO: Add check for remaining players if (mCurrentNumPlayers <= 0){return true;} else{return false;} return false; } unsigned short Instance::GetNumPlayers() { return mCurrentNumPlayers; } unsigned short Instance::GetCapacity() { return mCapacity; } long Instance::GetInstanceId() { return mInstanceId; } void Instance::SetInstanceId(long id) { mInstanceId=id; } bool Instance::IsFull() { //TODO: Check for full instance if (mCurrentNumPlayers >= this->mCapacity){return true;} return false; } //Relays a NetMessage to ALL players in the Instance that are connected void Instance::RelayToPlayers(NetMessage* pOutMessage) { 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* pCurrentPlayer=pController; pOutMessage->networkId=pCurrentPlayer->GetnetworkId(); gpGameServer->SendData(pOutMessage); } } } void Instance::SetCapacity(unsigned short cap) { mCapacity=cap; } int Instance::RemovePlayer(unsigned int nid) { Player* pPlayer=AccessPlayer(nid); if (pPlayer!=NULL) { if (!pPlayer->GetFlag(PLAYER_IS_GHOST)) { mCurrentNumPlayers--; } //Add a destroy agent action to the queue if (pPlayer->GetFlag(PLAYER_IS_CONNECTED)) { pPlayer->GetAgent()->AADestroyAgent(RELAY_TO_ALL); } //Free used id's mStreamMan.FreeID(pPlayer->GetStreamId()); mAgentIdMan.FreeID(pPlayer->GetAgentId()); mLocalIdMan.FreeID(pPlayer->GetLocalId()); pPlayer->FreeUsedIds(); //Delete the left over stuff if the agent destructor isn't called from some reason unsigned long agent_id=pPlayer->GetAgentId(); Agent* pAgent=AccessAgent(agent_id); pAgent->Destroy(); mAgents.erase(agent_id); return 0; } return -1; } int Instance::AddPlayer(Player* pPlayer) { //Add the player to the list unsigned long agent_id=this->mAgentIdMan.GetFreeID(); typedef pair ppair; BattleData* pBattle_data=new BattleData; memset(pBattle_data, 0, sizeof(BattleData)); mAgents.insert(ppair(agent_id, Agent(agent_id, AGENT_PLAYER, (void*)pPlayer, pBattle_data))); mAgents[agent_id].mpInstance=this; pPlayer->SetAgent(AccessAgent(agent_id)); if (!pPlayer->GetFlag(PLAYER_IS_GHOST)) { mCurrentNumPlayers++; } return 0; } int Instance::LoadZoneInfos() { ZoneInfos.clear(); char query[200]; sprintf(query, "SELECT zoneID, zoneName, mapID, spawnX, spawnY FROM sd_zone_ids WHERE mapID>0"); unsigned long qid=gDatabase.Query(query, strlen(query)); QUERY_RESULT* query_result=NULL; while((query_result=gDatabase.StealResult(qid))==NULL) { gDatabase.Update(); } int num_records=(int)mysql_num_rows(query_result->pResult); for (int i=0; ipResult); ZONE_INFO zone_info; zone_info.zoneId=(unsigned short)atoi(row[0]); strcpy(zone_info.zoneName, row[1]); zone_info.zoneLocation=atol(row[2]); zone_info.spawnX=atof(row[3]); zone_info.spawnY=atof(row[4]); ZoneInfos.push_back(zone_info); } gCoreLog.notice("Loaded: %d zones.", num_records); gDatabase.FreeResult(qid); return 0; } int Instance::Update() { //Cycle through all the agents in the instance to update them hash_map::iterator itr; for (itr=mAgents.begin(); itr!=mAgents.end(); itr++) { itr->second.Update(); } //Cycle through all action and check for invalid actions list::iterator aar; for (aar=mAgentActionQueue.begin(); aar!=mAgentActionQueue.end(); aar++) { if (BIT_TEST(aar->flags, AG_ACT_FLAG_EXPIRED) || ActionHasExpired(&(*aar))) { if (aar->pPckData!=NULL){delete [] aar->pPckData;} mAgentActionQueue.erase(aar); break; } } return 0; } //Returns a pointer to the player that has nid Player* Instance::AccessPlayer(int nid) { PLAYER_DATA* pLir=AccessLocalData(nid); if (pLir!=NULL) { return (Player*)pLir->pPlayer; } gCoreLog.error("FAIL"); return NULL; } Agent* Instance::AccessAgent(unsigned long agentId) { hash_map::iterator itr=mAgents.find(agentId); if (itr!=mAgents.end()) { return &itr->second; } return NULL; } unsigned short Instance::GetZoneId() { return this->mZoneId; } bool Instance::InitZone() { return 0; } int GM_CDECL gm_SpawnGhost(gmThread* a_thread) { GM_CHECK_NUM_PARAMS(3); GM_CHECK_STRING_PARAM(name, 0); GM_CHECK_TABLE_PARAM(vertex, 1); GM_CHECK_INT_PARAM(instance_id, 2); //Extract the Instance ID GET_AGENT_POINTERS(instance_id); short agent_id=pAg_instance->SpawnGhost((char*)name, Vertex(vertex->Get(0).GetIntSafe(), vertex->Get(1).GetIntSafe(), vertex->Get(2).GetIntSafe())); char cid[4]; memcpy(cid, &agent_id, 2); memcpy(cid+2, &ins_id, 2); int comp_id=0; memcpy(&comp_id, cid, 4); a_thread->PushInt(comp_id); return GM_OK; } int GM_CDECL gm_SpawnGhostEx(gmThread* a_thread) { GM_CHECK_NUM_PARAMS(4); GM_CHECK_STRING_PARAM(name, 0); GM_CHECK_TABLE_PARAM(pos, 1); GM_CHECK_TABLE_PARAM(extra, 2); GM_CHECK_INT_PARAM(instance_id, 3); GET_AGENT_POINTERS(instance_id); short agent_id=pAg_instance->SpawnGhostEx((char*)name, Vertex(pos->Get(0).GetIntSafe(), pos->Get(1).GetIntSafe(), pos->Get(2).GetIntSafe()), extra->Get(0).GetIntSafe(), extra->Get(1).GetIntSafe(), extra->Get(2).GetIntSafe(), extra->Get(3).GetIntSafe(), extra->Get(4).GetIntSafe(), extra->Get(5).GetIntSafe(), extra->Get(6).GetIntSafe(), extra->Get(7).GetIntSafe() ); char cid[4]; memcpy(cid, &agent_id, 2); memcpy(cid+2, &ins_id, 2); int comp_id=0; memcpy(&comp_id, cid, 4); a_thread->PushInt(comp_id); return GM_OK; } //************************************ // Name: SpawnGhostEx // // Spawns a ghost with extra options. // // Parameters: // pName - Ghost name // pos - Position to spawn at // profession - Profession of ghost // campaign - Campaign the ghost is from // sex - Sex of the ghost // height - Height of the ghost // face - What face style to use. // hairStyle - What hair style to use // hairColor - What hair color to use // skinColor - What skin color to use // // Return: // - Ghost's agent id //************************************ unsigned long Instance::SpawnGhostEx(char* pName, Vertex pos, int profession, int campaign, int sex, int height, int face, int hairStyle, int hairColor, int skinColor) { Player* pGhost=new Player(pName, 0); pGhost->SetFlag(PLAYER_IS_GHOST, true); AddPlayer(pGhost); pGhost->SetAppearance((height << 4)+sex, CHAR_APP_HEIGHT_SEX); pGhost->SetAppearance((hairColor << 4)+skinColor, CHAR_APP_HAIR_SKIN_COLOR); pGhost->SetAppearance((profession << 4)+hairStyle, CHAR_APP_PROFESSION_HAIR_STYLE); pGhost->SetAppearance((campaign << 4)+face, CHAR_APP_CAMPAIGN_FACE); //Set the PvP value pGhost->SetCharacterType(false); //Set the permissions group ID pGhost->SetGroupId(0); //ghost->Movement.SetCurrentPosition(positions[i]); //ghost->Movement.SetCurrentSpeed(288+rand()%25); pGhost->SetLocalId(mLocalIdMan.GetFreeID()); pGhost->GetAgent()->SetCurrentPosition(pos); pGhost->SetIdManagers(&mIsaMan, &mEisaMan); pGhost->SetFlag(PLAYER_IS_CONNECTED, true); PLAYER_CYCLE_START() SpawnPlayer(pGhost, curPlayer->GetnetworkId()); PLAYER_CYCLE_END() return pGhost->GetAgentId(); } //************************************ // Name: SpawnGhost // // Spawns a ghost player. // // Parameters: // pName - Name of ghost player // pos - Position to spawn at // // Return: // - Ghost's agent id //************************************ unsigned long Instance::SpawnGhost(char* pName, Vertex pos) { int profession=1; Player* pGhost=new Player(pName, 0); pGhost->SetFlag(PLAYER_IS_GHOST, true); AddPlayer(pGhost); pGhost->SetAppearance(0, CHAR_APP_HEIGHT_SEX); pGhost->SetAppearance(0, CHAR_APP_HAIR_SKIN_COLOR); pGhost->SetAppearance(1, CHAR_APP_PROFESSION_HAIR_STYLE); pGhost->SetAppearance(0, CHAR_APP_CAMPAIGN_FACE); //Extract the secondary profession pGhost->SetProfession(0, CHAR_PROF_SECONDARY); //Set the PvP value pGhost->SetCharacterType(false); //Set the permissions group ID pGhost->SetGroupId(0); //ghost->Movement.SetCurrentPosition(positions[i]); //ghost->Movement.SetCurrentSpeed(288+rand()%25); pGhost->SetLocalId(mLocalIdMan.GetFreeID()); pGhost->GetAgent()->SetCurrentPosition(pos); pGhost->SetIdManagers(&mIsaMan, &mEisaMan); pGhost->SetFlag(PLAYER_IS_CONNECTED, true); for (int j=INT_HEAD; j<=INT_FEET; j++) { Item item; if (!gItemDefMan.CreateArmor("Starter", &item, j, profession)) { gCoreLog.LOCATION; gCoreLog.error("Failed to create Armor!"); } item.GenerateIdHm(0, 0, NULL); item.SetItemStorageLocation(LOC_IN_EQUIPPED); item.SetItemStoragePosition(Item::ConvertToExternal(j)); pGhost->AddItem(&item); pGhost->SetEquippedArmor(j, item.GetId()); } PLAYER_CYCLE_START() SpawnPlayer(pGhost, curPlayer->GetnetworkId()); PLAYER_CYCLE_END() return pGhost->GetAgentId(); }