/plugins/source/RefCalculationsLib.pas

http://github.com/Drags111/Reflection_Dev · Pascal · 219 lines · 183 code · 27 blank · 9 comment · 13 complexity · 0767aba38a2808b41254e12158e3729c MD5 · raw file

  1. library RefCalculationsLib;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Classes, Sysutils, math;
  5. type
  6. T2DIntegerArray = array of array of Integer;
  7. function DijkstraDist(StartX, StartY, DestX, DestY: Integer; IsObject: Boolean; Blocks: T2DIntegerArray): Integer;
  8. var
  9. prev, dist: array of array of Integer;
  10. path_x, path_y: array of Integer;
  11. xx, yy, curr_x, curr_y, path_ptr, step_ptr, pathLength, goal, cost: Integer;
  12. foundPath: Boolean;
  13. begin
  14. try
  15. SetLength(prev, 104);
  16. SetLength(dist, 104);
  17. for xx := 0 to 103 do
  18. begin
  19. SetLength(prev[xx], 104);
  20. SetLength(dist[xx], 104);
  21. end;
  22. for xx := 0 to 103 do
  23. for yy := 0 to 103 do
  24. begin
  25. prev[xx][yy] := 0;
  26. dist[xx][yy] := 99999999;
  27. end;
  28. SetLength(path_x, 4000);
  29. SetLength(path_y, 4000);
  30. curr_x := startX;
  31. curr_y := startY;
  32. prev[startX][startY] := 99;
  33. dist[startX][startY] := 0;
  34. path_ptr := 0;
  35. step_ptr := 0;
  36. path_x[path_ptr] := startX;
  37. path_y[path_ptr] := startY;
  38. Inc(path_ptr);
  39. pathLength := 4000;
  40. foundPath := false;
  41. case IsObject of
  42. true: goal := 1;
  43. false: goal := 0;
  44. end;
  45. while(step_ptr <> path_ptr)do
  46. begin
  47. curr_x := path_x[step_ptr];
  48. curr_y := path_y[step_ptr];
  49. if(Abs(curr_x - destX) + Abs(curr_y - destY) = goal)then
  50. begin
  51. foundPath := true;
  52. break;
  53. end;
  54. step_ptr := (step_ptr + 1) mod pathLength;
  55. cost := dist[curr_x][curr_y] + 1;
  56. //south
  57. if((curr_y > 0)
  58. and (prev[curr_x][(curr_y - 1)] = 0)
  59. and ((blocks[(curr_x + 1)][curr_y] and $1280102) = 0))then
  60. begin
  61. path_x[path_ptr] := curr_x;
  62. path_y[path_ptr] := curr_y - 1;
  63. path_ptr := (path_ptr + 1) mod pathLength;
  64. prev[curr_x][curr_y - 1] := 1;
  65. dist[curr_x][curr_y - 1] := cost;
  66. end;
  67. // west
  68. if((curr_x > 0)
  69. and (prev[curr_x - 1][curr_y] = 0)
  70. and ((blocks[curr_x][curr_y + 1] and $1280108) = 0))then
  71. begin
  72. path_x[path_ptr] := curr_x - 1;
  73. path_y[path_ptr] := curr_y;
  74. path_ptr := (path_ptr + 1) mod pathLength;
  75. prev[curr_x - 1][curr_y] := 2;
  76. dist[curr_x - 1][curr_y] := cost;
  77. end;
  78. // north
  79. if((curr_y < 102)
  80. and (prev[curr_x][curr_y + 1] = 0)
  81. and ((blocks[curr_x + 1][curr_y + 2] and $1280120) = 0))then
  82. begin
  83. path_x[path_ptr] := curr_x;
  84. path_y[path_ptr] := curr_y + 1;
  85. path_ptr := (path_ptr + 1) mod pathLength;
  86. prev[curr_x][curr_y + 1] := 4;
  87. dist[curr_x][curr_y + 1] := cost;
  88. end;
  89. // east
  90. if ((curr_x < 102)
  91. and (prev[curr_x + 1][curr_y] = 0)
  92. and ((blocks[curr_x + 2][curr_y + 1] and $1280180) = 0))then
  93. begin
  94. path_x[path_ptr] := curr_x + 1;
  95. path_y[path_ptr] := curr_y;
  96. path_ptr := (path_ptr + 1) mod pathLength;
  97. prev[curr_x + 1][curr_y] := 8;
  98. dist[curr_x + 1][curr_y] := cost;
  99. end;
  100. // south west
  101. if((curr_x > 0) and (curr_y > 0)
  102. and (prev[curr_x - 1][curr_y - 1] = 0)
  103. and ((blocks[curr_x][curr_y] and $128010E) = 0)
  104. and ((blocks[curr_x][curr_y + 1] and $1280108) = 0)
  105. and ((blocks[curr_x + 1][curr_y] and $1280102) = 0))then
  106. begin
  107. path_x[path_ptr] := curr_x - 1;
  108. path_y[path_ptr] := curr_y - 1;
  109. path_ptr := (path_ptr + 1) mod pathLength;
  110. prev[curr_x - 1][curr_y - 1] := 3;
  111. dist[curr_x - 1][curr_y - 1] := cost;
  112. end;
  113. // north west
  114. if((curr_x > 0) and (curr_y < 102)
  115. and (prev[curr_x - 1][curr_y + 1] = 0)
  116. and ((blocks[curr_x][curr_y + 2] and $1280138) = 0)
  117. and ((blocks[curr_x][curr_y + 1] and $1280108) = 0)
  118. and ((blocks[curr_x + 1][curr_y + 2] and $1280120) = 0))then
  119. begin
  120. path_x[path_ptr] := curr_x - 1;
  121. path_y[path_ptr] := curr_y + 1;
  122. path_ptr := (path_ptr + 1) mod pathLength;
  123. prev[curr_x - 1][curr_y + 1] := 6;
  124. dist[curr_x - 1][curr_y + 1] := cost;
  125. end;
  126. // south east
  127. if((curr_x < 102) and (curr_y > 0)
  128. and (prev[curr_x + 1][curr_y - 1] = 0)
  129. and ((blocks[curr_x + 2][curr_y] and $1280183) = 0)
  130. and ((blocks[curr_x + 2][curr_y + 1] and $1280180) = 0)
  131. and ((blocks[curr_x + 1][curr_y] and $1280102) = 0))then
  132. begin
  133. path_x[path_ptr] := curr_x + 1;
  134. path_y[path_ptr] := curr_y - 1;
  135. path_ptr := (path_ptr + 1) mod pathLength;
  136. prev[curr_x + 1][curr_y - 1] := 9;
  137. dist[curr_x + 1][curr_y - 1] := cost;
  138. end;
  139. // north east
  140. if((curr_x < 102) and (curr_y < 102)
  141. and (prev[curr_x + 1][curr_y + 1] = 0)
  142. and ((blocks[curr_x + 2][curr_y + 2] and $12801E0) = 0)
  143. and ((blocks[curr_x + 2][curr_y + 1] and $1280180) = 0)
  144. and ((blocks[curr_x + 1][curr_y + 2] and $1280120) = 0))then
  145. begin
  146. path_x[path_ptr] := curr_x + 1;
  147. path_y[path_ptr] := curr_y + 1;
  148. path_ptr := (path_ptr + 1) mod pathLength;
  149. prev[curr_x + 1][curr_y + 1] := 12;
  150. dist[curr_x + 1][curr_y + 1] := cost;
  151. end;
  152. end;
  153. if foundPath then
  154. begin
  155. Result := dist[curr_x][curr_y];
  156. end
  157. else
  158. Result := -1;
  159. except
  160. Result := -1;
  161. end;
  162. end;
  163. function GetFunctionCount(): Integer; stdcall; export;
  164. begin
  165. Result := 1;
  166. end;
  167. function GetFunctionCallingConv(x : Integer) : Integer; stdcall; export;
  168. begin
  169. Result := 0;
  170. case x of
  171. 0 : Result := 1;
  172. end;
  173. end;
  174. function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall; export;
  175. begin
  176. case x of
  177. 0:
  178. begin
  179. ProcAddr := @DijkstraDist;
  180. StrPCopy(ProcDef, 'function DijkstraDist(StartX, StartY, DestX, DestY: Integer; IsObject: Boolean; Blocks: T2DIntegerArray) : Integer;');
  181. end;
  182. else
  183. x := -1;
  184. end;
  185. Result := x;
  186. end;
  187. exports GetFunctionCount;
  188. exports GetFunctionInfo;
  189. exports GetFunctionCallingConv;
  190. exports DijkstraDist;
  191. initialization
  192. end.