Skip to content

Commit

Permalink
Tests for smart terrain onObjectDeath. Remove package.path updati…
Browse files Browse the repository at this point in the history
…ng in _g root, it is part of oxr from now. Update game engines version.

Signed-off-by: Neloreck <Neloreck@gmail.com>
  • Loading branch information
Neloreck committed Dec 27, 2024
1 parent 40b78b0 commit ffe6918
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cli/bin
Submodule bin updated 42 files
+ engines/gold/bin/LuaJIT.dll
+1 −1 engines/gold/bin/bin.json
+ engines/gold/bin/xrCDB.dll
+ engines/gold/bin/xrCore.dll
+ engines/gold/bin/xrEngine.dll
+ engines/gold/bin/xrEngine.exe
+ engines/gold/bin/xrGame.dll
+ engines/gold/bin/xrParticles.dll
+ engines/gold/bin/xrRender_GL.dll
+ engines/gold/bin/xrRender_R4.dll
+ engines/gold/bin/xrScriptEngine.dll
+ engines/gold/bin/xrSound.dll
+ engines/gold/bin/xrUICore.dll
+ engines/mixed/bin/LuaJIT.dll
+1 −1 engines/mixed/bin/bin.json
+ engines/mixed/bin/xrAICore.dll
+ engines/mixed/bin/xrCDB.dll
+ engines/mixed/bin/xrCore.dll
+ engines/mixed/bin/xrEngine.dll
+ engines/mixed/bin/xrEngine.exe
+ engines/mixed/bin/xrGame.dll
+ engines/mixed/bin/xrParticles.dll
+ engines/mixed/bin/xrPhysics.dll
+ engines/mixed/bin/xrRender_GL.dll
+ engines/mixed/bin/xrRender_R4.dll
+ engines/mixed/bin/xrScriptEngine.dll
+ engines/mixed/bin/xrSound.dll
+ engines/mixed/bin/xrUICore.dll
+ engines/release/bin/LuaJIT.dll
+1 −1 engines/release/bin/bin.json
+ engines/release/bin/xrCDB.dll
+ engines/release/bin/xrCore.dll
+ engines/release/bin/xrEngine.dll
+ engines/release/bin/xrEngine.exe
+ engines/release/bin/xrGame.dll
+ engines/release/bin/xrParticles.dll
+ engines/release/bin/xrPhysics.dll
+ engines/release/bin/xrRender_GL.dll
+ engines/release/bin/xrRender_R4.dll
+ engines/release/bin/xrScriptEngine.dll
+ engines/release/bin/xrSound.dll
+ engines/release/bin/xrUICore.dll
62 changes: 59 additions & 3 deletions src/engine/core/objects/smart_terrain/SmartTerrain.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import { time_global } from "xray16";
import { beforeEach, describe, expect, fit, it, jest } from "@jest/globals";
import { game, time_global } from "xray16";

import { getManager, registerActorServer, registerSimulator, registry } from "@/engine/core/database";
import { EGameEvent, EventsManager } from "@/engine/core/managers/events";
Expand All @@ -14,7 +14,7 @@ import { createObjectJobDescriptor } from "@/engine/core/objects/smart_terrain/j
import { ESmartTerrainStatus } from "@/engine/core/objects/smart_terrain/smart_terrain_types";
import { parseConditionsList } from "@/engine/core/utils/ini";
import { TRUE } from "@/engine/lib/constants/words";
import { IniFile, ServerHumanObject } from "@/engine/lib/types";
import { GameObject, IniFile, ServerCreatureObject, ServerHumanObject, ServerObject } from "@/engine/lib/types";
import { MockSmartTerrain, resetRegistry } from "@/fixtures/engine";
import { replaceFunctionMock } from "@/fixtures/jest";
import {
Expand All @@ -23,8 +23,10 @@ import {
MockAlifeHumanStalker,
MockCALifeSmartTerrainTask,
MockCTime,
MockGameObject,
MockIniFile,
MockNetProcessor,
MockServerAlifeCreatureAbstract,
} from "@/fixtures/xray";

describe("SmartTerrain generic logic", () => {
Expand Down Expand Up @@ -423,6 +425,60 @@ describe("SmartTerrain generic logic", () => {
expect(terrain.objectsToRegister.get(second.id)).toBe(second);
});

it("onObjectDeath should correctly handle dying of object assigned", () => {
const terrain: SmartTerrain = MockSmartTerrain.mockRegistered();
const object: GameObject = MockGameObject.mock();
const serverObject: ServerCreatureObject = MockServerAlifeCreatureAbstract.mock({ id: object.id() });

jest.spyOn(serverObject.position, "distance_to_sqr").mockImplementation(() => 100 * 100 - 1);

terrain.register_npc(serverObject);

expect(serverObject.m_smart_terrain_id).toBe(terrain.id);
expect(terrain.stayingObjectsCount).toBe(1);
expect(terrain.objectJobDescriptors.length()).toBe(1);
expect(terrain.jobDeadTimeById.length()).toBe(0);
expect(terrain.arrivingObjects.length()).toBe(0);

terrain.onObjectDeath(serverObject);

expect(terrain.objectJobDescriptors.length()).toBe(0);
expect(terrain.arrivingObjects.length()).toBe(0);
expect(terrain.jobDeadTimeById.length()).toBe(1);
expect(MockCTime.areEqual(terrain.jobDeadTimeById.get(1), game.get_game_time())).toBe(true);
expect(serverObject.clear_smart_terrain).toHaveBeenCalledTimes(1);
});

it("onObjectDeath should correctly handle dying of object arriving", () => {
const terrain: SmartTerrain = MockSmartTerrain.mockRegistered();
const object: GameObject = MockGameObject.mock();
const serverObject: ServerCreatureObject = MockServerAlifeCreatureAbstract.mock({ id: object.id() });

jest.spyOn(serverObject.position, "distance_to_sqr").mockImplementation(() => 100 * 100 + 1);

terrain.register_npc(serverObject);

expect(serverObject.m_smart_terrain_id).toBe(terrain.id);
expect(terrain.stayingObjectsCount).toBe(1);
expect(terrain.objectJobDescriptors.length()).toBe(0);
expect(terrain.arrivingObjects).toEqualLuaTables({ [serverObject.id]: serverObject });

terrain.onObjectDeath(serverObject);

expect(terrain.arrivingObjects).toEqualLuaTables({});
expect(serverObject.clear_smart_terrain).toHaveBeenCalledTimes(1);
});

it("onObjectDeath should correctly throw on unexpected conditions", () => {
const terrain: SmartTerrain = MockSmartTerrain.mock();
const object: GameObject = MockGameObject.mock();
const serverObject: ServerCreatureObject = MockServerAlifeCreatureAbstract.mock({ id: object.id() });

expect(() => terrain.onObjectDeath(serverObject)).toThrow(
`Smart terrain object is assigned, but not arriving or on job: '${serverObject.name()}', at '${terrain.name()}'.`
);
});

it.todo("register_npc should correctly count and assign objects after registration");

it.todo("should handle simulation callbacks");
Expand Down
13 changes: 10 additions & 3 deletions src/engine/core/objects/smart_terrain/SmartTerrain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,30 +681,37 @@ export class SmartTerrain extends cse_alife_smart_zone implements ISimulationTar
}

/**
* todo: Description.
* Handle death of smart terrain assigned object.
*
* @param object - dying object assigned to terrain
*/
public onObjectDeath(object: ServerCreatureObject): void {
logger.info("Clear dead: %s %s", this.name(), object.name());
logger.info("Clear assigned object on death: %s %s", this.name(), object.name());

// Object arrived and aws assigned to job.
if (this.objectJobDescriptors.get(object.id) as Optional<IObjectJobState>) {
this.jobDeadTimeById.set(this.objectJobDescriptors.get(object.id).jobId, game.get_game_time());

this.objectJobDescriptors.get(object.id).job!.objectId = null;
this.objectJobDescriptors.delete(object.id);

object.clear_smart_terrain();

return;
}

// Object was in process of arriving.
if (this.arrivingObjects.get(object.id) as Optional<ServerCreatureObject>) {
this.arrivingObjects.delete(object.id);

object.clear_smart_terrain();

return;
}

abort("this.npc_info[obj.id] = null !!! obj.id=%d", object.id);
abort("Smart terrain object is assigned, but not arriving or on job: '%s', at '%s'.", object.name(), this.name());
}

/**
* todo: Description.
*/
Expand Down
11 changes: 1 addition & 10 deletions src/engine/scripts/_g.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { getFS } from "xray16";

/**
* Enable modules requiring.
* Set root folder for lua scripts as $game_data filesystem path.
*/
// @ts-ignore lua globals package, conflicting with javascript strict mode internals.
package.path += getFS().update_path("$game_data$", "?.script;");

/**
* Initialize external references.
* Initialize external references (effects/callbacks/conditions).
*/
// eslint-disable-next-line @typescript-eslint/no-var-requires
require("@/engine/scripts/register/externals_registrator").registerExternals();
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ export class MockAlifeObject extends MockLuabindClass {

public update = jest.fn();

public clear_smart_terrain = jest.fn();

public STATE_Write(packet: NetPacket): void {
packet.w_stringZ(`state_write_from_${this.constructor.name}`);
}
Expand Down

0 comments on commit ffe6918

Please sign in to comment.