Author Topic: If - Elseif - Else  (Read 5747 times)

Offline Shasta

  • Administrator
  • Bot Destroyer
  • *****
  • Posts: 231
    • View Profile
If - Elseif - Else
« on: August 21, 2008, 03:50:30 PM »
While I'm waiting for a person to get back to me on a bug in a module for the bestiary I decided to write a bot I've been thinking about for a while.

So, the question is, is it possible to create an if - elseif (0-# elseif statements) - else block just using the boolean stack and evaluating each conditional only once? My current thought is no, it is not. The best way I can see would be to place a number on the top of the int stack at the end of a section, telling the next one if it ran. (shown below for clarity)
Code: [Select]
*.refeye *.myeye !=
    *.refeye 0 = and
        'deal with veggie
        dropbool
        1 .temp store
    *.temp 1 != *.refnrg *.nrg 2 mult > and and
        'deal with scary bot
        dropbool dropbool
        1 .temp store
    *.temp 1 != and
        'feed from boot

Edit: I just realized you would need to store to a temp location for my way to work, edited the code above
« Last Edit: August 21, 2008, 04:36:15 PM by Shasta »

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
If - Elseif - Else
« Reply #1 on: August 21, 2008, 06:27:00 PM »
Hmm...  You could do this (in pseudocode, because this gets complicated):

Code: [Select]
condition1
  actions (must not pop or push any bools to the bool stack)
not dupbool condition2 and 'Else if condition 2
  actions (must not pop or push any boos to the bool stack)
dropbool 'else
  actions
« Last Edit: August 21, 2008, 06:27:32 PM by Numsgil »

Offline Shasta

  • Administrator
  • Bot Destroyer
  • *****
  • Posts: 231
    • View Profile
If - Elseif - Else
« Reply #2 on: August 21, 2008, 08:49:23 PM »
Quote from: Numsgil
Hmm...  You could do this (in pseudocode, because this gets complicated):

Code: [Select]
condition1
  actions (must not pop or push any bools to the bool stack)
not dupbool condition2 and 'Else if condition 2
  actions (must not pop or push any boos to the bool stack)
dropbool 'else
  actions
Yes, yes it does get complicated...... here is a section (everything but the logic sucks) of the bot I was working on in case it helps anyone:
Code: [Select]
def tieHardening 51   'If != 0, im becoming a multibot
def myType 52         'what type am I? wall/alone = 70 veggie = 60
def numVeggies 53     'How many veggies am I tied to? (when alone)
def temp 54           'Temp var used for if-elseif-else blocks

'Under ideal conditions, this tie information is true
def sameTypeTieOne 61 'wall-link 1 if wall   -- veggie-link 1 if veggie
def sameTypeTieTwo 62 'wall-link 2 if wall   -- veggie-link 2 if veggie
def opTypeTieOne 71   'veggie-link 1 if wall -- wall-link 1 if veggie
def opTypeTieTwo 72   'veggie-link 2 if wall -- wall-link 2 if veggie
def opTypeTieThree 73 'nothing if wall       -- wall-link 3 if veggie

start
    *.multi 0 =
    *.tieHardening 0 = and 'if im not a multibot, or becoming one
        *.eye5 0 > and
            *.refeye 0 = overbool overbool and 'if im seeing a veggie
                *.refvel .up store
                *.myType *.numVeggies 1 add add .tie store
                *.numVeggies 1 add .numVeggies store
                1 .tiehardening store
            not overbool overbool *.refnrg *.nrg 2 mult > and swapbool overbool and 'should I run?  ----> make this better
                628 rnd 314 add .aimdx store
                *.maxvel .up store
            dropbool swapbool and 'if neither of the above, eat  ----> I should make this a ton better
                *.refvelup .up store
                                *.refveldx .dx store
                -1 .shoot store
stop
Nested if and elseif are even worse  

I think what was throwing me off before was that I didnt realized that some of the boolean opperators copy while others overwrite (not) or merge (or, xor, and).

New question, is making if, elseif, and else with only booleans worth it?  

Edit: Some of my commands were.... more than idiotic, fixed them to make the bot a bit more interesting
« Last Edit: August 21, 2008, 08:55:45 PM by Shasta »

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
If - Elseif - Else
« Reply #3 on: August 22, 2008, 01:25:44 PM »
You can always use actual cond-start-else statements.  Most people use cond start stop but there's a command that works oppositely from start called else.

Offline Trafalgar

  • Bot Destroyer
  • ***
  • Posts: 122
    • View Profile
If - Elseif - Else
« Reply #4 on: August 30, 2008, 09:19:20 PM »
It is definitely possible, and has in fact been done before.  

Example from Nanite Detonators 2.3:

(The code may LOOK complicated, but each line in the first block of code corresponds to one line in the second block of code, and the lines which set shoot and shootval should set things apart nicely too.)

Note: elif is the same concept as elseif

The readable code prior to being compiled by PyBot:
Code: [Select]
    if (nrg>500):
        if ((eyef>0) and ((in1!=ID1) or ((memval!=genes) and (memloc==&genes))) and (robage>2) and (refage>2) and (shootval==0)):
            aimshoot = aim - angle(refxpos, refypos)
            if ((timer andbits 3)==0):
                shoot = -1
                shootval = 10
            elif ((timer andbits 3)==1):
                shoot = -6
                shootval = -10
            elif ((timer andbits 3)==2):
                shoot = &mkslime
                shootval = -31999
            else:
                shoot = &shootval
                shootval = 31999

DarwinBots code:
Code: [Select]
*.nrg 500 >
dupbool dupbool *.eyef 0 > *.in1 9136 != *.memval *.genes != *.memloc .genes = and or and *.robage 2 > and *.refage 2 > and *.shootval 0 = and and
*.aim *.refxpos *.refypos angle sub .aimshoot store
dupbool dupbool *.timer 3 & 0 = and
-1 .shoot store
10 .shootval store
not and not dupbool not dupbool *.timer 3 & 1 = and
-6 .shoot store
-10 .shootval store
not and dupbool *.timer 3 & 2 = and
.mkslime .shoot store
-31999 .shootval store
not and not or not
.shootval .shoot store
31999 .shootval store
dropbool
dropbool dropbool
dropbool

The last dropbool line is kind of like an "endif" sitting there by itself. The other places where blocks ended, the dropbools and such were placed at the beginning of the next line. (So did this one, but someone might have said "Where'd '*.numties 0 =' come from?" if I had pasted the entire line )

Offline Prsn828

  • Bot Destroyer
  • ***
  • Posts: 139
    • View Profile
If - Elseif - Else
« Reply #5 on: March 27, 2009, 10:51:18 AM »
This isn't worth putting in a new topic, but how exactly do you use else, because from what I understand the boolean stack is anded when you reach the start after a cond, so does this mean you can only have one else per gene?
So, what will it be? Will you submit to my will, or must I bend reality to suit my needs?
Better answer before I do BOTH!

Offline Moonfisher

  • Bot Overlord
  • ****
  • Posts: 592
    • View Profile
If - Elseif - Else
« Reply #6 on: March 27, 2009, 03:55:49 PM »
I'm not sure else even works... was suposed to be :
cond
start
else
stop

I think... but not so sure it actualy works properly...

But it's kinda funny people are discussing this, I'm currently working on a perl script to translate a C like syntax to DB code
It was only suposed to be a very simple and strict syntax at first, but the whole thing kind of grew as I got the idea to let people declare macros and operators themselves
So still need to get that sorted out, but I finaly know exactly how it's gonna work... sort of...
But then I also need to make it possible to use functions... and haven't gotten around to think that through yet so... not sure how much longer I'm gonna be.
And ofcourse it won't be C... it'll be my own very strict and simple version of C... but I think it's going to turn out pretty well, personaly plan to use for bot creation in the future.
(Also planning for this first verion to SG'fy everything and add a hibernate condition if the var is defined, so making a hibernating bot will be easyer than ever)

Anyway still got some work to do... but the whole syntax and how to use it has taken shape (Except for functions) and personaly I like the outcome, lets hope I'm not the only one
(I know one can use PyBot... but I like the C syntax and wanted to do something with perl... and planning on adding headers with all the operators and some good macros and such, then poeple can just overload the operators if they need things to work differently)
« Last Edit: March 27, 2009, 04:20:12 PM by Moonfisher »

Offline Prsn828

  • Bot Destroyer
  • ***
  • Posts: 139
    • View Profile
If - Elseif - Else
« Reply #7 on: March 27, 2009, 05:00:30 PM »
Quote from: Moonfisher
I'm not sure else even works... was suposed to be :
cond
start
else
stop

I think... but not so sure it actualy works properly...

But it's kinda funny people are discussing this, I'm currently working on a perl script to translate a C like syntax to DB code
It was only suposed to be a very simple and strict syntax at first, but the whole thing kind of grew as I got the idea to let people declare macros and operators themselves
So still need to get that sorted out, but I finaly know exactly how it's gonna work... sort of...
But then I also need to make it possible to use functions... and haven't gotten around to think that through yet so... not sure how much longer I'm gonna be.
And ofcourse it won't be C... it'll be my own very strict and simple version of C... but I think it's going to turn out pretty well, personaly plan to use for bot creation in the future.
(Also planning for this first verion to SG'fy everything and add a hibernate condition if the var is defined, so making a hibernating bot will be easyer than ever)

Anyway still got some work to do... but the whole syntax and how to use it has taken shape (Except for functions) and personaly I like the outcome, lets hope I'm not the only one
(I know one can use PyBot... but I like the C syntax and wanted to do something with perl... and planning on adding headers with all the operators and some good macros and such, then poeple can just overload the operators if they need things to work differently)

Actually, I could probably write something myself.
Too bad I'm too lazy, lol, but keep it up, I'll be expecting to see the results when you get done.

Oh, don't hold your breath on functions, any recursion would instantly disallow it from working with functions.
Without recursion it is just sticking the same code anywhere the function is called.
So, what will it be? Will you submit to my will, or must I bend reality to suit my needs?
Better answer before I do BOTH!

Offline Shasta

  • Administrator
  • Bot Destroyer
  • *****
  • Posts: 231
    • View Profile
If - Elseif - Else
« Reply #8 on: March 27, 2009, 06:05:00 PM »
Quote from: Moonfisher
cond
start
else
stop
Else on the wiki: Identical to start except it is executed if the previous condition resolved FALSE.

Else really is not very flexible, to me just using the boolean stack is easier (now) and more powerful.


Offline Prsn828

  • Bot Destroyer
  • ***
  • Posts: 139
    • View Profile
If - Elseif - Else
« Reply #9 on: March 27, 2009, 06:42:25 PM »
Quote from: Shasta
Else really is not very flexible, to me just using the boolean stack is easier (now) and more powerful.

How exactly do you "Just use the boolean stack" ?
I have yet to see anyone explain this.
So, what will it be? Will you submit to my will, or must I bend reality to suit my needs?
Better answer before I do BOTH!

Offline Shasta

  • Administrator
  • Bot Destroyer
  • *****
  • Posts: 231
    • View Profile
If - Elseif - Else
« Reply #10 on: March 27, 2009, 08:27:26 PM »
Quote from: Prsn828
How exactly do you "Just use the boolean stack" ?
I have yet to see anyone explain this.
By just using the stack I mean not using a Cond, or Else statement, just placing true and false on the stack through conditions and manipulating it to get the desired code to run. It allows for nested if-elseif-else statements much easier, without using the crazy condition-less code.

I have given an example of it in this very tread here. I also *think* I explained it fairly well here. Also, I may actually get around to putting up a tutorial on the wiki this weekend. Maybe.

Offline Shasta

  • Administrator
  • Bot Destroyer
  • *****
  • Posts: 231
    • View Profile
If - Elseif - Else
« Reply #11 on: March 29, 2009, 03:30:30 PM »
Quote from: Shasta
Also, I may actually get around to putting up a tutorial on the wiki this weekend. Maybe.
I did get around to this: Inline Conditions Tutorial. If someone could double check it that would be great, also is there anything confusing or left out?

Offline Prsn828

  • Bot Destroyer
  • ***
  • Posts: 139
    • View Profile
If - Elseif - Else
« Reply #12 on: March 30, 2009, 06:20:34 PM »
Quote from: Shasta
Quote from: Shasta
Also, I may actually get around to putting up a tutorial on the wiki this weekend. Maybe.
I did get around to this: Inline Conditions Tutorial. If someone could double check it that would be great, also is there anything confusing or left out?

Excellent explanation!

Before this I had no idea that there was a use for the bool stack, lol.
This tutorial is definitely a keeper.
So, what will it be? Will you submit to my will, or must I bend reality to suit my needs?
Better answer before I do BOTH!

Offline Moonfisher

  • Bot Overlord
  • ****
  • Posts: 592
    • View Profile
If - Elseif - Else
« Reply #13 on: April 01, 2009, 05:53:58 PM »
Didn't realy read it thoroughly, but had a quick look and couldn't see any errors...
Also for using else if statements I tryed to write a good example in this topic :

http://www.darwinbots.com/Forum/index.php?...p;#entry1379944

Not well explained... I think it should work though, and it covers any "obstacles" I could think of...
May not be the best solution or anything, but might show if there was something you could have missed.
« Last Edit: April 01, 2009, 05:57:10 PM by Moonfisher »