#include "x2_inc_itemprop"

//This is actually the main function.
void script(object oItem, object oPC)
{
     int nCurrentApp = GetItemAppearance(oItem, ITEM_APPR_TYPE_WEAPON_MODEL, ITEM_APPR_WEAPON_MODEL_TOP );
     int nNewApp;
     switch (nCurrentApp)
     {
     case 8: nNewApp = 1; break;
     default: nNewApp = nCurrentApp+1; break;
     }

     object oModified = CopyItemAndModify(oItem, ITEM_APPR_TYPE_WEAPON_MODEL, ITEM_APPR_WEAPON_MODEL_TOP, nNewApp, TRUE);
     SetItemCursedFlag(oModified, TRUE);
     if (oModified != OBJECT_INVALID) DestroyObject(oItem);
     return;
}

void main()
{
     object oPC = GetPCSpeaker();
     int nSlot = 0;
     object oItem;

     //Check if the item being crafted is equipped
     while (nSlot < 18)
     {
     oItem = GetItemInSlot(nSlot, oPC);
     if (GetLocalInt(oItem, "Prototype") == TRUE) {script(oItem, oPC); return; }
     nSlot++;
     }
     //If the item is not equipped, search for it in the inventory
     oItem = GetFirstItemInInventory(oPC);
     while (GetIsObjectValid(oItem))
     {
     if (GetLocalInt(oItem, "Prototype") == TRUE) {script(oItem, oPC); return; }
     oItem = GetNextItemInInventory(oPC);
     }
}