Skip to content

Commit

Permalink
Sharded object_check utils. Added new object_* modules. Added unit te…
Browse files Browse the repository at this point in the history
…sts for object utils.
  • Loading branch information
Neloreck committed Jul 8, 2023
1 parent e895266 commit 1e98460
Show file tree
Hide file tree
Showing 35 changed files with 692 additions and 256 deletions.
25 changes: 25 additions & 0 deletions src/engine/core/database/story_objects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
getObjectIdByStoryId,
getServerObjectByStoryId,
getStoryIdByObjectId,
isStoryObject,
isStoryObjectExisting,
registerObjectStoryLinks,
registerStoryLink,
unregisterStoryLinkByObjectId,
Expand Down Expand Up @@ -68,4 +70,27 @@ describe("'story_objects' module of the database", () => {
unregisterObject(clientObject);
unregisterStoryLinkByStoryId("test-sid");
});

it("'isStoryObjectExisting' should correctly check if object is existing", () => {
expect(isStoryObjectExisting("test-sid")).toBe(false);

const serverObject: ServerObject = mockServerAlifeObject();

registerStoryLink(serverObject.id, "test-sid");

expect(isStoryObjectExisting("test-sid")).toBe(true);
});

it("'isStoryObject' should correctly check if object is existing", () => {
const serverObject: ServerObject = mockServerAlifeObject();
const clientObject: ClientObject = mockClientGameObject({ id: () => serverObject.id });

expect(isStoryObject(serverObject)).toBe(false);
expect(isStoryObject(clientObject)).toBe(false);

registerStoryLink(serverObject.id, "is-story-object-example");

expect(isStoryObject(serverObject)).toBe(true);
expect(isStoryObject(clientObject)).toBe(true);
});
});
37 changes: 36 additions & 1 deletion src/engine/core/database/story_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ import { SYSTEM_INI } from "@/engine/core/database/ini_registry";
import { registry } from "@/engine/core/database/registry";
import { abort, assert } from "@/engine/core/utils/assertion";
import { readIniString } from "@/engine/core/utils/ini/ini_read";
import { ClientObject, IniFile, Optional, ServerObject, TName, TNumberId, TStringId } from "@/engine/lib/types";
import {
AnyGameObject,
ClientObject,
IniFile,
Optional,
ServerObject,
TName,
TNumberId,
TStringId,
} from "@/engine/lib/types";

/**
* Register story object link based on ini configuration.
Expand Down Expand Up @@ -86,6 +95,32 @@ export function unregisterStoryLinkByStoryId(storyId: TStringId): void {
}
}

/**
* Check whether object has story link.
*
* @param object - object to check
* @returns whether provided object has linked story id.
*/
export function isStoryObject(object: AnyGameObject): boolean {
if (type(object.id) === "function") {
return registry.storyLink.sidById.has((object as ClientObject).id());
} else {
return registry.storyLink.sidById.has((object as ServerObject).id);
}
}

/**
* Check whether story object exists.
*
* @param storyId - story ID to check existing
* @returns whether story object exists
*/
export function isStoryObjectExisting(storyId: TStringId): boolean {
const objectId: Optional<TNumberId> = registry.storyLink.idBySid.get(storyId);

return objectId === null ? false : alife().object(objectId) !== null;
}

/**
* Get object story ID by provided game object ID.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { alife, game } from "xray16";
import { alife, clsid, game } from "xray16";

import { getObjectIdByStoryId, registry } from "@/engine/core/database";
import { AbstractCoreManager } from "@/engine/core/managers/base/AbstractCoreManager";
Expand Down Expand Up @@ -29,7 +29,7 @@ import { GlobalSoundManager } from "@/engine/core/managers/sounds/GlobalSoundMan
import { Stalker } from "@/engine/core/objects";
import { abort, assert } from "@/engine/core/utils/assertion";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isObjectWounded, isStalkerClassId } from "@/engine/core/utils/object";
import { isObjectWounded } from "@/engine/core/utils/object";
import { getInventoryNameForItemSection } from "@/engine/core/utils/object/object_spawn";
import { captions, TCaption } from "@/engine/lib/constants/captions/captions";
import { scriptSounds } from "@/engine/lib/constants/sound/script_sounds";
Expand Down Expand Up @@ -338,7 +338,7 @@ export class NotificationManager extends AbstractCoreManager {

let textureName: TTexture = textures.ui_iconsTotal_grouping;

if (object !== null && isStalkerClassId(object.clsid())) {
if (object !== null && (object.clsid() === clsid.script_stalker || object.clsid() === clsid.stalker)) {
textureName = object.character_icon();
} else if (notificationManagerIcons[faction as TNotificationIconKey]) {
textureName = notificationManagerIcons[faction as TNotificationIconKey];
Expand Down
10 changes: 5 additions & 5 deletions src/engine/core/managers/world/DropManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { parseNumbersList, parseStringsList } from "@/engine/core/utils/ini";
import { LuaLogger } from "@/engine/core/utils/logging";
import {
getObjectCommunity,
isAmmoItem,
isAmmoSection,
isArtefact,
isExcludedFromLootDropItem,
isExcludedFromLootDropItemSection,
isGrenade,
isLootableItem,
isLootableItemSection,
isWeapon,
setItemCondition,
spawnItemsForObject,
Expand Down Expand Up @@ -233,7 +233,7 @@ export class DropManager extends AbstractCoreManager {
return;
}

if (isExcludedFromLootDropItem(item)) {
if (isExcludedFromLootDropItemSection(section)) {
alife().release(alife().object(item.id()), true);

return;
Expand Down Expand Up @@ -269,7 +269,7 @@ export class DropManager extends AbstractCoreManager {
return;
}

if (isLootableItem(item) && !isAmmoItem(item)) {
if (isLootableItemSection(item.section()) && !isAmmoSection(item.section())) {
logger.info("Keep item, misc lootable:", object.name(), item.name(), section);

return;
Expand Down
13 changes: 4 additions & 9 deletions src/engine/core/managers/world/SurgeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { alife, game, hit, level } from "xray16";
import {
closeLoadMarker,
closeSaveMarker,
isStoryObject,
openLoadMarker,
openSaveMarker,
registry,
Expand All @@ -25,13 +26,7 @@ import { createAutoSave } from "@/engine/core/utils/game/game_save";
import { readTimeFromPacket, writeTimeToPacket } from "@/engine/core/utils/game/game_time";
import { parseConditionsList, pickSectionFromCondList, TConditionList } from "@/engine/core/utils/ini";
import { LuaLogger } from "@/engine/core/utils/logging";
import {
isArtefact,
isImmuneToSurge,
isObjectOnLevel,
isStoryObject,
isSurgeEnabledOnLevel,
} from "@/engine/core/utils/object";
import { isArtefact, isImmuneToSurgeObject, isObjectOnLevel, isSurgeEnabledOnLevel } from "@/engine/core/utils/object";
import { disableInfo, giveInfo, hasAlifeInfo } from "@/engine/core/utils/object/object_info_portion";
import { createVector } from "@/engine/core/utils/vector";
import { surgeConfig } from "@/engine/lib/configs/SurgeConfig";
Expand Down Expand Up @@ -490,7 +485,7 @@ export class SurgeManager extends AbstractCoreManager {
logger.info("Releasing squads:", simulationBoardManager.getSquads().length());

for (const [squadId, squad] of simulationBoardManager.getSquads()) {
if (isObjectOnLevel(squad, levelName) && !isImmuneToSurge(squad) && !isStoryObject(squad)) {
if (isObjectOnLevel(squad, levelName) && !isImmuneToSurgeObject(squad) && !isStoryObject(squad)) {
for (const member of squad.squad_members()) {
if (!isStoryObject(member.object)) {
if (this.canReleaseSquad(squad)) {
Expand Down Expand Up @@ -571,7 +566,7 @@ export class SurgeManager extends AbstractCoreManager {
const activeCovers: LuaArray<ClientObject> = this.getCoverObjects();

for (const [squadId, squad] of simulationBoardManager.getSquads()) {
if (isObjectOnLevel(squad, levelName) && !isImmuneToSurge(squad)) {
if (isObjectOnLevel(squad, levelName) && !isImmuneToSurgeObject(squad)) {
for (const member of squad.squad_members()) {
let isInCover: boolean = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LuabindClass, property_evaluator } from "xray16";

import { ISchemeAnimpointState } from "@/engine/core/schemes/animpoint/ISchemeAnimpointState";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isActiveSection } from "@/engine/core/utils/object";
import { isActiveSection } from "@/engine/core/utils/scheme";

const logger: LuaLogger = new LuaLogger($filename);

Expand Down
4 changes: 2 additions & 2 deletions src/engine/core/schemes/base/evaluators/EvaluatorEnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LuabindClass, property_evaluator } from "xray16";

import { IBaseSchemeState } from "@/engine/core/schemes/base";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isActiveSection } from "@/engine/core/utils/object";
import { isActiveSection } from "@/engine/core/utils/scheme";

const logger: LuaLogger = new LuaLogger($filename);

Expand All @@ -19,7 +19,7 @@ export class EvaluatorEnd extends property_evaluator {
}

/**
* Check whether evaluator scheme state is still active or ended.
* Check whether scheme is still active or ended.
*/
public override evaluate(): boolean {
return !isActiveSection(this.object, this.state.section);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { LuabindClass, property_evaluator, stalker_ids, time_global } from "xray
import { EEvaluatorId } from "@/engine/core/schemes/base";
import { ISchemeCamperState } from "@/engine/core/schemes/camper/ISchemeCamperState";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isActiveSection } from "@/engine/core/utils/object";
import { isActiveSection } from "@/engine/core/utils/scheme";
import { ActionPlanner, Optional } from "@/engine/lib/types";

const logger: LuaLogger = new LuaLogger($filename);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LuabindClass, property_evaluator } from "xray16";

import { ISchemeCompanionState } from "@/engine/core/schemes/companion";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isActiveSection } from "@/engine/core/utils/object";
import { isActiveSection } from "@/engine/core/utils/scheme";

const logger: LuaLogger = new LuaLogger($filename);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { EvaluatorCorpseDetect } from "@/engine/core/schemes/corpse_detection/ev
import { ISchemeCorpseDetectionState } from "@/engine/core/schemes/corpse_detection/ISchemeCorpseDetectionState";
import { readIniBoolean } from "@/engine/core/utils/ini";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isLootableItem } from "@/engine/core/utils/object";
import { isLootableItemSection } from "@/engine/core/utils/object";
import { ActionPlanner, ClientObject, IniFile, Optional, TNumberId } from "@/engine/lib/types";
import { EScheme, ESchemeType, TSection } from "@/engine/lib/types/scheme";

Expand Down Expand Up @@ -116,7 +116,7 @@ export class SchemeCorpseDetection extends AbstractScheme {
}

corpseObject.iterate_inventory((object, item) => {
if (isLootableItem(item)) {
if (isLootableItemSection(item.section())) {
object.transfer_item(item, object);
}
}, corpseObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { level, LuabindClass, property_evaluator } from "xray16";
import { IRegistryObjectState, registry } from "@/engine/core/database";
import { IReleaseDescriptor, ReleaseBodyManager } from "@/engine/core/managers/world/ReleaseBodyManager";
import { ISchemeCorpseDetectionState } from "@/engine/core/schemes/corpse_detection";
import { isLootableItem, isObjectWounded } from "@/engine/core/utils/object";
import { isLootableItemSection, isObjectWounded } from "@/engine/core/utils/object";
import { communities } from "@/engine/lib/constants/communities";
import { ClientObject, Optional, TDistance, TNumberId, Vector } from "@/engine/lib/types";

Expand Down Expand Up @@ -46,7 +46,7 @@ export class EvaluatorCorpseDetect extends property_evaluator {

let hasValuableLoot: boolean = false;
const checkLoot = (npc: ClientObject, item: ClientObject) => {
if (isLootableItem(item)) {
if (isLootableItemSection(item.section())) {
hasValuableLoot = true;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LuabindClass, property_evaluator } from "xray16";

import { ISchemeCoverState } from "@/engine/core/schemes/cover";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isSectionActive } from "@/engine/core/utils/scheme/scheme_logic";
import { isActiveSectionState } from "@/engine/core/utils/scheme/scheme_logic";

const logger: LuaLogger = new LuaLogger($filename);

Expand All @@ -22,6 +22,6 @@ export class EvaluatorNeedCover extends property_evaluator {
* Check whether cover scheme is active.
*/
public override evaluate(): boolean {
return isSectionActive(this.object, this.state);
return isActiveSectionState(this.object, this.state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LuabindClass, property_evaluator } from "xray16";

import { ISchemePatrolState } from "@/engine/core/schemes/patrol";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isActiveSection } from "@/engine/core/utils/object";
import { isActiveSection } from "@/engine/core/utils/scheme";

const logger: LuaLogger = new LuaLogger($filename);

Expand Down
2 changes: 1 addition & 1 deletion src/engine/core/schemes/ph_button/PhysicalButtonManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AbstractSchemeManager } from "@/engine/core/schemes";
import { ISchemePhysicalButtonState } from "@/engine/core/schemes/ph_button/ISchemePhysicalButtonState";
import { pickSectionFromCondList } from "@/engine/core/utils/ini";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isActiveSection } from "@/engine/core/utils/object";
import { isActiveSection } from "@/engine/core/utils/scheme";
import { switchObjectSchemeToSection, trySwitchToAnotherSection } from "@/engine/core/utils/scheme/scheme_switch";
import { ClientObject, Optional, TIndex, TRate, TTimestamp, Vector } from "@/engine/lib/types";

Expand Down
3 changes: 2 additions & 1 deletion src/engine/core/schemes/ph_minigun/MinigunManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { AbstractSchemeManager } from "@/engine/core/schemes/base";
import { ISchemeMinigunState } from "@/engine/core/schemes/ph_minigun/ISchemeMinigunState";
import { abort } from "@/engine/core/utils/assertion";
import { pickSectionFromCondList, TConditionList } from "@/engine/core/utils/ini";
import { isActiveSection, isObjectWounded } from "@/engine/core/utils/object";
import { isObjectWounded } from "@/engine/core/utils/object";
import {
isActiveSection,
isMonsterScriptCaptured,
scriptReleaseMonster,
switchObjectSchemeToSection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LuabindClass, property_evaluator } from "xray16";

import { ISchemeRemarkState } from "@/engine/core/schemes/remark";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isActiveSection } from "@/engine/core/utils/object";
import { isActiveSection } from "@/engine/core/utils/scheme";

const logger: LuaLogger = new LuaLogger($filename);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LuabindClass, property_evaluator } from "xray16";

import { ISchemeSleeperState } from "@/engine/core/schemes/sleeper";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isSectionActive } from "@/engine/core/utils/scheme/scheme_logic";
import { isActiveSectionState } from "@/engine/core/utils/scheme/scheme_logic";

const logger: LuaLogger = new LuaLogger($filename);

Expand All @@ -25,6 +25,6 @@ export class EvaluatorNeedSleep extends property_evaluator {
* todo: Description.
*/
public override evaluate(): boolean {
return isSectionActive(this.object, this.state);
return isActiveSectionState(this.object, this.state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LuabindClass, property_evaluator } from "xray16";

import { ISchemeSmartCoverState } from "@/engine/core/schemes/smartcover";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isActiveSection } from "@/engine/core/utils/object";
import { isActiveSection } from "@/engine/core/utils/scheme";

const logger: LuaLogger = new LuaLogger($filename);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LuabindClass, property_evaluator } from "xray16";

import { ISchemeSmartCoverState } from "@/engine/core/schemes/smartcover";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isActiveSection } from "@/engine/core/utils/object";
import { isActiveSection } from "@/engine/core/utils/scheme";

const logger: LuaLogger = new LuaLogger($filename);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LuabindClass, property_evaluator } from "xray16";

import { ISchemeWalkerState } from "@/engine/core/schemes/walker";
import { LuaLogger } from "@/engine/core/utils/logging";
import { isSectionActive } from "@/engine/core/utils/scheme/scheme_logic";
import { isActiveSectionState } from "@/engine/core/utils/scheme/scheme_logic";

const logger: LuaLogger = new LuaLogger($filename);

Expand All @@ -22,6 +22,6 @@ export class EvaluatorNeedWalker extends property_evaluator {
* Check whether walker scheme is active and should still continue processing.
*/
public override evaluate(): boolean {
return isSectionActive(this.object, this.state);
return isActiveSectionState(this.object, this.state);
}
}
2 changes: 2 additions & 0 deletions src/engine/core/utils/object/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
export * from "@/engine/core/utils/object/object_alife";
export * from "@/engine/core/utils/object/object_anomaly";
export * from "@/engine/core/utils/object/object_check";
export * from "@/engine/core/utils/object/object_class";
export * from "@/engine/core/utils/object/object_danger";
export * from "@/engine/core/utils/object/object_find";
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_section";
export * from "@/engine/core/utils/object/object_set";
export * from "@/engine/core/utils/object/object_sound";
export * from "@/engine/core/utils/object/object_spawn";
Expand Down
Loading

0 comments on commit 1e98460

Please sign in to comment.