TGR Help!(Close Topic)

So far i have code that removes the random elements of targeting and just make foes attack the actor with the highest tgr, my issues is when there are 3 actors and two of them have the same tgr i them need to randomly choose between them and im having a hard time figuring out how best to do this. any ideas?


Ultimately im planning a rather large overhaul of tgr as RMMV uses it making it more of a dynamic stat that will change throughout  combat, and have all skills and item usage increase or decrease your tgr by a certain amount. effectively creating a threat system similar to that of an mmo.
If you show us how you targeting the most tgr maybe i can help
was kinda hoping i wouldn't have to show anyone my horrid coding lol

If you show us how you targeting the most tgr maybe i can help


Spoiler
var alias_Threat_Builder = Game_Unit.prototype.randomTarget;
Game_Unit.prototype.randomTarget = function(){
var target = null;
var Ckthreat = 0 ;
var hitwho = 0;
var Highest = 0;
var Rateck = 0;
var Equalcnt = 0;
var total = 0;
var Array = [];
var arrarycount = 0;
var pullit = [];
var element = 0;
var keeper = [];
var loopchk = 0;
var Gift = 0;
var p2 = 0;
var tgrRand = Math.random() * this.tgrSum();
this.aliveMembers().forEach(function(member) {
Highest = member.tgr;
pullit[element]= member.tgr;

element++;
})
var output = Math.max.apply(null,pullit);
var counter1 = 0;
while(counter1<pullit.length){
if(output==pullit[counter1]){
keeper[counter1]=output

}
counter1++;
}
if(keeper.length>1){

while(loopchk==0){
var cane = Math.random() * keeper.length
console.log(p2);
p2 = Math.floor(cane);

Gift = keeper[p2];
loopchk = 1;

if(keeper[Gift] != undefined){
loopchk=0
console.log(keeper[Gift]);

}
//console.log(loopchk)
}

}
else{
this.aliveMembers().forEach(function(member) {
if(member.tgr>Ckthreat){
Ckthreat = member.tgr;



hitwho = member;


}

target = hitwho;

})

}

this.aliveMembers().forEach(function(member) {
tgrRand -= member.tgr;
if (tgrRand <= 0 && !target) {
target = member;
}
}
)

var show = keeper[1];



return target;


}

ok So i have made it this far, i almost had it but for some reason( and maybe this is because its 2 am where i am) keeper[Gift] is undefined always no matter how many times it loops its an infinite loop but i don't understand it.
I am not sure how you can make the target with highest tgr, because it's still random even with someone with 100% and the other less.


But i saw small problem with the loop


Add 1 to the end of


var cane = Math.random() * keeper.length + 1;


because if not it will be random between 0 and 1


I hope this solve something.


But don't worry I am still trying.
I am not sure how you can make the target with highest tgr, because it's still random even with someone with 100% and the other less.


But i saw small problem with the loop


Add 1 to the end of


var cane = Math.random() * keeper.length + 1;


because if not it will be random between 0 and 1


I hope this solve something.


But don't worry I am still trying.

I already  have managed to remove the random chance from the targeting system when an actors tgr is higher than everyone else's there part I'm working on now is to help resolve ties for the highest.  like I'm my testing  if I have 3 actors  and two have tgr of 97 and the other 100 , the one with 100 is attacked Always that works great. The current part of the code giving issue is when 1 actor is at 97 tgr and the other 2 are at 100, in this senerio I want to choose randomly between the 2 actors that have the same. I just want someone to explain to me how keeper [Gift] could be undefined no matter how many times the loop loop goes on, it's suppose to keep looping until keeper[Gift] equal something other than undefined. The Cane variable  needs to be zero sometimes since 0 is the first index of the array
I made one works great maybe you can figure your solution .

Code:
Game_Unit.prototype.randomTarget = function() {
    var m = 0;
    var most = [];
    var all = [];
    this.aliveMembers().forEach(function(member) {
       all.push(member);     
              if (member.tgr>m){m=member.tgr};

});
        all.forEach(function(i) {
              if (i.tgr==m){most.push(i)};
});

   if (most.length>1){target=most[Math.floor((Math.random()*most.length))];}else{
          target = most[0]      
      };
    return target;
};
oh wow i like yours alot more so much neater lol but i did get my clunky code to produced my desired result thanks looking at your code helped me figure it out!

Original thread: https://forums.rpgmakerweb.com/threads/tgr-help-close-topic.65588/