Bots and Simulations > DNA - General

If - Elseif - Else

(1/3) > >>

Shasta:
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: ---*.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
--- End code ---

Edit: I just realized you would need to store to a temp location for my way to work, edited the code above

Numsgil:
Hmm...  You could do this (in pseudocode, because this gets complicated):


--- Code: ---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
--- End code ---

Shasta:

--- Quote from: Numsgil ---Hmm...  You could do this (in pseudocode, because this gets complicated):


--- Code: ---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
--- End code ---

--- End quote ---
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: ---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
--- End code ---
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

Numsgil:
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.

Trafalgar:
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: ---    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
--- End code ---

DarwinBots code:

--- Code: ---*.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
--- End code ---

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 )

Navigation

[0] Message Index

[#] Next page

Go to full version