Advertisement
kolton

Untitled

Sep 16th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. [Item-parser Syntax Information]
  4.  
  5. 1. [Keyword] separates into two groups
  6.    - [Property Keywords] : [Type], [Name], [Class], [Quality], [Flag], [Level], [Prefix], [Suffix]
  7.    - [Stat Keywords] : [Number or Alias]
  8.  
  9. 2. [Keyword] must be surrounded by '[' and ']'
  10.  
  11. 3. [Property Keywords] must be placed first
  12.  
  13. 4. Insert '#' symbol between [Property Keywords] and [Stat Keywords]
  14.  
  15. 5. Use '+', '-', '*', '/', '(', ')', '&&', '||', '>', '>=', '<', '<=', '==', '!=' symbols for comparison
  16.  
  17. 6. Use '//' symbol for comment
  18.  
  19. */
  20.  
  21. Include("libs/common/NTItemAlias.ntl");
  22.  
  23. var _NTIP_CheckList = [];
  24.  
  25. function NTIPOpenFile(filepath)
  26. {
  27.     var _nipfile;
  28.     var _line;
  29.  
  30.     _nipfile = FileOpen(filepath, 0);
  31.  
  32.     if(!_nipfile)
  33.         return false;
  34.  
  35.     while(!_nipfile.eof)
  36.     {
  37.         _line = NTIPParseLineInt(_nipfile.ReadLine());
  38.  
  39.         if(_line)
  40.             _NTIP_CheckList.push(_line);
  41.     }
  42.  
  43.     _nipfile.Close();
  44.  
  45.     return true;
  46. }
  47.  
  48. function NTIPCheckItem(item)
  49. {
  50.     var i;
  51.     var _identified;
  52.     var _result = 0;
  53.  
  54.     _identified = item.itemflag & 0x10;
  55.  
  56.     for(i = 0 ; i < _NTIP_CheckList.length ; i++)
  57.     {
  58.         if(_NTIP_CheckList[i][0].length > 0)
  59.         {
  60.             if(eval(_NTIP_CheckList[i][0]))
  61.             {
  62.                 if(_NTIP_CheckList[i][1].length > 0)
  63.                 {
  64.                     if(eval(_NTIP_CheckList[i][1]))
  65.                     {
  66.                         if(_NTIP_CheckList[i][2] && _NTIP_CheckList[i][2]["MaxQuantity"] && !isNaN(_NTIP_CheckList[i][2]["MaxQuantity"]))
  67.                         {
  68.                             if(NTIP_CheckQuantityOwned(_NTIP_CheckList[i][0],_NTIP_CheckList[i][1], false) < _NTIP_CheckList[i][2]["MaxQuantity"])
  69.                             {
  70.                                 //Print("I need more of these "+NTC_ItemQualityToMGRColor[item.quality] + item.itemdesc.split("\n")[0].substring(3));
  71.                                 return 1;
  72.                             }
  73.                             else
  74.                             {
  75.                                 //Print("I already have enough of these "+NTC_ItemQualityToMGRColor[item.quality] + item.itemdesc.split("\n")[0].substring(3)+" in my stash!");
  76.                                 return 0;
  77.                             }
  78.                         }
  79.                         else
  80.                         {
  81.                             //Print("No maximum for this item");
  82.                             return 1;
  83.                         }
  84.                     }
  85.                     else if(!_identified && _result == 0)
  86.                         _result = -1;
  87.                 }
  88.                 else
  89.                 {
  90.                     if(_NTIP_CheckList[i][2] && _NTIP_CheckList[i][2]["MaxQuantity"] && !isNaN(_NTIP_CheckList[i][2]["MaxQuantity"]))
  91.                     {
  92.                         if(NTIP_CheckQuantityOwned(_NTIP_CheckList[i][0], null, false) < _NTIP_CheckList[i][2]["MaxQuantity"])
  93.                         {
  94.                             //Print("I need more of these "+NTC_ItemQualityToMGRColor[item.quality] + item.itemdesc.split("\n")[0].substring(3));
  95.                             return 1;
  96.                         }
  97.                         else
  98.                         {
  99.                             //Print("I already have enough of these "+NTC_ItemQualityToMGRColor[item.quality] + item.itemdesc.split("\n")[0].substring(3)+" in my stash!");
  100.                             return 0;
  101.                         }
  102.                     }
  103.                     else
  104.                     {
  105.                         //Print("No maximum for this item");
  106.                         return 1;
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.         else if(_NTIP_CheckList[i][1].length > 0)
  112.         {
  113.             if(eval(_NTIP_CheckList[i][1]))
  114.             {
  115.                 if(_NTIP_CheckList[i][2] && _NTIP_CheckList[i][2]["MaxQuantity"] && !isNaN(_NTIP_CheckList[i][2]["MaxQuantity"]))
  116.                 {
  117.                     if(NTIP_CheckQuantityOwned(null, _NTIP_CheckList[i][1], false) < _NTIP_CheckList[i][2]["MaxQuantity"])
  118.                     {
  119.                         //Print("I need more of these "+NTC_ItemQualityToMGRColor[item.quality] + item.itemdesc.split("\n")[0].substring(3));
  120.                         return 1;
  121.                     }
  122.                     else
  123.                     {
  124.                         //Print("I already have enough of these "+NTC_ItemQualityToMGRColor[item.quality] + item.itemdesc.split("\n")[0].substring(3)+" in my stash!");
  125.                         return 0;
  126.                     }
  127.                 }
  128.                 else
  129.                 {
  130.                     return 1;
  131.                 }
  132.             }
  133.             else if(!_identified && _result == 0)
  134.                 _result = -1;
  135.         }
  136.     }
  137.  
  138.     return _result;
  139. }
  140.  
  141. // Internal function
  142. function NTIP_CheckQuantityOwned(item_type, item_stats, check_inventory_too)
  143. {
  144.     var _nb = 0;
  145.     var _items = me.GetItems();
  146.     if (!_items)
  147.     {
  148.         Print("I can't find my items!");
  149.         return 0;
  150.     }
  151.     for(var i = 0 ; i < _items.length ; i++)
  152.     {
  153.         if(_items[i].mode == 0 && _items[i].itemloc == 4)
  154.         {
  155.             var item = _items[i];
  156.             if((item_type != null && item_type.length > 0 && eval(item_type)) || item_type == null)
  157.                 if((item_stats != null && item_stats.length > 0 && eval(item_stats)) || item_stats == null)
  158.                     _nb++;
  159.         }
  160.         else if(check_inventory_too && _items[i].mode == 0 && _items[i].itemloc == 0)
  161.         {
  162.             var item = _items[i];
  163.             if((item_type != null && item_type.length > 0 && eval(item_type)) || item_type == null)
  164.                 if((item_stats != null && item_stats.length > 0 && eval(item_stats)) || item_stats == null)
  165.                     if(NTConfig_Columns[_items[i].y][_items[i].x] > 0) // we check only space that is supposed to be free
  166.                         _nb++;
  167.         }
  168.     }
  169.     //Print("I have "+_nb+" of these.");
  170.     return _nb;
  171. }
  172.  
  173. function NTIPParseLineInt(input)
  174. {
  175.     var i;
  176.     var _start, _end;
  177.     var _section, _keyword;
  178.     var _result;
  179.  
  180.     _end = input.indexOf("//");
  181.     if(_end != -1)
  182.         input = input.substring(0, _end);
  183.  
  184.     input = input.replace(/ |;|\t/g, "").toLowerCase();
  185.  
  186.     if(input.length < 5)
  187.         return null;
  188.  
  189.     _result = input.split("#");
  190.  
  191.     if(_result[0] && _result[0].length > 4)
  192.     {
  193.         _section = _result[0].split("[");
  194.  
  195.         _result[0] = _section[0];
  196.  
  197.         for(i = 1 ; i < _section.length ; i++)
  198.         {
  199.             _end = _section[i].indexOf("]") + 1;
  200.  
  201.             switch(_section[i][0])
  202.             {
  203.             case 't':
  204.                 _result[0] += "item.itemtype";
  205.                 break;
  206.             case 'n':
  207.                 _result[0] += "item.classid";
  208.                 break;
  209.             case 'c':
  210.                 _result[0] += "item.itemclass";
  211.                 break;
  212.             case 'q':
  213.                 _result[0] += "item.quality";
  214.                 break;
  215.             case 'f':
  216.                 if(_section[i][_end] == '!')
  217.                     _result[0] += "!(item.itemflag&";
  218.                 else
  219.                     _result[0] += "(item.itemflag&";
  220.  
  221.                 _end += 2;
  222.                 break;
  223.             case 'l':
  224.                 _result[0] += "item.itemlevel";
  225.                 break;
  226.             case 'p':
  227.                 _result[0] += "item.itemprefix";
  228.                 break;
  229.             case 's':
  230.                 _result[0] += "item.itemsuffix";
  231.                 break;
  232.             default:
  233.                 Print("Unknown Keyword : " + input);
  234.                 break;
  235.             }
  236.  
  237.             for(_start = _end ; _end < _section[i].length ; _end++)
  238.             {
  239.                 if(!NTIPIsSyntaxInt(_section[i][_end]))
  240.                     break;
  241.             }
  242.  
  243.             _result[0] += _section[i].substring(_start, _end);
  244.  
  245.             for(_start = _end ; _end < _section[i].length ; _end++)
  246.             {
  247.                 if(NTIPIsSyntaxInt(_section[i][_end]))
  248.                     break;
  249.             }
  250.  
  251.             _keyword = _section[i].substring(_start, _end);
  252.  
  253.             if(isNaN(_keyword))
  254.             {
  255.                 switch(_section[i][0])
  256.                 {
  257.                 case 't':
  258.                     _result[0] += _NTIPAliasType[_keyword];
  259.                     break;
  260.                 case 'n':
  261.                     _result[0] += _NTIPAliasClassID[_keyword];
  262.                     break;
  263.                 case 'c':
  264.                     _result[0] += _NTIPAliasClass[_keyword];
  265.                     break;
  266.                 case 'q':
  267.                     _result[0] += _NTIPAliasQuality[_keyword];
  268.                     break;
  269.                 case 'f':
  270.                     _result[0] += _NTIPAliasFlag[_keyword] + ")";
  271.                     break;
  272.                 }
  273.             }
  274.             else
  275.             {
  276.                 if(_section[i][0] == 'f')
  277.                     _result[0] += _keyword + ")";
  278.                 else
  279.                     _result[0] += _keyword;
  280.             }
  281.  
  282.             _result[0] += _section[i].substring(_end);
  283.         }
  284.     }
  285.     else
  286.         _result[0] = "";
  287.  
  288.     if(_result[1] && _result[1].length > 4)
  289.     {
  290.         _section = _result[1].split("[");
  291.         _result[1] = _section[0];
  292.  
  293.         for(i = 1 ; i < _section.length ; i++)
  294.         {
  295.             _end = _section[i].indexOf("]");
  296.             _keyword = _section[i].substring(0, _end);
  297.  
  298.             if(_keyword.toLowerCase() == "description")
  299.             {
  300.                 _string = _section[i].split("\"")[1];
  301.                 _result[1] += "(item.itemloc == 0 || item.itemloc == 1) && ";
  302.                 _result[1] += "item.itemdesc.replace(/ |;|\t/g, \"\").toLowerCase().indexOf(\"" + _string + "\") > -1";
  303.                 _result[1] += _section[i].split("\"")[2];
  304.             }
  305.             else
  306.             {
  307.                 if(isNaN(_keyword))
  308.                     _result[1] += "item.GetStatEx(" + _NTIPAliasStat[_keyword] + ")";
  309.                 else
  310.                     _result[1] += "item.GetStatEx(" + _keyword + ")";
  311.  
  312.                 _result[1] += _section[i].substring(_end+1);
  313.             }
  314.         }
  315.     }
  316.     else
  317.         _result[1] = "";
  318.  
  319.     if(_result[2] && _result[2].replace(/^\s+|\s+$/, "").length > 0)
  320.     {
  321.         _section = _result[2].split("[");
  322.         _result[2] = new Array();
  323.  
  324.         for(i = 1 ; i < _section.length ; i++)
  325.         {
  326.             _end = _section[i].indexOf("]");
  327.  
  328.             _keyword = _section[i].substring(0, _end);
  329.  
  330.             if(_keyword.toLowerCase().replace(/^\s+|\s+$/, "") == "maxquantity")
  331.             {
  332.                 _end = _section[i].split("==")[1].replace(/^\s+|\s+$/, "").indexOf("//");
  333.                 if(_end == -1)
  334.                     _end = _section[i].split("==")[1].replace(/^\s+|\s+$/, "").length;
  335.                 var _quantity = parseInt(_section[i].split("==")[1].replace(/^\s+|\s+$/, "").substring(0, _end));
  336.                 _result[2]["MaxQuantity"] = _quantity;
  337.             }
  338.             else
  339.             {
  340.                 Print("Error in your NIP file : unknown 3rd part keyword.");
  341.             }
  342.         }
  343.     }
  344.        
  345.     return _result;
  346. }
  347.  
  348. function NTIPIsSyntaxInt(ch)
  349. {
  350.     return (ch == '!' || ch == '%' || ch == '&' || (ch >= '(' && ch <= '+') || ch == '-' || ch == '/' || (ch >= ':' && ch <= '?') || ch == '|');
  351. }
  352.  
  353. // prototypes
  354.  
  355. Object.prototype.GetStatEx = function (id, subid) {
  356.     var i, desc, string, value;
  357.    
  358.     if (this.itemflag & _NTIPAliasFlag["runeword"]) { // some properties are bugged for runewords
  359.         if (id === 18 && subid === 0) { // enhanced damage
  360.             desc = this.itemdesc;
  361.             desc = desc.split("\n");
  362.             string = GetLocaleString(10023);
  363.            
  364.             for (i = 0; i < desc.length; i += 1) {
  365.                 if (desc[i].indexOf(string.substring(0, string.length - 1)) > -1) {
  366.                     value = parseInt(desc[i].split("+")[1].split("%")[0], 10);
  367.                    
  368.                     return value;
  369.                 }
  370.             }
  371.         } else if (id === 92 && !subid) { // level requirement
  372.             desc = this.itemdesc;
  373.             desc = desc.split("\n");
  374.             string = GetLocaleString(3469);
  375.            
  376.             for (i = 0; i < desc.length; i += 1) {
  377.                 if (desc[i].indexOf(string) > -1) {
  378.                     value = parseInt(desc[i].split(string)[1], 10);
  379.                    
  380.                     return value;
  381.                 }
  382.             }
  383.         }
  384.     }
  385.    
  386.     if (id === 204 && subid === 54) { // teleport charges, doesn't support non-english
  387.         desc = this.itemdesc;
  388.         desc = desc.split("\n");
  389.        
  390.         for (i = 0; i < desc.length; i += 1) {
  391.             if (desc[i].indexOf("Teleport") > -1 && desc[i].indexOf("Charges") > -1) {
  392.                 value = parseInt(desc[i].split("Level ")[1].split(" Charges")[0], 10);
  393.                
  394.                 return value;
  395.             }
  396.         }
  397.     }
  398.    
  399.     if (subid) {
  400.         return this.GetStat(id, subid);
  401.     }
  402.    
  403.     return this.GetStat(id);
  404. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement