Skip to content

Commit

Permalink
Adding tests for object set/sound utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neloreck committed Jul 6, 2023
1 parent 3783719 commit 67b54a3
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/engine/core/managers/interaction/SimulationBoardManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { abort } from "@/engine/core/utils/assertion";
import { parseStringsList } from "@/engine/core/utils/ini";
import { LuaLogger } from "@/engine/core/utils/logging";
import { evaluateSimulationPriority } from "@/engine/core/utils/object/object_alife";
import { changeTeamSquadGroup } from "@/engine/core/utils/object/object_set";
import { setObjectTeamSquadGroup } from "@/engine/core/utils/object/object_set";
import { setSquadRelationToActor } from "@/engine/core/utils/relation";
import { TCommunity } from "@/engine/lib/constants/communities";
import { levels, TLevel } from "@/engine/lib/constants/levels";
Expand Down Expand Up @@ -382,12 +382,12 @@ export class SimulationBoardManager extends AbstractCoreManager {
object = alife().object(object.id)!;

// todo: Check, probably magic or unused code with duplicated changeTeam calls.
changeTeamSquadGroup(object, object.team, object.squad, groupId);
setObjectTeamSquadGroup(object, object.team, object.squad, groupId);

const squad: Optional<Squad> = alife().object<Squad>(object.group_id);

if (squad === null) {
return changeTeamSquadGroup(object, object.team, 0, object.group);
return setObjectTeamSquadGroup(object, object.team, 0, object.group);
}

let smartTerrain: Optional<SmartTerrain> = null;
Expand All @@ -399,7 +399,7 @@ export class SimulationBoardManager extends AbstractCoreManager {
}

if (smartTerrain === null) {
return changeTeamSquadGroup(object, object.team, 0, object.group);
return setObjectTeamSquadGroup(object, object.team, 0, object.group);
}

let objectSquadId: TNumberId = 0;
Expand All @@ -408,7 +408,7 @@ export class SimulationBoardManager extends AbstractCoreManager {
objectSquadId = smartTerrain.squadId;
}

changeTeamSquadGroup(object, object.team, objectSquadId, object.group);
setObjectTeamSquadGroup(object, object.team, objectSquadId, object.group);
}

/**
Expand Down
46 changes: 46 additions & 0 deletions src/engine/core/utils/object/object_set.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, expect, it } from "@jest/globals";

import { registerObject } from "@/engine/core/database";
import { setItemCondition, setObjectTeamSquadGroup } from "@/engine/core/utils/object/object_set";
import { ClientObject, ServerHumanObject } from "@/engine/lib/types";
import { mockClientGameObject, mockServerAlifeHumanStalker } from "@/fixtures/xray";

describe("object_set utils", () => {
it("'setItemCondition' should correctly set condition", () => {
const object: ClientObject = mockClientGameObject();

setItemCondition(object, 25);
expect(object.set_condition).toHaveBeenCalledWith(0.25);

setItemCondition(object, 100);
expect(object.set_condition).toHaveBeenNthCalledWith(2, 1);

setItemCondition(object, 0);
expect(object.set_condition).toHaveBeenNthCalledWith(3, 0);
});

it("'setObjectTeamSquadGroup' should correctly set object group details", () => {
const firstObject: ClientObject = mockClientGameObject();
const firstServerObject: ServerHumanObject = mockServerAlifeHumanStalker({ id: firstObject.id() });

setObjectTeamSquadGroup(firstServerObject, 432, 543, 654);

expect(firstServerObject.team).toBe(432);
expect(firstServerObject.squad).toBe(543);
expect(firstServerObject.group).toBe(654);

expect(firstObject.change_team).not.toHaveBeenCalled();

const secondObject: ClientObject = mockClientGameObject();
const secondServerObject: ServerHumanObject = mockServerAlifeHumanStalker({ id: secondObject.id() });

registerObject(secondObject);
setObjectTeamSquadGroup(secondServerObject, 443, 444, 445);

expect(secondServerObject.team).not.toBe(443);
expect(secondServerObject.squad).not.toBe(444);
expect(secondServerObject.group).not.toBe(445);

expect(secondObject.change_team).toHaveBeenCalledWith(443, 444, 445);
});
});
21 changes: 7 additions & 14 deletions src/engine/core/utils/object/object_set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,26 @@ export function setItemCondition(object: ClientObject, condition: TRate): void {
}

/**
* todo;
*/
export function disableObjectInvulnerability(object: ClientObject): void {
object.invulnerable(false);
}

/**
* todo;
* Change object team/squad/group.
*
* @param serverObject - alife server object to change team parameters
* @param teamId - ?
* @param squadId - id of the parent squad, bound to spawning smart
* @param groupId - id of the level group
*
*/
export function changeTeamSquadGroup(
export function setObjectTeamSquadGroup(
serverObject: ServerCreatureObject,
teamId: TNumberId,
squadId: TNumberId,
groupId: TNumberId
): void {
const clientObject: Optional<ClientObject> = registry.objects.get(serverObject.id)?.object;
const clientObject: Optional<ClientObject> = registry.objects.get(serverObject.id)?.object as Optional<ClientObject>;

if (clientObject === null) {
if (clientObject) {
clientObject.change_team(teamId, squadId, groupId);
} else {
serverObject.team = teamId;
serverObject.squad = squadId;
serverObject.group = groupId;
} else {
clientObject.change_team(teamId, squadId, groupId);
}
}
22 changes: 22 additions & 0 deletions src/engine/core/utils/object/object_sound.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, it } from "@jest/globals";

import { stopPlayingObjectSound } from "@/engine/core/utils/object/object_sound";
import { ClientObject } from "@/engine/lib/types";
import { replaceFunctionMock } from "@/fixtures/utils";
import { mockClientGameObject } from "@/fixtures/xray";

describe("object_sound utils", () => {
it("'stopPlayingObjectSound' should correctly reset object sound play", () => {
const object: ClientObject = mockClientGameObject();

replaceFunctionMock(object.alive, () => false);
stopPlayingObjectSound(object);
expect(object.set_sound_mask).not.toHaveBeenCalled();

replaceFunctionMock(object.alive, () => true);
stopPlayingObjectSound(object);
expect(object.set_sound_mask).toHaveBeenCalledTimes(2);
expect(object.set_sound_mask).toHaveBeenNthCalledWith(1, -1);
expect(object.set_sound_mask).toHaveBeenNthCalledWith(2, 0);
});
});
4 changes: 2 additions & 2 deletions src/engine/core/utils/object/object_spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ export function spawnAmmoForObject(
* @param itemSections - list of possible sections
* @param count - count of items to spawn
*/
export function spawnItemsForObjectFromList(
export function spawnItemsForObjectFromList<T extends TSection>(
object: AnyGameObject,
itemSections: LuaArray<TSection>,
itemSections: LuaArray<T>,
count: TCount = 1
): void {
if (count < 1 || itemSections.length() < 1) {
Expand Down
6 changes: 3 additions & 3 deletions src/engine/core/utils/scheme/scheme_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { registry } from "@/engine/core/database";
import { EEvaluatorId, TAbstractSchemeConstructor } from "@/engine/core/schemes";
import { assert } from "@/engine/core/utils/assertion";
import { LuaLogger } from "@/engine/core/utils/logging";
import { disableObjectInvulnerability } from "@/engine/core/utils/object/object_set";
import { ActionBase, ClientObject, EScheme, ESchemeType, LuaArray } from "@/engine/lib/types";

const logger: LuaLogger = new LuaLogger($filename);
Expand Down Expand Up @@ -73,14 +72,15 @@ export function disableObjectBaseSchemes(object: ClientObject, schemeType: ESche
registry.schemes.get(EScheme.ACTOR_DIALOGS).disable(object, EScheme.ACTOR_DIALOGS);
registry.schemes.get(EScheme.COMBAT_IGNORE).disable(object, EScheme.COMBAT_IGNORE);

disableObjectInvulnerability(object);
object.invulnerable(false);

return;

case ESchemeType.MONSTER:
registry.schemes.get(EScheme.MOB_COMBAT).disable(object, EScheme.MOB_COMBAT);
registry.schemes.get(EScheme.COMBAT_IGNORE).disable(object, EScheme.COMBAT_IGNORE);
disableObjectInvulnerability(object);

object.invulnerable(false);

return;

Expand Down
3 changes: 3 additions & 0 deletions src/fixtures/xray/mocks/objects/client/game_object.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export function mockClientGameObject({
give_money: give_money || jest.fn((value: number) => (objectMoney += value)),
give_talk_message2,
give_task,
group: rest.group || jest.fn(),
has_info: has_info || jest.fn((it: string) => internalInfos.includes(it)),
health,
id: id || jest.fn(() => idOverride),
Expand Down Expand Up @@ -246,12 +247,14 @@ export function mockClientGameObject({
delete callbacks[id];
}
}),
set_condition: rest.set_condition || jest.fn(),
set_manual_invisibility: rest.set_manual_invisibility || jest.fn(),
set_mental_state: rest.set_mental_state || jest.fn(),
set_nonscript_usable: rest.set_nonscript_usable || jest.fn(),
set_home: rest.set_home || jest.fn(),
set_invisible: rest.set_invisible || jest.fn(),
set_relation: rest.set_relation || jest.fn(),
set_sound_mask: rest.set_sound_mask || jest.fn(),
set_sympathy: rest.set_sympathy || jest.fn(),
sight_params:
rest.sight_params ||
Expand Down

0 comments on commit 67b54a3

Please sign in to comment.