Subversion Repositories jumper

Compare Revisions

Regard whitespace Rev 22 → Rev 23

/trunk/LevelFunctions.pas
6,6 → 6,11
SysUtils, Dialogs, Functions, ExtCtrls, Classes, Math;
 
type
TCoord = record
X: integer;
Y: integer;
end;
 
TFieldType = (ftUndefined, ftFullSpace, ftEmpty, ftRed, ftYellow, ftGreen);
 
TFieldProperties = record
52,17 → 57,23
TPlayGroundMatrix = record
Fields: array of array of TField;
public
procedure InitFieldArray(width, height: integer);
function MatrixHasGoal: boolean;
function GoalFieldType: TFieldType;
function MatrixWorth: integer;
procedure ClearMatrix(FreeVCL: boolean);
function CloneMatrix: TPlayGroundMatrix;
function FieldState(t: TFieldType): TFieldState; overload;
class function FieldState(t: TFieldType): TFieldState; overload; static;
function FieldState(f: TField): TFieldState; overload;
function FieldState(x, y: integer): TFieldState; overload;
function CanJump(SourceX, SourceY, DestX, DestY: integer; DiagonalOK: boolean): boolean; overload;
function CanJump(SourceX, SourceY: integer; DiagonalOK: boolean): boolean; overload;
function CanJump(DiagonalOK: boolean): boolean; overload;
function IndexToCoord(index: integer): TCoord;
function CoordToIndex(coord: TCoord): integer; overload;
function CoordToIndex(x, y: integer): integer; overload;
function Width: integer;
function Height: integer;
end;
 
function FieldTypeWorth(t: TFieldType): integer;
88,7 → 99,7
begin
for y := Low(Fields[x]) to High(Fields[x]) do
begin
result := result or Fields[x][y].Goal;
result := result or Fields[x,y].Goal;
end;
end;
end;
102,11 → 113,39
begin
for y := Low(Fields[x]) to High(Fields[x]) do
begin
if Fields[x][y].Goal then result := Fields[x][y].FieldType
if Fields[x,y].Goal then result := Fields[x,y].FieldType
end;
end;
end;
 
function TPlayGroundMatrix.Height: integer;
begin
if Length(Fields) = 0 then
result := 0
else
result := Length(Fields[0]);
end;
 
function TPlayGroundMatrix.IndexToCoord(index: integer): TCoord;
begin
result.X := index mod Width;
result.Y := index div Width;
end;
 
procedure TPlayGroundMatrix.InitFieldArray(width, height: integer);
var
x, y: integer;
begin
SetLength(Fields, width, height);
for x := Low(Fields) to High(Fields) do
begin
for y := Low(Fields[x]) to High(Fields[x]) do
begin
Fields[x,y].FieldType := ftUndefined;
end;
end;
end;
 
function TPlayGroundMatrix.MatrixWorth: integer;
var
x, y: integer;
116,11 → 155,16
begin
for y := Low(Fields[x]) to High(Fields[x]) do
begin
Inc(result, FieldTypeWorth(Fields[x][y].FieldType));
Inc(result, FieldTypeWorth(Fields[x,y].FieldType));
end;
end;
end;
 
function TPlayGroundMatrix.Width: integer;
begin
result := Length(Fields);
end;
 
procedure TPlayGroundMatrix.ClearMatrix(FreeVCL: boolean);
var
x, y: integer;
131,13 → 175,12
begin
if FreeVCL then
begin
if Assigned(Fields[x][y].Stone) then Fields[x][y].Stone.Free;
if Assigned(Fields[x][y].Panel) then Fields[x][y].Panel.Free;
if Assigned(Fields[x,y].Stone) then Fields[x,y].Stone.Free;
if Assigned(Fields[x,y].Panel) then Fields[x,y].Panel.Free;
end;
end;
SetLength(Fields[x], 0);
end;
SetLength(Fields, 0);
SetLength(Fields, 0, 0);
end;
 
function TPlayGroundMatrix.CloneMatrix: TPlayGroundMatrix;
150,16 → 193,30
SetLength(result.Fields[x], Length(Fields[x]));
for y := Low(Fields[x]) to High(Fields[x]) do
begin
result.Fields[x][y].FieldType := Fields[x][y].FieldType;
result.Fields[x][y].Goal := Fields[x][y].Goal;
result.Fields[x][y].Panel := Fields[x][y].Panel;
result.Fields[x][y].Stone := Fields[x][y].Stone;
result.Fields[x,y].FieldType := Fields[x,y].FieldType;
result.Fields[x,y].Goal := Fields[x,y].Goal;
result.Fields[x,y].Panel := Fields[x,y].Panel;
result.Fields[x,y].Stone := Fields[x,y].Stone;
end;
end;
end;
 
function TPlayGroundMatrix.FieldState(t: TFieldType): TFieldState;
function TPlayGroundMatrix.CoordToIndex(x, y: integer): integer;
var
c: TCoord;
begin
c.X := x;
c.Y := y;
result := CoordToIndex(c);
end;
 
function TPlayGroundMatrix.CoordToIndex(coord: TCoord): integer;
begin
result := coord.X + coord.Y * Width;
end;
 
class function TPlayGroundMatrix.FieldState(t: TFieldType): TFieldState;
begin
result := fsError;
case t of
ftFullSpace: result := fsLocked;
181,7 → 238,7
if (x < Low(Fields)) or (x > High(Fields)) then exit;
if (y < Low(Fields[x])) or (y > High(Fields[x])) then exit;
 
result := FieldState(Fields[x][y]);
result := FieldState(Fields[x,y]);
end;
 
function TPlayGroundMatrix.CanJump(SourceX, SourceY, DestX, DestY: integer; DiagonalOK: boolean): boolean;
/trunk/Main.pas
50,7 → 50,6
NoCloseQuery: boolean;
CountedSeconds: Integer;
LevelFile: String;
LookupFieldCoordinateArray: array of TPoint;
PrevPlaygroundMatrixes: array of TPlayGroundMatrix;
PlaygroundMatrix: TPlayGroundMatrix;
Points: Integer;
98,16 → 97,16
 
procedure TMainForm.RedrawStonesFromMatrix(Matrix: TPlayGroundMatrix);
var
i, j: integer;
x, y: integer;
begin
for i := Low(Matrix.Fields) to High(Matrix.Fields) do
for x := Low(Matrix.Fields) to High(Matrix.Fields) do
begin
for j := Low(Matrix.Fields[i]) to High(Matrix.Fields[i]) do
for y := Low(Matrix.Fields[x]) to High(Matrix.Fields[x]) do
begin
if Assigned(Matrix.Fields[i][j].Stone) then
if Assigned(Matrix.Fields[x][y].Stone) then
begin
LoadPictureForType(Matrix.Fields[i][j].FieldType, Matrix.Fields[i][j].Stone.Picture);
StoneDraggingAllow(Matrix.Fields[i][j].Stone, Matrix.FieldState(Matrix.Fields[i][j].FieldType) <> fsAvailable);
LoadPictureForType(Matrix.Fields[x][y].FieldType, Matrix.Fields[x][y].Stone.Picture);
StoneDraggingAllow(Matrix.Fields[x][y].Stone, Matrix.FieldState(Matrix.Fields[x][y].FieldType) <> fsAvailable);
end;
end;
end;
143,8 → 142,6
SetLength(PrevPlaygroundMatrixes, 0);
MUndo.Enabled := false;
 
SetLength(LookupFieldCoordinateArray, 0);
 
if Assigned(Level) then FreeAndNil(Level);
end;
 
243,6 → 240,7
Inc(LevelRemovedStones);
RefreshStonesRemoved;
end;
 
PlayGroundMatrix.Fields[x, y].FieldType := ftEmpty;
LoadPictureForType(PlayGroundMatrix.Fields[x, y].FieldType, PlayGroundMatrix.Fields[x, y].Stone.Picture);
StoneDraggingAllow(PlayGroundMatrix.Fields[x, y].Stone, false);
281,14 → 279,14
resourcestring
LNG_JUMP_LOG = '%d [%d, %d] -> %d [%d, %d];';
var
d, s: TPoint;
d, s: TCoord;
old_fieldtype: TFieldType;
res: Integer;
begin
if not MayJump(SourceTag, DestTag) then exit;
 
d := LookupFieldCoordinateArray[DestTag];
s := LookupFieldCoordinateArray[SourceTag];
s := PlaygroundMatrix.IndexToCoord(SourceTag);
d := PlaygroundMatrix.IndexToCoord(DestTag);
 
JumpHistory.Add(Format(LNG_JUMP_LOG, [SourceTag+1, s.x+1, s.y+1, DestTag+1, d.x+1, d.y+1]));
 
355,10 → 353,10
 
function TMainForm.MayJump(SourceTag, DestTag: integer): boolean;
var
s, d: TPoint;
s, d: TCoord;
begin
d := LookupFieldCoordinateArray[DestTag];
s := LookupFieldCoordinateArray[SourceTag];
s := PlayGroundMatrix.IndexToCoord(SourceTag);
d := PlayGroundMatrix.IndexToCoord(DestTag);
 
result := PlaygroundMatrix.CanJump(s.X, s.Y, d.X, d.Y, Level.GameMode = gmDiagonal);
end;
382,22 → 380,13
ZeroMemory(@result, SizeOf(result));
if t.Typ = ftFullSpace then exit;
 
index := Length(LookupFieldCoordinateArray);
index := PlaygroundMatrix.CoordToIndex(x, y);
 
newField.FieldType := t.Typ;
newField.Goal := t.Goal;
newField.Panel := DrawStoneBox(x, y, index, indent, t.Goal);
newField.Stone := DrawStone(t.Typ, newField.Panel);
if PlayGroundMatrix.FieldState(t.Typ) = fsStone then Inc(LevelTotalStones);
 
SetLength(LookupFieldCoordinateArray, index + 1);
LookupFieldCoordinateArray[index].X := x;
LookupFieldCoordinateArray[index].Y := y;
 
if Length(PlayGroundMatrix.Fields) < x+1 then SetLength(PlayGroundMatrix.Fields, x+1);
if Length(PlayGroundMatrix.Fields[x]) < y+1 then SetLength(PlayGroundMatrix.Fields[x], y+1);
PlaygroundMatrix.Fields[x, y] := newField;
 
result := newField;
end;
 
406,10 → 395,14
y, x: integer;
max_x, max_y: integer;
p: TPanel;
newField: TField;
begin
PlayGround.Visible := false;
 
// Die Dimensionen ermitteln
// Attention: PlaygroundMatrix is indexed [x,y] while LevelArray is indexed [y,x]
// TODO: PlaygroundMatrix and LevelArray are redundant. Can't we just replace one with the other?
PlaygroundMatrix.InitFieldArray(Length(LevelArray[0].Fields), Length(LevelArray));
 
max_x := 0;
max_y := 0;
for y := Low(LevelArray) to High(LevelArray) do
416,7 → 409,11
begin
for x := Low(LevelArray[y].Fields) to High(LevelArray[y].Fields) do
begin
p := DrawField(x, y, LevelArray[y].Fields[x], LevelArray[y].Indent).Panel;
if TPlayGroundMatrix.FieldState(LevelArray[y].Fields[x].Typ) = fsStone then
Inc(LevelTotalStones);
newField := DrawField(x, y, LevelArray[y].Fields[x], LevelArray[y].Indent);
PlaygroundMatrix.Fields[x, y] := newField;
p := newField.Panel;
if Assigned(p) then
begin
max_x := Max(max_x, p.Left + p.Width);