Skip to content

Commit

Permalink
Add unit tests for monster command util. More mocks. Rename action
Browse files Browse the repository at this point in the history
…util method.
  • Loading branch information
Neloreck committed Jul 6, 2023
1 parent 75465d9 commit 4dd42d9
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 80 deletions.
9 changes: 5 additions & 4 deletions src/engine/core/objects/binders/creature/MonsterBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import { ESchemeEvent, IBaseSchemeState } from "@/engine/core/schemes";
import { SchemeHear } from "@/engine/core/schemes/hear/SchemeHear";
import { pickSectionFromCondList, TConditionList } from "@/engine/core/utils/ini";
import { LuaLogger } from "@/engine/core/utils/logging";
import { action, getObjectSquad } from "@/engine/core/utils/object";
import { getObjectSquad } from "@/engine/core/utils/object";
import {
emitSchemeEvent,
isMonsterScriptCaptured,
scriptCaptureMonster,
scriptCommandMonster,
scriptReleaseMonster,
trySwitchToAnotherSection,
} from "@/engine/core/utils/scheme";
Expand Down Expand Up @@ -124,14 +125,14 @@ export class MonsterBinder extends object_binder {
scriptCaptureMonster(this.object, true);

if (squad.commander_id() === this.object.id()) {
action(this.object, new move(move.walk_with_leader, targetPosition), new cond(cond.move_end));
scriptCommandMonster(this.object, new move(move.walk_with_leader, targetPosition), new cond(cond.move_end));
} else {
const commanderPosition: Vector = alife().object(squad.commander_id())!.position;

if (commanderPosition.distance_to_sqr(this.object.position()) > 100) {
action(this.object, new move(move.run_with_leader, targetPosition), new cond(cond.move_end));
scriptCommandMonster(this.object, new move(move.run_with_leader, targetPosition), new cond(cond.move_end));
} else {
action(this.object, new move(move.walk_with_leader, targetPosition), new cond(cond.move_end));
scriptCommandMonster(this.object, new move(move.walk_with_leader, targetPosition), new cond(cond.move_end));
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/engine/core/schemes/mob_jump/MobJumpManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { cond, look, patrol } from "xray16";
import { AbstractSchemeManager } from "@/engine/core/schemes";
import { ISchemeMobJumpState } from "@/engine/core/schemes/mob_jump/ISchemeMobJumpState";
import { abort } from "@/engine/core/utils/assertion";
import { action } from "@/engine/core/utils/object/object_action";
import { scriptCaptureMonster, scriptReleaseMonster } from "@/engine/core/utils/scheme";
import { scriptCaptureMonster, scriptCommandMonster, scriptReleaseMonster } from "@/engine/core/utils/scheme";
import { addVectors } from "@/engine/core/utils/vector";
import { Optional, Patrol, Vector } from "@/engine/lib/types";

Expand Down Expand Up @@ -52,7 +51,7 @@ export class MobJumpManager extends AbstractSchemeManager<ISchemeMobJumpState> {
public update(delta: number): void {
if (this.stateCurrent === STATE_START_LOOK) {
if (!this.object.action()) {
action(this.object, new look(look.point, this.point!), new cond(cond.look_end));
scriptCommandMonster(this.object, new look(look.point, this.point!), new cond(cond.look_end));

this.stateCurrent = STATE_WAIT_LOOK_END;
}
Expand Down
18 changes: 8 additions & 10 deletions src/engine/core/schemes/mob_remark/MobRemarkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import { anim, cond, MonsterSpace, sound } from "xray16";

import { registry, setMonsterState } from "@/engine/core/database";
import { NotificationManager } from "@/engine/core/managers/interface/notifications";
import { AbstractSchemeManager } from "@/engine/core/schemes";
import { AbstractSchemeManager } from "@/engine/core/schemes/base";
import { ISchemeMobRemarkState } from "@/engine/core/schemes/mob_remark/ISchemeMobRemarkState";
import { abort } from "@/engine/core/utils/assertion";
import { getExtern } from "@/engine/core/utils/binding";
import { pickSectionFromCondList } from "@/engine/core/utils/ini/ini_config";
import { parseStringsList } from "@/engine/core/utils/ini/ini_parse";
import { action } from "@/engine/core/utils/object/object_action";
import { scriptCaptureMonster } from "@/engine/core/utils/scheme/scheme_monster";
import { parseStringsList, pickSectionFromCondList } from "@/engine/core/utils/ini";
import { scriptCaptureMonster, scriptCommandMonster } from "@/engine/core/utils/scheme";
import { AnyCallablesModule, LuaArray, MonsterBodyStateKey, Optional, TName } from "@/engine/lib/types";

/**
Expand Down Expand Up @@ -79,17 +77,17 @@ export class MobRemarkManager extends AbstractSchemeManager<ISchemeMobRemarkStat
}

if (this.state.anim_head) {
action(
scriptCommandMonster(
this.object,
new anim(an),
new sound(snd, "bip01_head", MonsterSpace[this.state.anim_head as MonsterBodyStateKey]),
cnd
);
} else {
if (this.state.anim_movement === true) {
action(this.object, new anim(an, true), new sound(snd, "bip01_head"), cnd);
scriptCommandMonster(this.object, new anim(an, true), new sound(snd, "bip01_head"), cnd);
} else {
action(this.object, new anim(an), new sound(snd, "bip01_head"), cnd);
scriptCommandMonster(this.object, new anim(an), new sound(snd, "bip01_head"), cnd);
}
}
} else if (an !== null) {
Expand All @@ -100,9 +98,9 @@ export class MobRemarkManager extends AbstractSchemeManager<ISchemeMobRemarkStat
}

if (this.state.anim_movement === true) {
action(this.object, new anim(an, true), cnd);
scriptCommandMonster(this.object, new anim(an, true), cnd);
} else {
action(this.object, new anim(an), cnd);
scriptCommandMonster(this.object, new anim(an), cnd);
}
}
}
Expand Down
17 changes: 8 additions & 9 deletions src/engine/core/schemes/mob_walker/MobWalkerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { ISchemeMobWalkerState } from "@/engine/core/schemes/mob_walker/ISchemeM
import { abort } from "@/engine/core/utils/assertion";
import { IWaypointData, parseWaypointsData, pickSectionFromCondList } from "@/engine/core/utils/ini";
import { isStalkerAtWaypoint } from "@/engine/core/utils/object";
import { action } from "@/engine/core/utils/object/object_action";
import { isMonsterScriptCaptured, scriptCaptureMonster } from "@/engine/core/utils/scheme/scheme_monster";
import { isMonsterScriptCaptured, scriptCaptureMonster, scriptCommandMonster } from "@/engine/core/utils/scheme";
import { copyVector } from "@/engine/core/utils/vector";
import { EMonsterState } from "@/engine/lib/constants/monsters";
import { NIL, TRUE } from "@/engine/lib/constants/words";
Expand Down Expand Up @@ -97,7 +96,7 @@ export class MobWalkerManager extends AbstractSchemeManager<ISchemeMobWalkerStat
this.lastIndex = null;
this.lastLookIndex = null;

action(
scriptCommandMonster(
this.object,
new move(move.walk_fwd, new patrol(this.state.path_walk, patrol.next, patrol.continue)),
new cond(cond.move_end)
Expand Down Expand Up @@ -247,15 +246,15 @@ export class MobWalkerManager extends AbstractSchemeManager<ISchemeMobWalkerStat
}

if (this.scheduledSound) {
action(
scriptCommandMonster(
this.object,
new move(m, new patrol(this.state.path_walk, patrol.next, patrol.continue)),
new sound(sound[this.scheduledSound]),
new cond(cond.move_end)
);
this.scheduledSound = null;
} else {
action(
scriptCommandMonster(
this.object,
new move(m, new patrol(this.state.path_walk, patrol.next, patrol.continue)),
new cond(cond.move_end)
Expand All @@ -270,15 +269,15 @@ export class MobWalkerManager extends AbstractSchemeManager<ISchemeMobWalkerStat
scriptCaptureMonster(this.object, true);

if (this.scheduledSound) {
action(
scriptCommandMonster(
this.object,
new anim(this.curAnimSet!, 0),
new sound(sound[this.scheduledSound]),
new cond(cond.time_end, this.ptWaitTime!)
);
this.scheduledSound = null;
} else {
action(this.object, new anim(this.curAnimSet!, 0), new cond(cond.time_end, this.ptWaitTime!));
scriptCommandMonster(this.object, new anim(this.curAnimSet!, 0), new cond(cond.time_end, this.ptWaitTime!));
}
}

Expand All @@ -287,7 +286,7 @@ export class MobWalkerManager extends AbstractSchemeManager<ISchemeMobWalkerStat
*/
public override deactivate(): void {
scriptCaptureMonster(this.object, true);
action(this.object, new move(move.steal, this.patrolWalk!.point(0)), new cond(cond.move_end));
scriptCommandMonster(this.object, new move(move.steal, this.patrolWalk!.point(0)), new cond(cond.move_end));
}

/**
Expand All @@ -304,7 +303,7 @@ export class MobWalkerManager extends AbstractSchemeManager<ISchemeMobWalkerStat
// --this.object:set_sight(look.direction, look_pt, 0)

scriptCaptureMonster(this.object, true);
action(this.object, new look(look.direction, lookPoint), new cond(cond.look_end));
scriptCommandMonster(this.object, new look(look.direction, lookPoint), new cond(cond.look_end));

this.lastLookIndex = pt;
}
Expand Down
11 changes: 7 additions & 4 deletions src/engine/core/schemes/sr_monster/MonsterManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { registry } from "@/engine/core/database";
import { GlobalSoundManager } from "@/engine/core/managers/sounds/GlobalSoundManager";
import { AbstractSchemeManager } from "@/engine/core/schemes";
import { ISchemeMonsterState } from "@/engine/core/schemes/sr_monster/ISchemeMonsterState";
import { action } from "@/engine/core/utils/object/object_action";
import { scriptCaptureMonster, scriptReleaseMonster } from "@/engine/core/utils/scheme";
import { trySwitchToAnotherSection } from "@/engine/core/utils/scheme/scheme_switch";
import {
scriptCaptureMonster,
scriptCommandMonster,
scriptReleaseMonster,
trySwitchToAnotherSection,
} from "@/engine/core/utils/scheme";
import { copyVector, subVectors } from "@/engine/core/utils/vector";
import { sounds } from "@/engine/lib/constants/sound/sounds";
import {
Expand Down Expand Up @@ -130,7 +133,7 @@ export class MonsterManager extends AbstractSchemeManager<ISchemeMonsterState> {

scriptCaptureMonster(this.monsterObject, true);

action(
scriptCommandMonster(
this.monsterObject,
new move(move.run_fwd, this.state.path.point(this.state.path.count() - 1)),
new cond(cond.move_end)
Expand Down
9 changes: 4 additions & 5 deletions src/engine/core/utils/object/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
export * from "@/engine/core/utils/object/object_alife";
export * from "@/engine/core/utils/object/object_action";
export * from "@/engine/core/utils/object/object_anomaly";
export * from "@/engine/core/utils/object/object_check";
export * from "@/engine/core/utils/object/object_danger";
export * from "@/engine/core/utils/object/object_find";
export * from "@/engine/core/utils/object/object_general";
export * from "@/engine/core/utils/object/object_get";
export * from "@/engine/core/utils/object/object_info_portion";
export * from "@/engine/core/utils/object/object_location";
export * from "@/engine/core/utils/object/object_set";
export * from "@/engine/core/utils/object/object_sound";
export * from "@/engine/core/utils/object/object_location";
export * from "@/engine/core/utils/object/object_state";
export * from "@/engine/core/utils/object/object_info_portion";
export * from "@/engine/core/utils/object/object_spawn";
export * from "@/engine/core/utils/object/object_find";
export * from "@/engine/core/utils/object/object_state";
export * from "@/engine/core/utils/object/object_task_reward";
23 changes: 0 additions & 23 deletions src/engine/core/utils/object/object_action.ts

This file was deleted.

23 changes: 20 additions & 3 deletions src/engine/core/utils/scheme/scheme_monster.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { describe, expect, it } from "@jest/globals";
import { cond, move, vector } from "xray16";

import {
isMonsterScriptCaptured,
resetMonsterAction,
scriptCaptureMonster,
scriptCommandMonster,
scriptReleaseMonster,
} from "@/engine/core/utils/scheme/scheme_monster";
import { ClientObject } from "@/engine/lib/types";
import { replaceFunctionMock, resetFunctionMock } from "@/fixtures/utils";
import { mockClientGameObject } from "@/fixtures/xray";
import { ClientObject, Cond, Move } from "@/engine/lib/types";
import { getFunctionMock, replaceFunctionMock, resetFunctionMock } from "@/fixtures/utils";
import { mockClientGameObject, MockEntityAction } from "@/fixtures/xray";

describe("'monster' scheme utils", () => {
it("should correctly check if monster object is captured", () => {
Expand Down Expand Up @@ -83,4 +85,19 @@ describe("'monster' scheme utils", () => {
expect(object.get_script_name).toHaveBeenCalled();
expect(object.script).toHaveBeenCalledWith(false, "test_name");
});

it("'scriptCommandMonster' should correctly assign actions", () => {
const object: ClientObject = mockClientGameObject();
const moveAction: Move = new move(move.run_with_leader, new vector().set(1, 2, 3));
const condAction: Cond = new cond(cond.move_end);

scriptCommandMonster(object, moveAction, condAction);
expect(object.command).toHaveBeenCalledTimes(1);

const entityAction: MockEntityAction = getFunctionMock(object.command).mock.calls[0][0] as MockEntityAction;

expect(entityAction.set_action).toHaveBeenCalledTimes(2);
expect(entityAction.set_action).toHaveBeenNthCalledWith(1, moveAction);
expect(entityAction.set_action).toHaveBeenNthCalledWith(2, condAction);
});
});
40 changes: 29 additions & 11 deletions src/engine/core/utils/scheme/scheme_monster.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { entity_action } from "xray16";

import { logicsConfig } from "@/engine/lib/configs/LogicsConfig";
import { ClientObject, TName } from "@/engine/lib/types";
import { ClientObject, EntityAction, TEntityActionType, TIndex, TName } from "@/engine/lib/types";

/**
* Check whether monster is currently captured by script logic.
*
* @param object - target client monster object
* @returns whether monster currently has active script
*/
export function isMonsterScriptCaptured(object: ClientObject): boolean {
return object.get_script();
}

/**
* todo;
Expand All @@ -12,16 +24,6 @@ export function resetMonsterAction(object: ClientObject, scriptName: TName): voi
object.script(true, scriptName);
}

/**
* Check whether monster is currently captured by script logic.
*
* @param object - target client monster object
* @returns whether monster currently has active script
*/
export function isMonsterScriptCaptured(object: ClientObject): boolean {
return object.get_script();
}

/**
* Capture monster with script logic.
* Blocks default monster behaviour and fully rely on schemes logic.
Expand Down Expand Up @@ -53,3 +55,19 @@ export function scriptReleaseMonster(object: ClientObject): void {
object.script(false, object.get_script_name());
}
}

/**
* Command object to do some actions.
*
* @param object - target object to command
* @param actions - list of actions to perform
*/
export function scriptCommandMonster(object: ClientObject, ...actions: Array<TEntityActionType>): void {
const entityAction: EntityAction = new entity_action();

for (const type of actions) {
entityAction.set_action(type);
}

object.command(entityAction, false);
}
4 changes: 4 additions & 0 deletions src/engine/lib/types/xray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CConsole,
CGameTask,
color,
cond,
cover_point,
CPhrase,
CPhraseDialog,
Expand Down Expand Up @@ -66,6 +67,7 @@ import {
ini_file,
IXR_squad_member,
login_manager,
move,
net_packet,
noise,
object_factory,
Expand Down Expand Up @@ -118,6 +120,7 @@ export type AnyGameObject = game_object | cse_alife_object;
export type Car = CCar;
export type ClientObject = game_object;
export type Color = color;
export type Cond = cond;
export type Console = CConsole;
export type CoverPoint = cover_point;
export type DangerObject = danger_object;
Expand All @@ -134,6 +137,7 @@ export type Hit = hit;
export type IniFile = ini_file;
export type LoginManager = login_manager;
export type MonsterBodyStateKey = TXR_MonsterBodyStateKey;
export type Move = move;
export type NetPacket = net_packet;
export type NetProcessor = TXR_net_processor;
export type Noise = noise;
Expand Down
14 changes: 13 additions & 1 deletion src/fixtures/utils/function_mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, jest } from "@jest/globals";
import { Mock } from "jest-mock";

import { AnyCallable, AnyObject } from "@/engine/lib/types";
import { AnyCallable } from "@/engine/lib/types";

/**
* Reset hidden mock functions.
Expand All @@ -13,6 +14,17 @@ export function resetFunctionMock(callable: AnyCallable): void {
}
}

/**
* Reset hidden mock functions.
*/
export function getFunctionMock(callable: AnyCallable): Mock {
if (jest.isMockFunction(callable)) {
return callable;
} else {
throw new Error("Possibly not mocked function provided for mock reset.");
}
}

/**
* Replace mock function.
*/
Expand Down
Loading

0 comments on commit 4dd42d9

Please sign in to comment.