Skip to content

Commit

Permalink
Adding tests for section switchers. Memo for condlist parsing. Moved …
Browse files Browse the repository at this point in the history
…schemeWounded parse util to separate place.
  • Loading branch information
Neloreck committed Jun 25, 2023
1 parent ada8f1b commit 1085f5a
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 153 deletions.
3 changes: 2 additions & 1 deletion src/engine/core/database/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { registry } from "@/engine/core/database/registry";

describe("registry storage", () => {
it("storage to contain all fields", () => {
expect(Object.keys(registry)).toHaveLength(36);
expect(Object.keys(registry)).toHaveLength(37);
});

it("storage to initialize with correct data", () => {
Expand All @@ -13,6 +13,7 @@ describe("registry storage", () => {
expect(registry.activeSmartTerrainId).toBeNull();
expect(registry.managers instanceof LuaTable).toBeTruthy();
expect(registry.schemes instanceof LuaTable).toBeTruthy();
expect(registry.conditionLists instanceof LuaTable).toBeTruthy();
expect(registry.actorCombat instanceof LuaTable).toBeTruthy();
expect(registry.objects instanceof LuaTable).toBeTruthy();
expect(registry.offlineObjects instanceof LuaTable).toBeTruthy();
Expand Down
5 changes: 5 additions & 0 deletions src/engine/core/database/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { CampStoryManager } from "@/engine/core/schemes/camper";
import type { PatrolManager } from "@/engine/core/schemes/patrol";
import type { ReachTaskPatrolManager } from "@/engine/core/schemes/reach_task";
import type { LightManager } from "@/engine/core/schemes/sr_light";
import type { TConditionList } from "@/engine/core/utils/ini/types";
import type { ERelation } from "@/engine/core/utils/relation";
import { storyNames, TStoryName } from "@/engine/lib/constants/story_names";
import type {
Expand Down Expand Up @@ -59,6 +60,10 @@ export const registry = {
* List of activated schemes in game.
*/
schemes: new LuaTable<EScheme, TAbstractSchemeConstructor>(),
/**
* Memoized condlist for parsing simplification, where key is string data and value is parsed descriptor.
*/
conditionLists: new LuaTable<string, TConditionList>(),
/**
* Set of objects in combat with actor.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/engine/core/managers/world/DropManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class DropManager extends AbstractCoreManager {
const [, key, value] = DEATH_GENERIC_LTX.r_line(itemsDropSectionByDifficulty, it, "", "");
const sectionDropCount: LuaArray<TProbability> = parseNumbersList(value);

if (sectionDropCount.has(1)) {
if (!sectionDropCount.has(1)) {
abort("Error on [death_ini] declaration. Section [%s], line [%s]", itemsDropSectionByDifficulty, tostring(key));
}

Expand Down
5 changes: 2 additions & 3 deletions src/engine/core/schemes/base/ObjectRestrictionsManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { IRegistryObjectState, registry } from "@/engine/core/database";
import { getParametersString } from "@/engine/core/utils/ini/config";
import { parseStringsList } from "@/engine/core/utils/ini/parse";
import { readIniString } from "@/engine/core/utils/ini/read";
import { LuaLogger } from "@/engine/core/utils/logging";
Expand Down Expand Up @@ -64,7 +63,7 @@ export class ObjectRestrictionsManager {
logger.info("Activate restrictions:", objectName, section);

// Update OUT restrictors based on active / ini restrictors.
const [outRestrictorString] = getParametersString(readIniString(ini, section, "out_restr", false, "", ""));
const outRestrictorString: string = readIniString(ini, section, "out_restr", false, "", "");
const newOutRestrictors: LuaArray<TName> = parseStringsList(outRestrictorString);
const oldOutRestrictors: LuaArray<TName> = parseStringsList(this.object.out_restrictions());

Expand Down Expand Up @@ -115,7 +114,7 @@ export class ObjectRestrictionsManager {
}

// Update IN restrictors based on active / ini restrictors.
const [inRestrictorString] = getParametersString(readIniString(ini, section, "in_restr", false, "", ""));
const inRestrictorString: string = readIniString(ini, section, "in_restr", false, "", "");
const newInRestrictor: LuaArray<TName> = parseStringsList(inRestrictorString);
const oldInRestrictor: LuaArray<TName> = parseStringsList(this.object.in_restrictions());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import {
ISchemeSmartCoverState,
} from "@/engine/core/schemes/smartcover/ISchemeSmartCoverState";
import { abort } from "@/engine/core/utils/assertion";
import { getParametersString, pickSectionFromCondList } from "@/engine/core/utils/ini/config";
import { parseConditionsList } from "@/engine/core/utils/ini/parse";
import { TConditionList } from "@/engine/core/utils/ini/types";
import { parseConditionsList, pickSectionFromCondList, TConditionList } from "@/engine/core/utils/ini";
import { LuaLogger } from "@/engine/core/utils/logging";
import { NIL } from "@/engine/lib/constants/words";
import { ClientObject, Optional, StringOptional, TName, TNumberId, Vector } from "@/engine/lib/types";
Expand Down Expand Up @@ -42,9 +40,6 @@ export class ActionSmartCoverActivity extends action_base {
this.state = state;
}

/**
* todo: Description.
*/
public override initialize(): void {
super.initialize();

Expand Down Expand Up @@ -92,30 +87,26 @@ export class ActionSmartCoverActivity extends action_base {
// --object.set_smart_cover_target_selector()
this.targetEnemyId = null;

const [coverName, used] = getParametersString(this.state.cover_name as string);
this.coverName = this.state.cover_name as string;

this.coverName = coverName;

if (this.coverName !== this.state.cover_name || used === false) {
if (registry.smartCovers.get(this.coverName) === null) {
abort("There is no smart_cover with name [%s]", this.coverName);
}
if (registry.smartCovers.get(this.coverName) === null) {
abort("There is no smart_cover with name [%s]", this.coverName);
}

setStalkerState(this.object, EStalkerState.SMART_COVER, null, null, null, null);
setStalkerState(this.object, EStalkerState.SMART_COVER, null, null, null, null);

this.targetPathCondlist = parseConditionsList(this.state.target_path);
this.checkTarget();
this.targetPathCondlist = parseConditionsList(this.state.target_path);
this.checkTarget();

this.coverСondlist = parseConditionsList(this.state.cover_state);
this.coverState = pickSectionFromCondList(registry.actor, this.object, this.coverСondlist) as ECoverState;
this.targetSelector(this.object);
this.checkTargetSelector();
this.coverСondlist = parseConditionsList(this.state.cover_state);
this.coverState = pickSectionFromCondList(registry.actor, this.object, this.coverСondlist) as ECoverState;
this.targetSelector(this.object);
this.checkTargetSelector();

this.object.idle_min_time(this.state.idle_min_time);
this.object.idle_max_time(this.state.idle_max_time);
this.object.lookout_min_time(this.state.lookout_min_time);
this.object.lookout_max_time(this.state.lookout_max_time);
}
this.object.idle_min_time(this.state.idle_min_time);
this.object.idle_max_time(this.state.idle_max_time);
this.object.lookout_min_time(this.state.lookout_min_time);
this.object.lookout_max_time(this.state.lookout_max_time);
}

/**
Expand Down Expand Up @@ -149,9 +140,7 @@ export class ActionSmartCoverActivity extends action_base {
);

if (targetPathSection !== NIL && targetPathSection !== null) {
const [targetPath, used] = getParametersString(targetPathSection);

this.targetPath = targetPath;
this.targetPath = targetPathSection;

if (this.targetPath !== NIL) {
if (level.patrol_path_exists(this.targetPath)) {
Expand Down
104 changes: 80 additions & 24 deletions src/engine/core/schemes/wounded/SchemeWounded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ import { ActionWounded } from "@/engine/core/schemes/wounded/actions";
import { EvaluatorCanFight, EvaluatorWounded } from "@/engine/core/schemes/wounded/evaluators";
import { ISchemeWoundedState } from "@/engine/core/schemes/wounded/ISchemeWoundedState";
import { WoundManager } from "@/engine/core/schemes/wounded/WoundManager";
import { parseData } from "@/engine/core/utils/ini/parse";
import { readIniBoolean, readIniString } from "@/engine/core/utils/ini/read";
import { IConfigSwitchCondition, parseConditionsList, readIniBoolean, readIniString } from "@/engine/core/utils/ini";
import { LuaLogger } from "@/engine/core/utils/logging";
import { getCharacterCommunity } from "@/engine/core/utils/object/object_general";
import { communities, TCommunity } from "@/engine/lib/constants/communities";
import { NIL } from "@/engine/lib/constants/words";
import { ActionPlanner, AnyObject, ClientObject, IniFile, Maybe, Optional, TNumberId } from "@/engine/lib/types";
import {
ActionPlanner,
AnyObject,
ClientObject,
IniFile,
LuaArray,
Maybe,
Optional,
TDistance,
TNumberId,
} from "@/engine/lib/types";
import { EScheme, ESchemeType, TSection } from "@/engine/lib/types/scheme";

const woundedByState: Record<number, string> = {
Expand All @@ -30,18 +39,12 @@ export class SchemeWounded extends AbstractScheme {
public static override readonly SCHEME_SECTION: EScheme = EScheme.WOUNDED;
public static override readonly SCHEME_TYPE: ESchemeType = ESchemeType.STALKER;

/**
* todo: Description.
*/
public static override activate(object: ClientObject, ini: IniFile, scheme: EScheme, section: TSection): void {
const state: ISchemeWoundedState = AbstractScheme.assign(object, ini, scheme, section);

state.woundManager = new WoundManager(object, state);
}

/**
* todo: Description.
*/
public static override add(
object: ClientObject,
ini: IniFile,
Expand Down Expand Up @@ -79,9 +82,6 @@ export class SchemeWounded extends AbstractScheme {
.add_precondition(new world_property(EEvaluatorId.CAN_FIGHT, true));
}

/**
* todo: Description.
*/
public static override reset(
object: ClientObject,
scheme: EScheme,
Expand Down Expand Up @@ -169,25 +169,31 @@ export class SchemeWounded extends AbstractScheme {

// Initialize state:
if (tostring(section) === NIL) {
state.hp_state = parseData(defaults.hp_state);
state.hp_state_see = parseData(defaults.hp_state_see);
state.psy_state = parseData(defaults.psy_state);
state.hp_victim = parseData(defaults.hp_victim);
state.hp_cover = parseData(defaults.hp_cover);
state.hp_fight = parseData(defaults.hp_fight);
state.hp_state = SchemeWounded.parseData(defaults.hp_state);
state.hp_state_see = SchemeWounded.parseData(defaults.hp_state_see);
state.psy_state = SchemeWounded.parseData(defaults.psy_state);
state.hp_victim = SchemeWounded.parseData(defaults.hp_victim);
state.hp_cover = SchemeWounded.parseData(defaults.hp_cover);
state.hp_fight = SchemeWounded.parseData(defaults.hp_fight);
state.help_dialog = defaults.help_dialog;
state.help_start_dialog = null;
state.use_medkit = defaults.use_medkit;
state.autoheal = true;
state.enable_talk = true;
state.not_for_help = defaults.not_for_help;
} else {
state.hp_state = parseData(readIniString(ini, section, "hp_state", false, "", defaults.hp_state));
state.hp_state_see = parseData(readIniString(ini, section, "hp_state_see", false, "", defaults.hp_state_see));
state.psy_state = parseData(readIniString(ini, section, "psy_state", false, "", defaults.psy_state));
state.hp_victim = parseData(readIniString(ini, section, "hp_victim", false, "", defaults.hp_victim));
state.hp_cover = parseData(readIniString(ini, section, "hp_cover", false, "", defaults.hp_cover));
state.hp_fight = parseData(readIniString(ini, section, "hp_fight", false, "", defaults.hp_fight));
state.hp_state = SchemeWounded.parseData(readIniString(ini, section, "hp_state", false, "", defaults.hp_state));
state.hp_state_see = SchemeWounded.parseData(
readIniString(ini, section, "hp_state_see", false, "", defaults.hp_state_see)
);
state.psy_state = SchemeWounded.parseData(
readIniString(ini, section, "psy_state", false, "", defaults.psy_state)
);
state.hp_victim = SchemeWounded.parseData(
readIniString(ini, section, "hp_victim", false, "", defaults.hp_victim)
);
state.hp_cover = SchemeWounded.parseData(readIniString(ini, section, "hp_cover", false, "", defaults.hp_cover));
state.hp_fight = SchemeWounded.parseData(readIniString(ini, section, "hp_fight", false, "", defaults.hp_fight));
state.help_dialog = readIniString(ini, section, "help_dialog", false, "", defaults.help_dialog);
state.help_start_dialog = readIniString(ini, section, "help_start_dialog", false, "", null);
state.use_medkit = readIniBoolean(ini, section, "use_medkit", false, defaults.use_medkit);
Expand Down Expand Up @@ -246,4 +252,54 @@ export class SchemeWounded extends AbstractScheme {

return false;
}

/**
* todo;
*/
private static parseData(target: Optional<string>): LuaArray<{
dist: Optional<TDistance>;
state: Optional<LuaArray<IConfigSwitchCondition>>;
sound: Optional<LuaArray<IConfigSwitchCondition>>;
}> {
const collection: LuaArray<any> = new LuaTable();

if (target) {
for (const name of string.gfind(target, "(%|*%d+%|[^%|]+)%p*")) {
const dat = {
dist: null as Optional<number>,
state: null as Optional<LuaArray<IConfigSwitchCondition>>,
sound: null as Optional<LuaArray<IConfigSwitchCondition>>,
};

const [tPosition] = string.find(name, "|", 1, true);
const [sPosition] = string.find(name, "@", 1, true);

const dist = string.sub(name, 1, tPosition - 1);

let state: Optional<string> = null;
let sound: Optional<string> = null;

if (sPosition !== null) {
state = string.sub(name, tPosition + 1, sPosition - 1);
sound = string.sub(name, sPosition + 1);
} else {
state = string.sub(name, tPosition + 1);
}

dat.dist = tonumber(dist)!;

if (state !== null) {
dat.state = parseConditionsList(state);
}

if (sound !== null) {
dat.sound = parseConditionsList(sound);
}

table.insert(collection, dat);
}
}

return collection;
}
}
7 changes: 6 additions & 1 deletion src/engine/core/utils/ini/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { describe, expect, it } from "@jest/globals";

import { registerStoryLink } from "@/engine/core/database";
import { IBaseSchemeLogic } from "@/engine/core/schemes";
import { addConditionToList, getConfigSwitchConditions, parseConditionsList } from "@/engine/core/utils/ini";
import {
addConditionToList,
getConfigSwitchConditions,
getParametersString,
parseConditionsList,
} from "@/engine/core/utils/ini";
import { LuaArray, ServerObject, TIndex } from "@/engine/lib/types";
import { mockBaseSchemeLogic } from "@/fixtures/engine";
import { mockIniFile, mockServerAlifeObject } from "@/fixtures/xray";
Expand Down
Loading

0 comments on commit 1085f5a

Please sign in to comment.