Skip to content

Commit c112aa6

Browse files
committed
ags3/ags4-auto-test: add character direct move tests
1 parent 99dc405 commit c112aa6

File tree

2 files changed

+314
-100
lines changed

2 files changed

+314
-100
lines changed

ags3/auto-test/test-characters.asc

Lines changed: 157 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,65 @@
11
// Test Characters Module Script
22
int GetTestCharactersCount()
33
{
4-
return 8 + 8
5-
+ 24 * 3 // turn on spot animating
6-
+ 8 * 3 // idle start time
4+
return 0
75
+ 1 // graphic pos
86
+ 4 // view frames
7+
+ 8 + 8 // direction loops and instant turning
8+
+ 24 * 3 // turn on spot animating
9+
+ 8 * 3 // idle start time
10+
+ 12 * 3 // direct move
11+
+ 7 // waypoints move
912
;
1013
}
1114

15+
//int test_render_num;
16+
void _TestCharRenderF(Character *testChar, int sprite_num, eFlipDirection flip, const string test_name)
17+
{
18+
Wait(1); // let engine redraw
19+
// IMPORTANT: AGS 3.* has a historical mistake where the object's sprite is drawn 1 pixel higher than necessary
20+
int sprite_w = Game.SpriteWidth[sprite_num];
21+
int sprite_h = Game.SpriteHeight[sprite_num];
22+
int sprite_x = testChar.x - sprite_w / 2;
23+
int sprite_y = testChar.y - sprite_h;
24+
//tap.Comment(String.Format("sprite_x = %d, sprite_y = %d, sprite_w = %d, sprite_h = %d", sprite_x, sprite_y, sprite_w, sprite_h));
25+
//SaveScreenShot(String.Format("_%d.bmp", test_render_num));
26+
//test_render_num++;
27+
tap.ok(GfxComparer.TakeScreenshotAndMatchSprite(sprite_x, sprite_y, sprite_w, sprite_h, sprite_num, flip), test_name);
28+
}
29+
30+
void _TestCharRender(Character *testChar, int sprite_num, const string test_name)
31+
{
32+
_TestCharRenderF(testChar, sprite_num, -1, test_name);
33+
}
34+
35+
void _TestCharViewFrame(Character *testChar, int view, int loop, int frame, const string test_name)
36+
{
37+
testChar.LockViewFrame(view, loop, frame);
38+
ViewFrame* vf = Game.GetViewFrame(view, loop, frame);
39+
eFlipDirection flip = -1;
40+
if (vf.Flipped)
41+
flip = eFlipLeftToRight;
42+
_TestCharRenderF(testChar, vf.Graphic, flip, test_name);
43+
testChar.UnlockView();
44+
}
45+
46+
#define SPRITE_DEFAULT 9
47+
48+
void TestGraphicPosition()
49+
{
50+
cWalker1.LockViewFrame(VFLIP, 0, 0);
51+
_TestCharRender(cWalker1, SPRITE_DEFAULT, "Character: graphic position");
52+
cWalker1.UnlockView();
53+
}
54+
55+
void TestViewRender()
56+
{
57+
_TestCharViewFrame(cWalker1, VFLIP, 0, 0, "Character: view frame 0, normal");
58+
_TestCharViewFrame(cWalker1, VFLIP, 0, 1, "Character: view frame 1, normal");
59+
_TestCharViewFrame(cWalker1, VFLIP, 0, 2, "Character: view frame 2, h-flip");
60+
_TestCharViewFrame(cWalker1, VFLIP, 0, 3, "Character: view frame 3, h-flip");
61+
}
62+
1263
void TestChangingDirectionLoop()
1364
{
1465
Character* turner = cWalker2;
@@ -234,52 +285,107 @@ void TestIdleViewStart()
234285
TestIdleStartsInTime(idler, idle_start_delay, "after custom animate", 0, true);
235286
}
236287

237-
//int test_render_num;
238-
void _TestCharRenderF(Character *testChar, int sprite_num, eFlipDirection flip, const string test_name)
239-
{
240-
Wait(1); // let engine redraw
241-
// IMPORTANT: AGS 3.* has a historical mistake where the object's sprite is drawn 1 pixel higher than necessary
242-
int sprite_w = Game.SpriteWidth[sprite_num];
243-
int sprite_h = Game.SpriteHeight[sprite_num];
244-
int sprite_x = testChar.x - sprite_w / 2;
245-
int sprite_y = testChar.y - sprite_h;
246-
//tap.Comment(String.Format("sprite_x = %d, sprite_y = %d, sprite_w = %d, sprite_h = %d", sprite_x, sprite_y, sprite_w, sprite_h));
247-
//SaveScreenShot(String.Format("_%d.bmp", test_render_num));
248-
//test_render_num++;
249-
tap.ok(GfxComparer.TakeScreenshotAndMatchSprite(sprite_x, sprite_y, sprite_w, sprite_h, sprite_num, flip), test_name);
250-
}
251-
252-
void _TestCharRender(Character *testChar, int sprite_num, const string test_name)
253-
{
254-
_TestCharRenderF(testChar, sprite_num, -1, test_name);
255-
}
256-
257-
void _TestCharViewFrame(Character *testChar, int view, int loop, int frame, const string test_name)
288+
void TestMoveDirect(Character* walker, String test_name, int sx, int sy, int dx, int dy, bool use_walk, BlockingStyle style)
258289
{
259-
testChar.LockViewFrame(view, loop, frame);
260-
ViewFrame* vf = Game.GetViewFrame(view, loop, frame);
261-
eFlipDirection flip = -1;
262-
if (vf.Flipped)
263-
flip = eFlipLeftToRight;
264-
_TestCharRenderF(testChar, vf.Graphic, flip, test_name);
265-
testChar.UnlockView();
290+
walker.x = sx;
291+
walker.y = sy;
292+
if (use_walk)
293+
{
294+
walker.Walk(dx, dy, style, eAnywhere);
295+
}
296+
else
297+
{
298+
walker.Move(dx, dy, style, eAnywhere);
299+
}
300+
if (style == eNoBlock)
301+
{
302+
while (walker.Moving) Wait(1);
303+
}
304+
tap.ok(!walker.Moving, String.Format("%s: finished move", test_name));
305+
tap.is_int(walker.x, dx, String.Format("%s: destination x", test_name));
306+
tap.is_int(walker.y, dy, String.Format("%s: destination y", test_name));
266307
}
267308

268-
#define SPRITE_DEFAULT 9
269-
270-
void TestGraphicPosition()
309+
void TestMoveDirectToWaypoint(Character* walker, String test_name, int p_start, int p_end, int t_const, bool x_axis)
271310
{
272-
cWalker1.LockViewFrame(VFLIP, 0, 0);
273-
_TestCharRender(cWalker1, SPRITE_DEFAULT, "Character: graphic position");
274-
cWalker1.UnlockView();
311+
int p_range1, p_range2;
312+
if (p_start <= p_end)
313+
{
314+
p_range1 = p_start;
315+
p_range2 = p_end;
316+
}
317+
else
318+
{
319+
p_range1 = p_end;
320+
p_range2 = p_start;
321+
}
322+
323+
bool p_coords_in_range = true;
324+
bool t_coords_const = true;
325+
while (walker.Moving && (x_axis && walker.x != p_end) || (!x_axis && walker.y != p_end))
326+
{
327+
if (x_axis)
328+
{
329+
if (!(walker.x >= p_range1 && walker.x <= p_range2))
330+
{
331+
p_coords_in_range = false;
332+
}
333+
if (walker.x != p_end && walker.y != t_const)
334+
{
335+
t_coords_const = false;
336+
}
337+
}
338+
else
339+
{
340+
if (!(walker.y >= p_range1 && walker.y <= p_range2))
341+
{
342+
p_coords_in_range = false;
343+
}
344+
if (walker.y != p_end && walker.x != t_const)
345+
{
346+
t_coords_const = false;
347+
}
348+
}
349+
350+
Wait(1);
351+
}
352+
tap.ok(p_coords_in_range && t_coords_const, String.Format("%s: move position stayed in range", test_name));
353+
// NOTE: we cannot test exact coords here, because character may already be turned and start moving towards next waypoint
275354
}
276355

277-
void TestViewRender()
356+
// Tests moving and walking WITHOUT pathfinding (eAnywhere),
357+
// only for the sake of testing the fact of movement itself.
358+
void TestSimpleMovement(Character* walker)
278359
{
279-
_TestCharViewFrame(cWalker1, VFLIP, 0, 0, "Character: view frame 0, normal");
280-
_TestCharViewFrame(cWalker1, VFLIP, 0, 1, "Character: view frame 1, normal");
281-
_TestCharViewFrame(cWalker1, VFLIP, 0, 2, "Character: view frame 2, h-flip");
282-
_TestCharViewFrame(cWalker1, VFLIP, 0, 3, "Character: view frame 3, h-flip");
360+
// Move
361+
TestMoveDirect(walker, "Character.Move eBlock Hor", 0, 0, 100, 0, false, eBlock);
362+
TestMoveDirect(walker, "Character.Move eBlock Ver", 0, 0, 0, 100, false, eBlock);
363+
TestMoveDirect(walker, "Character.Move eBlock Diag", 0, 0, 100, 100, false, eBlock);
364+
TestMoveDirect(walker, "Character.Move eNoBlock Hor", 0, 0, 100, 0, false, eNoBlock);
365+
TestMoveDirect(walker, "Character.Move eNoBlock Ver", 0, 0, 0, 100, false, eNoBlock);
366+
TestMoveDirect(walker, "Character.Move eNoBlock Diag", 0, 0, 100, 100, false, eNoBlock);
367+
// Walk
368+
TestMoveDirect(walker, "Character.Walk eBlock Hor", 0, 0, 100, 0, true, eBlock);
369+
TestMoveDirect(walker, "Character.Walk eBlock Ver", 0, 0, 0, 100, true, eBlock);
370+
TestMoveDirect(walker, "Character.Walk eBlock Diag", 0, 0, 100, 100, true, eBlock);
371+
TestMoveDirect(walker, "Character.Walk eNoBlock Hor", 0, 0, 100, 0, true, eNoBlock);
372+
TestMoveDirect(walker, "Character.Walk eNoBlock Ver", 0, 0, 0, 100, true, eNoBlock);
373+
TestMoveDirect(walker, "Character.Walk eNoBlock Diag", 0, 0, 100, 100, true, eNoBlock);
374+
375+
// AddWaypoint
376+
walker.x = 0;
377+
walker.y = 0;
378+
walker.AddWaypoint(100, 0);
379+
walker.AddWaypoint(100, 100);
380+
walker.AddWaypoint(0, 100);
381+
walker.AddWaypoint(0, 0);
382+
TestMoveDirectToWaypoint(walker, "Character.AddWaypoint 0,0->1,0", 0, 100, 0, true);
383+
TestMoveDirectToWaypoint(walker, "Character.AddWaypoint 1,0->1,1", 0, 100, 100, false);
384+
TestMoveDirectToWaypoint(walker, "Character.AddWaypoint 1,1->0,1", 100, 0, 100, true);
385+
TestMoveDirectToWaypoint(walker, "Character.AddWaypoint 0,1->0,0", 100, 0, 0, false);
386+
tap.ok(!walker.Moving, String.Format("%s: finished move", "Character.AddWaypoint"));
387+
tap.is_int(walker.x, 0, String.Format("%s: destination x", "Character.AddWaypoint"));
388+
tap.is_int(walker.y, 0, String.Format("%s: destination y", "Character.AddWaypoint"));
283389
}
284390

285391
void TestCharacters()
@@ -288,14 +394,6 @@ void TestCharacters()
288394
int old_game_speed = GetGameSpeed();
289395
SetGameSpeed(1000);
290396

291-
cWalker1.on = true;
292-
cWalker2.on = true;
293-
294-
TestChangingDirectionLoop();
295-
TestTurnOnSpotInstant();
296-
TestTurnOnSpotAnimating();
297-
TestIdleViewStart();
298-
299397
// Disable characters and objects, avoid interfering with our tests
300398
for (int i = 0; i < Game.CharacterCount; i++)
301399
character[i].on = false;
@@ -313,6 +411,15 @@ void TestCharacters()
313411
TestGraphicPosition();
314412
TestViewRender();
315413

414+
cWalker1.on = true;
415+
cWalker2.on = true;
416+
417+
TestChangingDirectionLoop();
418+
TestTurnOnSpotInstant();
419+
TestTurnOnSpotAnimating();
420+
TestIdleViewStart();
421+
TestSimpleMovement(cWalker1);
422+
316423
SetGameSpeed(old_game_speed);
317424
tap.Comment("end Character tests");
318425
}

0 commit comments

Comments
 (0)