Skip to content

Commit

Permalink
Test utils related to alife updating. Mocks extension. Update of xray…
Browse files Browse the repository at this point in the history
…16 types.
  • Loading branch information
Neloreck committed Jul 6, 2023
1 parent 4dd42d9 commit 14d9004
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
18 changes: 18 additions & 0 deletions src/engine/core/utils/object/object_alife.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { describe, expect, it } from "@jest/globals";
import { alife } from "xray16";

import {
evaluateSimulationPriority,
evaluateSimulationPriorityByDistance,
setStableAlifeObjectsUpdate,
setUnlimitedAlifeObjectsUpdate,
} from "@/engine/core/utils/object/object_alife";
import { mockLuaTable } from "@/fixtures/lua/mocks/LuaTable.mock";
import { resetFunctionMock } from "@/fixtures/utils";
import { mockServerAlifeObject } from "@/fixtures/xray/mocks/objects/server/cse_alife_object.mock";
import { mockSquad } from "@/fixtures/xray/mocks/objects/server/Squad.mock";
import { MockVector } from "@/fixtures/xray/mocks/vector.mock";
Expand Down Expand Up @@ -35,4 +39,18 @@ describe("'alife' utils", () => {
)
).toBe(29.700000000000003);
});

it("'setUnlimitedAlifeObjectsUpdate' should correctly set high updates limits", () => {
resetFunctionMock(alife().set_objects_per_update);
setUnlimitedAlifeObjectsUpdate();

expect(alife().set_objects_per_update).toHaveBeenCalledWith(2_147_483_647);
});

it("'setStableAlifeObjectsUpdate' should correctly set high updates limits", () => {
resetFunctionMock(alife().set_objects_per_update);
setStableAlifeObjectsUpdate();

expect(alife().set_objects_per_update).toHaveBeenCalledWith(20);
});
});
25 changes: 17 additions & 8 deletions src/engine/core/utils/object/object_alife.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,35 @@ export function setStableAlifeObjectsUpdate(): void {
}

/**
* todo;
* Evaluates simulation priority by distance.
* Used as normalizer to pick better tasks based on distance from object.
*
* @param first - one of objects to measure priority by distance
* @param second - one of objects to measure priority by distance
* @returns priority evaluated by distance
*/
export function evaluateSimulationPriorityByDistance(target: ServerObject, squad: ServerObject): TRate {
const distance: TDistance = math.max(getServerDistanceBetween(target, squad), 1);
export function evaluateSimulationPriorityByDistance(first: ServerObject, second: ServerObject): TRate {
const distance: TDistance = math.max(getServerDistanceBetween(first, second), 1);

return 1 + 1 / distance;
}

/**
* todo;
* Evaluate objects selection priority for alife simulation.
*
* @param object - simulation participating game object
* @param squad - squad participating in simulation
* @returns alife simulation priority for target selection
*/
export function evaluateSimulationPriority(object: TSimulationObject, target: Squad): TRate {
export function evaluateSimulationPriority(object: TSimulationObject, squad: Squad): TRate {
let priority: TRate = 3;

// Blocking level traveling and specific preconditions.
if (!object.isValidSquadTarget(target) || !areObjectsOnSameLevel(object, target)) {
if (!object.isValidSquadTarget(squad) || !areObjectsOnSameLevel(object, squad)) {
return 0;
}

for (const [property, rate] of target.behaviour) {
for (const [property, rate] of squad.behaviour) {
const squadCoefficient: TRate = tonumber(rate) as TRate;
let targetCoefficient: TRate = 0;

Expand All @@ -57,5 +66,5 @@ export function evaluateSimulationPriority(object: TSimulationObject, target: Sq
priority = priority + squadCoefficient * targetCoefficient;
}

return priority * evaluateSimulationPriorityByDistance(object, target);
return priority * evaluateSimulationPriorityByDistance(object, squad);
}
3 changes: 2 additions & 1 deletion src/engine/core/utils/object/object_task_reward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { abort, assert, assertDefined } from "@/engine/core/utils/assertion";
import { LuaLogger } from "@/engine/core/utils/logging";
import { spawnItemsForObject } from "@/engine/core/utils/object/object_spawn";
import { ACTOR_ID } from "@/engine/lib/constants/ids";
import { ammo, TAmmoItem } from "@/engine/lib/constants/items/ammo";
import { medkits, TMedkit } from "@/engine/lib/constants/items/drugs";
import { ClientObject, LuaArray, Optional, TCount, TName, TNumberId, TSection } from "@/engine/lib/types";
Expand Down Expand Up @@ -51,7 +52,7 @@ export function transferMoneyFromActor(to: ClientObject, amount: TCount): void {
* From two possible speakers pick NPC one, omit actor.
*/
export function getNpcSpeaker(first: ClientObject, second: ClientObject): ClientObject {
return registry.actor.id() === second.id() ? first : second;
return second.id() === ACTOR_ID ? first : second;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/fixtures/xray/mocks/objects/AlifeSimulator.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export class MockAlifeSimulator {

public level_id = jest.fn(() => 3);

public set_objects_per_update = jest.fn(() => {});

public release = jest.fn((object: ServerObject) => {
MockAlifeSimulator.removeFromRegistry(object.id);
});
Expand Down

0 comments on commit 14d9004

Please sign in to comment.