Skip to content

Commit

Permalink
Moving object/checkers utils to separate modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neloreck committed Jun 25, 2023
1 parent e4003bb commit 58acb0d
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 118 deletions.
8 changes: 2 additions & 6 deletions src/engine/core/managers/world/SurgeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ import { GlobalSoundManager } from "@/engine/core/managers/sounds/GlobalSoundMan
import { WeatherManager } from "@/engine/core/managers/world/WeatherManager";
import { AnomalyZoneBinder, SmartTerrain } from "@/engine/core/objects";
import { Squad } from "@/engine/core/objects/server/squad/Squad";
import {
isBlackScreen,
isImmuneToSurge,
isObjectOnLevel,
isSurgeEnabledOnLevel,
} from "@/engine/core/utils/check/check";
import { isBlackScreen, isImmuneToSurge, isSurgeEnabledOnLevel } from "@/engine/core/utils/check/check";
import { isArtefact, isStoryObject } from "@/engine/core/utils/check/is";
import { executeConsoleCommand, getConsoleFloatCommand } from "@/engine/core/utils/console";
import { createAutoSave } from "@/engine/core/utils/game_save";
Expand All @@ -27,6 +22,7 @@ import { pickSectionFromCondList } from "@/engine/core/utils/ini/config";
import { parseConditionsList } from "@/engine/core/utils/ini/parse";
import { TConditionList } from "@/engine/core/utils/ini/types";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isObjectOnLevel } from "@/engine/core/utils/object/object_location";
import { readTimeFromPacket, writeTimeToPacket } from "@/engine/core/utils/time";
import { createVector } from "@/engine/core/utils/vector";
import { surgeConfig } from "@/engine/lib/configs/SurgeConfig";
Expand Down
82 changes: 1 addition & 81 deletions src/engine/core/utils/check/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ISchemeWoundedState } from "@/engine/core/schemes/wounded";
import { isStalker } from "@/engine/core/utils/check/is";
import { pickSectionFromCondList } from "@/engine/core/utils/ini/config";
import { getCharacterCommunity } from "@/engine/core/utils/object/object_general";
import { isObjectInZone } from "@/engine/core/utils/object/object_location";
import { logicsConfig } from "@/engine/lib/configs/LogicsConfig";
import { surgeConfig } from "@/engine/lib/configs/SurgeConfig";
import { communities } from "@/engine/lib/constants/communities";
Expand All @@ -30,10 +31,8 @@ import {
Optional,
ServerCreatureObject,
ServerHumanObject,
ServerObject,
TDangerType,
TDistance,
TName,
TNumberId,
TStringId,
} from "@/engine/lib/types";
Expand Down Expand Up @@ -74,13 +73,6 @@ export function isStalkerAlive(targetObject: ClientObject | ServerHumanObject |
}
}

/**
* @returns whether provided object is on a provided level.
*/
export function isObjectOnLevel(object: Optional<ServerObject>, levelName: TName): boolean {
return object !== null && alife().level_name(game_graph().vertex(object.m_game_vertex_id).level_id()) === levelName;
}

/**
* @returns whether provided community squad is immune to surge.
*/
Expand Down Expand Up @@ -113,13 +105,6 @@ export function isLevelChanging(): boolean {
: game_graph().vertex(simulator.actor().m_game_vertex_id).level_id() !== simulator.level_id();
}

/**
* @returns whether object is inside another object.
*/
export function isObjectInZone(object: Optional<ClientObject>, zone: Optional<ClientObject>): boolean {
return object !== null && zone !== null && zone.inside(object.position());
}

/**
* @returns whether object is wounded.
*/
Expand Down Expand Up @@ -313,35 +298,6 @@ export function isObjectEnemy(object: ClientObject, enemy: ClientObject, state:
return true;
}

/**
* todo;
* todo;
* todo;
*/
export function isActorInZone(zone: Optional<ClientObject>): boolean {
const actor: Optional<ClientObject> = registry.actor;

return actor !== null && zone !== null && zone.inside(actor.position());
}

/**
* todo;
* todo;
* todo;
*/
export function isActorInZoneWithName(zoneName: TName, actor: Optional<ClientObject> = registry.actor): boolean {
const zone: Optional<ClientObject> = registry.zones.get(zoneName);

return actor !== null && zone !== null && zone.inside(actor.position());
}

/**
* @returns whether provided enemy object is actor.
*/
export function isActorEnemy(object: ClientObject): boolean {
return object.id() === registry.actor.id();
}

/**
* @returns whether actor is alive.
*/
Expand All @@ -356,42 +312,6 @@ export function isSeenByActor(object: ClientObject): boolean {
return registry.actor.see(object);
}

/**
* @returns whether distance between objects greater or equal.
*/
export function isDistanceBetweenObjectsGreaterOrEqual(
first: ClientObject,
second: ClientObject,
distance: TDistance
): boolean {
return first.position().distance_to_sqr(second.position()) >= distance * distance;
}

/**
* @returns whether distance between objects less or equal.
*/
export function isDistanceBetweenObjectsLessOrEqual(
first: ClientObject,
second: ClientObject,
distance: TDistance
): boolean {
return first.position().distance_to_sqr(second.position()) <= distance * distance;
}

/**
* @returns whether distance to actor greater or equal.
*/
export function isDistanceToActorGreaterOrEqual(object: ClientObject, distance: TDistance): boolean {
return object.position().distance_to_sqr(registry.actor.position()) >= distance * distance;
}

/**
* @returns whether distance to actor less or equal.
*/
export function isDistanceToActorLessOrEqual(object: ClientObject, distance: TDistance): boolean {
return object.position().distance_to_sqr(registry.actor.position()) <= distance * distance;
}

/**
* @returns whether currently black screen is visible and rendering is paused.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/engine/core/utils/ini/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export function getConfigObjectAndZone(ini: IniFile, section: TSection, field: T
if (target) {
const simulator: Optional<AlifeSimulator> = alife();

if (simulator) {
if (simulator !== null) {
const serverObject: Optional<ServerObject> = simulator.object(getObjectIdByStoryId(target.v1 as string)!);

if (serverObject) {
Expand Down
1 change: 1 addition & 0 deletions src/engine/core/utils/object/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "@/engine/core/utils/object/object_general";
export * from "@/engine/core/utils/object/object_location";
export * from "@/engine/core/utils/object/object_find";
29 changes: 29 additions & 0 deletions src/engine/core/utils/object/object_location.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, expect, it } from "@jest/globals";
import { alife, game_graph } from "xray16";

import { isObjectInZone, isObjectOnLevel } from "@/engine/core/utils/object/object_location";
import { ClientObject, ServerObject } from "@/engine/lib/types";
import { mockClientGameObject, mockServerAlifeObject } from "@/fixtures/xray";

describe("object location utils", () => {
it("'isObjectInZone' check object inside", () => {
const object: ClientObject = mockClientGameObject();
const zone: ClientObject = mockClientGameObject();

expect(isObjectInZone(object, zone)).toBe(false);
expect(zone.inside).toHaveBeenCalledWith(object.position());
expect(isObjectInZone(null, null)).toBe(false);
expect(isObjectInZone(object, null)).toBe(false);
expect(isObjectInZone(null, zone)).toBe(false);
});

it("'isObjectOnLevel' check object inside", () => {
const object: ServerObject = mockServerAlifeObject();

expect(isObjectOnLevel(null, "zaton")).toBe(false);
expect(isObjectOnLevel(object, "pripyat")).toBe(true);

expect(game_graph().vertex(object.m_game_vertex_id).level_id()).toBe(1);
expect(alife().level_name).toHaveBeenCalledWith(1);
});
});
62 changes: 62 additions & 0 deletions src/engine/core/utils/object/object_location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { alife, game_graph } from "xray16";

import { registry } from "@/engine/core/database";
import { ClientObject, Optional, ServerObject, TDistance, TName } from "@/engine/lib/types";

/**
* Check whether object is inside another zone object.
*
* @param object - target client object to check
* @param zone - target zone to check
* @returns whether object is inside zone object.
*/
export function isObjectInZone(object: Optional<ClientObject>, zone: Optional<ClientObject>): boolean {
return object !== null && zone !== null && zone.inside(object.position());
}

/**
* Check whether object is on matching level.
*
* @param object - target object to check
* @param levelName - target level name
* @returns whether provided object is on a level
*/
export function isObjectOnLevel(object: Optional<ServerObject>, levelName: TName): boolean {
return object !== null && alife().level_name(game_graph().vertex(object.m_game_vertex_id).level_id()) === levelName;
}

/**
* @returns whether distance between objects greater or equal.
*/
export function isDistanceBetweenObjectsGreaterOrEqual(
first: ClientObject,
second: ClientObject,
distance: TDistance
): boolean {
return first.position().distance_to_sqr(second.position()) >= distance * distance;
}

/**
* @returns whether distance between objects less or equal.
*/
export function isDistanceBetweenObjectsLessOrEqual(
first: ClientObject,
second: ClientObject,
distance: TDistance
): boolean {
return first.position().distance_to_sqr(second.position()) <= distance * distance;
}

/**
* @returns whether distance to actor greater or equal.
*/
export function isDistanceToActorGreaterOrEqual(object: ClientObject, distance: TDistance): boolean {
return object.position().distance_to_sqr(registry.actor.position()) >= distance * distance;
}

/**
* @returns whether distance to actor less or equal.
*/
export function isDistanceToActorLessOrEqual(object: ClientObject, distance: TDistance): boolean {
return object.position().distance_to_sqr(registry.actor.position()) <= distance * distance;
}
2 changes: 1 addition & 1 deletion src/engine/core/utils/scheme/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { game, level, time_global } from "xray16";
import { IRegistryObjectState, registry } from "@/engine/core/database";
import { ESchemeEvent, IBaseSchemeLogic, IBaseSchemeState } from "@/engine/core/schemes";
import { abort, assert } from "@/engine/core/utils/assertion";
import { isObjectInZone } from "@/engine/core/utils/check/check";
import { pickSectionFromCondList } from "@/engine/core/utils/ini/config";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isActorSeenByObject } from "@/engine/core/utils/object/object_general";
import { isObjectInZone } from "@/engine/core/utils/object/object_location";
import { emitSchemeEvent } from "@/engine/core/utils/scheme/event";
import { activateSchemeBySection } from "@/engine/core/utils/scheme/logic";
import { getDistanceBetween } from "@/engine/core/utils/vector";
Expand Down
7 changes: 5 additions & 2 deletions src/engine/scripts/declarations/conditions/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { ISchemeDeathState } from "@/engine/core/schemes/death";
import { ISchemeHitState } from "@/engine/core/schemes/hit";
import { abort, assertDefined } from "@/engine/core/utils/assertion";
import { extern } from "@/engine/core/utils/binding";
import { isActorAlive, isActorEnemy, isObjectInZone } from "@/engine/core/utils/check/check";
import { isActorAlive } from "@/engine/core/utils/check/check";
import { isWeapon } from "@/engine/core/utils/check/is";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isObjectInZone } from "@/engine/core/utils/object/object_location";
import { npcInActorFrustum } from "@/engine/core/utils/vector";
import { ACTOR_ID } from "@/engine/lib/constants/ids";
import { AnyArgs, ClientObject, EScheme, LuaArray, Optional, TCount, TDistance, TSection } from "@/engine/lib/types";

const logger: LuaLogger = new LuaLogger($filename);
Expand Down Expand Up @@ -40,7 +42,8 @@ extern("xr_conditions.actor_in_surge_cover", (actor: ClientObject, npc: ClientOb
* todo;
*/
extern("xr_conditions.is_enemy_actor", (object: ClientObject): boolean => {
return isActorEnemy(object);
// todo: Probably always true.
return object.id() === ACTOR_ID;
});

/**
Expand Down
8 changes: 5 additions & 3 deletions src/engine/scripts/declarations/conditions/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import { SchemeDeimos } from "@/engine/core/schemes/sr_deimos";
import { abort } from "@/engine/core/utils/assertion";
import { extern } from "@/engine/core/utils/binding";
import {
isDistanceBetweenObjectsGreaterOrEqual,
isDistanceBetweenObjectsLessOrEqual,
isHeavilyWounded,
isObjectInZone,
isObjectWounded,
isPlayingSound,
isStoryObjectExisting,
Expand All @@ -31,6 +28,11 @@ import { isMonster, isStalker } from "@/engine/core/utils/check/is";
import { hasAlifeInfo } from "@/engine/core/utils/info_portion";
import { LuaLogger } from "@/engine/core/utils/logging";
import { getObjectSmartTerrain, getObjectSquad } from "@/engine/core/utils/object/object_general";
import {
isDistanceBetweenObjectsGreaterOrEqual,
isDistanceBetweenObjectsLessOrEqual,
isObjectInZone,
} from "@/engine/core/utils/object/object_location";
import { captions, TCaption } from "@/engine/lib/constants/captions";
import { infoPortions } from "@/engine/lib/constants/info_portions";
import { FALSE } from "@/engine/lib/constants/words";
Expand Down
2 changes: 1 addition & 1 deletion src/engine/scripts/declarations/conditions/quests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { getObjectByStoryId, getServerObjectByStoryId, registry } from "@/engine
import { AnomalyZoneBinder, Squad } from "@/engine/core/objects";
import { abort } from "@/engine/core/utils/assertion";
import { extern, getExtern } from "@/engine/core/utils/binding";
import { isObjectInZone } from "@/engine/core/utils/check/check";
import { hasAlifeInfo } from "@/engine/core/utils/info_portion";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isObjectInZone } from "@/engine/core/utils/object/object_location";
import { distanceBetween } from "@/engine/core/utils/vector";
import { infoPortions, TInfoPortion } from "@/engine/lib/constants/info_portions";
import { zones } from "@/engine/lib/constants/zones";
Expand Down
6 changes: 3 additions & 3 deletions src/engine/scripts/declarations/effects/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { TreasureManager } from "@/engine/core/managers/world/TreasureManager";
import { Squad } from "@/engine/core/objects";
import { abort, assert, assertDefined } from "@/engine/core/utils/assertion";
import { extern } from "@/engine/core/utils/binding";
import { isActorInZoneWithName } from "@/engine/core/utils/check/check";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isObjectInZone } from "@/engine/core/utils/object";
import { giveItemsToActor } from "@/engine/core/utils/task_reward";
import { animations } from "@/engine/lib/constants/animation/animations";
import { detectors, TDetector } from "@/engine/lib/constants/items/detectors";
Expand Down Expand Up @@ -416,8 +416,8 @@ extern("xr_effects.sleep", (actor: ClientObject): void => {
zones.actor_surge_hide_2,
]);

for (const [index, zone] of sleepZones) {
if (isActorInZoneWithName(zone, actor)) {
for (const [, zone] of sleepZones) {
if (isObjectInZone(actor, registry.zones.get(zone))) {
SleepManager.getInstance().showSleepDialog();
break;
}
Expand Down
7 changes: 4 additions & 3 deletions src/engine/scripts/declarations/effects/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { getObjectIdByStoryId, getServerObjectByStoryId, registry, resetStalkerS
import { Squad } from "@/engine/core/objects";
import { abort, assertDefined } from "@/engine/core/utils/assertion";
import { extern } from "@/engine/core/utils/binding";
import { isActorInZoneWithName } from "@/engine/core/utils/check/check";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isObjectInZone } from "@/engine/core/utils/object";
import {
ClientObject,
Optional,
Expand All @@ -16,6 +16,7 @@ import {
TName,
TNumberId,
TProbability,
TRate,
TStringId,
Vector,
} from "@/engine/lib/types";
Expand Down Expand Up @@ -101,13 +102,13 @@ extern("xr_effects.teleport_actor", (actor: ClientObject, object: ClientObject,

if (params[1] !== null) {
const look: Patrol = new patrol(params[1]);
const dir: number = -look.point(0).sub(point.point(0)).getH();
const dir: TRate = -look.point(0).sub(point.point(0)).getH();

actor.set_actor_direction(dir);
}

for (const [k, v] of registry.noWeaponZones) {
if (isActorInZoneWithName(k, actor)) {
if (isObjectInZone(actor, registry.zones.get(k))) {
registry.noWeaponZones.set(k, true);
}
}
Expand Down
Loading

0 comments on commit 58acb0d

Please sign in to comment.