zamaro

World Tracker by Frement/Samerdl

May 3rd, 2014
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.19 KB | None | 0 0
  1. Program WorldTracker;
  2.  
  3. { ++++ World Population Tracker for Oldschool RS +++ }
  4. { ++ Written by Frement, minor edits by Samerdl +++ }
  5.  
  6.  
  7. const
  8. ManualRefresh = false; // Make it so you have to click Refresh to re-load data.
  9. Sleep_Intervals = 500; // Wait time between refresh rates.
  10. MinCount = 3; // When a world increase this amount, it will show it green.
  11. MsgBoxe = true; // Display a message box warning when a world increase above WarnCount.
  12. WarnCount = 8; // How many players should the world increase for a warn message.
  13. XDistance = 70; // Formating of the text, X.
  14. Ydistance = 19; // Formating of the text, Y.
  15.  
  16. type
  17. TWorld = record // loading up a record of data.
  18. World, Players: Integer; // Two vars, World and Players.
  19. end;
  20.  
  21. TDisplay = record // Display Record.
  22. Surface: Integer; // The Display is a bitmap which is debugged using ImgForm
  23. Width, Height: Integer; // Width and Height definition.
  24. end;
  25.  
  26.  
  27. TWorldArray = Array of TWorld; // Defining that TworldArray is an Array of Tworld, so we can call it in the script.
  28.  
  29. function World(__World, __Players: Integer): TWorld; // Creating a function that is a Tworld.
  30. begin
  31. Result.World := __World; // What do the two underscores mean?
  32. Result.Players := __Players; // kinda of hard to understand this part, we are basically defining what Tworld is?
  33. end;
  34.  
  35. var
  36. MSGBoXES: boolean;
  37.  
  38. function SortWorlds(Worlds: TWorldArray): TWorldArray;
  39. var
  40. I, J, K: Integer;
  41. begin
  42. K := 301;
  43. repeat
  44. for I := 0 to High(Worlds) do begin // Loops each world 61 times (-1)
  45. if (Worlds[I].World = K) then begin // After the world slot is done 61 times, it increase slot to two and loops it 61 times again (-1)
  46. Inc(J);
  47. SetLength(Result, J);
  48. Result[J - 1] := Worlds[I];
  49. end;
  50. end;
  51. Inc(K); // After each loop it will increase K by 1 number.
  52. until(Length(Result) = Length(Worlds)); // This part i dont understand, how does the script know what worlds length is? It's not really defined is it?.
  53. end;
  54.  
  55. function CalculateDifference(LC, CC: TWorldArray): TWorldArray;
  56. var
  57. I: Integer;
  58. begin
  59. SetLength(Result, Length(LC)); // Sets the length of the Array.
  60. for I := 0 to High(LC) do begin // Loops all the world results.
  61. Result[I] := World(LC[I].World, CC[I].Players - LC[I].Players); // Then calculates the difference between The players in the array, in this case CC is the current world data.
  62. end; // So its Original Player count - Current player count (after script finishes the loop.
  63. end; // Which gives us the result for each world calculated, inside the array of loop.
  64.  
  65. // SO.. if i understood this correctly so far:
  66. // It sorts the worlds in The array, loops each world 62 times then gives it a minus 1, till it reaches 61 slots total. Sorts them from 301 to 378.
  67. // After that, It will calculate the difference, when we call Currentworld data it will load up the world data and then we'll loop it again and calculate the difference between all worlds - in real time player count.
  68.  
  69.  
  70. function GetData: String;
  71. var
  72. HTTPClient: Integer;
  73. begin
  74. HTTPClient := InitializeHTTPClient(False); // This creats a new client and assigns it an ID, used for web functions
  75. Result := GetHTTPPage(HTTPClient, 'http://oldschool.runescape.com/slu'); // gets the URL data - raw data, No post AddPostVariable needed here. GetHttpPage is same as GetPage except it lets us choose what client we want our data from.
  76. FreeHTTPClient(HTTPClient); // Frees the client to prevent memory leaks.
  77. end;
  78.  
  79. function GetWorldData: TWorldArray;
  80. var
  81. Pieces, WorldPieces: TStringArray;
  82. I, J: Integer;
  83. begin // here we are calling a function within a function... never thought about that lol.
  84. Pieces := MultiBetween(GetData, 'e(', '");'); // This grabs each line of world data, from the start e(3... till the end ,""), grabs the spacing between them, what it eventually does is remove all the spacing between the lines.
  85. for I := 0 to High(Pieces) do begin // Starts a loop from world 301 till 378.
  86. if (Pos('oldschool', Pieces[I]) > 0) then begin // If the position of the world oldschool till the end of the line is bigger than zero (fail safe i suppose).
  87. WorldPieces := Explode(',', Pieces[I]); // It deletes all the commas from our text array.
  88. Inc(J); // loops it total 61 times and each time it adds a new world to the array, including the number of the world and population.
  89. SetLength(Result, J); // Sets the length of the result in ascending
  90. Result[J - 1] := World(StrToInt(WorldPieces[0]), StrToInt(WorldPieces[4])); //This part i don't understand at all, when you put worldpieces to any other number other than 0 and 4 the script wont compile :Error: Exception: "false" is an invalid integer at line 73
  91. end;
  92. end;
  93. end;
  94.  
  95.  
  96. //Display Functionality
  97. function Display(Width, Height: Integer): TDisplay;
  98. begin
  99. Result.Surface := CreateBitmap(Width, Height); // Creates a bitmap as surface.
  100. Result.Width := Width; // Defines its width.
  101. Result.Height := Height; // And height.
  102. DisplayDebugImgWindow(Result.Width, Result.Height); // Displays the bitmap.
  103. end;
  104.  
  105. procedure ClearDisplay(__Display: TDisplay);
  106. begin
  107. FastDrawClear(__Display.Surface, clBlack); // Draws Color on every Pixel of the Bitmap (Thus creating a Background).
  108. end;
  109.  
  110. procedure FlushDisplay(__Display: TDisplay);
  111. begin
  112. DrawBitmapDebugImg(__Display.Surface); // Drawing.
  113. end;
  114.  
  115. procedure FreeDisplay(__Display: TDisplay);
  116. begin
  117. FreeBitmap(__Display.Surface); // Frees the Bitmap.
  118. end;
  119.  
  120. procedure DisplayDrawText(__Display: TDisplay; Text, Font: string; X, Y, Color: Integer);
  121. var
  122. TPA: TPointArray;
  123. W, H: Integer;
  124. begin
  125. TPA := TPAFromText(Text, Font, W, H); // Loads The world data info as a TPA.
  126. OffsetTPA(TPA, Point(X, Y)); // Offsets the TPA
  127. DrawTPABitmap(__Display.Surface, TPA, Color); // Draws it on the ImgForm.
  128. end;
  129.  
  130. procedure AnsweredNo;
  131. begin
  132. Writeln('Disabling MsgBox for world Tracking...');
  133. MSGBoXES := false; // Used to disable further MsgBox warnings when a player clicks 'cancel'.
  134. end;
  135.  
  136. procedure DisplayDrawWorldInfo(__Display: TDisplay; World, Players, X, Y: Integer);
  137. var
  138. TPA: TPointArray;
  139. WorldInfos, C, W, W2, H, H2: Integer;
  140. begin
  141. if (Players < 0) then // If Player count is negative then it draws red.
  142. C := 255;
  143. if (Players = 0) or ((Players < MinCount) and (Players > 0)) then
  144. c := 16777215; // if Player count is between 0 - mincount then draws white.
  145. if (Players >= MinCount) then
  146. C := 65280; // If its above then it draws green.
  147. TPA := TPAFromText(ToStr(World) + ':', 'SmallChars', W, H); // Loops the world Data.
  148. OffsetTPA(TPA, Point(X, Y)); // Offsets them in order.
  149. DrawTPABitmap(__Display.Surface, TPA, clYellow); // Draws the world Data.
  150. TPA := TPAFromText(ToStr(Players), 'SmallChars', W2, H2);// Loops the Player pop data.
  151. OffsetTPA(TPA, Point(X + W + 2, Y)); // Offsets them in order.
  152. DrawTPABitmap(__Display.Surface, TPA, C); // Draws it (Thus adding World + Player info.
  153. if (ManualRefresh = true) then // If manual refresh is Enabled, then it will draw a Refresh button, so to speak.
  154. DisplayDrawText(__Display, 'Refresh', 'SmallChars', 225, 310, 16744192);
  155. if (Players >= WarnCount) and (MSGBoXES = true) then
  156. begin
  157. WorldInfos := MessageBox('World: ' + IntToSTr(World) + ' Just went up by: ' + IntToStr(Players) + ' - Players', 'World Notice', 1);
  158. writeln(ToStr(WorldInfos));
  159. case (WorldInfos) of
  160. 1: Exit; // If a player Clicks ok then it exists the message box..
  161. 2: AnsweredNo;// If a player clicks cancel then it will disable further message box warnings.
  162. end;
  163. end;
  164. end;
  165.  
  166. procedure DrawWorldData(__Display: TDisplay; Worlds: TWorldArray);
  167. var
  168. I, XOffset, YOffset, X, Y: Integer;
  169. begin
  170. ClearDisplay(__Display); // Resets the Display each time with black background.
  171. XOffset := XDistance; // Off Sets X/Y distance to be displayed in order.
  172. YOffset := YDistance;
  173. DisplayDrawText(__Display, ' OSRS World Tracker - By Frement', 'SmallChars', 30, 10, 16744192);
  174. for I := 0 to High(Worlds) do // Loops all the worlds
  175. begin // Loops all the world Data
  176. DisplayDrawWorldInfo(__Display, Worlds[I].World, Worlds[I].Players, 20 + (XOffset * X), 40 + (YOffset * Y));
  177. Inc(Y); // Increases the Y spacing on each world
  178. if (Y > 15) then
  179. begin
  180. Y := 0; // if the Y goes beyond the limit, it resets it..
  181. Inc(X); // And then increases the X thus creating a new Column
  182. end;
  183. end;
  184. FlushDisplay(__Display); // Draws the Bitmap
  185. end;
  186.  
  187. var
  188. LastCount, CurrentCount, Diff: TWorldArray;
  189. __Display: TDisplay;
  190. Box1: Tbox;
  191.  
  192.  
  193. // 'Client' Display Functionality, used to read the refresh click area.
  194. // Taken from SmartParams file :-).
  195. function FindAndSetTarget(TitlePrefix: string; SetAsTarget: Boolean): Boolean;
  196. var
  197. T: TSysProcArr;
  198. I: Integer;
  199. begin
  200. T := GetProcesses; // Gets all the process.
  201. for I := High(T) downto 0 do // And loops..
  202. if Pos(TitlePrefix, T[I].Title) <> 0 then // Finds the Process with a similar title..
  203. begin
  204. Result := True; // Once it does,
  205. if SetAsTarget then // If a user choses to make it active then..
  206. SetTarget(T[I]); // It will set it as 'Active' Target!.
  207. Exit;
  208. end;
  209. end;
  210.  
  211.  
  212. //Taken from SmartParams file as well.
  213. procedure GetRealMousePos(var X, Y: Integer);
  214. var
  215. KMTarget, ITarget: Integer;
  216. begin
  217. {$IFDEF LAPE}
  218. writeln('GetRealMousePos not implemented yet in SRL-5 Lape!');
  219. TerminateScript;
  220. {$ELSE}
  221. KMTarget := GetKeyMouseTarget;
  222. ITarget := GetImageTarget;
  223. FindAndSetTarget('DebugImgForm', True);
  224. GetTClient.IOManager.GetMousePos(X, Y);
  225. FreeTarget(GetImageTarget);
  226. SetKeyMouseTarget(KMTarget);
  227. SetImageTarget(ITarget); // basically sets specific mouse area to that active client.
  228. {$ENDIF}
  229. end;
  230.  
  231. function IsRealMouseInBox(B: TBox): Boolean;
  232. var
  233. P: TPoint;
  234. begin
  235. GetRealMousePos(P.X, P.Y); // Uses the above command to get the mouse position on target.
  236. if PointInBox(P, B) then // And if the mouse is inside a Tbox area then..
  237. begin
  238. if ismouseButtonDown(Mouse_left) then // If a player does a mouse_left click in that area
  239. result := true; // THen it will read it as a 'refresh' its not a real button lol.
  240. end;
  241. end;
  242.  
  243. procedure OnTerminate;
  244. begin // Frees the bitmap on terminate, to prevent memory leaks.
  245. FreeDisplay(__Display);
  246. end;
  247.  
  248. procedure refresh;
  249. begin
  250. cleardebug; // Clears Debug.
  251. LastCount := SortWorlds(GetWorldData); ///Calls 3 functions within each other, 1) Grabs the Data then delets all the spacing between lines and useless information, 2) Creates all 61 worlds (-1) with player count and sorts them in an array. This gives us the current Count.
  252. Sleep(Sleep_Intervals); // Does a small wait between the data grab..
  253. CurrentCount := SortWorlds(GetWorldData); // Grabs the current data. Same as above.
  254. Diff := CalculateDifference(LastCount, CurrentCount); // Calculates the differece between eachworld
  255. DrawWorldData(__Display, Diff);
  256. SetTargetBitmap(__Display.Surface);
  257. end;
  258.  
  259. begin
  260. __Display := Display(305, 350); // __Sets the Height/Width of the Bitmap and displays it.
  261. AddOnTerminate('OnTerminate');
  262. FindAndSetTarget('DebugImgForm', true); // Sets the Debugged image as target.
  263. box1 := IntToBox(223, 325, 274, 350); // Defines where the "Refresh" button area is.
  264. MSGBoXES := MsgBoxe; // Relays MsgBoxes data (so it can be set to false if a user clicks 'cancel'
  265. repeat
  266. if (ManualRefresh = true) then // if it's manual refresh then.
  267. begin
  268. if IsRealMouseInBox(box1) then // It will only refresh Data when a player clicks on the 'refresh' button..
  269. refresh
  270. end else // Else (if refresh is disabled) then it will just loop it.
  271. refresh;
  272. until (False);
  273. end.
Add Comment
Please, Sign In to add comment