default javascript code for enemy target selection

● ARCHIVED · READ-ONLY
Started by Andar 4 posts View original ↗
  1. Could someone please copy the section of the default code that deals with the target selection in case of enemies here?

    This is not only to confirm the way target rate influences this, but more specifically I want to know what happens if an enemy uses a skill with a scope of "one ally" or "one ally (dead)". Is that also influenced by a possible target rate applied to the enemy?
    And would a target rate on an enemy affect a skill with scope "one random enemy" if the player casts it?

    There are quite a number of tricks that might be used to improve troops and troop AI (or to give the player some more tricky skill effects) if everything works in a more general way, but it might also be that the programmers made some shortcuts to disable target scopes that would be less effective or senseless, or to program the target rate trait in a way that it only works on actors.

    Checking the code would be the simplest way to test that, but I don't know where to start looking...

    Thank you
  2. Quick search on rpg_object.js because I believe it has the same structure as Ace, so
    Code:
    Game_Action.prototype.decideRandomTarget = function() {
        var target;
        if (this.isForDeadFriend()) {
            target = this.friendsUnit().randomDeadTarget();
        } else if (this.isForFriend()) {
            target = this.friendsUnit().randomTarget();
        } else {
            target = this.opponentsUnit().randomTarget();
        }
        if (target) {
            this._targetIndex = target.index();
        } else {
            this.clear();
        }
    };
    
    Game_Unit.prototype.tgrSum = function() {
        return this.aliveMembers().reduce(function(r, member) {
            return r + member.tgr;
        }, 0);
    };
    
    Game_Unit.prototype.randomTarget = function() {
        var tgrRand = Math.random() * this.tgrSum();
        var target = null;
        this.aliveMembers().forEach(function(member) {
            tgrRand -= member.tgr;
            if (tgrRand <= 0 && !target) {
                target = member;
            }
        });
        return target;
    };

    Edit: Added more
    Code:
    Game_Action.prototype.makeTargets = function() {
        var targets = [];
        if (!this._forcing && this.subject().isConfused()) {
            targets = [this.confusionTarget()];
        } else if (this.isForOpponent()) {
            targets = this.targetsForOpponents();
        } else if (this.isForFriend()) {
            targets = this.targetsForFriends();
        }
        return this.repeatTargets(targets);
    };
    
    Game_Action.prototype.repeatTargets = function(targets) {
        var repeatedTargets = [];
        var repeats = this.numRepeats();
        for (var i = 0; i < targets.length; i++) {
            var target = targets[i];
            if (target) {
                for (var j = 0; j < repeats; j++) {
                    repeatedTargets.push(target);
                }
            }
        }
        return repeatedTargets;
    };
    
    Game_Action.prototype.confusionTarget = function() {
        switch (this.subject().confusionLevel()) {
        case 1:
            return this.opponentsUnit().randomTarget();
        case 2:
            if (Math.randomInt(2) === 0) {
                return this.opponentsUnit().randomTarget();
            }
            return this.friendsUnit().randomTarget();
        default:
            return this.friendsUnit().randomTarget();
        }
    };
    
    Game_Action.prototype.targetsForOpponents = function() {
        var targets = [];
        var unit = this.opponentsUnit();
        if (this.isForRandom()) {
            for (var i = 0; i < this.numTargets(); i++) {
                targets.push(unit.randomTarget());
            }
        } else if (this.isForOne()) {
            if (this._targetIndex < 0) {
                targets.push(unit.randomTarget());
            } else {
                targets.push(unit.smoothTarget(this._targetIndex));
            }
        } else {
            targets = unit.aliveMembers();
        }
        return targets;
    };
    
    Game_Action.prototype.targetsForFriends = function() {
        var targets = [];
        var unit = this.friendsUnit();
        if (this.isForUser()) {
            return [this.subject()];
        } else if (this.isForDeadFriend()) {
            if (this.isForOne()) {
                targets.push(unit.smoothDeadTarget(this._targetIndex));
            } else {
                targets = unit.deadMembers();
            }
        } else if (this.isForOne()) {
            if (this._targetIndex < 0) {
                targets.push(unit.randomTarget());
            } else {
                targets.push(unit.smoothTarget(this._targetIndex));
            }
        } else {
            targets = unit.aliveMembers();
        }
        return targets;
    };
  3. @TheoAllen Thank, that answers most of my questions.

    Two things are still missing:
    could you please add the function randomDeadTarget(), and can you check if a dead enemy remains in the troop battler list or if it is removed (Basically can a revive-skill work for enemies)?


    Just in case anyone is wondering about this and the similiar question I asked a while ago about enemy skill selection:
    I'm slowly working my way through the options for an evented, troop-based AI. And with this I can confirm that it's not only possible, but the results can be better than any of the existing enemy AI plugins. It'll be a lot of work (admittedly even more work than using the AI-Plugins), but the possible effects will go far beyond that.
    But it'll be months before I can finish that tutorial due to very limited time in RL.
  4. randomDeadTarget() is randomTarget() without tgr involded
    Code:
    Game_Unit.prototype.randomDeadTarget = function() {
        var members = this.deadMembers();
        if (members.length === 0) {
            return null;
        }
        return members[Math.floor(Math.random() * members.length)];
    };