Prime - Amber Tweaks and Scripting API

Forum for alternative clients, mods & discussions on the same.

Re: Prime - Amber tweaks and Scripting API

Postby LostJustice » Mon Dec 19, 2016 12:12 pm

I thought I also add this script in... It a chopper script that cuts wood and waits for stamina to regenerate. A good script for anyone needing to clear large forests or set up a palisade.

Code: Select all
LoadGame();
var AutoChopping = true;
var changeDirectionCounter = 10;
var x;
var y;
var min = 30;
var max = 50;
var treeList = [];
var target;

function main(){
   
   java.lang.System.out.print("Checking stamina...\n");
   CheckStamina();
   java.lang.System.out.print("Chopping...\n");
    AutoChop();
   
}

function AutoWalk(){
   
    if(AutoWalking) {
      if(changeDirectionCounter >= 10)
      {
         changeDirectionCounter = 0;
         x = getRandomInt(-1, 1);
         y = getRandomInt(-1, 1);
      }
      var xAmount = getRandomInt(min, max);
      var yAmount = getRandomInt(min, max);
        var playerCoords = Game.getPlayerCoords();
        Game.goTo(playerCoords.x + xAmount*x, playerCoords.y+ yAmount*y);
      changeDirectionCounter++;
        sleep(500); //give time to move
    }
}

function CheckStamina()
{
   if(Game.getStamina() >= 90)
   {
      java.lang.System.out.print("You have the stamina!\n");
   }
   else{
      java.lang.System.out.print("You don't have enough stamina... Waiting "+(100-Game.getStamina())*7+" seconds.");
      sleep(1000*(100-Game.getStamina())*7);
   }
   if(Game.getEnergy() > 40)
   {
      
   }
   else
   {
      java.lang.System.out.print("You have ran out of energy! Feed your character! The script will now be stopped.\n")
      AutoChopping = false;
   }
}

function AutoChop()
{   
   if(AutoChopping)
   {
      if (treeList === undefined || treeList.length == 0) {
         java.lang.System.out.print("Finding trees...\n");
         findTrees();
      }
      else if(target === undefined || isEmpty(target))
      {
         java.lang.System.out.print("Finding target tree...\n");
         findClosestTree();
      }
      else if(target !== undefined){
         java.lang.System.out.print("Chopping tree!\n");
         chopTree()
      }
      else{
         java.lang.System.out.print("Nothing was triggered? Are there trees?\n");
         java.lang.System.out.print("Tree List Size: " + treeList.length + " Target: " + target + "/n");
      }
   }
   
   
}

function removeTree(tree)
{
   for(var i = 0; i < treeList.length; i++)
   {
      if(treeList[i] == tree)
      {
         treeList.splice(i, 1);
      }
   }
}

function chopTree()
{
   Game.goTo(target.coords.x, target.coords.y);
   java.lang.System.out.print("Going to tree...\n");
      WaitTillStopped();
      java.lang.System.out.print("Right clicking tree...\n");
      if(Game.mapObjectRightClick(target.id))
      {
         java.lang.System.out.print("Chop!\n");
         if(Game.chooseFlowerMenuOption('Chop'))
         {
            java.lang.System.out.print("Waiting to finish...\n");
            Game.waitForTaskToFinish();
                                treeList = []; //You must always clear the list of trees after each attempt in order to renew the map list. No real way to check existence of tree.
         }
         else{
            removeTree(target);
         }
      }
      else{
         removeTree(target);
      }
}
function findTrees()
{
   var mapObjects = Game.getAllMapObjects();
   for (i = 0; i < mapObjects.length; i++) {
      if(containsSubstring(mapObjects[i].fullName, "tree") && !containsSubstring(mapObjects[i].fullName, "stump") && !containsSubstring(mapObjects[i].fullName, "log"))
      {
         java.lang.System.out.print("Found a tree!\n");
         java.lang.System.out.print("Adding it to the list of tress...\n");
         treeList.push(mapObjects[i]);
         java.lang.System.out.print("Tree List Length is now: "+treeList.length+"\n");

      }
   }
}

function findClosestTree()
{
   if(target === undefined)
   {
      target = treeList[1];
   }
   if(treeList.length > 1)
   {
      for (var i = 0; i < treeList.length; i++)
      {
         var one = target;
         var two = treeList[i];
         var playerCoords = Game.getPlayerCoords();
         var d1 = distance(one.coords.x-playerCoords.x, one.coords.y-playerCoords.y);
         var d2 = distance(two.coords.x-playerCoords.x, two.coords.y-playerCoords.y);
         if(d1 > d2)
         {
            target = treeList[i];
         }
      }
   }
   
}

function isEmpty(value){
  return (value == null || value.length === 0);
}

function distance(x, y)
{
   return Math.sqrt( Math.pow(x, 2) + Math.pow(y, 2) );
}


function containsSubstring(obj, sub)
{
   return obj.includes(sub);
}

function onCreatureFound(id, name, coords) {
    if(name == 'bear' || name == 'boar' || name == 'badger' || name =='lynx')
   {
      home();
   }
}

function home()
{
   Game.travelToHearthFire();
    WaitTillStopped();
}

function safeExit()
{
   Game.travelToHearthFire();
    WaitTillStopped();
    Game.logout();
    Exit();
}
Image
User avatar
LostJustice
 
Posts: 677
Joined: Sun Mar 25, 2012 3:57 am

Re: Prime - Amber tweaks and Scripting API

Postby iamahh » Mon Dec 19, 2016 1:27 pm

would be nice if stockpiled the bough, barks and ate the fruits ¦]
iamahh
 
Posts: 1810
Joined: Sat Dec 12, 2015 8:23 pm

Re: Prime - Amber tweaks and Scripting API

Postby Thedrah » Mon Dec 19, 2016 5:43 pm

i'm only getting the error when i log in as my main as a hermit -.-


after about 10m of trial and error, it was that i had the chopper script hotkeyed from purus pasta haha. it uses the same saved options and hotkeys. would be nice to have a separate set of options/hotkeys but now i know the problem hehe

edit: ps: is there a way to run/call another script from inside another script?
  ▲
▲ ▲
Thedrah
 
Posts: 936
Joined: Fri Apr 08, 2011 2:20 am
Location: behind you

Re: Prime - Amber tweaks and Scripting API

Postby Paradoxs » Mon Dec 19, 2016 7:00 pm

Thedrah wrote:after about 10m of trial and error, it was that i had the chopper script hotkeyed from purus pasta haha. it uses the same saved options and hotkeys. would be nice to have a separate set of options/hotkeys but now i know the problem hehe


Currently Prime doesn't set any hot keys itself, so that is a bit odd. I'll add a warning to the main post following the next update.
The settings / hotkeys is a artifact of amber itself. I am in the proccess of porting everything to use .json (Like my old client and enders currently do) But it likely wont surface until the version after next since I plan to do an Amber-Tweaks only update that purely changes a few things about amber

Thedrah wrote:edit: ps: is there a way to run/call another script from inside another script?


At the moment no. It's on my list of to-dos.

Currently I am re-writing the Game object, since it was mostly written by Scrifen and just merged to the latest amber. To get it working I just used an older version of b0r3d0ms code. Now that Prime is out and mostly functional my main objective is to re-do this last relic of Scrifen. While adding some more controls along the way.

The way Prime works now, is on a single script basis, starting once stops the other, and Starting a script thats already running causes it to stop. While I am making some changes to this system for the next version It won't allow for multiple running scripts just yet, I have to modify my script runner first but lookout for it in the next day or two!
User avatar
Paradoxs
 
Posts: 294
Joined: Tue Aug 25, 2015 7:16 am

Re: Prime - Amber tweaks and Scripting API

Postby LostJustice » Tue Dec 20, 2016 12:50 am

Thedrah wrote:edit: ps: is there a way to run/call another script from inside another script?


A work around for this is to create a script as an object or a variable and put it in one large script. Then you could theoretically use multiple scripts within one script if that is what you are asking.
Image
User avatar
LostJustice
 
Posts: 677
Joined: Sun Mar 25, 2012 3:57 am

Re: Prime - Amber tweaks and Scripting API

Postby LostJustice » Tue Dec 20, 2016 12:52 am

Also I noticed another bug with the client that has to do with Game.getMapObjects.

If you do var list = Game.getMapObjects();

Then chop down a tree.

Do var list2 = Game.getMapObjects();

Then list == list2 is true. This should be false because you chopped down a tree which is an object hence the lists should be different but they are the same meaning the getMapObjects(); is not updating the chopped down tree.
Image
User avatar
LostJustice
 
Posts: 677
Joined: Sun Mar 25, 2012 3:57 am

Re: Prime - Amber tweaks and Scripting API

Postby slipper » Tue Dec 20, 2016 1:02 am

LostJustice wrote:Also I noticed another bug with the client that has to do with Game.getMapObjects.

If you do var list = Game.getMapObjects();

Then chop down a tree.

Do var list2 = Game.getMapObjects();

Then list == list2 is true. This should be false because you chopped down a tree which is an object hence the lists should be different but they are the same meaning the getMapObjects(); is not updating the chopped down tree.

Perhaps the stump retains the same ID?
User avatar
slipper
 
Posts: 816
Joined: Sat Oct 02, 2010 4:28 pm

Re: Prime - Amber tweaks and Scripting API

Postby lacucaracha » Tue Dec 20, 2016 1:39 am

LostJustice wrote:Also I noticed another bug with the client that has to do with Game.getMapObjects.

If you do var list = Game.getMapObjects();

Then chop down a tree.

Do var list2 = Game.getMapObjects();

Then list == list2 is true. This should be false because you chopped down a tree which is an object hence the lists should be different but they are the same meaning the getMapObjects(); is not updating the chopped down tree.

What slipper said. Its not a bug in the client, the object id just doesnt change. But thats weird because even tho the elements may be equal, the array itself isnt equal. Maybe its some JS 'feature'
User avatar
lacucaracha
 
Posts: 171
Joined: Thu Dec 05, 2013 4:50 pm

Re: Prime - Amber tweaks and Scripting API

Postby LostJustice » Tue Dec 20, 2016 2:25 am

slipper wrote:
LostJustice wrote:Also I noticed another bug with the client that has to do with Game.getMapObjects.

If you do var list = Game.getMapObjects();

Then chop down a tree.

Do var list2 = Game.getMapObjects();

Then list == list2 is true. This should be false because you chopped down a tree which is an object hence the lists should be different but they are the same meaning the getMapObjects(); is not updating the chopped down tree.

Perhaps the stump retains the same ID?


It still should not be equal since the there are now logs too. I am thinking that the object list is obtained on map section load and not updated. But you may be correct that the object retains the same id.
Last edited by LostJustice on Tue Dec 20, 2016 2:28 am, edited 1 time in total.
Image
User avatar
LostJustice
 
Posts: 677
Joined: Sun Mar 25, 2012 3:57 am

Re: Prime - Amber tweaks and Scripting API

Postby LostJustice » Tue Dec 20, 2016 2:26 am

lacucaracha wrote:
LostJustice wrote:Also I noticed another bug with the client that has to do with Game.getMapObjects.

If you do var list = Game.getMapObjects();

Then chop down a tree.

Do var list2 = Game.getMapObjects();

Then list == list2 is true. This should be false because you chopped down a tree which is an object hence the lists should be different but they are the same meaning the getMapObjects(); is not updating the chopped down tree.

What slipper said. Its not a bug in the client, the object id just doesnt change. But thats weird because even tho the elements may be equal, the array itself isnt equal. Maybe its some JS 'feature'


If array is equal to each other, by definition, then every element of one array is equal to every element of another array and the arrays are the same size. So it still should not be returning true.
Image
User avatar
LostJustice
 
Posts: 677
Joined: Sun Mar 25, 2012 3:57 am

PreviousNext

Return to The Wizards' Tower

Who is online

Users browsing this forum: Claude [Bot] and 313 guests