<commit_before>
/* This file contains action evaluators used in combination with actor Goals to
 * implement more diverse computational intelligence for NPCs.
 * Each evaluator should return a desirability of given action, which is a
 * number between 0 and 1.
 */
import RG from './rg';
import {Goal, GoalBase} from './goals';
import {GoalThief} from './goal.thief';
import {GoalTreasureHunter} from './goal.treasure-hunter';
import {Random} from './random';
import {SpellArgs} from './spell';
import {Brain} from './brain';
import {TCoord, TNoFuncVal} from './interfaces';
import {BBox} from './bbox';

type SentientActor = import('./actor').SentientActor;

Goal.Thief = GoalThief;
Goal.TreasureHunter = GoalTreasureHunter;

export const Evaluator: any = {};
Evaluator.hist = {};

// Should be returned if evaluator is not applicable to current situation
Evaluator.NOT_POSSIBLE = RG.BIAS.NOT_POSSIBLE;

// Should be returned if the case is always executed
Evaluator.ALWAYS = RG.BIAS.ALWAYS;

const RNG = Random.getRNG();

interface EvalArgs {
    [key: string]: TNoFuncVal;
}

/* Base class for all evaluators. Provides only the basic constructor. */
export class EvaluatorBase {

    public actorBias: number;
    public type: string;

    // To change the evaluation frequency
    public evalCount: number;
    public evalMaxCount: number;
    public isOneShot: boolean;
    public wasLastChosen: boolean;

    constructor(actorBias: number) {
        this.actorBias = actorBias;
        this.type = 'Base';
        this.evalCount = 0;
        this.evalMaxCount = 1;
        this.isOneShot = false;
        this.wasLastChosen = false;
    }

    public calculateDesirability(actor): number {
        throw new Error('Pure virtual function');
    }

    /* THis is used to inject any arbitrary args to a Goal. You need to override
     * this in derived class with the desired args. */
    public setActorGoal(actor, ...args: any[]): void {
        const topGoal = actor.getBrain().getGoal();
        if (Goal[this.type]) {
            const goal = new Goal[this.type](actor, ...args);
            topGoal.addGoal(goal);
            ++Evaluator.hist[this.type];
        }
        else {
            RG.err(`EvaluatorBase (${this.type})`, 'setActorGoal',
                `No Goal.${this.type} found!`);
        }
    }

    public isOrder(): boolean {return false;}

    public getType(): string {return this.type;}

    public setBias(bias: number): void {
        this.actorBias = bias;
    }

    public toJSON() {
        return {
            type: this.getType(),
            bias: this.actorBias
        };
    }

    /* Called by FromJSON. */
    public setArgs(args): void {}

}
Evaluator.Base = EvaluatorBase;


export class EvaluatorAttackActor extends EvaluatorBase {

    public enemyActor: SentientActor;

    constructor(actorBias: number) {
        super(actorBias);
        this.type = 'AttackActor';
    }

    public calculateDesirability(actor): number {
        const brain = actor.getBrain();
        const seenCells = brain.getSeenCells();
        const enemyCell = brain.findEnemyCell(seenCells);
        if (enemyCell) {
            const result = 1;
            this.enemyActor = enemyCell.getActors()[0];
            return this.actorBias * result * 2;
        }
        return Evaluator.NOT_POSSIBLE;
    }

    public setActorGoal(actor) {
        super.setActorGoal(actor, this.enemyActor);
    }

}
Evaluator.AttackActor = EvaluatorAttackActor;
Evaluator.hist.AttackActor = 0;<commit_msg>Uses findEnemyCellFast() now<commit_after>
/* This file contains action evaluators used in combination with actor Goals to
 * implement more diverse computational intelligence for NPCs.
 * Each evaluator should return a desirability of given action, which is a
 * number between 0 and 1.
 */
import RG from './rg';
import {Goal, GoalBase} from './goals';
import {GoalThief} from './goal.thief';
import {GoalTreasureHunter} from './goal.treasure-hunter';
import {Random} from './random';
import {SpellArgs} from './spell';
import {Brain} from './brain';
import {TCoord, TNoFuncVal} from './interfaces';
import {BBox} from './bbox';

type SentientActor = import('./actor').SentientActor;

Goal.Thief = GoalThief;
Goal.TreasureHunter = GoalTreasureHunter;

export const Evaluator: any = {};
Evaluator.hist = {};

// Should be returned if evaluator is not applicable to current situation
Evaluator.NOT_POSSIBLE = RG.BIAS.NOT_POSSIBLE;

// Should be returned if the case is always executed
Evaluator.ALWAYS = RG.BIAS.ALWAYS;

const RNG = Random.getRNG();

interface EvalArgs {
    [key: string]: TNoFuncVal;
}

/* Base class for all evaluators. Provides only the basic constructor. */
export class EvaluatorBase {

    public actorBias: number;
    public type: string;

    // To change the evaluation frequency
    public evalCount: number;
    public evalMaxCount: number;
    public isOneShot: boolean;
    public wasLastChosen: boolean;

    constructor(actorBias: number) {
        this.actorBias = actorBias;
        this.type = 'Base';
        this.evalCount = 0;
        this.evalMaxCount = 1;
        this.isOneShot = false;
        this.wasLastChosen = false;
    }

    public calculateDesirability(actor): number {
        throw new Error('Pure virtual function');
    }

    /* THis is used to inject any arbitrary args to a Goal. You need to override
     * this in derived class with the desired args. */
    public setActorGoal(actor, ...args: any[]): void {
        const topGoal = actor.getBrain().getGoal();
        if (Goal[this.type]) {
            const goal = new Goal[this.type](actor, ...args);
            topGoal.addGoal(goal);
            ++Evaluator.hist[this.type];
        }
        else {
            RG.err(`EvaluatorBase (${this.type})`, 'setActorGoal',
                `No Goal.${this.type} found!`);
        }
    }

    public isOrder(): boolean {return false;}

    public getType(): string {return this.type;}

    public setBias(bias: number): void {
        this.actorBias = bias;
    }

    public toJSON() {
        return {
            type: this.getType(),
            bias: this.actorBias
        };
    }

    /* Called by FromJSON. */
    public setArgs(args): void {}

}
Evaluator.Base = EvaluatorBase;


export class EvaluatorAttackActor extends EvaluatorBase {

    public enemyActor: SentientActor;

    constructor(actorBias: number) {
        super(actorBias);
        this.type = 'AttackActor';
    }

    public calculateDesirability(actor): number {
        const brain = actor.getBrain();
        // const seenCells = brain.getSeenCells();
        // const enemyCell = brain.findEnemyCell(seenCells);
        const enemyCell = brain.findEnemyCellFast();
        if (enemyCell) {
            const result = 1;
            this.enemyActor = enemyCell.getActors()[0];
            return this.actorBias * result * 2;
        }
        return Evaluator.NOT_POSSIBLE;
    }

    public setActorGoal(actor) {
        super.setActorGoal(actor, this.enemyActor);
    }

}
Evaluator.AttackActor = EvaluatorAttackActor;
Evaluator.hist.AttackActor = 0;
{'fraction_non_alphanumeric': 0.11175147596289008, 'fraction_numerical': 0.002249086308687096, 'mean_word_length': 2.7290356394129978, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '737701', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2145, 'n_words': 606}