Skip to content

Commit

Permalink
Add collector jobs tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neloreck committed Aug 15, 2023
1 parent 80a5df8 commit ff0c97a
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 29 deletions.
9 changes: 9 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 @@ -250,6 +250,15 @@ path_main = sleep_1
active = sleeper@test_smart_sleep_2
[sleeper@test_smart_sleep_2]
path_main = sleep_2
[logic@test_smart_collector_1_walk]
active = walker@test_smart_collector_1_walk
[walker@test_smart_collector_1_walk]
sound_idle = state
meet = meet@generic_lager
path_walk = collector_1_walk
def_state_standing = guard
def_state_moving = patrol
path_look = collector_1_look
[logic@test_smart_animpoint_1]
active = animpoint@test_smart_animpoint_1
[animpoint@test_smart_animpoint_1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ path_main = sleep_1
active = sleeper@test_smart_sleep_2
[sleeper@test_smart_sleep_2]
path_main = sleep_2
[logic@test_smart_collector_1_walk]
active = walker@test_smart_collector_1_walk
[walker@test_smart_collector_1_walk]
sound_idle = state
meet = meet@generic_lager
path_walk = collector_1_walk
def_state_standing = guard
def_state_moving = patrol
path_look = collector_1_look
[logic@test_smart_animpoint_1]
active = animpoint@test_smart_animpoint_1
[animpoint@test_smart_animpoint_1]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[logic@test_smart_collector_1_walk]
active = walker@test_smart_collector_1_walk
[walker@test_smart_collector_1_walk]
sound_idle = state
meet = meet@generic_lager
path_walk = collector_1_walk
def_state_standing = guard
def_state_moving = patrol
path_look = collector_1_look
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[logic@test_smart_collector_1_walk]
active = walker@test_smart_collector_1_walk
[walker@test_smart_collector_1_walk]
sound_idle = state
meet = meet@generic_lager
path_walk = collector_1_walk
def_state_standing = guard
def_state_moving = patrol
path_look = collector_1_look
out_restr = test_defend_restrictor
combat_ignore_cond = {=npc_in_zone(smart.base_on_actor_control.ignore_zone)} true
combat_ignore_keep_when_attacked = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[logic@test_smart_collector_1_walk]
active = walker@test_smart_collector_1_walk
[walker@test_smart_collector_1_walk]
sound_idle = state
meet = meet@generic_lager
path_walk = collector_1_walk
def_state_standing = guard
def_state_moving = patrol
path_look = collector_1_look
invulnerable = {=npc_in_zone(smart.safe_restr)} true
out_restr = test_defend_restrictor
combat_ignore_cond = {=npc_in_zone(smart.base_on_actor_control.ignore_zone)} true
combat_ignore_keep_when_attacked = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[logic@test_smart_collector_1_walk]
active = walker@test_smart_collector_1_walk
[walker@test_smart_collector_1_walk]
sound_idle = state
meet = meet@generic_lager
path_walk = collector_1_walk
def_state_standing = guard
def_state_moving = patrol
path_look = collector_1_look
out_restr = test_defend_restrictor
14 changes: 14 additions & 0 deletions src/engine/core/utils/job/job_create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ describe("jobs_general should correctly generate default jobs", () => {
),
priority: 10,
},
{
jobs: $fromArray([
{
_precondition_function: expect.any(Function),
_precondition_params: {},
job_id: {
job_type: "path_job",
section: "logic@test_smart_collector_1_walk",
},
priority: 25,
},
]),
priority: 25,
},
{
_precondition_function: expect.any(Function),
_precondition_params: {},
Expand Down
14 changes: 14 additions & 0 deletions src/engine/core/utils/job/job_create_stalker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ describe("jobs_general should correctly generate stalker jobs", () => {
),
priority: 10,
},
{
jobs: $fromArray([
{
_precondition_function: expect.any(Function),
_precondition_params: {},
job_id: {
job_type: "path_job",
section: "logic@test_smart_collector_1_walk",
},
priority: 25,
},
]),
priority: 25,
},
{
_precondition_function: expect.any(Function),
_precondition_params: {},
Expand Down
186 changes: 186 additions & 0 deletions src/engine/core/utils/job/job_create_stalker_collector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import * as path from "path";

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

import { IRegistryObjectState, registerObject, registerZone, registry } from "@/engine/core/database";
import { SmartTerrain, SmartTerrainControl } from "@/engine/core/objects";
import { createStalkerCollectorJobs } from "@/engine/core/utils/job/job_create_stalker_collector";
import { ServerHumanObject } from "@/engine/lib/types";
import { readInGameTestLtx } from "@/fixtures/engine";
import { mockClientGameObject, mockServerAlifeHumanStalker } from "@/fixtures/xray";

describe("jobs_general should correctly generate stalker collector jobs", () => {
it("should correctly generate default camper jobs with no collector patrols", async () => {
const smartTerrain: SmartTerrain = new SmartTerrain("test_smart");

smartTerrain.ini = smartTerrain.spawn_ini();

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

const [jobsList, ltx] = createStalkerCollectorJobs(smartTerrain);

expect(ltx).toBe("");
expect(jobsList).toEqualLuaTables({ priority: 25, jobs: $fromArray([]) });
});

it("should correctly generate default camper jobs with test smart", async () => {
const jobsLtx: string = await readInGameTestLtx(
path.resolve(__dirname, "__test__", "job_create_stalker_collector.default.ltx")
);

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

smartTerrain.ini = smartTerrain.spawn_ini();

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

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

expect(count).toBe(2);
expect(ltx).toBe(jobsLtx);
expect(jobsList).toEqualLuaTables({
priority: 25,
jobs: $fromArray([
{
_precondition_function: expect.any(Function),
_precondition_params: {},
job_id: {
job_type: "path_job",
section: "logic@test_smart_collector_1_walk",
},
priority: 25,
},
]),
});
});

it("should correctly generate default camper jobs with restrictor", async () => {
const jobsLtx: string = await readInGameTestLtx(
path.resolve(__dirname, "__test__", "job_create_stalker_collector.restrictor.ltx")
);

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

smartTerrain.ini = smartTerrain.spawn_ini();
smartTerrain.defendRestrictor = "test_defend_restrictor";

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

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

expect(count).toBe(2);
expect(ltx).toBe(jobsLtx);
expect(jobsList).toEqualLuaTables({
priority: 25,
jobs: $fromArray([
{
_precondition_function: expect.any(Function),
_precondition_params: {},
job_id: {
job_type: "path_job",
section: "logic@test_smart_collector_1_walk",
},
priority: 25,
},
]),
});
});

it("should correctly generate default camper jobs with ignore restrictor", async () => {
const jobsLtx: string = await readInGameTestLtx(
path.resolve(__dirname, "__test__", "job_create_stalker_collector.ignore.ltx")
);

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

smartTerrain.ini = smartTerrain.spawn_ini();
smartTerrain.defendRestrictor = "test_defend_restrictor";
smartTerrain.smartTerrainActorControl = { ignoreZone: "some_restrictor" } as SmartTerrainControl;

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

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

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

expect(count).toBe(2);
expect(ltx).toBe(jobsLtx);
expect(jobsList).toEqualLuaTables({
priority: 25,
jobs: $fromArray([
{
_precondition_function: expect.any(Function),
_precondition_params: {},
job_id: {
job_type: "path_job",
section: "logic@test_smart_collector_1_walk",
},
priority: 25,
},
]),
});
});

it("should correctly generate default camper jobs with invulnerable state", async () => {
const jobsLtx: string = await readInGameTestLtx(
path.resolve(__dirname, "__test__", "job_create_stalker_collector.invulnerable.ltx")
);

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

smartTerrain.ini = smartTerrain.spawn_ini();
smartTerrain.defendRestrictor = "test_defend_restrictor";
smartTerrain.smartTerrainActorControl = { ignoreZone: "some_restrictor" } as SmartTerrainControl;
smartTerrain.safeRestrictor = "safe_restrictor_test";

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

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

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

expect(count).toBe(2);
expect(ltx).toBe(jobsLtx);
expect(jobsList).toEqualLuaTables({
priority: 25,
jobs: $fromArray([
{
_precondition_function: expect.any(Function),
_precondition_params: {},
job_id: {
job_type: "path_job",
section: "logic@test_smart_collector_1_walk",
},
priority: 25,
},
]),
});
});

it("should correctly check camper jobs preconditions", async () => {
const smartTerrain: SmartTerrain = new SmartTerrain("test_smart");
const stalker: ServerHumanObject = mockServerAlifeHumanStalker();

smartTerrain.ini = smartTerrain.spawn_ini();

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

const [jobsList] = createStalkerCollectorJobs(smartTerrain);
const precondition = jobsList.jobs.get(1)._precondition_function;

expect(precondition?.(stalker, smartTerrain, {}, {})).toBe(false);

registerObject(
mockClientGameObject({
idOverride: stalker.id,
object: (section: string) => {
return section === "detector_elite" ? mockClientGameObject() : null;
},
})
);
expect(precondition?.(stalker, smartTerrain, {}, {})).toBe(true);

jest.spyOn(stalker, "community").mockImplementation(() => "zombied");
expect(precondition?.(stalker, smartTerrain, {}, {})).toBe(false);
});
});
38 changes: 11 additions & 27 deletions src/engine/core/utils/job/job_create_stalker_collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isJobPatrolInRestrictor } from "@/engine/core/utils/job/job_check";
import { IJobListDescriptor } from "@/engine/core/utils/job/job_types";
import { logicsConfig } from "@/engine/lib/configs/LogicsConfig";
import { communities } from "@/engine/lib/constants/communities";
import { detectors } from "@/engine/lib/constants/items/detectors";
import {
AnyObject,
ClientObject,
Expand Down Expand Up @@ -54,22 +55,13 @@ export function createStalkerCollectorJobs(

const state: Optional<IRegistryObjectState> = registry.objects.get(serverObject.id);

if (state === null) {
if (state === null || state.object === null) {
return false;
}

const object: Optional<ClientObject> = state.object!;

if (object === null) {
return false;
}

const detectors = ["detector_simple", "detector_advanced", "detector_elite", "detector_scientific"];

for (const [k, v] of detectors) {
const obj = object.object(v);

if (obj !== null) {
// todo: object has detector util?
for (const [, value] of pairs(detectors)) {
if (state.object.object(value) !== null) {
return true;
}
}
Expand All @@ -79,20 +71,12 @@ export function createStalkerCollectorJobs(
});

let jobLtx: string =
"[logic@" +
wayName +
"]\n" +
"active = walker@" +
wayName +
"\n" +
"[walker@" +
wayName +
"]\n" +
`[logic@${wayName}]\n` +
`active = walker@${wayName}\n` +
`[walker@${wayName}]\n` +
"sound_idle = state\n" +
"meet = meet@generic_lager\n" +
"path_walk = collector_" +
index +
"_walk\n" +
`path_walk = collector_${index}_walk\n` +
"def_state_standing = guard\n" +
"def_state_moving = patrol\n";

Expand All @@ -117,8 +101,8 @@ export function createStalkerCollectorJobs(
isJobPatrolInRestrictor(smartTerrain, smartTerrain.smartTerrainActorControl.ignoreZone, wayName)
) {
jobLtx +=
"combat_ignore_cond = {=npc_in_zone(smart.base_on_actor_control.ignore_zone)} true \n" +
"combat_ignore_keep_when_attacked = true \n";
"combat_ignore_cond = {=npc_in_zone(smart.base_on_actor_control.ignore_zone)} true\n" +
"combat_ignore_keep_when_attacked = true\n";
}

ltx += jobLtx;
Expand Down
4 changes: 2 additions & 2 deletions src/engine/core/utils/job/job_create_stalker_sleep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ describe("jobs_general should correctly generate stalkers sleep jobs", () => {

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

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

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

const [jobsList, ltx, count] = createStalkerSleepJobs(smartTerrain);
Expand Down
Loading

0 comments on commit ff0c97a

Please sign in to comment.