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