-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated crow spawner scheme. Added docs and tests for sr_crow_spawner…
…. Mock updates.
- Loading branch information
Showing
10 changed files
with
249 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/engine/core/schemes/restrictor/sr_crow_spawner/CrowSpawnerManager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
jest.mock("@/engine/core/utils/scheme/scheme_switch", () => ({ trySwitchToAnotherSection: jest.fn() })); | ||
|
||
import { describe, expect, it, jest } from "@jest/globals"; | ||
import { alife, patrol } from "xray16"; | ||
|
||
import { registerActor } from "@/engine/core/database"; | ||
import { CrowSpawnerManager } from "@/engine/core/schemes/restrictor/sr_crow_spawner/CrowSpawnerManager"; | ||
import { ISchemeCrowSpawnerState } from "@/engine/core/schemes/restrictor/sr_crow_spawner/ISchemeCrowSpawnerState"; | ||
import { range } from "@/engine/core/utils/number"; | ||
import { trySwitchToAnotherSection } from "@/engine/core/utils/scheme"; | ||
import { AlifeSimulator, ClientObject, EScheme, Patrol } from "@/engine/lib/types"; | ||
import { mockSchemeState } from "@/fixtures/engine"; | ||
import { mockActorClientGameObject, mockClientGameObject } from "@/fixtures/xray"; | ||
import { MockVector } from "@/fixtures/xray/mocks/vector.mock"; | ||
|
||
describe("CrowSpawnerManager class", () => { | ||
it("should correctly initialize", () => { | ||
const object: ClientObject = mockClientGameObject(); | ||
const state: ISchemeCrowSpawnerState = mockSchemeState(EScheme.SR_CROW_SPAWNER); | ||
const manager: CrowSpawnerManager = new CrowSpawnerManager(object, state); | ||
|
||
expect(manager.nextUpdateAt).toBe(0); | ||
expect(manager.spawnPointsUpdateAt).toEqualLuaTables({}); | ||
}); | ||
|
||
it("should correctly handle updates", () => { | ||
const object: ClientObject = mockClientGameObject(); | ||
const state: ISchemeCrowSpawnerState = mockSchemeState(EScheme.SR_CROW_SPAWNER); | ||
const manager: CrowSpawnerManager = new CrowSpawnerManager(object, state); | ||
|
||
jest.spyOn(Date, "now").mockImplementation(() => 5500); | ||
jest.spyOn(manager, "spawnCrows").mockImplementation(jest.fn()); | ||
|
||
state.maxCrowsOnLevel = 10; | ||
manager.nextUpdateAt = Infinity; | ||
manager.update(); | ||
expect(manager.spawnCrows).not.toHaveBeenCalled(); | ||
expect(trySwitchToAnotherSection).toHaveBeenCalledTimes(1); | ||
|
||
state.maxCrowsOnLevel = 0; | ||
manager.nextUpdateAt = 0; | ||
manager.update(); | ||
expect(manager.nextUpdateAt).toBe(125_500); | ||
expect(manager.spawnCrows).not.toHaveBeenCalled(); | ||
expect(trySwitchToAnotherSection).toHaveBeenCalledTimes(2); | ||
|
||
state.maxCrowsOnLevel = 10; | ||
manager.nextUpdateAt = 0; | ||
manager.update(); | ||
expect(manager.spawnCrows).toHaveBeenCalled(); | ||
expect(manager.nextUpdateAt).toBe(0); | ||
expect(trySwitchToAnotherSection).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it("should correctly handle crow spawn", () => { | ||
const simulator: AlifeSimulator = alife(); | ||
const object: ClientObject = mockClientGameObject(); | ||
const state: ISchemeCrowSpawnerState = mockSchemeState(EScheme.SR_CROW_SPAWNER); | ||
const manager: CrowSpawnerManager = new CrowSpawnerManager(object, state); | ||
|
||
registerActor(mockActorClientGameObject()); | ||
|
||
jest.spyOn(Date, "now").mockImplementation(() => 5500); | ||
|
||
state.maxCrowsOnLevel = 10; | ||
state.pathsList = $fromArray(["test_smart_guard_1_walk", "test_smart_patrol_1_walk"]); | ||
|
||
for (const [, name] of state.pathsList) { | ||
const crowPatrol: Patrol = new patrol(name); | ||
|
||
jest.spyOn(crowPatrol.point(0), "distance_to_sqr").mockImplementation(() => Infinity); | ||
} | ||
|
||
manager.spawnCrows(); | ||
manager.spawnCrows(); | ||
|
||
// After two iterations time is set. | ||
expect(manager.spawnPointsUpdateAt).toEqualLuaTables({ | ||
test_smart_guard_1_walk: 15_500, | ||
test_smart_patrol_1_walk: 15_500, | ||
}); | ||
expect(simulator.create).toHaveBeenCalledTimes(2); | ||
|
||
jest.spyOn(Date, "now").mockImplementation(() => 10_000); | ||
|
||
manager.spawnCrows(); | ||
manager.spawnCrows(); | ||
|
||
// No updates. | ||
expect(manager.spawnPointsUpdateAt).toEqualLuaTables({ | ||
test_smart_guard_1_walk: 15_500, | ||
test_smart_patrol_1_walk: 15_500, | ||
}); | ||
expect(simulator.create).toHaveBeenCalledTimes(2); | ||
|
||
jest.spyOn(Date, "now").mockImplementation(() => 50_000); | ||
|
||
manager.spawnCrows(); | ||
manager.spawnCrows(); | ||
|
||
// Updated on timeout. | ||
expect(manager.spawnPointsUpdateAt).toEqualLuaTables({ | ||
test_smart_guard_1_walk: 60_000, | ||
test_smart_patrol_1_walk: 60_000, | ||
}); | ||
expect(simulator.create).toHaveBeenCalledTimes(4); | ||
|
||
range(4).forEach((it) => { | ||
expect(simulator.create).toHaveBeenNthCalledWith( | ||
it + 1, | ||
"m_crow", | ||
expect.any(MockVector), | ||
expect.any(Number), | ||
expect.any(Number) | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/engine/core/schemes/restrictor/sr_crow_spawner/SchemeCrowSpawner.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { beforeEach, describe, expect, it } from "@jest/globals"; | ||
|
||
import { IRegistryObjectState, registerObject } from "@/engine/core/database"; | ||
import { ISchemeCrowSpawnerState } from "@/engine/core/schemes/restrictor/sr_crow_spawner/ISchemeCrowSpawnerState"; | ||
import { SchemeCrowSpawner } from "@/engine/core/schemes/restrictor/sr_crow_spawner/SchemeCrowSpawner"; | ||
import { parseConditionsList } from "@/engine/core/utils/ini"; | ||
import { loadSchemeImplementation } from "@/engine/core/utils/scheme"; | ||
import { ClientObject, EScheme, ESchemeType, IniFile } from "@/engine/lib/types"; | ||
import { mockBaseSchemeLogic } from "@/fixtures/engine"; | ||
import { mockClientGameObject, mockIniFile } from "@/fixtures/xray"; | ||
|
||
describe("SchemeCrowSpawner", () => { | ||
beforeEach(() => { | ||
loadSchemeImplementation(SchemeCrowSpawner); | ||
}); | ||
|
||
it("should be correctly defined", () => { | ||
expect(SchemeCrowSpawner.SCHEME_SECTION).toBe("sr_crow_spawner"); | ||
expect(SchemeCrowSpawner.SCHEME_TYPE).toBe(ESchemeType.RESTRICTOR); | ||
}); | ||
|
||
it("should correctly read ini configuration", () => { | ||
const object: ClientObject = mockClientGameObject(); | ||
const state: IRegistryObjectState = registerObject(object); | ||
const ini: IniFile = mockIniFile("example.ltx", { | ||
"sr_crow_spawner@test": { | ||
on_info: "{+test_info} another@section", | ||
max_crows_on_level: 48, | ||
spawn_path: "a, b, c, d", | ||
}, | ||
}); | ||
|
||
SchemeCrowSpawner.activate(object, ini, EScheme.SR_CROW_SPAWNER, "sr_crow_spawner@test"); | ||
|
||
const schemeState: ISchemeCrowSpawnerState = state[EScheme.SR_CROW_SPAWNER] as ISchemeCrowSpawnerState; | ||
|
||
expect(schemeState.maxCrowsOnLevel).toBe(48); | ||
expect(schemeState.pathsList).toEqualLuaArrays(["a", "b", "c", "d"]); | ||
expect(schemeState.logic).toEqualLuaArrays([ | ||
mockBaseSchemeLogic({ name: "on_info", condlist: parseConditionsList("{+test_info} another@section") }), | ||
]); | ||
}); | ||
|
||
it("should correctly read empty configuration", () => { | ||
const object: ClientObject = mockClientGameObject(); | ||
const state: IRegistryObjectState = registerObject(object); | ||
const ini: IniFile = mockIniFile("another.ltx", {}); | ||
|
||
SchemeCrowSpawner.activate(object, ini, EScheme.SR_CROW_SPAWNER, "sr_crow_spawner@another"); | ||
|
||
const schemeState: ISchemeCrowSpawnerState = state[EScheme.SR_CROW_SPAWNER] as ISchemeCrowSpawnerState; | ||
|
||
expect(schemeState.maxCrowsOnLevel).toBe(16); | ||
expect(schemeState.pathsList).toEqualLuaArrays([]); | ||
expect(schemeState.logic).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.