/// <reference path="../../AI.ts" />
/// <reference path="../../Things/Person.ts" />
module AIRules {
    export var standUp = new Rule({
        name: "Stand if down",
        firstPriority : AIRules.PRIORITY_ACTING_ON_SITUATION,
        priority : AIRules.PRIORITY_ACTING_ON_SITUATION,
        conditions : (runner : RulebookRunner<Person>) => {
            let person = runner.noun;
            return (person.stance != PersonStance.STANDING)
        },
        code : (runner : RulebookRunner<Person>) => {
            return new ActionStand(runner.noun);
        }
    });
    AI.combatRules.addRule(standUp);
    AI.rules.addRule(standUp);


    export var Hunt = AI.rules.createAndAddRule({
        name: "Follow based on grudge",
        firstPriority: AIRules.PRIORITY_ACTING_ON_SITUATION,
        priority: AIRules.PRIORITY_ACTING_ON_IDLE,
        code: (runner: RulebookRunner<Person>) => {
            let person = runner.noun;
            let hostiles = [...runner.noun.AI.hostileTargets].sort((a, b) => {
                return person.AI.getHostilityTo(b) - person.AI.getHostilityTo(a);
            });
            for (let i = 0; i < hostiles.length; i++) {
                if (ActionFollow.isCloseEnough(person, hostiles[i])) {
                    return new ActionFollow(person, hostiles[i]);
                }
            }
        },
        conditions: (runner: RulebookRunner<Person>) => {
            return runner.noun.AI.hostileTargets.length > 0;
        }
    });


    export var rngHit = AI.rules.createAndAddRule({
        name: "Hit someone else RNGly",
        firstPriority: AIRules.PRIORITY_ACTING_ON_IDLE,
        priority: AIRules.PRIORITY_ACTING_ON_SITUATION,
        code: (runner: RulebookRunner<Person>) => {
            for (let i = 0; i < runner.noun.AI.newNoticed.length; i++) {
                if (runner.noun.AI.newNoticed[i] instanceof Person && runner.noun.AI.newNoticed[i] != WorldState.player) {
                    return new ActionAttack(runner.noun, runner.noun.AI.newNoticed[i]);
                }
            }
        },
        conditions: (runner: RulebookRunner<Person>) => {
            return (Math.random() * 100) >= 50;
        }
    });
}