// *************************************************************** // Function: CreateWithTemplate.cpp // *************************************************************** #include "ItemDefManager.h" #include "Item.h" #include //************************************ // Name: CreateItemFromDef // // Creates an item from an item definition. Handles templated and non-templated definitions. // // Parameters: // pName - Name of item (The definition name.) to make // pItem - Pointer to an item to fill // pExtraMods - Any extra mods to add // // Return: // true - item created successfully // false - item creation failed //************************************ bool ItemDefManager::CreateItemFromDef(char* pName, Item* pItem, char* pExtraMods) { ////CoreLog.debug("Creating an item!"); //Get the item definition for the name char temp[255]; strcpy(temp, pName); int def=0; char *t=strstr(temp, "("); if (t!=NULL) { ////CoreLog.debug("+ Item is using a template"); *t=0; def=GetItemDef(temp); if (def==-1) return false; strcpy(temp, pName); //move to just past the ( in the template pName=strstr(temp, "(")+1; //remove the closing ) t=strstr(pName, ")"); *t=0; //Tokenize the template pName=strtok(pName, ","); if (pName==NULL) { gCoreLog.warning("Item template invalid"); return false; } } else { def=GetItemDef(temp); if (def==-1) return false; } //Template code for the item id name if (IsTemplate(itemDefs[def].itemIdName[0], pName)) { ////CoreLog.debug("+ Item id name is using a template."); int id=GetItemId(pName, itemDefs[def].profession); if (-1==id) return false; char bytes[2]; Util::ToBytes(itemIds[id].id, bytes, 2); memcpy(&pItem->mData.gwId, bytes, 2); pName=strtok(NULL, ","); } else { ////CoreLog.debug("+ Item id name is using a static value"); int id4=GetItemId(itemDefs[def].itemIdName, itemDefs[def].profession); if (id4==-1) return false; Util::ToBytes(itemIds[id4].id, (char*)&pItem->mData.gwId, 2); } //Template code for graphical options if (IsTemplate(itemDefs[def].graphOpt[2], pName)) { ////CoreLog.debug("+ Graphical options is using a template!");; if (strlen(pName)!=4) { gCoreLog.warning("Template parameter incorrect for graphical options: Param: %s", pName); return false; } char go[2]; Util::ToBytes(pName, go, 2); memcpy(&pItem->mData.graphicalOptions, go, 2); pName=strtok(NULL, ","); } else { ////CoreLog.debug("+ Graphical options is using a static value."); memcpy(&pItem->mData.graphicalOptions, itemDefs[def].graphOpt, 2); } //Template code for upgrade component if (IsTemplate(itemDefs[def].upgradeComp[1], pName)) { ////CoreLog.debug("+ upgrade component is using a template."); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for upgrade component: Param: %s", pName); return false; } char go; Util::ToBytes(pName, &go, 1); memcpy(&pItem->mData.upgradeComponent, &go, 1); pName=strtok(NULL, ","); } else { //debug("+ upgrade component is using a static value."); pItem->mData.upgradeComponent=itemDefs[def].upgradeComp[0]; } //Template code for color if (IsTemplate(itemDefs[def].color[3], pName)) { ////CoreLog.debug("+ Color is using a template."); if (strlen(pName)!=6) { gCoreLog.warning("Template parameter incorrect for color: Param: %s", pName); return false; } char go[3]; Util::ToBytes(pName, go, 3); memcpy(pItem->mData.color, go, 3); pName=strtok(NULL, ","); } else { ////CoreLog.debug("+ Color is using a static value.");; memcpy(pItem->mData.color, itemDefs[def].color, 3); } //Template code for unknown1 if (IsTemplate(itemDefs[def].u1[1], pName)) { ////CoreLog.debug("+ u1 is using a template."); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for u1: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.unknownBlock1, &u, 1); pName=strtok(NULL, ","); } else { ////CoreLog.debug("+ u1 is using a static value."); memcpy(&pItem->mData.unknownBlock1, &itemDefs[def].u1, 1); } //Template code for unknown2 if (IsTemplate(itemDefs[def].u2[1], pName)) { //CoreLog.debug("+ u2 is using a template. "); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for u2: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.unknownBlock2, &u, 1); pName=strtok(NULL, ","); } else { //CoreLog.debug("+ u2 is using a static value."); memcpy(&pItem->mData.unknownBlock2, &itemDefs[def].u2, 1); } //Template code for unknown3 if (IsTemplate(itemDefs[def].u3[1], pName)) { //CoreLog.debug("+ u3 is using a template."); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for u3: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.unknownBlock3, &u, 1); pName=strtok(NULL, ","); } else { //CoreLog.debug("+ u3 is using a static value.");; memcpy(&pItem->mData.unknownBlock3, &itemDefs[def].u3, 1); } //Template code for uniqueness if (IsTemplate(itemDefs[def].uniqueness[1], pName)) { //CoreLog.debug("+ uniqueness is using a template."); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for uniqueness: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.uniqueness, &u, 1); pName=strtok(NULL, ","); } else { //CoreLog.debug("+ uniqueness is using a static value."); memcpy(&pItem->mData.uniqueness, &itemDefs[def].uniqueness, 1); } //Template code for unknown4 if (IsTemplate(itemDefs[def].u4[1], pName)) { //CoreLog.debug("+ u4 is using template."); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for u4: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.unknownBlock4, &u, 1); pName=strtok(NULL, ","); } else { //CoreLog.debug("+ u4 using static.");; memcpy(&pItem->mData.unknownBlock4, &itemDefs[def].u4, 1); } //Template code for rarity if (IsTemplate(itemDefs[def].rarity[1], pName)) { //CoreLog.debug("+ rarity is using a template."); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for rarity: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.rarity, &u, 1); pName=strtok(NULL, ","); } else { //CoreLog.debug("+ rarity is using a static value.");; memcpy(&pItem->mData.rarity, &itemDefs[def].rarity, 1); } //Template code for unknown5 if (IsTemplate(itemDefs[def].u5[1], pName)) { //CoreLog.debug("+ u5 is using a template.");; if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for u5: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.unknownBlock5, &u, 1); pName=strtok(NULL, ","); } else { //CoreLog.debug("+ u5 is using a static value.");; memcpy(&pItem->mData.unknownBlock5, &itemDefs[def].u5, 1); } //Template code for unknown6 if (IsTemplate(itemDefs[def].u6[1], pName)) { //CoreLog.debug("+ u6 is using a template."); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for u6: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.unknownBlock6, &u, 1); pName=strtok(NULL, ","); } else { //CoreLog.debug("+ u6 is using a static value."); memcpy(&pItem->mData.unknownBlock6, &itemDefs[def].u6, 1); } //Template code for unknown7 if (IsTemplate(itemDefs[def].u7[1], pName)) { //CoreLog.debug("+ u7 is using a template."); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for u7: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.unknownBlock7, &u, 1); pName=strtok(NULL, ","); } else { //CoreLog.debug("+ u7 is using a static value."); memcpy(&pItem->mData.unknownBlock7, &itemDefs[def].u7, 1); } //Template code for unknown8 if (IsTemplate(itemDefs[def].u8[1], pName)) { //CoreLog.debug("+ u8 is using a template."); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for u8: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.unknownBlock8, &u, 1); pName=strtok(NULL, ","); } else { //CoreLog.debug("+ u8 is using a static value."); memcpy(&pItem->mData.unknownBlock8, &itemDefs[def].u8, 1); } //Template code for unknown6 if (IsTemplate(itemDefs[def].u9[1], pName)) { //CoreLog.debug("+ u9 is using a template."); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for u9: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.unknownBlock9, &u, 1); pName=strtok(NULL, ","); } else { //CoreLog.debug("+ u9 is using a static value."); memcpy(&pItem->mData.unknownBlock9, &itemDefs[def].u9, 1); } //Template code for unknown10 if (IsTemplate(itemDefs[def].u10[1], pName)) { //CoreLog.debug("+ u10 is using a template."); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for u10: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.unknownBlock10, &u, 1); pName=strtok(NULL, ","); } else { //CoreLog.debug("+ u11 is using a static value."); memcpy(&pItem->mData.unknownBlock10, &itemDefs[def].u10, 1); } //Template code for unknown11 if (IsTemplate(itemDefs[def].u11[1], pName)) { //CoreLog.debug("+ u11 is using a template."); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for u11: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.unknownBlock11, &u, 1); pName=strtok(NULL, ","); } else { //CoreLog.debug("+ u11 is using a static value."); memcpy(&pItem->mData.unknownBlock11, &itemDefs[def].u11, 1); } //Template code for unknown11 if (IsTemplate(itemDefs[def].u12[1], pName)) { //CoreLog.debug("+ u12 is using a template."); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for u12: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.unknownBlock12, &u, 1); pName=strtok(NULL, ","); } else { //CoreLog.debug("+ u12 is using a static value."); memcpy(&pItem->mData.unknownBlock12, &itemDefs[def].u12, 1); } //Template code for unknown11 if (IsTemplate(itemDefs[def].u13[1], pName)) { //CoreLog.debug("+ u13 is using a template."); if (strlen(pName)!=2) { gCoreLog.warning("Template parameter incorrect for u13: Param: %s", pName); return false; } char u; Util::ToBytes(pName, &u, 1); memcpy(&pItem->mData.unknownBlock13, &u, 1); pName=strtok(NULL, ","); } else { //CoreLog.debug("+ u13 is using a static value."); memcpy(&pItem->mData.unknownBlock13, &itemDefs[def].u13, 1); } //Template code for itemNameId if (IsTemplate(itemDefs[def].itemNameId[0], pName)) { //CoreLog.debug("+ itemNameId is using a template."); //CoreLog.debug("+ Name: %s", name); int id=GetNameId(pName); if (id==-1) return false; char bytes[20]; memset(bytes, 0, 20); Util::ToBytes(names[id].id, bytes, names[id].idLength); memcpy(&pItem->mData.itemName, bytes, names[id].idLength); pItem->mData.itemNameLength=names[id].idLength; pName=strtok(NULL, ","); } else { //CoreLog.debug("+ itemNameID is using a static value"); int id=GetNameId(itemDefs[def].itemNameId); if (id==-1) return false; Util::ToBytes(names[id].id, pItem->mData.itemName, names[id].idLength); pItem->mData.itemNameLength=names[id].idLength; } //Template the mods pItem->mData.numMods=0; if (itemDefs[def].mods[0]!=0) { //CoreLog.debug("+ Processing item mods..."); //Break down the mod list by , char mods2[500]; strcpy(mods2, itemDefs[def].mods); if (pExtraMods!=NULL) strcat(mods2, pExtraMods); char *modpart=strtok(mods2, ","); //Process all the segmented data pieces //The 1st one is the mod variables //The 2nd one is the mod id //clear our item's mods memset(pItem->mData.mod, 0, sizeof(int)*30); //initialize tbe mod count and loop through all the data segments int modnum=0; //CoreLog.debug("+ Item Modifiers..."); while (modpart!=NULL) { //If this data is not valid for a mod then break if (modpart[0]==NULL){break;} //If the data segment is not 4 bytes in length, fail if (strlen(modpart)!=4) { gCoreLog.warning("Item Creation failed. Mod values are incorrect. Param: %s", modpart); return false; } //Find the x's in the template part char *t=strstr(modpart, "x"); if (t==0){t=strstr(modpart, "X");} //If there is no parameter specified to fill in the template with, then fail if (t!=0 && pName==NULL) { gCoreLog.warning("Improper amount of mod parameters."); return false; } //If the mod variable has an x in it but is not xxxx or XXXX then fail if (t!=0 && memcmp(t, "xxxx", 4)!=0 && memcmp(t, "XXXX", 4)!=0) { gCoreLog.warning("Improper X designation in mod."); return false; } //Make sure the template variable is 4 bytes in length if (t!=0 && strlen(pName)!=4) { gCoreLog.warning("Mod template variables not proper length. MOD: %s", pName); return false; } //If this mod is a template then replace the X's with the parameters if (t!=0) { memcpy(t, pName, 4); } //CoreLog.debug("+ %s", modpart); //CoreLog.debug(" ------ "); //set the variables part of the item mod Util::ToBytes(modpart, (char*)&(pItem->mData.mod[modnum]), 2); modpart=strtok(NULL, ","); //CoreLog.debug("%s", modpart); //set the id part of the item mod int im=GetModId(modpart); if (im==-1) return false; Util::ToBytes(mods[im].id, (char*)&(pItem->mData.mod[modnum])+2, 2);//*/ modpart=strtok(NULL, ","); //increment what mod we are on modnum++; } pItem->mData.numMods=modnum; } //Set the other stuff that has not been set yet pItem->mData.customized[0]=0; pItem->mData.quantity=1; pItem->mData.profession=itemDefs[def].profession; //Output binary dump of the item //GWITEM *gwi=item->GetRAWItemData(); //CoreLog.LogKnownData("Item Dump", (unsigned char*)gwi, sizeof(GWITEM)); return true; }