Skip to content

Commit

Permalink
Add tests for surge jobs creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neloreck committed Aug 15, 2023
1 parent 7ab0119 commit 315ced2
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 47 deletions.
2 changes: 2 additions & 0 deletions src/engine/core/utils/job/__test__/job_create.default.ltx
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ meet = meet@generic_lager
path_walk = surge_1_walk
def_state_standing = guard
def_state_moving = patrol
path_look = surge_1_look
[logic@test_smart_surge_2_walk]
active = walker@test_smart_surge_2_walk
[walker@test_smart_surge_2_walk]
Expand All @@ -231,6 +232,7 @@ meet = meet@generic_lager
path_walk = surge_2_walk
def_state_standing = guard
def_state_moving = patrol
path_look = surge_2_look
[logic@test_smart_surge_3_walk]
active = walker@test_smart_surge_3_walk
[walker@test_smart_surge_3_walk]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[logic@test_smart_surge_1_walk]
active = walker@test_smart_surge_1_walk
[walker@test_smart_surge_1_walk]
sound_idle = state
use_camp = true
meet = meet@generic_lager
path_walk = surge_1_walk
def_state_standing = guard
def_state_moving = patrol
path_look = surge_1_look
out_restr = def_restrictor_test
combat_ignore_cond = {=npc_in_zone(smart.base_on_actor_control.ignore_zone)} true
combat_ignore_keep_when_attacked = true
[logic@test_smart_surge_2_walk]
active = walker@test_smart_surge_2_walk
[walker@test_smart_surge_2_walk]
sound_idle = state
use_camp = true
meet = meet@generic_lager
path_walk = surge_2_walk
def_state_standing = guard
def_state_moving = patrol
path_look = surge_2_look
out_restr = def_restrictor_test
combat_ignore_cond = {=npc_in_zone(smart.base_on_actor_control.ignore_zone)} true
combat_ignore_keep_when_attacked = true
[logic@test_smart_surge_3_walk]
active = walker@test_smart_surge_3_walk
[walker@test_smart_surge_3_walk]
sound_idle = state
use_camp = true
meet = meet@generic_lager
path_walk = surge_3_walk
def_state_standing = guard
def_state_moving = patrol
out_restr = def_restrictor_test
combat_ignore_cond = {=npc_in_zone(smart.base_on_actor_control.ignore_zone)} true
combat_ignore_keep_when_attacked = true
29 changes: 29 additions & 0 deletions src/engine/core/utils/job/__test__/job_create_stalker.surge.ltx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[logic@test_smart_surge_1_walk]
active = walker@test_smart_surge_1_walk
[walker@test_smart_surge_1_walk]
sound_idle = state
use_camp = true
meet = meet@generic_lager
path_walk = surge_1_walk
def_state_standing = guard
def_state_moving = patrol
path_look = surge_1_look
[logic@test_smart_surge_2_walk]
active = walker@test_smart_surge_2_walk
[walker@test_smart_surge_2_walk]
sound_idle = state
use_camp = true
meet = meet@generic_lager
path_walk = surge_2_walk
def_state_standing = guard
def_state_moving = patrol
path_look = surge_2_look
[logic@test_smart_surge_3_walk]
active = walker@test_smart_surge_3_walk
[walker@test_smart_surge_3_walk]
sound_idle = state
use_camp = true
meet = meet@generic_lager
path_walk = surge_3_walk
def_state_standing = guard
def_state_moving = patrol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[logic@test_smart_surge_1_walk]
active = walker@test_smart_surge_1_walk
[walker@test_smart_surge_1_walk]
sound_idle = state
use_camp = true
meet = meet@generic_lager
path_walk = surge_1_walk
def_state_standing = guard
def_state_moving = patrol
path_look = surge_1_look
out_restr = def_restrictor_test
[logic@test_smart_surge_2_walk]
active = walker@test_smart_surge_2_walk
[walker@test_smart_surge_2_walk]
sound_idle = state
use_camp = true
meet = meet@generic_lager
path_walk = surge_2_walk
def_state_standing = guard
def_state_moving = patrol
path_look = surge_2_look
out_restr = def_restrictor_test
[logic@test_smart_surge_3_walk]
active = walker@test_smart_surge_3_walk
[walker@test_smart_surge_3_walk]
sound_idle = state
use_camp = true
meet = meet@generic_lager
path_walk = surge_3_walk
def_state_standing = guard
def_state_moving = patrol
out_restr = def_restrictor_test
10 changes: 7 additions & 3 deletions src/engine/core/utils/job/job_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ export function areOnlyMonstersOnJobs(objectInfos: LuaArray<IObjectJobDescriptor
* todo;
* todo;
*/
export function isJobInRestrictor(smart: SmartTerrain, restrictorName: TName, wayName: string): Optional<boolean> {
export function isJobPatrolInRestrictor(
smartTerrain: SmartTerrain,
restrictorName: TName,
wayName: TName
): Optional<boolean> {
if (restrictorName === null) {
return null;
}
Expand All @@ -76,8 +80,8 @@ export function isJobInRestrictor(smart: SmartTerrain, restrictorName: TName, wa
const patrolObject: Patrol = new patrol(wayName);
const count: TCount = patrolObject.count();

for (const pt of $range(0, count - 1)) {
if (!restrictor.inside(patrolObject.point(pt))) {
for (const point of $range(0, count - 1)) {
if (!restrictor.inside(patrolObject.point(point))) {
return false;
}
}
Expand Down
131 changes: 131 additions & 0 deletions src/engine/core/utils/job/job_create_stalker.surge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import * as path from "path";

import { describe, expect, it, jest } from "@jest/globals";

import { IRegistryObjectState, registerZone } from "@/engine/core/database";
import { SmartTerrain, SmartTerrainControl } from "@/engine/core/objects";
import { createStalkerSurgeJobs } from "@/engine/core/utils/job/job_create_stalker";
import { range } from "@/engine/core/utils/number";
import { readInGameTestLtx } from "@/fixtures/engine";
import { mockClientGameObject } from "@/fixtures/xray";

describe("jobs_general should correctly generate stalkers surge jobs", () => {
it("should correctly generate surge jobs for stalkers when no patrols exist", async () => {
const smartTerrain: SmartTerrain = new SmartTerrain("test_smart");

smartTerrain.ini = smartTerrain.spawn_ini();

jest.spyOn(smartTerrain, "name").mockImplementation(() => "test_surge_paths_smart");

const [jobsList, ltx, count] = createStalkerSurgeJobs(smartTerrain);

expect(count).toBe(1);
expect(ltx).toBe("");
expect(jobsList).toEqualLuaTables({
jobs: {},
priority: 50,
});
});

it("should correctly generate surge jobs for stalkers when patrols exist", async () => {
const surgeJobsLtx: string = await readInGameTestLtx(
path.resolve(__dirname, "__test__", "job_create_stalker.surge.ltx")
);

const smartTerrain: SmartTerrain = new SmartTerrain("test_smart");

smartTerrain.ini = smartTerrain.spawn_ini();
smartTerrain.defendRestrictor = null;

jest.spyOn(smartTerrain, "name").mockImplementation(() => "test_smart");

const [jobsList, ltx, count] = createStalkerSurgeJobs(smartTerrain);

expect(count).toBe(4);
expect(ltx).toBe(surgeJobsLtx);
expect(jobsList).toEqualLuaTables({
jobs: $fromArray(
range(3, 1).map((it) => ({
_precondition_function: expect.any(Function),
_precondition_params: {},
job_id: {
job_type: "path_job",
section: `logic@test_smart_surge_${it}_walk`,
},
priority: 50,
}))
),
priority: 50,
});
});

it("should correctly generate surge jobs for stalkers when patrols exist with restrictors", async () => {
const surgeJobsLtx: string = await readInGameTestLtx(
path.resolve(__dirname, "__test__", "job_create_stalker.surge.restrictors.ltx")
);

const smartTerrain: SmartTerrain = new SmartTerrain("test_smart");

smartTerrain.ini = smartTerrain.spawn_ini();

jest.spyOn(smartTerrain, "name").mockImplementation(() => "test_smart");

smartTerrain.defendRestrictor = "def_restrictor_test";
smartTerrain.smartTerrainActorControl = { ignoreZone: "test_ignore_zone" } as SmartTerrainControl;

const [jobsList, ltx, count] = createStalkerSurgeJobs(smartTerrain);

expect(count).toBe(4);
expect(ltx).toBe(surgeJobsLtx);
expect(jobsList).toEqualLuaTables({
jobs: $fromArray(
range(3, 1).map((it) => ({
_precondition_function: expect.any(Function),
_precondition_params: {},
job_id: {
job_type: "path_job",
section: `logic@test_smart_surge_${it}_walk`,
},
priority: 50,
}))
),
priority: 50,
});
});

it("should correctly generate surge jobs for stalkers when patrols exist, when in restrictor", async () => {
const surgeJobsLtx: string = await readInGameTestLtx(
path.resolve(__dirname, "__test__", "job_create_stalker.surge.ignore.ltx")
);

const smartTerrain: SmartTerrain = new SmartTerrain("test_smart");

registerZone(mockClientGameObject({ name: () => "some_restrictor", inside: () => true }));

smartTerrain.ini = smartTerrain.spawn_ini();

jest.spyOn(smartTerrain, "name").mockImplementation(() => "test_smart");

smartTerrain.defendRestrictor = "def_restrictor_test";
smartTerrain.smartTerrainActorControl = { ignoreZone: "some_restrictor" } as SmartTerrainControl;

const [jobsList, ltx, count] = createStalkerSurgeJobs(smartTerrain);

expect(count).toBe(4);
expect(ltx).toBe(surgeJobsLtx);
expect(jobsList).toEqualLuaTables({
jobs: $fromArray(
range(3, 1).map((it) => ({
_precondition_function: expect.any(Function),
_precondition_params: {},
job_id: {
job_type: "path_job",
section: `logic@test_smart_surge_${it}_walk`,
},
priority: 50,
}))
),
priority: 50,
});
});
});
Loading

0 comments on commit 315ced2

Please sign in to comment.