-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtm.pas
454 lines (399 loc) · 11.3 KB
/
dtm.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
{
This file is part of the Mufasa Macro Library (MML)
Copyright (c) 2009-2012 by Raymond van Venetië and Merlijn Wajer
MML is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MML is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MML. If not, see <http://www.gnu.org/licenses/>.
See the file COPYING, included in this distribution,
for details about the copyright.
DTM class for the Mufasa Macro Library
}
unit dtm;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, MufasaTypes;
type
{ TMDTM }
TMDTM = class(TObject)
private
FPoints : TMDTMPointArray;
FLen : integer;
function GetPointerPoints: PMDTMPoint;
procedure SetPointCount(const AValue: integer);
public
Name : string;
Index : integer;
function ToString : string;
function SaveToFile(const FileName : string) : boolean;
function LoadFromString(const s : string) : boolean;
procedure Normalize;
function Valid : boolean;
procedure DeletePoint(Point : integer);
procedure SwapPoint(p1, p2: integer);
procedure MovePoint(fromIndex, toIndex: integer);
function AddPoint(Point: TMDTMPoint): integer;
property PPoints : PMDTMPoint read GetPointerPoints;
property Count : integer read FLen write SetPointCount;
property Points : TMDTMPointArray read FPoints;
end;
{ TMDTMS }
TMDTMS = class(TObject) //Manages the DTMs TMufasaDTMs
private
Client: TObject;
DTMList: Array Of TMDTM;
FreeSpots: Array Of Integer;
procedure CheckIndex(index : integer);
public
function AddDTM(const d: TSDTM): Integer;overload;
function AddDTM(const d: TMDTM): Integer;overload;
function ExistsDTM(index : integer) : boolean;
function GetDTM(index: Integer) :TMDTM;
procedure FreeDTM(DTM: Integer);
function StringToDTM(const S: String): Integer;
property DTM[Index : integer]: TMDTM read GetDTM; default;
constructor Create(Owner: TObject);
destructor Destroy; override;
end;
implementation
uses
dtmutil, paszlib,
client,
DCPbase64,
graphics, // for TColor
math // for max
;
constructor TMDTMS.Create(Owner: TObject);
begin
inherited Create;
Self.Client := Owner;
SetLength(DTMList, 0);
SetLength(FreeSpots, 0);
end;
{$DEFINE DTM_DEBUG}
destructor TMDTMS.Destroy;
var
i, j: integer;
b:boolean;
WriteStr : string;
begin
WriteStr := '[';
for i := 0 to high(DTMList) do
begin
b := false;
for j := 0 to high(freespots) do
if i = freespots[j] then
begin
b := true;
break;
end;
if not b then
begin;
if DTMList[i].name <> '' then
WriteStr := WriteStr + DTMList[i].name + ', '
else
WriteStr := WriteStr + inttostr(i) + ', ';
FreeDTM(i);
end;
end;
if WriteStr <> '[' then //Has unfreed DTMs
begin
SetLength(WriteStr,length(WriteStr)-1);
WriteStr[Length(writeStr)] := ']';
TClient(Client).Writeln(Format('The following DTMs were not freed: %s',[WriteStr]));
end;
SetLength(DTMList, 0);
SetLength(FreeSpots, 0);
inherited Destroy;
end;
// Rotates the given point (p) by A (in radians) around the point defined by cx, cy.
function RotatePoint(const p: TPoint;const angle, mx, my: Extended): TPoint; inline;
begin
Result.X := Round(mx + cos(angle) * (p.x - mx) - sin(angle) * (p.y - my));
Result.Y := Round(my + sin(angle) * (p.x - mx) + cos(angle) * (p.y- my));
end;
function HexToInt(const HexNum: string): LongInt;inline;
begin
Result:=StrToInt('$' + HexNum);
end;
function TMDTMS.StringToDTM(const S: String): Integer;
var
aDTM : TMDTM;
begin
aDTM := TMDTM.Create;
aDTM.LoadFromString(s);
Result := AddDTM(aDTM);
end;
procedure TMDTMS.CheckIndex(index: integer);
begin
if (index < 0) or (index >= Length(DTMList)) or (DTMList[Index] = nil) then
raise Exception.CreateFmt('The given DTM Index[%d] doesn''t exist',[index]);
end;
function TMDTMS.AddDTM(const d: TSDTM): Integer;
begin
Result := AddDTM(SDTMToMDTM(d));
end;
{/\
Adds the given pDTM to the DTM Array, and returns it's index.
/\}
function TMDTMS.AddDTM(const d: TMDTM): Integer;
begin
if Length(FreeSpots) > 0 then
begin
Result := FreeSpots[High(FreeSpots)];
SetLength(FreeSpots, High(FreeSpots));
end
else
begin
SetLength(DTMList, Length(DTMList) + 1);
Result := High(DTMList);
end;
DTMList[Result] := d;
DTMList[Result].Index:= Result;
DTMList[result].Normalize;
end;
function TMDTMS.ExistsDTM(index : integer) : boolean;
begin
result := false;
if (index >= 0) and (index <= High(DTMList)) then
result := Assigned(DTMList[index]);
end;
{/\
Returns the DTM (pDTM type) in the variable dtm at the given index.
Returns true is succesfull, false if the dtm does not exist.
/\}
function TMDTMS.GetDTM(index: Integer) :TMDTM;
begin
CheckIndex(index);
result := DTMList[index];
end;
{/\
Unloads the DTM at the given index from the DTM Array.
Notes:
Will keep track of not used index, so it is very memory efficient.
/\}
procedure TMDTMS.FreeDTM(DTM: Integer);
begin
CheckIndex(DTM);
DTMList[DTM].Free;
DTMList[DTM] := nil;
SetLength(FreeSpots, Length(FreeSpots) + 1);
FreeSpots[High(FreeSpots)] := DTM;
end;
{ TMDTM }
function TMDTM.GetPointerPoints: PMDTMPoint;
begin
if count < 1 then
result := nil
else
result := @FPoints[0];
end;
procedure TMDTM.SetPointCount(const AValue: integer);
begin
SetLength(FPoints,AValue);
FLen := AValue;
end;
//We save the data as arrays for each point (so we can add features to DTMs without having to change
//the way the ToString is done..
//E.G. A DTM with 3 points would become
//LenXXXYYYCCCTTTASZASZASZBPBPBP
function TMDTM.ToString: string;
var
i,len : integer;
Ptr,Start : Pointer;
Destlen : Longword;
procedure WriteInteger(int : integer);
begin
PLongInt(Ptr)^ := int;
Inc(ptr,sizeof(int));
end;
procedure WriteBool(bool : boolean);
begin;
PBoolean(Ptr)^ := bool;
inc(ptr,sizeof(boolean));
end;
begin
result := '';
if Count < 1 then
exit;
len := Count * TMDTMPointSize + SizeOf(Integer);
Start:= GetMem(len);
Ptr := Start;
WriteInteger(FLen);
for i := 0 to FLen-1 do
WriteInteger(FPoints[i].x);
for i := 0 to FLen-1 do
WriteInteger(FPoints[i].y);
for i := 0 to FLen-1 do
WriteInteger(FPoints[i].c);
for i := 0 to FLen-1 do
WriteInteger(FPoints[i].t);
for i := 0 to FLen-1 do
WriteInteger(FPoints[i].asz);
for i := 0 to FLen-1 do
WriteBool(FPoints[i].bp);
Destlen :=BufferLen;
if compress(BufferString,destlen,pchar(start),len) = Z_OK then
begin
setlength(result,Destlen + SizeOf(Integer));
PInteger(@result[1])^ := len;
Move(bufferstring[0],result[1 + sizeof(integer)],Destlen);
//We now have Size + Compressed data.. Lets Base64Encrypt it!
Result := 'm' + Base64EncodeStr(result);
//It now looks like m + base64encoded data! The 'm' is to indicate we used this encryption method.
end;
Freemem(start,len);
end;
function TMDTM.SaveToFile(const FileName: string): boolean;
begin
end;
function TMDTM.LoadFromString(const s: string): boolean;
var
MDTM : TMDTM;
Source : String;
DestLen : longword;
i,ii,c : integer;
DPoints : PMDTMPoint;
Ptr : Pointer;
function ReadInteger : integer;
begin
Result := PInteger(ptr)^;
inc(ptr,sizeof(integer));
end;
function ReadBoolean : boolean;
begin
result := PBoolean(ptr)^;
inc(ptr,sizeof(boolean));
end;
begin
Result := false;
ii := Length(S);
if (ii = 0) then
exit;
if S[1] = 'm' then
begin
if ii < 9 then
raise Exception.CreateFMT('Invalid DTM-String passed to StringToDTM: %s',[s]);
Source := Base64DecodeStr(copy(s,2,ii-1));
i:= PLongint(@source[1])^; //The 4 four bytes should contain the dest len!
if i < 1 then
raise Exception.CreateFMT('Invalid DTM-String passed to StringToDTM: %s',[s]);
DestLen := BufferLen;
ptr := @Source[1 + sizeof(longint)];
if uncompress(BufferString,DestLen,ptr,length(source)-sizeof(integer)) = Z_OK then
begin
ptr := BufferString;
Self.Count:= ReadInteger;
ii := Self.Count;
if (Self.Count * TMDTMPointSize) <> (Destlen - SizeOf(integer)) then
raise Exception.CreateFMT('Invalid DTM-String passed to StringToDTM: %s',[s]);
DPoints := Self.PPoints;
for i := 0 to ii-1 do
DPoints[i].x := ReadInteger;
for i := 0 to ii-1 do
DPoints[i].y := ReadInteger;
for i := 0 to ii-1 do
DPoints[i].c := ReadInteger;
for i := 0 to ii-1 do
DPoints[i].t := ReadInteger;
for i := 0 to ii-1 do
DPoints[i].asz := ReadInteger;
for i := 0 to ii-1 do
DPoints[i].bp := ReadBoolean;
Result := true;
end;
end else
begin
if (ii mod 2 <> 0) then
exit;
ii := ii div 2;
SetLength(Source,ii);
for i := 1 to ii do
Source[i] := Chr(HexToInt(S[i * 2 - 1] + S[i * 2]));
DestLen := BufferLen;
if uncompress(Bufferstring,Destlen,pchar(Source), ii) = Z_OK then
begin;
if (Destlen mod 36) > 0 then
raise Exception.CreateFMT('Invalid DTM-String passed to StringToDTM: %s',[s]);
DestLen := DestLen div 36;
Self.Count:= DestLen;
DPoints := Self.PPoints;
ptr := bufferstring;
for i := 0 to DestLen - 1 do
begin;
DPoints[i].x :=PInteger(ptr + 1)^;
DPoints[i].y := PInteger(ptr + 5)^;
DPoints[i].asz := PInteger(ptr + 12)^;
// DPoints.ash[i] := PInteger(@b^[c+16])^;
DPoints[i].c := PInteger(ptr + 20)^;
DPoints[i].t := PInteger(ptr + 24)^;
DPoints[i].bp := False;
inc(ptr,36);
end;
Result := true;
end;
end;
if result then
Normalize;
end;
procedure TMDTM.Normalize;
var
i:integer;
begin
if (self = nil) or (Self.count < 1) or ((Self.Points[0].x = 0) and (Self.Points[0].y = 0)) then //Already normalized
exit;
for i := 1 to Self.Count - 1 do
begin
Self.Points[i].x := Self.Points[i].x - Self.Points[0].x;
Self.Points[i].y := Self.Points[i].y - Self.Points[0].y;
end;
Self.Points[0].x := 0;
Self.Points[0].y := 0;
end;
function TMDTM.Valid: boolean;
begin
result := false;
if Count < 1 then
exit;
Normalize;
result := true;
end;
procedure TMDTM.DeletePoint(Point: integer);
begin
MovePoint(Point, FLen - 1);
Count := Count - 1;
end;
procedure TMDTM.SwapPoint(p1, p2: integer);
var
tempP: TMDTMPoint;
begin
tempP := FPoints[p1];
FPoints[p1] := FPoints[p2];
FPoints[p2] := tempP;
end;
procedure TMDTM.MovePoint(fromIndex, toIndex: integer);
var
i: integer;
begin
if fromIndex > toIndex then //We are going down
begin
for i := fromindex downto Toindex + 1 do
SwapPoint(i, i - 1);
end else if fromIndex < toIndex then
for i := fromindex to toindex - 1 do
SwapPoint(i, i + 1);
end;
function TMDTM.AddPoint(Point: TMDTMPoint): integer;
begin
Count := Count + 1;
Result := FLen - 1;
FPoints[Result] := Point;
end;
end.