// *************************************************************** // Function: Item.cpp // *************************************************************** #include "Item.h" #include "CompiledPacket.h" #include #include "Utility.h" #include "GWLPDatabase.h" #include GWLPDatabase* gpItemDatabase=NULL; Item::Item() { memset(mId, 0, 12); mExistsInDb=false; memset(&mData, 0, sizeof(GWITEM)); memset(&mLocation, 0, sizeof(long)); mIsa=0; mExternalIsa=0; } //************************************ // Name: ConvertToExternal // // Converts the internal armor slot to external (used by GW) // // Parameters: // num - Slot number // // Return: // - zero //************************************ int Item::ConvertToExternal(int num) { if (num==0){return 4;}//head if (num==1){return 2;}//chest if (num==2){return 6;}//hands if (num==3){return 3;}//legs if (num==4){return 5;}//feet return 0; } //************************************ // Name: SetIsa // // Set the item stream agent id // // Parameters: // id - The desired isa // // Return: // //************************************ void Item::SetIsa(long id) { mIsa=id; } //************************************ // Name: GetIsa // // Gets this items isa. // // Parameters: // // Return: // - The isa of the current item //************************************ long Item::GetIsa() { return mIsa; } //************************************ // Name: GetExternalIsa // // Gets the external isa for this item. // // Parameters: // // Return: // - This items eisa //************************************ long Item::GetExternalIsa() { return mExternalIsa; } //************************************ // Name: SetExternalIsa // // Sets this items eisa // // Parameters: // eisa - Document // // Return: // //************************************ void Item::SetExternalIsa(long eisa) { mExternalIsa=eisa; } //************************************ // Name: Customize // // Customize this item for a character. // // Parameters: // pCharacterName - Name of character to customize for. // // Return: // //************************************ bool Item::Customize(char* pCharacterName) { strcpy(this->mData.customized, pCharacterName); return true; } //************************************ // Name: GetId // // Returns this items id. // // Parameters: // // Return: // - A pointer to the 12 byte buffer contain this items id //************************************ char * Item::GetId() { return mId; } //************************************ // Name: Save // // Saves the item to the database. // // Parameters: // // Return: // true - saved ok // false - save failed //************************************ bool Item::Save() { //gCoreLog.Section("Saving item..."); //CoreLog.LogKnownData("Item Dump", (unsigned char*)&Data, sizeof(GWITEM)); char dest[4000]; if (mExistsInDb) { char myid[30]; Util::BinToHex((unsigned char*)&mId, 12, myid); Util::BinToHex((unsigned char*)&mData, sizeof(GWITEM), dest); char query[2000]; if (this->GetItemStorageLocation()==LOC_IN_STORAGE) { printf("Saving to Storage!\n"); //Extract the account ID from the item ID unsigned long account_id=Read_uLong(this->mId); unsigned long character_id=Read_uLong(this->mId+4); if (character_id==0 || character_id>=10){character_id=0;} sprintf(query, "UPDATE items SET ID=%s, Size=\"%d\", Data=\"%s\", ItemLocType=\"%d\", OwnerCID=\"%u\", OwnerAID=\"%u\" WHERE ID=\"%s\" LIMIT 1", myid, sizeof(GWITEM), dest, mLocation, character_id, account_id, myid); } else { //Extract the character ID from the item ID unsigned long character_id=Read_uLong(this->mId+4); sprintf(query, "UPDATE items SET ID=%s, Size=\"%d\", Data=\"%s\", ItemLocType=\"%d\", OwnerCID=\"%u\", OwnerAID=\"%u\" WHERE ID=\"%s\" LIMIT 1", myid, sizeof(GWITEM), dest, mLocation, character_id, 0, myid); } gpItemDatabase->Query(query, (long)strlen(query)); } else { char myid[30]; Util::BinToHex((unsigned char*)&mId, 12, myid); Util::BinToHex((unsigned char*)&mData, sizeof(GWITEM), dest); char query[600]; if (this->GetItemStorageLocation()==LOC_IN_STORAGE) { printf("Saving to Storage!\n"); //Extract the account ID from the item ID unsigned long account_id=Read_uLong(this->mId); unsigned long character_id=Read_uLong(this->mId+4); if (character_id==0 || character_id>=10){character_id=0;} sprintf(query, "INSERT INTO items (ID, Size, Data, ItemLocType, OwnerCID, OwnerAID) VALUES(%s, %d, %s, %d, %u, %u)", myid, sizeof(GWITEM), dest, mLocation, character_id, account_id); } else { //Extract the character ID from the item ID unsigned long character_id=Read_uLong(this->mId+4); sprintf(query, "INSERT INTO items (ID, Size, Data, ItemLocType, OwnerCID, OwnerAID) VALUES(%s, %d, %s, %d, %u, %u)", myid, sizeof(GWITEM), dest, mLocation, character_id, 0); } gpItemDatabase->Query(query, (long)strlen(query)); } return true; } //************************************ // Name: SetColorWithDye // // Sets the item's base color. // // Parameters: // color - Document // // Return: // //************************************ void Item::SetColorWithDye(int color) { mData.color[1]=color; } //************************************ // Name: GenerateId // // Generates an item id that is not already in-use // // Parameters: // accountId - Recipient player's account id // characterId - Recipient character's character id // pItemList - vector pointer with containing all other items // // Return: // //************************************ void Item::GenerateId(int accountId, int characterId, vector* pItemList) { memcpy(mId, &accountId, sizeof(int)); memcpy(mId+4, &characterId, sizeof(int)); while (true) { int rn=400+rand()%4000000000; memcpy(mId+8, &rn, sizeof(int)); if (pItemList!=NULL) { if (CheckForDuplicateIds(pItemList)) { break; } } else { break; } } } //************************************ // Name: GenerateIdHm // // Generates a unique item id // // Parameters: // accountId - Recipient player's account id // characterId - Recipient character's character id // pItemList - Hash map containing items to check against // // Return: // //************************************ void Item::GenerateIdHm(int accountId, int characterId, hash_map* pItemList) { memcpy(mId, &accountId, sizeof(int)); memcpy(mId+4, &characterId, sizeof(int)); while (true) { int random_number=400+rand()%4000000000; memcpy(mId+8, &random_number, sizeof(int)); if (pItemList!=NULL) { if (CheckForDuplicateIdsHM(pItemList)) { break; } } else { break; } } } //************************************ // Name: Load // // Executes query that loads all items for the account and character. // // Parameters: // accountId - Player's account id // characterId - Player's character id // // Return: // - Query reference number //************************************ unsigned long Item::Load(unsigned long accountId, unsigned long characterId) { char query[600]; sprintf(query, "SELECT ID, Data, ItemLocType FROM items WHERE (OwnerCID=%u AND OwnerAID=0) OR (OwnerAID=%u AND OwnerCID<10)", characterId, accountId); printf("LOAD: %s\n", query); return gpItemDatabase->Query(query, (long)strlen(query)); } //************************************ // Name: Load // // Loads a list of item id's. // // Parameters: // items - Pointer to start of item id list. // numItems - How many item id's are in the list. // // Return: // - Query reference number //************************************ unsigned long Item::Load(ITEM_ID *items, unsigned long numItems) { ostringstream s; s << "SELECT ID, Data, ItemLocType FROM items WHERE "; for (unsigned long i=0; iQuery((char*)s.str().c_str(), (long)strlen(s.str().c_str())); } //************************************ // Name: ConvertResultSet // // Converts an item selection query result into items. // // Parameters: // pResult - MySQL result * // pNumItems - Storage for number of items. // // Return: // NULL - No items in query or query failed // NOT NULL - Pointer to an array of items //************************************ Item* Item::ConvertResultSet(void* pResult, long* pNumItems) { if(pResult == NULL){return NULL;} assert(pNumItems!=NULL); Item *pItems=new Item[(unsigned long)mysql_num_rows((MYSQL_RES*)pResult)]; *pNumItems=(long)mysql_num_rows((MYSQL_RES*)pResult); if(mysql_num_rows((MYSQL_RES*)pResult) == 0){return NULL;} for (unsigned int i=0; iQuery(query, (long)strlen(query)); return true; } //************************************ // Name: ToCustomizationPacket // // Generates the item customization packet minus opcode. // // Parameters: // pDestBuffer - Destination buffer // // Return: // - Size of packet in bytes //************************************ int Item::ToCustomizationPacket(unsigned char* pDestBuffer) { assert(pDestBuffer!=NULL); unsigned char *pOrginal_ptr=pDestBuffer; //Set the name length unsigned long name_length=(unsigned long)strlen(mData.customized); pDestBuffer[0]=(unsigned char)name_length & 0xFF; pDestBuffer[1]=0x00; pDestBuffer+=2; unsigned char temp[50]; memset(temp, 0, 50); strcpy((char*)temp, mData.customized); //Convert the name to unicode Util::ConvertToUnicode(temp); //Copy the name over to the dest memcpy(pDestBuffer, temp, name_length*2); pDestBuffer+=name_length*2; return (int)(pDestBuffer-pOrginal_ptr); } //************************************ // Name: CreateItemPacket // // Creates an item packet. // // Parameters: // pIsa - Pointer to the desired item stream agent (isa) id // pPage - Page id to place the item // pStreamId - Stream id // pDestBuf - Destination buffer for the packet data // // Return: // - Size of packet in bytes //************************************ int Item::CreateItemPacket(short GEN_ITEM, short IT_LOC, short IT_ON, short IT_PROF, long *pIsa, short *pPage, short *pStreamId, char* pDestBuf) { this->mIsa=(long)*pIsa; char *pOp_ptr=pDestBuf; //Append the item packet header memcpy(pOp_ptr, &GEN_ITEM, sizeof(short)); pOp_ptr+=2; //append the item glue memcpy(pOp_ptr, pIsa, sizeof(long)); pOp_ptr+=4; //append the item data pOp_ptr+=ToItemPacket((unsigned char*)pOp_ptr); if (IsCustomized()) { //Append the item is customized header memcpy(pOp_ptr, &IT_ON, sizeof(short)); pOp_ptr+=sizeof(short); //Append the item glue memcpy(pOp_ptr, pIsa, sizeof(long)); pOp_ptr+=4; //Get the customization data pOp_ptr+=ToCustomizationPacket((unsigned char*)pOp_ptr); //Add the profession packet if needed if (mData.profession!=0) { //append the profession packet memcpy(pOp_ptr, &IT_PROF, 2); pOp_ptr+=2; //append the item glue memcpy(pOp_ptr, pIsa, sizeof(long)); pOp_ptr+=4; //append the profession char t=mData.profession & 0xFF; memcpy(pOp_ptr, &t, 1); pOp_ptr++; } } //If we are using an item position then add the position packet if (pPage!=NULL) { //append the item position header memcpy(pOp_ptr, &IT_LOC, 2); pOp_ptr+=2; //append the stream id memcpy(pOp_ptr, pStreamId, 2); pOp_ptr+=2; //append the item glue memcpy(pOp_ptr, pIsa, sizeof(long)); pOp_ptr+=4; //append the page memcpy(pOp_ptr, pPage, 2); pOp_ptr+=2; //append the item position char pos=GetItemStoragePosition(); memcpy(pOp_ptr, &pos, 1); pOp_ptr++; } return (int)(pOp_ptr-pDestBuf); } //************************************ // Name: GetItemStoragePosition // // Retrieve item storage position. What slot the item is in. // // Parameters: // // Return: // - Value 0-255 indicating what slot. //************************************ char Item::GetItemStoragePosition() { char *p=(char*)&mLocation; p+=3; return *p; } //************************************ // Name: IsCustomized // // Is this item customized? // // Parameters: // // Return: // true - Item is customized // false - Item is not customized //************************************ bool Item::IsCustomized() { if (mData.customized[0]!=0) return true; return false; } //************************************ // Name: Delete // // Deletes this item from the database // // Parameters: // // Return: // true - Item deleted //************************************ bool Item::Delete() { printf("Deleting current item from database...\n"); char buf[30]; Util::BinToHex((unsigned char*)mId, 12, buf); char query[200]; sprintf(query, "DELETE FROM items WHERE ID=%s", buf); gpItemDatabase->Query(query, (long)strlen(query)); mExistsInDb=false; return true; } //************************************ // Name: SetItemStorageLocation // // Sets the item storage location. e.g. bag, storage, equipped // // Parameters: // type - IS_BACKPACK 0, BACKPACK 1, STORAGE 2, EQUIPPED 3, etc // // Return: // //************************************ void Item::SetItemStorageLocation(int type) { char *p=(char*)&mLocation; *p=type; } //************************************ // Name: SetItemStoragePosition // // Set the position the item is stored in. e.g. what slot of the bag // // Parameters: // pos - Position of item 0-255 // // Return: // //************************************ void Item::SetItemStoragePosition(int pos) { char *p=(char*)&mLocation; p+=3; *p=pos; } //************************************ // Name: CheckForDuplicateIds // // Check for duplicate item ids // // Parameters: // pItemList - Pointer to item list // // Return: // true - NO duplicates found // false - Duplicate id's found //************************************ bool Item::CheckForDuplicateIds(vector* pItemList) { vector::iterator itr; for (itr=pItemList->begin(); itr!=pItemList->end(); itr++) { if (memcmp(itr->mId, mId, 12)==0) { return false; } } return true; } //************************************ // Name: CheckForDuplicateIdsHM // // Check for duplicate item ids in a hash map // // Parameters: // pItemList - Pointer to hash map containing items // // Return: // true - NO duplicates found // false - Duplicates found //************************************ bool Item::CheckForDuplicateIdsHM(hash_map* pItemList) { unsigned long item_id=Read_uLong(mId+8); hash_map::iterator itr=pItemList->find(item_id); if (itr!=pItemList->end()) { return false; } return true; } //************************************ // Name: GetItemStorageLocation // // Retrieve the current item's storage location. e.g. bag, backpack, etc // // Parameters: // // Return: // - Item storage location //************************************ int Item::GetItemStorageLocation() { char temp; memcpy(&temp, &mLocation, 1); return temp; } //************************************ // Name: ToItemPacket // // Generates an item packet for this item. // // Parameters: // pDestBuffer - Destination byte buffer for the packet data. // // Return: // - Size of generated packet in bytes //************************************ int Item::ToItemPacket(unsigned char* pDestBuffer) { //CoreLog.debug("Converting to item packet..."); unsigned char *pOp_ptr=pDestBuffer; memcpy(pDestBuffer, &mData.gwId, 2); pDestBuffer+=2; memcpy(pDestBuffer, &mData.graphicalOptions, 2); pDestBuffer+=2; memcpy(pDestBuffer, &mData.upgradeComponent, 1); pDestBuffer+=1; memcpy(pDestBuffer, mData.color, 3); pDestBuffer+=3; memcpy(pDestBuffer, &mData.unknownBlock1, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.unknownBlock2, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.unknownBlock3, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.uniqueness, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.unknownBlock4, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.rarity, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.unknownBlock5, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.unknownBlock6, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.unknownBlock7, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.unknownBlock8, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.unknownBlock9, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.unknownBlock10, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.unknownBlock11, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.unknownBlock12, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.unknownBlock13, 1); pDestBuffer+=1; memcpy(pDestBuffer, &mData.quantity, sizeof(int)); pDestBuffer+=sizeof(int); short item_name_length=this->mData.itemNameLength/2; memcpy(pDestBuffer, &item_name_length, sizeof(short)); pDestBuffer+=sizeof(short); memcpy(pDestBuffer, this->mData.itemName, this->mData.itemNameLength); pDestBuffer+=this->mData.itemNameLength; memcpy(pDestBuffer, &this->mData.numMods, 1); pDestBuffer+=1; for (int i=0; i