Hehe, easily done. I agree though.
and a way to set them to attack a different player via the servercommand.
hmm

[edit]

figured it out Very Happy you can select a name in the AdminGui and then use AdminPlayerList.getSelectedId()
Superb! I still like the idea of making them completely autonomous, attacking everyone except the client that spawned them...
yeah, we can do something for that too, but we could have several options for different types of games to allow for single player gaming and such.
elfprince13 wrote:
yeah, we can do something for that too, but we could have several options for different types of games to allow for single player gaming and such.
Ah, good call. I concur; I think jpez is going to be working on it a bit tonight.
This script allows you to give a player's name as an argument (as opposed to a client ID). The bots now dynamically auto-acquire their target, based on which player is closest to them. Instead of being the target, the second argument is now simply who the bot is modeled after.

Code:
//----------------------------------------AI PLAYER SCRIPT

function serverCmdcFA(%client,%wep,%number)
{
   createFighterArmy(%wep,%client,%number);
}
function createFighterArmy(%wep,%target,%number)
{
   createFighterBot(%wep,%target);
   %number--;
   if (%number > 0)
   {
      schedule(2000,0,createFighterArmy,%wep,%target,%number);
   }
}
function createFighterBot(%wep,%targetName)
{
    %who = new AIPlayer() {
        dataBlock = LightMaleHumanArmor;
        aiPlayer = true;
    };

    %target = getPlayerByName(%targetName);

    MissionCleanup.add(%who);
    echo("ID :"@%who);
    // Player setup
    %who.setMoveSpeed(1);
    %who.setTransform(pickSpawnPoint());
    %who.setEnergyLevel(60);
    %who.setShapeName(%target.namebase@"'s Evil Twin");
    %who.setSkinName(%target.colorSkin);
    %who.mountImage(%target.headCode, $headSlot, 1, %target.headCodeColor);
    %who.mountImage(%target.visorCode, $visorSlot, 1, %target.visorCodeColor);
    %who.mountImage(%target.backCode, $backSlot, 1, %target.backCodeColor);
    %who.mountImage(%target.leftHandCode, $leftHandSlot, 1, %target.leftHandCodeColor);
    %who.mountImage(%target.chestCode, $chestSlot, 1, %target.chestdecalcode);
    %who.mountImage(%target.faceCode, $faceSlot, 1, %target.facedecalcode);
    %who.mountImage(nametoid(%wep),$rightHandSlot);
    echo(%who.namebase);
    echo(%target.namebase);
    schedule(1000,0,mybotreaim,%who);
}
function mybotreaim(%who)
{
    %target = findClosestPlayer(%who);
   
    %who.setAimObject(%target);
    %who.setImageTrigger(0,1);
   
   if (isobject(%target))
   {
      %targetlocation = %target.player.getTransform();
      %targetx = getWord(%targetlocation, 0);
      %targety = getWord( %targetlocation, 1);
      %targetz = getWord( %targetlocation, 2);

      %whox = getWord( %who.getTransform(), 0 );
      %whoy = getWord( %who.getTransform(), 1 );
      %whoz = getWord( %who.getTransform(), 2 );

      %who.setMoveDestination(((%whox <= %targetx) ? %targetx - 1 : %targetx + 1)@" "@((%whoy <= %targety) ? %targety - 1 : %targety + 1)@" "@((%whoz <= %targetz) ? %targetz - 1 : %targetz + 1));
      %xdiff = mabs(getWord( %target.player.getTransform(), 0 ) - getWord( %who.getTransform(), 0 ));
      %ydiff = mabs(getWord( %target.player.getTransform(), 1 ) - getWord( %who.getTransform(), 1 ));
      %whotol = (%xdiff > %ydiff)? %xdiff/2 : %ydiff/2;
     
      if ( %whoz <  %targetz)
      {
         %who.setimagetrigger(2,1);
         %who.setimagetrigger(4,1);
      } else if ( %targetz + %whotol <  %whoz)
          %who.setMoveSpeed(1);
      else if (%whotol > 20 || %whotol < 5 && ( %targetz <=  %whoz))
      {
        %who.setimagetrigger(2,0);
         %who.setimagetrigger(4,0);
      }
   }
   
    %who.setAimLocation(%targetx@" "@%targety@" "@%targetz);
    if(isobject(%who))
        schedule(1000,0,mybotreaim,%who);
}

function distance(%objectOne,%objectTwo){
   %objectOneLocation = %objectOne.player.getTransform();
   %onex = getWord( %objectOneLocation, 0);
   %oney = getWord( %objectOneLocation, 1);
   %onez = getWord( %objectOneLocation, 2);

   %objectTwoLocation = %objectTwo.player.getTransform();
   %twox = getWord( %objectTwoLocation, 0);
   %twoy = getWord( %objectTwoLocation, 1);
   %twoz = getWord( %objectTwoLocation, 2);
   
   %dx = %onex - %twox;
   %dy = %oney - %twoy;
   %dz = %onez - %twoz;

   echo(mSqrt(%dx * %dx + %dy * %dy + %dz * %dz));
   return mSqrt(%dx * %dx + %dy * %dy + %dz * %dz);
}

function getPlayerByName(%playername){
   for (%clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++) {     
      %victim = ClientGroup.getObject( %clientIndex );   
      if (%victim.namebase == %playername) return %victim;
   }
   return 0;
}

function findClosestPlayer(%bot){
   %closest = 0;
   for (%clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++) {         
      if (distance(%bot,ClientGroup.getObject(%closest)) > distance(%bot,ClientGroup.getObject(%clientIndex)))
       %closest = %clientIndex;
   }
   echo(ClientGroup.getObject(%closet).namebase@" is closest.");
   return ClientGroup.getObject(%closest);
}
Nice. The only thing is that when you joined, it insisted on shooting at you, even if I was closer.
Yeah, it's hard to test with only one person. Do you understand what the code I added does?
I do indeed. Lemme examine it if/when I solve all the DCS bugs I'm slated to repair today.
nice work, but what happens when you get that 120 bots all running that for loop? Neutral lol
elfprince13 wrote:
nice work, but what happens when you get that 120 bots all running that for loop? Neutral 0x5
0x5, good point.
KermMartian wrote:
elfprince13 wrote:
nice work, but what happens when you get that 120 bots all running that for loop? Neutral 0x5
0x5, good point.


remember yesterday? we had at least 120, since at one point there were 40 just of me...
Oh yeah. Want to come work on it for a bit?
yup.
It's really driving me nuts why using "swordImage" as an argument doesn't work. Wtf?

Dynamic auto-acquire should work now:


Code:
//----------------------------------------AI PLAYER SCRIPT

function serverCmdcFA(%client,%wep,%number)
{
   createFighterArmy(%wep,%client,%number);
}
function createFighterArmy(%wep,%target,%number)
{
   createFighterBot(%wep,%target);
   %number--;
   if (%number > 0)
   {
      schedule(2000,0,createFighterArmy,%wep,%target,%number);
   }
}
function createFighterBot(%wep,%targetName)
{
    %who = new AIPlayer() {
        dataBlock = LightMaleHumanArmor;
        aiPlayer = true;
    };

    %target = getPlayerByName(%targetName);
    MissionCleanup.add(%who);
    echo("ID :"@%who);
    // Player setup
    %who.setMoveSpeed(1);
    %who.setTransform(pickSpawnPoint());
    %who.setEnergyLevel(60);
    %who.setShapeName(%target.namebase@"'s Evil Twin");
    %who.setSkinName(%target.colorSkin);
    %who.mountImage(%target.headCode, $headSlot, 1, %target.headCodeColor);
    %who.mountImage(%target.visorCode, $visorSlot, 1, %target.visorCodeColor);
    %who.mountImage(%target.backCode, $backSlot, 1, %target.backCodeColor);
    %who.mountImage(%target.leftHandCode, $leftHandSlot, 1, %target.leftHandCodeColor);
    %who.mountImage(%target.chestCode, $chestSlot, 1, %target.chestdecalcode);
    %who.mountImage(%target.faceCode, $faceSlot, 1, %target.facedecalcode);
    %who.mountImage(nametoid(%wep),$rightHandSlot);
    echo(%who.namebase);
    echo(%target.namebase);
    schedule(1000,0,mybotreaim,%who);
}
function mybotreaim(%who)
{
    %target = findClosestPlayer(%who);
   
    %who.setAimObject(%target);
    %who.setImageTrigger(0,1);
   
   if (isobject(%target))
   {
      %targetlocation = %target.player.getTransform();
      %targetx = getWord(%targetlocation, 0);
      %targety = getWord( %targetlocation, 1);
      %targetz = getWord( %targetlocation, 2);

      %whox = getWord( %who.getTransform(), 0 );
      %whoy = getWord( %who.getTransform(), 1 );
      %whoz = getWord( %who.getTransform(), 2 );

      %who.setMoveDestination(%targetlocation);
      %xdiff = mabs(getWord( %target.player.getTransform(), 0 ) - getWord( %who.getTransform(), 0 ));
      %ydiff = mabs(getWord( %target.player.getTransform(), 1 ) - getWord( %who.getTransform(), 1 ));
      %whotol = (%xdiff > %ydiff)? %xdiff/2 : %ydiff/2;
     
      if ( %whoz <  %targetz)
      {
         %who.setimagetrigger(2,1);
         %who.setimagetrigger(4,1);
      } else if ( %targetz + %whotol <  %whoz)
          %who.setMoveSpeed(1);
      else if (%whotol > 20 || %whotol < 5 && ( %targetz <=  %whoz))
      {
        %who.setimagetrigger(2,0);
         %who.setimagetrigger(4,0);
      }
   }
   
    %who.setAimLocation(%targetx@" "@%targety@" "@%targetz);
    if(isobject(%who))
        schedule(1000,0,mybotreaim,%who);
}


function getPlayerByName(%playername){
   for (%clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++) {     
      %victim = ClientGroup.getObject( %clientIndex );   
      if (%victim.namebase == %playername) return %victim;
   }
   return 0;
}

function findClosestPlayer(%bot){
   %closest = 0;
   for (%clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++) {
   %botpos = %bot.getTransform();
   %closestObj = ClientGroup.getObject(%closest);
   %clientObj = ClientGroup.getObject(%clientIndex);         
      if (VectorDist(%botpos,%closestObj.player.getTransform()) > VectorDist(%botpos,%clientObj.player.getTransform()))
       %closest = %clientIndex;
   }
   //echo(ClientGroup.getObject(%closet).namebase@" is closest.");
   return ClientGroup.getObject(%closest);
}
swordimage worked for me....
Ok, next issue. When we examine this function:

Code:
function getPlayerByName(%playername){
   for (%clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++) {     
      %victim = ClientGroup.getObject( %clientIndex );   
      if (%victim.namebase == %playername) return %victim;
   }
   return 0;
}

It has been working perfectly on my client. However, I did a little research on TorqueScript and found that I should be using the string comparison operator so the appropriate line would be this:

Code:
if (%victim.namebase [b]$=[/b] %playername) return %victim;

However, when I use that, it doesn't work on my client and the bots don't have the proper "clothing" on them, presumably because the function to find the player isn't working correctly. Yet, when it's on Kerm's server, the bots don't seem to work with the existing code. Ideas?
Im gonna go do some digging on the garage games website. I talked to mark_madness last night and he gave me the code for making a crouch, but it doesn't have the same effect on a bot that it does on a normal player. evidently the bot doesn't call the swimming code along with the crouch, whereas when a normal player crouches the swimming code gets called too. Im gonna go on the GG forum and see if I can find the code used in our swim function and add that along with the crouching code to our botscript. sorry if that was long and rambling.
'twasn't too long or rambling. Sounds like a plan though.
Here is the next revision. Improvements include: bots hunt each other (hehe), you only have to give the weapon name (without the image junk), there is now a default weapon (the sword), bots can be killed with clearbots(), bots will no longer hunt their "creator" if you choose to enable human targets, and a host of other good stuff. This should be fun to play with.
Code:
//----------------------------------------AI PLAYER SCRIPT
//written by members of Cemetech.net including Kerm Martian, elfprince13, & jpez


$botlist[0] = "DUMMY";
$numbots = 0;

function blist(){
   for(%i = 0; %i < $numbots; %i++){
      %bot = $botlist[%i];
      echo(%bot SPC %bot.getName());
   }
}

function clearbots(){
   for(%i = 0; %i < $numbots; %i++){
      %bot = $botlist[%i];
      %bot.kill();
   }
   $botlist = "";
   $numbots = 0;
}

function serverCmdcFA(%client,%wep,%number)
{
   createFighterArmy(%wep,%client,%number);
}
function createFighterArmy(%wep,%target,%number)
{
   createFighterBot(%wep,%target);
   %number--;
   if (%number > 0)
   {
      schedule(2000,0,createFighterArmy,%wep,%target,%number);
   }
}
function createFighterBot(%wep,%targetName)
{
    %who = new AIPlayer() {
        dataBlock = LightMaleHumanArmor;
        aiPlayer = true;
    };
   if(%wep $= "") %wep = "sword";
   %wep = %wep@"Image";

    %target = getPlayerByName(%targetName);
    MissionCleanup.add(%who);
    echo("ID :"@%who);
    // Player setup
    %who.setMoveSpeed(1);
    %who.setTransform(pickSpawnPoint());
    %who.setEnergyLevel(60);
    %who.setShapeName(%target.namebase@"'s Evil Twin");
   %who.setName(%target.namebase@" Bot");
    %who.setSkinName(%target.colorSkin);
    %who.mountImage(%target.headCode, $headSlot, 1, %target.headCodeColor);
    %who.mountImage(%target.visorCode, $visorSlot, 1, %target.visorCodeColor);
    %who.mountImage(%target.backCode, $backSlot, 1, %target.backCodeColor);
    %who.mountImage(%target.leftHandCode, $leftHandSlot, 1, %target.leftHandCodeColor);
    %who.mountImage(%target.chestCode, $chestSlot, 1, %target.chestdecalcode);
    %who.mountImage(%target.faceCode, $faceSlot, 1, %target.facedecalcode);
    %who.mountImage(nametoid(%wep),$rightHandSlot);
   $botlist[$numbots] = %who;
   $numbots++;
    echo(%who.getName());
    echo(%target.namebase);
    schedule(1000,0,mybotreaim,%who);
}

function mybotreaim(%who)
{
   //%target = findClosestPlayer(%who);
    %target = findClosestBot(%who);
   
   
    %who.setAimObject(%target);
    %who.setImageTrigger(0,1);
   
   if (isobject(%target))
   {
   if(%target.getClassName() $= "AIplayer"){
      %targetlocation = %target.getTransform(); //the target is a bot
   }else{
      %targetlocation = %target.player.getTransform(); //the target is a human
   }
      %targetx = getWord( %targetlocation, 0);
      %targety = getWord( %targetlocation, 1);
      %targetz = getWord( %targetlocation, 2);

      %whox = getWord( %who.getTransform(), 0 );
      %whoy = getWord( %who.getTransform(), 1 );
      %whoz = getWord( %who.getTransform(), 2 );

      %who.setMoveDestination(%targetlocation);
      %xdiff = mabs(getWord( %targetlocation, 0 ) - getWord( %who.getTransform(), 0 ));
      %ydiff = mabs(getWord( %targetlocation, 1 ) - getWord( %who.getTransform(), 1 ));
      %whotol = (%xdiff > %ydiff)? %xdiff/2 : %ydiff/2;
     
      if ( %whoz <  %targetz)
      {
         %who.setimagetrigger(2,1);
         %who.setimagetrigger(4,1);
      } else if ( %targetz + %whotol <  %whoz)
          %who.setMoveSpeed(1);
      else if (%whotol > 20 || %whotol < 5 && ( %targetz <=  %whoz))
      {
        %who.setimagetrigger(2,0);
         %who.setimagetrigger(4,0);
      }
   }
   
    %who.setAimLocation(%targetx@" "@%targety@" "@%targetz);
    if(isobject(%who))
        schedule(1000,0,mybotreaim,%who);
}


function getPlayerByName(%playername){
   for (%clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++) {     
      %victim = ClientGroup.getObject( %clientIndex );   
      if (%victim.namebase == %playername) return %victim;
   }
   return 0;
}

function findClosestPlayer(%bot){
   %closest = 0;
   %botpos = %bot.getTransform();
   for (%clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++) {
   %closestObj = ClientGroup.getObject(%closest);
   %clientObj = ClientGroup.getObject(%clientIndex);         
      if (VectorDist(%botpos,%closestObj.player.getTransform()) > VectorDist(%botpos,%clientObj.player.getTransform()) && %closestObj.namebase != %bot.getName())
       %closest = %clientIndex;
   }
   //echo(ClientGroup.getObject(%closet).namebase@" is closest.");
   return ClientGroup.getObject(%closest);
}

function findClosestBot(%bot){
   if($numbots <= 1) return %bot;
   %closest = 0;
   if($botlist[0] == %bot) %closest = 1;
   %botpos = %bot.getTransform();
      for (%i = 0; %i < $numbots; %i++) {
      %closestObj = $botlist[%closest];
      %botObj = $botlist[%i];         
         if (VectorDist(%botpos,%closestObj.getTransform()) > VectorDist(%botpos,%botObj.getTransform()) && %bot != %botObj)
          %closest = %i;
   }
   return $botlist[%closest];
}
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
» Goto page Previous  1, 2, 3, 4, 5, 6 ... 10, 11, 12  Next
» View previous topic :: View next topic  
Page 5 of 12
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement