Author Topic: Brackets and boolean multiplying. Tutorial.  (Read 4131 times)

0 Members and 1 Guest are viewing this topic.

Offline LaradelTopic starter

  • Newbie
  • *
  • Posts: 3
  • Activity:
    0%
  • Reputation Power: 1
  • Laradel has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Brackets and boolean multiplying. Tutorial.
« on: March 24, 2012, 02:05:38 PM »
0
Hello.

I trully hope that the material of this article can be usefull even for those who have a long easyuo programming practice, because most of this 'features' are undocumented and were found by chance :)

General statement:
The scripts IMHO can be put into two groups:
 a) educational (classic) and which will be supported\watched\inspected by someone else and
 b) 'wild' ones - where perfomance is the main target.

So i'll try to show you that 'wild' way :) wich in my case doesn't brake the main programming rules but uses some rarely used possibilities, some 'wild' tricks and compressed style - 'code in code'.


Part 1. Brackets and boolean multiplying.

Every bracket line in easyuo is considered to be a separate line and processed as a separate line.
So the #lpc cicle is reduced by the amount of bracket lines. If #lpc is 10 and we have 10 lines in a script, where 2 are brackets, the total script performance will go down 20% (theoretically), because only 8 lines are actually 'usefull' in a script and the delay after processing 10 lines (#lpc 10) will happen anyway.

Test example:
Code: [Select]
set #lpc 10  ;default
;set #lpc 200
set %iter 100

set %t1 #SYSTIME
for %i 1 %iter
{
 if %i < 50
 {
 set %return1 bla-bla
 }
 else
 {
 set %return1 bla-bla2
 }
}
set %t1 #SYSTIME - %t1

set %t2 #SYSTIME
for %i 1 %iter
{
 if %i < 500
  set %return2 bla-bla
 else
  set %return2 bla-bla2
}
set %t2 #SYSTIME - %t2

halt

For my PC the results are following:
#lpc 10 :  %t1=3547 - %t2=2594ms
#lpc 200:  %t1= 215 - %t2= 157ms
That speed test is not 100% accurate, because we have "for %i 1 %iter {}" in both cases which consumes the same amount ot lines.

So the brackets may not be used in following structures if you want to encrease the perfomance of your script:


for %i 1 100
 One line only, for more lines you will need brackets


while bCondition
 One line only, for more lines you will need brackets


repeat       ; brackets may not be used
 code block1
 code block2
 code block3  
 code block4
until


if bCondition
 one line of code


if bCondition
 one line of code
else
 one line of code


if bCondition 3      ; 3- you set how many lines to pass if bCondition=#false
 line 1
 line 2
 line 3
else 2               ; same here: you set how many lines should the script pass after
 line 1              ; execution of the 'true block' (bCondition=#true)
 line 2


if bCondition N      ; N lines of code
 line 1
 line 2
  ...
 line N


So you can make such construction without brackets:
Code: [Select]
if a>b 5    ; <- 5 here is the next 5 lines
 if a>c     ; 1
    line 1  ; 2
 else 2     ; 3
    line 1  ; 4
    line 2  ; 5
else 7      ; <- 7 here is the next 7 lines
 if a>c 2   ; 1
    line 1  ; 2
    line 2  ; 3
 else 3     ; 4       <-else 3  - 3 here is the next 3 lines
    line 1  ; 5
    line 2  ; 6
    line 3  ; 7

In this example we have cutted 12 brackets and got fast, but hardly readable and updateble code.
The main problem is when you\someone start editing watch the line shift indexes after if\else.
I personally do not recommend to 'pack' code cutting brackets before it's completely done.


Can the line shift index after IF be negative? yes

have a look on example:

Example 2:
Code: [Select]
set %timeout #SYSTIME + 5000   ; set timout current time + 5 secs
if %timeout < #SYSTIME -1         ; wait until timout

Warning! "#SYSTIME -1" in the last line not "#SYSTIME - 1",  "-1" - is a line shift index.
I other words it checks the condition if %timout already less than current time? if yes, then it will go to th next line after 'if' statement if

not, then it will take the current shift index and go according it.

Example 3:
Code: [Select]
gosub Send_party_chat Help_i'm_dead: , #CHARPOSX , _ , #CHARPOSY , _ , #CURSKIND  ;<----+
 gosub Check_Is_Someone_Going_to_resurect_me                                                       ;       |
 wait 20                                                                                                               ;       |
if #CHARGHOST = no -4                                                                                         ;>----+
 gosub Send_party_chat I'm_Alive!Ehha!
This code rotate until you'll be resurected :)

Example 4: (you'll find it in my Subs):
Code: [Select]
if #false ( %0 * -2 * ( %0 < 9 && _ , %1 , _ in _SubName1_SubName2_SubNameN_ ) )
 set #result Error_No_such_sub
 exit
 gosub %1
 exit
 gosub %1 %2
 exit
 gosub %1 %2 %3
 exit
 gosub %1 %2 %3 %4
 exit
 gosub %1 %2 %3 %4 %5
 exit
 gosub %1 %2 %3 %4 %5 %6
 exit
 gosub %1 %2 %3 %4 %5 %6 %7
 exit
 gosub %1 %2 %3 %4 %5 %6 %7 %8
 exit

Looks wild? :) but it works perfect and very fast. The purpose is to redirect incoming request to the appropriate sub in the subs library (all small subs are in a one file)
call Library.euox %SubName %parametr1 %parametr2 ... %parametrN
Any Sub can receive up to 7 parameters in this example.
Here comes another 'wild' aspect that works in easyuo - multiplying boleans and integers :)

#true is -1 and #false is 0.

Lets have a look on the first line in details.

if #false ( %0 * -2 * ( %0 < 9 && _ , %1 , _ in _SubName1_SubName2_SubNameN_ ) )
      a)          e)    d)  +- b) --+ +--------- c) ---------------------------+          

a) always #false - means to force script use line shift index every time
b) and c) error check bracket:
  b) if #true - 1-7 parameters passed      c) do we have such Sub in our file? - if b) or c) fails we will get #false (what means 0)
otherwise we'll get #true (what means -1).

d) we multiply -2 (number of lines in each sub call) on #true(-1) or #false(0) and get +2 or 0(if error check fails)
e) we calculate the line shift: number of parameters passed * 2 or 0
if d) gives us 0, the next line in statement  if #false 0   will be  set #result Error_No_such_sub, like there is no 'true' block at all
if d) gives us 2, we have an offset to the gosub with a proper number of parameters to be passed to the final Sub.

More multiplying:

Code: [Select]
set %a #true
set %d 0

if %a           ; %a can be #true or #false - some condition
  set %d %d - 1
esle
  set %d %d + 1


same, but in one line:
Code: [Select]
set %d 1 + %d + %a * 2


Can line shift indexes after ELSE be negative? yes
Example 5:
Code: [Select]
set %a 4
set %b 1
if %a > %b
 set %b %b + 1
else -3

This script will go further only when %b = 4.


Probably that's all for today post. Next part may be about strings in every day life :)
Please let me know if you like this post or not.
May be i'm just trying to tell you something evident and well known?  :'(

Thanks for reading. And sorry for my EnGliSh :)
« Last Edit: March 24, 2012, 04:06:48 PM by Laradel »

Offline ximan

  • Jr. Member
  • **
  • Posts: 98
  • Activity:
    0%
  • Reputation Power: 1
  • ximan has no influence.
  • Respect: +16
  • Referrals: 1
    • View Profile
Re: Brackets and boolean multiplying. Tutorial.
« Reply #1 on: March 25, 2012, 06:45:39 AM »
0
Kal In Ex uses the 'if (expr) jmp' technique extensively in his OCR routines.  Pretty sure he relayed that the code was actually machine generated, so he wasn't figuring the if offsets by hand (which would have been a nightmare).  In my testing for emulib that I posted a couple of days ago on the oeuo easyuo forum, I found that positive and negative and variable jmp parameters are allowed, e.g.  'if %a < %b %c', where %c can evaluate to a positive or negative integer.  More fun can be had with dynamic and conditional %until statements, as well, but they break things unless they are the outermost loop.
« Last Edit: March 25, 2012, 07:00:06 AM by ximan »

Offline LaradelTopic starter

  • Newbie
  • *
  • Posts: 3
  • Activity:
    0%
  • Reputation Power: 1
  • Laradel has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: Brackets and boolean multiplying. Tutorial.
« Reply #2 on: March 25, 2012, 11:32:50 AM »
0
Kal In Ex uses the 'if (expr) jmp' technique extensively in his OCR routines.  Pretty sure he relayed that the code was actually machine generated, so he wasn't figuring the if offsets by hand (which would have been a nightmare).

I'm quite agree with, you mate, that using this 'if (expr) jmp' for OCR is a nightmare making a script unreadable and huge without giving it any perfomance increase. I feel like many people were wondering what does that 'jump' mean? and how does it exactly work, so i did this tutorial.  ::) Sure i haven't seen your article before posting and would post it anyway 'cause it's directed to teach 'how to do' and explain 'where and why it can be used'.

 In my testing for emulib that I posted a couple of days ago on the oeuo easyuo forum, I found that positive and negative and variable jmp parameters are allowed, e.g.  'if %a < %b %c', where %c can evaluate to a positive or negative integer.  More fun can be had with dynamic and conditional %until statements, as well, but they break things unless they are the outermost loop.

hehe :) i've found it and using at least 5 years but i never thought to compile it all in a one atricle.

As for 'until' and 'cancatinate' (Cheffe had one posting) - that was supposed to be one of the next articles here.
You can add some material here too if you wish - i would love it! Take some topic: arry manipulations,string parsing,concatinate etc. and post.
Be our guest!

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: Brackets and boolean multiplying. Tutorial.
« Reply #3 on: March 25, 2012, 12:59:48 PM »
0
Interesting read!  Thanks for sharing!
Please read the ScriptUO site RULES
Come play RIFT with me!

Tags: