eventMacro

From OpenKore Wiki
Revision as of 05:35, 2 March 2017 by Henrybkgg (talk | contribs)
Jump to navigation Jump to search

Description

  • With this plugin you can create complex blocks of conditions called automacros and blocks of consecutive actions called macros.
  • Macros can be called by one or more automacros when their conditions become true or be called by hand using a console command.
  • Automacros have simple to use conditions like base level, zeny, job class and many others.
  • Macros can be as simple as 'say something in chat' or 'save in kafra' and as complex as 'do a complete quest'.
  • This page was last updated with the current version at the moment being 324.

Installation

  • Go to your OpenKore main folder (the folder which contains the file openkore.pl) and create a subfolder called plugins, if there isn't already one. Create a subfolder eventMacro in it.
  • Inside the eventMacro plugin's zipfile (normally eventMacro-master.zip), you will find the file eventMacro.pl, the folder eventMacro and other files and folders. Extract them to your plugins/eventMacro folder, so eventMacro.pl ends up in plugins/eventMacro/eventMacro.pl.
  • In your OpenKore control folder, create a blank file named eventMacros.txt. In this file you will put your macros/automacros.


After installation, your OpenKore file tree should look like this (ignoring OpenKore's own files):

openkore
|-- openkore.pl
|-- control
|   |-- eventMacros.txt
|-- fields
|-- logs
|-- plugins
|   |-- eventMacro
|   |   |-- eventMacro
|   |   |   |-- Automacro.pm
|   |   |   |-- Condition.pm
|   |   |   |-- Core.pm
|   |   |   |-- Data.pm
|   |   |   |-- FileParser.pm
|   |   |   |-- Lists.pm
|   |   |   |-- Macro.pm
|   |   |   |-- Runner.pm
|   |   |   |-- Utilities.pm
|   |   |   |-- Validator.pm
|   |   |   |-- Validator
|   |   |   |   |-- ListMemberCheck.pm
|   |   |   |   |-- NumericComparison.pm
|   |   |   |   |-- RegexCheck.pm
|   |   |   |-- Conditiontypes
|   |   |   |   |-- ListConditionEvent.pm
|   |   |   |   |-- ListConditionState.pm
|   |   |   |   |-- MultipleValidatorEvent.pm
|   |   |   |   |-- MultipleValidatorState.pm
|   |   |   |   |-- NumericConditionEvent.pm
|   |   |   |   |-- RegexConditionEvent.pm
|   |   |   |   |-- RegexConditionState.pm
|   |   |   |   |-- SimpleEvent.pm
|   |   |   |-- Condition
|   |   |   |   |-- AttackEnd.pm
|   |   |   |   |-- AttackStart.pm
|   |   |   |   |-- AttackStartRegex.pm
|   |   |   |   |-- BaseLevel.pm
|   |   |   |   |-- CartCurrentSize.pm
|   |   |   |   |-- CartCurrentWeight.pm
|   |   |   |   |-- CartMaxSize.pm
|   |   |   |   |-- More conditions...
|   |   |-- test
|   |   |   |-- unit_tests.pl
|   |   |   |-- RunnerStatementTest.pm
|   |   |   |-- RunnerParseCommandTest.pm
|   |   |   |-- LoadConditionsTest.pm
|   |   |   |-- GlobalVarRegexTest.pm
|   |   |   |-- FindVarTest.pm
|   |   |   |-- DynamicAutoVarsTest.pm
|   |   |   |-- CoreVarFunctionsTest.pm
|   |   |   |-- Validator
|   |   |   |   |-- ListMemberCheckTest.pm
|   |   |   |   |-- NumericComparisonTest.pm
|   |   |   |   |-- RegexCheckTest.pm
|   |   |   |-- textfiles
|   |   |   |   |-- DynamicAutoVarsTest.txt
|   |   |   |   |-- DynamicAutoVarsTest2.txt
|   |   |   |   |-- DynamicAutoVarsTest3.txt
|   |   |   |   |-- empty.txt
|   |   |   |   |-- LoadConditionsTest.txt
|   |   |-- eventMacro.pl
|-- src
|-- tables

Console Commands

Syntax

eventMacro [MACRO|auto|list|status|check|stop|pause|unpause|var_get|var_set|enable|disable] [extra]
eventMacro MACRO
  • Runs macro MACRO.
Option Value Description
-repeat n repeat the macro n times
-overrideAI none override openkore's AI
-macro_delay delay override global macro_delay for this macro
-exclusive none do not allow automacros to cancel this macro
-orphan method use method for handling orphaned macros


Parameters for the macro can be specified after a double dash (--). These parameters are saved to the variables $.param1 to $.paramN. Example:

macro foo {
 log Parameter 1 is $.param1
 log Parameter 2 is $.param2
}


When called as eventMacro foo -- foo bar it would print out

[eventmacro log] Parameter 1 is foo
[eventmacro log] Parameter 2 is bar
eventMacro auto AUTOMACRO
Get info on an automacro and it's conditions
eventMacro list
Lists available macros and automacros
eventMacro status [macro|automacro]
Shows current status of automacro, macro or both
eventMacro check [force_stop|force_start|resume]
Sets the state of automacros checking
eventMacro stop
Stops current running macro
eventMacro pause
Pauses current running macro
eventMacro unpause
Unpauses current running macro
eventMacro var_get
Shows the value of one or all variables
eventMacro var_set
Set the value of a variable
eventMacro enable [automacro]
Enable one or all automacros
eventMacro disable [automacro]
Disable one or all automacros


Configuration files

control/eventMacros.txt
Put your macros and automacros in here. You can change the file's name depending on what you configured on config eventMacro_file.
control/timeouts.txt
Add eventMacro_delay and set it to the number of seconds you want the plugin to pause between commands.
control/config.txt
Option Value Default Description
eventMacro_orphans terminate
reregister
reregister_safe
terminate_last_call
terminate How will openkore deal with macros that got cleanead out of AI queue
eventMacro_file file name eventMacros.txt file containing the macros and automacros
eventMacro_CheckOnAI combination of 'auto', 'manual' and 'off' auto In which AI states automacros will be checked

Macro Syntax

macro MacroName {
   do this..
   and that..
   yattayatta..
}
  • You can use any name you want for your macro. Be careful not to make two macros with the same name.
  • Macros is executed from top to bottom.
  • Only macro instructions can be used in macros. If you need to use console command, use do.

Macro Instructions

do <command>
Run <command>, as if it was entered in OpenKore terminal. Commands are from Console Commands.
macro foo {
   do move 123 234 prontera
   do sit
   do c hello world
}

The command ai clear is disabled by default in the plugin.
If has a macro the command do ai manual or do ai off , the macro will stop its execution.


log <text>
Prints a text in the console. Can contain macro $variables and @stuff.
macro foo {
  log This line logs a text to console.
  log All your base are belong to us!
}


pause [<n>]
Pauses the macro for n seconds.
macro foo {
   log It's 10:00:00
   pause 10
   log Now it's 10:00:10 
   log 10 seconds have passed after the first print.
}

pause not only pauses the macro running, pauses all our character's actions.


call <macroname> [<n>]
Calls macro <macroname> [<n> times]. When <macroname> is finished the current macro continues.


release (<name> | all)
Reenables a locked automacro ("run-once" keyword or locked by "lock") or reenables all automacros when using release all.


lock (<name> | all)
Locks an automacro and disables it's checks. To lock all automacros, use lock all.


stop
Immediately terminates the running macro.


set <option> <value>
Sets macro features:
  • orphan method
  • macro_delay timeout
  • overrideAI [0|1]
  • repeat times
  • exclusive [0|1]

Variables

  • You can work with variables. Variable declaration is not needed. All macro variables are global.
  • EventMacro variable system is based in perl and has support for scalars, arrays and hashes.
  • Variable names may only contain letters and numbers.
  • If you want to use any variable without replacing it for it's value you should escape it with \.

Types of variables and their definitions

Scalar
  • Scalar variables can hold a single piece of information, like a number or a string of characters.
  • Scalars start with the symbol $.
Array
  • Array variables hold a list of things.
  • Each member inside the array list has a index, which is it's position inside the list.
  • The first index of an array is always 0, and the last is always the size of the array minus 1.
  • Arrays start with the symbol @.
Hash
  • Hash variables hold many pairs of things.
  • Each hash pair in consisted of a key and a value.
  • Each key has only one value, but many keys can have the same value.
  • Hashes start with the symbol %.

Declaration and usage

Scalars

Set value of scalar variable:

$variable = value

Get a value of variable (note that this is "Macro Syntax" section, so it doesn't apply to automacro conditions, config options etc):

$variable
macro Hello {
   $var = Hello
   $var1 = World!
   log $var $var1
}

This would print in the console:

[eventmacro log] Hello World!

It is possible to increment and to decrement a variable using $variable++ or $variable--.

macro Counter {
    $counter = 0
    log Counter is at $counter
    $counter++
    log Now it's $counter
    $counter--
    log It's back to $counter
}

The result is:

[eventmacro log] Counter is at 0
[eventmacro log] Now it's 1
[eventmacro log] It's back to 0

You can also unset/vanish the existing variable using 'undef' or 'unset':

$x = 1
log \$x is $x
$x = undef  # or you can use 'unset'
log \$x now vanished: $x

Variable assignment doesn't evaluate, it only does macro substitutions, so use @eval for calculations.

macro math {
    $num = 2
    $num2 = 3
    $result = @eval($num+$num2)
    log sum of $num and $num2 is $result
}

The result is:

[eventmacro log] sum of 2 and 3 is 5.
Arrays

Set value of array variable, it must be declared with it's members inside parentheses, separated by comma:

@variable = (member0, member1, member2)

Get a value of a certain index, use $ at the start instead of @, and put the index after the variable name, encapsulated by [ and ]:

$variable[index]

Get a size of the variable:

@variable
macro Hello {
   @var = (drops, poring)
   log The size of \@var is @var, the first member is $var[0] and the second is $var[1]
}

This would print in the console:

[eventmacro log] The size of @var is 2, the first member is drops and the second is poring
  • The definition of separate array members is the same as normal scalar.

They can be set:

$myarray[0] = harry
$myarray[1] = potter

They can be incremented and decremented:

macro Counter {
    $counterArray[1] = 0
    log \$counterArray[1] is at $counterArray[1]
    $counterArray[1]++
    log Now it's $counterArray[1]
    $counterArray[1]--
    log It's back to $counterArray[1]
}

And they can be unset:

$var[5] = 1
log \$var[5] is $var[5]
$var[5] = undef  # or you can use 'unset'
log \$var[5] now vanished: $var[5]
Array Keywords
There are 4 keywords which can be used on arrays:
  • push: Adds a new element to the end fo the array, increasing it's size by 1.
  • pop: Removes the last element from the end of the array, decreasing it's size by 1.
  • shift: Removes the first element of the array and moves the whole array 1 index to the left, decreasing it's size by 1.
  • unshift: Adds a new element to the start of the array and moves the whole array 1 index to the right, increasing it's size by 1.


push

push syntax is as follows:

&push(@array, newmember)

push must have 2 parameters: the array you want to push, and the new member to be pushed.

Here's an example of an array started from scratch using push:

&push(@names, john)
&push(@names, mary)
&push(@names, juan)

This is the array afterwars:

(john, mary, juan)

And this macro:

macro print_names {
    &push(@monsters, poring)
    &push(@monsters, wolf)
    &push(@monsters, maya)
    log The array \@monsters has size @monsters and it's members are $monsters[0], $monsters[1] and $monsters[2]
}

Prints this:

[eventmacro log] The array @monsters has size 3 and it's members are poring, wolf and maya


pop

pop syntax is as follows:

&pop(@array)

pop must have 1 parameter: the array you want to pop.

Here's an example of an array, and it after a pop:

@jobs = (teacher, doctor, seller)
log The array \@jobs has size @jobs and it's members are $jobs[0], $jobs[1] and $jobs[2]
&pop(@jobs)
log The array \@jobs has size @jobs and it's members are $jobs[0], $jobs[1]
&pop(@jobs)
log The array \@jobs has size @jobs and it's members are $jobs[0]


This prints this:

[eventmacro log] The array @jobs has size 3 and it's members are teacher, doctor and seller
[eventmacro log] The array @jobs has size 2 and it's members are teacher, doctor
[eventmacro log] The array @jobs has size 1 and it's members are teacher


shift

shift syntax is as follows:

&shift(@array)

shift must have 1 parameter: the array you want to shift. Here's an example of an array, and it after a shift:

@jobs = (teacher, doctor, seller)
log The array \@jobs has size @jobs and it's members are $jobs[0], $jobs[1] and $jobs[2]
&shift(@jobs)
log The array \@jobs has size @jobs and it's members are $jobs[0], $jobs[1]
&shift(@jobs)
log The array \@jobs has size @jobs and it's members are $jobs[0]


This prints this:

[eventmacro log] The array @jobs has size 3 and it's members are teacher, doctor and seller
[eventmacro log] The array @jobs has size 2 and it's members are doctor, seller
[eventmacro log] The array @jobs has size 1 and it's members are seller


After a shift the whole array is moved to the left, so after the first shift the member 'seller' which was in index 2 moved to index 1, and after the second shift it moved to index 0.


unshift

unshift syntax is as follows:

&unshift(@array, newmember)

push must have 2 parameters: the array you want to push, and the new member to be pushed.

Here's an example of an array started from scratch using push:

&unshift(@foods, bread)
&unshift(@foods, carrot)
&unshift(@foods, apple)

This is the array afterwars:

(apple, carrot, bread)

Note that since unshift puts the new member in the start of the array the first member is now 'apple' which was added last.

And this macro:

macro print_names {
    @array = (apple, banana)
    log The array \@array has size @array and it's first member is $array[0], and the second is $array[1]
    &unshift(@array, pineapple)
    log The array \@array has size @array and it's first member is $array[0], and the second is $array[1]
    &unshift(@array, melon)
    log The array \@array has size @array and it's first member is $array[0], and the second is $array[1]
}

Prints this:

[eventmacro log] The array @array has size 2 and it's first member is apple, and the second is banana
[eventmacro log] The array @array has size 3 and it's first member is pineapple, and the second is apple
[eventmacro log] The array @array has size 4 and it's first member is melon, and the second is pineapple
Hashes

Set value of hash variable, it must be declared with it's pairs inside parentheses, separated by comma, the key and the value must be separated by =>:

%variable = (key1 => value1, key2 => value2, key3 => value3)

Get a value of a certain key, use $ at the start instead of %, and put the key after the variable name, encapsulated by { and }:

$variable{key1}
  • Keys can contain letters and numbers.

Get a size of the variable:

%variable
macro Hello {
   %ages = (john => 25, george => 32)
   log John is $ages{john} years old, and George is $ages{george} years old
}

This would print in the console:

[eventmacro log] log John is 25 years old, and George is 32 years old
  • The definition of separate hash members is the same as normal scalar.

They can be set:

$name{first} = harry
$name{last} = potter

They can be incremented and decremented:

macro Counter {
    $hash{monterskilled} = 0
    log \$hash{monterskilled} is at $hash{monterskilled}
    $hash{monterskilled}++
    log Now it's $hash{monterskilled}
    $hash{monterskilled}--
    log It's back to $hash{monterskilled}
}

And they can be unset:

$myhash{value} = 1
log \$myhash{value} is $myhash{value}
$myhash{value} = undef  # or you can use 'unset'
log \$myhash{value} now vanished: $myhash{value}


There's one hash keyword, delete, it completely delete a key from a hash, as if it had never existed.

delete usage:

&delete($names{john})

example:

macro print_names {
    %fruitsprice = (apple => 1000, banana => 700)
    log The hash \%fruitsprice has size %fruitsprice.
    log The apple price is $fruitsprice{apple} and the banana price is $fruitsprice{banana}
    &delete($fruitsprice{apple})
    log The hash \%fruitsprice has size %fruitsprice.
    log The price of banana is: $fruitsprice{banana}
    log The price of apple has vanished: $fruitsprice{apple}
}

Prints this:

[eventmacro log] The hash \%fruitsprice has size 2.
[eventmacro log] The apple price is 1000 and the banana price is 700
[eventmacro log] The hash \%fruitsprice has size 1.
[eventmacro log] The price of banana is 700
[eventmacro log] The price of apple has vanished:


Special Variables

There are special read-only variables which begin with a dot. They are pre-defined with the eventMacro plugin.

Variable Definition Example
$.map The map you're on prontera
$.pos Your current position 123 234
$.time Current time as unix timestamp 1482275422
$.datetime Current date and time Tue Dec 20 21:22:34 2016
$.hour Current hour time in 24h format 14
$.minute Current minute time (0 - 59) 34
$.second Current second time (0 - 59) 53
$.hp Current HP 2304
$.sp Current SP 345
$.lvl Current base level 175
$.joblvl Current job level 60
$.spirits Number of current spirit spheres or coin flip's coins 3
$.zeny Current zeny 8478341
$.status Current statuses in a comma-separated list Attention Concentrate,Endure,Two-Hand Quicken
$.paramN Command line parameters (see Syntax) N/A
$.caller Nome da última automacro ativada N/A
$.weight Current weight of the character 1948
$.maxweight Current maximum weight of the character 2400

Special Keywords

These keywords (having common form of &<keyword>(<arguments>) each) interpolated to corresponding values just about anywhere inside macro blocks (except goto, end, label definitions, macro name in call, set). Macro variables can be used in arguments, with the exception for &nick().

&npc (<x> <y> | /regexp/i | "<name>")
Return NPC's index which location is <x> <y> or NPC's name match regexp or NPC's name is equal to <name> . Returns -1 if no NPC was found.
&inventory (<item>)
Returns inventory item index of <item>. If <item> doesn't exist, it returns -1.
&Inventory (<item>)
Same as &inventory but returns all matching indexes as a comma-separated list or -1 if the item was not found.
&invamount (<item>)
Returns the amount of the given <item> in inventory.
&cart (<item>)
Returns cart item index of <item>. If <item> doesn't exist, it returns -1.
&Cart (<item>)
Same as &cart but returns all matching indexes as a comma-separated list or -1 if the item was not found.
&cartamount (<item>)
Returns the amount of the given <item> in cart.
&storage (<item>)
Returns storage item index of <item>. If <item> doesn't exist, it returns -1.
&Storage (<item>)
Same as &storage but returns all matching indexes as a comma-separated list or -1 if the item was not found.
&storamount (<item>)
Returns the amount of the given <item> in storage.
&player (<name>)
Returns player index of player <name>. If player <name> is not found, it returns -1.
&monster (<name|ID>)
Returns monster index of monster <name|ID>. If monster <name|ID> is not found, it returns -1.
&vender (<name>)
Returns vender index of vender <name>. If vender <name> is not found, it returns -1.
&store (<name>)
Looks for an item in a store and returns index or -1 if the item was not found.
&shopamount (<item>)
Returns the amount of the given <item> in shop.
&random ("<argument1>", "<argument2>", ...)
Returns randomly one of the given arguments .
&rand (<n>, <m>)
Returns a random number between (and including) <n> and <m>.
&eval (<argument>)
Evaluates the given <argument>. Contents of &eval is Perl and does NOT have eventMacro plugin syntax, except for variables and @() substitution.
&arg ("<argument>", <n>)
Returns the <n>th word of <argument> or an empty string if the word index is out of range.
&config (<variable>)
Returns the value of <variable> specified in config.txt.
&venderitem (<name>)
Looks for an item in a player's shop and returns index or -1 if the item was not found.
&venderprice (<indexID>)
Looks for an item in a player's shop and returns its price.
&nick (<word>)
Escapes all the regexp metacharacters and some of the perl special characters with \ (a backslash). Especially for player's name.

Chaining commands

You can run multiple commands one after another without having to wait for openkore's AI or macro_delay or whatever. Just enclose these commands with [ and ].

0 macro foo {
1  do whatever
2  log yet another line
3  [
4     do something
5     do something else
6     log foo
7  ]
8  log done
9 }

Line 3 starts the chaining mode. This line has no delay. Lines 4, 5 and 6 are run as soon as the previous command has finished with no delay and they cannot be interrupted. Line 7 stops the chaining mode and line 8 will be run $macro_delay seconds after that.

Sub-lines

Instead of using one command/var assignments per line, you can separate them using a semi-colon ";".

macro foo {
    $i = 1; pause 5; log \$i = $i; $ii = 2; $iii = 3; $i++; $ii--; lock automacroName; release automacroName; set overrideAI 1
}

Commands separated by a semi-colon ";" the normal macro_delay between them.

Operators

Supported operators in eventMacro plugin are:

Operator Description
< less than
<= less or equal to
== or = equal to (link)
> greater than
>= greater or equal to
!= not equal to
~ <left part> is element of <right part (comma-separated list)>
=~ <left part> matches regular expression <right part>

Further Discussion: Forums

arg .. arg2 Between arg and arg2. Where arg can be a number, a % or a variable.

Flow control and labels

  • Just as prevalent in high-level languages ​​constructs like "if .. else", "while", "foreach", "for .. next", "do .. while" and other commands.
  • eventMacro plugin also has the operations "if", "else", "elsif", "switch", "case", "goto" and "while". There's also label declaration.

Syntax of command conditions

If

Right now, macro if conditions are very close to the perl if statements. It accepts unlimited number of statements in just one if condition line, regexp matching is allowed (unfortunately, no backreference using parenthesis) and the use of && for and meaning and || for or meaning.

You can use a simple statement;

if (arg1 <Operators> arg2) (goto <label> | call <macro> [<n>] | stop | { )

A simple statement with OR condition;

if (arg1 <Operators> arg2 || arg3 <Operators> arg4) (goto <label> | call <macro> <n> | stop | { )

A simple statement with AND condition;

if (arg1 <Operators> arg2 && arg3 <Operators> arg4) (goto <label> | call <macro> <n> | stop | { )

Or with both;

if ((arg1 <Operators> arg2 || arg3 <Operators> arg4) && arg5 <Operators> arg6) (goto <label> | call <macro> <n> | stop | { )
if ((arg1 <Operators> arg2 && arg3 <Operators> arg4) || arg5 <Operators> arg6) (goto <label> | call <macro> <n> | stop | { )
if ((arg1 <Operators> arg2 && arg3 <Operators> arg4) || (arg5 <Operators> arg6 && arg7 <Operators> arg8)) (goto <label> | call <macro> <n> | stop | { )
if ((arg1 <Operators> arg2 || arg3 <Operators> arg4) && (arg5 <condition> arg6 || arg7 <condition> arg8)) (goto <label> | call <macro> <n> | stop | { )
  • Notice inside of each brackets containing the AND and OR symbols.

Where;

  • arg can be a variable, a nested variable, a special keyword, &eval, letters and numbers or even a Perl Subroutines function.
  • <label>, the name of an existing label, can only contain letters and numbers
  • <macro>, the name of a existing macro, and
  • <n>, the number of times it will call the macro.
  • < { >, the beginning of a block of commands that will be executed if the condition is true, to finish it, use }.


Note:

  • If <n> is defined as 0 or even if it is undefined, the called macro name will run at once and then stop, not continuing the previous macro caller at a run time.
  • If <n> is greater than 0, the called macro name will run at n time/s and then continuing the previous macro caller line at a run time.


Note²: If statements are unlimited, you can use as many statements as you want.

if (arg1 <Operators> arg2 || arg3 <Operators> arg4 || ... || argN <Operators> argN+1) (goto <label> | call <macro> <n> | stop)

Where;

  • arg can be a variable, a nested variable, a special keyword, &eval or letters and numbers.
  • All the conditions is up to the Nth argument <Operators> N+1th argument.
  • While N is an integer number/s which is greater than zero.
Postfix control

Another way to use the if is to put it at the end of the command.

<command> if (arg1 <Operators> arg2)

The command before the if statement will only be executed if the condition is true. The way to create the condition is the same as previously explained.

In this example below, the two commands are equivalent:

call buy if ($.zeny > 1000)
if ($.zeny > 1000) call buy

Else

In case if is used with open braces ( { ), it's possible to use the command else along closed braces ( } ) in the end of the command block if, to start a new command block that will be executed if if is false.

if (arg1 <Operators> arg2) {
   command1
   command2
   ...
   commandN
} else {
   command1
   command2
   ...
   commandN
}

In this case, the first command block will be executed if the condition turns out being true. If it's false, the second command block (preceding by else) will be executed.

Note: Inside braces you're allowed to use tons of commands.
Note: It is not compulsory to wear else.

Elsif

It is like the else + if. The elsif is in the same place else with the difference being added conditions to its command block is executed if its conditions are true.

if (arg1 <Operators> arg2) {
   command1
   command2
   ...
   comandoN
} elsif (arg1 <Operators> arg2) {
   command1
   command2
   ...
   commandN
} else {
   command1
   command2
   ...
   commandN
}

In this case, the first block of commands will be executed if the condition is true. If it is false, the second block of commands (preceded by the elsif) will be executed if its condition is true. Should also be false, the command blocks else to be executed.

Note: As in the else, inside braces of elsif you're allowed to use tons of commands.
Note: It is not compulsory to wear the else or elsif.

Switch/case

It's similar to if followed by enumerous elsif. It's useful to leave the code cleaner when the paramater being analyzed is the same, this way you don't need to repeat it.

switch (arg1) {
    case (<Operators> arg2) (goto <label> | call <macro> <n> | stop | {)
        (If you use "{", the commands should be used here and in a separated line that should be closed with the block "}")
    case (<Operators> arg2) (goto <label> | call <macro> <n> | stop | {)
        (If you use "{", the commands should be used here and in a separated line that should be closed with the block "}")
    ...
    else (goto <label> | call <macro> <n> | stop | {)
        (If you use "{", the commands should be used here and in a separated line that should be closed with the block "}")
}

Note: The use of else is optional. Note: In case that no case ends being true, else will be activate if it exists.

Examples

This macro will go a random walk

macro walk {
   $num = &rand(1, 4)
   if ($num == 1) {
       do c I will follow the path 1
       do north
   }
   if ($num == 2) {
       do c I will follow the path 2
       do south
   }
   if ($num == 3) {
       do c I will follow the path 3
       do east
   }
   if ($num == 4) {
       do c I will follow the path 4
       do west
   }
}

Simplified version of the above macro, using postfix control.

macro walk {
   $num = &rand(1, 4)
   do c I will follow the path $num
   do north if ($num == 1)
   do south if ($num == 2)
   do east  if ($num == 3)
   do west  if ($num == 4)
}

The following macro will tell if you've over 1.000z or 1.000z or less.

macro checkZeny {
   if ($.zeny > 1000) {
      do c I've over 1.000z! 
   } else {
      do c I've 1.000z or less...
   }
}

A little more complete than the above macro. Informa has more than 1.000z, has exactly 1.000z or has less than 1.000z with emoticons

macro checkZeny {
   if ($.zeny > 1000) {
      do c I've over 1.000z! 
      do e money
   } elsif ($.zeny == 1000) {
      do c I have exactly 1.000z.
      do e !
   } else {
      do c I've 1.000z or less...
      do e panic
   }
}

Similar to the above but with a different syntax

macro checkZeny {
    switch ($.zeny) {
        case (> 1000) {
            do c I've over 1.000z! 
            do e money
        }
        case (== 1000) {
            do c I have exactly 1.000z.
            do e !
        }
        else {
            do c I've 1.000z or less...
            do e panic
        }
    }
}

The below macro will print \$num is 1 if $num == 1, \$num is 2 if $num == 2, \$num is 3 if $num == 3.

macro checknum {
    $num = &rand(1, 3)
    if ($num == 1) goto one
    if ($num == 2) goto two
    if ($num == 3) goto three
    :one
    log \$num is 1
    stop
    :two
    log \$num is 2
    stop
    :three
    log \$num is 3
    stop
}

The above macro can be written using call instead of goto.

macro checknum {
    $num = &rand(1, 3)
    if ($num == 1) call one
    if ($num == 2) call two
    if ($num == 3) call three
}
macro one {
    log $num is 1
}
macro two {
    log $num is 2
}
macro three {
    log $num is 3
}


A more complicated macro

macro if {
 $i = 1
 log \$i = $i
 if (((($i = 1 || $i < 5 || $i ~ 0 .. 5) && &eval(&eval($i - 1) - &eval($i - 0)) = -1) && ($i != 2 && $i > 0 && &eval($i - 1) = 0) && ($i != 2 && $i > 0 && &eval($i - 1) = 0)) && $i = 1) goto somewhere
 if (($i = 1 || $i < 5 || $i ~ 0 .. 5) && ($i != "" && $i > 0 && &eval($i - 1) = 0)) goto somewhere
 if (&eval (&eval($i-1) - 1) != "") goto somewhere
 if ((($i = 1) || ($i < 5 && $i ~ 0 .. 5)) && ($i != "" && $i > 0 && &eval($i - 1) > 0)) goto somewhere
 log ko
 stop
:somewhere
 log OK
}

WHILE Loop

A while in macros means that certain commands will be ran while the defined conditions are met.

Syntax

while (arg <condition> arg) as <loop>
    do bla bla
    ...
end <loop>

Where;

  • arg can be a variable, a nested variable, a special keyword, &eval or letters and numbers. And
  • <loop> is the name of the loop. You can give any name you want.

Conditions can be found on Operators.

Examples

macro while {
    $i = 0
    while ($i < 10) as loop
    log \$i = $i
    $i++
    end loop
}

The result is

[eventmacro log] \$i = 0
[eventmacro log] \$i = 1
[eventmacro log] \$i = 2
[eventmacro log] \$i = 3
[eventmacro log] \$i = 4
[eventmacro log] \$i = 5
[eventmacro log] \$i = 6
[eventmacro log] \$i = 7
[eventmacro log] \$i = 8
[eventmacro log] \$i = 9

Automacros

At this point, you can define macros and call them, typing 'eventMacro <macro name>' in OpenKore's console input.

Automacro is an automatic trigger for calling your macros, just like blocks in config.txt are automatic triggers for certain AI actions.

Automacro block consists of:

  • One or more Parameters (call being the only required one).
  • One or more Conditions.

Automacro does not trigger if there is currently running macro in exclusive mode. Otherwise, automacro clears macro queue (which means that all currently running macros are stopped) before call.

The point of automacro is that you use them to check for certain conditions and call your macro when all conditions are fulfilled.

Parameters

  • Each parameter can only be used once.
  • Parameters define the way the automacro or the called macro will work.
  • Most parameters have a default value that will be used when it is not defined.
call
  • call is the only required parameter in every automacro.
  • call defines which macro the automacro will execute.
  • call can be a macro name or a macro block.

Example when using a macro name:

automacro <automacro name> {
    <automacro conditions and parameters (and only them)>
    call myMacro
}
macro myMacro {
    <macro instructions (and only them, as this is regular macro)>
    # for example:
    do move prontera
    do move payon
}

Example when using a macro block:

automacro <automacro name> {
    <automacro conditions and parameters (and only them)>
    call {
        <macro instructions (and only them, as this is regular macro)>
        # for example:
        do move prontera
        do move payon
    }
}


  • Two examples above do the same thing.
  • Note that the macro block used in the call parameter has the same rules as a normal macro block.


The first syntax is useful if you want more than one automacro to call one simple macro:

automacro First {
    <conditions> 
    call print
}
automacro Second {
    <conditions> 
    call print
}
macro print {
    log $.caller triggered
}


delay
  • delay is an optional parameter.
  • delay defines the time in seconds to wait before executing the macro in call parameter.
  • If not used in the automacro the default value will be used, which is: 0.
  • Must have a numeric value.
automacro <automacro name> {
    <automacro conditions and parameters (and only them)>
    delay 5
    call myMacro
}
macro myMacro {
    <macro instructions (and only them, as this is regular macro)>
    log This is being logged 5 seconds after the automacro activated
}
run-once
  • run-once is an optional parameter.
  • run-once defines if the automacro can activate only once, or multiple times.
  • If not used in the automacro the default value will be used, which is: 0.
  • When it's value is 1 the automacro will only activate once, when it's 0 it can activate multiple times.
  • To make an automacro with run-once value of 1 which already was already activated be able to run again you can use the command 'release' Macro Syntax.
automacro myautomacro {
    <automacro conditions and parameters (and only them)>
    run-once 1
    call myMacro
}
automacro myaotherauto {
    <automacro conditions and parameters (and only them)>
    run-once 0
    call myMacro
}
macro myMacro {
    <macro instructions (and only them, as this is regular macro)>
    log This macro will be called by myautomacro only once
    log but this macro can be called by myaotherauto many times
}
CheckOnAI
  • CheckOnAI is an optional parameter.
  • CheckOnAI defines in which AI states the automacro can activate.
  • The default value for this parameter can be set in the config key eventMacro_CheckOnAI Configuration files.
  • If not used in the automacro the default value will be used, which is the config key eventMacro_CheckOnAI.
  • If eventMacro_CheckOnAI is not set in the config the default value will be used, which is: auto
  • It's values can be a combination of auto, manual and off, separated by comma.
automacro MyAuto {
    <automacro conditions and parameters (and only them)>
    CheckOnAI manual
    call myMacro
}
macro myMacro {
    <macro instructions (and only them, as this is regular macro)>
    log This macro will only be called by MyAuto when AI is manual
}

automacro MyAuto2 {
    <automacro conditions and parameters (and only them)>
    CheckOnAI manual, off
    call myMacro
}
macro myMacro2 {
    <macro instructions (and only them, as this is regular macro)>
    log This macro will only be called by MyAuto2 when AI is manual or off
}
disabled
  • disabled is an optional parameter.
  • disabled defines if the automacro is active of not.
  • If not used in the automacro the default value will be used, which is: 0.
  • When it's value is 1 the automacro won't activate ever, when it's 0 it can activate normally.
automacro MyAuto {
    <automacro conditions and parameters (and only them)>
    disabled 1
    call myMacro
}
macro myMacro {
    <macro instructions (and only them, as this is regular macro)>
    log This macro will never be called by MyAuto
}

automacro MyAuto2 {
    <automacro conditions and parameters (and only them)>
    disabled 0
    call myMacro
}
macro myMacro2 {
    <macro instructions (and only them, as this is regular macro)>
    log This macro will be called by MyAuto2 normally
}
overrideAI
  • overrideAI is an optional parameter.
  • overrideAI defines if eventMacro will put itself in the AI queue.
  • If not used in the automacro the default value will be used, which is: 0.
  • When it's value is 0 the macro will be put into AI queue as 'eventMacro' and the next macro command will only be run if current AI is 'eventMacro'.
  • When it's value is 1 the macro will not be put into AI queue and the next macro command will always be run.
  • It's important to note that when it's value is 1 since it won't be put into AI queue normal AI functions will continue as normal, like mob hunting or autostorage.
automacro MyAuto {
    <automacro conditions and parameters (and only them)>
    overrideAI 0
    call myMacro
}
macro myMacro {
    <macro instructions (and only them, as this is regular macro)>
    do move prontera
    log This will only be printed when the 'move prontera' has been completed, since 'move' takes the first place in AI queue.
}

automacro MyAuto2 {
    <automacro conditions and parameters (and only them)>
    overrideAI 1
    call myMacro
}
macro myMacro2 {
    <macro instructions (and only them, as this is regular macro)>
    do move prontera
    log This will be printed after a macro_delay has passed before the last command, because it won't check AI queue.
}
exclusive
  • exclusive is an optional parameter.
  • exclusive defines if the macro can be interrupted or not.
  • If not used in the automacro the default value will be used, which is: 0.
  • When it's value is 0 the macro can be interrupted by other automacros.
  • When it's value is 1 the macro can not be interrupted by other automacros.
automacro MyAuto {
    <automacro conditions and parameters (and only them)>
    exclusive 0
    call myMacro
}
macro myMacro {
    <macro instructions (and only them, as this is regular macro)>
    log This would probably cause a bug because the automacro would activate every AI cycle since the macro is interruptible.
}

automacro MyAuto2 {
    <automacro conditions and parameters (and only them)>
    exclusive 1
    call myMacro
}
macro myMacro2 {
    <macro instructions (and only them, as this is regular macro)>
    log Since exclusive is 1 the macro is now uninterruptible and will work properly.
}
priority
  • priority is an optional parameter.
  • priority defines the priority for activation the automacro has between all automacros, the lower the priority, the sooner ir will be checked.
  • If not used in the automacro the default value will be used, which is: 0.
  • Must have a numeric value.
automacro MyAuto {
    <automacro conditions and parameters (and only them)>
    priority 5
    call {
        log This automacro will be checked after MyAuto2 because it's priority is higher
    }
}

automacro MyAuto2 {
    <automacro conditions and parameters (and only them)>
    priority 2
    call {
        log This automacro will be checked before MyAuto because it's priority is lower
    }
}
macro_delay
  • macro_delay is an optional parameter.
  • macro_delay defines the time in seconds to wait between macro commands.
  • If not used in the automacro the default value will be used, which is: 1.
  • Must have a numeric value.
automacro MyAuto {
    <automacro conditions and parameters (and only them)>
    macro_delay 2
    call {
        log This will be printed at the macro execution
        log This will be printed 2 seconds after
    }
}

automacro MyAuto2 {
    <automacro conditions and parameters (and only them)>
    macro_delay 5
    call {
        log This will be printed at the macro execution
        log This will be printed 5 seconds after
    }
}
orphan
  • orphan is an optional parameter.
  • orphan defines how will eventMacro deal with macros when 'eventMacro' is cleaned out of AI queue.
  • The default value for this parameter can be set in the config key eventMacro_orphans Configuration files.
  • If not used in the automacro the default value will be used, which is the config key eventMacro_orphans.
  • If eventMacro_orphans is not set in the config the default value will be used, which is: terminate
  • The valid values and their descriptions are the following:
Value Description
terminate terminates the macro (equivalent to eventMacro stop)
terminate_last_call terminates only the last call of the queue, this means that if macro 'mac1' called 'mac2' which called 'mac3', who got orphaned, only 'mac3' gets terminated, and queue returns to 'mac2'.
reregister re-registers to AI queue, overriding other entries. This means to force the continuation of the macro.
reregister_safe re-registers to AI queue when AI gets idle. This means the macro will continue when all other tasks are done.
repeat
  • repeat is an optional parameter.
  • repeat defines how many times the macro defined by call will be called.
  • If not used in the automacro the default value will be used, which is: 1.
  • Must have a numeric value.
automacro <automacro name> {
    <automacro conditions and parameters (and only them)>
    repeat 3
    call myMacro
}
macro myMacro {
    <macro instructions (and only them, as this is regular macro)>
    log This message will be printed 3 times, because the macro will be called 3 times
}
timeout
  • timeout is an optional parameter.
  • timeout defines the time in seconds before this automacro can activate again after an activation.
  • If not used in the automacro the default value will be used, which is: 0.
  • Must have a numeric value.
automacro <automacro name> {
    <automacro conditions and parameters (and only them)>
    timeout 60
    call myMacro
}
macro myMacro {
    <macro instructions (and only them, as this is regular macro)>
    log This macro will only be called every 60 seconds
}

Conditions

To do...


Perl Subroutines

eventMacro plugin support Perl subroutines in macros. You can now create any simple Perl function without limitations from the eval command.

Example No.1 - how to create a Perl Subroutine function in Macro

macro sub {
 $list = Orange, Milk, Soya, Peach
 if (existsInList("$list", "PeAch")) goto ok 
 log Not Match!!!; stop
 :ok
 log Match!!!
 $x = &eval(existsInList("$list", "PeAch"))
 log \$x = $x   # $x here is 1
}
sub existsInList {
  my ($list, $val) = @_;
  return 0 if ($val eq "");
  my @array = split / *, */, $list;
  $val = lc($val);
  foreach (@array) {
     s/^\s+//;
     s/\s+$//;
     s/\s+/ /g;
     next if $_ eq "";
     return 1 if lc eq $val;
  }
  return 0;
}


Example No.2 - how to create a re-writable file function using Perl Subroutine in Macro

automacro confHP1 {
   hp > 85%
   exclusive 1
   run-once 1
   set setting Demon Pungus   #becareful on your case, its case sensitive
   set attack 1
   set teleport 0
   set telesearch 1
   call HP
}
automacro confHP2 {
   hp < 75%
   exclusive 1
   run-once 1
   set setting Demon Pungus
   set attack 1
   set teleport 2
   set telesearch 1
   call HP
}
macro HP {
     #Getting the value of the $setting monster name Ex: $setting $exist1 $exist2 $exist3
       $exist1 = @eval (defined Misc::mon_control("$setting")?Misc::mon_control("$setting")->{attack_auto}:"None")
       $exist2 = @eval (defined Misc::mon_control("$setting")?Misc::mon_control("$setting")->{teleport_auto}:"None")
       $exist3 = @eval (defined Misc::mon_control("$setting")?Misc::mon_control("$setting")->{teleport_search}:"None")
       log Old Values are $setting $exist1 $exist2 $exist3
       log Changing the values to $setting $attack $teleport $telesearch
       do eval Misc::mon_control("$::Macro::Data::varStack{setting}")->{attack_auto} = $attack; Misc::mon_control("$::Macro::Data::varStack{setting}")->{teleport_auto} = $teleport; Misc::mon_control("$::Macro::Data::varStack{setting}")->{teleport_search} = $telesearch
       log Writting mon_control.txt with new values
       rewrite()  # see the sub-routine function below
       log Reloading mon_control.txt
       do reload mon_control
       $exist1 = @eval (defined Misc::mon_control("$setting")?Misc::mon_control("$setting")->{attack_auto}:"None")
       $exist2 = @eval (defined Misc::mon_control("$setting")?Misc::mon_control("$setting")->{teleport_auto}:"None")
       $exist3 = @eval (defined Misc::mon_control("$setting")?Misc::mon_control("$setting")->{teleport_search}:"None")
       log New mon_control.txt Setting: $setting $exist1 $exist2 $exist3
       log Macro done
    #if $teleport = 0 ; means the Higher automacro HP is currently triggered
    #if $teleport = 2 ; means the Lower automacro HP is currently triggered
       if ($teleport < 2) goto releaseHighHp
       :releaseLowHp
            release confHP1
            stop
       :releaseHighHp
            release confHP2
            stop
}
sub rewrite {
  my $monster = Misc::mon_control("$::Macro::Data::varStack{setting}");
  my @lines = ();
  if (open(FILE, "<:utf8", Settings::getControlFilename("mon_control.txt"))) {
     while (<FILE>) {
        s/\x{FEFF}//g; chomp;
        if (/^#/ || /^\n/ || /^\r/) {
           push @lines,$_;
           next
        }
        /^(\d+|([a-zA-Z' ]+)*) -?\d/;
        if ("$::Macro::Data::varStack{setting}" eq $1 && defined $monster) {
           $_ = $1; s/\s+$//;
           push @lines,$_ . " $monster->{attack_auto} $monster->{teleport_auto} $monster->{teleport_search} $monster->{attack_lvl} $monster->{attack_jlvl} $monster->{attack_hp} $monster->{attack_sp} $monster->{weight}"
        }
        else {push @lines,$_}
     }
     close FILE
  }
  open(FILE, ">:utf8", Settings::getControlFilename("mon_control.txt"));
  print FILE join "\n", @lines;
  close FILE;
}

Comments

The macro files allow comments, i.e. lines that are ignored by the eventMacro plugin.

  • Lines starting with a # will be treated as a comment.
  • Everything after a space and followed by a # will also be ignored.
macro happy {
     # this is a comment line
    log I'm Happy # this is also a comment
}

Will print:

[eventmacro log] I'm Happy