Scrifen -- Hafen scripting API

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

Re: Scrifen -- Hafen scripting API

Postby b0r3d0m » Thu May 12, 2016 2:21 pm

lacucaracha wrote:Suggestion:
getInventorySize();
(maybe)
getInventoryEmptySlots();

Added. Please see the updated client with the following new functions:

Code: Select all
// - getCharInvSize()
//   Returns an object with the `width` and `height` properties set to the appropriate cells count or null if there was an error
//
// - getCharInvCellsUsageMatrix()
//   Returns a 2D array with 0 and 1 values representing free and used cells correspondingly or null if there was an error
//   You can access specific cell via the Cartesian coordinate system syntax where (0, 0) is the top-left cell
//
// - getFreeCharInvCellsCount()
//   Returns a count of free cells in the character's inventory or -1 if there was an error
User avatar
b0r3d0m
 
Posts: 95
Joined: Sun Sep 13, 2015 2:39 pm

Re: Scrifen -- Hafen scripting API

Postby b0r3d0m » Thu May 12, 2016 2:24 pm

Thedrah wrote:you're amazing. currently attempting to add a script to automate walking around my base then to initiate other scripts/functions to make actions happen. then i can consider it a bot :twisted:
i got a carrot and barley harvesting script, now to add planting. then other plants and possibly work orders. the onMapObjectSelect function makes things go much smoother/quicker. with the animal stats i can make a true farmhand. if we can get stats of an item from inventory/window i'll be able to do automated farming with inspecting

only things holding me up are; i can't seem to carry a barrel through mineholes/ladders or travel along a milestone using g.mapObjectRightClick(id). i have to use buckets atm, which i guess is just an efficiency problem.
and entering buildings seems to escape me, is there a certain name i'm missing? trying to enter a stonetower by g.mapObjectRightClick(g.getMapObjects('stonetower')[0].id); which gets a proper id but it appears not to be selecting the door

convenience features would be to have a way to get liters from a foodtrough. that way i could log consumption of animals
the coordinates seem to not be static so i've started to rely on objects as land markers, would it be possible to get either the res of a tile or what is on a display sign? such as to allow for a script to fill an area paved of schist with wood blocks

another feature i could think of would be to repair stuff such as walls, looms, or plows. maybe a function such as getObjectHealth
for a mining script i think i need a function for ondustfall(x, y) unless i plan on throwing tons of metal at it. and maybe res tiles of walls
after that a scout script would need to be able to determine res tiles on the ground to be able to traverse rivers imo.

any chance of getting combat functions? such as a current list of combat cards and ability to activate one. i should really read into how to compile a custom client haha just seems like a cluster fuck to get started

edit: i want to use a cart but i can only pull the cart with the script. could we get the parts of the object, maybe in array form? or something more simple? could possibly allow for a more efficient script for changing hides, knowing if leather is done by the water, and possibly entering houses. might allow for knowing if a cupboard has stuff in it or a table have symbel on it

edit 2: trying to make a script to grind barley flour, i can't seem to be able figure out how to get barley seed from a barrel or equip a bucket of seed from hand or a container. can't equip an item from hand or container. i can't seem to open a chest next to a quern either :x
still can make a grinding script but requires no barley seed in barrel/bucket

Wow, that's a huge list :)

Will think about adding all that stuff. Stay in touch :)
User avatar
b0r3d0m
 
Posts: 95
Joined: Sun Sep 13, 2015 2:39 pm

Re: Scrifen -- Hafen scripting API

Postby Thedrah » Thu May 12, 2016 5:32 pm

b0r3d0m wrote:
Thedrah wrote:stuff

Wow, that's a huge list :)

Will think about adding all that stuff. Stay in touch :)


sorry :X

my wishlist in order is: left click ground, interact with object controlled stuff (buildings, carts and walls), animal info, trough info, repair, equip from hand, dust falling

any kind of script you would want as thanks? haha
i got a simple harvest plants and a change drying frame script done so far. i plan to do more as to make up for being a hermit with less freetime. if i knew how to use apache ant i would try to make my own or add to the basic functions
  ▲
▲ ▲
Thedrah
 
Posts: 936
Joined: Fri Apr 08, 2011 2:20 am
Location: behind you

Re: Scrifen -- Hafen scripting API

Postby dzielny_wojownik » Thu May 12, 2016 9:10 pm

anyone willing to release their cool scripts haha? :oops:
There was a wise soviet conscript that was surrounded by the enemy on the battle of Stalingrad. A mortar shot teared his pants apart causing a big hole in his pants right on his right butt cheek. He felt a glorious cold ass breeze of wind on his butt cheek, like it would be sent by stalin himself, he said - hide your butts
User avatar
dzielny_wojownik
 
Posts: 703
Joined: Thu Oct 02, 2014 10:38 am
Location: Trapped in the autism cage with William

Re: Scrifen -- Hafen scripting API

Postby mdsanta » Fri May 13, 2016 3:03 am

I went through your example you posted.
Tremendous work man!
I didn't try to use it yet, but I love it already sooo much.

Few questions if you don't mind, i'm not overly familiar with JS :(

- As I understand your script is event based?
So all lasting action sequences should be dont with intervals to check of fired events to react on them
- onCurioFound is fired only on curios or on any foundable object? like wild grass, clover, ancient tree trunks etc...
- is there an API to see what is currently in study? out of curiosity, based on what script is doing - it's better to check of dandelion is already in study :)
- is it possible to add detection the there is no active process? (no hourglass)

P.S. it would make sense to extend function goTo(x, y) to goToObject(id). Fairly obvios refactoring as you start approaching objects to do actions. Saves extracting coordinates out.
And since coords object is being passed all other the events - it makes sense to cut the corner as well and add goTo(coords)
User avatar
mdsanta
 
Posts: 99
Joined: Mon May 09, 2016 8:36 pm

Re: Scrifen -- Hafen scripting API

Postby Glorthan » Fri May 13, 2016 5:14 am

mdsanta wrote:is it possible to add detection the there is no active process? (no hourglass)

see waitForTaskToFinish.

P.S. it would make sense to extend function goTo(x, y) to goToObject(id). Fairly obvios refactoring as you start approaching objects to do actions. Saves extracting coordinates out.
And since coords object is being passed all other the events - it makes sense to cut the corner as well and add goTo(coords)

You can define these yourself in a couple of seconds if you want. Adding them by default would just make reading the documentation harder.

A carrot/beet harvest and replanting bot:
Functionality:
  • Drops surplus crops on the ground
  • Drinks when stamina is low
  • Eats crops when energy is low
  • waitLag function is dependant on your latency and fps, you can reduce it if you have a nice ping to the server :)

Still to be done:
  • Refill drinking items from nearby water barrels
  • Replant highest quality crops in inv, drop lowest
  • Stockpile crops or place in feed troughs instead of dropping
  • Use the new inventory size functionality to figure out how many crops to store
  • Make it work for seed crops too

Code: Select all
function onGameLoaded(game) {
  print('Game loaded');

  g = game;
 
  g.setSpeed(2); // run
 
  harvestAll('carrot', 5.0);
}

function harvestAll(crop, stage) {
  do {
    g.dropItemFromHandToWindow('Inventory'); // make sure we're not holding a crop
   
    if(!drinkCheck(50, crop)) {
      // TODO: fill water bottle from barrel
    }
   
    // fuckken filter didn't work QQ
    var crops = g.getMapObjectsByFullName('gfx/terobjs/plants/' + crop);
    var tldr = [];
   
    for(var i = 0; i < crops.length; i++) {
      if(g.getGrowthStage(crops[i].id) >= (stage - 0.1)) { // fp rounding
        tldr.push(crops[i]);
      }
    }
   
    crops = tldr;
     
    var closest = getClosestOf(crops);
   
    g.mapObjectRightClick(closest.id);
    waitLag();
    if(!g.chooseFlowerMenuOption('Harvest')) {
      print('harvest failed.');
    }
   
    waitLag();
    g.waitForPf(); // wait to walk to crop
    g.waitForTaskToFinish(); // wait to plant crop
       
    // plant crop
    waitLag();
    g.takeItem(crop); 
    waitLag();
    g.useItemFromHandOnCoords(g.playerCoords.x, g.playerCoords.y);
    waitLag();
   
    // too many crops
    var inv = g.getInvItems();
    var count = 0;
    inv.forEach(function(i) {
      if(i == crop) {
        count++;
      }
    });
   
    while(count > 10) { // change with new inventory functionality
      g.dropItem(crop);
      waitLag();
      count--;
    }
   
  } while(crops.length > 0); // while there is at least one harvestable crop
}

function getClosestOf(items) { 
   var pos = g.getPlayerCoords();
   var curItem = null;
   var leastDif = 9999999, curDif;
 
   items.forEach(function(item) {
      curDif = Math.abs(item.coords.x - pos.x) + Math.abs(item.coords.y - pos.y);
   
    if(leastDif > curDif) {
      leastDif = curDif;
    curItem = item;
    }
   });
   
   return curItem;
}

function drinkCheck(threshold, food) {
  food = food || null; // if no food parameter supplied make it null
 
  if(g.getStamina() > threshold) { // we have enough anyway
    return true;
  }
 
  if(g.getEnergy() < 30) {
    if(food != null)
    {
      eatFood(food, 50);
    }
   
    if(g.getEnergy() < 35) {
      print('no food to eat');
      g.logout;
    }
  }
 
  g.drink();
  waitLag();
 
  return (g.getEnergy() > 90);
}

function eatFood(food, level) {
  var success = false;
  do {
    success = eat(food);
  } while(g.getEnergy() < level && success)
}

function waitLag() {
  sleep(500);
}
Glorthan
 
Posts: 1099
Joined: Tue Jun 11, 2013 4:33 pm

Re: Scrifen -- Hafen scripting API

Postby b0r3d0m » Fri May 13, 2016 9:11 am

mdsanta wrote:I went through your example you posted.
Tremendous work man!
I didn't try to use it yet, but I love it already sooo much

Thanks!

mdsanta wrote:As I understand your script is event based?

Yep.

mdsanta wrote:So all lasting action sequences should be dont with intervals to check of fired events to react on them

Yes.

mdsanta wrote:onCurioFound is fired only on curios or on any foundable object? like wild grass, clover, ancient tree trunks etc...

Curios only.

mdsanta wrote:is there an API to see what is currently in study?

Nope.

mdsanta wrote:out of curiosity, based on what script is doing - it's better to check of dandelion is already in study :)

`studyCurio` function will do it for you

mdsanta wrote:is it possible to add detection the there is no active process? (no hourglass)

You can use `waitForTaskToFinish` function

mdsanta wrote:P.S. it would make sense to extend function goTo(x, y) to goToObject(id). Fairly obvios refactoring as you start approaching objects to do actions. Saves extracting coordinates out.
And since coords object is being passed all other the events - it makes sense to cut the corner as well and add goTo(coords)


You can define these yourself in a couple of seconds if you want. Adding them by default would just make reading the documentation harder

This.
User avatar
b0r3d0m
 
Posts: 95
Joined: Sun Sep 13, 2015 2:39 pm

Re: Scrifen -- Hafen scripting API

Postby b0r3d0m » Fri May 13, 2016 9:18 am

Glorthan wrote:A carrot/beet harvest and replanting bot

Thanks for script sharing!

Few notes:

  • You forgot to add parenthesses to the `logout` function call
  • Note that `logout` function switches to the character select screen and `onCharSelect` function will be called again

Also could you post the full `script.js` file? It'll be cool to post it in the first post of this thread (with your credits, ofc).
User avatar
b0r3d0m
 
Posts: 95
Joined: Sun Sep 13, 2015 2:39 pm

Re: Scrifen -- Hafen scripting API

Postby lacucaracha » Fri May 13, 2016 4:41 pm

Is there a way to create a new Thread(on the script)? I'm having trouble doing it :(
User avatar
lacucaracha
 
Posts: 171
Joined: Thu Dec 05, 2013 4:50 pm

Re: Scrifen -- Hafen scripting API

Postby Thedrah » Fri May 13, 2016 5:20 pm

lacucaracha wrote:Is there a way to create a new Thread(on the script)? I'm having trouble doing it :(


i copied and modified the autowalk function in the example to get something that's ran constant on an interval. i'm thinking about using it as a curio script that could be ran in the background but haven't gotten to it yet
  ▲
▲ ▲
Thedrah
 
Posts: 936
Joined: Fri Apr 08, 2011 2:20 am
Location: behind you

PreviousNext

Return to The Wizards' Tower

Who is online

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