Janilabo

Janilabo | ItemDB (Simba)

Aug 7th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.71 KB | None | 0 0
  1. const
  2.   NAME_SEARCH = VK_F10;
  3.   ID_SEARCH = VK_F11;
  4.   STOP = VK_F12; // Doesn't stop script if search is in process.
  5.  
  6. type
  7.   TItem = record
  8.     name, description: string;
  9.     basePrice: Integer;
  10.     wieldable, stackable: Boolean;
  11.   end;
  12.   TItemDatabase = array of TItem;
  13.  
  14. var
  15.   ItemDB: TItemDatabase;
  16.   IDs: TIntegerArray;
  17.   t, item_count: Integer;
  18.  
  19. function Expand(trgt_pos: Integer; s1, s2, str: string): string;
  20. var
  21.   l, p, start, finish, s1L, s2L: Integer;
  22.   tmp: string;
  23. begin
  24.   l := Length(str);
  25.   s1L := Length(s1);
  26.   s2L := Length(s2);
  27.   if ((s1L > l) or (s2L > l) or (trgt_pos > l) or (trgt_pos < 1)) then
  28.     Exit;
  29.   finish := PosEx(s2, str, trgt_pos);
  30.   if ((finish < 1) or (finish = trgt_pos)) then
  31.     Exit;
  32.   tmp := Copy(str, 1, trgt_pos);
  33.   repeat
  34.     p := PosEx(s1, tmp, (p + 1));
  35.     if (p > 0) then
  36.       start := p;
  37.   until (p <= 0);
  38.   if ((start <> trgt_pos) and (start > 0) and (finish > 0) and (start < finish)) then
  39.     Result := Copy(str, (start + s1L), ((finish - start) - s2L));
  40.   tmp := '';
  41. end;
  42.  
  43. function LoadXMLItemDB(file_path: string): TItemDatabase;
  44. var
  45.   i, xmlF: Integer;
  46.   xmlFD, tmp: string;
  47. begin
  48.   if (Lowercase(Copy(file_path, (Length(file_path) - 3), 4)) <> '.xml') then
  49.     Exit;
  50.   if FileExists(file_path) then
  51.   begin
  52.     xmlF := OpenFile(file_path, False);
  53.     ReadFileString(xmlF, xmlFD, FileSize(xmlF));
  54.     CloseFile(xmlF);
  55.     xmlFD := Between('<ItemDef-array>', '</ItemDef-array>', xmlFD);
  56.     if (xmlFD <> '') then
  57.     begin
  58.       SetLength(Result, 439);
  59.       for i := 0 to 438 do
  60.       begin
  61.         tmp := Expand(Pos(('<sprite>' + IntToStr(i) + '</sprite>'), xmlFD), '<ItemDef>', '</ItemDef>', xmlFD);
  62.         with Result[i] do
  63.         begin
  64.           name := Between('<name>', '</name>', tmp);
  65.           description := Between('<description>', '</description>', tmp);
  66.           basePrice := StrToIntDef(Between('<basePrice>', '</basePrice>', tmp), 0);
  67.           wieldable := StrToBoolDef(Between('<wieldable>', '</wieldable>', tmp), False);
  68.           stackable := StrToBoolDef(Between('<stackable>', '</stackable>', tmp), False);
  69.         end;
  70.         tmp := '';
  71.       end;
  72.     end;
  73.     xmlFD := '';
  74.   end;
  75. end;
  76.  
  77. function SearchItemsByID: TIntegerArray;
  78. var
  79.   h, i, c: Integer;
  80.   s: string;
  81.   tmp: TStringArray;
  82. begin
  83.   if InputQuery('Search Items with ID', 'Please enter Item IDs below, separate with comma (,).', s) then
  84.     if (s <> '') then
  85.     begin
  86.       s := Replace(s, ' ', '', [rfReplaceAll]);
  87.       if (s <> '') then
  88.       begin
  89.         tmp := Explode(',', s);
  90.         h := High(tmp);
  91.         SetLength(Result, (h + 1));
  92.         for i := 0 to h do
  93.           if (StrToIntDef(tmp[i], -1) <> -1) then
  94.           begin
  95.             Result[c] := StrToInt(tmp[i]);
  96.             Inc(c);
  97.           end;
  98.         SetLength(tmp, 0);
  99.         SetLength(Result, c);
  100.       end else
  101.         SetLength(Result, 0);
  102.     end else
  103.       SetLength(Result, 0);
  104. end;
  105.  
  106. function SearchItemsByName: TIntegerArray;
  107. var
  108.   s: string;
  109.   i, r: Integer;
  110. begin
  111.   if InputQuery('Search Items with Name', 'Please enter name (part) of the item below.', s) then
  112.     if (s <> '') then
  113.     begin
  114.       s := Trim(Lowercase(s));
  115.       SetLength(Result, item_count);
  116.       for i := 0 to (item_count - 1) do
  117.         if (Pos(s, Lowercase(ItemDB[i].name)) > 0) then
  118.         begin
  119.           Result[r] := i;
  120.           Inc(r);
  121.         end;
  122.       SetLength(Result, r);
  123.     end else
  124.       SetLength(Result, 0);
  125. end;
  126.  
  127. procedure PrintNames(IDs: TIntegerArray);
  128. var
  129.   h, i, c: Integer;
  130.   tmp: TStringArray;
  131. begin
  132.   ClearDebug;
  133.   h := High(IDs);
  134.   if (h < 0) then
  135.   begin
  136.     WriteLn('Search found 0 items.');
  137.     Exit;
  138.   end;
  139.   ClearSameIntegers(IDs);
  140.   h := High(IDs);
  141.   SetLength(tmp, (h + 1));
  142.   for i := 0 to h do
  143.     if InRange(IDs[i], 0, item_count) then
  144.     begin
  145.       tmp[c] := ('Item[' + IntToStr(IDs[i]) + '] - ' + ItemDB[IDs[i]].name);
  146.       Inc(c);
  147.     end;
  148.   SetLength(tmp, c);
  149.   if (c = 1) then
  150.     WriteLn('Search found 1 item:')
  151.   else
  152.     WriteLn('Search found ' + IntToStr(c) + ' items:');
  153.   if (c > 0) then
  154.   begin
  155.     for i := 0 to (c - 1) do
  156.       WriteLn(tmp[i]);
  157.     SetLength(tmp, 0);
  158.   end else
  159.     WriteLn('Search found 0 items.');
  160. end;
  161.  
  162. procedure PrintItems(IDs: TIntegerArray);
  163. var
  164.   h, i: Integer;
  165. begin
  166.   ClearDebug;
  167.   h := High(IDs);
  168.   if (h < 0) then
  169.     Exit;
  170.   ClearSameIntegers(IDs);
  171.   h := High(IDs);
  172.   for i := 0 to h do
  173.     if InRange(IDs[i], 0, item_count) then
  174.     begin
  175.       WriteLn('Item[' + IntToStr(IDs[i]) + ']' + #13#10 +
  176.               'Name: ' + ItemDB[IDs[i]].name + #13#10 +
  177.               'Description: ' + ItemDB[IDs[i]].description);
  178.       if ItemDB[IDs[i]].wieldable then
  179.         WriteLn('Wieldable: Yes')
  180.       else
  181.         WriteLn('Wieldable: No');
  182.       if ItemDB[IDs[i]].stackable then
  183.         WriteLn('Stackable: Yes')
  184.       else
  185.         WriteLn('Stackable: No');
  186.       if (i < h) then
  187.         WriteLn('');
  188.     end;
  189. end;
  190.  
  191. begin
  192.   ClearDebug;
  193.   WriteLn('Loading Item Database - Please wait...');
  194.   t := GetSystemTime;
  195.   ItemDB := LoadXMLItemDB(ScriptPath + 'ItemDef.xml');
  196.   item_count := Length(ItemDB);
  197.   WriteLn('Loaded ' + IntToStr(item_count) + ' items in ' + IntToStr(GetSystemTime - t) + ' ms!');
  198.   WriteLn('READY TO GO!');
  199.   if (item_count < 1) then
  200.   begin
  201.     WriteLn('Failed to load Item Database.');
  202.     TerminateScript;
  203.   end;
  204.   repeat
  205.     if IsKeyDown(NAME_SEARCH) then
  206.     begin
  207.       IDs := SearchItemsByName;
  208.       PrintNames(IDs);
  209.       Wait(250);
  210.     end;
  211.     if IsKeyDown(ID_SEARCH) then
  212.     begin
  213.       IDs := SearchItemsByID;
  214.       PrintItems(IDs);
  215.       Wait(250);
  216.     end;
  217.     Wait(10);
  218.   until IsKeyDown(STOP);
  219.   SetLength(ItemDB, 0);
  220. end.
Advertisement
Add Comment
Please, Sign In to add comment